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