PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.0.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.0.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / CliMulti / RequestParser.php
matomo / app / core / CliMulti Last commit date
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
106 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 class RequestParser
13 {
14 private $supportsAsync;
15
16 public function __construct($supportsAsync)
17 {
18 $this->supportsAsync = $supportsAsync;
19 }
20
21 public function getInProgressCommands()
22 {
23 $psOutput = $this->getPsOutput();
24
25 $climultiRequestCommands = $this->getPsLinesWithCliMulti($psOutput);
26 $climultiRequestCommands = $this->parseQueries($climultiRequestCommands);
27
28 return $climultiRequestCommands;
29 }
30
31 public function getInProgressArchivingCommands()
32 {
33 $commands = $this->getInProgressCommands();
34 $commands = $this->filterNonArchivingJobs($commands);
35 return $commands;
36 }
37
38 private function getPsOutput() // protected for tests
39 {
40 if (!$this->supportsAsync) {
41 // we cannot detect if web archive is still running
42 return '';
43 }
44
45 return $this->invokePs();
46 }
47
48 private function filterNonArchivingJobs($commands)
49 {
50 $result = array_filter($commands, function ($command) {
51 if (empty($command['trigger'])
52 || $command['trigger'] != 'archivephp'
53 ) {
54 return false;
55 }
56
57 return true;
58 });
59 $result = array_values($result);
60 return $result;
61 }
62
63 private function getPsLinesWithCliMulti(string $psOutput)
64 {
65 $lines = explode("\n", $psOutput);
66 $lines = array_map('trim', $lines);
67 $lines = array_filter($lines, function ($line) {
68 return strpos($line, 'climulti:request') !== false
69 && (
70 strpos($line, 'console') !== false || strpos($line, 'php') !== false
71 );
72 });
73 return $lines;
74 }
75
76 private function parseQueries(array $climultiRequestCommands)
77 {
78 $commandName = 'climulti:request';
79
80 $result = [];
81 foreach ($climultiRequestCommands as $command) {
82 $pos = strpos($command, $commandName);
83
84 $commandParts = substr($command, $pos + strlen($commandName));
85 $commandParts = explode(" ", $commandParts);
86 $commandParts = array_filter($commandParts, function ($p) {
87 return strlen($p) && substr($p, 0, 1) != '-';
88 });
89
90 $query = reset($commandParts);
91 parse_str($query, $parsed);
92
93 $result[] = $parsed;
94 }
95 return $result;
96 }
97
98 protected function invokePs()
99 {
100 if (defined('PIWIK_TEST_MODE')) {
101 return ''; // skip check in tests as it might result in random failures
102 }
103
104 return `ps aux`;
105 }
106 }