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.0';
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',
109 * MediaWiki configuration globals that will eventually be passed through
110 * to LocalSettings.php. The names only are given here, the defaults
111 * typically come from DefaultSettings.php.
115 protected $defaultVarNames = array(
127 'wgEmailAuthentication',
130 'wgImageMagickConvertCommand',
135 'wgDeletedDirectory',
140 'wgUseInstantCommons',
146 * Variables that are stored alongside globals, and are used for any
147 * configuration of the installation process aside from the MediaWiki
148 * configuration. Map of names to defaults.
152 protected $internalDefaults = array(
154 '_Environment' => false,
155 '_CompiledDBs' => array(),
156 '_SafeMode' => false,
157 '_RaiseMemory' => false,
158 '_UpgradeDone' => false,
159 '_InstallDone' => false,
160 '_Caches' => array(),
161 '_InstallUser' => 'root',
162 '_InstallPassword' => '',
163 '_SameAccount' => true,
164 '_CreateDBAccount' => false,
165 '_NamespaceType' => 'site-name',
166 '_AdminName' => '', // will be set later, when the user selects language
167 '_AdminPassword' => '',
168 '_AdminPassword2' => '',
170 '_Subscribe' => false,
171 '_SkipOptional' => 'continue',
172 '_RightsProfile' => 'wiki',
173 '_LicenseCode' => 'none',
175 '_Extensions' => array(),
176 '_MemCachedServers' => '',
177 '_UpgradeKeySupplied' => false,
178 '_ExistingDBSettings' => false,
182 * The actual list of installation steps. This will be initialized by getInstallSteps()
186 private $installSteps = array();
189 * Extra steps for installation, for things like DatabaseInstallers to modify
193 protected $extraInstallSteps = array();
196 * Known object cache types and the functions used to test for their existence.
200 protected $objectCaches = array(
201 'xcache' => 'xcache_get',
202 'apc' => 'apc_fetch',
203 'eaccel' => 'eaccelerator_get',
204 'wincache' => 'wincache_ucache_get'
208 * User rights profiles.
212 public $rightsProfiles = array(
215 '*' => array( 'edit' => false )
219 'createaccount' => false,
225 'createaccount' => false,
237 public $licenses = array(
239 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
240 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
242 'cc-by-nc-sa' => array(
243 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/',
244 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png',
247 'url' => 'http://creativecommons.org/licenses/publicdomain/',
248 'icon' => '{$wgStylePath}/common/images/public-domain.png',
251 'url' => 'http://www.gnu.org/licenses/old-licenses/fdl-1.2.html',
252 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
254 'gfdl-current' => array(
255 'url' => 'http://www.gnu.org/copyleft/fdl.html',
256 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
263 'cc-choose' => array(
264 // Details will be filled in by the selector.
272 * URL to mediawiki-announce subscription
274 protected $mediaWikiAnnounceUrl = 'https://lists.wikimedia.org/mailman/subscribe/mediawiki-announce';
277 * Supported language codes for Mailman
279 protected $mediaWikiAnnounceLanguages = array(
280 'ca', 'cs', 'da', 'de', 'en', 'es', 'et', 'eu', 'fi', 'fr', 'hr', 'hu',
281 'it', 'ja', 'ko', 'lt', 'nl', 'no', 'pl', 'pt', 'pt-br', 'ro', 'ru',
282 'sl', 'sr', 'sv', 'tr', 'uk'
286 * UI interface for displaying a short message
287 * The parameters are like parameters to wfMsg().
288 * The messages will be in wikitext format, which will be converted to an
289 * output format such as HTML or text before being sent to the user.
291 public abstract function showMessage( $msg /*, ... */ );
294 * Show a message to the installing user by using a Status object
295 * @param $status Status
297 public abstract function showStatusMessage( Status
$status );
300 * Constructor, always call this from child classes.
302 public function __construct() {
303 global $wgExtensionMessagesFiles, $wgUser, $wgHooks;
305 // Disable the i18n cache and LoadBalancer
306 Language
::getLocalisationCache()->disableBackend();
307 LBFactory
::disableBackend();
309 // Load the installer's i18n file.
310 $wgExtensionMessagesFiles['MediawikiInstaller'] =
311 dirname( __FILE__
) . '/Installer.i18n.php';
313 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
314 $wgUser = User
::newFromId( 0 );
316 // Set our custom <doclink> hook.
317 $wgHooks['ParserFirstCallInit'][] = array( $this, 'registerDocLink' );
319 $this->settings
= $this->internalDefaults
;
321 foreach ( $this->defaultVarNames
as $var ) {
322 $this->settings
[$var] = $GLOBALS[$var];
325 foreach ( self
::getDBTypes() as $type ) {
326 $installer = $this->getDBInstaller( $type );
328 if ( !$installer->isCompiled() ) {
332 $defaults = $installer->getGlobalDefaults();
334 foreach ( $installer->getGlobalNames() as $var ) {
335 if ( isset( $defaults[$var] ) ) {
336 $this->settings
[$var] = $defaults[$var];
338 $this->settings
[$var] = $GLOBALS[$var];
343 $this->parserTitle
= Title
::newFromText( 'Installer' );
344 $this->parserOptions
= new ParserOptions
; // language will be wrong :(
345 $this->parserOptions
->setEditSection( false );
349 * Get a list of known DB types.
351 public static function getDBTypes() {
352 return self
::$dbTypes;
356 * Do initial checks of the PHP environment. Set variables according to
357 * the observed environment.
359 * It's possible that this may be called under the CLI SAPI, not the SAPI
360 * that the wiki will primarily run under. In that case, the subclass should
361 * initialise variables such as wgScriptPath, before calling this function.
363 * Under the web subclass, it can already be assumed that PHP 5+ is in use
364 * and that sessions are working.
368 public function doEnvironmentChecks() {
369 $phpVersion = phpversion();
370 if( version_compare( $phpVersion, self
::MINIMUM_PHP_VERSION
, '>=' ) ) {
371 $this->showMessage( 'config-env-php', $phpVersion );
374 $this->showMessage( 'config-env-php-toolow', $phpVersion, self
::MINIMUM_PHP_VERSION
);
379 foreach ( $this->envChecks
as $check ) {
380 $status = $this->$check();
381 if ( $status === false ) {
387 $this->setVar( '_Environment', $good );
389 return $good ? Status
::newGood() : Status
::newFatal( 'config-env-bad' );
393 * Set a MW configuration variable, or internal installer configuration variable.
395 * @param $name String
396 * @param $value Mixed
398 public function setVar( $name, $value ) {
399 $this->settings
[$name] = $value;
403 * Get an MW configuration variable, or internal installer configuration variable.
404 * The defaults come from $GLOBALS (ultimately DefaultSettings.php).
405 * Installer variables are typically prefixed by an underscore.
407 * @param $name String
408 * @param $default Mixed
412 public function getVar( $name, $default = null ) {
413 if ( !isset( $this->settings
[$name] ) ) {
416 return $this->settings
[$name];
421 * Get an instance of DatabaseInstaller for the specified DB type.
423 * @param $type Mixed: DB installer for which is needed, false to use default.
425 * @return DatabaseInstaller
427 public function getDBInstaller( $type = false ) {
429 $type = $this->getVar( 'wgDBtype' );
432 $type = strtolower( $type );
434 if ( !isset( $this->dbInstallers
[$type] ) ) {
435 $class = ucfirst( $type ). 'Installer';
436 $this->dbInstallers
[$type] = new $class( $this );
439 return $this->dbInstallers
[$type];
443 * Determine if LocalSettings.php exists. If it does, return its variables,
444 * merged with those from AdminSettings.php, as an array.
448 public function getExistingLocalSettings() {
451 wfSuppressWarnings();
452 $_lsExists = file_exists( "$IP/LocalSettings.php" );
460 require( "$IP/includes/DefaultSettings.php" );
461 require( "$IP/LocalSettings.php" );
462 if ( file_exists( "$IP/AdminSettings.php" ) ) {
463 require( "$IP/AdminSettings.php" );
465 return get_defined_vars();
469 * Get a fake password for sending back to the user in HTML.
470 * This is a security mechanism to avoid compromise of the password in the
471 * event of session ID compromise.
473 * @param $realPassword String
477 public function getFakePassword( $realPassword ) {
478 return str_repeat( '*', strlen( $realPassword ) );
482 * Set a variable which stores a password, except if the new value is a
483 * fake password in which case leave it as it is.
485 * @param $name String
486 * @param $value Mixed
488 public function setPassword( $name, $value ) {
489 if ( !preg_match( '/^\*+$/', $value ) ) {
490 $this->setVar( $name, $value );
495 * On POSIX systems return the primary group of the webserver we're running under.
496 * On other systems just returns null.
498 * This is used to advice the user that he should chgrp his mw-config/data/images directory as the
499 * webserver user before he can install.
501 * Public because SqliteInstaller needs it, and doesn't subclass Installer.
505 public static function maybeGetWebserverPrimaryGroup() {
506 if ( !function_exists( 'posix_getegid' ) ||
!function_exists( 'posix_getpwuid' ) ) {
507 # I don't know this, this isn't UNIX.
511 # posix_getegid() *not* getmygid() because we want the group of the webserver,
512 # not whoever owns the current script.
513 $gid = posix_getegid();
514 $getpwuid = posix_getpwuid( $gid );
515 $group = $getpwuid['name'];
521 * Convert wikitext $text to HTML.
523 * This is potentially error prone since many parser features require a complete
524 * installed MW database. The solution is to just not use those features when you
525 * write your messages. This appears to work well enough. Basic formatting and
526 * external links work just fine.
528 * But in case a translator decides to throw in a #ifexist or internal link or
529 * whatever, this function is guarded to catch the attempted DB access and to present
530 * some fallback text.
532 * @param $text String
533 * @param $lineStart Boolean
536 public function parse( $text, $lineStart = false ) {
540 $out = $wgParser->parse( $text, $this->parserTitle
, $this->parserOptions
, $lineStart );
541 $html = $out->getText();
542 } catch ( DBAccessError
$e ) {
543 $html = '<!--DB access attempted during parse--> ' . htmlspecialchars( $text );
545 if ( !empty( $this->debug
) ) {
546 $html .= "<!--\n" . $e->getTraceAsString() . "\n-->";
553 public function getParserOptions() {
554 return $this->parserOptions
;
557 public function disableLinkPopups() {
558 $this->parserOptions
->setExternalLinkTarget( false );
561 public function restoreLinkPopups() {
562 global $wgExternalLinkTarget;
563 $this->parserOptions
->setExternalLinkTarget( $wgExternalLinkTarget );
567 * Install step which adds a row to the site_stats table with appropriate
570 public function populateSiteStats( DatabaseInstaller
$installer ) {
571 $status = $installer->getConnection();
572 if ( !$status->isOK() ) {
575 $status->value
->insert( 'site_stats', array(
577 'ss_total_views' => 0,
578 'ss_total_edits' => 0,
579 'ss_good_articles' => 0,
580 'ss_total_pages' => 0,
584 __METHOD__
, 'IGNORE' );
585 return Status
::newGood();
589 * Exports all wg* variables stored by the installer into global scope.
591 public function exportVars() {
592 foreach ( $this->settings
as $name => $value ) {
593 if ( substr( $name, 0, 2 ) == 'wg' ) {
594 $GLOBALS[$name] = $value;
600 * Environment check for DB types.
602 protected function envCheckDB() {
605 $compiledDBs = array();
608 foreach ( self
::getDBTypes() as $name ) {
609 $db = $this->getDBInstaller( $name );
610 $readableName = wfMsg( 'config-type-' . $name );
612 if ( $db->isCompiled() ) {
613 $compiledDBs[] = $name;
615 $allNames[] = $readableName;
618 $this->setVar( '_CompiledDBs', $compiledDBs );
620 if ( !$compiledDBs ) {
621 $this->showMessage( 'config-no-db' );
622 // FIXME: this only works for the web installer!
623 $this->showHelpBox( 'config-no-db-help', $wgLang->commaList( $allNames ) );
627 // Check for FTS3 full-text search module
628 $sqlite = $this->getDBInstaller( 'sqlite' );
629 if ( $sqlite->isCompiled() ) {
630 $db = new DatabaseSqliteStandalone( ':memory:' );
631 if( $db->getFulltextSearchModule() != 'FTS3' ) {
632 $this->showMessage( 'config-no-fts3' );
638 * Environment check for register_globals.
640 protected function envCheckRegisterGlobals() {
641 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
642 $this->showMessage( 'config-register-globals' );
647 * Some versions of libxml+PHP break < and > encoding horribly
649 protected function envCheckBrokenXML() {
650 $test = new PhpXmlBugTester();
652 $this->showMessage( 'config-brokenlibxml' );
658 * Test PHP (probably 5.3.1, but it could regress again) to make sure that
659 * reference parameters to __call() are not converted to null
661 protected function envCheckPHP531() {
662 $test = new PhpRefCallBugTester
;
665 $this->showMessage( 'config-using531', phpversion() );
671 * Environment check for magic_quotes_runtime.
673 protected function envCheckMagicQuotes() {
674 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
675 $this->showMessage( 'config-magic-quotes-runtime' );
681 * Environment check for magic_quotes_sybase.
683 protected function envCheckMagicSybase() {
684 if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
685 $this->showMessage( 'config-magic-quotes-sybase' );
691 * Environment check for mbstring.func_overload.
693 protected function envCheckMbstring() {
694 if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
695 $this->showMessage( 'config-mbstring' );
701 * Environment check for zend.ze1_compatibility_mode.
703 protected function envCheckZE1() {
704 if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
705 $this->showMessage( 'config-ze1' );
711 * Environment check for safe_mode.
713 protected function envCheckSafeMode() {
714 if ( wfIniGetBool( 'safe_mode' ) ) {
715 $this->setVar( '_SafeMode', true );
716 $this->showMessage( 'config-safe-mode' );
721 * Environment check for the XML module.
723 protected function envCheckXML() {
724 if ( !function_exists( "utf8_encode" ) ) {
725 $this->showMessage( 'config-xml-bad' );
731 * Environment check for the PCRE module.
733 protected function envCheckPCRE() {
734 if ( !function_exists( 'preg_match' ) ) {
735 $this->showMessage( 'config-pcre' );
738 wfSuppressWarnings();
739 $regexd = preg_replace( '/[\x{0430}-\x{04FF}]/iu', '', '-АБВГД-' );
741 if ( $regexd != '--' ) {
742 $this->showMessage( 'config-pcre-no-utf8' );
748 * Environment check for available memory.
750 protected function envCheckMemory() {
751 $limit = ini_get( 'memory_limit' );
753 if ( !$limit ||
$limit == -1 ) {
757 $n = wfShorthandToInteger( $limit );
759 if( $n < $this->minMemorySize
* 1024 * 1024 ) {
760 $newLimit = "{$this->minMemorySize}M";
762 if( ini_set( "memory_limit", $newLimit ) === false ) {
763 $this->showMessage( 'config-memory-bad', $limit );
765 $this->showMessage( 'config-memory-raised', $limit, $newLimit );
766 $this->setVar( '_RaiseMemory', true );
774 * Environment check for compiled object cache types.
776 protected function envCheckCache() {
778 foreach ( $this->objectCaches
as $name => $function ) {
779 if ( function_exists( $function ) ) {
780 $caches[$name] = true;
785 $this->showMessage( 'config-no-cache' );
788 $this->setVar( '_Caches', $caches );
792 * Search for GNU diff3.
794 protected function envCheckDiff3() {
795 $names = array( "gdiff3", "diff3", "diff3.exe" );
796 $versionInfo = array( '$1 --version 2>&1', 'GNU diffutils' );
798 $diff3 = self
::locateExecutableInDefaultPaths( $names, $versionInfo );
801 $this->setVar( 'wgDiff3', $diff3 );
803 $this->setVar( 'wgDiff3', false );
804 $this->showMessage( 'config-diff3-bad' );
809 * Environment check for ImageMagick and GD.
811 protected function envCheckGraphics() {
812 $names = array( wfIsWindows() ?
'convert.exe' : 'convert' );
813 $convert = self
::locateExecutableInDefaultPaths( $names, array( '$1 -version', 'ImageMagick' ) );
816 $this->setVar( 'wgImageMagickConvertCommand', $convert );
817 $this->showMessage( 'config-imagemagick', $convert );
819 } elseif ( function_exists( 'imagejpeg' ) ) {
820 $this->showMessage( 'config-gd' );
823 $this->showMessage( 'no-scaling' );
828 * Environment check for setting $IP and $wgScriptPath.
830 protected function envCheckPath() {
832 $IP = dirname( dirname( dirname( __FILE__
) ) );
834 $this->setVar( 'IP', $IP );
836 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
837 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
838 // to get the path to the current script... hopefully it's reliable. SIGH
839 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
840 $path = $_SERVER['PHP_SELF'];
841 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
842 $path = $_SERVER['SCRIPT_NAME'];
843 } elseif ( $this->getVar( 'wgScriptPath' ) ) {
844 // Some kind soul has set it for us already (e.g. debconf)
847 $this->showMessage( 'config-no-uri' );
851 $uri = preg_replace( '{^(.*)/(mw-)?config.*$}', '$1', $path );
852 $this->setVar( 'wgScriptPath', $uri );
856 * Environment check for setting the preferred PHP file extension.
858 protected function envCheckExtension() {
859 // FIXME: detect this properly
860 if ( defined( 'MW_INSTALL_PHP5_EXT' ) ) {
865 $this->setVar( 'wgScriptExtension', ".$ext" );
871 protected function envCheckShellLocale() {
872 $os = php_uname( 's' );
873 $supported = array( 'Linux', 'SunOS', 'HP-UX', 'Darwin' ); # Tested these
875 if ( !in_array( $os, $supported ) ) {
879 # Get a list of available locales.
881 $lines = wfShellExec( '/usr/bin/locale -a', $ret );
887 $lines = wfArrayMap( 'trim', explode( "\n", $lines ) );
888 $candidatesByLocale = array();
889 $candidatesByLang = array();
891 foreach ( $lines as $line ) {
892 if ( $line === '' ) {
896 if ( !preg_match( '/^([a-zA-Z]+)(_[a-zA-Z]+|)\.(utf8|UTF-8)(@[a-zA-Z_]*|)$/i', $line, $m ) ) {
900 list( $all, $lang, $territory, $charset, $modifier ) = $m;
902 $candidatesByLocale[$m[0]] = $m;
903 $candidatesByLang[$lang][] = $m;
906 # Try the current value of LANG.
907 if ( isset( $candidatesByLocale[ getenv( 'LANG' ) ] ) ) {
908 $this->setVar( 'wgShellLocale', getenv( 'LANG' ) );
912 # Try the most common ones.
913 $commonLocales = array( 'en_US.UTF-8', 'en_US.utf8', 'de_DE.UTF-8', 'de_DE.utf8' );
914 foreach ( $commonLocales as $commonLocale ) {
915 if ( isset( $candidatesByLocale[$commonLocale] ) ) {
916 $this->setVar( 'wgShellLocale', $commonLocale );
921 # Is there an available locale in the Wiki's language?
922 $wikiLang = $this->getVar( 'wgLanguageCode' );
924 if ( isset( $candidatesByLang[$wikiLang] ) ) {
925 $m = reset( $candidatesByLang[$wikiLang] );
926 $this->setVar( 'wgShellLocale', $m[0] );
930 # Are there any at all?
931 if ( count( $candidatesByLocale ) ) {
932 $m = reset( $candidatesByLocale );
933 $this->setVar( 'wgShellLocale', $m[0] );
944 protected function envCheckUploadsDirectory() {
945 global $IP, $wgServer;
947 $dir = $IP . '/images/';
948 $url = $wgServer . $this->getVar( 'wgScriptPath' ) . '/images/';
949 $safe = !$this->dirIsExecutable( $dir, $url );
954 $this->showMessage( 'config-uploads-not-safe', $dir );
959 * Convert a hex string representing a Unicode code point to that code point.
963 protected function unicodeChar( $c ) {
967 } else if ($c <= 0x7FF) {
968 return chr(0xC0 |
$c >> 6) . chr(0x80 |
$c & 0x3F);
969 } else if ($c <= 0xFFFF) {
970 return chr(0xE0 |
$c >> 12) . chr(0x80 |
$c >> 6 & 0x3F)
971 . chr(0x80 |
$c & 0x3F);
972 } else if ($c <= 0x10FFFF) {
973 return chr(0xF0 |
$c >> 18) . chr(0x80 |
$c >> 12 & 0x3F)
974 . chr(0x80 |
$c >> 6 & 0x3F)
975 . chr(0x80 |
$c & 0x3F);
983 * Check the libicu version
985 protected function envCheckLibicu() {
986 $utf8 = function_exists( 'utf8_normalize' );
987 $intl = function_exists( 'normalizer_normalize' );
990 * This needs to be updated something that the latest libicu
991 * will properly normalize. This normalization was found at
992 * http://www.unicode.org/versions/Unicode5.2.0/#Character_Additions
993 * Note that we use the hex representation to create the code
994 * points in order to avoid any Unicode-destroying during transit.
996 $not_normal_c = $this->unicodeChar("FA6C");
997 $normal_c = $this->unicodeChar("242EE");
999 $useNormalizer = 'php';
1000 $needsUpdate = false;
1003 * We're going to prefer the pecl extension here unless
1004 * utf8_normalize is more up to date.
1007 $useNormalizer = 'utf8';
1008 $utf8 = utf8_normalize( $not_normal_c, UNORM_NFC
);
1009 if ( $utf8 !== $normal_c ) $needsUpdate = true;
1012 $useNormalizer = 'intl';
1013 $intl = normalizer_normalize( $not_normal_c, Normalizer
::FORM_C
);
1014 if ( $intl !== $normal_c ) $needsUpdate = true;
1017 // Uses messages 'config-unicode-using-php', 'config-unicode-using-utf8', 'config-unicode-using-intl'
1018 if( $useNormalizer === 'php' ) {
1019 $this->showMessage( 'config-unicode-pure-php-warning' );
1021 $this->showMessage( 'config-unicode-using-' . $useNormalizer );
1022 if( $needsUpdate ) {
1023 $this->showMessage( 'config-unicode-update-warning' );
1029 * Get an array of likely places we can find executables. Check a bunch
1030 * of known Unix-like defaults, as well as the PATH environment variable
1031 * (which should maybe make it work for Windows?)
1035 protected static function getPossibleBinPaths() {
1037 array( '/usr/bin', '/usr/local/bin', '/opt/csw/bin',
1038 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ),
1039 explode( PATH_SEPARATOR
, getenv( 'PATH' ) )
1044 * Search a path for any of the given executable names. Returns the
1045 * executable name if found. Also checks the version string returned
1046 * by each executable.
1048 * Used only by environment checks.
1050 * @param $path String: path to search
1051 * @param $names Array of executable names
1052 * @param $versionInfo Boolean false or array with two members:
1053 * 0 => Command to run for version check, with $1 for the full executable name
1054 * 1 => String to compare the output with
1056 * If $versionInfo is not false, only executables with a version
1057 * matching $versionInfo[1] will be returned.
1059 public static function locateExecutable( $path, $names, $versionInfo = false ) {
1060 if ( !is_array( $names ) ) {
1061 $names = array( $names );
1064 foreach ( $names as $name ) {
1065 $command = $path . DIRECTORY_SEPARATOR
. $name;
1067 wfSuppressWarnings();
1068 $file_exists = file_exists( $command );
1069 wfRestoreWarnings();
1071 if ( $file_exists ) {
1072 if ( !$versionInfo ) {
1076 $file = str_replace( '$1', wfEscapeShellArg( $command ), $versionInfo[0] );
1077 if ( strstr( wfShellExec( $file ), $versionInfo[1] ) !== false ) {
1086 * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
1087 * @see locateExecutable()
1089 public static function locateExecutableInDefaultPaths( $names, $versionInfo = false ) {
1090 foreach( self
::getPossibleBinPaths() as $path ) {
1091 $exe = self
::locateExecutable( $path, $names, $versionInfo );
1092 if( $exe !== false ) {
1100 * Checks if scripts located in the given directory can be executed via the given URL.
1102 * Used only by environment checks.
1104 public function dirIsExecutable( $dir, $url ) {
1105 $scriptTypes = array(
1107 "<?php echo 'ex' . 'ec';",
1108 "#!/var/env php5\n<?php echo 'ex' . 'ec';",
1112 // it would be good to check other popular languages here, but it'll be slow.
1114 wfSuppressWarnings();
1116 foreach ( $scriptTypes as $ext => $contents ) {
1117 foreach ( $contents as $source ) {
1118 $file = 'exectest.' . $ext;
1120 if ( !file_put_contents( $dir . $file, $source ) ) {
1124 $text = Http
::get( $url . $file, array( 'timeout' => 3 ) );
1125 unlink( $dir . $file );
1127 if ( $text == 'exec' ) {
1128 wfRestoreWarnings();
1134 wfRestoreWarnings();
1140 * Register tag hook below.
1142 * @todo Move this to WebInstaller with the two things below?
1144 * @param $parser Parser
1146 public function registerDocLink( Parser
&$parser ) {
1147 $parser->setHook( 'doclink', array( $this, 'docLink' ) );
1152 * ParserOptions are constructed before we determined the language, so fix it
1154 public function setParserLanguage( $lang ) {
1155 $this->parserOptions
->setTargetLanguage( $lang );
1156 $this->parserOptions
->setUserLang( $lang->getCode() );
1160 * Extension tag hook for a documentation link.
1162 public function docLink( $linkText, $attribs, $parser ) {
1163 $url = $this->getDocUrl( $attribs['href'] );
1164 return '<a href="' . htmlspecialchars( $url ) . '">' .
1165 htmlspecialchars( $linkText ) .
1170 * Overridden by WebInstaller to provide lastPage parameters.
1172 protected function getDocUrl( $page ) {
1173 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1177 * Finds extensions that follow the format /extensions/Name/Name.php,
1178 * and returns an array containing the value for 'Name' for each found extension.
1182 public function findExtensions() {
1183 if( $this->getVar( 'IP' ) === null ) {
1188 $dir = $this->getVar( 'IP' ) . '/extensions';
1189 $dh = opendir( $dir );
1191 while ( ( $file = readdir( $dh ) ) !== false ) {
1192 if( file_exists( "$dir/$file/$file.php" ) ) {
1201 * Installs the auto-detected extensions.
1205 protected function includeExtensions() {
1207 $exts = $this->getVar( '_Extensions' );
1208 $IP = $this->getVar( 'IP' );
1209 $path = $IP . '/extensions';
1212 * We need to include DefaultSettings before including extensions to avoid
1213 * warnings about unset variables. However, the only thing we really
1214 * want here is $wgHooks['LoadExtensionSchemaUpdates']. This won't work
1215 * if the extension has hidden hook registration in $wgExtensionFunctions,
1216 * but we're not opening that can of worms
1217 * @see https://bugzilla.wikimedia.org/show_bug.cgi?id=26857
1219 global $wgHooks, $wgAutoloadClasses;
1220 require( "$IP/includes/DefaultSettings.php" );
1222 foreach( $exts as $e ) {
1223 require( "$path/$e/$e.php" );
1226 $hooksWeWant = isset( $wgHooks['LoadExtensionSchemaUpdates'] ) ?
1227 $wgHooks['LoadExtensionSchemaUpdates'] : array();
1229 // Unset everyone else's hooks. Lord knows what someone might be doing
1230 // in ParserFirstCallInit (see bug 27171)
1232 $wgHooks = array( 'LoadExtensionSchemaUpdates' => $hooksWeWant );
1234 return Status
::newGood();
1238 * Get an array of install steps. Should always be in the format of
1240 * 'name' => 'someuniquename',
1241 * 'callback' => array( $obj, 'method' ),
1243 * There must be a config-install-$name message defined per step, which will
1244 * be shown on install.
1246 * @param $installer DatabaseInstaller so we can make callbacks
1249 protected function getInstallSteps( DatabaseInstaller
$installer ) {
1250 $coreInstallSteps = array(
1251 array( 'name' => 'database', 'callback' => array( $installer, 'setupDatabase' ) ),
1252 array( 'name' => 'tables', 'callback' => array( $installer, 'createTables' ) ),
1253 array( 'name' => 'interwiki', 'callback' => array( $installer, 'populateInterwikiTable' ) ),
1254 array( 'name' => 'stats', 'callback' => array( $this, 'populateSiteStats' ) ),
1255 array( 'name' => 'secretkey', 'callback' => array( $this, 'generateSecretKey' ) ),
1256 array( 'name' => 'upgradekey', 'callback' => array( $this, 'generateUpgradeKey' ) ),
1257 array( 'name' => 'sysop', 'callback' => array( $this, 'createSysop' ) ),
1258 array( 'name' => 'mainpage', 'callback' => array( $this, 'createMainpage' ) ),
1261 // Build the array of install steps starting from the core install list,
1262 // then adding any callbacks that wanted to attach after a given step
1263 foreach( $coreInstallSteps as $step ) {
1264 $this->installSteps
[] = $step;
1265 if( isset( $this->extraInstallSteps
[ $step['name'] ] ) ) {
1266 $this->installSteps
= array_merge(
1267 $this->installSteps
,
1268 $this->extraInstallSteps
[ $step['name'] ]
1273 // Prepend any steps that want to be at the beginning
1274 if( isset( $this->extraInstallSteps
['BEGINNING'] ) ) {
1275 $this->installSteps
= array_merge(
1276 $this->extraInstallSteps
['BEGINNING'],
1281 // Extensions should always go first, chance to tie into hooks and such
1282 if( count( $this->getVar( '_Extensions' ) ) ) {
1283 array_unshift( $this->installSteps
,
1284 array( 'name' => 'extensions', 'callback' => array( $this, 'includeExtensions' ) )
1286 $this->installSteps
[] = array(
1287 'name' => 'extension-tables',
1288 'callback' => array( $installer, 'createExtensionTables' )
1291 return $this->installSteps
;
1295 * Actually perform the installation.
1297 * @param $startCB Array A callback array for the beginning of each step
1298 * @param $endCB Array A callback array for the end of each step
1300 * @return Array of Status objects
1302 public function performInstallation( $startCB, $endCB ) {
1303 $installResults = array();
1304 $installer = $this->getDBInstaller();
1305 $installer->preInstall();
1306 $steps = $this->getInstallSteps( $installer );
1307 foreach( $steps as $stepObj ) {
1308 $name = $stepObj['name'];
1309 call_user_func_array( $startCB, array( $name ) );
1311 // Perform the callback step
1312 $status = call_user_func( $stepObj['callback'], $installer );
1314 // Output and save the results
1315 call_user_func( $endCB, $name, $status );
1316 $installResults[$name] = $status;
1318 // If we've hit some sort of fatal, we need to bail.
1319 // Callback already had a chance to do output above.
1320 if( !$status->isOk() ) {
1324 if( $status->isOk() ) {
1325 $this->setVar( '_InstallDone', true );
1327 return $installResults;
1331 * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of
1336 protected function generateSecretKey() {
1337 return $this->generateSecret( 'wgSecretKey' );
1341 * Generate a secret value for a variable using either
1342 * /dev/urandom or mt_rand() Produce a warning in the later case.
1346 protected function generateSecret( $secretName, $length = 64 ) {
1347 if ( wfIsWindows() ) {
1350 wfSuppressWarnings();
1351 $file = fopen( "/dev/urandom", "r" );
1352 wfRestoreWarnings();
1355 $status = Status
::newGood();
1358 $secretKey = bin2hex( fread( $file, $length / 2 ) );
1363 for ( $i = 0; $i < $length / 8; $i++
) {
1364 $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
1367 $status->warning( 'config-insecure-secret', '$' . $secretName );
1370 $this->setVar( $secretName, $secretKey );
1376 * Generate a default $wgUpgradeKey. Will warn if we had to use
1377 * mt_rand() instead of /dev/urandom
1381 public function generateUpgradeKey() {
1382 if ( strval( $this->getVar( 'wgUpgradeKey' ) ) === '' ) {
1383 return $this->generateSecret( 'wgUpgradeKey', 16 );
1385 return Status
::newGood();
1389 * Create the first user account, grant it sysop and bureaucrat rights
1393 protected function createSysop() {
1394 $name = $this->getVar( '_AdminName' );
1395 $user = User
::newFromName( $name );
1398 // We should've validated this earlier anyway!
1399 return Status
::newFatal( 'config-admin-error-user', $name );
1402 if ( $user->idForName() == 0 ) {
1403 $user->addToDatabase();
1406 $user->setPassword( $this->getVar( '_AdminPassword' ) );
1407 } catch( PasswordError
$pwe ) {
1408 return Status
::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
1411 $user->addGroup( 'sysop' );
1412 $user->addGroup( 'bureaucrat' );
1413 if( $this->getVar( '_AdminEmail' ) ) {
1414 $user->setEmail( $this->getVar( '_AdminEmail' ) );
1416 $user->saveSettings();
1418 // Update user count
1419 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
1420 $ssUpdate->doUpdate();
1422 $status = Status
::newGood();
1424 if( $this->getVar( '_Subscribe' ) && $this->getVar( '_AdminEmail' ) ) {
1425 $this->subscribeToMediaWikiAnnounce( $status );
1431 private function subscribeToMediaWikiAnnounce( Status
$s ) {
1433 'email' => $this->getVar( '_AdminEmail' ),
1438 // Mailman doesn't support as many languages as we do, so check to make
1439 // sure their selected language is available
1440 $myLang = $this->getVar( '_UserLang' );
1441 if( in_array( $myLang, $this->mediaWikiAnnounceLanguages
) ) {
1442 $myLang = $myLang == 'pt-br' ?
'pt_BR' : $myLang; // rewrite to Mailman's pt_BR
1443 $params['language'] = $myLang;
1446 $res = Http
::post( $this->mediaWikiAnnounceUrl
, array( 'postData' => $params ) );
1448 $s->warning( 'config-install-subscribe-fail' );
1453 * Insert Main Page with default content.
1457 protected function createMainpage( DatabaseInstaller
$installer ) {
1458 $status = Status
::newGood();
1460 $article = new Article( Title
::newMainPage() );
1461 $article->doEdit( wfMsgForContent( 'mainpagetext' ) . "\n\n" .
1462 wfMsgForContent( 'mainpagedocfooter' ),
1466 User
::newFromName( 'MediaWiki default' ) );
1467 } catch (MWException
$e) {
1468 //using raw, because $wgShowExceptionDetails can not be set yet
1469 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
1476 * Override the necessary bits of the config to run an installation.
1478 public static function overrideConfig() {
1479 define( 'MW_NO_SESSION', 1 );
1481 // Don't access the database
1482 $GLOBALS['wgUseDatabaseMessages'] = false;
1484 $GLOBALS['wgShowExceptionDetails'] = true;
1485 // Don't break forms
1486 $GLOBALS['wgExternalLinkTarget'] = '_blank';
1488 // Extended debugging
1489 $GLOBALS['wgShowSQLErrors'] = true;
1490 $GLOBALS['wgShowDBErrorBacktrace'] = true;
1492 // Allow multiple ob_flush() calls
1493 $GLOBALS['wgDisableOutputCompression'] = true;
1495 // Use a sensible cookie prefix (not my_wiki)
1496 $GLOBALS['wgCookiePrefix'] = 'mw_installer';
1498 // Some of the environment checks make shell requests, remove limits
1499 $GLOBALS['wgMaxShellMemory'] = 0;
1503 * Add an installation step following the given step.
1505 * @param $callback Array A valid installation callback array, in this form:
1506 * array( 'name' => 'some-unique-name', 'callback' => array( $obj, 'function' ) );
1507 * @param $findStep String the step to find. Omit to put the step at the beginning
1509 public function addInstallStep( $callback, $findStep = 'BEGINNING' ) {
1510 $this->extraInstallSteps
[$findStep][] = $callback;