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