*/ class syntax_plugin_doi_doi extends \dokuwiki\Extension\SyntaxPlugin { /** @inheritDoc */ public function getType() { return 'substition'; } /** @inheritDoc */ public function getPType() { return 'normal'; } /** @inheritDoc */ public function getSort() { return 155; } /** @inheritDoc */ public function connectTo($mode) { $this->Lexer->addSpecialPattern('\[\[doi>[^\]]+\]\]', $mode, 'plugin_doi_doi'); } /** @inheritDoc */ public function handle($match, $state, $pos, Doku_Handler $handler) { $doi = substr($match, 6, -2); return ['doi' => $doi]; } /** @inheritDoc */ public function render($mode, Doku_Renderer $renderer, $data) { $publication = $this->fetchInfo($data['doi']); $title = $publication['title'][0] ?? $data['doi']; $url = $publication['URL'] ?? 'https://doi.org/' . $data['doi']; if ($mode !== 'xhtml' || !$publication) { $renderer->externallink($url, $title); return true; } /** @var Doku_Renderer_xhtml $renderer */ $this->formatPub($publication, $renderer); return true; } /** * Render the given message * * @param array $message * @param Doku_Renderer_xhtml $renderer * @return void */ protected function formatPub($message, $renderer) { $doi = $message['DOI']; $title = $message['title'] ?? $doi; $url = $message['URL'] ?? 'https://doi.org/' . $doi; $class = hsc($message['type']); $authorList = []; foreach ($message['author'] ?? $message['editor'] ?? [] as $author) { $authorList[] = '' . hsc($author['given'].' '.$author['family']) . ''; } if (!empty($message['container-title'])) { $journal = $message['container-title']; $journal .= ' ' . join('/', [$message['volume'] ?? null, $message['issue'] ?? null]); $journal = '' . hsc($journal) . ''; if (isset($message['page'])) { $journal .= ' p' . hsc($message['page']) . ''; } $journal = ' ' . $journal . ''; } else { $journal = ''; } $published = $message['issued']['date-parts'][0][0] ?? ''; if ($published) $published = ' (' . hsc($published) . ')'; $publisher = hsc($message['publisher'] ?? ''); //output $renderer->doc .= '
'; $renderer->externallink($url, $title); $renderer->doc .= $published; $renderer->doc .= '
'; if ($authorList) { $renderer->doc .= '' . join(', ', $authorList) . ''; } if ($journal) { $renderer->doc .= $journal; } $renderer->doc .= '
'; $renderer->doc .= '
'; if ($publisher) { $renderer->doc .= '' . $publisher . ''; } $renderer->doc .= ' DOI:' . $doi . ''; $renderer->doc .= '
'; $renderer->doc .= '
'; } /** * Fetch the info for the given DOI * * @param string $doi * @return false|array */ protected function fetchInfo($doi) { $cache = getCacheName($doi, '.doi.json'); if(@filemtime($cache) > filemtime(__FILE__)) { return json_decode(file_get_contents($cache), true); } $http = new \dokuwiki\HTTP\DokuHTTPClient(); $http->headers['Accept'] = 'application/vnd.citationstyles.csl+json'; $json = $http->get('https://doi.org/' . $doi); if (!$json) return false; file_put_contents($cache, $json); return json_decode($json, true); } }