MediaWiki  master
moveBatch.php
Go to the documentation of this file.
1 <?php
31 
32 require_once __DIR__ . '/Maintenance.php';
33 
39 class MoveBatch extends Maintenance {
40  public function __construct() {
41  parent::__construct();
42  $this->addDescription( 'Moves a batch of pages' );
43  $this->addOption( 'u', "User to perform move", false, true );
44  $this->addOption( 'r', "Reason to move page", false, true );
45  $this->addOption( 'i', "Interval to sleep between moves" );
46  $this->addOption( 'noredirects', "Suppress creation of redirects" );
47  $this->addArg( 'listfile', 'List of pages to move, newline delimited', false );
48  }
49 
50  public function execute() {
51  # Options processing
52  $username = $this->getOption( 'u', false );
53  $reason = $this->getOption( 'r', '' );
54  $interval = $this->getOption( 'i', 0 );
55  $noRedirects = $this->hasOption( 'noredirects' );
56  if ( $this->hasArg( 0 ) ) {
57  $file = fopen( $this->getArg( 0 ), 'r' );
58  } else {
59  $file = $this->getStdin();
60  }
61 
62  # Setup
63  if ( !$file ) {
64  $this->fatalError( "Unable to read file, exiting" );
65  }
66  if ( $username === false ) {
67  $user = User::newSystemUser( 'Move page script', [ 'steal' => true ] );
68  } else {
69  $user = User::newFromName( $username );
70  }
71  if ( !$user ) {
72  $this->fatalError( "Invalid username" );
73  }
74  StubGlobalUser::setUser( $user );
75 
76  # Setup complete, now start
77  $dbw = $this->getDB( DB_PRIMARY );
78  for ( $lineNum = 1; !feof( $file ); $lineNum++ ) {
79  $line = fgets( $file );
80  if ( $line === false ) {
81  break;
82  }
83  $parts = array_map( 'trim', explode( '|', $line ) );
84  if ( count( $parts ) !== 2 ) {
85  $this->error( "Error on line $lineNum, no pipe character" );
86  continue;
87  }
88  $source = Title::newFromText( $parts[0] );
89  $dest = Title::newFromText( $parts[1] );
90  if ( $source === null || $dest === null ) {
91  $this->error( "Invalid title on line $lineNum" );
92  continue;
93  }
94 
95  $this->output( $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText() );
96  $this->beginTransaction( $dbw, __METHOD__ );
97  $mp = MediaWikiServices::getInstance()->getMovePageFactory()
98  ->newMovePage( $source, $dest );
99  $status = $mp->move( $user, $reason, !$noRedirects );
100  if ( !$status->isOK() ) {
101  $this->output( "\nFAILED: " . $status->getMessage( false, false, 'en' )->text() );
102  }
103  $this->commitTransaction( $dbw, __METHOD__ );
104  $this->output( "\n" );
105 
106  if ( $interval ) {
107  sleep( $interval );
108  }
109  }
110  }
111 }
112 
113 $maintClass = MoveBatch::class;
114 require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getStdin( $len=null)
Return input from stdin.
hasArg( $argId=0)
Does a given argument exist?
hasOption( $name)
Checks to see if a particular option was set.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
Stub object for the global user ($wgUser) that makes it possible to change the relevant underlying ob...
Maintenance script to move a batch of pages.
Definition: moveBatch.php:39
execute()
Do the actual work.
Definition: moveBatch.php:50
__construct()
Default constructor.
Definition: moveBatch.php:40
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:373
static newFromName( $name, $validate='valid')
Definition: User.php:591
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:799
$maintClass
Definition: moveBatch.php:113
$source
const DB_PRIMARY
Definition: defines.php:28
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42