MediaWiki  master
moveBatch.php
Go to the documentation of this file.
1 <?php
32 
33 require_once __DIR__ . '/Maintenance.php';
34 
40 class MoveBatch extends Maintenance {
41  public function __construct() {
42  parent::__construct();
43  $this->addDescription( 'Moves a batch of pages' );
44  $this->addOption( 'u', "User to perform move", false, true );
45  $this->addOption( 'r', "Reason to move page", false, true );
46  $this->addOption( 'i', "Interval to sleep between moves" );
47  $this->addOption( 'noredirects', "Suppress creation of redirects" );
48  $this->addArg( 'listfile', 'List of pages to move, newline delimited', false );
49  }
50 
51  public function execute() {
52  # Options processing
53  $username = $this->getOption( 'u', false );
54  $reason = $this->getOption( 'r', '' );
55  $interval = $this->getOption( 'i', 0 );
56  $noRedirects = $this->hasOption( 'noredirects' );
57  if ( $this->hasArg( 0 ) ) {
58  $file = fopen( $this->getArg( 0 ), 'r' );
59  } else {
60  $file = $this->getStdin();
61  }
62 
63  # Setup
64  if ( !$file ) {
65  $this->fatalError( "Unable to read file, exiting" );
66  }
67  if ( $username === false ) {
68  $user = User::newSystemUser( 'Move page script', [ 'steal' => true ] );
69  } else {
70  $user = User::newFromName( $username );
71  }
72  if ( !$user ) {
73  $this->fatalError( "Invalid username" );
74  }
75  StubGlobalUser::setUser( $user );
76 
77  # Setup complete, now start
78  $dbw = $this->getDB( DB_PRIMARY );
79  for ( $lineNum = 1; !feof( $file ); $lineNum++ ) {
80  $line = fgets( $file );
81  if ( $line === false ) {
82  break;
83  }
84  $parts = array_map( 'trim', explode( '|', $line ) );
85  if ( count( $parts ) !== 2 ) {
86  $this->error( "Error on line $lineNum, no pipe character" );
87  continue;
88  }
89  $source = Title::newFromText( $parts[0] );
90  $dest = Title::newFromText( $parts[1] );
91  if ( $source === null || $dest === null ) {
92  $this->error( "Invalid title on line $lineNum" );
93  continue;
94  }
95 
96  $this->output( $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText() );
97  $this->beginTransaction( $dbw, __METHOD__ );
98  $mp = $this->getServiceContainer()->getMovePageFactory()
99  ->newMovePage( $source, $dest );
100  $status = $mp->move( $user, $reason, !$noRedirects );
101  if ( !$status->isOK() ) {
102  $this->output( "\nFAILED: " . $status->getMessage( false, false, 'en' )->text() );
103  }
104  $this->commitTransaction( $dbw, __METHOD__ );
105  $this->output( "\n" );
106 
107  if ( $interval ) {
108  sleep( $interval );
109  }
110  }
111  }
112 }
113 
114 $maintClass = MoveBatch::class;
115 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.
getServiceContainer()
Returns the main service container.
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.
Stub object for the global user ($wgUser) that makes it possible to change the relevant underlying ob...
Represents a title within MediaWiki.
Definition: Title.php:76
internal since 1.36
Definition: User.php:98
Maintenance script to move a batch of pages.
Definition: moveBatch.php:40
execute()
Do the actual work.
Definition: moveBatch.php:51
__construct()
Default constructor.
Definition: moveBatch.php:41
$maintClass
Definition: moveBatch.php:114
$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