* (bug 26733) Wrap initial table creation in transaction
[mediawiki.git] / includes / installer / DatabaseInstaller.php
blob51986cac16b4eb3c028dbe97a001197de3239a36
1 <?php
2 /**
3 * DBMS-specific installation helper.
5 * @file
6 * @ingroup Deployment
7 */
9 /**
10 * Base class for DBMS-specific installation helper classes.
12 * @ingroup Deployment
13 * @since 1.17
15 abstract class DatabaseInstaller {
17 /**
18 * The Installer object.
20 * TODO: naming this parent is confusing, 'installer' would be clearer.
22 * @var CoreInstaller
24 public $parent;
26 /**
27 * The database connection.
29 * @var DatabaseBase
31 public $db;
33 /**
34 * Internal variables for installation.
36 * @var array
38 protected $internalDefaults = array();
40 /**
41 * Array of MW configuration globals this class uses.
43 * @var array
45 protected $globalNames = array();
47 /**
48 * Return the internal name, e.g. 'mysql', or 'sqlite'.
50 public abstract function getName();
52 /**
53 * @return true if the client library is compiled in.
55 public abstract function isCompiled();
57 /**
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();
66 /**
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.
73 * @return Status
75 public abstract function submitConnectForm();
77 /**
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 function getSettingsForm() {
84 return false;
87 /**
88 * Set variables based on the request array, assuming it was submitted via
89 * the form return by getSettingsForm().
91 * @return Status
93 public function submitSettingsForm() {
94 return Status::newGood();
97 /**
98 * Connect to the database using the administrative user/password currently
99 * defined in the session. On success, return the connection, on failure,
101 * This may be called multiple times, so the result should be cached.
103 * @return Status
105 public abstract function getConnection();
108 * Create the database and return a Status object indicating success or
109 * failure.
111 * @return Status
113 public abstract function setupDatabase();
116 * Create database tables from scratch.
118 * @return Status
120 public function createTables() {
121 $status = $this->getConnection();
122 if ( !$status->isOK() ) {
123 return $status;
125 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
127 if( $this->db->tableExists( 'user' ) ) {
128 $status->warning( 'config-install-tables-exist' );
129 return $status;
132 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
133 $this->db->begin( __METHOD__ );
135 $error = $this->db->sourceFile( $this->db->getSchema() );
136 if( $error !== true ) {
137 $this->db->reportQueryError( $error, 0, '', __METHOD__ );
138 $this->db->rollback( __METHOD__ );
139 $status->fatal( 'config-install-tables-failed', $error );
140 } else {
141 $this->db->commit( __METHOD__ );
143 return $status;
147 * Get the DBMS-specific options for LocalSettings.php generation.
149 * @return String
151 public abstract function getLocalSettings();
154 * Perform database upgrades
156 * @return Boolean
158 public function doUpgrade() {
159 # Maintenance scripts like wfGetDB()
160 LBFactory::enableBackend();
162 $ret = true;
163 ob_start( array( $this, 'outputHandler' ) );
164 try {
165 $up = DatabaseUpdater::newForDB( $this->db );
166 $up->doUpdates();
167 } catch ( MWException $e ) {
168 echo "\nAn error occured:\n";
169 echo $e->getText();
170 $ret = false;
172 ob_end_flush();
173 return $ret;
177 * Allow DB installers a chance to make last-minute changes before installation
178 * occurs. This happens before setupDatabase() or createTables() is called, but
179 * long after the constructor. Helpful for things like modifying setup steps :)
181 public function preInstall() {
186 * Allow DB installers a chance to make checks before upgrade.
188 public function preUpgrade() {
193 * Get an array of MW configuration globals that will be configured by this class.
195 public function getGlobalNames() {
196 return $this->globalNames;
200 * Return any table options to be applied to all tables that don't
201 * override them.
203 * @return Array
205 public function getTableOptions() {
206 return array();
210 * Construct and initialise parent.
211 * This is typically only called from Installer::getDBInstaller()
213 public function __construct( $parent ) {
214 $this->parent = $parent;
218 * Convenience function.
219 * Check if a named extension is present.
221 * @see wfDl
223 protected static function checkExtension( $name ) {
224 wfSuppressWarnings();
225 $compiled = wfDl( $name );
226 wfRestoreWarnings();
227 return $compiled;
231 * Get the internationalised name for this DBMS.
233 public function getReadableName() {
234 return wfMsg( 'config-type-' . $this->getName() );
238 * Get a name=>value map of MW configuration globals that overrides.
239 * DefaultSettings.php
241 public function getGlobalDefaults() {
242 return array();
246 * Get a name=>value map of internal variables used during installation.
248 public function getInternalDefaults() {
249 return $this->internalDefaults;
253 * Get a variable, taking local defaults into account.
255 public function getVar( $var, $default = null ) {
256 $defaults = $this->getGlobalDefaults();
257 $internal = $this->getInternalDefaults();
258 if ( isset( $defaults[$var] ) ) {
259 $default = $defaults[$var];
260 } elseif ( isset( $internal[$var] ) ) {
261 $default = $internal[$var];
263 return $this->parent->getVar( $var, $default );
267 * Convenience alias for $this->parent->setVar()
269 public function setVar( $name, $value ) {
270 $this->parent->setVar( $name, $value );
274 * Get a labelled text box to configure a local variable.
276 public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
277 $name = $this->getName() . '_' . $var;
278 $value = $this->getVar( $var );
279 if ( !isset( $attribs ) ) {
280 $attribs = array();
282 return $this->parent->getTextBox( array(
283 'var' => $var,
284 'label' => $label,
285 'attribs' => $attribs,
286 'controlName' => $name,
287 'value' => $value,
288 'help' => $helpData
289 ) );
293 * Get a labelled password box to configure a local variable.
294 * Implements password hiding.
296 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
297 $name = $this->getName() . '_' . $var;
298 $value = $this->getVar( $var );
299 if ( !isset( $attribs ) ) {
300 $attribs = array();
302 return $this->parent->getPasswordBox( array(
303 'var' => $var,
304 'label' => $label,
305 'attribs' => $attribs,
306 'controlName' => $name,
307 'value' => $value,
308 'help' => $helpData
309 ) );
313 * Get a labelled checkbox to configure a local boolean variable.
315 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
316 $name = $this->getName() . '_' . $var;
317 $value = $this->getVar( $var );
318 return $this->parent->getCheckBox( array(
319 'var' => $var,
320 'label' => $label,
321 'attribs' => $attribs,
322 'controlName' => $name,
323 'value' => $value,
324 'help' => $helpData
329 * Get a set of labelled radio buttons.
331 * @param $params Array:
332 * Parameters are:
333 * var: The variable to be configured (required)
334 * label: The message name for the label (required)
335 * itemLabelPrefix: The message name prefix for the item labels (required)
336 * values: List of allowed values (required)
337 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
340 public function getRadioSet( $params ) {
341 $params['controlName'] = $this->getName() . '_' . $params['var'];
342 $params['value'] = $this->getVar( $params['var'] );
343 return $this->parent->getRadioSet( $params );
347 * Convenience function to set variables based on form data.
348 * Assumes that variables containing "password" in the name are (potentially
349 * fake) passwords.
350 * @param $varNames Array
352 public function setVarsFromRequest( $varNames ) {
353 return $this->parent->setVarsFromRequest( $varNames, $this->getName() . '_' );
357 * Determine whether an existing installation of MediaWiki is present in
358 * the configured administrative connection. Returns true if there is
359 * such a wiki, false if the database doesn't exist.
361 * Traditionally, this is done by testing for the existence of either
362 * the revision table or the cur table.
364 * @return Boolean
366 public function needsUpgrade() {
367 $status = $this->getConnection();
368 if ( !$status->isOK() ) {
369 return false;
372 if ( !$this->db->selectDB( $this->getVar( 'wgDBname' ) ) ) {
373 return false;
375 return $this->db->tableExists( 'cur' ) || $this->db->tableExists( 'revision' );
379 * Get a standard install-user fieldset.
381 public function getInstallUserBox() {
382 return
383 Html::openElement( 'fieldset' ) .
384 Html::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
385 $this->getTextBox( '_InstallUser', 'config-db-username', array(), $this->parent->getHelpBox( 'config-db-install-username' ) ) .
386 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array(), $this->parent->getHelpBox( 'config-db-install-password' ) ) .
387 Html::closeElement( 'fieldset' );
391 * Submit a standard install user fieldset.
393 public function submitInstallUserBox() {
394 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
395 return Status::newGood();
399 * Get a standard web-user fieldset
400 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
401 * Set this to false to show a creation checkbox.
403 public function getWebUserBox( $noCreateMsg = false ) {
404 $s = Html::openElement( 'fieldset' ) .
405 Html::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
406 $this->getCheckBox(
407 '_SameAccount', 'config-db-web-account-same',
408 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
410 Html::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
411 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
412 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
413 $this->parent->getHelpBox( 'config-db-web-help' );
414 if ( $noCreateMsg ) {
415 $s .= $this->parent->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
416 } else {
417 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
419 $s .= Html::closeElement( 'div' ) . Html::closeElement( 'fieldset' );
420 return $s;
424 * Submit the form from getWebUserBox().
426 * @return Status
428 public function submitWebUserBox() {
429 $this->setVarsFromRequest(
430 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
433 if ( $this->getVar( '_SameAccount' ) ) {
434 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
435 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
438 return Status::newGood();
442 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
444 public function populateInterwikiTable() {
445 $status = $this->getConnection();
446 if ( !$status->isOK() ) {
447 return $status;
449 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
451 if( $this->db->selectRow( 'interwiki', '*', array(), __METHOD__ ) ) {
452 $status->warning( 'config-install-interwiki-exists' );
453 return $status;
455 global $IP;
456 $rows = file( "$IP/maintenance/interwiki.list",
457 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
458 $interwikis = array();
459 if ( !$rows ) {
460 return Status::newFatal( 'config-install-interwiki-sql' );
462 foreach( $rows as $row ) {
463 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
464 if ( $row == "" ) continue;
465 $row .= "||";
466 $interwikis[] = array_combine(
467 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
468 explode( '|', $row )
471 $this->db->insert( 'interwiki', $interwikis, __METHOD__ );
472 return Status::newGood();
475 public function outputHandler( $string ) {
476 return htmlspecialchars( $string );