* Add Parser::incrementExpensiveFunctionCount() and use it in CoreParserFunctions...
authorAryeh Gregor <simetrical@users.mediawiki.org>
Fri, 18 Apr 2008 14:34:38 +0000 (14:34 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Fri, 18 Apr 2008 14:34:38 +0000 (14:34 +0000)
* 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
includes/Parser.php

index 61fad6c..7602da2 100644 (file)
@@ -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 = '' ) {
index 6f8ae85..28619e8 100644 (file)
@@ -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