MediaWiki  REL1_22
moveBatch.php
Go to the documentation of this file.
00001 <?php
00037 require_once __DIR__ . '/Maintenance.php';
00038 
00044 class MoveBatch extends Maintenance {
00045     public function __construct() {
00046         parent::__construct();
00047         $this->mDescription = "Moves a batch of pages";
00048         $this->addOption( 'u', "User to perform move", false, true );
00049         $this->addOption( 'r', "Reason to move page", false, true );
00050         $this->addOption( 'i', "Interval to sleep between moves" );
00051         $this->addArg( 'listfile', 'List of pages to move, newline delimited', false );
00052     }
00053 
00054     public function execute() {
00055         global $wgUser;
00056 
00057         # Change to current working directory
00058         $oldCwd = getcwd();
00059         chdir( $oldCwd );
00060 
00061         # Options processing
00062         $user = $this->getOption( 'u', 'Move page script' );
00063         $reason = $this->getOption( 'r', '' );
00064         $interval = $this->getOption( 'i', 0 );
00065         if ( $this->hasArg() ) {
00066             $file = fopen( $this->getArg(), 'r' );
00067         } else {
00068             $file = $this->getStdin();
00069         }
00070 
00071         # Setup
00072         if ( !$file ) {
00073             $this->error( "Unable to read file, exiting", true );
00074         }
00075         $wgUser = User::newFromName( $user );
00076         if ( !$wgUser ) {
00077             $this->error( "Invalid username", true );
00078         }
00079 
00080         # Setup complete, now start
00081         $dbw = wfGetDB( DB_MASTER );
00082         for ( $linenum = 1; !feof( $file ); $linenum++ ) {
00083             $line = fgets( $file );
00084             if ( $line === false ) {
00085                 break;
00086             }
00087             $parts = array_map( 'trim', explode( '|', $line ) );
00088             if ( count( $parts ) != 2 ) {
00089                 $this->error( "Error on line $linenum, no pipe character" );
00090                 continue;
00091             }
00092             $source = Title::newFromText( $parts[0] );
00093             $dest = Title::newFromText( $parts[1] );
00094             if ( is_null( $source ) || is_null( $dest ) ) {
00095                 $this->error( "Invalid title on line $linenum" );
00096                 continue;
00097             }
00098 
00099 
00100             $this->output( $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText() );
00101             $dbw->begin( __METHOD__ );
00102             $err = $source->moveTo( $dest, false, $reason );
00103             if ( $err !== true ) {
00104                 $msg = array_shift( $err[0] );
00105                 $this->output( "\nFAILED: " . wfMessage( $msg, $err[0] )->text() );
00106             }
00107             $dbw->commit( __METHOD__ );
00108             $this->output( "\n" );
00109 
00110             if ( $interval ) {
00111                 sleep( $interval );
00112             }
00113             wfWaitForSlaves();
00114         }
00115     }
00116 }
00117 
00118 $maintClass = "MoveBatch";
00119 require_once RUN_MAINTENANCE_IF_MAIN;