Passed
Push — master ( 35b4f7...a2b417 )
by Dispositif
02:30
created

shrinkLongSummaryIfNoImportantDetailsToVerify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
nc 3
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
    abstract protected function addErrorWarning(string $title, string $text): void;
24
25
    /**
26
     * Generate wiki edition summary.
27
     */
28
    protected function generateFinalSummary(): string
29
    {
30
        $prefix = $this->generatePrefix();
31
        $citeSummary = $this->getCiteSummary();
32
33
        $summary = sprintf(
34
            '%s [%s] %s %sx : %s',
35
            trim($prefix),
36
            str_replace('v', '', $this->pageWorkStatus->citationVersion),
37
            trim(self::TASK_NAME),
38
            $this->pageWorkStatus->nbRows,
39
            $citeSummary
40
        );
41
42
        $summary = $this->shrinkLongSummaryIfNoImportantDetailsToVerify($summary);
43
        $summary = $this->couldAddLuckMessage($summary);
44
        $this->log->notice($summary);
45
46
        return $summary;
47
    }
48
49
    /**
50
     * Shrink long summary if no important details to verify.
51
     */
52
    protected function shrinkLongSummaryIfNoImportantDetailsToVerify(string $summary): string
53
    {
54
        if (empty($this->pageWorkStatus->importantSummary)) {
55
            $length = strlen($summary);
56
            $summary = mb_substr($summary, 0, 80);
57
            $summary .= ($length > strlen($summary)) ? '…' : '';
58
        } else {
59
            $summary .= '…'; // ?
60
        }
61
        return $summary;
62
    }
63
64
    protected function couldAddLuckMessage(string $summary): string
65
    {
66
        if (!$this->pageWorkStatus->luckyState && (new DateTime())->format('H:i') === '11:11') {
67
            $this->pageWorkStatus->luckyState = true;
68
            $summary .= self::LUCKY_MESSAGE;
69
        }
70
71
        return $summary;
72
    }
73
74
    protected function generatePrefix(): string
75
    {
76
        $prefix = ($this->pageWorkStatus->botFlag) ? 'bot ' : '';
77
        $prefix .= (empty($this->pageWorkStatus->errorWarning)) ? '' : ' ⚠️';
78
        $prefix .= (empty($this->pageWorkStatus->featured_article)) ? '' : ' ☆'; // AdQ, BA
79
80
        return $prefix;
81
    }
82
83
    /**
84
     * Generate list of details about current bot edition.
85
     */
86
    protected function getCiteSummary(): string
87
    {
88
        // basic modifs
89
        $citeSummary = implode(' ', $this->pageWorkStatus->citationSummary);
90
        // replaced by list of modifs to verify by humans
91
        if (!empty($this->pageWorkStatus->importantSummary)) {
92
            $citeSummary = implode(', ', $this->pageWorkStatus->importantSummary);
93
        }
94
        return $citeSummary;
95
    }
96
97
    /**
98
     * For substantive or ambiguous modifications done.
99
     *
100
     * @param string $tag
101
     */
102
    protected function addSummaryTag(string $tag)
103
    {
104
        if (!in_array($tag, $this->pageWorkStatus->importantSummary)) {
105
            $this->pageWorkStatus->importantSummary[] = $tag;
106
        }
107
    }
108
109
    /**
110
     * todo extract. => responsability : pageWorkStatus + summary
111
     * Vérifie alerte d'erreurs humaines.
112
     */
113
    protected function addSummaryDataOnPageWorkStatus(array $ouvrageData): void
114
    {
115
        // paramètre inconnu
116
        if (preg_match_all(
117
                "#\|[^|]+<!-- ?(PARAMETRE [^>]+ N'EXISTE PAS|VALEUR SANS NOM DE PARAMETRE|ERREUR [^>]+) ?-->#",
118
                $ouvrageData['opti'],
119
                $matches
120
            ) > 0
121
        ) {
122
            foreach ($matches[0] as $line) {
123
                $this->addErrorWarning($ouvrageData['page'], $line);
124
            }
125
            //  $this->pageWorkStatus->botFlag = false;
126
            $this->addSummaryTag('paramètre non corrigé');
127
        }
128
129
        // ISBN invalide
130
        if (preg_match("#isbn invalide ?=[^|}]+#i", $ouvrageData['opti'], $matches) > 0) {
131
            $this->addErrorWarning($ouvrageData['page'], $matches[0]);
132
            $this->pageWorkStatus->botFlag = false;
133
            $this->addSummaryTag('ISBN invalide 💩');
134
        }
135
136
        // Edits avec ajout conséquent de donnée
137
        if (preg_match('#distinction des auteurs#', $ouvrageData['modifs']) > 0) {
138
            $this->pageWorkStatus->botFlag = false;
139
            $this->addSummaryTag('distinction auteurs 🧠');
140
        }
141
        // prédiction paramètre correct
142
        if (preg_match('#[^,]+(=>|⇒)[^,]+#', $ouvrageData['modifs'], $matches) > 0) {
143
            $this->pageWorkStatus->botFlag = false;
144
            $this->addSummaryTag($matches[0]);
145
        }
146
        if (preg_match('#\+\+sous-titre#', $ouvrageData['modifs']) > 0) {
147
            $this->pageWorkStatus->botFlag = false;
148
            $this->addSummaryTag('+sous-titre');
149
        }
150
        if (preg_match('#\+lieu#', $ouvrageData['modifs']) > 0) {
151
            $this->addSummaryTag('+lieu');
152
        }
153
        if (preg_match('#tracking#', $ouvrageData['modifs']) > 0) {
154
            $this->addSummaryTag('tracking');
155
        }
156
        if (preg_match('#présentation en ligne#', $ouvrageData['modifs']) > 0) {
157
            $this->addSummaryTag('+présentation en ligne✨');
158
        }
159
        if (preg_match('#distinction auteurs#', $ouvrageData['modifs']) > 0) {
160
            $this->addSummaryTag('distinction auteurs 🧠');
161
        }
162
        if (preg_match('#\+lire en ligne#', $ouvrageData['modifs']) > 0) {
163
            $this->addSummaryTag('+lire en ligne✨');
164
        }
165
        if (preg_match('#\+lien #', $ouvrageData['modifs']) > 0) {
166
            $this->addSummaryTag('wikif');
167
        }
168
169
        if (preg_match('#\+éditeur#', $ouvrageData['modifs']) > 0) {
170
            $this->addSummaryTag('éditeur');
171
        }
172
        //        if (preg_match('#\+langue#', $data['modifs']) > 0) {
173
        //            $this->addSummaryTag('langue');
174
        //        }
175
176
        // mention BnF si ajout donnée + ajout identifiant bnf=
177
        if (!empty($this->pageWorkStatus->importantSummary) && preg_match('#BnF#i', $ouvrageData['modifs'], $matches) > 0) {
178
            $this->addSummaryTag('©[[BnF]]');
179
        }
180
    }
181
}
182