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