Improved JSON-LD and OpenGraph support

This commit is contained in:
Giuseppe Di Terlizzi 2018-01-17 23:19:11 +01:00
parent bd1a021a40
commit d67a514409
2 changed files with 56 additions and 31 deletions

View file

@ -121,15 +121,18 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
if (! count($this->meta)) return false; if (! count($this->meta)) return false;
$license = $this->getLicense(); $license = $this->getLicense();
$type = $this->getSchemaOrgType(); $type = $this->getSchemaOrgType();
$user_data = $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 = $this->getDescription(); $description = $this->getDescription();
$created = date(DATE_W3C, $this->getCreatedDate()); $created = date(DATE_W3C, $this->getCreatedDate());
$modified = date(DATE_W3C, $this->getModifiedDate()); $modified = date(DATE_W3C, $this->getModifiedDate());
$title = (isset($this->meta['title']) ? $this->meta['title'] : $this->page); $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( $json_ld = array(
'@context' => 'http://schema.org', '@context' => 'http://schema.org',
@ -142,46 +145,51 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
'description' => $description, 'description' => $description,
'license' => $license_url, 'license' => $license_url,
'url' => $page_url, 'url' => $page_url,
'mainEntityOfPage' => array( 'mainEntityOfPage' => array(
'@type' => 'WebPage', '@type' => 'WebPage',
'@id' => $page_url, '@id' => $page_url,
), ),
'publisher' => array(
'@type' => 'Organization',
'name' => $conf['title'],
'logo' => array(
'@type' => 'ImageObject',
'url' => $wiki_logo,
),
)
); );
if ($image_url = $this->getFirstImageURL()) { if ($image_url = $this->getFirstImageURL()) {
$image_info = array(); $image_info = array();
$article_image = tpl_getMediaFile(array($this->getFirstImage()), true, $logo_info); $article_image = tpl_getMediaFile(array(':' . $this->getFirstImage()), true, $image_info);
$json_ld['image'] = array( $json_ld['image'] = array(
'@type' => 'ImageObject', '@type' => 'ImageObject',
'url' => $image_url, 'url' => $image_url,
'width' => 0, 'width' => $image_info[0],
'height' => 0, 'height' => $image_info[1],
); );
} else {
// Fallback
$json_ld['image'] = $json_ld['publisher']['logo'];
} }
if ($author = $this->getAuthor()) { if ($author = $this->getAuthor()) {
$json_ld['creator'] = 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']
); );
$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],
);
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 = $auth->getUserData($uid);
@ -275,12 +283,29 @@ class helper_plugin_semantic extends DokuWiki_Plugin {
if (! $this->meta) return array(); if (! $this->meta) return array();
$locale = $conf['lang'];
if ($locale == 'en') {
$locale = 'en_GB';
} else {
$locale .= '_' . strtoupper($locale);
}
$open_graph = array( $open_graph = array(
'og:title' => $this->getTitle(),
'og:description' => str_replace("\n", ' ', $this->getDescription()), 'og:title' => $this->getTitle(),
'og:url' => wl($this->page, '', true), 'og:description' => str_replace("\n", ' ', $this->getDescription()),
'og:type' => 'website', 'og:url' => wl($this->page, '', true),
'og:image' => $this->getFirstImageURL(), '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; return $open_graph;

View file

@ -1,7 +1,7 @@
base semantic base semantic
author Giuseppe Di Terlizzi author Giuseppe Di Terlizzi
email giuseppe.diterlizzi@gmail.com email giuseppe.diterlizzi@gmail.com
date 2017-05-23 date 2018-01-17
name Semantic Plugin name Semantic Plugin
desc Add Semantic Data in DokuWiki desc Add Semantic Data in DokuWiki
url http://www.dokuwiki.org/plugin:semantic url http://www.dokuwiki.org/plugin:semantic