3 * Base code for MediaWiki installer.
5 * DO NOT PATCH THIS FILE IF YOU NEED TO CHANGE INSTALLER BEHAVIOR IN YOUR PACKAGE!
6 * See mw-config/overrides/README for details.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 use MediaWiki\MediaWikiServices
;
29 * This documentation group collects source code files with deployment functionality.
31 * @defgroup Deployment Deployment
35 * Base installer class.
37 * This class provides the base for installation and update functionality
38 * for both MediaWiki core and extensions.
43 abstract class Installer
{
46 * The oldest version of PCRE we can support.
48 * Defining this is necessary because PHP may be linked with a system version
49 * of PCRE, which may be older than that bundled with the minimum PHP version.
51 const MINIMUM_PCRE_VERSION
= '7.2';
59 * List of detected DBs, access using getCompiledDBs().
63 protected $compiledDBs;
66 * Cached DB installer instances, access using getDBInstaller().
70 protected $dbInstallers = [];
73 * Minimum memory size in MB.
77 protected $minMemorySize = 50;
80 * Cached Title, used by parse().
84 protected $parserTitle;
87 * Cached ParserOptions, used by parse().
91 protected $parserOptions;
94 * Known database types. These correspond to the class names <type>Installer,
95 * and are also MediaWiki database types valid for $wgDBtype.
97 * To add a new type, create a <type>Installer class and a Database<type>
98 * class, and add a config-type-<type> message to MessagesEn.php.
102 protected static $dbTypes = [
111 * A list of environment check methods called by doEnvironmentChecks().
112 * These may output warnings using showMessage(), and/or abort the
113 * installation process by returning false.
115 * For the WebInstaller these are only called on the Welcome page,
116 * if these methods have side-effects that should affect later page loads
117 * (as well as the generated stylesheet), use envPreps instead.
121 protected $envChecks = [
127 'envCheckModSecurity',
133 'envCheckShellLocale',
134 'envCheckUploadsDirectory',
136 'envCheckSuhosinMaxValueLength',
141 * A list of environment preparation methods called by doEnvironmentPreps().
145 protected $envPreps = [
151 * MediaWiki configuration globals that will eventually be passed through
152 * to LocalSettings.php. The names only are given here, the defaults
153 * typically come from DefaultSettings.php.
157 protected $defaultVarNames = [
168 'wgEmailAuthentication',
172 'wgImageMagickConvertCommand',
177 'wgDeletedDirectory',
181 'wgUseInstantCommons',
188 * Variables that are stored alongside globals, and are used for any
189 * configuration of the installation process aside from the MediaWiki
190 * configuration. Map of names to defaults.
194 protected $internalDefaults = [
196 '_Environment' => false,
197 '_RaiseMemory' => false,
198 '_UpgradeDone' => false,
199 '_InstallDone' => false,
201 '_InstallPassword' => '',
202 '_SameAccount' => true,
203 '_CreateDBAccount' => false,
204 '_NamespaceType' => 'site-name',
205 '_AdminName' => '', // will be set later, when the user selects language
206 '_AdminPassword' => '',
207 '_AdminPasswordConfirm' => '',
209 '_Subscribe' => false,
210 '_SkipOptional' => 'continue',
211 '_RightsProfile' => 'wiki',
212 '_LicenseCode' => 'none',
216 '_MemCachedServers' => '',
217 '_UpgradeKeySupplied' => false,
218 '_ExistingDBSettings' => false,
220 // $wgLogo is probably wrong (T50084); set something that will work.
221 // Single quotes work fine here, as LocalSettingsGenerator outputs this unescaped.
222 'wgLogo' => '$wgResourceBasePath/resources/assets/wiki.png',
223 'wgAuthenticationTokenVersion' => 1,
227 * The actual list of installation steps. This will be initialized by getInstallSteps()
231 private $installSteps = [];
234 * Extra steps for installation, for things like DatabaseInstallers to modify
238 protected $extraInstallSteps = [];
241 * Known object cache types and the functions used to test for their existence.
245 protected $objectCaches = [
246 'apc' => 'apc_fetch',
247 'apcu' => 'apcu_fetch',
248 'wincache' => 'wincache_ucache_get'
252 * User rights profiles.
256 public $rightsProfiles = [
259 '*' => [ 'edit' => false ]
263 'createaccount' => false,
269 'createaccount' => false,
283 'url' => 'https://creativecommons.org/licenses/by/4.0/',
284 'icon' => '$wgResourceBasePath/resources/assets/licenses/cc-by.png',
287 'url' => 'https://creativecommons.org/licenses/by-sa/4.0/',
288 'icon' => '$wgResourceBasePath/resources/assets/licenses/cc-by-sa.png',
291 'url' => 'https://creativecommons.org/licenses/by-nc-sa/4.0/',
292 'icon' => '$wgResourceBasePath/resources/assets/licenses/cc-by-nc-sa.png',
295 'url' => 'https://creativecommons.org/publicdomain/zero/1.0/',
296 'icon' => '$wgResourceBasePath/resources/assets/licenses/cc-0.png',
299 'url' => 'https://www.gnu.org/copyleft/fdl.html',
300 'icon' => '$wgResourceBasePath/resources/assets/licenses/gnu-fdl.png',
308 // Details will be filled in by the selector.
316 * URL to mediawiki-announce subscription
318 protected $mediaWikiAnnounceUrl =
319 'https://lists.wikimedia.org/mailman/subscribe/mediawiki-announce';
322 * Supported language codes for Mailman
324 protected $mediaWikiAnnounceLanguages = [
325 'ca', 'cs', 'da', 'de', 'en', 'es', 'et', 'eu', 'fi', 'fr', 'hr', 'hu',
326 'it', 'ja', 'ko', 'lt', 'nl', 'no', 'pl', 'pt', 'pt-br', 'ro', 'ru',
327 'sl', 'sr', 'sv', 'tr', 'uk'
331 * UI interface for displaying a short message
332 * The parameters are like parameters to wfMessage().
333 * The messages will be in wikitext format, which will be converted to an
334 * output format such as HTML or text before being sent to the user.
337 abstract public function showMessage( $msg /*, ... */ );
340 * Same as showMessage(), but for displaying errors
343 abstract public function showError( $msg /*, ... */ );
346 * Show a message to the installing user by using a Status object
347 * @param Status $status
349 abstract public function showStatusMessage( Status
$status );
352 * Constructs a Config object that contains configuration settings that should be
353 * overwritten for the installation process.
357 * @param Config $baseConfig
359 * @return Config The config to use during installation.
361 public static function getInstallerConfig( Config
$baseConfig ) {
362 $configOverrides = new HashConfig();
364 // disable (problematic) object cache types explicitly, preserving all other (working) ones
366 $emptyCache = [ 'class' => EmptyBagOStuff
::class ];
369 CACHE_NONE
=> $emptyCache,
370 CACHE_DB
=> $emptyCache,
371 CACHE_ANYTHING
=> $emptyCache,
372 CACHE_MEMCACHED
=> $emptyCache,
373 ] +
$baseConfig->get( 'ObjectCaches' );
375 $configOverrides->set( 'ObjectCaches', $objectCaches );
377 // Load the installer's i18n.
378 $messageDirs = $baseConfig->get( 'MessagesDirs' );
379 $messageDirs['MediawikiInstaller'] = __DIR__
. '/i18n';
381 $configOverrides->set( 'MessagesDirs', $messageDirs );
383 $installerConfig = new MultiConfig( [ $configOverrides, $baseConfig ] );
385 // make sure we use the installer config as the main config
386 $configRegistry = $baseConfig->get( 'ConfigRegistry' );
387 $configRegistry['main'] = function () use ( $installerConfig ) {
388 return $installerConfig;
391 $configOverrides->set( 'ConfigRegistry', $configRegistry );
393 return $installerConfig;
397 * Constructor, always call this from child classes.
399 public function __construct() {
400 global $wgMemc, $wgUser, $wgObjectCaches;
402 $defaultConfig = new GlobalVarConfig(); // all the stuff from DefaultSettings.php
403 $installerConfig = self
::getInstallerConfig( $defaultConfig );
405 // Reset all services and inject config overrides
406 MediaWiki\MediaWikiServices
::resetGlobalInstance( $installerConfig );
408 // Don't attempt to load user language options (T126177)
409 // This will be overridden in the web installer with the user-specified language
410 RequestContext
::getMain()->setLanguage( 'en' );
412 // Disable the i18n cache
413 // TODO: manage LocalisationCache singleton in MediaWikiServices
414 Language
::getLocalisationCache()->disableBackend();
416 // Disable all global services, since we don't have any configuration yet!
417 MediaWiki\MediaWikiServices
::disableStorageBackend();
419 // Disable object cache (otherwise CACHE_ANYTHING will try CACHE_DB and
420 // SqlBagOStuff will then throw since we just disabled wfGetDB)
421 $wgObjectCaches = MediaWikiServices
::getInstance()->getMainConfig()->get( 'ObjectCaches' );
422 $wgMemc = ObjectCache
::getInstance( CACHE_NONE
);
424 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
425 $wgUser = User
::newFromId( 0 );
426 RequestContext
::getMain()->setUser( $wgUser );
428 $this->settings
= $this->internalDefaults
;
430 foreach ( $this->defaultVarNames
as $var ) {
431 $this->settings
[$var] = $GLOBALS[$var];
434 $this->doEnvironmentPreps();
436 $this->compiledDBs
= [];
437 foreach ( self
::getDBTypes() as $type ) {
438 $installer = $this->getDBInstaller( $type );
440 if ( !$installer->isCompiled() ) {
443 $this->compiledDBs
[] = $type;
446 $this->parserTitle
= Title
::newFromText( 'Installer' );
447 $this->parserOptions
= new ParserOptions( $wgUser ); // language will be wrong :(
448 // Don't try to access DB before user language is initialised
449 $this->setParserLanguage( Language
::factory( 'en' ) );
453 * Get a list of known DB types.
457 public static function getDBTypes() {
458 return self
::$dbTypes;
462 * Do initial checks of the PHP environment. Set variables according to
463 * the observed environment.
465 * It's possible that this may be called under the CLI SAPI, not the SAPI
466 * that the wiki will primarily run under. In that case, the subclass should
467 * initialise variables such as wgScriptPath, before calling this function.
469 * Under the web subclass, it can already be assumed that PHP 5+ is in use
470 * and that sessions are working.
474 public function doEnvironmentChecks() {
475 // Php version has already been checked by entry scripts
476 // Show message here for information purposes
478 $this->showMessage( 'config-env-hhvm', HHVM_VERSION
);
480 $this->showMessage( 'config-env-php', PHP_VERSION
);
484 // Must go here because an old version of PCRE can prevent other checks from completing
485 list( $pcreVersion ) = explode( ' ', PCRE_VERSION
, 2 );
486 if ( version_compare( $pcreVersion, self
::MINIMUM_PCRE_VERSION
, '<' ) ) {
487 $this->showError( 'config-pcre-old', self
::MINIMUM_PCRE_VERSION
, $pcreVersion );
490 foreach ( $this->envChecks
as $check ) {
491 $status = $this->$check();
492 if ( $status === false ) {
498 $this->setVar( '_Environment', $good );
500 return $good ? Status
::newGood() : Status
::newFatal( 'config-env-bad' );
503 public function doEnvironmentPreps() {
504 foreach ( $this->envPreps
as $prep ) {
510 * Set a MW configuration variable, or internal installer configuration variable.
512 * @param string $name
513 * @param mixed $value
515 public function setVar( $name, $value ) {
516 $this->settings
[$name] = $value;
520 * Get an MW configuration variable, or internal installer configuration variable.
521 * The defaults come from $GLOBALS (ultimately DefaultSettings.php).
522 * Installer variables are typically prefixed by an underscore.
524 * @param string $name
525 * @param mixed $default
529 public function getVar( $name, $default = null ) {
530 if ( !isset( $this->settings
[$name] ) ) {
533 return $this->settings
[$name];
538 * Get a list of DBs supported by current PHP setup
542 public function getCompiledDBs() {
543 return $this->compiledDBs
;
547 * Get the DatabaseInstaller class name for this type
549 * @param string $type database type ($wgDBtype)
550 * @return string Class name
553 public static function getDBInstallerClass( $type ) {
554 return ucfirst( $type ) . 'Installer';
558 * Get an instance of DatabaseInstaller for the specified DB type.
560 * @param mixed $type DB installer for which is needed, false to use default.
562 * @return DatabaseInstaller
564 public function getDBInstaller( $type = false ) {
566 $type = $this->getVar( 'wgDBtype' );
569 $type = strtolower( $type );
571 if ( !isset( $this->dbInstallers
[$type] ) ) {
572 $class = self
::getDBInstallerClass( $type );
573 $this->dbInstallers
[$type] = new $class( $this );
576 return $this->dbInstallers
[$type];
580 * Determine if LocalSettings.php exists. If it does, return its variables.
582 * @return array|false
584 public static function getExistingLocalSettings() {
587 // You might be wondering why this is here. Well if you don't do this
588 // then some poorly-formed extensions try to call their own classes
589 // after immediately registering them. We really need to get extension
590 // registration out of the global scope and into a real format.
591 // @see https://phabricator.wikimedia.org/T69440
592 global $wgAutoloadClasses;
593 $wgAutoloadClasses = [];
595 // LocalSettings.php should not call functions, except wfLoadSkin/wfLoadExtensions
596 // Define the required globals here, to ensure, the functions can do it work correctly.
597 // phpcs:ignore MediaWiki.VariableAnalysis.UnusedGlobalVariables
598 global $wgExtensionDirectory, $wgStyleDirectory;
600 Wikimedia\
suppressWarnings();
601 $_lsExists = file_exists( "$IP/LocalSettings.php" );
602 Wikimedia\restoreWarnings
();
609 require "$IP/includes/DefaultSettings.php";
610 require "$IP/LocalSettings.php";
612 return get_defined_vars();
616 * Get a fake password for sending back to the user in HTML.
617 * This is a security mechanism to avoid compromise of the password in the
618 * event of session ID compromise.
620 * @param string $realPassword
624 public function getFakePassword( $realPassword ) {
625 return str_repeat( '*', strlen( $realPassword ) );
629 * Set a variable which stores a password, except if the new value is a
630 * fake password in which case leave it as it is.
632 * @param string $name
633 * @param mixed $value
635 public function setPassword( $name, $value ) {
636 if ( !preg_match( '/^\*+$/', $value ) ) {
637 $this->setVar( $name, $value );
642 * On POSIX systems return the primary group of the webserver we're running under.
643 * On other systems just returns null.
645 * This is used to advice the user that he should chgrp his mw-config/data/images directory as the
646 * webserver user before he can install.
648 * Public because SqliteInstaller needs it, and doesn't subclass Installer.
652 public static function maybeGetWebserverPrimaryGroup() {
653 if ( !function_exists( 'posix_getegid' ) ||
!function_exists( 'posix_getpwuid' ) ) {
654 # I don't know this, this isn't UNIX.
658 # posix_getegid() *not* getmygid() because we want the group of the webserver,
659 # not whoever owns the current script.
660 $gid = posix_getegid();
661 $group = posix_getpwuid( $gid )['name'];
667 * Convert wikitext $text to HTML.
669 * This is potentially error prone since many parser features require a complete
670 * installed MW database. The solution is to just not use those features when you
671 * write your messages. This appears to work well enough. Basic formatting and
672 * external links work just fine.
674 * But in case a translator decides to throw in a "#ifexist" or internal link or
675 * whatever, this function is guarded to catch the attempted DB access and to present
676 * some fallback text.
678 * @param string $text
679 * @param bool $lineStart
682 public function parse( $text, $lineStart = false ) {
686 $out = $wgParser->parse( $text, $this->parserTitle
, $this->parserOptions
, $lineStart );
687 $html = $out->getText( [
688 'enableSectionEditLinks' => false,
691 } catch ( MediaWiki\Services\ServiceDisabledException
$e ) {
692 $html = '<!--DB access attempted during parse--> ' . htmlspecialchars( $text );
699 * @return ParserOptions
701 public function getParserOptions() {
702 return $this->parserOptions
;
705 public function disableLinkPopups() {
706 $this->parserOptions
->setExternalLinkTarget( false );
709 public function restoreLinkPopups() {
710 global $wgExternalLinkTarget;
711 $this->parserOptions
->setExternalLinkTarget( $wgExternalLinkTarget );
715 * Install step which adds a row to the site_stats table with appropriate
718 * @param DatabaseInstaller $installer
722 public function populateSiteStats( DatabaseInstaller
$installer ) {
723 $status = $installer->getConnection();
724 if ( !$status->isOK() ) {
727 $status->value
->insert(
731 'ss_total_edits' => 0,
732 'ss_good_articles' => 0,
733 'ss_total_pages' => 0,
735 'ss_active_users' => 0,
741 return Status
::newGood();
745 * Environment check for DB types.
748 protected function envCheckDB() {
753 // Messages: config-type-mysql, config-type-postgres, config-type-oracle,
754 // config-type-sqlite
755 foreach ( self
::getDBTypes() as $name ) {
756 $allNames[] = wfMessage( "config-type-$name" )->text();
759 $databases = $this->getCompiledDBs();
761 $databases = array_flip( $databases );
762 foreach ( array_keys( $databases ) as $db ) {
763 $installer = $this->getDBInstaller( $db );
764 $status = $installer->checkPrerequisites();
765 if ( !$status->isGood() ) {
766 $this->showStatusMessage( $status );
768 if ( !$status->isOK() ) {
769 unset( $databases[$db] );
772 $databases = array_flip( $databases );
774 $this->showError( 'config-no-db', $wgLang->commaList( $allNames ), count( $allNames ) );
776 // @todo FIXME: This only works for the web installer!
784 * Some versions of libxml+PHP break < and > encoding horribly
787 protected function envCheckBrokenXML() {
788 $test = new PhpXmlBugTester();
790 $this->showError( 'config-brokenlibxml' );
799 * Environment check for the PCRE module.
801 * @note If this check were to fail, the parser would
802 * probably throw an exception before the result
803 * of this check is shown to the user.
806 protected function envCheckPCRE() {
807 Wikimedia\
suppressWarnings();
808 $regexd = preg_replace( '/[\x{0430}-\x{04FF}]/iu', '', '-АБВГД-' );
809 // Need to check for \p support too, as PCRE can be compiled
810 // with utf8 support, but not unicode property support.
811 // check that \p{Zs} (space separators) matches
812 // U+3000 (Ideographic space)
813 $regexprop = preg_replace( '/\p{Zs}/u', '', "-\xE3\x80\x80-" );
814 Wikimedia\restoreWarnings
();
815 if ( $regexd != '--' ||
$regexprop != '--' ) {
816 $this->showError( 'config-pcre-no-utf8' );
825 * Environment check for available memory.
828 protected function envCheckMemory() {
829 $limit = ini_get( 'memory_limit' );
831 if ( !$limit ||
$limit == -1 ) {
835 $n = wfShorthandToInteger( $limit );
837 if ( $n < $this->minMemorySize
* 1024 * 1024 ) {
838 $newLimit = "{$this->minMemorySize}M";
840 if ( ini_set( "memory_limit", $newLimit ) === false ) {
841 $this->showMessage( 'config-memory-bad', $limit );
843 $this->showMessage( 'config-memory-raised', $limit, $newLimit );
844 $this->setVar( '_RaiseMemory', true );
852 * Environment check for compiled object cache types.
854 protected function envCheckCache() {
856 foreach ( $this->objectCaches
as $name => $function ) {
857 if ( function_exists( $function ) ) {
858 $caches[$name] = true;
863 $key = 'config-no-cache-apcu';
864 $this->showMessage( $key );
867 $this->setVar( '_Caches', $caches );
871 * Scare user to death if they have mod_security or mod_security2
874 protected function envCheckModSecurity() {
875 if ( self
::apacheModulePresent( 'mod_security' )
876 || self
::apacheModulePresent( 'mod_security2' ) ) {
877 $this->showMessage( 'config-mod-security' );
884 * Search for GNU diff3.
887 protected function envCheckDiff3() {
888 $names = [ "gdiff3", "diff3" ];
889 if ( wfIsWindows() ) {
890 $names[] = 'diff3.exe';
892 $versionInfo = [ '--version', 'GNU diffutils' ];
894 $diff3 = ExecutableFinder
::findInDefaultPaths( $names, $versionInfo );
897 $this->setVar( 'wgDiff3', $diff3 );
899 $this->setVar( 'wgDiff3', false );
900 $this->showMessage( 'config-diff3-bad' );
907 * Environment check for ImageMagick and GD.
910 protected function envCheckGraphics() {
911 $names = wfIsWindows() ?
'convert.exe' : 'convert';
912 $versionInfo = [ '-version', 'ImageMagick' ];
913 $convert = ExecutableFinder
::findInDefaultPaths( $names, $versionInfo );
915 $this->setVar( 'wgImageMagickConvertCommand', '' );
917 $this->setVar( 'wgImageMagickConvertCommand', $convert );
918 $this->showMessage( 'config-imagemagick', $convert );
921 } elseif ( function_exists( 'imagejpeg' ) ) {
922 $this->showMessage( 'config-gd' );
924 $this->showMessage( 'config-no-scaling' );
936 protected function envCheckGit() {
937 $names = wfIsWindows() ?
'git.exe' : 'git';
938 $versionInfo = [ '--version', 'git version' ];
940 $git = ExecutableFinder
::findInDefaultPaths( $names, $versionInfo );
943 $this->setVar( 'wgGitBin', $git );
944 $this->showMessage( 'config-git', $git );
946 $this->setVar( 'wgGitBin', false );
947 $this->showMessage( 'config-git-bad' );
954 * Environment check to inform user which server we've assumed.
958 protected function envCheckServer() {
959 $server = $this->envGetDefaultServer();
960 if ( $server !== null ) {
961 $this->showMessage( 'config-using-server', $server );
967 * Environment check to inform user which paths we've assumed.
971 protected function envCheckPath() {
974 $this->getVar( 'wgServer' ),
975 $this->getVar( 'wgScriptPath' )
981 * Environment check for preferred locale in shell
984 protected function envCheckShellLocale() {
985 $os = php_uname( 's' );
986 $supported = [ 'Linux', 'SunOS', 'HP-UX', 'Darwin' ]; # Tested these
988 if ( !in_array( $os, $supported ) ) {
992 # Get a list of available locales.
994 $lines = wfShellExec( '/usr/bin/locale -a', $ret );
1000 $lines = array_map( 'trim', explode( "\n", $lines ) );
1001 $candidatesByLocale = [];
1002 $candidatesByLang = [];
1004 foreach ( $lines as $line ) {
1005 if ( $line === '' ) {
1009 if ( !preg_match( '/^([a-zA-Z]+)(_[a-zA-Z]+|)\.(utf8|UTF-8)(@[a-zA-Z_]*|)$/i', $line, $m ) ) {
1013 list( , $lang, , , ) = $m;
1015 $candidatesByLocale[$m[0]] = $m;
1016 $candidatesByLang[$lang][] = $m;
1019 # Try the current value of LANG.
1020 if ( isset( $candidatesByLocale[getenv( 'LANG' )] ) ) {
1021 $this->setVar( 'wgShellLocale', getenv( 'LANG' ) );
1026 # Try the most common ones.
1027 $commonLocales = [ 'C.UTF-8', 'en_US.UTF-8', 'en_US.utf8', 'de_DE.UTF-8', 'de_DE.utf8' ];
1028 foreach ( $commonLocales as $commonLocale ) {
1029 if ( isset( $candidatesByLocale[$commonLocale] ) ) {
1030 $this->setVar( 'wgShellLocale', $commonLocale );
1036 # Is there an available locale in the Wiki's language?
1037 $wikiLang = $this->getVar( 'wgLanguageCode' );
1039 if ( isset( $candidatesByLang[$wikiLang] ) ) {
1040 $m = reset( $candidatesByLang[$wikiLang] );
1041 $this->setVar( 'wgShellLocale', $m[0] );
1046 # Are there any at all?
1047 if ( count( $candidatesByLocale ) ) {
1048 $m = reset( $candidatesByLocale );
1049 $this->setVar( 'wgShellLocale', $m[0] );
1059 * Environment check for the permissions of the uploads directory
1062 protected function envCheckUploadsDirectory() {
1065 $dir = $IP . '/images/';
1066 $url = $this->getVar( 'wgServer' ) . $this->getVar( 'wgScriptPath' ) . '/images/';
1067 $safe = !$this->dirIsExecutable( $dir, $url );
1070 $this->showMessage( 'config-uploads-not-safe', $dir );
1077 * Checks if suhosin.get.max_value_length is set, and if so generate
1078 * a warning because it decreases ResourceLoader performance.
1081 protected function envCheckSuhosinMaxValueLength() {
1082 $maxValueLength = ini_get( 'suhosin.get.max_value_length' );
1083 if ( $maxValueLength > 0 && $maxValueLength < 1024 ) {
1084 // Only warn if the value is below the sane 1024
1085 $this->showMessage( 'config-suhosin-max-value-length', $maxValueLength );
1092 * Checks if we're running on 64 bit or not. 32 bit is becoming increasingly
1093 * hard to support, so let's at least warn people.
1097 protected function envCheck64Bit() {
1098 if ( PHP_INT_SIZE
== 4 ) {
1099 $this->showMessage( 'config-using-32bit' );
1106 * Convert a hex string representing a Unicode code point to that code point.
1108 * @return string|false
1110 protected function unicodeChar( $c ) {
1114 } elseif ( $c <= 0x7FF ) {
1115 return chr( 0xC0 |
$c >> 6 ) . chr( 0x80 |
$c & 0x3F );
1116 } elseif ( $c <= 0xFFFF ) {
1117 return chr( 0xE0 |
$c >> 12 ) . chr( 0x80 |
$c >> 6 & 0x3F ) .
1118 chr( 0x80 |
$c & 0x3F );
1119 } elseif ( $c <= 0x10FFFF ) {
1120 return chr( 0xF0 |
$c >> 18 ) . chr( 0x80 |
$c >> 12 & 0x3F ) .
1121 chr( 0x80 |
$c >> 6 & 0x3F ) .
1122 chr( 0x80 |
$c & 0x3F );
1129 * Check the libicu version
1131 protected function envCheckLibicu() {
1133 * This needs to be updated something that the latest libicu
1134 * will properly normalize. This normalization was found at
1135 * http://www.unicode.org/versions/Unicode5.2.0/#Character_Additions
1136 * Note that we use the hex representation to create the code
1137 * points in order to avoid any Unicode-destroying during transit.
1139 $not_normal_c = $this->unicodeChar( "FA6C" );
1140 $normal_c = $this->unicodeChar( "242EE" );
1142 $useNormalizer = 'php';
1143 $needsUpdate = false;
1145 if ( function_exists( 'normalizer_normalize' ) ) {
1146 $useNormalizer = 'intl';
1147 $intl = normalizer_normalize( $not_normal_c, Normalizer
::FORM_C
);
1148 if ( $intl !== $normal_c ) {
1149 $needsUpdate = true;
1153 // Uses messages 'config-unicode-using-php' and 'config-unicode-using-intl'
1154 if ( $useNormalizer === 'php' ) {
1155 $this->showMessage( 'config-unicode-pure-php-warning' );
1157 $this->showMessage( 'config-unicode-using-' . $useNormalizer );
1158 if ( $needsUpdate ) {
1159 $this->showMessage( 'config-unicode-update-warning' );
1165 * Environment prep for the server hostname.
1167 protected function envPrepServer() {
1168 $server = $this->envGetDefaultServer();
1169 if ( $server !== null ) {
1170 $this->setVar( 'wgServer', $server );
1175 * Helper function to be called from envPrepServer()
1178 abstract protected function envGetDefaultServer();
1181 * Environment prep for setting $IP and $wgScriptPath.
1183 protected function envPrepPath() {
1185 $IP = dirname( dirname( __DIR__
) );
1186 $this->setVar( 'IP', $IP );
1190 * Checks if scripts located in the given directory can be executed via the given URL.
1192 * Used only by environment checks.
1193 * @param string $dir
1194 * @param string $url
1195 * @return bool|int|string
1197 public function dirIsExecutable( $dir, $url ) {
1200 "<?php echo 'ex' . 'ec';",
1201 "#!/var/env php5\n<?php echo 'ex' . 'ec';",
1205 // it would be good to check other popular languages here, but it'll be slow.
1207 Wikimedia\
suppressWarnings();
1209 foreach ( $scriptTypes as $ext => $contents ) {
1210 foreach ( $contents as $source ) {
1211 $file = 'exectest.' . $ext;
1213 if ( !file_put_contents( $dir . $file, $source ) ) {
1218 $text = Http
::get( $url . $file, [ 'timeout' => 3 ], __METHOD__
);
1219 } catch ( Exception
$e ) {
1220 // Http::get throws with allow_url_fopen = false and no curl extension.
1223 unlink( $dir . $file );
1225 if ( $text == 'exec' ) {
1226 Wikimedia\restoreWarnings
();
1233 Wikimedia\restoreWarnings
();
1239 * Checks for presence of an Apache module. Works only if PHP is running as an Apache module, too.
1241 * @param string $moduleName Name of module to check.
1244 public static function apacheModulePresent( $moduleName ) {
1245 if ( function_exists( 'apache_get_modules' ) && in_array( $moduleName, apache_get_modules() ) ) {
1248 // try it the hard way
1250 phpinfo( INFO_MODULES
);
1251 $info = ob_get_clean();
1253 return strpos( $info, $moduleName ) !== false;
1257 * ParserOptions are constructed before we determined the language, so fix it
1259 * @param Language $lang
1261 public function setParserLanguage( $lang ) {
1262 $this->parserOptions
->setTargetLanguage( $lang );
1263 $this->parserOptions
->setUserLang( $lang );
1267 * Overridden by WebInstaller to provide lastPage parameters.
1268 * @param string $page
1271 protected function getDocUrl( $page ) {
1272 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1276 * Finds extensions that follow the format /$directory/Name/Name.php,
1277 * and returns an array containing the value for 'Name' for each found extension.
1279 * Reasonable values for $directory include 'extensions' (the default) and 'skins'.
1281 * @param string $directory Directory to search in
1282 * @return array [ $extName => [ 'screenshots' => [ '...' ] ]
1284 public function findExtensions( $directory = 'extensions' ) {
1285 if ( $this->getVar( 'IP' ) === null ) {
1289 $extDir = $this->getVar( 'IP' ) . '/' . $directory;
1290 if ( !is_readable( $extDir ) ||
!is_dir( $extDir ) ) {
1294 // extensions -> extension.json, skins -> skin.json
1295 $jsonFile = substr( $directory, 0, strlen( $directory ) - 1 ) . '.json';
1297 $dh = opendir( $extDir );
1299 while ( ( $file = readdir( $dh ) ) !== false ) {
1300 if ( !is_dir( "$extDir/$file" ) ) {
1303 if ( file_exists( "$extDir/$file/$jsonFile" ) ||
file_exists( "$extDir/$file/$file.php" ) ) {
1304 // Extension exists. Now see if there are screenshots
1306 if ( is_dir( "$extDir/$file/screenshots" ) ) {
1307 $paths = glob( "$extDir/$file/screenshots/*.png" );
1308 foreach ( $paths as $path ) {
1309 $exts[$file]['screenshots'][] = str_replace( $extDir, "../$directory", $path );
1316 uksort( $exts, 'strnatcasecmp' );
1322 * Returns a default value to be used for $wgDefaultSkin: normally the one set in DefaultSettings,
1323 * but will fall back to another if the default skin is missing and some other one is present
1326 * @param string[] $skinNames Names of installed skins.
1329 public function getDefaultSkin( array $skinNames ) {
1330 $defaultSkin = $GLOBALS['wgDefaultSkin'];
1331 if ( !$skinNames ||
in_array( $defaultSkin, $skinNames ) ) {
1332 return $defaultSkin;
1334 return $skinNames[0];
1339 * Installs the auto-detected extensions.
1343 protected function includeExtensions() {
1345 $exts = $this->getVar( '_Extensions' );
1346 $IP = $this->getVar( 'IP' );
1349 * We need to include DefaultSettings before including extensions to avoid
1350 * warnings about unset variables. However, the only thing we really
1351 * want here is $wgHooks['LoadExtensionSchemaUpdates']. This won't work
1352 * if the extension has hidden hook registration in $wgExtensionFunctions,
1353 * but we're not opening that can of worms
1354 * @see https://phabricator.wikimedia.org/T28857
1356 global $wgAutoloadClasses;
1357 $wgAutoloadClasses = [];
1360 require "$IP/includes/DefaultSettings.php";
1362 foreach ( $exts as $e ) {
1363 if ( file_exists( "$IP/extensions/$e/extension.json" ) ) {
1364 $queue["$IP/extensions/$e/extension.json"] = 1;
1366 require_once "$IP/extensions/$e/$e.php";
1370 $registry = new ExtensionRegistry();
1371 $data = $registry->readFromQueue( $queue );
1372 $wgAutoloadClasses +
= $data['autoload'];
1374 $hooksWeWant = isset( $wgHooks['LoadExtensionSchemaUpdates'] ) ?
1375 /** @suppress PhanUndeclaredVariable $wgHooks is set by DefaultSettings */
1376 $wgHooks['LoadExtensionSchemaUpdates'] : [];
1378 if ( isset( $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
1379 $hooksWeWant = array_merge_recursive(
1381 $data['globals']['wgHooks']['LoadExtensionSchemaUpdates']
1384 // Unset everyone else's hooks. Lord knows what someone might be doing
1385 // in ParserFirstCallInit (see T29171)
1386 $GLOBALS['wgHooks'] = [ 'LoadExtensionSchemaUpdates' => $hooksWeWant ];
1388 return Status
::newGood();
1392 * Get an array of install steps. Should always be in the format of
1394 * 'name' => 'someuniquename',
1395 * 'callback' => [ $obj, 'method' ],
1397 * There must be a config-install-$name message defined per step, which will
1398 * be shown on install.
1400 * @param DatabaseInstaller $installer DatabaseInstaller so we can make callbacks
1403 protected function getInstallSteps( DatabaseInstaller
$installer ) {
1404 $coreInstallSteps = [
1405 [ 'name' => 'database', 'callback' => [ $installer, 'setupDatabase' ] ],
1406 [ 'name' => 'tables', 'callback' => [ $installer, 'createTables' ] ],
1407 [ 'name' => 'interwiki', 'callback' => [ $installer, 'populateInterwikiTable' ] ],
1408 [ 'name' => 'stats', 'callback' => [ $this, 'populateSiteStats' ] ],
1409 [ 'name' => 'keys', 'callback' => [ $this, 'generateKeys' ] ],
1410 [ 'name' => 'updates', 'callback' => [ $installer, 'insertUpdateKeys' ] ],
1411 [ 'name' => 'sysop', 'callback' => [ $this, 'createSysop' ] ],
1412 [ 'name' => 'mainpage', 'callback' => [ $this, 'createMainpage' ] ],
1415 // Build the array of install steps starting from the core install list,
1416 // then adding any callbacks that wanted to attach after a given step
1417 foreach ( $coreInstallSteps as $step ) {
1418 $this->installSteps
[] = $step;
1419 if ( isset( $this->extraInstallSteps
[$step['name']] ) ) {
1420 $this->installSteps
= array_merge(
1421 $this->installSteps
,
1422 $this->extraInstallSteps
[$step['name']]
1427 // Prepend any steps that want to be at the beginning
1428 if ( isset( $this->extraInstallSteps
['BEGINNING'] ) ) {
1429 $this->installSteps
= array_merge(
1430 $this->extraInstallSteps
['BEGINNING'],
1435 // Extensions should always go first, chance to tie into hooks and such
1436 if ( count( $this->getVar( '_Extensions' ) ) ) {
1437 array_unshift( $this->installSteps
,
1438 [ 'name' => 'extensions', 'callback' => [ $this, 'includeExtensions' ] ]
1440 $this->installSteps
[] = [
1441 'name' => 'extension-tables',
1442 'callback' => [ $installer, 'createExtensionTables' ]
1446 return $this->installSteps
;
1450 * Actually perform the installation.
1452 * @param callable $startCB A callback array for the beginning of each step
1453 * @param callable $endCB A callback array for the end of each step
1455 * @return array Array of Status objects
1457 public function performInstallation( $startCB, $endCB ) {
1458 $installResults = [];
1459 $installer = $this->getDBInstaller();
1460 $installer->preInstall();
1461 $steps = $this->getInstallSteps( $installer );
1462 foreach ( $steps as $stepObj ) {
1463 $name = $stepObj['name'];
1464 call_user_func_array( $startCB, [ $name ] );
1466 // Perform the callback step
1467 $status = call_user_func( $stepObj['callback'], $installer );
1469 // Output and save the results
1470 call_user_func( $endCB, $name, $status );
1471 $installResults[$name] = $status;
1473 // If we've hit some sort of fatal, we need to bail.
1474 // Callback already had a chance to do output above.
1475 if ( !$status->isOk() ) {
1479 if ( $status->isOk() ) {
1481 'config-install-success',
1482 $this->getVar( 'wgServer' ),
1483 $this->getVar( 'wgScriptPath' )
1485 $this->setVar( '_InstallDone', true );
1488 return $installResults;
1492 * Generate $wgSecretKey. Will warn if we had to use an insecure random source.
1496 public function generateKeys() {
1497 $keys = [ 'wgSecretKey' => 64 ];
1498 if ( strval( $this->getVar( 'wgUpgradeKey' ) ) === '' ) {
1499 $keys['wgUpgradeKey'] = 16;
1502 return $this->doGenerateKeys( $keys );
1506 * Generate a secret value for variables using our CryptRand generator.
1507 * Produce a warning if the random source was insecure.
1509 * @param array $keys
1512 protected function doGenerateKeys( $keys ) {
1513 $status = Status
::newGood();
1516 foreach ( $keys as $name => $length ) {
1517 $secretKey = MWCryptRand
::generateHex( $length, true );
1518 if ( !MWCryptRand
::wasStrong() ) {
1522 $this->setVar( $name, $secretKey );
1526 $names = array_keys( $keys );
1527 $names = preg_replace( '/^(.*)$/', '\$$1', $names );
1529 $status->warning( 'config-insecure-keys', $wgLang->listToText( $names ), count( $names ) );
1536 * Create the first user account, grant it sysop and bureaucrat rights
1540 protected function createSysop() {
1541 $name = $this->getVar( '_AdminName' );
1542 $user = User
::newFromName( $name );
1545 // We should've validated this earlier anyway!
1546 return Status
::newFatal( 'config-admin-error-user', $name );
1549 if ( $user->idForName() == 0 ) {
1550 $user->addToDatabase();
1553 $user->setPassword( $this->getVar( '_AdminPassword' ) );
1554 } catch ( PasswordError
$pwe ) {
1555 return Status
::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
1558 $user->addGroup( 'sysop' );
1559 $user->addGroup( 'bureaucrat' );
1560 if ( $this->getVar( '_AdminEmail' ) ) {
1561 $user->setEmail( $this->getVar( '_AdminEmail' ) );
1563 $user->saveSettings();
1565 // Update user count
1566 $ssUpdate = SiteStatsUpdate
::factory( [ 'users' => 1 ] );
1567 $ssUpdate->doUpdate();
1569 $status = Status
::newGood();
1571 if ( $this->getVar( '_Subscribe' ) && $this->getVar( '_AdminEmail' ) ) {
1572 $this->subscribeToMediaWikiAnnounce( $status );
1581 private function subscribeToMediaWikiAnnounce( Status
$s ) {
1583 'email' => $this->getVar( '_AdminEmail' ),
1588 // Mailman doesn't support as many languages as we do, so check to make
1589 // sure their selected language is available
1590 $myLang = $this->getVar( '_UserLang' );
1591 if ( in_array( $myLang, $this->mediaWikiAnnounceLanguages
) ) {
1592 $myLang = $myLang == 'pt-br' ?
'pt_BR' : $myLang; // rewrite to Mailman's pt_BR
1593 $params['language'] = $myLang;
1596 if ( MWHttpRequest
::canMakeRequests() ) {
1597 $res = MWHttpRequest
::factory( $this->mediaWikiAnnounceUrl
,
1598 [ 'method' => 'POST', 'postData' => $params ], __METHOD__
)->execute();
1599 if ( !$res->isOK() ) {
1600 $s->warning( 'config-install-subscribe-fail', $res->getMessage() );
1603 $s->warning( 'config-install-subscribe-notpossible' );
1608 * Insert Main Page with default content.
1610 * @param DatabaseInstaller $installer
1613 protected function createMainpage( DatabaseInstaller
$installer ) {
1614 $status = Status
::newGood();
1615 $title = Title
::newMainPage();
1616 if ( $title->exists() ) {
1617 $status->warning( 'config-install-mainpage-exists' );
1621 $page = WikiPage
::factory( $title );
1622 $content = new WikitextContent(
1623 wfMessage( 'mainpagetext' )->inContentLanguage()->text() . "\n\n" .
1624 wfMessage( 'mainpagedocfooter' )->inContentLanguage()->text()
1627 $status = $page->doEditContent( $content,
1631 User
::newFromName( 'MediaWiki default' )
1633 } catch ( Exception
$e ) {
1634 // using raw, because $wgShowExceptionDetails can not be set yet
1635 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
1642 * Override the necessary bits of the config to run an installation.
1644 public static function overrideConfig() {
1645 // Use PHP's built-in session handling, since MediaWiki's
1646 // SessionHandler can't work before we have an object cache set up.
1647 define( 'MW_NO_SESSION_HANDLER', 1 );
1649 // Don't access the database
1650 $GLOBALS['wgUseDatabaseMessages'] = false;
1651 // Don't cache langconv tables
1652 $GLOBALS['wgLanguageConverterCacheType'] = CACHE_NONE
;
1654 $GLOBALS['wgShowExceptionDetails'] = true;
1655 // Don't break forms
1656 $GLOBALS['wgExternalLinkTarget'] = '_blank';
1658 // Extended debugging
1659 $GLOBALS['wgShowSQLErrors'] = true;
1660 $GLOBALS['wgShowDBErrorBacktrace'] = true;
1662 // Allow multiple ob_flush() calls
1663 $GLOBALS['wgDisableOutputCompression'] = true;
1665 // Use a sensible cookie prefix (not my_wiki)
1666 $GLOBALS['wgCookiePrefix'] = 'mw_installer';
1668 // Some of the environment checks make shell requests, remove limits
1669 $GLOBALS['wgMaxShellMemory'] = 0;
1671 // Override the default CookieSessionProvider with a dummy
1672 // implementation that won't stomp on PHP's cookies.
1673 $GLOBALS['wgSessionProviders'] = [
1675 'class' => InstallerSessionProvider
::class,
1682 // Don't try to use any object cache for SessionManager either.
1683 $GLOBALS['wgSessionCacheType'] = CACHE_NONE
;
1687 * Add an installation step following the given step.
1689 * @param callable $callback A valid installation callback array, in this form:
1690 * [ 'name' => 'some-unique-name', 'callback' => [ $obj, 'function' ] ];
1691 * @param string $findStep The step to find. Omit to put the step at the beginning
1693 public function addInstallStep( $callback, $findStep = 'BEGINNING' ) {
1694 $this->extraInstallSteps
[$findStep][] = $callback;
1698 * Disable the time limit for execution.
1699 * Some long-running pages (Install, Upgrade) will want to do this
1701 protected function disableTimeLimit() {
1702 Wikimedia\
suppressWarnings();
1703 set_time_limit( 0 );
1704 Wikimedia\restoreWarnings
();