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