(bug 12698) Create PAGESIZE parser function, to return the size of a page. Quite...
authorAryeh Gregor <simetrical@users.mediawiki.org>
Fri, 18 Apr 2008 15:01:04 +0000 (15:01 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Fri, 18 Apr 2008 15:01:04 +0000 (15:01 +0000)
RELEASE-NOTES
includes/CoreParserFunctions.php
languages/messages/MessagesEn.php

index 65cf8fa..d86171e 100644 (file)
@@ -82,6 +82,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * Allow \C and \Q as TeX commands to match \R, \N, \Z
 * On Special:UserRights, when you can add a group you can't remove or remove
   one you can't add, a notice is printed to warn you
+* (bug 12698) Create PAGESIZE parser function, to return the size of a page
 
 === Bug fixes in 1.13 ===
 
index 7cd083e..3f6f437 100644 (file)
@@ -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 ) {
@@ -241,6 +242,43 @@ class CoreParserFunctions {
                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 = '' ) {
                global $wgContLang;
                $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
index 5cefa6d..c23ab44 100644 (file)
@@ -339,6 +339,7 @@ $magicWords = array(
        'tag'                    => array( 0,    'tag'                    ),
        'hiddencat'              => array( 1,    '__HIDDENCAT__'          ),
        'pagesincategory'        => array( 1,    'PAGESINCATEGORY', 'PAGESINCAT' ),
+       'pagesize'               => array( 1,    'PAGESIZE'               ),
 );
 
 /**