レ点腫瘍学ノート

pukiwikiカスタマイズ箇所/2021/jsonld.inc.phpの短縮URL対応 の履歴ソース(No.1)

#author("2021-06-16T18:32:31+09:00;1970-01-01T18:00:00+09:00","default:tgoto","tgoto")
pukiwikiのjsonld.inc.phpプラグインは簡単にpukiwikiにJSON-LDの構造化データを追加することができて重宝していましたが、短縮URLプラグインを使っているとパンくずリストのURLが正しく表示できず困っていましたので、ここに改変内容を記載しておきます。

jsonld.inc.phpと短縮URLについてはこちらのページをご覧ください。

#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]

 <?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;
 }

*改装後 [#zae46d27]

 <?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;
 }