From 5c753e7728897bdbfc5dd57c37f67bab7d7a6af2 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Fri, 18 Apr 2008 15:01:04 +0000 Subject: [PATCH] (bug 12698) Create PAGESIZE parser function, to return the size of a page. Quite possibly this is getting out of hand; in that case, revert. Patch based on one by CBM/carl-m. --- RELEASE-NOTES | 1 + includes/CoreParserFunctions.php | 38 +++++++++++++++++++++++++++++++ languages/messages/MessagesEn.php | 1 + 3 files changed, 40 insertions(+) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 65cf8fa456..d86171ed92 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 === diff --git a/includes/CoreParserFunctions.php b/includes/CoreParserFunctions.php index 7cd083e031..3f6f437496 100644 --- a/includes/CoreParserFunctions.php +++ b/includes/CoreParserFunctions.php @@ -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 ) ); diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 5cefa6d87c..c23ab4431b 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -339,6 +339,7 @@ $magicWords = array( 'tag' => array( 0, 'tag' ), 'hiddencat' => array( 1, '__HIDDENCAT__' ), 'pagesincategory' => array( 1, 'PAGESINCATEGORY', 'PAGESINCAT' ), + 'pagesize' => array( 1, 'PAGESIZE' ), ); /** -- 2.20.1