From 110c9f979dd9f9fdf3035afefe4a5221cc5f3573 Mon Sep 17 00:00:00 2001 From: umherirrender Date: Wed, 25 Jul 2012 17:51:05 +0200 Subject: [PATCH] (bug 14237) Allow PAGESINCATEGORY to distinguish between 'all', 'pages', 'files' and 'subcats' Change-Id: I6dc90c6701c857256343f3c8f874dc20c6cc098a --- RELEASE-NOTES-1.20 | 2 + includes/parser/CoreParserFunctions.php | 63 +++++++++++++++++++------ languages/messages/MessagesEn.php | 4 ++ 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/RELEASE-NOTES-1.20 b/RELEASE-NOTES-1.20 index b859eb1959..6a52143e30 100644 --- a/RELEASE-NOTES-1.20 +++ b/RELEASE-NOTES-1.20 @@ -99,6 +99,8 @@ upgrade PHP if you have not done so prior to upgrading MediaWiki. instead of the site content language * (bug 37926) Deleterevision will no longer allow users to delete log entries, the new deletelogentry permission is required for this. +* (bug 14237) Allow PAGESINCATEGORY to distinguish between 'all', 'pages', 'files' + and 'subcats' === Bug fixes in 1.20 === * (bug 30245) Use the correct way to construct a log page title. diff --git a/includes/parser/CoreParserFunctions.php b/includes/parser/CoreParserFunctions.php index 912de41365..bb0b99a17c 100644 --- a/includes/parser/CoreParserFunctions.php +++ b/includes/parser/CoreParserFunctions.php @@ -565,29 +565,64 @@ class CoreParserFunctions { } /** - * Return the number of pages in the given category, or 0 if it's nonexis- - * tent. This is an expensive parser function and can't be called too many - * times per page. + * 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. * @return string */ - static function pagesincategory( $parser, $name = '', $raw = null ) { + static function pagesincategory( $parser, $name = '', $arg1 = null, $arg2 = null ) { + static $magicWords = null; + if ( is_null( $magicWords ) ) { + $magicWords = new MagicWordArray( array( + 'pagesincategory_all', + 'pagesincategory_pages', + 'pagesincategory_subcats', + 'pagesincategory_files' + ) ); + } static $cache = array(); - $category = Category::newFromName( $name ); - if( !is_object( $category ) ) { - $cache[$name] = 0; + // split the given option to its variable + if( self::isRaw( $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 ); } - # Normalize name for cache - $name = $category->getName(); + // Normalize name for cache + $name = $title->getDBkey(); - $count = 0; - if( isset( $cache[$name] ) ) { - $count = $cache[$name]; - } elseif( $parser->incrementExpensiveFunctionCount() ) { - $count = $cache[$name] = (int)$category->getPageCount(); + 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 ); } diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 48ec9b9091..461db78053 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -356,6 +356,10 @@ $magicWords = array( 'url_query' => array( 0, 'QUERY' ), 'defaultsort_noerror' => array( 0, 'noerror' ), 'defaultsort_noreplace' => array( 0, 'noreplace' ), + 'pagesincategory_all' => array( 0, 'all' ), + 'pagesincategory_pages' => array( 0, 'pages' ), + 'pagesincategory_subcats' => array( 0, 'subcats' ), + 'pagesincategory_files' => array( 0, 'files' ), ); /** -- 2.20.1