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