MediaWiki master
moveBatch.php
Go to the documentation of this file.
1<?php
18
19// @codeCoverageIgnoreStart
20require_once __DIR__ . '/Maintenance.php';
21// @codeCoverageIgnoreEnd
22
28class MoveBatch extends Maintenance {
29 public function __construct() {
30 parent::__construct();
31 $this->addDescription( 'Moves a batch of pages' );
32 $this->addOption( 'u', "User to perform move", false, true );
33 $this->addOption( 'r', "Reason to move page", false, true );
34 $this->addOption( 'i', "Interval to sleep between moves" );
35 $this->addOption( 'noredirects', "Suppress creation of redirects" );
36 $this->addArg(
37 'listfile',
38 'List of pages to move (newline delimited) in the format <existing page name>|<new name>',
39 false
40 );
41 }
42
43 public function execute() {
44 # Options processing
45 $username = $this->getOption( 'u', false );
46 $reason = $this->getOption( 'r', '' );
47 $interval = $this->getOption( 'i', 0 );
48 $noRedirects = $this->hasOption( 'noredirects' );
49 if ( $this->hasArg( 0 ) ) {
50 $file = fopen( $this->getArg( 0 ), 'r' );
51 } else {
52 $file = $this->getStdin();
53 }
54
55 # Setup
56 if ( !$file ) {
57 $this->fatalError( "Unable to read file, exiting" );
58 }
59 if ( $username === false ) {
60 $user = User::newSystemUser( 'Move page script', [ 'steal' => true ] );
61 } else {
62 $user = User::newFromName( $username );
63 }
64 if ( !$user || !$user->isRegistered() ) {
65 $this->fatalError( "Invalid username" );
66 }
67
68 $movePageFactory = $this->getServiceContainer()->getMovePageFactory();
69
70 # Setup complete, now start
71 for ( $lineNum = 1; !feof( $file ); $lineNum++ ) {
72 $line = fgets( $file );
73 if ( $line === false ) {
74 break;
75 }
76 $parts = array_map( 'trim', explode( '|', $line ) );
77 if ( count( $parts ) !== 2 ) {
78 $this->error( "Error on line $lineNum, no pipe character" );
79 continue;
80 }
81 $source = Title::newFromText( $parts[0] );
82 $dest = Title::newFromText( $parts[1] );
83 if ( $source === null || $dest === null ) {
84 $this->error( "Invalid title on line $lineNum" );
85 continue;
86 }
87
88 $this->output( $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText() );
89 $this->beginTransactionRound( __METHOD__ );
90 $mp = $movePageFactory->newMovePage( $source, $dest );
91 $status = $mp->move( $user, $reason, !$noRedirects );
92 if ( !$status->isOK() ) {
93 $this->output( " FAILED\n" );
94 $this->error( $status );
95 }
96 $this->commitTransactionRound( __METHOD__ );
97 $this->output( "\n" );
98
99 if ( $interval ) {
100 sleep( $interval );
101 }
102 }
103 }
104}
105
106// @codeCoverageIgnoreStart
107$maintClass = MoveBatch::class;
108require_once RUN_MAINTENANCE_IF_MAIN;
109// @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.
error( $err)
Throw an error 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.
getServiceContainer()
Returns the main service container.
getStdin( $len=null)
Return input from stdin.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:69
User class for the MediaWiki software.
Definition User.php:129
Maintenance script to move a batch of pages.
Definition moveBatch.php:28
execute()
Do the actual work.
Definition moveBatch.php:43
__construct()
Default constructor.
Definition moveBatch.php:29
$maintClass
$source