API: Requesting a token you aren't allowed to request no longer dies with an error...
[lhc/web/wiklou.git] / includes / CoreParserFunctions.php
index 61fad6c..d9072e9 100644 (file)
@@ -2,7 +2,7 @@
 
 /**
  * Various core parser functions, registered in Parser::firstCallInit()
- * @addtogroup Parser
+ * @ingroup Parser
  */
 class CoreParserFunctions {
        static function register( $parser ) {
@@ -42,6 +42,7 @@ class CoreParserFunctions {
                $parser->setFunctionHook( 'defaultsort',      array( __CLASS__, 'defaultsort'      ), SFH_NO_HASH );
                $parser->setFunctionHook( 'filepath',         array( __CLASS__, 'filepath'         ), SFH_NO_HASH );
                $parser->setFunctionHook( 'pagesincategory',  array( __CLASS__, 'pagesincategory'  ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'pagesize',         array( __CLASS__, 'pagesize'         ), SFH_NO_HASH );
                $parser->setFunctionHook( 'tag',              array( __CLASS__, 'tagObj'           ), SFH_OBJECT_ARGS );
 
                if ( $wgAllowDisplayTitle ) {
@@ -220,17 +221,62 @@ 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);
+       static function pagesincategory( $parser, $name = '', $raw = null ) {
+               static $cache = array();
+               $category = Category::newFromName( $name );
+
                if( !is_object( $category ) ) {
-                       return 0;
+                       $cache[$name] = 0;
+                       return self::formatRaw( 0, $raw );
                }
-               $parser->mExpensiveFunctionCount++;
-               if ($parser->mExpensiveFunctionCount <= $wgExpensiveParserFunctionLimit) {
-                       return self::formatRaw( (int)$category->getPageCount(), $raw );
+
+               # Normalize name for cache
+               $name = $category->getName();
+
+               $count = 0;
+               if( isset( $cache[$name] ) ) {
+                       $count = $cache[$name];
+               } elseif( $parser->incrementExpensiveFunctionCount() ) {
+                       $count = $cache[$name] = (int)$category->getPageCount();
                }
-               return 0;
+               return self::formatRaw( $count, $raw );
+       }
+
+       /**
+        * Return the size of the given page, or 0 if it's nonexistent.  This is an
+        * expensive parser function and can't be called too many times per page.
+        *
+        * @FIXME This doesn't work correctly on preview for getting the size of
+        *   the current page.
+        * @FIXME Title::getLength() documentation claims that it adds things to
+        *   the link cache, so the local cache here should be unnecessary, but in
+        *   fact calling getLength() repeatedly for the same $page does seem to
+        *   run one query for each call?
+        */
+       static function pagesize( $parser, $page = '', $raw = null ) {
+               static $cache = array();
+               $title = Title::newFromText($page);
+
+               if( !is_object( $title ) ) {
+                       $cache[$page] = 0;
+                       return self::formatRaw( 0, $raw );
+               }
+
+               # Normalize name for cache
+               $page = $title->getPrefixedText();
+
+               $length = 0;
+               if( isset( $cache[$page] ) ) {
+                       $length = $cache[$page];
+               } elseif( $parser->incrementExpensiveFunctionCount() ) {
+                       $length = $cache[$page] = $title->getLength();
+       
+                       // Register dependency in templatelinks
+                       $id = $title->getArticleId();
+                       $revid = Revision::newFromTitle($title);
+                       $parser->mOutput->addTemplate($title, $id, $revid);
+               }       
+               return self::formatRaw( $length, $raw );
        }
 
        static function language( $parser, $arg = '' ) {