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 * Get the DBMS-specific options for LocalSettings.php generation.
178 public abstract function getLocalSettings();
181 * Override this to provide DBMS-specific schema variables, to be
182 * substituted into tables.sql and other schema files.
184 public function getSchemaVars() {
189 * Set appropriate schema variables in the current database connection.
191 * This should be called after any request data has been imported, but before
192 * any write operations to the database.
194 public function setupSchemaVars() {
195 $status = $this->getConnection();
196 if ( $status->isOK() ) {
197 $status->value
->setSchemaVars( $this->getSchemaVars() );
202 * Set up LBFactory so that wfGetDB() etc. works.
203 * We set up a special LBFactory instance which returns the current
204 * installer connection.
206 public function enableLB() {
207 $status = $this->getConnection();
208 if ( !$status->isOK() ) {
209 throw new MWException( __METHOD__
.': unexpected DB connection error' );
211 LBFactory
::setInstance( new LBFactory_Single( array(
212 'connection' => $status->value
) ) );
216 * Get a Database connection object. Throw an exception if we can't get one.
218 * @return DatabaseBase
220 public function getConnectionOrDie() {
224 * Perform database upgrades
228 public function doUpgrade() {
229 $this->setupSchemaVars();
233 ob_start( array( $this, 'outputHandler' ) );
235 $up = DatabaseUpdater
::newForDB( $this->db
);
237 } catch ( MWException
$e ) {
238 echo "\nAn error occured:\n";
247 * Allow DB installers a chance to make last-minute changes before installation
248 * occurs. This happens before setupDatabase() or createTables() is called, but
249 * long after the constructor. Helpful for things like modifying setup steps :)
251 public function preInstall() {
256 * Allow DB installers a chance to make checks before upgrade.
258 public function preUpgrade() {
263 * Get an array of MW configuration globals that will be configured by this class.
265 public function getGlobalNames() {
266 return $this->globalNames
;
270 * Construct and initialise parent.
271 * This is typically only called from Installer::getDBInstaller()
273 public function __construct( $parent ) {
274 $this->parent
= $parent;
278 * Convenience function.
279 * Check if a named extension is present.
283 protected static function checkExtension( $name ) {
284 wfSuppressWarnings();
285 $compiled = wfDl( $name );
291 * Get the internationalised name for this DBMS.
293 public function getReadableName() {
294 return wfMsg( 'config-type-' . $this->getName() );
298 * Get a name=>value map of MW configuration globals that overrides.
299 * DefaultSettings.php
301 public function getGlobalDefaults() {
306 * Get a name=>value map of internal variables used during installation.
308 public function getInternalDefaults() {
309 return $this->internalDefaults
;
313 * Get a variable, taking local defaults into account.
315 public function getVar( $var, $default = null ) {
316 $defaults = $this->getGlobalDefaults();
317 $internal = $this->getInternalDefaults();
318 if ( isset( $defaults[$var] ) ) {
319 $default = $defaults[$var];
320 } elseif ( isset( $internal[$var] ) ) {
321 $default = $internal[$var];
323 return $this->parent
->getVar( $var, $default );
327 * Convenience alias for $this->parent->setVar()
329 public function setVar( $name, $value ) {
330 $this->parent
->setVar( $name, $value );
334 * Get a labelled text box to configure a local variable.
336 public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
337 $name = $this->getName() . '_' . $var;
338 $value = $this->getVar( $var );
339 if ( !isset( $attribs ) ) {
342 return $this->parent
->getTextBox( array(
345 'attribs' => $attribs,
346 'controlName' => $name,
353 * Get a labelled password box to configure a local variable.
354 * Implements password hiding.
356 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
357 $name = $this->getName() . '_' . $var;
358 $value = $this->getVar( $var );
359 if ( !isset( $attribs ) ) {
362 return $this->parent
->getPasswordBox( array(
365 'attribs' => $attribs,
366 'controlName' => $name,
373 * Get a labelled checkbox to configure a local boolean variable.
375 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
376 $name = $this->getName() . '_' . $var;
377 $value = $this->getVar( $var );
378 return $this->parent
->getCheckBox( array(
381 'attribs' => $attribs,
382 'controlName' => $name,
389 * Get a set of labelled radio buttons.
391 * @param $params Array:
393 * var: The variable to be configured (required)
394 * label: The message name for the label (required)
395 * itemLabelPrefix: The message name prefix for the item labels (required)
396 * values: List of allowed values (required)
397 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
400 public function getRadioSet( $params ) {
401 $params['controlName'] = $this->getName() . '_' . $params['var'];
402 $params['value'] = $this->getVar( $params['var'] );
403 return $this->parent
->getRadioSet( $params );
407 * Convenience function to set variables based on form data.
408 * Assumes that variables containing "password" in the name are (potentially
410 * @param $varNames Array
412 public function setVarsFromRequest( $varNames ) {
413 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
417 * Determine whether an existing installation of MediaWiki is present in
418 * the configured administrative connection. Returns true if there is
419 * such a wiki, false if the database doesn't exist.
421 * Traditionally, this is done by testing for the existence of either
422 * the revision table or the cur table.
426 public function needsUpgrade() {
427 $status = $this->getConnection();
428 if ( !$status->isOK() ) {
432 if ( !$this->db
->selectDB( $this->getVar( 'wgDBname' ) ) ) {
435 return $this->db
->tableExists( 'cur' ) ||
$this->db
->tableExists( 'revision' );
439 * Get a standard install-user fieldset.
441 public function getInstallUserBox() {
443 Html
::openElement( 'fieldset' ) .
444 Html
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
445 $this->getTextBox( '_InstallUser', 'config-db-username', array(), $this->parent
->getHelpBox( 'config-db-install-username' ) ) .
446 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array(), $this->parent
->getHelpBox( 'config-db-install-password' ) ) .
447 Html
::closeElement( 'fieldset' );
451 * Submit a standard install user fieldset.
453 public function submitInstallUserBox() {
454 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
455 return Status
::newGood();
459 * Get a standard web-user fieldset
460 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
461 * Set this to false to show a creation checkbox.
463 public function getWebUserBox( $noCreateMsg = false ) {
464 $s = Html
::openElement( 'fieldset' ) .
465 Html
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
467 '_SameAccount', 'config-db-web-account-same',
468 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
470 Html
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
471 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
472 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
473 $this->parent
->getHelpBox( 'config-db-web-help' );
474 if ( $noCreateMsg ) {
475 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
477 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
479 $s .= Html
::closeElement( 'div' ) . Html
::closeElement( 'fieldset' );
484 * Submit the form from getWebUserBox().
488 public function submitWebUserBox() {
489 $this->setVarsFromRequest(
490 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
493 if ( $this->getVar( '_SameAccount' ) ) {
494 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
495 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
498 return Status
::newGood();
502 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
504 public function populateInterwikiTable() {
505 $status = $this->getConnection();
506 if ( !$status->isOK() ) {
509 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
511 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
512 $status->warning( 'config-install-interwiki-exists' );
516 $rows = file( "$IP/maintenance/interwiki.list",
517 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
518 $interwikis = array();
520 return Status
::newFatal( 'config-install-interwiki-sql' );
522 foreach( $rows as $row ) {
523 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
524 if ( $row == "" ) continue;
526 $interwikis[] = array_combine(
527 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
531 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
532 return Status
::newGood();
535 public function outputHandler( $string ) {
536 return htmlspecialchars( $string );