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