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 { 429f8980aeSAndreas Gohr $match = substr($match, 2, -2); 439f8980aeSAndreas Gohr list(, $id) = sexplode('>', $match, 2); 449f8980aeSAndreas Gohr list($id, $title) = sexplode('|', $id, 2); 45*17101be4SAndreas Gohr return [ 46*17101be4SAndreas Gohr 'id' => $id, 47*17101be4SAndreas Gohr 'title' => $title, 48*17101be4SAndreas Gohr ]; 4926b57c75SAndreas Gohr } 5026b57c75SAndreas Gohr 5126b57c75SAndreas Gohr /** @inheritDoc */ 5226b57c75SAndreas Gohr public function render($mode, Doku_Renderer $renderer, $data) 5326b57c75SAndreas Gohr { 54307c6980SAndreas Gohr $resolver = $this->getResolver(); 55307c6980SAndreas Gohr $data['id'] = $resolver->cleanID($data['id']); 5626b57c75SAndreas Gohr 57307c6980SAndreas Gohr try { 58307c6980SAndreas Gohr $publication = $resolver->getData($data['id']); 59307c6980SAndreas Gohr } catch (Exception $e) { 60307c6980SAndreas Gohr msg(hsc($e->getMessage()), -1); 61307c6980SAndreas Gohr $url = $resolver->getFallbackURL($data['id']); 629f8980aeSAndreas Gohr $title = empty($data['title']) ? $data['id'] : $data['title']; 63307c6980SAndreas Gohr 6426b57c75SAndreas Gohr $renderer->externallink($url, $title); 6526b57c75SAndreas Gohr return true; 6626b57c75SAndreas Gohr } 6726b57c75SAndreas Gohr 689f8980aeSAndreas Gohr // overwritten title? 699f8980aeSAndreas Gohr if (!empty($data['title'])) $publication['title'] = $data['title']; 709f8980aeSAndreas Gohr 71*17101be4SAndreas Gohr // overwritten url (eg. by amazonlight plugin)? 72*17101be4SAndreas Gohr if (!empty($data['url'])) $publication['url'] = $data['url']; 73*17101be4SAndreas Gohr 747195edb0SAndreas Gohr if ($mode === 'xhtml') { 7526b57c75SAndreas Gohr /** @var Doku_Renderer_xhtml $renderer */ 767195edb0SAndreas Gohr $this->renderXHTML($publication, $renderer); 777195edb0SAndreas Gohr } else { 787195edb0SAndreas Gohr $this->renderAny($publication, $renderer); 797195edb0SAndreas Gohr } 8026b57c75SAndreas Gohr 8126b57c75SAndreas Gohr return true; 8226b57c75SAndreas Gohr } 8326b57c75SAndreas Gohr 8426b57c75SAndreas Gohr /** 85307c6980SAndreas Gohr * @return AbstractResolver 8626b57c75SAndreas Gohr */ 87307c6980SAndreas Gohr protected function getResolver() 8826b57c75SAndreas Gohr { 89307c6980SAndreas Gohr return new DoiResolver(); 9026b57c75SAndreas Gohr } 9126b57c75SAndreas Gohr 9226b57c75SAndreas Gohr /** 937195edb0SAndreas Gohr * Render the given data on the XHTML renderer 947195edb0SAndreas Gohr * 957195edb0SAndreas Gohr * Adds various classes to the output for CSS styling 9626b57c75SAndreas Gohr * 97307c6980SAndreas Gohr * @param array $data 98307c6980SAndreas Gohr * @param Doku_Renderer_xhtml $renderer 99307c6980SAndreas Gohr * @return void 10026b57c75SAndreas Gohr */ 1017195edb0SAndreas Gohr protected function renderXHTML($data, $renderer) 10226b57c75SAndreas Gohr { 103307c6980SAndreas Gohr $renderer->doc .= '<div class="plugin_doi ' . hsc($data['type']) . '">'; 104*17101be4SAndreas Gohr 105*17101be4SAndreas Gohr if( $this->getConf('cover') && $data['image'] ) { 106*17101be4SAndreas Gohr $renderer->externallink( 107*17101be4SAndreas Gohr $data['url'], 108*17101be4SAndreas Gohr [ 109*17101be4SAndreas Gohr 'src' => $data['image'], 110*17101be4SAndreas Gohr 'title' => $data['title'], 111*17101be4SAndreas Gohr 'align' => 'left', 112*17101be4SAndreas Gohr 'width' => 64, 113*17101be4SAndreas Gohr 'height' => 90, 114*17101be4SAndreas Gohr 'cache' => true, 115*17101be4SAndreas Gohr 'type' => 'externalmedia' 116*17101be4SAndreas Gohr ] 117*17101be4SAndreas Gohr ); 118*17101be4SAndreas Gohr } 119*17101be4SAndreas Gohr 120307c6980SAndreas Gohr $renderer->externallink($data['url'], $data['title']); 121307c6980SAndreas Gohr 122307c6980SAndreas Gohr if ($data['published']) { 123307c6980SAndreas Gohr $renderer->doc .= ' <span>(' . hsc($data['published']) . ')</span>'; 1244163f19cSAndreas Gohr } 1254163f19cSAndreas Gohr 126307c6980SAndreas Gohr $renderer->doc .= '<div class="meta">'; 127307c6980SAndreas Gohr if ($data['authors']) { 128307c6980SAndreas Gohr $authors = array_map(function ($author) { 129307c6980SAndreas Gohr return '<strong>' . hsc($author) . '</strong>'; 130307c6980SAndreas Gohr }, $data['authors']); 131307c6980SAndreas Gohr $renderer->doc .= '<span class="authors">' . join(', ', $authors) . '</span>'; 132307c6980SAndreas Gohr } 133307c6980SAndreas Gohr if ($data['journal']) { 134307c6980SAndreas Gohr $journal = $data['journal']; 135307c6980SAndreas Gohr $journal .= ' ' . join('/', array_filter([$data['volume'] ?? null, $data['issue'] ?? null])); 136307c6980SAndreas Gohr $journal = '<span>' . hsc($journal) . '</span>'; 1377195edb0SAndreas Gohr if ($data['page']) { 138307c6980SAndreas Gohr $journal .= ' <i>p' . hsc($data['page']) . '</i>'; 139307c6980SAndreas Gohr } 140307c6980SAndreas Gohr $renderer->doc .= ' <span class="journal">' . $journal . '</span>'; 141307c6980SAndreas Gohr } 142307c6980SAndreas Gohr $renderer->doc .= '</div>'; 14326b57c75SAndreas Gohr 144307c6980SAndreas Gohr $renderer->doc .= '<div class="meta">'; 145307c6980SAndreas Gohr if ($data['publisher']) { 146307c6980SAndreas Gohr $renderer->doc .= '<span class="publisher">' . hsc($data['publisher']) . '</span>'; 147307c6980SAndreas Gohr } 148307c6980SAndreas Gohr $renderer->doc .= ' <code class="id">' . $data['idtype'] . ':' . hsc($data['id']) . '</code>'; 149307c6980SAndreas Gohr $renderer->doc .= '</div>'; 150307c6980SAndreas Gohr 151307c6980SAndreas Gohr $renderer->doc .= '</div>'; 15226b57c75SAndreas Gohr } 1537195edb0SAndreas Gohr 1547195edb0SAndreas Gohr /** 1557195edb0SAndreas Gohr * Render the given data on any renderer 1567195edb0SAndreas Gohr * 1577195edb0SAndreas Gohr * Uses renderer methods only 1587195edb0SAndreas Gohr * 1597195edb0SAndreas Gohr * @param array $data 1607195edb0SAndreas Gohr * @param Doku_Renderer $renderer 1617195edb0SAndreas Gohr * @return void 1627195edb0SAndreas Gohr */ 1637195edb0SAndreas Gohr protected function renderAny($data, $renderer) 1647195edb0SAndreas Gohr { 1657195edb0SAndreas Gohr $renderer->p_open(); 1667195edb0SAndreas Gohr $renderer->externallink($data['url'], $data['title']); 1677195edb0SAndreas Gohr 1687195edb0SAndreas Gohr if ($data['published']) { 1697195edb0SAndreas Gohr $renderer->cdata(' (' . hsc($data['published']) . ')'); 1707195edb0SAndreas Gohr } 1717195edb0SAndreas Gohr $renderer->linebreak(); 1727195edb0SAndreas Gohr 1737195edb0SAndreas Gohr if ($data['authors']) { 1747195edb0SAndreas Gohr $len = count($data['authors']); 1757195edb0SAndreas Gohr for ($i = 0; $i < $len; $i++) { 1767195edb0SAndreas Gohr $renderer->strong_open(); 1777195edb0SAndreas Gohr $renderer->cdata($data['authors'][$i]); 1787195edb0SAndreas Gohr $renderer->strong_close(); 1797195edb0SAndreas Gohr if ($i < $len - 1) { 1807195edb0SAndreas Gohr $renderer->cdata(', '); 1817195edb0SAndreas Gohr } 1827195edb0SAndreas Gohr } 1837195edb0SAndreas Gohr 1847195edb0SAndreas Gohr if ($data['journal']) { 1857195edb0SAndreas Gohr $journal = $data['journal']; 1867195edb0SAndreas Gohr $journal .= ' ' . join('/', array_filter([$data['volume'] ?? null, $data['issue'] ?? null])); 1877195edb0SAndreas Gohr $renderer->cdata(' ' . $journal); 1887195edb0SAndreas Gohr } 1897195edb0SAndreas Gohr 1907195edb0SAndreas Gohr if ($data['page']) { 1917195edb0SAndreas Gohr $renderer->cdata(' '); 1927195edb0SAndreas Gohr $renderer->emphasis_open(); 1937195edb0SAndreas Gohr $renderer->cdata('p' . $data['page']); 1947195edb0SAndreas Gohr $renderer->emphasis_close(); 1957195edb0SAndreas Gohr } 1967195edb0SAndreas Gohr } 1977195edb0SAndreas Gohr $renderer->linebreak(); 1987195edb0SAndreas Gohr 1997195edb0SAndreas Gohr if ($data['publisher']) { 2007195edb0SAndreas Gohr $renderer->cdata($data['publisher']); 2017195edb0SAndreas Gohr $renderer->cdata(' '); 2027195edb0SAndreas Gohr } 2037195edb0SAndreas Gohr $renderer->monospace_open(); 2047195edb0SAndreas Gohr $renderer->cdata($data['idtype'] . ':' . hsc($data['id'])); 2057195edb0SAndreas Gohr $renderer->monospace_close(); 2067195edb0SAndreas Gohr 2077195edb0SAndreas Gohr $renderer->p_close(); 2087195edb0SAndreas Gohr } 20926b57c75SAndreas Gohr} 21026b57c75SAndreas Gohr 211