Tools 是非官方社区Wiki。社区文档正在编写中,欢迎参与。 Wiki编辑答疑群:717421103
版本250722.2
全站通知:

帮助:解析函数/canonicalurl

来自WIKI实验室WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

Canonicalurl是一个魔术字。帮助:解析函数页列出了所有解析函数和魔术字的说明。

canonicalurl

生成页面的完整URL,支持附加参数。MediaWiki原生支持。

此URL为完整绝对路径(包含协议、域名和规范化页面路径),不会验证页面或参数是否有效。

语法

{{canonicalurl: 页面名 | 参数 }}
  • 页面名:目标页面的标题。不存在的页面也可生成URL。
  • 参数:可选,附加到URL的查询字符串(不自动添加`&`,需完整书写,如action=edita=111&b=233)。
    • 多参数需要用`&`连接(如`action=edit&section=new`)。
  • 相关函数是canonicalurle,它会将< > " &等符号编码为HTML实体 → &lt;&gt;&quot;&amp;

示例

底层代码

/* mediawiki-1.37.0\includes\parser\CoreParserFunctions.php */
public static function canonicalurl( $parser, $s = '', $arg = null ) {
	return self::urlFunction( 'getCanonicalURL', $s, $arg );
}

/* mediawiki-1.37.0\includes\parser\CoreParserFunctions.php */
public static function urlFunction( $func, $s = '', $arg = null ) {
	$title = Title::newFromText( $s );
	# Due to order of execution of a lot of bits, the values might be encoded
	# before arriving here; if that's true, then the title can't be created
	# and the variable will fail. If we can't get a decent title from the first
	# attempt, url-decode and try for a second.
	if ( $title === null ) {
		$title = Title::newFromURL( urldecode( $s ) );
	}
	if ( $title !== null ) {
		# Convert NS_MEDIA -> NS_FILE
		if ( $title->inNamespace( NS_MEDIA ) ) {
			$title = Title::makeTitle( NS_FILE, $title->getDBkey() );
		}
		if ( $arg !== null ) {
			$text = $title->$func( $arg );
		} else {
			$text = $title->$func();
		}
		return $text;
	} else {
		return [ 'found' => false ];
	}
}
代码逻辑:
  • 根据页面名参数构建一个 Title 对象,根据参数生成URL
  • getCanonicalURL函数涉及太多mediawiki处理URL的函数,不在此列出。

实际用例

一些Wiki使用了相关特性,如下所示这个静态列表可能在下列页面更改后过时仅供批判性参考