Passed
Push — master ( a1929c...27d925 )
by Dispositif
02:28
created

BotWorkerTrait::getSummaryText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\Traits;
11
12
use App\Application\WikiBotConfig;
13
use Codedungeon\PHPCliColors\Color;
14
use Mediawiki\Api\UsageException;
15
16
trait BotWorkerTrait
17
{
18
    /**
19
     * @throws UsageException
20
     */
21
    protected function checkAllowedNowEdition(string $title, string $text): bool
22
    {
23
        $this->bot->checkStopOnTalkpage(true);
24
25
        if (WikiBotConfig::isEditionTemporaryRestrictedOnWiki($text)) {
26
            echo "SKIP : protection/3R/travaux.\n";
27
28
            return false;
29
        }
30
        if ($this->bot->minutesSinceLastEdit($title) < static::MINUTES_DELAY_AFTER_LAST_HUMAN_EDIT) {
0 ignored issues
show
Bug introduced by
The constant App\Application\Traits\B...Y_AFTER_LAST_HUMAN_EDIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
31
            echo sprintf(
32
                "SKIP : édition humaine dans les dernières %s minutes\n",
33
                static::MINUTES_DELAY_AFTER_LAST_HUMAN_EDIT
34
            );
35
36
            return false;
37
        }
38
        if (static::SKIP_ADQ && preg_match('#{{ ?En-tête label ?\| ?AdQ#i', $text)) {
0 ignored issues
show
Bug introduced by
The constant App\Application\Traits\BotWorkerTrait::SKIP_ADQ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
            echo "SKIP : AdQ.\n"; // BA ??
40
41
            return false;
42
        }
43
44
        return true;
45
    }
46
47
    protected function getSummaryText(): string
48
    {
49
        return $this->summary->serialize();
50
    }
51
52
    protected function printTitle(string $title): void
53
    {
54
        echo "---------------------\n";
55
        echo date('d-m-Y H:i:s') . ' ' . Color::BG_CYAN . "  $title " . Color::NORMAL . "\n";
56
    }
57
58
    protected function canProcessTitleArticle(string $title, ?string $text): bool
59
    {
60
        if (empty($text)) {
61
            echo "Skip : page vide\n";
62
            $this->memorizeAndSaveAnalyzedTitle($title);
0 ignored issues
show
Bug introduced by
It seems like memorizeAndSaveAnalyzedTitle() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

62
            $this->/** @scrutinizer ignore-call */ 
63
                   memorizeAndSaveAnalyzedTitle($title);
Loading history...
63
64
            return false;
65
        }
66
        if (static::SKIP_LASTEDIT_BY_BOT && $this->pageAction->getLastEditor() === getenv('BOT_NAME')) {
0 ignored issues
show
Bug introduced by
The constant App\Application\Traits\B...t::SKIP_LASTEDIT_BY_BOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
67
            echo "Skip : bot est le dernier éditeur\n";
68
            $this->memorizeAndSaveAnalyzedTitle($title);
69
70
            return false;
71
        }
72
        if (!$this->checkAllowedNowEdition($title, $text)) {
73
            return false;
74
        }
75
76
        return true;
77
    }
78
79
    protected function isSomethingToChange(?string $text, ?string $newText): bool
80
    {
81
        if (empty($newText)) {
82
            echo "Skip: vide\n";
83
84
            return false;
85
        }
86
        if ($newText === $text) {
87
            echo "Skip: pas de changement\n";
88
89
            return false;
90
        }
91
92
        return true;
93
    }
94
}