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 $error = $this->db
->sourceFile( $this->db
->getSchema() );
134 if( $error !== true ) {
135 $this->db
->reportQueryError( $error, 0, '', __METHOD__
);
136 $status->fatal( 'config-install-tables-failed', $error );
142 * Get the DBMS-specific options for LocalSettings.php generation.
146 public abstract function getLocalSettings();
149 * Perform database upgrades
153 public function doUpgrade() {
154 # Maintenance scripts like wfGetDB()
155 LBFactory
::enableBackend();
158 ob_start( array( $this, 'outputHandler' ) );
160 $up = DatabaseUpdater
::newForDB( $this->db
);
162 } catch ( MWException
$e ) {
163 echo "\nAn error occured:\n";
172 * Allow DB installers a chance to make last-minute changes before installation
173 * occurs. This happens before setupDatabase() or createTables() is called, but
174 * long after the constructor. Helpful for things like modifying setup steps :)
176 public function preInstall() {
181 * Allow DB installers a chance to make checks before upgrade.
183 public function preUpgrade() {
188 * Get an array of MW configuration globals that will be configured by this class.
190 public function getGlobalNames() {
191 return $this->globalNames
;
195 * Return any table options to be applied to all tables that don't
200 public function getTableOptions() {
205 * Construct and initialise parent.
206 * This is typically only called from Installer::getDBInstaller()
208 public function __construct( $parent ) {
209 $this->parent
= $parent;
213 * Convenience function.
214 * Check if a named extension is present.
218 protected static function checkExtension( $name ) {
219 wfSuppressWarnings();
220 $compiled = wfDl( $name );
226 * Get the internationalised name for this DBMS.
228 public function getReadableName() {
229 return wfMsg( 'config-type-' . $this->getName() );
233 * Get a name=>value map of MW configuration globals that overrides.
234 * DefaultSettings.php
236 public function getGlobalDefaults() {
241 * Get a name=>value map of internal variables used during installation.
243 public function getInternalDefaults() {
244 return $this->internalDefaults
;
248 * Get a variable, taking local defaults into account.
250 public function getVar( $var, $default = null ) {
251 $defaults = $this->getGlobalDefaults();
252 $internal = $this->getInternalDefaults();
253 if ( isset( $defaults[$var] ) ) {
254 $default = $defaults[$var];
255 } elseif ( isset( $internal[$var] ) ) {
256 $default = $internal[$var];
258 return $this->parent
->getVar( $var, $default );
262 * Convenience alias for $this->parent->setVar()
264 public function setVar( $name, $value ) {
265 $this->parent
->setVar( $name, $value );
269 * Get a labelled text box to configure a local variable.
271 public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
272 $name = $this->getName() . '_' . $var;
273 $value = $this->getVar( $var );
274 if ( !isset( $attribs ) ) {
277 return $this->parent
->getTextBox( array(
280 'attribs' => $attribs,
281 'controlName' => $name,
288 * Get a labelled password box to configure a local variable.
289 * Implements password hiding.
291 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
292 $name = $this->getName() . '_' . $var;
293 $value = $this->getVar( $var );
294 if ( !isset( $attribs ) ) {
297 return $this->parent
->getPasswordBox( array(
300 'attribs' => $attribs,
301 'controlName' => $name,
308 * Get a labelled checkbox to configure a local boolean variable.
310 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
311 $name = $this->getName() . '_' . $var;
312 $value = $this->getVar( $var );
313 return $this->parent
->getCheckBox( array(
316 'attribs' => $attribs,
317 'controlName' => $name,
324 * Get a set of labelled radio buttons.
326 * @param $params Array:
328 * var: The variable to be configured (required)
329 * label: The message name for the label (required)
330 * itemLabelPrefix: The message name prefix for the item labels (required)
331 * values: List of allowed values (required)
332 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
335 public function getRadioSet( $params ) {
336 $params['controlName'] = $this->getName() . '_' . $params['var'];
337 $params['value'] = $this->getVar( $params['var'] );
338 return $this->parent
->getRadioSet( $params );
342 * Convenience function to set variables based on form data.
343 * Assumes that variables containing "password" in the name are (potentially
345 * @param $varNames Array
347 public function setVarsFromRequest( $varNames ) {
348 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
352 * Determine whether an existing installation of MediaWiki is present in
353 * the configured administrative connection. Returns true if there is
354 * such a wiki, false if the database doesn't exist.
356 * Traditionally, this is done by testing for the existence of either
357 * the revision table or the cur table.
361 public function needsUpgrade() {
362 $status = $this->getConnection();
363 if ( !$status->isOK() ) {
367 if ( !$this->db
->selectDB( $this->getVar( 'wgDBname' ) ) ) {
370 return $this->db
->tableExists( 'cur' ) ||
$this->db
->tableExists( 'revision' );
374 * Get a standard install-user fieldset.
376 public function getInstallUserBox() {
378 Html
::openElement( 'fieldset' ) .
379 Html
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
380 $this->getTextBox( '_InstallUser', 'config-db-username', array(), $this->parent
->getHelpBox( 'config-db-install-username' ) ) .
381 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array(), $this->parent
->getHelpBox( 'config-db-install-password' ) ) .
382 Html
::closeElement( 'fieldset' );
386 * Submit a standard install user fieldset.
388 public function submitInstallUserBox() {
389 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
390 return Status
::newGood();
394 * Get a standard web-user fieldset
395 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
396 * Set this to false to show a creation checkbox.
398 public function getWebUserBox( $noCreateMsg = false ) {
399 $s = Html
::openElement( 'fieldset' ) .
400 Html
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
402 '_SameAccount', 'config-db-web-account-same',
403 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
405 Html
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
406 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
407 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
408 $this->parent
->getHelpBox( 'config-db-web-help' );
409 if ( $noCreateMsg ) {
410 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
412 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
414 $s .= Html
::closeElement( 'div' ) . Html
::closeElement( 'fieldset' );
419 * Submit the form from getWebUserBox().
423 public function submitWebUserBox() {
424 $this->setVarsFromRequest(
425 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
428 if ( $this->getVar( '_SameAccount' ) ) {
429 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
430 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
433 return Status
::newGood();
437 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
439 public function populateInterwikiTable() {
440 $status = $this->getConnection();
441 if ( !$status->isOK() ) {
444 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
446 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
447 $status->warning( 'config-install-interwiki-exists' );
451 $rows = file( "$IP/maintenance/interwiki.list",
452 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
453 $interwikis = array();
455 return Status
::newFatal( 'config-install-interwiki-sql' );
457 foreach( $rows as $row ) {
458 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
459 if ( $row == "" ) continue;
461 $interwikis[] = array_combine(
462 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
466 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
467 return Status
::newGood();
470 public function outputHandler( $string ) {
471 return htmlspecialchars( $string );