New helper methods and new AJAX service

This commit is contained in:
Giuseppe Di Terlizzi 2015-11-21 16:28:21 +01:00
parent 4b31cb0eea
commit 6bc097ce97
4 changed files with 108 additions and 50 deletions

View file

@ -17,6 +17,12 @@ if(!defined('DOKU_INC')) die();
*/ */
class action_plugin_semantic extends DokuWiki_Action_Plugin { class action_plugin_semantic extends DokuWiki_Action_Plugin {
private $helper = null;
public function __construct() {
$this->helper =& $this->loadHelper('semantic');
}
/** /**
* Register events * Register events
* *
@ -40,9 +46,47 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_dublin_core'); $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_dublin_core');
} }
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax');
} }
/**
* Export in JSON-LD format
*
* @param Doku_Event &$event
* @return string
*/
public function ajax(Doku_Event &$event, $param) {
if ($event->data !== 'plugin_semantic') {
return false;
}
//no other ajax call handlers needed
$event->stopPropagation();
$event->preventDefault();
global $INPUT;
$export = $INPUT->str('export');
$id = $INPUT->str('id');
if (! $id) return false;
$this->helper->getMetadata($id);
$json_ld = $this->helper->getJsonLD();
//if ($INPUT->str('export') == 'json-ld' && $page = $INPUT->str('page') && $json_ld = $this->helper->getJsonLD()) {
header('Content-Type: application/ld+json');
print json_encode($json_ld);
return true;
//}
return false;
}
/** /**
* JSON-LD Event handler * JSON-LD Event handler
* *
@ -50,9 +94,12 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
*/ */
public function json_ld(Doku_Event &$event, $param) { public function json_ld(Doku_Event &$event, $param) {
$helper = $this->loadHelper('semantic'); global $ID;
if ($json_ld = $helper->getStructuredData()) { $this->helper->getMetadata($ID);
$json_ld = $this->helper->getJsonLD();
if (! count($json_ld)) return false;
$event->data["script"][] = array ( $event->data["script"][] = array (
"type" => "application/ld+json", "type" => "application/ld+json",
@ -61,21 +108,14 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
} }
if ($json_ld_relations = $helper->getBacklinks()) {
$event->data["script"][] = array (
"type" => "application/ld+json",
"_data" => json_encode($json_ld_relations),
);
}
}
public function meta_description(Doku_Event &$event, $params) { public function meta_description(Doku_Event &$event, $params) {
$helper = $this->loadHelper('semantic'); global $ID;
if ($description = $helper->getDescription()) { $this->helper->getMetadata($ID);
if ($description = $this->helper->getDescription()) {
$description = str_replace("\n", ' ', $description); $description = str_replace("\n", ' ', $description);
@ -91,9 +131,11 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
public function meta_author(Doku_Event &$event, $params) { public function meta_author(Doku_Event &$event, $params) {
$helper = $this->loadHelper('semantic'); global $ID;
if ($author = $helper->getAuthor()) { $this->helper->getMetadata($ID);
if ($author = $this->helper->getAuthor()) {
$event->data['meta'][] = array( $event->data['meta'][] = array(
'name' => 'author', 'name' => 'author',
@ -107,9 +149,11 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
public function meta_dublin_core(Doku_Event &$event, $params) { public function meta_dublin_core(Doku_Event &$event, $params) {
$helper = $this->loadHelper('semantic'); global $ID;
foreach ($helper->getDublinCore() as $name => $content) { $this->helper->getMetadata($ID);
foreach ($this->helper->getDublinCore() as $name => $content) {
if (! $content) continue; if (! $content) continue;

View file

@ -12,12 +12,9 @@ if(!defined('DOKU_INC')) die();
class helper_plugin_semantic extends DokuWiki_Plugin { class helper_plugin_semantic extends DokuWiki_Plugin {
private $meta = array(); private $meta = array();
private $page = null;
public function __construct() { public function getMetadata($page) {
$this->meta = $this->getMetadata();
}
private function getMetadata() {
global $INFO; global $INFO;
global $ID; global $ID;
@ -25,13 +22,17 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
global $auth; global $auth;
global $conf; global $conf;
if ((bool) preg_match('/'. trim($this->getConf('excludedPages')) .'/', $ID)) return false; $this->page = cleanID($page);
if (! $INFO['perm']) return false; $auth_check = auth_quickaclcheck($this->page);
$this->meta = $INFO['meta']; if ((bool) preg_match('/'. trim($this->getConf('excludedPages')) .'/', $this->page)) return false;
if (isset($this->meta['semantic']['enabled']) && ! $this->meta['semantic']['enabled']) { if (! $auth_check) return false;
$this->meta = p_get_metadata($page);
if (isset($this->meta['plugin']['semantic']['enabled']) && ! $this->meta['plugin']['semantic']['enabled']) {
return false; return false;
} }
@ -42,8 +43,9 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
} }
public function getSchemaOrgType() { public function getSchemaOrgType() {
return ((isset($this->meta['semantic']['schema.org']['type']))
? $this->meta['semantic']['schema.org']['type'] return ((isset($this->meta['plugin']['semantic']['schema.org']['type']))
? $this->meta['plugin']['semantic']['schema.org']['type']
: $this->getConf('defaultSchemaOrgType')); : $this->getConf('defaultSchemaOrgType'));
} }
@ -88,8 +90,6 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
*/ */
public function getStructuredData() { public function getStructuredData() {
global $INFO;
global $ID;
global $auth; global $auth;
global $conf; global $conf;
@ -99,17 +99,18 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
$type = $this->getSchemaOrgType(); $type = $this->getSchemaOrgType();
$user_data = $auth->getUserData($this->getAuthorID()); $user_data = $auth->getUserData($this->getAuthorID());
$license_url = $license['url']; $license_url = $license['url'];
$page_url = wl($ID, '', true); $page_url = wl($this->page, '', true);
$image_url = $this->getFirstImageURL(); $image_url = $this->getFirstImageURL();
$description = $this->getDescription(); $description = $this->getDescription();
$created = date(DATE_W3C, $this->getCreatedDate()); $created = date(DATE_W3C, $this->getCreatedDate());
$modified = date(DATE_W3C, $this->getModifiedDate()); $modified = date(DATE_W3C, $this->getModifiedDate());
$title = (isset($this->meta['title']) ? $this->meta['title'] : $this->page);
$json_ld = array( $json_ld = array(
'@context' => 'http://schema.org', '@context' => 'http://schema.org',
'@type' => $type, '@type' => $type,
'headline' => @$this->meta['title'], 'headline' => $title,
'name' => @$this->meta['title'], 'name' => $title,
'image' => array($image_url), 'image' => array($image_url),
'datePublished' => $created, 'datePublished' => $created,
'dateCreated' => $created, 'dateCreated' => $created,
@ -146,11 +147,27 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
} }
public function getJsonLD() {
$json_ld = array();
if ($structured_data = $this->getStructuredData()) {
$json_ld[] = $structured_data;
}
if ($backlinks = $this->getBacklinks()) {
$json_ld[] = $backlinks;
}
return $json_ld;
}
public function getBacklinks() { public function getBacklinks() {
global $ID; if (! $backlinks = ft_backlinks($this->page)) return false;
if (! $backlinks = ft_backlinks($ID)) return false;
$json_ld_webpage = array( $json_ld_webpage = array(
'@context' => 'http://schema.org', '@context' => 'http://schema.org',
@ -169,7 +186,6 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
public function getDublinCore() { public function getDublinCore() {
global $conf; global $conf;
global $ID;
if (! $this->meta) return array(); if (! $this->meta) return array();
@ -192,7 +208,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
'DC.Created' => date(DATE_W3C, $this->getCreatedDate()), 'DC.Created' => date(DATE_W3C, $this->getCreatedDate()),
'DC.Modified' => date(DATE_W3C, $this->getModifiedDate()), 'DC.Modified' => date(DATE_W3C, $this->getModifiedDate()),
'DC.Date' => date(DATE_W3C, $this->getCreatedDate()), 'DC.Date' => date(DATE_W3C, $this->getCreatedDate()),
'DC.Identifier' => "urn:$ID", 'DC.Identifier' => "urn:" . $this->page,
); );
return $dublin_core; return $dublin_core;

View file

@ -1,7 +1,7 @@
base semantic base semantic
author Giuseppe Di Terlizzi author Giuseppe Di Terlizzi
email giuseppe.diterlizzi@gmail.com email giuseppe.diterlizzi@gmail.com
date 2015-11-12 date 2015-11-21
name Semantic Plugin name Semantic Plugin
desc Add Semantic Data in DokuWiki desc Add Semantic Data in DokuWiki
url http://www.dokuwiki.org/plugin:semantic url http://www.dokuwiki.org/plugin:semantic

View file

@ -34,23 +34,21 @@ class syntax_plugin_semantic extends DokuWiki_Syntax_Plugin {
function render($mode, Doku_Renderer $renderer, $data) { function render($mode, Doku_Renderer $renderer, $data) {
if ($mode == 'xthml') { if ($mode == 'metadata') {
return true; // don't output anything
} elseif ($mode == 'metadata') {
list($match, $state, $pos) = $data; list($match, $state, $pos) = $data;
if ($match == '~~NOSEMANTIC~~') { if ($match == '~~NOSEMANTIC~~') {
$renderer->meta['semantic']['enabled'] = false; $renderer->meta['plugin']['semantic']['enabled'] = false;
} else { } else {
$renderer->meta['semantic']['schema.org']['type'] = trim(str_replace('Schema.org/', '', $match), '~~'); $renderer->meta['plugin']['semantic']['schema.org']['type'] = trim(str_replace('Schema.org/', '', $match), '~~');
$renderer->meta['semantic']['enabled'] = true; $renderer->meta['plugin']['semantic']['enabled'] = true;
} }
} }
return false;
} }
} }