MediaWiki  1.28.0
moveBatch.php
Go to the documentation of this file.
1 <?php
38 require_once __DIR__ . '/Maintenance.php';
39 
45 class MoveBatch extends Maintenance {
46  public function __construct() {
47  parent::__construct();
48  $this->addDescription( 'Moves a batch of pages' );
49  $this->addOption( 'u', "User to perform move", false, true );
50  $this->addOption( 'r', "Reason to move page", false, true );
51  $this->addOption( 'i', "Interval to sleep between moves" );
52  $this->addOption( 'noredirects', "Suppress creation of redirects" );
53  $this->addArg( 'listfile', 'List of pages to move, newline delimited', false );
54  }
55 
56  public function execute() {
58 
59  # Change to current working directory
60  $oldCwd = getcwd();
61  chdir( $oldCwd );
62 
63  # Options processing
64  $user = $this->getOption( 'u', false );
65  $reason = $this->getOption( 'r', '' );
66  $interval = $this->getOption( 'i', 0 );
67  $noredirects = $this->getOption( 'noredirects', false );
68  if ( $this->hasArg() ) {
69  $file = fopen( $this->getArg(), 'r' );
70  } else {
71  $file = $this->getStdin();
72  }
73 
74  # Setup
75  if ( !$file ) {
76  $this->error( "Unable to read file, exiting", true );
77  }
78  if ( $user === false ) {
79  $wgUser = User::newSystemUser( 'Move page script', [ 'steal' => true ] );
80  } else {
81  $wgUser = User::newFromName( $user );
82  }
83  if ( !$wgUser ) {
84  $this->error( "Invalid username", true );
85  }
86 
87  # Setup complete, now start
88  $dbw = $this->getDB( DB_MASTER );
89  // @codingStandardsIgnoreStart Ignore avoid function calls in a FOR loop test part warning
90  for ( $linenum = 1; !feof( $file ); $linenum++ ) {
91  // @codingStandardsIgnoreEnd
92  $line = fgets( $file );
93  if ( $line === false ) {
94  break;
95  }
96  $parts = array_map( 'trim', explode( '|', $line ) );
97  if ( count( $parts ) != 2 ) {
98  $this->error( "Error on line $linenum, no pipe character" );
99  continue;
100  }
101  $source = Title::newFromText( $parts[0] );
102  $dest = Title::newFromText( $parts[1] );
103  if ( is_null( $source ) || is_null( $dest ) ) {
104  $this->error( "Invalid title on line $linenum" );
105  continue;
106  }
107 
108  $this->output( $source->getPrefixedText() . ' --> ' . $dest->getPrefixedText() );
109  $this->beginTransaction( $dbw, __METHOD__ );
110  $mp = new MovePage( $source, $dest );
111  $status = $mp->move( $wgUser, $reason, !$noredirects );
112  if ( !$status->isOK() ) {
113  $this->output( "\nFAILED: " . $status->getWikiText( false, false, 'en' ) );
114  }
115  $this->commitTransaction( $dbw, __METHOD__ );
116  $this->output( "\n" );
117 
118  if ( $interval ) {
119  sleep( $interval );
120  }
121  wfWaitForSlaves();
122  }
123  }
124 }
125 
126 $maintClass = "MoveBatch";
127 require_once RUN_MAINTENANCE_IF_MAIN;
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:525
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
wfWaitForSlaves($ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Handles the backend logic of moving a page from one title to another.
Definition: MovePage.php:30
addArg($arg, $description, $required=true)
Add some args that are needed.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$source
getDB($db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
const DB_MASTER
Definition: defines.php:23
getStdin($len=null)
Return input from stdin.
addOption($name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
$maintClass
Definition: moveBatch.php:126
static newSystemUser($name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:653
hasArg($argId=0)
Does a given argument exist?
__construct()
Definition: moveBatch.php:46
addDescription($text)
Set the description text.
getOption($name, $default=null)
Get an option, or return the default.
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
output($out, $channel=null)
Throw some output to the user.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Maintenance script to move a batch of pages.
Definition: moveBatch.php:45
$line
Definition: cdb.php:59
error($err, $die=0)
Throw an error to the user.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1046
getArg($argId=0, $default=null)
Get an argument.
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
$wgUser
Definition: Setup.php:806