Script for moving large numbers of pages
[lhc/web/wiklou.git] / maintenance / moveBatch.php
1 <?php
2
3 # Move a batch of pages
4 # Usage: php moveBatch.php [-u <user>] [-r <reason>] [-i <interval>] <listfile>
5 # where
6 # <listfile> is a file where each line has two titles separated by a pipe
7 # character. The first title is the source, the second is the destination.
8 # <user> is the username
9 # <reason> is the move reason
10 # <interval> is the number of seconds to sleep for after each move
11
12 $oldCwd = getcwd();
13 $optionsWithArgs = array( 'u', 'r', 'i' );
14 require_once( 'commandLine.inc' );
15
16 chdir( $oldCwd );
17
18 # Options processing
19
20 $filename = 'php://stdin';
21 $user = 'Move page script';
22 $reason = '';
23 $interval = 0;
24
25 if ( isset( $args[0] ) ) {
26 $filename = $args[0];
27 }
28 if ( isset( $options['u'] ) ) {
29 $user = $options['u'];
30 }
31 if ( isset( $options['r'] ) ) {
32 $reason = $options['r'];
33 }
34 if ( isset( $options['i'] ) ) {
35 $interval = $options['i'];
36 }
37
38 $wgUser = User::newFromName( $user );
39
40
41 # Setup complete, now start
42
43 $file = fopen( $filename, 'r' );
44 if ( !$file ) {
45 print "Unable to read file, exiting\n";
46 }
47
48 $dbw =& wfGetDB( DB_MASTER );
49
50 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
51 $line = fgets( $file );
52 if ( $line === false ) {
53 break;
54 }
55 $parts = array_map( 'trim', explode( '|', $line ) );
56 if ( count( $parts ) != 2 ) {
57 print "Error on line $linenum, no pipe character\n";
58 continue;
59 }
60 $source = Title::newFromText( $parts[0] );
61 $dest = Title::newFromText( $parts[1] );
62 if ( is_null( $source ) || is_null( $dest ) ) {
63 print "Invalid title on line $linenum\n";
64 continue;
65 }
66
67
68 $dbw->begin();
69 $source->moveTo( $dest, false, $reason );
70 $dbw->immediateCommit();
71
72 wfWaitForSlaves( 5 );
73 if ( $interval ) {
74 sleep( $interval );
75 }
76 }
77
78
79 ?>