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\Domain\WikiOptimizer\Handlers; |
11
|
|
|
|
12
|
|
|
use App\Domain\Models\Wiki\OuvrageTemplate; |
13
|
|
|
use App\Domain\OptiStatus; |
14
|
|
|
use App\Domain\Utils\WikiTextUtil; |
15
|
|
|
use App\Domain\WikiOptimizer\OuvrageOptimize; |
16
|
|
|
use Exception; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Throwable; |
19
|
|
|
|
20
|
|
|
class EditeurHandler implements OptimizeHandlerInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var OuvrageTemplate |
24
|
|
|
*/ |
25
|
|
|
protected $ouvrage; |
26
|
|
|
/** |
27
|
|
|
* @var \App\Domain\OptiStatus |
28
|
|
|
*/ |
29
|
|
|
protected $optiStatus; |
30
|
|
|
/** |
31
|
|
|
* @var LoggerInterface |
32
|
|
|
*/ |
33
|
|
|
protected $log; |
34
|
|
|
/** |
35
|
|
|
* @var string|null |
36
|
|
|
*/ |
37
|
|
|
protected $articleTitle; |
38
|
|
|
|
39
|
|
|
public function __construct(OuvrageTemplate $ouvrage, OptiStatus $summary, ?string $articleTitle, LoggerInterface $log) |
40
|
|
|
{ |
41
|
|
|
$this->ouvrage = $ouvrage; |
42
|
|
|
$this->optiStatus = $summary; |
43
|
|
|
$this->log = $log; |
44
|
|
|
$this->articleTitle = $articleTitle; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* todo : vérif lien rouge |
49
|
|
|
* todo 'lien éditeur' affiché 1x par page |
50
|
|
|
* opti : Suppression lien éditeur si c'est l'article de l'éditeur. |
51
|
|
|
* @throws Exception |
52
|
|
|
*/ |
53
|
|
|
public function handle() |
54
|
|
|
{ |
55
|
|
|
$editeur = $this->ouvrage->getParam('éditeur'); |
56
|
|
|
if (empty($editeur)) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// FIX bug "GEO Art ([[Prisma Media]]) ; [[Le Monde]]" |
61
|
|
|
if (preg_match('#\[.*\[.*\[#', $editeur) > 0) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
// FIX bug "[[Fu|Bar]] bla" => [[Fu|Bar bla]] |
65
|
|
|
if (preg_match('#(.+\[\[|\]\].+)#', $editeur) > 0) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// [[éditeur]] |
70
|
|
|
if (preg_match('#\[\[([^|]+)]]#', $editeur, $matches) > 0) { |
71
|
|
|
$editeurUrl = $matches[1]; |
72
|
|
|
} |
73
|
|
|
// [[bla|éditeur]] |
74
|
|
|
if (preg_match('#\[\[([^]|]+)\|.+]]#', $editeur, $matches) > 0) { |
75
|
|
|
$editeurUrl = $matches[1]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Todo : traitement/suppression des abréviations communes : |
79
|
|
|
// ['éd. de ', 'éd. du ', 'éd.', 'ed.', 'Éd. de ', 'Éd.', 'édit.', 'Édit.', '(éd.)', '(ed.)', 'Ltd.'] |
80
|
|
|
|
81
|
|
|
$editeurStr = WikiTextUtil::unWikify($editeur); |
82
|
|
|
// On garde minuscule sur éditeur, pour nuance Éditeur/éditeur permettant de supprimer "éditeur" |
83
|
|
|
// ex: "éditions Milan" => "Milan" |
84
|
|
|
|
85
|
|
|
// Déconseillé : 'lien éditeur' (obsolete 2019) |
86
|
|
|
if ($this->ouvrage->hasParamValue('lien éditeur')) { |
87
|
|
|
if (empty($editeurUrl)) { |
88
|
|
|
$editeurUrl = $this->ouvrage->getParam('lien éditeur'); |
89
|
|
|
} |
90
|
|
|
$this->ouvrage->unsetParam('lien éditeur'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// TODO check history |
94
|
|
|
if (empty($editeurUrl)) { |
95
|
|
|
$editeurUrl = $this->predictPublisherWikiTitle($editeurStr); |
96
|
|
|
if (!empty($editeurUrl) && $this->articleTitle !== $editeurUrl) { |
97
|
|
|
$this->optiStatus->addSummaryLog('+lien éditeur'); |
98
|
|
|
$this->optiStatus->setNotCosmetic(true); |
99
|
|
|
$this->optiStatus->setMajor(true); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$newEditeur = $editeurStr; |
104
|
|
|
if (!empty($editeurUrl)) { |
105
|
|
|
$newEditeur = WikiTextUtil::wikilink($editeurStr, $editeurUrl); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($newEditeur !== $editeur) { |
109
|
|
|
$this->ouvrage->setParam('éditeur', $newEditeur); |
110
|
|
|
$this->optiStatus->addSummaryLog('±éditeur'); |
111
|
|
|
$this->optiStatus->setNotCosmetic(true); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* todo move (cf. Article/Lien web optimizing) |
117
|
|
|
* |
118
|
|
|
* |
119
|
|
|
*/ |
120
|
|
|
public function predictPublisherWikiTitle(string $publisherName): ?string |
121
|
|
|
{ |
122
|
|
|
$data = []; |
|
|
|
|
123
|
|
|
try { |
124
|
|
|
$data = json_decode( |
125
|
|
|
file_get_contents(OuvrageOptimize::PUBLISHER_FRWIKI_FILENAME), |
126
|
|
|
true, |
127
|
|
|
512, |
128
|
|
|
JSON_THROW_ON_ERROR |
129
|
|
|
); |
130
|
|
|
} catch (Throwable $e) { |
131
|
|
|
$this->log->error('Catch EDITOR_TITLES_FILENAME import ' . $e->getMessage()); |
132
|
|
|
} |
133
|
|
|
if (isset($data[$publisherName])) { |
134
|
|
|
return (string)urldecode((string) $data[$publisherName]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
} |