r33549 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r33548‎ | r33549 | r33550 >
Date:14:34, 18 April 2008
Author:simetrical
Status:old
Tags:
Comment:
* Add Parser::incrementExpensiveFunctionCount() and use it in CoreParserFunctions::getcategorycount(); refactor slightly so that formatRaw is called even for 0 (do any languages format that differently? maybe if they use non-Arabic numerals?). From bug 12698, by CBM.
* Add a cache so that calling getcategorycount for the same category many times won't stupidly repeat the same query over and over. Currently an early return avoids incrementing the expensive function count in this case, so technically this makes things *slower*, not faster, but that can be tweaked if anyone cares.
Modified paths:
  • /trunk/phase3/includes/CoreParserFunctions.php (modified) (history)
  • /trunk/phase3/includes/Parser.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Parser.php
@@ -3285,6 +3285,20 @@
32863286 }
32873287
32883288 /**
 3289+ * Increment the expensive function count
 3290+ *
 3291+ * @return boolean False if the limit has been exceeded
 3292+ */
 3293+ function incrementExpensiveFunctionCount() {
 3294+ global $wgExpensiveParserFunctionLimit;
 3295+ $this->mExpensiveFunctionCount++;
 3296+ if($this->mExpensiveFunctionCount <= $wgExpensiveParserFunctionLimit) {
 3297+ return true;
 3298+ }
 3299+ return false;
 3300+ }
 3301+
 3302+ /**
32893303 * Strip double-underscore items like __NOGALLERY__ and __NOTOC__
32903304 * Fills $this->mDoubleUnderscores, returns the modified text
32913305 */
Index: trunk/phase3/includes/CoreParserFunctions.php
@@ -220,17 +220,22 @@
221221 * tent. This is an expensive parser function and can't be called too many
222222 * times per page.
223223 */
224 - static function pagesincategory( $parser, $category = '', $raw = null ) {
225 - global $wgExpensiveParserFunctionLimit;
226 - $category = Category::newFromName($category);
227 - if( !is_object( $category ) ) {
228 - return 0;
 224+ static function pagesincategory( $parser, $name = '', $raw = null ) {
 225+ static $cache = array();
 226+ $category = Category::newFromName( $name );
 227+
 228+ # Normalize name for cache
 229+ $name = $category->getName();
 230+
 231+ if( isset( $cache[$name] ) ) {
 232+ return self::formatRaw( $cache[$name], $raw );
229233 }
230 - $parser->mExpensiveFunctionCount++;
231 - if ($parser->mExpensiveFunctionCount <= $wgExpensiveParserFunctionLimit) {
232 - return self::formatRaw( (int)$category->getPageCount(), $raw );
 234+
 235+ $count = 0;
 236+ if( is_object( $category ) && $parser->incrementExpensiveFunctionCount() ) {
 237+ $count = $cache[$name] = (int)$category->getPageCount();
233238 }
234 - return 0;
 239+ return self::formatRaw( $count, $raw );
235240 }
236241
237242 static function language( $parser, $arg = '' ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r33551(bug 12698) Create PAGESIZE parser function, to return the size of a page. Q...simetrical15:01, 18 April 2008

Status & tagging log