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