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.
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' );
156 $this->db
->setFlag( DBO_DDLMODE
); // For Oracle's handling of schema files
157 $this->db
->begin( __METHOD__
);
159 $error = $this->db
->sourceFile( $this->db
->getSchema() );
160 if( $error !== true ) {
161 $this->db
->reportQueryError( $error, 0, '', __METHOD__
);
162 $this->db
->rollback( __METHOD__
);
163 $status->fatal( 'config-install-tables-failed', $error );
165 $this->db
->commit( __METHOD__
);
167 // Resume normal operations
168 if( $status->isOk() ) {
175 * Create the tables for each extension the user enabled
178 public function createExtensionTables() {
179 $status = $this->getConnection();
180 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__
);
206 // Now run updates to create tables for old extensions
207 $updater->doUpdates( array( 'extensions' ) );
213 * Get the DBMS-specific options for LocalSettings.php generation.
217 public abstract function getLocalSettings();
220 * Override this to provide DBMS-specific schema variables, to be
221 * substituted into tables.sql and other schema files.
223 public function getSchemaVars() {
228 * Set appropriate schema variables in the current database connection.
230 * This should be called after any request data has been imported, but before
231 * any write operations to the database.
233 public function setupSchemaVars() {
234 $status = $this->getConnection();
235 if ( $status->isOK() ) {
236 $status->value
->setSchemaVars( $this->getSchemaVars() );
238 throw new MWException( __METHOD__
.': unexpected DB connection error' );
243 * Set up LBFactory so that wfGetDB() etc. works.
244 * We set up a special LBFactory instance which returns the current
245 * installer connection.
247 public function enableLB() {
248 $status = $this->getConnection();
249 if ( !$status->isOK() ) {
250 throw new MWException( __METHOD__
.': unexpected DB connection error' );
252 LBFactory
::setInstance( new LBFactory_Single( array(
253 'connection' => $status->value
) ) );
257 * Perform database upgrades
261 public function doUpgrade() {
262 $this->setupSchemaVars();
266 ob_start( array( $this, 'outputHandler' ) );
268 $up = DatabaseUpdater
::newForDB( $this->db
);
270 } catch ( MWException
$e ) {
271 echo "\nAn error occured:\n";
280 * Allow DB installers a chance to make last-minute changes before installation
281 * occurs. This happens before setupDatabase() or createTables() is called, but
282 * long after the constructor. Helpful for things like modifying setup steps :)
284 public function preInstall() {
289 * Allow DB installers a chance to make checks before upgrade.
291 public function preUpgrade() {
296 * Get an array of MW configuration globals that will be configured by this class.
298 public function getGlobalNames() {
299 return $this->globalNames
;
303 * Construct and initialise parent.
304 * This is typically only called from Installer::getDBInstaller()
306 public function __construct( $parent ) {
307 $this->parent
= $parent;
311 * Convenience function.
312 * Check if a named extension is present.
316 protected static function checkExtension( $name ) {
317 wfSuppressWarnings();
318 $compiled = wfDl( $name );
324 * Get the internationalised name for this DBMS.
326 public function getReadableName() {
327 return wfMsg( 'config-type-' . $this->getName() );
331 * Get a name=>value map of MW configuration globals that overrides.
332 * DefaultSettings.php
334 public function getGlobalDefaults() {
339 * Get a name=>value map of internal variables used during installation.
341 public function getInternalDefaults() {
342 return $this->internalDefaults
;
346 * Get a variable, taking local defaults into account.
348 public function getVar( $var, $default = null ) {
349 $defaults = $this->getGlobalDefaults();
350 $internal = $this->getInternalDefaults();
351 if ( isset( $defaults[$var] ) ) {
352 $default = $defaults[$var];
353 } elseif ( isset( $internal[$var] ) ) {
354 $default = $internal[$var];
356 return $this->parent
->getVar( $var, $default );
360 * Convenience alias for $this->parent->setVar()
362 public function setVar( $name, $value ) {
363 $this->parent
->setVar( $name, $value );
367 * Get a labelled text box to configure a local variable.
371 public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
372 $name = $this->getName() . '_' . $var;
373 $value = $this->getVar( $var );
374 if ( !isset( $attribs ) ) {
377 return $this->parent
->getTextBox( array(
380 'attribs' => $attribs,
381 'controlName' => $name,
388 * Get a labelled password box to configure a local variable.
389 * Implements password hiding.
393 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
394 $name = $this->getName() . '_' . $var;
395 $value = $this->getVar( $var );
396 if ( !isset( $attribs ) ) {
399 return $this->parent
->getPasswordBox( array(
402 'attribs' => $attribs,
403 'controlName' => $name,
410 * Get a labelled checkbox to configure a local boolean variable.
414 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
415 $name = $this->getName() . '_' . $var;
416 $value = $this->getVar( $var );
417 return $this->parent
->getCheckBox( array(
420 'attribs' => $attribs,
421 'controlName' => $name,
428 * Get a set of labelled radio buttons.
430 * @param $params Array:
432 * var: The variable to be configured (required)
433 * label: The message name for the label (required)
434 * itemLabelPrefix: The message name prefix for the item labels (required)
435 * values: List of allowed values (required)
436 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
439 public function getRadioSet( $params ) {
440 $params['controlName'] = $this->getName() . '_' . $params['var'];
441 $params['value'] = $this->getVar( $params['var'] );
442 return $this->parent
->getRadioSet( $params );
446 * Convenience function to set variables based on form data.
447 * Assumes that variables containing "password" in the name are (potentially
449 * @param $varNames Array
451 public function setVarsFromRequest( $varNames ) {
452 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
456 * Determine whether an existing installation of MediaWiki is present in
457 * the configured administrative connection. Returns true if there is
458 * such a wiki, false if the database doesn't exist.
460 * Traditionally, this is done by testing for the existence of either
461 * the revision table or the cur table.
465 public function needsUpgrade() {
466 $status = $this->getConnection();
467 if ( !$status->isOK() ) {
471 if ( !$this->db
->selectDB( $this->getVar( 'wgDBname' ) ) ) {
474 return $this->db
->tableExists( 'cur' ) ||
$this->db
->tableExists( 'revision' );
478 * Get a standard install-user fieldset.
482 public function getInstallUserBox() {
484 Html
::openElement( 'fieldset' ) .
485 Html
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
486 $this->getTextBox( '_InstallUser', 'config-db-username', array(), $this->parent
->getHelpBox( 'config-db-install-username' ) ) .
487 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array(), $this->parent
->getHelpBox( 'config-db-install-password' ) ) .
488 Html
::closeElement( 'fieldset' );
492 * Submit a standard install user fieldset.
494 public function submitInstallUserBox() {
495 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
496 return Status
::newGood();
500 * Get a standard web-user fieldset
501 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
502 * Set this to false to show a creation checkbox.
506 public function getWebUserBox( $noCreateMsg = false ) {
507 $wrapperStyle = $this->getVar( '_SameAccount' ) ?
'display: none' : '';
508 $s = Html
::openElement( 'fieldset' ) .
509 Html
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
511 '_SameAccount', 'config-db-web-account-same',
512 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
514 Html
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => $wrapperStyle ) ) .
515 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
516 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
517 $this->parent
->getHelpBox( 'config-db-web-help' );
518 if ( $noCreateMsg ) {
519 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
521 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
523 $s .= Html
::closeElement( 'div' ) . Html
::closeElement( 'fieldset' );
528 * Submit the form from getWebUserBox().
532 public function submitWebUserBox() {
533 $this->setVarsFromRequest(
534 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
537 if ( $this->getVar( '_SameAccount' ) ) {
538 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
539 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
542 if( $this->getVar( '_CreateDBAccount' ) && strval( $this->getVar( 'wgDBpassword' ) ) == '' ) {
543 return Status
::newFatal( 'config-db-password-empty', $this->getVar( 'wgDBuser' ) );
546 return Status
::newGood();
550 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
554 public function populateInterwikiTable() {
555 $status = $this->getConnection();
556 if ( !$status->isOK() ) {
559 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
561 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
562 $status->warning( 'config-install-interwiki-exists' );
566 wfSuppressWarnings();
567 $rows = file( "$IP/maintenance/interwiki.list",
568 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
570 $interwikis = array();
572 return Status
::newFatal( 'config-install-interwiki-list' );
574 foreach( $rows as $row ) {
575 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
576 if ( $row == "" ) continue;
578 $interwikis[] = array_combine(
579 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
583 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
584 return Status
::newGood();
587 public function outputHandler( $string ) {
588 return htmlspecialchars( $string );