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