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 $message = $message['details']; 20 21 $result['id'] = $message['isbn_13'][0] ?? $message['isbn_10'][0] ?? ''; 22 $result['title'] = $message['full_title'] ?? $message['title'] ?? $id; 23 24 $result['authors'] = array_map(function ($author) { 25 return $author['name']; 26 }, $message['authors'] ?? []); 27 28 $published = $message['publish_date'] ?? ''; 29 if (preg_match('/\b(\d{4})\b/', $published, $m)) { 30 $result['published'] = $m[1]; 31 } 32 $result['publisher'] = $message['publishers'][0] ?? ''; 33 34 return $result; 35 } 36 37 /** @inheritdoc */ 38 protected function fetchData($id) 39 { 40 $http = new DokuHTTPClient(); 41 $json = $http->get('https://openlibrary.org/api/books?jscmd=details&format=json&bibkeys=ISBN:' . $id); 42 if (!$json) throw new \Exception('Could not fetch data from Open Library. ' . $http->error); 43 $data = json_decode($json, true); 44 if (!count($data)) throw new \Exception('No ISBN results found at Open Library.'); 45 return array_shift($data); // first entry 46 } 47} 48