4b0abf7f1387f71e6ed4fd51e753f6675a1cbf7b
[lhc/web/wiklou.git] / maintenance / moveBatch.php
1 <?php
2
3 /**
4 * Maintenance script to move a batch of pages
5 *
6 * @package MediaWiki
7 * @subpackage Maintenance
8 * @author Tim Starling
9 *
10 * USAGE: php moveBatch.php [-u <user>] [-r <reason>] [-i <interval>] <listfile>
11 *
12 * <listfile> - file with two titles per line, separated with pipe characters;
13 * the first title is the source, the second is the destination
14 * <user> - username to perform moves as
15 * <reason> - reason to be given for moves
16 * <interval> - number of seconds to sleep after each move
17 *
18 * This will print out error codes from Title::moveTo() if something goes wrong,
19 * e.g. immobile_namespace for namespaces which can't be moved
20 */
21
22 $oldCwd = getcwd();
23 $optionsWithArgs = array( 'u', 'r', 'i' );
24 require_once( 'commandLine.inc' );
25
26 chdir( $oldCwd );
27
28 # Options processing
29
30 $filename = 'php://stdin';
31 $user = 'Move page script';
32 $reason = '';
33 $interval = 0;
34
35 if ( isset( $args[0] ) ) {
36 $filename = $args[0];
37 }
38 if ( isset( $options['u'] ) ) {
39 $user = $options['u'];
40 }
41 if ( isset( $options['r'] ) ) {
42 $reason = $options['r'];
43 }
44 if ( isset( $options['i'] ) ) {
45 $interval = $options['i'];
46 }
47
48 $wgUser = User::newFromName( $user );
49
50
51 # Setup complete, now start
52
53 $file = fopen( $filename, 'r' );
54 if ( !$file ) {
55 print "Unable to read file, exiting\n";
56 exit;
57 }
58
59 $dbw =& wfGetDB( DB_MASTER );
60
61 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
62 $line = fgets( $file );
63 if ( $line === false ) {
64 break;
65 }
66 $parts = array_map( 'trim', explode( '|', $line ) );
67 if ( count( $parts ) != 2 ) {
68 print "Error on line $linenum, no pipe character\n";
69 continue;
70 }
71 $source = Title::newFromText( $parts[0] );
72 $dest = Title::newFromText( $parts[1] );
73 if ( is_null( $source ) || is_null( $dest ) ) {
74 print "Invalid title on line $linenum\n";
75 continue;
76 }
77
78
79 print $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText();
80 $dbw->begin();
81 $err = $source->moveTo( $dest, false, $reason );
82 if( $err !== true ) {
83 print "\nFAILED: $err";
84 }
85 $dbw->immediateCommit();
86 print "\n";
87
88 if ( $interval ) {
89 sleep( $interval );
90 }
91 wfWaitForSlaves( 5 );
92 }
93
94
95 ?>