xref: /plugin/doi/Resolver/DoiResolver.php (revision 9f8980aeb04b800e022638072fad6807c450ae23)
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['type'] = $data['type'];
20        $result['idtype'] = 'DOI';
21
22        foreach ($data['author'] ?? $data['editor'] ?? [] as $author) {
23            $result['authors'][] = $author['given'] . ' ' . $author['family'];
24        }
25
26        $result['journal'] = $data['container-title'] ?? '';
27        $result['volume'] = $data['volume'] ?? '';
28        $result['issue'] = $data['issue'] ?? '';
29        $result['page'] = $data['page'] ?? '';
30
31        $result['published'] = $data['issued']['date-parts'][0][0] ?? '';
32        $result['publisher'] = $data['publisher'] ?? '';
33
34        return $result;
35    }
36
37    /** @inheritdoc */
38    protected function fetchData($id)
39    {
40        $http = new DokuHTTPClient();
41        $http->headers['Accept'] = 'application/vnd.citationstyles.csl+json';
42        $json = $http->get('https://doi.org/' . $id);
43        if (!$json) throw new \Exception('Could not fetch data from doi.org. ' . $http->error);
44        return json_decode($json, true);
45    }
46
47    /** @inheritdoc */
48    public function getFallbackURL($id)
49    {
50        return 'https://doi.org/' . $id;
51    }
52
53    /** @inheritdoc */
54    public function cleanID($id)
55    {
56        return trim($id, ' /.');
57    }
58}
59