Merge maintenance-work branch:
[lhc/web/wiklou.git] / maintenance / moveBatch.php
1 <?php
2 /**
3 * Maintenance script to move a batch of pages
4 *
5 * @ingroup Maintenance
6 * @author Tim Starling
7 *
8 * USAGE: php moveBatch.php [-u <user>] [-r <reason>] [-i <interval>] [listfile]
9 *
10 * [listfile] - file with two titles per line, separated with pipe characters;
11 * the first title is the source, the second is the destination.
12 * Standard input is used if listfile is not given.
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 require_once( "Maintenance.php" );
22
23 class MoveBatch extends Maintenance {
24 public function __construct() {
25 parent::__construct();
26 $this->mDescription = "Moves a batch of pages";
27 $this->addParam( 'u', "User to perform move", false, true );
28 $this->addParam( 'r', "Reason to move page", false, true );
29 $this->addParam( 'i', "Interval to sleep between moves" );
30 $this->addArgs( array( 'listfile' ) );
31 }
32
33 public function execute() {
34 global $wgUser;
35
36 # Change to current working directory
37 $oldCwd = getcwd();
38 chdir( $oldCwd );
39
40 # Options processing
41 $user = $this->getOption( 'u', 'Move page script' );
42 $reason = $this->getOption( 'r', '' );
43 $interval = $this->getOption( 'i', 0 );
44 if( $this->hasArg() ) {
45 $file = fopen( $this->getArg(), 'r' );
46 } else {
47 $file = $this->getStdin();
48 }
49
50 # Setup
51 if( !$file ) {
52 $this->error( "Unable to read file, exiting\n", true );
53 }
54 $wgUser = User::newFromName( $user );
55
56 # Setup complete, now start
57 $dbw = wfGetDB( DB_MASTER );
58 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
59 $line = fgets( $file );
60 if ( $line === false ) {
61 break;
62 }
63 $parts = array_map( 'trim', explode( '|', $line ) );
64 if ( count( $parts ) != 2 ) {
65 $this->error( "Error on line $linenum, no pipe character\n" );
66 continue;
67 }
68 $source = Title::newFromText( $parts[0] );
69 $dest = Title::newFromText( $parts[1] );
70 if ( is_null( $source ) || is_null( $dest ) ) {
71 $this->error( "Invalid title on line $linenum\n" );
72 continue;
73 }
74
75
76 $this->output( $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText() );
77 $dbw->begin();
78 $err = $source->moveTo( $dest, false, $reason );
79 if( $err !== true ) {
80 $this->output( "\nFAILED: $err" );
81 }
82 $dbw->immediateCommit();
83 $this->output( "\n" );
84
85 if ( $interval ) {
86 sleep( $interval );
87 }
88 wfWaitForSlaves( 5 );
89 }
90 }
91 }
92
93 $maintClass = "MoveBatch";
94 require_once( DO_MAINTENANCE );