Added "hideMail" option for enable/disable email in Semantic data

This option is useful for reduce loading of page when using "AuthLDAP/AD" plugin.

#14 #15
This commit is contained in:
Giuseppe Di Terlizzi 2019-10-08 11:07:33 +02:00
parent da1aaf0cdd
commit c41859fe17
6 changed files with 583 additions and 424 deletions

View file

@ -4,31 +4,36 @@
* *
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com> * @author Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
* @copyright (C) 2015, Giuseppe Di Terlizzi * @copyright (C) 2015-2019, Giuseppe Di Terlizzi
*/ */
// must be run within Dokuwiki // must be run within Dokuwiki
if(!defined('DOKU_INC')) die(); if (!defined('DOKU_INC')) {
die();
}
/** /**
* Class Semantic Action Plugin * Class Semantic Action Plugin
* *
* Add semantic data to DokuWiki * Add semantic data to DokuWiki
*/ */
class action_plugin_semantic extends DokuWiki_Action_Plugin { class action_plugin_semantic extends DokuWiki_Action_Plugin
{
private $helper = null; private $helper = null;
public function __construct() { public function __construct()
{
$this->helper = $this->loadHelper('semantic'); $this->helper = $this->loadHelper('semantic');
} }
/** /**
* Register events * Register events
* *
* @param Doku_Event_Handler $controller * @param Doku_Event_Handler $controller handler
*/ */
public function register(Doku_Event_Handler $controller) { public function register(Doku_Event_Handler $controller)
{
if ($this->getConf('useJSONLD')) { if ($this->getConf('useJSONLD')) {
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'website'); $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'website');
@ -58,8 +63,11 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'jsinfo'); $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'jsinfo');
} }
/**
public function jsinfo(Doku_Event &$event, $param) { * Export JSON-JD in $JSONINFO array
*/
public function jsinfo(Doku_Event &$event, $param)
{
global $JSINFO; global $JSINFO;
@ -72,10 +80,13 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
/** /**
* Export in JSON-LD format * Export in JSON-LD format
* *
* @param Doku_Event &$event * @param Doku_Event $event handler
* @param array $param
*
* @return string * @return string
*/ */
public function ajax(Doku_Event &$event, $param) { public function ajax(Doku_Event &$event, $param)
{
if ($event->data !== 'plugin_semantic') { if ($event->data !== 'plugin_semantic') {
return false; return false;
@ -90,7 +101,9 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
$export = $INPUT->str('export'); $export = $INPUT->str('export');
$id = $INPUT->str('id'); $id = $INPUT->str('id');
if (! $id) return false; if (!$id) {
return false;
}
$this->helper->getMetadata($id); $this->helper->getMetadata($id);
$json_ld = $this->helper->getJsonLD(); $json_ld = $this->helper->getJsonLD();
@ -103,38 +116,59 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
} }
/**
public function website(Doku_Event &$event, $param) { * Expose JSON-JD WebSite schema
$event->data["script"][] = array ( *
* @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", "type" => "application/ld+json",
"_data" => json_encode($this->helper->getWebSite()), "_data" => json_encode($this->helper->getWebSite(), JSON_PRETTY_PRINT),
); );
} }
/** /**
* JSON-LD Event handler * JSON-LD Event handler
* *
* @param Doku_Event &$event * @param Doku_Event $event handler
* @param array $params
*
* @return void
*/ */
public function json_ld(Doku_Event &$event, $param) { public function json_ld(Doku_Event &$event, $params)
{
global $ID; global $ID;
$this->helper->getMetadata($ID); $this->helper->getMetadata($ID);
$json_ld = $this->helper->getJsonLD(); $json_ld = $this->helper->getJsonLD();
if (! count($json_ld)) return false; if (!count($json_ld)) {
return false;
}
$event->data["script"][] = array ( $event->data["script"][] = array(
"type" => "application/ld+json", "type" => "application/ld+json",
"_data" => json_encode($json_ld), "_data" => json_encode($json_ld, JSON_PRETTY_PRINT),
); );
} }
/**
public function meta_description(Doku_Event &$event, $params) { * Meta Description handler
*
* @param Doku_Event $event handler
* @param array $params
*
* @return void
*/
public function meta_description(Doku_Event &$event, $params)
{
global $ID; global $ID;
@ -153,8 +187,16 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
} }
/**
public function meta_author(Doku_Event &$event, $params) { * Meta Description handler
*
* @param Doku_Event $event handler
* @param array $params
*
* @return void
*/
public function meta_author(Doku_Event &$event, $params)
{
global $ID; global $ID;
@ -171,8 +213,16 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
} }
/**
public function meta_open_graph(Doku_Event &$event, $params) { * OpenGraph handler
*
* @param Doku_Event $event handler
* @param array $params
*
* @return void
*/
public function meta_open_graph(Doku_Event &$event, $params)
{
global $ID; global $ID;
@ -180,7 +230,9 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
foreach ($this->helper->getOpenGraph() as $property => $content) { foreach ($this->helper->getOpenGraph() as $property => $content) {
if (! $content) continue; if (!$content) {
continue;
}
$event->data['meta'][] = array( $event->data['meta'][] = array(
'property' => $property, 'property' => $property,
@ -191,8 +243,16 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
} }
/**
public function meta_dublin_core(Doku_Event &$event, $params) { * Dublin Core handler
*
* @param Doku_Event $event handler
* @param array $params
*
* @return void
*/
public function meta_dublin_core(Doku_Event &$event, $params)
{
global $ID; global $ID;
@ -200,7 +260,9 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin {
foreach ($this->helper->getDublinCore() as $name => $content) { foreach ($this->helper->getDublinCore() as $name => $content) {
if (! $content) continue; if (!$content) {
continue;
}
$event->data['meta'][] = array( $event->data['meta'][] = array(
'name' => $name, 'name' => $name,

View file

@ -13,3 +13,4 @@ $conf['useMetaAuthor'] = 1;
$conf['useDublinCore'] = 0; $conf['useDublinCore'] = 0;
$conf['useOpenGraph'] = 0; $conf['useOpenGraph'] = 0;
$conf['excludedPages'] = '(wiki|playground)'; $conf['excludedPages'] = '(wiki|playground)';
$conf['hideMail'] = 0;

View file

@ -13,3 +13,4 @@ $meta['useJSONLD'] = array('onoff');
$meta['exposeWebService'] = array('onoff'); $meta['exposeWebService'] = array('onoff');
$meta['defaultSchemaOrgType'] = array('multichoice','_choices' => array('Article', 'NewsArticle', 'TechArticle', 'BlogPosting', 'Recipe')); $meta['defaultSchemaOrgType'] = array('multichoice','_choices' => array('Article', 'NewsArticle', 'TechArticle', 'BlogPosting', 'Recipe'));
$meta['excludedPages'] = array('regex'); $meta['excludedPages'] = array('regex');
$meta['hideMail'] = array('onoff');

View file

@ -4,18 +4,27 @@
* *
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com> * @author Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
* @copyright (C) 2015-2019, Giuseppe Di Terlizzi
*/ */
// must be run within Dokuwiki // must be run within Dokuwiki
if(!defined('DOKU_INC')) die(); 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; private $page = null;
/**
public function getWebSite() { * Get Schema.org WebSite
*
* @return array
*/
public function getWebSite()
{
global $conf; global $conf;
@ -26,17 +35,24 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
'name' => $conf['title'], 'name' => $conf['title'],
'potentialAction' => array( 'potentialAction' => array(
'@type' => 'SearchAction', '@type' => 'SearchAction',
'target' => DOKU_URL.DOKU_SCRIPT.'?do=search&amp;id={search_term_string}', 'target' => DOKU_URL . DOKU_SCRIPT . '?do=search&amp;id={search_term_string}',
'query-input' => 'required name=search_term_string' 'query-input' => 'required name=search_term_string',
) ),
); );
return $json_ld; return $json_ld;
} }
/**
public function getMetadata($page) { * Get the metadata of the page
*
* @param string $page ID
*
* @return string
*/
public function getMetadata($page)
{
global $INFO; global $INFO;
global $ID; global $ID;
@ -48,62 +64,128 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
$auth_check = auth_quickaclcheck($this->page); $auth_check = auth_quickaclcheck($this->page);
if ((bool) preg_match('/'. trim($this->getConf('excludedPages')) .'/', $this->page)) return false; if ((bool) preg_match('/' . trim($this->getConf('excludedPages')) . '/', $this->page)) {
if (! $auth_check) return false;
$this->meta = p_get_metadata($this->page);
if (isset($this->meta['plugin']['semantic']['enabled']) && ! $this->meta['plugin']['semantic']['enabled']) {
return false; return false;
} }
if (! isset($this->meta['date']) || $this->meta['date'] == '') return false; if (!$auth_check) {
return false;
}
$this->meta = p_get_metadata($this->page);
if (isset($this->meta['plugin']['semantic']['enabled']) && !$this->meta['plugin']['semantic']['enabled']) {
return false;
}
if (!isset($this->meta['date']) || $this->meta['date'] == '') {
return false;
}
return $this->meta; return $this->meta;
} }
public function getSchemaOrgType() { /**
* Get Schema.Org page type
*
* @return string
*/
public function getSchemaOrgType()
{
return ((isset($this->meta['plugin']['semantic']['schema.org']['type'])) return ((isset($this->meta['plugin']['semantic']['schema.org']['type']))
? $this->meta['plugin']['semantic']['schema.org']['type'] ? $this->meta['plugin']['semantic']['schema.org']['type']
: $this->getConf('defaultSchemaOrgType')); : $this->getConf('defaultSchemaOrgType'));
} }
public function getFirstImage() { /**
* Get the first image in page
*
* @return string
*/
public function getFirstImage()
{
return ((@$this->meta['relation']['firstimage']) ? $this->meta['relation']['firstimage'] : null); return ((@$this->meta['relation']['firstimage']) ? $this->meta['relation']['firstimage'] : null);
} }
public function getFirstImageURL() { /**
* Get the URL of the first image in page
*
* @return string
*/
public function getFirstImageURL()
{
return ($this->getFirstImage() ? ml($this->getFirstImage(), '', true, '&amp;', true) : null); return ($this->getFirstImage() ? ml($this->getFirstImage(), '', true, '&amp;', true) : null);
} }
public function getDescription() { /**
return (@$this->meta['description']['abstract'] ? $this->meta['description']['abstract']: $this->getTitle()); * Get page description
*
* @return string
*/
public function getDescription()
{
return (@$this->meta['description']['abstract'] ? $this->meta['description']['abstract'] : $this->getTitle());
} }
public function getAuthor() { /**
* Get author name
*
* @return string
*/
public function getAuthor()
{
return ($this->meta['creator'] ? $this->meta['creator'] : null); return ($this->meta['creator'] ? $this->meta['creator'] : null);
} }
public function getAuthorID() { /**
* Get author ID
*
* @return string
*/
public function getAuthorID()
{
return ($this->meta['user'] ? $this->meta['user'] : null); return ($this->meta['user'] ? $this->meta['user'] : null);
} }
public function getTitle() { /**
* Get the page title
*
* @return string
*/
public function getTitle()
{
return (@$this->meta['title'] ? $this->meta['title'] : null); return (@$this->meta['title'] ? $this->meta['title'] : null);
} }
public function getCreatedDate() { /**
* Get the create date of page
*
* @return int
*/
public function getCreatedDate()
{
return ((@$this->meta['date']['created']) ? $this->meta['date']['created'] : -1); return ((@$this->meta['date']['created']) ? $this->meta['date']['created'] : -1);
} }
public function getModifiedDate() { /**
* Get the modified date of page
*
* @return int
*/
public function getModifiedDate()
{
return ((@$this->meta['date']['modified']) ? $this->meta['date']['modified'] : -1); return ((@$this->meta['date']['modified']) ? $this->meta['date']['modified'] : -1);
} }
public function getLicense() { /**
* Get DokuWiki license
*
* @return string
*/
public function getLicense()
{
global $license; global $license;
global $conf; global $conf;
return @$license[$conf['license']]; return @$license[$conf['license']];
@ -114,16 +196,19 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
* *
* @return array * @return array
*/ */
public function getStructuredData() { public function getStructuredData()
{
global $auth; global $auth;
global $conf; global $conf;
if (! count($this->meta)) return false; if (!count($this->meta)) {
return false;
}
$license = $this->getLicense(); $license = $this->getLicense();
$type = $this->getSchemaOrgType(); $type = $this->getSchemaOrgType();
$user_data = $auth->getUserData($this->getAuthorID()); $user_data = ($this->getConf('hideMail') ? array('mail' => null) : $auth->getUserData($this->getAuthorID()));
$license_url = $license['url']; $license_url = $license['url'];
$page_url = wl($this->page, '', true); $page_url = wl($this->page, '', true);
$description = str_replace("\n", ' ', $this->getDescription()); $description = str_replace("\n", ' ', $this->getDescription());
@ -133,7 +218,6 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
$wiki_logo_info = array(); $wiki_logo_info = array();
$wiki_logo = tpl_getMediaFile(array(':wiki:logo.png', ':logo.png', 'images/logo.png'), true, $wiki_logo_info); $wiki_logo = tpl_getMediaFile(array(':wiki:logo.png', ':logo.png', 'images/logo.png'), true, $wiki_logo_info);
$json_ld = array( $json_ld = array(
'@context' => 'http://schema.org', '@context' => 'http://schema.org',
'@type' => $type, '@type' => $type,
@ -158,7 +242,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
'@type' => 'ImageObject', '@type' => 'ImageObject',
'url' => $wiki_logo, 'url' => $wiki_logo,
), ),
) ),
); );
@ -187,17 +271,19 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
'@context' => 'http://schema.org', '@context' => 'http://schema.org',
'@type' => 'Person', '@type' => 'Person',
'name' => $author, 'name' => $author,
'email' => $user_data['mail'] 'email' => $user_data['mail'],
); );
if (isset($this->meta['contributor'])) { if (isset($this->meta['contributor'])) {
foreach ($this->meta['contributor'] as $uid => $fullname) { foreach ($this->meta['contributor'] as $uid => $fullname) {
$contributor_data = $auth->getUserData($uid);
$contributor_data = ($this->getConf('hideMail') ? array('mail' => null) : $auth->getUserData($uid));
$json_ld['contributor'][] = array( $json_ld['contributor'][] = array(
'@context' => 'http://schema.org', '@context' => 'http://schema.org',
'@type' => 'Person', '@type' => 'Person',
'name' => $fullname, 'name' => $fullname,
'email' => $contributor_data['mail'] 'email' => $contributor_data['mail'],
); );
} }
} }
@ -208,8 +294,8 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
} }
public function getJsonLD()
public function getJsonLD() { {
$json_ld = array(); $json_ld = array();
@ -225,30 +311,36 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
} }
public function getBacklinks()
{
public function getBacklinks() { if (!$backlinks = ft_backlinks($this->page)) {
return false;
if (! $backlinks = ft_backlinks($this->page)) return false; }
$json_ld_webpage = array( $json_ld_webpage = array(
'@context' => 'http://schema.org', '@context' => 'http://schema.org',
'@type' => 'WebPage' '@type' => 'WebPage',
); );
foreach ($backlinks as $pageid) { foreach ($backlinks as $pageid) {
$json_ld_webpage['relatedLink'][] = wl($pageid, '', true); $json_ld_webpage['relatedLink'][] = wl($pageid, '', true);
} }
if (isset($json_ld_webpage['relatedLink'])) return $json_ld_webpage; if (isset($json_ld_webpage['relatedLink'])) {
return $json_ld_webpage;
}
} }
public function getDublinCore()
public function getDublinCore() { {
global $conf; global $conf;
if (! $this->meta) return array(); if (!$this->meta) {
return array();
}
$license = $this->getLicense(); $license = $this->getLicense();
$contributors = array(); $contributors = array();
@ -276,12 +368,14 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
} }
public function getOpenGraph()
public function getOpenGraph() { {
global $conf; global $conf;
if (! $this->meta) return array(); if (!$this->meta) {
return array();
}
$locale = $conf['lang']; $locale = $conf['lang'];

View file

@ -15,3 +15,4 @@ $lang['useJSONLD'] = 'Add JSON-LD';
$lang['useMetaAuthor'] = 'Add author meta tag'; $lang['useMetaAuthor'] = 'Add author meta tag';
$lang['useMetaDescription'] = 'Add description meta tag'; $lang['useMetaDescription'] = 'Add description meta tag';
$lang['useOpenGraph'] = 'Add Open Graph protocol metadata'; $lang['useOpenGraph'] = 'Add Open Graph protocol metadata';
$lang['hideMail'] = 'Hide Author e-Mail address';

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 2018-08-25 date 2019-10-08
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