2024418fe28070ec08ae6e0796c0c32ff5b99ef7
[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 );
87
88 /**
89 * Steps for installation.
90 *
91 * @var array
92 */
93 protected $installSteps = array(
94 'database',
95 'tables',
96 'interwiki',
97 'secretkey',
98 'sysop',
99 'mainpage',
100 );
101
102 /**
103 * Known object cache types and the functions used to test for their existence.
104 *
105 * @var array
106 */
107 protected $objectCaches = array(
108 'xcache' => 'xcache_get',
109 'apc' => 'apc_fetch',
110 'eaccel' => 'eaccelerator_get',
111 'wincache' => 'wincache_ucache_get'
112 );
113
114 /**
115 * User rights profiles.
116 *
117 * @var array
118 */
119 public $rightsProfiles = array(
120 'wiki' => array(),
121 'no-anon' => array(
122 '*' => array( 'edit' => false )
123 ),
124 'fishbowl' => array(
125 '*' => array(
126 'createaccount' => false,
127 'edit' => false,
128 ),
129 ),
130 'private' => array(
131 '*' => array(
132 'createaccount' => false,
133 'edit' => false,
134 'read' => false,
135 ),
136 ),
137 );
138
139 /**
140 * License types.
141 *
142 * @var array
143 */
144 public $licenses = array(
145 'cc-by-sa' => array(
146 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
147 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
148 ),
149 'cc-by-nc-sa' => array(
150 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/',
151 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png',
152 ),
153 'pd' => array(
154 'url' => 'http://creativecommons.org/licenses/publicdomain/',
155 'icon' => '{$wgStylePath}/common/images/public-domain.png',
156 ),
157 'gfdl-old' => array(
158 'url' => 'http://www.gnu.org/licenses/old-licenses/fdl-1.2.html',
159 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
160 ),
161 'gfdl-current' => array(
162 'url' => 'http://www.gnu.org/copyleft/fdl.html',
163 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
164 ),
165 'none' => array(
166 'url' => '',
167 'icon' => '',
168 'text' => ''
169 ),
170 'cc-choose' => array(
171 // Details will be filled in by the selector.
172 'url' => '',
173 'icon' => '',
174 'text' => '',
175 ),
176 );
177
178 /**
179 * TODO: document
180 *
181 * @param $status Status
182 */
183 public abstract function showStatusMessage( Status $status );
184
185
186 /**
187 * Constructor, always call this from child classes.
188 */
189 public function __construct() {
190 parent::__construct();
191
192 global $wgExtensionMessagesFiles, $wgUser, $wgHooks;
193
194 // Load the installer's i18n file.
195 $wgExtensionMessagesFiles['MediawikiInstaller'] =
196 dirname( __FILE__ ) . '/Installer.i18n.php';
197
198 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
199 $wgUser = User::newFromId( 0 );
200
201 // Set our custom <doclink> hook.
202 $wgHooks['ParserFirstCallInit'][] = array( $this, 'registerDocLink' );
203
204 $this->settings = $this->internalDefaults;
205
206 foreach ( $this->defaultVarNames as $var ) {
207 $this->settings[$var] = $GLOBALS[$var];
208 }
209
210 foreach ( self::getDBTypes() as $type ) {
211 $installer = $this->getDBInstaller( $type );
212
213 if ( !$installer->isCompiled() ) {
214 continue;
215 }
216
217 $defaults = $installer->getGlobalDefaults();
218
219 foreach ( $installer->getGlobalNames() as $var ) {
220 if ( isset( $defaults[$var] ) ) {
221 $this->settings[$var] = $defaults[$var];
222 } else {
223 $this->settings[$var] = $GLOBALS[$var];
224 }
225 }
226 }
227
228 $this->parserTitle = Title::newFromText( 'Installer' );
229 $this->parserOptions = new ParserOptions;
230 $this->parserOptions->setEditSection( false );
231 }
232
233 /**
234 * Register tag hook below.
235 *
236 * @todo Move this to WebInstaller with the two things below?
237 *
238 * @param $parser Parser
239 */
240 public function registerDocLink( Parser &$parser ) {
241 $parser->setHook( 'doclink', array( $this, 'docLink' ) );
242 return true;
243 }
244
245 /**
246 * Extension tag hook for a documentation link.
247 */
248 public function docLink( $linkText, $attribs, $parser ) {
249 $url = $this->getDocUrl( $attribs['href'] );
250 return '<a href="' . htmlspecialchars( $url ) . '">' .
251 htmlspecialchars( $linkText ) .
252 '</a>';
253 }
254
255 /**
256 * Overridden by WebInstaller to provide lastPage parameters.
257 */
258 protected function getDocUrl( $page ) {
259 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $attribs['href'] );
260 }
261
262 /**
263 * Finds extensions that follow the format /extensions/Name/Name.php,
264 * and returns an array containing the value for 'Name' for each found extension.
265 *
266 * @return array
267 */
268 public function findExtensions() {
269 if( $this->getVar( 'IP' ) === null ) {
270 return false;
271 }
272
273 $exts = array();
274 $dir = $this->getVar( 'IP' ) . '/extensions';
275 $dh = opendir( $dir );
276
277 while ( ( $file = readdir( $dh ) ) !== false ) {
278 if( file_exists( "$dir/$file/$file.php" ) ) {
279 $exts[] = $file;
280 }
281 }
282
283 return $exts;
284 }
285
286 /**
287 * Installs the auto-detected extensions.
288 *
289 * @TODO: this only requires them? That's all it's supposed to do. Poorly
290 * named step.
291 *
292 * @return Status
293 */
294 protected function installExtensions() {
295 $exts = $this->getVar( '_Extensions' );
296 $path = $this->getVar( 'IP' ) . '/extensions';
297
298 foreach( $exts as $e ) {
299 require( "$path/$e/$e.php" );
300 }
301
302 return Status::newGood();
303 }
304
305 /**
306 * Get an array of install steps. These could be a plain key like the defaults
307 * in $installSteps, or could be an array with a name and a specific callback
308 *
309 * @return array
310 */
311 protected function getInstallSteps() {
312 if( $this->getVar( '_UpgradeDone' ) ) {
313 $this->installSteps = array( 'localsettings' );
314 }
315
316 if( count( $this->getVar( '_Extensions' ) ) ) {
317 array_unshift( $this->installSteps, 'extensions' );
318 }
319
320 return $this->installSteps;
321 }
322
323 /**
324 * Actually perform the installation.
325 *
326 * @param $startCB A callback array for the beginning of each step
327 * @param $endCB A callback array for the end of each step
328 *
329 * @return Array of Status objects
330 */
331 public function performInstallation( $startCB, $endCB ) {
332 $installResults = array();
333 $installer = $this->getDBInstaller();
334 $installer->preInstall();
335
336 foreach( $this->getInstallSteps() as $stepObj ) {
337 $step = is_array( $stepObj ) ? $stepObj['name'] : $stepObj;
338 call_user_func_array( $startCB, array( $step ) );
339
340 # Call our working function
341 if ( is_array( $stepObj ) ) {
342 # A custom callaback
343 $callback = $stepObj['callback'];
344 $status = call_user_func_array( $callback, array( $installer ) );
345 } else {
346 # Boring implicitly named callback
347 $func = 'install' . ucfirst( $step );
348 $status = $this->{$func}( $installer );
349 }
350
351 call_user_func_array( $endCB, array( $step, $status ) );
352 $installResults[$step] = $status;
353
354 // If we've hit some sort of fatal, we need to bail.
355 // Callback already had a chance to do output above.
356 if( !$status->isOk() ) {
357 break;
358 }
359
360 }
361
362 if( $status->isOk() ) {
363 $this->setVar( '_InstallDone', true );
364 }
365
366 return $installResults;
367 }
368
369 /**
370 * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of
371 * /dev/urandom
372 *
373 * @return Status
374 */
375 protected function installSecretKey() {
376 if ( wfIsWindows() ) {
377 $file = null;
378 } else {
379 wfSuppressWarnings();
380 $file = fopen( "/dev/urandom", "r" );
381 wfRestoreWarnings();
382 }
383
384 $status = Status::newGood();
385
386 if ( $file ) {
387 $secretKey = bin2hex( fread( $file, 32 ) );
388 fclose( $file );
389 } else {
390 $secretKey = '';
391
392 for ( $i=0; $i<8; $i++ ) {
393 $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
394 }
395
396 $status->warning( 'config-insecure-secretkey' );
397 }
398
399 $this->setVar( 'wgSecretKey', $secretKey );
400
401 return $status;
402 }
403
404 /**
405 * Create the first user account, grant it sysop and bureaucrat rights
406 *
407 * @return Status
408 */
409 protected function installSysop() {
410 $name = $this->getVar( '_AdminName' );
411 $user = User::newFromName( $name );
412
413 if ( !$user ) {
414 // We should've validated this earlier anyway!
415 return Status::newFatal( 'config-admin-error-user', $name );
416 }
417
418 if ( $user->idForName() == 0 ) {
419 $user->addToDatabase();
420
421 try {
422 $user->setPassword( $this->getVar( '_AdminPassword' ) );
423 } catch( PasswordError $pwe ) {
424 return Status::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
425 }
426
427 $user->addGroup( 'sysop' );
428 $user->addGroup( 'bureaucrat' );
429 $user->saveSettings();
430 }
431
432 return Status::newGood();
433 }
434
435 /**
436 * Override the necessary bits of the config to run an installation.
437 */
438 public static function overrideConfig() {
439 define( 'MW_NO_SESSION', 1 );
440
441 // Don't access the database
442 $GLOBALS['wgUseDatabaseMessages'] = false;
443 // Debug-friendly
444 $GLOBALS['wgShowExceptionDetails'] = true;
445 // Don't break forms
446 $GLOBALS['wgExternalLinkTarget'] = '_blank';
447
448 // Extended debugging. Maybe disable before release?
449 $GLOBALS['wgShowSQLErrors'] = true;
450 $GLOBALS['wgShowDBErrorBacktrace'] = true;
451
452 // Allow multiple ob_flush() calls
453 $GLOBALS['wgDisableOutputCompression'] = true;
454
455 // Some of the environment checks make shell requests, remove limits
456 $GLOBALS['wgMaxShellMemory'] = 0;
457 }
458
459 /**
460 * Add an installation step following the given step.
461 *
462 * @param $findStep String the step to find. Use NULL to put the step at the beginning.
463 * @param $callback array
464 */
465 public function addInstallStepFollowing( $findStep, $callback ) {
466 $where = 0;
467
468 if( $findStep !== null ) {
469 $where = array_search( $findStep, $this->installSteps );
470 }
471
472 array_splice( $this->installSteps, $where, 0, $callback );
473 }
474
475 }