3 * DBMS-specific installation helper.
10 * Base class for DBMS-specific installation helper classes.
15 abstract class DatabaseInstaller
{
18 * The Installer object.
20 * TODO: naming this parent is confusing, 'installer' would be clearer.
27 * The database connection.
34 * Internal variables for installation.
38 protected $internalDefaults = array();
41 * Array of MW configuration globals this class uses.
45 protected $globalNames = array();
48 * Return the internal name, e.g. 'mysql', or 'sqlite'.
50 public abstract function getName();
53 * @return true if the client library is compiled in.
55 public abstract function isCompiled();
58 * Get HTML for a web form that configures this database. Configuration
59 * at this time should be the minimum needed to connect and test
60 * whether install or upgrade is required.
62 * If this is called, $this->parent can be assumed to be a WebInstaller.
64 public abstract function getConnectForm();
67 * Set variables based on the request array, assuming it was submitted
68 * via the form returned by getConnectForm(). Validate the connection
69 * settings by attempting to connect with them.
71 * If this is called, $this->parent can be assumed to be a WebInstaller.
75 public abstract function submitConnectForm();
78 * Get HTML for a web form that retrieves settings used for installation.
79 * $this->parent can be assumed to be a WebInstaller.
80 * If the DB type has no settings beyond those already configured with
81 * getConnectForm(), this should return false.
83 public function getSettingsForm() {
88 * Set variables based on the request array, assuming it was submitted via
89 * the form return by getSettingsForm().
93 public function submitSettingsForm() {
94 return Status
::newGood();
98 * Open a connection to the database using the administrative user/password
99 * currently defined in the session, without any caching. Returns a status
100 * object. On success, the status object will contain a Database object in
105 public abstract function openConnection();
108 * Create the database and return a Status object indicating success or
113 public abstract function setupDatabase();
116 * Connect to the database using the administrative user/password currently
117 * defined in the session. Returns a status object. On success, the status
118 * object will contain a Database object in its value member.
120 * This will return a cached connection if one is available.
122 * @return DatabaseBase
124 public function getConnection() {
126 return Status
::newGood( $this->db
);
128 $status = $this->openConnection();
129 if ( $status->isOK() ) {
130 $this->db
= $status->value
;
132 $this->db
->clearFlag( DBO_TRX
);
139 * Create database tables from scratch.
143 public function createTables() {
144 $status = $this->getConnection();
145 if ( !$status->isOK() ) {
148 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
150 if( $this->db
->tableExists( 'user' ) ) {
151 $status->warning( 'config-install-tables-exist' );
155 $this->db
->setFlag( DBO_DDLMODE
); // For Oracle's handling of schema files
156 $this->db
->begin( __METHOD__
);
158 $error = $this->db
->sourceFile( $this->db
->getSchema() );
159 if( $error !== true ) {
160 $this->db
->reportQueryError( $error, 0, '', __METHOD__
);
161 $this->db
->rollback( __METHOD__
);
162 $status->fatal( 'config-install-tables-failed', $error );
164 $this->db
->commit( __METHOD__
);
166 // Resume normal operations
167 if( $status->isOk() ) {
174 * Create the tables for each extension the user enabled
177 public function createExtensionTables() {
178 $status = $this->getConnection();
179 if ( !$status->isOK() ) {
183 $updater = DatabaseUpdater
::newForDB( $this->db
);
184 $extensionUpdates = $updater->getNewExtensions();
186 // No extensions need tables (or haven't updated to new installer support)
187 if( !count( $extensionUpdates ) ) {
191 $ourExtensions = array_map( 'strtolower', $this->getVar( '_Extensions' ) );
193 foreach( $ourExtensions as $ext ) {
194 if( isset( $extensionUpdates[$ext] ) ) {
195 $this->db
->begin( __METHOD__
);
196 $error = $this->db
->sourceFile( $extensionUpdates[$ext] );
197 if( $error !== true ) {
198 $this->db
->rollback( __METHOD__
);
199 $status->warning( 'config-install-tables-failed', $error );
201 $this->db
->commit( __METHOD__
);
209 * Create the tables for each extension the user enabled
212 public function createExtensionTables() {
213 $status = $this->getConnection();
214 if ( !$status->isOK() ) {
218 $updater = DatabaseUpdater
::newForDB( $this->db
);
219 $extensionUpdates = $updater->getNewExtensions();
221 // No extensions need tables (or haven't updated to new installer support)
222 if( !count( $extensionUpdates ) ) {
226 $ourExtensions = array_map( 'strtolower', $this->getVar( '_Extensions' ) );
228 foreach( $ourExtensions as $ext ) {
229 if( isset( $extensionUpdates[$ext] ) ) {
230 $this->db
->begin( __METHOD__
);
231 $error = $this->db
->sourceFile( $extensionUpdates[$ext] );
232 if( $error !== true ) {
233 $this->db
->rollback( __METHOD__
);
234 $status->warning( 'config-install-tables-failed', $error );
236 $this->db
->commit( __METHOD__
);
244 * Create the tables for each extension the user enabled
247 public function createExtensionTables() {
248 $status = $this->getConnection();
249 if ( !$status->isOK() ) {
253 $updater = DatabaseUpdater
::newForDB( $this->db
);
254 $extensionUpdates = $updater->getNewExtensions();
256 // No extensions need tables (or haven't updated to new installer support)
257 if( !count( $extensionUpdates ) ) {
261 $ourExtensions = array_map( 'strtolower', $this->getVar( '_Extensions' ) );
263 foreach( $ourExtensions as $ext ) {
264 if( isset( $extensionUpdates[$ext] ) ) {
265 $this->db
->begin( __METHOD__
);
266 $error = $this->db
->sourceFile( $extensionUpdates[$ext] );
267 if( $error !== true ) {
268 $this->db
->rollback( __METHOD__
);
269 $status->warning( 'config-install-tables-failed', $error );
271 $this->db
->commit( __METHOD__
);
279 * Get the DBMS-specific options for LocalSettings.php generation.
283 public abstract function getLocalSettings();
286 * Override this to provide DBMS-specific schema variables, to be
287 * substituted into tables.sql and other schema files.
289 public function getSchemaVars() {
294 * Set appropriate schema variables in the current database connection.
296 * This should be called after any request data has been imported, but before
297 * any write operations to the database.
299 public function setupSchemaVars() {
300 $status = $this->getConnection();
301 if ( $status->isOK() ) {
302 $status->value
->setSchemaVars( $this->getSchemaVars() );
304 throw new MWException( __METHOD__
.': unexpected DB connection error' );
309 * Set up LBFactory so that wfGetDB() etc. works.
310 * We set up a special LBFactory instance which returns the current
311 * installer connection.
313 public function enableLB() {
314 $status = $this->getConnection();
315 if ( !$status->isOK() ) {
316 throw new MWException( __METHOD__
.': unexpected DB connection error' );
318 LBFactory
::setInstance( new LBFactory_Single( array(
319 'connection' => $status->value
) ) );
323 * Perform database upgrades
327 public function doUpgrade() {
328 $this->setupSchemaVars();
332 ob_start( array( $this, 'outputHandler' ) );
334 $up = DatabaseUpdater
::newForDB( $this->db
);
336 } catch ( MWException
$e ) {
337 echo "\nAn error occured:\n";
346 * Allow DB installers a chance to make last-minute changes before installation
347 * occurs. This happens before setupDatabase() or createTables() is called, but
348 * long after the constructor. Helpful for things like modifying setup steps :)
350 public function preInstall() {
355 * Allow DB installers a chance to make checks before upgrade.
357 public function preUpgrade() {
362 * Get an array of MW configuration globals that will be configured by this class.
364 public function getGlobalNames() {
365 return $this->globalNames
;
369 * Construct and initialise parent.
370 * This is typically only called from Installer::getDBInstaller()
372 public function __construct( $parent ) {
373 $this->parent
= $parent;
377 * Convenience function.
378 * Check if a named extension is present.
382 protected static function checkExtension( $name ) {
383 wfSuppressWarnings();
384 $compiled = wfDl( $name );
390 * Get the internationalised name for this DBMS.
392 public function getReadableName() {
393 return wfMsg( 'config-type-' . $this->getName() );
397 * Get a name=>value map of MW configuration globals that overrides.
398 * DefaultSettings.php
400 public function getGlobalDefaults() {
405 * Get a name=>value map of internal variables used during installation.
407 public function getInternalDefaults() {
408 return $this->internalDefaults
;
412 * Get a variable, taking local defaults into account.
414 public function getVar( $var, $default = null ) {
415 $defaults = $this->getGlobalDefaults();
416 $internal = $this->getInternalDefaults();
417 if ( isset( $defaults[$var] ) ) {
418 $default = $defaults[$var];
419 } elseif ( isset( $internal[$var] ) ) {
420 $default = $internal[$var];
422 return $this->parent
->getVar( $var, $default );
426 * Convenience alias for $this->parent->setVar()
428 public function setVar( $name, $value ) {
429 $this->parent
->setVar( $name, $value );
433 * Get a labelled text box to configure a local variable.
435 public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
436 $name = $this->getName() . '_' . $var;
437 $value = $this->getVar( $var );
438 if ( !isset( $attribs ) ) {
441 return $this->parent
->getTextBox( array(
444 'attribs' => $attribs,
445 'controlName' => $name,
452 * Get a labelled password box to configure a local variable.
453 * Implements password hiding.
455 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
456 $name = $this->getName() . '_' . $var;
457 $value = $this->getVar( $var );
458 if ( !isset( $attribs ) ) {
461 return $this->parent
->getPasswordBox( array(
464 'attribs' => $attribs,
465 'controlName' => $name,
472 * Get a labelled checkbox to configure a local boolean variable.
474 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
475 $name = $this->getName() . '_' . $var;
476 $value = $this->getVar( $var );
477 return $this->parent
->getCheckBox( array(
480 'attribs' => $attribs,
481 'controlName' => $name,
488 * Get a set of labelled radio buttons.
490 * @param $params Array:
492 * var: The variable to be configured (required)
493 * label: The message name for the label (required)
494 * itemLabelPrefix: The message name prefix for the item labels (required)
495 * values: List of allowed values (required)
496 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
499 public function getRadioSet( $params ) {
500 $params['controlName'] = $this->getName() . '_' . $params['var'];
501 $params['value'] = $this->getVar( $params['var'] );
502 return $this->parent
->getRadioSet( $params );
506 * Convenience function to set variables based on form data.
507 * Assumes that variables containing "password" in the name are (potentially
509 * @param $varNames Array
511 public function setVarsFromRequest( $varNames ) {
512 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
516 * Determine whether an existing installation of MediaWiki is present in
517 * the configured administrative connection. Returns true if there is
518 * such a wiki, false if the database doesn't exist.
520 * Traditionally, this is done by testing for the existence of either
521 * the revision table or the cur table.
525 public function needsUpgrade() {
526 $status = $this->getConnection();
527 if ( !$status->isOK() ) {
531 if ( !$this->db
->selectDB( $this->getVar( 'wgDBname' ) ) ) {
534 return $this->db
->tableExists( 'cur' ) ||
$this->db
->tableExists( 'revision' );
538 * Get a standard install-user fieldset.
540 public function getInstallUserBox() {
542 Html
::openElement( 'fieldset' ) .
543 Html
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
544 $this->getTextBox( '_InstallUser', 'config-db-username', array(), $this->parent
->getHelpBox( 'config-db-install-username' ) ) .
545 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array(), $this->parent
->getHelpBox( 'config-db-install-password' ) ) .
546 Html
::closeElement( 'fieldset' );
550 * Submit a standard install user fieldset.
552 public function submitInstallUserBox() {
553 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
554 return Status
::newGood();
558 * Get a standard web-user fieldset
559 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
560 * Set this to false to show a creation checkbox.
562 public function getWebUserBox( $noCreateMsg = false ) {
563 $s = Html
::openElement( 'fieldset' ) .
564 Html
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
566 '_SameAccount', 'config-db-web-account-same',
567 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
569 Html
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
570 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
571 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
572 $this->parent
->getHelpBox( 'config-db-web-help' );
573 if ( $noCreateMsg ) {
574 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
576 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
578 $s .= Html
::closeElement( 'div' ) . Html
::closeElement( 'fieldset' );
583 * Submit the form from getWebUserBox().
587 public function submitWebUserBox() {
588 $this->setVarsFromRequest(
589 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
592 if ( $this->getVar( '_SameAccount' ) ) {
593 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
594 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
597 return Status
::newGood();
601 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
603 public function populateInterwikiTable() {
604 $status = $this->getConnection();
605 if ( !$status->isOK() ) {
608 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
610 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
611 $status->warning( 'config-install-interwiki-exists' );
615 $rows = file( "$IP/maintenance/interwiki.list",
616 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
617 $interwikis = array();
619 return Status
::newFatal( 'config-install-interwiki-sql' );
621 foreach( $rows as $row ) {
622 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
623 if ( $row == "" ) continue;
625 $interwikis[] = array_combine(
626 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
630 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
631 return Status
::newGood();
634 public function outputHandler( $string ) {
635 return htmlspecialchars( $string );