r32932 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r32931‎ | r32932 | r32933 >
Date:22:11, 7 April 2008
Author:brion
Status:old
Tags:
Comment:
* (6943) Added PAGESINCATEGORY: magic word
Patch by Mr.Z-man, https://bugzilla.wikimedia.org/attachment.cgi?id=4793
Moves some of the 'expensive parser function' tracking to core.
Modified paths:
  • /trunk/extensions/ParserFunctions/ParserFunctions.php (modified) (history)
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/CoreParserFunctions.php (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/MagicWord.php (modified) (history)
  • /trunk/phase3/includes/Parser.php (modified) (history)
  • /trunk/phase3/languages/messages/MessagesEn.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/ParserFunctions.php
@@ -16,10 +16,7 @@
1717
1818 $wgExtensionMessagesFiles['ParserFunctions'] = dirname(__FILE__) . '/ParserFunctions.i18n.php';
1919 $wgHooks['LanguageGetMagic'][] = 'wfParserFunctionsLanguageGetMagic';
20 -$wgHooks['ParserLimitReport'][] = 'wfParserFunctionsLimitReport';
2120
22 -$wgMaxIfExistCount = 100;
23 -
2421 class ExtParserFunctions {
2522 var $mExprParser;
2623 var $mTimeCache = array();
@@ -55,7 +52,6 @@
5653
5754 function clearState(&$parser) {
5855 $this->mTimeChars = 0;
59 - $parser->pf_ifexist_count = 0;
6056 $parser->pf_ifexist_breakdown = array();
6157 return true;
6258 }
@@ -315,8 +311,8 @@
316312
317313 function incrementIfexistCount( $parser, $frame ) {
318314 // Don't let this be called more than a certain number of times. It tends to make the database explode.
319 - global $wgMaxIfExistCount;
320 - $parser->pf_ifexist_count++;
 315+ global $wgExpensiveParserFunctionLimit;
 316+ $parser->mExpensiveFunctionCount++;
321317 if ( $frame ) {
322318 $pdbk = $frame->getPDBK( 1 );
323319 if ( !isset( $parser->pf_ifexist_breakdown[$pdbk] ) ) {
@@ -324,7 +320,7 @@
325321 }
326322 $parser->pf_ifexist_breakdown[$pdbk] ++;
327323 }
328 - return $parser->pf_ifexist_count <= $wgMaxIfExistCount;
 324+ return $parser->mExpensiveFunctionCount <= $wgExpensiveParserFunctionLimit;
329325 }
330326
331327 function ifexist( &$parser, $title = '', $then = '', $else = '' ) {
@@ -357,15 +353,14 @@
358354 } else {
359355 $pdbk = $title->getPrefixedDBkey();
360356 $lc = LinkCache::singleton();
 357+ if ( !$this->incrementIfexistCount( $parser, $frame ) ) {
 358+ return $else;
 359+ }
361360 if ( $lc->getGoodLinkID( $pdbk ) ) {
362361 return $then;
363362 } elseif ( $lc->isBadLink( $pdbk ) ) {
364363 return $else;
365364 }
366 - if ( !$this->incrementIfexistCount( $parser, $frame ) ) {
367 - return $else;
368 - }
369 -
370365 $id = $title->getArticleID();
371366 $parser->mOutput->addLink( $title, $id );
372367 if ( $id ) {
@@ -476,22 +471,6 @@
477472 return $title;
478473 }
479474 }
480 -
481 - function afterTidy( &$parser, &$text ) {
482 - global $wgMaxIfExistCount;
483 - if ( $parser->pf_ifexist_count > $wgMaxIfExistCount ) {
484 - if ( is_callable( array( $parser->mOutput, 'addWarning' ) ) ) {
485 - wfLoadExtensionMessages( 'ParserFunctions' );
486 - $warning = wfMsg( 'pfunc_ifexist_warning', $parser->pf_ifexist_count, $wgMaxIfExistCount );
487 - $parser->mOutput->addWarning( $warning );
488 - $cat = Title::makeTitleSafe( NS_CATEGORY, wfMsgForContent( 'pfunc_max_ifexist_category' ) );
489 - if ( $cat ) {
490 - $parser->mOutput->addCategory( $cat->getDBkey(), $parser->getDefaultSort() );
491 - }
492 - }
493 - }
494 - return true;
495 - }
496475 }
497476
498477 function wfSetupParserFunctions() {
@@ -510,7 +489,6 @@
511490 }
512491
513492 $wgHooks['ParserClearState'][] = array( &$wgExtParserFunctions, 'clearState' );
514 - $wgHooks['ParserAfterTidy'][] = array( &$wgExtParserFunctions, 'afterTidy' );
515493 }
516494
517495 function wfParserFunctionsLanguageGetMagic( &$magicWords, $langCode ) {
@@ -520,11 +498,3 @@
521499 return true;
522500 }
523501
524 -function wfParserFunctionsLimitReport( $parser, &$report ) {
525 - global $wgMaxIfExistCount;
526 - if ( isset( $parser->pf_ifexist_count ) ) {
527 - $report .= "#ifexist count: {$parser->pf_ifexist_count}/$wgMaxIfExistCount\n";
528 - }
529 - return true;
530 -}
531 -
Index: trunk/phase3/includes/Parser.php
@@ -102,6 +102,7 @@
103103 var $mIncludeSizes, $mPPNodeCount, $mDefaultSort;
104104 var $mTplExpandCache; // empty-frame expansion cache
105105 var $mTplRedirCache, $mTplDomCache, $mHeadings, $mDoubleUnderscores;
 106+ var $mExpensiveFunctionCount; // number of expensive parser function calls
