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