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 { 42*9f8980aeSAndreas Gohr $match = substr($match, 2, -2); 43*9f8980aeSAndreas Gohr list(, $id) = sexplode('>', $match, 2); 44*9f8980aeSAndreas Gohr list($id, $title) = sexplode('|', $id, 2); 45*9f8980aeSAndreas Gohr return ['id' => $id, 'title' => $title]; 4626b57c75SAndreas Gohr } 4726b57c75SAndreas Gohr 4826b57c75SAndreas Gohr /** @inheritDoc */ 4926b57c75SAndreas Gohr public function render($mode, Doku_Renderer $renderer, $data) 5026b57c75SAndreas Gohr { 51307c6980SAndreas Gohr $resolver = $this->getResolver(); 52307c6980SAndreas Gohr $data['id'] = $resolver->cleanID($data['id']); 5326b57c75SAndreas Gohr 54307c6980SAndreas Gohr try { 55307c6980SAndreas Gohr $publication = $resolver->getData($data['id']); 56307c6980SAndreas Gohr } catch (Exception $e) { 57307c6980SAndreas Gohr msg(hsc($e->getMessage()), -1); 58307c6980SAndreas Gohr $url = $resolver->getFallbackURL($data['id']); 59*9f8980aeSAndreas Gohr $title = empty($data['title']) ? $data['id'] : $data['title']; 60307c6980SAndreas Gohr 6126b57c75SAndreas Gohr $renderer->externallink($url, $title); 6226b57c75SAndreas Gohr return true; 6326b57c75SAndreas Gohr } 6426b57c75SAndreas Gohr 65*9f8980aeSAndreas Gohr // overwritten title? 66*9f8980aeSAndreas Gohr if (!empty($data['title'])) $publication['title'] = $data['title']; 67*9f8980aeSAndreas Gohr 687195edb0SAndreas Gohr if ($mode === 'xhtml') { 6926b57c75SAndreas Gohr /** @var Doku_Renderer_xhtml $renderer */ 707195edb0SAndreas Gohr $this->renderXHTML($publication, $renderer); 717195edb0SAndreas Gohr } else { 727195edb0SAndreas Gohr $this->renderAny($publication, $renderer); 737195edb0SAndreas Gohr } 7426b57c75SAndreas Gohr 7526b57c75SAndreas Gohr return true; 7626b57c75SAndreas Gohr } 7726b57c75SAndreas Gohr 7826b57c75SAndreas Gohr /** 79307c6980SAndreas Gohr * @return AbstractResolver 8026b57c75SAndreas Gohr */ 81307c6980SAndreas Gohr protected function getResolver() 8226b57c75SAndreas Gohr { 83307c6980SAndreas Gohr return new DoiResolver(); 8426b57c75SAndreas Gohr } 8526b57c75SAndreas Gohr 8626b57c75SAndreas Gohr /** 877195edb0SAndreas Gohr * Render the given data on the XHTML renderer 887195edb0SAndreas Gohr * 897195edb0SAndreas Gohr * Adds various classes to the output for CSS styling 9026b57c75SAndreas Gohr * 91307c6980SAndreas Gohr * @param array $data 92307c6980SAndreas Gohr * @param Doku_Renderer_xhtml $renderer 93307c6980SAndreas Gohr * @return void 9426b57c75SAndreas Gohr */ 957195edb0SAndreas Gohr protected function renderXHTML($data, $renderer) 9626b57c75SAndreas Gohr { 97307c6980SAndreas Gohr $renderer->doc .= '<div class="plugin_doi ' . hsc($data['type']) . '">'; 98307c6980SAndreas Gohr $renderer->externallink($data['url'], $data['title']); 99307c6980SAndreas Gohr 100307c6980SAndreas Gohr if ($data['published']) { 101307c6980SAndreas Gohr $renderer->doc .= ' <span>(' . hsc($data['published']) . ')</span>'; 1024163f19cSAndreas Gohr } 1034163f19cSAndreas Gohr 104307c6980SAndreas Gohr $renderer->doc .= '<div class="meta">'; 105307c6980SAndreas Gohr if ($data['authors']) { 106307c6980SAndreas Gohr $authors = array_map(function ($author) { 107307c6980SAndreas Gohr return '<strong>' . hsc($author) . '</strong>'; 108307c6980SAndreas Gohr }, $data['authors']); 109307c6980SAndreas Gohr $renderer->doc .= '<span class="authors">' . join(', ', $authors) . '</span>'; 110307c6980SAndreas Gohr } 111307c6980SAndreas Gohr if ($data['journal']) { 112307c6980SAndreas Gohr $journal = $data['journal']; 113307c6980SAndreas Gohr $journal .= ' ' . join('/', array_filter([$data['volume'] ?? null, $data['issue'] ?? null])); 114307c6980SAndreas Gohr $journal = '<span>' . hsc($journal) . '</span>'; 1157195edb0SAndreas Gohr if ($data['page']) { 116307c6980SAndreas Gohr $journal .= ' <i>p' . hsc($data['page']) . '</i>'; 117307c6980SAndreas Gohr } 118307c6980SAndreas Gohr $renderer->doc .= ' <span class="journal">' . $journal . '</span>'; 119307c6980SAndreas Gohr } 120307c6980SAndreas Gohr $renderer->doc .= '</div>'; 12126b57c75SAndreas Gohr 122307c6980SAndreas Gohr $renderer->doc .= '<div class="meta">'; 123307c6980SAndreas Gohr if ($data['publisher']) { 124307c6980SAndreas Gohr $renderer->doc .= '<span class="publisher">' . hsc($data['publisher']) . '</span>'; 125307c6980SAndreas Gohr } 126307c6980SAndreas Gohr $renderer->doc .= ' <code class="id">' . $data['idtype'] . ':' . hsc($data['id']) . '</code>'; 127307c6980SAndreas Gohr $renderer->doc .= '</div>'; 128307c6980SAndreas Gohr 129307c6980SAndreas Gohr $renderer->doc .= '</div>'; 13026b57c75SAndreas Gohr } 1317195edb0SAndreas Gohr 1327195edb0SAndreas Gohr /** 1337195edb0SAndreas Gohr * Render the given data on any renderer 1347195edb0SAndreas Gohr * 1357195edb0SAndreas Gohr * Uses renderer methods only 1367195edb0SAndreas Gohr * 1377195edb0SAndreas Gohr * @param array $data 1387195edb0SAndreas Gohr * @param Doku_Renderer $renderer 1397195edb0SAndreas Gohr * @return void 1407195edb0SAndreas Gohr */ 1417195edb0SAndreas Gohr protected function renderAny($data, $renderer) 1427195edb0SAndreas Gohr { 1437195edb0SAndreas Gohr $renderer->p_open(); 1447195edb0SAndreas Gohr $renderer->externallink($data['url'], $data['title']); 1457195edb0SAndreas Gohr 1467195edb0SAndreas Gohr if ($data['published']) { 1477195edb0SAndreas Gohr $renderer->cdata(' (' . hsc($data['published']) . ')'); 1487195edb0SAndreas Gohr } 1497195edb0SAndreas Gohr $renderer->linebreak(); 1507195edb0SAndreas Gohr 1517195edb0SAndreas Gohr if ($data['authors']) { 1527195edb0SAndreas Gohr $len = count($data['authors']); 1537195edb0SAndreas Gohr for ($i = 0; $i < $len; $i++) { 1547195edb0SAndreas Gohr $renderer->strong_open(); 1557195edb0SAndreas Gohr $renderer->cdata($data['authors'][$i]); 1567195edb0SAndreas Gohr $renderer->strong_close(); 1577195edb0SAndreas Gohr if ($i < $len - 1) { 1587195edb0SAndreas Gohr $renderer->cdata(', '); 1597195edb0SAndreas Gohr } 1607195edb0SAndreas Gohr } 1617195edb0SAndreas Gohr 1627195edb0SAndreas Gohr if ($data['journal']) { 1637195edb0SAndreas Gohr $journal = $data['journal']; 1647195edb0SAndreas Gohr $journal .= ' ' . join('/', array_filter([$data['volume'] ?? null, $data['issue'] ?? null])); 1657195edb0SAndreas Gohr $renderer->cdata(' ' . $journal); 1667195edb0SAndreas Gohr } 1677195edb0SAndreas Gohr 1687195edb0SAndreas Gohr if ($data['page']) { 1697195edb0SAndreas Gohr $renderer->cdata(' '); 1707195edb0SAndreas Gohr $renderer->emphasis_open(); 1717195edb0SAndreas Gohr $renderer->cdata('p' . $data['page']); 1727195edb0SAndreas Gohr $renderer->emphasis_close(); 1737195edb0SAndreas Gohr } 1747195edb0SAndreas Gohr } 1757195edb0SAndreas Gohr $renderer->linebreak(); 1767195edb0SAndreas Gohr 1777195edb0SAndreas Gohr if ($data['publisher']) { 1787195edb0SAndreas Gohr $renderer->cdata($data['publisher']); 1797195edb0SAndreas Gohr $renderer->cdata(' '); 1807195edb0SAndreas Gohr } 1817195edb0SAndreas Gohr $renderer->monospace_open(); 1827195edb0SAndreas Gohr $renderer->cdata($data['idtype'] . ':' . hsc($data['id'])); 1837195edb0SAndreas Gohr $renderer->monospace_close(); 1847195edb0SAndreas Gohr 1857195edb0SAndreas Gohr $renderer->p_close(); 1867195edb0SAndreas Gohr } 18726b57c75SAndreas Gohr} 18826b57c75SAndreas Gohr 189