4 * Base class for DBMS-specific installation helper classes.
6 abstract class DatabaseInstaller
{
8 /** The Installer object */
11 /* Database connection */
14 /** Internal variables for installation */
15 protected $internalDefaults = array();
17 /** Array of MW configuration globals this class uses */
18 protected $globalNames = array();
21 * Return the internal name, e.g. 'mysql', or 'sqlite'.
23 public abstract function getName();
26 * @return true if the client library is compiled in.
28 public abstract function isCompiled();
31 * Get an array of MW configuration globals that will be configured by this class.
33 public function getGlobalNames() {
34 return $this->globalNames
;
38 * Get HTML for a web form that configures this database. Configuration
39 * at this time should be the minimum needed to connect and test
40 * whether install or upgrade is required.
42 * If this is called, $this->parent can be assumed to be a WebInstaller
44 public abstract function getConnectForm();
47 * Set variables based on the request array, assuming it was submitted
48 * via the form returned by getConnectForm(). Validate the connection
49 * settings by attempting to connect with them.
51 * If this is called, $this->parent can be assumed to be a WebInstaller
55 public abstract function submitConnectForm();
58 * Get HTML for a web form that retrieves settings used for installation.
59 * $this->parent can be assumed to be a WebInstaller.
60 * If the DB type has no settings beyond those already configured with
61 * getConnectForm(), this should return false.
63 public abstract function getSettingsForm();
66 * Set variables based on the request array, assuming it was submitted via
67 * the form return by getSettingsForm().
70 public abstract function submitSettingsForm();
73 * Connect to the database using the administrative user/password currently
74 * defined in the session. On success, return the connection, on failure,
75 * return a Status object.
77 * This may be called multiple times, so the result should be cached.
79 public abstract function getConnection();
82 * Allow DB installers a chance to make last-minute changes before installation
83 * occurs. This happens before setupDatabase() or createTables() is called, but
84 * long after the constructor. Helpful for things like modifying setup steps :)
86 public function preInstall() {}
89 * Create the database and return a Status object indicating success or
94 public abstract function setupDatabase();
97 * Create database tables from scratch
98 * @return \type Status
100 public abstract function createTables();
103 * Perform database upgrades
104 * @todo make abstract
106 /*abstract*/ function doUpgrade() {
111 * Return any table options to be applied to all tables that don't
115 public function getTableOptions() {
120 * Get the DBMS-specific options for LocalSettings.php generation.
123 public abstract function getLocalSettings();
126 * Construct and initialise parent.
127 * This is typically only called from Installer::getDBInstaller()
129 public function __construct( $parent ) {
130 $this->parent
= $parent;
134 * Convenience function
135 * Check if a named extension is present
137 protected static function checkExtension( $name ) {
138 wfSuppressWarnings();
139 $compiled = wfDl( $name );
145 * Get the internationalised name for this DBMS
147 public function getReadableName() {
148 return wfMsg( 'config-type-' . $this->getName() );
152 * Get a name=>value map of MW configuration globals that overrides
153 * DefaultSettings.php
155 public function getGlobalDefaults() {
160 * Get a name=>value map of internal variables used during installation
162 public function getInternalDefaults() {
163 return $this->internalDefaults
;
167 * Get a variable, taking local defaults into account
169 public function getVar( $var, $default = null ) {
170 $defaults = $this->getGlobalDefaults();
171 $internal = $this->getInternalDefaults();
172 if ( isset( $defaults[$var] ) ) {
173 $default = $defaults[$var];
174 } elseif ( isset( $internal[$var] ) ) {
175 $default = $internal[$var];
177 return $this->parent
->getVar( $var, $default );
181 * Convenience alias for $this->parent->setVar()
183 public function setVar( $name, $value ) {
184 $this->parent
->setVar( $name, $value );
188 * Get a labelled text box to configure a local variable
190 public function getTextBox( $var, $label, $attribs = array() ) {
191 $name = $this->getName() . '_' . $var;
192 $value = $this->getVar( $var );
193 return $this->parent
->getTextBox( array(
196 'attribs' => $attribs,
197 'controlName' => $name,
203 * Get a labelled password box to configure a local variable
204 * Implements password hiding
206 public function getPasswordBox( $var, $label, $attribs = array() ) {
207 $name = $this->getName() . '_' . $var;
208 $value = $this->getVar( $var );
209 return $this->parent
->getPasswordBox( array(
212 'attribs' => $attribs,
213 'controlName' => $name,
219 * Get a labelled checkbox to configure a local boolean variable
221 public function getCheckBox( $var, $label, $attribs = array() ) {
222 $name = $this->getName() . '_' . $var;
223 $value = $this->getVar( $var );
224 return $this->parent
->getCheckBox( array(
227 'attribs' => $attribs,
228 'controlName' => $name,
234 * Get a set of labelled radio buttons
236 * @param $params Array:
238 * var: The variable to be configured (required)
239 * label: The message name for the label (required)
240 * itemLabelPrefix: The message name prefix for the item labels (required)
241 * values: List of allowed values (required)
242 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
245 public function getRadioSet( $params ) {
246 $params['controlName'] = $this->getName() . '_' . $params['var'];
247 $params['value'] = $this->getVar( $params['var'] );
248 return $this->parent
->getRadioSet( $params );
252 * Convenience function to set variables based on form data.
253 * Assumes that variables containing "password" in the name are (potentially
255 * @param $varNames Array
257 public function setVarsFromRequest( $varNames ) {
258 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
262 * Determine whether an existing installation of MediaWiki is present in
263 * the configured administrative connection. Returns true if there is
264 * such a wiki, false if the database doesn't exist.
266 * Traditionally, this is done by testing for the existence of either
267 * the revision table or the cur table.
271 public function needsUpgrade() {
272 $status = $this->getConnection();
273 if ( !$status->isOK() ) {
276 $conn = $status->value
;
277 if ( !$conn->selectDB( $this->getVar( 'wgDBname' ) ) ) {
280 return $conn->tableExists( 'cur' ) ||
$conn->tableExists( 'revision' );
284 * Get a standard install-user fieldset
286 public function getInstallUserBox() {
288 Xml
::openElement( 'fieldset' ) .
289 Xml
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
290 $this->getTextBox( '_InstallUser', 'config-db-username' ) .
291 $this->getPasswordBox( '_InstallPassword', 'config-db-password' ) .
292 $this->parent
->getHelpBox( 'config-db-install-help' ) .
293 Xml
::closeElement( 'fieldset' );
297 * Submit a standard install user fieldset
299 public function submitInstallUserBox() {
300 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
301 return Status
::newGood();
305 * Get a standard web-user fieldset
306 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
307 * Set this to false to show a creation checkbox.
309 public function getWebUserBox( $noCreateMsg = false ) {
310 $name = $this->getName();
311 $s = Xml
::openElement( 'fieldset' ) .
312 Xml
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
314 '_SameAccount', 'config-db-web-account-same',
315 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
317 Xml
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
318 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
319 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
320 $this->parent
->getHelpBox( 'config-db-web-help' );
321 if ( $noCreateMsg ) {
322 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
324 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
326 $s .= Xml
::closeElement( 'div' ) . Xml
::closeElement( 'fieldset' );
331 * Submit the form from getWebUserBox().
334 public function submitWebUserBox() {
335 $this->setVarsFromRequest( array( 'wgDBuser', 'wgDBpassword',
336 '_SameAccount', '_CreateDBAccount' ) );
337 if ( $this->getVar( '_SameAccount' ) ) {
338 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
339 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
341 return Status
::newGood();
345 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql
347 public function populateInterwikiTable() {
348 $status = $this->getConnection();
349 if ( !$status->isOK() ) {
352 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
354 if( $this->db
->selectRow( 'interwiki', '*', array(), __METHOD__
) ) {
355 $status->warning( 'config-install-interwiki-exists' );
359 $rows = file( "$IP/maintenance/interwiki.list",
360 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
361 $interwikis = array();
363 return Status
::newFatal( 'config-install-interwiki-sql' );
365 foreach( $rows as $row ) {
366 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
367 if ( $row == "" ) continue;
368 $interwikis[] = array_combine(
369 array( 'iw_prefix', 'iw_url', 'iw_local' ),
373 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
374 return Status
::newGood();