Localisation updates for core and extension messages from translatewiki.net (2011...
[lhc/web/wiklou.git] / includes / installer / DatabaseInstaller.php
1 <?php
2 /**
3 * DBMS-specific installation helper.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Base class for DBMS-specific installation helper classes.
11 *
12 * @ingroup Deployment
13 * @since 1.17
14 */
15 abstract class DatabaseInstaller {
16
17 /**
18 * The Installer object.
19 *
20 * TODO: naming this parent is confusing, 'installer' would be clearer.
21 *
22 * @var WebInstaller
23 */
24 public $parent;
25
26 /**
27 * The database connection.
28 *
29 * @var DatabaseBase
30 */
31 public $db = null;
32
33 /**
34 * Internal variables for installation.
35 *
36 * @var array
37 */
38 protected $internalDefaults = array();
39
40 /**
41 * Array of MW configuration globals this class uses.
42 *
43 * @var array
44 */
45 protected $globalNames = array();
46
47 /**
48 * Return the internal name, e.g. 'mysql', or 'sqlite'.
49 */
50 public abstract function getName();
51
52 /**
53 * @return true if the client library is compiled in.
54 */
55 public abstract function isCompiled();
56
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.
61 *
62 * If this is called, $this->parent can be assumed to be a WebInstaller.
63 */
64 public abstract function getConnectForm();
65
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.
70 *
71 * If this is called, $this->parent can be assumed to be a WebInstaller.
72 *
73 * @return Status
74 */
75 public abstract function submitConnectForm();
76
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.
82 */
83 public function getSettingsForm() {
84 return false;
85 }
86
87 /**
88 * Set variables based on the request array, assuming it was submitted via
89 * the form return by getSettingsForm().
90 *
91 * @return Status
92 */
93 public function submitSettingsForm() {
94 return Status::newGood();
95 }
96
97 /**
98 * Open a connection to the database using the administrative user/password
99 * currently defined in the session, without any caching. Returns a status
100 * object. On success, the status object will contain a Database object in
101 * its value member.
102 *
103 * @return Status
104 */
105 public abstract function openConnection();
106
107 /**
108 * Create the database and return a Status object indicating success or
109 * failure.
110 *
111 * @return Status
112 */
113 public abstract function setupDatabase();
114
115 /**
116 * Connect to the database using the administrative user/password currently
117 * defined in the session. Returns a status object. On success, the status
118 * object will contain a Database object in its value member.
119 *
120 * This will return a cached connection if one is available.
121 *
122 * @return Status
123 */
124 public function getConnection() {
125 if ( $this->db ) {
126 return Status::newGood( $this->db );
127 }
128 $status = $this->openConnection();
129 if ( $status->isOK() ) {
130 $this->db = $status->value;
131 // Enable autocommit
132 $this->db->clearFlag( DBO_TRX );
133 $this->db->commit();
134 }
135 return $status;
136 }
137
138 /**
139 * Create database tables from scratch.
140 *
141 * @return Status
142 */
143 public function createTables() {
144 $status = $this->getConnection();
145 if ( !$status->isOK() ) {
146 return $status;
147 }
148 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
149
150 if( $this->db->tableExists( 'user' ) ) {
151 $status->warning( 'config-install-tables-exist' );
152 $this->enableLB();
153 return $status;
154 }
155
156 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
157 $this->db->begin( __METHOD__ );
158
159 $error = $this->db->sourceFile( $this->db->getSchema() );
160 if( $error !== true ) {
161 $this->db->reportQueryError( $error, 0, '', __METHOD__ );
162 $this->db->rollback( __METHOD__ );
163 $status->fatal( 'config-install-tables-failed', $error );
164 } else {
165 $this->db->commit( __METHOD__ );
166 }
167 // Resume normal operations
168 if( $status->isOk() ) {
169 $this->enableLB();
170 }
171 return $status;
172 }
173
174 /**
175 * Create the tables for each extension the user enabled
176 * @return Status
177 */
178 public function createExtensionTables() {
179 $status = $this->getConnection();
180 if ( !$status->isOK() ) {
181 return $status;
182 }
183 $updater = DatabaseUpdater::newForDB( $this->db );
184 $extensionUpdates = $updater->getNewExtensions();
185
186 // No extensions need tables (or haven't updated to new installer support)
187 if( !count( $extensionUpdates ) ) {
188 return $status;
189 }
190
191 $ourExtensions = array_map( 'strtolower', $this->getVar( '_Extensions' ) );
192
193 foreach( $ourExtensions as $ext ) {
194 if( isset( $extensionUpdates[$ext] ) ) {
195 $this->db->begin( __METHOD__ );
196 $error = $this->db->sourceFile( $extensionUpdates[$ext] );
197 if( $error !== true ) {
198 $this->db->rollback( __METHOD__ );
199 $status->warning( 'config-install-tables-failed', $error );
200 } else {
201 $this->db->commit( __METHOD__ );
202 }
203 }
204 }
205
206 // Now run updates to create tables for old extensions
207 $updater->doUpdates( array( 'extensions' ) );
208
209 return $status;
210 }
211
212 /**
213 * Get the DBMS-specific options for LocalSettings.php generation.
214 *
215 * @return String
216 */
217 public abstract function getLocalSettings();
218
219 /**
220 * Override this to provide DBMS-specific schema variables, to be
221 * substituted into tables.sql and other schema files.
222 */
223 public function getSchemaVars() {
224 return array();
225 }
226
227 /**
228 * Set appropriate schema variables in the current database connection.
229 *
230 * This should be called after any request data has been imported, but before
231 * any write operations to the database.
232 */
233 public function setupSchemaVars() {
234 $status = $this->getConnection();
235 if ( $status->isOK() ) {
236 $status->value->setSchemaVars( $this->getSchemaVars() );
237 } else {
238 throw new MWException( __METHOD__.': unexpected DB connection error' );
239 }
240 }
241
242 /**
243 * Set up LBFactory so that wfGetDB() etc. works.
244 * We set up a special LBFactory instance which returns the current
245 * installer connection.
246 */
247 public function enableLB() {
248 $status = $this->getConnection();
249 if ( !$status->isOK() ) {
250 throw new MWException( __METHOD__.': unexpected DB connection error' );
251 }
252 LBFactory::setInstance( new LBFactory_Single( array(
253 'connection' => $status->value ) ) );
254 }
255
256 /**
257 * Perform database upgrades
258 *
259 * @return Boolean
260 */
261 public function doUpgrade() {
262 $this->setupSchemaVars();
263 $this->enableLB();
264
265 $ret = true;
266 ob_start( array( $this, 'outputHandler' ) );
267 try {
268 $up = DatabaseUpdater::newForDB( $this->db );
269 $up->doUpdates();
270 } catch ( MWException $e ) {
271 echo "\nAn error occured:\n";
272 echo $e->getText();
273 $ret = false;
274 }
275 ob_end_flush();
276 return $ret;
277 }
278
279 /**
280 * Allow DB installers a chance to make last-minute changes before installation
281 * occurs. This happens before setupDatabase() or createTables() is called, but
282 * long after the constructor. Helpful for things like modifying setup steps :)
283 */
284 public function preInstall() {
285
286 }
287
288 /**
289 * Allow DB installers a chance to make checks before upgrade.
290 */
291 public function preUpgrade() {
292
293 }
294
295 /**
296 * Get an array of MW configuration globals that will be configured by this class.
297 */
298 public function getGlobalNames() {
299 return $this->globalNames;
300 }
301
302 /**
303 * Construct and initialise parent.
304 * This is typically only called from Installer::getDBInstaller()
305 */
306 public function __construct( $parent ) {
307 $this->parent = $parent;
308 }
309
310 /**
311 * Convenience function.
312 * Check if a named extension is present.
313 *
314 * @see wfDl
315 */
316 protected static function checkExtension( $name ) {
317 wfSuppressWarnings();
318 $compiled = wfDl( $name );
319 wfRestoreWarnings();
320 return $compiled;
321 }
322
323 /**
324 * Get the internationalised name for this DBMS.
325 */
326 public function getReadableName() {
327 return wfMsg( 'config-type-' . $this->getName() );
328 }
329
330 /**
331 * Get a name=>value map of MW configuration globals that overrides.
332 * DefaultSettings.php
333 */
334 public function getGlobalDefaults() {
335 return array();
336 }
337
338 /**
339 * Get a name=>value map of internal variables used during installation.
340 */
341 public function getInternalDefaults() {
342 return $this->internalDefaults;
343 }
344
345 /**
346 * Get a variable, taking local defaults into account.
347 */
348 public function getVar( $var, $default = null ) {
349 $defaults = $this->getGlobalDefaults();
350 $internal = $this->getInternalDefaults();
351 if ( isset( $defaults[$var] ) ) {
352 $default = $defaults[$var];
353 } elseif ( isset( $internal[$var] ) ) {
354 $default = $internal[$var];
355 }
356 return $this->parent->getVar( $var, $default );
357 }
358
359 /**
360 * Convenience alias for $this->parent->setVar()
361 */
362 public function setVar( $name, $value ) {
363 $this->parent->setVar( $name, $value );
364 }
365
366 /**
367 * Get a labelled text box to configure a local variable.
368 */
369 public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
370 $name = $this->getName() . '_' . $var;
371 $value = $this->getVar( $var );
372 if ( !isset( $attribs ) ) {
373 $attribs = array();
374 }
375 return $this->parent->getTextBox( array(
376 'var' => $var,
377 'label' => $label,
378 'attribs' => $attribs,
379 'controlName' => $name,
380 'value' => $value,
381 'help' => $helpData
382 ) );
383 }
384
385 /**
386 * Get a labelled password box to configure a local variable.
387 * Implements password hiding.
388 */
389 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
390 $name = $this->getName() . '_' . $var;
391 $value = $this->getVar( $var );
392 if ( !isset( $attribs ) ) {
393 $attribs = array();
394 }
395 return $this->parent->getPasswordBox( array(
396 'var' => $var,
397 'label' => $label,
398 'attribs' => $attribs,
399 'controlName' => $name,
400 'value' => $value,
401 'help' => $helpData
402 ) );
403 }
404
405 /**
406 * Get a labelled checkbox to configure a local boolean variable.
407 */
408 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
409 $name = $this->getName() . '_' . $var;
410 $value = $this->getVar( $var );
411 return $this->parent->getCheckBox( array(
412 'var' => $var,
413 'label' => $label,
414 'attribs' => $attribs,
415 'controlName' => $name,
416 'value' => $value,
417 'help' => $helpData
418 ));
419 }
420
421 /**
422 * Get a set of labelled radio buttons.
423 *
424 * @param $params Array:
425 * Parameters are:
426 * var: The variable to be configured (required)
427 * label: The message name for the label (required)
428 * itemLabelPrefix: The message name prefix for the item labels (required)
429 * values: List of allowed values (required)
430 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
431 *
432 */
433 public function getRadioSet( $params ) {
434 $params['controlName'] = $this->getName() . '_' . $params['var'];
435 $params['value'] = $this->getVar( $params['var'] );
436 return $this->parent->getRadioSet( $params );
437 }
438
439 /**
440 * Convenience function to set variables based on form data.
441 * Assumes that variables containing "password" in the name are (potentially
442 * fake) passwords.
443 * @param $varNames Array
444 */
445 public function setVarsFromRequest( $varNames ) {
446 return $this->parent->setVarsFromRequest( $varNames, $this->getName() . '_' );
447 }
448
449 /**
450 * Determine whether an existing installation of MediaWiki is present in
451 * the configured administrative connection. Returns true if there is
452 * such a wiki, false if the database doesn't exist.
453 *
454 * Traditionally, this is done by testing for the existence of either
455 * the revision table or the cur table.
456 *
457 * @return Boolean
458 */
459 public function needsUpgrade() {
460 $status = $this->getConnection();
461 if ( !$status->isOK() ) {
462 return false;
463 }
464
465 if ( !$this->db->selectDB( $this->getVar( 'wgDBname' ) ) ) {
466 return false;
467 }
468 return $this->db->tableExists( 'cur' ) || $this->db->tableExists( 'revision' );
469 }
470
471 /**
472 * Get a standard install-user fieldset.
473 *
474 * @return String
475 */
476 public function getInstallUserBox() {
477 return
478 Html::openElement( 'fieldset' ) .
479 Html::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
480 $this->getTextBox( '_InstallUser', 'config-db-username', array(), $this->parent->getHelpBox( 'config-db-install-username' ) ) .
481 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array(), $this->parent->getHelpBox( 'config-db-install-password' ) ) .
482 Html::closeElement( 'fieldset' );
483 }
484
485 /**
486 * Submit a standard install user fieldset.
487 */
488 public function submitInstallUserBox() {
489 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
490 return Status::newGood();
491 }
492
493 /**
494 * Get a standard web-user fieldset
495 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
496 * Set this to false to show a creation checkbox.
497 *
498 * @return String
499 */
500 public function getWebUserBox( $noCreateMsg = false ) {
501 $wrapperStyle = $this->getVar( '_SameAccount' ) ? 'display: none' : '';
502 $s = Html::openElement( 'fieldset' ) .
503 Html::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
504 $this->getCheckBox(
505 '_SameAccount', 'config-db-web-account-same',
506 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
507 ) .
508 Html::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => $wrapperStyle ) ) .
509 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
510 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
511 $this->parent->getHelpBox( 'config-db-web-help' );
512 if ( $noCreateMsg ) {
513 $s .= $this->parent->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
514 } else {
515 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
516 }
517 $s .= Html::closeElement( 'div' ) . Html::closeElement( 'fieldset' );
518 return $s;
519 }
520
521 /**
522 * Submit the form from getWebUserBox().
523 *
524 * @return Status
525 */
526 public function submitWebUserBox() {
527 $this->setVarsFromRequest(
528 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
529 );
530
531 if ( $this->getVar( '_SameAccount' ) ) {
532 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
533 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
534 }
535
536 if( $this->getVar( '_CreateDBAccount' ) && strval( $this->getVar( 'wgDBpassword' ) ) == '' ) {
537 return Status::newFatal( 'config-db-password-empty', $this->getVar( 'wgDBuser' ) );
538 }
539
540 return Status::newGood();
541 }
542
543 /**
544 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
545 *
546 * @return Status
547 */
548 public function populateInterwikiTable() {
549 $status = $this->getConnection();
550 if ( !$status->isOK() ) {
551 return $status;
552 }
553 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
554
555 if( $this->db->selectRow( 'interwiki', '*', array(), __METHOD__ ) ) {
556 $status->warning( 'config-install-interwiki-exists' );
557 return $status;
558 }
559 global $IP;
560 wfSuppressWarnings();
561 $rows = file( "$IP/maintenance/interwiki.list",
562 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
563 wfRestoreWarnings();
564 $interwikis = array();
565 if ( !$rows ) {
566 return Status::newFatal( 'config-install-interwiki-list' );
567 }
568 foreach( $rows as $row ) {
569 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
570 if ( $row == "" ) continue;
571 $row .= "||";
572 $interwikis[] = array_combine(
573 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
574 explode( '|', $row )
575 );
576 }
577 $this->db->insert( 'interwiki', $interwikis, __METHOD__ );
578 return Status::newGood();
579 }
580
581 public function outputHandler( $string ) {
582 return htmlspecialchars( $string );
583 }
584 }