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