Daniel Kinzler has uploaded a new change for review.
https://gerrit.wikimedia.org/r/250430
Change subject: Allow maintenance scripts to be created via a constructor
callback.
......................................................................
Allow maintenance scripts to be created via a constructor callback.
This allows dependency injection for maintenance scripts.
The ImportSites script is refactored to use injection to illustrate
the point.
Change-Id: I237e65bd378e727d2450e54e7d9d338125a17a2b
---
M maintenance/Maintenance.php
M maintenance/doMaintenance.php
M maintenance/importSites.php
3 files changed, 61 insertions(+), 21 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/30/250430/2
diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php
index 8dad5dc..ae04bec 100644
--- a/maintenance/Maintenance.php
+++ b/maintenance/Maintenance.php
@@ -35,6 +35,7 @@
define( 'DO_MAINTENANCE', RUN_MAINTENANCE_IF_MAIN ); // original name, harmless
$maintClass = false;
+$maintConstructor = false;
use MediaWiki\Logger\LoggerFactory;
@@ -485,32 +486,53 @@
}
/**
- * Run a child maintenance script. Pass all of the current arguments
+ * Create and run a child maintenance script. Pass all of the current
arguments
* to it.
- * @param string $maintClass A name of a child maintenance class
- * @param string $classFile Full path of where the child is
+ * @param string|callable $maintScript A name of a child maintenance
class,
+ * or a constructor callback.
+ * @param string|null $classFile Full path of where the maintenance
class is defined.
* @return Maintenance
*/
- public function runChild( $maintClass, $classFile = null ) {
- // Make sure the class is loaded first
- if ( !class_exists( $maintClass ) ) {
+ public function runChild( $maintScript, $classFile = null ) {
+ $child = $this->createChild( $maintScript, $classFile );
+ $child->loadParamsAndArgs( $this->mSelf, $this->mOptions,
$this->mArgs );
+ if ( !is_null( $this->mDb ) ) {
+ $child->setDB( $this->mDb );
+ }
+
+ return $child;
+ }
+
+ /**
+ * Create a child maintenance script.
+ *
+ * @param string|callable $maintScript A name of a child maintenance
class,
+ * or a constructor callback.
+ * @param string|null $classFile Full path of where the maintenance
class is defined.
+ * @return Maintenance
+ */
+ public function createChild( $maintScript, $classFile = null ) {
+ if ( !is_string( $maintScript ) && is_callable( $maintScript )
) {
if ( $classFile ) {
require_once $classFile;
}
- if ( !class_exists( $maintClass ) ) {
- $this->error( "Cannot spawn child: $maintClass"
);
+ return call_user_func( $maintScript );
+ }
+
+ // Make sure the class is loaded first
+ if ( !class_exists( $maintScript ) ) {
+ if ( $classFile ) {
+ require_once $classFile;
+ }
+ if ( !class_exists( $maintScript ) ) {
+ $this->error( "Cannot spawn child:
$maintScript" );
}
}
/**
* @var $child Maintenance
*/
- $child = new $maintClass();
- $child->loadParamsAndArgs( $this->mSelf, $this->mOptions,
$this->mArgs );
- if ( !is_null( $this->mDb ) ) {
- $child->setDB( $this->mDb );
- }
-
+ $child = new $maintScript();
return $child;
}
diff --git a/maintenance/doMaintenance.php b/maintenance/doMaintenance.php
index e66b729..8f047e5 100644
--- a/maintenance/doMaintenance.php
+++ b/maintenance/doMaintenance.php
@@ -38,14 +38,18 @@
return;
}
-if ( !$maintClass || !class_exists( $maintClass ) ) {
+if ( !$maintConstructor && ( !$maintClass || !class_exists( $maintClass ) ) ) {
echo "\$maintClass is not set or is set to a non-existent class.\n";
exit( 1 );
}
// Get an object to start us off
/** @var Maintenance $maintenance */
-$maintenance = new $maintClass();
+if ( $maintConstructor ) {
+ $maintenance = call_user_func( $maintConstructor );
+} else {
+ $maintenance = new $maintClass();
+}
// Basic sanity checks and such
$maintenance->setup();
diff --git a/maintenance/importSites.php b/maintenance/importSites.php
index 7cd2000..ad4b6fc 100644
--- a/maintenance/importSites.php
+++ b/maintenance/importSites.php
@@ -14,7 +14,15 @@
*/
class ImportSites extends Maintenance {
- public function __construct() {
+ /**
+ * @var SiteImporter
+ */
+ private $importer;
+
+ /**
+ * @param SiteImporter $importer
+ */
+ public function __construct( SiteImporter $importer ) {
$this->mDescription = 'Imports site definitions from XML into
the sites table.';
$this->addArg( 'file', 'An XML file containing site definitions
(see docs/sitelist.txt). ' .
@@ -22,6 +30,8 @@
);
parent::__construct();
+
+ $this->importer = $importer;
}
@@ -31,10 +41,9 @@
public function execute() {
$file = $this->getArg( 0 );
- $importer = new SiteImporter( SiteSQLStore::newInstance() );
- $importer->setExceptionCallback( array( $this,
'reportException' ) );
+ $this->importer->setExceptionCallback( array( $this,
'reportException' ) );
- $importer->importFromFile( $file );
+ $this->importer->importFromFile( $file );
$this->output( "Done.\n" );
}
@@ -50,5 +59,10 @@
}
}
-$maintClass = 'ImportSites';
+$maintConstructor = function() {
+ return new ImportSites(
+ new SiteImporter(
MediaWikiServices::getInstance()->getSiteStore() )
+ );
+};
+
require_once RUN_MAINTENANCE_IF_MAIN;
--
To view, visit https://gerrit.wikimedia.org/r/250430
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I237e65bd378e727d2450e54e7d9d338125a17a2b
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits