PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.0
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
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