xref: /plugin/doi/syntax/isbn.php (revision 26b57c75afc4fc107c7f94d3d6fc20313ea9020d)
1<?php
2
3/**
4 * DokuWiki Plugin doi (Syntax Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr <gohr@cosmocode.de>
8 */
9class syntax_plugin_doi_isbn extends \dokuwiki\Extension\SyntaxPlugin
10{
11    /** @inheritDoc */
12    public function getType()
13    {
14        return 'substition';
15    }
16
17    /** @inheritDoc */
18    public function getPType()
19    {
20        return 'normal';
21    }
22
23    /** @inheritDoc */
24    public function getSort()
25    {
26        return 155;
27    }
28
29    /** @inheritDoc */
30    public function connectTo($mode)
31    {
32        $this->Lexer->addSpecialPattern('\[\[isbn>[^\]]+\]\]', $mode, 'plugin_doi_isbn');
33    }
34
35    /** @inheritDoc */
36    public function handle($match, $state, $pos, Doku_Handler $handler)
37    {
38        $doi = substr($match, 7, -2);
39
40        return ['isbn' => $doi];
41    }
42
43    /** @inheritDoc */
44    public function render($mode, Doku_Renderer $renderer, $data)
45    {
46        $publication = $this->fetchInfo($data['isbn']);
47        $title = $publication['details']['title'] ?? $data['isbn'];
48        $url = $publication['details']['info_url'] ?? 'https://www.google.com/search?q=isbn+' . $data['isbn'];
49
50        if ($mode !== 'xhtml' || !$publication) {
51            $renderer->externallink($url, $title);
52            return true;
53        }
54
55        /** @var Doku_Renderer_xhtml $renderer */
56        $this->formatPub($publication, $renderer);
57
58        return true;
59    }
60
61    /**
62     * Render the given message
63     *
64     * @param array $message
65     * @param Doku_Renderer_xhtml $renderer
66     * @return void
67     */
68    protected function formatPub($message, $renderer)
69    {
70        $url = $message['info_url'];
71        $message = $message['details'];
72
73        $isbn = $message['isbn_13'][0] ?? $message['isbn_10'][0] ?? '';
74        $title = $message['title'] ?? $isbn;
75
76        $class = 'book';
77
78        $authorList = [];
79        foreach ($message['authors'] ?? [] as $author) {
80            $authorList[] = '<strong>' . hsc($author['name']) . '</strong>';
81        }
82
83        /*
84        if (isset($message['container-title'][0])) {
85            $journal = $message['container-title'][0];
86            $journal .= ' ' . join('/', [$message['volume'] ?? null, $message['issue'] ?? null]);
87            $journal = '<span>' . hsc($journal) . '</span>';
88            if (isset($message['page'])) {
89                $journal .= ' <i>p' . hsc($message['page']) . '</i>';
90            }
91            $journal = ' <span class="journal">' . $journal . '</span>';
92        } else {
93            $journal = '';
94        }  */
95
96        $published = $message['publish_date'] ?? '';
97        if(preg_match('/\b(\d{4})\b/', $published, $m)) {
98            $published = ' <span>(' . hsc($m[1]) . ')</span>';
99        }else {
100            $published = '';
101        }
102
103        $publisher = hsc($message['publishers'][0] ?? '');
104
105        //output
106        $renderer->doc .= '<div class="plugin_doi ' . $class . '">';
107        $renderer->externallink($url, $title);
108        $renderer->doc .= $published;
109
110        $renderer->doc .= '<div class="meta">';
111        if ($authorList) {
112            $renderer->doc .= '<span class="authors">' . join(', ', $authorList) . '</span>';
113        }
114        $renderer->doc .= '</div>';
115
116        $renderer->doc .= '<div class="meta">';
117        if ($publisher) {
118            $renderer->doc .= '<span class="publisher">' . $publisher . '</span>';
119        }
120        $renderer->doc .= ' <code class="isbn">ISBN:' . $isbn . '</code>';
121        $renderer->doc .= '</div>';
122
123        $renderer->doc .= '</div>';
124    }
125
126    /**
127     * Fetch the info for the given ISBN
128     *
129     * @param string $isbn
130     * @return false|array
131     */
132    protected function fetchInfo($isbn)
133    {
134        $http = new \dokuwiki\HTTP\DokuHTTPClient();
135
136        $json = $http->get('https://openlibrary.org/api/books?jscmd=details&format=json&bibkeys=ISBN:' . $isbn);
137        if (!$json) return false;
138
139        $data = json_decode($json, true);
140        return array_shift($data); // first entry
141    }
142}
143
144