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(
79 * A list of environment check methods called by doEnvironmentChecks().
80 * These may output warnings using showMessage(), and/or abort the
81 * installation process by returning false.
85 protected $envChecks = array(
87 'envCheckRegisterGlobals',
90 'envCheckMagicQuotes',
91 'envCheckMagicSybase',
103 'envCheckShellLocale',
104 'envCheckUploadsDirectory',
106 'envCheckSuhosinMaxValueLength',
110 * MediaWiki configuration globals that will eventually be passed through
111 * to LocalSettings.php. The names only are given here, the defaults
112 * typically come from DefaultSettings.php.
116 protected $defaultVarNames = array(
128 'wgEmailAuthentication',
131 'wgImageMagickConvertCommand',
136 'wgDeletedDirectory',
141 'wgUseInstantCommons',
144 'wgResourceLoaderMaxQueryLength',
148 * Variables that are stored alongside globals, and are used for any
149 * configuration of the installation process aside from the MediaWiki
150 * configuration. Map of names to defaults.
154 protected $internalDefaults = array(
156 '_Environment' => false,
157 '_CompiledDBs' => array(),
158 '_SafeMode' => false,
159 '_RaiseMemory' => false,
160 '_UpgradeDone' => false,
161 '_InstallDone' => false,
162 '_Caches' => array(),
163 '_InstallPassword' => '',
164 '_SameAccount' => true,
165 '_CreateDBAccount' => false,
166 '_NamespaceType' => 'site-name',
167 '_AdminName' => '', // will be set later, when the user selects language
168 '_AdminPassword' => '',
169 '_AdminPassword2' => '',
171 '_Subscribe' => false,
172 '_SkipOptional' => 'continue',
173 '_RightsProfile' => 'wiki',
174 '_LicenseCode' => 'none',
176 '_Extensions' => array(),
177 '_MemCachedServers' => '',
178 '_UpgradeKeySupplied' => false,
179 '_ExistingDBSettings' => false,
183 * The actual list of installation steps. This will be initialized by getInstallSteps()
187 private $installSteps = array();
190 * Extra steps for installation, for things like DatabaseInstallers to modify
194 protected $extraInstallSteps = array();
197 * Known object cache types and the functions used to test for their existence.
201 protected $objectCaches = array(
202 'xcache' => 'xcache_get',
203 'apc' => 'apc_fetch',
204 'eaccel' => 'eaccelerator_get',
205 'wincache' => 'wincache_ucache_get'
209 * User rights profiles.
213 public $rightsProfiles = array(
216 '*' => array( 'edit' => false )
220 'createaccount' => false,
226 'createaccount' => false,
238 public $licenses = array(
240 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
241 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
243 'cc-by-nc-sa' => array(
244 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/',
245 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png',
248 'url' => 'https://creativecommons.org/publicdomain/zero/1.0/',
249 'icon' => '{$wgStylePath}/common/images/cc-0.png',
252 'url' => 'http://creativecommons.org/licenses/publicdomain/',
253 'icon' => '{$wgStylePath}/common/images/public-domain.png',
256 'url' => 'http://www.gnu.org/licenses/old-licenses/fdl-1.2.html',
257 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
259 'gfdl-current' => array(
260 'url' => 'http://www.gnu.org/copyleft/fdl.html',
261 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
268 'cc-choose' => array(
269 // Details will be filled in by the selector.
277 * URL to mediawiki-announce subscription
279 protected $mediaWikiAnnounceUrl = 'https://lists.wikimedia.org/mailman/subscribe/mediawiki-announce';
282 * Supported language codes for Mailman
284 protected $mediaWikiAnnounceLanguages = array(
285 'ca', 'cs', 'da', 'de', 'en', 'es', 'et', 'eu', 'fi', 'fr', 'hr', 'hu',
286 'it', 'ja', 'ko', 'lt', 'nl', 'no', 'pl', 'pt', 'pt-br', 'ro', 'ru',
287 'sl', 'sr', 'sv', 'tr', 'uk'
291 * UI interface for displaying a short message
292 * The parameters are like parameters to wfMsg().
293 * The messages will be in wikitext format, which will be converted to an
294 * output format such as HTML or text before being sent to the user.
296 public abstract function showMessage( $msg /*, ... */ );
299 * Same as showMessage(), but for displaying errors
301 public abstract function showError( $msg /*, ... */ );
304 * Show a message to the installing user by using a Status object
305 * @param $status Status
307 public abstract function showStatusMessage( Status $status );
310 * Constructor, always call this from child classes.
312 public function __construct() {
313 global $wgExtensionMessagesFiles, $wgUser, $wgHooks;
315 // Disable the i18n cache and LoadBalancer
316 Language::getLocalisationCache()->disableBackend();
317 LBFactory::disableBackend();
319 // Load the installer's i18n file.
320 $wgExtensionMessagesFiles['MediawikiInstaller'] =
321 dirname( __FILE__ ) . '/Installer.i18n.php';
323 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
324 $wgUser = User::newFromId( 0 );
326 $this->settings = $this->internalDefaults;
328 foreach ( $this->defaultVarNames as $var ) {
329 $this->settings[$var] = $GLOBALS[$var];
332 foreach ( self::getDBTypes() as $type ) {
333 $installer = $this->getDBInstaller( $type );
335 if ( !$installer->isCompiled() ) {
339 $defaults = $installer->getGlobalDefaults();
341 foreach ( $installer->getGlobalNames() as $var ) {
342 if ( isset( $defaults[$var] ) ) {
343 $this->settings[$var] = $defaults[$var];
345 $this->settings[$var] = $GLOBALS[$var];
350 $this->parserTitle = Title::newFromText( 'Installer' );
351 $this->parserOptions = new ParserOptions; // language will be wrong :(
352 $this->parserOptions->setEditSection( false );
356 * Get a list of known DB types.
358 public static function getDBTypes() {
359 return self::$dbTypes;
363 * Do initial checks of the PHP environment. Set variables according to
364 * the observed environment.
366 * It's possible that this may be called under the CLI SAPI, not the SAPI
367 * that the wiki will primarily run under. In that case, the subclass should
368 * initialise variables such as wgScriptPath, before calling this function.
370 * Under the web subclass, it can already be assumed that PHP 5+ is in use
371 * and that sessions are working.
375 public function doEnvironmentChecks() {
376 $phpVersion = phpversion();
377 if( version_compare( $phpVersion, self::MINIMUM_PHP_VERSION, '>=' ) ) {
378 $this->showMessage( 'config-env-php', $phpVersion );
381 $this->showMessage( 'config-env-php-toolow', $phpVersion, self::MINIMUM_PHP_VERSION );
386 foreach ( $this->envChecks as $check ) {
387 $status = $this->$check();
388 if ( $status === false ) {
394 $this->setVar( '_Environment', $good );
396 return $good ? Status::newGood() : Status::newFatal( 'config-env-bad' );
400 * Set a MW configuration variable, or internal installer configuration variable.
402 * @param $name String
403 * @param $value Mixed
405 public function setVar( $name, $value ) {
406 $this->settings[$name] = $value;
410 * Get an MW configuration variable, or internal installer configuration variable.
411 * The defaults come from $GLOBALS (ultimately DefaultSettings.php).
412 * Installer variables are typically prefixed by an underscore.
414 * @param $name String
415 * @param $default Mixed
419 public function getVar( $name, $default = null ) {
420 if ( !isset( $this->settings[$name] ) ) {
423 return $this->settings[$name];
428 * Get an instance of DatabaseInstaller for the specified DB type.
430 * @param $type Mixed: DB installer for which is needed, false to use default.
432 * @return DatabaseInstaller
434 public function getDBInstaller( $type = false ) {
436 $type = $this->getVar( 'wgDBtype' );
439 $type = strtolower( $type );
441 if ( !isset( $this->dbInstallers[$type] ) ) {
442 $class = ucfirst( $type ). 'Installer';
443 $this->dbInstallers[$type] = new $class( $this );
446 return $this->dbInstallers[$type];
450 * Determine if LocalSettings.php exists. If it does, return its variables,
451 * merged with those from AdminSettings.php, as an array.
455 public static function getExistingLocalSettings() {
458 wfSuppressWarnings();
459 $_lsExists = file_exists( "$IP/LocalSettings.php" );
467 require( "$IP/includes/DefaultSettings.php" );
468 require( "$IP/LocalSettings.php" );
469 if ( file_exists( "$IP/AdminSettings.php" ) ) {
470 require( "$IP/AdminSettings.php" );
472 return get_defined_vars();
476 * Get a fake password for sending back to the user in HTML.
477 * This is a security mechanism to avoid compromise of the password in the
478 * event of session ID compromise.
480 * @param $realPassword String
484 public function getFakePassword( $realPassword ) {
485 return str_repeat( '*', strlen( $realPassword ) );
489 * Set a variable which stores a password, except if the new value is a
490 * fake password in which case leave it as it is.
492 * @param $name String
493 * @param $value Mixed
495 public function setPassword( $name, $value ) {
496 if ( !preg_match( '/^\*+$/', $value ) ) {
497 $this->setVar( $name, $value );
502 * On POSIX systems return the primary group of the webserver we're running under.
503 * On other systems just returns null.
505 * This is used to advice the user that he should chgrp his mw-config/data/images directory as the
506 * webserver user before he can install.
508 * Public because SqliteInstaller needs it, and doesn't subclass Installer.
512 public static function maybeGetWebserverPrimaryGroup() {
513 if ( !function_exists( 'posix_getegid' ) || !function_exists( 'posix_getpwuid' ) ) {
514 # I don't know this, this isn't UNIX.
518 # posix_getegid() *not* getmygid() because we want the group of the webserver,
519 # not whoever owns the current script.
520 $gid = posix_getegid();
521 $getpwuid = posix_getpwuid( $gid );
522 $group = $getpwuid['name'];
528 * Convert wikitext $text to HTML.
530 * This is potentially error prone since many parser features require a complete
531 * installed MW database. The solution is to just not use those features when you
532 * write your messages. This appears to work well enough. Basic formatting and
533 * external links work just fine.
535 * But in case a translator decides to throw in a #ifexist or internal link or
536 * whatever, this function is guarded to catch the attempted DB access and to present
537 * some fallback text.
539 * @param $text String
540 * @param $lineStart Boolean
543 public function parse( $text, $lineStart = false ) {
547 $out = $wgParser->parse( $text, $this->parserTitle, $this->parserOptions, $lineStart );
548 $html = $out->getText();
549 } catch ( DBAccessError $e ) {
550 $html = '<!--DB access attempted during parse--> ' . htmlspecialchars( $text );
552 if ( !empty( $this->debug ) ) {
553 $html .= "<!--\n" . $e->getTraceAsString() . "\n-->";
560 public function getParserOptions() {
561 return $this->parserOptions;
564 public function disableLinkPopups() {
565 $this->parserOptions->setExternalLinkTarget( false );
568 public function restoreLinkPopups() {
569 global $wgExternalLinkTarget;
570 $this->parserOptions->setExternalLinkTarget( $wgExternalLinkTarget );
574 * Install step which adds a row to the site_stats table with appropriate
577 public function populateSiteStats( DatabaseInstaller $installer ) {
578 $status = $installer->getConnection();
579 if ( !$status->isOK() ) {
582 $status->value->insert( 'site_stats', array(
584 'ss_total_views' => 0,
585 'ss_total_edits' => 0,
586 'ss_good_articles' => 0,
587 'ss_total_pages' => 0,
591 __METHOD__, 'IGNORE' );
592 return Status::newGood();
596 * Exports all wg* variables stored by the installer into global scope.
598 public function exportVars() {
599 foreach ( $this->settings as $name => $value ) {
600 if ( substr( $name, 0, 2 ) == 'wg' ) {
601 $GLOBALS[$name] = $value;
607 * Environment check for DB types.
609 protected function envCheckDB() {
612 $compiledDBs = array();
615 foreach ( self::getDBTypes() as $name ) {
616 $db = $this->getDBInstaller( $name );
617 $readableName = wfMsg( 'config-type-' . $name );
619 if ( $db->isCompiled() ) {
620 $compiledDBs[] = $name;
622 $allNames[] = $readableName;
625 $this->setVar( '_CompiledDBs', $compiledDBs );
627 if ( !$compiledDBs ) {
628 $this->showError( 'config-no-db', $wgLang->commaList( $allNames ) );
629 // FIXME: this only works for the web installer!
633 // Check for FTS3 full-text search module
634 $sqlite = $this->getDBInstaller( 'sqlite' );
635 if ( $sqlite->isCompiled() ) {
636 if( DatabaseSqlite::getFulltextSearchModule() != 'FTS3' ) {
637 $this->showMessage( 'config-no-fts3' );
643 * Environment check for register_globals.
645 protected function envCheckRegisterGlobals() {
646 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
647 $this->showMessage( 'config-register-globals' );
652 * Some versions of libxml+PHP break < and > encoding horribly
654 protected function envCheckBrokenXML() {
655 $test = new PhpXmlBugTester();
657 $this->showError( 'config-brokenlibxml' );
663 * Test PHP (probably 5.3.1, but it could regress again) to make sure that
664 * reference parameters to __call() are not converted to null
666 protected function envCheckPHP531() {
667 $test = new PhpRefCallBugTester;
670 $this->showError( 'config-using531', phpversion() );
676 * Environment check for magic_quotes_runtime.
678 protected function envCheckMagicQuotes() {
679 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
680 $this->showError( 'config-magic-quotes-runtime' );
686 * Environment check for magic_quotes_sybase.
688 protected function envCheckMagicSybase() {
689 if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
690 $this->showError( 'config-magic-quotes-sybase' );
696 * Environment check for mbstring.func_overload.
698 protected function envCheckMbstring() {
699 if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
700 $this->showError( 'config-mbstring' );
706 * Environment check for zend.ze1_compatibility_mode.
708 protected function envCheckZE1() {
709 if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
710 $this->showError( 'config-ze1' );
716 * Environment check for safe_mode.
718 protected function envCheckSafeMode() {
719 if ( wfIniGetBool( 'safe_mode' ) ) {
720 $this->setVar( '_SafeMode', true );
721 $this->showMessage( 'config-safe-mode' );
726 * Environment check for the XML module.
728 protected function envCheckXML() {
729 if ( !function_exists( "utf8_encode" ) ) {
730 $this->showError( 'config-xml-bad' );
736 * Environment check for the PCRE module.
738 protected function envCheckPCRE() {
739 if ( !function_exists( 'preg_match' ) ) {
740 $this->showError( 'config-pcre' );
743 wfSuppressWarnings();
744 $regexd = preg_replace( '/[\x{0430}-\x{04FF}]/iu', '', '-АБВГД-' );
746 if ( $regexd != '--' ) {
747 $this->showError( 'config-pcre-no-utf8' );
753 * Environment check for available memory.
755 protected function envCheckMemory() {
756 $limit = ini_get( 'memory_limit' );
758 if ( !$limit || $limit == -1 ) {
762 $n = wfShorthandToInteger( $limit );
764 if( $n < $this->minMemorySize * 1024 * 1024 ) {
765 $newLimit = "{$this->minMemorySize}M";
767 if( ini_set( "memory_limit", $newLimit ) === false ) {
768 $this->showMessage( 'config-memory-bad', $limit );
770 $this->showMessage( 'config-memory-raised', $limit, $newLimit );
771 $this->setVar( '_RaiseMemory', true );
779 * Environment check for compiled object cache types.
781 protected function envCheckCache() {
783 foreach ( $this->objectCaches as $name => $function ) {
784 if ( function_exists( $function ) ) {
785 $caches[$name] = true;
790 $this->showMessage( 'config-no-cache' );
793 $this->setVar( '_Caches', $caches );
797 * Search for GNU diff3.
799 protected function envCheckDiff3() {
800 $names = array( "gdiff3", "diff3", "diff3.exe" );
801 $versionInfo = array( '$1 --version 2>&1', 'GNU diffutils' );
803 $diff3 = self::locateExecutableInDefaultPaths( $names, $versionInfo );
806 $this->setVar( 'wgDiff3', $diff3 );
808 $this->setVar( 'wgDiff3', false );
809 $this->showMessage( 'config-diff3-bad' );
814 * Environment check for ImageMagick and GD.
816 protected function envCheckGraphics() {
817 $names = array( wfIsWindows() ? 'convert.exe' : 'convert' );
818 $convert = self::locateExecutableInDefaultPaths( $names, array( '$1 -version', 'ImageMagick' ) );
820 $this->setVar( 'wgImageMagickConvertCommand', '' );
822 $this->setVar( 'wgImageMagickConvertCommand', $convert );
823 $this->showMessage( 'config-imagemagick', $convert );
825 } elseif ( function_exists( 'imagejpeg' ) ) {
826 $this->showMessage( 'config-gd' );
829 $this->showMessage( 'config-no-scaling' );
834 * Environment check for setting $IP and $wgScriptPath.
836 protected function envCheckPath() {
838 $IP = dirname( dirname( dirname( __FILE__ ) ) );
840 $this->setVar( 'IP', $IP );
842 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
843 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
844 // to get the path to the current script... hopefully it's reliable. SIGH
845 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
846 $path = $_SERVER['PHP_SELF'];
847 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
848 $path = $_SERVER['SCRIPT_NAME'];
849 } elseif ( $this->getVar( 'wgScriptPath' ) ) {
850 // Some kind soul has set it for us already (e.g. debconf)
853 $this->showError( 'config-no-uri' );
857 $uri = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
858 $this->setVar( 'wgScriptPath', $uri );
862 * Environment check for setting the preferred PHP file extension.
864 protected function envCheckExtension() {
865 // FIXME: detect this properly
866 if ( defined( 'MW_INSTALL_PHP5_EXT' ) ) {
871 $this->setVar( 'wgScriptExtension', ".$ext" );
877 protected function envCheckShellLocale() {
878 $os = php_uname( 's' );
879 $supported = array( 'Linux', 'SunOS', 'HP-UX', 'Darwin' ); # Tested these
881 if ( !in_array( $os, $supported ) ) {
885 # Get a list of available locales.
887 $lines = wfShellExec( '/usr/bin/locale -a', $ret );
893 $lines = wfArrayMap( 'trim', explode( "\n", $lines ) );
894 $candidatesByLocale = array();
895 $candidatesByLang = array();
897 foreach ( $lines as $line ) {
898 if ( $line === '' ) {
902 if ( !preg_match( '/^([a-zA-Z]+)(_[a-zA-Z]+|)\.(utf8|UTF-8)(@[a-zA-Z_]*|)$/i', $line, $m ) ) {
906 list( $all, $lang, $territory, $charset, $modifier ) = $m;
908 $candidatesByLocale[$m[0]] = $m;
909 $candidatesByLang[$lang][] = $m;
912 # Try the current value of LANG.
913 if ( isset( $candidatesByLocale[ getenv( 'LANG' ) ] ) ) {
914 $this->setVar( 'wgShellLocale', getenv( 'LANG' ) );
918 # Try the most common ones.
919 $commonLocales = array( 'en_US.UTF-8', 'en_US.utf8', 'de_DE.UTF-8', 'de_DE.utf8' );
920 foreach ( $commonLocales as $commonLocale ) {
921 if ( isset( $candidatesByLocale[$commonLocale] ) ) {
922 $this->setVar( 'wgShellLocale', $commonLocale );
927 # Is there an available locale in the Wiki's language?
928 $wikiLang = $this->getVar( 'wgLanguageCode' );
930 if ( isset( $candidatesByLang[$wikiLang] ) ) {
931 $m = reset( $candidatesByLang[$wikiLang] );
932 $this->setVar( 'wgShellLocale', $m[0] );
936 # Are there any at all?
937 if ( count( $candidatesByLocale ) ) {
938 $m = reset( $candidatesByLocale );
939 $this->setVar( 'wgShellLocale', $m[0] );
950 protected function envCheckUploadsDirectory() {
951 global $IP, $wgServer;
953 $dir = $IP . '/images/';
954 $url = $wgServer . $this->getVar( 'wgScriptPath' ) . '/images/';
955 $safe = !$this->dirIsExecutable( $dir, $url );
960 $this->showMessage( 'config-uploads-not-safe', $dir );
965 * Checks if suhosin.get.max_value_length is set, and if so, sets
966 * $wgResourceLoaderMaxQueryLength to that value in the generated
969 protected function envCheckSuhosinMaxValueLength() {
970 $maxValueLength = ini_get( 'suhosin.get.max_value_length' );
971 if ( $maxValueLength > 0 ) {
972 $this->showMessage( 'config-suhosin-max-value-length', $maxValueLength );
974 $maxValueLength = -1;
976 $this->setVar( 'wgResourceLoaderMaxQueryLength', $maxValueLength );
980 * Convert a hex string representing a Unicode code point to that code point.
984 protected function unicodeChar( $c ) {
988 } else if ($c <= 0x7FF) {
989 return chr(0xC0 | $c >> 6) . chr(0x80 | $c & 0x3F);
990 } else if ($c <= 0xFFFF) {
991 return chr(0xE0 | $c >> 12) . chr(0x80 | $c >> 6 & 0x3F)
992 . chr(0x80 | $c & 0x3F);
993 } else if ($c <= 0x10FFFF) {
994 return chr(0xF0 | $c >> 18) . chr(0x80 | $c >> 12 & 0x3F)
995 . chr(0x80 | $c >> 6 & 0x3F)
996 . chr(0x80 | $c & 0x3F);
1004 * Check the libicu version
1006 protected function envCheckLibicu() {
1007 $utf8 = function_exists( 'utf8_normalize' );
1008 $intl = function_exists( 'normalizer_normalize' );
1011 * This needs to be updated something that the latest libicu
1012 * will properly normalize. This normalization was found at
1013 * http://www.unicode.org/versions/Unicode5.2.0/#Character_Additions
1014 * Note that we use the hex representation to create the code
1015 * points in order to avoid any Unicode-destroying during transit.
1017 $not_normal_c = $this->unicodeChar("FA6C");
1018 $normal_c = $this->unicodeChar("242EE");
1020 $useNormalizer = 'php';
1021 $needsUpdate = false;
1024 * We're going to prefer the pecl extension here unless
1025 * utf8_normalize is more up to date.
1028 $useNormalizer = 'utf8';
1029 $utf8 = utf8_normalize( $not_normal_c, UNORM_NFC );
1030 if ( $utf8 !== $normal_c ) $needsUpdate = true;
1033 $useNormalizer = 'intl';
1034 $intl = normalizer_normalize( $not_normal_c, Normalizer::FORM_C );
1035 if ( $intl !== $normal_c ) $needsUpdate = true;
1038 // Uses messages 'config-unicode-using-php', 'config-unicode-using-utf8', 'config-unicode-using-intl'
1039 if( $useNormalizer === 'php' ) {
1040 $this->showMessage( 'config-unicode-pure-php-warning' );
1042 $this->showMessage( 'config-unicode-using-' . $useNormalizer );
1043 if( $needsUpdate ) {
1044 $this->showMessage( 'config-unicode-update-warning' );
1050 * Get an array of likely places we can find executables. Check a bunch
1051 * of known Unix-like defaults, as well as the PATH environment variable
1052 * (which should maybe make it work for Windows?)
1056 protected static function getPossibleBinPaths() {
1058 array( '/usr/bin', '/usr/local/bin', '/opt/csw/bin',
1059 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ),
1060 explode( PATH_SEPARATOR, getenv( 'PATH' ) )
1065 * Search a path for any of the given executable names. Returns the
1066 * executable name if found. Also checks the version string returned
1067 * by each executable.
1069 * Used only by environment checks.
1071 * @param $path String: path to search
1072 * @param $names Array of executable names
1073 * @param $versionInfo Boolean false or array with two members:
1074 * 0 => Command to run for version check, with $1 for the full executable name
1075 * 1 => String to compare the output with
1077 * If $versionInfo is not false, only executables with a version
1078 * matching $versionInfo[1] will be returned.
1080 public static function locateExecutable( $path, $names, $versionInfo = false ) {
1081 if ( !is_array( $names ) ) {
1082 $names = array( $names );
1085 foreach ( $names as $name ) {
1086 $command = $path . DIRECTORY_SEPARATOR . $name;
1088 wfSuppressWarnings();
1089 $file_exists = file_exists( $command );
1090 wfRestoreWarnings();
1092 if ( $file_exists ) {
1093 if ( !$versionInfo ) {
1097 $file = str_replace( '$1', wfEscapeShellArg( $command ), $versionInfo[0] );
1098 if ( strstr( wfShellExec( $file ), $versionInfo[1] ) !== false ) {
1107 * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
1108 * @see locateExecutable()
1110 public static function locateExecutableInDefaultPaths( $names, $versionInfo = false ) {
1111 foreach( self::getPossibleBinPaths() as $path ) {
1112 $exe = self::locateExecutable( $path, $names, $versionInfo );
1113 if( $exe !== false ) {
1121 * Checks if scripts located in the given directory can be executed via the given URL.
1123 * Used only by environment checks.
1125 public function dirIsExecutable( $dir, $url ) {
1126 $scriptTypes = array(
1128 "<?php echo 'ex' . 'ec';",
1129 "#!/var/env php5\n<?php echo 'ex' . 'ec';",
1133 // it would be good to check other popular languages here, but it'll be slow.
1135 wfSuppressWarnings();
1137 foreach ( $scriptTypes as $ext => $contents ) {
1138 foreach ( $contents as $source ) {
1139 $file = 'exectest.' . $ext;
1141 if ( !file_put_contents( $dir . $file, $source ) ) {
1146 $text = Http::get( $url . $file, array( 'timeout' => 3 ) );
1148 catch( MWException $e ) {
1149 // Http::get throws with allow_url_fopen = false and no curl extension.
1152 unlink( $dir . $file );
1154 if ( $text == 'exec' ) {
1155 wfRestoreWarnings();
1161 wfRestoreWarnings();
1167 * ParserOptions are constructed before we determined the language, so fix it
1169 public function setParserLanguage( $lang ) {
1170 $this->parserOptions->setTargetLanguage( $lang );
1171 $this->parserOptions->setUserLang( $lang->getCode() );
1175 * Overridden by WebInstaller to provide lastPage parameters.
1177 protected function getDocUrl( $page ) {
1178 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1182 * Finds extensions that follow the format /extensions/Name/Name.php,
1183 * and returns an array containing the value for 'Name' for each found extension.
1187 public function findExtensions() {
1188 if( $this->getVar( 'IP' ) === null ) {
1193 $dir = $this->getVar( 'IP' ) . '/extensions';
1194 $dh = opendir( $dir );
1196 while ( ( $file = readdir( $dh ) ) !== false ) {
1197 if( file_exists( "$dir/$file/$file.php" ) ) {
1206 * Installs the auto-detected extensions.
1210 protected function includeExtensions() {
1212 $exts = $this->getVar( '_Extensions' );
1213 $IP = $this->getVar( 'IP' );
1216 * We need to include DefaultSettings before including extensions to avoid
1217 * warnings about unset variables. However, the only thing we really
1218 * want here is $wgHooks['LoadExtensionSchemaUpdates']. This won't work
1219 * if the extension has hidden hook registration in $wgExtensionFunctions,
1220 * but we're not opening that can of worms
1221 * @see https://bugzilla.wikimedia.org/show_bug.cgi?id=26857
1223 global $wgAutoloadClasses;
1224 require( "$IP/includes/DefaultSettings.php" );
1226 foreach( $exts as $e ) {
1227 require_once( $IP . '/extensions' . "/$e/$e.php" );
1230 $hooksWeWant = isset( $wgHooks['LoadExtensionSchemaUpdates'] ) ?
1231 $wgHooks['LoadExtensionSchemaUpdates'] : array();
1233 // Unset everyone else's hooks. Lord knows what someone might be doing
1234 // in ParserFirstCallInit (see bug 27171)
1235 $GLOBALS['wgHooks'] = array( 'LoadExtensionSchemaUpdates' => $hooksWeWant );
1237 return Status::newGood();
1241 * Get an array of install steps. Should always be in the format of
1243 * 'name' => 'someuniquename',
1244 * 'callback' => array( $obj, 'method' ),
1246 * There must be a config-install-$name message defined per step, which will
1247 * be shown on install.
1249 * @param $installer DatabaseInstaller so we can make callbacks
1252 protected function getInstallSteps( DatabaseInstaller $installer ) {
1253 $coreInstallSteps = array(
1254 array( 'name' => 'database', 'callback' => array( $installer, 'setupDatabase' ) ),
1255 array( 'name' => 'tables', 'callback' => array( $installer, 'createTables' ) ),
1256 array( 'name' => 'interwiki', 'callback' => array( $installer, 'populateInterwikiTable' ) ),
1257 array( 'name' => 'stats', 'callback' => array( $this, 'populateSiteStats' ) ),
1258 array( 'name' => 'keys', 'callback' => array( $this, 'generateKeys' ) ),
1259 array( 'name' => 'sysop', 'callback' => array( $this, 'createSysop' ) ),
1260 array( 'name' => 'mainpage', 'callback' => array( $this, 'createMainpage' ) ),
1263 // Build the array of install steps starting from the core install list,
1264 // then adding any callbacks that wanted to attach after a given step
1265 foreach( $coreInstallSteps as $step ) {
1266 $this->installSteps[] = $step;
1267 if( isset( $this->extraInstallSteps[ $step['name'] ] ) ) {
1268 $this->installSteps = array_merge(
1269 $this->installSteps,
1270 $this->extraInstallSteps[ $step['name'] ]
1275 // Prepend any steps that want to be at the beginning
1276 if( isset( $this->extraInstallSteps['BEGINNING'] ) ) {
1277 $this->installSteps = array_merge(
1278 $this->extraInstallSteps['BEGINNING'],
1283 // Extensions should always go first, chance to tie into hooks and such
1284 if( count( $this->getVar( '_Extensions' ) ) ) {
1285 array_unshift( $this->installSteps,
1286 array( 'name' => 'extensions', 'callback' => array( $this, 'includeExtensions' ) )
1288 $this->installSteps[] = array(
1289 'name' => 'extension-tables',
1290 'callback' => array( $installer, 'createExtensionTables' )
1293 return $this->installSteps;
1297 * Actually perform the installation.
1299 * @param $startCB Array A callback array for the beginning of each step
1300 * @param $endCB Array A callback array for the end of each step
1302 * @return Array of Status objects
1304 public function performInstallation( $startCB, $endCB ) {
1305 $installResults = array();
1306 $installer = $this->getDBInstaller();
1307 $installer->preInstall();
1308 $steps = $this->getInstallSteps( $installer );
1309 foreach( $steps as $stepObj ) {
1310 $name = $stepObj['name'];
1311 call_user_func_array( $startCB, array( $name ) );
1313 // Perform the callback step
1314 $status = call_user_func( $stepObj['callback'], $installer );
1316 // Output and save the results
1317 call_user_func( $endCB, $name, $status );
1318 $installResults[$name] = $status;
1320 // If we've hit some sort of fatal, we need to bail.
1321 // Callback already had a chance to do output above.
1322 if( !$status->isOk() ) {
1326 if( $status->isOk() ) {
1327 $this->setVar( '_InstallDone', true );
1329 return $installResults;
1333 * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of
1338 public function generateKeys() {
1339 $keys = array( 'wgSecretKey' => 64 );
1340 if ( strval( $this->getVar( 'wgUpgradeKey' ) ) === '' ) {
1341 $keys['wgUpgradeKey'] = 16;
1343 return $this->doGenerateKeys( $keys );
1347 * Generate a secret value for variables using either
1348 * /dev/urandom or mt_rand(). Produce a warning in the later case.
1350 * @param $keys Array
1353 protected function doGenerateKeys( $keys ) {
1354 $status = Status::newGood();
1356 wfSuppressWarnings();
1357 $file = fopen( "/dev/urandom", "r" );
1358 wfRestoreWarnings();
1360 foreach ( $keys as $name => $length ) {
1362 $secretKey = bin2hex( fread( $file, $length / 2 ) );
1366 for ( $i = 0; $i < $length / 8; $i++ ) {
1367 $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
1371 $this->setVar( $name, $secretKey );
1377 $names = array_keys ( $keys );
1378 $names = preg_replace( '/^(.*)$/', '\$$1', $names );
1380 $status->warning( 'config-insecure-keys', $wgLang->listToText( $names ), count( $names ) );
1387 * Create the first user account, grant it sysop and bureaucrat rights
1391 protected function createSysop() {
1392 $name = $this->getVar( '_AdminName' );
1393 $user = User::newFromName( $name );
1396 // We should've validated this earlier anyway!
1397 return Status::newFatal( 'config-admin-error-user', $name );
1400 if ( $user->idForName() == 0 ) {
1401 $user->addToDatabase();
1404 $user->setPassword( $this->getVar( '_AdminPassword' ) );
1405 } catch( PasswordError $pwe ) {
1406 return Status::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
1409 $user->addGroup( 'sysop' );
1410 $user->addGroup( 'bureaucrat' );
1411 if( $this->getVar( '_AdminEmail' ) ) {
1412 $user->setEmail( $this->getVar( '_AdminEmail' ) );
1414 $user->saveSettings();
1416 // Update user count
1417 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
1418 $ssUpdate->doUpdate();
1420 $status = Status::newGood();
1422 if( $this->getVar( '_Subscribe' ) && $this->getVar( '_AdminEmail' ) ) {
1423 $this->subscribeToMediaWikiAnnounce( $status );
1429 private function subscribeToMediaWikiAnnounce( Status $s ) {
1431 'email' => $this->getVar( '_AdminEmail' ),
1436 // Mailman doesn't support as many languages as we do, so check to make
1437 // sure their selected language is available
1438 $myLang = $this->getVar( '_UserLang' );
1439 if( in_array( $myLang, $this->mediaWikiAnnounceLanguages ) ) {
1440 $myLang = $myLang == 'pt-br' ? 'pt_BR' : $myLang; // rewrite to Mailman's pt_BR
1441 $params['language'] = $myLang;
1444 $res = MWHttpRequest::factory( $this->mediaWikiAnnounceUrl,
1445 array( 'method' => 'POST', 'postData' => $params ) )->execute();
1446 if( !$res->isOK() ) {
1447 $s->warning( 'config-install-subscribe-fail', $res->getMessage() );
1452 * Insert Main Page with default content.
1456 protected function createMainpage( DatabaseInstaller $installer ) {
1457 $status = Status::newGood();
1459 $article = new Article( Title::newMainPage() );
1460 $article->doEdit( wfMsgForContent( 'mainpagetext' ) . "\n\n" .
1461 wfMsgForContent( 'mainpagedocfooter' ),
1465 User::newFromName( 'MediaWiki default' ) );
1466 } catch (MWException $e) {
1467 //using raw, because $wgShowExceptionDetails can not be set yet
1468 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
1475 * Override the necessary bits of the config to run an installation.
1477 public static function overrideConfig() {
1478 define( 'MW_NO_SESSION', 1 );
1480 // Don't access the database
1481 $GLOBALS['wgUseDatabaseMessages'] = false;
1483 $GLOBALS['wgShowExceptionDetails'] = true;
1484 // Don't break forms
1485 $GLOBALS['wgExternalLinkTarget'] = '_blank';
1487 // Extended debugging
1488 $GLOBALS['wgShowSQLErrors'] = true;
1489 $GLOBALS['wgShowDBErrorBacktrace'] = true;
1491 // Allow multiple ob_flush() calls
1492 $GLOBALS['wgDisableOutputCompression'] = true;
1494 // Use a sensible cookie prefix (not my_wiki)
1495 $GLOBALS['wgCookiePrefix'] = 'mw_installer';
1497 // Some of the environment checks make shell requests, remove limits
1498 $GLOBALS['wgMaxShellMemory'] = 0;
1502 * Add an installation step following the given step.
1504 * @param $callback Array A valid installation callback array, in this form:
1505 * array( 'name' => 'some-unique-name', 'callback' => array( $obj, 'function' ) );
1506 * @param $findStep String the step to find. Omit to put the step at the beginning
1508 public function addInstallStep( $callback, $findStep = 'BEGINNING' ) {
1509 $this->extraInstallSteps[$findStep][] = $callback;