- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2021-06-16T19:07:18+09:00;2021-06-16T18:32:31+09:00","default:tgoto","tgoto")
#author("2021-06-19T10:12:02+09:00;2021-06-19T09:54:48+09:00","default:tgoto","tgoto")
#ref(https://oncologynote.com/img/69e89e3479.jpg,nolink)
pukiwikiのjsonld.inc.phpプラグインは簡単にpukiwikiにJSON-LDの構造化データを追加することができて重宝していましたが、短縮URLプラグインを使っているとパンくずリストのURLが正しく表示できず困っていましたので、ここに改変内容を記載しておきます。
pukiwikiの[[jsonld.inc.phpプラグイン>https://pukiwiki.osdn.jp/?%E8%87%AA%E4%BD%9C%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3/jsonld.inc.php]]は簡単にpukiwikiにJSON-LDの構造化データを追加することができて重宝していましたが、[[短縮URLプラグイン>https://dajya-ranger.com/pukiwiki/embed-url-shortener/]]を使っているとパンくずリストのURLが正しく表示できず困っていましたので、ここに改変内容を記載しておきます。
jsonld.inc.phpと短縮URLについてはこちらのページをご覧ください。
#ogpi(https://oncologynote.com/?ab056d28f5)
#ogp(https://pukiwiki.osdn.jp/?%E8%87%AA%E4%BD%9C%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3/jsonld.inc.php)
#ogp(https://dajya-ranger.com/pukiwiki/embed-url-shortener/)
*改装前のオリジナル [#x0ee32db]
*変更点 [#w35b918a]
<?php
/*
PukiWiki - Yet another WikiWikiWeb clone.
jsonld.inc.php, v1.02 2020 M.Taniguchi
License: GPL v3 or (at your option) any later version
JSON-LDを出力するプラグイン。
ページの情報に基づくJSON-LD構造化データを生成し出力します。
具体的には、記事情報 Article とパンくずリスト情報 BreadcrumbList を生成します。
ウィキの構造を検索エンジンにより良く伝えるため(SEO)に役立ちます。
【使い方】
#jsonld
本プラグインは、MenuBar など全画面共通で表示されるページに挿入してください。
もしくは、次のコードをスキンファイル(skin/pukiwiki.skin.php等)HTML内の</body>閉じタグ直前に挿入してください。
<?php if (exist_plugin_convert('jsonld')) echo do_plugin_convert('jsonld'); ?>
なお、本プラグインを挿入できるのは1ページにつき1箇所のみです。
*/
define('PLUGIN_JSONLD_ARTICLE', true); // Article を出力
define('PLUGIN_JSONLD_BREADCRUMBLIST', true); // BreadcrumbList を出力
function plugin_jsonld_convert() {
global $modifier, $defaultpage, $page_title, $title;
// if (!PKWK_ALLOW_JAVASCRIPT) return ''; // JavaScriptではなくJSONなので無視
// 二重起動禁止
static $included = false;
if ($included) return '';
$included = true;
$script = get_script_uri();
$isHome = ($title == $defaultpage);
$long_title = (!$isHome ? $title . ' | ' : '') . $page_title;
$thisPageUri = $script . (!$isHome ? '?' . str_replace('%2F', '/', urlencode($title)) : '');
$modifiedDate = date('Y-m-d\TH:i:sP', get_filetime($title));
$jsonOption = JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT;
// Article(記事情報)生成
if (PLUGIN_JSONLD_ARTICLE) {
$article = array(
'@context' => 'http:'.'//schema.org',
'@type' => 'Article',
'mainEntityOfPage' => array(
'@type' => 'WebPage',
'@id' => $thisPageUri
),
'datePublished' => $modifiedDate,
'dateModified' => $modifiedDate,
'author' => array(
'@type' => 'Person',
'name' => $modifier
),
'publisher' => array(
'@type' => 'Organization',
'name' => $page_title
),
'headline' => $long_title
);
$article = '<script type="application/ld+json">' . json_encode($article, $jsonOption) . '</script>';
} else $article = '';
// BreadcrumbList(パンくずリスト)生成
if (PLUGIN_JSONLD_BREADCRUMBLIST && !$isHome ) {
$names = explode('/', $title);
$path = '';
$i = 0;
$bread = array();
$bread[] = array(
'@type' => 'ListItem',
'position' => ++$i,
'name' => $defaultpage,
'item' => $script
);
foreach ($names as $name) {
$path .= (($path != '')? '/' : '') . urlencode($name);
$bread[] = array(
'@type' => 'ListItem',
'position' => ++$i,
'name' => $name,
'item' => $script . '?' . $path
);
}
$bread = array(
'@context' => 'http:'.'//schema.org',
'@type' => 'BreadcrumbList',
'itemListElement' => $bread
);
$bread = '<script type="application/ld+json">' . json_encode($bread, $jsonOption) . '</script>';
} else $bread = '';
return $article . $bread;
}
**短縮URLに対応する [#g663b32d]
*改装後 [#zae46d27]
$thisPageUri = $script . (!$isHome ? '?' . str_replace('%2F', '/', urlencode($title)) : '');の下に次の行を追加します。38行目付近です。
<?php
define('PLUGIN_JSONLD_ARTICLE', true); // Article を出力
define('PLUGIN_JSONLD_BREADCRUMBLIST', true); // BreadcrumbList を出力
function plugin_jsonld_convert() {
global $modifier, $defaultpage, $page_title, $title;
// if (!PKWK_ALLOW_JAVASCRIPT) return ''; // JavaScriptではなくJSONなので無視
// 二重起動禁止
static $included = false;
if ($included) return '';
$included = true;
$script = get_script_uri();
$isHome = ($title == $defaultpage);
$long_title = (!$isHome ? $title . ' | ' : '') . $page_title;
$thisPageUri = $script . (!$isHome ? '?' . str_replace('%2F', '/', urlencode($title)) : '');
$shorturl = get_short_url_from_pagename($title); //PLUGIN_JSONLD_ARTICLE用の短縮URLを生成
$modifiedDate = date('Y-m-d\TH:i:sP', get_filetime($title));
$jsonOption = JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_PRETTY_PRINT; //改行ありのJSONになるようJSON_PRETTY_PRINTを追加
// Article(記事情報)生成
if (PLUGIN_JSONLD_ARTICLE) {
$article = array(
'@context' => 'http:'.'//schema.org',
'@type' => 'Article',
'mainEntityOfPage' => array(
'@type' => 'WebPage',
'@id' => $script . $shorturl
),
'datePublished' => $modifiedDate,
'dateModified' => $modifiedDate,
'author' => array(
'@type' => 'Person',
'name' => $page_title
),
'publisher' => array(
'@type' => 'Organization',
'name' => $page_title
),
'headline' => $long_title
);
$article = '<script type="application/ld+json">' . json_encode($article, $jsonOption) . '</script>';
} else $article = '';
// BreadcrumbList(パンくずリスト)生成
if (PLUGIN_JSONLD_BREADCRUMBLIST && !$isHome ) {
$names = explode('/', $title);
$path = '';
$i = 0;
$bread = array();
$bread[] = array(
'@type' => 'ListItem',
'position' => ++$i,
'name' => $defaultpage,
'item' => $script
);
foreach ($names as $name) {
$path .= (($path != '')? '/' : '') . urlencode($name);
$pathname .= (($pathname != '')? '/' : '') . $name; //パンくずリストの階層構造用
$shorturl = get_short_url_from_pagename($pathname);
$bread[] = array(
'@type' => 'ListItem',
'position' => ++$i,
'name' => $name,
'item' => $script . $shorturl,
);
}
$bread = array(
'@context' => 'http:'.'//schema.org',
'@type' => 'BreadcrumbList',
'name' => 'BreadcrumbList', //この行は無くてもOK
'itemListElement' => $bread
);
$bread = '<script type="application/ld+json">' . json_encode($bread, $jsonOption) . '</script>';
} else $bread = '';
return $article . $bread;
}
$shorturl = get_short_url_from_pagename($title);
foreach ($names as $name) {}の中に、下記の2行を追加します。「$path .= (($path != '')? '/' : '') . urlencode($name);」の次の行あたりで良いでしょう。85行目付近です。
$pathname .= (($pathname != '')? '/' : '') . $name;
$shorturl = get_short_url_from_pagename($pathname);
さらに各行を次のように書き換えます。
if (PLUGIN_JSONLD_ARTICLE) {}内(49行目付近)
'@id' => $thisPageUri //この行を削除
'@id' => $script . $shorturl //この行を追加
if (PLUGIN_JSONLD_BREADCRUMBLIST && !$isHome ) {}内(86行目付近)
'item' => $script . '?' . $path //この行を削除
'item' => $script . $shorturl //この行を追加
**見栄えを良くする [#hf8d79b5]
見栄えを良くするためにJSONのオプションにJSON_PRETTY_PRINTも適用します。これで適宜改行が入って見やすくなります。
$jsonOption = JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT;
$jsonOption = JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_PRETTY_PRINT;
また、リッチリザルトテストのサイトで「名前のないアイテム」とならないようにパンくずリストのJSON-LD内に下記の一行を加えていても良いと思います(SEO的には特に意味は無さそうですが)。92行目付近です。
'name' => 'BreadcrumbList'
*ダウンロード [#k09a092e]
いずれもpukiwiki 1.5.3をベースとした2021.6.18時点のファイルで、その後アップデートされていることがありますのでご注意ください。
&ref(jsonld.inc.php); (今回改装後のプラグイン)
&ref(jsonld_original.inc.php); (オリジナルのプラグイン)
#ogpi(https://oncologynote.com/?59a15c9237)