Passed
Push — master ( 983f4d...aa2bf5 )
by Dispositif
04:52
created

Monitor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 : Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Application\Examples;
11
12
use App\Application\Memory;
13
use App\Application\WikiPageAction;
14
use App\Domain\Utils\WikiTextUtil;
15
use App\Infrastructure\DbAdapter;
16
use App\Infrastructure\ServiceFactory;
17
use Normalizer;
18
19
20
include __DIR__.'/../myBootstrap.php';
21
22
$process = new Monitor();
23
$process->run();
24
25
/**
26
 * TODO refac
27
 * Class EditProcess
28
 *
29
 * @package App\Application\Examples
30
 */
31
class Monitor
32
{
33
    const SLEEP_TIME = 5;
34
35
    private $db;
36
    private $lastTitle = '';
37
    /**
38
     * @var \Mediawiki\Api\MediawikiFactory
39
     */
40
    private $wiki;
41
42
    public function __construct()
43
    {
44
        $this->db = new DbAdapter();
45
        $this->wiki = ServiceFactory::wikiApi();
46
    }
47
48
    public function run()
49
    {
50
        $memory = new Memory();
0 ignored issues
show
Unused Code introduced by
The assignment to $memory is dead and can be removed.
Loading history...
51
        while (true) {
52
            echo "\n-----MONITOR------------------------\n\n";
53
            //$memory->echoMemory(true);
54
55
            $this->pageProcess();
56
            sleep(self::SLEEP_TIME);
57
        }
58
    }
59
60
    private function pageProcess()
61
    {
62
        $data = $this->db->getMonitor();
63
        if (empty($data)) {
64
            echo "new data vide\n";
65
            exit();
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
66
        }
67
68
        $title = $data[0]['page'];
69
        if ($title === $this->lastTitle) {
70
            echo "end\n";
71
            exit;
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
72
        }
73
        echo "$title \n";
74
75
        $pageAction = new WikiPageAction($this->wiki, $title);
76
        $text = $pageAction->getText();
77
        if (!$text || empty($text)) {
78
            echo "Pas de wiki texte\n";
79
            $stat = 0;
80
            goto updateMonitor;
81
        }
82
83
        $stat = '0';
84
        $suffix = '';
85
        if (!in_array($pageAction->getLastEditor(), ['CodexBot', 'ZiziBot', getenv('BOT_NAME'), getenv('BOT_OWNER')])) {
86
            $stat = $this->checkAltered($data, $text);
87
            $suffix = '[found]';
88
        }
89
90
        echo $data[0]['edited']." : ".$stat." % ".$suffix."\n";
91
92
        updateMonitor:
93
        $this->db->updateMonitor(
94
            [
95
                'page' => $title ?? '',
96
                'verify' => date("Y-m-d H:i:s"),
97
                'altered' => (int)$stat,
98
            ]
99
        );
100
    }
101
102
    /**
103
     * TODO : if find raw -> reverted
104
     *
105
     * @param array  $data
106
     * @param string $text
107
     *
108
     * @return int
109
     */
110
    private function checkAltered(array $data, string $text): int
111
    {
112
        if (count($data) === 0) {
113
            return 99;
114
        }
115
        $found = 0;
116
        $count = 0;
117
        $text = mb_strtolower($text);
118
        foreach ($data as $dat) {
119
            if (1 === intval($dat['skip']) || empty($dat['edited'])) {
120
                continue;
121
            }
122
            $count++;
123
            if (empty($dat['opti'])) {
124
                echo 'opti vide';
125
                continue;
126
            }
127
128
            // compte pas différence Unicode entre BD et wiki
129
            $opti = Normalizer::normalize($dat['opti']); // hack
130
            // compte pas les changements de typo majuscule/minuscule
131
            $optiLower = mb_strtolower($opti);
132
            // compte pas la correction sur ouvrage avec commentaire HTML
133
            $optiComment = WikiTextUtil::removeHTMLcomments($opti);
134
            // compte pas la suppression de langue=fr : provisoire (fix on SQL)
135
            $optiLanfr = preg_replace('#\|[\n ]*langue=fr[\n ]*#', '', $opti);
136
137
            if (!empty($opti)
138
                && (mb_strpos($text, $opti) !== false
139
                    || mb_strpos(mb_strtolower($text), $optiLower) !== false
140
                    || mb_strpos($text, $optiComment) !== false
141
                    || mb_strpos($text, $optiLanfr) !== false)
142
            ) {
143
                echo '+';
144
                $found++;
145
            } else {
146
                echo '-';
147
            }
148
            // ici update DB
149
150
        }
151
152
        return (int)round(($count - $found) / count($data) * 100);
153
    }
154
155
}
156