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
|
|
|
|
14
|
|
|
use App\Domain\IsbnFacade; |
15
|
|
|
use Exception; |
16
|
|
|
use Throwable; |
17
|
|
|
|
18
|
|
|
class OuvrageIsbnHandler extends AbstractOuvrageHandler |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Refac complexity (lines, 20 conditions) |
22
|
|
|
* Validate or correct ISBN. |
23
|
|
|
* @throws Exception |
24
|
|
|
*/ |
25
|
|
|
public function handle() |
26
|
|
|
{ |
27
|
|
|
$isbn = $this->getParam('isbn') ?? ''; |
28
|
|
|
if (empty($isbn)) { |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// ISBN-13 à partir de 2007 |
33
|
|
|
$year = $this->findBookYear(); |
34
|
|
|
if ($year !== null && $year < 2007 && 10 === strlen($this->stripIsbn($isbn))) { |
35
|
|
|
// juste mise en forme ISBN-10 pour 'isbn' |
36
|
|
|
try { |
37
|
|
|
$isbnMachine = new IsbnFacade($isbn); |
38
|
|
|
// skip trigger_error() for deprecated method |
39
|
|
|
@$isbnMachine->validate(); |
|
|
|
|
40
|
|
|
$isbn10pretty = $isbnMachine->format('ISBN-10'); |
|
|
|
|
41
|
|
|
if ($isbn10pretty !== $isbn) { |
42
|
|
|
$this->setParam('isbn', $isbn10pretty); |
43
|
|
|
$this->addSummaryLog('ISBN10'); |
44
|
|
|
// $this->notCosmetic = true; |
45
|
|
|
} |
46
|
|
|
} catch (Throwable $e) { |
47
|
|
|
// ISBN not validated |
48
|
|
|
$this->setParam( |
49
|
|
|
'isbn invalide', |
50
|
|
|
sprintf('%s %s', $isbn, $e->getMessage() ?? '') |
51
|
|
|
); |
52
|
|
|
$this->addSummaryLog(sprintf('ISBN invalide: %s', $e->getMessage())); |
53
|
|
|
$this->optiStatus->setNotCosmetic(true); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
$isbnMachine = new IsbnFacade($isbn); |
61
|
|
|
// skip trigger_error() for deprecated method |
62
|
|
|
@$isbnMachine->validate(); |
|
|
|
|
63
|
|
|
$isbn13 = @$isbnMachine->format('ISBN-13'); |
|
|
|
|
64
|
|
|
} catch (Throwable $e) { |
65
|
|
|
// ISBN not validated |
66
|
|
|
// TODO : bot ISBN invalide (queue, message PD...) |
67
|
|
|
$this->setParam( |
68
|
|
|
'isbn invalide', |
69
|
|
|
sprintf('%s %s', $isbn, $e->getMessage() ?? '') |
70
|
|
|
); |
71
|
|
|
$this->addSummaryLog(sprintf('ISBN invalide: %s', $e->getMessage())); |
72
|
|
|
$this->optiStatus->setNotCosmetic(true); |
73
|
|
|
|
74
|
|
|
// TODO log file ISBNinvalide |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Si $isbn13 et 'isbn2' correspond à ISBN-13 => suppression |
79
|
|
|
if ($this->hasParamValue('isbn2') |
80
|
|
|
&& $this->stripIsbn($this->getParam('isbn2')) === $this->stripIsbn($isbn13) |
|
|
|
|
81
|
|
|
) { |
82
|
|
|
$this->unsetParam('isbn2'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// ISBN-10 ? |
86
|
|
|
$stripIsbn = $this->stripIsbn($isbn); |
87
|
|
|
if (10 === mb_strlen($stripIsbn)) { |
88
|
|
|
// ajout des tirets |
89
|
|
|
$isbn10pretty = $isbn; |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
$isbn10pretty = $isbnMachine->format('ISBN-10'); |
|
|
|
|
93
|
|
|
} catch (Throwable $e) { |
94
|
|
|
unset($e); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// archivage ISBN-10 dans 'isbn2' |
98
|
|
|
if (!$this->getParam('isbn2')) { |
99
|
|
|
$this->setParam('isbn2', $isbn10pretty); |
100
|
|
|
} |
101
|
|
|
// sinon dans 'isbn3' |
102
|
|
|
if ($this->hasParamValue('isbn2') |
103
|
|
|
&& $this->stripIsbn($this->getParam('isbn2')) !== $stripIsbn |
104
|
|
|
&& empty($this->getParam('isbn3')) |
105
|
|
|
) { |
106
|
|
|
$this->setParam('isbn3', $isbn10pretty); |
107
|
|
|
} |
108
|
|
|
// delete 'isbn10' (en attendant modification modèle) |
109
|
|
|
if ($this->hasParamValue('isbn10') && $this->stripIsbn($this->getParam('isbn10')) === $stripIsbn) { |
110
|
|
|
$this->unsetParam('isbn10'); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// ISBN correction |
115
|
|
|
if ($isbn13 !== $isbn) { |
116
|
|
|
$this->setParam('isbn', $isbn13); |
117
|
|
|
$this->addSummaryLog('ISBN'); |
118
|
|
|
// $this->notCosmetic = true; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Find year of book publication. |
124
|
|
|
*/ |
125
|
|
|
protected function findBookYear(): ?int |
126
|
|
|
{ |
127
|
|
|
$annee = $this->getParam('année'); |
128
|
|
|
if (!empty($annee) && is_numeric($annee)) { |
129
|
|
|
return (int)$annee; |
130
|
|
|
} |
131
|
|
|
$date = $this->getParam('date'); |
132
|
|
|
if ($date && preg_match('#[^0-9]?([12]\d\d\d)[^0-9]?#', $date, $matches) > 0) { |
133
|
|
|
return (int)$matches[1]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function stripIsbn(string $isbn): string |
140
|
|
|
{ |
141
|
|
|
return trim(preg_replace('#[^0-9Xx]#', '', $isbn)); |
142
|
|
|
} |
143
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: