Fix for r72553: DatabaseUpdater::execute() doesn't exist...
[mediawiki.git] / includes / installer / DatabaseInstaller.php
blobb63749520e0666e9978de526a932bd5d1851d850
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 Installer
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,
100 * return a Status object.
102 * This may be called multiple times, so the result should be cached.
104 public abstract function getConnection();
107 * Create the database and return a Status object indicating success or
108 * failure.
110 * @return Status
112 public abstract function setupDatabase();
115 * Create database tables from scratch.
117 * @return Status
119 public function createTables() {
120 $status = $this->getConnection();
121 if ( !$status->isOK() ) {
122 return $status;
124 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
126 if( $this->db->tableExists( 'user' ) ) {
127 $status->warning( 'config-install-tables-exist' );
128 return $status;
131 $error = $this->db->sourceFile( $this->db->getSchema() );
132 if( $error !== true ) {
133 $this->db->reportQueryError( $error, 0, $sql, __METHOD__ );
134 $status->fatal( 'config-install-tables-failed', $error );
136 return $status;
140 * Get the DBMS-specific options for LocalSettings.php generation.
142 * @return String
144 public abstract function getLocalSettings();
147 * Perform database upgrades
149 * @return Boolean
151 public function doUpgrade() {
152 # Maintenance scripts like wfGetDB()
153 LBFactory::enableBackend();
155 $ret = true;
156 ob_start( array( $this, 'outputHandler' ) );
157 try {
158 $up = DatabaseUpdater::newForDB( $this->db );
159 $up->doUpdates();
160 } catch ( MWException $e ) {
161 echo "\nAn error occured:\n";
162 echo $e->getText();
163 $ret = false;
165 ob_end_flush();
166 return $ret;
170 * Allow DB installers a chance to make last-minute changes before installation
171 * occurs. This happens before setupDatabase() or createTables() is called, but
172 * long after the constructor. Helpful for things like modifying setup steps :)
174 public function preInstall() {
179 * Allow DB installers a chance to make checks before upgrade.
181 public function preUpgrade() {
186 * Get an array of MW configuration globals that will be configured by this class.
188 public function getGlobalNames() {
189 return $this->globalNames;
193 * Return any table options to be applied to all tables that don't
194 * override them.
196 * @return Array
198 public function getTableOptions() {
199 return array();
202 /**
203 * Construct and initialise parent.
204 * This is typically only called from Installer::getDBInstaller()
206 public function __construct( $parent ) {
207 $this->parent = $parent;
211 * Convenience function.
212 * Check if a named extension is present.
214 * @see wfDl
216 protected static function checkExtension( $name ) {
217 wfSuppressWarnings();
218 $compiled = wfDl( $name );
219 wfRestoreWarnings();
220 return $compiled;
224 * Get the internationalised name for this DBMS.
226 public function getReadableName() {
227 return wfMsg( 'config-type-' . $this->getName() );
231 * Get a name=>value map of MW configuration globals that overrides.
232 * DefaultSettings.php
234 public function getGlobalDefaults() {
235 return array();
239 * Get a name=>value map of internal variables used during installation.
241 public function getInternalDefaults() {
242 return $this->internalDefaults;
246 * Get a variable, taking local defaults into account.
248 public function getVar( $var, $default = null ) {
249 $defaults = $this->getGlobalDefaults();
250 $internal = $this->getInternalDefaults();
251 if ( isset( $defaults[$var] ) ) {
252 $default = $defaults[$var];
253 } elseif ( isset( $internal[$var] ) ) {
254 $default = $internal[$var];
256 return $this->parent->getVar( $var, $default );
260 * Convenience alias for $this->parent->setVar()
262 public function setVar( $name, $value ) {
263 $this->parent->setVar( $name, $value );
267 * Get a labelled text box to configure a local variable.
269 public function getTextBox( $var, $label, $attribs = array() ) {
270 $name = $this->getName() . '_' . $var;
271 $value = $this->getVar( $var );
272 return $this->parent->getTextBox( array(
273 'var' => $var,
274 'label' => $label,
275 'attribs' => $attribs,
276 'controlName' => $name,
277 'value' => $value
278 ) );
282 * Get a labelled password box to configure a local variable.
283 * Implements password hiding.
285 public function getPasswordBox( $var, $label, $attribs = array() ) {
286 $name = $this->getName() . '_' . $var;
287 $value = $this->getVar( $var );
288 return $this->parent->getPasswordBox( array(
289 'var' => $var,
290 'label' => $label,
291 'attribs' => $attribs,
292 'controlName' => $name,
293 'value' => $value
294 ) );
298 * Get a labelled checkbox to configure a local boolean variable.
300 public function getCheckBox( $var, $label, $attribs = array() ) {
301 $name = $this->getName() . '_' . $var;
302 $value = $this->getVar( $var );
303 return $this->parent->getCheckBox( array(
304 'var' => $var,
305 'label' => $label,
306 'attribs' => $attribs,
307 'controlName' => $name,
308 'value' => $value,
313 * Get a set of labelled radio buttons.
315 * @param $params Array:
316 * Parameters are:
317 * var: The variable to be configured (required)
318 * label: The message name for the label (required)
319 * itemLabelPrefix: The message name prefix for the item labels (required)
320 * values: List of allowed values (required)
321 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
324 public function getRadioSet( $params ) {
325 $params['controlName'] = $this->getName() . '_' . $params['var'];
326 $params['value'] = $this->getVar( $params['var'] );
327 return $this->parent->getRadioSet( $params );
331 * Convenience function to set variables based on form data.
332 * Assumes that variables containing "password" in the name are (potentially
333 * fake) passwords.
334 * @param $varNames Array
336 public function setVarsFromRequest( $varNames ) {
337 return $this->parent->setVarsFromRequest( $varNames, $this->getName() . '_' );
341 * Determine whether an existing installation of MediaWiki is present in
342 * the configured administrative connection. Returns true if there is
343 * such a wiki, false if the database doesn't exist.
345 * Traditionally, this is done by testing for the existence of either
346 * the revision table or the cur table.
348 * @return Boolean
350 public function needsUpgrade() {
351 $status = $this->getConnection();
352 if ( !$status->isOK() ) {
353 return false;
356 if ( !$this->db->selectDB( $this->getVar( 'wgDBname' ) ) ) {
357 return false;
359 return $this->db->tableExists( 'cur' ) || $this->db->tableExists( 'revision' );
363 * Get a standard install-user fieldset.
365 public function getInstallUserBox() {
366 return
367 Xml::openElement( 'fieldset' ) .
368 Xml::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
369 $this->getTextBox( '_InstallUser', 'config-db-username' ) .
370 $this->getPasswordBox( '_InstallPassword', 'config-db-password' ) .
371 $this->parent->getHelpBox( 'config-db-install-help' ) .
372 Xml::closeElement( 'fieldset' );
376 * Submit a standard install user fieldset.
378 public function submitInstallUserBox() {
379 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
380 return Status::newGood();
384 * Get a standard web-user fieldset
385 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
386 * Set this to false to show a creation checkbox.
388 public function getWebUserBox( $noCreateMsg = false ) {
389 $name = $this->getName();
390 $s = Xml::openElement( 'fieldset' ) .
391 Xml::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
392 $this->getCheckBox(
393 '_SameAccount', 'config-db-web-account-same',
394 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
396 Xml::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
397 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
398 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
399 $this->parent->getHelpBox( 'config-db-web-help' );
400 if ( $noCreateMsg ) {
401 $s .= $this->parent->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
402 } else {
403 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
405 $s .= Xml::closeElement( 'div' ) . Xml::closeElement( 'fieldset' );
406 return $s;
410 * Submit the form from getWebUserBox().
412 * @return Status
414 public function submitWebUserBox() {
415 $this->setVarsFromRequest(
416 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
419 if ( $this->getVar( '_SameAccount' ) ) {
420 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
421 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
424 return Status::newGood();
428 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
430 public function populateInterwikiTable() {
431 $status = $this->getConnection();
432 if ( !$status->isOK() ) {
433 return $status;
435 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
437 if( $this->db->selectRow( 'interwiki', '*', array(), __METHOD__ ) ) {
438 $status->warning( 'config-install-interwiki-exists' );
439 return $status;
441 global $IP;
442 $rows = file( "$IP/maintenance/interwiki.list",
443 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
444 $interwikis = array();
445 if ( !$rows ) {
446 return Status::newFatal( 'config-install-interwiki-sql' );
448 foreach( $rows as $row ) {
449 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
450 if ( $row == "" ) continue;
451 $row .= "||";
452 $interwikis[] = array_combine(
453 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
454 explode( '|', $row )
457 $this->db->insert( 'interwiki', $interwikis, __METHOD__ );
458 return Status::newGood();
461 public function outputHandler( $string ) {
462 return htmlspecialchars( $string );