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 abstract function getSettingsForm();
86 * Set variables based on the request array, assuming it was submitted via
87 * the form return by getSettingsForm().
91 public abstract function submitSettingsForm();
94 * Connect to the database using the administrative user/password currently
95 * defined in the session. On success, return the connection, on failure,
96 * return a Status object.
98 * This may be called multiple times, so the result should be cached.
100 public abstract function getConnection();
103 * Create the database and return a Status object indicating success or
108 public abstract function setupDatabase();
111 * Create database tables from scratch.
115 public abstract function createTables();
118 * Get the DBMS-specific options for LocalSettings.php generation.
122 public abstract function getLocalSettings();
125 * Perform database upgrades
129 public abstract function doUpgrade();
132 * Allow DB installers a chance to make last-minute changes before installation
133 * occurs. This happens before setupDatabase() or createTables() is called, but
134 * long after the constructor. Helpful for things like modifying setup steps :)
136 public function preInstall() {
141 * Allow DB installers a chance to make checks before upgrade.
143 public function preUpgrade() {
148 * Get an array of MW configuration globals that will be configured by this class.
150 public function getGlobalNames() {
151 return $this->globalNames
;
155 * Return any table options to be applied to all tables that don't
160 public function getTableOptions() {
165 * Construct and initialise parent.
166 * This is typically only called from Installer::getDBInstaller()
168 public function __construct( $parent ) {
169 $this->parent
= $parent;
173 * Convenience function.
174 * Check if a named extension is present.
178 protected static function checkExtension( $name ) {
179 wfSuppressWarnings();
180 $compiled = wfDl( $name );
186 * Get the internationalised name for this DBMS.
188 public function getReadableName() {
189 return wfMsg( 'config-type-' . $this->getName() );
193 * Get a name=>value map of MW configuration globals that overrides.
194 * DefaultSettings.php
196 public function getGlobalDefaults() {
201 * Get a name=>value map of internal variables used during installation.
203 public function getInternalDefaults() {
204 return $this->internalDefaults
;
208 * Get a variable, taking local defaults into account.
210 public function getVar( $var, $default = null ) {
211 $defaults = $this->getGlobalDefaults();
212 $internal = $this->getInternalDefaults();
213 if ( isset( $defaults[$var] ) ) {
214 $default = $defaults[$var];
215 } elseif ( isset( $internal[$var] ) ) {
216 $default = $internal[$var];
218 return $this->parent
->getVar( $var, $default );
222 * Convenience alias for $this->parent->setVar()
224 public function setVar( $name, $value ) {
225 $this->parent
->setVar( $name, $value );
229 * Get a labelled text box to configure a local variable.
231 public function getTextBox( $var, $label, $attribs = array() ) {
232 $name = $this->getName() . '_' . $var;
233 $value = $this->getVar( $var );
234 return $this->parent
->getTextBox( array(
237 'attribs' => $attribs,
238 'controlName' => $name,
244 * Get a labelled password box to configure a local variable.
245 * Implements password hiding.
247 public function getPasswordBox( $var, $label, $attribs = array() ) {
248 $name = $this->getName() . '_' . $var;
249 $value = $this->getVar( $var );
250 return $this->parent
->getPasswordBox( array(
253 'attribs' => $attribs,
254 'controlName' => $name,
260 * Get a labelled checkbox to configure a local boolean variable.
262 public function getCheckBox( $var, $label, $attribs = array() ) {
263 $name = $this->getName() . '_' . $var;
264 $value = $this->getVar( $var );
265 return $this->parent
->getCheckBox( array(
268 'attribs' => $attribs,
269 'controlName' => $name,
275 * Get a set of labelled radio buttons.
277 * @param $params Array:
279 * var: The variable to be configured (required)
280 * label: The message name for the label (required)
281 * itemLabelPrefix: The message name prefix for the item labels (required)
282 * values: List of allowed values (required)
283 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
286 public function getRadioSet( $params ) {
287 $params['controlName'] = $this->getName() . '_' . $params['var'];
288 $params['value'] = $this->getVar( $params['var'] );
289 return $this->parent
->getRadioSet( $params );
293 * Convenience function to set variables based on form data.
294 * Assumes that variables containing "password" in the name are (potentially
296 * @param $varNames Array
298 public function setVarsFromRequest( $varNames ) {
299 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
303 * Determine whether an existing installation of MediaWiki is present in
304 * the configured administrative connection. Returns true if there is
305 * such a wiki, false if the database doesn't exist.
307 * Traditionally, this is done by testing for the existence of either
308 * the revision table or the cur table.
312 public function needsUpgrade() {
313 $status = $this->getConnection();
314 if ( !$status->isOK() ) {
318 if ( !$this->db
->selectDB( $this->getVar( 'wgDBname' ) ) ) {
321 return $this->db
->tableExists( 'cur' ) ||
$this->db
->tableExists( 'revision' );
325 * Get a standard install-user fieldset.
327 public function getInstallUserBox() {
329 Xml
::openElement( 'fieldset' ) .
330 Xml
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
331 $this->getTextBox( '_InstallUser', 'config-db-username' ) .
332 $this->getPasswordBox( '_InstallPassword', 'config-db-password' ) .
333 $this->parent
->getHelpBox( 'config-db-install-help' ) .
334 Xml
::closeElement( 'fieldset' );
338 * Submit a standard install user fieldset.
340 public function submitInstallUserBox() {
341 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
342 return Status
::newGood();
346 * Get a standard web-user fieldset
347 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
348 * Set this to false to show a creation checkbox.
350 public function getWebUserBox( $noCreateMsg = false ) {
351 $name = $this->getName();
352 $s = Xml
::openElement( 'fieldset' ) .
353 Xml
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
355 '_SameAccount', 'config-db-web-account-same',
356 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
358 Xml
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
359 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
360 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
361 $this->parent
->getHelpBox( 'config-db-web-help' );
362 if ( $noCreateMsg ) {
363 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
365 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
367 $s .= Xml
::closeElement( 'div' ) . Xml
::closeElement( 'fieldset' );
372 * Submit the form from getWebUserBox().
376 public function submitWebUserBox() {
377 $this->setVarsFromRequest(
378 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
381 if ( $this->getVar( '_SameAccount' ) ) {
382 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
383 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
386 return Status
::newGood();
390 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
392 public function populateInterwikiTable() {
393 $status = $this->getConnection();
394 if ( !$status->isOK() ) {
397 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
399 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
400 $status->warning( 'config-install-interwiki-exists' );
404 $rows = file( "$IP/maintenance/interwiki.list",
405 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
406 $interwikis = array();
408 return Status
::newFatal( 'config-install-interwiki-sql' );
410 foreach( $rows as $row ) {
411 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
412 if ( $row == "" ) continue;
414 $interwikis[] = array_combine(
415 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
419 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
420 return Status
::newGood();