From 82a87bd33c2130f980e4d8c85841ba5084bf7424 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Fri, 18 Apr 2008 14:34:38 +0000 Subject: [PATCH] * Add Parser::incrementExpensiveFunctionCount() and use it in CoreParserFunctions::getcategorycount(); refactor slightly so that formatRaw is called even for 0 (do any languages format that differently? maybe if they use non-Arabic numerals?). From bug 12698, by CBM. * Add a cache so that calling getcategorycount for the same category many times won't stupidly repeat the same query over and over. Currently an early return avoids incrementing the expensive function count in this case, so technically this makes things *slower*, not faster, but that can be tweaked if anyone cares. --- includes/CoreParserFunctions.php | 23 ++++++++++++++--------- includes/Parser.php | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/includes/CoreParserFunctions.php b/includes/CoreParserFunctions.php index 61fad6cfac..7602da229a 100644 --- a/includes/CoreParserFunctions.php +++ b/includes/CoreParserFunctions.php @@ -220,17 +220,22 @@ class CoreParserFunctions { * tent. This is an expensive parser function and can't be called too many * times per page. */ - static function pagesincategory( $parser, $category = '', $raw = null ) { - global $wgExpensiveParserFunctionLimit; - $category = Category::newFromName($category); - if( !is_object( $category ) ) { - return 0; + static function pagesincategory( $parser, $name = '', $raw = null ) { + static $cache = array(); + $category = Category::newFromName( $name ); + + # Normalize name for cache + $name = $category->getName(); + + if( isset( $cache[$name] ) ) { + return self::formatRaw( $cache[$name], $raw ); } - $parser->mExpensiveFunctionCount++; - if ($parser->mExpensiveFunctionCount <= $wgExpensiveParserFunctionLimit) { - return self::formatRaw( (int)$category->getPageCount(), $raw ); + + $count = 0; + if( is_object( $category ) && $parser->incrementExpensiveFunctionCount() ) { + $count = $cache[$name] = (int)$category->getPageCount(); } - return 0; + return self::formatRaw( $count, $raw ); } static function language( $parser, $arg = '' ) { diff --git a/includes/Parser.php b/includes/Parser.php index 6f8ae8520c..28619e81b6 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -3284,6 +3284,20 @@ class Parser } } + /** + * Increment the expensive function count + * + * @return boolean False if the limit has been exceeded + */ + function incrementExpensiveFunctionCount() { + global $wgExpensiveParserFunctionLimit; + $this->mExpensiveFunctionCount++; + if($this->mExpensiveFunctionCount <= $wgExpensiveParserFunctionLimit) { + return true; + } + return false; + } + /** * Strip double-underscore items like __NOGALLERY__ and __NOTOC__ * Fills $this->mDoubleUnderscores, returns the modified text -- 2.20.1