xref: /plugin/doi/syntax/doi.php (revision 307c69801b7d8b027f4d0b7119b53e92e2931cb6)
126b57c75SAndreas Gohr<?php
226b57c75SAndreas Gohr
3*307c6980SAndreas Gohruse dokuwiki\plugin\doi\Resolver\AbstractResolver;
4*307c6980SAndreas Gohruse \dokuwiki\plugin\doi\Resolver\DoiResolver;
5*307c6980SAndreas Gohr
6*307c6980SAndreas 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
44*307c6980SAndreas Gohr        return ['id' => $doi];
4526b57c75SAndreas Gohr    }
4626b57c75SAndreas Gohr
4726b57c75SAndreas Gohr    /** @inheritDoc */
4826b57c75SAndreas Gohr    public function render($mode, Doku_Renderer $renderer, $data)
4926b57c75SAndreas Gohr    {
50*307c6980SAndreas Gohr        $resolver = $this->getResolver();
51*307c6980SAndreas Gohr        $data['id'] = $resolver->cleanID($data['id']);
5226b57c75SAndreas Gohr
53*307c6980SAndreas Gohr        try {
54*307c6980SAndreas Gohr            $publication = $resolver->getData($data['id']);
55*307c6980SAndreas Gohr            $url = $publication['url'];
56*307c6980SAndreas Gohr            $title = $publication['title'];
57*307c6980SAndreas Gohr        } catch (Exception $e) {
58*307c6980SAndreas Gohr            msg(hsc($e->getMessage()), -1);
59*307c6980SAndreas Gohr            $publication = null;
60*307c6980SAndreas Gohr            $url = $resolver->getFallbackURL($data['id']);
61*307c6980SAndreas Gohr            $title = $data['id'];
62*307c6980SAndreas Gohr        }
63*307c6980SAndreas Gohr
64*307c6980SAndreas Gohr        // FIXME use nicer rendering for non-xhtml modes
6526b57c75SAndreas Gohr        if ($mode !== 'xhtml' || !$publication) {
6626b57c75SAndreas Gohr            $renderer->externallink($url, $title);
6726b57c75SAndreas Gohr            return true;
6826b57c75SAndreas Gohr        }
6926b57c75SAndreas Gohr
7026b57c75SAndreas Gohr        /** @var Doku_Renderer_xhtml $renderer */
7190f426f5SAndreas Gohr        $this->formatPub($publication, $renderer);
7226b57c75SAndreas Gohr
7326b57c75SAndreas Gohr        return true;
7426b57c75SAndreas Gohr    }
7526b57c75SAndreas Gohr
7626b57c75SAndreas Gohr    /**
77*307c6980SAndreas Gohr     * @return AbstractResolver
7826b57c75SAndreas Gohr     */
79*307c6980SAndreas Gohr    protected function getResolver()
8026b57c75SAndreas Gohr    {
81*307c6980SAndreas Gohr        return new DoiResolver();
8226b57c75SAndreas Gohr    }
8326b57c75SAndreas Gohr
8426b57c75SAndreas Gohr    /**
85*307c6980SAndreas Gohr     * Render the given data
8626b57c75SAndreas Gohr     *
87*307c6980SAndreas Gohr     * @param array $data
88*307c6980SAndreas Gohr     * @param Doku_Renderer_xhtml $renderer
89*307c6980SAndreas Gohr     * @return void
9026b57c75SAndreas Gohr     */
91*307c6980SAndreas Gohr    protected function formatPub($data, $renderer)
9226b57c75SAndreas Gohr    {
93*307c6980SAndreas Gohr        $renderer->doc .= '<div class="plugin_doi ' . hsc($data['type']) . '">';
94*307c6980SAndreas Gohr        $renderer->externallink($data['url'], $data['title']);
95*307c6980SAndreas Gohr
96*307c6980SAndreas Gohr        if ($data['published']) {
97*307c6980SAndreas Gohr            $renderer->doc .= ' <span>(' . hsc($data['published']) . ')</span>';
984163f19cSAndreas Gohr        }
994163f19cSAndreas Gohr
100*307c6980SAndreas Gohr        $renderer->doc .= '<div class="meta">';
101*307c6980SAndreas Gohr        if ($data['authors']) {
102*307c6980SAndreas Gohr            $authors = array_map(function ($author) {
103*307c6980SAndreas Gohr                return '<strong>' . hsc($author) . '</strong>';
104*307c6980SAndreas Gohr            }, $data['authors']);
105*307c6980SAndreas Gohr            $renderer->doc .= '<span class="authors">' . join(', ', $authors) . '</span>';
106*307c6980SAndreas Gohr        }
107*307c6980SAndreas Gohr        if ($data['journal']) {
108*307c6980SAndreas Gohr            $journal = $data['journal'];
109*307c6980SAndreas Gohr            $journal .= ' ' . join('/', array_filter([$data['volume'] ?? null, $data['issue'] ?? null]));
110*307c6980SAndreas Gohr            $journal = '<span>' . hsc($journal) . '</span>';
111*307c6980SAndreas Gohr            if (isset($data['page'])) {
112*307c6980SAndreas Gohr                $journal .= ' <i>p' . hsc($data['page']) . '</i>';
113*307c6980SAndreas Gohr            }
114*307c6980SAndreas Gohr            $renderer->doc .= ' <span class="journal">' . $journal . '</span>';
115*307c6980SAndreas Gohr        }
116*307c6980SAndreas Gohr        $renderer->doc .= '</div>';
11726b57c75SAndreas Gohr
118*307c6980SAndreas Gohr        $renderer->doc .= '<div class="meta">';
119*307c6980SAndreas Gohr        if ($data['publisher']) {
120*307c6980SAndreas Gohr            $renderer->doc .= '<span class="publisher">' . hsc($data['publisher']) . '</span>';
121*307c6980SAndreas Gohr        }
122*307c6980SAndreas Gohr        $renderer->doc .= ' <code class="id">'.$data['idtype'].':' . hsc($data['id']) . '</code>';
123*307c6980SAndreas Gohr        $renderer->doc .= '</div>';
124*307c6980SAndreas Gohr
125*307c6980SAndreas Gohr        $renderer->doc .= '</div>';
12626b57c75SAndreas Gohr    }
12726b57c75SAndreas Gohr}
12826b57c75SAndreas Gohr
129