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
|
|
|
*/ /** @noinspection PhpUndefinedFieldInspection */ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace App\Domain\Publisher; |
11
|
|
|
|
12
|
|
|
use App\Domain\Enums\Language; |
13
|
|
|
use DomainException; |
14
|
|
|
use Scriptotek\GoogleBooks\Volume; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Google Books API data mapping. |
18
|
|
|
* Doc : https://developers.google.com/books/docs/v1/reference/volumes |
19
|
|
|
* Class GoogleBookMapper. |
20
|
|
|
*/ |
21
|
|
|
class GoogleBookMapper extends AbstractBookMapper implements MapperInterface |
22
|
|
|
{ |
23
|
|
|
// raw URL or wiki-template ? |
24
|
|
|
public const MODE_RAW_URL = true; |
25
|
|
|
|
26
|
|
|
public const GOOGLE_URL_REPLACE = 'https://books.google.com/books?id=%s&printsec=frontcover'; |
27
|
|
|
|
28
|
|
|
// sous-titre non ajoutés : |
29
|
|
|
public const SUBTITLE_FILTER = ['roman', 'récit', 'poèmes', 'biographie']; |
30
|
|
|
/** |
31
|
|
|
* @var bool|null |
32
|
|
|
*/ |
33
|
|
|
private $mapLanguageData = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Volume|null $volume |
37
|
|
|
* |
38
|
|
|
* @noinspection PhpParameterNameChangedDuringInheritanceInspection |
39
|
|
|
*/ |
40
|
|
|
public function process($volume): array |
41
|
|
|
{ |
42
|
|
|
if (!$volume instanceof Volume) { |
43
|
|
|
throw new DomainException('dataObject to process is not a googleBook Volume'); |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
return [ |
47
|
|
|
'langue' => $this->langFilterByIsbn($volume), |
48
|
2 |
|
'auteur1' => $volume->authors[0] ?? null, |
49
|
2 |
|
'auteur2' => $volume->authors[1] ?? null, |
50
|
2 |
|
'auteur3' => $volume->authors[2] ?? null, |
51
|
2 |
|
'titre' => $volume->title, |
52
|
2 |
|
'sous-titre' => $this->filterSubtitle($volume), |
53
|
2 |
|
'année' => $this->convertDate2Year($volume->publishedDate ?? null), |
54
|
2 |
|
'pages totales' => (string)$volume->pageCount ?? null, |
55
|
2 |
|
'isbn' => $this->convertIsbn($volume), |
56
|
2 |
|
'présentation en ligne' => $this->presentationEnLigne($volume), |
57
|
2 |
|
'lire en ligne' => $this->lireEnLigne($volume), |
58
|
2 |
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Use the inconstant 'language' ? |
63
|
|
|
* |
64
|
|
|
* @param bool $useLanguage |
65
|
|
|
*/ |
66
|
|
|
public function mapLanguageData(bool $useLanguage) |
67
|
1 |
|
{ |
68
|
|
|
$this->mapLanguageData = $useLanguage; |
69
|
1 |
|
} |
70
|
1 |
|
|
71
|
|
|
/** |
72
|
|
|
* @param $volume |
73
|
|
|
* |
74
|
|
|
* @return string|null |
75
|
|
|
*/ |
76
|
|
|
private function filterSubtitle($volume): ?string |
77
|
2 |
|
{ |
78
|
|
|
// "biographie" ? |
79
|
|
|
if (empty($volume->subtitle) || in_array(mb_strtolower($volume->subtitle), self::SUBTITLE_FILTER)) { |
80
|
2 |
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (strlen($volume->subtitle) > 50) { |
84
|
2 |
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $volume->subtitle; |
88
|
2 |
|
} |
89
|
|
|
|
90
|
|
|
private function convertDate2Year(?string $data): ?string |
91
|
|
|
{ |
92
|
|
|
if (null === $data) { |
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
if (preg_match('/[^0-9]?([12]\d{3})[^0-9]?/', $data, $matches) > 0) { |
96
|
2 |
|
return $matches[1]; |
97
|
|
|
} |
98
|
2 |
|
|
99
|
|
|
return null; |
100
|
|
|
} |
101
|
2 |
|
|
102
|
2 |
|
/** |
103
|
|
|
* @param Volume $volume |
104
|
|
|
* |
105
|
|
|
* @return string|null |
106
|
|
|
*/ |
107
|
|
|
private function convertIsbn(Volume $volume): ?string |
108
|
|
|
{ |
109
|
|
|
if (!isset($volume->industryIdentifiers)) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
// so isbn-13 replace isbn-10 |
113
|
2 |
|
// todo refac algo (if 2x isbn13?) |
114
|
|
|
$isbn = null; |
115
|
2 |
|
$ids = (array)$volume->industryIdentifiers; |
116
|
|
|
foreach ($ids as $id) { |
117
|
|
|
if (!$isbn && in_array($id->type, ['ISBN_10', 'ISBN_13'])) { |
118
|
|
|
$isbn = $id->identifier; |
119
|
|
|
} |
120
|
2 |
|
if ('ISBN_13' === $id->type) { |
121
|
2 |
|
$isbn = $id->identifier; |
122
|
2 |
|
} |
123
|
2 |
|
} |
124
|
2 |
|
|
125
|
|
|
return $isbn; |
126
|
2 |
|
} |
127
|
2 |
|
|
128
|
|
|
/** |
129
|
|
|
* @param Volume $volume |
130
|
|
|
* |
131
|
2 |
|
* @return string|null |
132
|
|
|
*/ |
133
|
|
|
private function presentationEnLigne(Volume $volume): ?string |
134
|
|
|
{ |
135
|
|
|
if (empty($volume->id) || $volume->accessInfo->viewability != 'PARTIAL') { |
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
2 |
|
return $this->returnGoogleRef($volume); |
140
|
|
|
} |
141
|
2 |
|
|
142
|
2 |
|
/** |
143
|
|
|
* @param Volume $volume |
144
|
|
|
* |
145
|
|
|
* @return string|null |
146
|
|
|
*/ |
147
|
|
|
private function lireEnLigne(Volume $volume): ?string |
148
|
|
|
{ |
149
|
|
|
if (empty($volume->id) || $volume->accessInfo->viewability != 'ALL_PAGES') { |
150
|
|
|
return null; |
151
|
|
|
} |
152
|
|
|
|
153
|
2 |
|
return $this->returnGoogleRef($volume); |
154
|
|
|
} |
155
|
2 |
|
|
156
|
2 |
|
/** |
157
|
|
|
* @param Volume $volume |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
private function returnGoogleRef(Volume $volume): string |
162
|
|
|
{ |
163
|
|
|
if (self::MODE_RAW_URL) { |
164
|
|
|
return sprintf(self::GOOGLE_URL_REPLACE, $volume->id); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return sprintf('{{Google Livres|%s}}', $volume->id); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* /!\ Google Books language data not consistant ! |
172
|
|
|
* set $mapLanguageData to true to use that data in mapping. |
173
|
|
|
* |
174
|
|
|
* @param Volume $volume |
175
|
|
|
* |
176
|
|
|
* @return string|null |
177
|
|
|
*/ |
178
|
|
|
private function langFilterByIsbn(Volume $volume): ?string |
179
|
|
|
{ |
180
|
|
|
if ($this->mapLanguageData !== true) { |
181
|
|
|
return null; |
182
|
|
|
} |
183
|
|
|
// $isbn = $this->convertIsbn($volume); |
184
|
2 |
|
// if ($isbn) { |
185
|
|
|
// try { |
186
|
2 |
|
// $isbnMachine = new IsbnFacade($isbn); |
187
|
1 |
|
// @$isbnMachine->validate(); |
188
|
|
|
// $isbnLang = $isbnMachine->getCountryShortName(); |
189
|
|
|
// } catch (\Throwable $e) { |
190
|
|
|
// unset($e); |
191
|
|
|
// } |
192
|
|
|
// } |
193
|
|
|
// if (isset($isbnLang)) { |
194
|
|
|
// echo "(ISBN lang: ".$isbnLang.")\n"; |
195
|
|
|
// } else { |
196
|
|
|
// echo "(no ISBN lang)\n"; |
197
|
|
|
// } |
198
|
|
|
|
199
|
|
|
$gooLang = $volume->language ?? null; |
200
|
|
|
// filtering lang because seems inconsistant |
201
|
|
|
// if (isset($isbnLang) && !empty($gooLang) && $gooLang !== $isbnLang) { |
202
|
|
|
// echo sprintf( |
203
|
|
|
// "*** Filtering Google langue=%s because ISBN langue=%s ", |
204
|
|
|
// $gooLang, |
205
|
1 |
|
// $isbnLang |
206
|
|
|
// ); |
207
|
|
|
// |
208
|
|
|
// return null; |
209
|
|
|
// } |
210
|
|
|
|
211
|
|
|
return Language::all2wiki($gooLang); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|