r111668 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111667‎ | r111668 | r111669 >
Date:19:51, 16 February 2012
Author:reedy
Status:ok (Comments)
Tags:
Comment:
Seeing as just including a maintenance script to be subclassed means the original one is just executed, move the class into it's own file, and require it back into it's old maintenance script home

Then have the subclassed maintenance script include the file without the maintenance wrappers itself
Modified paths:
  • /trunk/extensions/AntiSpoof/maintenance/BatchAntiSpoofClass.php (added) (history)
  • /trunk/extensions/AntiSpoof/maintenance/batchAntiSpoof.php (modified) (history)
  • /trunk/extensions/CentralAuth/AntiSpoof/batchCAAntiSpoof.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralAuth/AntiSpoof/batchCAAntiSpoof.php
@@ -5,7 +5,7 @@
66 if ( $IP === false ) {
77 $IP = dirname( __FILE__ ) . '/../../..';
88 }
9 -require_once( "$IP/extensions/AntiSpoof/maintenance/batchAntiSpoof.php" );
 9+require_once( "$IP/extensions/AntiSpoof/maintenance/BatcbAntiSpoofClass.php" );
1010
1111 class BatchCAAntiSpoof extends BatchAntiSpoof {
1212
Index: trunk/extensions/AntiSpoof/maintenance/BatchAntiSpoofClass.php
@@ -0,0 +1,74 @@
 2+<?php
 3+
 4+$IP = getenv( 'MW_INSTALL_PATH' );
 5+if ( $IP === false ) {
 6+ $IP = dirname( __FILE__ ) . '/../../..';
 7+}
 8+require_once( "$IP/maintenance/Maintenance.php" );
 9+
 10+/**
 11+ * Go through all usernames and calculate and record spoof thingies
 12+ */
 13+class BatchAntiSpoof extends Maintenance {
 14+
 15+ /**
 16+ * @param $items array
 17+ */
 18+ protected function batchRecord( $items ) {
 19+ SpoofUser::batchRecord( $items );
 20+ }
 21+
 22+ /**
 23+ * @return string
 24+ */
 25+ protected function getTableName() {
 26+ return 'user';
 27+ }
 28+
 29+ /**
 30+ * @return string
 31+ */
 32+ protected function getUserColumn() {
 33+ return 'user_name';
 34+ }
 35+
 36+ /**
 37+ * @param $name string
 38+ * @return SpoofUser
 39+ */
 40+ protected function makeSpoofUser( $name ) {
 41+ return new SpoofUser( $name );
 42+ }
 43+
 44+ /**
 45+ * Do the actual work. All child classes will need to implement this
 46+ */
 47+ public function execute() {
 48+ $dbw = $this->getDB( DB_MASTER );
 49+
 50+ $batchSize = 1000;
 51+
 52+ $this->output( "Creating username spoofs...\n" );
 53+ $userCol = $this->getUserColumn();
 54+ $result = $dbw->select( $this->getTableName(), $userCol, null, __FUNCTION__ );
 55+ $n = 0;
 56+ $items = array();
 57+ foreach( $result as $row ) {
 58+ if ( $n++ % $batchSize == 0 ) {
 59+ $this->output( "...$n\n" );
 60+ }
 61+
 62+ $items[] = $this->makeSpoofUser( $row->$userCol );
 63+
 64+ if ( $n % $batchSize == 0 ) {
 65+ $this->batchRecord( $items );
 66+ $items = array();
 67+ wfWaitForSlaves();
 68+ }
 69+ }
 70+
 71+ $this->batchRecord( $items );
 72+ $this->output( "$n user(s) done.\n" );
 73+ }
 74+}
 75+
Property changes on: trunk/extensions/AntiSpoof/maintenance/BatchAntiSpoofClass.php
___________________________________________________________________
Added: svn:eol-style
176 + native
Index: trunk/extensions/AntiSpoof/maintenance/batchAntiSpoof.php
@@ -1,75 +1,5 @@
22 <?php
3 -// Go through all usernames and calculate and record spoof thingies
 3+require_once( "BatchAntiSpoofClass.php" );
44
5 -$IP = getenv( 'MW_INSTALL_PATH' );
6 -if ( $IP === false ) {
7 - $IP = dirname( __FILE__ ) . '/../../..';
8 -}
9 -require_once( "$IP/maintenance/Maintenance.php" );
10 -
11 -class BatchAntiSpoof extends Maintenance {
12 -
13 - /**
14 - * @param $items array
15 - */
16 - protected function batchRecord( $items ) {
17 - SpoofUser::batchRecord( $items );
18 - }
19 -
20 - /**
21 - * @return string
22 - */
23 - protected function getTableName() {
24 - return 'user';
25 - }
26 -
27 - /**
28 - * @return string
29 - */
30 - protected function getUserColumn() {
31 - return 'user_name';
32 - }
33 -
34 - /**
35 - * @param $name string
36 - * @return SpoofUser
37 - */
38 - protected function makeSpoofUser( $name ) {
39 - return new SpoofUser( $name );
40 - }
41 -
42 - /**
43 - * Do the actual work. All child classes will need to implement this
44 - */
45 - public function execute() {
46 - $dbw = $this->getDB( DB_MASTER );
47 -
48 - // $dbw->bufferResults( false );
49 -
50 - $batchSize = 1000;
51 -
52 - $this->output( "Creating username spoofs...\n" );
53 - $userCol = $this->getUserColumn();
54 - $result = $dbw->select( $this->getTableName(), $userCol, null, __FUNCTION__ );
55 - $n = 0;
56 - $items = array();
57 - foreach( $result as $row ) {
58 - if ( $n++ % $batchSize == 0 ) {
59 - $this->output( "...$n\n" );
60 - }
61 -
62 - $items[] = $this->makeSpoofUser( $row->$userCol );
63 -
64 - if ( $n % $batchSize == 0 ) {
65 - $this->batchRecord( $items );
66 - $items = array();
67 - }
68 - }
69 -
70 - $this->batchRecord( $items );
71 - $this->output( "$n user(s) done.\n" );
72 - }
73 -}
74 -
755 $maintClass = "BatchAntiSpoof";
766 require_once( DO_MAINTENANCE );

Follow-up revisions

RevisionCommit summaryAuthorDate
r111669MFT r111668reedy19:53, 16 February 2012
r111670Batcb to Batch from r111668reedy19:59, 16 February 2012

Comments

#Comment by Nikerabbit (talk | contribs)   06:41, 17 February 2012

You know there is

require_once( RUN_MAINTENANCE_IF_MAIN );

?

Status & tagging log