Merge maintenance-work branch:
[lhc/web/wiklou.git] / maintenance / Maintenance.php
1 <?php
2 // Define this so scripts can easily find doMaintenance.php
3 define( 'DO_MAINTENANCE', dirname(__FILE__) . '/doMaintenance.php' );
4
5 /**
6 * Abstract maintenance class for quickly writing and churning out
7 * maintenance scripts with minimal effort. All that _must_ be defined
8 * is the execute() method. See docs/maintenance.txt for more info
9 * and a quick demo of how to use it.
10 *
11 * @author Chad Horohoe <chad@anyonecanedit.org>
12 * @since 1.16
13 * @ingroup Maintenance
14 */
15 abstract class Maintenance {
16
17 /**
18 * Constants for DB access type
19 * @see Maintenance::getDbType()
20 */
21 const NO_DB = 0;
22 const NORMAL_DB = 1;
23 const ADMIN_DB = 2;
24
25 // This is the desired params
26 private $mParams = array();
27
28 // Array of desired args
29 private $mArgList = array();
30
31 // This is the list of options that were actually passed
32 private $mOptions = array();
33
34 // This is the list of arguments that were actually passed
35 protected $mArgs = array();
36
37 // Name of the script currently running
38 protected $mSelf;
39
40 // Special vars for params that are always used
41 private $mQuiet = false;
42 private $mDbUser, $mDbPass;
43
44 // A description of the script, children should change this
45 protected $mDescription = '';
46
47 // Have we already loaded our user input?
48 private $inputLoaded = false;
49
50 // Batch size
51 protected $mBatchSize = 100;
52
53 /**
54 * Default constructor. Children should call this if implementing
55 * their own constructors
56 */
57 public function __construct() {
58 $this->addDefaultParams();
59 }
60
61 /**
62 * Do the actual work. All child classes will need to implement this
63 */
64 abstract public function execute();
65
66 /**
67 * Add a parameter to the script. Will be displayed on --help
68 * with the associated description
69 *
70 * @param $name String The name of the param (help, version, etc)
71 * @param $description String The description of the param to show on --help
72 * @param $required boolean Is the param required?
73 * @param $withArg Boolean Is an argument required with this option?
74 */
75 protected function addParam( $name, $description, $required = false, $withArg = false ) {
76 $this->mParams[ $name ] = array( 'desc' => $description, 'require' => $required, 'withArg' => $withArg );
77 }
78
79 /**
80 * Checks to see if a particular param exists.
81 * @param $name String The name of the param
82 * @return boolean
83 */
84 protected function hasOption( $name ) {
85 return isset( $this->mOptions[ $name ] );
86 }
87
88 /**
89 * Get an option, or return the default
90 * @param $name String The name of the param
91 * @param $default mixed Anything you want, default null
92 * @return mixed
93 */
94 protected function getOption( $name, $default = null ) {
95 if( $this->hasOption($name) ) {
96 return $this->mOptions[$name];
97 } else {
98 // Set it so we don't have to provide the default again
99 $this->mOptions[$name] = $default;
100 return $this->mOptions[$name];
101 }
102 }
103
104 /**
105 * Add some args that are needed. Used in formatting help
106 */
107 protected function addArgs( $args ) {
108 $this->mArgList = array_merge( $this->mArgList, $args );
109 }
110
111 /**
112 * Does a given argument exist?
113 * @param $argId int The integer value (from zero) for the arg
114 * @return boolean
115 */
116 protected function hasArg( $argId = 0 ) {
117 return isset( $this->mArgs[ $argId ] ) ;
118 }
119
120 /**
121 * Get an argument.
122 * @param $argId int The integer value (from zero) for the arg
123 * @param $default mixed The default if it doesn't exist
124 * @return mixed
125 */
126 protected function getArg( $argId = 0, $default = null ) {
127 return $this->hasArg($name) ? $this->mArgs[$name] : $default;
128 }
129
130 /**
131 * Set the batch size.
132 * @param $s int The number of operations to do in a batch
133 */
134 protected function setBatchSize( $s = 0 ) {
135 $this->mBatchSize = $s;
136 }
137
138 /**
139 * Return input from stdin.
140 * @param $length int The number of bytes to read. If null,
141 * just return the handle
142 * @return mixed
143 */
144 protected function getStdin( $len = null ) {
145 $f = fopen( 'php://stdin', 'rt' );
146 if( !$len ) {
147 return $f;
148 }
149 $input = fgets( $f, $len );
150 fclose ( $f );
151 return rtrim( $input );
152 }
153
154 /**
155 * Throw some output to the user. Scripts can call this with no fears,
156 * as we handle all --quiet stuff here
157 * @param $out String The text to show to the user
158 */
159 protected function output( $out ) {
160 if( $this->mQuiet ) {
161 return;
162 }
163 $f = fopen( 'php://stdout', 'w' );
164 fwrite( $f, $out );
165 fclose( $f );
166 }
167
168 /**
169 * Throw an error to the user. Doesn't respect --quiet, so don't use
170 * this for non-error output
171 * @param $err String The error to display
172 * @param $die boolean If true, go ahead and die out.
173 */
174 protected function error( $err, $die = false ) {
175 $f = fopen( 'php://stderr', 'w' );
176 fwrite( $f, $err );
177 fclose( $f );
178 if( $die ) die();
179 }
180
181 /**
182 * Does the script need normal DB access? By default, we give Maintenance
183 * scripts admin rights to the DB (when available). Sometimes, a script needs
184 * normal access for a reason and sometimes they want no access. Subclasses
185 * should override and return one of the following values, as needed:
186 * Maintenance::NO_DB - For no DB access at all
187 * Maintenance::NORMAL_DB - For normal DB access
188 * Maintenance::ADMIN_DB - For admin DB access, default
189 * @return int
190 */
191 protected function getDbType() {
192 return Maintenance :: ADMIN_DB;
193 }
194
195 /**
196 * Add the default parameters to the scripts
197 */
198 private function addDefaultParams() {
199 $this->addParam( 'help', "Display this help message" );
200 $this->addParam( 'quiet', "Whether to supress non-error output" );
201 $this->addParam( 'conf', "Location of LocalSettings.php, if not default", false, true );
202 $this->addParam( 'wiki', "For specifying the wiki ID", false, true );
203 if( $this->getDbType() > 0 ) {
204 $this->addParam( 'dbuser', "The DB user to use for this script", false, true );
205 $this->addParam( 'dbpass', "The password to use for this script", false, true );
206 }
207 }
208
209 /**
210 * Spawn a child maintenance script. Pass all of the current arguments
211 * to it.
212 * @param $maintClass String A name of a child maintenance class
213 * @param $classFile String Full path of where the child is
214 * @return Maintenance child
215 */
216 protected function spawnChild( $maintClass, $classFile = null ) {
217 // If we haven't already specified, kill setup procedures
218 // for child scripts, we've already got a sane environment
219 if( !defined( 'MW_NO_SETUP' ) ) {
220 define( 'MW_NO_SETUP', true );
221 }
222
223 // Make sure the class is loaded first
224 if( !class_exists( $maintClass ) ) {
225 if( $classFile ) {
226 require_once( $classFile );
227 }
228 if( !class_exists( $maintClass ) ) {
229 $this->error( "Cannot spawn child: $maintClass\n" );
230 }
231 }
232
233 $child = new $maintClass();
234 $child->loadParamsAndArgs( $this->mSelf, $this->mOptions, $this->mArgs );
235 return $child;
236 }
237
238 /**
239 * Do some sanity checking and basic setup
240 */
241 public function setup() {
242 global $IP, $wgCommandLineMode, $wgUseNormalUser, $wgRequestTime;
243
244 # Abort if called from a web server
245 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
246 $this->error( "This script must be run from the command line\n", true );
247 }
248
249 # Make sure we can handle script parameters
250 if( !ini_get( 'register_argc_argv' ) ) {
251 $this->error( "Cannot get command line arguments, register_argc_argv is set to false", true );
252 }
253
254 # Make sure we're on PHP5 or better
255 if( version_compare( PHP_VERSION, '5.0.0' ) < 0 ) {
256 $this->error( "Sorry! This version of MediaWiki requires PHP 5; you are running " .
257 PHP_VERSION . ".\n\n" .
258 "If you are sure you already have PHP 5 installed, it may be installed\n" .
259 "in a different path from PHP 4. Check with your system administrator.\n", true );
260 }
261
262 if( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
263 // Send PHP warnings and errors to stderr instead of stdout.
264 // This aids in diagnosing problems, while keeping messages
265 // out of redirected output.
266 if( ini_get( 'display_errors' ) ) {
267 ini_set( 'display_errors', 'stderr' );
268 }
269
270 // Don't touch the setting on earlier versions of PHP,
271 // as setting it would disable output if you'd wanted it.
272
273 // Note that exceptions are also sent to stderr when
274 // command-line mode is on, regardless of PHP version.
275 }
276
277 # Set the memory limit
278 ini_set( 'memory_limit', -1 );
279
280 $wgRequestTime = microtime(true);
281
282 # Define us as being in Mediawiki
283 define( 'MEDIAWIKI', true );
284
285 # Setup $IP, using MW_INSTALL_PATH if it exists
286 $IP = strval( getenv('MW_INSTALL_PATH') ) !== ''
287 ? getenv('MW_INSTALL_PATH')
288 : realpath( dirname( __FILE__ ) . '/..' );
289
290 $wgCommandLineMode = true;
291 # Turn off output buffering if it's on
292 @ob_end_flush();
293
294 if (!isset( $wgUseNormalUser ) ) {
295 $wgUseNormalUser = false;
296 }
297
298 $this->loadParamsAndArgs();
299 $this->maybeHelp();
300 }
301
302 /**
303 * Clear all params and arguments.
304 */
305 public function clearParamsAndArgs() {
306 $this->mOptions = array();
307 $this->mArgs = array();
308 $this->inputLoaded = false;
309 }
310
311 /**
312 * Process command line arguments
313 * $mOptions becomes an array with keys set to the option names
314 * $mArgs becomes a zero-based array containing the non-option arguments
315 *
316 * @param $self String The name of the script, if any
317 * @param $opts Array An array of options, in form of key=>value
318 * @param $args Array An array of command line arguments
319 */
320 public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) {
321 # If we were given opts or args, set those and return early
322 if( $self ) {
323 $this->mSelf = $self;
324 $this->inputLoaded = true;
325 }
326 if( $opts ) {
327 $this->mOptions = $opts;
328 $this->inputLoaded = true;
329 }
330 if( $args ) {
331 $this->mArgs = $args;
332 $this->inputLoaded = true;
333 }
334
335 # If we've already loaded input (either by user values or from $argv)
336 # skip on loading it again. The array_shift() will corrupt values if
337 # it's run again and again
338 if( $this->inputLoaded ) {
339 $this->loadSpecialVars();
340 return;
341 }
342
343 global $argv;
344 $this->mSelf = array_shift( $argv );
345
346 $options = array();
347 $args = array();
348
349 # Parse arguments
350 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
351 if ( $arg == '--' ) {
352 # End of options, remainder should be considered arguments
353 $arg = next( $argv );
354 while( $arg !== false ) {
355 $args[] = $arg;
356 $arg = next( $argv );
357 }
358 break;
359 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
360 # Long options
361 $option = substr( $arg, 2 );
362 if ( isset( $this->mParams[$option] ) && $this->mParams[$option]['withArg'] ) {
363 $param = next( $argv );
364 if ( $param === false ) {
365 $this->error( "$arg needs a value after it\n", true );
366 }
367 $options[$option] = $param;
368 } else {
369 $bits = explode( '=', $option, 2 );
370 if( count( $bits ) > 1 ) {
371 $option = $bits[0];
372 $param = $bits[1];
373 } else {
374 $param = 1;
375 }
376 $options[$option] = $param;
377 }
378 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
379 # Short options
380 for ( $p=1; $p<strlen( $arg ); $p++ ) {
381 $option = $arg{$p};
382 if ( isset( $this->mParams[$option]['withArg'] ) ) {
383 $param = next( $argv );
384 if ( $param === false ) {
385 $this->error( "$arg needs a value after it\n", true );
386 }
387 $options[$option] = $param;
388 } else {
389 $options[$option] = 1;
390 }
391 }
392 } else {
393 $args[] = $arg;
394 }
395 }
396
397 # Check to make sure we've got all the required ones
398 foreach( $this->mParams as $opt => $info ) {
399 if( $info['require'] && !$this->hasOption($opt) ) {
400 $this->error( "Param $opt required.\n", true );
401 }
402 }
403
404 # Also make sure we've got enough arguments
405 if ( count( $args ) < count( $this->mArgList ) ) {
406 $this->error( "Not enough arguments passed", true );
407 }
408
409 $this->mOptions = $options;
410 $this->mArgs = $args;
411 $this->loadSpecialVars();
412 $this->inputLoaded = true;
413 }
414
415 /**
416 * Handle the special variables that are global to all scripts
417 */
418 private function loadSpecialVars() {
419 if( $this->hasOption( 'dbuser' ) )
420 $this->mDbUser = $this->getOption( 'dbuser' );
421 if( $this->hasOption( 'dbpass' ) )
422 $this->mDbPass = $this->getOption( 'dbpass' );
423 if( $this->hasOption( 'quiet' ) )
424 $this->mQuiet = true;
425 }
426
427 /**
428 * Maybe show the help.
429 * @param $force boolean Whether to force the help to show, default false
430 */
431 private function maybeHelp( $force = false ) {
432 if( $this->hasOption('help') || in_array( 'help', $this->mArgs ) || $force ) {
433 $this->mQuiet = false;
434 if( $this->mDescription ) {
435 $this->output( $this->mDescription . "\n" );
436 }
437 $this->output( "\nUsage: php " . $this->mSelf );
438 if( $this->mParams ) {
439 $this->output( " [--" . implode( array_keys( $this->mParams ), "|--" ) . "]" );
440 }
441 if( $this->mArgList ) {
442 $this->output( " <" . implode( $this->mArgList, "> <" ) . ">" );
443 }
444 $this->output( "\n" );
445 foreach( $this->mParams as $par => $info ) {
446 $this->output( "\t$par : " . $info['desc'] . "\n" );
447 }
448 die( 1 );
449 }
450 }
451
452 /**
453 * Handle some last-minute setup here.
454 */
455 private function finalSetup() {
456 global $wgCommandLineMode, $wgUseNormalUser, $wgShowSQLErrors;
457 global $wgTitle, $wgProfiling, $IP, $wgDBadminuser, $wgDBadminpassword;
458 global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
459
460 # Turn off output buffering again, it might have been turned on in the settings files
461 if( ob_get_level() ) {
462 ob_end_flush();
463 }
464 # Same with these
465 $wgCommandLineMode = true;
466
467 # If these were passed, use them
468 if( $this->mDbUser )
469 $wgDBadminuser = $this->mDbUser;
470 if( $this->mDbPass )
471 $wgDBadminpass = $this->mDbPass;
472
473 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
474 $wgDBuser = $wgDBadminuser;
475 $wgDBpassword = $wgDBadminpassword;
476
477 if( $wgDBservers ) {
478 foreach ( $wgDBservers as $i => $server ) {
479 $wgDBservers[$i]['user'] = $wgDBuser;
480 $wgDBservers[$i]['password'] = $wgDBpassword;
481 }
482 }
483 if( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
484 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
485 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
486 }
487 }
488
489 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
490 $fn = MW_CMDLINE_CALLBACK;
491 $fn();
492 }
493
494 $wgShowSQLErrors = true;
495 @set_time_limit( 0 );
496
497 $wgProfiling = false; // only for Profiler.php mode; avoids OOM errors
498 }
499
500 /**
501 * Do setup specific to WMF
502 */
503 public function loadWikimediaSettings() {
504 global $IP, $wgNoDBParam, $wgUseNormalUser, $wgConf;
505
506 if ( empty( $wgNoDBParam ) ) {
507 # Check if we were passed a db name
508 if ( isset( $this->mOptions['wiki'] ) ) {
509 $db = $this->mOptions['wiki'];
510 } else {
511 $db = array_shift( $this->mArgs );
512 }
513 list( $site, $lang ) = $wgConf->siteFromDB( $db );
514
515 # If not, work out the language and site the old way
516 if ( is_null( $site ) || is_null( $lang ) ) {
517 if ( !$db ) {
518 $lang = 'aa';
519 } else {
520 $lang = $db;
521 }
522 if ( isset( $this->mArgs[0] ) ) {
523 $site = array_shift( $this->mArgs );
524 } else {
525 $site = 'wikipedia';
526 }
527 }
528 } else {
529 $lang = 'aa';
530 $site = 'wikipedia';
531 }
532
533 # This is for the IRC scripts, which now run as the apache user
534 # The apache user doesn't have access to the wikiadmin_pass command
535 if ( $_ENV['USER'] == 'apache' ) {
536 #if ( posix_geteuid() == 48 ) {
537 $wgUseNormalUser = true;
538 }
539
540 putenv( 'wikilang=' . $lang );
541
542 $DP = $IP;
543 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
544
545 if ( $lang == 'test' && $site == 'wikipedia' ) {
546 define( 'TESTWIKI', 1 );
547 }
548 }
549
550 /**
551 * Generic setup for most installs. Returns the location of LocalSettings
552 * @return String
553 */
554 public function loadSettings() {
555 global $wgWikiFarm, $wgCommandLineMode, $IP, $DP;
556
557 $wgWikiFarm = false;
558 if ( isset( $this->mOptions['conf'] ) ) {
559 $settingsFile = $this->mOptions['conf'];
560 } else {
561 $settingsFile = "$IP/LocalSettings.php";
562 }
563 if ( isset( $this->mOptions['wiki'] ) ) {
564 $bits = explode( '-', $this->mOptions['wiki'] );
565 if ( count( $bits ) == 1 ) {
566 $bits[] = '';
567 }
568 define( 'MW_DB', $bits[0] );
569 define( 'MW_PREFIX', $bits[1] );
570 }
571
572 if ( ! is_readable( $settingsFile ) ) {
573 $this->error( "A copy of your installation's LocalSettings.php\n" .
574 "must exist and be readable in the source directory.\n", true );
575 }
576 $wgCommandLineMode = true;
577 $DP = $IP;
578 $this->finalSetup();
579 return $settingsFile;
580 }
581
582 /**
583 * Support function for cleaning up redundant text records
584 * @param $delete boolean Whether or not to actually delete the records
585 * @author Rob Church <robchur@gmail.com>
586 */
587 protected function purgeRedundantText( $delete = true ) {
588 # Data should come off the master, wrapped in a transaction
589 $dbw = wfGetDB( DB_MASTER );
590 $dbw->begin();
591
592 $tbl_arc = $dbw->tableName( 'archive' );
593 $tbl_rev = $dbw->tableName( 'revision' );
594 $tbl_txt = $dbw->tableName( 'text' );
595
596 # Get "active" text records from the revisions table
597 $this->output( "Searching for active text records in revisions table..." );
598 $res = $dbw->query( "SELECT DISTINCT rev_text_id FROM $tbl_rev" );
599 while( $row = $dbw->fetchObject( $res ) ) {
600 $cur[] = $row->rev_text_id;
601 }
602 $this->output( "done.\n" );
603
604 # Get "active" text records from the archive table
605 $this->output( "Searching for active text records in archive table..." );
606 $res = $dbw->query( "SELECT DISTINCT ar_text_id FROM $tbl_arc" );
607 while( $row = $dbw->fetchObject( $res ) ) {
608 $cur[] = $row->ar_text_id;
609 }
610 $this->output( "done.\n" );
611
612 # Get the IDs of all text records not in these sets
613 $this->output( "Searching for inactive text records..." );
614 $set = implode( ', ', $cur );
615 $res = $dbw->query( "SELECT old_id FROM $tbl_txt WHERE old_id NOT IN ( $set )" );
616 $old = array();
617 while( $row = $dbw->fetchObject( $res ) ) {
618 $old[] = $row->old_id;
619 }
620 $this->output( "done.\n" );
621
622 # Inform the user of what we're going to do
623 $count = count( $old );
624 $this->output( "$count inactive items found.\n" );
625
626 # Delete as appropriate
627 if( $delete && $count ) {
628 $this->output( "Deleting..." );
629 $set = implode( ', ', $old );
630 $dbw->query( "DELETE FROM $tbl_txt WHERE old_id IN ( $set )" );
631 $this->output( "done.\n" );
632 }
633
634 # Done
635 $dbw->commit();
636
637 }
638 }
639