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