xref: /plugin/doi/Resolver/IsbnGoogleBooksResolver.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 Gohr/**
8*307c6980SAndreas Gohr * ISBN resolver using Google Books API
9*307c6980SAndreas Gohr */
10*307c6980SAndreas Gohrclass IsbnGoogleBooksResolver extends AbstractIsbnResolver
11*307c6980SAndreas Gohr{
12*307c6980SAndreas Gohr
13*307c6980SAndreas Gohr    /** @inheritdoc */
14*307c6980SAndreas Gohr    public function getData($id)
15*307c6980SAndreas Gohr    {
16*307c6980SAndreas Gohr        $data = $this->fetchCachedData($id);
17*307c6980SAndreas Gohr        $data = $data['volumeInfo'];
18*307c6980SAndreas Gohr        $result = $this->defaultResult;
19*307c6980SAndreas Gohr
20*307c6980SAndreas Gohr        $result['url'] = $data['canonicalVolumeLink'];
21*307c6980SAndreas Gohr        foreach ($data['industryIdentifiers'] as $identifier) {
22*307c6980SAndreas Gohr            if ($identifier['type'] === 'ISBN_13') {
23*307c6980SAndreas Gohr                $result['id'] = $identifier['identifier'];
24*307c6980SAndreas Gohr                break;
25*307c6980SAndreas Gohr            }
26*307c6980SAndreas Gohr            if ($identifier['type'] === 'ISBN_10') {
27*307c6980SAndreas Gohr                $result['id'] = $identifier['identifier'];
28*307c6980SAndreas Gohr            }
29*307c6980SAndreas Gohr        }
30*307c6980SAndreas Gohr
31*307c6980SAndreas Gohr        $result['title'] = $data['title'];
32*307c6980SAndreas Gohr        if (isset($data['subtitle'])) $result['title'] .= ': ' . $data['subtitle'];
33*307c6980SAndreas Gohr
34*307c6980SAndreas Gohr        $result['authors'] = $data['authors'] ?? [];
35*307c6980SAndreas Gohr
36*307c6980SAndreas Gohr        $published = $data['publishedDate'] ?? '';
37*307c6980SAndreas Gohr        if (preg_match('/\b(\d{4})\b/', $published, $m)) {
38*307c6980SAndreas Gohr            $result['published'] = $m[1];
39*307c6980SAndreas Gohr        }
40*307c6980SAndreas Gohr
41*307c6980SAndreas Gohr        $result['publisher'] = $data['publisher'] ?? '';
42*307c6980SAndreas Gohr
43*307c6980SAndreas Gohr        return $result;
44*307c6980SAndreas Gohr    }
45*307c6980SAndreas Gohr
46*307c6980SAndreas Gohr    /** @inheritdoc */
47*307c6980SAndreas Gohr    protected function fetchData($id)
48*307c6980SAndreas Gohr    {
49*307c6980SAndreas Gohr        $http = new DokuHTTPClient();
50*307c6980SAndreas Gohr        $json = $http->get('https://www.googleapis.com/books/v1/volumes?q=isbn:' . $id);
51*307c6980SAndreas Gohr        if (!$json) throw new \Exception('Could not fetch data from Google Books. ' . $http->error);
52*307c6980SAndreas Gohr        $data = json_decode($json, true);
53*307c6980SAndreas Gohr        if (!isset($data['items'])) throw new \Exception('No ISBN results found at Google Books.');
54*307c6980SAndreas Gohr        return $data['items'][0]; // first entry
55*307c6980SAndreas Gohr    }
56*307c6980SAndreas Gohr}
57