12440d825e5ab2e34ad011420ccbb6ebb95cd0bd
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',
55 * Variables that are stored alongside globals, and are used for any
56 * configuration of the installation process aside from the MediaWiki
57 * configuration. Map of names to defaults.
61 protected $internalDefaults = array(
63 '_Environment' => false,
64 '_CompiledDBs' => array(),
66 '_RaiseMemory' => false,
67 '_UpgradeDone' => false,
68 '_InstallDone' => false,
70 '_InstallUser' => 'root',
71 '_InstallPassword' => '',
72 '_SameAccount' => true,
73 '_CreateDBAccount' => false,
74 '_NamespaceType' => 'site-name',
75 '_AdminName' => '', // will be set later, when the user selects language
76 '_AdminPassword' => '',
77 '_AdminPassword2' => '',
79 '_Subscribe' => false,
80 '_SkipOptional' => 'continue',
81 '_RightsProfile' => 'wiki',
82 '_LicenseCode' => 'none',
84 '_Extensions' => array(),
85 '_MemCachedServers' => '',
86 '_ExternalHTTP' => false,
87 '_LocalSettingsLocked' => true,
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 are
302 * shown by the CliInstaller.
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 ) {
385 if ( wfIsWindows() ) {
388 wfSuppressWarnings();
389 $file = fopen( "/dev/urandom", "r" );
393 $status = Status
::newGood();
396 $secretKey = bin2hex( fread( $file, 32 ) );
401 for ( $i=0; $i<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 $wgUpradeKey, Will warn if we had to use
415 * mt_rand() instead of /dev/urandom
419 protected function generateUpgradeKey() {
420 return $this->generateSecret( 'wgUpgradeKey' );
424 * Create the first user account, grant it sysop and bureaucrat rights
428 protected function createSysop() {
429 $name = $this->getVar( '_AdminName' );
430 $user = User
::newFromName( $name );
433 // We should've validated this earlier anyway!
434 return Status
::newFatal( 'config-admin-error-user', $name );
437 if ( $user->idForName() == 0 ) {
438 $user->addToDatabase();
441 $user->setPassword( $this->getVar( '_AdminPassword' ) );
442 } catch( PasswordError
$pwe ) {
443 return Status
::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
446 $user->addGroup( 'sysop' );
447 $user->addGroup( 'bureaucrat' );
448 $user->saveSettings();
451 return Status
::newGood();
455 * Insert Main Page with default content.
459 protected function createMainpage( DatabaseInstaller
&$installer ) {
460 $status = Status
::newGood();
462 $article = new Article( Title
::newMainPage() );
463 $article->doEdit( wfMsgForContent( 'mainpagetext' ) . "\n\n" .
464 wfMsgForContent( 'mainpagedocfooter' ),
468 User
::newFromName( 'MediaWiki Default' ) );
469 } catch (MWException
$e) {
470 //using raw, because $wgShowExceptionDetails can not be set yet
471 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
478 * Override the necessary bits of the config to run an installation.
480 public static function overrideConfig() {
481 define( 'MW_NO_SESSION', 1 );
483 // Don't access the database
484 $GLOBALS['wgUseDatabaseMessages'] = false;
486 $GLOBALS['wgShowExceptionDetails'] = true;
488 $GLOBALS['wgExternalLinkTarget'] = '_blank';
490 // Extended debugging. Maybe disable before release?
491 $GLOBALS['wgShowSQLErrors'] = true;
492 $GLOBALS['wgShowDBErrorBacktrace'] = true;
494 // Allow multiple ob_flush() calls
495 $GLOBALS['wgDisableOutputCompression'] = true;
497 // Some of the environment checks make shell requests, remove limits
498 $GLOBALS['wgMaxShellMemory'] = 0;
502 * Add an installation step following the given step.
504 * @param $findStep String the step to find. Use NULL to put the step at the beginning.
505 * @param $callback array A valid callback array, with name and callback given
507 public function addInstallStepFollowing( $findStep, $callback ) {
508 $this->extraInstallSteps
[$findStep] = $callback;