Passed
Push — dev ( 4ef148...eba0dc )
by Dispositif
07:17
created

AbstractBotTaskWorker   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
c 1
b 0
f 0
dl 0
loc 161
rs 10
wmc 19

7 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 6 2
A getTitles() 0 7 2
A checkAllowedEdition() 0 17 3
A __construct() 0 9 2
A getText() 0 8 2
B titleProcess() 0 30 7
A doEdition() 0 9 1
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;
11
12
use App\Infrastructure\PageListInterface;
13
use Mediawiki\Api\MediawikiFactory;
14
use Mediawiki\Api\UsageException;
15
use Mediawiki\DataModel\EditInfo;
16
17
abstract class AbstractBotTaskWorker
18
{
19
    const TASK_NAME           = "bot : Amélioration bibliographique";
20
    const SLEEP_AFTER_EDITION = 60;
21
    /**
22
     * @var PageListInterface
23
     */
24
    protected $pageListGenerator;
25
    /**
26
     * @var WikiBotConfig
27
     */
28
    protected $bot;
29
    /**
30
     * @var MediawikiFactory
31
     */
32
    protected $wiki;
33
    /**
34
     * @var WikiPageAction
35
     */
36
    protected $pageAction;
37
    // TODO : move taskName, botFlag... to to WikiBotConfig
38
    protected $taskName;
39
    protected $minorFlag = false;
40
    protected $botFlag = false;
41
    protected $modeAuto = false;
42
43
    /**
44
     * Goo2ouvrageWorker constructor.
45
     *
46
     * @param WikiBotConfig     $bot
47
     * @param MediawikiFactory  $wiki
48
     * @param PageListInterface $pagesGen
49
     */
50
    public function __construct(WikiBotConfig $bot, MediawikiFactory $wiki, ?PageListInterface $pagesGen = null)
51
    {
52
        $this->wiki = $wiki;
53
        $this->bot = $bot;
54
        if ($pagesGen) {
55
            $this->pageListGenerator = $pagesGen;
56
        }
57
58
        $this->run();
59
    }
60
61
    public function run()
62
    {
63
        $titles = $this->getTitles();
64
65
        foreach ($titles as $title) {
66
            $this->titleProcess($title);
67
        }
68
    }
69
70
    protected function getTitles(): array
71
    {
72
        if ($this->pageListGenerator) {
73
            return $this->pageListGenerator->getPageTitles();
74
        }
75
76
        return [];
77
    }
78
79
    /**
80
     * @param string $title
81
     *
82
     * @throws UsageException
83
     * @throws Throwable
84
     */
85
    protected function titleProcess(string $title): void
86
    {
87
        echo "$title \n";
88
        sleep(2);
89
90
        $this->taskName = static::TASK_NAME;
91
92
        $text = $this->getText($title);
93
        if (!$this->checkAllowedEdition($title, $text)) {
1 ignored issue
show
Bug introduced by
It seems like $text can also be of type null; however, parameter $text of App\Application\Abstract...::checkAllowedEdition() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
        if (!$this->checkAllowedEdition($title, /** @scrutinizer ignore-type */ $text)) {
Loading history...
94
            return;
95
        }
96
97
        $newText = $this->processDomain($title, $text);
98
99
        if (empty($newText) || $newText === $text) {
100
            echo "Skip identique ou vide\n";
101
102
            return;
103
        }
104
105
        if (!$this->modeAuto) {
106
            $ask = readline("*** ÉDITION ? [y/n/auto]");
107
            if ('auto' === $ask) {
108
                $this->modeAuto = true;
109
            } elseif ('y' !== $ask) {
110
                return;
111
            }
112
        }
113
114
        $this->doEdition($newText);
115
    }
116
117
    /**
118
     * todo DI
119
     *
120
     * @param string $title
121
     *
122
     * @return string|null
123
     * @throws Exception
124
     * @throws \Exception
125
     */
126
    protected function getText(string $title): ?string
127
    {
128
        $this->pageAction = new WikiPageAction($this->wiki, $title); // throw Exception
129
        if ($this->pageAction->getNs() !== 0) {
130
            throw new Exception("La page n'est pas dans Main (ns!==0)");
0 ignored issues
show
Bug introduced by
The type App\Application\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
131
        }
132
133
        return $this->pageAction->getText();
134
    }
135
136
    /**
137
     * Controle droit d'edition.
138
     *
139
     * @param string $title
140
     * @param string $text
141
     *
142
     * @return bool
143
     * @throws UsageException
144
     */
145
    protected function checkAllowedEdition(string $title, string $text): bool
146
    {
147
        // CONTROLES EDITION
148
        $this->bot->checkStopOnTalkpage(true);
149
150
        if (WikiBotConfig::isEditionRestricted($text)) {
151
            echo "SKIP : protection/3R.\n";
152
153
            return false;
154
        }
155
        if ($this->bot->minutesSinceLastEdit($title) < 4) {
156
            echo "SKIP : édition humaine dans les dernières 4 minutes.\n";
157
158
            return false;
159
        }
160
161
        return true;
162
    }
163
164
    /**
165
     * return $newText for editing
166
     */
167
    abstract protected function processDomain(string $title, ?string $text): ?string;
168
169
    protected function doEdition(string $newText): void
170
    {
171
        $result = $this->pageAction->editPage(
172
            $newText,
173
            new EditInfo($this->taskName, $this->minorFlag, $this->botFlag)
174
        );
175
        dump($result);
176
        echo "Sleep ".(string)static::SLEEP_AFTER_EDITION."\n";
177
        sleep(static::SLEEP_AFTER_EDITION);
178
    }
179
}
180