4 * Base class for DBMS-specific installation helper classes
6 abstract class InstallerDBType
{
7 /** The Installer object */
10 /* Database connection */
13 /** Internal variables for installation */
14 protected $internalDefaults = array();
16 /** Array of MW configuration globals this class uses */
17 protected $globalNames = array();
20 * Return the internal name, e.g. 'mysql', or 'sqlite'
22 abstract function getName();
25 * @return true if the client library is compiled in
27 abstract public function isCompiled();
30 * Get an array of MW configuration globals that will be configured by this class.
32 public function getGlobalNames() {
33 return $this->globalNames
;
37 * Get HTML for a web form that configures this database. Configuration
38 * at this time should be the minimum needed to connect and test
39 * whether install or upgrade is required.
41 * If this is called, $this->parent can be assumed to be a WebInstaller
43 abstract function getConnectForm();
46 * Set variables based on the request array, assuming it was submitted
47 * via the form returned by getConnectForm(). Validate the connection
48 * settings by attempting to connect with them.
50 * If this is called, $this->parent can be assumed to be a WebInstaller
54 abstract function submitConnectForm();
57 * Get HTML for a web form that retrieves settings used for installation.
58 * $this->parent can be assumed to be a WebInstaller.
59 * If the DB type has no settings beyond those already configured with
60 * getConnectForm(), this should return false.
62 abstract function getSettingsForm();
65 * Set variables based on the request array, assuming it was submitted via
66 * the form return by getSettingsForm().
69 abstract function submitSettingsForm();
72 * Connect to the database using the administrative user/password currently
73 * defined in the session. On success, return the connection, on failure,
74 * return a Status object.
76 * This may be called multiple times, so the result should be cached.
78 abstract function getConnection();
81 * Allow DB installers a chance to make last-minute changes before installation
82 * occurs. This happens before setupDatabase() or createTables() is called, but
83 * long after the constructor. Helpful for things like modifying setup steps :)
85 public function preInstall() {}
88 * Create the database and return a Status object indicating success or
93 abstract function setupDatabase();
96 * Create database tables from scratch
97 * @return \type Status
99 abstract function createTables();
102 * Perform database upgrades
103 * @todo make abstract
105 /*abstract*/ function doUpgrade() {
110 * Return any table options to be applied to all tables that don't
114 function getTableOptions() {
119 * Get the DBMS-specific options for LocalSettings.php generation.
122 abstract function getLocalSettings();
125 * Construct and initialise parent.
126 * This is typically only called from Installer::getDBInstaller()
128 function __construct( $parent ) {
129 $this->parent
= $parent;
133 * Convenience function
134 * Check if a named extension is present
136 protected static function checkExtension( $name ) {
137 wfSuppressWarnings();
138 $compiled = wfDl( $name );
144 * Get the internationalised name for this DBMS
146 function getReadableName() {
147 return wfMsg( 'config-type-' . $this->getName() );
151 * Get a name=>value map of MW configuration globals that overrides
152 * DefaultSettings.php
154 function getGlobalDefaults() {
159 * Get a name=>value map of internal variables used during installation
161 public function getInternalDefaults() {
162 return $this->internalDefaults
;
166 * Get a variable, taking local defaults into account
168 function getVar( $var, $default = null ) {
169 $defaults = $this->getGlobalDefaults();
170 $internal = $this->getInternalDefaults();
171 if ( isset( $defaults[$var] ) ) {
172 $default = $defaults[$var];
173 } elseif ( isset( $internal[$var] ) ) {
174 $default = $internal[$var];
176 return $this->parent
->getVar( $var, $default );
180 * Convenience alias for $this->parent->setVar()
182 function setVar( $name, $value ) {
183 $this->parent
->setVar( $name, $value );
187 * Get a labelled text box to configure a local variable
189 function getTextBox( $var, $label, $attribs = array() ) {
190 $name = $this->getName() . '_' . $var;
191 $value = $this->getVar( $var );
192 return $this->parent
->getTextBox( array(
195 'attribs' => $attribs,
196 'controlName' => $name,
202 * Get a labelled password box to configure a local variable
203 * Implements password hiding
205 function getPasswordBox( $var, $label, $attribs = array() ) {
206 $name = $this->getName() . '_' . $var;
207 $value = $this->getVar( $var );
208 return $this->parent
->getPasswordBox( array(
211 'attribs' => $attribs,
212 'controlName' => $name,
218 * Get a labelled checkbox to configure a local boolean variable
220 function getCheckBox( $var, $label, $attribs = array() ) {
221 $name = $this->getName() . '_' . $var;
222 $value = $this->getVar( $var );
223 return $this->parent
->getCheckBox( array(
226 'attribs' => $attribs,
227 'controlName' => $name,
233 * Get a set of labelled radio buttons
235 * @param $params Array:
237 * var: The variable to be configured (required)
238 * label: The message name for the label (required)
239 * itemLabelPrefix: The message name prefix for the item labels (required)
240 * values: List of allowed values (required)
241 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
244 function getRadioSet( $params ) {
245 $params['controlName'] = $this->getName() . '_' . $params['var'];
246 $params['value'] = $this->getVar( $params['var'] );
247 return $this->parent
->getRadioSet( $params );
251 * Convenience function to set variables based on form data.
252 * Assumes that variables containing "password" in the name are (potentially
254 * @param $varNames Array
256 function setVarsFromRequest( $varNames ) {
257 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
261 * Determine whether an existing installation of MediaWiki is present in
262 * the configured administrative connection. Returns true if there is
263 * such a wiki, false if the database doesn't exist.
265 * Traditionally, this is done by testing for the existence of either
266 * the revision table or the cur table.
270 function needsUpgrade() {
271 $status = $this->getConnection();
272 if ( !$status->isOK() ) {
275 $conn = $status->value
;
276 if ( !$conn->selectDB( $this->getVar( 'wgDBname' ) ) ) {
279 return $conn->tableExists( 'cur' ) ||
$conn->tableExists( 'revision' );
283 * Get a standard install-user fieldset
285 function getInstallUserBox() {
287 Xml
::openElement( 'fieldset' ) .
288 Xml
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
289 $this->getTextBox( '_InstallUser', 'config-db-username' ) .
290 $this->getPasswordBox( '_InstallPassword', 'config-db-password' ) .
291 $this->parent
->getHelpBox( 'config-db-install-help' ) .
292 Xml
::closeElement( 'fieldset' );
296 * Submit a standard install user fieldset
298 function submitInstallUserBox() {
299 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
300 return Status
::newGood();
304 * Get a standard web-user fieldset
305 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
306 * Set this to false to show a creation checkbox.
308 function getWebUserBox( $noCreateMsg = false ) {
309 $name = $this->getName();
310 $s = Xml
::openElement( 'fieldset' ) .
311 Xml
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
313 '_SameAccount', 'config-db-web-account-same',
314 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
316 Xml
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
317 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
318 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
319 $this->parent
->getHelpBox( 'config-db-web-help' );
320 if ( $noCreateMsg ) {
321 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
323 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
325 $s .= Xml
::closeElement( 'div' ) . Xml
::closeElement( 'fieldset' );
330 * Submit the form from getWebUserBox().
333 function submitWebUserBox() {
334 $this->setVarsFromRequest( array( 'wgDBuser', 'wgDBpassword',
335 '_SameAccount', '_CreateDBAccount' ) );
336 if ( $this->getVar( '_SameAccount' ) ) {
337 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
338 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
340 return Status
::newGood();
344 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql
346 public function populateInterwikiTable() {
347 $status = $this->getConnection();
348 if ( !$status->isOK() ) {
351 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
353 $rows = file( "$IP/maintenance/interwiki.list",
354 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
355 $interwikis = array();
357 return Status
::newFatal( 'config-install-interwiki-sql' );
359 foreach( $rows as $row ) {
360 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
361 if ( $row == "" ) continue;
362 $interwikis[] = array_combine(
363 array( 'iw_prefix', 'iw_url', 'iw_local' ),
367 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
368 return Status
::newGood();