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