* Oracle schema update to latest (tested with phpunit)
[mediawiki.git] / includes / installer / DatabaseInstaller.php
blobb086478bd33fdef850c4204e3f018c6976a0b37b
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 WebInstaller
24 public $parent;
26 /**
27 * The database connection.
29 * @var DatabaseBase
31 public $db = null;
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 * Checks for installation prerequisites other than those checked by isCompiled()
59 * @since 1.19
60 * @return Status
62 public function checkPrerequisites() {
63 return Status::newGood();
66 /**
67 * Get HTML for a web form that configures this database. Configuration
68 * at this time should be the minimum needed to connect and test
69 * whether install or upgrade is required.
71 * If this is called, $this->parent can be assumed to be a WebInstaller.
73 public abstract function getConnectForm();
75 /**
76 * Set variables based on the request array, assuming it was submitted
77 * via the form returned by getConnectForm(). Validate the connection
78 * settings by attempting to connect with them.
80 * If this is called, $this->parent can be assumed to be a WebInstaller.
82 * @return Status
84 public abstract function submitConnectForm();
86 /**
87 * Get HTML for a web form that retrieves settings used for installation.
88 * $this->parent can be assumed to be a WebInstaller.
89 * If the DB type has no settings beyond those already configured with
90 * getConnectForm(), this should return false.
92 public function getSettingsForm() {
93 return false;
96 /**
97 * Set variables based on the request array, assuming it was submitted via
98 * the form return by getSettingsForm().
100 * @return Status
102 public function submitSettingsForm() {
103 return Status::newGood();
107 * Open a connection to the database using the administrative user/password
108 * currently defined in the session, without any caching. Returns a status
109 * object. On success, the status object will contain a Database object in
110 * its value member.
112 * @return Status
114 public abstract function openConnection();
117 * Create the database and return a Status object indicating success or
118 * failure.
120 * @return Status
122 public abstract function setupDatabase();
125 * Connect to the database using the administrative user/password currently
126 * defined in the session. Returns a status object. On success, the status
127 * object will contain a Database object in its value member.
129 * This will return a cached connection if one is available.
131 * @return Status
133 public function getConnection() {
134 if ( $this->db ) {
135 return Status::newGood( $this->db );
138 $status = $this->openConnection();
139 if ( $status->isOK() ) {
140 $this->db = $status->value;
141 // Enable autocommit
142 $this->db->clearFlag( DBO_TRX );
143 $this->db->commit();
145 return $status;
149 * Create database tables from scratch.
151 * @return Status
153 public function createTables() {
154 $status = $this->getConnection();
155 if ( !$status->isOK() ) {
156 return $status;
158 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
160 if( $this->db->tableExists( 'user', __METHOD__ ) ) {
161 $status->warning( 'config-install-tables-exist' );
162 $this->enableLB();
163 return $status;
166 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
167 $this->db->begin( __METHOD__ );
169 $error = $this->db->sourceFile( $this->db->getSchemaPath() );
170 if( $error !== true ) {
171 $this->db->reportQueryError( $error, 0, '', __METHOD__ );
172 $this->db->rollback( __METHOD__ );
173 $status->fatal( 'config-install-tables-failed', $error );
174 } else {
175 $this->db->commit( __METHOD__ );
177 // Resume normal operations
178 if( $status->isOk() ) {
179 $this->enableLB();
181 return $status;
185 * Create the tables for each extension the user enabled
186 * @return Status
188 public function createExtensionTables() {
189 $status = $this->getConnection();
190 if ( !$status->isOK() ) {
191 return $status;
194 // Now run updates to create tables for old extensions
195 DatabaseUpdater::newForDB( $this->db )->doUpdates( array( 'extensions' ) );
197 return $status;
201 * Get the DBMS-specific options for LocalSettings.php generation.
203 * @return String
205 public abstract function getLocalSettings();
208 * Override this to provide DBMS-specific schema variables, to be
209 * substituted into tables.sql and other schema files.
211 public function getSchemaVars() {
212 return array();
216 * Set appropriate schema variables in the current database connection.
218 * This should be called after any request data has been imported, but before
219 * any write operations to the database.
221 public function setupSchemaVars() {
222 $status = $this->getConnection();
223 if ( $status->isOK() ) {
224 $status->value->setSchemaVars( $this->getSchemaVars() );
225 } else {
226 throw new MWException( __METHOD__.': unexpected DB connection error' );
231 * Set up LBFactory so that wfGetDB() etc. works.
232 * We set up a special LBFactory instance which returns the current
233 * installer connection.
235 public function enableLB() {
236 $status = $this->getConnection();
237 if ( !$status->isOK() ) {
238 throw new MWException( __METHOD__.': unexpected DB connection error' );
240 LBFactory::setInstance( new LBFactory_Single( array(
241 'connection' => $status->value ) ) );
245 * Perform database upgrades
247 * @return Boolean
249 public function doUpgrade() {
250 $this->setupSchemaVars();
251 $this->enableLB();
253 $ret = true;
254 ob_start( array( $this, 'outputHandler' ) );
255 try {
256 $up = DatabaseUpdater::newForDB( $this->db );
257 $up->doUpdates();
258 } catch ( MWException $e ) {
259 echo "\nAn error occured:\n";
260 echo $e->getText();
261 $ret = false;
263 ob_end_flush();
264 return $ret;
268 * Allow DB installers a chance to make last-minute changes before installation
269 * occurs. This happens before setupDatabase() or createTables() is called, but
270 * long after the constructor. Helpful for things like modifying setup steps :)
272 public function preInstall() {
277 * Allow DB installers a chance to make checks before upgrade.
279 public function preUpgrade() {
284 * Get an array of MW configuration globals that will be configured by this class.
286 public function getGlobalNames() {
287 return $this->globalNames;
291 * Construct and initialise parent.
292 * This is typically only called from Installer::getDBInstaller()
293 * @param $parent
295 public function __construct( $parent ) {
296 $this->parent = $parent;
300 * Convenience function.
301 * Check if a named extension is present.
303 * @see wfDl
304 * @param $name
305 * @return bool
307 protected static function checkExtension( $name ) {
308 wfSuppressWarnings();
309 $compiled = wfDl( $name );
310 wfRestoreWarnings();
311 return $compiled;
315 * Get the internationalised name for this DBMS.
317 public function getReadableName() {
318 return wfMsg( 'config-type-' . $this->getName() );
322 * Get a name=>value map of MW configuration globals that overrides.
323 * DefaultSettings.php
325 public function getGlobalDefaults() {
326 return array();
330 * Get a name=>value map of internal variables used during installation.
332 public function getInternalDefaults() {
333 return $this->internalDefaults;
337 * Get a variable, taking local defaults into account.
338 * @param $var string
339 * @param $default null
340 * @return mixed
342 public function getVar( $var, $default = null ) {
343 $defaults = $this->getGlobalDefaults();
344 $internal = $this->getInternalDefaults();
345 if ( isset( $defaults[$var] ) ) {
346 $default = $defaults[$var];
347 } elseif ( isset( $internal[$var] ) ) {
348 $default = $internal[$var];
350 return $this->parent->getVar( $var, $default );
354 * Convenience alias for $this->parent->setVar()
355 * @param $name string
356 * @param $value mixed
358 public function setVar( $name, $value ) {
359 $this->parent->setVar( $name, $value );
363 * Get a labelled text box to configure a local variable.
365 * @param $var string
366 * @param $label string
367 * @param $attribs array
368 * @param $helpData string
369 * @return string
371 public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
372 $name = $this->getName() . '_' . $var;
373 $value = $this->getVar( $var );
374 if ( !isset( $attribs ) ) {
375 $attribs = array();
377 return $this->parent->getTextBox( array(
378 'var' => $var,
379 'label' => $label,
380 'attribs' => $attribs,
381 'controlName' => $name,
382 'value' => $value,
383 'help' => $helpData
384 ) );
388 * Get a labelled password box to configure a local variable.
389 * Implements password hiding.
391 * @param $var string
392 * @param $label string
393 * @param $attribs array
394 * @param $helpData string
395 * @return string
397 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
398 $name = $this->getName() . '_' . $var;
399 $value = $this->getVar( $var );
400 if ( !isset( $attribs ) ) {
401 $attribs = array();
403 return $this->parent->getPasswordBox( array(
404 'var' => $var,
405 'label' => $label,
406 'attribs' => $attribs,
407 'controlName' => $name,
408 'value' => $value,
409 'help' => $helpData
410 ) );
414 * Get a labelled checkbox to configure a local boolean variable.
416 * @return string
418 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
419 $name = $this->getName() . '_' . $var;
420 $value = $this->getVar( $var );
421 return $this->parent->getCheckBox( array(
422 'var' => $var,
423 'label' => $label,
424 'attribs' => $attribs,
425 'controlName' => $name,
426 'value' => $value,
427 'help' => $helpData
432 * Get a set of labelled radio buttons.
434 * @param $params Array:
435 * Parameters are:
436 * var: The variable to be configured (required)
437 * label: The message name for the label (required)
438 * itemLabelPrefix: The message name prefix for the item labels (required)
439 * values: List of allowed values (required)
440 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
443 public function getRadioSet( $params ) {
444 $params['controlName'] = $this->getName() . '_' . $params['var'];
445 $params['value'] = $this->getVar( $params['var'] );
446 return $this->parent->getRadioSet( $params );
450 * Convenience function to set variables based on form data.
451 * Assumes that variables containing "password" in the name are (potentially
452 * fake) passwords.
453 * @param $varNames Array
455 public function setVarsFromRequest( $varNames ) {
456 return $this->parent->setVarsFromRequest( $varNames, $this->getName() . '_' );
460 * Determine whether an existing installation of MediaWiki is present in
461 * the configured administrative connection. Returns true if there is
462 * such a wiki, false if the database doesn't exist.
464 * Traditionally, this is done by testing for the existence of either
465 * the revision table or the cur table.
467 * @return Boolean
469 public function needsUpgrade() {
470 $status = $this->getConnection();
471 if ( !$status->isOK() ) {
472 return false;
475 if ( !$this->db->selectDB( $this->getVar( 'wgDBname' ) ) ) {
476 return false;
478 return $this->db->tableExists( 'cur', __METHOD__ ) || $this->db->tableExists( 'revision', __METHOD__ );
482 * Get a standard install-user fieldset.
484 * @return String
486 public function getInstallUserBox() {
487 return
488 Html::openElement( 'fieldset' ) .
489 Html::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
490 $this->getTextBox( '_InstallUser', 'config-db-username', array( 'dir' => 'ltr' ), $this->parent->getHelpBox( 'config-db-install-username' ) ) .
491 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array( 'dir' => 'ltr' ), $this->parent->getHelpBox( 'config-db-install-password' ) ) .
492 Html::closeElement( 'fieldset' );
496 * Submit a standard install user fieldset.
498 public function submitInstallUserBox() {
499 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
500 return Status::newGood();
504 * Get a standard web-user fieldset
505 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
506 * Set this to false to show a creation checkbox.
508 * @return String
510 public function getWebUserBox( $noCreateMsg = false ) {
511 $wrapperStyle = $this->getVar( '_SameAccount' ) ? 'display: none' : '';
512 $s = Html::openElement( 'fieldset' ) .
513 Html::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
514 $this->getCheckBox(
515 '_SameAccount', 'config-db-web-account-same',
516 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
518 Html::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => $wrapperStyle ) ) .
519 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
520 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
521 $this->parent->getHelpBox( 'config-db-web-help' );
522 if ( $noCreateMsg ) {
523 $s .= $this->parent->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
524 } else {
525 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
527 $s .= Html::closeElement( 'div' ) . Html::closeElement( 'fieldset' );
528 return $s;
532 * Submit the form from getWebUserBox().
534 * @return Status
536 public function submitWebUserBox() {
537 $this->setVarsFromRequest(
538 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
541 if ( $this->getVar( '_SameAccount' ) ) {
542 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
543 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
546 if( $this->getVar( '_CreateDBAccount' ) && strval( $this->getVar( 'wgDBpassword' ) ) == '' ) {
547 return Status::newFatal( 'config-db-password-empty', $this->getVar( 'wgDBuser' ) );
550 return Status::newGood();
554 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
556 * @return Status
558 public function populateInterwikiTable() {
559 $status = $this->getConnection();
560 if ( !$status->isOK() ) {
561 return $status;
563 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
565 if( $this->db->selectRow( 'interwiki', '*', array(), __METHOD__ ) ) {
566 $status->warning( 'config-install-interwiki-exists' );
567 return $status;
569 global $IP;
570 wfSuppressWarnings();
571 $rows = file( "$IP/maintenance/interwiki.list",
572 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
573 wfRestoreWarnings();
574 $interwikis = array();
575 if ( !$rows ) {
576 return Status::newFatal( 'config-install-interwiki-list' );
578 foreach( $rows as $row ) {
579 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
580 if ( $row == "" ) continue;
581 $row .= "||";
582 $interwikis[] = array_combine(
583 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
584 explode( '|', $row )
587 $this->db->insert( 'interwiki', $interwikis, __METHOD__ );
588 return Status::newGood();
591 public function outputHandler( $string ) {
592 return htmlspecialchars( $string );