Compare commits

..

No commits in common. "679e377c92e930bdac4626f0778ea3649fdbb7af" and "da1aaf0cdd892b1af007b6d5edd7e97ad48d2660" have entirely different histories.

9 changed files with 479 additions and 631 deletions

View file

@ -1,13 +1,16 @@
language: php language: php
php: php:
- "7.3"
- "7.2" - "7.2"
- "7.1" - "7.1"
- "7.0" - "7.0"
- "5.6" - "5.6"
- "5.5"
- "5.4"
env: env:
- DOKUWIKI=master - DOKUWIKI=master
- DOKUWIKI=stable - DOKUWIKI=stable
- DOKUWIKI=old-stable
before_install: wget https://raw.github.com/splitbrain/dokuwiki-travis/master/travis.sh before_install: wget https://raw.github.com/splitbrain/dokuwiki-travis/master/travis.sh
install: sh travis.sh install: sh travis.sh
script: cd _test && ./phpunit.phar --stderr --group plugin_semantic script: cd _test && phpunit --stderr --group plugin_semantic

View file

@ -6,15 +6,13 @@
* @group plugin_semantic * @group plugin_semantic
* @group plugins * @group plugins
*/ */
class general_plugin_semantic_test extends DokuWikiTest class general_plugin_semantic_test extends DokuWikiTest {
{
/** /**
* Simple test to make sure the plugin.info.txt is in correct format * Simple test to make sure the plugin.info.txt is in correct format
*/ */
public function test_plugininfo() public function test_plugininfo() {
{ $file = __DIR__.'/../plugin.info.txt';
$file = __DIR__ . '/../plugin.info.txt';
$this->assertFileExists($file); $this->assertFileExists($file);
$info = confToHash($file); $info = confToHash($file);
@ -34,3 +32,4 @@ class general_plugin_semantic_test extends DokuWikiTest
$this->assertTrue(false !== strtotime($info['date'])); $this->assertTrue(false !== strtotime($info['date']));
} }
} }

View file

