10 * Base core installer class.
11 * Handles everything that is independent of user interface.
16 abstract class CoreInstaller
extends Installer
{
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.
25 protected $defaultVarNames = array(
37 'wgEmailAuthentication',
40 'wgImageMagickConvertCommand',
50 'wgUseInstantCommons',
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.
60 protected $internalDefaults = array(
62 '_Environment' => false,
63 '_CompiledDBs' => array(),
65 '_RaiseMemory' => false,
66 '_UpgradeDone' => false,
67 '_InstallDone' => false,
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' => '',
78 '_Subscribe' => false,
79 '_SkipOptional' => 'continue',
80 '_RightsProfile' => 'wiki',
81 '_LicenseCode' => 'none',
83 '_Extensions' => array(),
84 '_MemCachedServers' => '',
85 '_ExternalHTTP' => false,
86 '_LocalSettingsLocked' => true,
91 * Steps for installation.
95 protected $installSteps = array(
105 * Known object cache types and the functions used to test for their existence.
109 protected $objectCaches = array(
110 'xcache' => 'xcache_get',
111 'apc' => 'apc_fetch',
112 'eaccel' => 'eaccelerator_get',
113 'wincache' => 'wincache_ucache_get'
117 * User rights profiles.
121 public $rightsProfiles = array(
124 '*' => array( 'edit' => false )
128 'createaccount' => false,
134 'createaccount' => false,
146 public $licenses = array(
148 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
149 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
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',
156 'url' => 'http://creativecommons.org/licenses/publicdomain/',
157 'icon' => '{$wgStylePath}/common/images/public-domain.png',
160 'url' => 'http://www.gnu.org/licenses/old-licenses/fdl-1.2.html',
161 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
163 'gfdl-current' => array(
164 'url' => 'http://www.gnu.org/copyleft/fdl.html',
165 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
172 'cc-choose' => array(
173 // Details will be filled in by the selector.
183 * @param $status Status
185 public abstract function showStatusMessage( Status
$status );
189 * Constructor, always call this from child classes.
191 public function __construct() {
192 parent
::__construct();
194 global $wgExtensionMessagesFiles, $wgUser, $wgHooks;
196 // Load the installer's i18n file.
197 $wgExtensionMessagesFiles['MediawikiInstaller'] =
198 dirname( __FILE__
) . '/Installer.i18n.php';
200 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
201 $wgUser = User
::newFromId( 0 );
203 // Set our custom <doclink> hook.
204 $wgHooks['ParserFirstCallInit'][] = array( $this, 'registerDocLink' );
206 $this->settings
= $this->internalDefaults
;
208 foreach ( $this->defaultVarNames
as $var ) {
209 $this->settings
[$var] = $GLOBALS[$var];
212 foreach ( self
::getDBTypes() as $type ) {
213 $installer = $this->getDBInstaller( $type );
215 if ( !$installer->isCompiled() ) {
219 $defaults = $installer->getGlobalDefaults();
221 foreach ( $installer->getGlobalNames() as $var ) {
222 if ( isset( $defaults[$var] ) ) {
223 $this->settings
[$var] = $defaults[$var];
225 $this->settings
[$var] = $GLOBALS[$var];
230 $this->parserTitle
= Title
::newFromText( 'Installer' );
231 $this->parserOptions
= new ParserOptions
;
232 $this->parserOptions
->setEditSection( false );
236 * Register tag hook below.
238 * @todo Move this to WebInstaller with the two things below?
240 * @param $parser Parser
242 public function registerDocLink( Parser
&$parser ) {
243 $parser->setHook( 'doclink', array( $this, 'docLink' ) );
248 * Extension tag hook for a documentation link.
250 public function docLink( $linkText, $attribs, $parser ) {
251 $url = $this->getDocUrl( $attribs['href'] );
252 return '<a href="' . htmlspecialchars( $url ) . '">' .
253 htmlspecialchars( $linkText ) .
258 * Overridden by WebInstaller to provide lastPage parameters.
260 protected function getDocUrl( $page ) {
261 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $attribs['href'] );
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.
270 public function findExtensions() {
271 if( $this->getVar( 'IP' ) === null ) {
276 $dir = $this->getVar( 'IP' ) . '/extensions';
277 $dh = opendir( $dir );
279 while ( ( $file = readdir( $dh ) ) !== false ) {
280 if( file_exists( "$dir/$file/$file.php" ) ) {
289 * Installs the auto-detected extensions.
291 * @TODO: this only requires them? That's all it's supposed to do. Poorly
296 protected function installExtensions() {
297 $exts = $this->getVar( '_Extensions' );
298 $path = $this->getVar( 'IP' ) . '/extensions';
300 foreach( $exts as $e ) {
301 require( "$path/$e/$e.php" );
304 return Status
::newGood();
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
313 protected function getInstallSteps() {
314 if( $this->getVar( '_UpgradeDone' ) ) {
315 $this->installSteps
= array( 'localsettings' );
318 if( count( $this->getVar( '_Extensions' ) ) ) {
319 array_unshift( $this->installSteps
, 'extensions' );
322 return $this->installSteps
;
326 * Actually perform the installation.
328 * @param $startCB A callback array for the beginning of each step
329 * @param $endCB A callback array for the end of each step
331 * @return Array of Status objects
333 public function performInstallation( $startCB, $endCB ) {
334 $installResults = array();
335 $installer = $this->getDBInstaller();
336 $installer->preInstall();
338 foreach( $this->getInstallSteps() as $stepObj ) {
339 $step = is_array( $stepObj ) ?
$stepObj['name'] : $stepObj;
340 call_user_func_array( $startCB, array( $step ) );
342 # Call our working function
343 if ( is_array( $stepObj ) ) {
345 $callback = $stepObj['callback'];
346 $status = call_user_func_array( $callback, array( $installer ) );
348 # Boring implicitly named callback
349 $func = 'install' . ucfirst( $step );
350 $status = $this->{$func}( $installer );
353 call_user_func_array( $endCB, array( $step, $status ) );
354 $installResults[$step] = $status;
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() ) {
364 if( $status->isOk() ) {
365 $this->setVar( '_InstallDone', true );
368 return $installResults;
372 * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of
377 protected function installSecretKey() {
378 if ( wfIsWindows() ) {
381 wfSuppressWarnings();
382 $file = fopen( "/dev/urandom", "r" );
386 $status = Status
::newGood();
389 $secretKey = bin2hex( fread( $file, 32 ) );
394 for ( $i=0; $i<8; $i++
) {
395 $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
398 $status->warning( 'config-insecure-secretkey' );
401 $this->setVar( 'wgSecretKey', $secretKey );
407 * Create the first user account, grant it sysop and bureaucrat rights
411 protected function installSysop() {
412 $name = $this->getVar( '_AdminName' );
413 $user = User
::newFromName( $name );
416 // We should've validated this earlier anyway!
417 return Status
::newFatal( 'config-admin-error-user', $name );
420 if ( $user->idForName() == 0 ) {
421 $user->addToDatabase();
424 $user->setPassword( $this->getVar( '_AdminPassword' ) );
425 } catch( PasswordError
$pwe ) {
426 return Status
::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
429 $user->addGroup( 'sysop' );
430 $user->addGroup( 'bureaucrat' );
431 $user->saveSettings();
434 return Status
::newGood();
438 * Insert Main Page with default content.
442 public function installMainpage( DatabaseInstaller
&$installer ) {
443 $status = Status
::newGood();
445 $article = new Article( Title
::newMainPage() );
446 $article->doEdit( wfMsgForContent( 'mainpagetext' ) . "\n\n" .
447 wfMsgForContent( 'mainpagedocfooter' ),
451 User
::newFromName( 'MediaWiki Default' ) );
452 } catch (MWException
$e) {
453 //using raw, because $wgShowExceptionDetails can not be set yet
454 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
461 * Override the necessary bits of the config to run an installation.
463 public static function overrideConfig() {
464 define( 'MW_NO_SESSION', 1 );
466 // Don't access the database
467 $GLOBALS['wgUseDatabaseMessages'] = false;
469 $GLOBALS['wgShowExceptionDetails'] = true;
471 $GLOBALS['wgExternalLinkTarget'] = '_blank';
473 // Extended debugging. Maybe disable before release?
474 $GLOBALS['wgShowSQLErrors'] = true;
475 $GLOBALS['wgShowDBErrorBacktrace'] = true;
477 // Allow multiple ob_flush() calls
478 $GLOBALS['wgDisableOutputCompression'] = true;
480 // Some of the environment checks make shell requests, remove limits
481 $GLOBALS['wgMaxShellMemory'] = 0;
485 * Add an installation step following the given step.
487 * @param $findStep String the step to find. Use NULL to put the step at the beginning.
488 * @param $callback array
490 public function addInstallStepFollowing( $findStep, $callback ) {
493 if( $findStep !== null ) {
494 $where = array_search( $findStep, $this->installSteps
);
497 array_splice( $this->installSteps
, $where, 0, $callback );