xref: /plugin/doi/Resolver/IsbnGoogleBooksResolver.php (revision 9f8980aeb04b800e022638072fad6807c450ae23)
1307c6980SAndreas Gohr<?php
2307c6980SAndreas Gohr
3307c6980SAndreas Gohrnamespace dokuwiki\plugin\doi\Resolver;
4307c6980SAndreas Gohr
5307c6980SAndreas Gohruse dokuwiki\HTTP\DokuHTTPClient;
6307c6980SAndreas Gohr
7307c6980SAndreas Gohr/**
8307c6980SAndreas Gohr * ISBN resolver using Google Books API
9307c6980SAndreas Gohr */
10307c6980SAndreas Gohrclass IsbnGoogleBooksResolver extends AbstractIsbnResolver
11307c6980SAndreas Gohr{
12307c6980SAndreas Gohr
13307c6980SAndreas Gohr    /** @inheritdoc */
14307c6980SAndreas Gohr    public function getData($id)
15307c6980SAndreas Gohr    {
16307c6980SAndreas Gohr        $data = $this->fetchCachedData($id);
17307c6980SAndreas Gohr        $data = $data['volumeInfo'];
18307c6980SAndreas Gohr        $result = $this->defaultResult;
19307c6980SAndreas Gohr
20307c6980SAndreas Gohr        $result['url'] = $data['canonicalVolumeLink'];
21307c6980SAndreas Gohr        foreach ($data['industryIdentifiers'] as $identifier) {
22307c6980SAndreas Gohr            if ($identifier['type'] === 'ISBN_13') {
23307c6980SAndreas Gohr                $result['id'] = $identifier['identifier'];
24307c6980SAndreas Gohr                break;
25307c6980SAndreas Gohr            }
26307c6980SAndreas Gohr            if ($identifier['type'] === 'ISBN_10') {
27307c6980SAndreas Gohr                $result['id'] = $identifier['identifier'];
28307c6980SAndreas Gohr            }
29307c6980SAndreas Gohr        }
30307c6980SAndreas Gohr
31307c6980SAndreas Gohr        $result['title'] = $data['title'];
32307c6980SAndreas Gohr        if (isset($data['subtitle'])) $result['title'] .= ': ' . $data['subtitle'];
33*9f8980aeSAndreas Gohr        if(empty($result['title'])) $result['title'] = $id;
34307c6980SAndreas Gohr
35307c6980SAndreas Gohr        $result['authors'] = $data['authors'] ?? [];
36307c6980SAndreas Gohr
37307c6980SAndreas Gohr        $published = $data['publishedDate'] ?? '';
38307c6980SAndreas Gohr        if (preg_match('/\b(\d{4})\b/', $published, $m)) {
39307c6980SAndreas Gohr            $result['published'] = $m[1];
40307c6980SAndreas Gohr        }
41307c6980SAndreas Gohr
42307c6980SAndreas Gohr        $result['publisher'] = $data['publisher'] ?? '';
43307c6980SAndreas Gohr
44307c6980SAndreas Gohr        return $result;
45307c6980SAndreas Gohr    }
46307c6980SAndreas Gohr
47307c6980SAndreas Gohr    /** @inheritdoc */
48307c6980SAndreas Gohr    protected function fetchData($id)
49307c6980SAndreas Gohr    {
50307c6980SAndreas Gohr        $http = new DokuHTTPClient();
51307c6980SAndreas Gohr        $json = $http->get('https://www.googleapis.com/books/v1/volumes?q=isbn:' . $id);
52307c6980SAndreas Gohr        if (!$json) throw new \Exception('Could not fetch data from Google Books. ' . $http->error);
53307c6980SAndreas Gohr        $data = json_decode($json, true);
54307c6980SAndreas Gohr        if (!isset($data['items'])) throw new \Exception('No ISBN results found at Google Books.');
55307c6980SAndreas Gohr        return $data['items'][0]; // first entry
56307c6980SAndreas Gohr    }
57307c6980SAndreas Gohr}
58