(bug 22308) Search should find text in default main page immediately after setup...
[mediawiki.git] / includes / installer / CoreInstaller.php
blob436dc27840e3cbe471f6fa14ac743dffecd87a96
1 <?php
2 /**
3 * Base core installer.
5 * @file
6 * @ingroup Deployment
7 */
9 /**
10 * Base core installer class.
11 * Handles everything that is independent of user interface.
13 * @ingroup Deployment
14 * @since 1.17
16 abstract class CoreInstaller extends Installer {
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.
23 * @var array
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',
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.
58 * @var array
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 '_LocalSettingsLocked' => true,
87 '_UpgradeKey' => '',
90 /**
91 * Steps for installation.
93 * @var array
95 protected $installSteps = array(
96 'database',
97 'tables',
98 'interwiki',
99 'secretkey',
100 'sysop',
101 'mainpage',
105 * Known object cache types and the functions used to test for their existence.
107 * @var array
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.
119 * @var array
121 public $rightsProfiles = array(
122 'wiki' => array(),
123 'no-anon' => array(
124 '*' => array( 'edit' => false )
126 'fishbowl' => array(
127 '*' => array(
128 'createaccount' => false,
129 'edit' => false,
132 'private' => array(
133 '*' => array(
134 'createaccount' => false,
135 'edit' => false,
136 'read' => false,
142 * License types.
144 * @var array
146 public $licenses = array(
147 'cc-by-sa' => 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',
155 'pd' => array(
156 'url' => 'http://creativecommons.org/licenses/publicdomain/',
157 'icon' => '{$wgStylePath}/common/images/public-domain.png',
159 'gfdl-old' => array(
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',
167 'none' => array(
168 'url' => '',
169 'icon' => '',
170 'text' => ''
172 'cc-choose' => array(
173 // Details will be filled in by the selector.
174 'url' => '',
175 'icon' => '',
176 'text' => '',
181 * TODO: document
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() ) {
216 continue;
219 $defaults = $installer->getGlobalDefaults();
221 foreach ( $installer->getGlobalNames() as $var ) {
222 if ( isset( $defaults[$var] ) ) {
223 $this->settings[$var] = $defaults[$var];
224 } else {
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' ) );
244 return true;
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 ) .
254 '</a>';
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.
268 * @return array
270 public function findExtensions() {
271 if( $this->getVar( 'IP' ) === null ) {
272 return false;
275 $exts = array();
276 $dir = $this->getVar( 'IP' ) . '/extensions';
277 $dh = opendir( $dir );
279 while ( ( $file = readdir( $dh ) ) !== false ) {
280 if( file_exists( "$dir/$file/$file.php" ) ) {
281 $exts[] = $file;
285 return $exts;
289 * Installs the auto-detected extensions.
291 * @TODO: this only requires them? That's all it's supposed to do. Poorly
292 * named step.
294 * @return Status
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
311 * @return array
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 ) ) {
344 # A custom callaback
345 $callback = $stepObj['callback'];
346 $status = call_user_func_array( $callback, array( $installer ) );
347 } else {
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() ) {
359 break;
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
373 * /dev/urandom
375 * @return Status
377 protected function installSecretKey() {
378 if ( wfIsWindows() ) {
379 $file = null;
380 } else {
381 wfSuppressWarnings();
382 $file = fopen( "/dev/urandom", "r" );
383 wfRestoreWarnings();
386 $status = Status::newGood();
388 if ( $file ) {
389 $secretKey = bin2hex( fread( $file, 32 ) );
390 fclose( $file );
391 } else {
392 $secretKey = '';
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 );
403 return $status;
407 * Create the first user account, grant it sysop and bureaucrat rights
409 * @return Status
411 protected function installSysop() {
412 $name = $this->getVar( '_AdminName' );
413 $user = User::newFromName( $name );
415 if ( !$user ) {
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();
423 try {
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.
440 * @return Status
442 public function installMainpage( DatabaseInstaller &$installer ) {
443 $status = Status::newGood();
444 try {
445 $titleobj = Title::newFromText( wfMsgForContent( "mainpage" ) );
446 $article = new Article( $titleobj );
447 $text = wfMsgForContent( 'mainpagetext' ) . "\n\n" .
448 wfMsgForContent( 'mainpagedocfooter' );
449 $article->doEdit( $text, '', EDIT_NEW, false,
450 User::newFromName( 'MediaWiki Default' ) );
451 $u = new SearchUpdate( $titleobj->getArticleID(),
452 $titleobj->getPrefixedDBkey(), $text );
453 $u->doUpdate();
454 } catch (MWException $e) {
455 //using raw, because $wgShowExceptionDetails can not be set yet
456 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
459 return $status;
463 * Override the necessary bits of the config to run an installation.
465 public static function overrideConfig() {
466 define( 'MW_NO_SESSION', 1 );
468 // Don't access the database
469 $GLOBALS['wgUseDatabaseMessages'] = false;
470 // Debug-friendly
471 $GLOBALS['wgShowExceptionDetails'] = true;
472 // Don't break forms
473 $GLOBALS['wgExternalLinkTarget'] = '_blank';
475 // Extended debugging. Maybe disable before release?
476 $GLOBALS['wgShowSQLErrors'] = true;
477 $GLOBALS['wgShowDBErrorBacktrace'] = true;
479 // Allow multiple ob_flush() calls
480 $GLOBALS['wgDisableOutputCompression'] = true;
482 // Some of the environment checks make shell requests, remove limits
483 $GLOBALS['wgMaxShellMemory'] = 0;
487 * Add an installation step following the given step.
489 * @param $findStep String the step to find. Use NULL to put the step at the beginning.
490 * @param $callback array
492 public function addInstallStepFollowing( $findStep, $callback ) {
493 $where = 0;
495 if( $findStep !== null ) {
496 $where = array_search( $findStep, $this->installSteps );
499 array_splice( $this->installSteps, $where, 0, $callback );