| 1 |
<?php |
| 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 |
use MediaWiki\StubObject\StubGlobalUser; |
| 30 |
use MediaWiki\Title\Title; |
| 31 |
use MediaWiki\User\User; |
| 32 |
|
| 33 |
|
| 34 |
require_once __DIR__ . '/Maintenance.php'; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class MoveBatch extends Maintenance { |
| 43 |
public function __construct() { |
| 44 |
parent::__construct(); |
| 45 |
$this->addDescription( 'Moves a batch of pages' ); |
| 46 |
$this->addOption( 'u', "User to perform move", false, true ); |
| 47 |
$this->addOption( 'r', "Reason to move page", false, true ); |
| 48 |
$this->addOption( 'i', "Interval to sleep between moves" ); |
| 49 |
$this->addOption( 'noredirects', "Suppress creation of redirects" ); |
| 50 |
$this->addArg( 'listfile', 'List of pages to move, newline delimited', false ); |
| 51 |
} |
| 52 |
|
| 53 |
public function execute() { |
| 54 |
|
| 55 |
$username = $this->getOption( 'u', false ); |
| 56 |
$reason = $this->getOption( 'r', '' ); |
| 57 |
$interval = $this->getOption( 'i', 0 ); |
| 58 |
$noRedirects = $this->hasOption( 'noredirects' ); |
| 59 |
if ( $this->hasArg( 0 ) ) { |
| 60 |
$file = fopen( $this->getArg( 0 ), 'r' ); |
| 61 |
} else { |
| 62 |
$file = $this->getStdin(); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
if ( !$file ) { |
| 67 |
$this->fatalError( "Unable to read file, exiting" ); |
| 68 |
} |
| 69 |
if ( $username === false ) { |
| 70 |
$user = User::newSystemUser( 'Move page script', [ 'steal' => true ] ); |
| 71 |
} else { |
| 72 |
$user = User::newFromName( $username ); |
| 73 |
} |
| 74 |
if ( !$user || !$user->isRegistered() ) { |
| 75 |
$this->fatalError( "Invalid username" ); |
| 76 |
} |
| 77 |
StubGlobalUser::setUser( $user ); |
| 78 |
|
| 79 |
|
| 80 |
$dbw = $this->getPrimaryDB(); |
| 81 |
for ( $lineNum = 1; !feof( $file ); $lineNum++ ) { |
| 82 |
$line = fgets( $file ); |
| 83 |
if ( $line === false ) { |
| 84 |
break; |
| 85 |
} |
| 86 |
$parts = array_map( 'trim', explode( '|', $line ) ); |
| 87 |
if ( count( $parts ) !== 2 ) { |
| 88 |
$this->error( "Error on line $lineNum, no pipe character" ); |
| 89 |
continue; |
| 90 |
} |
| 91 |
$source = Title::newFromText( $parts[0] ); |
| 92 |
$dest = Title::newFromText( $parts[1] ); |
| 93 |
if ( $source === null || $dest === null ) { |
| 94 |
$this->error( "Invalid title on line $lineNum" ); |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
$this->output( $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText() ); |
| 99 |
$this->beginTransaction( $dbw, __METHOD__ ); |
| 100 |
$mp = $this->getServiceContainer()->getMovePageFactory() |
| 101 |
->newMovePage( $source, $dest ); |
| 102 |
$status = $mp->move( $user, $reason, !$noRedirects ); |
| 103 |
if ( !$status->isOK() ) { |
| 104 |
$this->output( " FAILED\n" ); |
| 105 |
$this->error( $status ); |
| 106 |
} |
| 107 |
$this->commitTransaction( $dbw, __METHOD__ ); |
| 108 |
$this->output( "\n" ); |
| 109 |
|
| 110 |
if ( $interval ) { |
| 111 |
sleep( $interval ); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
$maintClass = MoveBatch::class; |
| 119 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 120 |
|