r33551 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r33550‎ | r33551 | r33552 >
Date:15:01, 18 April 2008
Author:simetrical
Status:old
Tags:
Comment:
(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.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/CoreParserFunctions.php (modified) (history)
  • /trunk/phase3/languages/messages/MessagesEn.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/CoreParserFunctions.php
@@ -42,6 +42,7 @@
4343 $parser->setFunctionHook( 'defaultsort', array( __CLASS__, 'defaultsort' ), SFH_NO_HASH );
4444 $parser->setFunctionHook( 'filepath', array( __CLASS__, 'filepath' ), SFH_NO_HASH );
4545 $parser->setFunctionHook( 'pagesincategory', array( __CLASS__, 'pagesincategory' ), SFH_NO_HASH );
 46+ $parser->setFunctionHook( 'pagesize', array( __CLASS__, 'pagesize' ), SFH_NO_HASH );
4647 $parser->setFunctionHook( 'tag', array( __CLASS__, 'tagObj' ), SFH_OBJECT_ARGS );
4748
4849 if ( $wgAllowDisplayTitle ) {
@@ -241,6 +242,43 @@
242243 return self::formatRaw( $count, $raw );
243244 }
244245
 246+ /**
 247+ * Return the size of the given page, or 0 if it's nonexistent. This is an
 248+ * expensive parser function and can't be called too many times per page.
 249+ *
 250+ * @FIXME This doesn't work correctly on preview for getting the size of
 251+ * the current page.
 252+ * @FIXME Title::getLength() documentation claims that it adds things to
 253+ * the link cache, so the local cache here should be unnecessary, but in
 254+ * fact calling getLength() repeatedly for the same $page does seem to
 255+ * run one query for each call?
 256+ */
 257+ static function pagesize( $parser, $page = '', $raw = null ) {
 258+ static $cache = array();
 259+ $title = Title::newFromText($page);
 260+
 261+ if( !is_object( $title ) ) {
 262+ $cache[$page] = 0;
 263+ return self::formatRaw( 0, $raw );
 264+ }
 265+
 266+ # Normalize name for cache
 267+ $page = $title->getPrefixedText();
 268+
 269+ $length = 0;
 270+ if( isset( $cache[$page] ) ) {
 271+ $length = $cache[$page];
 272+ } elseif( $parser->incrementExpensiveFunctionCount() ) {
 273+ $length = $cache[$page] = $title->getLength();
 274+
 275+ // Register dependency in templatelinks
 276+ $id = $title->getArticleId();
 277+ $revid = Revision::newFromTitle($title);
 278+ $parser->mOutput->addTemplate($title, $id, $revid);
 279+ }
 280+ return self::formatRaw( $length, $raw );
 281+ }
 282+
245283 static function language( $parser, $arg = '' ) {
246284 global $wgContLang;
247285 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
Index: trunk/phase3/languages/messages/MessagesEn.php
@@ -339,6 +339,7 @@
340340 'tag' => array( 0, 'tag' ),
341341 'hiddencat' => array( 1, '__HIDDENCAT__' ),
342342 'pagesincategory' => array( 1, 'PAGESINCATEGORY', 'PAGESINCAT' ),
 343+ 'pagesize' => array( 1, 'PAGESIZE' ),
343344 );
344345
345346 /**
Index: trunk/phase3/RELEASE-NOTES
@@ -82,6 +82,7 @@
8383 * Allow \C and \Q as TeX commands to match \R, \N, \Z
8484 * On Special:UserRights, when you can add a group you can't remove or remove
8585 one you can't add, a notice is printed to warn you
 86+* (bug 12698) Create PAGESIZE parser function, to return the size of a page
8687
8788 === Bug fixes in 1.13 ===
8889

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r33549* Add Parser::incrementExpensiveFunctionCount() and use it in CoreParserFunct...simetrical14:34, 18 April 2008

Status & tagging log