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,
100 * return a Status object.
102 * This may be called multiple times, so the result should be cached.
104 public abstract function getConnection();
107 * Create the database and return a Status object indicating success or
112 public abstract function setupDatabase();
115 * Create database tables from scratch.
119 public function createTables() {
120 $status = $this->getConnection();
121 if ( !$status->isOK() ) {
124 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
126 if( $this->db
->tableExists( 'user' ) ) {
127 $status->warning( 'config-install-tables-exist' );
131 $error = $this->db
->sourceFile( $this->db
->getSchema() );
132 if( $error !== true ) {
133 $this->db
->reportQueryError( $error, 0, $sql, __METHOD__
);
134 $status->fatal( 'config-install-tables-failed', $error );
140 * Get the DBMS-specific options for LocalSettings.php generation.
144 public abstract function getLocalSettings();
147 * Perform database upgrades
151 public abstract function doUpgrade();
154 * Allow DB installers a chance to make last-minute changes before installation
155 * occurs. This happens before setupDatabase() or createTables() is called, but
156 * long after the constructor. Helpful for things like modifying setup steps :)
158 public function preInstall() {
163 * Allow DB installers a chance to make checks before upgrade.
165 public function preUpgrade() {
170 * Get an array of MW configuration globals that will be configured by this class.
172 public function getGlobalNames() {
173 return $this->globalNames
;
177 * Return any table options to be applied to all tables that don't
182 public function getTableOptions() {
187 * Construct and initialise parent.
188 * This is typically only called from Installer::getDBInstaller()
190 public function __construct( $parent ) {
191 $this->parent
= $parent;
195 * Convenience function.
196 * Check if a named extension is present.
200 protected static function checkExtension( $name ) {
201 wfSuppressWarnings();
202 $compiled = wfDl( $name );
208 * Get the internationalised name for this DBMS.
210 public function getReadableName() {
211 return wfMsg( 'config-type-' . $this->getName() );
215 * Get a name=>value map of MW configuration globals that overrides.
216 * DefaultSettings.php
218 public function getGlobalDefaults() {
223 * Get a name=>value map of internal variables used during installation.
225 public function getInternalDefaults() {
226 return $this->internalDefaults
;
230 * Get a variable, taking local defaults into account.
232 public function getVar( $var, $default = null ) {
233 $defaults = $this->getGlobalDefaults();
234 $internal = $this->getInternalDefaults();
235 if ( isset( $defaults[$var] ) ) {
236 $default = $defaults[$var];
237 } elseif ( isset( $internal[$var] ) ) {
238 $default = $internal[$var];
240 return $this->parent
->getVar( $var, $default );
244 * Convenience alias for $this->parent->setVar()
246 public function setVar( $name, $value ) {
247 $this->parent
->setVar( $name, $value );
251 * Get a labelled text box to configure a local variable.
253 public function getTextBox( $var, $label, $attribs = array() ) {
254 $name = $this->getName() . '_' . $var;
255 $value = $this->getVar( $var );
256 return $this->parent
->getTextBox( array(
259 'attribs' => $attribs,
260 'controlName' => $name,
266 * Get a labelled password box to configure a local variable.
267 * Implements password hiding.
269 public function getPasswordBox( $var, $label, $attribs = array() ) {
270 $name = $this->getName() . '_' . $var;
271 $value = $this->getVar( $var );
272 return $this->parent
->getPasswordBox( array(
275 'attribs' => $attribs,
276 'controlName' => $name,
282 * Get a labelled checkbox to configure a local boolean variable.
284 public function getCheckBox( $var, $label, $attribs = array() ) {
285 $name = $this->getName() . '_' . $var;
286 $value = $this->getVar( $var );
287 return $this->parent
->getCheckBox( array(
290 'attribs' => $attribs,
291 'controlName' => $name,
297 * Get a set of labelled radio buttons.
299 * @param $params Array:
301 * var: The variable to be configured (required)
302 * label: The message name for the label (required)
303 * itemLabelPrefix: The message name prefix for the item labels (required)
304 * values: List of allowed values (required)
305 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
308 public function getRadioSet( $params ) {
309 $params['controlName'] = $this->getName() . '_' . $params['var'];
310 $params['value'] = $this->getVar( $params['var'] );
311 return $this->parent
->getRadioSet( $params );
315 * Convenience function to set variables based on form data.
316 * Assumes that variables containing "password" in the name are (potentially
318 * @param $varNames Array
320 public function setVarsFromRequest( $varNames ) {
321 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
325 * Determine whether an existing installation of MediaWiki is present in
326 * the configured administrative connection. Returns true if there is
327 * such a wiki, false if the database doesn't exist.
329 * Traditionally, this is done by testing for the existence of either
330 * the revision table or the cur table.
334 public function needsUpgrade() {
335 $status = $this->getConnection();
336 if ( !$status->isOK() ) {
340 if ( !$this->db
->selectDB( $this->getVar( 'wgDBname' ) ) ) {
343 return $this->db
->tableExists( 'cur' ) ||
$this->db
->tableExists( 'revision' );
347 * Get a standard install-user fieldset.
349 public function getInstallUserBox() {
351 Xml
::openElement( 'fieldset' ) .
352 Xml
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
353 $this->getTextBox( '_InstallUser', 'config-db-username' ) .
354 $this->getPasswordBox( '_InstallPassword', 'config-db-password' ) .
355 $this->parent
->getHelpBox( 'config-db-install-help' ) .
356 Xml
::closeElement( 'fieldset' );
360 * Submit a standard install user fieldset.
362 public function submitInstallUserBox() {
363 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
364 return Status
::newGood();
368 * Get a standard web-user fieldset
369 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
370 * Set this to false to show a creation checkbox.
372 public function getWebUserBox( $noCreateMsg = false ) {
373 $name = $this->getName();
374 $s = Xml
::openElement( 'fieldset' ) .
375 Xml
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
377 '_SameAccount', 'config-db-web-account-same',
378 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
380 Xml
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
381 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
382 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
383 $this->parent
->getHelpBox( 'config-db-web-help' );
384 if ( $noCreateMsg ) {
385 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
387 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
389 $s .= Xml
::closeElement( 'div' ) . Xml
::closeElement( 'fieldset' );
394 * Submit the form from getWebUserBox().
398 public function submitWebUserBox() {
399 $this->setVarsFromRequest(
400 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
403 if ( $this->getVar( '_SameAccount' ) ) {
404 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
405 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
408 return Status
::newGood();
412 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
414 public function populateInterwikiTable() {
415 $status = $this->getConnection();
416 if ( !$status->isOK() ) {
419 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
421 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
422 $status->warning( 'config-install-interwiki-exists' );
426 $rows = file( "$IP/maintenance/interwiki.list",
427 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
428 $interwikis = array();
430 return Status
::newFatal( 'config-install-interwiki-sql' );
432 foreach( $rows as $row ) {
433 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
434 if ( $row == "" ) continue;
436 $interwikis[] = array_combine(
437 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
441 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
442 return Status
::newGood();
445 public function outputHandler( $string ) {
446 return htmlspecialchars( $string );