1307c6980SAndreas Gohr<?php 2307c6980SAndreas Gohr 3307c6980SAndreas Gohrnamespace dokuwiki\plugin\doi\Resolver; 4307c6980SAndreas Gohr 5307c6980SAndreas Gohruse dokuwiki\HTTP\DokuHTTPClient; 6307c6980SAndreas Gohr 7307c6980SAndreas Gohr/** 8307c6980SAndreas Gohr * ISBN resolver using Google Books API 9307c6980SAndreas Gohr */ 10307c6980SAndreas Gohrclass IsbnGoogleBooksResolver extends AbstractIsbnResolver 11307c6980SAndreas Gohr{ 12307c6980SAndreas Gohr 13307c6980SAndreas Gohr /** @inheritdoc */ 14307c6980SAndreas Gohr public function getData($id) 15307c6980SAndreas Gohr { 16307c6980SAndreas Gohr $data = $this->fetchCachedData($id); 17307c6980SAndreas Gohr $data = $data['volumeInfo']; 18307c6980SAndreas Gohr $result = $this->defaultResult; 19307c6980SAndreas Gohr 20307c6980SAndreas Gohr $result['url'] = $data['canonicalVolumeLink']; 21307c6980SAndreas Gohr foreach ($data['industryIdentifiers'] as $identifier) { 22307c6980SAndreas Gohr if ($identifier['type'] === 'ISBN_13') { 23307c6980SAndreas Gohr $result['id'] = $identifier['identifier']; 24307c6980SAndreas Gohr break; 25307c6980SAndreas Gohr } 26307c6980SAndreas Gohr if ($identifier['type'] === 'ISBN_10') { 27307c6980SAndreas Gohr $result['id'] = $identifier['identifier']; 28307c6980SAndreas Gohr } 29307c6980SAndreas Gohr } 30307c6980SAndreas Gohr 31307c6980SAndreas Gohr $result['title'] = $data['title']; 32307c6980SAndreas Gohr if (isset($data['subtitle'])) $result['title'] .= ': ' . $data['subtitle']; 339f8980aeSAndreas Gohr if(empty($result['title'])) $result['title'] = $id; 34307c6980SAndreas Gohr 35307c6980SAndreas Gohr $result['authors'] = $data['authors'] ?? []; 36307c6980SAndreas Gohr 37307c6980SAndreas Gohr $published = $data['publishedDate'] ?? ''; 38307c6980SAndreas Gohr if (preg_match('/\b(\d{4})\b/', $published, $m)) { 39307c6980SAndreas Gohr $result['published'] = $m[1]; 40307c6980SAndreas Gohr } 41307c6980SAndreas Gohr 42307c6980SAndreas Gohr $result['publisher'] = $data['publisher'] ?? ''; 43307c6980SAndreas Gohr 44*17101be4SAndreas Gohr $result['image'] = $data['imageLinks']['thumbnail'] ?? ''; 45*17101be4SAndreas Gohr 46*17101be4SAndreas Gohr if($result['image']) { 47*17101be4SAndreas Gohr $result['image'] .= '?.jpg'; // force jpg extension 48*17101be4SAndreas Gohr } 49*17101be4SAndreas Gohr 50307c6980SAndreas Gohr return $result; 51307c6980SAndreas Gohr } 52307c6980SAndreas Gohr 53307c6980SAndreas Gohr /** @inheritdoc */ 54307c6980SAndreas Gohr protected function fetchData($id) 55307c6980SAndreas Gohr { 56307c6980SAndreas Gohr $http = new DokuHTTPClient(); 57307c6980SAndreas Gohr $json = $http->get('https://www.googleapis.com/books/v1/volumes?q=isbn:' . $id); 58307c6980SAndreas Gohr if (!$json) throw new \Exception('Could not fetch data from Google Books. ' . $http->error); 59307c6980SAndreas Gohr $data = json_decode($json, true); 60307c6980SAndreas Gohr if (!isset($data['items'])) throw new \Exception('No ISBN results found at Google Books.'); 61307c6980SAndreas Gohr return $data['items'][0]; // first entry 62307c6980SAndreas Gohr } 63307c6980SAndreas Gohr} 64