r52505 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52504‎ | r52505 | r52506 >
Date:12:20, 28 June 2009
Author:demon
Status:deferred
Tags:
Comment:
* Use generic batch size in a few places
* __FUNCTION__ -> __METHOD__ in a few places
* Port updateRestrictions
* Remove useless param validation in changePassword.
Modified paths:
  • /branches/maintenance-work/maintenance/Maintenance.php (modified) (history)
  • /branches/maintenance-work/maintenance/changePassword.php (modified) (history)
  • /branches/maintenance-work/maintenance/checkImages.php (modified) (history)
  • /branches/maintenance-work/maintenance/populateParentId.php (modified) (history)
  • /branches/maintenance-work/maintenance/rebuildFileCache.php (modified) (history)
  • /branches/maintenance-work/maintenance/updateRestrictions.php (modified) (history)

Diff [purge]

Index: branches/maintenance-work/maintenance/rebuildFileCache.php
@@ -13,6 +13,7 @@
1414 parent::__construct();
1515 $this->mDescription = "Build file cache for content pages";
1616 $this->addArgs( array( 'start', 'overwrite' ) );
 17+ $this->setBatchSize( 100 );
1718 }
1819
1920 public function execute() {
@@ -24,22 +25,21 @@
2526 $start = intval( $this->getArg( 0, 0 ) );
2627 $overwrite = $this->hasArg(1) && $this->getArg(1) === 'overwrite';
2728 $this->output( "Building content page file cache from page {$start}!\n" );
28 -
 29+
2930 $dbr = wfGetDB( DB_SLAVE );
3031 $start = $start > 0 ? $start : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
3132 $end = $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
3233 if( !$start ) {
3334 $this->error( "Nothing to do.\n", true );
3435 }
35 -
 36+
3637 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
3738 OutputPage::setEncodings(); # Not really used yet
38 -
39 - $BATCH_SIZE = 100;
 39+
4040 # Do remaining chunk
41 - $end += $BATCH_SIZE - 1;
 41+ $end += $this->mBatchSize - 1;
4242 $blockStart = $start;
43 - $blockEnd = $start + $BATCH_SIZE - 1;
 43+ $blockEnd = $start + $this->mBatchSize - 1;
4444
4545 $dbw = wfGetDB( DB_MASTER );
4646 // Go through each page and save the output
@@ -85,8 +85,8 @@
8686 }
8787 $dbw->commit(); // commit any changes
8888 }
89 - $blockStart += $BATCH_SIZE;
90 - $blockEnd += $BATCH_SIZE;
 89+ $blockStart += $this->mBatchSize;
 90+ $blockEnd += $this->mBatchSize;
