MediaWiki
REL1_21
|
00001 <?php 00023 // Make sure we're on PHP5.3.2 or better 00024 if ( !function_exists( 'version_compare' ) || version_compare( PHP_VERSION, '5.3.2' ) < 0 ) { 00025 // We need to use dirname( __FILE__ ) here cause __DIR__ is PHP5.3+ 00026 require_once( dirname( __FILE__ ) . '/../includes/PHPVersionError.php' ); 00027 wfPHPVersionError( 'cli' ); 00028 } 00029 00035 // Define this so scripts can easily find doMaintenance.php 00036 define( 'RUN_MAINTENANCE_IF_MAIN', __DIR__ . '/doMaintenance.php' ); 00037 define( 'DO_MAINTENANCE', RUN_MAINTENANCE_IF_MAIN ); // original name, harmless 00038 00039 $maintClass = false; 00040 00051 abstract class Maintenance { 00052 00057 const DB_NONE = 0; 00058 const DB_STD = 1; 00059 const DB_ADMIN = 2; 00060 00061 // Const for getStdin() 00062 const STDIN_ALL = 'all'; 00063 00064 // This is the desired params 00065 protected $mParams = array(); 00066 00067 // Array of mapping short parameters to long ones 00068 protected $mShortParamsMap = array(); 00069 00070 // Array of desired args 00071 protected $mArgList = array(); 00072 00073 // This is the list of options that were actually passed 00074 protected $mOptions = array(); 00075 00076 // This is the list of arguments that were actually passed 00077 protected $mArgs = array(); 00078 00079 // Name of the script currently running 00080 protected $mSelf; 00081 00082 // Special vars for params that are always used 00083 protected $mQuiet = false; 00084 protected $mDbUser, $mDbPass; 00085 00086 // A description of the script, children should change this 00087 protected $mDescription = ''; 00088 00089 // Have we already loaded our user input? 00090 protected $mInputLoaded = false; 00091 00098 protected $mBatchSize = null; 00099 00100 // Generic options added by addDefaultParams() 00101 private $mGenericParameters = array(); 00102 // Generic options which might or not be supported by the script 00103 private $mDependantParameters = array(); 00104 00109 private $mDb = null; 00110 00115 public $fileHandle; 00116 00122 protected static $mCoreScripts = null; 00123 00128 public function __construct() { 00129 // Setup $IP, using MW_INSTALL_PATH if it exists 00130 global $IP; 00131 $IP = strval( getenv( 'MW_INSTALL_PATH' ) ) !== '' 00132 ? getenv( 'MW_INSTALL_PATH' ) 00133 : realpath( __DIR__ . '/..' ); 00134 00135 $this->addDefaultParams(); 00136 register_shutdown_function( array( $this, 'outputChanneled' ), false ); 00137 } 00138 00146 public static function shouldExecute() { 00147 $bt = debug_backtrace(); 00148 $count = count( $bt ); 00149 if ( $count < 2 ) { 00150 return false; // sanity 00151 } 00152 if ( $bt[0]['class'] !== 'Maintenance' || $bt[0]['function'] !== 'shouldExecute' ) { 00153 return false; // last call should be to this function 00154 } 00155 $includeFuncs = array( 'require_once', 'require', 'include', 'include_once' ); 00156 for( $i=1; $i < $count; $i++ ) { 00157 if ( !in_array( $bt[$i]['function'], $includeFuncs ) ) { 00158 return false; // previous calls should all be "requires" 00159 } 00160 } 00161 return true; 00162 } 00163 00167 abstract public function execute(); 00168 00179 protected function addOption( $name, $description, $required = false, $withArg = false, $shortName = false ) { 00180 $this->mParams[$name] = array( 'desc' => $description, 'require' => $required, 'withArg' => $withArg, 'shortName' => $shortName ); 00181 if ( $shortName !== false ) { 00182 $this->mShortParamsMap[$shortName] = $name; 00183 } 00184 } 00185 00191 protected function hasOption( $name ) { 00192 return isset( $this->mOptions[$name] ); 00193 } 00194 00201 protected function getOption( $name, $default = null ) { 00202 if ( $this->hasOption( $name ) ) { 00203 return $this->mOptions[$name]; 00204 } else { 00205 // Set it so we don't have to provide the default again 00206 $this->mOptions[$name] = $default; 00207 return $this->mOptions[$name]; 00208 } 00209 } 00210 00217 protected function addArg( $arg, $description, $required = true ) { 00218 $this->mArgList[] = array( 00219 'name' => $arg, 00220 'desc' => $description, 00221 'require' => $required 00222 ); 00223 } 00224 00229 protected function deleteOption( $name ) { 00230 unset( $this->mParams[$name] ); 00231 } 00232 00237 protected function addDescription( $text ) { 00238 $this->mDescription = $text; 00239 } 00240 00246 protected function hasArg( $argId = 0 ) { 00247 return isset( $this->mArgs[$argId] ); 00248 } 00249 00256 protected function getArg( $argId = 0, $default = null ) { 00257 return $this->hasArg( $argId ) ? $this->mArgs[$argId] : $default; 00258 } 00259 00264 protected function setBatchSize( $s = 0 ) { 00265 $this->mBatchSize = $s; 00266 00267 // If we support $mBatchSize, show the option. 00268 // Used to be in addDefaultParams, but in order for that to 00269 // work, subclasses would have to call this function in the constructor 00270 // before they called parent::__construct which is just weird 00271 // (and really wasn't done). 00272 if ( $this->mBatchSize ) { 00273 $this->addOption( 'batch-size', 'Run this many operations ' . 00274 'per batch, default: ' . $this->mBatchSize, false, true ); 00275 if ( isset( $this->mParams['batch-size'] ) ) { 00276 // This seems a little ugly... 00277 $this->mDependantParameters['batch-size'] = $this->mParams['batch-size']; 00278 } 00279 } 00280 } 00281 00286 public function getName() { 00287 return $this->mSelf; 00288 } 00289 00297 protected function getStdin( $len = null ) { 00298 if ( $len == Maintenance::STDIN_ALL ) { 00299 return file_get_contents( 'php://stdin' ); 00300 } 00301 $f = fopen( 'php://stdin', 'rt' ); 00302 if ( !$len ) { 00303 return $f; 00304 } 00305 $input = fgets( $f, $len ); 00306 fclose( $f ); 00307 return rtrim( $input ); 00308 } 00309 00313 public function isQuiet() { 00314 return $this->mQuiet; 00315 } 00316 00324 protected function output( $out, $channel = null ) { 00325 if ( $this->mQuiet ) { 00326 return; 00327 } 00328 if ( $channel === null ) { 00329 $this->cleanupChanneled(); 00330 print( $out ); 00331 } else { 00332 $out = preg_replace( '/\n\z/', '', $out ); 00333 $this->outputChanneled( $out, $channel ); 00334 } 00335 } 00336 00343 protected function error( $err, $die = 0 ) { 00344 $this->outputChanneled( false ); 00345 if ( PHP_SAPI == 'cli' ) { 00346 fwrite( STDERR, $err . "\n" ); 00347 } else { 00348 print $err; 00349 } 00350 $die = intval( $die ); 00351 if ( $die > 0 ) { 00352 die( $die ); 00353 } 00354 } 00355 00356 private $atLineStart = true; 00357 private $lastChannel = null; 00358 00362 public function cleanupChanneled() { 00363 if ( !$this->atLineStart ) { 00364 print "\n"; 00365 $this->atLineStart = true; 00366 } 00367 } 00368 00377 public function outputChanneled( $msg, $channel = null ) { 00378 if ( $msg === false ) { 00379 $this->cleanupChanneled(); 00380 return; 00381 } 00382 00383 // End the current line if necessary 00384 if ( !$this->atLineStart && $channel !== $this->lastChannel ) { 00385 print "\n"; 00386 } 00387 00388 print $msg; 00389 00390 $this->atLineStart = false; 00391 if ( $channel === null ) { 00392 // For unchanneled messages, output trailing newline immediately 00393 print "\n"; 00394 $this->atLineStart = true; 00395 } 00396 $this->lastChannel = $channel; 00397 } 00398 00409 public function getDbType() { 00410 return Maintenance::DB_STD; 00411 } 00412 00416 protected function addDefaultParams() { 00417 00418 # Generic (non script dependant) options: 00419 00420 $this->addOption( 'help', 'Display this help message', false, false, 'h' ); 00421 $this->addOption( 'quiet', 'Whether to supress non-error output', false, false, 'q' ); 00422 $this->addOption( 'conf', 'Location of LocalSettings.php, if not default', false, true ); 00423 $this->addOption( 'wiki', 'For specifying the wiki ID', false, true ); 00424 $this->addOption( 'globals', 'Output globals at the end of processing for debugging' ); 00425 $this->addOption( 'memory-limit', 'Set a specific memory limit for the script, "max" for no limit or "default" to avoid changing it' ); 00426 $this->addOption( 'server', "The protocol and server name to use in URLs, e.g. " . 00427 "http://en.wikipedia.org. This is sometimes necessary because " . 00428 "server name detection may fail in command line scripts.", false, true ); 00429 00430 # Save generic options to display them separately in help 00431 $this->mGenericParameters = $this->mParams ; 00432 00433 # Script dependant options: 00434 00435 // If we support a DB, show the options 00436 if ( $this->getDbType() > 0 ) { 00437 $this->addOption( 'dbuser', 'The DB user to use for this script', false, true ); 00438 $this->addOption( 'dbpass', 'The password to use for this script', false, true ); 00439 } 00440 00441 # Save additional script dependant options to display 00442 # them separately in help 00443 $this->mDependantParameters = array_diff_key( $this->mParams, $this->mGenericParameters ); 00444 } 00445 00453 public function runChild( $maintClass, $classFile = null ) { 00454 // Make sure the class is loaded first 00455 if ( !MWInit::classExists( $maintClass ) ) { 00456 if ( $classFile ) { 00457 require_once( $classFile ); 00458 } 00459 if ( !MWInit::classExists( $maintClass ) ) { 00460 $this->error( "Cannot spawn child: $maintClass" ); 00461 } 00462 } 00463 00467 $child = new $maintClass(); 00468 $child->loadParamsAndArgs( $this->mSelf, $this->mOptions, $this->mArgs ); 00469 if ( !is_null( $this->mDb ) ) { 00470 $child->setDB( $this->mDb ); 00471 } 00472 return $child; 00473 } 00474 00478 public function setup() { 00479 global $wgCommandLineMode, $wgRequestTime; 00480 00481 # Abort if called from a web server 00482 if ( isset( $_SERVER ) && isset( $_SERVER['REQUEST_METHOD'] ) ) { 00483 $this->error( 'This script must be run from the command line', true ); 00484 } 00485 00486 # Make sure we can handle script parameters 00487 if ( !function_exists( 'hphp_thread_set_warmup_enabled' ) && !ini_get( 'register_argc_argv' ) ) { 00488 $this->error( 'Cannot get command line arguments, register_argc_argv is set to false', true ); 00489 } 00490 00491 // Send PHP warnings and errors to stderr instead of stdout. 00492 // This aids in diagnosing problems, while keeping messages 00493 // out of redirected output. 00494 if ( ini_get( 'display_errors' ) ) { 00495 ini_set( 'display_errors', 'stderr' ); 00496 } 00497 00498 $this->loadParamsAndArgs(); 00499 $this->maybeHelp(); 00500 00501 # Set the memory limit 00502 # Note we need to set it again later in cache LocalSettings changed it 00503 $this->adjustMemoryLimit(); 00504 00505 # Set max execution time to 0 (no limit). PHP.net says that 00506 # "When running PHP from the command line the default setting is 0." 00507 # But sometimes this doesn't seem to be the case. 00508 ini_set( 'max_execution_time', 0 ); 00509 00510 $wgRequestTime = microtime( true ); 00511 00512 # Define us as being in MediaWiki 00513 define( 'MEDIAWIKI', true ); 00514 00515 $wgCommandLineMode = true; 00516 00517 # Turn off output buffering if it's on 00518 while( ob_get_level() > 0 ) { 00519 ob_end_flush(); 00520 } 00521 00522 $this->validateParamsAndArgs(); 00523 } 00524 00534 public function memoryLimit() { 00535 $limit = $this->getOption( 'memory-limit', 'max' ); 00536 $limit = trim( $limit, "\" '" ); // trim quotes in case someone misunderstood 00537 return $limit; 00538 } 00539 00543 protected function adjustMemoryLimit() { 00544 $limit = $this->memoryLimit(); 00545 if ( $limit == 'max' ) { 00546 $limit = -1; // no memory limit 00547 } 00548 if ( $limit != 'default' ) { 00549 ini_set( 'memory_limit', $limit ); 00550 } 00551 } 00552 00556 public function clearParamsAndArgs() { 00557 $this->mOptions = array(); 00558 $this->mArgs = array(); 00559 $this->mInputLoaded = false; 00560 } 00561 00571 public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) { 00572 # If we were given opts or args, set those and return early 00573 if ( $self ) { 00574 $this->mSelf = $self; 00575 $this->mInputLoaded = true; 00576 } 00577 if ( $opts ) { 00578 $this->mOptions = $opts; 00579 $this->mInputLoaded = true; 00580 } 00581 if ( $args ) { 00582 $this->mArgs = $args; 00583 $this->mInputLoaded = true; 00584 } 00585 00586 # If we've already loaded input (either by user values or from $argv) 00587 # skip on loading it again. The array_shift() will corrupt values if 00588 # it's run again and again 00589 if ( $this->mInputLoaded ) { 00590 $this->loadSpecialVars(); 00591 return; 00592 } 00593 00594 global $argv; 00595 $this->mSelf = array_shift( $argv ); 00596 00597 $options = array(); 00598 $args = array(); 00599 00600 # Parse arguments 00601 for ( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) { 00602 if ( $arg == '--' ) { 00603 # End of options, remainder should be considered arguments 00604 $arg = next( $argv ); 00605 while ( $arg !== false ) { 00606 $args[] = $arg; 00607 $arg = next( $argv ); 00608 } 00609 break; 00610 } elseif ( substr( $arg, 0, 2 ) == '--' ) { 00611 # Long options 00612 $option = substr( $arg, 2 ); 00613 if ( array_key_exists( $option, $options ) ) { 00614 $this->error( "\nERROR: $option parameter given twice\n" ); 00615 $this->maybeHelp( true ); 00616 } 00617 if ( isset( $this->mParams[$option] ) && $this->mParams[$option]['withArg'] ) { 00618 $param = next( $argv ); 00619 if ( $param === false ) { 00620 $this->error( "\nERROR: $option parameter needs a value after it\n" ); 00621 $this->maybeHelp( true ); 00622 } 00623 $options[$option] = $param; 00624 } else { 00625 $bits = explode( '=', $option, 2 ); 00626 if ( count( $bits ) > 1 ) { 00627 $option = $bits[0]; 00628 $param = $bits[1]; 00629 } else { 00630 $param = 1; 00631 } 00632 $options[$option] = $param; 00633 } 00634 } elseif ( substr( $arg, 0, 1 ) == '-' ) { 00635 # Short options 00636 for ( $p = 1; $p < strlen( $arg ); $p++ ) { 00637 $option = $arg { $p } ; 00638 if ( !isset( $this->mParams[$option] ) && isset( $this->mShortParamsMap[$option] ) ) { 00639 $option = $this->mShortParamsMap[$option]; 00640 } 00641 if ( array_key_exists( $option, $options ) ) { 00642 $this->error( "\nERROR: $option parameter given twice\n" ); 00643 $this->maybeHelp( true ); 00644 } 00645 if ( isset( $this->mParams[$option]['withArg'] ) && $this->mParams[$option]['withArg'] ) { 00646 $param = next( $argv ); 00647 if ( $param === false ) { 00648 $this->error( "\nERROR: $option parameter needs a value after it\n" ); 00649 $this->maybeHelp( true ); 00650 } 00651 $options[$option] = $param; 00652 } else { 00653 $options[$option] = 1; 00654 } 00655 } 00656 } else { 00657 $args[] = $arg; 00658 } 00659 } 00660 00661 $this->mOptions = $options; 00662 $this->mArgs = $args; 00663 $this->loadSpecialVars(); 00664 $this->mInputLoaded = true; 00665 } 00666 00670 protected function validateParamsAndArgs() { 00671 $die = false; 00672 # Check to make sure we've got all the required options 00673 foreach ( $this->mParams as $opt => $info ) { 00674 if ( $info['require'] && !$this->hasOption( $opt ) ) { 00675 $this->error( "Param $opt required!" ); 00676 $die = true; 00677 } 00678 } 00679 # Check arg list too 00680 foreach ( $this->mArgList as $k => $info ) { 00681 if ( $info['require'] && !$this->hasArg( $k ) ) { 00682 $this->error( 'Argument <' . $info['name'] . '> required!' ); 00683 $die = true; 00684 } 00685 } 00686 00687 if ( $die ) { 00688 $this->maybeHelp( true ); 00689 } 00690 } 00691 00695 protected function loadSpecialVars() { 00696 if ( $this->hasOption( 'dbuser' ) ) { 00697 $this->mDbUser = $this->getOption( 'dbuser' ); 00698 } 00699 if ( $this->hasOption( 'dbpass' ) ) { 00700 $this->mDbPass = $this->getOption( 'dbpass' ); 00701 } 00702 if ( $this->hasOption( 'quiet' ) ) { 00703 $this->mQuiet = true; 00704 } 00705 if ( $this->hasOption( 'batch-size' ) ) { 00706 $this->mBatchSize = intval( $this->getOption( 'batch-size' ) ); 00707 } 00708 } 00709 00714 protected function maybeHelp( $force = false ) { 00715 if( !$force && !$this->hasOption( 'help' ) ) { 00716 return; 00717 } 00718 00719 $screenWidth = 80; // TODO: Caculate this! 00720 $tab = " "; 00721 $descWidth = $screenWidth - ( 2 * strlen( $tab ) ); 00722 00723 ksort( $this->mParams ); 00724 $this->mQuiet = false; 00725 00726 // Description ... 00727 if ( $this->mDescription ) { 00728 $this->output( "\n" . $this->mDescription . "\n" ); 00729 } 00730 $output = "\nUsage: php " . basename( $this->mSelf ); 00731 00732 // ... append parameters ... 00733 if ( $this->mParams ) { 00734 $output .= " [--" . implode( array_keys( $this->mParams ), "|--" ) . "]"; 00735 } 00736 00737 // ... and append arguments. 00738 if ( $this->mArgList ) { 00739 $output .= ' '; 00740 foreach ( $this->mArgList as $k => $arg ) { 00741 if ( $arg['require'] ) { 00742 $output .= '<' . $arg['name'] . '>'; 00743 } else { 00744 $output .= '[' . $arg['name'] . ']'; 00745 } 00746 if ( $k < count( $this->mArgList ) - 1 ) 00747 $output .= ' '; 00748 } 00749 } 00750 $this->output( "$output\n\n" ); 00751 00752 # TODO abstract some repetitive code below 00753 00754 // Generic parameters 00755 $this->output( "Generic maintenance parameters:\n" ); 00756 foreach ( $this->mGenericParameters as $par => $info ) { 00757 if ( $info['shortName'] !== false ) { 00758 $par .= " (-{$info['shortName']})"; 00759 } 00760 $this->output( 00761 wordwrap( "$tab--$par: " . $info['desc'], $descWidth, 00762 "\n$tab$tab" ) . "\n" 00763 ); 00764 } 00765 $this->output( "\n" ); 00766 00767 $scriptDependantParams = $this->mDependantParameters; 00768 if( count($scriptDependantParams) > 0 ) { 00769 $this->output( "Script dependant parameters:\n" ); 00770 // Parameters description 00771 foreach ( $scriptDependantParams as $par => $info ) { 00772 if ( $info['shortName'] !== false ) { 00773 $par .= " (-{$info['shortName']})"; 00774 } 00775 $this->output( 00776 wordwrap( "$tab--$par: " . $info['desc'], $descWidth, 00777 "\n$tab$tab" ) . "\n" 00778 ); 00779 } 00780 $this->output( "\n" ); 00781 } 00782 00783 00784 // Script specific parameters not defined on construction by 00785 // Maintenance::addDefaultParams() 00786 $scriptSpecificParams = array_diff_key( 00787 # all script parameters: 00788 $this->mParams, 00789 # remove the Maintenance default parameters: 00790 $this->mGenericParameters, 00791 $this->mDependantParameters 00792 ); 00793 if( count($scriptSpecificParams) > 0 ) { 00794 $this->output( "Script specific parameters:\n" ); 00795 // Parameters description 00796 foreach ( $scriptSpecificParams as $par => $info ) { 00797 if ( $info['shortName'] !== false ) { 00798 $par .= " (-{$info['shortName']})"; 00799 } 00800 $this->output( 00801 wordwrap( "$tab--$par: " . $info['desc'], $descWidth, 00802 "\n$tab$tab" ) . "\n" 00803 ); 00804 } 00805 $this->output( "\n" ); 00806 } 00807 00808 // Print arguments 00809 if( count( $this->mArgList ) > 0 ) { 00810 $this->output( "Arguments:\n" ); 00811 // Arguments description 00812 foreach ( $this->mArgList as $info ) { 00813 $openChar = $info['require'] ? '<' : '['; 00814 $closeChar = $info['require'] ? '>' : ']'; 00815 $this->output( 00816 wordwrap( "$tab$openChar" . $info['name'] . "$closeChar: " . 00817 $info['desc'], $descWidth, "\n$tab$tab" ) . "\n" 00818 ); 00819 } 00820 $this->output( "\n" ); 00821 } 00822 00823 die( 1 ); 00824 } 00825 00829 public function finalSetup() { 00830 global $wgCommandLineMode, $wgShowSQLErrors, $wgServer; 00831 global $wgDBadminuser, $wgDBadminpassword; 00832 global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf; 00833 00834 # Turn off output buffering again, it might have been turned on in the settings files 00835 if ( ob_get_level() ) { 00836 ob_end_flush(); 00837 } 00838 # Same with these 00839 $wgCommandLineMode = true; 00840 00841 # Override $wgServer 00842 if( $this->hasOption( 'server') ) { 00843 $wgServer = $this->getOption( 'server', $wgServer ); 00844 } 00845 00846 # If these were passed, use them 00847 if ( $this->mDbUser ) { 00848 $wgDBadminuser = $this->mDbUser; 00849 } 00850 if ( $this->mDbPass ) { 00851 $wgDBadminpassword = $this->mDbPass; 00852 } 00853 00854 if ( $this->getDbType() == self::DB_ADMIN && isset( $wgDBadminuser ) ) { 00855 $wgDBuser = $wgDBadminuser; 00856 $wgDBpassword = $wgDBadminpassword; 00857 00858 if ( $wgDBservers ) { 00862 foreach ( $wgDBservers as $i => $server ) { 00863 $wgDBservers[$i]['user'] = $wgDBuser; 00864 $wgDBservers[$i]['password'] = $wgDBpassword; 00865 } 00866 } 00867 if ( isset( $wgLBFactoryConf['serverTemplate'] ) ) { 00868 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser; 00869 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword; 00870 } 00871 LBFactory::destroyInstance(); 00872 } 00873 00874 $this->afterFinalSetup(); 00875 00876 $wgShowSQLErrors = true; 00877 @set_time_limit( 0 ); 00878 $this->adjustMemoryLimit(); 00879 } 00880 00884 protected function afterFinalSetup() { 00885 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) { 00886 call_user_func( MW_CMDLINE_CALLBACK ); 00887 } 00888 } 00889 00894 public function globals() { 00895 if ( $this->hasOption( 'globals' ) ) { 00896 print_r( $GLOBALS ); 00897 } 00898 } 00899 00904 public function loadSettings() { 00905 global $wgCommandLineMode, $IP; 00906 00907 if ( isset( $this->mOptions['conf'] ) ) { 00908 $settingsFile = $this->mOptions['conf']; 00909 } elseif ( defined("MW_CONFIG_FILE") ) { 00910 $settingsFile = MW_CONFIG_FILE; 00911 } else { 00912 $settingsFile = "$IP/LocalSettings.php"; 00913 } 00914 if ( isset( $this->mOptions['wiki'] ) ) { 00915 $bits = explode( '-', $this->mOptions['wiki'] ); 00916 if ( count( $bits ) == 1 ) { 00917 $bits[] = ''; 00918 } 00919 define( 'MW_DB', $bits[0] ); 00920 define( 'MW_PREFIX', $bits[1] ); 00921 } 00922 00923 if ( !is_readable( $settingsFile ) ) { 00924 $this->error( "A copy of your installation's LocalSettings.php\n" . 00925 "must exist and be readable in the source directory.\n" . 00926 "Use --conf to specify it.", true ); 00927 } 00928 $wgCommandLineMode = true; 00929 return $settingsFile; 00930 } 00931 00937 public function purgeRedundantText( $delete = true ) { 00938 # Data should come off the master, wrapped in a transaction 00939 $dbw = $this->getDB( DB_MASTER ); 00940 $dbw->begin( __METHOD__ ); 00941 00942 # Get "active" text records from the revisions table 00943 $this->output( 'Searching for active text records in revisions table...' ); 00944 $res = $dbw->select( 'revision', 'rev_text_id', array(), __METHOD__, array( 'DISTINCT' ) ); 00945 foreach ( $res as $row ) { 00946 $cur[] = $row->rev_text_id; 00947 } 00948 $this->output( "done.\n" ); 00949 00950 # Get "active" text records from the archive table 00951 $this->output( 'Searching for active text records in archive table...' ); 00952 $res = $dbw->select( 'archive', 'ar_text_id', array(), __METHOD__, array( 'DISTINCT' ) ); 00953 foreach ( $res as $row ) { 00954 # old pre-MW 1.5 records can have null ar_text_id's. 00955 if ( $row->ar_text_id !== null ) { 00956 $cur[] = $row->ar_text_id; 00957 } 00958 } 00959 $this->output( "done.\n" ); 00960 00961 # Get the IDs of all text records not in these sets 00962 $this->output( 'Searching for inactive text records...' ); 00963 $cond = 'old_id NOT IN ( ' . $dbw->makeList( $cur ) . ' )'; 00964 $res = $dbw->select( 'text', 'old_id', array( $cond ), __METHOD__, array( 'DISTINCT' ) ); 00965 $old = array(); 00966 foreach ( $res as $row ) { 00967 $old[] = $row->old_id; 00968 } 00969 $this->output( "done.\n" ); 00970 00971 # Inform the user of what we're going to do 00972 $count = count( $old ); 00973 $this->output( "$count inactive items found.\n" ); 00974 00975 # Delete as appropriate 00976 if ( $delete && $count ) { 00977 $this->output( 'Deleting...' ); 00978 $dbw->delete( 'text', array( 'old_id' => $old ), __METHOD__ ); 00979 $this->output( "done.\n" ); 00980 } 00981 00982 # Done 00983 $dbw->commit( __METHOD__ ); 00984 } 00985 00990 protected function getDir() { 00991 return __DIR__; 00992 } 00993 01000 public static function getMaintenanceScripts() { 01001 global $wgMaintenanceScripts; 01002 return $wgMaintenanceScripts + self::getCoreScripts(); 01003 } 01004 01009 protected static function getCoreScripts() { 01010 if ( !self::$mCoreScripts ) { 01011 $paths = array( 01012 __DIR__, 01013 __DIR__ . '/language', 01014 __DIR__ . '/storage', 01015 ); 01016 self::$mCoreScripts = array(); 01017 foreach ( $paths as $p ) { 01018 $handle = opendir( $p ); 01019 while ( ( $file = readdir( $handle ) ) !== false ) { 01020 if ( $file == 'Maintenance.php' ) { 01021 continue; 01022 } 01023 $file = $p . '/' . $file; 01024 if ( is_dir( $file ) || !strpos( $file, '.php' ) || 01025 ( strpos( file_get_contents( $file ), '$maintClass' ) === false ) ) { 01026 continue; 01027 } 01028 require( $file ); 01029 $vars = get_defined_vars(); 01030 if ( array_key_exists( 'maintClass', $vars ) ) { 01031 self::$mCoreScripts[$vars['maintClass']] = $file; 01032 } 01033 } 01034 closedir( $handle ); 01035 } 01036 } 01037 return self::$mCoreScripts; 01038 } 01039 01047 protected function &getDB( $db, $groups = array(), $wiki = false ) { 01048 if ( is_null( $this->mDb ) ) { 01049 return wfGetDB( $db, $groups, $wiki ); 01050 } else { 01051 return $this->mDb; 01052 } 01053 } 01054 01060 public function setDB( &$db ) { 01061 $this->mDb = $db; 01062 } 01063 01068 private function lockSearchindex( &$db ) { 01069 $write = array( 'searchindex' ); 01070 $read = array( 'page', 'revision', 'text', 'interwiki', 'l10n_cache', 'user' ); 01071 $db->lockTables( $read, $write, __CLASS__ . '::' . __METHOD__ ); 01072 } 01073 01078 private function unlockSearchindex( &$db ) { 01079 $db->unlockTables( __CLASS__ . '::' . __METHOD__ ); 01080 } 01081 01087 private function relockSearchindex( &$db ) { 01088 $this->unlockSearchindex( $db ); 01089 $this->lockSearchindex( $db ); 01090 } 01091 01099 public function updateSearchIndex( $maxLockTime, $callback, $dbw, $results ) { 01100 $lockTime = time(); 01101 01102 # Lock searchindex 01103 if ( $maxLockTime ) { 01104 $this->output( " --- Waiting for lock ---" ); 01105 $this->lockSearchindex( $dbw ); 01106 $lockTime = time(); 01107 $this->output( "\n" ); 01108 } 01109 01110 # Loop through the results and do a search update 01111 foreach ( $results as $row ) { 01112 # Allow reads to be processed 01113 if ( $maxLockTime && time() > $lockTime + $maxLockTime ) { 01114 $this->output( " --- Relocking ---" ); 01115 $this->relockSearchindex( $dbw ); 01116 $lockTime = time(); 01117 $this->output( "\n" ); 01118 } 01119 call_user_func( $callback, $dbw, $row ); 01120 } 01121 01122 # Unlock searchindex 01123 if ( $maxLockTime ) { 01124 $this->output( " --- Unlocking --" ); 01125 $this->unlockSearchindex( $dbw ); 01126 $this->output( "\n" ); 01127 } 01128 01129 } 01130 01137 public function updateSearchIndexForPage( $dbw, $pageId ) { 01138 // Get current revision 01139 $rev = Revision::loadFromPageId( $dbw, $pageId ); 01140 $title = null; 01141 if ( $rev ) { 01142 $titleObj = $rev->getTitle(); 01143 $title = $titleObj->getPrefixedDBkey(); 01144 $this->output( "$title..." ); 01145 # Update searchindex 01146 # TODO: pass the Content object to SearchUpdate, let the search engine decide how to deal with it. 01147 $u = new SearchUpdate( $pageId, $titleObj->getText(), $rev->getContent()->getTextForSearchIndex() ); 01148 $u->doUpdate(); 01149 $this->output( "\n" ); 01150 } 01151 return $title; 01152 } 01153 01162 public static function posix_isatty( $fd ) { 01163 if ( !MWInit::functionExists( 'posix_isatty' ) ) { 01164 return !$fd; 01165 } else { 01166 return posix_isatty( $fd ); 01167 } 01168 } 01169 01175 public static function readconsole( $prompt = '> ' ) { 01176 static $isatty = null; 01177 if ( is_null( $isatty ) ) { 01178 $isatty = self::posix_isatty( 0 /*STDIN*/ ); 01179 } 01180 01181 if ( $isatty && function_exists( 'readline' ) ) { 01182 return readline( $prompt ); 01183 } else { 01184 if ( $isatty ) { 01185 $st = self::readlineEmulation( $prompt ); 01186 } else { 01187 if ( feof( STDIN ) ) { 01188 $st = false; 01189 } else { 01190 $st = fgets( STDIN, 1024 ); 01191 } 01192 } 01193 if ( $st === false ) return false; 01194 $resp = trim( $st ); 01195 return $resp; 01196 } 01197 } 01198 01204 private static function readlineEmulation( $prompt ) { 01205 $bash = Installer::locateExecutableInDefaultPaths( array( 'bash' ) ); 01206 if ( !wfIsWindows() && $bash ) { 01207 $retval = false; 01208 $encPrompt = wfEscapeShellArg( $prompt ); 01209 $command = "read -er -p $encPrompt && echo \"\$REPLY\""; 01210 $encCommand = wfEscapeShellArg( $command ); 01211 $line = wfShellExec( "$bash -c $encCommand", $retval, array(), array( 'walltime' => 0 ) ); 01212 01213 if ( $retval == 0 ) { 01214 return $line; 01215 } elseif ( $retval == 127 ) { 01216 // Couldn't execute bash even though we thought we saw it. 01217 // Shell probably spit out an error message, sorry :( 01218 // Fall through to fgets()... 01219 } else { 01220 // EOF/ctrl+D 01221 return false; 01222 } 01223 } 01224 01225 // Fallback... we'll have no editing controls, EWWW 01226 if ( feof( STDIN ) ) { 01227 return false; 01228 } 01229 print $prompt; 01230 return fgets( STDIN, 1024 ); 01231 } 01232 } 01233 01237 class FakeMaintenance extends Maintenance { 01238 protected $mSelf = "FakeMaintenanceScript"; 01239 public function execute() { 01240 return; 01241 } 01242 } 01243 01248 abstract class LoggedUpdateMaintenance extends Maintenance { 01249 public function __construct() { 01250 parent::__construct(); 01251 $this->addOption( 'force', 'Run the update even if it was completed already' ); 01252 $this->setBatchSize( 200 ); 01253 } 01254 01255 public function execute() { 01256 $db = $this->getDB( DB_MASTER ); 01257 $key = $this->getUpdateKey(); 01258 01259 if ( !$this->hasOption( 'force' ) && 01260 $db->selectRow( 'updatelog', '1', array( 'ul_key' => $key ), __METHOD__ ) ) 01261 { 01262 $this->output( "..." . $this->updateSkippedMessage() . "\n" ); 01263 return true; 01264 } 01265 01266 if ( !$this->doDBUpdates() ) { 01267 return false; 01268 } 01269 01270 if ( 01271 $db->insert( 'updatelog', array( 'ul_key' => $key ), __METHOD__, 'IGNORE' ) ) 01272 { 01273 return true; 01274 } else { 01275 $this->output( $this->updatelogFailedMessage() . "\n" ); 01276 return false; 01277 } 01278 } 01279 01284 protected function updateSkippedMessage() { 01285 $key = $this->getUpdateKey(); 01286 return "Update '{$key}' already logged as completed."; 01287 } 01288 01293 protected function updatelogFailedMessage() { 01294 $key = $this->getUpdateKey(); 01295 return "Unable to log update '{$key}' as completed."; 01296 } 01297 01303 abstract protected function doDBUpdates(); 01304 01309 abstract protected function getUpdateKey(); 01310 }