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