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
126 * @todo make abstract
128 /*abstract*/ function doUpgrade() {
133 * Allow DB installers a chance to make last-minute changes before installation
134 * occurs. This happens before setupDatabase() or createTables() is called, but
135 * long after the constructor. Helpful for things like modifying setup steps :)
137 public function preInstall() {
142 * Get an array of MW configuration globals that will be configured by this class.
144 public function getGlobalNames() {
145 return $this->globalNames
;
149 * Return any table options to be applied to all tables that don't
154 public function getTableOptions() {
159 * Construct and initialise parent.
160 * This is typically only called from Installer::getDBInstaller()
162 public function __construct( $parent ) {
163 $this->parent
= $parent;
167 * Convenience function.
168 * Check if a named extension is present.
172 protected static function checkExtension( $name ) {
173 wfSuppressWarnings();
174 $compiled = wfDl( $name );
180 * Get the internationalised name for this DBMS.
182 public function getReadableName() {
183 return wfMsg( 'config-type-' . $this->getName() );
187 * Get a name=>value map of MW configuration globals that overrides.
188 * DefaultSettings.php
190 public function getGlobalDefaults() {
195 * Get a name=>value map of internal variables used during installation.
197 public function getInternalDefaults() {
198 return $this->internalDefaults
;
202 * Get a variable, taking local defaults into account.
204 public function getVar( $var, $default = null ) {
205 $defaults = $this->getGlobalDefaults();
206 $internal = $this->getInternalDefaults();
207 if ( isset( $defaults[$var] ) ) {
208 $default = $defaults[$var];
209 } elseif ( isset( $internal[$var] ) ) {
210 $default = $internal[$var];
212 return $this->parent
->getVar( $var, $default );
216 * Convenience alias for $this->parent->setVar()
218 public function setVar( $name, $value ) {
219 $this->parent
->setVar( $name, $value );
223 * Get a labelled text box to configure a local variable.
225 public function getTextBox( $var, $label, $attribs = array() ) {
226 $name = $this->getName() . '_' . $var;
227 $value = $this->getVar( $var );
228 return $this->parent
->getTextBox( array(
231 'attribs' => $attribs,
232 'controlName' => $name,
238 * Get a labelled password box to configure a local variable.
239 * Implements password hiding.
241 public function getPasswordBox( $var, $label, $attribs = array() ) {
242 $name = $this->getName() . '_' . $var;
243 $value = $this->getVar( $var );
244 return $this->parent
->getPasswordBox( array(
247 'attribs' => $attribs,
248 'controlName' => $name,
254 * Get a labelled checkbox to configure a local boolean variable.
256 public function getCheckBox( $var, $label, $attribs = array() ) {
257 $name = $this->getName() . '_' . $var;
258 $value = $this->getVar( $var );
259 return $this->parent
->getCheckBox( array(
262 'attribs' => $attribs,
263 'controlName' => $name,
269 * Get a set of labelled radio buttons.
271 * @param $params Array:
273 * var: The variable to be configured (required)
274 * label: The message name for the label (required)
275 * itemLabelPrefix: The message name prefix for the item labels (required)
276 * values: List of allowed values (required)
277 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
280 public function getRadioSet( $params ) {
281 $params['controlName'] = $this->getName() . '_' . $params['var'];
282 $params['value'] = $this->getVar( $params['var'] );
283 return $this->parent
->getRadioSet( $params );
287 * Convenience function to set variables based on form data.
288 * Assumes that variables containing "password" in the name are (potentially
290 * @param $varNames Array
292 public function setVarsFromRequest( $varNames ) {
293 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
297 * Determine whether an existing installation of MediaWiki is present in
298 * the configured administrative connection. Returns true if there is
299 * such a wiki, false if the database doesn't exist.
301 * Traditionally, this is done by testing for the existence of either
302 * the revision table or the cur table.
306 public function needsUpgrade() {
307 $status = $this->getConnection();
308 if ( !$status->isOK() ) {
312 if ( !$this->db
->selectDB( $this->getVar( 'wgDBname' ) ) ) {
315 return $this->db
->tableExists( 'cur' ) ||
$this->db
->tableExists( 'revision' );
319 * Get a standard install-user fieldset.
321 public function getInstallUserBox() {
323 Xml
::openElement( 'fieldset' ) .
324 Xml
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
325 $this->getTextBox( '_InstallUser', 'config-db-username' ) .
326 $this->getPasswordBox( '_InstallPassword', 'config-db-password' ) .
327 $this->parent
->getHelpBox( 'config-db-install-help' ) .
328 Xml
::closeElement( 'fieldset' );
332 * Submit a standard install user fieldset.
334 public function submitInstallUserBox() {
335 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
336 return Status
::newGood();
340 * Get a standard web-user fieldset
341 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
342 * Set this to false to show a creation checkbox.
344 public function getWebUserBox( $noCreateMsg = false ) {
345 $name = $this->getName();
346 $s = Xml
::openElement( 'fieldset' ) .
347 Xml
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
349 '_SameAccount', 'config-db-web-account-same',
350 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
352 Xml
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
353 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
354 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
355 $this->parent
->getHelpBox( 'config-db-web-help' );
356 if ( $noCreateMsg ) {
357 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
359 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
361 $s .= Xml
::closeElement( 'div' ) . Xml
::closeElement( 'fieldset' );
366 * Submit the form from getWebUserBox().
370 public function submitWebUserBox() {
371 $this->setVarsFromRequest(
372 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
375 if ( $this->getVar( '_SameAccount' ) ) {
376 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
377 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
380 return Status
::newGood();
384 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
386 public function populateInterwikiTable() {
387 $status = $this->getConnection();
388 if ( !$status->isOK() ) {
391 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
393 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
394 $status->warning( 'config-install-interwiki-exists' );
398 $rows = file( "$IP/maintenance/interwiki.list",
399 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
400 $interwikis = array();
402 return Status
::newFatal( 'config-install-interwiki-sql' );
404 foreach( $rows as $row ) {
405 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
406 if ( $row == "" ) continue;
408 $interwikis[] = array_combine(
409 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
413 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
414 return Status
::newGood();