maintenance/version: Namespace and rename
[mediawiki.git] / maintenance / moveBatch.php
blob6d75998f2726a4594b6d36cfd7b09c8aba442b40
1 <?php
2 /**
3 * Move a batch of pages.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
22 * @author Tim Starling
25 * This will print out error codes from Title::moveTo() if something goes wrong,
26 * e.g. immobile_namespace for namespaces which can't be moved
29 use MediaWiki\StubObject\StubGlobalUser;
30 use MediaWiki\Title\Title;
31 use MediaWiki\User\User;
33 require_once __DIR__ . '/Maintenance.php';
35 /**
36 * Maintenance script to move a batch of pages.
38 * @ingroup Maintenance
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 );
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();
63 # Setup
64 if ( !$file ) {
65 $this->fatalError( "Unable to read file, exiting" );
67 if ( $username === false ) {
68 $user = User::newSystemUser( 'Move page script', [ 'steal' => true ] );
69 } else {
70 $user = User::newFromName( $username );
72 if ( !$user ) {
73 $this->fatalError( "Invalid username" );
75 StubGlobalUser::setUser( $user );
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;
84 $parts = array_map( 'trim', explode( '|', $line ) );
85 if ( count( $parts ) !== 2 ) {
86 $this->error( "Error on line $lineNum, no pipe character" );
87 continue;
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;
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() );
104 $this->commitTransaction( $dbw, __METHOD__ );
105 $this->output( "\n" );
107 if ( $interval ) {
108 sleep( $interval );
114 $maintClass = MoveBatch::class;
115 require_once RUN_MAINTENANCE_IF_MAIN;