* Installer for Oracle fixes
[mediawiki.git] / includes / installer / CoreInstaller.php
blob60d5acc6919ad89394df4203997aed6827eac678
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,
88 /**
89 * Steps for installation.
91 * @var array
93 protected $installSteps = array(
94 'database',
95 'tables',
96 'interwiki',
97 'secretkey',
98 'sysop',
102 * Known object cache types and the functions used to test for their existence.
104 * @var array
106 protected $objectCaches = array(
107 'xcache' => 'xcache_get',
108 'apc' => 'apc_fetch',
109 'eaccel' => 'eaccelerator_get',
110 'wincache' => 'wincache_ucache_get'
114 * User rights profiles.
116 * @var array
118 public $rightsProfiles = array(
119 'wiki' => array(),
120 'no-anon' => array(
121 '*' => array( 'edit' => false )
123 'fishbowl' => array(
124 '*' => array(
125 'createaccount' => false,
126 'edit' => false,
129 'private' => array(
130 '*' => array(
131 'createaccount' => false,
132 'edit' => false,
133 'read' => false,
139 * License types.
141 * @var array
143 public $licenses = array(
144 'cc-by-sa' => array(
145 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
146 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
148 'cc-by-nc-sa' => array(
149 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/',
150 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png',
152 'pd' => array(
153 'url' => 'http://creativecommons.org/licenses/publicdomain/',
154 'icon' => '{$wgStylePath}/common/images/public-domain.png',
156 'gfdl-old' => array(
157 'url' => 'http://www.gnu.org/licenses/old-licenses/fdl-1.2.html',
158 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
160 'gfdl-current' => array(
161 'url' => 'http://www.gnu.org/copyleft/fdl.html',
162 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
164 'none' => array(
165 'url' => '',
166 'icon' => '',
167 'text' => ''
169 'cc-choose' => array(
170 // Details will be filled in by the selector.
171 'url' => '',
172 'icon' => '',
173 'text' => '',
178 * TODO: document
180 * @param $status Status
182 public abstract function showStatusMessage( Status $status );
186 * Constructor, always call this from child classes.
188 public function __construct() {
189 parent::__construct();
191 global $wgExtensionMessagesFiles, $wgUser, $wgHooks;
193 // Load the installer's i18n file.
194 $wgExtensionMessagesFiles['MediawikiInstaller'] =
195 dirname( __FILE__ ) . '/Installer.i18n.php';
197 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
198 $wgUser = User::newFromId( 0 );
200 // Set our custom <doclink> hook.
201 $wgHooks['ParserFirstCallInit'][] = array( $this, 'registerDocLink' );
203 $this->settings = $this->internalDefaults;
205 foreach ( $this->defaultVarNames as $var ) {
206 $this->settings[$var] = $GLOBALS[$var];
209 foreach ( self::getDBTypes() as $type ) {
210 $installer = $this->getDBInstaller( $type );
212 if ( !$installer->isCompiled() ) {
213 continue;
216 $defaults = $installer->getGlobalDefaults();
218 foreach ( $installer->getGlobalNames() as $var ) {
219 if ( isset( $defaults[$var] ) ) {
220 $this->settings[$var] = $defaults[$var];
221 } else {
222 $this->settings[$var] = $GLOBALS[$var];
227 $this->parserTitle = Title::newFromText( 'Installer' );
228 $this->parserOptions = new ParserOptions;
229 $this->parserOptions->setEditSection( false );
233 * Register tag hook below.
235 * @todo Move this to WebInstaller with the two things below?
237 * @param $parser Parser
239 public function registerDocLink( Parser &$parser ) {
240 $parser->setHook( 'doclink', array( $this, 'docLink' ) );
241 return true;
245 * Extension tag hook for a documentation link.
247 public function docLink( $linkText, $attribs, $parser ) {
248 $url = $this->getDocUrl( $attribs['href'] );
249 return '<a href="' . htmlspecialchars( $url ) . '">' .
250 htmlspecialchars( $linkText ) .
251 '</a>';
255 * Overridden by WebInstaller to provide lastPage parameters.
257 protected function getDocUrl( $page ) {
258 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $attribs['href'] );
262 * Finds extensions that follow the format /extensions/Name/Name.php,
263 * and returns an array containing the value for 'Name' for each found extension.
265 * @return array
267 public function findExtensions() {
268 if( $this->getVar( 'IP' ) === null ) {
269 return false;
272 $exts = array();
273 $dir = $this->getVar( 'IP' ) . '/extensions';
274 $dh = opendir( $dir );
276 while ( ( $file = readdir( $dh ) ) !== false ) {
277 if( file_exists( "$dir/$file/$file.php" ) ) {
278 $exts[] = $file;
282 return $exts;
286 * Installs the auto-detected extensions.
288 * @TODO: this only requires them? That's all it's supposed to do. Poorly
289 * named step.
291 * @return Status
293 protected function installExtensions() {
294 $exts = $this->getVar( '_Extensions' );
295 $path = $this->getVar( 'IP' ) . '/extensions';
297 foreach( $exts as $e ) {
298 require( "$path/$e/$e.php" );
301 return Status::newGood();
305 * Get an array of install steps. These could be a plain key like the defaults
306 * in $installSteps, or could be an array with a name and a specific callback
308 * @return array
310 protected function getInstallSteps() {
311 if( $this->getVar( '_UpgradeDone' ) ) {
312 $this->installSteps = array( 'localsettings' );
315 if( count( $this->getVar( '_Extensions' ) ) ) {
316 array_unshift( $this->installSteps, 'extensions' );
319 return $this->installSteps;
323 * Actually perform the installation.
325 * @param $startCB A callback array for the beginning of each step
326 * @param $endCB A callback array for the end of each step
328 * @return Array of Status objects
330 public function performInstallation( $startCB, $endCB ) {
331 $installResults = array();
332 $installer = $this->getDBInstaller();
333 $installer->preInstall();
335 foreach( $this->getInstallSteps() as $stepObj ) {
336 $step = is_array( $stepObj ) ? $stepObj['name'] : $stepObj;
337 call_user_func_array( $startCB, array( $step ) );
339 # Call our working function
340 if ( is_array( $stepObj ) ) {
341 # A custom callaback
342 $callback = $stepObj['callback'];
343 $status = call_user_func_array( $callback, array( $installer ) );
344 } else {
345 # Boring implicitly named callback
346 $func = 'install' . ucfirst( $step );
347 $status = $this->{$func}( $installer );
350 call_user_func_array( $endCB, array( $step, $status ) );
351 $installResults[$step] = $status;
353 // If we've hit some sort of fatal, we need to bail.
354 // Callback already had a chance to do output above.
355 if( !$status->isOk() ) {
356 break;
361 if( $status->isOk() ) {
362 $this->setVar( '_InstallDone', true );
365 return $installResults;
369 * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of
370 * /dev/urandom
372 * @return Status
374 protected function installSecretKey() {
375 if ( wfIsWindows() ) {
376 $file = null;
377 } else {
378 wfSuppressWarnings();
379 $file = fopen( "/dev/urandom", "r" );
380 wfRestoreWarnings();
383 $status = Status::newGood();
385 if ( $file ) {
386 $secretKey = bin2hex( fread( $file, 32 ) );
387 fclose( $file );
388 } else {
389 $secretKey = '';
391 for ( $i=0; $i<8; $i++ ) {
392 $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
395 $status->warning( 'config-insecure-secretkey' );
398 $this->setVar( 'wgSecretKey', $secretKey );
400 return $status;
404 * Create the first user account, grant it sysop and bureaucrat rights
406 * @return Status
408 protected function installSysop() {
409 $name = $this->getVar( '_AdminName' );
410 $user = User::newFromName( $name );
412 if ( !$user ) {
413 // We should've validated this earlier anyway!
414 return Status::newFatal( 'config-admin-error-user', $name );
417 if ( $user->idForName() == 0 ) {
418 $user->addToDatabase();
420 try {
421 $user->setPassword( $this->getVar( '_AdminPassword' ) );
422 } catch( PasswordError $pwe ) {
423 return Status::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
426 $user->addGroup( 'sysop' );
427 $user->addGroup( 'bureaucrat' );
428 $user->saveSettings();
431 return Status::newGood();
435 * Override the necessary bits of the config to run an installation.
437 public static function overrideConfig() {
438 define( 'MW_NO_SESSION', 1 );
440 // Don't access the database
441 $GLOBALS['wgUseDatabaseMessages'] = false;
442 // Debug-friendly
443 $GLOBALS['wgShowExceptionDetails'] = true;
444 // Don't break forms
445 $GLOBALS['wgExternalLinkTarget'] = '_blank';
447 // Extended debugging. Maybe disable before release?
448 $GLOBALS['wgShowSQLErrors'] = true;
449 $GLOBALS['wgShowDBErrorBacktrace'] = true;
451 // Allow multiple ob_flush() calls
452 $GLOBALS['wgDisableOutputCompression'] = true;
454 // Some of the environment checks make shell requests, remove limits
455 $GLOBALS['wgMaxShellMemory'] = 0;
459 * Add an installation step following the given step.
461 * @param $findStep String the step to find. Use NULL to put the step at the beginning.
462 * @param $callback array
464 public function addInstallStepFollowing( $findStep, $callback ) {
465 $where = 0;
467 if( $findStep !== null ) {
468 $where = array_search( $findStep, $this->installSteps );
471 array_splice( $this->installSteps, $where, 0, $callback );