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