$maintClass = false;
use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\MediaWikiServices;
/**
* Abstract maintenance class for quickly writing and churning out
*/
private $config;
+ /**
+ * @see Maintenance::requireExtension
+ * @var array
+ */
+ private $requiredExtensions = [];
+
/**
* Used to read the options in the order they were passed.
* Useful for option chaining (Ex. dumpBackup.php). It will
$this->config = $config;
}
+ /**
+ * Indicate that the specified extension must be
+ * loaded before the script can run.
+ *
+ * This *must* be called in the constructor.
+ *
+ * @since 1.28
+ * @param string $name
+ */
+ protected function requireExtension( $name ) {
+ $this->requiredExtensions[] = $name;
+ }
+
+ /**
+ * Verify that the required extensions are installed
+ *
+ * @since 1.28
+ */
+ public function checkRequiredExtensions() {
+ $registry = ExtensionRegistry::getInstance();
+ $missing = [];
+ foreach ( $this->requiredExtensions as $name ) {
+ if ( !$registry->isLoaded( $name ) ) {
+ $missing[] = $name;
+ }
+ }
+
+ if ( $missing ) {
+ $joined = implode( ', ', $missing );
+ $msg = "The following extensions are required to be installed "
+ . "for this script to run: $joined. Please enable them and then try again.";
+ $this->error( $msg, 1 );
+ }
+
+ }
+
+ /**
+ * Set triggers like when to try to run deferred updates
+ * @since 1.28
+ */
+ public function setTriggers() {
+ $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+ self::setLBFactoryTriggers( $lbFactory );
+ }
+
+ /**
+ * @param LBFactory $LBFactory
+ * @since 1.28
+ */
+ public static function setLBFactoryTriggers( LBFactory $LBFactory ) {
+ // Hook into period lag checks which often happen in long-running scripts
+ $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+ $lbFactory->setWaitForReplicationListener(
+ __METHOD__,
+ function () {
+ global $wgCommandLineMode;
+ // Check config in case of JobRunner and unit tests
+ if ( $wgCommandLineMode ) {
+ DeferredUpdates::tryOpportunisticExecute( 'run' );
+ }
+ }
+ );
+ // Check for other windows to run them. A script may read or do a few writes
+ // to the master but mostly be writing to something else, like a file store.
+ $lbFactory->getMainLB()->setTransactionListener(
+ __METHOD__,
+ function ( $trigger ) {
+ global $wgCommandLineMode;
+ // Check config in case of JobRunner and unit tests
+ if ( $wgCommandLineMode && $trigger === IDatabase::TRIGGER_COMMIT ) {
+ DeferredUpdates::tryOpportunisticExecute( 'run' );
+ }
+ }
+ );
+ }
+
/**
* Run a child maintenance script. Pass all of the current arguments
* to it.
// Description ...
if ( $this->mDescription ) {
- $this->output( "\n" . $this->mDescription . "\n" );
+ $this->output( "\n" . wordwrap( $this->mDescription, $screenWidth ) . "\n" );
}
$output = "\nUsage: php " . basename( $this->mSelf );
* If not set, wfGetDB() will be used.
* This function has the same parameters as wfGetDB()
*
- * @param integer $db DB index (DB_SLAVE/DB_MASTER)
+ * @param integer $db DB index (DB_REPLICA/DB_MASTER)
* @param array $groups; default: empty array
* @param string|bool $wiki; default: current wiki
* @return IDatabase
}
/**
- * Commit the transcation on a DB handle and wait for slaves to catch up
+ * Commit the transcation on a DB handle and wait for replica DBs to catch up
*
* This method makes it clear that commit() is called from a maintenance script,
* which has outermost scope. This is safe, unlike $dbw->commit() called in other places.
*
* @param IDatabase $dbw
* @param string $fname Caller name
- * @return bool Whether the slave wait succeeded
+ * @return bool Whether the replica DB wait succeeded
* @since 1.27
*/
protected function commitTransaction( IDatabase $dbw, $fname ) {