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( $dbName = null );
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( $dbName = null ) {
125 if ( isset($this->db
) && $this->db
) { /* Weirdly get E_STRICT
128 return Status
::newGood( $this->db
);
131 $status = $this->openConnection( $dbName );
132 if ( $status->isOK() ) {
133 $this->db
= $status->value
;
135 $this->db
->clearFlag( DBO_TRX
);
142 * Create database tables from scratch.
146 public function createTables() {
147 $status = $this->getConnection();
148 if ( !$status->isOK() ) {
151 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
153 if( $this->db
->tableExists( 'user' ) ) {
154 $status->warning( 'config-install-tables-exist' );
159 $this->db
->setFlag( DBO_DDLMODE
); // For Oracle's handling of schema files
160 $this->db
->begin( __METHOD__
);
162 $error = $this->db
->sourceFile( $this->db
->getSchema() );
163 if( $error !== true ) {
164 $this->db
->reportQueryError( $error, 0, '', __METHOD__
);
165 $this->db
->rollback( __METHOD__
);
166 $status->fatal( 'config-install-tables-failed', $error );
168 $this->db
->commit( __METHOD__
);
170 // Resume normal operations
171 if( $status->isOk() ) {
178 * Create the tables for each extension the user enabled
181 public function createExtensionTables() {
182 $status = $this->getConnection();
183 if ( !$status->isOK() ) {
187 // Now run updates to create tables for old extensions
188 DatabaseUpdater
::newForDB( $this->db
)->doUpdates( array( 'extensions' ) );
194 * Get the DBMS-specific options for LocalSettings.php generation.
198 public abstract function getLocalSettings();
201 * Override this to provide DBMS-specific schema variables, to be
202 * substituted into tables.sql and other schema files.
204 public function getSchemaVars() {
209 * Set appropriate schema variables in the current database connection.
211 * This should be called after any request data has been imported, but before
212 * any write operations to the database.
214 public function setupSchemaVars() {
215 $status = $this->getConnection();
216 if ( $status->isOK() ) {
217 $status->value
->setSchemaVars( $this->getSchemaVars() );
219 throw new MWException( __METHOD__
.': unexpected DB connection error' );
224 * Set up LBFactory so that wfGetDB() etc. works.
225 * We set up a special LBFactory instance which returns the current
226 * installer connection.
228 public function enableLB() {
229 $status = $this->getConnection();
230 if ( !$status->isOK() ) {
231 throw new MWException( __METHOD__
.': unexpected DB connection error' );
233 LBFactory
::setInstance( new LBFactory_Single( array(
234 'connection' => $status->value
) ) );
238 * Perform database upgrades
242 public function doUpgrade() {
243 $this->setupSchemaVars();
247 ob_start( array( $this, 'outputHandler' ) );
249 $up = DatabaseUpdater
::newForDB( $this->db
);
251 } catch ( MWException
$e ) {
252 echo "\nAn error occured:\n";
261 * Allow DB installers a chance to make last-minute changes before installation
262 * occurs. This happens before setupDatabase() or createTables() is called, but
263 * long after the constructor. Helpful for things like modifying setup steps :)
265 public function preInstall() {
270 * Allow DB installers a chance to make checks before upgrade.
272 public function preUpgrade() {
277 * Get an array of MW configuration globals that will be configured by this class.
279 public function getGlobalNames() {
280 return $this->globalNames
;
284 * Construct and initialise parent.
285 * This is typically only called from Installer::getDBInstaller()
287 public function __construct( $parent ) {
288 $this->parent
= $parent;
292 * Convenience function.
293 * Check if a named extension is present.
297 protected static function checkExtension( $name ) {
298 wfSuppressWarnings();
299 $compiled = wfDl( $name );
305 * Get the internationalised name for this DBMS.
307 public function getReadableName() {
308 return wfMsg( 'config-type-' . $this->getName() );
312 * Get a name=>value map of MW configuration globals that overrides.
313 * DefaultSettings.php
315 public function getGlobalDefaults() {
320 * Get a name=>value map of internal variables used during installation.
322 public function getInternalDefaults() {
323 return $this->internalDefaults
;
327 * Get a variable, taking local defaults into account.
329 public function getVar( $var, $default = null ) {
330 $defaults = $this->getGlobalDefaults();
331 $internal = $this->getInternalDefaults();
332 if ( isset( $defaults[$var] ) ) {
333 $default = $defaults[$var];
334 } elseif ( isset( $internal[$var] ) ) {
335 $default = $internal[$var];
337 return $this->parent
->getVar( $var, $default );
341 * Convenience alias for $this->parent->setVar()
343 public function setVar( $name, $value ) {
344 $this->parent
->setVar( $name, $value );
348 * Get a labelled text box to configure a local variable.
352 public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
353 $name = $this->getName() . '_' . $var;
354 $value = $this->getVar( $var );
355 if ( !isset( $attribs ) ) {
358 return $this->parent
->getTextBox( array(
361 'attribs' => $attribs,
362 'controlName' => $name,
369 * Get a labelled password box to configure a local variable.
370 * Implements password hiding.
374 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
375 $name = $this->getName() . '_' . $var;
376 $value = $this->getVar( $var );
377 if ( !isset( $attribs ) ) {
380 return $this->parent
->getPasswordBox( array(
383 'attribs' => $attribs,
384 'controlName' => $name,
391 * Get a labelled checkbox to configure a local boolean variable.
395 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
396 $name = $this->getName() . '_' . $var;
397 $value = $this->getVar( $var );
398 return $this->parent
->getCheckBox( array(
401 'attribs' => $attribs,
402 'controlName' => $name,
409 * Get a set of labelled radio buttons.
411 * @param $params Array:
413 * var: The variable to be configured (required)
414 * label: The message name for the label (required)
415 * itemLabelPrefix: The message name prefix for the item labels (required)
416 * values: List of allowed values (required)
417 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
420 public function getRadioSet( $params ) {
421 $params['controlName'] = $this->getName() . '_' . $params['var'];
422 $params['value'] = $this->getVar( $params['var'] );
423 return $this->parent
->getRadioSet( $params );
427 * Convenience function to set variables based on form data.
428 * Assumes that variables containing "password" in the name are (potentially
430 * @param $varNames Array
432 public function setVarsFromRequest( $varNames ) {
433 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
437 * Determine whether an existing installation of MediaWiki is present in
438 * the configured administrative connection. Returns true if there is
439 * such a wiki, false if the database doesn't exist.
441 * Traditionally, this is done by testing for the existence of either
442 * the revision table or the cur table.
446 public function needsUpgrade() {
447 $status = $this->getConnection();
448 if ( !$status->isOK() ) {
452 if ( !$this->db
->selectDB( $this->getVar( 'wgDBname' ) ) ) {
455 return $this->db
->tableExists( 'cur' ) ||
$this->db
->tableExists( 'revision' );
459 * Get a standard install-user fieldset.
463 public function getInstallUserBox() {
465 Html
::openElement( 'fieldset' ) .
466 Html
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
467 $this->getTextBox( '_InstallUser', 'config-db-username', array(), $this->parent
->getHelpBox( 'config-db-install-username' ) ) .
468 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array(), $this->parent
->getHelpBox( 'config-db-install-password' ) ) .
469 Html
::closeElement( 'fieldset' );
473 * Submit a standard install user fieldset.
475 public function submitInstallUserBox() {
476 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
477 return Status
::newGood();
481 * Get a standard web-user fieldset
482 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
483 * Set this to false to show a creation checkbox.
487 public function getWebUserBox( $noCreateMsg = false ) {
488 $wrapperStyle = $this->getVar( '_SameAccount' ) ?
'display: none' : '';
489 $s = Html
::openElement( 'fieldset' ) .
490 Html
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
492 '_SameAccount', 'config-db-web-account-same',
493 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
495 Html
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => $wrapperStyle ) ) .
496 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
497 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
498 $this->parent
->getHelpBox( 'config-db-web-help' );
499 if ( $noCreateMsg ) {
500 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
502 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
504 $s .= Html
::closeElement( 'div' ) . Html
::closeElement( 'fieldset' );
509 * Submit the form from getWebUserBox().
513 public function submitWebUserBox() {
514 $this->setVarsFromRequest(
515 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
518 if ( $this->getVar( '_SameAccount' ) ) {
519 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
520 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
523 if( $this->getVar( '_CreateDBAccount' ) && strval( $this->getVar( 'wgDBpassword' ) ) == '' ) {
524 return Status
::newFatal( 'config-db-password-empty', $this->getVar( 'wgDBuser' ) );
527 return Status
::newGood();
531 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
535 public function populateInterwikiTable() {
536 $status = $this->getConnection();
537 if ( !$status->isOK() ) {
540 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
542 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
543 $status->warning( 'config-install-interwiki-exists' );
547 wfSuppressWarnings();
548 $rows = file( "$IP/maintenance/interwiki.list",
549 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
551 $interwikis = array();
553 return Status
::newFatal( 'config-install-interwiki-list' );
555 foreach( $rows as $row ) {
556 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
557 if ( $row == "" ) continue;
559 $interwikis[] = array_combine(
560 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
564 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
565 return Status
::newGood();
568 public function outputHandler( $string ) {
569 return htmlspecialchars( $string );