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