106107
107108 # Temporary
108109 # These are variables reset at least once per parse regardless of $clearState
@@ -217,6 +218,7 @@
218219 $this->mDefaultSort = false;
219220 $this->mHeadings = array();
220221 $this->mDoubleUnderscores = array();
 222+ $this->mExpensiveFunctionCount = 0;
221223
222224 # Fix cloning
223225 if ( isset( $this->mPreprocessor ) && $this->mPreprocessor->parser !== $this ) {
@@ -390,17 +392,31 @@
391393 array_values( $tidyregs ),
392394 $text );
393395 }
 396+ global $wgExpensiveParserFunctionLimit;
 397+ if ( $this->mExpensiveFunctionCount > $wgExpensiveParserFunctionLimit ) {
 398+ if ( is_callable( array( $this->mOutput, 'addWarning' ) ) ) {
 399+ $warning = wfMsg( 'expensive-parserfunction-warning', $this->mExpensiveFunctionCount, $wgExpensiveParserFunctionLimit );
 400+ $this->mOutput->addWarning( $warning );
 401+ $cat = Title::makeTitleSafe( NS_CATEGORY, wfMsgForContent( 'expensive-parserfunction-category' ) );
 402+ if ( $cat ) {
 403+ $this->mOutput->addCategory( $cat->getDBkey(), $this->getDefaultSort() );
 404+ }
 405+ }
 406+ }
394407
395408 wfRunHooks( 'ParserAfterTidy', array( &$this, &$text ) );
396409
397410 # Information on include size limits, for the benefit of users who try to skirt them
398411 if ( $this->mOptions->getEnableLimitReport() ) {
 412+ global $wgExpensiveParserFunctionLimit;
399413 $max = $this->mOptions->getMaxIncludeSize();
 414+ $PFreport = "Expensive parser function count: {$this->mExpensiveFunctionCount}/$wgExpensiveParserFunctionLimit\n";
400415 $limitReport =
401416 "NewPP limit report\n" .
402417 "Preprocessor node count: {$this->mPPNodeCount}/{$this->mOptions->mMaxPPNodeCount}\n" .
403418 "Post-expand include size: {$this->mIncludeSizes['post-expand']}/$max bytes\n" .
404 - "Template argument size: {$this->mIncludeSizes['arg']}/$max bytes\n";
 419+ "Template argument size: {$this->mIncludeSizes['arg']}/$max bytes\n".
 420+ $PFreport;
405421 wfRunHooks( 'ParserLimitReport', array( $this, &$limitReport ) );
406422 $text .= "\n<!-- \n$limitReport-->\n";
407423 }
Index: trunk/phase3/includes/MagicWord.php
@@ -102,6 +102,7 @@
103103 'pagesinnamespace',
104104 'numberofadmins',
105105 'defaultsort',
 106+ 'pagesincategory',
