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