r106813 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106812‎ | r106813 | r106814 >
Date:17:04, 20 December 2011
Author:reedy
Status:ok (Comments)
Tags:
Comment:
Refactored batchAntiSpoof.php to subclass Maintenance

For part of bug 28747
Modified paths:
  • /trunk/extensions/AntiSpoof/AntiSpoof.php (modified) (history)
  • /trunk/extensions/AntiSpoof/batchAntiSpoof.php (modified) (history)

Diff [purge]

Index: trunk/extensions/AntiSpoof/AntiSpoof.php
@@ -36,6 +36,7 @@
3737 $wgAutoloadClasses['AntiSpoof'] = "$dir/AntiSpoof_body.php";
3838 $wgAutoloadClasses['AntiSpoofHooks'] = "$dir/AntiSpoofHooks.php";
3939 $wgAutoloadClasses['SpoofUser'] = "$dir/SpoofUser.php";
 40+$wgAutoloadClasses['BatchAntiSpoof'] = "$dir/batchAntiSpoof.php";
4041
4142 $wgHooks['LoadExtensionSchemaUpdates'][] = 'AntiSpoofHooks::asUpdateSchema';
4243 $wgHooks['AbortNewAccount'][] = 'AntiSpoofHooks::asAbortNewAccountHook';
Index: trunk/extensions/AntiSpoof/batchAntiSpoof.php
@@ -1,31 +1,51 @@
22 <?php
33 // Go through all usernames and calculate and record spoof thingies
44
5 -require_once ( getenv( 'MW_INSTALL_PATH' ) !== false
6 - ? getenv( 'MW_INSTALL_PATH' ) . "/maintenance/commandLine.inc"
7 - : dirname( __FILE__ ) . '/../../maintenance/commandLine.inc' );
 5+$IP = getenv( 'MW_INSTALL_PATH' );
 6+if ( $IP === false ) {
 7+ $IP = dirname( __FILE__ ) . '/../..';
 8+}
 9+require( "$IP/maintenance/Maintenance.php" );
810
9 -$dbw = wfGetDB( DB_MASTER );
 11+class BatchAntiSpoof extends Maintenance {
1012
11 -$dbw->bufferResults( false );
 13+ /**
 14+ * @param $items array
 15+ */
 16+ private function batchRecord( $items ) {
 17+ SpoofUser::batchRecord( $items );
 18+ }
1219
13 -$batchSize = 1000;
 20+ /**
 21+ * Do the actual work. All child classes will need to implement this
 22+ */
 23+ public function execute() {
 24+ $dbw = $this->getDB( DB_MASTER );
1425
15 -$result = $dbw->select( 'user', 'user_name', null, 'batchAntiSpoof.php' );
16 -$n = 0;
17 -foreach( $result as $row ) {
18 - if ( $n++ % $batchSize == 0 ) {
19 - echo "$wgDBname $n\n";
20 - }
 26+ $dbw->bufferResults( false );
2127
22 - $items[] = new SpoofUser( $row->user_name );
 28+ $batchSize = 1000;
2329
24 - if ( $n % $batchSize == 0 ) {
25 - SpoofUser::batchRecord( $items );
 30+ $result = $dbw->select( 'user', 'user_name', null, __FUNCTION__ );
 31+ $n = 0;
2632 $items = array();
 33+ foreach( $result as $row ) {
 34+ if ( $n++ % $batchSize == 0 ) {
 35+ $this->output( "...$n\n" );
 36+ }
 37+
 38+ $items[] = new SpoofUser( $row->user_name );
 39+
 40+ if ( $n % $batchSize == 0 ) {
 41+ $this->batchRecord( $items );
 42+ $items = array();
 43+ }
 44+ }
 45+
 46+ $this->batchRecord( $items );
 47+ $this->output( "$n user(s) done.\n" );
2748 }
2849 }
2950
30 -SpoofUser::batchRecord( $items );
31 -echo "$wgDBname $n done.\n";
32 -$dbw->freeResult( $result );
 51+$maintClass = "BatchAntiSpoof";
 52+require_once( DO_MAINTENANCE );

Follow-up revisions

RevisionCommit summaryAuthorDate
r106816Followup r106813, make batchRecord protected not private...reedy17:14, 20 December 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r106809Followup r106808...reedy16:29, 20 December 2011

Comments

#Comment by Catrope (talk | contribs)   20:25, 21 December 2011
+		$this->batchRecord( $items );

Shouldn't this be in the if statement where you're clearing $items? Otherwise only the last batch will be recorded.

#Comment by Reedy (talk | contribs)   00:28, 22 December 2011

It is?

+			if ( $n % $batchSize == 0 ) {
+				$this->batchRecord( $items );
+				$items = array();
+			}
<?php
// Go through all usernames and calculate and record spoof thingies

$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
	$IP = dirname( __FILE__ ) . '/../..';
}
require( "$IP/maintenance/Maintenance.php" );

class BatchAntiSpoof extends Maintenance {

	/**
	 * @param $items array
	 */
	private function batchRecord( $items ) {
		SpoofUser::batchRecord( $items );
	}

	/**
	 * Do the actual work. All child classes will need to implement this
	 */
	public function execute() {
		$dbw = $this->getDB( DB_MASTER );

		$dbw->bufferResults( false );

		$batchSize = 1000;

		$result = $dbw->select( 'user', 'user_name', null, __FUNCTION__ );
		$n = 0;
		$items = array();
		foreach( $result as $row ) {
			if ( $n++ % $batchSize == 0 ) {
				$this->output( "...$n\n" );
			}

			$items[] = new SpoofUser( $row->user_name );

			if ( $n % $batchSize == 0 ) {
				$this->batchRecord( $items );
				$items = array();
			}
		}

		$this->batchRecord( $items );
		$this->output( "$n user(s) done.\n" );
	}
}

$maintClass = "BatchAntiSpoof";
require_once( DO_MAINTENANCE );
#Comment by Catrope (talk | contribs)   16:28, 9 January 2012
+		$result = $dbw->select( 'user', 'user_name', null, __FUNCTION__ );

Shouldn't you use __METHOD__ ?

Fine otherwise, marking OK.

Status & tagging log