Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RequestParser.php « CliMulti « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 217b0bbbb4df81c100193392785d2788ae32922f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\CliMulti;

class RequestParser
{
    private $supportsAsync;

    public function __construct($supportsAsync)
    {
        $this->supportsAsync = $supportsAsync;
    }

    public function getInProgressCommands()
    {
        $psOutput = $this->getPsOutput();

        $climultiRequestCommands = $this->getPsLinesWithCliMulti($psOutput);
        $climultiRequestCommands = $this->parseQueries($climultiRequestCommands);

        return $climultiRequestCommands;
    }

    public function getInProgressArchivingCommands()
    {
        $commands = $this->getInProgressCommands();
        $commands = $this->filterNonArchivingJobs($commands);
        return $commands;
    }

    private function getPsOutput() // protected for tests
    {
        if (!$this->supportsAsync) {
            // we cannot detect if web archive is still running
            return '';
        }

        return $this->invokePs();
    }

    private function filterNonArchivingJobs($commands)
    {
        $result = array_filter($commands, function ($command) {
            if (empty($command['trigger'])
                || $command['trigger'] != 'archivephp'
            ) {
                return false;
            }

            return true;
        });
        $result = array_values($result);
        return $result;
    }

    private function getPsLinesWithCliMulti(string $psOutput)
    {
        $lines = explode("\n", $psOutput);
        $lines = array_map('trim', $lines);
        $lines = array_filter($lines, function ($line) {
            return strpos($line, 'climulti:request') !== false
                && (
                    strpos($line, 'console') !== false || strpos($line, 'php') !== false
                );
        });
        return $lines;
    }

    private function parseQueries(array $climultiRequestCommands)
    {
        $commandName = 'climulti:request';

        $result = [];
        foreach ($climultiRequestCommands as $command) {
            $pos = strpos($command, $commandName);

            $commandParts = substr($command, $pos + strlen($commandName));
            $commandParts = explode(" ", $commandParts);
            $commandParts = array_filter($commandParts, function ($p) {
                return strlen($p) && substr($p, 0, 1) != '-';
            });

            $query = reset($commandParts);
            parse_str($query, $parsed);

            $result[] = $parsed;
        }
        return $result;
    }

    protected function invokePs()
    {
        if (defined('PIWIK_TEST_MODE')) {
            return ''; // skip check in tests as it might result in random failures
        }

        return `ps aux`;
    }
}