| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Maintenance; |
| 22 |
|
| 23 |
use ExecutableFinder; |
| 24 |
use MediaWiki; |
| 25 |
use MediaWiki\Config\Config; |
| 26 |
use MediaWiki\Debug\MWDebug; |
| 27 |
use MediaWiki\HookContainer\HookContainer; |
| 28 |
use MediaWiki\HookContainer\HookRunner; |
| 29 |
use MediaWiki\MainConfigNames; |
| 30 |
use MediaWiki\MediaWikiServices; |
| 31 |
use MediaWiki\Registration\ExtensionRegistry; |
| 32 |
use MediaWiki\Settings\SettingsBuilder; |
| 33 |
use MediaWiki\Shell\Shell; |
| 34 |
use MediaWiki\User\User; |
| 35 |
use StatusValue; |
| 36 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 37 |
use Wikimedia\Rdbms\IDatabase; |
| 38 |
use Wikimedia\Rdbms\IMaintainableDatabase; |
| 39 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
require_once __DIR__ . '/MaintenanceParameters.php'; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
abstract class Maintenance { |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public const DB_NONE = 0; |
| 80 |
public const DB_STD = 1; |
| 81 |
public const DB_ADMIN = 2; |
| 82 |
|
| 83 |
|
| 84 |
public const STDIN_ALL = -1; |
| 85 |
|
| 86 |
|
| 87 |
public const SCRIPT_DEPENDENT_PARAMETERS = 'Common options'; |
| 88 |
public const GENERIC_MAINTENANCE_PARAMETERS = 'Script runner options'; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
protected $parameters; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
protected $mParams = []; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
protected $mOptions = []; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
protected $mArgs = []; |
| 114 |
|
| 115 |
|
| 116 |
protected $mSelf; |
| 117 |
|
| 118 |
|
| 119 |
protected $mQuiet = false; |
| 120 |
protected ?string $mDbUser = null; |
| 121 |
protected ?string $mDbPass = null; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
protected $mDescription = ''; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
protected $mInputLoaded = false; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
protected $mBatchSize = null; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
private $mDb = null; |
| 148 |
|
| 149 |
|
| 150 |
private $lastReplicationWait = 0.0; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public $fileHandle; |
| 157 |
|
| 158 |
|
| 159 |
private $hookContainer; |
| 160 |
|
| 161 |
|
| 162 |
private $hookRunner; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
private $config; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
private $requiredExtensions = []; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public $orderedOptions = []; |
| 190 |
private ?IConnectionProvider $dbProvider = null; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public function __construct() { |
| 199 |
$this->parameters = new MaintenanceParameters(); |
| 200 |
$this->mOptions =& $this->parameters->getFieldReference( 'mOptions' ); |
| 201 |
$this->orderedOptions =& $this->parameters->getFieldReference( 'optionsSequence' ); |
| 202 |
$this->mArgs =& $this->parameters->getFieldReference( 'mArgs' ); |
| 203 |
$this->addDefaultParams(); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function getParameters() { |
| 211 |
return $this->parameters; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
abstract public function execute(); |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public function canExecuteWithoutLocalSettings(): bool { |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
protected function supportsOption( $name ) { |
| 251 |
return $this->parameters->supportsOption( $name ); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
protected function addOption( $name, $description, $required = false, |
| 266 |
$withArg = false, $shortName = false, $multiOccurrence = false |
| 267 |
) { |
| 268 |
$this->parameters->addOption( |
| 269 |
$name, |
| 270 |
$description, |
| 271 |
$required, |
| 272 |
$withArg, |
| 273 |
$shortName, |
| 274 |
$multiOccurrence |
| 275 |
); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
protected function hasOption( $name ) { |
| 285 |
return $this->parameters->hasOption( $name ); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
protected function getOption( $name, $default = null ) { |
| 300 |
return $this->parameters->getOption( $name, $default ); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
protected function addArg( $arg, $description, $required = true, $multi = false ) { |
| 311 |
$this->parameters->addArg( $arg, $description, $required, $multi ); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
protected function deleteOption( $name ) { |
| 319 |
$this->parameters->deleteOption( $name ); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
protected function setAllowUnregisteredOptions( $allow ) { |
| 328 |
$this->parameters->setAllowUnregisteredOptions( $allow ); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
protected function addDescription( $text ) { |
| 336 |
$this->parameters->setDescription( $text ); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
protected function hasArg( $argId = 0 ) { |
| 346 |
return $this->parameters->hasArg( $argId ); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
protected function getArg( $argId = 0, $default = null ) { |
| 358 |
return $this->parameters->getArg( $argId, $default ); |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
protected function getArgs( $offset = 0 ) { |
| 370 |
return $this->parameters->getArgs( $offset ); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
protected function getArgName( int $argId ): ?string { |
| 382 |
return $this->parameters->getArgName( $argId ); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
public function setOption( string $name, $value ): void { |
| 395 |
$this->parameters->setOption( $name, $value ); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public function setArg( $argId, $value ): void { |
| 408 |
$this->parameters->setArg( $argId, $value ); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
protected function getBatchSize() { |
| 419 |
return $this->mBatchSize; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
protected function setBatchSize( $s = 0 ) { |
| 426 |
$this->mBatchSize = $s; |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
if ( $this->mBatchSize ) { |
| 434 |
$this->addOption( 'batch-size', 'Run this many operations ' . |
| 435 |
'per batch, default: ' . $this->mBatchSize, false, true ); |
| 436 |
if ( $this->supportsOption( 'batch-size' ) ) { |
| 437 |
|
| 438 |
$this->parameters->assignGroup( self::SCRIPT_DEPENDENT_PARAMETERS, [ 'batch-size' ] ); |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public function getName() { |
| 448 |
return $this->mSelf; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
protected function getStdin( $len = null ) { |
| 458 |
if ( $len == self::STDIN_ALL ) { |
| 459 |
return file_get_contents( 'php://stdin' ); |
| 460 |
} |
| 461 |
$f = fopen( 'php://stdin', 'rt' ); |
| 462 |
if ( !$len ) { |
| 463 |
return $f; |
| 464 |
} |
| 465 |
$input = fgets( $f, $len ); |
| 466 |
fclose( $f ); |
| 467 |
|
| 468 |
return rtrim( $input ); |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
public function isQuiet() { |
| 475 |
return $this->mQuiet; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
protected function output( $out, $channel = null ) { |
| 486 |
|
| 487 |
if ( defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' ) ) { |
| 488 |
|
| 489 |
$stats = $this->getServiceContainer()->getStatsdDataFactory(); |
| 490 |
if ( $stats->getDataCount() > 1000 ) { |
| 491 |
MediaWiki::emitBufferedStatsdData( $stats, $this->getConfig() ); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
if ( $this->mQuiet ) { |
| 496 |
return; |
| 497 |
} |
| 498 |
if ( $channel === null ) { |
| 499 |
$this->cleanupChanneled(); |
| 500 |
print $out; |
| 501 |
} else { |
| 502 |
$out = preg_replace( '/\n\z/', '', $out ); |
| 503 |
$this->outputChanneled( $out, $channel ); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
protected function error( $err, $die = 0 ) { |
| 515 |
if ( intval( $die ) !== 0 ) { |
| 516 |
wfDeprecated( __METHOD__ . '( $err, $die )', '1.31' ); |
| 517 |
$this->fatalError( $err, intval( $die ) ); |
| 518 |
} |
| 519 |
if ( $err instanceof StatusValue ) { |
| 520 |
foreach ( [ 'warning' => 'Warning: ', 'error' => 'Error: ' ] as $type => $prefix ) { |
| 521 |
foreach ( $err->getMessages( $type ) as $msg ) { |
| 522 |
$this->error( |
| 523 |
$prefix . wfMessage( $msg ) |
| 524 |
->inLanguage( 'en' ) |
| 525 |
->useDatabase( false ) |
| 526 |
->text() |
| 527 |
); |
| 528 |
} |
| 529 |
} |
| 530 |
return; |
| 531 |
} |
| 532 |
$this->outputChanneled( false ); |
| 533 |
if ( |
| 534 |
( PHP_SAPI == 'cli' || PHP_SAPI == 'phpdbg' ) && |
| 535 |
!defined( 'MW_PHPUNIT_TEST' ) |
| 536 |
) { |
| 537 |
fwrite( STDERR, $err . "\n" ); |
| 538 |
} else { |
| 539 |
print $err . "\n"; |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
protected function fatalError( $msg, $exitCode = 1 ) { |
| 553 |
$this->error( $msg ); |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
if ( defined( 'MW_PHPUNIT_TEST' ) ) { |
| 558 |
throw new MaintenanceFatalError( $exitCode ); |
| 559 |
} else { |
| 560 |
exit( $exitCode ); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
private $atLineStart = true; |
| 566 |
|
| 567 |
private $lastChannel = null; |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
public function cleanupChanneled() { |
| 573 |
if ( !$this->atLineStart ) { |
| 574 |
print "\n"; |
| 575 |
$this->atLineStart = true; |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
public function outputChanneled( $msg, $channel = null ) { |
| 588 |
if ( $msg === false ) { |
| 589 |
$this->cleanupChanneled(); |
| 590 |
|
| 591 |
return; |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
if ( !$this->atLineStart && $channel !== $this->lastChannel ) { |
| 596 |
print "\n"; |
| 597 |
} |
| 598 |
|
| 599 |
print $msg; |
| 600 |
|
| 601 |
$this->atLineStart = false; |
| 602 |
if ( $channel === null ) { |
| 603 |
|
| 604 |
print "\n"; |
| 605 |
$this->atLineStart = true; |
| 606 |
} |
| 607 |
$this->lastChannel = $channel; |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
public function getDbType() { |
| 628 |
return self::DB_STD; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
protected function addDefaultParams() { |
| 635 |
|
| 636 |
|
| 637 |
$this->addOption( 'help', 'Display this help message', false, false, 'h' ); |
| 638 |
$this->addOption( 'quiet', 'Whether to suppress non-error output', false, false, 'q' ); |
| 639 |
|
| 640 |
|
| 641 |
$generic = [ 'help', 'quiet' ]; |
| 642 |
$this->parameters->assignGroup( self::GENERIC_MAINTENANCE_PARAMETERS, $generic ); |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
if ( $this->getDbType() > 0 ) { |
| 648 |
$this->addOption( 'dbuser', 'The DB user to use for this script', false, true ); |
| 649 |
$this->addOption( 'dbpass', 'The password to use for this script', false, true ); |
| 650 |
$this->addOption( 'dbgroupdefault', 'The default DB group to use.', false, true ); |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
$dependent = array_diff( |
| 656 |
$this->parameters->getOptionNames(), |
| 657 |
$generic |
| 658 |
); |
| 659 |
$this->parameters->assignGroup( self::SCRIPT_DEPENDENT_PARAMETERS, $dependent ); |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
public function getConfig() { |
| 668 |
if ( $this->config === null ) { |
| 669 |
$this->config = $this->getServiceContainer()->getMainConfig(); |
| 670 |
} |
| 671 |
|
| 672 |
return $this->config; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
protected function getServiceContainer() { |
| 682 |
return MediaWikiServices::getInstance(); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
public function setConfig( Config $config ) { |
| 690 |
$this->config = $config; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
protected function requireExtension( $name ) { |
| 703 |
$this->requiredExtensions[] = $name; |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
public function checkRequiredExtensions() { |
| 712 |
$registry = ExtensionRegistry::getInstance(); |
| 713 |
$missing = []; |
| 714 |
foreach ( $this->requiredExtensions as $name ) { |
| 715 |
if ( !$registry->isLoaded( $name ) ) { |
| 716 |
$missing[] = $name; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
if ( $missing ) { |
| 721 |
if ( count( $missing ) === 1 ) { |
| 722 |
$msg = 'The "' . $missing[ 0 ] . '" extension must be installed for this script to run. ' |
| 723 |
. 'Please enable it and then try again.'; |
| 724 |
} else { |
| 725 |
$msg = 'The following extensions must be installed for this script to run: "' |
| 726 |
. implode( '", "', $missing ) . '". Please enable them and then try again.'; |
| 727 |
} |
| 728 |
$this->fatalError( $msg ); |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
public function runChild( $maintClass, $classFile = null ) { |
| 745 |
MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'runChild', '1.43' ); |
| 746 |
return self::createChild( $maintClass, $classFile ); |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
public function createChild( string $maintClass, ?string $classFile = null ): Maintenance { |
| 762 |
|
| 763 |
if ( !class_exists( $maintClass ) ) { |
| 764 |
if ( $classFile ) { |
| 765 |
require_once $classFile; |
| 766 |
} |
| 767 |
if ( !class_exists( $maintClass ) ) { |
| 768 |
$this->fatalError( "Cannot spawn child: $maintClass" ); |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
$child = new $maintClass(); |
| 776 |
$child->loadParamsAndArgs( |
| 777 |
$this->mSelf, |
| 778 |
$this->parameters->getOptions(), |
| 779 |
$this->parameters->getArgs() |
| 780 |
); |
| 781 |
if ( $this->mDb !== null ) { |
| 782 |
$child->setDB( $this->mDb ); |
| 783 |
} |
| 784 |
if ( $this->dbProvider !== null ) { |
| 785 |
$child->setDBProvider( $this->dbProvider ); |
| 786 |
} |
| 787 |
|
| 788 |
return $child; |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
public function setup() { |
| 796 |
|
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
public function memoryLimit() { |
| 807 |
return 'max'; |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
public function clearParamsAndArgs() { |
| 814 |
$this->parameters->clear(); |
| 815 |
$this->mInputLoaded = false; |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
public function setName( string $name ) { |
| 824 |
$this->mSelf = $name; |
| 825 |
$this->parameters->setName( $this->mSelf ); |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
public function loadWithArgv( $argv ) { |
| 836 |
if ( $this->mDescription ) { |
| 837 |
$this->parameters->setDescription( $this->mDescription ); |
| 838 |
} |
| 839 |
|
| 840 |
$this->parameters->loadWithArgv( $argv ); |
| 841 |
|
| 842 |
if ( $this->parameters->hasErrors() ) { |
| 843 |
$errors = "\nERROR: " . implode( "\nERROR: ", $this->parameters->getErrors() ) . "\n"; |
| 844 |
$this->error( $errors ); |
| 845 |
$this->maybeHelp( true ); |
| 846 |
} |
| 847 |
|
| 848 |
$this->loadSpecialVars(); |
| 849 |
$this->mInputLoaded = true; |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
public function loadParamsAndArgs( $self = null, $opts = null, $args = null ) { |
| 860 |
|
| 861 |
if ( $self !== null || $opts !== null || $args !== null ) { |
| 862 |
if ( $self !== null ) { |
| 863 |
$this->mSelf = $self; |
| 864 |
$this->parameters->setName( $self ); |
| 865 |
} |
| 866 |
$this->parameters->setOptionsAndArgs( $opts ?? [], $args ?? [] ); |
| 867 |
$this->mInputLoaded = true; |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
if ( $this->mInputLoaded ) { |
| 873 |
$this->loadSpecialVars(); |
| 874 |
|
| 875 |
return; |
| 876 |
} |
| 877 |
|
| 878 |
global $argv; |
| 879 |
$this->mSelf = $argv[0]; |
| 880 |
$this->parameters->setName( $this->mSelf ); |
| 881 |
$this->loadWithArgv( array_slice( $argv, 1 ) ); |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
public function validateParamsAndArgs() { |
| 889 |
$valid = $this->parameters->validate(); |
| 890 |
|
| 891 |
$this->maybeHelp( !$valid ); |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
protected function loadSpecialVars() { |
| 899 |
if ( $this->hasOption( 'dbuser' ) ) { |
| 900 |
$this->mDbUser = $this->getOption( 'dbuser' ); |
| 901 |
} |
| 902 |
if ( $this->hasOption( 'dbpass' ) ) { |
| 903 |
$this->mDbPass = $this->getOption( 'dbpass' ); |
| 904 |
} |
| 905 |
if ( $this->hasOption( 'quiet' ) ) { |
| 906 |
$this->mQuiet = true; |
| 907 |
} |
| 908 |
if ( $this->hasOption( 'batch-size' ) ) { |
| 909 |
$this->mBatchSize = intval( $this->getOption( 'batch-size' ) ); |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
protected function maybeHelp( $force = false ) { |
| 919 |
if ( !$force && !$this->hasOption( 'help' ) ) { |
| 920 |
return; |
| 921 |
} |
| 922 |
|
| 923 |
if ( $this->parameters->hasErrors() && !$this->hasOption( 'help' ) ) { |
| 924 |
$errors = "\nERROR: " . implode( "\nERROR: ", $this->parameters->getErrors() ) . "\n"; |
| 925 |
$this->error( $errors ); |
| 926 |
} |
| 927 |
|
| 928 |
$this->showHelp(); |
| 929 |
$this->fatalError( '' ); |
| 930 |
} |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
protected function showHelp() { |
| 936 |
$this->mQuiet = false; |
| 937 |
$help = $this->parameters->getHelp(); |
| 938 |
$this->output( $help ); |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
public function finalSetup( SettingsBuilder $settingsBuilder ) { |
| 949 |
$config = $settingsBuilder->getConfig(); |
| 950 |
$overrides = []; |
| 951 |
$overrides['DBadminuser'] = $config->get( MainConfigNames::DBadminuser ); |
| 952 |
$overrides['DBadminpassword'] = $config->get( MainConfigNames::DBadminpassword ); |
| 953 |
|
| 954 |
|
| 955 |
if ( ob_get_level() ) { |
| 956 |
ob_end_flush(); |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
if ( $this->hasOption( 'server' ) ) { |
| 961 |
$overrides['Server'] = $this->getOption( 'server', $config->get( MainConfigNames::Server ) ); |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
if ( $this->mDbUser ) { |
| 966 |
$overrides['DBadminuser'] = $this->mDbUser; |
| 967 |
} |
| 968 |
if ( $this->mDbPass ) { |
| 969 |
$overrides['DBadminpassword'] = $this->mDbPass; |
| 970 |
} |
| 971 |
if ( $this->hasOption( 'dbgroupdefault' ) ) { |
| 972 |
$overrides['DBDefaultGroup'] = $this->getOption( 'dbgroupdefault', null ); |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
if ( MediaWikiServices::hasInstance() ) { |
| 978 |
$service = $this->getServiceContainer()->peekService( 'DBLoadBalancerFactory' ); |
| 979 |
if ( $service ) { |
| 980 |
$service->destroy(); |
| 981 |
} |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
if ( $this->getDbType() == self::DB_ADMIN && isset( $overrides[ 'DBadminuser' ] ) ) { |
| 986 |
$overrides['DBuser'] = $overrides[ 'DBadminuser' ]; |
| 987 |
$overrides['DBpassword'] = $overrides[ 'DBadminpassword' ]; |
| 988 |
|
| 989 |
|
| 990 |
$dbServers = $config->get( MainConfigNames::DBservers ); |
| 991 |
if ( $dbServers ) { |
| 992 |
foreach ( $dbServers as $i => $server ) { |
| 993 |
$dbServers[$i]['user'] = $overrides['DBuser']; |
| 994 |
$dbServers[$i]['password'] = $overrides['DBpassword']; |
| 995 |
} |
| 996 |
$overrides['DBservers'] = $dbServers; |
| 997 |
} |
| 998 |
|
| 999 |
$lbFactoryConf = $config->get( MainConfigNames::LBFactoryConf ); |
| 1000 |
if ( isset( $lbFactoryConf['serverTemplate'] ) ) { |
| 1001 |
$lbFactoryConf['serverTemplate']['user'] = $overrides['DBuser']; |
| 1002 |
$lbFactoryConf['serverTemplate']['password'] = $overrides['DBpassword']; |
| 1003 |
$overrides['LBFactoryConf'] = $lbFactoryConf; |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
if ( MediaWikiServices::hasInstance() ) { |
| 1011 |
$service = $this->getServiceContainer()->peekService( 'DBLoadBalancerFactory' ); |
| 1012 |
if ( $service ) { |
| 1013 |
$service->destroy(); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
$this->afterFinalSetup(); |
| 1019 |
|
| 1020 |
$overrides['ShowExceptionDetails'] = true; |
| 1021 |
$overrides['ShowHostname'] = true; |
| 1022 |
|
| 1023 |
ini_set( 'max_execution_time', '0' ); |
| 1024 |
$settingsBuilder->putConfigValues( $overrides ); |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
protected function afterFinalSetup() { |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
public function purgeRedundantText( $delete = true ) { |
| 1040 |
|
| 1041 |
$dbw = $this->getPrimaryDB(); |
| 1042 |
$this->beginTransaction( $dbw, __METHOD__ ); |
| 1043 |
|
| 1044 |
|
| 1045 |
$cur = []; |
| 1046 |
$this->output( 'Searching for active text records via contents table...' ); |
| 1047 |
$res = $dbw->newSelectQueryBuilder() |
| 1048 |
->select( 'content_address' ) |
| 1049 |
->distinct() |
| 1050 |
->from( 'content' ) |
| 1051 |
->caller( __METHOD__ )->fetchResultSet(); |
| 1052 |
$blobStore = $this->getServiceContainer()->getBlobStore(); |
| 1053 |
foreach ( $res as $row ) { |
| 1054 |
|
| 1055 |
$textId = $blobStore->getTextIdFromAddress( $row->content_address ); |
| 1056 |
if ( $textId ) { |
| 1057 |
$cur[] = $textId; |
| 1058 |
} |
| 1059 |
} |
| 1060 |
$this->output( "done.\n" ); |
| 1061 |
|
| 1062 |
|
| 1063 |
$this->output( 'Searching for inactive text records...' ); |
| 1064 |
$textTableQueryBuilder = $dbw->newSelectQueryBuilder() |
| 1065 |
->select( 'old_id' ) |
| 1066 |
->distinct() |
| 1067 |
->from( 'text' ); |
| 1068 |
if ( count( $cur ) ) { |
| 1069 |
$textTableQueryBuilder->where( $dbw->expr( 'old_id', '!=', $cur ) ); |
| 1070 |
} |
| 1071 |
$res = $textTableQueryBuilder |
| 1072 |
->caller( __METHOD__ ) |
| 1073 |
->fetchResultSet(); |
| 1074 |
$old = []; |
| 1075 |
foreach ( $res as $row ) { |
| 1076 |
$old[] = $row->old_id; |
| 1077 |
} |
| 1078 |
$this->output( "done.\n" ); |
| 1079 |
|
| 1080 |
|
| 1081 |
$count = count( $old ); |
| 1082 |
$this->output( "$count inactive items found.\n" ); |
| 1083 |
|
| 1084 |
|
| 1085 |
if ( $delete && $count ) { |
| 1086 |
$this->output( 'Deleting...' ); |
| 1087 |
$dbw->newDeleteQueryBuilder() |
| 1088 |
->deleteFrom( 'text' ) |
| 1089 |
->where( [ 'old_id' => $old ] ) |
| 1090 |
->caller( __METHOD__ ) |
| 1091 |
->execute(); |
| 1092 |
$this->output( "done.\n" ); |
| 1093 |
} |
| 1094 |
|
| 1095 |
$this->commitTransaction( $dbw, __METHOD__ ); |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
protected function getDir() { |
| 1103 |
return __DIR__ . '/../'; |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
protected function getDB( $db, $groups = [], $dbDomain = false ) { |
| 1123 |
if ( $this->mDb === null ) { |
| 1124 |
return $this->getServiceContainer() |
| 1125 |
->getDBLoadBalancerFactory() |
| 1126 |
->getMainLB( $dbDomain ) |
| 1127 |
->getMaintenanceConnectionRef( $db, $groups, $dbDomain ); |
| 1128 |
} |
| 1129 |
|
| 1130 |
return $this->mDb; |
| 1131 |
} |
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
public function setDB( IMaintainableDatabase $db ) { |
| 1140 |
$this->mDb = $db; |
| 1141 |
} |
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
protected function getReplicaDB(): IReadableDatabase { |
| 1148 |
if ( $this->dbProvider === null ) { |
| 1149 |
$this->dbProvider = $this->getServiceContainer()->getConnectionProvider(); |
| 1150 |
} |
| 1151 |
return $this->dbProvider->getReplicaDatabase(); |
| 1152 |
} |
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
protected function getPrimaryDB(): IDatabase { |
| 1159 |
if ( $this->dbProvider === null ) { |
| 1160 |
$this->dbProvider = $this->getServiceContainer()->getConnectionProvider(); |
| 1161 |
} |
| 1162 |
return $this->dbProvider->getPrimaryDatabase(); |
| 1163 |
} |
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
public function setDBProvider( IConnectionProvider $dbProvider ) { |
| 1171 |
$this->dbProvider = $dbProvider; |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
protected function beginTransaction( IDatabase $dbw, $fname ) { |
| 1185 |
$dbw->begin( $fname ); |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
protected function commitTransaction( IDatabase $dbw, $fname ) { |
| 1200 |
$dbw->commit( $fname ); |
| 1201 |
return $this->waitForReplication(); |
| 1202 |
} |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
protected function waitForReplication() { |
| 1213 |
$lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory(); |
| 1214 |
$waitSucceeded = $lbFactory->waitForReplication( |
| 1215 |
[ 'timeout' => 30, 'ifWritesSince' => $this->lastReplicationWait ] |
| 1216 |
); |
| 1217 |
$this->lastReplicationWait = microtime( true ); |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
$lbFactory->autoReconfigure(); |
| 1225 |
|
| 1226 |
return $waitSucceeded; |
| 1227 |
} |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
protected function rollbackTransaction( IDatabase $dbw, $fname ) { |
| 1240 |
$dbw->rollback( $fname ); |
| 1241 |
} |
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
protected function countDown( $seconds ) { |
| 1254 |
if ( $this->isQuiet() ) { |
| 1255 |
return; |
| 1256 |
} |
| 1257 |
for ( $i = $seconds; $i >= 0; $i-- ) { |
| 1258 |
if ( $i != $seconds ) { |
| 1259 |
$this->output( str_repeat( "\x08", strlen( (string)( $i + 1 ) ) ) ); |
| 1260 |
} |
| 1261 |
$this->output( (string)$i ); |
| 1262 |
if ( $i ) { |
| 1263 |
sleep( 1 ); |
| 1264 |
} |
| 1265 |
} |
| 1266 |
$this->output( "\n" ); |
| 1267 |
} |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
public static function posix_isatty( $fd ) { |
| 1278 |
if ( !function_exists( 'posix_isatty' ) ) { |
| 1279 |
return !$fd; |
| 1280 |
} |
| 1281 |
|
| 1282 |
return posix_isatty( $fd ); |
| 1283 |
} |
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
public static function readconsole( $prompt = '> ' ) { |
| 1291 |
static $isatty = null; |
| 1292 |
$isatty ??= self::posix_isatty( 0 ); |
| 1293 |
|
| 1294 |
if ( $isatty && function_exists( 'readline' ) ) { |
| 1295 |
return readline( $prompt ); |
| 1296 |
} |
| 1297 |
|
| 1298 |
if ( $isatty ) { |
| 1299 |
$st = self::readlineEmulation( $prompt ); |
| 1300 |
} elseif ( feof( STDIN ) ) { |
| 1301 |
$st = false; |
| 1302 |
} else { |
| 1303 |
$st = fgets( STDIN, 1024 ); |
| 1304 |
} |
| 1305 |
if ( $st === false ) { |
| 1306 |
return false; |
| 1307 |
} |
| 1308 |
|
| 1309 |
return trim( $st ); |
| 1310 |
} |
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
private static function readlineEmulation( $prompt ) { |
| 1318 |
$bash = ExecutableFinder::findInDefaultPaths( 'bash' ); |
| 1319 |
if ( !wfIsWindows() && $bash ) { |
| 1320 |
$encPrompt = Shell::escape( $prompt ); |
| 1321 |
$command = "read -er -p $encPrompt && echo \"\$REPLY\""; |
| 1322 |
$result = Shell::command( $bash, '-c', $command ) |
| 1323 |
->passStdin() |
| 1324 |
->forwardStderr() |
| 1325 |
->execute(); |
| 1326 |
|
| 1327 |
if ( $result->getExitCode() == 0 ) { |
| 1328 |
return $result->getStdout(); |
| 1329 |
} |
| 1330 |
|
| 1331 |
if ( $result->getExitCode() == 127 ) { |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
} else { |
| 1336 |
|
| 1337 |
return false; |
| 1338 |
} |
| 1339 |
} |
| 1340 |
|
| 1341 |
|
| 1342 |
if ( feof( STDIN ) ) { |
| 1343 |
return false; |
| 1344 |
} |
| 1345 |
print $prompt; |
| 1346 |
|
| 1347 |
return fgets( STDIN, 1024 ); |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
public static function getTermSize() { |
| 1358 |
static $termSize = null; |
| 1359 |
|
| 1360 |
if ( $termSize !== null ) { |
| 1361 |
return $termSize; |
| 1362 |
} |
| 1363 |
|
| 1364 |
$default = [ 80, 50 ]; |
| 1365 |
|
| 1366 |
if ( wfIsWindows() || Shell::isDisabled() ) { |
| 1367 |
$termSize = $default; |
| 1368 |
|
| 1369 |
return $termSize; |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
$result = Shell::command( 'stty', 'size' )->passStdin()->execute(); |
| 1380 |
if ( $result->getExitCode() !== 0 || |
| 1381 |
!preg_match( '/^(\d+) (\d+)$/', $result->getStdout(), $m ) |
| 1382 |
) { |
| 1383 |
$termSize = $default; |
| 1384 |
|
| 1385 |
return $termSize; |
| 1386 |
} |
| 1387 |
|
| 1388 |
$termSize = [ intval( $m[2] ), intval( $m[1] ) ]; |
| 1389 |
|
| 1390 |
return $termSize; |
| 1391 |
} |
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
public static function requireTestsAutoloader() { |
| 1400 |
require_once __DIR__ . '/../../tests/common/TestsAutoLoader.php'; |
| 1401 |
} |
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
protected function getHookContainer() { |
| 1410 |
if ( !$this->hookContainer ) { |
| 1411 |
$this->hookContainer = $this->getServiceContainer()->getHookContainer(); |
| 1412 |
} |
| 1413 |
return $this->hookContainer; |
| 1414 |
} |
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
protected function getHookRunner() { |
| 1425 |
if ( !$this->hookRunner ) { |
| 1426 |
$this->hookRunner = new HookRunner( $this->getHookContainer() ); |
| 1427 |
} |
| 1428 |
return $this->hookRunner; |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
protected function parseIntList( $text ) { |
| 1442 |
$ids = preg_split( '/[\s,;:|]+/', $text ); |
| 1443 |
$ids = array_map( |
| 1444 |
static function ( $id ) { |
| 1445 |
return (int)$id; |
| 1446 |
}, |
| 1447 |
$ids |
| 1448 |
); |
| 1449 |
return array_filter( $ids ); |
| 1450 |
} |
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
protected function validateUserOption( $errorMsg ) { |
| 1460 |
if ( $this->hasOption( "user" ) ) { |
| 1461 |
$user = User::newFromName( $this->getOption( 'user' ) ); |
| 1462 |
} elseif ( $this->hasOption( "userid" ) ) { |
| 1463 |
$user = User::newFromId( $this->getOption( 'userid' ) ); |
| 1464 |
} else { |
| 1465 |
$this->fatalError( $errorMsg ); |
| 1466 |
} |
| 1467 |
if ( !$user || !$user->isRegistered() ) { |
| 1468 |
if ( $this->hasOption( "user" ) ) { |
| 1469 |
$this->fatalError( "No such user: " . $this->getOption( 'user' ) ); |
| 1470 |
} elseif ( $this->hasOption( "userid" ) ) { |
| 1471 |
$this->fatalError( "No such user id: " . $this->getOption( 'userid' ) ); |
| 1472 |
} |
| 1473 |
} |
| 1474 |
|
| 1475 |
return $user; |
| 1476 |
} |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
protected function prompt( string $prompt, ?string $default = null ): ?string { |
| 1487 |
$defaultText = $default === null ? ' > ' : " [{$default}] > "; |
| 1488 |
$promptWithDefault = $prompt . $defaultText; |
| 1489 |
$line = self::readconsole( $promptWithDefault ); |
| 1490 |
if ( $line === false ) { |
| 1491 |
return $default; |
| 1492 |
} |
| 1493 |
if ( $line === '' ) { |
| 1494 |
return $default; |
| 1495 |
} |
| 1496 |
|
| 1497 |
return $line; |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
|
| 1502 |
class_alias( Maintenance::class, 'Maintenance' ); |