Remove double ; from r84971
[mediawiki.git] / includes / installer / Installer.php
blob87036c5e621c41cf52f8502aaebf5b00d81b5ef8
1 <?php
2 /**
3 * Base code for MediaWiki installer.
5 * @file
6 * @ingroup Deployment
7 */
9 /**
10 * This documentation group collects source code files with deployment functionality.
12 * @defgroup Deployment Deployment
15 /**
16 * Base installer class.
18 * This class provides the base for installation and update functionality
19 * for both MediaWiki core and extensions.
21 * @ingroup Deployment
22 * @since 1.17
24 abstract class Installer {
26 // This is the absolute minimum PHP version we can support
27 const MINIMUM_PHP_VERSION = '5.2.3';
29 /**
30 * @var array
32 protected $settings;
34 /**
35 * Cached DB installer instances, access using getDBInstaller().
37 * @var array
39 protected $dbInstallers = array();
41 /**
42 * Minimum memory size in MB.
44 * @var integer
46 protected $minMemorySize = 50;
48 /**
49 * Cached Title, used by parse().
51 * @var Title
53 protected $parserTitle;
55 /**
56 * Cached ParserOptions, used by parse().
58 * @var ParserOptions
60 protected $parserOptions;
62 /**
63 * Known database types. These correspond to the class names <type>Installer,
64 * and are also MediaWiki database types valid for $wgDBtype.
66 * To add a new type, create a <type>Installer class and a Database<type>
67 * class, and add a config-type-<type> message to MessagesEn.php.
69 * @var array
71 protected static $dbTypes = array(
72 'mysql',
73 'postgres',
74 'oracle',
75 'sqlite',
78 /**
79 * A list of environment check methods called by doEnvironmentChecks().
80 * These may output warnings using showMessage(), and/or abort the
81 * installation process by returning false.
83 * @var array
85 protected $envChecks = array(
86 'envCheckDB',
87 'envCheckRegisterGlobals',
88 'envCheckBrokenXML',
89 'envCheckPHP531',
90 'envCheckMagicQuotes',
91 'envCheckMagicSybase',
92 'envCheckMbstring',
93 'envCheckZE1',
94 'envCheckSafeMode',
95 'envCheckXML',
96 'envCheckPCRE',
97 'envCheckMemory',
98 'envCheckCache',
99 'envCheckDiff3',
100 'envCheckGraphics',
101 'envCheckPath',
102 'envCheckExtension',
103 'envCheckShellLocale',
104 'envCheckUploadsDirectory',
105 'envCheckLibicu'
109 * MediaWiki configuration globals that will eventually be passed through
110 * to LocalSettings.php. The names only are given here, the defaults
111 * typically come from DefaultSettings.php.
113 * @var array
115 protected $defaultVarNames = array(
116 'wgSitename',
117 'wgPasswordSender',
118 'wgLanguageCode',
119 'wgRightsIcon',
120 'wgRightsText',
121 'wgRightsUrl',
122 'wgMainCacheType',
123 'wgEnableEmail',
124 'wgEnableUserEmail',
125 'wgEnotifUserTalk',
126 'wgEnotifWatchlist',
127 'wgEmailAuthentication',
128 'wgDBtype',
129 'wgDiff3',
130 'wgImageMagickConvertCommand',
131 'IP',
132 'wgScriptPath',
133 'wgScriptExtension',
134 'wgMetaNamespace',
135 'wgDeletedDirectory',
136 'wgEnableUploads',
137 'wgLogo',
138 'wgShellLocale',
139 'wgSecretKey',
140 'wgUseInstantCommons',
141 'wgUpgradeKey',
142 'wgDefaultSkin',
146 * Variables that are stored alongside globals, and are used for any
147 * configuration of the installation process aside from the MediaWiki
148 * configuration. Map of names to defaults.
150 * @var array
152 protected $internalDefaults = array(
153 '_UserLang' => 'en',
154 '_Environment' => false,
155 '_CompiledDBs' => array(),
156 '_SafeMode' => false,
157 '_RaiseMemory' => false,
158 '_UpgradeDone' => false,
159 '_InstallDone' => false,
160 '_Caches' => array(),
161 '_InstallUser' => 'root',
162 '_InstallPassword' => '',
163 '_SameAccount' => true,
164 '_CreateDBAccount' => false,
165 '_NamespaceType' => 'site-name',
166 '_AdminName' => '', // will be set later, when the user selects language
167 '_AdminPassword' => '',
168 '_AdminPassword2' => '',
169 '_AdminEmail' => '',
170 '_Subscribe' => false,
171 '_SkipOptional' => 'continue',
172 '_RightsProfile' => 'wiki',
173 '_LicenseCode' => 'none',
174 '_CCDone' => false,
175 '_Extensions' => array(),
176 '_MemCachedServers' => '',
177 '_UpgradeKeySupplied' => false,
178 '_ExistingDBSettings' => false,
182 * The actual list of installation steps. This will be initialized by getInstallSteps()
184 * @var array
186 private $installSteps = array();
189 * Extra steps for installation, for things like DatabaseInstallers to modify
191 * @var array
193 protected $extraInstallSteps = array();
196 * Known object cache types and the functions used to test for their existence.
198 * @var array
200 protected $objectCaches = array(
201 'xcache' => 'xcache_get',
202 'apc' => 'apc_fetch',
203 'eaccel' => 'eaccelerator_get',
204 'wincache' => 'wincache_ucache_get'
208 * User rights profiles.
210 * @var array
212 public $rightsProfiles = array(
213 'wiki' => array(),
214 'no-anon' => array(
215 '*' => array( 'edit' => false )
217 'fishbowl' => array(
218 '*' => array(
219 'createaccount' => false,
220 'edit' => false,
223 'private' => array(
224 '*' => array(
225 'createaccount' => false,
226 'edit' => false,
227 'read' => false,
233 * License types.
235 * @var array
237 public $licenses = array(
238 'cc-by-sa' => array(
239 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
240 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
242 'cc-by-nc-sa' => array(
243 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/',
244 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png',
246 'cc-0' => array(
247 'url' => 'https://creativecommons.org/publicdomain/zero/1.0/',
248 'icon' => '{$wgStylePath}/common/images/cc-0.png',
250 'pd' => array(
251 'url' => 'http://creativecommons.org/licenses/publicdomain/',
252 'icon' => '{$wgStylePath}/common/images/public-domain.png',
254 'gfdl-old' => array(
255 'url' => 'http://www.gnu.org/licenses/old-licenses/fdl-1.2.html',
256 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
258 'gfdl-current' => array(
259 'url' => 'http://www.gnu.org/copyleft/fdl.html',
260 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
262 'none' => array(
263 'url' => '',
264 'icon' => '',
265 'text' => ''
267 'cc-choose' => array(
268 // Details will be filled in by the selector.
269 'url' => '',
270 'icon' => '',
271 'text' => '',
276 * URL to mediawiki-announce subscription
278 protected $mediaWikiAnnounceUrl = 'https://lists.wikimedia.org/mailman/subscribe/mediawiki-announce';
281 * Supported language codes for Mailman
283 protected $mediaWikiAnnounceLanguages = array(
284 'ca', 'cs', 'da', 'de', 'en', 'es', 'et', 'eu', 'fi', 'fr', 'hr', 'hu',
285 'it', 'ja', 'ko', 'lt', 'nl', 'no', 'pl', 'pt', 'pt-br', 'ro', 'ru',
286 'sl', 'sr', 'sv', 'tr', 'uk'
290 * UI interface for displaying a short message
291 * The parameters are like parameters to wfMsg().
292 * The messages will be in wikitext format, which will be converted to an
293 * output format such as HTML or text before being sent to the user.
295 public abstract function showMessage( $msg /*, ... */ );
298 * Show a message to the installing user by using a Status object
299 * @param $status Status
301 public abstract function showStatusMessage( Status $status );
304 * Constructor, always call this from child classes.
306 public function __construct() {
307 global $wgExtensionMessagesFiles, $wgUser;
309 // Disable the i18n cache and LoadBalancer
310 Language::getLocalisationCache()->disableBackend();
311 LBFactory::disableBackend();
313 // Load the installer's i18n file.
314 $wgExtensionMessagesFiles['MediawikiInstaller'] =
315 dirname( __FILE__ ) . '/Installer.i18n.php';
317 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
318 $wgUser = User::newFromId( 0 );
320 $this->settings = $this->internalDefaults;
322 foreach ( $this->defaultVarNames as $var ) {
323 $this->settings[$var] = $GLOBALS[$var];
326 foreach ( self::getDBTypes() as $type ) {
327 $installer = $this->getDBInstaller( $type );
329 if ( !$installer->isCompiled() ) {
330 continue;
333 $defaults = $installer->getGlobalDefaults();
335 foreach ( $installer->getGlobalNames() as $var ) {
336 if ( isset( $defaults[$var] ) ) {
337 $this->settings[$var] = $defaults[$var];
338 } else {
339 $this->settings[$var] = $GLOBALS[$var];
344 $this->parserTitle = Title::newFromText( 'Installer' );
345 $this->parserOptions = new ParserOptions; // language will be wrong :(
346 $this->parserOptions->setEditSection( false );
350 * Get a list of known DB types.
352 public static function getDBTypes() {
353 return self::$dbTypes;
357 * Do initial checks of the PHP environment. Set variables according to
358 * the observed environment.
360 * It's possible that this may be called under the CLI SAPI, not the SAPI
361 * that the wiki will primarily run under. In that case, the subclass should
362 * initialise variables such as wgScriptPath, before calling this function.
364 * Under the web subclass, it can already be assumed that PHP 5+ is in use
365 * and that sessions are working.
367 * @return Status
369 public function doEnvironmentChecks() {
370 $phpVersion = phpversion();
371 if( version_compare( $phpVersion, self::MINIMUM_PHP_VERSION, '>=' ) ) {
372 $this->showMessage( 'config-env-php', $phpVersion );
373 $good = true;
374 } else {
375 $this->showMessage( 'config-env-php-toolow', $phpVersion, self::MINIMUM_PHP_VERSION );
376 $good = false;
379 if( $good ) {
380 foreach ( $this->envChecks as $check ) {
381 $status = $this->$check();
382 if ( $status === false ) {
383 $good = false;
388 $this->setVar( '_Environment', $good );
390 return $good ? Status::newGood() : Status::newFatal( 'config-env-bad' );
394 * Set a MW configuration variable, or internal installer configuration variable.
396 * @param $name String
397 * @param $value Mixed
399 public function setVar( $name, $value ) {
400 $this->settings[$name] = $value;
404 * Get an MW configuration variable, or internal installer configuration variable.
405 * The defaults come from $GLOBALS (ultimately DefaultSettings.php).
406 * Installer variables are typically prefixed by an underscore.
408 * @param $name String
409 * @param $default Mixed
411 * @return mixed
413 public function getVar( $name, $default = null ) {
414 if ( !isset( $this->settings[$name] ) ) {
415 return $default;
416 } else {
417 return $this->settings[$name];
422 * Get an instance of DatabaseInstaller for the specified DB type.
424 * @param $type Mixed: DB installer for which is needed, false to use default.
426 * @return DatabaseInstaller
428 public function getDBInstaller( $type = false ) {
429 if ( !$type ) {
430 $type = $this->getVar( 'wgDBtype' );
433 $type = strtolower( $type );
435 if ( !isset( $this->dbInstallers[$type] ) ) {
436 $class = ucfirst( $type ). 'Installer';
437 $this->dbInstallers[$type] = new $class( $this );
440 return $this->dbInstallers[$type];
444 * Determine if LocalSettings.php exists. If it does, return its variables,
445 * merged with those from AdminSettings.php, as an array.
447 * @return Array
449 public function getExistingLocalSettings() {
450 global $IP;
452 wfSuppressWarnings();
453 $_lsExists = file_exists( "$IP/LocalSettings.php" );
454 wfRestoreWarnings();
456 if( !$_lsExists ) {
457 return false;
459 unset($_lsExists);
461 require( "$IP/includes/DefaultSettings.php" );
462 require( "$IP/LocalSettings.php" );
463 if ( file_exists( "$IP/AdminSettings.php" ) ) {
464 require( "$IP/AdminSettings.php" );
466 return get_defined_vars();
470 * Get a fake password for sending back to the user in HTML.
471 * This is a security mechanism to avoid compromise of the password in the
472 * event of session ID compromise.
474 * @param $realPassword String
476 * @return string
478 public function getFakePassword( $realPassword ) {
479 return str_repeat( '*', strlen( $realPassword ) );
483 * Set a variable which stores a password, except if the new value is a
484 * fake password in which case leave it as it is.
486 * @param $name String
487 * @param $value Mixed
489 public function setPassword( $name, $value ) {
490 if ( !preg_match( '/^\*+$/', $value ) ) {
491 $this->setVar( $name, $value );
496 * On POSIX systems return the primary group of the webserver we're running under.
497 * On other systems just returns null.
499 * This is used to advice the user that he should chgrp his mw-config/data/images directory as the
500 * webserver user before he can install.
502 * Public because SqliteInstaller needs it, and doesn't subclass Installer.
504 * @return mixed
506 public static function maybeGetWebserverPrimaryGroup() {
507 if ( !function_exists( 'posix_getegid' ) || !function_exists( 'posix_getpwuid' ) ) {
508 # I don't know this, this isn't UNIX.
509 return null;
512 # posix_getegid() *not* getmygid() because we want the group of the webserver,
513 # not whoever owns the current script.
514 $gid = posix_getegid();
515 $getpwuid = posix_getpwuid( $gid );
516 $group = $getpwuid['name'];
518 return $group;
522 * Convert wikitext $text to HTML.
524 * This is potentially error prone since many parser features require a complete
525 * installed MW database. The solution is to just not use those features when you
526 * write your messages. This appears to work well enough. Basic formatting and
527 * external links work just fine.
529 * But in case a translator decides to throw in a #ifexist or internal link or
530 * whatever, this function is guarded to catch the attempted DB access and to present
531 * some fallback text.
533 * @param $text String
534 * @param $lineStart Boolean
535 * @return String
537 public function parse( $text, $lineStart = false ) {
538 global $wgParser;
540 try {
541 $out = $wgParser->parse( $text, $this->parserTitle, $this->parserOptions, $lineStart );
542 $html = $out->getText();
543 } catch ( DBAccessError $e ) {
544 $html = '<!--DB access attempted during parse--> ' . htmlspecialchars( $text );
546 if ( !empty( $this->debug ) ) {
547 $html .= "<!--\n" . $e->getTraceAsString() . "\n-->";
551 return $html;
554 public function getParserOptions() {
555 return $this->parserOptions;
558 public function disableLinkPopups() {
559 $this->parserOptions->setExternalLinkTarget( false );
562 public function restoreLinkPopups() {
563 global $wgExternalLinkTarget;
564 $this->parserOptions->setExternalLinkTarget( $wgExternalLinkTarget );
568 * Install step which adds a row to the site_stats table with appropriate
569 * initial values.
571 public function populateSiteStats( DatabaseInstaller $installer ) {
572 $status = $installer->getConnection();
573 if ( !$status->isOK() ) {
574 return $status;
576 $status->value->insert( 'site_stats', array(
577 'ss_row_id' => 1,
578 'ss_total_views' => 0,
579 'ss_total_edits' => 0,
580 'ss_good_articles' => 0,
581 'ss_total_pages' => 0,
582 'ss_users' => 0,
583 'ss_admins' => 0,
584 'ss_images' => 0 ),
585 __METHOD__, 'IGNORE' );
586 return Status::newGood();
590 * Exports all wg* variables stored by the installer into global scope.
592 public function exportVars() {
593 foreach ( $this->settings as $name => $value ) {
594 if ( substr( $name, 0, 2 ) == 'wg' ) {
595 $GLOBALS[$name] = $value;
601 * Environment check for DB types.
603 protected function envCheckDB() {
604 global $wgLang;
606 $compiledDBs = array();
607 $allNames = array();
609 foreach ( self::getDBTypes() as $name ) {
610 if ( $this->getDBInstaller( $name )->isCompiled() ) {
611 $compiledDBs[] = $name;
613 $allNames[] = wfMsg( 'config-type-' . $name );
616 $this->setVar( '_CompiledDBs', $compiledDBs );
618 if ( !$compiledDBs ) {
619 $this->showMessage( 'config-no-db' );
620 // FIXME: this only works for the web installer!
621 $this->showHelpBox( 'config-no-db-help', $wgLang->commaList( $allNames ) );
622 return false;
625 // Check for FTS3 full-text search module
626 $sqlite = $this->getDBInstaller( 'sqlite' );
627 if ( $sqlite->isCompiled() ) {
628 $db = new DatabaseSqliteStandalone( ':memory:' );
629 if( $db->getFulltextSearchModule() != 'FTS3' ) {
630 $this->showMessage( 'config-no-fts3' );
636 * Environment check for register_globals.
638 protected function envCheckRegisterGlobals() {
639 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
640 $this->showMessage( 'config-register-globals' );
645 * Some versions of libxml+PHP break < and > encoding horribly
647 protected function envCheckBrokenXML() {
648 $test = new PhpXmlBugTester();
649 if ( !$test->ok ) {
650 $this->showMessage( 'config-brokenlibxml' );
651 return false;
656 * Test PHP (probably 5.3.1, but it could regress again) to make sure that
657 * reference parameters to __call() are not converted to null
659 protected function envCheckPHP531() {
660 $test = new PhpRefCallBugTester;
661 $test->execute();
662 if ( !$test->ok ) {
663 $this->showMessage( 'config-using531', phpversion() );
664 return false;
669 * Environment check for magic_quotes_runtime.
671 protected function envCheckMagicQuotes() {
672 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
673 $this->showMessage( 'config-magic-quotes-runtime' );
674 return false;
679 * Environment check for magic_quotes_sybase.
681 protected function envCheckMagicSybase() {
682 if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
683 $this->showMessage( 'config-magic-quotes-sybase' );
684 return false;
689 * Environment check for mbstring.func_overload.
691 protected function envCheckMbstring() {
692 if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
693 $this->showMessage( 'config-mbstring' );
694 return false;
699 * Environment check for zend.ze1_compatibility_mode.
701 protected function envCheckZE1() {
702 if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
703 $this->showMessage( 'config-ze1' );
704 return false;
709 * Environment check for safe_mode.
711 protected function envCheckSafeMode() {
712 if ( wfIniGetBool( 'safe_mode' ) ) {
713 $this->setVar( '_SafeMode', true );
714 $this->showMessage( 'config-safe-mode' );
719 * Environment check for the XML module.
721 protected function envCheckXML() {
722 if ( !function_exists( "utf8_encode" ) ) {
723 $this->showMessage( 'config-xml-bad' );
724 return false;
729 * Environment check for the PCRE module.
731 protected function envCheckPCRE() {
732 if ( !function_exists( 'preg_match' ) ) {
733 $this->showMessage( 'config-pcre' );
734 return false;
736 wfSuppressWarnings();
737 $regexd = preg_replace( '/[\x{0430}-\x{04FF}]/iu', '', '-АБВГД-' );
738 wfRestoreWarnings();
739 if ( $regexd != '--' ) {
740 $this->showMessage( 'config-pcre-no-utf8' );
741 return false;
746 * Environment check for available memory.
748 protected function envCheckMemory() {
749 $limit = ini_get( 'memory_limit' );
751 if ( !$limit || $limit == -1 ) {
752 return true;
755 $n = wfShorthandToInteger( $limit );
757 if( $n < $this->minMemorySize * 1024 * 1024 ) {
758 $newLimit = "{$this->minMemorySize}M";
760 if( ini_set( "memory_limit", $newLimit ) === false ) {
761 $this->showMessage( 'config-memory-bad', $limit );
762 } else {
763 $this->showMessage( 'config-memory-raised', $limit, $newLimit );
764 $this->setVar( '_RaiseMemory', true );
766 } else {
767 return true;
772 * Environment check for compiled object cache types.
774 protected function envCheckCache() {
775 $caches = array();
776 foreach ( $this->objectCaches as $name => $function ) {
777 if ( function_exists( $function ) ) {
778 $caches[$name] = true;
782 if ( !$caches ) {
783 $this->showMessage( 'config-no-cache' );
786 $this->setVar( '_Caches', $caches );
790 * Search for GNU diff3.
792 protected function envCheckDiff3() {
793 $names = array( "gdiff3", "diff3", "diff3.exe" );
794 $versionInfo = array( '$1 --version 2>&1', 'GNU diffutils' );
796 $diff3 = self::locateExecutableInDefaultPaths( $names, $versionInfo );
798 if ( $diff3 ) {
799 $this->setVar( 'wgDiff3', $diff3 );
800 } else {
801 $this->setVar( 'wgDiff3', false );
802 $this->showMessage( 'config-diff3-bad' );
807 * Environment check for ImageMagick and GD.
809 protected function envCheckGraphics() {
810 $names = array( wfIsWindows() ? 'convert.exe' : 'convert' );
811 $convert = self::locateExecutableInDefaultPaths( $names, array( '$1 -version', 'ImageMagick' ) );
813 $this->setVar( 'wgImageMagickConvertCommand', '' );
814 if ( $convert ) {
815 $this->setVar( 'wgImageMagickConvertCommand', $convert );
816 $this->showMessage( 'config-imagemagick', $convert );
817 return true;
818 } elseif ( function_exists( 'imagejpeg' ) ) {
819 $this->showMessage( 'config-gd' );
820 return true;
821 } else {
822 $this->showMessage( 'config-no-scaling' );
827 * Environment check for setting $IP and $wgScriptPath.
829 protected function envCheckPath() {
830 global $IP;
831 $IP = dirname( dirname( dirname( __FILE__ ) ) );
833 $this->setVar( 'IP', $IP );
835 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
836 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
837 // to get the path to the current script... hopefully it's reliable. SIGH
838 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
839 $path = $_SERVER['PHP_SELF'];
840 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
841 $path = $_SERVER['SCRIPT_NAME'];
842 } elseif ( $this->getVar( 'wgScriptPath' ) ) {
843 // Some kind soul has set it for us already (e.g. debconf)
844 return true;
845 } else {
846 $this->showMessage( 'config-no-uri' );
847 return false;
850 $uri = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
851 $this->setVar( 'wgScriptPath', $uri );
855 * Environment check for setting the preferred PHP file extension.
857 protected function envCheckExtension() {
858 // FIXME: detect this properly
859 if ( defined( 'MW_INSTALL_PHP5_EXT' ) ) {
860 $ext = 'php5';
861 } else {
862 $ext = 'php';
864 $this->setVar( 'wgScriptExtension', ".$ext" );
868 * TODO: document
870 protected function envCheckShellLocale() {
871 $os = php_uname( 's' );
872 $supported = array( 'Linux', 'SunOS', 'HP-UX', 'Darwin' ); # Tested these
874 if ( !in_array( $os, $supported ) ) {
875 return true;
878 # Get a list of available locales.
879 $ret = false;
880 $lines = wfShellExec( '/usr/bin/locale -a', $ret );
882 if ( $ret ) {
883 return true;
886 $lines = wfArrayMap( 'trim', explode( "\n", $lines ) );
887 $candidatesByLocale = array();
888 $candidatesByLang = array();
890 foreach ( $lines as $line ) {
891 if ( $line === '' ) {
892 continue;
895 if ( !preg_match( '/^([a-zA-Z]+)(_[a-zA-Z]+|)\.(utf8|UTF-8)(@[a-zA-Z_]*|)$/i', $line, $m ) ) {
896 continue;
899 list( $all, $lang, $territory, $charset, $modifier ) = $m;
901 $candidatesByLocale[$m[0]] = $m;
902 $candidatesByLang[$lang][] = $m;
905 # Try the current value of LANG.
906 if ( isset( $candidatesByLocale[ getenv( 'LANG' ) ] ) ) {
907 $this->setVar( 'wgShellLocale', getenv( 'LANG' ) );
908 return true;
911 # Try the most common ones.
912 $commonLocales = array( 'en_US.UTF-8', 'en_US.utf8', 'de_DE.UTF-8', 'de_DE.utf8' );
913 foreach ( $commonLocales as $commonLocale ) {
914 if ( isset( $candidatesByLocale[$commonLocale] ) ) {
915 $this->setVar( 'wgShellLocale', $commonLocale );
916 return true;
920 # Is there an available locale in the Wiki's language?
921 $wikiLang = $this->getVar( 'wgLanguageCode' );
923 if ( isset( $candidatesByLang[$wikiLang] ) ) {
924 $m = reset( $candidatesByLang[$wikiLang] );
925 $this->setVar( 'wgShellLocale', $m[0] );
926 return true;
929 # Are there any at all?
930 if ( count( $candidatesByLocale ) ) {
931 $m = reset( $candidatesByLocale );
932 $this->setVar( 'wgShellLocale', $m[0] );
933 return true;
936 # Give up.
937 return true;
941 * TODO: document
943 protected function envCheckUploadsDirectory() {
944 global $IP, $wgServer;
946 $dir = $IP . '/images/';
947 $url = $wgServer . $this->getVar( 'wgScriptPath' ) . '/images/';
948 $safe = !$this->dirIsExecutable( $dir, $url );
950 if ( $safe ) {
951 return true;
952 } else {
953 $this->showMessage( 'config-uploads-not-safe', $dir );
958 * Convert a hex string representing a Unicode code point to that code point.
959 * @param $c String
960 * @return string
962 protected function unicodeChar( $c ) {
963 $c = hexdec($c);
964 if ($c <= 0x7F) {
965 return chr($c);
966 } else if ($c <= 0x7FF) {
967 return chr(0xC0 | $c >> 6) . chr(0x80 | $c & 0x3F);
968 } else if ($c <= 0xFFFF) {
969 return chr(0xE0 | $c >> 12) . chr(0x80 | $c >> 6 & 0x3F)
970 . chr(0x80 | $c & 0x3F);
971 } else if ($c <= 0x10FFFF) {
972 return chr(0xF0 | $c >> 18) . chr(0x80 | $c >> 12 & 0x3F)
973 . chr(0x80 | $c >> 6 & 0x3F)
974 . chr(0x80 | $c & 0x3F);
975 } else {
976 return false;
982 * Check the libicu version
984 protected function envCheckLibicu() {
985 $utf8 = function_exists( 'utf8_normalize' );
986 $intl = function_exists( 'normalizer_normalize' );
989 * This needs to be updated something that the latest libicu
990 * will properly normalize. This normalization was found at
991 * http://www.unicode.org/versions/Unicode5.2.0/#Character_Additions
992 * Note that we use the hex representation to create the code
993 * points in order to avoid any Unicode-destroying during transit.
995 $not_normal_c = $this->unicodeChar("FA6C");
996 $normal_c = $this->unicodeChar("242EE");
998 $useNormalizer = 'php';
999 $needsUpdate = false;
1002 * We're going to prefer the pecl extension here unless
1003 * utf8_normalize is more up to date.
1005 if( $utf8 ) {
1006 $useNormalizer = 'utf8';
1007 $utf8 = utf8_normalize( $not_normal_c, UNORM_NFC );
1008 if ( $utf8 !== $normal_c ) $needsUpdate = true;
1010 if( $intl ) {
1011 $useNormalizer = 'intl';
1012 $intl = normalizer_normalize( $not_normal_c, Normalizer::FORM_C );
1013 if ( $intl !== $normal_c ) $needsUpdate = true;
1016 // Uses messages 'config-unicode-using-php', 'config-unicode-using-utf8', 'config-unicode-using-intl'
1017 if( $useNormalizer === 'php' ) {
1018 $this->showMessage( 'config-unicode-pure-php-warning' );
1019 } else {
1020 $this->showMessage( 'config-unicode-using-' . $useNormalizer );
1021 if( $needsUpdate ) {
1022 $this->showMessage( 'config-unicode-update-warning' );
1028 * Get an array of likely places we can find executables. Check a bunch
1029 * of known Unix-like defaults, as well as the PATH environment variable
1030 * (which should maybe make it work for Windows?)
1032 * @return Array
1034 protected static function getPossibleBinPaths() {
1035 return array_merge(
1036 array( '/usr/bin', '/usr/local/bin', '/opt/csw/bin',
1037 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ),
1038 explode( PATH_SEPARATOR, getenv( 'PATH' ) )
1043 * Search a path for any of the given executable names. Returns the
1044 * executable name if found. Also checks the version string returned
1045 * by each executable.
1047 * Used only by environment checks.
1049 * @param $path String: path to search
1050 * @param $names Array of executable names
1051 * @param $versionInfo Boolean false or array with two members:
1052 * 0 => Command to run for version check, with $1 for the full executable name
1053 * 1 => String to compare the output with
1055 * If $versionInfo is not false, only executables with a version
1056 * matching $versionInfo[1] will be returned.
1058 public static function locateExecutable( $path, $names, $versionInfo = false ) {
1059 if ( !is_array( $names ) ) {
1060 $names = array( $names );
1063 foreach ( $names as $name ) {
1064 $command = $path . DIRECTORY_SEPARATOR . $name;
1066 wfSuppressWarnings();
1067 $file_exists = file_exists( $command );
1068 wfRestoreWarnings();
1070 if ( $file_exists ) {
1071 if ( !$versionInfo ) {
1072 return $command;
1075 $file = str_replace( '$1', wfEscapeShellArg( $command ), $versionInfo[0] );
1076 if ( strstr( wfShellExec( $file ), $versionInfo[1] ) !== false ) {
1077 return $command;
1081 return false;
1085 * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
1086 * @see locateExecutable()
1088 public static function locateExecutableInDefaultPaths( $names, $versionInfo = false ) {
1089 foreach( self::getPossibleBinPaths() as $path ) {
1090 $exe = self::locateExecutable( $path, $names, $versionInfo );
1091 if( $exe !== false ) {
1092 return $exe;
1095 return false;
1099 * Checks if scripts located in the given directory can be executed via the given URL.
1101 * Used only by environment checks.
1103 public function dirIsExecutable( $dir, $url ) {
1104 $scriptTypes = array(
1105 'php' => array(
1106 "<?php echo 'ex' . 'ec';",
1107 "#!/var/env php5\n<?php echo 'ex' . 'ec';",
1111 // it would be good to check other popular languages here, but it'll be slow.
1113 wfSuppressWarnings();
1115 foreach ( $scriptTypes as $ext => $contents ) {
1116 foreach ( $contents as $source ) {
1117 $file = 'exectest.' . $ext;
1119 if ( !file_put_contents( $dir . $file, $source ) ) {
1120 break;
1123 $text = Http::get( $url . $file, array( 'timeout' => 3 ) );
1124 unlink( $dir . $file );
1126 if ( $text == 'exec' ) {
1127 wfRestoreWarnings();
1128 return $ext;
1133 wfRestoreWarnings();
1135 return false;
1139 * ParserOptions are constructed before we determined the language, so fix it
1141 public function setParserLanguage( $lang ) {
1142 $this->parserOptions->setTargetLanguage( $lang );
1143 $this->parserOptions->setUserLang( $lang->getCode() );
1147 * Overridden by WebInstaller to provide lastPage parameters.
1149 protected function getDocUrl( $page ) {
1150 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1154 * Finds extensions that follow the format /extensions/Name/Name.php,
1155 * and returns an array containing the value for 'Name' for each found extension.
1157 * @return array
1159 public function findExtensions() {
1160 if( $this->getVar( 'IP' ) === null ) {
1161 return false;
1164 $exts = array();
1165 $dir = $this->getVar( 'IP' ) . '/extensions';
1166 $dh = opendir( $dir );
1168 while ( ( $file = readdir( $dh ) ) !== false ) {
1169 if( file_exists( "$dir/$file/$file.php" ) ) {
1170 $exts[] = $file;
1174 return $exts;
1178 * Installs the auto-detected extensions.
1180 * @return Status
1182 protected function includeExtensions() {
1183 global $IP;
1184 $exts = $this->getVar( '_Extensions' );
1185 $IP = $this->getVar( 'IP' );
1186 $path = $IP . '/extensions';
1189 * We need to include DefaultSettings before including extensions to avoid
1190 * warnings about unset variables. However, the only thing we really
1191 * want here is $wgHooks['LoadExtensionSchemaUpdates']. This won't work
1192 * if the extension has hidden hook registration in $wgExtensionFunctions,
1193 * but we're not opening that can of worms
1194 * @see https://bugzilla.wikimedia.org/show_bug.cgi?id=26857
1196 global $wgAutoloadClasses;
1197 $wgAutoloadClasses = array();
1199 require( "$IP/includes/DefaultSettings.php" );
1201 foreach( $exts as $e ) {
1202 require_once( "$path/$e/$e.php" );
1205 $hooksWeWant = isset( $wgHooks['LoadExtensionSchemaUpdates'] ) ?
1206 $wgHooks['LoadExtensionSchemaUpdates'] : array();
1208 // Unset everyone else's hooks. Lord knows what someone might be doing
1209 // in ParserFirstCallInit (see bug 27171)
1210 $GLOBALS['wgHooks'] = array( 'LoadExtensionSchemaUpdates' => $hooksWeWant );
1212 return Status::newGood();
1216 * Get an array of install steps. Should always be in the format of
1217 * array(
1218 * 'name' => 'someuniquename',
1219 * 'callback' => array( $obj, 'method' ),
1221 * There must be a config-install-$name message defined per step, which will
1222 * be shown on install.
1224 * @param $installer DatabaseInstaller so we can make callbacks
1225 * @return array
1227 protected function getInstallSteps( DatabaseInstaller $installer ) {
1228 $coreInstallSteps = array(
1229 array( 'name' => 'database', 'callback' => array( $installer, 'setupDatabase' ) ),
1230 array( 'name' => 'tables', 'callback' => array( $installer, 'createTables' ) ),
1231 array( 'name' => 'interwiki', 'callback' => array( $installer, 'populateInterwikiTable' ) ),
1232 array( 'name' => 'stats', 'callback' => array( $this, 'populateSiteStats' ) ),
1233 array( 'name' => 'keys', 'callback' => array( $this, 'generateKeys' ) ),
1234 array( 'name' => 'sysop', 'callback' => array( $this, 'createSysop' ) ),
1235 array( 'name' => 'mainpage', 'callback' => array( $this, 'createMainpage' ) ),
1238 // Build the array of install steps starting from the core install list,
1239 // then adding any callbacks that wanted to attach after a given step
1240 foreach( $coreInstallSteps as $step ) {
1241 $this->installSteps[] = $step;
1242 if( isset( $this->extraInstallSteps[ $step['name'] ] ) ) {
1243 $this->installSteps = array_merge(
1244 $this->installSteps,
1245 $this->extraInstallSteps[ $step['name'] ]
1250 // Prepend any steps that want to be at the beginning
1251 if( isset( $this->extraInstallSteps['BEGINNING'] ) ) {
1252 $this->installSteps = array_merge(
1253 $this->extraInstallSteps['BEGINNING'],
1254 $this->installSteps
1258 // Extensions should always go first, chance to tie into hooks and such
1259 if( count( $this->getVar( '_Extensions' ) ) ) {
1260 array_unshift( $this->installSteps,
1261 array( 'name' => 'extensions', 'callback' => array( $this, 'includeExtensions' ) )
1263 $this->installSteps[] = array(
1264 'name' => 'extension-tables',
1265 'callback' => array( $installer, 'createExtensionTables' )
1268 return $this->installSteps;
1272 * Actually perform the installation.
1274 * @param $startCB Array A callback array for the beginning of each step
1275 * @param $endCB Array A callback array for the end of each step
1277 * @return Array of Status objects
1279 public function performInstallation( $startCB, $endCB ) {
1280 $installResults = array();
1281 $installer = $this->getDBInstaller();
1282 $installer->preInstall();
1283 $steps = $this->getInstallSteps( $installer );
1284 foreach( $steps as $stepObj ) {
1285 $name = $stepObj['name'];
1286 call_user_func_array( $startCB, array( $name ) );
1288 // Perform the callback step
1289 $status = call_user_func( $stepObj['callback'], $installer );
1291 // Output and save the results
1292 call_user_func( $endCB, $name, $status );
1293 $installResults[$name] = $status;
1295 // If we've hit some sort of fatal, we need to bail.
1296 // Callback already had a chance to do output above.
1297 if( !$status->isOk() ) {
1298 break;
1301 if( $status->isOk() ) {
1302 $this->setVar( '_InstallDone', true );
1304 return $installResults;
1308 * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of
1309 * /dev/urandom
1311 * @return Status
1313 public function generateKeys() {
1314 $keys = array( 'wgSecretKey' => 64 );
1315 if ( strval( $this->getVar( 'wgUpgradeKey' ) ) === '' ) {
1316 $keys['wgUpgradeKey'] = 16;
1318 return $this->doGenerateKeys( $keys );
1322 * Generate a secret value for variables using either
1323 * /dev/urandom or mt_rand(). Produce a warning in the later case.
1325 * @param $keys Array
1326 * @return Status
1328 protected function doGenerateKeys( $keys ) {
1329 $status = Status::newGood();
1331 wfSuppressWarnings();
1332 $file = fopen( "/dev/urandom", "r" );
1333 wfRestoreWarnings();
1335 foreach ( $keys as $name => $length ) {
1336 if ( $file ) {
1337 $secretKey = bin2hex( fread( $file, $length / 2 ) );
1338 } else {
1339 $secretKey = '';
1341 for ( $i = 0; $i < $length / 8; $i++ ) {
1342 $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
1346 $this->setVar( $name, $secretKey );
1349 if ( $file ) {
1350 fclose( $file );
1351 } else {
1352 $names = array_keys ( $keys );
1353 $names = preg_replace( '/^(.*)$/', '\$$1', $names );
1354 global $wgLang;
1355 $status->warning( 'config-insecure-keys', $wgLang->listToText( $names ), count( $names ) );
1358 return $status;
1362 * Create the first user account, grant it sysop and bureaucrat rights
1364 * @return Status
1366 protected function createSysop() {
1367 $name = $this->getVar( '_AdminName' );
1368 $user = User::newFromName( $name );
1370 if ( !$user ) {
1371 // We should've validated this earlier anyway!
1372 return Status::newFatal( 'config-admin-error-user', $name );
1375 if ( $user->idForName() == 0 ) {
1376 $user->addToDatabase();
1378 try {
1379 $user->setPassword( $this->getVar( '_AdminPassword' ) );
1380 } catch( PasswordError $pwe ) {
1381 return Status::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
1384 $user->addGroup( 'sysop' );
1385 $user->addGroup( 'bureaucrat' );
1386 if( $this->getVar( '_AdminEmail' ) ) {
1387 $user->setEmail( $this->getVar( '_AdminEmail' ) );
1389 $user->saveSettings();
1391 // Update user count
1392 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
1393 $ssUpdate->doUpdate();
1395 $status = Status::newGood();
1397 if( $this->getVar( '_Subscribe' ) && $this->getVar( '_AdminEmail' ) ) {
1398 $this->subscribeToMediaWikiAnnounce( $status );
1401 return $status;
1404 private function subscribeToMediaWikiAnnounce( Status $s ) {
1405 $params = array(
1406 'email' => $this->getVar( '_AdminEmail' ),
1407 'language' => 'en',
1408 'digest' => 0
1411 // Mailman doesn't support as many languages as we do, so check to make
1412 // sure their selected language is available
1413 $myLang = $this->getVar( '_UserLang' );
1414 if( in_array( $myLang, $this->mediaWikiAnnounceLanguages ) ) {
1415 $myLang = $myLang == 'pt-br' ? 'pt_BR' : $myLang; // rewrite to Mailman's pt_BR
1416 $params['language'] = $myLang;
1419 $res = Http::post( $this->mediaWikiAnnounceUrl, array( 'postData' => $params ) );
1420 if( !$res ) {
1421 $s->warning( 'config-install-subscribe-fail' );
1426 * Insert Main Page with default content.
1428 * @return Status
1430 protected function createMainpage( DatabaseInstaller $installer ) {
1431 $status = Status::newGood();
1432 try {
1433 $article = new Article( Title::newMainPage() );
1434 $article->doEdit( wfMsgForContent( 'mainpagetext' ) . "\n\n" .
1435 wfMsgForContent( 'mainpagedocfooter' ),
1437 EDIT_NEW,
1438 false,
1439 User::newFromName( 'MediaWiki default' ) );
1440 } catch (MWException $e) {
1441 //using raw, because $wgShowExceptionDetails can not be set yet
1442 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
1445 return $status;
1449 * Override the necessary bits of the config to run an installation.
1451 public static function overrideConfig() {
1452 define( 'MW_NO_SESSION', 1 );
1454 // Don't access the database
1455 $GLOBALS['wgUseDatabaseMessages'] = false;
1456 // Debug-friendly
1457 $GLOBALS['wgShowExceptionDetails'] = true;
1458 // Don't break forms
1459 $GLOBALS['wgExternalLinkTarget'] = '_blank';
1461 // Extended debugging
1462 $GLOBALS['wgShowSQLErrors'] = true;
1463 $GLOBALS['wgShowDBErrorBacktrace'] = true;
1465 // Allow multiple ob_flush() calls
1466 $GLOBALS['wgDisableOutputCompression'] = true;
1468 // Use a sensible cookie prefix (not my_wiki)
1469 $GLOBALS['wgCookiePrefix'] = 'mw_installer';
1471 // Some of the environment checks make shell requests, remove limits
1472 $GLOBALS['wgMaxShellMemory'] = 0;
1476 * Add an installation step following the given step.
1478 * @param $callback Array A valid installation callback array, in this form:
1479 * array( 'name' => 'some-unique-name', 'callback' => array( $obj, 'function' ) );
1480 * @param $findStep String the step to find. Omit to put the step at the beginning
1482 public function addInstallStep( $callback, $findStep = 'BEGINNING' ) {
1483 $this->extraInstallSteps[$findStep][] = $callback;