| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Maintenance; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use LCStoreNull; |
| 7 |
use LogicException; |
| 8 |
use Maintenance; |
| 9 |
use MediaWiki; |
| 10 |
use MediaWiki\Config\Config; |
| 11 |
use MediaWiki\Deferred\DeferredUpdates; |
| 12 |
use MediaWiki\Logger\LoggerFactory; |
| 13 |
use MediaWiki\MainConfigNames; |
| 14 |
use MediaWiki\MediaWikiServices; |
| 15 |
use MediaWiki\Settings\SettingsBuilder; |
| 16 |
use Profiler; |
| 17 |
use ReflectionClass; |
| 18 |
use Throwable; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class MaintenanceRunner { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private $script = null; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private $scriptClass = null; |
| 42 |
|
| 43 |
|
| 44 |
private $scriptArgv = null; |
| 45 |
|
| 46 |
|
| 47 |
private $scriptObject = null; |
| 48 |
|
| 49 |
|
| 50 |
private $parameters; |
| 51 |
|
| 52 |
|
| 53 |
private $runFromWrapper = false; |
| 54 |
|
| 55 |
|
| 56 |
private bool $withoutLocalSettings = false; |
| 57 |
|
| 58 |
|
| 59 |
private ?Config $config = null; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function __construct() { |
| 68 |
$this->parameters = new MaintenanceParameters(); |
| 69 |
$this->addDefaultParams(); |
| 70 |
} |
| 71 |
|
| 72 |
private function getConfig() { |
| 73 |
if ( $this->config === null ) { |
| 74 |
$this->config = $this->getServiceContainer()->getMainConfig(); |
| 75 |
} |
| 76 |
|
| 77 |
return $this->config; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
protected function addDefaultParams() { |
| 84 |
|
| 85 |
|
| 86 |
$this->parameters->addOption( 'conf', 'Location of LocalSettings.php, if not default', false, true ); |
| 87 |
$this->parameters->addOption( 'wiki', 'For specifying the wiki ID', false, true ); |
| 88 |
$this->parameters->addOption( 'globals', 'Output globals at the end of processing for debugging' ); |
| 89 |
$this->parameters->addOption( |
| 90 |
'memory-limit', |
| 91 |
'Set a specific memory limit for the script, ' |
| 92 |
. '"max" for no limit or "default" to avoid changing it', |
| 93 |
false, |
| 94 |
true |
| 95 |
); |
| 96 |
$this->parameters->addOption( 'server', "The protocol and server name to use in URLs, e.g. " . |
| 97 |
"https://en.wikipedia.org. This is sometimes necessary because " . |
| 98 |
"server name detection may fail in command line scripts.", false, true ); |
| 99 |
$this->parameters->addOption( 'profiler', 'Profiler output format (usually "text")', false, true ); |
| 100 |
|
| 101 |
|
| 102 |
$generic = $this->parameters->getOptionNames(); |
| 103 |
$this->parameters->assignGroup( Maintenance::GENERIC_MAINTENANCE_PARAMETERS, $generic ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private function showHelpAndExit( $code = 0 ) { |
| 112 |
foreach ( $this->parameters->getErrors() as $error ) { |
| 113 |
$this->error( "$error\n" ); |
| 114 |
$code = 1; |
| 115 |
} |
| 116 |
|
| 117 |
$this->parameters->setDescription( 'Runner for maintenance scripts' ); |
| 118 |
|
| 119 |
$help = $this->parameters->getHelp(); |
| 120 |
echo $help; |
| 121 |
exit( $code ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function initFromWrapper( array $argv ) { |
| 135 |
$script = null; |
| 136 |
|
| 137 |
$this->parameters->setName( $argv[0] ); |
| 138 |
$this->parameters->setAllowUnregisteredOptions( true ); |
| 139 |
$this->parameters->addArg( |
| 140 |
'script', |
| 141 |
'The name of the maintenance script to run. ' . |
| 142 |
'Can be given as a class name or file path. The `.php` suffix is optional. ' . |
| 143 |
'Paths starting with `./` or `../` are interpreted to be relative to the current working directory. ' . |
| 144 |
'Other relative paths are interpreted relative to the maintenance script directory. ' . |
| 145 |
'Dots (.) are supported as namespace separators in class names. ' . |
| 146 |
'An extension name may be provided as a prefix, followed by a colon, e.g. "MyExtension:...", ' . |
| 147 |
'to indicate that the path or class name should be interpreted relative to the extension.' |
| 148 |
); |
| 149 |
|
| 150 |
$this->runFromWrapper = true; |
| 151 |
$this->parameters->loadWithArgv( $argv, 1 ); |
| 152 |
|
| 153 |
|
| 154 |
$argv = array_slice( $argv, 2 ); |
| 155 |
|
| 156 |
if ( $this->parameters->validate() ) { |
| 157 |
$script = $this->parameters->getArg( 0 ); |
| 158 |
|
| 159 |
|
| 160 |
if ( $script === 'help' ) { |
| 161 |
if ( $this->parameters->hasArg( 1 ) ) { |
| 162 |
$script = $this->parameters->getArg( 1 ); |
| 163 |
|
| 164 |
|
| 165 |
$this->parameters->loadWithArgv( [ $script ] ); |
| 166 |
$argv = [ '--help' ]; |
| 167 |
} else { |
| 168 |
|
| 169 |
$script = null; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if ( $script ) { |
| 175 |
|
| 176 |
$this->initInternal( $script, $argv ); |
| 177 |
} else { |
| 178 |
$this->showHelpAndExit(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public function initForClass( string $scriptClass, $argv ) { |
| 193 |
$this->runFromWrapper = false; |
| 194 |
$this->script = $scriptClass; |
| 195 |
$this->scriptClass = $scriptClass; |
| 196 |
$this->parameters->setName( $argv[0] ); |
| 197 |
$this->parameters->loadWithArgv( $argv ); |
| 198 |
$this->initInternal( $scriptClass, array_slice( $argv, 1 ) ); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private function initInternal( string $script, array $scriptArgv ) { |
| 211 |
$this->script = $script; |
| 212 |
$this->scriptArgv = $scriptArgv; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
if ( ini_get( 'display_errors' ) ) { |
| 218 |
ini_set( 'display_errors', 'stderr' ); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
register_shutdown_function( [ $this, 'cleanup' ] ); |
| 223 |
|
| 224 |
|
| 225 |
while ( ob_get_level() > 0 ) { |
| 226 |
ob_end_flush(); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
private static function isAbsolutePath( $path ) { |
| 231 |
if ( str_starts_with( $path, '/' ) ) { |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
if ( wfIsWindows() ) { |
| 236 |
if ( str_starts_with( $path, '\\' ) ) { |
| 237 |
return true; |
| 238 |
} |
| 239 |
if ( preg_match( '!^[a-zA-Z]:[/\\\\]!', $path ) ) { |
| 240 |
return true; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
protected function getExtensionInfo( string $extName ): ?array { |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
$config = SettingsBuilder::getInstance()->getConfig(); |
| 253 |
$extDir = $config->get( MainConfigNames::ExtensionDirectory ); |
| 254 |
$skinDir = $config->get( MainConfigNames::StyleDirectory ); |
| 255 |
|
| 256 |
$extension = []; |
| 257 |
if ( file_exists( "$extDir/$extName/extension.json" ) ) { |
| 258 |
$extension['path'] = "$extDir/$extName/extension.json"; |
| 259 |
$extension['namespace'] = "MediaWiki\\Extension\\$extName"; |
| 260 |
} elseif ( file_exists( "$skinDir/$extName/skin.json" ) ) { |
| 261 |
$extension['path'] = "$skinDir/$extName/skin.json"; |
| 262 |
$extension['namespace'] = "MediaWiki\\Skins\\$extName"; |
| 263 |
} else { |
| 264 |
return null; |
| 265 |
} |
| 266 |
|
| 267 |
return $extension; |
| 268 |
} |
| 269 |
|
| 270 |
private function loadScriptFile( string $scriptFile ): string { |
| 271 |
$maintClass = null; |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
$scriptClass = include $scriptFile; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
if ( $maintClass ) { |
| 281 |
$scriptClass = $maintClass; |
| 282 |
} |
| 283 |
|
| 284 |
if ( !is_string( $scriptClass ) ) { |
| 285 |
$this->error( "ERROR: The script file '{$scriptFile}' cannot be executed using MaintenanceRunner.\n" ); |
| 286 |
$this->error( "It does not set \$maintClass and does not return a class name.\n" ); |
| 287 |
$this->fatalError( "Try running it directly as a php script: php $scriptFile\n" ); |
| 288 |
} |
| 289 |
|
| 290 |
return $scriptClass; |
| 291 |
} |
| 292 |
|
| 293 |
private function splitScript( string $script ): array { |
| 294 |
|
| 295 |
if ( preg_match( '!^(\w+):(.*)$!', $script, $m ) ) { |
| 296 |
return [ $m[1], $m[2] ]; |
| 297 |
} |
| 298 |
|
| 299 |
return [ null, $script ]; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
protected function getMwInstallPath(): string { |
| 306 |
return MW_INSTALL_PATH; |
| 307 |
} |
| 308 |
|
| 309 |
private function expandScriptFile( string $scriptName, ?array $extension ): string { |
| 310 |
|
| 311 |
$scriptFile = $scriptName; |
| 312 |
if ( !str_ends_with( $scriptFile, '.php' ) ) { |
| 313 |
$scriptFile .= '.php'; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
if ( !preg_match( '!^\.\.?[/\\\\]!', $scriptFile ) && !self::isAbsolutePath( $scriptFile ) ) { |
| 319 |
if ( $extension !== null ) { |
| 320 |
|
| 321 |
$scriptFile = dirname( $extension['path'] ) . "/maintenance/{$scriptFile}"; |
| 322 |
} else { |
| 323 |
|
| 324 |
$scriptFile = $this->getMwInstallPath() . "/maintenance/{$scriptFile}"; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
return $scriptFile; |
| 329 |
} |
| 330 |
|
| 331 |
private function expandScriptClass( string $scriptName, ?array $extension ): string { |
| 332 |
$scriptClass = $scriptName; |
| 333 |
|
| 334 |
|
| 335 |
if ( $extension ) { |
| 336 |
$scriptClass = "{$extension['namespace']}\\Maintenance\\$scriptClass"; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
$scriptClass = strtr( $scriptClass, '.', '\\' ); |
| 342 |
|
| 343 |
return $scriptClass; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
protected function preloadScriptFile( string $script ): void { |
| 354 |
if ( $this->scriptClass !== null && class_exists( $this->scriptClass ) ) { |
| 355 |
|
| 356 |
return; |
| 357 |
} |
| 358 |
|
| 359 |
[ $extName, $scriptName ] = $this->splitScript( $script ); |
| 360 |
|
| 361 |
if ( $extName !== null ) { |
| 362 |
|
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
$scriptClass = $this->expandScriptClass( $scriptName, null ); |
| 367 |
$scriptFile = $this->expandScriptFile( $scriptName, null ); |
| 368 |
|
| 369 |
if ( !class_exists( $scriptClass ) && file_exists( $scriptFile ) ) { |
| 370 |
$scriptFileClass = $this->loadScriptFile( $scriptFile ); |
| 371 |
if ( $scriptFileClass ) { |
| 372 |
$scriptClass = $scriptFileClass; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
if ( class_exists( $scriptClass ) ) { |
| 378 |
|
| 379 |
$this->scriptClass = $scriptClass; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
protected function getScriptClass(): string { |
| 389 |
if ( $this->scriptClass === null ) { |
| 390 |
if ( $this->runFromWrapper ) { |
| 391 |
$this->scriptClass = $this->findScriptClass( $this->script ); |
| 392 |
} else { |
| 393 |
$this->scriptClass = $this->script; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
if ( !class_exists( $this->scriptClass ) ) { |
| 398 |
$this->fatalError( "Script class {$this->scriptClass} not found.\n" ); |
| 399 |
} |
| 400 |
|
| 401 |
return $this->scriptClass; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
protected function findScriptClass( string $script ): string { |
| 411 |
[ $extName, $scriptName ] = $this->splitScript( $script ); |
| 412 |
|
| 413 |
if ( $extName !== null ) { |
| 414 |
$extension = $this->getExtensionInfo( $extName ); |
| 415 |
|
| 416 |
if ( !$extension ) { |
| 417 |
$this->fatalError( "Extension '{$extName}' not found.\n" ); |
| 418 |
} |
| 419 |
} else { |
| 420 |
$extension = null; |
| 421 |
} |
| 422 |
|
| 423 |
$scriptClass = $this->expandScriptClass( $scriptName, $extension ); |
| 424 |
$scriptFile = $this->expandScriptFile( $scriptName, $extension ); |
| 425 |
|
| 426 |
if ( !class_exists( $scriptClass ) && file_exists( $scriptFile ) ) { |
| 427 |
$scriptFileClass = $this->loadScriptFile( $scriptFile ); |
| 428 |
if ( $scriptFileClass ) { |
| 429 |
$scriptClass = $scriptFileClass; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
if ( !class_exists( $scriptClass ) ) { |
| 434 |
$this->fatalError( "Script '{$script}' not found (tried path '$scriptFile' and class '$scriptClass').\n" ); |
| 435 |
} |
| 436 |
|
| 437 |
return $scriptClass; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
public function setup( SettingsBuilder $settings ) { |
| 446 |
|
| 447 |
$scriptClass = $this->getScriptClass(); |
| 448 |
|
| 449 |
$cls = new ReflectionClass( $scriptClass ); |
| 450 |
if ( !$cls->isSubclassOf( Maintenance::class ) ) { |
| 451 |
$this->fatalError( "Class {$this->script} is not a subclass of Maintenance.\n" ); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
try { |
| 456 |
$this->scriptObject = new $scriptClass; |
| 457 |
$this->scriptObject->setName( $this->getName() ); |
| 458 |
} catch ( Throwable $ex ) { |
| 459 |
$this->fatalError( |
| 460 |
"Failed to initialize Maintenance object.\n" . |
| 461 |
"(Did you forget to call parent::__construct() in your maintenance script?)\n" . |
| 462 |
"$ex\n" |
| 463 |
); |
| 464 |
} |
| 465 |
|
| 466 |
if ( !$this->scriptObject instanceof Maintenance ) { |
| 467 |
|
| 468 |
throw new LogicException( 'Incompatible script object' ); |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
$scriptParameters = $this->scriptObject->getParameters(); |
| 474 |
|
| 475 |
if ( $this->runFromWrapper ) { |
| 476 |
$scriptParameters->setUsagePrefix( 'php ' . $this->parameters->getName() ); |
| 477 |
} |
| 478 |
|
| 479 |
$scriptParameters->mergeOptions( $this->parameters ); |
| 480 |
$this->parameters = $scriptParameters; |
| 481 |
|
| 482 |
|
| 483 |
$this->scriptObject->loadWithArgv( $this->scriptArgv ); |
| 484 |
|
| 485 |
|
| 486 |
$this->scriptObject->setup(); |
| 487 |
|
| 488 |
|
| 489 |
$this->adjustMemoryLimit(); |
| 490 |
|
| 491 |
|
| 492 |
$this->overrideConfig( $settings ); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
public function getName(): string { |
| 501 |
|
| 502 |
|
| 503 |
return $this->parameters->getArg( 0 ) ?? 'UNKNOWN'; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
private function memoryLimit() { |
| 514 |
if ( $this->parameters->hasOption( 'memory-limit' ) ) { |
| 515 |
$limit = $this->parameters->getOption( 'memory-limit', 'max' ); |
| 516 |
$limit = trim( $limit, "\" '" ); |
| 517 |
return $limit; |
| 518 |
} |
| 519 |
|
| 520 |
$limit = $this->scriptObject->memoryLimit(); |
| 521 |
return $limit ?: 'max'; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
private function adjustMemoryLimit() { |
| 528 |
$limit = $this->memoryLimit(); |
| 529 |
if ( $limit == 'max' ) { |
| 530 |
$limit = -1; |
| 531 |
} |
| 532 |
if ( $limit != 'default' ) { |
| 533 |
ini_set( 'memory_limit', $limit ); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
public function defineSettings() { |
| 545 |
global $IP; |
| 546 |
|
| 547 |
if ( $this->parameters->hasOption( 'conf' ) ) { |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
define( 'MW_CONFIG_FILE', $this->parameters->getOption( 'conf' ) ); |
| 552 |
|
| 553 |
if ( !is_readable( MW_CONFIG_FILE ) ) { |
| 554 |
$this->fatalError( "\nConfig file " . MW_CONFIG_FILE . " was not found or is not readable.\n\n" ); |
| 555 |
} |
| 556 |
} |
| 557 |
$settingsFile = wfDetectLocalSettingsFile( $IP ); |
| 558 |
|
| 559 |
if ( $this->parameters->hasOption( 'wiki' ) ) { |
| 560 |
$wikiName = $this->parameters->getOption( 'wiki' ); |
| 561 |
$bits = explode( '-', $wikiName, 2 ); |
| 562 |
define( 'MW_DB', $bits[0] ); |
| 563 |
define( 'MW_PREFIX', $bits[1] ?? '' ); |
| 564 |
define( 'MW_WIKI_NAME', $wikiName ); |
| 565 |
} elseif ( $this->parameters->hasOption( 'server' ) ) { |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
$_SERVER['SERVER_NAME'] = $this->parameters->getOption( 'server' ); |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
if ( $this->runFromWrapper ) { |
| 578 |
$this->preloadScriptFile( $this->script ); |
| 579 |
} |
| 580 |
|
| 581 |
if ( !is_readable( $settingsFile ) ) { |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
if ( !defined( 'MW_CONFIG_CALLBACK' ) ) { |
| 586 |
define( 'MW_CONFIG_CALLBACK', __CLASS__ . '::emulateConfig' ); |
| 587 |
} |
| 588 |
$this->withoutLocalSettings = true; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
public static function emulateConfig( SettingsBuilder $settings ) { |
| 598 |
|
| 599 |
|
| 600 |
$settings->overrideConfigValues( [ |
| 601 |
|
| 602 |
MainConfigNames::Server => 'https://unknown.invalid', |
| 603 |
|
| 604 |
|
| 605 |
MainConfigNames::InvalidateCacheOnLocalSettingsChange => false, |
| 606 |
] ); |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
private function overrideConfig( SettingsBuilder $settingsBuilder ) { |
| 615 |
$config = $settingsBuilder->getConfig(); |
| 616 |
|
| 617 |
if ( $this->scriptObject->getDbType() === Maintenance::DB_NONE ) { |
| 618 |
$cacheConf = $config->get( MainConfigNames::LocalisationCacheConf ); |
| 619 |
if ( $cacheConf['storeClass'] === false |
| 620 |
&& ( $cacheConf['store'] == 'db' |
| 621 |
|| ( $cacheConf['store'] == 'detect' |
| 622 |
&& !$config->get( MainConfigNames::CacheDirectory ) ) ) |
| 623 |
) { |
| 624 |
$cacheConf['storeClass'] = LCStoreNull::class; |
| 625 |
$settingsBuilder->putConfigValue( MainConfigNames::LocalisationCacheConf, $cacheConf ); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
$output = $this->parameters->getOption( 'profiler' ); |
| 630 |
if ( $output ) { |
| 631 |
|
| 632 |
$profilerConf = $config->get( MainConfigNames::Profiler ); |
| 633 |
if ( isset( $profilerConf['class'] ) ) { |
| 634 |
$profilerConf = [ |
| 635 |
'sampling' => 1, |
| 636 |
'output' => [ $output ], |
| 637 |
'cliEnable' => true, |
| 638 |
] + $profilerConf; |
| 639 |
|
| 640 |
$settingsBuilder->putConfigValue( MainConfigNames::Profiler, $profilerConf ); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
$this->scriptObject->finalSetup( $settingsBuilder ); |
| 645 |
} |
| 646 |
|
| 647 |
private function getServiceContainer(): MediaWikiServices { |
| 648 |
return MediaWikiServices::getInstance(); |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
public function run(): bool { |
| 666 |
$config = $this->getConfig(); |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
$limits = $config->get( MainConfigNames::TrxProfilerLimits ); |
| 674 |
$trxProfiler = Profiler::instance()->getTransactionProfiler(); |
| 675 |
$trxProfiler->setLogger( LoggerFactory::getInstance( 'rdbms' ) ); |
| 676 |
$trxProfiler->setExpectations( $limits['Maintenance'], __METHOD__ ); |
| 677 |
Profiler::instance()->setAllowOutput(); |
| 678 |
|
| 679 |
|
| 680 |
$this->scriptObject->setConfig( $config ); |
| 681 |
|
| 682 |
|
| 683 |
$this->scriptObject->checkRequiredExtensions(); |
| 684 |
|
| 685 |
if ( $this->withoutLocalSettings && !$this->scriptObject->canExecuteWithoutLocalSettings() ) { |
| 686 |
$this->fatalError( |
| 687 |
"\nThe LocalSettings.php file was not found or is not readable.\n" . |
| 688 |
"Use --conf to specify an alternative config file.\n\n" |
| 689 |
); |
| 690 |
} |
| 691 |
|
| 692 |
if ( $this->scriptObject->getDbType() == Maintenance::DB_NONE || $this->withoutLocalSettings ) { |
| 693 |
|
| 694 |
|
| 695 |
MediaWikiServices::resetGlobalInstance( $config ); |
| 696 |
MediaWikiServices::getInstance()->disableStorage(); |
| 697 |
} |
| 698 |
|
| 699 |
$this->scriptObject->validateParamsAndArgs(); |
| 700 |
|
| 701 |
|
| 702 |
try { |
| 703 |
$success = $this->scriptObject->execute() !== false; |
| 704 |
|
| 705 |
|
| 706 |
if ( $this->parameters->hasOption( 'globals' ) ) { |
| 707 |
print_r( $GLOBALS ); |
| 708 |
} |
| 709 |
|
| 710 |
$this->shutdown(); |
| 711 |
|
| 712 |
return $success; |
| 713 |
} catch ( Exception $ex ) { |
| 714 |
$exReportMessage = ''; |
| 715 |
while ( $ex ) { |
| 716 |
$cls = get_class( $ex ); |
| 717 |
$exReportMessage .= "$cls from line {$ex->getLine()} of {$ex->getFile()}: {$ex->getMessage()}\n"; |
| 718 |
$exReportMessage .= $ex->getTraceAsString() . "\n"; |
| 719 |
$ex = $ex->getPrevious(); |
| 720 |
} |
| 721 |
$this->error( $exReportMessage ); |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
exit( 1 ); |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
protected function fatalError( $msg, $exitCode = 1 ) { |
| 738 |
$this->error( $msg ); |
| 739 |
exit( $exitCode ); |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
protected function error( string $msg ) { |
| 746 |
|
| 747 |
if ( defined( 'STDERR' ) ) { |
| 748 |
fwrite( STDERR, $msg ); |
| 749 |
} else { |
| 750 |
echo $msg; |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
public static function shouldExecute() { |
| 762 |
if ( !function_exists( 'debug_backtrace' ) ) { |
| 763 |
|
| 764 |
return MW_ENTRY_POINT === 'cli'; |
| 765 |
} |
| 766 |
|
| 767 |
$bt = debug_backtrace(); |
| 768 |
$count = count( $bt ); |
| 769 |
if ( $bt[0]['class'] !== self::class || $bt[0]['function'] !== 'shouldExecute' ) { |
| 770 |
return false; |
| 771 |
} |
| 772 |
$includeFuncs = [ 'require_once', 'require', 'include', 'include_once' ]; |
| 773 |
for ( $i = 1; $i < $count; $i++ ) { |
| 774 |
if ( !in_array( $bt[$i]['function'], $includeFuncs ) ) { |
| 775 |
return false; |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
return true; |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
public function cleanup() { |
| 788 |
if ( $this->scriptObject ) { |
| 789 |
$this->scriptObject->cleanupChanneled(); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
private function shutdown() { |
| 803 |
$lbFactory = null; |
| 804 |
if ( |
| 805 |
$this->scriptObject->getDbType() !== Maintenance::DB_NONE && |
| 806 |
|
| 807 |
!$this->getServiceContainer()->isServiceDisabled( 'DBLoadBalancerFactory' ) |
| 808 |
) { |
| 809 |
$lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory(); |
| 810 |
if ( $lbFactory->isReadyForRoundOperations() ) { |
| 811 |
$lbFactory->commitPrimaryChanges( get_class( $this ) ); |
| 812 |
} |
| 813 |
|
| 814 |
DeferredUpdates::doUpdates(); |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
$profiler = Profiler::instance(); |
| 820 |
$profiler->logData(); |
| 821 |
$profiler->logDataPageOutputOnly(); |
| 822 |
|
| 823 |
MediaWiki::emitBufferedStatsdData( |
| 824 |
$this->getServiceContainer()->getStatsdDataFactory(), |
| 825 |
$this->getConfig() |
| 826 |
); |
| 827 |
|
| 828 |
if ( $lbFactory ) { |
| 829 |
if ( $lbFactory->isReadyForRoundOperations() ) { |
| 830 |
$lbFactory->shutdown( $lbFactory::SHUTDOWN_NO_CHRONPROT ); |
| 831 |
} |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
} |