@ -4,32 +4,31 @@
* *
* @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-2023, Giuseppe Di Terlizzi * @copyright (C) 2015, Giuseppe Di Terlizzi
*/ */
// must be run within Dokuwiki
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 handler * @param Doku_Event_Handler $controller
*/ */
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');
@ -59,11 +58,8 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'jsinfo'); $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'jsinfo');
} }
/**
* Export JSON-JD in $JSONINFO array public function jsinfo(Doku_Event &$event, $param) {
*/
public function jsinfo(Doku_Event &$event, $param)
{
global $JSINFO; global $JSINFO;
@ -76,13 +72,10 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin
/** /**
* Export in JSON-LD format * Export in JSON-LD format
* *
* @param Doku_Event $event handler * @param Doku_Event &$event
* @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;
@ -97,72 +90,51 @@ 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) { if (! $id) return false;
return false;
}
$this->helper->getMetadata($id); $this->helper->getMetadata($id);
$json_ld = $this->helper->getJsonLD(); $json_ld = $this->helper->getJsonLD();
$json = new JSON();
header('Content-Type: application/ld+json'); header('Content-Type: application/ld+json');
print json_encode($json_ld); print $json->encode($json_ld);
return true; return true;
} }
/**
* Expose JSON-JD WebSite schema public function website(Doku_Event &$event, $param) {
* $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(), JSON_PRETTY_PRINT), "_data" => json_encode($this->helper->getWebSite()),
); );
} }
/** /**
* JSON-LD Event handler * JSON-LD Event handler
* *
* @param Doku_Event $event handler * @param Doku_Event &$event
* @param array $params
*
* @return void
*/ */
public function json_ld(Doku_Event &$event, $params) public function json_ld(Doku_Event &$event, $param) {
{
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)) { if (! count($json_ld)) return false;
return false;
}
$event->data["script"][] = array( $event->data["script"][] = array (
"type" => "application/ld+json", "type" => "application/ld+json",
"_data" => json_encode($json_ld, JSON_PRETTY_PRINT), "_data" => json_encode($json_ld),
); );
} }
/**
* Meta Description handler public function meta_description(Doku_Event &$event, $params) {
*
* @param Doku_Event $event handler
* @param array $params
*
* @return void
*/
public function meta_description(Doku_Event &$event, $params)
{
global $ID; global $ID;
@ -181,16 +153,8 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin
} }
/**
* Meta Description handler public function meta_author(Doku_Event &$event, $params) {
*
* @param Doku_Event $event handler
* @param array $params
*
* @return void
*/
public function meta_author(Doku_Event &$event, $params)
{
global $ID; global $ID;
@ -207,16 +171,8 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin
} }
/**
* OpenGraph handler public function meta_open_graph(Doku_Event &$event, $params) {
*
* @param Doku_Event $event handler
* @param array $params
*
* @return void
*/
public function meta_open_graph(Doku_Event &$event, $params)
{
global $ID; global $ID;
@ -224,9 +180,7 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin
foreach ($this->helper->getOpenGraph() as $property => $content) { foreach ($this->helper->getOpenGraph() as $property => $content) {
if (!$content) { if (! $content) continue;
continue;
}
$event->data['meta'][] = array( $event->data['meta'][] = array(
'property' => $property, 'property' => $property,
@ -237,16 +191,8 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin
} }
/**
* Dublin Core handler public function meta_dublin_core(Doku_Event &$event, $params) {
*
* @param Doku_Event $event handler
* @param array $params
*
* @return void
*/
public function meta_dublin_core(Doku_Event &$event, $params)
{
global $ID; global $ID;
@ -254,9 +200,7 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin
foreach ($this->helper->getDublinCore() as $name => $content) { foreach ($this->helper->getDublinCore() as $name => $content) {
if (!$content) { if (! $content) continue;
continue;
}
$event->data['meta'][] = array( $event->data['meta'][] = array(
'name' => $name, 'name' => $name,

View file

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

View file

@ -11,6 +11,5 @@ $meta['useDublinCore'] = array('onoff');
$meta['useOpenGraph'] = array('onoff'); $meta['useOpenGraph'] = array('onoff');
$meta['useJSONLD'] = array('onoff'); $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,50 +4,39 @@
* *
* @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-2022, Giuseppe Di Terlizzi
*/ */
class helper_plugin_semantic extends DokuWiki_Plugin // must be run within Dokuwiki
{ if(!defined('DOKU_INC')) die();
class helper_plugin_semantic extends DokuWiki_Plugin {
private $meta = array(); private $meta = array();
private $page = null; private $page = null;
/**
* Get Schema.org WebSite public function getWebSite() {
*
* @return array
*/
public function getWebSite()
{
global $conf; global $conf;
$json_ld = array( $json_ld = array(
'@context' => 'http://schema.org/', '@context' => 'http://schema.org',
'@type' => 'WebSite', '@type' => 'WebSite',
'url' => DOKU_URL, 'url' => DOKU_URL,
'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;
} }
/**
* Get the metadata of the page public function getMetadata($page) {
*
* @param string $page ID
*
* @return string
*/
public function getMetadata($page)
{
global $INFO; global $INFO;
global $ID; global $ID;
@ -59,134 +48,65 @@ 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)) { if ((bool) preg_match('/'. trim($this->getConf('excludedPages')) .'/', $this->page)) return false;
return false;
}
if (!$auth_check) { if (! $auth_check) return false;
return false;
}
$this->meta = p_get_metadata($this->page); $this->meta = p_get_metadata($this->page);
if (isset($this->meta['plugin']['semantic']['enabled']) && !$this->meta['plugin']['semantic']['enabled']) { if (isset($this->meta['plugin']['semantic']['enabled']) && ! $this->meta['plugin']['semantic']['enabled']) {
return false; return false;
} }
if (!isset($this->meta['date']) || $this->meta['date'] == '') { if (! isset($this->meta['date']) || $this->meta['date'] == '') return false;
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 ((@$this->meta['relation']['firstimage']) ? $this->meta['relation']['firstimage'] : null);
*
* @return string
*/
public function getFirstImage()
{
return (isset($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() {
* Get page description return (@$this->meta['description']['abstract'] ? $this->meta['description']['abstract']: $this->getTitle());
*
* @return string
*/
public function getDescription()
{
return (isset($this->meta['description']['abstract']) ? $this->meta['description']['abstract'] : $this->getTitle());
} }
/** public function getAuthor() {
* Get author name return ($this->meta['creator'] ? $this->meta['creator'] : null);
*
* @return string
*/
public function getAuthor()
{
return ( array_key_exists('creator', $this->meta ) ?
$this->meta['creator'] : null;
} }
/** public function getAuthorID() {
* Get author ID return ($this->meta['user'] ? $this->meta['user'] : null);
*
* @return string
*/
public function getAuthorID()
{
return ( array_key_exists('user', $this->meta ) ?
$this->meta['user'] : null;
} }
/** public function getTitle() {
* Get the page title return (@$this->meta['title'] ? $this->meta['title'] : null);
*
* @return string
*/
public function getTitle()
{
return (isset($this->meta['title']) ? $this->meta['title'] : null);
} }
/** public function getCreatedDate() {
* Get the create date of page return ((@$this->meta['date']['created']) ? $this->meta['date']['created'] : -1);
*
* @return int
*/
public function getCreatedDate()
{
return (isset($this->meta['date']['created']) ? $this->meta['date']['created'] : -1);
} }
/** public function getModifiedDate() {
* Get the modified date of page return ((@$this->meta['date']['modified']) ? $this->meta['date']['modified'] : -1);
*
* @return int
*/
public function getModifiedDate()
{
return (isset($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 (isset($license[$conf['license']]) ? $license[$conf['license']] : null);
} }
/** /**
@ -194,19 +114,16 @@ 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)) { if (! count($this->meta)) return false;
return false;
}
$license = $this->getLicense(); $license = $this->getLicense();
$type = $this->getSchemaOrgType(); $type = $this->getSchemaOrgType();
$user_data = ($this->getConf('hideMail') ? array('mail' => null) : $auth->getUserData($this->getAuthorID())); $user_data = $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());
@ -216,8 +133,9 @@ 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,
'headline' => $title, 'headline' => $title,
'name' => $title, 'name' => $title,
@ -240,7 +158,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin
'@type' => 'ImageObject', '@type' => 'ImageObject',
'url' => $wiki_logo, 'url' => $wiki_logo,
), ),
), )
); );
@ -266,22 +184,20 @@ class helper_plugin_semantic extends DokuWiki_Plugin
if ($author = $this->getAuthor()) { if ($author = $this->getAuthor()) {
$json_ld['author'] = array( $json_ld['author'] = array(
'@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']
); );
} }
} }
@ -292,8 +208,8 @@ class helper_plugin_semantic extends DokuWiki_Plugin
} }
public function getJsonLD()
{ public function getJsonLD() {
$json_ld = array(); $json_ld = array();
@ -309,36 +225,30 @@ class helper_plugin_semantic extends DokuWiki_Plugin
} }
public function getBacklinks()
{
if (!$backlinks = ft_backlinks($this->page)) { public function getBacklinks() {
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'])) { if (isset($json_ld_webpage['relatedLink'])) return $json_ld_webpage;
return $json_ld_webpage;
}
} }
public function getDublinCore()
{ public function getDublinCore() {
global $conf; global $conf;
if (!$this->meta) { if (! $this->meta) return array();
return array();
}
$license = $this->getLicense(); $license = $this->getLicense();
$contributors = array(); $contributors = array();
@ -366,14 +276,12 @@ class helper_plugin_semantic extends DokuWiki_Plugin
} }
public function getOpenGraph()
{ public function getOpenGraph() {
global $conf; global $conf;
if (!$this->meta) { if (! $this->meta) return array();
return array();
}
$locale = $conf['lang']; $locale = $conf['lang'];

View file

@ -15,4 +15,3 @@ $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, patched by vv221 for Numenaute author Giuseppe Di Terlizzi
email numenaute@vv221.fr email giuseppe.diterlizzi@gmail.com
date 2024-10-04 date 2018-08-25
name Semantic Plugin name Semantic Plugin
desc Add Semantic Data in DokuWiki desc Add Semantic Data in DokuWiki
url https://git.vv221.fr/numenaute.org/dokuwiki-plugin-semantic/about/ url http://www.dokuwiki.org/plugin:semantic

View file

@ -4,24 +4,23 @@
* *
* @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-2023, Giuseppe Di Terlizzi
*/ */
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
class syntax_plugin_semantic extends DokuWiki_Syntax_Plugin if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
{
class syntax_plugin_semantic extends DokuWiki_Syntax_Plugin {
private $macros = array( private $macros = array(
'~~NewsArticle~~', '~~Article~~', '~~TechArticle~~', '~~NewsArticle~~', '~~Article~~', '~~TechArticle~~',
'~~BlogPosting~~', '~~Recipe~~', '~~NOSEMANTIC~~', '~~BlogPosting~~', '~~Recipe~~', '~~NOSEMANTIC~~'
); );
public function getType() function getType() { return 'substition'; }
{return 'substition';} function getSort() { return 99; }
public function getSort()
{return 99;}
public function connectTo($mode) function connectTo($mode) {
{
foreach ($this->macros as $macro) { foreach ($this->macros as $macro) {
$this->Lexer->addSpecialPattern($macro, $mode, 'plugin_semantic'); $this->Lexer->addSpecialPattern($macro, $mode, 'plugin_semantic');
@ -29,13 +28,11 @@ class syntax_plugin_semantic extends DokuWiki_Syntax_Plugin
} }
public function handle($match, $state, $pos, Doku_Handler $handler) function handle($match, $state, $pos, Doku_Handler $handler) {
{
return array($match, $state, $pos); return array($match, $state, $pos);
} }
public function render($mode, Doku_Renderer $renderer, $data) function render($mode, Doku_Renderer $renderer, $data) {
{
if ($mode == 'metadata') { if ($mode == 'metadata') {