Monitor::pageProcess()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 28
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 42
rs 8.8497
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
class Monitor
28
{
29
    final public const SLEEP_TIME = 60;
30
31
    private readonly DbAdapter $db;
32
    private string $lastTitle = '';
33
    private readonly MediawikiFactory $wiki;
34
35
    public function __construct()
36
    {
37
        $this->db = new DbAdapter();
0 ignored issues
show
Bug introduced by
The property db is declared read-only in App\Application\CLI\Monitor.
Loading history...
38
        $this->wiki = ServiceFactory::getMediawikiFactory();
0 ignored issues
show
Bug introduced by
The property wiki is declared read-only in App\Application\CLI\Monitor.
Loading history...
39
    }
40
41
    public function run(): void
42
    {
43
        $i = 0;
44
        while (true) {
45
            $i++;
46
            echo "\n-----MONITOR------------------------\n\n";
47
            //$memory->echoMemory(true);
48
49
            $this->pageProcess();
50
            sleep(self::SLEEP_TIME);
51
52
            if ($i > 1000) {
53
                echo "1000 monitoring => break";
54
                break;
55
            }
56
        }
57
    }
58
59
    private function pageProcess(): void
60
    {
61
        $data = $this->db->getMonitor();
62
        if (empty($data)) {
63
            echo "new data vide. Sleep 1h\n";
64
            sleep(60 * 60);
65
66
            return;
67
        }
68
69
        $title = $data[0]['page'];
70
        if ($title === $this->lastTitle) {
71
            echo "end. Sleep 1h\n";
72
            sleep(60 * 60);
73
74
            return;
75
        }
76
        echo "$title \n";
77
78
        $pageAction = new WikiPageAction($this->wiki, $title);
79
        $text = $pageAction->getText();
80
        if (!$text || empty($text)) {
81
            echo "Pas de wiki texte\n";
82
            $stat = 0;
83
            goto updateMonitor;
84
        }
85
86
        $stat = '0';
87
        $suffix = '';
88
        if (!in_array($pageAction->getLastEditor(), ['CodexBot', 'ZiziBot', getenv('BOT_NAME'), getenv('BOT_OWNER')])) {
89
            $stat = $this->checkAltered($data, $text);
90
            $suffix = '[found]';
91
        }
92
93
        echo $data[0]['edited']." : ".$stat." % ".$suffix."\n";
94
95
        updateMonitor:
96
        $this->db->updateMonitor(
97
            [
98
                'page' => $title ?? '',
99
                'verify' => date("Y-m-d H:i:s"),
100
                'altered' => (int)$stat,
101
            ]
102
        );
103
    }
104
105
    /**
106
     * TODO : if find raw -> reverted
107
     */
108
    private function checkAltered(array $data, string $text): int
109
    {
110
        if ($data === []) {
111
            return 99;
112
        }
113
        $found = 0;
114
        $count = 0;
115
        $text = mb_strtolower($text);
116
        foreach ($data as $dat) {
117
            if (1 === (int) $dat['skip'] || empty($dat['edited'])) {
118
                continue;
119
            }
120
            $count++;
121
            if (empty($dat['opti'])) {
122
                echo 'opti vide';
123
                continue;
124
            }
125
126
            // compte pas différence Unicode entre BD et wiki
127
            $opti = Normalizer::normalize($dat['opti']); // hack
128
            // compte pas les changements de typo majuscule/minuscule
129
            $optiLower = mb_strtolower($opti);
130
            // compte pas la correction sur ouvrage avec commentaire HTML
131
            $optiComment = WikiTextUtil::removeHTMLcomments($opti);
132
            // compte pas la suppression de langue=fr : provisoire (fix on SQL)
133
            $optiLanfr = preg_replace('#\|[\n ]*langue=fr[\n ]*#', '', $opti);
134
135
            if (!empty($opti)
136
                && (mb_strpos($text, $opti) !== false
137
                    || mb_strpos(mb_strtolower($text), $optiLower) !== false
138
                    || mb_strpos($text, $optiComment) !== false
139
                    || mb_strpos($text, $optiLanfr) !== false)
140
            ) {
141
                echo '+';
142
                $found++;
143
            } else {
144
                echo '-';
145
            }
146
            // ici update DB
147
148
        }
149
150
        return (int)round(($count - $found) / count($data) * 100);
151
    }
152
}
153