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

帮助:解析函数/pagenamee

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

PAGENAMEE是一个变量。帮助:解析函数页列出了所有解析函数的说明。

PAGENAMEE

获取页面名,以URL编码,不带命名空间。MediaWiki原生支持。

语法

  • {{PAGENAMEE}}
    • 当前页面名
  • {{PAGENAMEE: 页面名称}}
    • 获取指定页面名称的页面名

示例

  • {{PAGENAMEE}} → %E8%A7%A3%E6%9E%90%E5%87%BD%E6%95%B0/pagenamee (解析函数/pagenamee)
  • {{PAGENAMEE:帮助:解析函数}} → %E8%A7%A3%E6%9E%90%E5%87%BD%E6%95%B0 (解析函数)

底层代码

来自MediaWiki及其扩展的源代码,运行在服务端。此处仅供快速查阅,便于更充分的挖掘其“特性”。

// mediawiki-1.37.0\includes\parser\CoreParserFunctions.php
public static function pagenamee( $parser, $title = null ) {
	$t = Title::newFromText( $title );
	if ( $t === null ) {
		return '';
	}
	return wfEscapeWikiText( $t->getPartialURL() );
}


/** mediawiki-1.37.0\includes\GlobalFunctions.php
 * We want some things to be included as literal characters in our title URLs
 * for prettiness, which urlencode encodes by default.  According to RFC 1738,
 * all of the following should be safe:
 *
 * ;:@&=$-_.+!*'(),
 *
 * RFC 1738 says ~ is unsafe, however RFC 3986 considers it an unreserved
 * character which should not be encoded. More importantly, google chrome
 * always converts %7E back to ~, and converting it in this function can
 * cause a redirect loop (T105265).
 *
 * But + is not safe because it's used to indicate a space; &= are only safe in
 * paths and not in queries (and we don't distinguish here); ' seems kind of
 * scary; and urlencode() doesn't touch -_. to begin with.  Plus, although /
 * is reserved, we don't care.  So the list we unescape is:
 *
 * ;:@$!*(),/~
 *
 * However, IIS7 redirects fail when the url contains a colon (see T24709),
 * so no fancy : for IIS7.
 *
 * %2F in the page titles seems to fatally break for some reason.
 *
 * @param string $s
 * @return string
 */
function wfUrlencode( $s ) {
	static $needle;

	if ( $s === null ) {
		// Reset $needle for testing.
		$needle = null;
		return '';
	}

	if ( $needle === null ) {
		$needle = [ '%3B', '%40', '%24', '%21', '%2A', '%28', '%29', '%2C', '%2F', '%7E' ];
		if ( !isset( $_SERVER['SERVER_SOFTWARE'] ) ||
			( strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/7' ) === false )
		) {
			$needle[] = '%3A';
		}
	}

	$s = urlencode( $s );
	$s = str_ireplace(
		$needle,
		[ ';', '@', '$', '!', '*', '(', ')', ',', '/', '~', ':' ],
		$s
	);

	return $s;
}
代码逻辑:
  • 将输入参数转换为标题
  • 不合规的标题会返回空字符串
  • 返回不带命名空间的标题
    • 获取标题的方法与pagename函数不同,getPartialURL方法会返回经过 wfUrlencode 的标题
    • 尝试转义标题中的wiki语法,确保可以安全输出到wiki文本中防止特殊字符被解析为wiki标记

实际用例

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