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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
// must be run within Dokuwiki
|
|
|
|
if(!defined('DOKU_INC')) die();
|
|
|
|
|
|
|
|
class helper_plugin_semantic extends DokuWiki_Plugin {
|
|
|
|
|
|
|
|
private $meta = array();
|
2015-11-21 15:28:21 +00:00
|
|
|
private $page = null;
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2016-10-02 21:55:20 +00:00
|
|
|
|
|
|
|
public function getWebSite() {
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-21 15:28:21 +00:00
|
|
|
public function getMetadata($page) {
|
2015-11-11 22:45:46 +00:00
|
|
|
|
|
|
|
global $INFO;
|
|
|
|
global $ID;
|
|
|
|
global $license;
|
|
|
|
global $auth;
|
|
|
|
global $conf;
|
|
|
|
|
2015-11-21 15:28:21 +00:00
|
|
|
$this->page = cleanID($page);
|
|
|
|
|
|
|
|
$auth_check = auth_quickaclcheck($this->page);
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2015-11-21 15:28:21 +00:00
|
|
|
if ((bool) preg_match('/'. trim($this->getConf('excludedPages')) .'/', $this->page)) return false;
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2015-11-21 15:28:21 +00:00
|
|
|
if (! $auth_check) return false;
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2016-12-12 22:57:13 +00:00
|
|
|
$this->meta = p_get_metadata($this->page);
|
2015-11-21 15:28:21 +00:00
|
|
|
|
|
|
|
if (isset($this->meta['plugin']['semantic']['enabled']) && ! $this->meta['plugin']['semantic']['enabled']) {
|
2015-11-11 22:45:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! isset($this->meta['date']) || $this->meta['date'] == '') return false;
|
|
|
|
|
|
|
|
return $this->meta;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSchemaOrgType() {
|
2015-11-21 15:28:21 +00:00
|
|
|
|
|
|
|
return ((isset($this->meta['plugin']['semantic']['schema.org']['type']))
|
|
|
|
? $this->meta['plugin']['semantic']['schema.org']['type']
|
2015-11-11 22:45:46 +00:00
|
|
|
: $this->getConf('defaultSchemaOrgType'));
|
|
|
|
}
|
|
|
|
|
2016-10-02 21:55:20 +00:00
|
|
|
public function getFirstImage() {
|
|
|
|
return ((@$this->meta['relation']['firstimage']) ? $this->meta['relation']['firstimage'] : null);
|
|
|
|
}
|
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
public function getFirstImageURL() {
|
2016-10-02 21:55:20 +00:00
|
|
|
return ($this->getFirstImage() ? ml($this->getFirstImage(), '', true, '&', true) : null);
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription() {
|
2016-10-02 21:55:20 +00:00
|
|
|
return trim(ltrim(@$this->meta['description']['abstract'], $this->getTitle()));
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2016-10-02 21:55:20 +00:00
|
|
|
return (@$this->meta['title'] ? $this->meta['title'] : null);
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCreatedDate() {
|
2016-10-02 21:55:20 +00:00
|
|
|
return ((@$this->meta['date']['created']) ? $this->meta['date']['created'] : -1);
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getModifiedDate() {
|
2016-10-02 21:55:20 +00:00
|
|
|
return ((@$this->meta['date']['modified']) ? $this->meta['date']['modified'] : -1);
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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'];
|
2015-11-21 15:28:21 +00:00
|
|
|
$page_url = wl($this->page, '', true);
|
2015-11-11 22:45:46 +00:00
|
|
|
$description = $this->getDescription();
|
|
|
|
$created = date(DATE_W3C, $this->getCreatedDate());
|
|
|
|
$modified = date(DATE_W3C, $this->getModifiedDate());
|
2015-11-21 15:28:21 +00:00
|
|
|
$title = (isset($this->meta['title']) ? $this->meta['title'] : $this->page);
|
2015-11-11 22:45:46 +00:00
|
|
|
|
|
|
|
$json_ld = array(
|
|
|
|
'@context' => 'http://schema.org',
|
|
|
|
'@type' => $type,
|
2015-11-21 15:28:21 +00:00
|
|
|
'headline' => $title,
|
|
|
|
'name' => $title,
|
2015-11-11 22:45:46 +00:00
|
|
|
'datePublished' => $created,
|
|
|
|
'dateCreated' => $created,
|
|
|
|
'dateModified' => $modified,
|
|
|
|
'description' => $description,
|
|
|
|
'license' => $license_url,
|
|
|
|
'url' => $page_url,
|
2016-10-02 21:55:20 +00:00
|
|
|
'mainEntityOfPage' => array(
|
|
|
|
'@type' => 'WebPage',
|
|
|
|
'@id' => $page_url,
|
|
|
|
),
|
2015-11-11 22:45:46 +00:00
|
|
|
);
|
|
|
|
|
2016-10-02 21:55:20 +00:00
|
|
|
if ($image_url = $this->getFirstImageURL()) {
|
|
|
|
|
|
|
|
$image_info = array();
|
|
|
|
$article_image = tpl_getMediaFile(array($this->getFirstImage()), true, $logo_info);
|
|
|
|
|
|
|
|
$json_ld['image'] = array(
|
|
|
|
'@type' => 'ImageObject',
|
|
|
|
'url' => $image_url,
|
|
|
|
'width' => 0,
|
|
|
|
'height' => 0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
if ($author = $this->getAuthor()) {
|
|
|
|
|
|
|
|
$json_ld['creator'] = array(
|
|
|
|
'@context' => 'http://schema.org',
|
|
|
|
'@type' => 'Person',
|
|
|
|
'name' => $author,
|
|
|
|
'email' => $user_data['mail']
|
|
|
|
);
|
|
|
|
|
2016-10-02 21:55:20 +00:00
|
|
|
$logo_info = array();
|
|
|
|
$wiki_logo = tpl_getMediaFile(array(':wiki:logo.png', ':logo.png', 'images/logo.png'), true, $logo_info);
|
|
|
|
|
|
|
|
$json_ld['author'] = $json_ld['creator'];
|
|
|
|
$json_ld['publisher'] = $json_ld['creator'];
|
|
|
|
$json_ld['publisher']['logo'] = array(
|
|
|
|
'@type' => 'ImageObject',
|
|
|
|
'url' => $wiki_logo,
|
|
|
|
'width' => $logo_info[0],
|
|
|
|
'height' => $logo_info[1],
|
|
|
|
);
|
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
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']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $json_ld;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-21 15:28:21 +00:00
|
|
|
public function getJsonLD() {
|
|
|
|
|
|
|
|
$json_ld = array();
|
|
|
|
|
|
|
|
if ($structured_data = $this->getStructuredData()) {
|
|
|
|
$json_ld[] = $structured_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($backlinks = $this->getBacklinks()) {
|
|
|
|
$json_ld[] = $backlinks;
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
|
2015-11-21 15:28:21 +00:00
|
|
|
return $json_ld;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getBacklinks() {
|
|
|
|
|
|
|
|
if (! $backlinks = ft_backlinks($this->page)) return false;
|
2015-11-11 22:45:46 +00:00
|
|
|
|
|
|
|
$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;
|
|
|
|
|
2015-11-12 21:10:27 +00:00
|
|
|
if (! $this->meta) return array();
|
2015-11-11 22:45:46 +00:00
|
|
|
|
|
|
|
$license = $this->getLicense();
|
|
|
|
$contributors = array();
|
2015-11-12 13:09:07 +00:00
|
|
|
|
2016-10-02 21:55:20 +00:00
|
|
|
if (isset($this->meta['contributor']) && is_array($this->meta['contributor'])) {
|
2015-11-12 13:09:07 +00:00
|
|
|
foreach ($this->meta['contributor'] as $uid => $fullname) {
|
|
|
|
$contributors[] = $fullname;
|
|
|
|
}
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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()),
|
2015-11-21 15:28:21 +00:00
|
|
|
'DC.Identifier' => "urn:" . $this->page,
|
2015-11-11 22:45:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $dublin_core;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:57:13 +00:00
|
|
|
|
|
|
|
public function getOpenGraph() {
|
|
|
|
|
|
|
|
global $conf;
|
|
|
|
|
|
|
|
if (! $this->meta) return array();
|
|
|
|
|
|
|
|
$open_graph = array(
|
|
|
|
'og:title' => $this->getTitle(),
|
|
|
|
'og:description' => str_replace("\n", ' ', $this->getDescription()),
|
|
|
|
'og:url' => wl($this->page, '', true),
|
|
|
|
'og:type' => 'website',
|
2017-05-23 14:39:42 +00:00
|
|
|
'og:image' => $this->getFirstImageURL(),
|
2016-12-12 22:57:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $open_graph;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-11 22:45:46 +00:00
|
|
|
}
|