xref: /plugin/doi/syntax/doi.php (revision 7195edb0278765975aaf826cd01dd104859d32f0)
126b57c75SAndreas Gohr<?php
226b57c75SAndreas Gohr
3307c6980SAndreas Gohruse dokuwiki\plugin\doi\Resolver\AbstractResolver;
4307c6980SAndreas Gohruse \dokuwiki\plugin\doi\Resolver\DoiResolver;
5307c6980SAndreas Gohr
6307c6980SAndreas Gohr
726b57c75SAndreas Gohr/**
826b57c75SAndreas Gohr * DokuWiki Plugin doi (Syntax Component)
926b57c75SAndreas Gohr *
1026b57c75SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
1126b57c75SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
1226b57c75SAndreas Gohr */
1326b57c75SAndreas Gohrclass syntax_plugin_doi_doi extends \dokuwiki\Extension\SyntaxPlugin
1426b57c75SAndreas Gohr{
1526b57c75SAndreas Gohr    /** @inheritDoc */
1626b57c75SAndreas Gohr    public function getType()
1726b57c75SAndreas Gohr    {
1826b57c75SAndreas Gohr        return 'substition';
1926b57c75SAndreas Gohr    }
2026b57c75SAndreas Gohr
2126b57c75SAndreas Gohr    /** @inheritDoc */
2226b57c75SAndreas Gohr    public function getPType()
2326b57c75SAndreas Gohr    {
2426b57c75SAndreas Gohr        return 'normal';
2526b57c75SAndreas Gohr    }
2626b57c75SAndreas Gohr
2726b57c75SAndreas Gohr    /** @inheritDoc */
2826b57c75SAndreas Gohr    public function getSort()
2926b57c75SAndreas Gohr    {
3026b57c75SAndreas Gohr        return 155;
3126b57c75SAndreas Gohr    }
3226b57c75SAndreas Gohr
3326b57c75SAndreas Gohr    /** @inheritDoc */
3426b57c75SAndreas Gohr    public function connectTo($mode)
3526b57c75SAndreas Gohr    {
3626b57c75SAndreas Gohr        $this->Lexer->addSpecialPattern('\[\[doi>[^\]]+\]\]', $mode, 'plugin_doi_doi');
3726b57c75SAndreas Gohr    }
3826b57c75SAndreas Gohr
3926b57c75SAndreas Gohr    /** @inheritDoc */
4026b57c75SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler)
4126b57c75SAndreas Gohr    {
4226b57c75SAndreas Gohr        $doi = substr($match, 6, -2);
4326b57c75SAndreas Gohr
44307c6980SAndreas Gohr        return ['id' => $doi];
4526b57c75SAndreas Gohr    }
4626b57c75SAndreas Gohr
4726b57c75SAndreas Gohr    /** @inheritDoc */
4826b57c75SAndreas Gohr    public function render($mode, Doku_Renderer $renderer, $data)
4926b57c75SAndreas Gohr    {
50307c6980SAndreas Gohr        $resolver = $this->getResolver();
51307c6980SAndreas Gohr        $data['id'] = $resolver->cleanID($data['id']);
5226b57c75SAndreas Gohr
53307c6980SAndreas Gohr        try {
54307c6980SAndreas Gohr            $publication = $resolver->getData($data['id']);
55307c6980SAndreas Gohr        } catch (Exception $e) {
56307c6980SAndreas Gohr            msg(hsc($e->getMessage()), -1);
57307c6980SAndreas Gohr            $url = $resolver->getFallbackURL($data['id']);
58307c6980SAndreas Gohr            $title = $data['id'];
59307c6980SAndreas Gohr
6026b57c75SAndreas Gohr            $renderer->externallink($url, $title);
6126b57c75SAndreas Gohr            return true;
6226b57c75SAndreas Gohr        }
6326b57c75SAndreas Gohr
64*7195edb0SAndreas Gohr        if ($mode === 'xhtml') {
6526b57c75SAndreas Gohr            /** @var Doku_Renderer_xhtml $renderer */
66*7195edb0SAndreas Gohr            $this->renderXHTML($publication, $renderer);
67*7195edb0SAndreas Gohr        } else {
68*7195edb0SAndreas Gohr            $this->renderAny($publication, $renderer);
69*7195edb0SAndreas Gohr        }
7026b57c75SAndreas Gohr
7126b57c75SAndreas Gohr        return true;
7226b57c75SAndreas Gohr    }
7326b57c75SAndreas Gohr
7426b57c75SAndreas Gohr    /**
75307c6980SAndreas Gohr     * @return AbstractResolver
7626b57c75SAndreas Gohr     */
77307c6980SAndreas Gohr    protected function getResolver()
7826b57c75SAndreas Gohr    {
79307c6980SAndreas Gohr        return new DoiResolver();
8026b57c75SAndreas Gohr    }
8126b57c75SAndreas Gohr
8226b57c75SAndreas Gohr    /**
83*7195edb0SAndreas Gohr     * Render the given data on the XHTML renderer
84*7195edb0SAndreas Gohr     *
85*7195edb0SAndreas Gohr     * Adds various classes to the output for CSS styling
8626b57c75SAndreas Gohr     *
87307c6980SAndreas Gohr     * @param array $data
88307c6980SAndreas Gohr     * @param Doku_Renderer_xhtml $renderer
89307c6980SAndreas Gohr     * @return void
9026b57c75SAndreas Gohr     */
91*7195edb0SAndreas Gohr    protected function renderXHTML($data, $renderer)
9226b57c75SAndreas Gohr    {
93307c6980SAndreas Gohr        $renderer->doc .= '<div class="plugin_doi ' . hsc($data['type']) . '">';
94307c6980SAndreas Gohr        $renderer->externallink($data['url'], $data['title']);
95307c6980SAndreas Gohr
96307c6980SAndreas Gohr        if ($data['published']) {
97307c6980SAndreas Gohr            $renderer->doc .= ' <span>(' . hsc($data['published']) . ')</span>';
984163f19cSAndreas Gohr        }
994163f19cSAndreas Gohr
100307c6980SAndreas Gohr        $renderer->doc .= '<div class="meta">';
101307c6980SAndreas Gohr        if ($data['authors']) {
102307c6980SAndreas Gohr            $authors = array_map(function ($author) {
103307c6980SAndreas Gohr                return '<strong>' . hsc($author) . '</strong>';
104307c6980SAndreas Gohr            }, $data['authors']);
105307c6980SAndreas Gohr            $renderer->doc .= '<span class="authors">' . join(', ', $authors) . '</span>';
106307c6980SAndreas Gohr        }
107307c6980SAndreas Gohr        if ($data['journal']) {
108307c6980SAndreas Gohr            $journal = $data['journal'];
109307c6980SAndreas Gohr            $journal .= ' ' . join('/', array_filter([$data['volume'] ?? null, $data['issue'] ?? null]));
110307c6980SAndreas Gohr            $journal = '<span>' . hsc($journal) . '</span>';
111*7195edb0SAndreas Gohr            if ($data['page']) {
112307c6980SAndreas Gohr                $journal .= ' <i>p' . hsc($data['page']) . '</i>';
113307c6980SAndreas Gohr            }
114307c6980SAndreas Gohr            $renderer->doc .= ' <span class="journal">' . $journal . '</span>';
115307c6980SAndreas Gohr        }
116307c6980SAndreas Gohr        $renderer->doc .= '</div>';
11726b57c75SAndreas Gohr
118307c6980SAndreas Gohr        $renderer->doc .= '<div class="meta">';
119307c6980SAndreas Gohr        if ($data['publisher']) {
120307c6980SAndreas Gohr            $renderer->doc .= '<span class="publisher">' . hsc($data['publisher']) . '</span>';
121307c6980SAndreas Gohr        }
122307c6980SAndreas Gohr        $renderer->doc .= ' <code class="id">' . $data['idtype'] . ':' . hsc($data['id']) . '</code>';
123307c6980SAndreas Gohr        $renderer->doc .= '</div>';
124307c6980SAndreas Gohr
125307c6980SAndreas Gohr        $renderer->doc .= '</div>';
12626b57c75SAndreas Gohr    }
127*7195edb0SAndreas Gohr
128*7195edb0SAndreas Gohr    /**
129*7195edb0SAndreas Gohr     * Render the given data on any renderer
130*7195edb0SAndreas Gohr     *
131*7195edb0SAndreas Gohr     * Uses renderer methods only
132*7195edb0SAndreas Gohr     *
133*7195edb0SAndreas Gohr     * @param array $data
134*7195edb0SAndreas Gohr     * @param Doku_Renderer $renderer
135*7195edb0SAndreas Gohr     * @return void
136*7195edb0SAndreas Gohr     */
137*7195edb0SAndreas Gohr    protected function renderAny($data, $renderer)
138*7195edb0SAndreas Gohr    {
139*7195edb0SAndreas Gohr        $renderer->p_open();
140*7195edb0SAndreas Gohr        $renderer->externallink($data['url'], $data['title']);
141*7195edb0SAndreas Gohr
142*7195edb0SAndreas Gohr        if ($data['published']) {
143*7195edb0SAndreas Gohr            $renderer->cdata(' (' . hsc($data['published']) . ')');
144*7195edb0SAndreas Gohr        }
145*7195edb0SAndreas Gohr        $renderer->linebreak();
146*7195edb0SAndreas Gohr
147*7195edb0SAndreas Gohr        if ($data['authors']) {
148*7195edb0SAndreas Gohr            $len = count($data['authors']);
149*7195edb0SAndreas Gohr            for ($i = 0; $i < $len; $i++) {
150*7195edb0SAndreas Gohr                $renderer->strong_open();
151*7195edb0SAndreas Gohr                $renderer->cdata($data['authors'][$i]);
152*7195edb0SAndreas Gohr                $renderer->strong_close();
153*7195edb0SAndreas Gohr                if ($i < $len - 1) {
154*7195edb0SAndreas Gohr                    $renderer->cdata(', ');
155*7195edb0SAndreas Gohr                }
156*7195edb0SAndreas Gohr            }
157*7195edb0SAndreas Gohr
158*7195edb0SAndreas Gohr            if ($data['journal']) {
159*7195edb0SAndreas Gohr                $journal = $data['journal'];
160*7195edb0SAndreas Gohr                $journal .= ' ' . join('/', array_filter([$data['volume'] ?? null, $data['issue'] ?? null]));
161*7195edb0SAndreas Gohr                $renderer->cdata(' ' . $journal);
162*7195edb0SAndreas Gohr            }
163*7195edb0SAndreas Gohr
164*7195edb0SAndreas Gohr            if ($data['page']) {
165*7195edb0SAndreas Gohr                $renderer->cdata(' ');
166*7195edb0SAndreas Gohr                $renderer->emphasis_open();
167*7195edb0SAndreas Gohr                $renderer->cdata('p' . $data['page']);
168*7195edb0SAndreas Gohr                $renderer->emphasis_close();
169*7195edb0SAndreas Gohr            }
170*7195edb0SAndreas Gohr        }
171*7195edb0SAndreas Gohr        $renderer->linebreak();
172*7195edb0SAndreas Gohr
173*7195edb0SAndreas Gohr        if ($data['publisher']) {
174*7195edb0SAndreas Gohr            $renderer->cdata($data['publisher']);
175*7195edb0SAndreas Gohr            $renderer->cdata(' ');
176*7195edb0SAndreas Gohr        }
177*7195edb0SAndreas Gohr        $renderer->monospace_open();
178*7195edb0SAndreas Gohr        $renderer->cdata($data['idtype'] . ':' . hsc($data['id']));
179*7195edb0SAndreas Gohr        $renderer->monospace_close();
180*7195edb0SAndreas Gohr
181*7195edb0SAndreas Gohr        $renderer->p_close();
182*7195edb0SAndreas Gohr    }
18326b57c75SAndreas Gohr}
18426b57c75SAndreas Gohr
185