Compare commits
No commits in common. "679e377c92e930bdac4626f0778ea3649fdbb7af" and "da1aaf0cdd892b1af007b6d5edd7e97ad48d2660" have entirely different histories.
679e377c92
...
da1aaf0cdd
9 changed files with 479 additions and 631 deletions
|
@ -1,13 +1,16 @@
|
|||
|
||||
language: php
|
||||
php:
|
||||
- "7.3"
|
||||
- "7.2"
|
||||
- "7.1"
|
||||
- "7.0"
|
||||
- "5.6"
|
||||
- "5.5"
|
||||
- "5.4"
|
||||
env:
|
||||
- DOKUWIKI=master
|
||||
- DOKUWIKI=stable
|
||||
- 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
|
||||
script: cd _test && phpunit --stderr --group plugin_semantic
|
||||
|
|
|
@ -6,15 +6,13 @@
|
|||
* @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);
|
||||
|
@ -34,3 +32,4 @@ class general_plugin_semantic_test extends DokuWikiTest
|
|||
$this->assertTrue(false !== strtotime($info['date']));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
360
action.php
360
action.php
|
@ -4,267 +4,211 @@
|
|||
*
|
||||
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
|
||||
* @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
|
||||
*
|
||||
* 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()
|
||||
{
|
||||
$this->helper = $this->loadHelper('semantic');
|
||||
public function __construct() {
|
||||
$this->helper = $this->loadHelper('semantic');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register events
|
||||
*
|
||||
* @param Doku_Event_Handler $controller
|
||||
*/
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register events
|
||||
*
|
||||
* @param Doku_Event_Handler $controller handler
|
||||
*/
|
||||
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');
|
||||
}
|
||||
|
||||
if ($this->getConf('useMetaDescription')) {
|
||||
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_description');
|
||||
}
|
||||
|
||||
if ($this->getConf('useMetaAuthor')) {
|
||||
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_author');
|
||||
}
|
||||
|
||||
if ($this->getConf('useDublinCore')) {
|
||||
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_dublin_core');
|
||||
}
|
||||
|
||||
if ($this->getConf('useOpenGraph')) {
|
||||
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_open_graph');
|
||||
}
|
||||
|
||||
if ($this->getConf('exposeWebService')) {
|
||||
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax');
|
||||
}
|
||||
|
||||
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'jsinfo');
|
||||
if ($this->getConf('useMetaDescription')) {
|
||||
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_description');
|
||||
}
|
||||
|
||||
/**
|
||||
* Export JSON-JD in $JSONINFO array
|
||||
*/
|
||||
public function jsinfo(Doku_Event &$event, $param)
|
||||
{
|
||||
if ($this->getConf('useMetaAuthor')) {
|
||||
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_author');
|
||||
}
|
||||
|
||||
global $JSINFO;
|
||||
if ($this->getConf('useDublinCore')) {
|
||||
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_dublin_core');
|
||||
}
|
||||
|
||||
$JSINFO['plugin']['semantic'] = array(
|
||||
'exposeWebService' => $this->getConf('exposeWebService'),
|
||||
);
|
||||
if ($this->getConf('useOpenGraph')) {
|
||||
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_open_graph');
|
||||
}
|
||||
|
||||
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
|
||||
*
|
||||
* @param Doku_Event $event handler
|
||||
* @param array $param
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function ajax(Doku_Event &$event, $param)
|
||||
{
|
||||
/**
|
||||
* Export in JSON-LD format
|
||||
*
|
||||
* @param Doku_Event &$event
|
||||
* @return string
|
||||
*/
|
||||
public function ajax(Doku_Event &$event, $param) {
|
||||
|
||||
if ($event->data !== 'plugin_semantic') {
|
||||
return false;
|
||||
}
|
||||
if ($event->data !== 'plugin_semantic') {
|
||||
return false;
|
||||
}
|
||||
|
||||
//no other ajax call handlers needed
|
||||
$event->stopPropagation();
|
||||
$event->preventDefault();
|
||||
//no other ajax call handlers needed
|
||||
$event->stopPropagation();
|
||||
$event->preventDefault();
|
||||
|
||||
global $INPUT;
|
||||
global $INPUT;
|
||||
|
||||
$export = $INPUT->str('export');
|
||||
$id = $INPUT->str('id');
|
||||
$export = $INPUT->str('export');
|
||||
$id = $INPUT->str('id');
|
||||
|
||||
if (!$id) {
|
||||
return false;
|
||||
}
|
||||
if (! $id) return false;
|
||||
|
||||
$this->helper->getMetadata($id);
|
||||
$json_ld = $this->helper->getJsonLD();
|
||||
$this->helper->getMetadata($id);
|
||||
$json_ld = $this->helper->getJsonLD();
|
||||
|
||||
header('Content-Type: application/ld+json');
|
||||
print json_encode($json_ld);
|
||||
return true;
|
||||
$json = new JSON();
|
||||
|
||||
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
|
||||
*
|
||||
* @param Doku_Event &$event
|
||||
*/
|
||||
public function json_ld(Doku_Event &$event, $param) {
|
||||
|
||||
global $ID;
|
||||
|
||||
$this->helper->getMetadata($ID);
|
||||
$json_ld = $this->helper->getJsonLD();
|
||||
|
||||
if (! count($json_ld)) return false;
|
||||
|
||||
$event->data["script"][] = array (
|
||||
"type" => "application/ld+json",
|
||||
"_data" => json_encode($json_ld),
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function meta_description(Doku_Event &$event, $params) {
|
||||
|
||||
global $ID;
|
||||
|
||||
$this->helper->getMetadata($ID);
|
||||
|
||||
if ($description = $this->helper->getDescription()) {
|
||||
|
||||
$description = str_replace("\n", ' ', $description);
|
||||
|
||||
$event->data['meta'][] = array(
|
||||
'name' => 'description',
|
||||
'content' => $description,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose JSON-JD WebSite schema
|
||||
*
|
||||
* @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",
|
||||
"_data" => json_encode($this->helper->getWebSite(), JSON_PRETTY_PRINT),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON-LD Event handler
|
||||
*
|
||||
* @param Doku_Event $event handler
|
||||
* @param array $params
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function json_ld(Doku_Event &$event, $params)
|
||||
{
|
||||
|
||||
global $ID;
|
||||
public function meta_author(Doku_Event &$event, $params) {
|
||||
|
||||
$this->helper->getMetadata($ID);
|
||||
$json_ld = $this->helper->getJsonLD();
|
||||
global $ID;
|
||||
|
||||
if (!count($json_ld)) {
|
||||
return false;
|
||||
}
|
||||
$this->helper->getMetadata($ID);
|
||||
|
||||
$event->data["script"][] = array(
|
||||
"type" => "application/ld+json",
|
||||
"_data" => json_encode($json_ld, JSON_PRETTY_PRINT),
|
||||
);
|
||||
if ($author = $this->helper->getAuthor()) {
|
||||
|
||||
$event->data['meta'][] = array(
|
||||
'name' => 'author',
|
||||
'content' => $author,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Meta Description handler
|
||||
*
|
||||
* @param Doku_Event $event handler
|
||||
* @param array $params
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function meta_description(Doku_Event &$event, $params)
|
||||
{
|
||||
}
|
||||
|
||||
global $ID;
|
||||
|
||||
$this->helper->getMetadata($ID);
|
||||
public function meta_open_graph(Doku_Event &$event, $params) {
|
||||
|
||||
if ($description = $this->helper->getDescription()) {
|
||||
global $ID;
|
||||
|
||||
$description = str_replace("\n", ' ', $description);
|
||||
$this->helper->getMetadata($ID);
|
||||
|
||||
$event->data['meta'][] = array(
|
||||
'name' => 'description',
|
||||
'content' => $description,
|
||||
);
|
||||
foreach ($this->helper->getOpenGraph() as $property => $content) {
|
||||
|
||||
}
|
||||
if (! $content) continue;
|
||||
|
||||
$event->data['meta'][] = array(
|
||||
'property' => $property,
|
||||
'content' => $content,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Meta Description handler
|
||||
*
|
||||
* @param Doku_Event $event handler
|
||||
* @param array $params
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function meta_author(Doku_Event &$event, $params)
|
||||
{
|
||||
}
|
||||
|
||||
global $ID;
|
||||
|
||||
$this->helper->getMetadata($ID);
|
||||
public function meta_dublin_core(Doku_Event &$event, $params) {
|
||||
|
||||
if ($author = $this->helper->getAuthor()) {
|
||||
global $ID;
|
||||
|
||||
$event->data['meta'][] = array(
|
||||
'name' => 'author',
|
||||
'content' => $author,
|
||||
);
|
||||
$this->helper->getMetadata($ID);
|
||||
|
||||
}
|
||||
foreach ($this->helper->getDublinCore() as $name => $content) {
|
||||
|
||||
if (! $content) continue;
|
||||
|
||||
$event->data['meta'][] = array(
|
||||
'name' => $name,
|
||||
'content' => $content,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* OpenGraph handler
|
||||
*
|
||||
* @param Doku_Event $event handler
|
||||
* @param array $params
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function meta_open_graph(Doku_Event &$event, $params)
|
||||
{
|
||||
|
||||
global $ID;
|
||||
|
||||
$this->helper->getMetadata($ID);
|
||||
|
||||
foreach ($this->helper->getOpenGraph() as $property => $content) {
|
||||
|
||||
if (!$content) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$event->data['meta'][] = array(
|
||||
'property' => $property,
|
||||
'content' => $content,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Dublin Core handler
|
||||
*
|
||||
* @param Doku_Event $event handler
|
||||
* @param array $params
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function meta_dublin_core(Doku_Event &$event, $params)
|
||||
{
|
||||
|
||||
global $ID;
|
||||
|
||||
$this->helper->getMetadata($ID);
|
||||
|
||||
foreach ($this->helper->getDublinCore() as $name => $content) {
|
||||
|
||||
if (!$content) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$event->data['meta'][] = array(
|
||||
'name' => $name,
|
||||
'content' => $content,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,5 +12,4 @@ $conf['useMetaDescription'] = 1;
|
|||
$conf['useMetaAuthor'] = 1;
|
||||
$conf['useDublinCore'] = 0;
|
||||
$conf['useOpenGraph'] = 0;
|
||||
$conf['excludedPages'] = '(wiki)';
|
||||
$conf['hideMail'] = 0;
|
||||
$conf['excludedPages'] = '(wiki|playground)';
|
||||
|
|
|
@ -11,6 +11,5 @@ $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');
|
||||
|
|
642
helper.php
642
helper.php
|
@ -4,404 +4,312 @@
|
|||
*
|
||||
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
|
||||
* @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();
|
||||
|
||||
private $meta = array();
|
||||
private $page = null;
|
||||
class helper_plugin_semantic extends DokuWiki_Plugin {
|
||||
|
||||
/**
|
||||
* Get Schema.org WebSite
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getWebSite()
|
||||
{
|
||||
private $meta = array();
|
||||
private $page = null;
|
||||
|
||||
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',
|
||||
),
|
||||
);
|
||||
public function getWebSite() {
|
||||
|
||||
return $json_ld;
|
||||
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;
|
||||
global $ID;
|
||||
global $license;
|
||||
global $auth;
|
||||
global $conf;
|
||||
|
||||
$this->page = cleanID($page);
|
||||
|
||||
$auth_check = auth_quickaclcheck($this->page);
|
||||
|
||||
if ((bool) preg_match('/'. trim($this->getConf('excludedPages')) .'/', $this->page)) 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;
|
||||
|
||||
}
|
||||
|
||||
public function getSchemaOrgType() {
|
||||
|
||||
return ((isset($this->meta['plugin']['semantic']['schema.org']['type']))
|
||||
? $this->meta['plugin']['semantic']['schema.org']['type']
|
||||
: $this->getConf('defaultSchemaOrgType'));
|
||||
}
|
||||
|
||||
public function getFirstImage() {
|
||||
return ((@$this->meta['relation']['firstimage']) ? $this->meta['relation']['firstimage'] : null);
|
||||
}
|
||||
|
||||
public function getFirstImageURL() {
|
||||
return ($this->getFirstImage() ? ml($this->getFirstImage(), '', true, '&', true) : null);
|
||||
}
|
||||
|
||||
public function getDescription() {
|
||||
return (@$this->meta['description']['abstract'] ? $this->meta['description']['abstract']: $this->getTitle());
|
||||
}
|
||||
|
||||
public function getAuthor() {
|
||||
return ($this->meta['creator'] ? $this->meta['creator'] : null);
|
||||
}
|
||||
|
||||
public function getAuthorID() {
|
||||
return ($this->meta['user'] ? $this->meta['user'] : null);
|
||||
}
|
||||
|
||||
public function getTitle() {
|
||||
return (@$this->meta['title'] ? $this->meta['title'] : null);
|
||||
}
|
||||
|
||||
public function getCreatedDate() {
|
||||
return ((@$this->meta['date']['created']) ? $this->meta['date']['created'] : -1);
|
||||
}
|
||||
|
||||
public function getModifiedDate() {
|
||||
return ((@$this->meta['date']['modified']) ? $this->meta['date']['modified'] : -1);
|
||||
}
|
||||
|
||||
public function getLicense() {
|
||||
global $license;
|
||||
global $conf;
|
||||
return @$license[$conf['license']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return JSON-LD structured data in according of selected Schema.org type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getStructuredData() {
|
||||
|
||||
global $auth;
|
||||
global $conf;
|
||||
|
||||
if (! count($this->meta)) return false;
|
||||
|
||||
$license = $this->getLicense();
|
||||
$type = $this->getSchemaOrgType();
|
||||
$user_data = $auth->getUserData($this->getAuthorID());
|
||||
$license_url = $license['url'];
|
||||
$page_url = wl($this->page, '', true);
|
||||
$description = str_replace("\n", ' ', $this->getDescription());
|
||||
$created = date(DATE_W3C, $this->getCreatedDate());
|
||||
$modified = date(DATE_W3C, $this->getModifiedDate());
|
||||
$title = (isset($this->meta['title']) ? $this->meta['title'] : $this->page);
|
||||
$wiki_logo_info = array();
|
||||
$wiki_logo = tpl_getMediaFile(array(':wiki:logo.png', ':logo.png', 'images/logo.png'), true, $wiki_logo_info);
|
||||
|
||||
|
||||
$json_ld = array(
|
||||
'@context' => 'http://schema.org',
|
||||
'@type' => $type,
|
||||
'headline' => $title,
|
||||
'name' => $title,
|
||||
'datePublished' => $created,
|
||||
'dateCreated' => $created,
|
||||
'dateModified' => $modified,
|
||||
'description' => $description,
|
||||
'license' => $license_url,
|
||||
'url' => $page_url,
|
||||
|
||||
'mainEntityOfPage' => array(
|
||||
'@type' => 'WebPage',
|
||||
'@id' => $page_url,
|
||||
),
|
||||
|
||||
'publisher' => array(
|
||||
'@type' => 'Organization',
|
||||
'name' => $conf['title'],
|
||||
'logo' => array(
|
||||
'@type' => 'ImageObject',
|
||||
'url' => $wiki_logo,
|
||||
),
|
||||
)
|
||||
|
||||
);
|
||||
|
||||
if ($image_url = $this->getFirstImageURL()) {
|
||||
|
||||
$image_info = array();
|
||||
$article_image = tpl_getMediaFile(array(':' . $this->getFirstImage()), true, $image_info);
|
||||
|
||||
$json_ld['image'] = array(
|
||||
'@type' => 'ImageObject',
|
||||
'url' => $image_url,
|
||||
'width' => $image_info[0],
|
||||
'height' => $image_info[1],
|
||||
);
|
||||
|
||||
} else {
|
||||
|
||||
// Fallback
|
||||
//$json_ld['image'] = $json_ld['publisher']['logo'];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the metadata of the page
|
||||
*
|
||||
* @param string $page ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMetadata($page)
|
||||
{
|
||||
if ($author = $this->getAuthor()) {
|
||||
|
||||
global $INFO;
|
||||
global $ID;
|
||||
global $license;
|
||||
global $auth;
|
||||
global $conf;
|
||||
$json_ld['author'] = array(
|
||||
'@context' => 'http://schema.org',
|
||||
'@type' => 'Person',
|
||||
'name' => $author,
|
||||
'email' => $user_data['mail']
|
||||
);
|
||||
|
||||
$this->page = cleanID($page);
|
||||
|
||||
$auth_check = auth_quickaclcheck($this->page);
|
||||
|
||||
if ((bool) preg_match('/' . trim($this->getConf('excludedPages')) . '/', $this->page)) {
|
||||
return false;
|
||||
if (isset($this->meta['contributor'])) {
|
||||
foreach ($this->meta['contributor'] as $uid => $fullname) {
|
||||
$contributor_data = $auth->getUserData($uid);
|
||||
$json_ld['contributor'][] = array(
|
||||
'@context' => 'http://schema.org',
|
||||
'@type' => 'Person',
|
||||
'name' => $fullname,
|
||||
'email' => $contributor_data['mail']
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Schema.Org page type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSchemaOrgType()
|
||||
{
|
||||
return $json_ld;
|
||||
|
||||
return ((isset($this->meta['plugin']['semantic']['schema.org']['type']))
|
||||
? $this->meta['plugin']['semantic']['schema.org']['type']
|
||||
: $this->getConf('defaultSchemaOrgType'));
|
||||
}
|
||||
|
||||
|
||||
public function getJsonLD() {
|
||||
|
||||
$json_ld = array();
|
||||
|
||||
if ($structured_data = $this->getStructuredData()) {
|
||||
$json_ld[] = $structured_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first image in page
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFirstImage()
|
||||
{
|
||||
return (isset($this->meta['relation']['firstimage']) ? $this->meta['relation']['firstimage'] : null);
|
||||
if ($backlinks = $this->getBacklinks()) {
|
||||
$json_ld[] = $backlinks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL of the first image in page
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFirstImageURL()
|
||||
{
|
||||
return ($this->getFirstImage() ? ml($this->getFirstImage(), '', true, '&', true) : null);
|
||||
return $json_ld;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function getBacklinks() {
|
||||
|
||||
if (! $backlinks = ft_backlinks($this->page)) return false;
|
||||
|
||||
$json_ld_webpage = array(
|
||||
'@context' => 'http://schema.org',
|
||||
'@type' => 'WebPage'
|
||||
);
|
||||
|
||||
foreach ($backlinks as $pageid) {
|
||||
$json_ld_webpage['relatedLink'][] = wl($pageid, '', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get page description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return (isset($this->meta['description']['abstract']) ? $this->meta['description']['abstract'] : $this->getTitle());
|
||||
if (isset($json_ld_webpage['relatedLink'])) return $json_ld_webpage;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function getDublinCore() {
|
||||
|
||||
global $conf;
|
||||
|
||||
if (! $this->meta) return array();
|
||||
|
||||
$license = $this->getLicense();
|
||||
$contributors = array();
|
||||
|
||||
if (isset($this->meta['contributor']) && is_array($this->meta['contributor'])) {
|
||||
foreach ($this->meta['contributor'] as $uid => $fullname) {
|
||||
$contributors[] = $fullname;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get author name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAuthor()
|
||||
{
|
||||
return ( array_key_exists('creator', $this->meta ) ?
|
||||
$this->meta['creator'] : null;
|
||||
$dublin_core = array(
|
||||
'DC.Title' => $this->getTitle(),
|
||||
'DC.Description' => str_replace("\n", ' ', $this->getDescription()),
|
||||
'DC.Publisher' => $this->getAuthor(),
|
||||
'DC.Contributors' => implode(', ', $contributors),
|
||||
'DC.Rights' => $license['name'],
|
||||
'DC.Language' => $conf['lang'],
|
||||
'DC.Created' => date(DATE_W3C, $this->getCreatedDate()),
|
||||
'DC.Modified' => date(DATE_W3C, $this->getModifiedDate()),
|
||||
'DC.Date' => date(DATE_W3C, $this->getCreatedDate()),
|
||||
'DC.Identifier' => "urn:" . $this->page,
|
||||
);
|
||||
|
||||
return $dublin_core;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function getOpenGraph() {
|
||||
|
||||
global $conf;
|
||||
|
||||
if (! $this->meta) return array();
|
||||
|
||||
$locale = $conf['lang'];
|
||||
|
||||
if ($locale == 'en') {
|
||||
$locale = 'en_GB';
|
||||
} else {
|
||||
$locale .= '_' . strtoupper($locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get author ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAuthorID()
|
||||
{
|
||||
return ( array_key_exists('user', $this->meta ) ?
|
||||
$this->meta['user'] : null;
|
||||
}
|
||||
$open_graph = array(
|
||||
|
||||
/**
|
||||
* Get the page title
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle()
|
||||
{
|
||||
return (isset($this->meta['title']) ? $this->meta['title'] : null);
|
||||
}
|
||||
'og:title' => $this->getTitle(),
|
||||
'og:description' => str_replace("\n", ' ', $this->getDescription()),
|
||||
'og:url' => wl($this->page, '', true),
|
||||
'og:type' => 'article',
|
||||
'og:image' => $this->getFirstImageURL(),
|
||||
'og:locale' => $locale,
|
||||
'og:site_name' => $conf['title'],
|
||||
|
||||
/**
|
||||
* Get the create date of page
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getCreatedDate()
|
||||
{
|
||||
return (isset($this->meta['date']['created']) ? $this->meta['date']['created'] : -1);
|
||||
}
|
||||
'article:published_time' => date(DATE_W3C, $this->getCreatedDate()),
|
||||
'article:modified_time' => date(DATE_W3C, $this->getModifiedDate()),
|
||||
'article:section' => date(DATE_W3C, $this->getModifiedDate()),
|
||||
'article:author' => $this->getAuthor(),
|
||||
|
||||
/**
|
||||
* Get the modified date of page
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getModifiedDate()
|
||||
{
|
||||
return (isset($this->meta['date']['modified']) ? $this->meta['date']['modified'] : -1);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Get DokuWiki license
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLicense()
|
||||
{
|
||||
global $license;
|
||||
global $conf;
|
||||
return $open_graph;
|
||||
|
||||
return (isset($license[$conf['license']]) ? $license[$conf['license']] : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return JSON-LD structured data in according of selected Schema.org type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getStructuredData()
|
||||
{
|
||||
|
||||
global $auth;
|
||||
global $conf;
|
||||
|
||||
if (!count($this->meta)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$license = $this->getLicense();
|
||||
$type = $this->getSchemaOrgType();
|
||||
$user_data = ($this->getConf('hideMail') ? array('mail' => null) : $auth->getUserData($this->getAuthorID()));
|
||||
$license_url = $license['url'];
|
||||
$page_url = wl($this->page, '', true);
|
||||
$description = str_replace("\n", ' ', $this->getDescription());
|
||||
$created = date(DATE_W3C, $this->getCreatedDate());
|
||||
$modified = date(DATE_W3C, $this->getModifiedDate());
|
||||
$title = (isset($this->meta['title']) ? $this->meta['title'] : $this->page);
|
||||
$wiki_logo_info = array();
|
||||
$wiki_logo = tpl_getMediaFile(array(':wiki:logo.png', ':logo.png', 'images/logo.png'), true, $wiki_logo_info);
|
||||
|
||||
$json_ld = array(
|
||||
'@context' => 'http://schema.org/',
|
||||
'@type' => $type,
|
||||
'headline' => $title,
|
||||
'name' => $title,
|
||||
'datePublished' => $created,
|
||||
'dateCreated' => $created,
|
||||
'dateModified' => $modified,
|
||||
'description' => $description,
|
||||
'license' => $license_url,
|
||||
'url' => $page_url,
|
||||
|
||||
'mainEntityOfPage' => array(
|
||||
'@type' => 'WebPage',
|
||||
'@id' => $page_url,
|
||||
),
|
||||
|
||||
'publisher' => array(
|
||||
'@type' => 'Organization',
|
||||
'name' => $conf['title'],
|
||||
'logo' => array(
|
||||
'@type' => 'ImageObject',
|
||||
'url' => $wiki_logo,
|
||||
),
|
||||
),
|
||||
|
||||
);
|
||||
|
||||
if ($image_url = $this->getFirstImageURL()) {
|
||||
|
||||
$image_info = array();
|
||||
$article_image = tpl_getMediaFile(array(':' . $this->getFirstImage()), true, $image_info);
|
||||
|
||||
$json_ld['image'] = array(
|
||||
'@type' => 'ImageObject',
|
||||
'url' => $image_url,
|
||||
'width' => $image_info[0],
|
||||
'height' => $image_info[1],
|
||||
);
|
||||
|
||||
} else {
|
||||
|
||||
// Fallback
|
||||
//$json_ld['image'] = $json_ld['publisher']['logo'];
|
||||
|
||||
}
|
||||
|
||||
if ($author = $this->getAuthor()) {
|
||||
|
||||
$json_ld['author'] = array(
|
||||
'@context' => 'http://schema.org/',
|
||||
'@type' => 'Person',
|
||||
'name' => $author,
|
||||
'email' => $user_data['mail'],
|
||||
);
|
||||
|
||||
if (isset($this->meta['contributor'])) {
|
||||
foreach ($this->meta['contributor'] as $uid => $fullname) {
|
||||
|
||||
$contributor_data = ($this->getConf('hideMail') ? array('mail' => null) : $auth->getUserData($uid));
|
||||
|
||||
$json_ld['contributor'][] = array(
|
||||
'@context' => 'http://schema.org/',
|
||||
'@type' => 'Person',
|
||||
'name' => $fullname,
|
||||
'email' => $contributor_data['mail'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $json_ld;
|
||||
|
||||
}
|
||||
|
||||
public function getJsonLD()
|
||||
{
|
||||
|
||||
$json_ld = array();
|
||||
|
||||
if ($structured_data = $this->getStructuredData()) {
|
||||
$json_ld[] = $structured_data;
|
||||
}
|
||||
|
||||
if ($backlinks = $this->getBacklinks()) {
|
||||
$json_ld[] = $backlinks;
|
||||
}
|
||||
|
||||
return $json_ld;
|
||||
|
||||
}
|
||||
|
||||
public function getBacklinks()
|
||||
{
|
||||
|
||||
if (!$backlinks = ft_backlinks($this->page)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$json_ld_webpage = array(
|
||||
'@context' => 'http://schema.org/',
|
||||
'@type' => 'WebPage',
|
||||
);
|
||||
|
||||
foreach ($backlinks as $pageid) {
|
||||
$json_ld_webpage['relatedLink'][] = wl($pageid, '', true);
|
||||
}
|
||||
|
||||
if (isset($json_ld_webpage['relatedLink'])) {
|
||||
return $json_ld_webpage;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getDublinCore()
|
||||
{
|
||||
|
||||
global $conf;
|
||||
|
||||
if (!$this->meta) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$license = $this->getLicense();
|
||||
$contributors = array();
|
||||
|
||||
if (isset($this->meta['contributor']) && is_array($this->meta['contributor'])) {
|
||||
foreach ($this->meta['contributor'] as $uid => $fullname) {
|
||||
$contributors[] = $fullname;
|
||||
}
|
||||
}
|
||||
|
||||
$dublin_core = array(
|
||||
'DC.Title' => $this->getTitle(),
|
||||
'DC.Description' => str_replace("\n", ' ', $this->getDescription()),
|
||||
'DC.Publisher' => $this->getAuthor(),
|
||||
'DC.Contributors' => implode(', ', $contributors),
|
||||
'DC.Rights' => $license['name'],
|
||||
'DC.Language' => $conf['lang'],
|
||||
'DC.Created' => date(DATE_W3C, $this->getCreatedDate()),
|
||||
'DC.Modified' => date(DATE_W3C, $this->getModifiedDate()),
|
||||
'DC.Date' => date(DATE_W3C, $this->getCreatedDate()),
|
||||
'DC.Identifier' => "urn:" . $this->page,
|
||||
);
|
||||
|
||||
return $dublin_core;
|
||||
|
||||
}
|
||||
|
||||
public function getOpenGraph()
|
||||
{
|
||||
|
||||
global $conf;
|
||||
|
||||
if (!$this->meta) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$locale = $conf['lang'];
|
||||
|
||||
if ($locale == 'en') {
|
||||
$locale = 'en_GB';
|
||||
} else {
|
||||
$locale .= '_' . strtoupper($locale);
|
||||
}
|
||||
|
||||
$open_graph = array(
|
||||
|
||||
'og:title' => $this->getTitle(),
|
||||
'og:description' => str_replace("\n", ' ', $this->getDescription()),
|
||||
'og:url' => wl($this->page, '', true),
|
||||
'og:type' => 'article',
|
||||
'og:image' => $this->getFirstImageURL(),
|
||||
'og:locale' => $locale,
|
||||
'og:site_name' => $conf['title'],
|
||||
|
||||
'article:published_time' => date(DATE_W3C, $this->getCreatedDate()),
|
||||
'article:modified_time' => date(DATE_W3C, $this->getModifiedDate()),
|
||||
'article:section' => date(DATE_W3C, $this->getModifiedDate()),
|
||||
'article:author' => $this->getAuthor(),
|
||||
|
||||
);
|
||||
|
||||
return $open_graph;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,4 +15,3 @@ $lang['useJSONLD'] = 'Add JSON-LD';
|
|||
$lang['useMetaAuthor'] = 'Add author meta tag';
|
||||
$lang['useMetaDescription'] = 'Add description meta tag';
|
||||
$lang['useOpenGraph'] = 'Add Open Graph protocol metadata';
|
||||
$lang['hideMail'] = 'Hide Author e-Mail address';
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
base semantic
|
||||
author Giuseppe Di Terlizzi, patched by vv221 for Numenaute
|
||||
email numenaute@vv221.fr
|
||||
date 2024-10-04
|
||||
author Giuseppe Di Terlizzi
|
||||
email giuseppe.diterlizzi@gmail.com
|
||||
date 2018-08-25
|
||||
name Semantic Plugin
|
||||
desc Add Semantic Data in DokuWiki
|
||||
url https://git.vv221.fr/numenaute.org/dokuwiki-plugin-semantic/about/
|
||||
url http://www.dokuwiki.org/plugin:semantic
|
||||
|
|
77
syntax.php
77
syntax.php
|
@ -1,57 +1,54 @@
|
|||
<?php
|
||||
/**
|
||||
* Semantic plugin: Add Schema.org News Article using JSON-LD
|
||||
*
|
||||
*
|
||||
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
|
||||
* @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/');
|
||||
|
||||
private $macros = array(
|
||||
'~~NewsArticle~~', '~~Article~~', '~~TechArticle~~',
|
||||
'~~BlogPosting~~', '~~Recipe~~', '~~NOSEMANTIC~~',
|
||||
);
|
||||
class syntax_plugin_semantic extends DokuWiki_Syntax_Plugin {
|
||||
|
||||
public function getType()
|
||||
{return 'substition';}
|
||||
public function getSort()
|
||||
{return 99;}
|
||||
private $macros = array(
|
||||
'~~NewsArticle~~', '~~Article~~', '~~TechArticle~~',
|
||||
'~~BlogPosting~~', '~~Recipe~~', '~~NOSEMANTIC~~'
|
||||
);
|
||||
|
||||
public function connectTo($mode)
|
||||
{
|
||||
function getType() { return 'substition'; }
|
||||
function getSort() { return 99; }
|
||||
|
||||
foreach ($this->macros as $macro) {
|
||||
$this->Lexer->addSpecialPattern($macro, $mode, 'plugin_semantic');
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function handle($match, $state, $pos, Doku_Handler $handler)
|
||||
{
|
||||
return array($match, $state, $pos);
|
||||
}
|
||||
return false;
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue