3 * Base code for MediaWiki installer.
10 * This documentation group collects source code files with deployment functionality.
12 * @defgroup Deployment Deployment
16 * Base installer class.
18 * This class provides the base for installation and update functionality
19 * for both MediaWiki core and extensions.
24 abstract class Installer
{
26 // This is the absolute minimum PHP version we can support
27 const MINIMUM_PHP_VERSION
= '5.2.3';
35 * Cached DB installer instances, access using getDBInstaller().
39 protected $dbInstallers = array();
42 * Minimum memory size in MB.
46 protected $minMemorySize = 50;
49 * Cached Title, used by parse().
53 protected $parserTitle;
56 * Cached ParserOptions, used by parse().
60 protected $parserOptions;
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.
71 protected static $dbTypes = array(
80 * A list of environment check methods called by doEnvironmentChecks().
81 * These may output warnings using showMessage(), and/or abort the
82 * installation process by returning false.
86 protected $envChecks = array(
88 'envCheckRegisterGlobals',
91 'envCheckMagicQuotes',
92 'envCheckMagicSybase',
100 'envCheckModSecurity',
106 'envCheckShellLocale',
107 'envCheckUploadsDirectory',
109 'envCheckSuhosinMaxValueLength',
113 * MediaWiki configuration globals that will eventually be passed through
114 * to LocalSettings.php. The names only are given here, the defaults
115 * typically come from DefaultSettings.php.
119 protected $defaultVarNames = array(
131 'wgEmailAuthentication',
134 'wgImageMagickConvertCommand',
140 'wgDeletedDirectory',
145 'wgUseInstantCommons',
148 'wgResourceLoaderMaxQueryLength',
152 * Variables that are stored alongside globals, and are used for any
153 * configuration of the installation process aside from the MediaWiki
154 * configuration. Map of names to defaults.
158 protected $internalDefaults = array(
160 '_Environment' => false,
161 '_CompiledDBs' => array(),
162 '_SafeMode' => false,
163 '_RaiseMemory' => false,
164 '_UpgradeDone' => false,
165 '_InstallDone' => false,
166 '_Caches' => array(),
167 '_InstallPassword' => '',
168 '_SameAccount' => true,
169 '_CreateDBAccount' => false,
170 '_NamespaceType' => 'site-name',
171 '_AdminName' => '', // will be set later, when the user selects language
172 '_AdminPassword' => '',
173 '_AdminPassword2' => '',
175 '_Subscribe' => false,
176 '_SkipOptional' => 'continue',
177 '_RightsProfile' => 'wiki',
178 '_LicenseCode' => 'none',
180 '_Extensions' => array(),
181 '_MemCachedServers' => '',
182 '_UpgradeKeySupplied' => false,
183 '_ExistingDBSettings' => false,
187 * The actual list of installation steps. This will be initialized by getInstallSteps()
191 private $installSteps = array();
194 * Extra steps for installation, for things like DatabaseInstallers to modify
198 protected $extraInstallSteps = array();
201 * Known object cache types and the functions used to test for their existence.
205 protected $objectCaches = array(
206 'xcache' => 'xcache_get',
207 'apc' => 'apc_fetch',
208 'eaccel' => 'eaccelerator_get',
209 'wincache' => 'wincache_ucache_get'
213 * User rights profiles.
217 public $rightsProfiles = array(
220 '*' => array( 'edit' => false )
224 'createaccount' => false,
230 'createaccount' => false,
242 public $licenses = array(
244 'url' => 'http://creativecommons.org/licenses/by/3.0/',
245 'icon' => '{$wgStylePath}/common/images/cc-by.png',
248 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
249 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
251 'cc-by-nc-sa' => array(
252 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/',
253 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png',
256 'url' => 'https://creativecommons.org/publicdomain/zero/1.0/',
257 'icon' => '{$wgStylePath}/common/images/cc-0.png',
261 'icon' => '{$wgStylePath}/common/images/public-domain.png',
264 'url' => 'http://www.gnu.org/copyleft/fdl.html',
265 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
272 'cc-choose' => array(
273 // Details will be filled in by the selector.
281 * URL to mediawiki-announce subscription
283 protected $mediaWikiAnnounceUrl = 'https://lists.wikimedia.org/mailman/subscribe/mediawiki-announce';
286 * Supported language codes for Mailman
288 protected $mediaWikiAnnounceLanguages = array(
289 'ca', 'cs', 'da', 'de', 'en', 'es', 'et', 'eu', 'fi', 'fr', 'hr', 'hu',
290 'it', 'ja', 'ko', 'lt', 'nl', 'no', 'pl', 'pt', 'pt-br', 'ro', 'ru',
291 'sl', 'sr', 'sv', 'tr', 'uk'
295 * UI interface for displaying a short message
296 * The parameters are like parameters to wfMsg().
297 * The messages will be in wikitext format, which will be converted to an
298 * output format such as HTML or text before being sent to the user.
300 public abstract function showMessage( $msg /*, ... */ );
303 * Same as showMessage(), but for displaying errors
305 public abstract function showError( $msg /*, ... */ );
308 * Show a message to the installing user by using a Status object
309 * @param $status Status
311 public abstract function showStatusMessage( Status
$status );
314 * Constructor, always call this from child classes.
316 public function __construct() {
317 global $wgExtensionMessagesFiles, $wgUser;
319 // Disable the i18n cache and LoadBalancer
320 Language
::getLocalisationCache()->disableBackend();
321 LBFactory
::disableBackend();
323 // Load the installer's i18n file.
324 $wgExtensionMessagesFiles['MediawikiInstaller'] =
325 dirname( __FILE__
) . '/Installer.i18n.php';
327 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
328 $wgUser = User
::newFromId( 0 );
330 $this->settings
= $this->internalDefaults
;
332 foreach ( $this->defaultVarNames
as $var ) {
333 $this->settings
[$var] = $GLOBALS[$var];
336 foreach ( self
::getDBTypes() as $type ) {
337 $installer = $this->getDBInstaller( $type );
339 if ( !$installer->isCompiled() ) {
343 $defaults = $installer->getGlobalDefaults();
345 foreach ( $installer->getGlobalNames() as $var ) {
346 if ( isset( $defaults[$var] ) ) {
347 $this->settings
[$var] = $defaults[$var];
349 $this->settings
[$var] = $GLOBALS[$var];
354 $this->parserTitle
= Title
::newFromText( 'Installer' );
355 $this->parserOptions
= new ParserOptions
; // language will be wrong :(
356 $this->parserOptions
->setEditSection( false );
360 * Get a list of known DB types.
364 public static function getDBTypes() {
365 return self
::$dbTypes;
369 * Do initial checks of the PHP environment. Set variables according to
370 * the observed environment.
372 * It's possible that this may be called under the CLI SAPI, not the SAPI
373 * that the wiki will primarily run under. In that case, the subclass should
374 * initialise variables such as wgScriptPath, before calling this function.
376 * Under the web subclass, it can already be assumed that PHP 5+ is in use
377 * and that sessions are working.
381 public function doEnvironmentChecks() {
382 $phpVersion = phpversion();
383 if( version_compare( $phpVersion, self
::MINIMUM_PHP_VERSION
, '>=' ) ) {
384 $this->showMessage( 'config-env-php', $phpVersion );
387 $this->showMessage( 'config-env-php-toolow', $phpVersion, self
::MINIMUM_PHP_VERSION
);
392 foreach ( $this->envChecks
as $check ) {
393 $status = $this->$check();
394 if ( $status === false ) {
400 $this->setVar( '_Environment', $good );
402 return $good ? Status
::newGood() : Status
::newFatal( 'config-env-bad' );
406 * Set a MW configuration variable, or internal installer configuration variable.
408 * @param $name String
409 * @param $value Mixed
411 public function setVar( $name, $value ) {
412 $this->settings
[$name] = $value;
416 * Get an MW configuration variable, or internal installer configuration variable.
417 * The defaults come from $GLOBALS (ultimately DefaultSettings.php).
418 * Installer variables are typically prefixed by an underscore.
420 * @param $name String
421 * @param $default Mixed
425 public function getVar( $name, $default = null ) {
426 if ( !isset( $this->settings
[$name] ) ) {
429 return $this->settings
[$name];
434 * Get an instance of DatabaseInstaller for the specified DB type.
436 * @param $type Mixed: DB installer for which is needed, false to use default.
438 * @return DatabaseInstaller
440 public function getDBInstaller( $type = false ) {
442 $type = $this->getVar( 'wgDBtype' );
445 $type = strtolower( $type );
447 if ( !isset( $this->dbInstallers
[$type] ) ) {
448 $class = ucfirst( $type ). 'Installer';
449 $this->dbInstallers
[$type] = new $class( $this );
452 return $this->dbInstallers
[$type];
456 * Determine if LocalSettings.php exists. If it does, return its variables,
457 * merged with those from AdminSettings.php, as an array.
461 public static function getExistingLocalSettings() {
464 wfSuppressWarnings();
465 $_lsExists = file_exists( "$IP/LocalSettings.php" );
473 require( "$IP/includes/DefaultSettings.php" );
474 require( "$IP/LocalSettings.php" );
475 if ( file_exists( "$IP/AdminSettings.php" ) ) {
476 require( "$IP/AdminSettings.php" );
478 return get_defined_vars();
482 * Get a fake password for sending back to the user in HTML.
483 * This is a security mechanism to avoid compromise of the password in the
484 * event of session ID compromise.
486 * @param $realPassword String
490 public function getFakePassword( $realPassword ) {
491 return str_repeat( '*', strlen( $realPassword ) );
495 * Set a variable which stores a password, except if the new value is a
496 * fake password in which case leave it as it is.
498 * @param $name String
499 * @param $value Mixed
501 public function setPassword( $name, $value ) {
502 if ( !preg_match( '/^\*+$/', $value ) ) {
503 $this->setVar( $name, $value );
508 * On POSIX systems return the primary group of the webserver we're running under.
509 * On other systems just returns null.
511 * This is used to advice the user that he should chgrp his mw-config/data/images directory as the
512 * webserver user before he can install.
514 * Public because SqliteInstaller needs it, and doesn't subclass Installer.
518 public static function maybeGetWebserverPrimaryGroup() {
519 if ( !function_exists( 'posix_getegid' ) ||
!function_exists( 'posix_getpwuid' ) ) {
520 # I don't know this, this isn't UNIX.
524 # posix_getegid() *not* getmygid() because we want the group of the webserver,
525 # not whoever owns the current script.
526 $gid = posix_getegid();
527 $getpwuid = posix_getpwuid( $gid );
528 $group = $getpwuid['name'];
534 * Convert wikitext $text to HTML.
536 * This is potentially error prone since many parser features require a complete
537 * installed MW database. The solution is to just not use those features when you
538 * write your messages. This appears to work well enough. Basic formatting and
539 * external links work just fine.
541 * But in case a translator decides to throw in a #ifexist or internal link or
542 * whatever, this function is guarded to catch the attempted DB access and to present
543 * some fallback text.
545 * @param $text String
546 * @param $lineStart Boolean
549 public function parse( $text, $lineStart = false ) {
553 $out = $wgParser->parse( $text, $this->parserTitle
, $this->parserOptions
, $lineStart );
554 $html = $out->getText();
555 } catch ( DBAccessError
$e ) {
556 $html = '<!--DB access attempted during parse--> ' . htmlspecialchars( $text );
558 if ( !empty( $this->debug
) ) {
559 $html .= "<!--\n" . $e->getTraceAsString() . "\n-->";
567 * @return ParserOptions
569 public function getParserOptions() {
570 return $this->parserOptions
;
573 public function disableLinkPopups() {
574 $this->parserOptions
->setExternalLinkTarget( false );
577 public function restoreLinkPopups() {
578 global $wgExternalLinkTarget;
579 $this->parserOptions
->setExternalLinkTarget( $wgExternalLinkTarget );
583 * Install step which adds a row to the site_stats table with appropriate
586 * @param $installer DatabaseInstaller
590 public function populateSiteStats( DatabaseInstaller
$installer ) {
591 $status = $installer->getConnection();
592 if ( !$status->isOK() ) {
595 $status->value
->insert( 'site_stats', array(
597 'ss_total_views' => 0,
598 'ss_total_edits' => 0,
599 'ss_good_articles' => 0,
600 'ss_total_pages' => 0,
604 __METHOD__
, 'IGNORE' );
605 return Status
::newGood();
609 * Exports all wg* variables stored by the installer into global scope.
611 public function exportVars() {
612 foreach ( $this->settings
as $name => $value ) {
613 if ( substr( $name, 0, 2 ) == 'wg' ) {
614 $GLOBALS[$name] = $value;
620 * Environment check for DB types.
622 protected function envCheckDB() {
625 $compiledDBs = array();
628 foreach ( self
::getDBTypes() as $name ) {
629 if ( $this->getDBInstaller( $name )->isCompiled() ) {
630 $compiledDBs[] = $name;
632 $allNames[] = wfMsg( 'config-type-' . $name );
635 $this->setVar( '_CompiledDBs', $compiledDBs );
637 if ( !$compiledDBs ) {
638 $this->showError( 'config-no-db', $wgLang->commaList( $allNames ) );
639 // @todo FIXME: This only works for the web installer!
643 // Check for FTS3 full-text search module
644 $sqlite = $this->getDBInstaller( 'sqlite' );
645 if ( $sqlite->isCompiled() ) {
646 if( DatabaseSqlite
::getFulltextSearchModule() != 'FTS3' ) {
647 $this->showMessage( 'config-no-fts3' );
653 * Environment check for register_globals.
655 protected function envCheckRegisterGlobals() {
656 if( wfIniGetBool( 'register_globals' ) ) {
657 $this->showMessage( 'config-register-globals' );
662 * Some versions of libxml+PHP break < and > encoding horribly
664 protected function envCheckBrokenXML() {
665 $test = new PhpXmlBugTester();
667 $this->showError( 'config-brokenlibxml' );
673 * Test PHP (probably 5.3.1, but it could regress again) to make sure that
674 * reference parameters to __call() are not converted to null
676 protected function envCheckPHP531() {
677 $test = new PhpRefCallBugTester
;
680 $this->showError( 'config-using531', phpversion() );
686 * Environment check for magic_quotes_runtime.
688 protected function envCheckMagicQuotes() {
689 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
690 $this->showError( 'config-magic-quotes-runtime' );
696 * Environment check for magic_quotes_sybase.
698 protected function envCheckMagicSybase() {
699 if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
700 $this->showError( 'config-magic-quotes-sybase' );
706 * Environment check for mbstring.func_overload.
708 protected function envCheckMbstring() {
709 if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
710 $this->showError( 'config-mbstring' );
716 * Environment check for zend.ze1_compatibility_mode.
718 protected function envCheckZE1() {
719 if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
720 $this->showError( 'config-ze1' );
726 * Environment check for safe_mode.
728 protected function envCheckSafeMode() {
729 if ( wfIniGetBool( 'safe_mode' ) ) {
730 $this->setVar( '_SafeMode', true );
731 $this->showMessage( 'config-safe-mode' );
736 * Environment check for the XML module.
738 protected function envCheckXML() {
739 if ( !function_exists( "utf8_encode" ) ) {
740 $this->showError( 'config-xml-bad' );
746 * Environment check for the PCRE module.
748 protected function envCheckPCRE() {
749 if ( !function_exists( 'preg_match' ) ) {
750 $this->showError( 'config-pcre' );
753 wfSuppressWarnings();
754 $regexd = preg_replace( '/[\x{0430}-\x{04FF}]/iu', '', '-АБВГД-' );
756 if ( $regexd != '--' ) {
757 $this->showError( 'config-pcre-no-utf8' );
763 * Environment check for available memory.
765 protected function envCheckMemory() {
766 $limit = ini_get( 'memory_limit' );
768 if ( !$limit ||
$limit == -1 ) {
772 $n = wfShorthandToInteger( $limit );
774 if( $n < $this->minMemorySize
* 1024 * 1024 ) {
775 $newLimit = "{$this->minMemorySize}M";
777 if( ini_set( "memory_limit", $newLimit ) === false ) {
778 $this->showMessage( 'config-memory-bad', $limit );
780 $this->showMessage( 'config-memory-raised', $limit, $newLimit );
781 $this->setVar( '_RaiseMemory', true );
789 * Environment check for compiled object cache types.
791 protected function envCheckCache() {
793 foreach ( $this->objectCaches
as $name => $function ) {
794 if ( function_exists( $function ) ) {
795 if ( $name == 'xcache' && !wfIniGetBool( 'xcache.var_size' ) ) {
798 $caches[$name] = true;
803 $this->showMessage( 'config-no-cache' );
806 $this->setVar( '_Caches', $caches );
810 * Scare user to death if they have mod_security
812 protected function envCheckModSecurity() {
813 if ( !self
::apacheModulePresent( 'mod_security' ) ) {
814 $this->showMessage( 'config-mod-security' );
819 * Search for GNU diff3.
821 protected function envCheckDiff3() {
822 $names = array( "gdiff3", "diff3", "diff3.exe" );
823 $versionInfo = array( '$1 --version 2>&1', 'GNU diffutils' );
825 $diff3 = self
::locateExecutableInDefaultPaths( $names, $versionInfo );
828 $this->setVar( 'wgDiff3', $diff3 );
830 $this->setVar( 'wgDiff3', false );
831 $this->showMessage( 'config-diff3-bad' );
836 * Environment check for ImageMagick and GD.
838 protected function envCheckGraphics() {
839 $names = array( wfIsWindows() ?
'convert.exe' : 'convert' );
840 $convert = self
::locateExecutableInDefaultPaths( $names, array( '$1 -version', 'ImageMagick' ) );
842 $this->setVar( 'wgImageMagickConvertCommand', '' );
844 $this->setVar( 'wgImageMagickConvertCommand', $convert );
845 $this->showMessage( 'config-imagemagick', $convert );
847 } elseif ( function_exists( 'imagejpeg' ) ) {
848 $this->showMessage( 'config-gd' );
851 $this->showMessage( 'config-no-scaling' );
856 * Environment check for the server hostname.
858 protected function envCheckServer() {
859 $server = WebRequest
::detectServer();
860 $this->showMessage( 'config-using-server', $server );
861 $this->setVar( 'wgServer', $server );
865 * Environment check for setting $IP and $wgScriptPath.
867 protected function envCheckPath() {
869 $IP = dirname( dirname( dirname( __FILE__
) ) );
870 $this->setVar( 'IP', $IP );
872 $this->showMessage( 'config-using-uri', $this->getVar( 'wgServer' ), $this->getVar( 'wgScriptPath' ) );
877 * Environment check for setting the preferred PHP file extension.
879 protected function envCheckExtension() {
880 // @todo FIXME: Detect this properly
881 if ( defined( 'MW_INSTALL_PHP5_EXT' ) ) {
886 $this->setVar( 'wgScriptExtension', ".$ext" );
892 protected function envCheckShellLocale() {
893 $os = php_uname( 's' );
894 $supported = array( 'Linux', 'SunOS', 'HP-UX', 'Darwin' ); # Tested these
896 if ( !in_array( $os, $supported ) ) {
900 # Get a list of available locales.
902 $lines = wfShellExec( '/usr/bin/locale -a', $ret );
908 $lines = wfArrayMap( 'trim', explode( "\n", $lines ) );
909 $candidatesByLocale = array();
910 $candidatesByLang = array();
912 foreach ( $lines as $line ) {
913 if ( $line === '' ) {
917 if ( !preg_match( '/^([a-zA-Z]+)(_[a-zA-Z]+|)\.(utf8|UTF-8)(@[a-zA-Z_]*|)$/i', $line, $m ) ) {
921 list( $all, $lang, $territory, $charset, $modifier ) = $m;
923 $candidatesByLocale[$m[0]] = $m;
924 $candidatesByLang[$lang][] = $m;
927 # Try the current value of LANG.
928 if ( isset( $candidatesByLocale[ getenv( 'LANG' ) ] ) ) {
929 $this->setVar( 'wgShellLocale', getenv( 'LANG' ) );
933 # Try the most common ones.
934 $commonLocales = array( 'en_US.UTF-8', 'en_US.utf8', 'de_DE.UTF-8', 'de_DE.utf8' );
935 foreach ( $commonLocales as $commonLocale ) {
936 if ( isset( $candidatesByLocale[$commonLocale] ) ) {
937 $this->setVar( 'wgShellLocale', $commonLocale );
942 # Is there an available locale in the Wiki's language?
943 $wikiLang = $this->getVar( 'wgLanguageCode' );
945 if ( isset( $candidatesByLang[$wikiLang] ) ) {
946 $m = reset( $candidatesByLang[$wikiLang] );
947 $this->setVar( 'wgShellLocale', $m[0] );
951 # Are there any at all?
952 if ( count( $candidatesByLocale ) ) {
953 $m = reset( $candidatesByLocale );
954 $this->setVar( 'wgShellLocale', $m[0] );
965 protected function envCheckUploadsDirectory() {
968 $dir = $IP . '/images/';
969 $url = $this->getVar( 'wgServer' ) . $this->getVar( 'wgScriptPath' ) . '/images/';
970 $safe = !$this->dirIsExecutable( $dir, $url );
975 $this->showMessage( 'config-uploads-not-safe', $dir );
980 * Checks if suhosin.get.max_value_length is set, and if so, sets
981 * $wgResourceLoaderMaxQueryLength to that value in the generated
984 protected function envCheckSuhosinMaxValueLength() {
985 $maxValueLength = ini_get( 'suhosin.get.max_value_length' );
986 if ( $maxValueLength > 0 ) {
987 $this->showMessage( 'config-suhosin-max-value-length', $maxValueLength );
989 $maxValueLength = -1;
991 $this->setVar( 'wgResourceLoaderMaxQueryLength', $maxValueLength );
995 * Convert a hex string representing a Unicode code point to that code point.
999 protected function unicodeChar( $c ) {
1003 } elseif ($c <= 0x7FF) {
1004 return chr(0xC0 |
$c >> 6) . chr(0x80 |
$c & 0x3F);
1005 } elseif ($c <= 0xFFFF) {
1006 return chr(0xE0 |
$c >> 12) . chr(0x80 |
$c >> 6 & 0x3F)
1007 . chr(0x80 |
$c & 0x3F);
1008 } elseif ($c <= 0x10FFFF) {
1009 return chr(0xF0 |
$c >> 18) . chr(0x80 |
$c >> 12 & 0x3F)
1010 . chr(0x80 |
$c >> 6 & 0x3F)
1011 . chr(0x80 |
$c & 0x3F);
1019 * Check the libicu version
1021 protected function envCheckLibicu() {
1022 $utf8 = function_exists( 'utf8_normalize' );
1023 $intl = function_exists( 'normalizer_normalize' );
1026 * This needs to be updated something that the latest libicu
1027 * will properly normalize. This normalization was found at
1028 * http://www.unicode.org/versions/Unicode5.2.0/#Character_Additions
1029 * Note that we use the hex representation to create the code
1030 * points in order to avoid any Unicode-destroying during transit.
1032 $not_normal_c = $this->unicodeChar("FA6C");
1033 $normal_c = $this->unicodeChar("242EE");
1035 $useNormalizer = 'php';
1036 $needsUpdate = false;
1039 * We're going to prefer the pecl extension here unless
1040 * utf8_normalize is more up to date.
1043 $useNormalizer = 'utf8';
1044 $utf8 = utf8_normalize( $not_normal_c, UtfNormal
::UNORM_NFC
);
1045 if ( $utf8 !== $normal_c ) $needsUpdate = true;
1048 $useNormalizer = 'intl';
1049 $intl = normalizer_normalize( $not_normal_c, Normalizer
::FORM_C
);
1050 if ( $intl !== $normal_c ) $needsUpdate = true;
1053 // Uses messages 'config-unicode-using-php', 'config-unicode-using-utf8', 'config-unicode-using-intl'
1054 if( $useNormalizer === 'php' ) {
1055 $this->showMessage( 'config-unicode-pure-php-warning' );
1057 $this->showMessage( 'config-unicode-using-' . $useNormalizer );
1058 if( $needsUpdate ) {
1059 $this->showMessage( 'config-unicode-update-warning' );
1065 * Get an array of likely places we can find executables. Check a bunch
1066 * of known Unix-like defaults, as well as the PATH environment variable
1067 * (which should maybe make it work for Windows?)
1071 protected static function getPossibleBinPaths() {
1073 array( '/usr/bin', '/usr/local/bin', '/opt/csw/bin',
1074 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ),
1075 explode( PATH_SEPARATOR
, getenv( 'PATH' ) )
1080 * Search a path for any of the given executable names. Returns the
1081 * executable name if found. Also checks the version string returned
1082 * by each executable.
1084 * Used only by environment checks.
1086 * @param $path String: path to search
1087 * @param $names Array of executable names
1088 * @param $versionInfo Boolean false or array with two members:
1089 * 0 => Command to run for version check, with $1 for the full executable name
1090 * 1 => String to compare the output with
1092 * If $versionInfo is not false, only executables with a version
1093 * matching $versionInfo[1] will be returned.
1095 public static function locateExecutable( $path, $names, $versionInfo = false ) {
1096 if ( !is_array( $names ) ) {
1097 $names = array( $names );
1100 foreach ( $names as $name ) {
1101 $command = $path . DIRECTORY_SEPARATOR
. $name;
1103 wfSuppressWarnings();
1104 $file_exists = file_exists( $command );
1105 wfRestoreWarnings();
1107 if ( $file_exists ) {
1108 if ( !$versionInfo ) {
1112 $file = str_replace( '$1', wfEscapeShellArg( $command ), $versionInfo[0] );
1113 if ( strstr( wfShellExec( $file ), $versionInfo[1] ) !== false ) {
1122 * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
1123 * @see locateExecutable()
1125 public static function locateExecutableInDefaultPaths( $names, $versionInfo = false ) {
1126 foreach( self
::getPossibleBinPaths() as $path ) {
1127 $exe = self
::locateExecutable( $path, $names, $versionInfo );
1128 if( $exe !== false ) {
1136 * Checks if scripts located in the given directory can be executed via the given URL.
1138 * Used only by environment checks.
1140 public function dirIsExecutable( $dir, $url ) {
1141 $scriptTypes = array(
1143 "<?php echo 'ex' . 'ec';",
1144 "#!/var/env php5\n<?php echo 'ex' . 'ec';",
1148 // it would be good to check other popular languages here, but it'll be slow.
1150 wfSuppressWarnings();
1152 foreach ( $scriptTypes as $ext => $contents ) {
1153 foreach ( $contents as $source ) {
1154 $file = 'exectest.' . $ext;
1156 if ( !file_put_contents( $dir . $file, $source ) ) {
1161 $text = Http
::get( $url . $file, array( 'timeout' => 3 ) );
1163 catch( MWException
$e ) {
1164 // Http::get throws with allow_url_fopen = false and no curl extension.
1167 unlink( $dir . $file );
1169 if ( $text == 'exec' ) {
1170 wfRestoreWarnings();
1176 wfRestoreWarnings();
1182 * Checks for presence of an Apache module. Works only if PHP is running as an Apache module, too.
1184 * @param $moduleName String: Name of module to check.
1187 public static function apacheModulePresent( $moduleName ) {
1188 if ( function_exists( 'apache_get_modules' ) && in_array( $moduleName, apache_get_modules() ) ) {
1191 // try it the hard way
1193 phpinfo( INFO_MODULES
);
1194 $info = ob_get_clean();
1195 return strpos( $info, $moduleName ) !== false;
1199 * ParserOptions are constructed before we determined the language, so fix it
1201 * @param $lang Language
1203 public function setParserLanguage( $lang ) {
1204 $this->parserOptions
->setTargetLanguage( $lang );
1205 $this->parserOptions
->setUserLang( $lang->getCode() );
1209 * Overridden by WebInstaller to provide lastPage parameters.
1211 protected function getDocUrl( $page ) {
1212 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1216 * Finds extensions that follow the format /extensions/Name/Name.php,
1217 * and returns an array containing the value for 'Name' for each found extension.
1221 public function findExtensions() {
1222 if( $this->getVar( 'IP' ) === null ) {
1227 $extDir = $this->getVar( 'IP' ) . '/extensions';
1228 $dh = opendir( $extDir );
1230 while ( ( $file = readdir( $dh ) ) !== false ) {
1231 if( !is_dir( "$extDir/$file" ) ) {
1234 if( file_exists( "$extDir/$file/$file.php" ) ) {
1243 * Installs the auto-detected extensions.
1247 protected function includeExtensions() {
1249 $exts = $this->getVar( '_Extensions' );
1250 $IP = $this->getVar( 'IP' );
1253 * We need to include DefaultSettings before including extensions to avoid
1254 * warnings about unset variables. However, the only thing we really
1255 * want here is $wgHooks['LoadExtensionSchemaUpdates']. This won't work
1256 * if the extension has hidden hook registration in $wgExtensionFunctions,
1257 * but we're not opening that can of worms
1258 * @see https://bugzilla.wikimedia.org/show_bug.cgi?id=26857
1260 global $wgAutoloadClasses;
1261 $wgAutoloadClasses = array();
1263 require( "$IP/includes/DefaultSettings.php" );
1265 foreach( $exts as $e ) {
1266 require_once( "$IP/extensions/$e/$e.php" );
1269 $hooksWeWant = isset( $wgHooks['LoadExtensionSchemaUpdates'] ) ?
1270 $wgHooks['LoadExtensionSchemaUpdates'] : array();
1272 // Unset everyone else's hooks. Lord knows what someone might be doing
1273 // in ParserFirstCallInit (see bug 27171)
1274 $GLOBALS['wgHooks'] = array( 'LoadExtensionSchemaUpdates' => $hooksWeWant );
1276 return Status
::newGood();
1280 * Get an array of install steps. Should always be in the format of
1282 * 'name' => 'someuniquename',
1283 * 'callback' => array( $obj, 'method' ),
1285 * There must be a config-install-$name message defined per step, which will
1286 * be shown on install.
1288 * @param $installer DatabaseInstaller so we can make callbacks
1291 protected function getInstallSteps( DatabaseInstaller
$installer ) {
1292 $coreInstallSteps = array(
1293 array( 'name' => 'database', 'callback' => array( $installer, 'setupDatabase' ) ),
1294 array( 'name' => 'tables', 'callback' => array( $installer, 'createTables' ) ),
1295 array( 'name' => 'interwiki', 'callback' => array( $installer, 'populateInterwikiTable' ) ),
1296 array( 'name' => 'stats', 'callback' => array( $this, 'populateSiteStats' ) ),
1297 array( 'name' => 'keys', 'callback' => array( $this, 'generateKeys' ) ),
1298 array( 'name' => 'sysop', 'callback' => array( $this, 'createSysop' ) ),
1299 array( 'name' => 'mainpage', 'callback' => array( $this, 'createMainpage' ) ),
1302 // Build the array of install steps starting from the core install list,
1303 // then adding any callbacks that wanted to attach after a given step
1304 foreach( $coreInstallSteps as $step ) {
1305 $this->installSteps
[] = $step;
1306 if( isset( $this->extraInstallSteps
[ $step['name'] ] ) ) {
1307 $this->installSteps
= array_merge(
1308 $this->installSteps
,
1309 $this->extraInstallSteps
[ $step['name'] ]
1314 // Prepend any steps that want to be at the beginning
1315 if( isset( $this->extraInstallSteps
['BEGINNING'] ) ) {
1316 $this->installSteps
= array_merge(
1317 $this->extraInstallSteps
['BEGINNING'],
1322 // Extensions should always go first, chance to tie into hooks and such
1323 if( count( $this->getVar( '_Extensions' ) ) ) {
1324 array_unshift( $this->installSteps
,
1325 array( 'name' => 'extensions', 'callback' => array( $this, 'includeExtensions' ) )
1327 $this->installSteps
[] = array(
1328 'name' => 'extension-tables',
1329 'callback' => array( $installer, 'createExtensionTables' )
1332 return $this->installSteps
;
1336 * Actually perform the installation.
1338 * @param $startCB Array A callback array for the beginning of each step
1339 * @param $endCB Array A callback array for the end of each step
1341 * @return Array of Status objects
1343 public function performInstallation( $startCB, $endCB ) {
1344 $installResults = array();
1345 $installer = $this->getDBInstaller();
1346 $installer->preInstall();
1347 $steps = $this->getInstallSteps( $installer );
1348 foreach( $steps as $stepObj ) {
1349 $name = $stepObj['name'];
1350 call_user_func_array( $startCB, array( $name ) );
1352 // Perform the callback step
1353 $status = call_user_func( $stepObj['callback'], $installer );
1355 // Output and save the results
1356 call_user_func( $endCB, $name, $status );
1357 $installResults[$name] = $status;
1359 // If we've hit some sort of fatal, we need to bail.
1360 // Callback already had a chance to do output above.
1361 if( !$status->isOk() ) {
1365 if( $status->isOk() ) {
1366 $this->setVar( '_InstallDone', true );
1368 return $installResults;
1372 * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of
1377 public function generateKeys() {
1378 $keys = array( 'wgSecretKey' => 64 );
1379 if ( strval( $this->getVar( 'wgUpgradeKey' ) ) === '' ) {
1380 $keys['wgUpgradeKey'] = 16;
1382 return $this->doGenerateKeys( $keys );
1386 * Generate a secret value for variables using either
1387 * /dev/urandom or mt_rand(). Produce a warning in the later case.
1389 * @param $keys Array
1392 protected function doGenerateKeys( $keys ) {
1393 $status = Status
::newGood();
1395 wfSuppressWarnings();
1396 $file = fopen( "/dev/urandom", "r" );
1397 wfRestoreWarnings();
1399 foreach ( $keys as $name => $length ) {
1401 $secretKey = bin2hex( fread( $file, $length / 2 ) );
1405 for ( $i = 0; $i < $length / 8; $i++
) {
1406 $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
1410 $this->setVar( $name, $secretKey );
1416 $names = array_keys ( $keys );
1417 $names = preg_replace( '/^(.*)$/', '\$$1', $names );
1419 $status->warning( 'config-insecure-keys', $wgLang->listToText( $names ), count( $names ) );
1426 * Create the first user account, grant it sysop and bureaucrat rights
1430 protected function createSysop() {
1431 $name = $this->getVar( '_AdminName' );
1432 $user = User
::newFromName( $name );
1435 // We should've validated this earlier anyway!
1436 return Status
::newFatal( 'config-admin-error-user', $name );
1439 if ( $user->idForName() == 0 ) {
1440 $user->addToDatabase();
1443 $user->setPassword( $this->getVar( '_AdminPassword' ) );
1444 } catch( PasswordError
$pwe ) {
1445 return Status
::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
1448 $user->addGroup( 'sysop' );
1449 $user->addGroup( 'bureaucrat' );
1450 if( $this->getVar( '_AdminEmail' ) ) {
1451 $user->setEmail( $this->getVar( '_AdminEmail' ) );
1453 $user->saveSettings();
1455 // Update user count
1456 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
1457 $ssUpdate->doUpdate();
1459 $status = Status
::newGood();
1461 if( $this->getVar( '_Subscribe' ) && $this->getVar( '_AdminEmail' ) ) {
1462 $this->subscribeToMediaWikiAnnounce( $status );
1468 private function subscribeToMediaWikiAnnounce( Status
$s ) {
1470 'email' => $this->getVar( '_AdminEmail' ),
1475 // Mailman doesn't support as many languages as we do, so check to make
1476 // sure their selected language is available
1477 $myLang = $this->getVar( '_UserLang' );
1478 if( in_array( $myLang, $this->mediaWikiAnnounceLanguages
) ) {
1479 $myLang = $myLang == 'pt-br' ?
'pt_BR' : $myLang; // rewrite to Mailman's pt_BR
1480 $params['language'] = $myLang;
1483 if( MWHttpRequest
::canMakeRequests() ) {
1484 $res = MWHttpRequest
::factory( $this->mediaWikiAnnounceUrl
,
1485 array( 'method' => 'POST', 'postData' => $params ) )->execute();
1486 if( !$res->isOK() ) {
1487 $s->warning( 'config-install-subscribe-fail', $res->getMessage() );
1490 $s->warning( 'config-install-subscribe-notpossible' );
1495 * Insert Main Page with default content.
1499 protected function createMainpage( DatabaseInstaller
$installer ) {
1500 $status = Status
::newGood();
1502 $article = new Article( Title
::newMainPage() );
1503 $article->doEdit( wfMsgForContent( 'mainpagetext' ) . "\n\n" .
1504 wfMsgForContent( 'mainpagedocfooter' ),
1508 User
::newFromName( 'MediaWiki default' ) );
1509 } catch (MWException
$e) {
1510 //using raw, because $wgShowExceptionDetails can not be set yet
1511 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
1518 * Override the necessary bits of the config to run an installation.
1520 public static function overrideConfig() {
1521 define( 'MW_NO_SESSION', 1 );
1523 // Don't access the database
1524 $GLOBALS['wgUseDatabaseMessages'] = false;
1526 $GLOBALS['wgShowExceptionDetails'] = true;
1527 // Don't break forms
1528 $GLOBALS['wgExternalLinkTarget'] = '_blank';
1530 // Extended debugging
1531 $GLOBALS['wgShowSQLErrors'] = true;
1532 $GLOBALS['wgShowDBErrorBacktrace'] = true;
1534 // Allow multiple ob_flush() calls
1535 $GLOBALS['wgDisableOutputCompression'] = true;
1537 // Use a sensible cookie prefix (not my_wiki)
1538 $GLOBALS['wgCookiePrefix'] = 'mw_installer';
1540 // Some of the environment checks make shell requests, remove limits
1541 $GLOBALS['wgMaxShellMemory'] = 0;
1545 * Add an installation step following the given step.
1547 * @param $callback Array A valid installation callback array, in this form:
1548 * array( 'name' => 'some-unique-name', 'callback' => array( $obj, 'function' ) );
1549 * @param $findStep String the step to find. Omit to put the step at the beginning
1551 public function addInstallStep( $callback, $findStep = 'BEGINNING' ) {
1552 $this->extraInstallSteps
[$findStep][] = $callback;
1556 * Disable the time limit for execution.
1557 * Some long-running pages (Install, Upgrade) will want to do this
1559 protected function disableTimeLimit() {
1560 wfSuppressWarnings();
1561 set_time_limit( 0 );
1562 wfRestoreWarnings();