00001 <?php
00008
00009 define( 'DO_MAINTENANCE', dirname( __FILE__ ) . '/doMaintenance.php' );
00010 $maintClass = false;
00011
00012
00013 if( version_compare( PHP_VERSION, '5.0.0' ) < 0 ) {
00014 echo( "Sorry! This version of MediaWiki requires PHP 5; you are running " .
00015 PHP_VERSION . ".\n\n" .
00016 "If you are sure you already have PHP 5 installed, it may be installed\n" .
00017 "in a different path from PHP 4. Check with your system administrator.\n" );
00018 die();
00019 }
00020
00046 abstract class Maintenance {
00047
00052 const DB_NONE = 0;
00053 const DB_STD = 1;
00054 const DB_ADMIN = 2;
00055
00056
00057 const STDIN_ALL = 'all';
00058
00059
00060 protected $mParams = array();
00061
00062
00063 protected $mArgList = array();
00064
00065
00066 protected $mOptions = array();
00067
00068
00069 protected $mArgs = array();
00070
00071
00072 protected $mSelf;
00073
00074
00075 protected $mQuiet = false;
00076 protected $mDbUser, $mDbPass;
00077
00078
00079 protected $mDescription = '';
00080
00081
00082 protected $mInputLoaded = false;
00083
00084
00085
00086 protected $mBatchSize = null;
00087
00093 protected static $mCoreScripts = null;
00094
00099 public function __construct() {
00100 $this->addDefaultParams();
00101 register_shutdown_function( array( $this, 'outputChanneled' ), false );
00102 }
00103
00107 abstract public function execute();
00108
00118 protected function addOption( $name, $description, $required = false, $withArg = false ) {
00119 $this->mParams[$name] = array( 'desc' => $description, 'require' => $required, 'withArg' => $withArg );
00120 }
00121
00127 protected function hasOption( $name ) {
00128 return isset( $this->mOptions[$name] );
00129 }
00130
00137 protected function getOption( $name, $default = null ) {
00138 if( $this->hasOption( $name ) ) {
00139 return $this->mOptions[$name];
00140 } else {
00141
00142 $this->mOptions[$name] = $default;
00143 return $this->mOptions[$name];
00144 }
00145 }
00146
00153 protected function addArg( $arg, $description, $required = true ) {
00154 $this->mArgList[] = array(
00155 'name' => $arg,
00156 'desc' => $description,
00157 'require' => $required
00158 );
00159 }
00160
00166 protected function hasArg( $argId = 0 ) {
00167 return isset( $this->mArgs[$argId] );
00168 }
00169
00176 protected function getArg( $argId = 0, $default = null ) {
00177 return $this->hasArg( $argId ) ? $this->mArgs[$argId] : $default;
00178 }
00179
00184 protected function setBatchSize( $s = 0 ) {
00185 $this->mBatchSize = $s;
00186 }
00187
00192 public function getName() {
00193 return $this->mSelf;
00194 }
00195
00203 protected function getStdin( $len = null ) {
00204 if ( $len == Maintenance::STDIN_ALL )
00205 return file_get_contents( 'php://stdin' );
00206 $f = fopen( 'php://stdin', 'rt' );
00207 if( !$len )
00208 return $f;
00209 $input = fgets( $f, $len );
00210 fclose( $f );
00211 return rtrim( $input );
00212 }
00213
00220 protected function output( $out, $channel = null ) {
00221 if( $this->mQuiet ) {
00222 return;
00223 }
00224 if ( $channel === null ) {
00225 $f = fopen( 'php://stdout', 'w' );
00226 fwrite( $f, $out );
00227 fclose( $f );
00228 }
00229 else {
00230 $out = preg_replace( '/\n\z/', '', $out );
00231 $this->outputChanneled( $out, $channel );
00232 }
00233 }
00234
00241 protected function error( $err, $die = false ) {
00242 $this->outputChanneled( false );
00243 if ( php_sapi_name() == 'cli' ) {
00244 fwrite( STDERR, $err . "\n" );
00245 } else {
00246 $f = fopen( 'php://stderr', 'w' );
00247 fwrite( $f, $err . "\n" );
00248 fclose( $f );
00249 }
00250 if( $die ) die();
00251 }
00252
00253 private $atLineStart = true;
00254 private $lastChannel = null;
00255
00263 public function outputChanneled( $msg, $channel = null ) {
00264 $handle = fopen( 'php://stdout', 'w' );
00265
00266 if ( $msg === false ) {
00267
00268 if ( !$this->atLineStart ) fwrite( $handle, "\n" );
00269 fclose( $handle );
00270 return;
00271 }
00272
00273
00274 if ( !$this->atLineStart && $channel !== $this->lastChannel ) {
00275 fwrite( $handle, "\n" );
00276 }
00277
00278 fwrite( $handle, $msg );
00279
00280 $this->atLineStart = false;
00281 if ( $channel === null ) {
00282
00283 fwrite( $handle, "\n" );
00284 $this->atLineStart = true;
00285 }
00286 $this->lastChannel = $channel;
00287
00288
00289 fclose( $handle );
00290 }
00291
00302 public function getDbType() {
00303 return Maintenance::DB_STD;
00304 }
00305
00309 protected function addDefaultParams() {
00310 $this->addOption( 'help', "Display this help message" );
00311 $this->addOption( 'quiet', "Whether to supress non-error output" );
00312 $this->addOption( 'conf', "Location of LocalSettings.php, if not default", false, true );
00313 $this->addOption( 'wiki', "For specifying the wiki ID", false, true );
00314 $this->addOption( 'globals', "Output globals at the end of processing for debugging" );
00315 $this->addOption( 'server', "The protocol and server name to use in URLs, e.g.\n" .
00316 "\t\thttp://en.wikipedia.org. This is sometimes necessary because\n" .
00317 "\t\tserver name detection may fail in command line scripts.", false, true );
00318
00319 if( $this->getDbType() > 0 ) {
00320 $this->addOption( 'dbuser', "The DB user to use for this script", false, true );
00321 $this->addOption( 'dbpass', "The password to use for this script", false, true );
00322 }
00323
00324 if( $this->mBatchSize ) {
00325 $this->addOption( 'batch-size', 'Run this many operations ' .
00326 'per batch, default: ' . $this->mBatchSize , false, true );
00327 }
00328 }
00329
00337 protected function runChild( $maintClass, $classFile = null ) {
00338
00339
00340 self::disableSetup();
00341
00342
00343 if( !class_exists( $maintClass ) ) {
00344 if( $classFile ) {
00345 require_once( $classFile );
00346 }
00347 if( !class_exists( $maintClass ) ) {
00348 $this->error( "Cannot spawn child: $maintClass" );
00349 }
00350 }
00351
00352 $child = new $maintClass();
00353 $child->loadParamsAndArgs( $this->mSelf, $this->mOptions, $this->mArgs );
00354 return $child;
00355 }
00356
00360 protected static function disableSetup() {
00361 if( !defined( 'MW_NO_SETUP' ) )
00362 define( 'MW_NO_SETUP', true );
00363 }
00364
00368 public function setup() {
00369 global $IP, $wgCommandLineMode, $wgRequestTime;
00370
00371 # Abort if called from a web server
00372 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
00373 $this->error( "This script must be run from the command line", true );
00374 }
00375
00376 # Make sure we can handle script parameters
00377 if( !ini_get( 'register_argc_argv' ) ) {
00378 $this->error( "Cannot get command line arguments, register_argc_argv is set to false", true );
00379 }
00380
00381 if( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
00382
00383
00384
00385 if( ini_get( 'display_errors' ) ) {
00386 ini_set( 'display_errors', 'stderr' );
00387 }
00388
00389
00390
00391
00392
00393
00394 }
00395
00396 # Set the memory limit
00397 # Note we need to set it again later in cache LocalSettings changed it
00398 ini_set( 'memory_limit', $this->memoryLimit() );
00399
00400 # Set max execution time to 0 (no limit). PHP.net says that
00401 # "When running PHP from the command line the default setting is 0."
00402 # But sometimes this doesn't seem to be the case.
00403 ini_set( 'max_execution_time', 0 );
00404
00405 $wgRequestTime = microtime( true );
00406
00407 # Define us as being in MediaWiki
00408 define( 'MEDIAWIKI', true );
00409
00410 # Setup $IP, using MW_INSTALL_PATH if it exists
00411 $IP = strval( getenv( 'MW_INSTALL_PATH' ) ) !== ''
00412 ? getenv( 'MW_INSTALL_PATH' )
00413 : realpath( dirname( __FILE__ ) . '/..' );
00414
00415 $wgCommandLineMode = true;
00416 # Turn off output buffering if it's on
00417 @ob_end_flush();
00418
00419 $this->loadParamsAndArgs();
00420 $this->maybeHelp();
00421 $this->validateParamsAndArgs();
00422 }
00423
00429 public function memoryLimit() {
00430 return -1;
00431 }
00432
00436 public function clearParamsAndArgs() {
00437 $this->mOptions = array();
00438 $this->mArgs = array();
00439 $this->mInputLoaded = false;
00440 }
00441
00451 public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) {
00452 # If we were given opts or args, set those and return early
00453 if( $self ) {
00454 $this->mSelf = $self;
00455 $this->mInputLoaded = true;
00456 }
00457 if( $opts ) {
00458 $this->mOptions = $opts;
00459 $this->mInputLoaded = true;
00460 }
00461 if( $args ) {
00462 $this->mArgs = $args;
00463 $this->mInputLoaded = true;
00464 }
00465
00466 # If we've already loaded input (either by user values or from $argv)
00467 # skip on loading it again. The array_shift() will corrupt values if
00468 # it's run again and again
00469 if( $this->mInputLoaded ) {
00470 $this->loadSpecialVars();
00471 return;
00472 }
00473
00474 global $argv;
00475 $this->mSelf = array_shift( $argv );
00476
00477 $options = array();
00478 $args = array();
00479
00480 # Parse arguments
00481 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
00482 if ( $arg == '--' ) {
00483 # End of options, remainder should be considered arguments
00484 $arg = next( $argv );
00485 while( $arg !== false ) {
00486 $args[] = $arg;
00487 $arg = next( $argv );
00488 }
00489 break;
00490 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
00491 # Long options
00492 $option = substr( $arg, 2 );
00493 if ( isset( $this->mParams[$option] ) && $this->mParams[$option]['withArg'] ) {
00494 $param = next( $argv );
00495 if ( $param === false ) {
00496 $this->error( "\nERROR: $option needs a value after it\n" );
00497 $this->maybeHelp( true );
00498 }
00499 $options[$option] = $param;
00500 } else {
00501 $bits = explode( '=', $option, 2 );
00502 if( count( $bits ) > 1 ) {
00503 $option = $bits[0];
00504 $param = $bits[1];
00505 } else {
00506 $param = 1;
00507 }
00508 $options[$option] = $param;
00509 }
00510 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
00511 # Short options
00512 for ( $p=1; $p<strlen( $arg ); $p++ ) {
00513 $option = $arg{$p};
00514 if ( isset( $this->mParams[$option]['withArg'] ) && $this->mParams[$option]['withArg'] ) {
00515 $param = next( $argv );
00516 if ( $param === false ) {
00517 $this->error( "\nERROR: $option needs a value after it\n" );
00518 $this->maybeHelp( true );
00519 }
00520 $options[$option] = $param;
00521 } else {
00522 $options[$option] = 1;
00523 }
00524 }
00525 } else {
00526 $args[] = $arg;
00527 }
00528 }
00529
00530 $this->mOptions = $options;
00531 $this->mArgs = $args;
00532 $this->loadSpecialVars();
00533 $this->mInputLoaded = true;
00534 }
00535
00539 protected function validateParamsAndArgs() {
00540 $die = false;
00541 # Check to make sure we've got all the required options
00542 foreach( $this->mParams as $opt => $info ) {
00543 if( $info['require'] && !$this->hasOption( $opt ) ) {
00544 $this->error( "Param $opt required!" );
00545 $die = true;
00546 }
00547 }
00548 # Check arg list too
00549 foreach( $this->mArgList as $k => $info ) {
00550 if( $info['require'] && !$this->hasArg($k) ) {
00551 $this->error( "Argument <" . $info['name'] . "> required!" );
00552 $die = true;
00553 }
00554 }
00555
00556 if( $die ) $this->maybeHelp( true );
00557 }
00558
00562 protected function loadSpecialVars() {
00563 if( $this->hasOption( 'dbuser' ) )
00564 $this->mDbUser = $this->getOption( 'dbuser' );
00565 if( $this->hasOption( 'dbpass' ) )
00566 $this->mDbPass = $this->getOption( 'dbpass' );
00567 if( $this->hasOption( 'quiet' ) )
00568 $this->mQuiet = true;
00569 if( $this->hasOption( 'batch-size' ) )
00570 $this->mBatchSize = $this->getOption( 'batch-size' );
00571 }
00572
00577 protected function maybeHelp( $force = false ) {
00578 $screenWidth = 80;
00579 $tab = " ";
00580 $descWidth = $screenWidth - ( 2 * strlen( $tab ) );
00581
00582 ksort( $this->mParams );
00583 if( $this->hasOption( 'help' ) || $force ) {
00584 $this->mQuiet = false;
00585
00586 if( $this->mDescription ) {
00587 $this->output( "\n" . $this->mDescription . "\n" );
00588 }
00589 $output = "\nUsage: php " . basename( $this->mSelf );
00590 if( $this->mParams ) {
00591 $output .= " [--" . implode( array_keys( $this->mParams ), "|--" ) . "]";
00592 }
00593 if( $this->mArgList ) {
00594 $output .= " <";
00595 foreach( $this->mArgList as $k => $arg ) {
00596 $output .= $arg['name'] . ">";
00597 if( $k < count( $this->mArgList ) - 1 )
00598 $output .= " <";
00599 }
00600 }
00601 $this->output( "$output\n" );
00602 foreach( $this->mParams as $par => $info ) {
00603 $this->output( wordwrap( "$tab$par : " . $info['desc'], $descWidth,
00604 "\n$tab$tab" ) . "\n" );
00605 }
00606 foreach( $this->mArgList as $info ) {
00607 $this->output( wordwrap( "$tab<" . $info['name'] . "> : " .
00608 $info['desc'], $descWidth, "\n$tab$tab" ) . "\n" );
00609 }
00610 die( 1 );
00611 }
00612 }
00613
00617 public function finalSetup() {
00618 global $wgCommandLineMode, $wgShowSQLErrors, $wgServer;
00619 global $wgTitle, $wgProfiling, $IP, $wgDBadminuser, $wgDBadminpassword;
00620 global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
00621
00622 # Turn off output buffering again, it might have been turned on in the settings files
00623 if( ob_get_level() ) {
00624 ob_end_flush();
00625 }
00626 # Same with these
00627 $wgCommandLineMode = true;
00628
00629 # Override $wgServer
00630 if( $this->hasOption( 'server') ) {
00631 $wgServer = $this->getOption( 'server', $wgServer );
00632 }
00633
00634 # If these were passed, use them
00635 if( $this->mDbUser )
00636 $wgDBadminuser = $this->mDbUser;
00637 if( $this->mDbPass )
00638 $wgDBadminpassword = $this->mDbPass;
00639
00640 if ( $this->getDbType() == self::DB_ADMIN && isset( $wgDBadminuser ) ) {
00641 $wgDBuser = $wgDBadminuser;
00642 $wgDBpassword = $wgDBadminpassword;
00643
00644 if( $wgDBservers ) {
00645 foreach ( $wgDBservers as $i => $server ) {
00646 $wgDBservers[$i]['user'] = $wgDBuser;
00647 $wgDBservers[$i]['password'] = $wgDBpassword;
00648 }
00649 }
00650 if( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
00651 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
00652 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
00653 }
00654 }
00655
00656 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
00657 $fn = MW_CMDLINE_CALLBACK;
00658 $fn();
00659 }
00660
00661 $wgShowSQLErrors = true;
00662 @set_time_limit( 0 );
00663 ini_set( 'memory_limit', $this->memoryLimit() );
00664
00665 $wgProfiling = false;
00666 }
00667
00672 public function globals() {
00673 if( $this->hasOption( 'globals' ) ) {
00674 print_r( $GLOBALS );
00675 }
00676 }
00677
00681 public function loadWikimediaSettings() {
00682 global $IP, $wgNoDBParam, $wgUseNormalUser, $wgConf, $site, $lang;
00683
00684 if ( empty( $wgNoDBParam ) ) {
00685 # Check if we were passed a db name
00686 if ( isset( $this->mOptions['wiki'] ) ) {
00687 $db = $this->mOptions['wiki'];
00688 } else {
00689 $db = array_shift( $this->mArgs );
00690 }
00691 list( $site, $lang ) = $wgConf->siteFromDB( $db );
00692
00693 # If not, work out the language and site the old way
00694 if ( is_null( $site ) || is_null( $lang ) ) {
00695 if ( !$db ) {
00696 $lang = 'aa';
00697 } else {
00698 $lang = $db;
00699 }
00700 if ( isset( $this->mArgs[0] ) ) {
00701 $site = array_shift( $this->mArgs );
00702 } else {
00703 $site = 'wikipedia';
00704 }
00705 }
00706 } else {
00707 $lang = 'aa';
00708 $site = 'wikipedia';
00709 }
00710
00711 # This is for the IRC scripts, which now run as the apache user
00712 # The apache user doesn't have access to the wikiadmin_pass command
00713 if ( $_ENV['USER'] == 'apache' ) {
00714 #if ( posix_geteuid() == 48 ) {
00715 $wgUseNormalUser = true;
00716 }
00717
00718 putenv( 'wikilang=' . $lang );
00719
00720 $DP = $IP;
00721 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
00722
00723 if ( $lang == 'test' && $site == 'wikipedia' ) {
00724 define( 'TESTWIKI', 1 );
00725 }
00726 }
00727
00732 public function loadSettings() {
00733 global $wgWikiFarm, $wgCommandLineMode, $IP, $DP;
00734
00735 $wgWikiFarm = false;
00736 if ( isset( $this->mOptions['conf'] ) ) {
00737 $settingsFile = $this->mOptions['conf'];
00738 } else {
00739 $settingsFile = "$IP/LocalSettings.php";
00740 }
00741 if ( isset( $this->mOptions['wiki'] ) ) {
00742 $bits = explode( '-', $this->mOptions['wiki'] );
00743 if ( count( $bits ) == 1 ) {
00744 $bits[] = '';
00745 }
00746 define( 'MW_DB', $bits[0] );
00747 define( 'MW_PREFIX', $bits[1] );
00748 }
00749
00750 if ( !is_readable( $settingsFile ) ) {
00751 $this->error( "A copy of your installation's LocalSettings.php\n" .
00752 "must exist and be readable in the source directory.", true );
00753 }
00754 $wgCommandLineMode = true;
00755 $DP = $IP;
00756 return $settingsFile;
00757 }
00758
00764 protected function purgeRedundantText( $delete = true ) {
00765 # Data should come off the master, wrapped in a transaction
00766 $dbw = wfGetDB( DB_MASTER );
00767 $dbw->begin();
00768
00769 $tbl_arc = $dbw->tableName( 'archive' );
00770 $tbl_rev = $dbw->tableName( 'revision' );
00771 $tbl_txt = $dbw->tableName( 'text' );
00772
00773 # Get "active" text records from the revisions table
00774 $this->output( "Searching for active text records in revisions table..." );
00775 $res = $dbw->query( "SELECT DISTINCT rev_text_id FROM $tbl_rev" );
00776 foreach( $res as $row ) {
00777 $cur[] = $row->rev_text_id;
00778 }
00779 $this->output( "done.\n" );
00780
00781 # Get "active" text records from the archive table
00782 $this->output( "Searching for active text records in archive table..." );
00783 $res = $dbw->query( "SELECT DISTINCT ar_text_id FROM $tbl_arc" );
00784 foreach( $res as $row ) {
00785 $cur[] = $row->ar_text_id;
00786 }
00787 $this->output( "done.\n" );
00788
00789 # Get the IDs of all text records not in these sets
00790 $this->output( "Searching for inactive text records..." );
00791 $set = implode( ', ', $cur );
00792 $res = $dbw->query( "SELECT old_id FROM $tbl_txt WHERE old_id NOT IN ( $set )" );
00793 $old = array();
00794 foreach( $res as $row ) {
00795 $old[] = $row->old_id;
00796 }
00797 $this->output( "done.\n" );
00798
00799 # Inform the user of what we're going to do
00800 $count = count( $old );
00801 $this->output( "$count inactive items found.\n" );
00802
00803 # Delete as appropriate
00804 if( $delete && $count ) {
00805 $this->output( "Deleting..." );
00806 $set = implode( ', ', $old );
00807 $dbw->query( "DELETE FROM $tbl_txt WHERE old_id IN ( $set )" );
00808 $this->output( "done.\n" );
00809 }
00810
00811 # Done
00812 $dbw->commit();
00813 }
00814
00818 protected function getDir() {
00819 return dirname( __FILE__ );
00820 }
00821
00828 public static function getMaintenanceScripts() {
00829 global $wgMaintenanceScripts;
00830 return $wgMaintenanceScripts + self::getCoreScripts();
00831 }
00832
00837 protected static function getCoreScripts() {
00838 if( !self::$mCoreScripts ) {
00839 self::disableSetup();
00840 $paths = array(
00841 dirname( __FILE__ ),
00842 dirname( __FILE__ ) . '/gearman',
00843 dirname( __FILE__ ) . '/language',
00844 dirname( __FILE__ ) . '/storage',
00845 );
00846 self::$mCoreScripts = array();
00847 foreach( $paths as $p ) {
00848 $handle = opendir( $p );
00849 while( ( $file = readdir( $handle ) ) !== false ) {
00850 if( $file == 'Maintenance.php' )
00851 continue;
00852 $file = $p . '/' . $file;
00853 if( is_dir( $file ) || !strpos( $file, '.php' ) ||
00854 ( strpos( file_get_contents( $file ), '$maintClass' ) === false ) ) {
00855 continue;
00856 }
00857 require( $file );
00858 $vars = get_defined_vars();
00859 if( array_key_exists( 'maintClass', $vars ) ) {
00860 self::$mCoreScripts[$vars['maintClass']] = $file;
00861 }
00862 }
00863 closedir( $handle );
00864 }
00865 }
00866 return self::$mCoreScripts;
00867 }
00868 }