Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.49% |
43 / 47 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
MoveBatch | |
91.49% |
43 / 47 |
|
50.00% |
1 / 2 |
14.12 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
90.00% |
36 / 40 |
|
0.00% |
0 / 1 |
13.17 |
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\Maintenance\Maintenance; |
30 | use MediaWiki\StubObject\StubGlobalUser; |
31 | use MediaWiki\Title\Title; |
32 | use MediaWiki\User\User; |
33 | |
34 | // @codeCoverageIgnoreStart |
35 | require_once __DIR__ . '/Maintenance.php'; |
36 | // @codeCoverageIgnoreEnd |
37 | |
38 | /** |
39 | * Maintenance script to move a batch of pages. |
40 | * |
41 | * @ingroup Maintenance |
42 | */ |
43 | class 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; |
120 | require_once RUN_MAINTENANCE_IF_MAIN; |
121 | // @codeCoverageIgnoreEnd |