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',
56 * Variables that are stored alongside globals, and are used for any
57 * configuration of the installation process aside from the MediaWiki
58 * configuration. Map of names to defaults.
62 protected $internalDefaults = array(
64 '_Environment' => false,
65 '_CompiledDBs' => array(),
67 '_RaiseMemory' => false,
68 '_UpgradeDone' => false,
69 '_InstallDone' => false,
71 '_InstallUser' => 'root',
72 '_InstallPassword' => '',
73 '_SameAccount' => true,
74 '_CreateDBAccount' => false,
75 '_NamespaceType' => 'site-name',
76 '_AdminName' => '', // will be set later, when the user selects language
77 '_AdminPassword' => '',
78 '_AdminPassword2' => '',
80 '_Subscribe' => false,
81 '_SkipOptional' => 'continue',
82 '_RightsProfile' => 'wiki',
83 '_LicenseCode' => 'none',
85 '_Extensions' => array(),
86 '_MemCachedServers' => '',
87 '_UpgradeKeySupplied' => false,
88 '_ExistingDBSettings' => false,
92 * Extra steps for installation, for things like DatabaseInstallers to modify
96 protected $extraInstallSteps = array();
99 * Known object cache types and the functions used to test for their existence.
103 protected $objectCaches = array(
104 'xcache' => 'xcache_get',
105 'apc' => 'apc_fetch',
106 'eaccel' => 'eaccelerator_get',
107 'wincache' => 'wincache_ucache_get'
111 * User rights profiles.
115 public $rightsProfiles = array(
118 '*' => array( 'edit' => false )
122 'createaccount' => false,
128 'createaccount' => false,
140 public $licenses = array(
142 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
143 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
145 'cc-by-nc-sa' => array(
146 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/',
147 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png',
150 'url' => 'http://creativecommons.org/licenses/publicdomain/',
151 'icon' => '{$wgStylePath}/common/images/public-domain.png',
154 'url' => 'http://www.gnu.org/licenses/old-licenses/fdl-1.2.html',
155 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
157 'gfdl-current' => array(
158 'url' => 'http://www.gnu.org/copyleft/fdl.html',
159 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
166 'cc-choose' => array(
167 // Details will be filled in by the selector.
177 * @param $status Status
179 public abstract function showStatusMessage( Status
$status );
183 * Constructor, always call this from child classes.
185 public function __construct() {
186 parent
::__construct();
188 global $wgExtensionMessagesFiles, $wgUser, $wgHooks;
190 // Load the installer's i18n file.
191 $wgExtensionMessagesFiles['MediawikiInstaller'] =
192 dirname( __FILE__
) . '/Installer.i18n.php';
194 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
195 $wgUser = User
::newFromId( 0 );
197 // Set our custom <doclink> hook.
198 $wgHooks['ParserFirstCallInit'][] = array( $this, 'registerDocLink' );
200 $this->settings
= $this->internalDefaults
;
202 foreach ( $this->defaultVarNames
as $var ) {
203 $this->settings
[$var] = $GLOBALS[$var];
206 foreach ( self
::getDBTypes() as $type ) {
207 $installer = $this->getDBInstaller( $type );
209 if ( !$installer->isCompiled() ) {
213 $defaults = $installer->getGlobalDefaults();
215 foreach ( $installer->getGlobalNames() as $var ) {
216 if ( isset( $defaults[$var] ) ) {
217 $this->settings
[$var] = $defaults[$var];
219 $this->settings
[$var] = $GLOBALS[$var];
224 $this->parserTitle
= Title
::newFromText( 'Installer' );
225 $this->parserOptions
= new ParserOptions
;
226 $this->parserOptions
->setEditSection( false );
230 * Register tag hook below.
232 * @todo Move this to WebInstaller with the two things below?
234 * @param $parser Parser
236 public function registerDocLink( Parser
&$parser ) {
237 $parser->setHook( 'doclink', array( $this, 'docLink' ) );
242 * Extension tag hook for a documentation link.
244 public function docLink( $linkText, $attribs, $parser ) {
245 $url = $this->getDocUrl( $attribs['href'] );
246 return '<a href="' . htmlspecialchars( $url ) . '">' .
247 htmlspecialchars( $linkText ) .
252 * Overridden by WebInstaller to provide lastPage parameters.
254 protected function getDocUrl( $page ) {
255 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $attribs['href'] );
259 * Finds extensions that follow the format /extensions/Name/Name.php,
260 * and returns an array containing the value for 'Name' for each found extension.
264 public function findExtensions() {
265 if( $this->getVar( 'IP' ) === null ) {
270 $dir = $this->getVar( 'IP' ) . '/extensions';
271 $dh = opendir( $dir );
273 while ( ( $file = readdir( $dh ) ) !== false ) {
274 if( file_exists( "$dir/$file/$file.php" ) ) {
283 * Installs the auto-detected extensions.
287 protected function includeExtensions() {
288 $exts = $this->getVar( '_Extensions' );
289 $path = $this->getVar( 'IP' ) . '/extensions';
291 foreach( $exts as $e ) {
292 require( "$path/$e/$e.php" );
295 return Status
::newGood();
299 * Get an array of install steps. These could be a plain key like the defaults
300 * in $installSteps, or could be an array with a name and a specific callback
301 * There must be a config-install-$step message defined per step, which will
302 * be shown on install.
304 * @param $installer DatabaseInstaller so we can make callbacks
307 protected function getInstallSteps( DatabaseInstaller
&$installer ) {
308 $installSteps = array(
309 array( 'name' => 'database', 'callback' => array( $installer, 'setupDatabase' ) ),
310 array( 'name' => 'tables', 'callback' => array( $this, 'installTables' ) ),
311 array( 'name' => 'interwiki', 'callback' => array( $installer, 'populateInterwikiTable' ) ),
312 array( 'name' => 'secretkey', 'callback' => array( $this, 'generateSecretKey' ) ),
313 array( 'name' => 'upgradekey', 'callback' => array( $this, 'generateUpgradeKey' ) ),
314 array( 'name' => 'sysop', 'callback' => array( $this, 'createSysop' ) ),
315 array( 'name' => 'mainpage', 'callback' => array( $this, 'createMainpage' ) ),
317 if( count( $this->getVar( '_Extensions' ) ) ) {
318 array_unshift( $installSteps,
319 array( 'name' => 'extensions', 'callback' => array( $this, 'includeExtensions' ) )
322 foreach( $installSteps as $idx => $stepObj ) {
323 if( isset( $this->extraInstallSteps
[ $stepObj['name'] ] ) ) {
324 $tmp = array_slice( $installSteps, 0, $idx );
325 $tmp[] = $this->extraInstallSteps
[ $stepObj['name'] ];
326 $installSteps = array_merge( $tmp, array_slice( $installSteps, $idx ) );
329 return $installSteps;
333 * Actually perform the installation.
335 * @param $startCB A callback array for the beginning of each step
336 * @param $endCB A callback array for the end of each step
338 * @return Array of Status objects
340 public function performInstallation( $startCB, $endCB ) {
341 $installResults = array();
342 $installer = $this->getDBInstaller();
343 $installer->preInstall();
344 $steps = $this->getInstallSteps( $installer );
345 foreach( $steps as $stepObj ) {
346 $name = $stepObj['name'];
347 call_user_func_array( $startCB, array( $name ) );
349 // Perform the callback step
350 $status = call_user_func_array( $stepObj['callback'], array( &$installer ) );
352 // Output and save the results
353 call_user_func_array( $endCB, array( $name, $status ) );
354 $installResults[$name] = $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() ) {
362 if( $status->isOk() ) {
363 $this->setVar( '_InstallDone', true );
365 return $installResults;
369 * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of
374 protected function generateSecretKey() {
375 return $this->generateSecret( 'wgSecretKey' );
379 * Generate a secret value for a variable using either
380 * /dev/urandom or mt_rand() Produce a warning in the later case.
384 protected function generateSecret( $secretName, $length = 64 ) {
385 if ( wfIsWindows() ) {
388 wfSuppressWarnings();
389 $file = fopen( "/dev/urandom", "r" );
393 $status = Status
::newGood();
396 $secretKey = bin2hex( fread( $file, $length / 2 ) );
401 for ( $i = 0; $i < $length / 8; $i++
) {
402 $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
405 $status->warning( 'config-insecure-secret', '$' . $secretName );
408 $this->setVar( $secretName, $secretKey );
414 * Generate a default $wgUpgradeKey. Will warn if we had to use
415 * mt_rand() instead of /dev/urandom
419 public function generateUpgradeKey() {
420 if ( strval( $this->getVar( 'wgUpgradeKey' ) ) === '' ) {
421 return $this->generateSecret( 'wgUpgradeKey', 16 );
426 * Create the first user account, grant it sysop and bureaucrat rights
430 protected function createSysop() {
431 $name = $this->getVar( '_AdminName' );
432 $user = User
::newFromName( $name );
435 // We should've validated this earlier anyway!
436 return Status
::newFatal( 'config-admin-error-user', $name );
439 if ( $user->idForName() == 0 ) {
440 $user->addToDatabase();
443 $user->setPassword( $this->getVar( '_AdminPassword' ) );
444 } catch( PasswordError
$pwe ) {
445 return Status
::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
448 $user->addGroup( 'sysop' );
449 $user->addGroup( 'bureaucrat' );
450 $user->saveSettings();
453 return Status
::newGood();
457 * Insert Main Page with default content.
461 protected function createMainpage( DatabaseInstaller
&$installer ) {
462 $status = Status
::newGood();
464 $article = new Article( Title
::newMainPage() );
465 $article->doEdit( wfMsgForContent( 'mainpagetext' ) . "\n\n" .
466 wfMsgForContent( 'mainpagedocfooter' ),
470 User
::newFromName( 'MediaWiki Default' ) );
471 } catch (MWException
$e) {
472 //using raw, because $wgShowExceptionDetails can not be set yet
473 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
480 * Override the necessary bits of the config to run an installation.
482 public static function overrideConfig() {
483 define( 'MW_NO_SESSION', 1 );
485 // Don't access the database
486 $GLOBALS['wgUseDatabaseMessages'] = false;
488 $GLOBALS['wgShowExceptionDetails'] = true;
490 $GLOBALS['wgExternalLinkTarget'] = '_blank';
492 // Extended debugging
493 $GLOBALS['wgShowSQLErrors'] = true;
494 $GLOBALS['wgShowDBErrorBacktrace'] = true;
496 // Allow multiple ob_flush() calls
497 $GLOBALS['wgDisableOutputCompression'] = true;
499 // Use a sensible cookie prefix (not my_wiki)
500 $GLOBALS['wgCookiePrefix'] = 'mw_installer';
502 // Some of the environment checks make shell requests, remove limits
503 $GLOBALS['wgMaxShellMemory'] = 0;
507 * Add an installation step following the given step.
509 * @param $findStep String the step to find. Use NULL to put the step at the beginning.
510 * @param $callback array A valid callback array, with name and callback given
512 public function addInstallStepFollowing( $findStep, $callback ) {
513 $this->extraInstallSteps
[$findStep] = $callback;