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

帮助:解析函数/pagesincategory

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

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

PAGESINCATEGORY

【高开销】指定分类的页面数量。MediaWiki原生支持。

语法

{{PAGESINCATEGORY: 分类名 | 选项 }}

  • 分类名:获取此分类的页面数量
  • 选项(可选):以下值中的一个
    • all 默认,分类中的所有页面
    • pages 仅普通页面(不包括子分类和文件)
    • subcats 仅子类别
    • files 仅文件

示例

  • {{PAGESINCATEGORY: 解析函数 }} → 21
  • {{PAGESINCATEGORY: 解析函数 | files }} → 0
  • {{PAGESINCATEGORY: 热力图 | all }} → 2
  • {{PAGESINCATEGORY: 热力图 | all | R }} → 2
  • {{PAGESINCATEGORY: 热力图 | R | all }} → 2

底层代码

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

/** mediawiki-1.37.0\includes\parser\CoreParserFunctions.php
 * Return the number of pages, files or subcats in the given category,
 * or 0 if it's nonexistent. This is an expensive parser function and
 * can't be called too many times per page.
 * @param Parser $parser
 * @param string $name
 * @param string|null $arg1
 * @param string|null $arg2
 * @return string
 */
public static function pagesincategory( $parser, $name = '', $arg1 = null, $arg2 = null ) {
	static $magicWords = null;
	if ( $magicWords === null ) {
		$magicWords = $parser->getMagicWordFactory()->newArray( [
			'pagesincategory_all',
			'pagesincategory_pages',
			'pagesincategory_subcats',
			'pagesincategory_files'
		] );
	}
	static $cache = [];

	// split the given option to its variable
	if ( self::matchAgainstMagicword( $parser->getMagicWordFactory(), 'rawsuffix', $arg1 ) ) {
		// {{pagesincategory:|raw[|type]}}
		$raw = $arg1;
		$type = $magicWords->matchStartToEnd( $arg2 );
	} else {
		// {{pagesincategory:[|type[|raw]]}}
		$type = $magicWords->matchStartToEnd( $arg1 );
		$raw = $arg2;
	}
	if ( !$type ) { // backward compatibility
		$type = 'pagesincategory_all';
	}

	$title = Title::makeTitleSafe( NS_CATEGORY, $name );
	if ( !$title ) { # invalid title
		return self::formatRaw( 0, $raw, $parser->getFunctionLang() );
	}
	$languageConverter = MediaWikiServices::getInstance()
		->getLanguageConverterFactory()
		->getLanguageConverter( $parser->getContentLanguage() );
	$languageConverter->findVariantLink( $name, $title, true );

	// Normalize name for cache
	$name = $title->getDBkey();

	if ( !isset( $cache[$name] ) ) {
		$category = Category::newFromTitle( $title );

		$allCount = $subcatCount = $fileCount = $pagesCount = 0;
		if ( $parser->incrementExpensiveFunctionCount() ) {
			// $allCount is the total number of cat members,
			// not the count of how many members are normal pages.
			$allCount = (int)$category->getPageCount();
			$subcatCount = (int)$category->getSubcatCount();
			$fileCount = (int)$category->getFileCount();
			$pagesCount = $allCount - $subcatCount - $fileCount;
		}
		$cache[$name]['pagesincategory_all'] = $allCount;
		$cache[$name]['pagesincategory_pages'] = $pagesCount;
		$cache[$name]['pagesincategory_subcats'] = $subcatCount;
		$cache[$name]['pagesincategory_files'] = $fileCount;
	}

	$count = $cache[$name][$type];
	return self::formatRaw( $count, $raw, $parser->getFunctionLang() );
}
代码逻辑:
  • 解析输入参数。支持指定统计特定类型的页面数量,和R参数(输出数字不用逗号分隔符)
  • 如果没有缓存(页面级),则记为调用一次高开销函数。查询数据并缓存。
    • 如果超过开销限制,获取的数据为默认值0
  • 从缓存获取指定类型的页面数量,格式化并返回

实际用例

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