1<?php 2 3namespace dokuwiki\plugin\doi\Resolver; 4 5use dokuwiki\HTTP\DokuHTTPClient; 6 7/** 8 * ISBN resolver using the Open Library API 9 */ 10class IsbnOpenLibraryResolver extends AbstractIsbnResolver 11{ 12 /** @inheritdoc */ 13 public function getData($id) 14 { 15 $message = $this->fetchCachedData($id); 16 $result = $this->defaultResult; 17 18 $result['url'] = $message['info_url']; 19 $result['image'] = $message['thumbnail_url'] ?? ''; 20 $message = $message['details']; 21 22 $result['id'] = $message['isbn_13'][0] ?? $message['isbn_10'][0] ?? ''; 23 $result['title'] = $message['full_title'] ?? $message['title'] ?? $id; 24 if(empty($result['title'])) $result['title'] = $id; 25 26 $result['authors'] = array_map(function ($author) { 27 return $author['name']; 28 }, $message['authors'] ?? []); 29 30 $published = $message['publish_date'] ?? ''; 31 if (preg_match('/\b(\d{4})\b/', $published, $m)) { 32 $result['published'] = $m[1]; 33 } 34 $result['publisher'] = $message['publishers'][0] ?? ''; 35 36 return $result; 37 } 38 39 /** @inheritdoc */ 40 protected function fetchData($id) 41 { 42 $http = new DokuHTTPClient(); 43 $json = $http->get('https://openlibrary.org/api/books?jscmd=details&format=json&bibkeys=ISBN:' . $id); 44 if (!$json) throw new \Exception('Could not fetch data from Open Library. ' . $http->error); 45 $data = json_decode($json, true); 46 if (!count($data)) throw new \Exception('No ISBN results found at Open Library.'); 47 return array_shift($data); // first entry 48 } 49} 50