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