3 * Base code for MediaWiki installer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * This documentation group collects source code files with deployment functionality.
27 * @defgroup Deployment Deployment
31 * Base installer class.
33 * This class provides the base for installation and update functionality
34 * for both MediaWiki core and extensions.
39 abstract class Installer
{
41 // This is the absolute minimum PHP version we can support
42 const MINIMUM_PHP_VERSION
= '5.3.2';
51 * List of detected DBs, access using getCompiledDBs().
55 protected $compiledDBs;
58 * Cached DB installer instances, access using getDBInstaller().
62 protected $dbInstallers = array();
65 * Minimum memory size in MB.
69 protected $minMemorySize = 50;
72 * Cached Title, used by parse().
76 protected $parserTitle;
79 * Cached ParserOptions, used by parse().
83 protected $parserOptions;
86 * Known database types. These correspond to the class names <type>Installer,
87 * and are also MediaWiki database types valid for $wgDBtype.
89 * To add a new type, create a <type>Installer class and a Database<type>
90 * class, and add a config-type-<type> message to MessagesEn.php.
94 protected static $dbTypes = array(
102 * A list of environment check methods called by doEnvironmentChecks().
103 * These may output warnings using showMessage(), and/or abort the
104 * installation process by returning false.
108 protected $envChecks = array(
110 'envCheckRegisterGlobals',
113 'envCheckMagicQuotes',
114 'envCheckMagicSybase',
122 'envCheckModSecurity',
129 'envCheckShellLocale',
130 'envCheckUploadsDirectory',
132 'envCheckSuhosinMaxValueLength',
138 * MediaWiki configuration globals that will eventually be passed through
139 * to LocalSettings.php. The names only are given here, the defaults
140 * typically come from DefaultSettings.php.
144 protected $defaultVarNames = array(
156 'wgEmailAuthentication',
159 'wgImageMagickConvertCommand',
166 'wgDeletedDirectory',
171 'wgUseInstantCommons',
174 'wgResourceLoaderMaxQueryLength',
178 * Variables that are stored alongside globals, and are used for any
179 * configuration of the installation process aside from the MediaWiki
180 * configuration. Map of names to defaults.
184 protected $internalDefaults = array(
186 '_Environment' => false,
187 '_SafeMode' => false,
188 '_RaiseMemory' => false,
189 '_UpgradeDone' => false,
190 '_InstallDone' => false,
191 '_Caches' => array(),
192 '_InstallPassword' => '',
193 '_SameAccount' => true,
194 '_CreateDBAccount' => false,
195 '_NamespaceType' => 'site-name',
196 '_AdminName' => '', // will be set later, when the user selects language
197 '_AdminPassword' => '',
198 '_AdminPassword2' => '',
200 '_Subscribe' => false,
201 '_SkipOptional' => 'continue',
202 '_RightsProfile' => 'wiki',
203 '_LicenseCode' => 'none',
205 '_Extensions' => array(),
206 '_MemCachedServers' => '',
207 '_UpgradeKeySupplied' => false,
208 '_ExistingDBSettings' => false,
212 * The actual list of installation steps. This will be initialized by getInstallSteps()
216 private $installSteps = array();
219 * Extra steps for installation, for things like DatabaseInstallers to modify
223 protected $extraInstallSteps = array();
226 * Known object cache types and the functions used to test for their existence.
230 protected $objectCaches = array(
231 'xcache' => 'xcache_get',
232 'apc' => 'apc_fetch',
233 'wincache' => 'wincache_ucache_get'
237 * User rights profiles.
241 public $rightsProfiles = array(
244 '*' => array( 'edit' => false )
248 'createaccount' => false,
254 'createaccount' => false,
266 public $licenses = array(
268 'url' => 'http://creativecommons.org/licenses/by/3.0/',
269 'icon' => '{$wgStylePath}/common/images/cc-by.png',
272 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
273 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
275 'cc-by-nc-sa' => array(
276 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/',
277 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png',
280 'url' => 'https://creativecommons.org/publicdomain/zero/1.0/',
281 'icon' => '{$wgStylePath}/common/images/cc-0.png',
285 'icon' => '{$wgStylePath}/common/images/public-domain.png',
288 'url' => 'http://www.gnu.org/copyleft/fdl.html',
289 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
296 'cc-choose' => array(
297 // Details will be filled in by the selector.
305 * URL to mediawiki-announce subscription
307 protected $mediaWikiAnnounceUrl = 'https://lists.wikimedia.org/mailman/subscribe/mediawiki-announce';
310 * Supported language codes for Mailman
312 protected $mediaWikiAnnounceLanguages = array(
313 'ca', 'cs', 'da', 'de', 'en', 'es', 'et', 'eu', 'fi', 'fr', 'hr', 'hu',
314 'it', 'ja', 'ko', 'lt', 'nl', 'no', 'pl', 'pt', 'pt-br', 'ro', 'ru',
315 'sl', 'sr', 'sv', 'tr', 'uk'
319 * UI interface for displaying a short message
320 * The parameters are like parameters to wfMessage().
321 * The messages will be in wikitext format, which will be converted to an
322 * output format such as HTML or text before being sent to the user.
325 abstract public function showMessage( $msg /*, ... */ );
328 * Same as showMessage(), but for displaying errors
331 abstract public function showError( $msg /*, ... */ );
334 * Show a message to the installing user by using a Status object
335 * @param $status Status
337 abstract public function showStatusMessage( Status
$status );
340 * Constructor, always call this from child classes.
342 public function __construct() {
343 global $wgExtensionMessagesFiles, $wgUser;
345 // Disable the i18n cache and LoadBalancer
346 Language
::getLocalisationCache()->disableBackend();
347 LBFactory
::disableBackend();
349 // Load the installer's i18n file.
350 $wgExtensionMessagesFiles['MediawikiInstaller'] =
351 __DIR__
. '/Installer.i18n.php';
353 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
354 $wgUser = User
::newFromId( 0 );
356 $this->settings
= $this->internalDefaults
;
358 foreach ( $this->defaultVarNames
as $var ) {
359 $this->settings
[$var] = $GLOBALS[$var];
362 $compiledDBs = array();
363 foreach ( self
::getDBTypes() as $type ) {
364 $installer = $this->getDBInstaller( $type );
366 if ( !$installer->isCompiled() ) {
369 $compiledDBs[] = $type;
371 $defaults = $installer->getGlobalDefaults();
373 foreach ( $installer->getGlobalNames() as $var ) {
374 if ( isset( $defaults[$var] ) ) {
375 $this->settings
[$var] = $defaults[$var];
377 $this->settings
[$var] = $GLOBALS[$var];
381 $this->compiledDBs
= $compiledDBs;
383 $this->parserTitle
= Title
::newFromText( 'Installer' );
384 $this->parserOptions
= new ParserOptions
; // language will be wrong :(
385 $this->parserOptions
->setEditSection( false );
389 * Get a list of known DB types.
393 public static function getDBTypes() {
394 return self
::$dbTypes;
398 * Do initial checks of the PHP environment. Set variables according to
399 * the observed environment.
401 * It's possible that this may be called under the CLI SAPI, not the SAPI
402 * that the wiki will primarily run under. In that case, the subclass should
403 * initialise variables such as wgScriptPath, before calling this function.
405 * Under the web subclass, it can already be assumed that PHP 5+ is in use
406 * and that sessions are working.
410 public function doEnvironmentChecks() {
411 $phpVersion = phpversion();
412 if ( version_compare( $phpVersion, self
::MINIMUM_PHP_VERSION
, '>=' ) ) {
413 $this->showMessage( 'config-env-php', $phpVersion );
416 $this->showMessage( 'config-env-php-toolow', $phpVersion, self
::MINIMUM_PHP_VERSION
);
421 foreach ( $this->envChecks
as $check ) {
422 $status = $this->$check();
423 if ( $status === false ) {
429 $this->setVar( '_Environment', $good );
431 return $good ? Status
::newGood() : Status
::newFatal( 'config-env-bad' );
435 * Set a MW configuration variable, or internal installer configuration variable.
437 * @param $name String
438 * @param $value Mixed
440 public function setVar( $name, $value ) {
441 $this->settings
[$name] = $value;
445 * Get an MW configuration variable, or internal installer configuration variable.
446 * The defaults come from $GLOBALS (ultimately DefaultSettings.php).
447 * Installer variables are typically prefixed by an underscore.
449 * @param $name String
450 * @param $default Mixed
454 public function getVar( $name, $default = null ) {
455 if ( !isset( $this->settings
[$name] ) ) {
458 return $this->settings
[$name];
463 * Get a list of DBs supported by current PHP setup
467 public function getCompiledDBs() {
468 return $this->compiledDBs
;
472 * Get an instance of DatabaseInstaller for the specified DB type.
474 * @param $type Mixed: DB installer for which is needed, false to use default.
476 * @return DatabaseInstaller
478 public function getDBInstaller( $type = false ) {
480 $type = $this->getVar( 'wgDBtype' );
483 $type = strtolower( $type );
485 if ( !isset( $this->dbInstallers
[$type] ) ) {
486 $class = ucfirst( $type ) . 'Installer';
487 $this->dbInstallers
[$type] = new $class( $this );
490 return $this->dbInstallers
[$type];
494 * Determine if LocalSettings.php exists. If it does, return its variables,
495 * merged with those from AdminSettings.php, as an array.
499 public static function getExistingLocalSettings() {
502 wfSuppressWarnings();
503 $_lsExists = file_exists( "$IP/LocalSettings.php" );
511 require "$IP/includes/DefaultSettings.php";
512 require "$IP/LocalSettings.php";
513 if ( file_exists( "$IP/AdminSettings.php" ) ) {
514 require "$IP/AdminSettings.php";
516 return get_defined_vars();
520 * Get a fake password for sending back to the user in HTML.
521 * This is a security mechanism to avoid compromise of the password in the
522 * event of session ID compromise.
524 * @param $realPassword String
528 public function getFakePassword( $realPassword ) {
529 return str_repeat( '*', strlen( $realPassword ) );
533 * Set a variable which stores a password, except if the new value is a
534 * fake password in which case leave it as it is.
536 * @param $name String
537 * @param $value Mixed
539 public function setPassword( $name, $value ) {
540 if ( !preg_match( '/^\*+$/', $value ) ) {
541 $this->setVar( $name, $value );
546 * On POSIX systems return the primary group of the webserver we're running under.
547 * On other systems just returns null.
549 * This is used to advice the user that he should chgrp his mw-config/data/images directory as the
550 * webserver user before he can install.
552 * Public because SqliteInstaller needs it, and doesn't subclass Installer.
556 public static function maybeGetWebserverPrimaryGroup() {
557 if ( !function_exists( 'posix_getegid' ) ||
!function_exists( 'posix_getpwuid' ) ) {
558 # I don't know this, this isn't UNIX.
562 # posix_getegid() *not* getmygid() because we want the group of the webserver,
563 # not whoever owns the current script.
564 $gid = posix_getegid();
565 $getpwuid = posix_getpwuid( $gid );
566 $group = $getpwuid['name'];
572 * Convert wikitext $text to HTML.
574 * This is potentially error prone since many parser features require a complete
575 * installed MW database. The solution is to just not use those features when you
576 * write your messages. This appears to work well enough. Basic formatting and
577 * external links work just fine.
579 * But in case a translator decides to throw in a "#ifexist" or internal link or
580 * whatever, this function is guarded to catch the attempted DB access and to present
581 * some fallback text.
583 * @param $text String
584 * @param $lineStart Boolean
587 public function parse( $text, $lineStart = false ) {
591 $out = $wgParser->parse( $text, $this->parserTitle
, $this->parserOptions
, $lineStart );
592 $html = $out->getText();
593 } catch ( DBAccessError
$e ) {
594 $html = '<!--DB access attempted during parse--> ' . htmlspecialchars( $text );
596 if ( !empty( $this->debug
) ) {
597 $html .= "<!--\n" . $e->getTraceAsString() . "\n-->";
605 * @return ParserOptions
607 public function getParserOptions() {
608 return $this->parserOptions
;
611 public function disableLinkPopups() {
612 $this->parserOptions
->setExternalLinkTarget( false );
615 public function restoreLinkPopups() {
616 global $wgExternalLinkTarget;
617 $this->parserOptions
->setExternalLinkTarget( $wgExternalLinkTarget );
621 * Install step which adds a row to the site_stats table with appropriate
624 * @param $installer DatabaseInstaller
628 public function populateSiteStats( DatabaseInstaller
$installer ) {
629 $status = $installer->getConnection();
630 if ( !$status->isOK() ) {
633 $status->value
->insert( 'site_stats', array(
635 'ss_total_views' => 0,
636 'ss_total_edits' => 0,
637 'ss_good_articles' => 0,
638 'ss_total_pages' => 0,
641 __METHOD__
, 'IGNORE' );
642 return Status
::newGood();
646 * Exports all wg* variables stored by the installer into global scope.
648 public function exportVars() {
649 foreach ( $this->settings
as $name => $value ) {
650 if ( substr( $name, 0, 2 ) == 'wg' ) {
651 $GLOBALS[$name] = $value;
657 * Environment check for DB types.
660 protected function envCheckDB() {
665 // Messages: config-type-mysql, config-type-postgres, config-type-oracle,
666 // config-type-sqlite
667 foreach ( self
::getDBTypes() as $name ) {
668 $allNames[] = wfMessage( "config-type-$name" )->text();
671 $databases = $this->getCompiledDBs();
673 $databases = array_flip ( $databases );
674 foreach ( array_keys( $databases ) as $db ) {
675 $installer = $this->getDBInstaller( $db );
676 $status = $installer->checkPrerequisites();
677 if ( !$status->isGood() ) {
678 $this->showStatusMessage( $status );
680 if ( !$status->isOK() ) {
681 unset( $databases[$db] );
684 $databases = array_flip( $databases );
686 $this->showError( 'config-no-db', $wgLang->commaList( $allNames ) );
687 // @todo FIXME: This only works for the web installer!
694 * Environment check for register_globals.
696 protected function envCheckRegisterGlobals() {
697 if ( wfIniGetBool( 'register_globals' ) ) {
698 $this->showMessage( 'config-register-globals' );
703 * Some versions of libxml+PHP break < and > encoding horribly
706 protected function envCheckBrokenXML() {
707 $test = new PhpXmlBugTester();
709 $this->showError( 'config-brokenlibxml' );
716 * Test PHP (probably 5.3.1, but it could regress again) to make sure that
717 * reference parameters to __call() are not converted to null
720 protected function envCheckPHP531() {
721 $test = new PhpRefCallBugTester
;
724 $this->showError( 'config-using531', phpversion() );
731 * Environment check for magic_quotes_runtime.
734 protected function envCheckMagicQuotes() {
735 if ( wfIniGetBool( "magic_quotes_runtime" ) ) {
736 $this->showError( 'config-magic-quotes-runtime' );
743 * Environment check for magic_quotes_sybase.
746 protected function envCheckMagicSybase() {
747 if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
748 $this->showError( 'config-magic-quotes-sybase' );
755 * Environment check for mbstring.func_overload.
758 protected function envCheckMbstring() {
759 if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
760 $this->showError( 'config-mbstring' );
767 * Environment check for zend.ze1_compatibility_mode.
770 protected function envCheckZE1() {
771 if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
772 $this->showError( 'config-ze1' );
779 * Environment check for safe_mode.
782 protected function envCheckSafeMode() {
783 if ( wfIniGetBool( 'safe_mode' ) ) {
784 $this->setVar( '_SafeMode', true );
785 $this->showMessage( 'config-safe-mode' );
791 * Environment check for the XML module.
794 protected function envCheckXML() {
795 if ( !function_exists( "utf8_encode" ) ) {
796 $this->showError( 'config-xml-bad' );
803 * Environment check for the PCRE module.
805 * @note If this check were to fail, the parser would
806 * probably throw an exception before the result
807 * of this check is shown to the user.
810 protected function envCheckPCRE() {
811 if ( !function_exists( 'preg_match' ) ) {
812 $this->showError( 'config-pcre' );
815 wfSuppressWarnings();
816 $regexd = preg_replace( '/[\x{0430}-\x{04FF}]/iu', '', '-АБВГД-' );
817 // Need to check for \p support too, as PCRE can be compiled
818 // with utf8 support, but not unicode property support.
819 // check that \p{Zs} (space separators) matches
820 // U+3000 (Ideographic space)
821 $regexprop = preg_replace( '/\p{Zs}/u', '', "-\xE3\x80\x80-" );
823 if ( $regexd != '--' ||
$regexprop != '--' ) {
824 $this->showError( 'config-pcre-no-utf8' );
831 * Environment check for available memory.
834 protected function envCheckMemory() {
835 $limit = ini_get( 'memory_limit' );
837 if ( !$limit ||
$limit == -1 ) {
841 $n = wfShorthandToInteger( $limit );
843 if ( $n < $this->minMemorySize
* 1024 * 1024 ) {
844 $newLimit = "{$this->minMemorySize}M";
846 if ( ini_set( "memory_limit", $newLimit ) === false ) {
847 $this->showMessage( 'config-memory-bad', $limit );
849 $this->showMessage( 'config-memory-raised', $limit, $newLimit );
850 $this->setVar( '_RaiseMemory', true );
857 * Environment check for compiled object cache types.
859 protected function envCheckCache() {
861 foreach ( $this->objectCaches
as $name => $function ) {
862 if ( function_exists( $function ) ) {
863 if ( $name == 'xcache' && !wfIniGetBool( 'xcache.var_size' ) ) {
866 $caches[$name] = true;
871 $this->showMessage( 'config-no-cache' );
874 $this->setVar( '_Caches', $caches );
878 * Scare user to death if they have mod_security
881 protected function envCheckModSecurity() {
882 if ( self
::apacheModulePresent( 'mod_security' ) ) {
883 $this->showMessage( 'config-mod-security' );
889 * Search for GNU diff3.
892 protected function envCheckDiff3() {
893 $names = array( "gdiff3", "diff3", "diff3.exe" );
894 $versionInfo = array( '$1 --version 2>&1', 'GNU diffutils' );
896 $diff3 = self
::locateExecutableInDefaultPaths( $names, $versionInfo );
899 $this->setVar( 'wgDiff3', $diff3 );
901 $this->setVar( 'wgDiff3', false );
902 $this->showMessage( 'config-diff3-bad' );
908 * Environment check for ImageMagick and GD.
911 protected function envCheckGraphics() {
912 $names = array( wfIsWindows() ?
'convert.exe' : 'convert' );
913 $versionInfo = array( '$1 -version', 'ImageMagick' );
914 $convert = self
::locateExecutableInDefaultPaths( $names, $versionInfo );
916 $this->setVar( 'wgImageMagickConvertCommand', '' );
918 $this->setVar( 'wgImageMagickConvertCommand', $convert );
919 $this->showMessage( 'config-imagemagick', $convert );
921 } elseif ( function_exists( 'imagejpeg' ) ) {
922 $this->showMessage( 'config-gd' );
925 $this->showMessage( 'config-no-scaling' );
936 protected function envCheckGit() {
937 $names = array( wfIsWindows() ?
'git.exe' : 'git' );
938 $versionInfo = array( '$1 --version', 'git version' );
940 $git = self
::locateExecutableInDefaultPaths( $names, $versionInfo );
943 $this->setVar( 'wgGitBin', $git );
944 $this->showMessage( 'config-git', $git );
946 $this->setVar( 'wgGitBin', false );
947 $this->showMessage( 'config-git-bad' );
953 * Environment check for the server hostname.
955 protected function envCheckServer() {
956 $server = $this->envGetDefaultServer();
957 $this->showMessage( 'config-using-server', $server );
958 $this->setVar( 'wgServer', $server );
963 * Helper function to be called from envCheckServer()
966 abstract protected function envGetDefaultServer();
969 * Environment check for setting $IP and $wgScriptPath.
972 protected function envCheckPath() {
974 $IP = dirname( dirname( __DIR__
) );
975 $this->setVar( 'IP', $IP );
977 $this->showMessage( 'config-using-uri', $this->getVar( 'wgServer' ), $this->getVar( 'wgScriptPath' ) );
982 * Environment check for setting the preferred PHP file extension.
985 protected function envCheckExtension() {
986 // @todo FIXME: Detect this properly
987 if ( defined( 'MW_INSTALL_PHP5_EXT' ) ) {
992 $this->setVar( 'wgScriptExtension', ".$ext" );
997 * Environment check for preferred locale in shell
1000 protected function envCheckShellLocale() {
1001 $os = php_uname( 's' );
1002 $supported = array( 'Linux', 'SunOS', 'HP-UX', 'Darwin' ); # Tested these
1004 if ( !in_array( $os, $supported ) ) {
1008 # Get a list of available locales.
1010 $lines = wfShellExec( '/usr/bin/locale -a', $ret );
1016 $lines = array_map( 'trim', explode( "\n", $lines ) );
1017 $candidatesByLocale = array();
1018 $candidatesByLang = array();
1020 foreach ( $lines as $line ) {
1021 if ( $line === '' ) {
1025 if ( !preg_match( '/^([a-zA-Z]+)(_[a-zA-Z]+|)\.(utf8|UTF-8)(@[a-zA-Z_]*|)$/i', $line, $m ) ) {
1029 list( , $lang, , , ) = $m;
1031 $candidatesByLocale[$m[0]] = $m;
1032 $candidatesByLang[$lang][] = $m;
1035 # Try the current value of LANG.
1036 if ( isset( $candidatesByLocale[getenv( 'LANG' )] ) ) {
1037 $this->setVar( 'wgShellLocale', getenv( 'LANG' ) );
1041 # Try the most common ones.
1042 $commonLocales = array( 'en_US.UTF-8', 'en_US.utf8', 'de_DE.UTF-8', 'de_DE.utf8' );
1043 foreach ( $commonLocales as $commonLocale ) {
1044 if ( isset( $candidatesByLocale[$commonLocale] ) ) {
1045 $this->setVar( 'wgShellLocale', $commonLocale );
1050 # Is there an available locale in the Wiki's language?
1051 $wikiLang = $this->getVar( 'wgLanguageCode' );
1053 if ( isset( $candidatesByLang[$wikiLang] ) ) {
1054 $m = reset( $candidatesByLang[$wikiLang] );
1055 $this->setVar( 'wgShellLocale', $m[0] );
1059 # Are there any at all?
1060 if ( count( $candidatesByLocale ) ) {
1061 $m = reset( $candidatesByLocale );
1062 $this->setVar( 'wgShellLocale', $m[0] );
1071 * Environment check for the permissions of the uploads directory
1074 protected function envCheckUploadsDirectory() {
1077 $dir = $IP . '/images/';
1078 $url = $this->getVar( 'wgServer' ) . $this->getVar( 'wgScriptPath' ) . '/images/';
1079 $safe = !$this->dirIsExecutable( $dir, $url );
1082 $this->showMessage( 'config-uploads-not-safe', $dir );
1088 * Checks if suhosin.get.max_value_length is set, and if so generate
1089 * a warning because it decreases ResourceLoader performance.
1092 protected function envCheckSuhosinMaxValueLength() {
1093 $maxValueLength = ini_get( 'suhosin.get.max_value_length' );
1094 if ( $maxValueLength > 0 && $maxValueLength < 1024 ) {
1095 // Only warn if the value is below the sane 1024
1096 $this->showMessage( 'config-suhosin-max-value-length', $maxValueLength );
1102 * Convert a hex string representing a Unicode code point to that code point.
1106 protected function unicodeChar( $c ) {
1110 } elseif ( $c <= 0x7FF ) {
1111 return chr( 0xC0 |
$c >> 6 ) . chr( 0x80 |
$c & 0x3F );
1112 } elseif ( $c <= 0xFFFF ) {
1113 return chr( 0xE0 |
$c >> 12 ) . chr( 0x80 |
$c >> 6 & 0x3F )
1114 . chr( 0x80 |
$c & 0x3F );
1115 } elseif ( $c <= 0x10FFFF ) {
1116 return chr( 0xF0 |
$c >> 18 ) . chr( 0x80 |
$c >> 12 & 0x3F )
1117 . chr( 0x80 |
$c >> 6 & 0x3F )
1118 . chr( 0x80 |
$c & 0x3F );
1125 * Check the libicu version
1127 protected function envCheckLibicu() {
1128 $utf8 = function_exists( 'utf8_normalize' );
1129 $intl = function_exists( 'normalizer_normalize' );
1132 * This needs to be updated something that the latest libicu
1133 * will properly normalize. This normalization was found at
1134 * http://www.unicode.org/versions/Unicode5.2.0/#Character_Additions
1135 * Note that we use the hex representation to create the code
1136 * points in order to avoid any Unicode-destroying during transit.
1138 $not_normal_c = $this->unicodeChar( "FA6C" );
1139 $normal_c = $this->unicodeChar( "242EE" );
1141 $useNormalizer = 'php';
1142 $needsUpdate = false;
1145 * We're going to prefer the pecl extension here unless
1146 * utf8_normalize is more up to date.
1149 $useNormalizer = 'utf8';
1150 $utf8 = utf8_normalize( $not_normal_c, UtfNormal
::UNORM_NFC
);
1151 if ( $utf8 !== $normal_c ) {
1152 $needsUpdate = true;
1156 $useNormalizer = 'intl';
1157 $intl = normalizer_normalize( $not_normal_c, Normalizer
::FORM_C
);
1158 if ( $intl !== $normal_c ) {
1159 $needsUpdate = true;
1163 // Uses messages 'config-unicode-using-php', 'config-unicode-using-utf8', 'config-unicode-using-intl'
1164 if ( $useNormalizer === 'php' ) {
1165 $this->showMessage( 'config-unicode-pure-php-warning' );
1167 $this->showMessage( 'config-unicode-using-' . $useNormalizer );
1168 if ( $needsUpdate ) {
1169 $this->showMessage( 'config-unicode-update-warning' );
1177 protected function envCheckCtype() {
1178 if ( !function_exists( 'ctype_digit' ) ) {
1179 $this->showError( 'config-ctype' );
1188 protected function envCheckJSON() {
1189 if ( !function_exists( 'json_decode' ) ) {
1190 $this->showError( 'config-json' );
1197 * Get an array of likely places we can find executables. Check a bunch
1198 * of known Unix-like defaults, as well as the PATH environment variable
1199 * (which should maybe make it work for Windows?)
1203 protected static function getPossibleBinPaths() {
1205 array( '/usr/bin', '/usr/local/bin', '/opt/csw/bin',
1206 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ),
1207 explode( PATH_SEPARATOR
, getenv( 'PATH' ) )
1212 * Search a path for any of the given executable names. Returns the
1213 * executable name if found. Also checks the version string returned
1214 * by each executable.
1216 * Used only by environment checks.
1218 * @param string $path path to search
1219 * @param array $names of executable names
1220 * @param $versionInfo Boolean false or array with two members:
1221 * 0 => Command to run for version check, with $1 for the full executable name
1222 * 1 => String to compare the output with
1224 * If $versionInfo is not false, only executables with a version
1225 * matching $versionInfo[1] will be returned.
1226 * @return bool|string
1228 public static function locateExecutable( $path, $names, $versionInfo = false ) {
1229 if ( !is_array( $names ) ) {
1230 $names = array( $names );
1233 foreach ( $names as $name ) {
1234 $command = $path . DIRECTORY_SEPARATOR
. $name;
1236 wfSuppressWarnings();
1237 $file_exists = file_exists( $command );
1238 wfRestoreWarnings();
1240 if ( $file_exists ) {
1241 if ( !$versionInfo ) {
1245 $file = str_replace( '$1', wfEscapeShellArg( $command ), $versionInfo[0] );
1246 if ( strstr( wfShellExec( $file ), $versionInfo[1] ) !== false ) {
1255 * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
1256 * @see locateExecutable()
1258 * @param $versionInfo bool
1259 * @return bool|string
1261 public static function locateExecutableInDefaultPaths( $names, $versionInfo = false ) {
1262 foreach ( self
::getPossibleBinPaths() as $path ) {
1263 $exe = self
::locateExecutable( $path, $names, $versionInfo );
1264 if ( $exe !== false ) {
1272 * Checks if scripts located in the given directory can be executed via the given URL.
1274 * Used only by environment checks.
1275 * @param $dir string
1276 * @param $url string
1277 * @return bool|int|string
1279 public function dirIsExecutable( $dir, $url ) {
1280 $scriptTypes = array(
1282 "<?php echo 'ex' . 'ec';",
1283 "#!/var/env php5\n<?php echo 'ex' . 'ec';",
1287 // it would be good to check other popular languages here, but it'll be slow.
1289 wfSuppressWarnings();
1291 foreach ( $scriptTypes as $ext => $contents ) {
1292 foreach ( $contents as $source ) {
1293 $file = 'exectest.' . $ext;
1295 if ( !file_put_contents( $dir . $file, $source ) ) {
1300 $text = Http
::get( $url . $file, array( 'timeout' => 3 ) );
1302 catch ( MWException
$e ) {
1303 // Http::get throws with allow_url_fopen = false and no curl extension.
1306 unlink( $dir . $file );
1308 if ( $text == 'exec' ) {
1309 wfRestoreWarnings();
1315 wfRestoreWarnings();
1321 * Checks for presence of an Apache module. Works only if PHP is running as an Apache module, too.
1323 * @param string $moduleName Name of module to check.
1326 public static function apacheModulePresent( $moduleName ) {
1327 if ( function_exists( 'apache_get_modules' ) && in_array( $moduleName, apache_get_modules() ) ) {
1330 // try it the hard way
1332 phpinfo( INFO_MODULES
);
1333 $info = ob_get_clean();
1334 return strpos( $info, $moduleName ) !== false;
1338 * ParserOptions are constructed before we determined the language, so fix it
1340 * @param $lang Language
1342 public function setParserLanguage( $lang ) {
1343 $this->parserOptions
->setTargetLanguage( $lang );
1344 $this->parserOptions
->setUserLang( $lang );
1348 * Overridden by WebInstaller to provide lastPage parameters.
1349 * @param $page string
1352 protected function getDocUrl( $page ) {
1353 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1357 * Finds extensions that follow the format /extensions/Name/Name.php,
1358 * and returns an array containing the value for 'Name' for each found extension.
1362 public function findExtensions() {
1363 if ( $this->getVar( 'IP' ) === null ) {
1367 $extDir = $this->getVar( 'IP' ) . '/extensions';
1368 if ( !is_readable( $extDir ) ||
!is_dir( $extDir ) ) {
1372 $dh = opendir( $extDir );
1374 while ( ( $file = readdir( $dh ) ) !== false ) {
1375 if ( !is_dir( "$extDir/$file" ) ) {
1378 if ( file_exists( "$extDir/$file/$file.php" ) ) {
1383 natcasesort( $exts );
1389 * Installs the auto-detected extensions.
1393 protected function includeExtensions() {
1395 $exts = $this->getVar( '_Extensions' );
1396 $IP = $this->getVar( 'IP' );
1399 * We need to include DefaultSettings before including extensions to avoid
1400 * warnings about unset variables. However, the only thing we really
1401 * want here is $wgHooks['LoadExtensionSchemaUpdates']. This won't work
1402 * if the extension has hidden hook registration in $wgExtensionFunctions,
1403 * but we're not opening that can of worms
1404 * @see https://bugzilla.wikimedia.org/show_bug.cgi?id=26857
1406 global $wgAutoloadClasses;
1407 $wgAutoloadClasses = array();
1409 require "$IP/includes/DefaultSettings.php";
1411 foreach ( $exts as $e ) {
1412 require_once "$IP/extensions/$e/$e.php";
1415 $hooksWeWant = isset( $wgHooks['LoadExtensionSchemaUpdates'] ) ?
1416 $wgHooks['LoadExtensionSchemaUpdates'] : array();
1418 // Unset everyone else's hooks. Lord knows what someone might be doing
1419 // in ParserFirstCallInit (see bug 27171)
1420 $GLOBALS['wgHooks'] = array( 'LoadExtensionSchemaUpdates' => $hooksWeWant );
1422 return Status
::newGood();
1426 * Get an array of install steps. Should always be in the format of
1428 * 'name' => 'someuniquename',
1429 * 'callback' => array( $obj, 'method' ),
1431 * There must be a config-install-$name message defined per step, which will
1432 * be shown on install.
1434 * @param $installer DatabaseInstaller so we can make callbacks
1437 protected function getInstallSteps( DatabaseInstaller
$installer ) {
1438 $coreInstallSteps = array(
1439 array( 'name' => 'database', 'callback' => array( $installer, 'setupDatabase' ) ),
1440 array( 'name' => 'tables', 'callback' => array( $installer, 'createTables' ) ),
1441 array( 'name' => 'interwiki', 'callback' => array( $installer, 'populateInterwikiTable' ) ),
1442 array( 'name' => 'stats', 'callback' => array( $this, 'populateSiteStats' ) ),
1443 array( 'name' => 'keys', 'callback' => array( $this, 'generateKeys' ) ),
1444 array( 'name' => 'sysop', 'callback' => array( $this, 'createSysop' ) ),
1445 array( 'name' => 'mainpage', 'callback' => array( $this, 'createMainpage' ) ),
1448 // Build the array of install steps starting from the core install list,
1449 // then adding any callbacks that wanted to attach after a given step
1450 foreach ( $coreInstallSteps as $step ) {
1451 $this->installSteps
[] = $step;
1452 if ( isset( $this->extraInstallSteps
[$step['name']] ) ) {
1453 $this->installSteps
= array_merge(
1454 $this->installSteps
,
1455 $this->extraInstallSteps
[$step['name']]
1460 // Prepend any steps that want to be at the beginning
1461 if ( isset( $this->extraInstallSteps
['BEGINNING'] ) ) {
1462 $this->installSteps
= array_merge(
1463 $this->extraInstallSteps
['BEGINNING'],
1468 // Extensions should always go first, chance to tie into hooks and such
1469 if ( count( $this->getVar( '_Extensions' ) ) ) {
1470 array_unshift( $this->installSteps
,
1471 array( 'name' => 'extensions', 'callback' => array( $this, 'includeExtensions' ) )
1473 $this->installSteps
[] = array(
1474 'name' => 'extension-tables',
1475 'callback' => array( $installer, 'createExtensionTables' )
1478 return $this->installSteps
;
1482 * Actually perform the installation.
1484 * @param array $startCB A callback array for the beginning of each step
1485 * @param array $endCB A callback array for the end of each step
1487 * @return Array of Status objects
1489 public function performInstallation( $startCB, $endCB ) {
1490 $installResults = array();
1491 $installer = $this->getDBInstaller();
1492 $installer->preInstall();
1493 $steps = $this->getInstallSteps( $installer );
1494 foreach ( $steps as $stepObj ) {
1495 $name = $stepObj['name'];
1496 call_user_func_array( $startCB, array( $name ) );
1498 // Perform the callback step
1499 $status = call_user_func( $stepObj['callback'], $installer );
1501 // Output and save the results
1502 call_user_func( $endCB, $name, $status );
1503 $installResults[$name] = $status;
1505 // If we've hit some sort of fatal, we need to bail.
1506 // Callback already had a chance to do output above.
1507 if ( !$status->isOk() ) {
1511 if ( $status->isOk() ) {
1512 $this->setVar( '_InstallDone', true );
1514 return $installResults;
1518 * Generate $wgSecretKey. Will warn if we had to use an insecure random source.
1522 public function generateKeys() {
1523 $keys = array( 'wgSecretKey' => 64 );
1524 if ( strval( $this->getVar( 'wgUpgradeKey' ) ) === '' ) {
1525 $keys['wgUpgradeKey'] = 16;
1527 return $this->doGenerateKeys( $keys );
1531 * Generate a secret value for variables using our CryptRand generator.
1532 * Produce a warning if the random source was insecure.
1534 * @param $keys Array
1537 protected function doGenerateKeys( $keys ) {
1538 $status = Status
::newGood();
1541 foreach ( $keys as $name => $length ) {
1542 $secretKey = MWCryptRand
::generateHex( $length, true );
1543 if ( !MWCryptRand
::wasStrong() ) {
1547 $this->setVar( $name, $secretKey );
1551 $names = array_keys( $keys );
1552 $names = preg_replace( '/^(.*)$/', '\$$1', $names );
1554 $status->warning( 'config-insecure-keys', $wgLang->listToText( $names ), count( $names ) );
1561 * Create the first user account, grant it sysop and bureaucrat rights
1565 protected function createSysop() {
1566 $name = $this->getVar( '_AdminName' );
1567 $user = User
::newFromName( $name );
1570 // We should've validated this earlier anyway!
1571 return Status
::newFatal( 'config-admin-error-user', $name );
1574 if ( $user->idForName() == 0 ) {
1575 $user->addToDatabase();
1578 $user->setPassword( $this->getVar( '_AdminPassword' ) );
1579 } catch ( PasswordError
$pwe ) {
1580 return Status
::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
1583 $user->addGroup( 'sysop' );
1584 $user->addGroup( 'bureaucrat' );
1585 if ( $this->getVar( '_AdminEmail' ) ) {
1586 $user->setEmail( $this->getVar( '_AdminEmail' ) );
1588 $user->saveSettings();
1590 // Update user count
1591 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
1592 $ssUpdate->doUpdate();
1594 $status = Status
::newGood();
1596 if ( $this->getVar( '_Subscribe' ) && $this->getVar( '_AdminEmail' ) ) {
1597 $this->subscribeToMediaWikiAnnounce( $status );
1606 private function subscribeToMediaWikiAnnounce( Status
$s ) {
1608 'email' => $this->getVar( '_AdminEmail' ),
1613 // Mailman doesn't support as many languages as we do, so check to make
1614 // sure their selected language is available
1615 $myLang = $this->getVar( '_UserLang' );
1616 if ( in_array( $myLang, $this->mediaWikiAnnounceLanguages
) ) {
1617 $myLang = $myLang == 'pt-br' ?
'pt_BR' : $myLang; // rewrite to Mailman's pt_BR
1618 $params['language'] = $myLang;
1621 if ( MWHttpRequest
::canMakeRequests() ) {
1622 $res = MWHttpRequest
::factory( $this->mediaWikiAnnounceUrl
,
1623 array( 'method' => 'POST', 'postData' => $params ) )->execute();
1624 if ( !$res->isOK() ) {
1625 $s->warning( 'config-install-subscribe-fail', $res->getMessage() );
1628 $s->warning( 'config-install-subscribe-notpossible' );
1633 * Insert Main Page with default content.
1635 * @param $installer DatabaseInstaller
1638 protected function createMainpage( DatabaseInstaller
$installer ) {
1639 $status = Status
::newGood();
1641 $page = WikiPage
::factory( Title
::newMainPage() );
1642 $content = new WikitextContent(
1643 wfMessage( 'mainpagetext' )->inContentLanguage()->text() . "\n\n" .
1644 wfMessage( 'mainpagedocfooter' )->inContentLanguage()->text()
1647 $page->doEditContent( $content,
1651 User
::newFromName( 'MediaWiki default' ) );
1652 } catch ( MWException
$e ) {
1653 //using raw, because $wgShowExceptionDetails can not be set yet
1654 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
1661 * Override the necessary bits of the config to run an installation.
1663 public static function overrideConfig() {
1664 define( 'MW_NO_SESSION', 1 );
1666 // Don't access the database
1667 $GLOBALS['wgUseDatabaseMessages'] = false;
1668 // Don't cache langconv tables
1669 $GLOBALS['wgLanguageConverterCacheType'] = CACHE_NONE
;
1671 $GLOBALS['wgShowExceptionDetails'] = true;
1672 // Don't break forms
1673 $GLOBALS['wgExternalLinkTarget'] = '_blank';
1675 // Extended debugging
1676 $GLOBALS['wgShowSQLErrors'] = true;
1677 $GLOBALS['wgShowDBErrorBacktrace'] = true;
1679 // Allow multiple ob_flush() calls
1680 $GLOBALS['wgDisableOutputCompression'] = true;
1682 // Use a sensible cookie prefix (not my_wiki)
1683 $GLOBALS['wgCookiePrefix'] = 'mw_installer';
1685 // Some of the environment checks make shell requests, remove limits
1686 $GLOBALS['wgMaxShellMemory'] = 0;
1690 * Add an installation step following the given step.
1692 * @param array $callback A valid installation callback array, in this form:
1693 * array( 'name' => 'some-unique-name', 'callback' => array( $obj, 'function' ) );
1694 * @param string $findStep the step to find. Omit to put the step at the beginning
1696 public function addInstallStep( $callback, $findStep = 'BEGINNING' ) {
1697 $this->extraInstallSteps
[$findStep][] = $callback;
1701 * Disable the time limit for execution.
1702 * Some long-running pages (Install, Upgrade) will want to do this
1704 protected function disableTimeLimit() {
1705 wfSuppressWarnings();
1706 set_time_limit( 0 );
1707 wfRestoreWarnings();