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 00071 # Setup complete, now start 00072 $dbw = wfGetDB( DB_MASTER ); 00073 for ( $linenum = 1; !feof( $file ); $linenum++ ) { 00074 $line = fgets( $file ); 00075 if ( $line === false ) { 00076 break; 00077 } 00078 $parts = array_map( 'trim', explode( '|', $line ) ); 00079 if ( count( $parts ) != 2 ) { 00080 $this->error( "Error on line $linenum, no pipe character" ); 00081 continue; 00082 } 00083 $source = Title::newFromText( $parts[0] ); 00084 $dest = Title::newFromText( $parts[1] ); 00085 if ( is_null( $source ) || is_null( $dest ) ) { 00086 $this->error( "Invalid title on line $linenum" ); 00087 continue; 00088 } 00089 00090 00091 $this->output( $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText() ); 00092 $dbw->begin(); 00093 $err = $source->moveTo( $dest, false, $reason ); 00094 if( $err !== true ) { 00095 $msg = array_shift( $err[0] ); 00096 $this->output( "\nFAILED: " . wfMsg( $msg, $err[0] ) ); 00097 } 00098 $dbw->commit(); 00099 $this->output( "\n" ); 00100 00101 if ( $interval ) { 00102 sleep( $interval ); 00103 } 00104 wfWaitForSlaves( 5 ); 00105 } 00106 } 00107 } 00108 00109 $maintClass = "MoveBatch"; 00110 require_once( DO_MAINTENANCE );