Debian Codegraph @

maintenance/moveBatch.php

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