TOOLS为非官方社区,欢迎一起建设,答疑群:717421103

版本260427.1

全站通知:

帮助:解析函数/basepagename

来自ToolsWIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

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

BASEPAGENAME

获取上级页面名(不含命名空间)。MediaWiki原生支持。

在启用子页面特性的命名空间中,将页面标题去掉最后一个“/”及其后续部分,保留余下部分(且不含命名空间)。

语法

  • {{BASEPAGENAME}} 作用于当前页面
  • {{BASEPAGENAME: 页面名}} 指定页面名进行计算

仅对启用子页面特性的命名空间生效(如:主、用户、Project、MediaWiki、模板、帮助、模块)。未启用的命名空间(如:文件、分类、Widget)中,其结果等同于整页名(不视为子页面)。

若标题中不包含“/”,结果为整页名(不含命名空间)。

示例

{{BASEPAGENAME}} → 解析函数
{{BASEPAGENAME:帮助:解析函数/basepagename}} → 解析函数
{{BASEPAGENAME:模板:大模板/中模板/小模板}} → 大模板/中模板
{{BASEPAGENAME:分类:超大分类/大分类/中分类}} → 超大分类/大分类/中分类 (分类空间不支持子页面特性,因此这一页面是独立页面,不被视为“超大分类”的子页面)

可用命名空间

命名空间 可用 命名空间 可用
主 ✓ 讨论 ✓
用户 ✓ 用户讨论 ✓
Project ✓ Project talk ✓
文件 ✕ 文件讨论 ✓
MediaWiki ✓ MediaWiki讨论 ✓
模板 ✓ 模板讨论 ✓
帮助 ✓ 帮助讨论 ✓
命名空间 可用 命名空间 可用
分类 ✕ 分类讨论 ✓
属性 ✕ 属性讨论 ✕
表单 ✕ 表单讨论 ✕
概念 ✕ 概念讨论 ✕
Widget ✕ Widget talk ✓
模块 ✓ 模块讨论 ✓

底层代码

/** mediawiki-1.37.0\includes\parser\CoreMagicVariables.php
    * Expand the magic variable given by $index.
    * @internal
    * @param Parser $parser
    * @param string $id The name of the variable, and equivalently, the magic
    *   word ID which was used to match the variable
    * @param int $ts Timestamp to use when expanding magic variable
    * @param NamespaceInfo $nsInfo The NamespaceInfo to use when expanding
    * @param ServiceOptions $svcOptions Service options for the parser
    * @param LoggerInterface $logger
    * @return string|null The expanded value, or null to indicate the given
    *  index wasn't a known magic variable.
    */
public static function expand(
    // Fundamental options
    Parser $parser, 
    string $id,
    // Context passed over from the parser
    int $ts,
    NamespaceInfo $nsInfo,
    ServiceOptions $svcOptions,
    LoggerInterface $logger
): ?string {
    $pageLang = $parser->getFunctionLang();
    $title = $parser->getTitle();

    switch ( $id ) {

        ...

        case 'basepagename':
            return wfEscapeWikiText( $title->getBaseText() );
        case 'basepagenamee':
            return wfEscapeWikiText( wfUrlencode( str_replace(
                ' ',
                '_',
                $title->getBaseText()
            ) ) );

        ...

    }
}


/** mediawiki-1.37.0\includes\Title.php
 * Get the base page name without a namespace, i.e. the part before the subpage name
 *
 * @note the return value may contain trailing whitespace and is thus
 * not safe for use with makeTitle or TitleValue.
 *
 * @par Example: 
 * @code
 * Title::newFromText('User:Foo/Bar/Baz')->getBaseText();
 * # returns: 'Foo/Bar'
 * @endcode
 *
 * @return string Base name
 */
public function getBaseText() {
	$text = $this->getText();
	if ( !$this->hasSubpagesEnabled() ) {
		return $text;
	}

	$lastSlashPos = $this->findSubpageDivider( $text, -1 );
	// Don't discard the real title if there's no subpage involved
	if ( $lastSlashPos === false ) {
		return $text;
	}

	return substr( $text, 0, $lastSlashPos );
}
代码逻辑:
  • MediaWiki的魔术字变量由 expand 函数实现。对于BASEPAGENAME,直接将标题的getBaseText()方法结果作为返回值。
  • getBaseText()方法中,首先获取不带命名空间的页面名,存入 $text。
  • 判断当前页面所在命名空间是否启用了子页面,如果没有启用子页面,就直接返回 $text,因为此时任何页面都不被视为子页面。
  • 如果启用了子页面,就寻找标题中最后一个“/”符号位置,返回标题中此位置前的部分。比如对“Foo/Bar/Baz”,返回“Foo/Bar”

实际用例

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

黑神话:悟空 - wukong

交错战线 - crosscore

觅长生 - mcs

烟雨江湖 - yanyu

灰烬战线 - hjzx

缺氧 - oni

骷髅女孩 - skullgirls

腐蚀 - rust

CSGO - csgo