MediaWiki master
moveBatch.php
Go to the documentation of this file.
1<?php
19
20// @codeCoverageIgnoreStart
21require_once __DIR__ . '/Maintenance.php';
22// @codeCoverageIgnoreEnd
23
29class MoveBatch extends Maintenance {
30 public function __construct() {
31 parent::__construct();
32 $this->addDescription( 'Moves a batch of pages' );
33 $this->addOption( 'u', "User to perform move", false, true );
34 $this->addOption( 'r', "Reason to move page", false, true );
35 $this->addOption( 'i', "Interval to sleep between moves" );
36 $this->addOption( 'noredirects', "Suppress creation of redirects" );
37 $this->addArg(
38 'listfile',
39 'List of pages to move (newline delimited) in the format <existing page name>|<new name>',
40 false
41 );
42 }
43
44 public function execute() {
45 # Options processing
46 $username = $this->getOption( 'u', false );
47 $reason = $this->getOption( 'r', '' );
48 $interval = $this->getOption( 'i', 0 );
49 $noRedirects = $this->hasOption( 'noredirects' );
50 if ( $this->hasArg( 0 ) ) {
51 $file = fopen( $this->getArg( 0 ), 'r' );
52 } else {
53 $file = $this->getStdin();
54 }
55
56 # Setup
57 if ( !$file ) {
58 $this->fatalError( "Unable to read file, exiting" );
59 }
60 if ( $username === false ) {
61 $user = User::newSystemUser( 'Move page script', [ 'steal' => true ] );
62 } else {
63 $user = User::newFromName( $username );
64 }
65 if ( !$user || !$user->isRegistered() ) {
66 $this->fatalError( "Invalid username" );
67 }
68 StubGlobalUser::setUser( $user );
69
70 $movePageFactory = $this->getServiceContainer()->getMovePageFactory();
71
72 # Setup complete, now start
73 for ( $lineNum = 1; !feof( $file ); $lineNum++ ) {
74 $line = fgets( $file );
75 if ( $line === false ) {
76 break;
77 }
78 $parts = array_map( 'trim', explode( '|', $line ) );
79 if ( count( $parts ) !== 2 ) {
80 $this->error( "Error on line $lineNum, no pipe character" );
81 continue;
82 }
83 $source = Title::newFromText( $parts[0] );
84 $dest = Title::newFromText( $parts[1] );
85 if ( $source === null || $dest === null ) {
86 $this->error( "Invalid title on line $lineNum" );
87 continue;
88 }
89
90 $this->output( $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText() );
91 $this->beginTransactionRound( __METHOD__ );
92 $mp = $movePageFactory->newMovePage( $source, $dest );
93 $status = $mp->move( $user, $reason, !$noRedirects );
94 if ( !$status->isOK() ) {
95 $this->output( " FAILED\n" );
96 $this->error( $status );
97 }
98 $this->commitTransactionRound( __METHOD__ );
99 $this->output( "\n" );
100
101 if ( $interval ) {
102 sleep( $interval );
103 }
104 }
105 }
106}
107
108// @codeCoverageIgnoreStart
109$maintClass = MoveBatch::class;
110require_once RUN_MAINTENANCE_IF_MAIN;
111// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
hasArg( $argId=0)
Does a given argument exist?
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
getStdin( $len=null)
Return input from stdin.
addDescription( $text)
Set the description text.
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:69
User class for the MediaWiki software.
Definition User.php:109
Maintenance script to move a batch of pages.
Definition moveBatch.php:29
execute()
Do the actual work.
Definition moveBatch.php:44
__construct()
Default constructor.
Definition moveBatch.php:30
$maintClass
$source