xref: /plugin/doi/Resolver/DoiResolver.php (revision 6469567fc4aa5692ab05e61820957a895bd01449)
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'];
179f8980aeSAndreas Gohr        $result['title'] = empty($data['title']) ? $id : $data['title'];
18307c6980SAndreas Gohr        $result['url'] = $data['URL'] ?? 'https://doi.org/' . $id;
19*6469567fSAndreas Gohr        $result['url'] = preg_replace('/^http:/', 'https:', $result['url']); // always use https
20307c6980SAndreas Gohr        $result['type'] = $data['type'];
21307c6980SAndreas Gohr        $result['idtype'] = 'DOI';
22307c6980SAndreas Gohr
23307c6980SAndreas Gohr        foreach ($data['author'] ?? $data['editor'] ?? [] as $author) {
24307c6980SAndreas Gohr            $result['authors'][] = $author['given'] . ' ' . $author['family'];
25307c6980SAndreas Gohr        }
26307c6980SAndreas Gohr
27307c6980SAndreas Gohr        $result['journal'] = $data['container-title'] ?? '';
28307c6980SAndreas Gohr        $result['volume'] = $data['volume'] ?? '';
29307c6980SAndreas Gohr        $result['issue'] = $data['issue'] ?? '';
30307c6980SAndreas Gohr        $result['page'] = $data['page'] ?? '';
31307c6980SAndreas Gohr
32307c6980SAndreas Gohr        $result['published'] = $data['issued']['date-parts'][0][0] ?? '';
33307c6980SAndreas Gohr        $result['publisher'] = $data['publisher'] ?? '';
34307c6980SAndreas Gohr
35307c6980SAndreas Gohr        return $result;
36307c6980SAndreas Gohr    }
37307c6980SAndreas Gohr
38307c6980SAndreas Gohr    /** @inheritdoc */
39307c6980SAndreas Gohr    protected function fetchData($id)
40307c6980SAndreas Gohr    {
41307c6980SAndreas Gohr        $http = new DokuHTTPClient();
42307c6980SAndreas Gohr        $http->headers['Accept'] = 'application/vnd.citationstyles.csl+json';
43307c6980SAndreas Gohr        $json = $http->get('https://doi.org/' . $id);
44307c6980SAndreas Gohr        if (!$json) throw new \Exception('Could not fetch data from doi.org. ' . $http->error);
45307c6980SAndreas Gohr        return json_decode($json, true);
46307c6980SAndreas Gohr    }
47307c6980SAndreas Gohr
48307c6980SAndreas Gohr    /** @inheritdoc */
49307c6980SAndreas Gohr    public function getFallbackURL($id)
50307c6980SAndreas Gohr    {
51307c6980SAndreas Gohr        return 'https://doi.org/' . $id;
52307c6980SAndreas Gohr    }
53307c6980SAndreas Gohr
54307c6980SAndreas Gohr    /** @inheritdoc */
55307c6980SAndreas Gohr    public function cleanID($id)
56307c6980SAndreas Gohr    {
57307c6980SAndreas Gohr        return trim($id, ' /.');
58307c6980SAndreas Gohr    }
59307c6980SAndreas Gohr}
60