a1f9c9332f129a7d8b3311c12ef956e40c0e0bb8
[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;
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 * Connect to the database using the administrative user/password currently
99 * defined in the session. On success, return the connection, on failure,
100 * return a Status object.
101 *
102 * This may be called multiple times, so the result should be cached.
103 */
104 public abstract function getConnection();
105
106 /**
107 * Create the database and return a Status object indicating success or
108 * failure.
109 *
110 * @return Status
111 */
112 public abstract function setupDatabase();
113
114 /**
115 * Create database tables from scratch.
116 *
117 * @return Status
118 */
119 public function createTables() {
120 $status = $this->getConnection();
121 if ( !$status->isOK() ) {
122 return $status;
123 }
124 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
125
126 if( $this->db->tableExists( 'user' ) ) {
127 $status->warning( 'config-install-tables-exist' );
128 return $status;
129 }
130
131 $error = $this->db->sourceFile( $this->db->getSchema() );
132 if( $error !== true ) {
133 $this->db->reportQueryError( $error, 0, $sql, __METHOD__ );
134 $status->fatal( 'config-install-tables-failed', $error );
135 }
136 return $status;
137 }
138
139 /**
140 * Insert Main Page with default content.
141 *
142 * @return Status
143 */
144 public function createMainpage() {
145 $status = Status::newGood();
146 try {
147 $titleobj = Title::newFromText( wfMsgNoDB( "mainpage" ) );
148 $article = new Article( $titleobj );
149 $article->doEdit( wfMsg( 'mainpagetext' ) . "\n\n" . wfMsgNoTrans( 'mainpagedocfooter' ),
150 '',
151 EDIT_NEW );
152 } catch (MWException $e) {
153 //using raw, because $wgShowExceptionDetails can not be set yet
154 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
155 }
156
157 return $status;
158 }
159
160 /**
161 * Get the DBMS-specific options for LocalSettings.php generation.
162 *
163 * @return String
164 */
165 public abstract function getLocalSettings();
166
167 /**
168 * Perform database upgrades
169 *
170 * @return Boolean
171 */
172 public function doUpgrade() {
173 # Maintenance scripts like wfGetDB()
174 LBFactory::enableBackend();
175
176 $ret = true;
177 ob_start( array( $this, 'outputHandler' ) );
178 try {
179 $up = DatabaseUpdater::newForDB( $this->db );
180 $up->doUpdates();
181 } catch ( MWException $e ) {
182 echo "\nAn error occured:\n";
183 echo $e->getText();
184 $ret = false;
185 }
186 ob_end_flush();
187 return $ret;
188 }
189
190 /**
191 * Allow DB installers a chance to make last-minute changes before installation
192 * occurs. This happens before setupDatabase() or createTables() is called, but
193 * long after the constructor. Helpful for things like modifying setup steps :)
194 */
195 public function preInstall() {
196
197 }
198
199 /**
200 * Allow DB installers a chance to make checks before upgrade.
201 */
202 public function preUpgrade() {
203
204 }
205
206 /**
207 * Get an array of MW configuration globals that will be configured by this class.
208 */
209 public function getGlobalNames() {
210 return $this->globalNames;
211 }
212
213 /**
214 * Return any table options to be applied to all tables that don't
215 * override them.
216 *
217 * @return Array
218 */
219 public function getTableOptions() {
220 return array();
221 }
222
223 /**
224 * Construct and initialise parent.
225 * This is typically only called from Installer::getDBInstaller()
226 */
227 public function __construct( $parent ) {
228 $this->parent = $parent;
229 }
230
231 /**
232 * Convenience function.
233 * Check if a named extension is present.
234 *
235 * @see wfDl
236 */
237 protected static function checkExtension( $name ) {
238 wfSuppressWarnings();
239 $compiled = wfDl( $name );
240 wfRestoreWarnings();
241 return $compiled;
242 }
243
244 /**
245 * Get the internationalised name for this DBMS.
246 */
247 public function getReadableName() {
248 return wfMsg( 'config-type-' . $this->getName() );
249 }
250
251 /**
252 * Get a name=>value map of MW configuration globals that overrides.
253 * DefaultSettings.php
254 */
255 public function getGlobalDefaults() {
256 return array();
257 }
258
259 /**
260 * Get a name=>value map of internal variables used during installation.
261 */
262 public function getInternalDefaults() {
263 return $this->internalDefaults;
264 }
265
266 /**
267 * Get a variable, taking local defaults into account.
268 */
269 public function getVar( $var, $default = null ) {
270 $defaults = $this->getGlobalDefaults();
271 $internal = $this->getInternalDefaults();
272 if ( isset( $defaults[$var] ) ) {
273 $default = $defaults[$var];
274 } elseif ( isset( $internal[$var] ) ) {
275 $default = $internal[$var];
276 }
277 return $this->parent->getVar( $var, $default );
278 }
279
280 /**
281 * Convenience alias for $this->parent->setVar()
282 */
283 public function setVar( $name, $value ) {
284 $this->parent->setVar( $name, $value );
285 }
286
287 /**
288 * Get a labelled text box to configure a local variable.
289 */
290 public function getTextBox( $var, $label, $attribs = array() ) {
291 $name = $this->getName() . '_' . $var;
292 $value = $this->getVar( $var );
293 return $this->parent->getTextBox( array(
294 'var' => $var,
295 'label' => $label,
296 'attribs' => $attribs,
297 'controlName' => $name,
298 'value' => $value
299 ) );
300 }
301
302 /**
303 * Get a labelled password box to configure a local variable.
304 * Implements password hiding.
305 */
306 public function getPasswordBox( $var, $label, $attribs = array() ) {
307 $name = $this->getName() . '_' . $var;
308 $value = $this->getVar( $var );
309 return $this->parent->getPasswordBox( array(
310 'var' => $var,
311 'label' => $label,
312 'attribs' => $attribs,
313 'controlName' => $name,
314 'value' => $value
315 ) );
316 }
317
318 /**
319 * Get a labelled checkbox to configure a local boolean variable.
320 */
321 public function getCheckBox( $var, $label, $attribs = array() ) {
322 $name = $this->getName() . '_' . $var;
323 $value = $this->getVar( $var );
324 return $this->parent->getCheckBox( array(
325 'var' => $var,
326 'label' => $label,
327 'attribs' => $attribs,
328 'controlName' => $name,
329 'value' => $value,
330 ));
331 }
332
333 /**
334 * Get a set of labelled radio buttons.
335 *
336 * @param $params Array:
337 * Parameters are:
338 * var: The variable to be configured (required)
339 * label: The message name for the label (required)
340 * itemLabelPrefix: The message name prefix for the item labels (required)
341 * values: List of allowed values (required)
342 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
343 *
344 */
345 public function getRadioSet( $params ) {
346 $params['controlName'] = $this->getName() . '_' . $params['var'];
347 $params['value'] = $this->getVar( $params['var'] );
348 return $this->parent->getRadioSet( $params );
349 }
350
351 /**
352 * Convenience function to set variables based on form data.
353 * Assumes that variables containing "password" in the name are (potentially
354 * fake) passwords.
355 * @param $varNames Array
356 */
357 public function setVarsFromRequest( $varNames ) {
358 return $this->parent->setVarsFromRequest( $varNames, $this->getName() . '_' );
359 }
360
361 /**
362 * Determine whether an existing installation of MediaWiki is present in
363 * the configured administrative connection. Returns true if there is
364 * such a wiki, false if the database doesn't exist.
365 *
366 * Traditionally, this is done by testing for the existence of either
367 * the revision table or the cur table.
368 *
369 * @return Boolean
370 */
371 public function needsUpgrade() {
372 $status = $this->getConnection();
373 if ( !$status->isOK() ) {
374 return false;
375 }
376
377 if ( !$this->db->selectDB( $this->getVar( 'wgDBname' ) ) ) {
378 return false;
379 }
380 return $this->db->tableExists( 'cur' ) || $this->db->tableExists( 'revision' );
381 }
382
383 /**
384 * Get a standard install-user fieldset.
385 */
386 public function getInstallUserBox() {
387 return
388 Xml::openElement( 'fieldset' ) .
389 Xml::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
390 $this->getTextBox( '_InstallUser', 'config-db-username' ) .
391 $this->getPasswordBox( '_InstallPassword', 'config-db-password' ) .
392 $this->parent->getHelpBox( 'config-db-install-help' ) .
393 Xml::closeElement( 'fieldset' );
394 }
395
396 /**
397 * Submit a standard install user fieldset.
398 */
399 public function submitInstallUserBox() {
400 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
401 return Status::newGood();
402 }
403
404 /**
405 * Get a standard web-user fieldset
406 * @param $noCreateMsg String: Message to display instead of the creation checkbox.
407 * Set this to false to show a creation checkbox.
408 */
409 public function getWebUserBox( $noCreateMsg = false ) {
410 $name = $this->getName();
411 $s = Xml::openElement( 'fieldset' ) .
412 Xml::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
413 $this->getCheckBox(
414 '_SameAccount', 'config-db-web-account-same',
415 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
416 ) .
417 Xml::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
418 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
419 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
420 $this->parent->getHelpBox( 'config-db-web-help' );
421 if ( $noCreateMsg ) {
422 $s .= $this->parent->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
423 } else {
424 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
425 }
426 $s .= Xml::closeElement( 'div' ) . Xml::closeElement( 'fieldset' );
427 return $s;
428 }
429
430 /**
431 * Submit the form from getWebUserBox().
432 *
433 * @return Status
434 */
435 public function submitWebUserBox() {
436 $this->setVarsFromRequest(
437 array( 'wgDBuser', 'wgDBpassword', '_SameAccount', '_CreateDBAccount' )
438 );
439
440 if ( $this->getVar( '_SameAccount' ) ) {
441 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
442 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
443 }
444
445 return Status::newGood();
446 }
447
448 /**
449 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql.
450 */
451 public function populateInterwikiTable() {
452 $status = $this->getConnection();
453 if ( !$status->isOK() ) {
454 return $status;
455 }
456 $this->db->selectDB( $this->getVar( 'wgDBname' ) );
457
458 if( $this->db->selectRow( 'interwiki', '*', array(), __METHOD__ ) ) {
459 $status->warning( 'config-install-interwiki-exists' );
460 return $status;
461 }
462 global $IP;
463 $rows = file( "$IP/maintenance/interwiki.list",
464 FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
465 $interwikis = array();
466 if ( !$rows ) {
467 return Status::newFatal( 'config-install-interwiki-sql' );
468 }
469 foreach( $rows as $row ) {
470 $row = preg_replace( '/^\s*([^#]*?)\s*(#.*)?$/', '\\1', $row ); // strip comments - whee
471 if ( $row == "" ) continue;
472 $row .= "||";
473 $interwikis[] = array_combine(
474 array( 'iw_prefix', 'iw_url', 'iw_local', 'iw_api', 'iw_wikiid' ),
475 explode( '|', $row )
476 );
477 }
478 $this->db->insert( 'interwiki', $interwikis, __METHOD__ );
479 return Status::newGood();
480 }
481
482 public function outputHandler( $string ) {
483 return htmlspecialchars( $string );
484 }
485 }