9191 wfWaitForSlaves( 5 );
9292 }
9393 $this->output( "Done!\n" );
Index: branches/maintenance-work/maintenance/changePassword.php
@@ -21,9 +21,6 @@
2222 }
2323
2424 public function execute() {
25 - if( !$this->hasOption('user') || !$this->hasOption('password') ) {
26 - $this->error( "Username or password not provided, halting.", true );
27 - }
2825 $user = User::newFromName( $this->getOption('user') );
2926 if( !$user->getId() ) {
3027 $this->error( "No such user: " . $this->getOption('user') . "\n", true );
Index: branches/maintenance-work/maintenance/checkImages.php
@@ -9,10 +9,10 @@
1010 public function __construct() {
1111 parent::__construct();
1212 $this->mDescription = "Check images to see if they exist, are readable, etc";
 13+ $this->setBatchSize( 1000 );
1314 }
1415
1516 public function execute() {
16 - $batchSize = 1000;
1717 $start = '';
1818 $dbr = wfGetDB( DB_SLAVE );
1919
@@ -21,7 +21,7 @@
2222
2323 do {
2424 $res = $dbr->select( 'image', '*', array( 'img_name > ' . $dbr->addQuotes( $start ) ),
25 - __METHOD__, array( 'LIMIT' => $batchSize ) );
 25+ __METHOD__, array( 'LIMIT' => $this->mBatchSize ) );
2626 foreach ( $res as $row ) {
2727 $numImages++;
2828 $start = $row->img_name;
Index: branches/maintenance-work/maintenance/populateParentId.php
@@ -8,13 +8,10 @@
99 require_once( "Maintenance.php" );
1010
1111 class PopulateParentId extends Maintenance {
12 -
13 - // Batch size
14 - const BATCH_SIZE = 200;
15 -
1612 public function __construct() {
1713 parent::__construct();
1814 $this->mDescription = "Populates rev_parent_id";
 15+ $this->setBatchSize( 200 );
1916 }
2017
2118 public function execute() {
@@ -29,14 +26,14 @@
3027 $this->output( "...revision table seems to be empty.\n" );
3128 $db->insert( 'updatelog',
3229 array( 'ul_key' => 'populate rev_parent_id' ),
33 - __FUNCTION__,
 30+ __METHOD__,
3431 'IGNORE' );
3532 return;
3633 }
3734 # Do remaining chunk
38 - $end += self::BATCH_SIZE - 1;
 35+ $end += $this->mBatchSize - 1;
3936 $blockStart = intval( $start );
40 - $blockEnd = intval( $start ) + self::BATCH_SIZE - 1;
 37+ $blockEnd = intval( $start ) + $this->mBatchSize - 1;
4138 $count = 0;
4239 $changed = 0;
4340 while( $blockEnd <= $end ) {
@@ -44,7 +41,7 @@
4542 $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
4643 $res = $db->select( 'revision',
4744 array('rev_id','rev_page','rev_timestamp','rev_parent_id'),
48 - $cond, __FUNCTION__ );
 45+ $cond, __METHOD__ );
4946 # Go through and update rev_parent_id from these rows.
5047 # Assume that the previous revision of the title was
5148 # the original previous revision of the title when the
@@ -56,20 +53,20 @@
5754 $previousID = $db->selectField( 'revision', 'rev_id',
5855 array( 'rev_page' => $row->rev_page, 'rev_timestamp' => $row->rev_timestamp,
5956 "rev_id < " . intval( $row->rev_id ) ),
60 - __FUNCTION__,
 57+ __METHOD__,
6158 array( 'ORDER BY' => 'rev_id DESC' ) );
6259 # If there are none, check the the highest ID with a lower timestamp
6360 if( !$previousID ) {
6461 # Get the highest older timestamp
6562 $lastTimestamp = $db->selectField( 'revision', 'rev_timestamp',
6663 array( 'rev_page' => $row->rev_page, "rev_timestamp < " . $db->addQuotes( $row->rev_timestamp ) ),
67 - __FUNCTION__,
 64+ __METHOD__,
6865 array( 'ORDER BY' => 'rev_timestamp DESC' ) );
6966 # If there is one, let the highest rev ID win
7067 if( $lastTimestamp ) {
7168 $previousID = $db->selectField( 'revision', 'rev_id',
7269 array( 'rev_page' => $row->rev_page, 'rev_timestamp' => $lastTimestamp ),
73 - __FUNCTION__,
 70+ __METHOD__,
7471 array( 'ORDER BY' => 'rev_id DESC' ) );
7572 }
7673 }
@@ -80,16 +77,16 @@
8178 $db->update( 'revision',
8279 array( 'rev_parent_id' => $previousID ),
8380 array( 'rev_id' => $row->rev_id ),
84 - __FUNCTION__ );
 81+ __METHOD__ );
8582 $count++;
8683 }
87 - $blockStart += self::BATCH_SIZE - 1;
88 - $blockEnd += self::BATCH_SIZE - 1;
 84+ $blockStart += $this->mBatchSize - 1;
 85+ $blockEnd += $this->mBatchSize - 1;
8986 wfWaitForSlaves( 5 );
9087 }
9188 $logged = $db->insert( 'updatelog',
9289 array( 'ul_key' => 'populate rev_parent_id' ),
93 - __FUNCTION__,
 90+ __METHOD__,
9491 'IGNORE' );
9592 if( $logged ) {
9693 $this->output( "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n" );
Index: branches/maintenance-work/maintenance/Maintenance.php
@@ -313,6 +313,7 @@
314314
315315 $this->loadParamsAndArgs();
316316 $this->maybeHelp();
 317+ $this->validateParamsAndArgs();
317318 }
318319
319320 /**
@@ -410,22 +411,27 @@
411412 }
412413 }
413414
 415+ $this->mOptions = $options;
 416+ $this->mArgs = $args;
 417+ $this->loadSpecialVars();
 418+ $this->inputLoaded = true;
 419+ }
 420+
 421+ /**
 422+ * Run some validation checks on the params, etc
 423+ */
 424+ private function validateParamsAndArgs() {
414425 # Check to make sure we've got all the required ones
415426 foreach( $this->mParams as $opt => $info ) {
416427 if( $info['require'] && !$this->hasOption($opt) ) {
417428 $this->error( "Param $opt required.\n", true );
418429 }
419430 }
420 -
 431+
421432 # Also make sure we've got enough arguments
422 - if ( count( $args ) < count( $this->mArgList ) ) {
 433+ if ( count( $this->mArgs ) < count( $this->mArgList ) ) {
423434 $this->error( "Not enough arguments passed", true );
424435 }
425 -
426 - $this->mOptions = $options;
427 - $this->mArgs = $args;
428 - $this->loadSpecialVars();
429 - $this->inputLoaded = true;
430436 }
431437
432438 /**
Index: branches/maintenance-work/maintenance/updateRestrictions.php
@@ -9,84 +9,88 @@
1010 * @ingroup Maintenance
1111 */
1212
13 -define( 'BATCH_SIZE', 100 );
 13+require_once( "Maintenance.php" );
1414
15 -require_once 'commandLine.inc';
16 -
17 -$db =& wfGetDB( DB_MASTER );
18 -if ( !$db->tableExists( 'page_restrictions' ) ) {
19 - echo "page_restrictions does not exist\n";
20 - exit( 1 );
21 -}
 15+class UpdateRestrictions extends Maintenance {
 16+ public function __construct() {
 17+ parent::__construct();
 18+ $this->mDescription = "";
 19+ $this->setBatchSize( 100 );
 20+ }
2221
23 -migrate_page_restrictions( $db );
 22+ private function execute() {
 23+ $db = wfGetDB( DB_MASTER );
 24+ if( !$db->tableExists( 'page_restrictions' ) ) {
 25+ $this->error( "page_restrictions table does not exist\n", true );
 26+ }
2427
25 -function migrate_page_restrictions( $db ) {
26 - $start = $db->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
27 - if( !$start ) {
28 - die("Nothing to do.\n");
29 - }
30 - $end = $db->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
 28+ $start = $db->selectField( 'page', 'MIN(page_id)', false, __METHOD__ );
 29+ if( !$start ) {
 30+ $this->error( "Nothing to do.\n", true );
 31+ }
 32+ $end = $db->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
3133
32 - # Do remaining chunk
33 - $end += BATCH_SIZE - 1;
34 - $blockStart = $start;
35 - $blockEnd = $start + BATCH_SIZE - 1;
36 - $encodedExpiry = 'infinity';
37 - while( $blockEnd <= $end ) {
38 - echo "...doing page_id from $blockStart to $blockEnd\n";
39 - $cond = "page_id BETWEEN $blockStart AND $blockEnd AND page_restrictions !=''";
40 - $res = $db->select( 'page', array('page_id','page_namespace','page_restrictions'), $cond, __FUNCTION__ );
41 - $batch = array();
42 - while( $row = $db->fetchObject( $res ) ) {
43 - $oldRestrictions = array();
44 - foreach( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
45 - $temp = explode( '=', trim( $restrict ) );
46 - // Make sure we are not settings restrictions to ""
47 - if( count($temp) == 1 && $temp[0] ) {
48 - // old old format should be treated as edit/move restriction
49 - $oldRestrictions["edit"] = trim( $temp[0] );
50 - $oldRestrictions["move"] = trim( $temp[0] );
51 - } else if( $temp[1] ) {
52 - $oldRestrictions[$temp[0]] = trim( $temp[1] );
 34+ # Do remaining chunk
 35+ $end += $this->mBatchSize - 1;
 36+ $blockStart = $start;
 37+ $blockEnd = $start + $this->mBatchSize - 1;
 38+ $encodedExpiry = 'infinity';
 39+ while( $blockEnd <= $end ) {
 40+ $this->output( "...doing page_id from $blockStart to $blockEnd\n" );
 41+ $cond = "page_id BETWEEN $blockStart AND $blockEnd AND page_restrictions !=''";
 42+ $res = $db->select( 'page', array('page_id','page_namespace','page_restrictions'), $cond, __METHOD__ );
 43+ $batch = array();
 44+ while( $row = $db->fetchObject( $res ) ) {
 45+ $oldRestrictions = array();
 46+ foreach( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
 47+ $temp = explode( '=', trim( $restrict ) );
 48+ // Make sure we are not settings restrictions to ""
 49+ if( count($temp) == 1 && $temp[0] ) {
 50+ // old old format should be treated as edit/move restriction
 51+ $oldRestrictions["edit"] = trim( $temp[0] );
 52+ $oldRestrictions["move"] = trim( $temp[0] );
 53+ } else if( $temp[1] ) {
 54+ $oldRestrictions[$temp[0]] = trim( $temp[1] );
 55+ }
5356 }
 57+ # Clear invalid columns
 58+ if( $row->page_namespace == NS_MEDIAWIKI ) {
 59+ $db->update( 'page', array( 'page_restrictions' => '' ),
 60+ array( 'page_id' => $row->page_id ), __FUNCTION__ );
 61+ $this->output( "...removed dead page_restrictions column for page {$row->page_id}\n" );
 62+ }
 63+ # Update restrictions table
 64+ foreach( $oldRestrictions as $action => $restrictions ) {
 65+ $batch[] = array(
 66+ 'pr_page' => $row->page_id,
 67+ 'pr_type' => $action,
 68+ 'pr_level' => $restrictions,
 69+ 'pr_cascade' => 0,
 70+ 'pr_expiry' => $encodedExpiry
 71+ );
 72+ }
5473 }
55 - # Clear invalid columns
56 - if( $row->page_namespace == NS_MEDIAWIKI ) {
57 - $db->update( 'page', array( 'page_restrictions' => '' ),
58 - array( 'page_id' => $row->page_id ), __FUNCTION__ );
59 - echo "...removed dead page_restrictions column for page {$row->page_id}\n";
 74+ # We use insert() and not replace() as Article.php replaces
 75+ # page_restrictions with '' when protected in the restrictions table
 76+ if ( count( $batch ) ) {
 77+ $ok = $db->deadlockLoop( array( $db, 'insert' ), 'page_restrictions',
 78+ $batch, __FUNCTION__, array( 'IGNORE' ) );
 79+ if( !$ok ) {
 80+ throw new MWException( "Deadlock loop failed wtf :(" );
 81+ }
6082 }
61 - # Update restrictions table
62 - foreach( $oldRestrictions as $action => $restrictions ) {
63 - $batch[] = array(
64 - 'pr_page' => $row->page_id,
65 - 'pr_type' => $action,
66 - 'pr_level' => $restrictions,
67 - 'pr_cascade' => 0,
68 - 'pr_expiry' => $encodedExpiry
69 - );
70 - }
 83+ $blockStart += $this->mBatchSize - 1;
 84+ $blockEnd += $this->mBatchSize - 1;
 85+ wfWaitForSlaves( 5 );
7186 }
72 - # We use insert() and not replace() as Article.php replaces
73 - # page_restrictions with '' when protected in the restrictions table
74 - if ( count( $batch ) ) {
75 - $ok = $db->deadlockLoop( array( $db, 'insert' ), 'page_restrictions',
76 - $batch, __FUNCTION__, array( 'IGNORE' ) );
77 - if( !$ok ) {
78 - throw new MWException( "Deadlock loop failed wtf :(" );
79 - }
80 - }
81 - $blockStart += BATCH_SIZE - 1;
82 - $blockEnd += BATCH_SIZE - 1;
83 - wfWaitForSlaves( 5 );
 87+ $this->output( "...removing dead rows from page_restrictions\n" );
 88+ // Kill any broken rows from previous imports
 89+ $db->delete( 'page_restrictions', array( 'pr_level' => '' ) );
 90+ // Kill other invalid rows
 91+ $db->deleteJoin( 'page_restrictions', 'page', 'pr_page', 'page_id', array('page_namespace' => NS_MEDIAWIKI) );
 92+ $this->output( "...Done!\n" );
8493 }
85 - echo "...removing dead rows from page_restrictions\n";
86 - // Kill any broken rows from previous imports
87 - $db->delete( 'page_restrictions', array( 'pr_level' => '' ) );
88 - // Kill other invalid rows
89 - $db->deleteJoin( 'page_restrictions', 'page', 'pr_page', 'page_id', array('page_namespace' => NS_MEDIAWIKI) );
90 - echo "...Done!\n";
9194 }
9295
93 -
 96+$maintClass = "UpdateRestrictions";
 97+require_once( DO_MAINTENANCE );

Status & tagging log