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 * Checks for installation prerequisites other than those checked by isCompiled()
62 public function checkPrerequisites() {
63 return Status
::newGood();
67 * Get HTML for a web form that configures this database. Configuration
68 * at this time should be the minimum needed to connect and test
69 * whether install or upgrade is required.
71 * If this is called, $this->parent can be assumed to be a WebInstaller.
73 public abstract function getConnectForm();
76 * Set variables based on the request array, assuming it was submitted
77 * via the form returned by getConnectForm(). Validate the connection
78 * settings by attempting to connect with them.
80 * If this is called, $this->parent can be assumed to be a WebInstaller.
84 public abstract function submitConnectForm();
87 * Get HTML for a web form that retrieves settings used for installation.
88 * $this->parent can be assumed to be a WebInstaller.
89 * If the DB type has no settings beyond those already configured with
90 * getConnectForm(), this should return false.
92 public function getSettingsForm() {
97 * Set variables based on the request array, assuming it was submitted via
98 * the form return by getSettingsForm().
102 public function submitSettingsForm() {
103 return Status
::newGood();
107 * Open a connection to the database using the administrative user/password
108 * currently defined in the session, without any caching. Returns a status
109 * object. On success, the status object will contain a Database object in
114 public abstract function openConnection();
117 * Create the database and return a Status object indicating success or
122 public abstract function setupDatabase();
125 * Connect to the database using the administrative user/password currently
126 * defined in the session. Returns a status object. On success, the status
127 * object will contain a Database object in its value member.
129 * This will return a cached connection if one is available.
133 public function getConnection() {
135 return Status
::newGood( $this->db
);
138 $status = $this->openConnection();
139 if ( $status->isOK() ) {
140 $this->db
= $status->value
;
142 $this->db
->clearFlag( DBO_TRX
);
149 * Create database tables from scratch.
153 public function createTables() {
154 $status = $this->getConnection();
155 if ( !$status->isOK() ) {
158 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
160 if( $this->db
->tableExists( 'user', __METHOD__
) ) {
161 $status->warning( 'config-install-tables-exist' );
166 $this->db
->setFlag( DBO_DDLMODE
); // For Oracle's handling of schema files
167 $this->db
->begin( __METHOD__
);
169 $error = $this->db
->sourceFile( $this->db
->getSchemaPath() );
170 if( $error !== true ) {
171 $this->db
->reportQueryError( $error, 0, '', __METHOD__
);
172 $this->db
->rollback( __METHOD__
);
173 $status->fatal( 'config-install-tables-failed', $error );
175 $this->db
->commit( __METHOD__
);
177 // Resume normal operations
178 if( $status->isOk() ) {
185 * Create the tables for each extension the user enabled
188 public function createExtensionTables() {
189 $status = $this->getConnection();
190 if ( !$status->isOK() ) {
194 // Now run updates to create tables for old extensions
195 DatabaseUpdater
::newForDB( $this->db
)->doUpdates( array( 'extensions' ) );
201 * Get the DBMS-specific options for LocalSettings.php generation.
205 public abstract function getLocalSettings();
208 * Override this to provide DBMS-specific schema variables, to be
209 * substituted into tables.sql and other schema files.
211 public function getSchemaVars() {
216 * Set appropriate schema variables in the current database connection.
218 * This should be called after any request data has been imported, but before
219 * any write operations to the database.
221 public function setupSchemaVars() {
222 $status = $this->getConnection();
223 if ( $status->isOK() ) {
224 $status->value
->setSchemaVars( $this->getSchemaVars() );
226 throw new MWException( __METHOD__
.': unexpected DB connection error' );
231 * Set up LBFactory so that wfGetDB() etc. works.
232 * We set up a special LBFactory instance which returns the current
233 * installer connection.
235 public function enableLB() {
236 $status = $this->getConnection();
237 if ( !$status->isOK() ) {
238 throw new MWException( __METHOD__
.': unexpected DB connection error' );
240 LBFactory
::setInstance( new LBFactory_Single( array(
241 'connection' => $status->value
) ) );
245 * Perform database upgrades
249 public function doUpgrade() {
250 $this->setupSchemaVars();
254 ob_start( array( $this, 'outputHandler' ) );
256 $up = DatabaseUpdater
::newForDB( $this->db
);
258 } catch ( MWException
$e ) {
259 echo "\nAn error occured:\n";
268 * Allow DB installers a chance to make last-minute changes before installation
269 * occurs. This happens before setupDatabase() or createTables() is called, but
270 * long after the constructor. Helpful for things like modifying setup steps :)
272 public function preInstall() {
277 * Allow DB installers a chance to make checks before upgrade.
279 public function preUpgrade() {
284 * Get an array of MW configuration globals that will be configured by this class.
286 public function getGlobalNames() {
287 return $this->globalNames
;
291 * Construct and initialise parent.
292 * This is typically only called from Installer::getDBInstaller()
295 public function __construct( $parent ) {
296 $this->parent
= $parent;
300 * Convenience function.
301 * Check if a named extension is present.
307 protected static function checkExtension( $name ) {
308 wfSuppressWarnings();
309 $compiled = wfDl( $name );
315 * Get the internationalised name for this DBMS.
317 public function getReadableName() {
318 return wfMsg( 'config-type-' . $this->getName() );
322 * Get a name=>value map of MW configuration globals that overrides.
323 * DefaultSettings.php
325 public function getGlobalDefaults() {
330 * Get a name=>value map of internal variables used during installation.
332 public function getInternalDefaults() {
333 return $this->internalDefaults
;
337 * Get a variable, taking local defaults into account.
339 * @param $default null
342 public function getVar( $var, $default = null ) {
343 $defaults = $this->getGlobalDefaults();
344 $internal = $this->getInternalDefaults();
345 if ( isset( $defaults[$var] ) ) {
346 $default = $defaults[$var];
347 } elseif ( isset( $internal[$var] ) ) {
348 $default = $internal[$var];
350 return $this->parent
->getVar( $var, $default );
354 * Convenience alias for $this->parent->setVar()
355 * @param $name string
356 * @param $value mixed
358 public function setVar( $name, $value ) {
359 $this->parent
->setVar( $name, $value );
363 * Get a labelled text box to configure a local variable.
366 * @param $label string
367 * @param $attribs array
368 * @param $helpData string
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.
392 * @param $label string
393 * @param $attribs array
394 * @param $helpData string
397 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
398 $name = $this->getName() . '_' . $var;
399 $value = $this->getVar( $var );
400 if ( !isset( $attribs ) ) {
403 return $this->parent
->getPasswordBox( array(
406 'attribs' => $attribs,
407 'controlName' => $name,
414 * Get a labelled checkbox to configure a local boolean variable.
418 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
419 $name = $this->getName() . '_' . $var;
420 $value = $this->getVar( $var );
421 return $this->parent
->getCheckBox( array(
424 'attribs' => $attribs,
425 'controlName' => $name,
432 * Get a set of labelled radio buttons.
434 * @param $params Array:
436 * var: The variable to be configured (required)
437 * label: The message name for the label (required)
438 * itemLabelPrefix: The message name prefix for the item labels (required)
439 * values: List of allowed values (required)
440 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
443 public function getRadioSet( $params ) {
444 $params['controlName'] = $this->getName() . '_' . $params['var'];
445 $params['value'] = $this->getVar( $params['var'] );
446 return $this->parent
->getRadioSet( $params );
450 * Convenience function to set variables based on form data.
451 * Assumes that variables containing "password" in the name are (potentially
453 * @param $varNames Array
455 public function setVarsFromRequest( $varNames ) {
456 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
460 * Determine whether an existing installation of MediaWiki is present in
461 * the configured administrative connection. Returns true if there is
462 * such a wiki, false if the database doesn't exist.
464 * Traditionally, this is done by testing for the existence of either
465 * the revision table or the cur table.
469 public function needsUpgrade() {
470 $status = $this->getConnection();
471 if ( !$status->isOK() ) {
475 if ( !$this->db
->selectDB( $this->getVar( 'wgDBname' ) ) ) {
478 return $this->db
->tableExists( 'cur', __METHOD__
) ||
$this->db
->tableExists( 'revision', __METHOD__
);
482 * Get a standard install-user fieldset.
486 public function getInstallUserBox() {
488 Html
::openElement( 'fieldset' ) .
489 Html
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
490 $this->getTextBox( '_InstallUser', 'config-db-username', array( 'dir' => 'ltr' ), $this->parent
->getHelpBox( 'config-db-install-username' ) ) .
491 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array( 'dir' => 'ltr' ), $this->parent
->getHelpBox( 'config-db-install-password' ) ) .
492 Html
::closeElement( 'fieldset' );
496 * Submit a standard install user fieldset.
498 public function submitInstallUserBox() {
499 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
500 return Status
::newGood();
504 * Get a standard web-user fieldset
505 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
506 * Set this to false to show a creation checkbox.
510 public function getWebUserBox( $noCreateMsg = false ) {
511 $wrapperStyle = $this->getVar( '_SameAccount' ) ?
'display: none' : '';
512 $s = Html
::openElement( 'fieldset' ) .
513 Html
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
515 '_SameAccount', 'config-db-web-account-same',
516 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
518 Html
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => $wrapperStyle ) ) .
519 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
520 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
521 $this->parent
->getHelpBox( 'config-db-web-help' );
522 if ( $noCreateMsg ) {
523 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
525 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
527 $s .= Html
::closeElement( 'div' ) . Html
::closeElement( 'fieldset' );
532 * Submit the form from getWebUserBox().
536 public function submitWebUserBox() {
537 $this->setVarsFromRequest(
538 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
541 if ( $this->getVar( '_SameAccount' ) ) {
542 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
543 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
546 if( $this->getVar( '_CreateDBAccount' ) && strval( $this->getVar( 'wgDBpassword' ) ) == '' ) {
547 return Status
::newFatal( 'config-db-password-empty', $this->getVar( 'wgDBuser' ) );
550 return Status
::newGood();
554 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
558 public function populateInterwikiTable() {
559 $status = $this->getConnection();
560 if ( !$status->isOK() ) {
563 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
565 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
566 $status->warning( 'config-install-interwiki-exists' );
570 wfSuppressWarnings();
571 $rows = file( "$IP/maintenance/interwiki.list",
572 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
574 $interwikis = array();
576 return Status
::newFatal( 'config-install-interwiki-list' );
578 foreach( $rows as $row ) {
579 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
580 if ( $row == "" ) continue;
582 $interwikis[] = array_combine(
583 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
587 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
588 return Status
::newGood();
591 public function outputHandler( $string ) {
592 return htmlspecialchars( $string );