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