Merge new-installer branch to trunk
[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 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 function checkExtension( $name ) {
130 wfSuppressWarnings();
131 $compiled = extension_loaded( $name )
132 || ( $this->parent->haveDl() && dl( $name . '.' . PHP_SHLIB_SUFFIX ) );
133 wfRestoreWarnings();
134 return $compiled;
135 }
136
137 /**
138 * Get the internationalised name for this DBMS
139 */
140 function getReadableName() {
141 return wfMsg( 'config-type-' . $this->getName() );
142 }
143
144 /**
145 * Get a name=>value map of MW configuration globals that overrides
146 * DefaultSettings.php
147 */
148 function getGlobalDefaults() {
149 return array();
150 }
151
152 /**
153 * Get a name=>value map of internal variables used during installation
154 */
155 public function getInternalDefaults() {
156 return $this->internalDefaults;
157 }
158
159 /**
160 * Get a variable, taking local defaults into account
161 */
162 function getVar( $var, $default = null ) {
163 $defaults = $this->getGlobalDefaults();
164 $internal = $this->getInternalDefaults();
165 if ( isset( $defaults[$var] ) ) {
166 $default = $defaults[$var];
167 } elseif ( isset( $internal[$var] ) ) {
168 $default = $internal[$var];
169 }
170 return $this->parent->getVar( $var, $default );
171 }
172
173 /**
174 * Convenience alias for $this->parent->setVar()
175 */
176 function setVar( $name, $value ) {
177 $this->parent->setVar( $name, $value );
178 }
179
180 /**
181 * Get a labelled text box to configure a local variable
182 */
183 function getTextBox( $var, $label, $attribs = array() ) {
184 $name = $this->getName() . '_' . $var;
185 $value = $this->getVar( $var );
186 return $this->parent->getTextBox( array(
187 'var' => $var,
188 'label' => $label,
189 'attribs' => $attribs,
190 'controlName' => $name,
191 'value' => $value
192 ) );
193 }
194
195 /**
196 * Get a labelled password box to configure a local variable
197 * Implements password hiding
198 */
199 function getPasswordBox( $var, $label, $attribs = array() ) {
200 $name = $this->getName() . '_' . $var;
201 $value = $this->getVar( $var );
202 return $this->parent->getPasswordBox( array(
203 'var' => $var,
204 'label' => $label,
205 'attribs' => $attribs,
206 'controlName' => $name,
207 'value' => $value
208 ) );
209 }
210
211 /**
212 * Get a labelled checkbox to configure a local boolean variable
213 */
214 function getCheckBox( $var, $label, $attribs = array() ) {
215 $name = $this->getName() . '_' . $var;
216 $value = $this->getVar( $var );
217 return $this->parent->getCheckBox( array(
218 'var' => $var,
219 'label' => $label,
220 'attribs' => $attribs,
221 'controlName' => $name,
222 'value' => $value,
223 ));
224 }
225
226 /**
227 * Get a set of labelled radio buttons
228 *
229 * @param array $params
230 * Parameters are:
231 * var: The variable to be configured (required)
232 * label: The message name for the label (required)
233 * itemLabelPrefix: The message name prefix for the item labels (required)
234 * values: List of allowed values (required)
235 * itemAttribs Array of attribute arrays, outer key is the value name (optional)
236 *
237 */
238 function getRadioSet( $params ) {
239 $params['controlName'] = $this->getName() . '_' . $params['var'];
240 $params['value'] = $this->getVar( $params['var'] );
241 return $this->parent->getRadioSet( $params );
242 }
243
244 /**
245 * Convenience function to set variables based on form data.
246 * Assumes that variables containing "password" in the name are (potentially
247 * fake) passwords.
248 * @param array $varNames
249 */
250 function setVarsFromRequest( $varNames ) {
251 return $this->parent->setVarsFromRequest( $varNames, $this->getName() . '_' );
252 }
253
254 /**
255 * Determine whether an existing installation of MediaWiki is present in
256 * the configured administrative connection. Returns true if there is
257 * such a wiki, false if the database doesn't exist.
258 *
259 * Traditionally, this is done by testing for the existence of either
260 * the revision table or the cur table.
261 *
262 * @return boolean
263 */
264 function needsUpgrade() {
265 $status = $this->getConnection();
266 if ( !$status->isOK() ) {
267 return false;
268 }
269 $conn = $status->value;
270 if ( !$conn->selectDB( $this->getVar( 'wgDBname' ) ) ) {
271 return false;
272 }
273 return $conn->tableExists( 'cur' ) || $conn->tableExists( 'revision' );
274 }
275
276 /**
277 * Get a standard install-user fieldset
278 */
279 function getInstallUserBox() {
280 return
281 Xml::openElement( 'fieldset' ) .
282 Xml::element( 'legend', array(), wfMsg( 'config-db-install-account' ) ) .
283 $this->getTextBox( '_InstallUser', 'config-db-username' ) .
284 $this->getPasswordBox( '_InstallPassword', 'config-db-password' ) .
285 $this->parent->getHelpBox( 'config-db-install-help' ) .
286 Xml::closeElement( 'fieldset' );
287 }
288
289 /**
290 * Submit a standard install user fieldset
291 */
292 function submitInstallUserBox() {
293 $this->setVarsFromRequest( array( '_InstallUser', '_InstallPassword' ) );
294 return Status::newGood();
295 }
296
297 /**
298 * Get a standard web-user fieldset
299 * @param string $noCreateMsg Message to display instead of the creation checkbox.
300 * Set this to false to show a creation checkbox.
301 */
302 function getWebUserBox( $noCreateMsg = false ) {
303 $name = $this->getName();
304 $s = Xml::openElement( 'fieldset' ) .
305 Xml::element( 'legend', array(), wfMsg( 'config-db-web-account' ) ) .
306 $this->getCheckBox(
307 '_SameAccount', 'config-db-web-account-same',
308 array( 'class' => 'hideShowRadio', 'rel' => 'dbOtherAccount' )
309 ) .
310 Xml::openElement( 'div', array( 'id' => 'dbOtherAccount', 'style' => 'display: none;' ) ) .
311 $this->getTextBox( 'wgDBuser', 'config-db-username' ) .
312 $this->getPasswordBox( 'wgDBpassword', 'config-db-password' ) .
313 $this->parent->getHelpBox( 'config-db-web-help' );
314 if ( $noCreateMsg ) {
315 $s .= $this->parent->getWarningBox( wfMsgNoTrans( $noCreateMsg ) );
316 } else {
317 $s .= $this->getCheckBox( '_CreateDBAccount', 'config-db-web-create' );
318 }
319 $s .= Xml::closeElement( 'div' ) . Xml::closeElement( 'fieldset' );
320 return $s;
321 }
322
323 /**
324 * Submit the form from getWebUserBox().
325 * @return Status
326 */
327 function submitWebUserBox() {
328 $this->setVarsFromRequest( array( 'wgDBuser', 'wgDBpassword',
329 '_SameAccount', '_CreateDBAccount' ) );
330 if ( $this->getVar( '_SameAccount' ) ) {
331 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
332 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
333 }
334 return Status::newGood();
335 }
336
337 /**
338 * Common function for databases that don't understand the MySQLish syntax of interwiki.sql
339 */
340 protected function populateInterwikiTable( $db ) {
341 global $IP;
342 // Originally from DatabasePostgres
343 $f = fopen( "$IP/maintenance/interwiki.sql", 'r' );
344 if ( $f == false ) {
345 return Status::newFatal( 'config-install-interwiki-sql' );
346 }
347 $table = $db->tableName( 'interwiki' );
348 $sql = "INSERT INTO $table(iw_prefix,iw_url,iw_local) VALUES ";
349 while ( !feof( $f ) ) {
350 $line = fgets( $f, 1024 );
351 $matches = array();
352 if ( !preg_match( '/^\s*(\(.+?),(\d)\)/', $line, $matches ) ) continue;
353 $db->query( "$sql $matches[1],$matches[2])" );
354 }
355 return Status::newGood();
356 }
357
358 }
359