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