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

帮助:解析函数/ifeq

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

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

ifeq

条件分支函数。出自扩展 ParserFunctions

比较两个输入值是否相等,根据比较结果返回对应字符串。

语法

{{#ifeq: 值1 | 值2 | 相等返回值 | 不等返回值 }}

  • 值1:第一个比较值
  • 值2:第二个比较值
  • 相等返回值:当两值相等时返回的内容
  • 不等返回值(可选):当两值不等时返回的内容(可选,默认空字符串)

当两个参数都可转换为有效数值时进行数值比较(忽略前导零和科学计数法差异),否则进行严格文本比较(区分大小写)。

示例

  • 数值比较 {{#ifeq: 01 | 1 | equal | not equal}} → equal
  • 数值比较 {{#ifeq: 1e3 | 1000 | equal | not equal}} → equal
  • 文本比较 {{#ifeq: "01" | "1" | equal | not equal}} → not equal
  • 大小写敏感 {{#ifeq: Foo | foo | equal | not equal}} → not equal
  • 标签比较
    {{#ifeq: <nowiki>foo</nowiki> | <nowiki>foo</nowiki> | equal | not equal}}
    
    → not equal(在解析函数内部的标签和解析函数(如nowiki、pre、includeonly)会被暂时替换为一个唯一的Strip marker。这会影响到比较结果)

底层代码

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

/** mediawiki-extensions-ParserFunctions-REL1_37\includes\ParserFunctions.php
 * {{#ifeq: string 1 | string 2 | value if identical | value if different }}
 *
 * @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##ifeq
 *
 * @param Parser $parser
 * @param PPFrame $frame
 * @param array $args
 * @return string
 */
public static function ifeq( Parser $parser, PPFrame $frame, array $args ) {
	$left = isset( $args[0] ) ? self::decodeTrimExpand( $args[0], $frame ) : '';
	$right = isset( $args[1] ) ? self::decodeTrimExpand( $args[1], $frame ) : '';

	// Strict compare is not possible here. 01 should equal 1 for example.
	/** @noinspection TypeUnsafeComparisonInspection */
	if ( $left == $right ) {
		return isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : '';
	} else {
		return isset( $args[3] ) ? trim( $frame->expand( $args[3] ) ) : '';
	}
}
代码逻辑:
  • 解码并展开前两个参数(去除前后空白)
  • 比较并返回对应分支的展开值(去除前后空白)

实际用例

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

原神 - ys

战双帕弥什 - zspms

明日方舟 - arknights

恋与深空 - lysk

崩坏:星穹铁道 - sr

代号鸢 - yuan

赛马娘 - umamusume

第五人格 - dwrg

坎特伯雷公主与骑士唤醒冠军之剑的奇幻冒险 - gt

黑神话:悟空 - wukong

WIKI实验室 - tools