xref: /plugin/doi/Resolver/DoiResolver.php (revision 307c69801b7d8b027f4d0b7119b53e92e2931cb6)
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 Gohrclass DoiResolver extends AbstractResolver
8*307c6980SAndreas Gohr{
9*307c6980SAndreas Gohr
10*307c6980SAndreas Gohr    /** @inheritdoc */
11*307c6980SAndreas Gohr    public function getData($id)
12*307c6980SAndreas Gohr    {
13*307c6980SAndreas Gohr        $data = $this->fetchCachedData($id);
14*307c6980SAndreas Gohr        $result = $this->defaultResult;
15*307c6980SAndreas Gohr
16*307c6980SAndreas Gohr        $result['id'] = $data['DOI'];
17*307c6980SAndreas Gohr        $result['title'] = $data['title'] ?? $id;
18*307c6980SAndreas Gohr        $result['url'] = $data['URL'] ?? 'https://doi.org/' . $id;
19*307c6980SAndreas Gohr        $result['type'] = $data['type'];
20*307c6980SAndreas Gohr        $result['idtype'] = 'DOI';
21*307c6980SAndreas Gohr
22*307c6980SAndreas Gohr        foreach ($data['author'] ?? $data['editor'] ?? [] as $author) {
23*307c6980SAndreas Gohr            $result['authors'][] = $author['given'] . ' ' . $author['family'];
24*307c6980SAndreas Gohr        }
25*307c6980SAndreas Gohr
26*307c6980SAndreas Gohr        $result['journal'] = $data['container-title'] ?? '';
27*307c6980SAndreas Gohr        $result['volume'] = $data['volume'] ?? '';
28*307c6980SAndreas Gohr        $result['issue'] = $data['issue'] ?? '';
29*307c6980SAndreas Gohr        $result['page'] = $data['page'] ?? '';
30*307c6980SAndreas Gohr
31*307c6980SAndreas Gohr        $result['published'] = $data['issued']['date-parts'][0][0] ?? '';
32*307c6980SAndreas Gohr        $result['publisher'] = $data['publisher'] ?? '';
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        $http->headers['Accept'] = 'application/vnd.citationstyles.csl+json';
42*307c6980SAndreas Gohr        $json = $http->get('https://doi.org/' . $id);
43*307c6980SAndreas Gohr        if (!$json) throw new \Exception('Could not fetch data from doi.org. ' . $http->error);
44*307c6980SAndreas Gohr        return json_decode($json, true);
45*307c6980SAndreas Gohr    }
46*307c6980SAndreas Gohr
47*307c6980SAndreas Gohr    /** @inheritdoc */
48*307c6980SAndreas Gohr    public function getFallbackURL($id)
49*307c6980SAndreas Gohr    {
50*307c6980SAndreas Gohr        return 'https://doi.org/' . $id;
51*307c6980SAndreas Gohr    }
52*307c6980SAndreas Gohr
53*307c6980SAndreas Gohr    /** @inheritdoc */
54*307c6980SAndreas Gohr    public function cleanID($id)
55*307c6980SAndreas Gohr    {
56*307c6980SAndreas Gohr        return trim($id, ' /.');
57*307c6980SAndreas Gohr    }
58*307c6980SAndreas Gohr}
59