2015-03-25 23:48:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Semantic Action Plugin
|
2015-10-30 15:25:39 +00:00
|
|
|
*
|
2015-03-25 23:48:00 +00:00
|
|
|
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
|
2015-10-30 15:25:39 +00:00
|
|
|
* @author Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
|
2015-03-25 23:48:00 +00:00
|
|
|
* @copyright (C) 2015, Giuseppe Di Terlizzi
|
|
|
|
*/
|
|
|
|
|
|
|
|
// must be run within Dokuwiki
|
|
|
|
if(!defined('DOKU_INC')) die();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Semantic Action Plugin
|
|
|
|
*
|
|
|
|
* Add semantic data to DokuWiki
|
|
|
|
*/
|
|
|
|
class action_plugin_semantic extends DokuWiki_Action_Plugin {
|
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
/**
|
|
|
|
* Register events
|
|
|
|
*
|
|
|
|
* @param Doku_Event_Handler $controller
|
|
|
|
*/
|
|
|
|
public function register(Doku_Event_Handler $controller) {
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
if ($this->getConf('useJSONLD')) {
|
|
|
|
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'json_ld');
|
|
|
|
}
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
if ($this->getConf('useMetaDescription')) {
|
|
|
|
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_description');
|
2015-03-25 23:48:00 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
if ($this->getConf('useMetaAuthor')) {
|
|
|
|
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_author');
|
|
|
|
}
|
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
if ($this->getConf('useDublinCore')) {
|
|
|
|
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_dublin_core');
|
|
|
|
}
|
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
/**
|
|
|
|
* JSON-LD Event handler
|
|
|
|
*
|
|
|
|
* @param Doku_Event &$event
|
|
|
|
*/
|
|
|
|
public function json_ld(Doku_Event &$event, $param) {
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
$helper = $this->loadHelper('semantic');
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
if ($json_ld = $helper->getStructuredData()) {
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
$event->data["script"][] = array (
|
|
|
|
"type" => "application/ld+json",
|
|
|
|
"_data" => json_encode($json_ld),
|
2015-10-30 15:25:39 +00:00
|
|
|
);
|
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
if ($json_ld_relations = $helper->getBacklinks()) {
|
2015-10-30 15:25:39 +00:00
|
|
|
$event->data["script"][] = array (
|
|
|
|
"type" => "application/ld+json",
|
2015-11-11 22:45:46 +00:00
|
|
|
"_data" => json_encode($json_ld_relations),
|
2015-10-30 15:25:39 +00:00
|
|
|
);
|
2015-03-25 23:48:00 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
}
|
2015-03-25 23:48:00 +00:00
|
|
|
|
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
public function meta_description(Doku_Event &$event, $params) {
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
$helper = $this->loadHelper('semantic');
|
2015-03-30 08:32:15 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
if ($description = $helper->getDescription()) {
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
$description = str_replace("\n", ' ', $description);
|
2015-03-30 08:32:15 +00:00
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
$event->data['meta'][] = array(
|
|
|
|
'name' => 'description',
|
|
|
|
'content' => $description,
|
|
|
|
);
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
}
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
}
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
|
|
|
|
public function meta_author(Doku_Event &$event, $params) {
|
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
$helper = $this->loadHelper('semantic');
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
if ($author = $helper->getAuthor()) {
|
|
|
|
|
|
|
|
$event->data['meta'][] = array(
|
|
|
|
'name' => 'author',
|
|
|
|
'content' => $author,
|
|
|
|
);
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
}
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|
2015-10-30 15:25:39 +00:00
|
|
|
|
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
public function meta_dublin_core(Doku_Event &$event, $params) {
|
|
|
|
|
|
|
|
$helper = $this->loadHelper('semantic');
|
|
|
|
|
|
|
|
foreach ($helper->getDublinCore() as $name => $content) {
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
if (! $content) continue;
|
2015-10-30 15:25:39 +00:00
|
|
|
|
|
|
|
$event->data['meta'][] = array(
|
2015-11-11 22:45:46 +00:00
|
|
|
'name' => $name,
|
|
|
|
'content' => $content,
|
2015-10-30 15:25:39 +00:00
|
|
|
);
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|