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