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

帮助:解析函数/arraymap

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

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

arraymap

将一个字符串按指定分隔符拆分,对每个子串按给定格式输出,并用分隔符或连接词拼接。出自扩展 Page Forms

语法

{{#arraymap: 字符串 | 分隔符 | 占位符 | 输出格式 | 输出分隔符 | 输出连接词 }}

参数说明:

  • 字符串:要处理的原始字符串,通常来自模板参数、SMW 查询或表达式结果。
  • 分隔符(可选):拆分用的符号,默认为英文逗号 ,。留空时逐字符拆分(包括空格与换行)。
  • 占位符:在“输出格式”中用作替换的标记。
  • 输出格式:包含占位符的 wikitext,解析时会将占位符替换为当前子串。
  • 输出分隔符(可选):拼接各结果时使用的分隔符,默认 , (逗号+空格)。
  • 输出连接词(可选):替代最后两个元素之间的分隔符,如“和”“或”。默认与输出分隔符相同。

示例

用法 代码 结果
基本替换 {{#arraymap:a_b_c | _ | @ | '''@''' | + }} a+b+c
自定义格式 {{#arraymap:a_b_c | _ | @ | 有'''@'''的内容 | …… }} a的内容……有b的内容……有c的内容
添加连接词 {{#arraymap:托奇_环理_小满_秘银_从云_辰纱 | _ | @ | [[首页|@]] | , |和 }}一起开会 托奇环理小满秘银从云辰纱一起开会
配合模板 {{#arraymap:apple, banana, cherry | , | @ | {{ucfirst:@}} }} Apple, Banana, Cherry
条件筛选 {{#arraymap: 2, 7, 4, 9, 3 |,|@|{{#ifexpr: @ > 5 | @ }} }} 7, 9

底层代码

/** mediawiki-extensions-PageForms-REL1_37 includes\parserfunctions\PF_ArrayMap.php
 * '#arraymap' is called as:
 * {{#arraymap:value|delimiter|var|formula|new_delimiter}}
 *
 * This function applies the same transformation to every section of a
 * delimited string; each such section, as dictated by the 'delimiter'
 * value, is given the same transformation that the 'var' string is
 * given in 'formula'. Finally, the transformed strings are joined
 * together using the 'new_delimiter' string. Both 'delimiter' and
 * 'new_delimiter' default to commas.
 *
 * Example: to take a semicolon-delimited list, and make each element
 * in the list a link, you could call the following:
 *
 * {{#arraymap:blue;red;yellow|;|x|[[x]]|;}}
 */

class PFArrayMap {
	public static function run( Parser $parser, $frame, $args ) {
		// Set variables.
		$value = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
		$delimiter = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ',';
		$var = isset( $args[2] ) ? trim( $frame->expand( $args[2], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : 'x';
		$formula = isset( $args[3] ) ? $args[3] : 'x';
		$new_delimiter = isset( $args[4] ) ? trim( $frame->expand( $args[4] ) ) : ', ';
		$conjunction = isset( $args[5] ) ? trim( $frame->expand( $args[5] ) ) : $new_delimiter;
		// Unstrip some.
		if ( method_exists( $parser, 'getStripState' ) ) {
			// MW 1.35+
			$delimiter = $parser->getStripState()->unstripNoWiki( $delimiter );
		} else {
			$delimiter = $parser->mStripState->unstripNoWiki( $delimiter );
		}
		// Let '\n' represent newlines, and '\s' represent spaces.
		$delimiter = str_replace( [ '\n', '\s' ], [ "\n", ' ' ], $delimiter );
		$new_delimiter = str_replace( [ '\n', '\s' ], [ "\n", ' ' ], $new_delimiter );
		$conjunction = str_replace( [ '\n', '\s' ], [ "\n", ' ' ], $conjunction );

		if ( $delimiter == '' ) {
			$values_array = preg_split( '/(.)/u', $value, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
		} else {
			$values_array = explode( $delimiter, $value );
		}

		$results_array = [];
		// Add results to the results array only if the old value was
		// non-null, and the new, mapped value is non-null as well.
		foreach ( $values_array as $old_value ) {
			$old_value = trim( $old_value );
			if ( $old_value == '' ) {
				continue;
			}
			$result_value = $frame->expand( $formula, PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES );
			$result_value = str_replace( $var, $old_value, $result_value );
			$result_value = $parser->preprocessToDom( $result_value, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 );
			$result_value = trim( $frame->expand( $result_value ) );
			if ( $result_value == '' ) {
				continue;
			}
			$results_array[] = $result_value;
		}
		if ( $conjunction != $new_delimiter ) {
			$conjunction = " " . trim( $conjunction ) . " ";
		}

		$result_text = "";
		$num_values = count( $results_array );
		for ( $i = 0; $i < $num_values; $i++ ) {
			if ( $i == 0 ) {
				$result_text .= $results_array[$i];
			} elseif ( $i == $num_values - 1 ) {
				$result_text .= $conjunction . $results_array[$i];
			} else {
				$result_text .= $new_delimiter . $results_array[$i];
			}
		}
		return $result_text;
	}
}
从 $args 数组中提取传递给函数的参数,并进行必要的处理和展开。
根据指定的分隔符将输入字符串拆分为一个数组。
遍历数组中的每个元素,使用指定的输出wikitext($formula)进行替换和展开,wikitext中的别名($var)被替换为当前元素的值。
对处理后的结果进行修整和展开。
如果结果非空,则将其添加到结果数组中。
根据指定的输出分隔符和输出连接词,将结果数组中的元素连接成最终的结果字符串。
返回结果字符串,用于输出。