4 * Base class for DBMS-specific installation helper classes.
9 abstract class DatabaseInstaller
{
12 * The Installer object.
14 * TODO: naming this parent is confusing, 'installer' would be clearer.
21 * The database connection.
28 * Internal variables for installation.
32 protected $internalDefaults = array();
35 * Array of MW configuration globals this class uses.
39 protected $globalNames = array();
42 * Return the internal name, e.g. 'mysql', or 'sqlite'.
44 public abstract function getName();
47 * @return true if the client library is compiled in.
49 public abstract function isCompiled();
52 * Get HTML for a web form that configures this database. Configuration
53 * at this time should be the minimum needed to connect and test
54 * whether install or upgrade is required.
56 * If this is called, $this->parent can be assumed to be a WebInstaller.
58 public abstract function getConnectForm();
61 * Set variables based on the request array, assuming it was submitted
62 * via the form returned by getConnectForm(). Validate the connection
63 * settings by attempting to connect with them.
65 * If this is called, $this->parent can be assumed to be a WebInstaller.
69 public abstract function submitConnectForm();
72 * Get HTML for a web form that retrieves settings used for installation.
73 * $this->parent can be assumed to be a WebInstaller.
74 * If the DB type has no settings beyond those already configured with
75 * getConnectForm(), this should return false.
77 public abstract function getSettingsForm();
80 * Set variables based on the request array, assuming it was submitted via
81 * the form return by getSettingsForm().
85 public abstract function submitSettingsForm();
88 * Connect to the database using the administrative user/password currently
89 * defined in the session. On success, return the connection, on failure,
90 * return a Status object.
92 * This may be called multiple times, so the result should be cached.
94 public abstract function getConnection();
97 * Create the database and return a Status object indicating success or
102 public abstract function setupDatabase();
105 * Create database tables from scratch.
109 public abstract function createTables();
112 * Get the DBMS-specific options for LocalSettings.php generation.
116 public abstract function getLocalSettings();
119 * Perform database upgrades
120 * @todo make abstract
122 /*abstract*/ function doUpgrade() {
127 * Allow DB installers a chance to make last-minute changes before installation
128 * occurs. This happens before setupDatabase() or createTables() is called, but
129 * long after the constructor. Helpful for things like modifying setup steps :)
131 public function preInstall() {
136 * Get an array of MW configuration globals that will be configured by this class.
138 public function getGlobalNames() {
139 return $this->globalNames
;
143 * Return any table options to be applied to all tables that don't
148 public function getTableOptions() {
153 * Construct and initialise parent.
154 * This is typically only called from Installer::getDBInstaller()
156 public function __construct( $parent ) {
157 $this->parent
= $parent;
161 * Convenience function.
162 * Check if a named extension is present.
166 protected static function checkExtension( $name ) {
167 wfSuppressWarnings();
168 $compiled = wfDl( $name );
174 * Get the internationalised name for this DBMS.
176 public function getReadableName() {
177 return wfMsg( 'config-type-' . $this->getName() );
181 * Get a name=>value map of MW configuration globals that overrides.
182 * DefaultSettings.php
184 public function getGlobalDefaults() {
189 * Get a name=>value map of internal variables used during installation.
191 public function getInternalDefaults() {
192 return $this->internalDefaults
;
196 * Get a variable, taking local defaults into account.
198 public function getVar( $var, $default = null ) {
199 $defaults = $this->getGlobalDefaults();
200 $internal = $this->getInternalDefaults();
201 if ( isset( $defaults[$var] ) ) {
202 $default = $defaults[$var];
203 } elseif ( isset( $internal[$var] ) ) {
204 $default = $internal[$var];
206 return $this->parent
->getVar( $var, $default );
210 * Convenience alias for $this->parent->setVar()
212 public function setVar( $name, $value ) {
213 $this->parent
->setVar( $name, $value );
217 * Get a labelled text box to configure a local variable.
219 public function getTextBox( $var, $label, $attribs = array() ) {
220 $name = $this->getName() . '_' . $var;
221 $value = $this->getVar( $var );
222 return $this->parent
->getTextBox( array(
225 'attribs' => $attribs,
226 'controlName' => $name,
232 * Get a labelled password box to configure a local variable.
233 * Implements password hiding.
235 public function getPasswordBox( $var, $label, $attribs = array() ) {
236 $name = $this->getName() . '_' . $var;
237 $value = $this->getVar( $var );
238 return $this->parent
->getPasswordBox( array(
241 'attribs' => $attribs,
242 'controlName' => $name,
248 * Get a labelled checkbox to configure a local boolean variable.
250 public function getCheckBox( $var, $label, $attribs = array() ) {
251 $name = $this->getName() . '_' . $var;
252 $value = $this->getVar( $var );
253 return $this->parent
->getCheckBox( array(
256 'attribs' => $attribs,
257 'controlName' => $name,
263 * Get a set of labelled radio buttons.
265 * @param $params Array:
267 * var: The variable to be configured (required)
268 * label: The message name for the label (required)
269 * itemLabelPrefix: The message name prefix for the item labels (required)
270 * values: List of allowed values (required)
271 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
274 public function getRadioSet( $params ) {
275 $params['controlName'] = $this->getName() . '_' . $params['var'];
276 $params['value'] = $this->getVar( $params['var'] );
277 return $this->parent
->getRadioSet( $params );
281 * Convenience function to set variables based on form data.
282 * Assumes that variables containing "password" in the name are (potentially
284 * @param $varNames Array
286 public function setVarsFromRequest( $varNames ) {
287 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
291 * Determine whether an existing installation of MediaWiki is present in
292 * the configured administrative connection. Returns true if there is
293 * such a wiki, false if the database doesn't exist.
295 * Traditionally, this is done by testing for the existence of either
296 * the revision table or the cur table.
300 public function needsUpgrade() {
301 $status = $this->getConnection();
302 if ( !$status->isOK() ) {
306 if ( !$this->db
->selectDB( $this->getVar( 'wgDBname' ) ) ) {
309 return $this->db
->tableExists( 'cur' ) ||
$this->db
->tableExists( 'revision' );
313 * Get a standard install-user fieldset.
315 public function getInstallUserBox() {
317 Xml
::openElement( 'fieldset' ) .
318 Xml
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
319 $this->getTextBox( '_InstallUser', 'config-db-username' ) .
320 $this->getPasswordBox( '_InstallPassword', 'config-db-password' ) .
321 $this->parent
->getHelpBox( 'config-db-install-help' ) .
322 Xml
::closeElement( 'fieldset' );
326 * Submit a standard install user fieldset.
328 public function submitInstallUserBox() {
329 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
330 return Status
::newGood();
334 * Get a standard web-user fieldset
335 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
336 * Set this to false to show a creation checkbox.
338 public function getWebUserBox( $noCreateMsg = false ) {
339 $name = $this->getName();
340 $s = Xml
::openElement( 'fieldset' ) .
341 Xml
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
343 '_SameAccount', 'config-db-web-account-same',
344 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
346 Xml
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
347 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
348 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
349 $this->parent
->getHelpBox( 'config-db-web-help' );
350 if ( $noCreateMsg ) {
351 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
353 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
355 $s .= Xml
::closeElement( 'div' ) . Xml
::closeElement( 'fieldset' );
360 * Submit the form from getWebUserBox().
364 public function submitWebUserBox() {
365 $this->setVarsFromRequest(
366 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
369 if ( $this->getVar( '_SameAccount' ) ) {
370 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
371 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
374 return Status
::newGood();
378 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
380 public function populateInterwikiTable() {
381 $status = $this->getConnection();
382 if ( !$status->isOK() ) {
385 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
387 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
388 $status->warning( 'config-install-interwiki-exists' );
392 $rows = file( "$IP/maintenance/interwiki.list",
393 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
394 $interwikis = array();
396 return Status
::newFatal( 'config-install-interwiki-sql' );
398 foreach( $rows as $row ) {
399 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
400 if ( $row == "" ) continue;
402 $interwikis[] = array_combine(
403 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
407 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
408 return Status
::newGood();