<?php
/**
- * Maintenance script to move a batch of pages
+ * Move a batch of pages.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
+ * @file
* @ingroup Maintenance
* @author Tim Starling
*
- * USAGE: php moveBatch.php [-u <user>] [-r <reason>] [-i <interval>] [listfile]
+ * USAGE: php moveBatch.php [-u <user>] [-r <reason>] [-i <interval>] [-noredirects] [listfile]
*
* [listfile] - file with two titles per line, separated with pipe characters;
* the first title is the source, the second is the destination.
* <user> - username to perform moves as
* <reason> - reason to be given for moves
* <interval> - number of seconds to sleep after each move
+ * <noredirects> - suppress creation of redirects
*
* This will print out error codes from Title::moveTo() if something goes wrong,
* e.g. immobile_namespace for namespaces which can't be moved
*/
-require_once( dirname( __FILE__ ) . '/Maintenance.php' );
+require_once __DIR__ . '/Maintenance.php';
+/**
+ * Maintenance script to move a batch of pages.
+ *
+ * @ingroup Maintenance
+ */
class MoveBatch extends Maintenance {
public function __construct() {
parent::__construct();
- $this->mDescription = "Moves a batch of pages";
+ $this->addDescription( 'Moves a batch of pages' );
$this->addOption( 'u', "User to perform move", false, true );
$this->addOption( 'r', "Reason to move page", false, true );
$this->addOption( 'i', "Interval to sleep between moves" );
+ $this->addOption( 'noredirects', "Suppress creation of redirects" );
$this->addArg( 'listfile', 'List of pages to move, newline delimited', false );
}
chdir( $oldCwd );
# Options processing
- $user = $this->getOption( 'u', 'Move page script' );
+ $user = $this->getOption( 'u', false );
$reason = $this->getOption( 'r', '' );
$interval = $this->getOption( 'i', 0 );
+ $noredirects = $this->hasOption( 'noredirects' );
if ( $this->hasArg() ) {
$file = fopen( $this->getArg(), 'r' );
} else {
if ( !$file ) {
$this->error( "Unable to read file, exiting", true );
}
- $wgUser = User::newFromName( $user );
+ if ( $user === false ) {
+ $wgUser = User::newSystemUser( 'Move page script', [ 'steal' => true ] );
+ } else {
+ $wgUser = User::newFromName( $user );
+ }
+ if ( !$wgUser ) {
+ $this->error( "Invalid username", true );
+ }
# Setup complete, now start
- $dbw = wfGetDB( DB_MASTER );
+ $dbw = $this->getDB( DB_MASTER );
+ // @codingStandardsIgnoreStart Ignore avoid function calls in a FOR loop test part warning
for ( $linenum = 1; !feof( $file ); $linenum++ ) {
+ // @codingStandardsIgnoreEnd
$line = fgets( $file );
if ( $line === false ) {
break;
continue;
}
-
$this->output( $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText() );
- $dbw->begin();
- $err = $source->moveTo( $dest, false, $reason );
- if ( $err !== true ) {
- $msg = array_shift( $err[0] );
- $this->output( "\nFAILED: " . wfMsg( $msg, $err[0] ) );
+ $this->beginTransaction( $dbw, __METHOD__ );
+ $mp = new MovePage( $source, $dest );
+ $status = $mp->move( $wgUser, $reason, !$noredirects );
+ if ( !$status->isOK() ) {
+ $this->output( "\nFAILED: " . $status->getWikiText( false, false, 'en' ) );
}
- $dbw->commit();
+ $this->commitTransaction( $dbw, __METHOD__ );
$this->output( "\n" );
if ( $interval ) {
sleep( $interval );
}
- wfWaitForSlaves( 5 );
+ wfWaitForSlaves();
}
}
}
$maintClass = "MoveBatch";
-require_once( RUN_MAINTENANCE_IF_MAIN );
+require_once RUN_MAINTENANCE_IF_MAIN;