Removal of unused globals
[lhc/web/wiklou.git] / includes / installer / CoreInstaller.php
1 <?php
2
3 /**
4 * Base core installer class.
5 * Handles everything that is independent of user interface.
6 *
7 * @since 1.17
8 */
9 abstract class CoreInstaller extends Installer {
10
11 /**
12 * MediaWiki configuration globals that will eventually be passed through
13 * to LocalSettings.php. The names only are given here, the defaults
14 * typically come from DefaultSettings.php.
15 *
16 * @var array
17 */
18 protected $defaultVarNames = array(
19 'wgSitename',
20 'wgPasswordSender',
21 'wgLanguageCode',
22 'wgRightsIcon',
23 'wgRightsText',
24 'wgRightsUrl',
25 'wgMainCacheType',
26 'wgEnableEmail',
27 'wgEnableUserEmail',
28 'wgEnotifUserTalk',
29 'wgEnotifWatchlist',
30 'wgEmailAuthentication',
31 'wgDBtype',
32 'wgDiff3',
33 'wgImageMagickConvertCommand',
34 'IP',
35 'wgScriptPath',
36 'wgScriptExtension',
37 'wgMetaNamespace',
38 'wgDeletedDirectory',
39 'wgEnableUploads',
40 'wgLogo',
41 'wgShellLocale',
42 'wgSecretKey',
43 'wgUseInstantCommons',
44 );
45
46 /**
47 * Variables that are stored alongside globals, and are used for any
48 * configuration of the installation process aside from the MediaWiki
49 * configuration. Map of names to defaults.
50 *
51 * @var array
52 */
53 protected $internalDefaults = array(
54 '_UserLang' => 'en',
55 '_Environment' => false,
56 '_CompiledDBs' => array(),
57 '_SafeMode' => false,
58 '_RaiseMemory' => false,
59 '_UpgradeDone' => false,
60 '_InstallDone' => false,
61 '_Caches' => array(),
62 '_InstallUser' => 'root',
63 '_InstallPassword' => '',
64 '_SameAccount' => true,
65 '_CreateDBAccount' => false,
66 '_NamespaceType' => 'site-name',
67 '_AdminName' => '', // will be set later, when the user selects language
68 '_AdminPassword' => '',
69 '_AdminPassword2' => '',
70 '_AdminEmail' => '',
71 '_Subscribe' => false,
72 '_SkipOptional' => 'continue',
73 '_RightsProfile' => 'wiki',
74 '_LicenseCode' => 'none',
75 '_CCDone' => false,
76 '_Extensions' => array(),
77 '_MemCachedServers' => '',
78 '_ExternalHTTP' => false,
79 );
80
81 /**
82 * Steps for installation.
83 *
84 * @var array
85 */
86 protected $installSteps = array(
87 'database',
88 'tables',
89 'interwiki',
90 'secretkey',
91 'sysop',
92 );
93
94 /**
95 * Known object cache types and the functions used to test for their existence.
96 *
97 * @var array
98 */
99 protected $objectCaches = array(
100 'xcache' => 'xcache_get',
101 'apc' => 'apc_fetch',
102 'eaccel' => 'eaccelerator_get',
103 'wincache' => 'wincache_ucache_get'
104 );
105
106 /**
107 * User rights profiles.
108 *
109 * @var array
110 */
111 public $rightsProfiles = array(
112 'wiki' => array(),
113 'no-anon' => array(
114 '*' => array( 'edit' => false )
115 ),
116 'fishbowl' => array(
117 '*' => array(
118 'createaccount' => false,
119 'edit' => false,
120 ),
121 ),
122 'private' => array(
123 '*' => array(
124 'createaccount' => false,
125 'edit' => false,
126 'read' => false,
127 ),
128 ),
129 );
130
131 /**
132 * License types.
133 *
134 * @var array
135 */
136 public $licenses = array(
137 'none' => array(
138 'url' => '',
139 'icon' => '',
140 'text' => ''
141 ),
142 'cc-by-sa' => array(
143 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
144 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
145 ),
146 'cc-by-nc-sa' => array(
147 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/',
148 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png',
149 ),
150 'pd' => array(
151 'url' => 'http://creativecommons.org/licenses/publicdomain/',
152 'icon' => '{$wgStylePath}/common/images/public-domain.png',
153 ),
154 'gfdl-old' => array(
155 'url' => 'http://www.gnu.org/licenses/old-licenses/fdl-1.2.html',
156 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
157 ),
158 'gfdl-current' => array(
159 'url' => 'http://www.gnu.org/copyleft/fdl.html',
160 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
161 ),
162 'cc-choose' => array(
163 // details will be filled in by the selector
164 'url' => '',
165 'icon' => '',
166 'text' => '',
167 ),
168 );
169
170 /**
171 * Constructor, always call this from child classes.
172 */
173 public function __construct() {
174 parent::__construct();
175
176 global $wgExtensionMessagesFiles, $wgUser, $wgHooks;
177
178 // Load the installer's i18n file.
179 $wgExtensionMessagesFiles['MediawikiInstaller'] =
180 './includes/installer/Installer.i18n.php';
181
182 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
183 $wgUser = User::newFromId( 0 );
184
185 // Set our custom <doclink> hook.
186 $wgHooks['ParserFirstCallInit'][] = array( $this, 'registerDocLink' );
187
188 $this->settings = $this->internalDefaults;
189
190 foreach ( $this->defaultVarNames as $var ) {
191 $this->settings[$var] = $GLOBALS[$var];
192 }
193
194 foreach ( $this->dbTypes as $type ) {
195 $installer = $this->getDBInstaller( $type );
196
197 if ( !$installer->isCompiled() ) {
198 continue;
199 }
200
201 $defaults = $installer->getGlobalDefaults();
202
203 foreach ( $installer->getGlobalNames() as $var ) {
204 if ( isset( $defaults[$var] ) ) {
205 $this->settings[$var] = $defaults[$var];
206 } else {
207 $this->settings[$var] = $GLOBALS[$var];
208 }
209 }
210 }
211
212 $this->parserTitle = Title::newFromText( 'Installer' );
213 $this->parserOptions = new ParserOptions;
214 $this->parserOptions->setEditSection( false );
215 }
216
217 /**
218 * Register tag hook below.
219 *
220 * @param $parser Parser
221 */
222 public function registerDocLink( Parser &$parser ) {
223 $parser->setHook( 'doclink', array( $this, 'docLink' ) );
224 return true;
225 }
226
227 /**
228 * Extension tag hook for a documentation link.
229 */
230 public function docLink( $linkText, $attribs, $parser ) {
231 $url = $this->getDocUrl( $attribs['href'] );
232 return '<a href="' . htmlspecialchars( $url ) . '">' .
233 htmlspecialchars( $linkText ) .
234 '</a>';
235 }
236
237 /**
238 * Overridden by WebInstaller to provide lastPage parameters.
239 */
240 protected function getDocUrl( $page ) {
241 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $attribs['href'] );
242 }
243
244 /**
245 * Finds extensions that follow the format /extensions/Name/Name.php,
246 * and returns an array containing the value for 'Name' for each found extension.
247 *
248 * @return array
249 */
250 public function findExtensions() {
251 if( $this->getVar( 'IP' ) === null ) {
252 return false;
253 }
254
255 $exts = array();
256 $dir = $this->getVar( 'IP' ) . '/extensions';
257 $dh = opendir( $dir );
258
259 while ( ( $file = readdir( $dh ) ) !== false ) {
260 if( file_exists( "$dir/$file/$file.php" ) ) {
261 $exts[] = $file;
262 }
263 }
264
265 $this->setVar( '_Extensions', $exts );
266
267 return $exts;
268 }
269
270 /**
271 * Installs the auto-detected extensions.
272 *
273 * TODO: this only requires them?
274 *
275 * @return Status
276 */
277 public function installExtensions() {
278 $exts = $this->getVar( '_Extensions' );
279 $path = $this->getVar( 'IP' ) . '/extensions';
280
281 foreach( $exts as $e ) {
282 require( "$path/$e/$e.php" );
283 }
284
285 return Status::newGood();
286 }
287
288 public function getInstallSteps() {
289 if( $this->getVar( '_UpgradeDone' ) ) {
290 $this->installSteps = array( 'localsettings' );
291 }
292
293 if( count( $this->getVar( '_Extensions' ) ) ) {
294 array_unshift( $this->installSteps, 'extensions' );
295 }
296
297 return $this->installSteps;
298 }
299
300 /**
301 * Actually perform the installation.
302 *
303 * @param Array $startCB A callback array for the beginning of each step
304 * @param Array $endCB A callback array for the end of each step
305 *
306 * @return Array of Status objects
307 */
308 public function performInstallation( $startCB, $endCB ) {
309 $installResults = array();
310 $installer = $this->getDBInstaller();
311
312 foreach( $this->getInstallSteps() as $stepObj ) {
313 $step = is_array( $stepObj ) ? $stepObj['name'] : $stepObj;
314 call_user_func_array( $startCB, array( $step ) );
315 $status = null;
316
317 # Call our working function
318 if ( is_array( $stepObj ) ) {
319 # A custom callaback
320 $callback = $stepObj['callback'];
321 $status = call_user_func_array( $callback, array( $installer ) );
322 } else {
323 # Boring implicitly named callback
324 $func = 'install' . ucfirst( $step );
325 $status = $this->{$func}( $installer );
326 }
327
328 call_user_func_array( $endCB, array( $step, $status ) );
329 $installResults[$step] = $status;
330
331 // If we've hit some sort of fatal, we need to bail.
332 // Callback already had a chance to do output above.
333 if( !$status->isOk() ) {
334 break;
335 }
336
337 }
338
339 if( $status->isOk() ) {
340 $this->setVar( '_InstallDone', true );
341 }
342
343 return $installResults;
344 }
345
346 /**
347 * TODO: document
348 *
349 * @return Status
350 */
351 public function installSecretKey() {
352 if ( wfIsWindows() ) {
353 $file = null;
354 } else {
355 wfSuppressWarnings();
356 $file = fopen( "/dev/urandom", "r" );
357 wfRestoreWarnings();
358 }
359
360 $status = Status::newGood();
361
362 if ( $file ) {
363 $secretKey = bin2hex( fread( $file, 32 ) );
364 fclose( $file );
365 } else {
366 $secretKey = '';
367
368 for ( $i=0; $i<8; $i++ ) {
369 $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
370 }
371
372 $status->warning( 'config-insecure-secretkey' );
373 }
374
375 $this->setVar( 'wgSecretKey', $secretKey );
376
377 return $status;
378 }
379
380 /**
381 * TODO: document
382 *
383 * @return Status
384 */
385 public function installSysop() {
386 $name = $this->getVar( '_AdminName' );
387 $user = User::newFromName( $name );
388
389 if ( !$user ) {
390 // We should've validated this earlier anyway!
391 return Status::newFatal( 'config-admin-error-user', $name );
392 }
393
394 if ( $user->idForName() == 0 ) {
395 $user->addToDatabase();
396
397 try {
398 $user->setPassword( $this->getVar( '_AdminPassword' ) );
399 } catch( PasswordError $pwe ) {
400 return Status::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
401 }
402
403 $user->addGroup( 'sysop' );
404 $user->addGroup( 'bureaucrat' );
405 $user->saveSettings();
406 }
407
408 return Status::newGood();
409 }
410
411 /**
412 * Override the necessary bits of the config to run an installation.
413 */
414 public static function overrideConfig() {
415 define( 'MW_NO_SESSION', 1 );
416
417 // Don't access the database
418 $GLOBALS['wgUseDatabaseMessages'] = false;
419 // Debug-friendly
420 $GLOBALS['wgShowExceptionDetails'] = true;
421 // Don't break forms
422 $GLOBALS['wgExternalLinkTarget'] = '_blank';
423
424 // Extended debugging. Maybe disable before release?
425 $GLOBALS['wgShowSQLErrors'] = true;
426 $GLOBALS['wgShowDBErrorBacktrace'] = true;
427 }
428
429 /**
430 * Add an installation step following the given step.
431 *
432 * @param $findStep String the step to find. Use NULL to put the step at the beginning.
433 * @param $callback array
434 */
435 public function addInstallStepFollowing( $findStep, $callback ) {
436 $where = 0;
437
438 if( $findStep !== null ) {
439 $where = array_search( $findStep, $this->installSteps );
440 }
441
442 array_splice( $this->installSteps, $where, 0, $callback );
443 }
444
445 }