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

帮助:解析函数/arraydefine

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

arraydefine

定义临时的数组变量,以便在同一页面后续使用。出自扩展 Arrays BWIKI和各大Wiki平台广泛使用此扩展。<br>在遥远的未来,它可能与Mediawiki新的并行解析器不兼容,请参阅扩展主页了解更多信息。

此函数根据指定的分隔符,将输入字符串切分成数组元素并存储到变量中,可选地进行去重、排序、直接输出等操作。

输入数据可以是固定文本,也可以来自模板参数、SMW 查询结果等。

基于 Arrays 扩展,可以快速构造表格、列表、图鉴等重复性结构(也可用 Loops 扩展或 Lua 模块实现)。

语法

{{#arraydefine: 数组名 | 数据字符串 | 分隔符 | 选项 }}

参数:

数组名:必填,要定义的变量名(区分大小写)
数据字符串:要拆分的内容,留空则定义空数组
分隔符:默认是逗号 (,),可使用正则。如留空,则不拆分,整个字符串作为一个元素
选项:逗号分隔的键=值对或单独的标志,支持:

定义的数组在当前页面的解析生命周期内有效,可与 #arrayprint 等函数配合。

示例

{{#arraydefine:a|red}} → 定义数组a,只有1个元素:red
{{#arraydefine:b|orange,red ,yellow, yellow}} → 定义数组b,有4个元素(分隔符没有指定,默认为逗号):orange、red、yellow、yellow
{{#arraydefine:c}} → 定义空数组c:
{{#arraydefine:d|apple, pear; orange|/\s*[;,]\s*/}} → 定义数组d,分隔符是正则表达式。d有3个元素:apple、pear、orange
{{#arraydefine:e|orange,red ,yellow, yellow|,|unique,sort=desc, print=list}} → 去重、降序排列,输出列表:yellow、red、orange
{{#arraydefine:e|orange,red ,yellow, yellow|,|unique,sort=desc, print=pretty}} → 同上,但是输出格式为pretty:yellow、red和orange


底层代码

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

/** mediawiki-extensions-Arrays-REL1_37 ExtArrays.php
	* Define an array by a list of 'values' deliminated by 'delimiter',
	* the delimiter should be perl regular expression pattern
	* usage:
	*      {{#arraydefine:arrayid|values|delimiter|options}}
	*
	* http://us2.php.net/manual/en/book.pcre.php
	* see also: http://us2.php.net/manual/en/function.preg-split.php
	*/
public static function pf_arraydefine(
		Parser &$parser,
		$arrayId,
		$value = null,
		$delimiter = '/\s*,\s*/',
		$options = ''
) {
	if ( !isset( $arrayId ) ) {
		return '';
	}

	$out = '';
	$array = [];
	$trimDone = false; // whether or not we can be sure that all array elements are trimmed

	// normalize
	$delimiter = trim( $delimiter );

	if ( $value === null ) {
		// no element set, not even an empty one
		$array = [];
	} else {
		// fill array with user input:
		if ( $delimiter === '' ) {
			// whole input one element, also takes care of special case empty '' value and 'unique' option set
			$array = [ $value ];
			$trimDone = true;
		} else {
			// if no regex delimiter given, build one:
			if ( !self::isValidRegEx( $delimiter ) ) {
				$delimiter = '/\s*' . preg_quote( $delimiter, '/' ) . '\s*/';
				$trimDone = true; // spaces are part of the delimiter now
			}
			$array = preg_split( $delimiter, $value );
		}

		// trim all values before unique if still necessary, otherwise unique might not work correctly
		if ( !$trimDone ) {
			$array = self::sanitizeArray( $array );
		}

		// now parse the options, and do posterior process on the created array
		$arrayOptions = self::parse_options( $options );

		// make it unique if option is set
		if ( array_key_exists( 'unique', $arrayOptions ) ) {
			// unique like the parser function would do it
			$array = self::array_unique( $array );
		}

		// if 'singleempty' is NOT set, {{#arraydefine:a|}} will be empty.
		// by default this would give an empty array (due to historical as well as usability reasons)
		if ( !array_key_exists( 'singleempty', $arrayOptions ) ) {
			// there is no other uncomplicated way than this to define a single empty elemented array currently!
			if ( count( $array ) === 1 && $array[0] === '' ) {
				$array = [];
			}
		}

		// sort array if the option is set
		if ( array_key_exists( 'sort', $arrayOptions ) ) {
			$array = self::arraySort( $array, self::array_value( $arrayOptions, 'sort' ) );
		}

		// print the array upon request
		switch ( self::array_value( $arrayOptions, 'print' ) ) {
			case 'list':
				// simple list output
				$out = implode( self::$mDefaultSep, $array );
				break;
			case 'pretty':
				global $wgLang;
				$out = $wgLang->listToText( $array );
				break;
		}
	}

	self::get( $parser )->setArray( $arrayId, $array );

	return $out;
}

工作原理简述

  1. 根据分隔符切分数据字符串(默认逗号)。
  2. 选项 unique:去重。
  3. 选项 sort:按指定方式排序。
  4. 选项 print:立即输出为文本。
  5. 存储:按给定数组名保存到当前解析上下文。