Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / installer / DatabaseInstaller.php
blob50d73de4c8468be0c199f2c231174393a5091123
1 <?php
2 /**
3 * DBMS-specific installation helper.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Deployment
24 /**
25 * Base class for DBMS-specific installation helper classes.
27 * @ingroup Deployment
28 * @since 1.17
30 abstract class DatabaseInstaller {
32 /**
33 * The Installer object.
35 * @todo Naming this parent is confusing, 'installer' would be clearer.
37 * @var WebInstaller
39 public $parent;
41 /**
42 * The database connection.
44 * @var Database
46 public $db = null;
48 /**
49 * Internal variables for installation.
51 * @var array
53 protected $internalDefaults = [];
55 /**
56 * Array of MW configuration globals this class uses.
58 * @var array
60 protected $globalNames = [];
62 /**
63 * Return the internal name, e.g. 'mysql', or 'sqlite'.
65 abstract public function getName();
67 /**
68 * @return bool Returns true if the client library is compiled in.
70 abstract public function isCompiled();
72 /**
73 * Checks for installation prerequisites other than those checked by isCompiled()
74 * @since 1.19
75 * @return Status
77 public function checkPrerequisites() {
78 return Status::newGood();
81 /**
82 * Get HTML for a web form that configures this database. Configuration
83 * at this time should be the minimum needed to connect and test
84 * whether install or upgrade is required.
86 * If this is called, $this->parent can be assumed to be a WebInstaller.
88 abstract public function getConnectForm();
90 /**
91 * Set variables based on the request array, assuming it was submitted
92 * via the form returned by getConnectForm(). Validate the connection
93 * settings by attempting to connect with them.
95 * If this is called, $this->parent can be assumed to be a WebInstaller.
97 * @return Status
99 abstract public function submitConnectForm();
102 * Get HTML for a web form that retrieves settings used for installation.
103 * $this->parent can be assumed to be a WebInstaller.
104 * If the DB type has no settings beyond those already configured with
105 * getConnectForm(), this should return false.
106 * @return bool
108 public function getSettingsForm() {
109 return false;
113 * Set variables based on the request array, assuming it was submitted via
114 * the form return by getSettingsForm().
116 * @return Status
118 public function submitSettingsForm() {
119 return Status::newGood();
123 * Open a connection to the database using the administrative user/password
124 * currently defined in the session, without any caching. Returns a status
125 * object. On success, the status object will contain a Database object in
126 * its value member.
128 * @return Status
130 abstract public function openConnection();
133 * Create the database and return a Status object indicating success or
134 * failure.
136 * @return Status
138 abstract public function setupDatabase();
141 * Connect to the database using the administrative user/password currently
142 * defined in the session. Returns a status object. On success, the status
143 * object will contain a Database object in its value member.
145 * This will return a cached connection if one is available.
147 * @return Status
149 public function getConnection() {
150 if ( $this->db ) {
151 return Status::newGood( $this->db );
154 $status = $this->openConnection();
155 if ( $status->isOK() ) {
156 $this->db = $status->value;
157 // Enable autocommit
158 $this->db->clearFlag( DBO_TRX );
159 $this->db->commit( __METHOD__ );
162 return $status;
166 * Apply a SQL source file to the database as part of running an installation step.
168 * @param string $sourceFileMethod
169 * @param string $stepName
170 * @param bool $archiveTableMustNotExist
171 * @return Status
173 private function stepApplySourceFile(
174 $sourceFileMethod,
175 $stepName,
176 $archiveTableMustNotExist = false
178 $status = $this->getConnection();
179 if ( !$status->isOK() ) {
180 return $status;
182 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
184 if ( $archiveTableMustNotExist && $this->db->tableExists( 'archive', __METHOD__ ) ) {
185 $status->warning( "config-$stepName-tables-exist" );
186 $this->enableLB();
188 return $status;
191 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
192 $this->db->begin( __METHOD__ );
194 $error = $this->db->sourceFile(
195 call_user_func( [ $this, $sourceFileMethod ], $this->db )
197 if ( $error !== true ) {
198 $this->db->reportQueryError( $error, 0, '', __METHOD__ );
199 $this->db->rollback( __METHOD__ );
200 $status->fatal( "config-$stepName-tables-failed", $error );
201 } else {
202 $this->db->commit( __METHOD__ );
204 // Resume normal operations
205 if ( $status->isOK() ) {
206 $this->enableLB();
209 return $status;
213 * Create database tables from scratch.
215 * @return Status
217 public function createTables() {
218 return $this->stepApplySourceFile( 'getSchemaPath', 'install', true );
222 * Insert update keys into table to prevent running unneded updates.
224 * @return Status
226 public function insertUpdateKeys() {
227 return $this->stepApplySourceFile( 'getUpdateKeysPath', 'updates', false );
231 * Return a path to the DBMS-specific SQL file if it exists,
232 * otherwise default SQL file
234 * @param IDatabase $db
235 * @param string $filename
236 * @return string
238 private function getSqlFilePath( $db, $filename ) {
239 global $IP;
241 $dbmsSpecificFilePath = "$IP/maintenance/" . $db->getType() . "/$filename";
242 if ( file_exists( $dbmsSpecificFilePath ) ) {
243 return $dbmsSpecificFilePath;
244 } else {
245 return "$IP/maintenance/$filename";
250 * Return a path to the DBMS-specific schema file,
251 * otherwise default to tables.sql
253 * @param IDatabase $db
254 * @return string
256 public function getSchemaPath( $db ) {
257 return $this->getSqlFilePath( $db, 'tables.sql' );
261 * Return a path to the DBMS-specific update key file,
262 * otherwise default to update-keys.sql
264 * @param IDatabase $db
265 * @return string
267 public function getUpdateKeysPath( $db ) {
268 return $this->getSqlFilePath( $db, 'update-keys.sql' );
272 * Create the tables for each extension the user enabled
273 * @return Status
275 public function createExtensionTables() {
276 $status = $this->getConnection();
277 if ( !$status->isOK() ) {
278 return $status;
281 // Now run updates to create tables for old extensions
282 DatabaseUpdater::newForDB( $this->db )->doUpdates( [ 'extensions' ] );
284 return $status;
288 * Get the DBMS-specific options for LocalSettings.php generation.
290 * @return string
292 abstract public function getLocalSettings();
295 * Override this to provide DBMS-specific schema variables, to be
296 * substituted into tables.sql and other schema files.
297 * @return array
299 public function getSchemaVars() {
300 return [];
304 * Set appropriate schema variables in the current database connection.
306 * This should be called after any request data has been imported, but before
307 * any write operations to the database.
309 public function setupSchemaVars() {
310 $status = $this->getConnection();
311 if ( $status->isOK() ) {
312 $status->value->setSchemaVars( $this->getSchemaVars() );
313 } else {
314 $msg = __METHOD__ . ': unexpected error while establishing'
315 . ' a database connection with message: '
316 . $status->getMessage()->plain();
317 throw new MWException( $msg );
322 * Set up LBFactory so that wfGetDB() etc. works.
323 * We set up a special LBFactory instance which returns the current
324 * installer connection.
326 public function enableLB() {
327 $status = $this->getConnection();
328 if ( !$status->isOK() ) {
329 throw new MWException( __METHOD__ . ': unexpected DB connection error' );
332 \MediaWiki\MediaWikiServices::resetGlobalInstance();
333 $services = \MediaWiki\MediaWikiServices::getInstance();
335 $connection = $status->value;
336 $services->redefineService( 'DBLoadBalancerFactory', function() use ( $connection ) {
337 return LBFactorySingle::newFromConnection( $connection );
338 } );
342 * Perform database upgrades
344 * @return bool
346 public function doUpgrade() {
347 $this->setupSchemaVars();
348 $this->enableLB();
350 $ret = true;
351 ob_start( [ $this, 'outputHandler' ] );
352 $up = DatabaseUpdater::newForDB( $this->db );
353 try {
354 $up->doUpdates();
355 } catch ( MWException $e ) {
356 echo "\nAn error occurred:\n";
357 echo $e->getText();
358 $ret = false;
359 } catch ( Exception $e ) {
360 echo "\nAn error occurred:\n";
361 echo $e->getMessage();
362 $ret = false;
364 $up->purgeCache();
365 ob_end_flush();
367 return $ret;
371 * Allow DB installers a chance to make last-minute changes before installation
372 * occurs. This happens before setupDatabase() or createTables() is called, but
373 * long after the constructor. Helpful for things like modifying setup steps :)
375 public function preInstall() {
379 * Allow DB installers a chance to make checks before upgrade.
381 public function preUpgrade() {
385 * Get an array of MW configuration globals that will be configured by this class.
386 * @return array
388 public function getGlobalNames() {
389 return $this->globalNames;
393 * Construct and initialise parent.
394 * This is typically only called from Installer::getDBInstaller()
395 * @param WebInstaller $parent
397 public function __construct( $parent ) {
398 $this->parent = $parent;
402 * Convenience function.
403 * Check if a named extension is present.
405 * @param string $name
406 * @return bool
408 protected static function checkExtension( $name ) {
409 return extension_loaded( $name );
413 * Get the internationalised name for this DBMS.
414 * @return string
416 public function getReadableName() {
417 // Messages: config-type-mysql, config-type-postgres, config-type-sqlite,
418 // config-type-oracle
419 return wfMessage( 'config-type-' . $this->getName() )->text();
423 * Get a name=>value map of MW configuration globals for the default values.
424 * @return array
426 public function getGlobalDefaults() {
427 $defaults = [];
428 foreach ( $this->getGlobalNames() as $var ) {
429 if ( isset( $GLOBALS[$var] ) ) {
430 $defaults[$var] = $GLOBALS[$var];
433 return $defaults;
437 * Get a name=>value map of internal variables used during installation.
438 * @return array
440 public function getInternalDefaults() {
441 return $this->internalDefaults;
445 * Get a variable, taking local defaults into account.
446 * @param string $var
447 * @param mixed|null $default
448 * @return mixed
450 public function getVar( $var, $default = null ) {
451 $defaults = $this->getGlobalDefaults();
452 $internal = $this->getInternalDefaults();
453 if ( isset( $defaults[$var] ) ) {
454 $default = $defaults[$var];
455 } elseif ( isset( $internal[$var] ) ) {
456 $default = $internal[$var];
459 return $this->parent->getVar( $var, $default );
463 * Convenience alias for $this->parent->setVar()
464 * @param string $name
465 * @param mixed $value
467 public function setVar( $name, $value ) {
468 $this->parent->setVar( $name, $value );
472 * Get a labelled text box to configure a local variable.
474 * @param string $var
475 * @param string $label
476 * @param array $attribs
477 * @param string $helpData
478 * @return string
480 public function getTextBox( $var, $label, $attribs = [], $helpData = "" ) {
481 $name = $this->getName() . '_' . $var;
482 $value = $this->getVar( $var );
483 if ( !isset( $attribs ) ) {
484 $attribs = [];
487 return $this->parent->getTextBox( [
488 'var' => $var,
489 'label' => $label,
490 'attribs' => $attribs,
491 'controlName' => $name,
492 'value' => $value,
493 'help' => $helpData
494 ] );
498 * Get a labelled password box to configure a local variable.
499 * Implements password hiding.
501 * @param string $var
502 * @param string $label
503 * @param array $attribs
504 * @param string $helpData
505 * @return string
507 public function getPasswordBox( $var, $label, $attribs = [], $helpData = "" ) {
508 $name = $this->getName() . '_' . $var;
509 $value = $this->getVar( $var );
510 if ( !isset( $attribs ) ) {
511 $attribs = [];
514 return $this->parent->getPasswordBox( [
515 'var' => $var,
516 'label' => $label,
517 'attribs' => $attribs,
518 'controlName' => $name,
519 'value' => $value,
520 'help' => $helpData
521 ] );
525 * Get a labelled checkbox to configure a local boolean variable.
527 * @param string $var
528 * @param string $label
529 * @param array $attribs Optional.
530 * @param string $helpData Optional.
531 * @return string
533 public function getCheckBox( $var, $label, $attribs = [], $helpData = "" ) {
534 $name = $this->getName() . '_' . $var;
535 $value = $this->getVar( $var );
537 return $this->parent->getCheckBox( [
538 'var' => $var,
539 'label' => $label,
540 'attribs' => $attribs,
541 'controlName' => $name,
542 'value' => $value,
543 'help' => $helpData
544 ] );
548 * Get a set of labelled radio buttons.
550 * @param array $params Parameters are:
551 * var: The variable to be configured (required)
552 * label: The message name for the label (required)
553 * itemLabelPrefix: The message name prefix for the item labels (required)
554 * values: List of allowed values (required)
555 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
557 * @return string
559 public function getRadioSet( $params ) {
560 $params['controlName'] = $this->getName() . '_' . $params['var'];
561 $params['value'] = $this->getVar( $params['var'] );
563 return $this->parent->getRadioSet( $params );
567 * Convenience function to set variables based on form data.
568 * Assumes that variables containing "password" in the name are (potentially
569 * fake) passwords.
570 * @param array $varNames
571 * @return array
573 public function setVarsFromRequest( $varNames ) {
574 return $this->parent->setVarsFromRequest( $varNames, $this->getName() . '_' );
578 * Determine whether an existing installation of MediaWiki is present in
579 * the configured administrative connection. Returns true if there is
580 * such a wiki, false if the database doesn't exist.
582 * Traditionally, this is done by testing for the existence of either
583 * the revision table or the cur table.
585 * @return bool
587 public function needsUpgrade() {
588 $status = $this->getConnection();
589 if ( !$status->isOK() ) {
590 return false;
593 if ( !$this->db->selectDB( $this->getVar( 'wgDBname' ) ) ) {
594 return false;
597 return $this->db->tableExists( 'cur', __METHOD__ ) ||
598 $this->db->tableExists( 'revision', __METHOD__ );
602 * Get a standard install-user fieldset.
604 * @return string
606 public function getInstallUserBox() {
607 return Html::openElement( 'fieldset' ) .
608 Html::element( 'legend', [], wfMessage( 'config-db-install-account' )->text() ) .
609 $this->getTextBox(
610 '_InstallUser',
611 'config-db-username',
612 [ 'dir' => 'ltr' ],
613 $this->parent->getHelpBox( 'config-db-install-username' )
615 $this->getPasswordBox(
616 '_InstallPassword',
617 'config-db-password',
618 [ 'dir' => 'ltr' ],
619 $this->parent->getHelpBox( 'config-db-install-password' )
621 Html::closeElement( 'fieldset' );
625 * Submit a standard install user fieldset.
626 * @return Status
628 public function submitInstallUserBox() {
629 $this->setVarsFromRequest( [ '_InstallUser', '_InstallPassword' ] );
631 return Status::newGood();
635 * Get a standard web-user fieldset
636 * @param string|bool $noCreateMsg Message to display instead of the creation checkbox.
637 * Set this to false to show a creation checkbox (default).
639 * @return string
641 public function getWebUserBox( $noCreateMsg = false ) {
642 $wrapperStyle = $this->getVar( '_SameAccount' ) ? 'display: none' : '';
643 $s = Html::openElement( 'fieldset' ) .
644 Html::element( 'legend', [], wfMessage( 'config-db-web-account' )->text() ) .
645 $this->getCheckBox(
646 '_SameAccount', 'config-db-web-account-same',
647 [ 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' ]
649 Html::openElement( 'div', [ 'id' => 'dbOtherAccount', 'style' => $wrapperStyle ] ) .
650 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
651 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
652 $this->parent->getHelpBox( 'config-db-web-help' );
653 if ( $noCreateMsg ) {
654 $s .= $this->parent->getWarningBox( wfMessage( $noCreateMsg )->plain() );
655 } else {
656 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
658 $s .= Html::closeElement( 'div' ) . Html::closeElement( 'fieldset' );
660 return $s;
664 * Submit the form from getWebUserBox().
666 * @return Status
668 public function submitWebUserBox() {
669 $this->setVarsFromRequest(
670 [ 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' ]
673 if ( $this->getVar( '_SameAccount' ) ) {
674 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
675 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
678 if ( $this->getVar( '_CreateDBAccount' ) && strval( $this->getVar( 'wgDBpassword' ) ) == '' ) {
679 return Status::newFatal( 'config-db-password-empty', $this->getVar( 'wgDBuser' ) );
682 return Status::newGood();
686 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
688 * @return Status
690 public function populateInterwikiTable() {
691 $status = $this->getConnection();
692 if ( !$status->isOK() ) {
693 return $status;
695 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
697 if ( $this->db->selectRow( 'interwiki', '*', [], __METHOD__ ) ) {
698 $status->warning( 'config-install-interwiki-exists' );
700 return $status;
702 global $IP;
703 MediaWiki\suppressWarnings();
704 $rows = file( "$IP/maintenance/interwiki.list",
705 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
706 MediaWiki\restoreWarnings();
707 $interwikis = [];
708 if ( !$rows ) {
709 return Status::newFatal( 'config-install-interwiki-list' );
711 foreach ( $rows as $row ) {
712 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
713 if ( $row == "" ) {
714 continue;
716 $row .= "|";
717 $interwikis[] = array_combine(
718 [ 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ],
719 explode( '|', $row )
722 $this->db->insert( 'interwiki', $interwikis, __METHOD__ );
724 return Status::newGood();
727 public function outputHandler( $string ) {
728 return htmlspecialchars( $string );