Revision: 51797 Author: demon Date: 2009-06-12 19:05:55 +0000 (Fri, 12 Jun 2009)
Log Message: ----------- * Tweak getOption() to set the value in $mOptions so we don't have to re-provide the default again and again * Port nextJobDB, renameDbPrefix. Modified Paths: -------------- branches/maintenance-work/maintenance/Maintenance.php branches/maintenance-work/maintenance/nextJobDB.php branches/maintenance-work/maintenance/renameDbPrefix.php Modified: branches/maintenance-work/maintenance/Maintenance.php =================================================================== --- branches/maintenance-work/maintenance/Maintenance.php 2009-06-12 18:31:07 UTC (rev 51796) +++ branches/maintenance-work/maintenance/Maintenance.php 2009-06-12 19:05:55 UTC (rev 51797) @@ -78,7 +78,13 @@ * @return mixed */ protected function getOption( $name, $default = null ) { - return $this->hasOption($name) ? $this->mOptions[$name] : $default; + if( $this->hasOption($name) ) { + return $this->mOptions[$name]; + } else { + // Set it so we don't have to provide the default again + $this->mOptions[$name] = $default; + return $this->mOptions[$name]; + } } /** Modified: branches/maintenance-work/maintenance/nextJobDB.php =================================================================== --- branches/maintenance-work/maintenance/nextJobDB.php 2009-06-12 18:31:07 UTC (rev 51796) +++ branches/maintenance-work/maintenance/nextJobDB.php 2009-06-12 19:05:55 UTC (rev 51797) @@ -6,55 +6,71 @@ * @ingroup Maintenance */ -$options = array( 'type' ); +require_once( "Maintenance.php" ); -require_once( 'commandLine.inc' ); - -$type = isset($options['type']) - ? $options['type'] - : false; - -$mckey = $type === false - ? "jobqueue:dbs" - : "jobqueue:dbs:$type"; - -$pendingDBs = $wgMemc->get( $mckey ); -if ( !$pendingDBs ) { - $pendingDBs = array(); - # Cross-reference DBs by master DB server - $dbsByMaster = array(); - foreach ( $wgLocalDatabases as $db ) { - $lb = wfGetLB( $db ); - $dbsByMaster[$lb->getServerName(0)][] = $db; +class nextJobDB extends Maintenance { + public function __construct() { + parent::__construct(); + $this->mDescription = "Pick a database that has pending jobs"; + $this->addParam( 'type', "The type of job to search for", false, true ); } - - foreach ( $dbsByMaster as $master => $dbs ) { - $dbConn = wfGetDB( DB_MASTER, array(), $dbs[0] ); - $stype = $dbConn->addQuotes($type); - - # Padding row for MySQL bug - $sql = "(SELECT '-------------------------------------------')"; - foreach ( $dbs as $dbName ) { - if ( $sql != '' ) { - $sql .= ' UNION '; - } - if ($type === false) - $sql .= "(SELECT '$dbName' FROM `$dbName`.job LIMIT 1)"; - else - $sql .= "(SELECT '$dbName' FROM `$dbName`.job WHERE job_cmd=$stype LIMIT 1)"; + public function execute() { + global $wgMemc; + $type = $this->getParam( 'type', false ); + $mckey = $type === false + ? "jobqueue:dbs" + : "jobqueue:dbs:$type"; + $pendingDBs = $wgMemcKey->get( $mckey ); + + # If we didn't get it from the cache + if( !$pendingDBs ) { + $pendingDBs = $this->getPendingDbs( $type ); + $wgMemc->get( $mckey, $pendingDBs, 300 ) } - $res = $dbConn->query( $sql, 'nextJobDB.php' ); - $row = $dbConn->fetchRow( $res ); // discard padding row - while ( $row = $dbConn->fetchRow( $res ) ) { - $pendingDBs[] = $row[0]; + # If we've got a pending job in a db, display it. + if ( $pendingDBs ) { + $this->output( $pendingDBs[mt_rand(0, count( $pendingDBs ) - 1)] ); } } - - $wgMemc->set( $mckey, $pendingDBs, 300 ); + + /** + * Get all databases that have a pending job + * @param $type String Job type + * @return array + */ + private function getPendingDbs( $type ) { + $pendingDBs = array(); + # Cross-reference DBs by master DB server + $dbsByMaster = array(); + foreach ( $wgLocalDatabases as $db ) { + $lb = wfGetLB( $db ); + $dbsByMaster[$lb->getServerName(0)][] = $db; + } + + foreach ( $dbsByMaster as $master => $dbs ) { + $dbConn = wfGetDB( DB_MASTER, array(), $dbs[0] ); + $stype = $dbConn->addQuotes($type); + $jobTable = $dbConn->getTable( 'job' ); + + # Padding row for MySQL bug + $sql = "(SELECT '-------------------------------------------')"; + foreach ( $dbs as $dbName ) { + if ( $sql != '' ) { + $sql .= ' UNION '; + } + if ($type === false) + $sql .= "(SELECT '$dbName' FROM `$dbName`.$jobTable LIMIT 1)"; + else + $sql .= "(SELECT '$dbName' FROM `$dbName`.$jobTable WHERE job_cmd=$stype LIMIT 1)"; + } + $res = $dbConn->query( $sql, __METHOD__ ); + $row = $dbConn->fetchRow( $res ); // discard padding row + while ( $row = $dbConn->fetchRow( $res ) ) { + $pendingDBs[] = $row[0]; + } + } + } } -if ( $pendingDBs ) { - echo $pendingDBs[mt_rand(0, count( $pendingDBs ) - 1)]; -} - - +$maintClass = "nextJobDb"; +require_once( DO_MAINTENANCE ); Modified: branches/maintenance-work/maintenance/renameDbPrefix.php =================================================================== --- branches/maintenance-work/maintenance/renameDbPrefix.php 2009-06-12 18:31:07 UTC (rev 51796) +++ branches/maintenance-work/maintenance/renameDbPrefix.php 2009-06-12 19:05:55 UTC (rev 51797) @@ -6,62 +6,59 @@ * @file * @ingroup Maintenance */ -$optionsWithArgs = array( 'old', 'new', 'help' ); + +require_once( "Maintenance.php" ); -require_once( 'commandLine.inc' ); - -if( @$options['help'] || !isset( $options['old'] ) || !isset( $options['new'] ) ) { - print "usage: renameDbPrefix.php [--help] [--old x] [new y]\n"; - print " --help : this help message\n"; - print " --old x : old db prefix x\n"; - print " --old 0 : EMPTY old db prefix x\n"; - print " --new y : new db prefix y\n"; - print " --new 0 : EMPTY new db prefix\n"; - wfDie(); -} - -// Allow for no old prefix -if( $options['old'] === '0' ) { - $old = ''; -} else { - // Use nice safe, sane, prefixes - preg_match( '/^[a-zA-Z]+_$/', $options['old'], $m ); - $old = isset( $m[0] ) ? $m[0] : false; -} -// Allow for no new prefix -if( $options['new'] === '0' ) { - $new = ''; -} else { - // Use nice safe, sane, prefixes - preg_match( '/^[a-zA-Z]+_$/', $options['new'], $m ); - $new = isset( $m[0] ) ? $m[0] : false; -} - -if( $old === false || $new === false ) { - print "Invalid prefix!\n"; - wfDie(); -} -if( $old === $new ) { - print "Same prefix. Nothing to rename!\n"; - wfDie(); -} - -print "Renaming DB prefix for tables of $wgDBname from '$old' to '$new'\n"; -$count = 0; - -$dbw = wfGetDB( DB_MASTER ); -$res = $dbw->query( "SHOW TABLES LIKE '".$dbw->escapeLike( $old )."%'" ); -foreach( $res as $row ) { - // XXX: odd syntax. MySQL outputs an oddly cased "Tables of X" - // sort of message. Best not to try $row->x stuff... - $fields = get_object_vars( $row ); - // Silly for loop over one field... - foreach( $fields as $resName => $table ) { - // $old should be regexp safe ([a-zA-Z_]) - $newTable = preg_replace( '/^'.$old.'/', $new, $table ); - print "Renaming table $table to $newTable\n"; - $dbw->query( "RENAME TABLE $table TO $newTable" ); +class RenameDbPrefix extends Maintenance { + public function __construct() { + parent::__construct(); + $this->addParam( "old", "Old db prefix [0 for none]", true, true ); + $this->addParam( "new", "New db prefix [0 for none]", true, true ); } - $count++; + + public function execute() { + // Allow for no old prefix + if( $this->getOption( 'old', 0 ) === '0' ) { + $old = ''; + } else { + // Use nice safe, sane, prefixes + preg_match( '/^[a-zA-Z]+_$/', $this->getOption('old'), $m ); + $old = isset( $m[0] ) ? $m[0] : false; + } + // Allow for no new prefix + if( $this->getOption( 'new', 0 ) === '0' ) { + $new = ''; + } else { + // Use nice safe, sane, prefixes + preg_match( '/^[a-zA-Z]+_$/', $this->getOption('new'), $m ); + $new = isset( $m[0] ) ? $m[0] : false; + } + + if( $old === false || $new === false ) { + $this->error( "Invalid prefix!\n", true ); + } + if( $old === $new ) { + $this->( "Same prefix. Nothing to rename!\n", true ); + } + + $this->output( "Renaming DB prefix for tables of $wgDBname from '$old' to '$new'\n" ); + $count = 0; + + $dbw = wfGetDB( DB_MASTER ); + $res = $dbw->query( "SHOW TABLES LIKE '".$dbw->escapeLike( $old )."%'" ); + foreach( $res as $row ) { + // XXX: odd syntax. MySQL outputs an oddly cased "Tables of X" + // sort of message. Best not to try $row->x stuff... + $fields = get_object_vars( $row ); + // Silly for loop over one field... + foreach( $fields as $resName => $table ) { + // $old should be regexp safe ([a-zA-Z_]) + $newTable = preg_replace( '/^'.$old.'/', $new, $table ); + $this->output( "Renaming table $table to $newTable\n" ); + $dbw->query( "RENAME TABLE $table TO $newTable" ); + } + $count++; + } + $this->output( "Done! [$count tables]\n" ); + } } -print "Done! [$count tables]\n"; \ No newline at end of file _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs