Passed
Branch master (ee24dc)
by Dispositif
03:19 queued 39s
created

DistinguishAuthorsHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 27
c 1
b 0
f 0
dl 0
loc 52
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 43 7
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
11
namespace App\Domain\WikiOptimizer\Handlers;
12
13
use App\Domain\PredictAuthors;
14
use App\Domain\Utils\WikiTextUtil;
15
use Exception;
16
17
class DistinguishAuthorsHandler extends AbstractOuvrageHandler
18
{
19
    /**
20
     * Detect and correct multiple authors in same parameter.
21
     * Like "auteurs=J. M. Waller, M. Bigger, R. J. Hillocks".
22
     *
23
     *
24
     * @throws Exception
25
     */
26
    public function handle()
27
    {
28
        // merge params of author 1
29
        $auteur1 = $this->ouvrage->getParam('auteur') ?? '';
30
        $auteur1 .= $this->ouvrage->getParam('auteurs') ?? '';
31
        $auteur1 .= $this->ouvrage->getParam('prénom1') ?? '';
32
        $auteur1 .= ' ' . $this->ouvrage->getParam('nom1') ?? '';
33
        $auteur1 = trim($auteur1);
34
        // of authors 2
35
        $auteur2 = $this->ouvrage->getParam('auteur2') ?? '';
36
        $auteur2 .= $this->ouvrage->getParam('prénom2') ?? '';
37
        $auteur2 .= ' ' . $this->ouvrage->getParam('nom2') ?? '';
38
        $auteur2 = trim($auteur2);
39
40
        // skip if wikilink in author
41
        if (empty($auteur1) || WikiTextUtil::isWikify($auteur1)) {
42
            return;
43
        }
44
45
        $machine = new PredictAuthors();
46
        $res = $machine->predictAuthorNames($auteur1);
47
48
        if (1 === count((array)$res)) {
49
            // auteurs->auteur?
50
            return;
51
        }
52
        // Many authors... and empty "auteur2"
53
        if (count((array)$res) >= 2 && empty($auteur2)) {
54
            // delete author-params
55
            array_map(
56
                function ($param) {
57
                    $this->ouvrage->unsetParam($param);
58
                },
59
                ['auteur', 'auteurs', 'prénom1', 'nom1']
60
            );
61
            // iterate and edit new values
62
            $count = count((array)$res);
63
            for ($i = 0; $i < $count; ++$i) {
64
                $this->ouvrage->setParam(sprintf('auteur%s', $i + 1), $res[$i]);
65
            }
66
            $this->optiStatus->addSummaryLog('distinction auteurs');
67
            $this->optiStatus->setMajor(true);
68
            $this->optiStatus->setNotCosmetic(true);
69
        }
70
    }
71
}