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 static 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 * Create the database and return a Status object indicating success or
86 abstract function setupDatabase();
89 * Create database tables from scratch
90 * @return \type Status
92 abstract function createTables();
95 * Perform database upgrades
98 /*abstract*/ function doUpgrade() {
103 * Return any table options to be applied to all tables that don't
107 function getTableOptions() {
112 * Get the DBMS-specific options for LocalSettings.php generation.
115 abstract function getLocalSettings();
118 * Construct and initialise parent.
119 * This is typically only called from Installer::getDBInstaller()
121 function __construct( $parent ) {
122 $this->parent
= $parent;
126 * Convenience function
127 * Check if a named extension is present
129 static function checkExtension( $name ) {
130 wfSuppressWarnings();
131 $compiled = wfDl( $name );
137 * Get the internationalised name for this DBMS
139 function getReadableName() {
140 return wfMsg( 'config-type-' . $this->getName() );
144 * Get a name=>value map of MW configuration globals that overrides
145 * DefaultSettings.php
147 function getGlobalDefaults() {
152 * Get a name=>value map of internal variables used during installation
154 public function getInternalDefaults() {
155 return $this->internalDefaults
;
159 * Get a variable, taking local defaults into account
161 function getVar( $var, $default = null ) {
162 $defaults = $this->getGlobalDefaults();
163 $internal = $this->getInternalDefaults();
164 if ( isset( $defaults[$var] ) ) {
165 $default = $defaults[$var];
166 } elseif ( isset( $internal[$var] ) ) {
167 $default = $internal[$var];
169 return $this->parent
->getVar( $var, $default );
173 * Convenience alias for $this->parent->setVar()
175 function setVar( $name, $value ) {
176 $this->parent
->setVar( $name, $value );
180 * Get a labelled text box to configure a local variable
182 function getTextBox( $var, $label, $attribs = array() ) {
183 $name = $this->getName() . '_' . $var;
184 $value = $this->getVar( $var );
185 return $this->parent
->getTextBox( array(
188 'attribs' => $attribs,
189 'controlName' => $name,
195 * Get a labelled password box to configure a local variable
196 * Implements password hiding
198 function getPasswordBox( $var, $label, $attribs = array() ) {
199 $name = $this->getName() . '_' . $var;
200 $value = $this->getVar( $var );
201 return $this->parent
->getPasswordBox( array(
204 'attribs' => $attribs,
205 'controlName' => $name,
211 * Get a labelled checkbox to configure a local boolean variable
213 function getCheckBox( $var, $label, $attribs = array() ) {
214 $name = $this->getName() . '_' . $var;
215 $value = $this->getVar( $var );
216 return $this->parent
->getCheckBox( array(
219 'attribs' => $attribs,
220 'controlName' => $name,
226 * Get a set of labelled radio buttons
228 * @param $params Array:
230 * var: The variable to be configured (required)
231 * label: The message name for the label (required)
232 * itemLabelPrefix: The message name prefix for the item labels (required)
233 * values: List of allowed values (required)
234 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
237 function getRadioSet( $params ) {
238 $params['controlName'] = $this->getName() . '_' . $params['var'];
239 $params['value'] = $this->getVar( $params['var'] );
240 return $this->parent
->getRadioSet( $params );
244 * Convenience function to set variables based on form data.
245 * Assumes that variables containing "password" in the name are (potentially
247 * @param $varNames Array
249 function setVarsFromRequest( $varNames ) {
250 return $this->parent
->setVarsFromRequest( $varNames, $this->getName() . '_' );
254 * Determine whether an existing installation of MediaWiki is present in
255 * the configured administrative connection. Returns true if there is
256 * such a wiki, false if the database doesn't exist.
258 * Traditionally, this is done by testing for the existence of either
259 * the revision table or the cur table.
263 function needsUpgrade() {
264 $status = $this->getConnection();
265 if ( !$status->isOK() ) {
268 $conn = $status->value
;
269 if ( !$conn->selectDB( $this->getVar( 'wgDBname' ) ) ) {
272 return $conn->tableExists( 'cur' ) ||
$conn->tableExists( 'revision' );
276 * Get a standard install-user fieldset
278 function getInstallUserBox() {
280 Xml
::openElement( 'fieldset' ) .
281 Xml
::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
282 $this->getTextBox( '_InstallUser', 'config-db-username' ) .
283 $this->getPasswordBox( '_InstallPassword', 'config-db-password' ) .
284 $this->parent
->getHelpBox( 'config-db-install-help' ) .
285 Xml
::closeElement( 'fieldset' );
289 * Submit a standard install user fieldset
291 function submitInstallUserBox() {
292 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
293 return Status
::newGood();
297 * Get a standard web-user fieldset
298 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
299 * Set this to false to show a creation checkbox.
301 function getWebUserBox( $noCreateMsg = false ) {
302 $name = $this->getName();
303 $s = Xml
::openElement( 'fieldset' ) .
304 Xml
::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
306 '_SameAccount', 'config-db-web-account-same',
307 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
309 Xml
::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
310 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
311 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
312 $this->parent
->getHelpBox( 'config-db-web-help' );
313 if ( $noCreateMsg ) {
314 $s .= $this->parent
->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
316 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
318 $s .= Xml
::closeElement( 'div' ) . Xml
::closeElement( 'fieldset' );
323 * Submit the form from getWebUserBox().
326 function submitWebUserBox() {
327 $this->setVarsFromRequest( array( 'wgDBuser', 'wgDBpassword',
328 '_SameAccount', '_CreateDBAccount' ) );
329 if ( $this->getVar( '_SameAccount' ) ) {
330 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
331 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
333 return Status
::newGood();
337 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql
339 public function populateInterwikiTable() {
340 $status = $this->getConnection();
341 if ( !$status->isOK() ) {
344 $this->db
->selectDB( $this->getVar( 'wgDBname' ) );
346 $rows = file( "$IP/maintenance/interwiki.list",
347 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
348 $interwikis = array();
350 return Status
::newFatal( 'config-install-interwiki-sql' );
352 foreach( $rows as $row ) {
353 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
354 if ( $row == "" ) continue;
355 $interwikis[] = array_combine(
356 array( 'iw_prefix', 'iw_url', 'iw_local' ),
360 $this->db
->insert( 'interwiki', $interwikis, __METHOD__
);
361 return Status
::newGood();