Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / installer / DatabaseInstaller.php
blob13d5d425e0e655162bc3a79087b6501a8f624fee
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 DatabaseBase
46 public $db = null;
48 /**
49 * Internal variables for installation.
51 * @var array
53 protected $internalDefaults = array();
55 /**
56 * Array of MW configuration globals this class uses.
58 * @var array
60 protected $globalNames = array();
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__ );
161 return $status;
165 * Create database tables from scratch.
167 * @return Status
169 public function createTables() {
170 $status = $this->getConnection();
171 if ( !$status->isOK() ) {
172 return $status;
174 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
176 if ( $this->db->tableExists( 'archive', __METHOD__ ) ) {
177 $status->warning( 'config-install-tables-exist' );
178 $this->enableLB();
179 return $status;
182 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
183 $this->db->begin( __METHOD__ );
185 $error = $this->db->sourceFile( $this->db->getSchemaPath() );
186 if ( $error !== true ) {
187 $this->db->reportQueryError( $error, 0, '', __METHOD__ );
188 $this->db->rollback( __METHOD__ );
189 $status->fatal( 'config-install-tables-failed', $error );
190 } else {
191 $this->db->commit( __METHOD__ );
193 // Resume normal operations
194 if ( $status->isOk() ) {
195 $this->enableLB();
197 return $status;
201 * Create the tables for each extension the user enabled
202 * @return Status
204 public function createExtensionTables() {
205 $status = $this->getConnection();
206 if ( !$status->isOK() ) {
207 return $status;
210 // Now run updates to create tables for old extensions
211 DatabaseUpdater::newForDB( $this->db )->doUpdates( array( 'extensions' ) );
213 return $status;
217 * Get the DBMS-specific options for LocalSettings.php generation.
219 * @return String
221 abstract public function getLocalSettings();
224 * Override this to provide DBMS-specific schema variables, to be
225 * substituted into tables.sql and other schema files.
226 * @return array
228 public function getSchemaVars() {
229 return array();
233 * Set appropriate schema variables in the current database connection.
235 * This should be called after any request data has been imported, but before
236 * any write operations to the database.
238 public function setupSchemaVars() {
239 $status = $this->getConnection();
240 if ( $status->isOK() ) {
241 $status->value->setSchemaVars( $this->getSchemaVars() );
242 } else {
243 throw new MWException( __METHOD__ . ': unexpected DB connection error' );
248 * Set up LBFactory so that wfGetDB() etc. works.
249 * We set up a special LBFactory instance which returns the current
250 * installer connection.
252 public function enableLB() {
253 $status = $this->getConnection();
254 if ( !$status->isOK() ) {
255 throw new MWException( __METHOD__ . ': unexpected DB connection error' );
257 LBFactory::setInstance( new LBFactory_Single( array(
258 'connection' => $status->value ) ) );
262 * Perform database upgrades
264 * @return Boolean
266 public function doUpgrade() {
267 $this->setupSchemaVars();
268 $this->enableLB();
270 $ret = true;
271 ob_start( array( $this, 'outputHandler' ) );
272 $up = DatabaseUpdater::newForDB( $this->db );
273 try {
274 $up->doUpdates();
275 } catch ( MWException $e ) {
276 echo "\nAn error occurred:\n";
277 echo $e->getText();
278 $ret = false;
280 $up->purgeCache();
281 ob_end_flush();
282 return $ret;
286 * Allow DB installers a chance to make last-minute changes before installation
287 * occurs. This happens before setupDatabase() or createTables() is called, but
288 * long after the constructor. Helpful for things like modifying setup steps :)
290 public function preInstall() {
295 * Allow DB installers a chance to make checks before upgrade.
297 public function preUpgrade() {
302 * Get an array of MW configuration globals that will be configured by this class.
303 * @return array
305 public function getGlobalNames() {
306 return $this->globalNames;
310 * Construct and initialise parent.
311 * This is typically only called from Installer::getDBInstaller()
312 * @param $parent
314 public function __construct( $parent ) {
315 $this->parent = $parent;
319 * Convenience function.
320 * Check if a named extension is present.
322 * @see wfDl
323 * @param $name
324 * @return bool
326 protected static function checkExtension( $name ) {
327 wfSuppressWarnings();
328 $compiled = wfDl( $name );
329 wfRestoreWarnings();
330 return $compiled;
334 * Get the internationalised name for this DBMS.
335 * @return String
337 public function getReadableName() {
338 // Give grep a chance to find the usages:
339 // config-type-mysql, config-type-postgres, config-type-sqlite, config-type-oracle
340 return wfMessage( 'config-type-' . $this->getName() )->text();
344 * Get a name=>value map of MW configuration globals that overrides.
345 * DefaultSettings.php
346 * @return array
348 public function getGlobalDefaults() {
349 return array();
353 * Get a name=>value map of internal variables used during installation.
354 * @return array
356 public function getInternalDefaults() {
357 return $this->internalDefaults;
361 * Get a variable, taking local defaults into account.
362 * @param $var string
363 * @param $default null
364 * @return mixed
366 public function getVar( $var, $default = null ) {
367 $defaults = $this->getGlobalDefaults();
368 $internal = $this->getInternalDefaults();
369 if ( isset( $defaults[$var] ) ) {
370 $default = $defaults[$var];
371 } elseif ( isset( $internal[$var] ) ) {
372 $default = $internal[$var];
374 return $this->parent->getVar( $var, $default );
378 * Convenience alias for $this->parent->setVar()
379 * @param $name string
380 * @param $value mixed
382 public function setVar( $name, $value ) {
383 $this->parent->setVar( $name, $value );
387 * Get a labelled text box to configure a local variable.
389 * @param $var string
390 * @param $label string
391 * @param $attribs array
392 * @param $helpData string
393 * @return string
395 public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
396 $name = $this->getName() . '_' . $var;
397 $value = $this->getVar( $var );
398 if ( !isset( $attribs ) ) {
399 $attribs = array();
401 return $this->parent->getTextBox( array(
402 'var' => $var,
403 'label' => $label,
404 'attribs' => $attribs,
405 'controlName' => $name,
406 'value' => $value,
407 'help' => $helpData
408 ) );
412 * Get a labelled password box to configure a local variable.
413 * Implements password hiding.
415 * @param $var string
416 * @param $label string
417 * @param $attribs array
418 * @param $helpData string
419 * @return string
421 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
422 $name = $this->getName() . '_' . $var;
423 $value = $this->getVar( $var );
424 if ( !isset( $attribs ) ) {
425 $attribs = array();
427 return $this->parent->getPasswordBox( array(
428 'var' => $var,
429 'label' => $label,
430 'attribs' => $attribs,
431 'controlName' => $name,
432 'value' => $value,
433 'help' => $helpData
434 ) );
438 * Get a labelled checkbox to configure a local boolean variable.
440 * @return string
442 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
443 $name = $this->getName() . '_' . $var;
444 $value = $this->getVar( $var );
445 return $this->parent->getCheckBox( array(
446 'var' => $var,
447 'label' => $label,
448 'attribs' => $attribs,
449 'controlName' => $name,
450 'value' => $value,
451 'help' => $helpData
456 * Get a set of labelled radio buttons.
458 * @param $params Array:
459 * Parameters are:
460 * var: The variable to be configured (required)
461 * label: The message name for the label (required)
462 * itemLabelPrefix: The message name prefix for the item labels (required)
463 * values: List of allowed values (required)
464 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
466 * @return string
468 public function getRadioSet( $params ) {
469 $params['controlName'] = $this->getName() . '_' . $params['var'];
470 $params['value'] = $this->getVar( $params['var'] );
471 return $this->parent->getRadioSet( $params );
475 * Convenience function to set variables based on form data.
476 * Assumes that variables containing "password" in the name are (potentially
477 * fake) passwords.
478 * @param $varNames Array
479 * @return array
481 public function setVarsFromRequest( $varNames ) {
482 return $this->parent->setVarsFromRequest( $varNames, $this->getName() . '_' );
486 * Determine whether an existing installation of MediaWiki is present in
487 * the configured administrative connection. Returns true if there is
488 * such a wiki, false if the database doesn't exist.
490 * Traditionally, this is done by testing for the existence of either
491 * the revision table or the cur table.
493 * @return Boolean
495 public function needsUpgrade() {
496 $status = $this->getConnection();
497 if ( !$status->isOK() ) {
498 return false;
501 if ( !$this->db->selectDB( $this->getVar( 'wgDBname' ) ) ) {
502 return false;
504 return $this->db->tableExists( 'cur', __METHOD__ ) || $this->db->tableExists( 'revision', __METHOD__ );
508 * Get a standard install-user fieldset.
510 * @return String
512 public function getInstallUserBox() {
513 return Html::openElement( 'fieldset' ) .
514 Html::element( 'legend', array(), wfMessage( 'config-db-install-account' )->text() ) .
515 $this->getTextBox( '_InstallUser', 'config-db-username', array( 'dir' => 'ltr' ), $this->parent->getHelpBox( 'config-db-install-username' ) ) .
516 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array( 'dir' => 'ltr' ), $this->parent->getHelpBox( 'config-db-install-password' ) ) .
517 Html::closeElement( 'fieldset' );
521 * Submit a standard install user fieldset.
522 * @return Status
524 public function submitInstallUserBox() {
525 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
526 return Status::newGood();
530 * Get a standard web-user fieldset
531 * @param string $noCreateMsg Message to display instead of the creation checkbox.
532 * Set this to false to show a creation checkbox.
534 * @return String
536 public function getWebUserBox( $noCreateMsg = false ) {
537 $wrapperStyle = $this->getVar( '_SameAccount' ) ? 'display: none' : '';
538 $s = Html::openElement( 'fieldset' ) .
539 Html::element( 'legend', array(), wfMessage( 'config-db-web-account' )->text() ) .
540 $this->getCheckBox(
541 '_SameAccount', 'config-db-web-account-same',
542 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
544 Html::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => $wrapperStyle ) ) .
545 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
546 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
547 $this->parent->getHelpBox( 'config-db-web-help' );
548 if ( $noCreateMsg ) {
549 $s .= $this->parent->getWarningBox( wfMessage( $noCreateMsg )->plain() );
550 } else {
551 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
553 $s .= Html::closeElement( 'div' ) . Html::closeElement( 'fieldset' );
554 return $s;
558 * Submit the form from getWebUserBox().
560 * @return Status
562 public function submitWebUserBox() {
563 $this->setVarsFromRequest(
564 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
567 if ( $this->getVar( '_SameAccount' ) ) {
568 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
569 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
572 if ( $this->getVar( '_CreateDBAccount' ) && strval( $this->getVar( 'wgDBpassword' ) ) == '' ) {
573 return Status::newFatal( 'config-db-password-empty', $this->getVar( 'wgDBuser' ) );
576 return Status::newGood();
580 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
582 * @return Status
584 public function populateInterwikiTable() {
585 $status = $this->getConnection();
586 if ( !$status->isOK() ) {
587 return $status;
589 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
591 if ( $this->db->selectRow( 'interwiki', '*', array(), __METHOD__ ) ) {
592 $status->warning( 'config-install-interwiki-exists' );
593 return $status;
595 global $IP;
596 wfSuppressWarnings();
597 $rows = file( "$IP/maintenance/interwiki.list",
598 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
599 wfRestoreWarnings();
600 $interwikis = array();
601 if ( !$rows ) {
602 return Status::newFatal( 'config-install-interwiki-list' );
604 foreach ( $rows as $row ) {
605 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
606 if ( $row == "" ) {
607 continue;
609 $row .= "||";
610 $interwikis[] = array_combine(
611 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
612 explode( '|', $row )
615 $this->db->insert( 'interwiki', $interwikis, __METHOD__ );
616 return Status::newGood();
619 public function outputHandler( $string ) {
620 return htmlspecialchars( $string );