1<?php
2
3namespace dokuwiki\plugin\doi\Resolver;
4
5use dokuwiki\HTTP\DokuHTTPClient;
6
7class DoiResolver extends AbstractResolver
8{
9
10    /** @inheritdoc */
11    public function getData($id)
12    {
13        $data = $this->fetchCachedData($id);
14        $result = $this->defaultResult;
15
16        $result['id'] = $data['DOI'];
17        $result['title'] = empty($data['title']) ? $id : $data['title'];
18        $result['url'] = $data['URL'] ?? 'https://doi.org/' . $id;
19        $result['url'] = preg_replace('/^http:/', 'https:', $result['url']); // always use https
20        $result['type'] = $data['type'];
21        $result['idtype'] = 'DOI';
22
23        foreach ($data['author'] ?? $data['editor'] ?? [] as $author) {
24            $result['authors'][] = $author['given'] . ' ' . $author['family'];
25        }
26
27        $result['journal'] = $data['container-title'] ?? '';
28        $result['volume'] = $data['volume'] ?? '';
29        $result['issue'] = $data['issue'] ?? '';
30        $result['page'] = $data['page'] ?? '';
31
32        $result['published'] = $data['issued']['date-parts'][0][0] ?? '';
33        $result['publisher'] = $data['publisher'] ?? '';
34
35        return $result;
36    }
37
38    /** @inheritdoc */
39    protected function fetchData($id)
40    {
41        $http = new DokuHTTPClient();
42        $http->headers['Accept'] = 'application/vnd.citationstyles.csl+json';
43        $json = $http->get('https://doi.org/' . $id);
44        if (!$json) throw new \Exception('Could not fetch data from doi.org. ' . $http->error);
45        return json_decode($json, true);
46    }
47
48    /** @inheritdoc */
49    public function getFallbackURL($id)
50    {
51        return 'https://doi.org/' . $id;
52    }
53
54    /** @inheritdoc */
55    public function cleanID($id)
56    {
57        return trim($id, ' /.');
58    }
59}
60