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