r48563 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r48562‎ | r48563 | r48564 >
Date:02:05, 19 March 2009
Author:werdna
Status:ok
Tags:
Comment:
Load link vars from DB on edit. Includes some tracking of context, so it knows whether to check the DB or the parser.
Modified paths:
  • /trunk/extensions/AbuseFilter/AbuseFilter.class.php (modified) (history)
  • /trunk/extensions/AbuseFilter/AbuseFilterVariableHolder.php (modified) (history)

Diff [purge]

Index: trunk/extensions/AbuseFilter/AbuseFilter.class.php
@@ -97,8 +97,8 @@
9898 'article_restrictions_edit' => 'restrictions-edit',
9999 'article_restrictions_move' => 'restrictions-move',
100100 'article_recent_contributors' => 'recent-contributors',
101 - 'old_text' => 'old-text-stripped',
102 - 'old_html' => 'old-html',
 101+# 'old_text' => 'old-text-stripped', ## Disabled, performance
 102+# 'old_html' => 'old-html', ## Disabled, performance
103103 'old_links' => 'old-links',
104104 'minor_edit' => 'minor-edit',
105105 ),
@@ -342,6 +342,8 @@
343343 } catch (Exception $excep) {
344344 // Sigh.
345345 $result = false;
 346+
 347+ wfDebugLog( 'AbuseFilter', "AbuseFilter parser error: ".$excep->getMessage()."\n" );
346348
347349 if (!$ignoreError) {
348350 throw $excep;
@@ -506,6 +508,9 @@
507509
508510 // Add vars from extensions
509511 wfRunHooks( 'AbuseFilter-filterAction', array( &$vars, $title ) );
 512+
 513+ // Set context
 514+ $vars->setVar( 'context', 'filter' );
510515
511516 $dbr = wfGetDB( DB_SLAVE );
512517
@@ -1184,12 +1189,14 @@
11851190 public static function getVarsFromRCRow( $row ) {
11861191 if ($row->rc_this_oldid) {
11871192 // It's an edit.
1188 - return self::getEditVarsFromRCRow( $row );
 1193+ $vars = self::getEditVarsFromRCRow( $row );
11891194 } elseif ( $row->rc_log_type == 'move' ) {
1190 - return self::getMoveVarsFromRCRow( $row );
 1195+ $vars = self::getMoveVarsFromRCRow( $row );
11911196 } elseif ( $row->rc_log_type == 'newusers' ) {
1192 - return self::getCreateVarsFromRCRow( $row );
 1197+ $vars = self::getCreateVarsFromRCRow( $row );
11931198 }
 1199+ $vars->setVar( 'context', 'generated' );
 1200+ return $vars;
11941201 }
11951202
11961203 public static function getCreateVarsFromRCRow( $row ) {
@@ -1287,7 +1294,7 @@
12881295 'title' => $title->getText(),
12891296 'text-var' => 'new_wikitext'
12901297 ) );
1291 - $vars->setLazyLoadVar( 'old_links', 'links-from-wikitext-nonedit',
 1298+ $vars->setLazyLoadVar( 'old_links', 'links-from-wikitext-or-database',
12921299 array(
12931300 'namespace' => $title->getNamespace(),
12941301 'title' => $title->getText(),
Index: trunk/extensions/AbuseFilter/AbuseFilterVariableHolder.php
@@ -2,6 +2,7 @@
33
44 class AbuseFilterVariableHolder {
55 var $mVars = array();
 6+ static $varBlacklist = array( 'context' );
67
78 function setVar( $variable, $datum ) {
89 $variable = strtolower( $variable );
@@ -49,13 +50,20 @@
5051 $this->mVars = array_merge( $this->mVars, $addHolder->mVars );
5152 }
5253
 54+ function __wakeup() {
 55+ // Reset the context.
 56+ $this->setVar( 'context', 'stored' );
 57+ }
 58+
5359 function exportAllVars() {
5460 $allVarNames = array_keys( $this->mVars );
5561 $exported = array();
5662
5763 foreach( $allVarNames as $varName ) {
58 - $exported[$varName] = $this->getVar( $varName )->toString();
 64+ if (!in_array( $varName, self::$varBlacklist ) )
 65+ $exported[$varName] = $this->getVar( $varName )->toString();
5966 }
 67+
6068 return $exported;
6169 }
6270
@@ -121,6 +129,21 @@
122130 return self::$articleCache["$namespace:$title"];
123131 }
124132
 133+ static function getLinksFromDB( $article ) {
 134+ // Stolen from ConfirmEdit
 135+ $id = $article->getId();
 136+ if (!$id) return array();
 137+
 138+ $dbr = wfGetDB( DB_SLAVE );
 139+ $res = $dbr->select( 'externallinks', array( 'el_to' ),
 140+ array( 'el_from' => $id ), __METHOD__ );
 141+ $links = array();
 142+ while ( $row = $dbr->fetchObject( $res ) ) {
 143+ $links[] = $row->el_to;
 144+ }
 145+ return $links;
 146+ }
 147+
125148 function compute( $vars ) {
126149 $parameters = $this->mParameters;
127150 $result = null;
@@ -146,7 +169,8 @@
147170 $result = implode( "\n", $interest_lines );
148171 break;
149172 case 'links-from-wikitext':
150 - $article = self::articleFromTitle( $parameters['namespace'], $parameters['title'] );
 173+ $article = self::articleFromTitle( $parameters['namespace'],
 174+ $parameters['title'] );
151175 $textVar = $parameters['text-var'];
152176
153177 $new_text = $vars->getVar( $textVar )->toString();
@@ -154,13 +178,22 @@
155179 $links = array_keys( $editInfo->output->getExternalLinks() );
156180 $result = implode( "\n", $links );
157181 break;
158 - case 'links-from-wikitext-nonedit':
159 - $article = self::articleFromTitle( $parameters['namespace'], $parameters['title'] );
160 - $textVar = $parameters['text-var'];
 182+ case 'links-from-wikitext-or-database':
 183+ $article = self::articleFromTitle( $parameters['namespace'],
 184+ $parameters['title'] );
161185
162 - $wikitext = $vars->getVar( $textVar )->toString();
163 - $editInfo = $this->parseNonEditWikitext( $wikitext, $article );
164 - $links = array_keys( $editInfo->output->getExternalLinks() );
 186+ if ($vars->getVar( 'context' )->toString() == 'filter') {
 187+ $links = $this->getLinksFromDB( $article );
 188+ wfDebug( "AbuseFilter: loading old links from DB\n" );
 189+ } else {
 190+ wfDebug( "AbuseFilter: loading old links from Parser\n" );
 191+ $textVar = $parameters['text-var'];
 192+
 193+ $wikitext = $vars->getVar( $textVar )->toString();
 194+ $editInfo = $this->parseNonEditWikitext( $wikitext, $article );
 195+ $links = array_keys( $editInfo->output->getExternalLinks() );
 196+ }
 197+
165198 $result = implode( "\n", $links );
166199 break;
167200 case 'link-diff-added':

Status & tagging log