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>
|
2019-10-08 09:07:33 +00:00
|
|
|
* @copyright (C) 2015-2019, Giuseppe Di Terlizzi
|
2015-03-25 23:48:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// must be run within Dokuwiki
|
2019-10-08 09:07:33 +00:00
|
|
|
if (!defined('DOKU_INC')) {
|
|
|
|
die();
|
|
|
|
}
|
2015-03-25 23:48:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Semantic Action Plugin
|
|
|
|
*
|
|
|
|
* Add semantic data to DokuWiki
|
|
|
|
*/
|
2019-10-08 09:07:33 +00:00
|
|
|
class action_plugin_semantic extends DokuWiki_Action_Plugin
|
|
|
|
{
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
private $helper = null;
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->helper = $this->loadHelper('semantic');
|
2015-03-25 23:48:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Register events
|
|
|
|
*
|
|
|
|
* @param Doku_Event_Handler $controller handler
|
|
|
|
*/
|
|
|
|
public function register(Doku_Event_Handler $controller)
|
|
|
|
{
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($this->getConf('useJSONLD')) {
|
|
|
|
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'website');
|
|
|
|
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'json_ld');
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($this->getConf('useMetaDescription')) {
|
|
|
|
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_description');
|
|
|
|
}
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($this->getConf('useMetaAuthor')) {
|
|
|
|
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_author');
|
|
|
|
}
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($this->getConf('useDublinCore')) {
|
|
|
|
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_dublin_core');
|
|
|
|
}
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($this->getConf('useOpenGraph')) {
|
|
|
|
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_open_graph');
|
|
|
|
}
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($this->getConf('exposeWebService')) {
|
|
|
|
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax');
|
|
|
|
}
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'jsinfo');
|
|
|
|
}
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Export JSON-JD in $JSONINFO array
|
|
|
|
*/
|
|
|
|
public function jsinfo(Doku_Event &$event, $param)
|
|
|
|
{
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
global $JSINFO;
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$JSINFO['plugin']['semantic'] = array(
|
|
|
|
'exposeWebService' => $this->getConf('exposeWebService'),
|
|
|
|
);
|
2015-11-21 15:28:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Export in JSON-LD format
|
|
|
|
*
|
|
|
|
* @param Doku_Event $event handler
|
|
|
|
* @param array $param
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function ajax(Doku_Event &$event, $param)
|
|
|
|
{
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($event->data !== 'plugin_semantic') {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
//no other ajax call handlers needed
|
|
|
|
$event->stopPropagation();
|
|
|
|
$event->preventDefault();
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
global $INPUT;
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$export = $INPUT->str('export');
|
|
|
|
$id = $INPUT->str('id');
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if (!$id) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$this->helper->getMetadata($id);
|
|
|
|
$json_ld = $this->helper->getJsonLD();
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$json = new JSON();
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
header('Content-Type: application/ld+json');
|
|
|
|
print $json->encode($json_ld);
|
|
|
|
return true;
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Expose JSON-JD WebSite schema
|
|
|
|
*
|
|
|
|
* @param Doku_Event $event handler
|
|
|
|
* @param array $params
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function website(Doku_Event &$event, $params)
|
|
|
|
{
|
|
|
|
$event->data["script"][] = array(
|
|
|
|
"type" => "application/ld+json",
|
|
|
|
"_data" => json_encode($this->helper->getWebSite(), JSON_PRETTY_PRINT),
|
|
|
|
);
|
|
|
|
}
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* JSON-LD Event handler
|
|
|
|
*
|
|
|
|
* @param Doku_Event $event handler
|
|
|
|
* @param array $params
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function json_ld(Doku_Event &$event, $params)
|
|
|
|
{
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
global $ID;
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$this->helper->getMetadata($ID);
|
|
|
|
$json_ld = $this->helper->getJsonLD();
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if (!count($json_ld)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$event->data["script"][] = array(
|
|
|
|
"type" => "application/ld+json",
|
|
|
|
"_data" => json_encode($json_ld, JSON_PRETTY_PRINT),
|
|
|
|
);
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Meta Description handler
|
|
|
|
*
|
|
|
|
* @param Doku_Event $event handler
|
|
|
|
* @param array $params
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function meta_description(Doku_Event &$event, $params)
|
|
|
|
{
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
global $ID;
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$this->helper->getMetadata($ID);
|
2015-03-30 08:32:15 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($description = $this->helper->getDescription()) {
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$description = str_replace("\n", ' ', $description);
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$event->data['meta'][] = array(
|
|
|
|
'name' => 'description',
|
|
|
|
'content' => $description,
|
|
|
|
);
|
2015-03-30 08:32:15 +00:00
|
|
|
|
2019-10-08 09:07:33 +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
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Meta Description handler
|
|
|
|
*
|
|
|
|
* @param Doku_Event $event handler
|
|
|
|
* @param array $params
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function meta_author(Doku_Event &$event, $params)
|
|
|
|
{
|
2015-03-25 23:48:00 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
global $ID;
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$this->helper->getMetadata($ID);
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($author = $this->helper->getAuthor()) {
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$event->data['meta'][] = array(
|
|
|
|
'name' => 'author',
|
|
|
|
'content' => $author,
|
|
|
|
);
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2019-10-08 09:07:33 +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
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* OpenGraph handler
|
|
|
|
*
|
|
|
|
* @param Doku_Event $event handler
|
|
|
|
* @param array $params
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function meta_open_graph(Doku_Event &$event, $params)
|
|
|
|
{
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
global $ID;
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$this->helper->getMetadata($ID);
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
foreach ($this->helper->getOpenGraph() as $property => $content) {
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if (!$content) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$event->data['meta'][] = array(
|
|
|
|
'property' => $property,
|
|
|
|
'content' => $content,
|
|
|
|
);
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2016-12-12 22:57:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Dublin Core handler
|
|
|
|
*
|
|
|
|
* @param Doku_Event $event handler
|
|
|
|
* @param array $params
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function meta_dublin_core(Doku_Event &$event, $params)
|
|
|
|
{
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
global $ID;
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$this->helper->getMetadata($ID);
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
foreach ($this->helper->getDublinCore() as $name => $content) {
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if (!$content) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$event->data['meta'][] = array(
|
|
|
|
'name' => $name,
|
|
|
|
'content' => $content,
|
|
|
|
);
|
2015-10-30 15:25:39 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2015-10-30 15:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|