summaryrefslogtreecommitdiff
path: root/maintenance/moveBatch.php
blob: 67d513ed52788f344df31db666970502ca724e5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

/**
 * Maintenance script to move a batch of pages
 *
 * @file
 * @ingroup Maintenance
 * @author Tim Starling
 *
 * USAGE: php moveBatch.php [-u <user>] [-r <reason>] [-i <interval>] [listfile]
 *
 * [listfile] - file with two titles per line, separated with pipe characters;
 * the first title is the source, the second is the destination.
 * Standard input is used if listfile is not given.
 * <user> - username to perform moves as
 * <reason> - reason to be given for moves
 * <interval> - number of seconds to sleep after each move
 *
 * This will print out error codes from Title::moveTo() if something goes wrong,
 * e.g. immobile_namespace for namespaces which can't be moved
 */

$oldCwd = getcwd();
$optionsWithArgs = array( 'u', 'r', 'i' );
require_once( 'commandLine.inc' );

chdir( $oldCwd );

# Options processing

$filename = 'php://stdin';
$user = 'Move page script';
$reason = '';
$interval = 0;

if ( isset( $args[0] ) ) {
	$filename = $args[0];
}
if ( isset( $options['u'] ) ) {
	$user = $options['u'];
}
if ( isset( $options['r'] ) ) {
	$reason = $options['r'];
}
if ( isset( $options['i'] ) ) {
	$interval = $options['i'];
}

$wgUser = User::newFromName( $user );


# Setup complete, now start

$file = fopen( $filename, 'r' );
if ( !$file ) {
	print "Unable to read file, exiting\n";
	exit;
}

$dbw = wfGetDB( DB_MASTER );

for ( $linenum = 1; !feof( $file ); $linenum++ ) {
	$line = fgets( $file );
	if ( $line === false ) {
		break;
	}
	$parts = array_map( 'trim', explode( '|', $line ) );
	if ( count( $parts ) != 2 ) {
		print "Error on line $linenum, no pipe character\n";
		continue;
	}
	$source = Title::newFromText( $parts[0] );
	$dest = Title::newFromText( $parts[1] );
	if ( is_null( $source ) || is_null( $dest ) ) {
		print "Invalid title on line $linenum\n";
		continue;
	}


	print $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText();
	$dbw->begin();
	$err = $source->moveTo( $dest, false, $reason );
	if( $err !== true ) {
		$msg = array_shift( $err[0] );
		print "\nFAILED: " . wfMsg( $msg, $err[0] );
	}
	$dbw->immediateCommit();
	print "\n";

	if ( $interval ) {
		sleep( $interval );
	}
	wfWaitForSlaves( 5 );
}