Passed
Push — master ( d67625...fee625 )
by Dispositif
08:37
created

OuvrageOptimize::distinguishAuthors()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 26
nc 5
nop 0
dl 0
loc 43
ccs 23
cts 23
cp 1
crap 7
rs 8.5706
c 3
b 0
f 0
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\Domain;
11
12
use App\Domain\Enums\Language;
13
use App\Domain\Models\Wiki\GoogleLivresTemplate;
14
use App\Domain\Publisher\GoogleBooksUtil;
15
use App\Domain\Utils\TextUtil;
16
use App\Domain\Utils\WikiTextUtil;
17
use App\Infrastructure\FileManager;
18
use DomainException;
19
use Exception;
20
21
/**
22
 * Legacy.
23
 * TODO move methods to OuvrageClean setters
24
 * TODO AbstractProcess
25
 * TODO observer/event (log, MajorEdition)
26
 * Class OuvrageProcess.
27
 */
28
class OuvrageOptimize extends AbstractTemplateOptimizer
29
{
30
    use OptimizeISBNTrait;
31
32
    const CONVERT_GOOGLEBOOK_TEMPLATE = false; // change OuvrageOptimizeTest !!
33
34
    const WIKI_LANGUAGE = 'fr';
35
36
    const PUBLISHER_FRWIKI_FILENAME = __DIR__.'/resources/data_editors_wiki.json';
37
38
    public $notCosmetic = false;
39
40
    public $major = false;
41
42
    /**
43
     * @return $this
44
     * @throws Exception
45
     */
46
    public function doTasks(): self
47
    {
48
        $this->cleanAndPredictErrorParameters();
49
50
        $this->processAuthors();
51
52
        $this->processLang();
53
        $this->processLang('langue originale');
54
55
        $this->processTitle();
56
        $this->convertLienAuteurTitre();
57
        $this->processEditeur();
58 47
        $this->processDates();
59
        $this->externalTemplates();
60 47
        $this->predictFormatByPattern();
61 47
62 47
        $this->processIsbn();
63 47
        $this->processBnf();
64 47
65
        $this->processLocation(); // 'lieu'
66
67
        $this->GoogleBookURL('lire en ligne');
68
        $this->GoogleBookURL('présentation en ligne');
69
70 47
        return $this;
71
    }
72 47
73
    /**
74 47
     * Todo: injection dep.
75
     * Todo : "[s. l.]" sans lieu "s.l.n.d." sans lieu ni date.
76 47
     *
77 47
     * @throws Exception
78
     */
79 47
    protected function processLocation()
80 47
    {
81 47
        $location = $this->getParam('lieu');
82 47
        if (empty($location)) {
83 47
            return;
84 47
        }
85
86 47
        // typo and unwikify
87 47
        $memo = $location;
88
        $location = WikiTextUtil::unWikify($location);
89 47
        $location = TextUtil::mb_ucfirst($location);
90
        if ($memo !== $location) {
91 47
            $this->setParam('lieu', $location);
92 47
            $this->addSummaryLog('±lieu');
93
            $this->notCosmetic = true;
94 47
        }
95
96
        // translation : "London"->"Londres" seulement si langue=fr
97
        if ($this->hasParamValue('langue')
98
            && $this->getParam('langue') === self::WIKI_LANGUAGE
99
        ) {
100
            $manager = new FileManager();
101
            $row = $manager->findCSVline(__DIR__.'/resources/traduction_ville.csv', $location);
102
            if (!empty($row) && !empty($row[1])) {
103 47
                $this->setParam('lieu', $row[1]);
104
                $this->addSummaryLog('lieu francisé');
105 47
                $this->notCosmetic = true;
106 47
            }
107 42
        }
108
    }
109
110
    protected function processBnf()
111 5
    {
112 5
        $bnf = $this->getParam('bnf');
113 5
        if (!$bnf) {
114 5
            return;
115 1
        }
116 1
        $bnf = str_ireplace('FRBNF', '', $bnf);
117 1
        $this->setParam('bnf', $bnf);
118
    }
119
120
    /**
121 5
     * @throws Exception
122 5
     */
123
    protected function processAuthors()
124 1
    {
125 1
        $this->distinguishAuthors();
126 1
        //$this->fusionFirstNameAndName(); // desactived : no consensus
127 1
    }
128 1
129 1
    protected function convertLienAuteurTitre(): void
130
    {
131
        $auteurParams = ['auteur1', 'auteur2', 'auteur2', 'titre'];
132 5
        foreach ($auteurParams as $auteurParam) {
133
            if ($this->hasParamValue($auteurParam)
134 47
                && $this->hasParamValue('lien '.$auteurParam)
135
            ) {
136 47
                $this->setParam(
137 47
                    $auteurParam,
138 46
                    WikiTextUtil::wikilink(
139
                        $this->getParam($auteurParam),
140 1
                        $this->getParam('lien '.$auteurParam)
141 1
                    )
142 1
                );
143
                $this->unsetParam('lien '.$auteurParam);
144
            }
145
        }
146
    }
147 47
148
    /**
149 47
     * Detect and correct multiple authors in same parameter.
150
     * Like "auteurs=J. M. Waller, M. Bigger, R. J. Hillocks".
151 47
     *
152
     * @throws Exception
153 47
     */
154
    protected function distinguishAuthors()
155 47
    {
156 47
        // merge params of author 1
157 47
        $auteur1 = $this->getParam('auteur') ?? '';
158 47
        $auteur1 .= $this->getParam('auteurs') ?? '';
159
        $auteur1 .= $this->getParam('prénom1') ?? '';
160 1
        $auteur1 .= ' '.$this->getParam('nom1') ?? '';
161 1
        $auteur1 = trim($auteur1);
162 1
        // of authors 2
163 1
        $auteur2 = $this->getParam('auteur2') ?? '';
164 1
        $auteur2 .= $this->getParam('prénom2') ?? '';
165
        $auteur2 .= ' '.$this->getParam('nom2') ?? '';
166
        $auteur2 = trim($auteur2);
167 1
168
        // skip if wikilink in author
169
        if (empty($auteur1) || WikiTextUtil::isWikify($auteur1)) {
170 47
            return;
171
        }
172
173
        $machine = new PredictAuthors();
174
        $res = $machine->predictAuthorNames($auteur1);
175
176
        if (1 === count($res)) {
177
            // auteurs->auteur?
178 47
            return;
179
        }
180
        // Many authors... and empty "auteur2"
181 47
        if (count($res) >= 2 && empty($auteur2)) {
182 47
            // delete author-params
183 47
            array_map(
184 47
                function ($param) {
185 47
                    $this->unsetParam($param);
186
                },
187 47
                ['auteur', 'auteurs', 'prénom1', 'nom1']
188 47
            );
189 47
            // iterate and edit new values
190 47
            $count = count($res);
191
            for ($i = 0; $i < $count; ++$i) {
192
                $this->setParam(sprintf('auteur%s', $i + 1), $res[$i]);
193 47
            }
194 42
            $this->addSummaryLog('distinction auteurs');
195
            $this->major = true;
196
            $this->notCosmetic = true;
197 5
        }
198 5
    }
199
200 5
    /**
201
     * todo: move/implement.
202 3
     *
203
     * @param string|null $param
204
     *
205 2
     * @throws Exception
206
     */
207 1
    protected function processLang(?string $param = 'langue')
208
    {
209 1
        $lang = $this->getParam($param) ?? null;
0 ignored issues
show
Bug introduced by
It seems like $param can also be of type null; however, parameter $name of App\Domain\AbstractTemplateOptimizer::getParam() 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

209
        $lang = $this->getParam(/** @scrutinizer ignore-type */ $param) ?? null;
Loading history...
210 1
211 1
        if ($lang) {
212
            $lang2 = Language::all2wiki($lang);
213
214 1
            // strip "langue originale=fr"
215 1
            if ('langue originale' === $param && self::WIKI_LANGUAGE === $lang2
216 1
                && (!$this->getParam('langue') || $this->getParam('langue') === $lang2)
217
            ) {
218 1
                $this->unsetParam('langue originale');
219 1
                $this->addSummaryLog('-langue originale');
220 1
            }
221
222 2
            if ($lang2 && $lang !== $lang2) {
223
                $this->setParam($param, $lang2);
224
                if (self::WIKI_LANGUAGE !== $lang2) {
225
                    $this->addSummaryLog('±'.$param);
226
                }
227
            }
228
        }
229
    }
230
231 47
    /**
232
     * Find year of book publication.
233 47
     *
234
     * @return int|null
235 47
     * @throws Exception
236 6
     */
237
    protected function findBookYear(): ?int
238
    {
239 6
        $annee = $this->getParam('année');
240 6
        if (!empty($annee) && is_numeric($annee)) {
241
            return intval($annee);
242 1
        }
243 1
        $date = $this->getParam('date');
244
        if ($date && preg_match('#[^0-9]?([12][0-9][0-9][0-9])[^0-9]?#', $date, $matches) > 0) {
245
            return intval($matches[1]);
246 6
        }
247 4
248 4
        return null;
249 3
    }
250
251
    protected function stripIsbn(string $isbn): string
252
    {
253 47
        return trim(preg_replace('#[^0-9Xx]#', '', $isbn));
254
    }
255
256
    protected function processTitle()
257
    {
258
        $oldtitre = $this->getParam('titre');
259
        $this->langInTitle();
260 47
        $this->deWikifyExternalLink('titre');
261
        $this->upperCaseFirstLetter('titre');
262 47
        $this->typoDeuxPoints('titre');
263 47
264 37
        $this->extractSubTitle();
265
266
        // 20-11-2019 : Retiré majuscule à sous-titre
267
268 10
        if ($this->getParam('titre') !== $oldtitre) {
269 10
            $this->addSummaryLog('±titre');
270
        }
271
272 2
        $this->valideNumeroChapitre();
273 2
        $this->deWikifyExternalLink('titre chapitre');
274 2
        $this->upperCaseFirstLetter('titre chapitre');
275 2
    }
276 2
277 2
    protected function detectColon($param): bool
278
    {
279
        // > 0 don't count a starting colon ":bla"
280
        if ($this->hasParamValue($param) && mb_strrpos($this->getParam('titre'), ':') > 0) {
281
            return true;
282
        }
283
284
        return false;
285
    }
286
287
    protected function extractSubTitle(): void
288
    {
289
        // FIXED bug [[fu:bar]]
290 2
        if (!$this->getParam('titre') || WikiTextUtil::isWikify($this->getParam('titre'))) {
291
            return;
292
        }
293
294 8
        if (!$this->detectColon('titre')) {
295 8
            return;
296 7
        }
297 1
        // Que faire si déjà un sous-titre ?
298
        if ($this->hasParamValue('sous-titre')) {
299
            return;
300 1
        }
301 1
302 1
        // titre>5 and sous-titre>5 and sous-titre<40
303
        if (preg_match('#^(?<titre>[^:]{5,}):(?<st>.{5,40})$#', $this->getParam('titre'), $matches) > 0) {
304 1
            $this->setParam('titre', trim($matches['titre']));
305 1
            $this->setParam('sous-titre', trim($matches['st']));
306
            $this->addSummaryLog('>sous-titre');
307
        }
308 1
    }
309
310
    /**
311
     * Normalize a Google Book links.
312 7
     * Clean the useless URL parameters or transform into wiki-template.
313 7
     *
314 7
     * @param $param
315
     *
316 1
     * @throws Exception
317
     */
318
    protected function googleBookUrl(string $param): void
319
    {
320 7
        $url = $this->getParam($param);
321 7
        if (empty($url)
322
            || !GoogleBooksUtil::isGoogleBookURL($url)
323 4
        ) {
324
            return;
325
        }
326 4
327
        if (self::CONVERT_GOOGLEBOOK_TEMPLATE) {
328
            $template = GoogleLivresTemplate::createFromURL($url);
329
            if ($template) {
330
                $this->setParam($param, $template->serialize());
331
                $this->addSummaryLog('{Google}');
332 4
                $this->notCosmetic = true;
333 4
334
                return;
335
            }
336 4
        }
337 4
338 4
        try {
339
            $goo = GoogleBooksUtil::simplifyGoogleUrl($url);
340
        } catch (DomainException $e) {
341
            // ID manquant ou malformé
342
            $errorValue = sprintf(
343 4
                '%s <!-- ERREUR %s -->',
344
                $url,
345
                $e->getMessage()
346
            );
347
            $this->setParam($param, $errorValue);
348
            $this->addSummaryLog('erreur URL');
349 7
            $this->notCosmetic = true;
350 7
            $this->major = true;
351 7
        }
352
353
        if (!empty($goo) && $goo !== $url) {
354 7
            $this->setParam($param, $goo);
355
            // cleaned tracking parameters in Google URL ?
356
            if (GoogleBooksUtil::isTrackingUrl($url)) {
357
                $this->addSummaryLog('tracking');
358
                $this->notCosmetic = true;
359
            }
360
        }
361
    }
362 10
363
    /**
364 10
     * - {{lang|...}} dans titre => langue=... puis titre nettoyé
365 10
     *  langue=L’utilisation de ce paramètre permet aussi aux synthétiseurs vocaux de reconnaître la langue du titre de
366 1
     * l’ouvrage.
367
     * Il est possible d'afficher plusieurs langues, en saisissant le nom séparé par des espaces ou des virgules. La première langue doit être celle du titre.
368 9
     *
369 9
     * @throws Exception
370 1
     */
371
    protected function langInTitle(): void
372
    {
373 8
        if (preg_match(
374
                '#^{{ ?(?:lang|langue) ?\| ?([a-z-]{2,5}) ?\| ?(?:texte=)?([^{}=]+)(?:\|dir=rtl)?}}$#i',
375
                $this->getParam('titre'),
376 9
                $matches
377
            ) > 0
378 9
        ) {
379
            $lang = trim($matches[1]);
380
            $newtitre = str_replace($matches[0], trim($matches[2]), $this->getParam('titre'));
381 47
382
            // problème : titre anglais de livre français
383 47
            // => conversion {{lang}} du titre seulement si langue= défini
384 47
            // opti : restreindre à ISBN zone 2 fr ?
385 47
            if ($lang === $this->getParam('langue')) {
386 47
                $this->setParam('titre', $newtitre);
387 47
                $this->addSummaryLog('°titre');
388
            }
389 47
390
            // desactivé à cause de l'exception décrite ci-dessus
391
            // si langue=VIDE : ajout langue= à partir de langue titre
392
            //            if (self::WIKI_LANGUAGE !== $lang && empty($this->getParam('langue'))) {
393 47
            //                $this->setParam('langue', $lang);
394 5
            //                $this->log('+langue='.$lang);
395
            //            }
396
        }
397 47
    }
398 47
399 47
    protected function processDates()
400 47
    {
401
        // dewikification
402 13
        $params = ['date', 'année', 'mois', 'jour'];
403
        foreach ($params as $param) {
404
            if ($this->hasParamValue($param) && WikiTextUtil::isWikify(' '.$this->getParam($param))) {
405 13
                $this->setParam($param, WikiTextUtil::unWikify($this->getParam($param)));
406 3
            }
407
        }
408
409 10
        try {
410
            $this->moveDate2Year();
411
        } catch (Exception $e) {
412 47
            $this->log->warning('Exception '.$e->getMessage());
413
        }
414
    }
415 47
416 34
    /**
417
     * Bool ?
418
     * déwikification du titre : consensus Bistro 27 août 2011
419 13
     * idem  'titre chapitre'.
420 10
     *
421
     * @param string $param
422
     *
423 3
     * @throws Exception
424
     */
425
    protected function deWikifyExternalLink(string $param): void
426
    {
427
        if (empty($this->getParam($param))) {
428 3
            return;
429 3
        }
430 3
        if (preg_match('#^\[(http[^ \]]+) ([^]]+)]#i', $this->getParam($param), $matches) > 0) {
431 3
            $this->setParam($param, str_replace($matches[0], $matches[2], $this->getParam($param)));
432
            $this->addSummaryLog('±'.$param);
433 3
434
            if (in_array($param, ['titre', 'titre chapitre'])) {
435
                if (empty($this->getParam('lire en ligne'))) {
436
                    $this->setParam('lire en ligne', $matches[1]);
437
                    $this->addSummaryLog('+lire en ligne');
438
439
                    return;
440
                }
441
                $this->addSummaryLog('autre lien externe: '.$matches[1]);
442
            }
443 47
        }
444
    }
445 47
446 47
    protected function upperCaseFirstLetter($param)
447 47
    {
448
        if (!$this->hasParamValue($param)) {
449 47
            return;
450
        }
451
        $newValue = TextUtil::mb_ucfirst(trim($this->getParam($param)));
452 1
        $this->setParam($param, $newValue);
453
    }
454
455
    /**
456
     * Typo internationale 'titre : sous-titre'.
457
     * Fix fantasy typo of subtitle with '. ' or ' - '.
458
     * International Standard Bibliographic Description :
459
     * https://fr.wikipedia.org/wiki/Wikip%C3%A9dia:Le_Bistro/13_janvier_2016#Modif_du_mod%C3%A8le:Ouvrage.
460
     *
461
     * @param $param
462
     *
463
     * @throws Exception
464 1
     */
465
    protected function typoDeuxPoints($param)
466
    {
467
        $origin = $this->getParam($param) ?? '';
468
        if (empty($origin)) {
469
            return;
470
        }
471
        // FIXED bug [[fu:bar]]
472
        if (WikiTextUtil::isWikify($origin)) {
473
            return;
474
        }
475
476
        $origin = TextUtil::replaceNonBreakingSpaces($origin);
477
478 1
        $strTitle = $origin;
479 1
480
        // CORRECTING TYPO FANTASY OF SUBTITLE
481 1
482 1
        // Replace first '.' by ':' if no ': ' and no numbers around (as PHP 7.3)
483 1
        // exlude pattern "blabla... blabla"
484
        // TODO: statistics
485
486 1
        // Replace ' - ' or ' / ' (spaced!) by ' : ' if no ':' and no numbers after (as PHP 7.3 or 1939-1945)
487
        if (!mb_strpos(':', $strTitle) && preg_match('#.{6,} ?[-/] ?[^0-9)]{6,}#', $strTitle) > 0) {
488
            $strTitle = preg_replace('#(.{6,}) [-/] ([^0-9)]{6,})#', '$1 : $2', $strTitle);
489
        }
490
491
        // international typo style " : " (first occurrence)
492
        $strTitle = preg_replace('#[ ]*:[ ]*#', ' : ', $strTitle);
493
494
        if ($strTitle !== $origin) {
495
            $this->setParam($param, $strTitle);
496 47
            $this->addSummaryLog(sprintf(':%s', $param));
497
        }
498 47
    }
499 47
500 47
    protected function valideNumeroChapitre()
501 47
    {
502 47
        $value = $this->getParam('numéro chapitre');
503
        if (empty($value)) {
504
            return;
505
        }
506
        // "12" ou "VI", {{II}}, II:3
507
        if (preg_match('#^[0-9IVXL\-.:{}]+$#i', $value) > 0) {
508
            return;
509
        }
510
        // déplace vers "titre chapitre" ?
511
        if (!$this->getParam('titre chapitre')) {
512
            $this->unsetParam('numéro chapitre');
513
            $this->setParam('titre chapitre', $value);
514
        }
515
        $this->addSummaryLog('≠numéro chapitre');
516
    }
517
518
    /**
519
     * TODO move+refac
520
     * TODO CommentaireBiblioTemplate  ExtraitTemplate
521
     * Probleme {{commentaire biblio}} <> {{commentaire biblio SRL}}
522 47
     * Generate supplementary templates from obsoletes params.
523
     *
524 47
     * @throws Exception
525
     */
526
    protected function externalTemplates()
527 47
    {
528 47
        // "extrait=bla" => {{citation bloc|bla}}
529 47
        if ($this->hasParamValue('extrait')) {
530 1
            $extrait = $this->getParam('extrait');
531
            // todo bug {{citation bloc}} si "=" ou "|" dans texte de citation
532
            // Legacy : use {{début citation}} ... {{fin citation}}
533
            if (preg_match('#[=|]#', $extrait) > 0) {
534
                $this->optiTemplate->externalTemplates[] = (object)[
535 47
                    'template' => 'début citation',
536
                    '1' => '',
537
                    'raw' => '{{Début citation}}'.$extrait.'{{Fin citation}}',
538
                ];
539 47
                $this->addSummaryLog('{Début citation}');
540
                $this->notCosmetic = true;
541
            } else {
542
                // StdClass
543
                $this->optiTemplate->externalTemplates[] = (object)[
544
                    'template' => 'citation bloc',
545
                    '1' => $extrait,
546
                    'raw' => '{{Citation bloc|'.$extrait.'}}',
547 47
                ];
548
                $this->addSummaryLog('{Citation bloc}');
549 47
                $this->notCosmetic = true;
550 45
            }
551
552 2
            $this->unsetParam('extrait');
553
            $this->notCosmetic = true;
554 2
        }
555 2
556
        // "commentaire=bla" => {{Commentaire biblio|1=bla}}
557 1
        if ($this->hasParamValue('commentaire')) {
558
            $commentaire = $this->getParam('commentaire');
559
            $this->optiTemplate->externalTemplates[] = (object)[
560
                'template' => 'commentaire biblio',
561 2
                '1' => $commentaire,
562
                'raw' => '{{Commentaire biblio|'.$commentaire.'}}',
563
            ];
564
            $this->unsetParam('commentaire');
565
            $this->addSummaryLog('{commentaire}');
566
            $this->notCosmetic = true;
567 2
        }
568 2
    }
569 2
570
    // ----------------------
571 2
    // ----------------------
572
    // ----------------------
573
574
    /**
575 2
     * Date->année (nécessaire pour OuvrageComplete).
576 2
     *
577 2
     * @throws Exception
578 2
     */
579 2
    protected function moveDate2Year()
580 2
    {
581 2
        $date = $this->getParam('date') ?? false;
582 2
        if ($date) {
583
            if (preg_match('#^-?[12][0-9][0-9][0-9]$#', $date)) {
584
                $this->setParam('année', $date);
585
                $this->unsetParam('date');
586 2
                //$this->log('>année');
587
            }
588
        }
589
    }
590
591
    protected function predictFormatByPattern()
592
    {
593
        if (($value = $this->getParam('format'))) {
594
            // predict if 'format électronique'
595
            // format electronique lié au champ 'lire en ligne'
596 47
            // 2015 https://fr.wikipedia.org/wiki/Discussion_mod%C3%A8le:Ouvrage#format,_format_livre,_format_%C3%A9lectronique
597
            //            if (preg_match('#(pdf|epub|html|kindle|audio|\{\{aud|jpg)#i', $value) > 0) {
598 47
            //
599
            //                $this->setParam('format électronique', $value);
600
            //                $this->unsetParam('format');
601 47
            //                $this->log('format:électronique?');
602
            //
603 47
            //                return;
604
            //            }
605
            if (preg_match(
606 35
                    '#(ill\.|couv\.|in-[0-9]|in-fol|poche|broché|relié|{{unité|{{Dunité|[0-9]{2} ?cm|\|cm}}|vol\.|A4)#i',
607
                    $value
608
                ) > 0
609 35
            ) {
610 35
                $this->setParam('format livre', $value);
611
                $this->unsetParam('format');
612 35
                $this->addSummaryLog('format:livre?');
613
                $this->notCosmetic = true;
614 26
            }
615
            // Certainement 'format électronique'...
616 26
        }
617 26
    }
618
619 26
    /**
620
     * todo : vérif lien rouge
621
     * todo 'lien éditeur' affiché 1x par page
622
     * opti : Suppression lien éditeur si c'est l'article de l'éditeur.
623
     *
624
     * @throws Exception
625
     */
626
    protected function processEditeur()
627
    {
628
        $editeur = $this->getParam('éditeur');
629
        if (empty($editeur)) {
630 47
            return;
631
        }
632 47
633 47
        // FIX bug "GEO Art ([[Prisma Media]]) ; [[Le Monde]]"
634
        if (preg_match('#\[.*\[.*\[#', $editeur) > 0) {
635 15
            return;
636 1
        }
637 1
        // FIX bug "[[Fu|Bar]] bla" => [[Fu|Bar bla]]
638
        if (preg_match('#(.+\[\[|\]\].+)#', $editeur) > 0) {
639 1
            return;
640 1
        }
641 1
642 1
        // [[éditeur]]
643
        if (preg_match('#\[\[([^|]+)]]#', $editeur, $matches) > 0) {
644 1
            $editeurUrl = $matches[1];
645
        }
646
        // [[bla|éditeur]]
647
        if (preg_match('#\[\[([^]|]+)\|.+]]#', $editeur, $matches) > 0) {
648
            $editeurUrl = $matches[1];
649 14
        }
650
651 47
        // Todo : traitement/suppression des abréviations communes :
652
        // ['éd. de ', 'éd. du ', 'éd.', 'ed.', 'Éd. de ', 'Éd.', 'édit.', 'Édit.', '(éd.)', '(ed.)', 'Ltd.']
653 47
654 47
        $editeurStr = WikiTextUtil::unWikify($editeur);
655
        // On garde minuscule sur éditeur, pour nuance Éditeur/éditeur permettant de supprimer "éditeur"
656 15
        // ex: "éditions Milan" => "Milan"
657 15
658 15
        // Déconseillé : 'lien éditeur' (obsolete 2019)
659
        if ($this->hasParamValue('lien éditeur')) {
660
            if (empty($editeurUrl)) {
661
                $editeurUrl = $this->getParam('lien éditeur');
662
            }
663
            $this->unsetParam('lien éditeur');
664
        }
665
666
        if (empty($editeurUrl)) {
667
            $editeurUrl = $this->predictPublisherWikiTitle($editeurStr);
668
            if (!empty($editeurUrl) && $this->wikiPageTitle !== $editeurUrl) {
669
                $this->addSummaryLog('+lien éditeur');
670 47
                $this->notCosmetic = true;
671
                $this->major = true;
672 47
            }
673 47
        }
674 32
675
676
        $newEditeur = $editeurStr;
677 15
        if (!empty($editeurUrl)) {
678 2
            $newEditeur = WikiTextUtil::wikilink($editeurStr, $editeurUrl);
679
        }
680
681 13
        if ($newEditeur !== $editeur) {
682
            $this->setParam('éditeur', $newEditeur);
683 13
            $this->addSummaryLog('±éditeur');
684
            $this->notCosmetic = true;
685
        }
686
    }
687
688
    /**
689
     * todo move (cf. Article/Lien web optimizing)
690
     *
691
     * @param string $publisherName
692 13
     *
693 4
     * @return string|null
694
     */
695
    public function predictPublisherWikiTitle(string $publisherName): ?string
696
    {
697 13
        try {
698
            $data = json_decode(file_get_contents(self::PUBLISHER_FRWIKI_FILENAME), true);
699 13
        } catch (\Throwable $e) {
700 3
            $this->log->error('Catch EDITOR_TITLES_FILENAME import '.$e->getMessage());
701 3
        }
702
        if (isset($data[$publisherName])) {
703 13
            return (string)urldecode($data[$publisherName]);
704
        }
705 47
706
        return null;
707 47
    }
708
}
709