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