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 * Connect to the database using the administrative user/password currently
99 * defined in the session. On success, return the connection, on failure,
101 * This may be called multiple times, so the result should be cached.
105 public abstract function getConnection();
108 * Create the database and return a Status object indicating success or
113 public abstract function setupDatabase();
116 * Create database tables from scratch.
120 public function createTables() {
121 $status = $this->getConnection();
122 if ( !$status->isOK() ) {
125 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
127 if( $this->db
->tableExists( 'user' ) ) {
128 $status->warning( 'config-install-tables-exist' );
132 $this->db
->setFlag( DBO_DDLMODE
); // For Oracle's handling of schema files
133 $this->db
->begin( __METHOD__
);
135 $error = $this->db
->sourceFile( $this->db
->getSchema() );
136 if( $error !== true ) {
137 $this->db
->reportQueryError( $error, 0, '', __METHOD__
);
138 $this->db
->rollback( __METHOD__
);
139 $status->fatal( 'config-install-tables-failed', $error );
141 $this->db
->commit( __METHOD__
);
147 * Get the DBMS-specific options for LocalSettings.php generation.
151 public abstract function getLocalSettings();
154 * Perform database upgrades
158 public function doUpgrade() {
159 # Maintenance scripts like wfGetDB()
160 LBFactory
::enableBackend();
163 ob_start( array( $this, 'outputHandler' ) );
165 $up = DatabaseUpdater
::newForDB( $this->db
);
167 } catch ( MWException
$e ) {
168 echo "\nAn error occured:\n";
177 * Allow DB installers a chance to make last-minute changes before installation
178 * occurs. This happens before setupDatabase() or createTables() is called, but
179 * long after the constructor. Helpful for things like modifying setup steps :)
181 public function preInstall() {
186 * Allow DB installers a chance to make checks before upgrade.
188 public function preUpgrade() {
193 * Get an array of MW configuration globals that will be configured by this class.
195 public function getGlobalNames() {
196 return $this->globalNames
;
200 * Return any table options to be applied to all tables that don't
205 public function getTableOptions() {
210 * Construct and initialise parent.
211 * This is typically only called from Installer::getDBInstaller()
213 public function __construct( $parent ) {
214 $this->parent
= $parent;
218 * Convenience function.
219 * Check if a named extension is present.
223 protected static function checkExtension( $name ) {
224 wfSuppressWarnings();
225 $compiled = wfDl( $name );
231 * Get the internationalised name for this DBMS.
233 public function getReadableName() {
234 return wfMsg( 'config-type-' . $this->getName() );
238 * Get a name=>value map of MW configuration globals that overrides.
239 * DefaultSettings.php
241 public function getGlobalDefaults() {
246 * Get a name=>value map of internal variables used during installation.
248 public function getInternalDefaults() {
249 return $this->internalDefaults
;
253 * Get a variable, taking local defaults into account.
255 public function getVar( $var, $default = null ) {
256 $defaults = $this->getGlobalDefaults();
257 $internal = $this->getInternalDefaults();
258 if ( isset( $defaults[$var] ) ) {
259 $default = $defaults[$var];
260 } elseif ( isset( $internal[$var] ) ) {
261 $default = $internal[$var];
263 return $this->parent
->getVar( $var, $default );
267 * Convenience alias for $this->parent->setVar()
269 public function setVar( $name, $value ) {
270 $this->parent
->setVar( $name, $value );
274 * Get a labelled text box to configure a local variable.
276 public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
277 $name = $this->getName() . '_' . $var;
278 $value = $this->getVar( $var );
279 if ( !isset( $attribs ) ) {
282 return $this->parent
->getTextBox( array(
285 'attribs' => $attribs,
286 'controlName' => $name,
293 * Get a labelled password box to configure a local variable.
294 * Implements password hiding.
296 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
297 $name = $this->getName() . '_' . $var;
298 $value = $this->getVar( $var );
299 if ( !isset( $attribs ) ) {
302 return $this->parent
->getPasswordBox( array(
305 'attribs' => $attribs,
306 'controlName' => $name,
313 * Get a labelled checkbox to configure a local boolean variable.
315 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
316 $name = $this->getName() . '_' . $var;
317 $value = $this->getVar( $var );
318 return $this->parent
->getCheckBox( array(
321 'attribs' => $attribs,
322 'controlName' => $name,
329 * Get a set of labelled radio buttons.
331 * @param $params Array:
333 * var: The variable to be configured (required)
334 * label: The message name for the label (required)
335 * itemLabelPrefix: The message name prefix for the item labels (required)
336 * values: List of allowed values (required)
337 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
340 public function getRadioSet( $params ) {
341 $params['controlName'] = $this->getName() . '_' . $params['var'];
342 $params['value'] = $this->getVar( $params['var'] );
343 return $this->parent
->getRadioSet( $params );
347 * Convenience function to set variables based on form data.
348 * Assumes that variables containing "password" in the name are (potentially
350 * @param $varNames Array
352 public function setVarsFromRequest( $varNames ) {
353 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
357 * Determine whether an existing installation of MediaWiki is present in
358 * the configured administrative connection. Returns true if there is
359 * such a wiki, false if the database doesn't exist.
361 * Traditionally, this is done by testing for the existence of either
362 * the revision table or the cur table.
366 public function needsUpgrade() {
367 $status = $this->getConnection();
368 if ( !$status->isOK() ) {
372 if ( !$this->db
->selectDB( $this->getVar( 'wgDBname' ) ) ) {
375 return $this->db
->tableExists( 'cur' ) ||
$this->db
->tableExists( 'revision' );
379 * Get a standard install-user fieldset.
381 public function getInstallUserBox() {
383 Html
::openElement( 'fieldset' ) .
384 Html
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
385 $this->getTextBox( '_InstallUser', 'config-db-username', array(), $this->parent
->getHelpBox( 'config-db-install-username' ) ) .
386 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array(), $this->parent
->getHelpBox( 'config-db-install-password' ) ) .
387 Html
::closeElement( 'fieldset' );
391 * Submit a standard install user fieldset.
393 public function submitInstallUserBox() {
394 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
395 return Status
::newGood();
399 * Get a standard web-user fieldset
400 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
401 * Set this to false to show a creation checkbox.
403 public function getWebUserBox( $noCreateMsg = false ) {
404 $s = Html
::openElement( 'fieldset' ) .
405 Html
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
407 '_SameAccount', 'config-db-web-account-same',
408 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
410 Html
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
411 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
412 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
413 $this->parent
->getHelpBox( 'config-db-web-help' );
414 if ( $noCreateMsg ) {
415 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
417 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
419 $s .= Html
::closeElement( 'div' ) . Html
::closeElement( 'fieldset' );
424 * Submit the form from getWebUserBox().
428 public function submitWebUserBox() {
429 $this->setVarsFromRequest(
430 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
433 if ( $this->getVar( '_SameAccount' ) ) {
434 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
435 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
438 return Status
::newGood();
442 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
444 public function populateInterwikiTable() {
445 $status = $this->getConnection();
446 if ( !$status->isOK() ) {
449 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
451 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
452 $status->warning( 'config-install-interwiki-exists' );
456 $rows = file( "$IP/maintenance/interwiki.list",
457 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
458 $interwikis = array();
460 return Status
::newFatal( 'config-install-interwiki-sql' );
462 foreach( $rows as $row ) {
463 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
464 if ( $row == "" ) continue;
466 $interwikis[] = array_combine(
467 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
471 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
472 return Status
::newGood();
475 public function outputHandler( $string ) {
476 return htmlspecialchars( $string );