Passed
Push — master ( e121f1...792cb7 )
by Dispositif
02:56
created

Monitor::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 14
rs 9.9666
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Application\CLI;
11
12
use App\Application\WikiPageAction;
13
use App\Domain\Utils\WikiTextUtil;
14
use App\Infrastructure\DbAdapter;
15
use App\Infrastructure\ServiceFactory;
16
use Mediawiki\Api\MediawikiFactory;
17
use Normalizer;
18
19
include __DIR__.'/../myBootstrap.php';
20
21
$process = new Monitor();
22
$process->run();
23
24
/**
25
 * TODO refac
26
 *
27
 */
28
class Monitor
29
{
30
    public const SLEEP_TIME = 60;
31
32
    private $db;
33
    private $lastTitle = '';
34
    /**
35
     * @var MediawikiFactory
36
     */
37
    private $wiki;
38
39
    public function __construct()
40
    {
41
        $this->db = new DbAdapter();
42
        $this->wiki = ServiceFactory::getMediawikiFactory();
43
    }
44
45
    public function run(): void
46
    {
47
        $i = 0;
48
        while (true) {
49
            $i++;
50
            echo "\n-----MONITOR------------------------\n\n";
51
            //$memory->echoMemory(true);
52
53
            $this->pageProcess();
54
            sleep(self::SLEEP_TIME);
55
56
            if ($i > 1000) {
57
                echo "1000 monitoring => break";
58
                break;
59
            }
60
        }
61
    }
62
63
    private function pageProcess(): void
64
    {
65
        $data = $this->db->getMonitor();
66
        if (empty($data)) {
67
            echo "new data vide. Sleep 1h\n";
68
            sleep(60 * 60);
69
70
            return;
71
        }
72
73
        $title = $data[0]['page'];
74
        if ($title === $this->lastTitle) {
75
            echo "end. Sleep 1h\n";
76
            sleep(60 * 60);
77
78
            return;
79
        }
80
        echo "$title \n";
81
82
        $pageAction = new WikiPageAction($this->wiki, $title);
83
        $text = $pageAction->getText();
84
        if (!$text || empty($text)) {
85
            echo "Pas de wiki texte\n";
86
            $stat = 0;
87
            goto updateMonitor;
88
        }
89
90
        $stat = '0';
91
        $suffix = '';
92
        if (!in_array($pageAction->getLastEditor(), ['CodexBot', 'ZiziBot', getenv('BOT_NAME'), getenv('BOT_OWNER')])) {
93
            $stat = $this->checkAltered($data, $text);
94
            $suffix = '[found]';
95
        }
96
97
        echo $data[0]['edited']." : ".$stat." % ".$suffix."\n";
98
99
        updateMonitor:
100
        $this->db->updateMonitor(
101
            [
102
                'page' => $title ?? '',
103
                'verify' => date("Y-m-d H:i:s"),
104
                'altered' => (int)$stat,
105
            ]
106
        );
107
    }
108
109
    /**
110
     * TODO : if find raw -> reverted
111
     *
112
     * @param array  $data
113
     * @param string $text
114
     *
115
     * @return int
116
     */
117
    private function checkAltered(array $data, string $text): int
118
    {
119
        if ($data === []) {
120
            return 99;
121
        }
122
        $found = 0;
123
        $count = 0;
124
        $text = mb_strtolower($text);
125
        foreach ($data as $dat) {
126
            if (1 === (int) $dat['skip'] || empty($dat['edited'])) {
127
                continue;
128
            }
129
            $count++;
130
            if (empty($dat['opti'])) {
131
                echo 'opti vide';
132
                continue;
133
            }
134
135
            // compte pas différence Unicode entre BD et wiki
136
            $opti = Normalizer::normalize($dat['opti']); // hack
137
            // compte pas les changements de typo majuscule/minuscule
138
            $optiLower = mb_strtolower($opti);
139
            // compte pas la correction sur ouvrage avec commentaire HTML
140
            $optiComment = WikiTextUtil::removeHTMLcomments($opti);
141
            // compte pas la suppression de langue=fr : provisoire (fix on SQL)
142
            $optiLanfr = preg_replace('#\|[\n ]*langue=fr[\n ]*#', '', $opti);
143
144
            if (!empty($opti)
145
                && (mb_strpos($text, $opti) !== false
146
                    || mb_strpos(mb_strtolower($text), $optiLower) !== false
147
                    || mb_strpos($text, $optiComment) !== false
148
                    || mb_strpos($text, $optiLanfr) !== false)
149
            ) {
150
                echo '+';
151
                $found++;
152
            } else {
153
                echo '-';
154
            }
155
            // ici update DB
156
157
        }
158
159
        return (int)round(($count - $found) / count($data) * 100);
160
    }
161
162
}
163