2015-11-11 22:45:46 +00:00
|
|
|
<?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>
|
2023-02-02 23:04:08 +00:00
|
|
|
* @copyright (C) 2015-2022, Giuseppe Di Terlizzi
|
2015-11-11 22:45:46 +00:00
|
|
|
*/
|
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
class helper_plugin_semantic extends DokuWiki_Plugin
|
|
|
|
{
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
private $meta = array();
|
|
|
|
private $page = null;
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Get Schema.org WebSite
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getWebSite()
|
|
|
|
{
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
global $conf;
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$json_ld = array(
|
2020-08-25 10:49:39 +00:00
|
|
|
'@context' => 'http://schema.org/',
|
2019-10-08 09:07:33 +00:00
|
|
|
'@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',
|
|
|
|
),
|
|
|
|
);
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
return $json_ld;
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Get the metadata of the page
|
|
|
|
*
|
|
|
|
* @param string $page ID
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getMetadata($page)
|
|
|
|
{
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
global $INFO;
|
|
|
|
global $ID;
|
|
|
|
global $license;
|
|
|
|
global $auth;
|
|
|
|
global $conf;
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$this->page = cleanID($page);
|
2016-10-02 21:55:20 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$auth_check = auth_quickaclcheck($this->page);
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ((bool) preg_match('/' . trim($this->getConf('excludedPages')) . '/', $this->page)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if (!$auth_check) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$this->meta = p_get_metadata($this->page);
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if (isset($this->meta['plugin']['semantic']['enabled']) && !$this->meta['plugin']['semantic']['enabled']) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if (!isset($this->meta['date']) || $this->meta['date'] == '') {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
return $this->meta;
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Get Schema.Org page type
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSchemaOrgType()
|
|
|
|
{
|
|
|
|
|
|
|
|
return ((isset($this->meta['plugin']['semantic']['schema.org']['type']))
|
|
|
|
? $this->meta['plugin']['semantic']['schema.org']['type']
|
|
|
|
: $this->getConf('defaultSchemaOrgType'));
|
|
|
|
}
|
2018-01-17 22:19:11 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Get the first image in page
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFirstImage()
|
|
|
|
{
|
2023-02-02 23:04:08 +00:00
|
|
|
return (isset($this->meta['relation']['firstimage']) ? $this->meta['relation']['firstimage'] : null);
|
2016-10-02 21:55:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Get the URL of the first image in page
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFirstImageURL()
|
|
|
|
{
|
|
|
|
return ($this->getFirstImage() ? ml($this->getFirstImage(), '', true, '&', true) : null);
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Get page description
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDescription()
|
|
|
|
{
|
2023-02-02 23:04:08 +00:00
|
|
|
return (isset($this->meta['description']['abstract']) ? $this->meta['description']['abstract'] : $this->getTitle());
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Get author name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAuthor()
|
|
|
|
{
|
2024-10-04 17:19:43 +00:00
|
|
|
return ( array_key_exists('creator', $this->meta) ) ?
|
|
|
|
$this->meta['creator'] : null;
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Get author ID
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAuthorID()
|
|
|
|
{
|
2024-10-04 17:19:43 +00:00
|
|
|
return ( array_key_exists('user', $this->meta) ) ?
|
|
|
|
$this->meta['user'] : null;
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Get the page title
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitle()
|
|
|
|
{
|
2023-02-02 23:04:08 +00:00
|
|
|
return (isset($this->meta['title']) ? $this->meta['title'] : null);
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Get the create date of page
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getCreatedDate()
|
|
|
|
{
|
2023-02-02 23:04:08 +00:00
|
|
|
return (isset($this->meta['date']['created']) ? $this->meta['date']['created'] : -1);
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Get the modified date of page
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getModifiedDate()
|
|
|
|
{
|
2023-02-02 23:04:08 +00:00
|
|
|
return (isset($this->meta['date']['modified']) ? $this->meta['date']['modified'] : -1);
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Get DokuWiki license
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLicense()
|
|
|
|
{
|
|
|
|
global $license;
|
|
|
|
global $conf;
|
2023-02-02 23:04:08 +00:00
|
|
|
|
|
|
|
return (isset($license[$conf['license']]) ? $license[$conf['license']] : null);
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
/**
|
|
|
|
* Return JSON-LD structured data in according of selected Schema.org type
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getStructuredData()
|
|
|
|
{
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
global $auth;
|
|
|
|
global $conf;
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if (!count($this->meta)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$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(
|
2020-08-25 10:49:39 +00:00
|
|
|
'@context' => 'http://schema.org/',
|
2019-10-08 09:07:33 +00:00
|
|
|
'@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'];
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($author = $this->getAuthor()) {
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$json_ld['author'] = array(
|
2020-08-25 10:49:39 +00:00
|
|
|
'@context' => 'http://schema.org/',
|
2019-10-08 09:07:33 +00:00
|
|
|
'@type' => 'Person',
|
|
|
|
'name' => $author,
|
|
|
|
'email' => $user_data['mail'],
|
|
|
|
);
|
2015-11-21 15:28:21 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if (isset($this->meta['contributor'])) {
|
|
|
|
foreach ($this->meta['contributor'] as $uid => $fullname) {
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$contributor_data = ($this->getConf('hideMail') ? array('mail' => null) : $auth->getUserData($uid));
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$json_ld['contributor'][] = array(
|
2020-08-25 10:49:39 +00:00
|
|
|
'@context' => 'http://schema.org/',
|
2019-10-08 09:07:33 +00:00
|
|
|
'@type' => 'Person',
|
|
|
|
'name' => $fullname,
|
|
|
|
'email' => $contributor_data['mail'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
return $json_ld;
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
public function getJsonLD()
|
|
|
|
{
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$json_ld = array();
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($structured_data = $this->getStructuredData()) {
|
|
|
|
$json_ld[] = $structured_data;
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($backlinks = $this->getBacklinks()) {
|
|
|
|
$json_ld[] = $backlinks;
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
return $json_ld;
|
2015-11-12 13:09:07 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
public function getBacklinks()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!$backlinks = ft_backlinks($this->page)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$json_ld_webpage = array(
|
2020-08-25 10:49:39 +00:00
|
|
|
'@context' => 'http://schema.org/',
|
2019-10-08 09:07:33 +00:00
|
|
|
'@type' => 'WebPage',
|
|
|
|
);
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
foreach ($backlinks as $pageid) {
|
|
|
|
$json_ld_webpage['relatedLink'][] = wl($pageid, '', true);
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if (isset($json_ld_webpage['relatedLink'])) {
|
|
|
|
return $json_ld_webpage;
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
}
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
public function getDublinCore()
|
|
|
|
{
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
global $conf;
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if (!$this->meta) {
|
|
|
|
return array();
|
|
|
|
}
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$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;
|
2018-01-17 22:19:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
public function getOpenGraph()
|
|
|
|
{
|
2018-01-17 22:19:11 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
global $conf;
|
2018-01-17 22:19:11 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if (!$this->meta) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$locale = $conf['lang'];
|
2018-01-17 22:19:11 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
if ($locale == 'en') {
|
|
|
|
$locale = 'en_GB';
|
|
|
|
} else {
|
|
|
|
$locale .= '_' . strtoupper($locale);
|
|
|
|
}
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
$open_graph = array(
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2019-10-08 09:07:33 +00:00
|
|
|
'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;
|
|
|
|
|
|
|
|
}
|
2016-12-12 22:57:13 +00:00
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|