From 5c8fdeb24005603785dd665c711af2deb15e1974 Mon Sep 17 00:00:00 2001 From: Giuseppe Di Terlizzi Date: Sun, 2 Oct 2016 23:55:20 +0200 Subject: [PATCH] Improved performance and stability --- action.php | 39 +++++++++++++++++++------ conf/default.php | 1 + conf/metadata.php | 1 + helper.php | 69 +++++++++++++++++++++++++++++++++++++++----- lang/en/settings.php | 1 + plugin.info.txt | 2 +- 6 files changed, 95 insertions(+), 18 deletions(-) diff --git a/action.php b/action.php index 22db33c..8fb7576 100644 --- a/action.php +++ b/action.php @@ -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 * diff --git a/conf/default.php b/conf/default.php index 7d81720..22b909a 100644 --- a/conf/default.php +++ b/conf/default.php @@ -7,6 +7,7 @@ $conf['useJSONLD'] = 1; $conf['defaultSchemaOrgType'] = 'Article'; +$conf['exposeWebService'] = 1; $conf['useMetaDescription'] = 1; $conf['useMetaAuthor'] = 1; $conf['useDublinCore'] = 0; diff --git a/conf/metadata.php b/conf/metadata.php index 0f64419..b0eec13 100644 --- a/conf/metadata.php +++ b/conf/metadata.php @@ -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'); diff --git a/helper.php b/helper.php index eb91975..a30501b 100644 --- a/helper.php +++ b/helper.php @@ -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; } diff --git a/lang/en/settings.php b/lang/en/settings.php index 1d37773..d8df7a6 100644 --- a/lang/en/settings.php +++ b/lang/en/settings.php @@ -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'; \ No newline at end of file diff --git a/plugin.info.txt b/plugin.info.txt index f6e8b84..a942603 100644 --- a/plugin.info.txt +++ b/plugin.info.txt @@ -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