1307c6980SAndreas Gohr<?php 2307c6980SAndreas Gohr 3307c6980SAndreas Gohrnamespace dokuwiki\plugin\doi\Resolver; 4307c6980SAndreas Gohr 5307c6980SAndreas Gohruse dokuwiki\HTTP\DokuHTTPClient; 6307c6980SAndreas Gohr 7307c6980SAndreas Gohrclass DoiResolver extends AbstractResolver 8307c6980SAndreas Gohr{ 9307c6980SAndreas Gohr 10307c6980SAndreas Gohr /** @inheritdoc */ 11307c6980SAndreas Gohr public function getData($id) 12307c6980SAndreas Gohr { 13307c6980SAndreas Gohr $data = $this->fetchCachedData($id); 14307c6980SAndreas Gohr $result = $this->defaultResult; 15307c6980SAndreas Gohr 16307c6980SAndreas Gohr $result['id'] = $data['DOI']; 17*9f8980aeSAndreas Gohr $result['title'] = empty($data['title']) ? $id : $data['title']; 18307c6980SAndreas Gohr $result['url'] = $data['URL'] ?? 'https://doi.org/' . $id; 19307c6980SAndreas Gohr $result['type'] = $data['type']; 20307c6980SAndreas Gohr $result['idtype'] = 'DOI'; 21307c6980SAndreas Gohr 22307c6980SAndreas Gohr foreach ($data['author'] ?? $data['editor'] ?? [] as $author) { 23307c6980SAndreas Gohr $result['authors'][] = $author['given'] . ' ' . $author['family']; 24307c6980SAndreas Gohr } 25307c6980SAndreas Gohr 26307c6980SAndreas Gohr $result['journal'] = $data['container-title'] ?? ''; 27307c6980SAndreas Gohr $result['volume'] = $data['volume'] ?? ''; 28307c6980SAndreas Gohr $result['issue'] = $data['issue'] ?? ''; 29307c6980SAndreas Gohr $result['page'] = $data['page'] ?? ''; 30307c6980SAndreas Gohr 31307c6980SAndreas Gohr $result['published'] = $data['issued']['date-parts'][0][0] ?? ''; 32307c6980SAndreas Gohr $result['publisher'] = $data['publisher'] ?? ''; 33307c6980SAndreas Gohr 34307c6980SAndreas Gohr return $result; 35307c6980SAndreas Gohr } 36307c6980SAndreas Gohr 37307c6980SAndreas Gohr /** @inheritdoc */ 38307c6980SAndreas Gohr protected function fetchData($id) 39307c6980SAndreas Gohr { 40307c6980SAndreas Gohr $http = new DokuHTTPClient(); 41307c6980SAndreas Gohr $http->headers['Accept'] = 'application/vnd.citationstyles.csl+json'; 42307c6980SAndreas Gohr $json = $http->get('https://doi.org/' . $id); 43307c6980SAndreas Gohr if (!$json) throw new \Exception('Could not fetch data from doi.org. ' . $http->error); 44307c6980SAndreas Gohr return json_decode($json, true); 45307c6980SAndreas Gohr } 46307c6980SAndreas Gohr 47307c6980SAndreas Gohr /** @inheritdoc */ 48307c6980SAndreas Gohr public function getFallbackURL($id) 49307c6980SAndreas Gohr { 50307c6980SAndreas Gohr return 'https://doi.org/' . $id; 51307c6980SAndreas Gohr } 52307c6980SAndreas Gohr 53307c6980SAndreas Gohr /** @inheritdoc */ 54307c6980SAndreas Gohr public function cleanID($id) 55307c6980SAndreas Gohr { 56307c6980SAndreas Gohr return trim($id, ' /.'); 57307c6980SAndreas Gohr } 58307c6980SAndreas Gohr} 59