106107 );
107108
108109 /* Array of caching hints for ParserCache */
Index: trunk/phase3/includes/DefaultSettings.php
@@ -3011,3 +3011,9 @@
30123012 * Special:Whatlinkshere/RedirectDestination
30133013 */
30143014 $wgMaxRedirectLinksRetrieved = 500;
 3015+
 3016+/**
 3017+* Maximum number of calls to expensive parser functions
 3018+* such as PAGESINCATEGORY.
 3019+*/
 3020+$wgExpensiveParserFunctionLimit = 100;
Index: trunk/phase3/includes/CoreParserFunctions.php
@@ -41,6 +41,7 @@
4242 $parser->setFunctionHook( 'special', array( __CLASS__, 'special' ) );
4343 $parser->setFunctionHook( 'defaultsort', array( __CLASS__, 'defaultsort' ), SFH_NO_HASH );
4444 $parser->setFunctionHook( 'filepath', array( __CLASS__, 'filepath' ), SFH_NO_HASH );
 45+ $parser->setFunctionHook( 'pagesincategory', array( __CLASS__, 'pagesincategory' ), SFH_NO_HASH );
4546 $parser->setFunctionHook( 'tag', array( __CLASS__, 'tagObj' ), SFH_OBJECT_ARGS );
4647
4748 if ( $wgAllowDisplayTitle ) {
@@ -214,6 +215,23 @@
215216 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
216217 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
217218 }
 219+
 220+ static function pagesincategory( $parser, $category = '', $raw = null ) {
 221+ global $wgExpensiveParserFunctionLimit;
 222+ if ($category == '') {
 223+ return 0;
 224+ }
 225+ $parser->mExpensiveFunctionCount++;
 226+ if ($parser->mExpensiveFunctionCount <= $wgExpensiveParserFunctionLimit) {
 227+ $category = Category::newFromName($category);
 228+ $count = $category->getPageCount();
 229+ if ( !$count ) {
 230+ $count = 0;
 231+ }
 232+ return self::formatRaw( $count, $raw );
 233+ }
 234+ return 0;
 235+ }
218236
219237 static function language( $parser, $arg = '' ) {
220238 global $wgContLang;
Index: trunk/phase3/languages/messages/MessagesEn.php
@@ -338,6 +338,7 @@
339339 'filepath' => array( 0, 'FILEPATH:' ),
340340 'tag' => array( 0, 'tag' ),
341341 'hiddencat' => array( 1, '__HIDDENCAT__' ),
 342+ 'pagesincategory' => array( 1, 'PAGESINCATEGORY', 'PAGESINCAT' ),
342343 );
343344
344345 /**
@@ -1128,7 +1129,11 @@
11291130
11301131 You should consider whether it is appropriate to continue editing this page.
11311132 The deletion log for this page is provided here for convenience:",
 1133+'expensive-parserfunction-warning' => 'Warning: This page contains too many expensive parser function calls.
11321134
 1135+It should have less than $2, there are now $1.',
 1136+'expensive-parserfunction-category' => 'Pages with too many expensive parser function calls',
 1137+
11331138 # "Undo" feature
11341139 'undo-success' => 'The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.',
11351140 'undo-failure' => 'The edit could not be undone due to conflicting intermediate edits.',
Index: trunk/phase3/RELEASE-NOTES
@@ -65,7 +65,9 @@
6666 * Redesign of Special:Userrights
6767 * Make rev_deleted log entries more intelligible.
6868 * Logs are grouped under date headings similar to enhanced changes list
 69+* (6943) Added PAGESINCATEGORY: magic word
6970
 71+
7072 === Bug fixes in 1.13 ===
7173
7274 * (bug 10677) Add link to the file description page on the shared repository

Follow-up revisions

RevisionCommit summaryAuthorDate
r32965* (Bug 13657) Remove unused ParserFunctions messages (r32932)raymond16:00, 8 April 2008

Status & tagging log