MediaWiki  REL1_24
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         // @codingStandardsIgnoreStart Ignore avoid function calls in a FOR loop test part warning
00086         for ( $linenum = 1; !feof( $file ); $linenum++ ) {
00087             // @codingStandardsIgnoreEnd
00088             $line = fgets( $file );
00089             if ( $line === false ) {
00090                 break;
00091             }
00092             $parts = array_map( 'trim', explode( '|', $line ) );
00093             if ( count( $parts ) != 2 ) {
00094                 $this->error( "Error on line $linenum, no pipe character" );
00095                 continue;
00096             }
00097             $source = Title::newFromText( $parts[0] );
00098             $dest = Title::newFromText( $parts[1] );
00099             if ( is_null( $source ) || is_null( $dest ) ) {
00100                 $this->error( "Invalid title on line $linenum" );
00101                 continue;
00102             }
00103 
00104             $this->output( $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText() );
00105             $dbw->begin( __METHOD__ );
00106             $err = $source->moveTo( $dest, false, $reason, !$noredirects );
00107             if ( $err !== true ) {
00108                 $msg = array_shift( $err[0] );
00109                 $this->output( "\nFAILED: " . wfMessage( $msg, $err[0] )->text() );
00110             }
00111             $dbw->commit( __METHOD__ );
00112             $this->output( "\n" );
00113 
00114             if ( $interval ) {
00115                 sleep( $interval );
00116             }
00117             wfWaitForSlaves();
00118         }
00119     }
00120 }
00121 
00122 $maintClass = "MoveBatch";
00123 require_once RUN_MAINTENANCE_IF_MAIN;