From 879939ea3ef80ba5804ed6e21908764e74005f86 Mon Sep 17 00:00:00 2001 From: Giuseppe Di Terlizzi Date: Tue, 25 Aug 2020 12:13:34 +0200 Subject: [PATCH 1/3] Code beautifications and some little improvments --- .travis.yml | 6 +--- _test/general.test.php | 9 ++--- action.php | 10 ++---- conf/default.php | 2 +- conf/metadata.php | 2 +- helper.php | 7 +--- syntax.php | 74 ++++++++++++++++++++++-------------------- 7 files changed, 49 insertions(+), 61 deletions(-) diff --git a/.travis.yml b/.travis.yml index 441aa2c..d3bc082 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,10 +8,6 @@ php: env: - DOKUWIKI=master - DOKUWIKI=stable -matrix: - include: - - php: "5.6" - env: DOKUWIKI=old-stable before_install: wget https://raw.github.com/splitbrain/dokuwiki-travis/master/travis.sh install: sh travis.sh -script: cd _test && ./phpunit.phar --stderr --group plugin_semantic \ No newline at end of file +script: cd _test && ./phpunit.phar --stderr --group plugin_semantic diff --git a/_test/general.test.php b/_test/general.test.php index 9165d10..772cbc3 100644 --- a/_test/general.test.php +++ b/_test/general.test.php @@ -6,13 +6,15 @@ * @group plugin_semantic * @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 */ - public function test_plugininfo() { - $file = __DIR__.'/../plugin.info.txt'; + public function test_plugininfo() + { + $file = __DIR__ . '/../plugin.info.txt'; $this->assertFileExists($file); $info = confToHash($file); @@ -32,4 +34,3 @@ class general_plugin_semantic_test extends DokuWikiTest { $this->assertTrue(false !== strtotime($info['date'])); } } - diff --git a/action.php b/action.php index a322f48..502a119 100644 --- a/action.php +++ b/action.php @@ -4,13 +4,9 @@ * * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @author Giuseppe Di Terlizzi - * @copyright (C) 2015-2019, Giuseppe Di Terlizzi + * @copyright (C) 2015-2020, Giuseppe Di Terlizzi */ -// must be run within Dokuwiki -if (!defined('DOKU_INC')) { - die(); -} /** * Class Semantic Action Plugin @@ -108,10 +104,8 @@ class action_plugin_semantic extends DokuWiki_Action_Plugin $this->helper->getMetadata($id); $json_ld = $this->helper->getJsonLD(); - $json = new JSON(); - header('Content-Type: application/ld+json'); - print $json->encode($json_ld); + print json_encode($json_ld); return true; } diff --git a/conf/default.php b/conf/default.php index 8b669ef..201651e 100644 --- a/conf/default.php +++ b/conf/default.php @@ -12,5 +12,5 @@ $conf['useMetaDescription'] = 1; $conf['useMetaAuthor'] = 1; $conf['useDublinCore'] = 0; $conf['useOpenGraph'] = 0; -$conf['excludedPages'] = '(wiki|playground)'; +$conf['excludedPages'] = '(wiki)'; $conf['hideMail'] = 0; diff --git a/conf/metadata.php b/conf/metadata.php index 9b4335d..4f77fac 100644 --- a/conf/metadata.php +++ b/conf/metadata.php @@ -11,6 +11,6 @@ $meta['useDublinCore'] = array('onoff'); $meta['useOpenGraph'] = array('onoff'); $meta['useJSONLD'] = 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['hideMail'] = array('onoff'); diff --git a/helper.php b/helper.php index 14df72e..6c23a1b 100644 --- a/helper.php +++ b/helper.php @@ -4,14 +4,9 @@ * * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @author Giuseppe Di Terlizzi - * @copyright (C) 2015-2019, Giuseppe Di Terlizzi + * @copyright (C) 2015-2020, Giuseppe Di Terlizzi */ -// must be run within Dokuwiki -if (!defined('DOKU_INC')) { - die(); -} - class helper_plugin_semantic extends DokuWiki_Plugin { diff --git a/syntax.php b/syntax.php index 21269af..b1db13e 100644 --- a/syntax.php +++ b/syntax.php @@ -5,50 +5,52 @@ * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @author Giuseppe Di Terlizzi */ -// must be run within Dokuwiki -if (!defined('DOKU_INC')) die(); -if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); +class syntax_plugin_semantic extends DokuWiki_Syntax_Plugin +{ -class syntax_plugin_semantic extends DokuWiki_Syntax_Plugin { + private $macros = array( + '~~NewsArticle~~', '~~Article~~', '~~TechArticle~~', + '~~BlogPosting~~', '~~Recipe~~', '~~NOSEMANTIC~~', + ); - private $macros = array( - '~~NewsArticle~~', '~~Article~~', '~~TechArticle~~', - '~~BlogPosting~~', '~~Recipe~~', '~~NOSEMANTIC~~' - ); + public function getType() + {return 'substition';} + public function getSort() + {return 99;} - function getType() { return 'substition'; } - function getSort() { return 99; } + public function connectTo($mode) + { - function connectTo($mode) { - - foreach ($this->macros as $macro) { - $this->Lexer->addSpecialPattern($macro, $mode, 'plugin_semantic'); - } - - } - - function handle($match, $state, $pos, Doku_Handler $handler) { - return array($match, $state, $pos); - } - - function render($mode, Doku_Renderer $renderer, $data) { - - if ($mode == 'metadata') { - - list($match, $state, $pos) = $data; - - if ($match == '~~NOSEMANTIC~~') { - $renderer->meta['plugin']['semantic']['enabled'] = false; - } else { - $renderer->meta['plugin']['semantic']['schema.org']['type'] = trim(str_replace('Schema.org/', '', $match), '~~'); - $renderer->meta['plugin']['semantic']['enabled'] = true; - } + foreach ($this->macros as $macro) { + $this->Lexer->addSpecialPattern($macro, $mode, 'plugin_semantic'); + } } - return false; + public function handle($match, $state, $pos, Doku_Handler $handler) + { + return array($match, $state, $pos); + } - } + public function render($mode, Doku_Renderer $renderer, $data) + { + + if ($mode == 'metadata') { + + list($match, $state, $pos) = $data; + + if ($match == '~~NOSEMANTIC~~') { + $renderer->meta['plugin']['semantic']['enabled'] = false; + } else { + $renderer->meta['plugin']['semantic']['schema.org']['type'] = trim(str_replace('Schema.org/', '', $match), '~~'); + $renderer->meta['plugin']['semantic']['enabled'] = true; + } + + } + + return false; + + } } From 21aaf22b53f8e89b3c548dfac4bce3ea4c108c51 Mon Sep 17 00:00:00 2001 From: Giuseppe Di Terlizzi Date: Tue, 25 Aug 2020 12:49:39 +0200 Subject: [PATCH 2/3] Added trailing slash in @context --- helper.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/helper.php b/helper.php index 6c23a1b..ec06e45 100644 --- a/helper.php +++ b/helper.php @@ -24,7 +24,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin global $conf; $json_ld = array( - '@context' => 'http://schema.org', + '@context' => 'http://schema.org/', '@type' => 'WebSite', 'url' => DOKU_URL, 'name' => $conf['title'], @@ -214,7 +214,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin $wiki_logo = tpl_getMediaFile(array(':wiki:logo.png', ':logo.png', 'images/logo.png'), true, $wiki_logo_info); $json_ld = array( - '@context' => 'http://schema.org', + '@context' => 'http://schema.org/', '@type' => $type, 'headline' => $title, 'name' => $title, @@ -263,7 +263,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin if ($author = $this->getAuthor()) { $json_ld['author'] = array( - '@context' => 'http://schema.org', + '@context' => 'http://schema.org/', '@type' => 'Person', 'name' => $author, 'email' => $user_data['mail'], @@ -275,7 +275,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin $contributor_data = ($this->getConf('hideMail') ? array('mail' => null) : $auth->getUserData($uid)); $json_ld['contributor'][] = array( - '@context' => 'http://schema.org', + '@context' => 'http://schema.org/', '@type' => 'Person', 'name' => $fullname, 'email' => $contributor_data['mail'], @@ -314,7 +314,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin } $json_ld_webpage = array( - '@context' => 'http://schema.org', + '@context' => 'http://schema.org/', '@type' => 'WebPage', ); From 0247fcbe7cf9c08ca934879af56d99dcddf3f52d Mon Sep 17 00:00:00 2001 From: Giuseppe Di Terlizzi Date: Fri, 3 Feb 2023 00:04:08 +0100 Subject: [PATCH 3/3] FIX "TypeError: Cannot access offset of type string on string" #22 --- action.php | 2 +- helper.php | 15 ++++++++------- syntax.php | 3 ++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/action.php b/action.php index 502a119..7a176a0 100644 --- a/action.php +++ b/action.php @@ -4,7 +4,7 @@ * * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @author Giuseppe Di Terlizzi - * @copyright (C) 2015-2020, Giuseppe Di Terlizzi + * @copyright (C) 2015-2023, Giuseppe Di Terlizzi */ diff --git a/helper.php b/helper.php index ec06e45..721422e 100644 --- a/helper.php +++ b/helper.php @@ -4,7 +4,7 @@ * * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @author Giuseppe Di Terlizzi - * @copyright (C) 2015-2020, Giuseppe Di Terlizzi + * @copyright (C) 2015-2022, Giuseppe Di Terlizzi */ class helper_plugin_semantic extends DokuWiki_Plugin @@ -101,7 +101,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin */ public function getFirstImage() { - return ((@$this->meta['relation']['firstimage']) ? $this->meta['relation']['firstimage'] : null); + return (isset($this->meta['relation']['firstimage']) ? $this->meta['relation']['firstimage'] : null); } /** @@ -121,7 +121,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin */ public function getDescription() { - return (@$this->meta['description']['abstract'] ? $this->meta['description']['abstract'] : $this->getTitle()); + return (isset($this->meta['description']['abstract']) ? $this->meta['description']['abstract'] : $this->getTitle()); } /** @@ -151,7 +151,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin */ public function getTitle() { - return (@$this->meta['title'] ? $this->meta['title'] : null); + return (isset($this->meta['title']) ? $this->meta['title'] : null); } /** @@ -161,7 +161,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin */ public function getCreatedDate() { - return ((@$this->meta['date']['created']) ? $this->meta['date']['created'] : -1); + return (isset($this->meta['date']['created']) ? $this->meta['date']['created'] : -1); } /** @@ -171,7 +171,7 @@ class helper_plugin_semantic extends DokuWiki_Plugin */ public function getModifiedDate() { - return ((@$this->meta['date']['modified']) ? $this->meta['date']['modified'] : -1); + return (isset($this->meta['date']['modified']) ? $this->meta['date']['modified'] : -1); } /** @@ -183,7 +183,8 @@ class helper_plugin_semantic extends DokuWiki_Plugin { global $license; global $conf; - return @$license[$conf['license']]; + + return (isset($license[$conf['license']]) ? $license[$conf['license']] : null); } /** diff --git a/syntax.php b/syntax.php index b1db13e..fb52d27 100644 --- a/syntax.php +++ b/syntax.php @@ -1,9 +1,10 @@ + * @copyright (C) 2015-2023, Giuseppe Di Terlizzi */ class syntax_plugin_semantic extends DokuWiki_Syntax_Plugin