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
302 * @param $installer DatabaseInstaller so we can make callbacks
305 protected function getInstallSteps( DatabaseInstaller
&$installer ) {
306 $installSteps = array(
307 array( 'name' => 'database', 'callback' => array( $installer, 'setupDatabase' ) ),
308 array( 'name' => 'tables', 'callback' => array( $this, 'installTables' ) ),
309 array( 'name' => 'interwiki', 'callback' => array( $installer, 'populateInterwikiTable' ) ),
310 array( 'name' => 'secretkey', 'callback' => array( $this, 'generateSecretKey' ) ),
311 array( 'name' => 'upgradekey', 'callback' => array( $this, 'generateUpgradeKey' ) ),
312 array( 'name' => 'sysop', 'callback' => array( $this, 'createSysop' ) ),
313 array( 'name' => 'mainpage', 'callback' => array( $this, 'createMainpage' ) ),
315 if( count( $this->getVar( '_Extensions' ) ) ) {
316 array_unshift( $installSteps,
317 array( 'extensions', array( $this, 'includeExtensions' ) )
320 foreach( $installSteps as $idx => $stepObj ) {
321 if( isset( $this->extraInstallSteps
[ $stepObj['name'] ] ) ) {
322 $tmp = array_slice( $installSteps, 0, $idx );
323 $tmp[] = $this->extraInstallSteps
[ $stepObj['name'] ];
324 $installSteps = array_merge( $tmp, array_slice( $installSteps, $idx ) );
327 return $installSteps;
331 * Actually perform the installation.
333 * @param $startCB A callback array for the beginning of each step
334 * @param $endCB A callback array for the end of each step
336 * @return Array of Status objects
338 public function performInstallation( $startCB, $endCB ) {
339 $installResults = array();
340 $installer = $this->getDBInstaller();
341 $installer->preInstall();
342 $steps = $this->getInstallSteps( $installer );
343 foreach( $steps as $stepObj ) {
344 $name = $stepObj['name'];
345 call_user_func_array( $startCB, array( $name ) );
347 // Perform the callback step
348 $status = call_user_func_array( $stepObj['callback'], array( &$installer ) );
350 // Output and save the results
351 call_user_func_array( $endCB, array( $name, $status ) );
352 $installResults[$name] = $status;
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() ) {
360 if( $status->isOk() ) {
361 $this->setVar( '_InstallDone', true );
363 return $installResults;
367 * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of
372 protected function generateSecretKey() {
373 if ( wfIsWindows() ) {
376 wfSuppressWarnings();
377 $file = fopen( "/dev/urandom", "r" );
381 $status = Status
::newGood();
384 $secretKey = bin2hex( fread( $file, 32 ) );
389 for ( $i=0; $i<8; $i++
) {
390 $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
393 $status->warning( 'config-insecure-secretkey' );
396 $this->setVar( 'wgSecretKey', $secretKey );
402 * Generate a default $wgUpradeKey, using a semi-random 8 character portion
403 * of md5($wgSecretKey)
407 protected function generateUpgradeKey() {
408 $secret = md5( $this->getVar( 'wgSecretKey' ) );
409 $randPos = mt_rand( 0, strlen( $secret ) - 8 );
410 $this->setVar( 'wgUpgradeKey', substr( $secret, $randPos, $randPos +
8 ) );
411 return Status
::newGood();
415 * Create the first user account, grant it sysop and bureaucrat rights
419 protected function createSysop() {
420 $name = $this->getVar( '_AdminName' );
421 $user = User
::newFromName( $name );
424 // We should've validated this earlier anyway!
425 return Status
::newFatal( 'config-admin-error-user', $name );
428 if ( $user->idForName() == 0 ) {
429 $user->addToDatabase();
432 $user->setPassword( $this->getVar( '_AdminPassword' ) );
433 } catch( PasswordError
$pwe ) {
434 return Status
::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
437 $user->addGroup( 'sysop' );
438 $user->addGroup( 'bureaucrat' );
439 $user->saveSettings();
442 return Status
::newGood();
446 * Insert Main Page with default content.
450 protected function createMainpage( DatabaseInstaller
&$installer ) {
451 $status = Status
::newGood();
453 $article = new Article( Title
::newMainPage() );
454 $article->doEdit( wfMsgForContent( 'mainpagetext' ) . "\n\n" .
455 wfMsgForContent( 'mainpagedocfooter' ),
459 User
::newFromName( 'MediaWiki Default' ) );
460 } catch (MWException
$e) {
461 //using raw, because $wgShowExceptionDetails can not be set yet
462 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
469 * Override the necessary bits of the config to run an installation.
471 public static function overrideConfig() {
472 define( 'MW_NO_SESSION', 1 );
474 // Don't access the database
475 $GLOBALS['wgUseDatabaseMessages'] = false;
477 $GLOBALS['wgShowExceptionDetails'] = true;
479 $GLOBALS['wgExternalLinkTarget'] = '_blank';
481 // Extended debugging. Maybe disable before release?
482 $GLOBALS['wgShowSQLErrors'] = true;
483 $GLOBALS['wgShowDBErrorBacktrace'] = true;
485 // Allow multiple ob_flush() calls
486 $GLOBALS['wgDisableOutputCompression'] = true;
488 // Some of the environment checks make shell requests, remove limits
489 $GLOBALS['wgMaxShellMemory'] = 0;
493 * Add an installation step following the given step.
495 * @param $findStep String the step to find. Use NULL to put the step at the beginning.
496 * @param $callback array A valid callback array, with name and callback given
498 public function addInstallStepFollowing( $findStep, $callback ) {
499 $this->extraInstallSteps
[$findStep] = $callback;