CliPhp.php
5 years ago
Output.php
5 years ago
Process.php
5 years ago
RequestCommand.php
5 years ago
RequestParser.php
5 years ago
RequestParser.php
113 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik\CliMulti; |
| 11 | |
| 12 | use Piwik\SettingsPiwik; |
| 13 | |
| 14 | class RequestParser |
| 15 | { |
| 16 | private $supportsAsync; |
| 17 | |
| 18 | public function __construct($supportsAsync) |
| 19 | { |
| 20 | $this->supportsAsync = $supportsAsync; |
| 21 | } |
| 22 | |
| 23 | public function getInProgressCommands() |
| 24 | { |
| 25 | $psOutput = $this->getPsOutput(); |
| 26 | |
| 27 | $climultiRequestCommands = $this->getPsLinesWithCliMulti($psOutput); |
| 28 | $climultiRequestCommands = $this->parseQueries($climultiRequestCommands); |
| 29 | |
| 30 | return $climultiRequestCommands; |
| 31 | } |
| 32 | |
| 33 | public function getInProgressArchivingCommands() |
| 34 | { |
| 35 | $commands = $this->getInProgressCommands(); |
| 36 | $commands = $this->filterNonArchivingJobs($commands); |
| 37 | return $commands; |
| 38 | } |
| 39 | |
| 40 | private function getPsOutput() // protected for tests |
| 41 | { |
| 42 | if (!$this->supportsAsync) { |
| 43 | // we cannot detect if web archive is still running |
| 44 | return ''; |
| 45 | } |
| 46 | |
| 47 | return $this->invokePs(); |
| 48 | } |
| 49 | |
| 50 | private function filterNonArchivingJobs($commands) |
| 51 | { |
| 52 | $result = array_filter($commands, function ($command) { |
| 53 | if (empty($command['trigger']) |
| 54 | || $command['trigger'] != 'archivephp' |
| 55 | ) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | return true; |
| 60 | }); |
| 61 | $result = array_values($result); |
| 62 | return $result; |
| 63 | } |
| 64 | |
| 65 | private function getPsLinesWithCliMulti(string $psOutput) |
| 66 | { |
| 67 | $instanceId = SettingsPiwik::getPiwikInstanceId(); |
| 68 | $lines = explode("\n", $psOutput); |
| 69 | $lines = array_map('trim', $lines); |
| 70 | $lines = array_filter($lines, function ($line) use ($instanceId) { |
| 71 | if (!empty($instanceId) && strpos($line, 'matomo-domain=' . $instanceId) === false) { |
| 72 | return false; |
| 73 | } |
| 74 | return strpos($line, 'climulti:request') !== false |
| 75 | && ( |
| 76 | strpos($line, 'console') !== false || strpos($line, 'php') !== false |
| 77 | ); |
| 78 | }); |
| 79 | return $lines; |
| 80 | } |
| 81 | |
| 82 | private function parseQueries(array $climultiRequestCommands) |
| 83 | { |
| 84 | $commandName = 'climulti:request'; |
| 85 | |
| 86 | $result = []; |
| 87 | foreach ($climultiRequestCommands as $command) { |
| 88 | $pos = strpos($command, $commandName); |
| 89 | |
| 90 | $commandParts = substr($command, $pos + strlen($commandName)); |
| 91 | $commandParts = explode(" ", $commandParts); |
| 92 | $commandParts = array_filter($commandParts, function ($p) { |
| 93 | return strlen($p) && substr($p, 0, 1) != '-'; |
| 94 | }); |
| 95 | |
| 96 | $query = reset($commandParts); |
| 97 | parse_str($query, $parsed); |
| 98 | |
| 99 | $result[] = $parsed; |
| 100 | } |
| 101 | return $result; |
| 102 | } |
| 103 | |
| 104 | protected function invokePs() |
| 105 | { |
| 106 | if (defined('PIWIK_TEST_MODE')) { |
| 107 | return ''; // skip check in tests as it might result in random failures |
| 108 | } |
| 109 | |
| 110 | return `ps aux`; |
| 111 | } |
| 112 | } |
| 113 |