Mailing List Archive

SVN: [28337] trunk/extensions/ParserFunctions
Revision: 28337
Author: tstarling
Date: 2007-12-10 06:04:34 +0000 (Mon, 10 Dec 2007)

Log Message:
-----------
* Add a warning on preview if the #ifexist limit is exceeded
* Add all pages which exceed the #ifexist limit to [[Category:Pages with too many ifexist calls]]

Modified Paths:
--------------
trunk/extensions/ParserFunctions/ParserFunctions.i18n.php
trunk/extensions/ParserFunctions/ParserFunctions.php

Modified: trunk/extensions/ParserFunctions/ParserFunctions.i18n.php
===================================================================
--- trunk/extensions/ParserFunctions/ParserFunctions.i18n.php 2007-12-10 06:02:29 UTC (rev 28336)
+++ trunk/extensions/ParserFunctions/ParserFunctions.i18n.php 2007-12-10 06:04:34 UTC (rev 28337)
@@ -106,6 +106,8 @@
'pfunc_expr_division_by_zero' => 'Division by zero',
'pfunc_expr_unknown_error' => 'Expression error: Unknown error ($1)',
'pfunc_expr_not_a_number' => 'In $1: result is not a number',
+ 'pfunc_ifexist_warning' => 'Warning: This article contains too many #ifexist calls. It should have less than $2, there are now $1.',
+ 'pfunc_max_ifexist_category' => 'Pages with too many ifexist calls',
),

'ar' => array(

Modified: trunk/extensions/ParserFunctions/ParserFunctions.php
===================================================================
--- trunk/extensions/ParserFunctions/ParserFunctions.php 2007-12-10 06:02:29 UTC (rev 28336)
+++ trunk/extensions/ParserFunctions/ParserFunctions.php 2007-12-10 06:04:34 UTC (rev 28337)
@@ -26,6 +26,7 @@
function clearState(&$parser) {
$this->mTimeChars = 0;
$parser->pf_ifexist_count = 0;
+ $parser->pf_ifexist_breakdown = array();
return true;
}

@@ -255,43 +256,58 @@
return implode( '/' , $newExploded );
}

- function ifexist( &$parser, $title = '', $then = '', $else = '' ) {
+ function incrementIfexistCount( $parser, $frame ) {
// Don't let this be called more than a certain number of times. It tends to make the database explode.
global $wgMaxIfExistCount;
+ $parser->pf_ifexist_count++;
+ if ( $frame ) {
+ $pdbk = $frame->getPDBK( 1 );
+ if ( !isset( $parser->pf_ifexist_breakdown[$pdbk] ) ) {
+ $parser->pf_ifexist_breakdown[$pdbk] = 0;
+ }
+ $parser->pf_ifexist_breakdown[$pdbk] ++;
+ }
+ return $parser->pf_ifexist_count <= $wgMaxIfExistCount;
+ }

+ function ifexist( &$parser, $title = '', $then = '', $else = '' ) {
+ return $this->ifexistCommon( $parser, false, $title, $then, $else );
+ }
+
+ function ifexistCommon( &$parser, $frame, $title = '', $then = '', $else = '' ) {
$title = Title::newFromText( $title );
if ( $title ) {
/* If namespace is specified as NS_MEDIA, then we want to check the physical file,
* not the "description" page.
*/
if( $title->getNamespace() == NS_MEDIA ) {
- $parser->pf_ifexist_count++;
- if ($parser->pf_ifexist_count > $wgMaxIfExistCount) {
+ if ( !$this->incrementIfexistCount( $parser, $frame ) ) {
return $else;
}
$file = wfFindFile($title);
- if(!$file)
- return $else;
+ if ( !$file ) {
+ return $else;
+ }
$parser->mOutput->addImage($file->getName());
return $file->exists() ? $then : $else;
+ } else {
+ $pdbk = $title->getPrefixedDBkey();
+ $lc = LinkCache::singleton();
+ if ( $lc->getGoodLinkID( $pdbk ) ) {
+ return $then;
+ } elseif ( $lc->isBadLink( $pdbk ) ) {
+ return $else;
+ }
+ if ( !$this->incrementIfexistCount( $parser, $frame ) ) {
+ return $else;
+ }

+ $id = $title->getArticleID();
+ $parser->mOutput->addLink( $title, $id );
+ if ( $id ) {
+ return $then;
+ }
}
- $pdbk = $title->getPrefixedDBkey();
- $lc = LinkCache::singleton();
- if ( $lc->getGoodLinkID( $pdbk ) ) {
- return $then;
- } elseif ( $lc->isBadLink( $pdbk ) ) {
- return $else;
- }
- $parser->pf_ifexist_count++;
- if ($parser->pf_ifexist_count > $wgMaxIfExistCount) {
- return $else;
- }
- $id = $title->getArticleID();
- $parser->mOutput->addLink( $title, $id );
- if ( $id ) {
- return $then;
- }
}
return $else;
}
@@ -301,7 +317,7 @@
$then = isset( $args[1] ) ? $args[1] : null;
$else = isset( $args[2] ) ? $args[2] : null;

- $result = $this->ifexist( $parser, $title, $then, $else );
+ $result = $this->ifexistCommon( $parser, $frame, $title, $then, $else );
if ( $result === null ) {
return '';
} else {
@@ -393,8 +409,20 @@
} else {
return $title;
}
- }
-
+ }
+
+ function afterTidy( &$parser, &$text ) {
+ global $wgMaxIfexistCount;
+ if ( $parser->pf_ifexist_count > $wgMaxIfexistCount ) {
+ if ( is_callable( array( $parser->mOutput, 'addWarning' ) ) ) {
+ $warning = wfMsg( 'pfunc_ifexist_warning', $parser->pf_ifexist_count, $wgMaxIfexistCount );
+ $parser->mOutput->addWarning( $warning );
+ $cat = Title::makeTitleSafe( NS_CATEGORY, wfMsg( 'pfunc_max_ifexist_category' ) );
+ $parser->mOutput->addCategory( $cat->getDBkey(), $parser->getDefaultSort() );
+ }
+ }
+ return true;
+ }
}

function wfSetupParserFunctions() {
@@ -432,6 +460,7 @@
$wgMessageCache->addMessages( $messages, $lang );

$wgHooks['ParserClearState'][] = array( &$wgExtParserFunctions, 'clearState' );
+ $wgHooks['ParserAfterTidy'][] = array( &$wgExtParserFunctions, 'afterTidy' );
}

function wfParserFunctionsLanguageGetMagic( &$magicWords, $langCode ) {



_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
http://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs