Passed
Branch dev3 (ae391d)
by Dispositif
02:29
created

OuvrageEditSummaryTrait::couldAddLuckMessage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
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\OuvrageEdit;
11
12
use DateTime;
13
use function App\Application\mb_substr;
0 ignored issues
show
introduced by
The function App\Application\mb_substr was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
14
15
/**
16
 * Used only by OuvrageEditWorker.
17
 */
18
trait OuvrageEditSummaryTrait
19
{
20
    /* Beware !! $importantSummary also defined in OuvrageEditWorker */
21
    public $importantSummary = [];
22
23
    /**
24
     * Generate wiki edition summary.
25
     */
26
    public function generateFinalSummary(): string
27
    {
28
        $prefix = $this->generatePrefix();
29
        $citeSummary = $this->getCiteSummary();
30
31
        $summary = sprintf(
32
            '%s [%s] %s %sx : %s',
33
            trim($prefix),
34
            str_replace('v', '', $this->pageWorkStatus->citationVersion),
35
            trim(self::TASK_NAME),
36
            $this->pageWorkStatus->nbRows,
37
            $citeSummary
38
        );
39
40
        $summary = $this->shrinkLongSummaryIfNoImportantDetailsToVerify($summary);
41
        $summary = $this->couldAddLuckMessage($summary);
42
        $this->log->notice($summary);
43
44
        return $summary;
45
    }
46
47
    /**
48
     * Shrink long summary if no important details to verify.
49
     */
50
    protected function shrinkLongSummaryIfNoImportantDetailsToVerify(string $summary): string
51
    {
52
        if (empty($this->pageWorkStatus->importantSummary)) {
53
            $length = strlen($summary);
54
            $summary = mb_substr($summary, 0, 80);
55
            $summary .= ($length > strlen($summary)) ? '…' : '';
56
        } else {
57
            $summary .= '…'; // ?
58
        }
59
        return $summary;
60
    }
61
62
    protected function couldAddLuckMessage(string $summary): string
63
    {
64
        if (!$this->pageWorkStatus->luckyState && (new DateTime())->format('H:i') === '11:11') {
65
            $this->pageWorkStatus->luckyState = true;
66
            $summary .= self::LUCKY_MESSAGE;
67
        }
68
69
        return $summary;
70
    }
71
72
    protected function generatePrefix(): string
73
    {
74
        $prefix = ($this->pageWorkStatus->botFlag) ? 'bot ' : '';
75
        $prefix .= (empty($this->pageWorkStatus->errorWarning)) ? '' : ' ⚠️';
76
        $prefix .= (empty($this->pageWorkStatus->featured_article)) ? '' : ' ☆'; // AdQ, BA
77
78
        return $prefix;
79
    }
80
81
    /**
82
     * Generate list of details about current bot edition.
83
     */
84
    protected function getCiteSummary(): string
85
    {
86
        // basic modifs
87
        $citeSummary = implode(' ', $this->pageWorkStatus->citationSummary);
88
        // replaced by list of modifs to verify by humans
89
        if (!empty($this->pageWorkStatus->importantSummary)) {
90
            $citeSummary = implode(', ', $this->pageWorkStatus->importantSummary);
91
        }
92
        return $citeSummary;
93
    }
94
95
    /**
96
     * For substantive or ambiguous modifications done.
97
     *
98
     * @param string $tag
99
     */
100
    protected function addSummaryTag(string $tag)
101
    {
102
        if (!in_array($tag, $this->pageWorkStatus->importantSummary)) {
103
            $this->pageWorkStatus->importantSummary[] = $tag;
104
        }
105
    }
106
107
    /**
108
     * todo extract. => responsability : pageWorkStatus + summary
109
     * Vérifie alerte d'erreurs humaines.
110
     */
111
    protected function generateSummaryOnPageWorkStatus(array $ouvrageData): void
112
    {
113
        // paramètre inconnu
114
        if (preg_match_all(
115
                "#\|[^|]+<!-- ?(PARAMETRE [^>]+ N'EXISTE PAS|VALEUR SANS NOM DE PARAMETRE|ERREUR [^>]+) ?-->#",
116
                $ouvrageData['opti'],
117
                $matches
118
            ) > 0
119
        ) {
120
            foreach ($matches[0] as $line) {
121
                $this->addErrorWarning($ouvrageData['page'], $line);
1 ignored issue
show
Bug introduced by
It seems like addErrorWarning() 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

121
                $this->/** @scrutinizer ignore-call */ 
122
                       addErrorWarning($ouvrageData['page'], $line);
Loading history...
122
            }
123
            //  $this->pageWorkStatus->botFlag = false;
124
            $this->addSummaryTag('paramètre non corrigé');
125
        }
126
127
        // ISBN invalide
128
        if (preg_match("#isbn invalide ?=[^|}]+#i", $ouvrageData['opti'], $matches) > 0) {
129
            $this->addErrorWarning($ouvrageData['page'], $matches[0]);
130
            $this->pageWorkStatus->botFlag = false;
131
            $this->addSummaryTag('ISBN invalide 💩');
132
        }
133
134
        // Edits avec ajout conséquent de donnée
135
        if (preg_match('#distinction des auteurs#', $ouvrageData['modifs']) > 0) {
136
            $this->pageWorkStatus->botFlag = false;
137
            $this->addSummaryTag('distinction auteurs 🧠');
138
        }
139
        // prédiction paramètre correct
140
        if (preg_match('#[^,]+(=>|⇒)[^,]+#', $ouvrageData['modifs'], $matches) > 0) {
141
            $this->pageWorkStatus->botFlag = false;
142
            $this->addSummaryTag($matches[0]);
143
        }
144
        if (preg_match('#\+\+sous-titre#', $ouvrageData['modifs']) > 0) {
145
            $this->pageWorkStatus->botFlag = false;
146
            $this->addSummaryTag('+sous-titre');
147
        }
148
        if (preg_match('#\+lieu#', $ouvrageData['modifs']) > 0) {
149
            $this->addSummaryTag('+lieu');
150
        }
151
        if (preg_match('#tracking#', $ouvrageData['modifs']) > 0) {
152
            $this->addSummaryTag('tracking');
153
        }
154
        if (preg_match('#présentation en ligne#', $ouvrageData['modifs']) > 0) {
155
            $this->addSummaryTag('+présentation en ligne✨');
156
        }
157
        if (preg_match('#distinction auteurs#', $ouvrageData['modifs']) > 0) {
158
            $this->addSummaryTag('distinction auteurs 🧠');
159
        }
160
        if (preg_match('#\+lire en ligne#', $ouvrageData['modifs']) > 0) {
161
            $this->addSummaryTag('+lire en ligne✨');
162
        }
163
        if (preg_match('#\+lien #', $ouvrageData['modifs']) > 0) {
164
            $this->addSummaryTag('wikif');
165
        }
166
167
        if (preg_match('#\+éditeur#', $ouvrageData['modifs']) > 0) {
168
            $this->addSummaryTag('éditeur');
169
        }
170
        //        if (preg_match('#\+langue#', $data['modifs']) > 0) {
171
        //            $this->addSummaryTag('langue');
172
        //        }
173
174
        // mention BnF si ajout donnée + ajout identifiant bnf=
175
        if (!empty($this->pageWorkStatus->importantSummary) && preg_match('#BnF#i', $ouvrageData['modifs'], $matches) > 0) {
176
            $this->addSummaryTag('©[[BnF]]');
177
        }
178
    }
179
}
180