Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MoveBatch
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 2
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2/**
3 * Move a batch of pages.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 * @author Tim Starling
23 *
24 *
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
27 */
28
29use MediaWiki\StubObject\StubGlobalUser;
30use MediaWiki\Title\Title;
31use MediaWiki\User\User;
32
33require_once __DIR__ . '/Maintenance.php';
34
35/**
36 * Maintenance script to move a batch of pages.
37 *
38 * @ingroup Maintenance
39 */
40class 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->getPrimaryDB();
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;
115require_once RUN_MAINTENANCE_IF_MAIN;