MediaWiki
master
|
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_name() == '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 # Turn off output buffering if it's on 00517 @ob_end_flush(); 00518 00519 $this->validateParamsAndArgs(); 00520 } 00521 00531 public function memoryLimit() { 00532 $limit = $this->getOption( 'memory-limit', 'max' ); 00533 $limit = trim( $limit, "\" '" ); // trim quotes in case someone misunderstood 00534 return $limit; 00535 } 00536 00540 protected function adjustMemoryLimit() { 00541 $limit = $this->memoryLimit(); 00542 if ( $limit == 'max' ) { 00543 $limit = -1; // no memory limit 00544 } 00545 if ( $limit != 'default' ) { 00546 ini_set( 'memory_limit', $limit ); 00547 } 00548 } 00549 00553 public function clearParamsAndArgs() { 00554 $this->mOptions = array(); 00555 $this->mArgs = array(); 00556 $this->mInputLoaded = false; 00557 } 00558 00568 public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) { 00569 # If we were given opts or args, set those and return early 00570 if ( $self ) { 00571 $this->mSelf = $self; 00572 $this->mInputLoaded = true; 00573 } 00574 if ( $opts ) { 00575 $this->mOptions = $opts; 00576 $this->mInputLoaded = true; 00577 } 00578 if ( $args ) { 00579 $this->mArgs = $args; 00580 $this->mInputLoaded = true; 00581 } 00582 00583 # If we've already loaded input (either by user values or from $argv) 00584 # skip on loading it again. The array_shift() will corrupt values if 00585 # it's run again and again 00586 if ( $this->mInputLoaded ) { 00587 $this->loadSpecialVars(); 00588 return; 00589 } 00590 00591 global $argv; 00592 $this->mSelf = array_shift( $argv ); 00593 00594 $options = array(); 00595 $args = array(); 00596 00597 # Parse arguments 00598 for ( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) { 00599 if ( $arg == '--' ) { 00600 # End of options, remainder should be considered arguments 00601 $arg = next( $argv ); 00602 while ( $arg !== false ) { 00603 $args[] = $arg; 00604 $arg = next( $argv ); 00605 } 00606 break; 00607 } elseif ( substr( $arg, 0, 2 ) == '--' ) { 00608 # Long options 00609 $option = substr( $arg, 2 ); 00610 if ( array_key_exists( $option, $options ) ) { 00611 $this->error( "\nERROR: $option parameter given twice\n" ); 00612 $this->maybeHelp( true ); 00613 } 00614 if ( isset( $this->mParams[$option] ) && $this->mParams[$option]['withArg'] ) { 00615 $param = next( $argv ); 00616 if ( $param === false ) { 00617 $this->error( "\nERROR: $option parameter needs a value after it\n" ); 00618 $this->maybeHelp( true ); 00619 } 00620 $options[$option] = $param; 00621 } else { 00622 $bits = explode( '=', $option, 2 ); 00623 if ( count( $bits ) > 1 ) { 00624 $option = $bits[0]; 00625 $param = $bits[1]; 00626 } else { 00627 $param = 1; 00628 } 00629 $options[$option] = $param; 00630 } 00631 } elseif ( substr( $arg, 0, 1 ) == '-' ) { 00632 # Short options 00633 for ( $p = 1; $p < strlen( $arg ); $p++ ) { 00634 $option = $arg { $p } ; 00635 if ( !isset( $this->mParams[$option] ) && isset( $this->mShortParamsMap[$option] ) ) { 00636 $option = $this->mShortParamsMap[$option]; 00637 } 00638 if ( array_key_exists( $option, $options ) ) { 00639 $this->error( "\nERROR: $option parameter given twice\n" ); 00640 $this->maybeHelp( true ); 00641 } 00642 if ( isset( $this->mParams[$option]['withArg'] ) && $this->mParams[$option]['withArg'] ) { 00643 $param = next( $argv ); 00644 if ( $param === false ) { 00645 $this->error( "\nERROR: $option parameter needs a value after it\n" ); 00646 $this->maybeHelp( true ); 00647 } 00648 $options[$option] = $param; 00649 } else { 00650 $options[$option] = 1; 00651 } 00652 } 00653 } else { 00654 $args[] = $arg; 00655 } 00656 } 00657 00658 $this->mOptions = $options; 00659 $this->mArgs = $args; 00660 $this->loadSpecialVars(); 00661 $this->mInputLoaded = true; 00662 } 00663 00667 protected function validateParamsAndArgs() { 00668 $die = false; 00669 # Check to make sure we've got all the required options 00670 foreach ( $this->mParams as $opt => $info ) { 00671 if ( $info['require'] && !$this->hasOption( $opt ) ) { 00672 $this->error( "Param $opt required!" ); 00673 $die = true; 00674 } 00675 } 00676 # Check arg list too 00677 foreach ( $this->mArgList as $k => $info ) { 00678 if ( $info['require'] && !$this->hasArg( $k ) ) { 00679 $this->error( 'Argument <' . $info['name'] . '> required!' ); 00680 $die = true; 00681 } 00682 } 00683 00684 if ( $die ) { 00685 $this->maybeHelp( true ); 00686 } 00687 } 00688 00692 protected function loadSpecialVars() { 00693 if ( $this->hasOption( 'dbuser' ) ) { 00694 $this->mDbUser = $this->getOption( 'dbuser' ); 00695 } 00696 if ( $this->hasOption( 'dbpass' ) ) { 00697 $this->mDbPass = $this->getOption( 'dbpass' ); 00698 } 00699 if ( $this->hasOption( 'quiet' ) ) { 00700 $this->mQuiet = true; 00701 } 00702 if ( $this->hasOption( 'batch-size' ) ) { 00703 $this->mBatchSize = intval( $this->getOption( 'batch-size' ) ); 00704 } 00705 } 00706 00711 protected function maybeHelp( $force = false ) { 00712 if( !$force && !$this->hasOption( 'help' ) ) { 00713 return; 00714 } 00715 00716 $screenWidth = 80; // TODO: Caculate this! 00717 $tab = " "; 00718 $descWidth = $screenWidth - ( 2 * strlen( $tab ) ); 00719 00720 ksort( $this->mParams ); 00721 $this->mQuiet = false; 00722 00723 // Description ... 00724 if ( $this->mDescription ) { 00725 $this->output( "\n" . $this->mDescription . "\n" ); 00726 } 00727 $output = "\nUsage: php " . basename( $this->mSelf ); 00728 00729 // ... append parameters ... 00730 if ( $this->mParams ) { 00731 $output .= " [--" . implode( array_keys( $this->mParams ), "|--" ) . "]"; 00732 } 00733 00734 // ... and append arguments. 00735 if ( $this->mArgList ) { 00736 $output .= ' '; 00737 foreach ( $this->mArgList as $k => $arg ) { 00738 if ( $arg['require'] ) { 00739 $output .= '<' . $arg['name'] . '>'; 00740 } else { 00741 $output .= '[' . $arg['name'] . ']'; 00742 } 00743 if ( $k < count( $this->mArgList ) - 1 ) 00744 $output .= ' '; 00745 } 00746 } 00747 $this->output( "$output\n\n" ); 00748 00749 # TODO abstract some repetitive code below 00750 00751 // Generic parameters 00752 $this->output( "Generic maintenance parameters:\n" ); 00753 foreach ( $this->mGenericParameters as $par => $info ) { 00754 if ( $info['shortName'] !== false ) { 00755 $par .= " (-{$info['shortName']})"; 00756 } 00757 $this->output( 00758 wordwrap( "$tab--$par: " . $info['desc'], $descWidth, 00759 "\n$tab$tab" ) . "\n" 00760 ); 00761 } 00762 $this->output( "\n" ); 00763 00764 $scriptDependantParams = $this->mDependantParameters; 00765 if( count($scriptDependantParams) > 0 ) { 00766 $this->output( "Script dependant parameters:\n" ); 00767 // Parameters description 00768 foreach ( $scriptDependantParams as $par => $info ) { 00769 if ( $info['shortName'] !== false ) { 00770 $par .= " (-{$info['shortName']})"; 00771 } 00772 $this->output( 00773 wordwrap( "$tab--$par: " . $info['desc'], $descWidth, 00774 "\n$tab$tab" ) . "\n" 00775 ); 00776 } 00777 $this->output( "\n" ); 00778 } 00779 00780 00781 // Script specific parameters not defined on construction by 00782 // Maintenance::addDefaultParams() 00783 $scriptSpecificParams = array_diff_key( 00784 # all script parameters: 00785 $this->mParams, 00786 # remove the Maintenance default parameters: 00787 $this->mGenericParameters, 00788 $this->mDependantParameters 00789 ); 00790 if( count($scriptSpecificParams) > 0 ) { 00791 $this->output( "Script specific parameters:\n" ); 00792 // Parameters description 00793 foreach ( $scriptSpecificParams as $par => $info ) { 00794 if ( $info['shortName'] !== false ) { 00795 $par .= " (-{$info['shortName']})"; 00796 } 00797 $this->output( 00798 wordwrap( "$tab--$par: " . $info['desc'], $descWidth, 00799 "\n$tab$tab" ) . "\n" 00800 ); 00801 } 00802 $this->output( "\n" ); 00803 } 00804 00805 // Print arguments 00806 if( count( $this->mArgList ) > 0 ) { 00807 $this->output( "Arguments:\n" ); 00808 // Arguments description 00809 foreach ( $this->mArgList as $info ) { 00810 $openChar = $info['require'] ? '<' : '['; 00811 $closeChar = $info['require'] ? '>' : ']'; 00812 $this->output( 00813 wordwrap( "$tab$openChar" . $info['name'] . "$closeChar: " . 00814 $info['desc'], $descWidth, "\n$tab$tab" ) . "\n" 00815 ); 00816 } 00817 $this->output( "\n" ); 00818 } 00819 00820 die( 1 ); 00821 } 00822 00826 public function finalSetup() { 00827 global $wgCommandLineMode, $wgShowSQLErrors, $wgServer; 00828 global $wgDBadminuser, $wgDBadminpassword; 00829 global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf; 00830 00831 # Turn off output buffering again, it might have been turned on in the settings files 00832 if ( ob_get_level() ) { 00833 ob_end_flush(); 00834 } 00835 # Same with these 00836 $wgCommandLineMode = true; 00837 00838 # Override $wgServer 00839 if( $this->hasOption( 'server') ) { 00840 $wgServer = $this->getOption( 'server', $wgServer ); 00841 } 00842 00843 # If these were passed, use them 00844 if ( $this->mDbUser ) { 00845 $wgDBadminuser = $this->mDbUser; 00846 } 00847 if ( $this->mDbPass ) { 00848 $wgDBadminpassword = $this->mDbPass; 00849 } 00850 00851 if ( $this->getDbType() == self::DB_ADMIN && isset( $wgDBadminuser ) ) { 00852 $wgDBuser = $wgDBadminuser; 00853 $wgDBpassword = $wgDBadminpassword; 00854 00855 if ( $wgDBservers ) { 00859 foreach ( $wgDBservers as $i => $server ) { 00860 $wgDBservers[$i]['user'] = $wgDBuser; 00861 $wgDBservers[$i]['password'] = $wgDBpassword; 00862 } 00863 } 00864 if ( isset( $wgLBFactoryConf['serverTemplate'] ) ) { 00865 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser; 00866 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword; 00867 } 00868 LBFactory::destroyInstance(); 00869 } 00870 00871 $this->afterFinalSetup(); 00872 00873 $wgShowSQLErrors = true; 00874 @set_time_limit( 0 ); 00875 $this->adjustMemoryLimit(); 00876 } 00877 00881 protected function afterFinalSetup() { 00882 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) { 00883 call_user_func( MW_CMDLINE_CALLBACK ); 00884 } 00885 } 00886 00891 public function globals() { 00892 if ( $this->hasOption( 'globals' ) ) { 00893 print_r( $GLOBALS ); 00894 } 00895 } 00896 00901 public function loadSettings() { 00902 global $wgCommandLineMode, $IP; 00903 00904 if ( isset( $this->mOptions['conf'] ) ) { 00905 $settingsFile = $this->mOptions['conf']; 00906 } elseif ( defined("MW_CONFIG_FILE") ) { 00907 $settingsFile = MW_CONFIG_FILE; 00908 } else { 00909 $settingsFile = "$IP/LocalSettings.php"; 00910 } 00911 if ( isset( $this->mOptions['wiki'] ) ) { 00912 $bits = explode( '-', $this->mOptions['wiki'] ); 00913 if ( count( $bits ) == 1 ) { 00914 $bits[] = ''; 00915 } 00916 define( 'MW_DB', $bits[0] ); 00917 define( 'MW_PREFIX', $bits[1] ); 00918 } 00919 00920 if ( !is_readable( $settingsFile ) ) { 00921 $this->error( "A copy of your installation's LocalSettings.php\n" . 00922 "must exist and be readable in the source directory.\n" . 00923 "Use --conf to specify it." , true ); 00924 } 00925 $wgCommandLineMode = true; 00926 return $settingsFile; 00927 } 00928 00934 public function purgeRedundantText( $delete = true ) { 00935 # Data should come off the master, wrapped in a transaction 00936 $dbw = $this->getDB( DB_MASTER ); 00937 $dbw->begin( __METHOD__ ); 00938 00939 $tbl_arc = $dbw->tableName( 'archive' ); 00940 $tbl_rev = $dbw->tableName( 'revision' ); 00941 $tbl_txt = $dbw->tableName( 'text' ); 00942 00943 # Get "active" text records from the revisions table 00944 $this->output( 'Searching for active text records in revisions table...' ); 00945 $res = $dbw->query( "SELECT DISTINCT rev_text_id FROM $tbl_rev" ); 00946 foreach ( $res as $row ) { 00947 $cur[] = $row->rev_text_id; 00948 } 00949 $this->output( "done.\n" ); 00950 00951 # Get "active" text records from the archive table 00952 $this->output( 'Searching for active text records in archive table...' ); 00953 $res = $dbw->query( "SELECT DISTINCT ar_text_id FROM $tbl_arc" ); 00954 foreach ( $res as $row ) { 00955 $cur[] = $row->ar_text_id; 00956 } 00957 $this->output( "done.\n" ); 00958 00959 # Get the IDs of all text records not in these sets 00960 $this->output( 'Searching for inactive text records...' ); 00961 $set = implode( ', ', $cur ); 00962 $res = $dbw->query( "SELECT old_id FROM $tbl_txt WHERE old_id NOT IN ( $set )" ); 00963 $old = array(); 00964 foreach ( $res as $row ) { 00965 $old[] = $row->old_id; 00966 } 00967 $this->output( "done.\n" ); 00968 00969 # Inform the user of what we're going to do 00970 $count = count( $old ); 00971 $this->output( "$count inactive items found.\n" ); 00972 00973 # Delete as appropriate 00974 if ( $delete && $count ) { 00975 $this->output( 'Deleting...' ); 00976 $set = implode( ', ', $old ); 00977 $dbw->query( "DELETE FROM $tbl_txt WHERE old_id IN ( $set )" ); 00978 $this->output( "done.\n" ); 00979 } 00980 00981 # Done 00982 $dbw->commit( __METHOD__ ); 00983 } 00984 00989 protected function getDir() { 00990 return __DIR__; 00991 } 00992 00999 public static function getMaintenanceScripts() { 01000 global $wgMaintenanceScripts; 01001 return $wgMaintenanceScripts + self::getCoreScripts(); 01002 } 01003 01008 protected static function getCoreScripts() { 01009 if ( !self::$mCoreScripts ) { 01010 $paths = array( 01011 __DIR__, 01012 __DIR__ . '/language', 01013 __DIR__ . '/storage', 01014 ); 01015 self::$mCoreScripts = array(); 01016 foreach ( $paths as $p ) { 01017 $handle = opendir( $p ); 01018 while ( ( $file = readdir( $handle ) ) !== false ) { 01019 if ( $file == 'Maintenance.php' ) { 01020 continue; 01021 } 01022 $file = $p . '/' . $file; 01023 if ( is_dir( $file ) || !strpos( $file, '.php' ) || 01024 ( strpos( file_get_contents( $file ), '$maintClass' ) === false ) ) { 01025 continue; 01026 } 01027 require( $file ); 01028 $vars = get_defined_vars(); 01029 if ( array_key_exists( 'maintClass', $vars ) ) { 01030 self::$mCoreScripts[$vars['maintClass']] = $file; 01031 } 01032 } 01033 closedir( $handle ); 01034 } 01035 } 01036 return self::$mCoreScripts; 01037 } 01038 01046 protected function &getDB( $db, $groups = array(), $wiki = false ) { 01047 if ( is_null( $this->mDb ) ) { 01048 return wfGetDB( $db, $groups, $wiki ); 01049 } else { 01050 return $this->mDb; 01051 } 01052 } 01053 01059 public function setDB( &$db ) { 01060 $this->mDb = $db; 01061 } 01062 01067 private function lockSearchindex( &$db ) { 01068 $write = array( 'searchindex' ); 01069 $read = array( 'page', 'revision', 'text', 'interwiki', 'l10n_cache', 'user' ); 01070 $db->lockTables( $read, $write, __CLASS__ . '::' . __METHOD__ ); 01071 } 01072 01077 private function unlockSearchindex( &$db ) { 01078 $db->unlockTables( __CLASS__ . '::' . __METHOD__ ); 01079 } 01080 01086 private function relockSearchindex( &$db ) { 01087 $this->unlockSearchindex( $db ); 01088 $this->lockSearchindex( $db ); 01089 } 01090 01098 public function updateSearchIndex( $maxLockTime, $callback, $dbw, $results ) { 01099 $lockTime = time(); 01100 01101 # Lock searchindex 01102 if ( $maxLockTime ) { 01103 $this->output( " --- Waiting for lock ---" ); 01104 $this->lockSearchindex( $dbw ); 01105 $lockTime = time(); 01106 $this->output( "\n" ); 01107 } 01108 01109 # Loop through the results and do a search update 01110 foreach ( $results as $row ) { 01111 # Allow reads to be processed 01112 if ( $maxLockTime && time() > $lockTime + $maxLockTime ) { 01113 $this->output( " --- Relocking ---" ); 01114 $this->relockSearchindex( $dbw ); 01115 $lockTime = time(); 01116 $this->output( "\n" ); 01117 } 01118 call_user_func( $callback, $dbw, $row ); 01119 } 01120 01121 # Unlock searchindex 01122 if ( $maxLockTime ) { 01123 $this->output( " --- Unlocking --" ); 01124 $this->unlockSearchindex( $dbw ); 01125 $this->output( "\n" ); 01126 } 01127 01128 } 01129 01136 public function updateSearchIndexForPage( $dbw, $pageId ) { 01137 // Get current revision 01138 $rev = Revision::loadFromPageId( $dbw, $pageId ); 01139 $title = null; 01140 if ( $rev ) { 01141 $titleObj = $rev->getTitle(); 01142 $title = $titleObj->getPrefixedDBkey(); 01143 $this->output( "$title..." ); 01144 # Update searchindex 01145 # TODO: pass the Content object to SearchUpdate, let the search engine decide how to deal with it. 01146 $u = new SearchUpdate( $pageId, $titleObj->getText(), $rev->getContent()->getTextForSearchIndex() ); 01147 $u->doUpdate(); 01148 $this->output( "\n" ); 01149 } 01150 return $title; 01151 } 01152 01161 public static function posix_isatty( $fd ) { 01162 if ( !MWInit::functionExists( 'posix_isatty' ) ) { 01163 return !$fd; 01164 } else { 01165 return posix_isatty( $fd ); 01166 } 01167 } 01168 01174 public static function readconsole( $prompt = '> ' ) { 01175 static $isatty = null; 01176 if ( is_null( $isatty ) ) { 01177 $isatty = self::posix_isatty( 0 /*STDIN*/ ); 01178 } 01179 01180 if ( $isatty && function_exists( 'readline' ) ) { 01181 return readline( $prompt ); 01182 } else { 01183 if ( $isatty ) { 01184 $st = self::readlineEmulation( $prompt ); 01185 } else { 01186 if ( feof( STDIN ) ) { 01187 $st = false; 01188 } else { 01189 $st = fgets( STDIN, 1024 ); 01190 } 01191 } 01192 if ( $st === false ) return false; 01193 $resp = trim( $st ); 01194 return $resp; 01195 } 01196 } 01197 01203 private static function readlineEmulation( $prompt ) { 01204 $bash = Installer::locateExecutableInDefaultPaths( array( 'bash' ) ); 01205 if ( !wfIsWindows() && $bash ) { 01206 $retval = false; 01207 $encPrompt = wfEscapeShellArg( $prompt ); 01208 $command = "read -er -p $encPrompt && echo \"\$REPLY\""; 01209 $encCommand = wfEscapeShellArg( $command ); 01210 $line = wfShellExec( "$bash -c $encCommand", $retval ); 01211 01212 if ( $retval == 0 ) { 01213 return $line; 01214 } elseif ( $retval == 127 ) { 01215 // Couldn't execute bash even though we thought we saw it. 01216 // Shell probably spit out an error message, sorry :( 01217 // Fall through to fgets()... 01218 } else { 01219 // EOF/ctrl+D 01220 return false; 01221 } 01222 } 01223 01224 // Fallback... we'll have no editing controls, EWWW 01225 if ( feof( STDIN ) ) { 01226 return false; 01227 } 01228 print $prompt; 01229 return fgets( STDIN, 1024 ); 01230 } 01231 } 01232 01236 class FakeMaintenance extends Maintenance { 01237 protected $mSelf = "FakeMaintenanceScript"; 01238 public function execute() { 01239 return; 01240 } 01241 } 01242 01247 abstract class LoggedUpdateMaintenance extends Maintenance { 01248 public function __construct() { 01249 parent::__construct(); 01250 $this->addOption( 'force', 'Run the update even if it was completed already' ); 01251 $this->setBatchSize( 200 ); 01252 } 01253 01254 public function execute() { 01255 $db = $this->getDB( DB_MASTER ); 01256 $key = $this->getUpdateKey(); 01257 01258 if ( !$this->hasOption( 'force' ) && 01259 $db->selectRow( 'updatelog', '1', array( 'ul_key' => $key ), __METHOD__ ) ) 01260 { 01261 $this->output( "..." . $this->updateSkippedMessage() . "\n" ); 01262 return true; 01263 } 01264 01265 if ( !$this->doDBUpdates() ) { 01266 return false; 01267 } 01268 01269 if ( 01270 $db->insert( 'updatelog', array( 'ul_key' => $key ), __METHOD__, 'IGNORE' ) ) 01271 { 01272 return true; 01273 } else { 01274 $this->output( $this->updatelogFailedMessage() . "\n" ); 01275 return false; 01276 } 01277 } 01278 01283 protected function updateSkippedMessage() { 01284 $key = $this->getUpdateKey(); 01285 return "Update '{$key}' already logged as completed."; 01286 } 01287 01292 protected function updatelogFailedMessage() { 01293 $key = $this->getUpdateKey(); 01294 return "Unable to log update '{$key}' as completed."; 01295 } 01296 01302 abstract protected function doDBUpdates(); 01303 01308 abstract protected function getUpdateKey(); 01309 }