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