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
);
129 $status = $this->openConnection();
130 if ( $status->isOK() ) {
131 $this->db
= $status->value
;
133 $this->db
->clearFlag( DBO_TRX
);
140 * Create database tables from scratch.
144 public function createTables() {
145 $status = $this->getConnection();
146 if ( !$status->isOK() ) {
149 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
151 if( $this->db
->tableExists( 'user' ) ) {
152 $status->warning( 'config-install-tables-exist' );
157 $this->db
->setFlag( DBO_DDLMODE
); // For Oracle's handling of schema files
158 $this->db
->begin( __METHOD__
);
160 $error = $this->db
->sourceFile( $this->db
->getSchemaPath() );
161 if( $error !== true ) {
162 $this->db
->reportQueryError( $error, 0, '', __METHOD__
);
163 $this->db
->rollback( __METHOD__
);
164 $status->fatal( 'config-install-tables-failed', $error );
166 $this->db
->commit( __METHOD__
);
168 // Resume normal operations
169 if( $status->isOk() ) {
176 * Create the tables for each extension the user enabled
179 public function createExtensionTables() {
180 $status = $this->getConnection();
181 if ( !$status->isOK() ) {
185 // Now run updates to create tables for old extensions
186 DatabaseUpdater
::newForDB( $this->db
)->doUpdates( array( 'extensions' ) );
192 * Get the DBMS-specific options for LocalSettings.php generation.
196 public abstract function getLocalSettings();
199 * Override this to provide DBMS-specific schema variables, to be
200 * substituted into tables.sql and other schema files.
202 public function getSchemaVars() {
207 * Set appropriate schema variables in the current database connection.
209 * This should be called after any request data has been imported, but before
210 * any write operations to the database.
212 public function setupSchemaVars() {
213 $status = $this->getConnection();
214 if ( $status->isOK() ) {
215 $status->value
->setSchemaVars( $this->getSchemaVars() );
217 throw new MWException( __METHOD__
.': unexpected DB connection error' );
222 * Set up LBFactory so that wfGetDB() etc. works.
223 * We set up a special LBFactory instance which returns the current
224 * installer connection.
226 public function enableLB() {
227 $status = $this->getConnection();
228 if ( !$status->isOK() ) {
229 throw new MWException( __METHOD__
.': unexpected DB connection error' );
231 LBFactory
::setInstance( new LBFactory_Single( array(
232 'connection' => $status->value
) ) );
236 * Perform database upgrades
240 public function doUpgrade() {
241 $this->setupSchemaVars();
245 ob_start( array( $this, 'outputHandler' ) );
247 $up = DatabaseUpdater
::newForDB( $this->db
);
249 } catch ( MWException
$e ) {
250 echo "\nAn error occured:\n";
259 * Allow DB installers a chance to make last-minute changes before installation
260 * occurs. This happens before setupDatabase() or createTables() is called, but
261 * long after the constructor. Helpful for things like modifying setup steps :)
263 public function preInstall() {
268 * Allow DB installers a chance to make checks before upgrade.
270 public function preUpgrade() {
275 * Get an array of MW configuration globals that will be configured by this class.
277 public function getGlobalNames() {
278 return $this->globalNames
;
282 * Construct and initialise parent.
283 * This is typically only called from Installer::getDBInstaller()
285 public function __construct( $parent ) {
286 $this->parent
= $parent;
290 * Convenience function.
291 * Check if a named extension is present.
295 protected static function checkExtension( $name ) {
296 wfSuppressWarnings();
297 $compiled = wfDl( $name );
303 * Get the internationalised name for this DBMS.
305 public function getReadableName() {
306 return wfMsg( 'config-type-' . $this->getName() );
310 * Get a name=>value map of MW configuration globals that overrides.
311 * DefaultSettings.php
313 public function getGlobalDefaults() {
318 * Get a name=>value map of internal variables used during installation.
320 public function getInternalDefaults() {
321 return $this->internalDefaults
;
325 * Get a variable, taking local defaults into account.
327 public function getVar( $var, $default = null ) {
328 $defaults = $this->getGlobalDefaults();
329 $internal = $this->getInternalDefaults();
330 if ( isset( $defaults[$var] ) ) {
331 $default = $defaults[$var];
332 } elseif ( isset( $internal[$var] ) ) {
333 $default = $internal[$var];
335 return $this->parent
->getVar( $var, $default );
339 * Convenience alias for $this->parent->setVar()
341 public function setVar( $name, $value ) {
342 $this->parent
->setVar( $name, $value );
346 * Get a labelled text box to configure a local variable.
350 public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
351 $name = $this->getName() . '_' . $var;
352 $value = $this->getVar( $var );
353 if ( !isset( $attribs ) ) {
356 return $this->parent
->getTextBox( array(
359 'attribs' => $attribs,
360 'controlName' => $name,
367 * Get a labelled password box to configure a local variable.
368 * Implements password hiding.
372 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
373 $name = $this->getName() . '_' . $var;
374 $value = $this->getVar( $var );
375 if ( !isset( $attribs ) ) {
378 return $this->parent
->getPasswordBox( array(
381 'attribs' => $attribs,
382 'controlName' => $name,
389 * Get a labelled checkbox to configure a local boolean variable.
393 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
394 $name = $this->getName() . '_' . $var;
395 $value = $this->getVar( $var );
396 return $this->parent
->getCheckBox( array(
399 'attribs' => $attribs,
400 'controlName' => $name,
407 * Get a set of labelled radio buttons.
409 * @param $params Array:
411 * var: The variable to be configured (required)
412 * label: The message name for the label (required)
413 * itemLabelPrefix: The message name prefix for the item labels (required)
414 * values: List of allowed values (required)
415 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
418 public function getRadioSet( $params ) {
419 $params['controlName'] = $this->getName() . '_' . $params['var'];
420 $params['value'] = $this->getVar( $params['var'] );
421 return $this->parent
->getRadioSet( $params );
425 * Convenience function to set variables based on form data.
426 * Assumes that variables containing "password" in the name are (potentially
428 * @param $varNames Array
430 public function setVarsFromRequest( $varNames ) {
431 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
435 * Determine whether an existing installation of MediaWiki is present in
436 * the configured administrative connection. Returns true if there is
437 * such a wiki, false if the database doesn't exist.
439 * Traditionally, this is done by testing for the existence of either
440 * the revision table or the cur table.
444 public function needsUpgrade() {
445 $status = $this->getConnection();
446 if ( !$status->isOK() ) {
450 if ( !$this->db
->selectDB( $this->getVar( 'wgDBname' ) ) ) {
453 return $this->db
->tableExists( 'cur' ) ||
$this->db
->tableExists( 'revision' );
457 * Get a standard install-user fieldset.
461 public function getInstallUserBox() {
463 Html
::openElement( 'fieldset' ) .
464 Html
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
465 $this->getTextBox( '_InstallUser', 'config-db-username', array(), $this->parent
->getHelpBox( 'config-db-install-username' ) ) .
466 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array(), $this->parent
->getHelpBox( 'config-db-install-password' ) ) .
467 Html
::closeElement( 'fieldset' );
471 * Submit a standard install user fieldset.
473 public function submitInstallUserBox() {
474 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
475 return Status
::newGood();
479 * Get a standard web-user fieldset
480 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
481 * Set this to false to show a creation checkbox.
485 public function getWebUserBox( $noCreateMsg = false ) {
486 $wrapperStyle = $this->getVar( '_SameAccount' ) ?
'display: none' : '';
487 $s = Html
::openElement( 'fieldset' ) .
488 Html
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
490 '_SameAccount', 'config-db-web-account-same',
491 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
493 Html
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => $wrapperStyle ) ) .
494 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
495 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
496 $this->parent
->getHelpBox( 'config-db-web-help' );
497 if ( $noCreateMsg ) {
498 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
500 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
502 $s .= Html
::closeElement( 'div' ) . Html
::closeElement( 'fieldset' );
507 * Submit the form from getWebUserBox().
511 public function submitWebUserBox() {
512 $this->setVarsFromRequest(
513 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
516 if ( $this->getVar( '_SameAccount' ) ) {
517 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
518 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
521 if( $this->getVar( '_CreateDBAccount' ) && strval( $this->getVar( 'wgDBpassword' ) ) == '' ) {
522 return Status
::newFatal( 'config-db-password-empty', $this->getVar( 'wgDBuser' ) );
525 return Status
::newGood();
529 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
533 public function populateInterwikiTable() {
534 $status = $this->getConnection();
535 if ( !$status->isOK() ) {
538 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
540 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
541 $status->warning( 'config-install-interwiki-exists' );
545 wfSuppressWarnings();
546 $rows = file( "$IP/maintenance/interwiki.list",
547 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
549 $interwikis = array();
551 return Status
::newFatal( 'config-install-interwiki-list' );
553 foreach( $rows as $row ) {
554 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
555 if ( $row == "" ) continue;
557 $interwikis[] = array_combine(
558 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
562 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
563 return Status
::newGood();
566 public function outputHandler( $string ) {
567 return htmlspecialchars( $string );