(bug 26857) Fatal error during installation enabling 'Vector' extension (or any exten...
[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 Installer
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 DatabaseBase
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 return $status;
153 }
154
155 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
156 $this->db->begin( __METHOD__ );
157
158 $error = $this->db->sourceFile( $this->db->getSchema() );
159 if( $error !== true ) {
160 $this->db->reportQueryError( $error, 0, '', __METHOD__ );
161 $this->db->rollback( __METHOD__ );
162 $status->fatal( 'config-install-tables-failed', $error );
163 } else {
164 $this->db->commit( __METHOD__ );
165 }
166 // Resume normal operations
167 if( $status->isOk() ) {
168 $this->enableLB();
169 }
170 return $status;
171 }
172
173 /**
174 * Create the tables for each extension the user enabled
175 * @return Status
176 */
177 public function createExtensionTables() {
178 $status = $this->getConnection();
179 if ( !$status->isOK() ) {
180 return $status;
181 }
182 $this->db;
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 return $status;
206 }
207
208 /**
209 * Create the tables for each extension the user enabled
210 * @return Status
211 */
212 public function createExtensionTables() {
213 $status = $this->getConnection();
214 if ( !$status->isOK() ) {
215 return $status;
216 }
217 $this->db;
218 $updater = DatabaseUpdater::newForDB( $this->db );
219 $extensionUpdates = $updater->getNewExtensions();
220
221 // No extensions need tables (or haven't updated to new installer support)
222 if( !count( $extensionUpdates ) ) {
223 return $status;
224 }
225
226 $ourExtensions = array_map( 'strtolower', $this->getVar( '_Extensions' ) );
227
228 foreach( $ourExtensions as $ext ) {
229 if( isset( $extensionUpdates[$ext] ) ) {
230 $this->db->begin( __METHOD__ );
231 $error = $this->db->sourceFile( $extensionUpdates[$ext] );
232 if( $error !== true ) {
233 $this->db->rollback( __METHOD__ );
234 $status->warning( 'config-install-tables-failed', $error );
235 } else {
236 $this->db->commit( __METHOD__ );
237 }
238 }
239 }
240 return $status;
241 }
242
243 /**
244 * Create the tables for each extension the user enabled
245 * @return Status
246 */
247 public function createExtensionTables() {
248 $status = $this->getConnection();
249 if ( !$status->isOK() ) {
250 return $status;
251 }
252 $this->db;
253 $updater = DatabaseUpdater::newForDB( $this->db );
254 $extensionUpdates = $updater->getNewExtensions();
255
256 // No extensions need tables (or haven't updated to new installer support)
257 if( !count( $extensionUpdates ) ) {
258 return $status;
259 }
260
261 $ourExtensions = array_map( 'strtolower', $this->getVar( '_Extensions' ) );
262
263 foreach( $ourExtensions as $ext ) {
264 if( isset( $extensionUpdates[$ext] ) ) {
265 $this->db->begin( __METHOD__ );
266 $error = $this->db->sourceFile( $extensionUpdates[$ext] );
267 if( $error !== true ) {
268 $this->db->rollback( __METHOD__ );
269 $status->warning( 'config-install-tables-failed', $error );
270 } else {
271 $this->db->commit( __METHOD__ );
272 }
273 }
274 }
275 return $status;
276 }
277
278 /**
279 * Get the DBMS-specific options for LocalSettings.php generation.
280 *
281 * @return String
282 */
283 public abstract function getLocalSettings();
284
285 /**
286 * Override this to provide DBMS-specific schema variables, to be
287 * substituted into tables.sql and other schema files.
288 */
289 public function getSchemaVars() {
290 return array();
291 }
292
293 /**
294 * Set appropriate schema variables in the current database connection.
295 *
296 * This should be called after any request data has been imported, but before
297 * any write operations to the database.
298 */
299 public function setupSchemaVars() {
300 $status = $this->getConnection();
301 if ( $status->isOK() ) {
302 $status->value->setSchemaVars( $this->getSchemaVars() );
303 } else {
304 throw new MWException( __METHOD__.': unexpected DB connection error' );
305 }
306 }
307
308 /**
309 * Set up LBFactory so that wfGetDB() etc. works.
310 * We set up a special LBFactory instance which returns the current
311 * installer connection.
312 */
313 public function enableLB() {
314 $status = $this->getConnection();
315 if ( !$status->isOK() ) {
316 throw new MWException( __METHOD__.': unexpected DB connection error' );
317 }
318 LBFactory::setInstance( new LBFactory_Single( array(
319 'connection' => $status->value ) ) );
320 }
321
322 /**
323 * Perform database upgrades
324 *
325 * @return Boolean
326 */
327 public function doUpgrade() {
328 $this->setupSchemaVars();
329 $this->enableLB();
330
331 $ret = true;
332 ob_start( array( $this, 'outputHandler' ) );
333 try {
334 $up = DatabaseUpdater::newForDB( $this->db );
335 $up->doUpdates();
336 } catch ( MWException $e ) {
337 echo "\nAn error occured:\n";
338 echo $e->getText();
339 $ret = false;
340 }
341 ob_end_flush();
342 return $ret;
343 }
344
345 /**
346 * Allow DB installers a chance to make last-minute changes before installation
347 * occurs. This happens before setupDatabase() or createTables() is called, but
348 * long after the constructor. Helpful for things like modifying setup steps :)
349 */
350 public function preInstall() {
351
352 }
353
354 /**
355 * Allow DB installers a chance to make checks before upgrade.
356 */
357 public function preUpgrade() {
358
359 }
360
361 /**
362 * Get an array of MW configuration globals that will be configured by this class.
363 */
364 public function getGlobalNames() {
365 return $this->globalNames;
366 }
367
368 /**
369 * Construct and initialise parent.
370 * This is typically only called from Installer::getDBInstaller()
371 */
372 public function __construct( $parent ) {
373 $this->parent = $parent;
374 }
375
376 /**
377 * Convenience function.
378 * Check if a named extension is present.
379 *
380 * @see wfDl
381 */
382 protected static function checkExtension( $name ) {
383 wfSuppressWarnings();
384 $compiled = wfDl( $name );
385 wfRestoreWarnings();
386 return $compiled;
387 }
388
389 /**
390 * Get the internationalised name for this DBMS.
391 */
392 public function getReadableName() {
393 return wfMsg( 'config-type-' . $this->getName() );
394 }
395
396 /**
397 * Get a name=>value map of MW configuration globals that overrides.
398 * DefaultSettings.php
399 */
400 public function getGlobalDefaults() {
401 return array();
402 }
403
404 /**
405 * Get a name=>value map of internal variables used during installation.
406 */
407 public function getInternalDefaults() {
408 return $this->internalDefaults;
409 }
410
411 /**
412 * Get a variable, taking local defaults into account.
413 */
414 public function getVar( $var, $default = null ) {
415 $defaults = $this->getGlobalDefaults();
416 $internal = $this->getInternalDefaults();
417 if ( isset( $defaults[$var] ) ) {
418 $default = $defaults[$var];
419 } elseif ( isset( $internal[$var] ) ) {
420 $default = $internal[$var];
421 }
422 return $this->parent->getVar( $var, $default );
423 }
424
425 /**
426 * Convenience alias for $this->parent->setVar()
427 */
428 public function setVar( $name, $value ) {
429 $this->parent->setVar( $name, $value );
430 }
431
432 /**
433 * Get a labelled text box to configure a local variable.
434 */
435 public function getTextBox( $var, $label, $attribs = array(), $helpData = "" ) {
436 $name = $this->getName() . '_' . $var;
437 $value = $this->getVar( $var );
438 if ( !isset( $attribs ) ) {
439 $attribs = array();
440 }
441 return $this->parent->getTextBox( array(
442 'var' => $var,
443 'label' => $label,
444 'attribs' => $attribs,
445 'controlName' => $name,
446 'value' => $value,
447 'help' => $helpData
448 ) );
449 }
450
451 /**
452 * Get a labelled password box to configure a local variable.
453 * Implements password hiding.
454 */
455 public function getPasswordBox( $var, $label, $attribs = array(), $helpData = "" ) {
456 $name = $this->getName() . '_' . $var;
457 $value = $this->getVar( $var );
458 if ( !isset( $attribs ) ) {
459 $attribs = array();
460 }
461 return $this->parent->getPasswordBox( array(
462 'var' => $var,
463 'label' => $label,
464 'attribs' => $attribs,
465 'controlName' => $name,
466 'value' => $value,
467 'help' => $helpData
468 ) );
469 }
470
471 /**
472 * Get a labelled checkbox to configure a local boolean variable.
473 */
474 public function getCheckBox( $var, $label, $attribs = array(), $helpData = "" ) {
475 $name = $this->getName() . '_' . $var;
476 $value = $this->getVar( $var );
477 return $this->parent->getCheckBox( array(
478 'var' => $var,
479 'label' => $label,
480 'attribs' => $attribs,
481 'controlName' => $name,
482 'value' => $value,
483 'help' => $helpData
484 ));
485 }
486
487 /**
488 * Get a set of labelled radio buttons.
489 *
490 * @param $params Array:
491 * Parameters are:
492 * var: The variable to be configured (required)
493 * label: The message name for the label (required)
494 * itemLabelPrefix: The message name prefix for the item labels (required)
495 * values: List of allowed values (required)
496 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
497 *
498 */
499 public function getRadioSet( $params ) {
500 $params['controlName'] = $this->getName() . '_' . $params['var'];
501 $params['value'] = $this->getVar( $params['var'] );
502 return $this->parent->getRadioSet( $params );
503 }
504
505 /**
506 * Convenience function to set variables based on form data.
507 * Assumes that variables containing "password" in the name are (potentially
508 * fake) passwords.
509 * @param $varNames Array
510 */
511 public function setVarsFromRequest( $varNames ) {
512 return $this->parent->setVarsFromRequest( $varNames, $this->getName() . '_' );
513 }
514
515 /**
516 * Determine whether an existing installation of MediaWiki is present in
517 * the configured administrative connection. Returns true if there is
518 * such a wiki, false if the database doesn't exist.
519 *
520 * Traditionally, this is done by testing for the existence of either
521 * the revision table or the cur table.
522 *
523 * @return Boolean
524 */
525 public function needsUpgrade() {
526 $status = $this->getConnection();
527 if ( !$status->isOK() ) {
528 return false;
529 }
530
531 if ( !$this->db->selectDB( $this->getVar( 'wgDBname' ) ) ) {
532 return false;
533 }
534 return $this->db->tableExists( 'cur' ) || $this->db->tableExists( 'revision' );
535 }
536
537 /**
538 * Get a standard install-user fieldset.
539 */
540 public function getInstallUserBox() {
541 return
542 Html::openElement( 'fieldset' ) .
543 Html::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
544 $this->getTextBox( '_InstallUser', 'config-db-username', array(), $this->parent->getHelpBox( 'config-db-install-username' ) ) .
545 $this->getPasswordBox( '_InstallPassword', 'config-db-password', array(), $this->parent->getHelpBox( 'config-db-install-password' ) ) .
546 Html::closeElement( 'fieldset' );
547 }
548
549 /**
550 * Submit a standard install user fieldset.
551 */
552 public function submitInstallUserBox() {
553 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
554 return Status::newGood();
555 }
556
557 /**
558 * Get a standard web-user fieldset
559 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
560 * Set this to false to show a creation checkbox.
561 */
562 public function getWebUserBox( $noCreateMsg = false ) {
563 $s = Html::openElement( 'fieldset' ) .
564 Html::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
565 $this->getCheckBox(
566 '_SameAccount', 'config-db-web-account-same',
567 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
568 ) .
569 Html::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
570 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
571 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
572 $this->parent->getHelpBox( 'config-db-web-help' );
573 if ( $noCreateMsg ) {
574 $s .= $this->parent->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
575 } else {
576 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
577 }
578 $s .= Html::closeElement( 'div' ) . Html::closeElement( 'fieldset' );
579 return $s;
580 }
581
582 /**
583 * Submit the form from getWebUserBox().
584 *
585 * @return Status
586 */
587 public function submitWebUserBox() {
588 $this->setVarsFromRequest(
589 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
590 );
591
592 if ( $this->getVar( '_SameAccount' ) ) {
593 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
594 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
595 }
596
597 return Status::newGood();
598 }
599
600 /**
601 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
602 */
603 public function populateInterwikiTable() {
604 $status = $this->getConnection();
605 if ( !$status->isOK() ) {
606 return $status;
607 }
608 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
609
610 if( $this->db->selectRow( 'interwiki', '*', array(), __METHOD__ ) ) {
611 $status->warning( 'config-install-interwiki-exists' );
612 return $status;
613 }
614 global $IP;
615 $rows = file( "$IP/maintenance/interwiki.list",
616 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
617 $interwikis = array();
618 if ( !$rows ) {
619 return Status::newFatal( 'config-install-interwiki-sql' );
620 }
621 foreach( $rows as $row ) {
622 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
623 if ( $row == "" ) continue;
624 $row .= "||";
625 $interwikis[] = array_combine(
626 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
627 explode( '|', $row )
628 );
629 }
630 $this->db->insert( 'interwiki', $interwikis, __METHOD__ );
631 return Status::newGood();
632 }
633
634 public function outputHandler( $string ) {
635 return htmlspecialchars( $string );
636 }
637 }