wiki.techinc.nl/includes/installer/DatabaseInstaller.php

427 lines
11 KiB
PHP
Raw Normal View History

<?php
/**
* DBMS-specific installation helper.
*
* @file
* @ingroup Deployment
*/
/**
* Base class for DBMS-specific installation helper classes.
2010-07-29 18:36:39 +00:00
*
* @ingroup Deployment
* @since 1.17
*/
abstract class DatabaseInstaller {
2010-07-20 11:00:51 +00:00
2010-07-20 11:17:36 +00:00
/**
* The Installer object.
*
* TODO: naming this parent is confusing, 'installer' would be clearer.
*
* @var Installer
*/
2010-07-20 11:00:51 +00:00
public $parent;
2010-07-20 11:17:36 +00:00
/**
* The database connection.
*
* @var DatabaseBase
*/
2010-07-20 11:00:51 +00:00
public $db;
2010-07-20 11:17:36 +00:00
/**
* Internal variables for installation.
*
* @var array
*/
protected $internalDefaults = array();
2010-07-20 11:17:36 +00:00
/**
* Array of MW configuration globals this class uses.
*
* @var array
*/
protected $globalNames = array();
/**
2010-07-20 11:00:51 +00:00
* Return the internal name, e.g. 'mysql', or 'sqlite'.
*/
2010-07-20 11:00:51 +00:00
public abstract function getName();
/**
2010-07-20 11:00:51 +00:00
* @return true if the client library is compiled in.
*/
2010-07-20 11:00:51 +00:00
public abstract function isCompiled();
/**
* Get HTML for a web form that configures this database. Configuration
* at this time should be the minimum needed to connect and test
* whether install or upgrade is required.
*
2010-07-20 11:17:36 +00:00
* If this is called, $this->parent can be assumed to be a WebInstaller.
*/
2010-07-20 11:00:51 +00:00
public abstract function getConnectForm();
/**
* Set variables based on the request array, assuming it was submitted
* via the form returned by getConnectForm(). Validate the connection
* settings by attempting to connect with them.
*
2010-07-20 11:17:36 +00:00
* If this is called, $this->parent can be assumed to be a WebInstaller.
*
* @return Status
*/
2010-07-20 11:00:51 +00:00
public abstract function submitConnectForm();
/**
* Get HTML for a web form that retrieves settings used for installation.
* $this->parent can be assumed to be a WebInstaller.
* If the DB type has no settings beyond those already configured with
* getConnectForm(), this should return false.
*/
public function getSettingsForm() {
return false;
}
/**
* Set variables based on the request array, assuming it was submitted via
* the form return by getSettingsForm().
2010-07-20 11:17:36 +00:00
*
* @return Status
*/
public function submitSettingsForm() {
return Status::newGood();
}
/**
* Connect to the database using the administrative user/password currently
* defined in the session. On success, return the connection, on failure,
* return a Status object.
*
* This may be called multiple times, so the result should be cached.
*/
2010-07-20 11:00:51 +00:00
public abstract function getConnection();
2010-07-20 11:17:36 +00:00
/**
* Create the database and return a Status object indicating success or
* failure.
*
* @return Status
*/
2010-07-20 11:00:51 +00:00
public abstract function setupDatabase();
/**
2010-07-20 11:17:36 +00:00
* Create database tables from scratch.
*
* @return Status
*/
2010-07-20 11:00:51 +00:00
public abstract function createTables();
2010-07-20 11:17:36 +00:00
/**
* Get the DBMS-specific options for LocalSettings.php generation.
*
* @return String
*/
public abstract function getLocalSettings();
/**
* Perform database upgrades
*
* @return Boolean
*/
public abstract function doUpgrade();
2010-07-20 11:17:36 +00:00
/**
* Allow DB installers a chance to make last-minute changes before installation
* occurs. This happens before setupDatabase() or createTables() is called, but
* long after the constructor. Helpful for things like modifying setup steps :)
*/
public function preInstall() {
}
/**
* Allow DB installers a chance to make checks before upgrade.
*/
public function preUpgrade() {
}
2010-07-20 11:17:36 +00:00
/**
* Get an array of MW configuration globals that will be configured by this class.
*/
public function getGlobalNames() {
return $this->globalNames;
}
/**
* Return any table options to be applied to all tables that don't
2010-07-20 11:17:36 +00:00
* override them.
*
* @return Array
*/
2010-07-20 11:00:51 +00:00
public function getTableOptions() {
return array();
}
/**
* Construct and initialise parent.
* This is typically only called from Installer::getDBInstaller()
*/
2010-07-20 11:00:51 +00:00
public function __construct( $parent ) {
$this->parent = $parent;
}
/**
2010-07-20 11:17:36 +00:00
* Convenience function.
* Check if a named extension is present.
*
* @see wfDl
*/
protected static function checkExtension( $name ) {
wfSuppressWarnings();
$compiled = wfDl( $name );
wfRestoreWarnings();
return $compiled;
}
/**
2010-07-20 11:17:36 +00:00
* Get the internationalised name for this DBMS.
*/
2010-07-20 11:00:51 +00:00
public function getReadableName() {
return wfMsg( 'config-type-' . $this->getName() );
}
/**
2010-07-20 11:17:36 +00:00
* Get a name=>value map of MW configuration globals that overrides.
* DefaultSettings.php
*/
2010-07-20 11:00:51 +00:00
public function getGlobalDefaults() {
return array();
}
/**
2010-07-20 11:17:36 +00:00
* Get a name=>value map of internal variables used during installation.
*/
public function getInternalDefaults() {
return $this->internalDefaults;
}
/**
2010-07-20 11:17:36 +00:00
* Get a variable, taking local defaults into account.
*/
2010-07-20 11:00:51 +00:00
public function getVar( $var, $default = null ) {
$defaults = $this->getGlobalDefaults();
$internal = $this->getInternalDefaults();
if ( isset( $defaults[$var] ) ) {
$default = $defaults[$var];
} elseif ( isset( $internal[$var] ) ) {
$default = $internal[$var];
}
return $this->parent->getVar( $var, $default );
}
/**
* Convenience alias for $this->parent->setVar()
*/
2010-07-20 11:00:51 +00:00
public function setVar( $name, $value ) {
$this->parent->setVar( $name, $value );
}
/**
2010-07-20 11:17:36 +00:00
* Get a labelled text box to configure a local variable.
*/
2010-07-20 11:00:51 +00:00
public function getTextBox( $var, $label, $attribs = array() ) {
$name = $this->getName() . '_' . $var;
$value = $this->getVar( $var );
return $this->parent->getTextBox( array(
'var' => $var,
'label' => $label,
'attribs' => $attribs,
'controlName' => $name,
'value' => $value
) );
}
/**
2010-07-20 11:17:36 +00:00
* Get a labelled password box to configure a local variable.
* Implements password hiding.
*/
2010-07-20 11:00:51 +00:00
public function getPasswordBox( $var, $label, $attribs = array() ) {
$name = $this->getName() . '_' . $var;
$value = $this->getVar( $var );
return $this->parent->getPasswordBox( array(
'var' => $var,
'label' => $label,
'attribs' => $attribs,
'controlName' => $name,
'value' => $value
) );
}
/**
2010-07-20 11:17:36 +00:00
* Get a labelled checkbox to configure a local boolean variable.
*/
2010-07-20 11:00:51 +00:00
public function getCheckBox( $var, $label, $attribs = array() ) {
$name = $this->getName() . '_' . $var;
$value = $this->getVar( $var );
return $this->parent->getCheckBox( array(
'var' => $var,
'label' => $label,
'attribs' => $attribs,
'controlName' => $name,
'value' => $value,
));
}
/**
2010-07-20 11:17:36 +00:00
* Get a set of labelled radio buttons.
*
* @param $params Array:
* Parameters are:
* var: The variable to be configured (required)
* label: The message name for the label (required)
* itemLabelPrefix: The message name prefix for the item labels (required)
* values: List of allowed values (required)
* itemAttribs Array of attribute arrays, outer key is the value name (optional)
*
*/
2010-07-20 11:00:51 +00:00
public function getRadioSet( $params ) {
$params['controlName'] = $this->getName() . '_' . $params['var'];
$params['value'] = $this->getVar( $params['var'] );
return $this->parent->getRadioSet( $params );
}
/**
* Convenience function to set variables based on form data.
* Assumes that variables containing "password" in the name are (potentially
* fake) passwords.
* @param $varNames Array
*/
2010-07-20 11:00:51 +00:00
public function setVarsFromRequest( $varNames ) {
return $this->parent->setVarsFromRequest( $varNames, $this->getName() . '_' );
}
/**
* Determine whether an existing installation of MediaWiki is present in
* the configured administrative connection. Returns true if there is
* such a wiki, false if the database doesn't exist.
*
* Traditionally, this is done by testing for the existence of either
* the revision table or the cur table.
*
* @return Boolean
*/
2010-07-20 11:00:51 +00:00
public function needsUpgrade() {
$status = $this->getConnection();
if ( !$status->isOK() ) {
return false;
}
if ( !$this->db->selectDB( $this->getVar( 'wgDBname' ) ) ) {
return false;
}
return $this->db->tableExists( 'cur' ) || $this->db->tableExists( 'revision' );
}
/**
2010-07-20 11:17:36 +00:00
* Get a standard install-user fieldset.
*/
2010-07-20 11:00:51 +00:00
public function getInstallUserBox() {
return
Xml::openElement( 'fieldset' ) .
Xml::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
$this->getTextBox( '_InstallUser', 'config-db-username' ) .
$this->getPasswordBox( '_InstallPassword', 'config-db-password' ) .
$this->parent->getHelpBox( 'config-db-install-help' ) .
Xml::closeElement( 'fieldset' );
}
/**
2010-07-20 11:17:36 +00:00
* Submit a standard install user fieldset.
*/
2010-07-20 11:00:51 +00:00
public function submitInstallUserBox() {
$this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
return Status::newGood();
}
/**
* Get a standard web-user fieldset
* @param $noCreateMsg String: Message to display instead of the creation checkbox.
* Set this to false to show a creation checkbox.
*/
2010-07-20 11:00:51 +00:00
public function getWebUserBox( $noCreateMsg = false ) {
$name = $this->getName();
$s = Xml::openElement( 'fieldset' ) .
Xml::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
$this->getCheckBox(
'_SameAccount', 'config-db-web-account-same',
array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
) .
Xml::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
$this->getTextBox( 'wgDBuser', 'config-db-username' ) .
$this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
$this->parent->getHelpBox( 'config-db-web-help' );
if ( $noCreateMsg ) {
$s .= $this->parent->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
} else {
$s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
}
$s .= Xml::closeElement( 'div' ) . Xml::closeElement( 'fieldset' );
return $s;
}
/**
* Submit the form from getWebUserBox().
2010-07-20 11:17:36 +00:00
*
* @return Status
*/
2010-07-20 11:00:51 +00:00
public function submitWebUserBox() {
2010-07-20 11:17:36 +00:00
$this->setVarsFromRequest(
array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
);
if ( $this->getVar( '_SameAccount' ) ) {
$this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
$this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
}
2010-07-20 11:17:36 +00:00
return Status::newGood();
}
/**
2010-07-20 11:17:36 +00:00
* Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
*/
public function populateInterwikiTable() {
$status = $this->getConnection();
if ( !$status->isOK() ) {
return $status;
}
$this->db->selectDB( $this->getVar( 'wgDBname' ) );
if( $this->db->selectRow( 'interwiki', '*', array(), __METHOD__ ) ) {
$status->warning( 'config-install-interwiki-exists' );
return $status;
}
global $IP;
$rows = file( "$IP/maintenance/interwiki.list",
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$interwikis = array();
if ( !$rows ) {
return Status::newFatal( 'config-install-interwiki-sql' );
}
foreach( $rows as $row ) {
$row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
if ( $row == "" ) continue;
$row .= "||";
$interwikis[] = array_combine(
array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
explode( '|', $row )
);
}
$this->db->insert( 'interwiki', $interwikis, __METHOD__ );
return Status::newGood();
}
2010-07-20 11:00:51 +00:00
}