Improved performance and stability

This commit is contained in:
Giuseppe Di Terlizzi 2016-10-02 23:55:20 +02:00
parent 82b6a676e4
commit 5c8fdeb240
6 changed files with 95 additions and 18 deletions

View file

@ -20,7 +20,7 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
private $helper = null;
public function __construct() {
$this->helper =& $this->loadHelper('semantic');
$this->helper = $this->loadHelper('semantic');
}
/**
@ -31,6 +31,7 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
public function register(Doku_Event_Handler $controller) {
if ($this->getConf('useJSONLD')) {
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'website');
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'json_ld');
}
@ -46,11 +47,24 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_dublin_core');
}
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax');
if ($this->getConf('exposeWebService')) {
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax');
}
}
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'jsinfo');
}
public function jsinfo(Doku_Event &$event, $param) {
global $JSINFO;
$JSINFO['plugin']['semantic'] = array(
'exposeWebService' => $this->getConf('exposeWebService'),
);
}
/**
* Export in JSON-LD format
*
@ -77,16 +91,23 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
$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;
//}
$json = new JSON();
return false;
header('Content-Type: application/ld+json');
print $json->encode($json_ld);
return true;
}
public function website(Doku_Event &$event, $param) {
$event->data["script"][] = array (
"type" => "application/ld+json",
"_data" => json_encode($this->helper->getWebSite()),
);
}
/**
* JSON-LD Event handler
*

View file

@ -7,6 +7,7 @@
$conf['useJSONLD'] = 1;
$conf['defaultSchemaOrgType'] = 'Article';
$conf['exposeWebService'] = 1;
$conf['useMetaDescription'] = 1;
$conf['useMetaAuthor'] = 1;
$conf['useDublinCore'] = 0;

View file

@ -9,5 +9,6 @@ $meta['useMetaDescription'] = array('onoff');
$meta['useMetaAuthor'] = array('onoff');
$meta['useDublinCore'] = array('onoff');
$meta['useJSONLD'] = array('onoff');
$meta['exposeWebService'] = array('onoff');
$meta['defaultSchemaOrgType'] = array('multichoice','_choices' => array('Article', 'NewsArticle', 'TechArticle', 'BlogPosting', 'Recipe'));
$meta['excludedPages'] = array('regex');

View file

@ -14,6 +14,28 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
private $meta = array();
private $page = null;
public function getWebSite() {
global $conf;
$json_ld = array(
'@context' => 'http://schema.org',
'@type' => 'Website',
'url' => DOKU_URL,
'name' => $conf['title'],
'potentialAction' => array(
'@type' => 'SearchAction',
'target' => DOKU_URL.DOKU_SCRIPT.'?do=search&id={search_term_string}',
'query-input' => 'required name=search_term_string'
)
);
return $json_ld;
}
public function getMetadata($page) {
global $INFO;
@ -49,12 +71,16 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
: $this->getConf('defaultSchemaOrgType'));
}
public function getFirstImage() {
return ((@$this->meta['relation']['firstimage']) ? $this->meta['relation']['firstimage'] : null);
}
public function getFirstImageURL() {
return (($this->meta['relation']['firstimage']) ? ml($this->meta['relation']['firstimage'], '', true, '&', true) : null);
return ($this->getFirstImage() ? ml($this->getFirstImage(), '', true, '&', true) : null);
}
public function getDescription() {
return trim(ltrim($this->meta['description']['abstract'], @$this->meta['title']));
return trim(ltrim(@$this->meta['description']['abstract'], $this->getTitle()));
}
public function getAuthor() {
@ -66,15 +92,15 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
}
public function getTitle() {
return ($this->meta['title'] ? $this->meta['title'] : null);
return (@$this->meta['title'] ? $this->meta['title'] : null);
}
public function getCreatedDate() {
return $this->meta['date']['created'];
return ((@$this->meta['date']['created']) ? $this->meta['date']['created'] : -1);
}
public function getModifiedDate() {
return $this->meta['date']['modified'];
return ((@$this->meta['date']['modified']) ? $this->meta['date']['modified'] : -1);
}
public function getLicense() {
@ -100,7 +126,6 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
$user_data = $auth->getUserData($this->getAuthorID());
$license_url = $license['url'];
$page_url = wl($this->page, '', true);
$image_url = $this->getFirstImageURL();
$description = $this->getDescription();
$created = date(DATE_W3C, $this->getCreatedDate());
$modified = date(DATE_W3C, $this->getModifiedDate());
@ -111,15 +136,31 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
'@type' => $type,
'headline' => $title,
'name' => $title,
'image' => array($image_url),
'datePublished' => $created,
'dateCreated' => $created,
'dateModified' => $modified,
'description' => $description,
'license' => $license_url,
'url' => $page_url,
'mainEntityOfPage' => array(
'@type' => 'WebPage',
'@id' => $page_url,
),
);
if ($image_url = $this->getFirstImageURL()) {
$image_info = array();
$article_image = tpl_getMediaFile(array($this->getFirstImage()), true, $logo_info);
$json_ld['image'] = array(
'@type' => 'ImageObject',
'url' => $image_url,
'width' => 0,
'height' => 0,
);
}
if ($author = $this->getAuthor()) {
$json_ld['creator'] = array(
@ -129,6 +170,18 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
'email' => $user_data['mail']
);
$logo_info = array();
$wiki_logo = tpl_getMediaFile(array(':wiki:logo.png', ':logo.png', 'images/logo.png'), true, $logo_info);
$json_ld['author'] = $json_ld['creator'];
$json_ld['publisher'] = $json_ld['creator'];
$json_ld['publisher']['logo'] = array(
'@type' => 'ImageObject',
'url' => $wiki_logo,
'width' => $logo_info[0],
'height' => $logo_info[1],
);
if (isset($this->meta['contributor'])) {
foreach ($this->meta['contributor'] as $uid => $fullname) {
$contributor_data = $auth->getUserData($uid);
@ -192,7 +245,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
$license = $this->getLicense();
$contributors = array();
if (isset($this->meta['contributor'])) {
if (isset($this->meta['contributor']) && is_array($this->meta['contributor'])) {
foreach ($this->meta['contributor'] as $uid => $fullname) {
$contributors[] = $fullname;
}

View file

@ -13,3 +13,4 @@ $lang['useDublinCore'] = 'Add Dublin Core metadata';
$lang['useJSONLD'] = 'Add JSON-LD';
$lang['defaultSchemaOrgType'] = 'Default Schema.org type for JSON-LD';
$lang['excludedPages'] = 'Excluded pages (insert a regex)';
$lang['exposeWebService'] = 'Expose Ajax WebService';

View file

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