X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Finstaller%2FInstaller.php;h=d8afba134d5a5d290740549893ce654995b0ec96;hb=4143a62299f0517d36cc452ef5585b3c36d3098f;hp=1222802ed96b8b713ae5475c4663abd0002ed365;hpb=987c5cf9f00722e21c412b7a9f31d93a8e352d76;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/installer/Installer.php b/includes/installer/Installer.php index 1222802ed9..d8afba134d 100644 --- a/includes/installer/Installer.php +++ b/includes/installer/Installer.php @@ -23,12 +23,13 @@ */ abstract class Installer { + // This is the absolute minimum PHP version we can support + const MINIMUM_PHP_VERSION = '5.2.3'; + /** - * TODO: make protected? - * * @var array */ - public $settings; + protected $settings; /** * Cached DB installer instances, access using getDBInstaller(). @@ -72,6 +73,7 @@ abstract class Installer { 'postgres', 'oracle', 'sqlite', + 'ibm_db2', ); /** @@ -82,7 +84,6 @@ abstract class Installer { * @var array */ protected $envChecks = array( - 'envCheckMediaWikiVersion', 'envCheckDB', 'envCheckRegisterGlobals', 'envCheckBrokenXML', @@ -96,13 +97,198 @@ abstract class Installer { 'envCheckPCRE', 'envCheckMemory', 'envCheckCache', + 'envCheckModSecurity', 'envCheckDiff3', 'envCheckGraphics', + 'envCheckServer', 'envCheckPath', 'envCheckExtension', 'envCheckShellLocale', 'envCheckUploadsDirectory', - 'envCheckLibicu' + 'envCheckLibicu', + 'envCheckSuhosinMaxValueLength', + ); + + /** + * MediaWiki configuration globals that will eventually be passed through + * to LocalSettings.php. The names only are given here, the defaults + * typically come from DefaultSettings.php. + * + * @var array + */ + protected $defaultVarNames = array( + 'wgSitename', + 'wgPasswordSender', + 'wgLanguageCode', + 'wgRightsIcon', + 'wgRightsText', + 'wgRightsUrl', + 'wgMainCacheType', + 'wgEnableEmail', + 'wgEnableUserEmail', + 'wgEnotifUserTalk', + 'wgEnotifWatchlist', + 'wgEmailAuthentication', + 'wgDBtype', + 'wgDiff3', + 'wgImageMagickConvertCommand', + 'IP', + 'wgServer', + 'wgScriptPath', + 'wgScriptExtension', + 'wgMetaNamespace', + 'wgDeletedDirectory', + 'wgEnableUploads', + 'wgLogo', + 'wgShellLocale', + 'wgSecretKey', + 'wgUseInstantCommons', + 'wgUpgradeKey', + 'wgDefaultSkin', + 'wgResourceLoaderMaxQueryLength', + ); + + /** + * Variables that are stored alongside globals, and are used for any + * configuration of the installation process aside from the MediaWiki + * configuration. Map of names to defaults. + * + * @var array + */ + protected $internalDefaults = array( + '_UserLang' => 'en', + '_Environment' => false, + '_CompiledDBs' => array(), + '_SafeMode' => false, + '_RaiseMemory' => false, + '_UpgradeDone' => false, + '_InstallDone' => false, + '_Caches' => array(), + '_InstallPassword' => '', + '_SameAccount' => true, + '_CreateDBAccount' => false, + '_NamespaceType' => 'site-name', + '_AdminName' => '', // will be set later, when the user selects language + '_AdminPassword' => '', + '_AdminPassword2' => '', + '_AdminEmail' => '', + '_Subscribe' => false, + '_SkipOptional' => 'continue', + '_RightsProfile' => 'wiki', + '_LicenseCode' => 'none', + '_CCDone' => false, + '_Extensions' => array(), + '_MemCachedServers' => '', + '_UpgradeKeySupplied' => false, + '_ExistingDBSettings' => false, + ); + + /** + * The actual list of installation steps. This will be initialized by getInstallSteps() + * + * @var array + */ + private $installSteps = array(); + + /** + * Extra steps for installation, for things like DatabaseInstallers to modify + * + * @var array + */ + protected $extraInstallSteps = array(); + + /** + * Known object cache types and the functions used to test for their existence. + * + * @var array + */ + protected $objectCaches = array( + 'xcache' => 'xcache_get', + 'apc' => 'apc_fetch', + 'eaccel' => 'eaccelerator_get', + 'wincache' => 'wincache_ucache_get' + ); + + /** + * User rights profiles. + * + * @var array + */ + public $rightsProfiles = array( + 'wiki' => array(), + 'no-anon' => array( + '*' => array( 'edit' => false ) + ), + 'fishbowl' => array( + '*' => array( + 'createaccount' => false, + 'edit' => false, + ), + ), + 'private' => array( + '*' => array( + 'createaccount' => false, + 'edit' => false, + 'read' => false, + ), + ), + ); + + /** + * License types. + * + * @var array + */ + public $licenses = array( + 'cc-by' => array( + 'url' => 'http://creativecommons.org/licenses/by/3.0/', + 'icon' => '{$wgStylePath}/common/images/cc-by.png', + ), + 'cc-by-sa' => array( + 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/', + 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png', + ), + 'cc-by-nc-sa' => array( + 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/', + 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png', + ), + 'cc-0' => array( + 'url' => 'https://creativecommons.org/publicdomain/zero/1.0/', + 'icon' => '{$wgStylePath}/common/images/cc-0.png', + ), + 'pd' => array( + 'url' => '', + 'icon' => '{$wgStylePath}/common/images/public-domain.png', + ), + 'gfdl' => array( + 'url' => 'http://www.gnu.org/copyleft/fdl.html', + 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png', + ), + 'none' => array( + 'url' => '', + 'icon' => '', + 'text' => '' + ), + 'cc-choose' => array( + // Details will be filled in by the selector. + 'url' => '', + 'icon' => '', + 'text' => '', + ), + ); + + /** + * URL to mediawiki-announce subscription + */ + protected $mediaWikiAnnounceUrl = 'https://lists.wikimedia.org/mailman/subscribe/mediawiki-announce'; + + /** + * Supported language codes for Mailman + */ + protected $mediaWikiAnnounceLanguages = array( + 'ca', 'cs', 'da', 'de', 'en', 'es', 'et', 'eu', 'fi', 'fr', 'hr', 'hu', + 'it', 'ja', 'ko', 'lt', 'nl', 'no', 'pl', 'pt', 'pt-br', 'ro', 'ru', + 'sl', 'sr', 'sv', 'tr', 'uk' ); /** @@ -113,17 +299,70 @@ abstract class Installer { */ public abstract function showMessage( $msg /*, ... */ ); + /** + * Same as showMessage(), but for displaying errors + */ + public abstract function showError( $msg /*, ... */ ); + + /** + * Show a message to the installing user by using a Status object + * @param $status Status + */ + public abstract function showStatusMessage( Status $status ); + /** * Constructor, always call this from child classes. */ public function __construct() { + global $wgExtensionMessagesFiles, $wgUser; + // Disable the i18n cache and LoadBalancer Language::getLocalisationCache()->disableBackend(); LBFactory::disableBackend(); + + // Load the installer's i18n file. + $wgExtensionMessagesFiles['MediawikiInstaller'] = + dirname( __FILE__ ) . '/Installer.i18n.php'; + + // Having a user with id = 0 safeguards us from DB access via User::loadOptions(). + $wgUser = User::newFromId( 0 ); + + $this->settings = $this->internalDefaults; + + foreach ( $this->defaultVarNames as $var ) { + $this->settings[$var] = $GLOBALS[$var]; + } + + $compiledDBs = array(); + foreach ( self::getDBTypes() as $type ) { + $installer = $this->getDBInstaller( $type ); + + if ( !$installer->isCompiled() ) { + continue; + } + $compiledDBs[] = $type; + + $defaults = $installer->getGlobalDefaults(); + + foreach ( $installer->getGlobalNames() as $var ) { + if ( isset( $defaults[$var] ) ) { + $this->settings[$var] = $defaults[$var]; + } else { + $this->settings[$var] = $GLOBALS[$var]; + } + } + } + $this->setVar( '_CompiledDBs', $compiledDBs ); + + $this->parserTitle = Title::newFromText( 'Installer' ); + $this->parserOptions = new ParserOptions; // language will be wrong :( + $this->parserOptions->setEditSection( false ); } /** * Get a list of known DB types. + * + * @return array */ public static function getDBTypes() { return self::$dbTypes; @@ -140,29 +379,30 @@ abstract class Installer { * Under the web subclass, it can already be assumed that PHP 5+ is in use * and that sessions are working. * - * @return boolean + * @return Status */ public function doEnvironmentChecks() { - $this->showMessage( 'config-env-php', phpversion() ); - - $good = true; + $phpVersion = phpversion(); + if( version_compare( $phpVersion, self::MINIMUM_PHP_VERSION, '>=' ) ) { + $this->showMessage( 'config-env-php', $phpVersion ); + $good = true; + } else { + $this->showMessage( 'config-env-php-toolow', $phpVersion, self::MINIMUM_PHP_VERSION ); + $good = false; + } - foreach ( $this->envChecks as $check ) { - $status = $this->$check(); - if ( $status === false ) { - $good = false; + if( $good ) { + foreach ( $this->envChecks as $check ) { + $status = $this->$check(); + if ( $status === false ) { + $good = false; + } } } $this->setVar( '_Environment', $good ); - if ( $good ) { - $this->showMessage( 'config-env-good' ); - } else { - $this->showMessage( 'config-env-bad' ); - } - - return $good; + return $good ? Status::newGood() : Status::newFatal( 'config-env-bad' ); } /** @@ -216,33 +456,29 @@ abstract class Installer { } /** - * Determine if LocalSettings exists. If it does, return an appropriate - * status for whether we should can upgrade or not. + * Determine if LocalSettings.php exists. If it does, return its variables, + * merged with those from AdminSettings.php, as an array. * - * @return Status + * @return Array */ - public function getLocalSettingsStatus() { + public static function getExistingLocalSettings() { global $IP; - $status = Status::newGood(); - wfSuppressWarnings(); - $ls = file_exists( "$IP/LocalSettings.php" ); + $_lsExists = file_exists( "$IP/LocalSettings.php" ); wfRestoreWarnings(); - if( $ls ) { - require( "$IP/includes/DefaultSettings.php" ); - require_once( "$IP/LocalSettings.php" ); - $vars = get_defined_vars(); - if( isset( $vars['wgUpgradeKey'] ) && $vars['wgUpgradeKey'] ) { - $status->warning( 'config-localsettings-upgrade' ); - $this->setVar( '_UpgradeKey', $vars['wgUpgradeKey' ] ); - } else { - $status->fatal( 'config-localsettings-noupgrade' ); - } + if( !$_lsExists ) { + return false; } + unset($_lsExists); - return $status; + require( "$IP/includes/DefaultSettings.php" ); + require( "$IP/LocalSettings.php" ); + if ( file_exists( "$IP/AdminSettings.php" ) ) { + require( "$IP/AdminSettings.php" ); + } + return get_defined_vars(); } /** @@ -275,7 +511,7 @@ abstract class Installer { * On POSIX systems return the primary group of the webserver we're running under. * On other systems just returns null. * - * This is used to advice the user that he should chgrp his config/data/images directory as the + * This is used to advice the user that he should chgrp his mw-config/data/images directory as the * webserver user before he can install. * * Public because SqliteInstaller needs it, and doesn't subclass Installer. @@ -306,7 +542,7 @@ abstract class Installer { * external links work just fine. * * But in case a translator decides to throw in a #ifexist or internal link or - * whatever, this function is guarded to catch attempted DB access and to present + * whatever, this function is guarded to catch the attempted DB access and to present * some fallback text. * * @param $text String @@ -331,49 +567,45 @@ abstract class Installer { } /** - * TODO: document - * - * @param $installer DatabaseInstaller - * - * @return Status + * @return ParserOptions */ - public function installDatabase( DatabaseInstaller &$installer ) { - if( !$installer ) { - $type = $this->getVar( 'wgDBtype' ); - $status = Status::newFatal( "config-no-db", $type ); - } else { - $status = $installer->setupDatabase(); - } - - return $status; + public function getParserOptions() { + return $this->parserOptions; } - /** - * TODO: document - * - * @param $installer DatabaseInstaller - * - * @return Status - */ - public function installTables( DatabaseInstaller &$installer ) { - $status = $installer->createTables(); + public function disableLinkPopups() { + $this->parserOptions->setExternalLinkTarget( false ); + } - if( $status->isOK() ) { - LBFactory::enableBackend(); - } - - return $status; + public function restoreLinkPopups() { + global $wgExternalLinkTarget; + $this->parserOptions->setExternalLinkTarget( $wgExternalLinkTarget ); } /** - * TODO: document + * Install step which adds a row to the site_stats table with appropriate + * initial values. * * @param $installer DatabaseInstaller * * @return Status */ - public function installInterwiki( DatabaseInstaller &$installer ) { - return $installer->populateInterwikiTable(); + public function populateSiteStats( DatabaseInstaller $installer ) { + $status = $installer->getConnection(); + if ( !$status->isOK() ) { + return $status; + } + $status->value->insert( 'site_stats', array( + 'ss_row_id' => 1, + 'ss_total_views' => 0, + 'ss_total_edits' => 0, + 'ss_good_articles' => 0, + 'ss_total_pages' => 0, + 'ss_users' => 0, + 'ss_admins' => 0, + 'ss_images' => 0 ), + __METHOD__, 'IGNORE' ); + return Status::newGood(); } /** @@ -387,87 +619,38 @@ abstract class Installer { } } - /** - * Check if we're installing the latest version. - */ - public function envCheckMediaWikiVersion() { - global $wgVersion; - - if( !$this->getVar( '_ExternalHTTP' ) ) { - $this->showMessage( 'config-env-latest-disabled' ); - return; - } - - $repository = wfGetRepository(); - $currentVersion = $repository->getLatestCoreVersion(); - - if ( $currentVersion === false ) { - # For when the request is successful but there's e.g. some silly man in - # the middle firewall blocking us, e.g. one of those annoying airport ones - $this->showMessage( 'config-env-latest-can-not-check', $repository->getLocation() ); - return; - } - - if( version_compare( $wgVersion, $currentVersion, '<' ) ) { - $this->showMessage( 'config-env-latest-old' ); - // FIXME: this only works for the web installer! - $this->showHelpBox( 'config-env-latest-help', $wgVersion, $currentVersion ); - } elseif( version_compare( $wgVersion, $currentVersion, '>' ) ) { - $this->showMessage( 'config-env-latest-new' ); - } else { - $this->showMessage( 'config-env-latest-ok' ); - } - } - /** * Environment check for DB types. */ - public function envCheckDB() { + protected function envCheckDB() { global $wgLang; - $compiledDBs = array(); - $goodNames = array(); $allNames = array(); foreach ( self::getDBTypes() as $name ) { - $db = $this->getDBInstaller( $name ); - $readableName = wfMsg( 'config-type-' . $name ); - - if ( $db->isCompiled() ) { - $compiledDBs[] = $name; - $goodNames[] = $readableName; - } - - $allNames[] = $readableName; + $allNames[] = wfMsg( "config-type-$name" ); } - $this->setVar( '_CompiledDBs', $compiledDBs ); - - if ( !$compiledDBs ) { - $this->showMessage( 'config-no-db' ); - // FIXME: this only works for the web installer! - $this->showHelpBox( 'config-no-db-help', $wgLang->commaList( $allNames ) ); + if ( !$this->getVar( '_CompiledDBs' ) ) { + $this->showError( 'config-no-db', $wgLang->commaList( $allNames ) ); + // @todo FIXME: This only works for the web installer! return false; } - $this->showMessage( 'config-have-db', $wgLang->listToText( $goodNames ), count( $goodNames ) ); - // Check for FTS3 full-text search module $sqlite = $this->getDBInstaller( 'sqlite' ); if ( $sqlite->isCompiled() ) { - $db = new DatabaseSqliteStandalone( ':memory:' ); - $this->showMessage( $db->getFulltextSearchModule() == 'FTS3' - ? 'config-have-fts3' - : 'config-no-fts3' - ); + if( DatabaseSqlite::getFulltextSearchModule() != 'FTS3' ) { + $this->showMessage( 'config-no-fts3' ); + } } } /** * Environment check for register_globals. */ - public function envCheckRegisterGlobals() { - if( wfIniGetBool( "magic_quotes_runtime" ) ) { + protected function envCheckRegisterGlobals() { + if( wfIniGetBool( 'register_globals' ) ) { $this->showMessage( 'config-register-globals' ); } } @@ -475,10 +658,10 @@ abstract class Installer { /** * Some versions of libxml+PHP break < and > encoding horribly */ - public function envCheckBrokenXML() { + protected function envCheckBrokenXML() { $test = new PhpXmlBugTester(); if ( !$test->ok ) { - $this->showMessage( 'config-brokenlibxml' ); + $this->showError( 'config-brokenlibxml' ); return false; } } @@ -487,11 +670,11 @@ abstract class Installer { * Test PHP (probably 5.3.1, but it could regress again) to make sure that * reference parameters to __call() are not converted to null */ - public function envCheckPHP531() { + protected function envCheckPHP531() { $test = new PhpRefCallBugTester; $test->execute(); if ( !$test->ok ) { - $this->showMessage( 'config-using531' ); + $this->showError( 'config-using531', phpversion() ); return false; } } @@ -499,9 +682,9 @@ abstract class Installer { /** * Environment check for magic_quotes_runtime. */ - public function envCheckMagicQuotes() { + protected function envCheckMagicQuotes() { if( wfIniGetBool( "magic_quotes_runtime" ) ) { - $this->showMessage( 'config-magic-quotes-runtime' ); + $this->showError( 'config-magic-quotes-runtime' ); return false; } } @@ -509,9 +692,9 @@ abstract class Installer { /** * Environment check for magic_quotes_sybase. */ - public function envCheckMagicSybase() { + protected function envCheckMagicSybase() { if ( wfIniGetBool( 'magic_quotes_sybase' ) ) { - $this->showMessage( 'config-magic-quotes-sybase' ); + $this->showError( 'config-magic-quotes-sybase' ); return false; } } @@ -519,9 +702,9 @@ abstract class Installer { /** * Environment check for mbstring.func_overload. */ - public function envCheckMbstring() { + protected function envCheckMbstring() { if ( wfIniGetBool( 'mbstring.func_overload' ) ) { - $this->showMessage( 'config-mbstring' ); + $this->showError( 'config-mbstring' ); return false; } } @@ -529,9 +712,9 @@ abstract class Installer { /** * Environment check for zend.ze1_compatibility_mode. */ - public function envCheckZE1() { + protected function envCheckZE1() { if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) { - $this->showMessage( 'config-ze1' ); + $this->showError( 'config-ze1' ); return false; } } @@ -539,7 +722,7 @@ abstract class Installer { /** * Environment check for safe_mode. */ - public function envCheckSafeMode() { + protected function envCheckSafeMode() { if ( wfIniGetBool( 'safe_mode' ) ) { $this->setVar( '_SafeMode', true ); $this->showMessage( 'config-safe-mode' ); @@ -549,20 +732,26 @@ abstract class Installer { /** * Environment check for the XML module. */ - public function envCheckXML() { + protected function envCheckXML() { if ( !function_exists( "utf8_encode" ) ) { - $this->showMessage( 'config-xml-bad' ); + $this->showError( 'config-xml-bad' ); return false; } - $this->showMessage( 'config-xml-good' ); } /** * Environment check for the PCRE module. */ - public function envCheckPCRE() { + protected function envCheckPCRE() { if ( !function_exists( 'preg_match' ) ) { - $this->showMessage( 'config-pcre' ); + $this->showError( 'config-pcre' ); + return false; + } + wfSuppressWarnings(); + $regexd = preg_replace( '/[\x{0430}-\x{04FF}]/iu', '', '-АБВГД-' ); + wfRestoreWarnings(); + if ( $regexd != '--' ) { + $this->showError( 'config-pcre-no-utf8' ); return false; } } @@ -570,19 +759,14 @@ abstract class Installer { /** * Environment check for available memory. */ - public function envCheckMemory() { + protected function envCheckMemory() { $limit = ini_get( 'memory_limit' ); if ( !$limit || $limit == -1 ) { - $this->showMessage( 'config-memory-none' ); return true; } - $n = intval( $limit ); - - if( preg_match( '/^([0-9]+)[Mm]$/', trim( $limit ), $m ) ) { - $n = intval( $m[1] * ( 1024 * 1024 ) ); - } + $n = wfShorthandToInteger( $limit ); if( $n < $this->minMemorySize * 1024 * 1024 ) { $newLimit = "{$this->minMemorySize}M"; @@ -594,20 +778,21 @@ abstract class Installer { $this->setVar( '_RaiseMemory', true ); } } else { - $this->showMessage( 'config-memory-ok', $limit ); + return true; } } /** * Environment check for compiled object cache types. */ - public function envCheckCache() { + protected function envCheckCache() { $caches = array(); - foreach ( $this->objectCaches as $name => $function ) { if ( function_exists( $function ) ) { + if ( $name == 'xcache' && !wfIniGetBool( 'xcache.var_size' ) ) { + continue; + } $caches[$name] = true; - $this->showMessage( 'config-' . $name ); } } @@ -618,17 +803,25 @@ abstract class Installer { $this->setVar( '_Caches', $caches ); } + /** + * Scare user to death if they have mod_security + */ + protected function envCheckModSecurity() { + if ( self::apacheModulePresent( 'mod_security' ) ) { + $this->showMessage( 'config-mod-security' ); + } + } + /** * Search for GNU diff3. */ - public function envCheckDiff3() { + protected function envCheckDiff3() { $names = array( "gdiff3", "diff3", "diff3.exe" ); - $versionInfo = array( '$1 --version 2>&1', 'diff3 (GNU diffutils)' ); + $versionInfo = array( '$1 --version 2>&1', 'GNU diffutils' ); - $diff3 = $this->locateExecutableInDefaultPaths( $names, $versionInfo ); + $diff3 = self::locateExecutableInDefaultPaths( $names, $versionInfo ); if ( $diff3 ) { - $this->showMessage( 'config-diff3-good', $diff3 ); $this->setVar( 'wgDiff3', $diff3 ); } else { $this->setVar( 'wgDiff3', false ); @@ -639,10 +832,11 @@ abstract class Installer { /** * Environment check for ImageMagick and GD. */ - public function envCheckGraphics() { + protected function envCheckGraphics() { $names = array( wfIsWindows() ? 'convert.exe' : 'convert' ); - $convert = $this->locateExecutableInDefaultPaths( $names, array( '$1 -version', 'ImageMagick' ) ); + $convert = self::locateExecutableInDefaultPaths( $names, array( '$1 -version', 'ImageMagick' ) ); + $this->setVar( 'wgImageMagickConvertCommand', '' ); if ( $convert ) { $this->setVar( 'wgImageMagickConvertCommand', $convert ); $this->showMessage( 'config-imagemagick', $convert ); @@ -651,59 +845,53 @@ abstract class Installer { $this->showMessage( 'config-gd' ); return true; } else { - $this->showMessage( 'no-scaling' ); + $this->showMessage( 'config-no-scaling' ); } } + /** + * Environment check for the server hostname. + */ + protected function envCheckServer() { + if ( $this->getVar( 'wgServer' ) ) { + // wgServer was pre-defined, perhaps by the cli installer + $server = $this->getVar( 'wgServer' ); + } else { + $server = WebRequest::detectServer(); + } + $this->showMessage( 'config-using-server', $server ); + $this->setVar( 'wgServer', $server ); + } + /** * Environment check for setting $IP and $wgScriptPath. */ - public function envCheckPath() { + protected function envCheckPath() { global $IP; $IP = dirname( dirname( dirname( __FILE__ ) ) ); - $this->setVar( 'IP', $IP ); - $this->showMessage( 'config-dir', $IP ); - - // PHP_SELF isn't available sometimes, such as when PHP is CGI but - // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME - // to get the path to the current script... hopefully it's reliable. SIGH - if ( !empty( $_SERVER['PHP_SELF'] ) ) { - $path = $_SERVER['PHP_SELF']; - } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) { - $path = $_SERVER['SCRIPT_NAME']; - } elseif ( $this->getVar( 'wgScriptPath' ) ) { - // Some kind soul has set it for us already (e.g. debconf) - return true; - } else { - $this->showMessage( 'config-no-uri' ); - return false; - } - $uri = preg_replace( '{^(.*)/config.*$}', '$1', $path ); - $this->setVar( 'wgScriptPath', $uri ); - $this->showMessage( 'config-uri', $uri ); + $this->showMessage( 'config-using-uri', $this->getVar( 'wgServer' ), $this->getVar( 'wgScriptPath' ) ); + return true; } /** * Environment check for setting the preferred PHP file extension. */ - public function envCheckExtension() { - // FIXME: detect this properly + protected function envCheckExtension() { + // @todo FIXME: Detect this properly if ( defined( 'MW_INSTALL_PHP5_EXT' ) ) { $ext = 'php5'; } else { $ext = 'php'; } - $this->setVar( 'wgScriptExtension', ".$ext" ); - $this->showMessage( 'config-file-extension', $ext ); } /** * TODO: document */ - public function envCheckShellLocale() { + protected function envCheckShellLocale() { $os = php_uname( 's' ); $supported = array( 'Linux', 'SunOS', 'HP-UX', 'Darwin' ); # Tested these @@ -741,7 +929,6 @@ abstract class Installer { # Try the current value of LANG. if ( isset( $candidatesByLocale[ getenv( 'LANG' ) ] ) ) { $this->setVar( 'wgShellLocale', getenv( 'LANG' ) ); - $this->showMessage( 'config-shell-locale', getenv( 'LANG' ) ); return true; } @@ -750,7 +937,6 @@ abstract class Installer { foreach ( $commonLocales as $commonLocale ) { if ( isset( $candidatesByLocale[$commonLocale] ) ) { $this->setVar( 'wgShellLocale', $commonLocale ); - $this->showMessage( 'config-shell-locale', $commonLocale ); return true; } } @@ -761,7 +947,6 @@ abstract class Installer { if ( isset( $candidatesByLang[$wikiLang] ) ) { $m = reset( $candidatesByLang[$wikiLang] ); $this->setVar( 'wgShellLocale', $m[0] ); - $this->showMessage( 'config-shell-locale', $m[0] ); return true; } @@ -769,7 +954,6 @@ abstract class Installer { if ( count( $candidatesByLocale ) ) { $m = reset( $candidatesByLocale ); $this->setVar( 'wgShellLocale', $m[0] ); - $this->showMessage( 'config-shell-locale', $m[0] ); return true; } @@ -780,20 +964,38 @@ abstract class Installer { /** * TODO: document */ - public function envCheckUploadsDirectory() { - global $IP, $wgServer; + protected function envCheckUploadsDirectory() { + global $IP; $dir = $IP . '/images/'; - $url = $wgServer . $this->getVar( 'wgScriptPath' ) . '/images/'; + $url = $this->getVar( 'wgServer' ) . $this->getVar( 'wgScriptPath' ) . '/images/'; $safe = !$this->dirIsExecutable( $dir, $url ); if ( $safe ) { - $this->showMessage( 'config-uploads-safe' ); + return true; } else { $this->showMessage( 'config-uploads-not-safe', $dir ); } } + /** + * Checks if suhosin.get.max_value_length is set, and if so, sets + * $wgResourceLoaderMaxQueryLength to that value in the generated + * LocalSettings file + */ + protected function envCheckSuhosinMaxValueLength() { + $maxValueLength = ini_get( 'suhosin.get.max_value_length' ); + if ( $maxValueLength > 0 ) { + if( $maxValueLength < 1024 ) { + # Only warn if the value is below the sane 1024 + $this->showMessage( 'config-suhosin-max-value-length', $maxValueLength ); + } + } else { + $maxValueLength = -1; + } + $this->setVar( 'wgResourceLoaderMaxQueryLength', $maxValueLength ); + } + /** * Convert a hex string representing a Unicode code point to that code point. * @param $c String @@ -803,12 +1005,12 @@ abstract class Installer { $c = hexdec($c); if ($c <= 0x7F) { return chr($c); - } else if ($c <= 0x7FF) { + } elseif ($c <= 0x7FF) { return chr(0xC0 | $c >> 6) . chr(0x80 | $c & 0x3F); - } else if ($c <= 0xFFFF) { + } elseif ($c <= 0xFFFF) { return chr(0xE0 | $c >> 12) . chr(0x80 | $c >> 6 & 0x3F) . chr(0x80 | $c & 0x3F); - } else if ($c <= 0x10FFFF) { + } elseif ($c <= 0x10FFFF) { return chr(0xF0 | $c >> 18) . chr(0x80 | $c >> 12 & 0x3F) . chr(0x80 | $c >> 6 & 0x3F) . chr(0x80 | $c & 0x3F); @@ -821,7 +1023,7 @@ abstract class Installer { /** * Check the libicu version */ - public function envCheckLibicu() { + protected function envCheckLibicu() { $utf8 = function_exists( 'utf8_normalize' ); $intl = function_exists( 'normalizer_normalize' ); @@ -844,7 +1046,7 @@ abstract class Installer { */ if( $utf8 ) { $useNormalizer = 'utf8'; - $utf8 = utf8_normalize( $not_normal_c, UNORM_NFC ); + $utf8 = utf8_normalize( $not_normal_c, UtfNormal::UNORM_NFC ); if ( $utf8 !== $normal_c ) $needsUpdate = true; } if( $intl ) { @@ -889,7 +1091,7 @@ abstract class Installer { * @param $path String: path to search * @param $names Array of executable names * @param $versionInfo Boolean false or array with two members: - * 0 => Command to run for version check, with $1 for the path + * 0 => Command to run for version check, with $1 for the full executable name * 1 => String to compare the output with * * If $versionInfo is not false, only executables with a version @@ -912,11 +1114,8 @@ abstract class Installer { return $command; } - if ( wfIsWindows() ) { - $command = "\"$command\""; - } - $file = str_replace( '$1', $command, $versionInfo[0] ); - if ( strstr( wfShellExec( $file ), $versionInfo[1]) !== false ) { + $file = str_replace( '$1', wfEscapeShellArg( $command ), $versionInfo[0] ); + if ( strstr( wfShellExec( $file ), $versionInfo[1] ) !== false ) { return $command; } } @@ -927,6 +1126,9 @@ abstract class Installer { /** * Same as locateExecutable(), but checks in getPossibleBinPaths() by default * @see locateExecutable() + * @param $names + * @param $versionInfo bool + * @return bool|string */ public static function locateExecutableInDefaultPaths( $names, $versionInfo = false ) { foreach( self::getPossibleBinPaths() as $path ) { @@ -963,7 +1165,13 @@ abstract class Installer { break; } - $text = Http::get( $url . $file ); + try { + $text = Http::get( $url . $file, array( 'timeout' => 3 ) ); + } + catch( MWException $e ) { + // Http::get throws with allow_url_fopen = false and no curl extension. + $text = null; + } unlink( $dir . $file ); if ( $text == 'exec' ) { @@ -978,4 +1186,390 @@ abstract class Installer { return false; } + /** + * Checks for presence of an Apache module. Works only if PHP is running as an Apache module, too. + * + * @param $moduleName String: Name of module to check. + * @return bool + */ + public static function apacheModulePresent( $moduleName ) { + if ( function_exists( 'apache_get_modules' ) && in_array( $moduleName, apache_get_modules() ) ) { + return true; + } + // try it the hard way + ob_start(); + phpinfo( INFO_MODULES ); + $info = ob_get_clean(); + return strpos( $info, $moduleName ) !== false; + } + + /** + * ParserOptions are constructed before we determined the language, so fix it + * + * @param $lang Language + */ + public function setParserLanguage( $lang ) { + $this->parserOptions->setTargetLanguage( $lang ); + $this->parserOptions->setUserLang( $lang ); + } + + /** + * Overridden by WebInstaller to provide lastPage parameters. + * @param $page stirng + * @return string + */ + protected function getDocUrl( $page ) { + return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page ); + } + + /** + * Finds extensions that follow the format /extensions/Name/Name.php, + * and returns an array containing the value for 'Name' for each found extension. + * + * @return array + */ + public function findExtensions() { + if( $this->getVar( 'IP' ) === null ) { + return false; + } + + $exts = array(); + $extDir = $this->getVar( 'IP' ) . '/extensions'; + $dh = opendir( $extDir ); + + while ( ( $file = readdir( $dh ) ) !== false ) { + if( !is_dir( "$extDir/$file" ) ) { + continue; + } + if( file_exists( "$extDir/$file/$file.php" ) ) { + $exts[] = $file; + } + } + + return $exts; + } + + /** + * Installs the auto-detected extensions. + * + * @return Status + */ + protected function includeExtensions() { + global $IP; + $exts = $this->getVar( '_Extensions' ); + $IP = $this->getVar( 'IP' ); + + /** + * We need to include DefaultSettings before including extensions to avoid + * warnings about unset variables. However, the only thing we really + * want here is $wgHooks['LoadExtensionSchemaUpdates']. This won't work + * if the extension has hidden hook registration in $wgExtensionFunctions, + * but we're not opening that can of worms + * @see https://bugzilla.wikimedia.org/show_bug.cgi?id=26857 + */ + global $wgAutoloadClasses; + $wgAutoloadClasses = array(); + + require( "$IP/includes/DefaultSettings.php" ); + + foreach( $exts as $e ) { + require_once( "$IP/extensions/$e/$e.php" ); + } + + $hooksWeWant = isset( $wgHooks['LoadExtensionSchemaUpdates'] ) ? + $wgHooks['LoadExtensionSchemaUpdates'] : array(); + + // Unset everyone else's hooks. Lord knows what someone might be doing + // in ParserFirstCallInit (see bug 27171) + $GLOBALS['wgHooks'] = array( 'LoadExtensionSchemaUpdates' => $hooksWeWant ); + + return Status::newGood(); + } + + /** + * Get an array of install steps. Should always be in the format of + * array( + * 'name' => 'someuniquename', + * 'callback' => array( $obj, 'method' ), + * ) + * There must be a config-install-$name message defined per step, which will + * be shown on install. + * + * @param $installer DatabaseInstaller so we can make callbacks + * @return array + */ + protected function getInstallSteps( DatabaseInstaller $installer ) { + $coreInstallSteps = array( + array( 'name' => 'database', 'callback' => array( $installer, 'setupDatabase' ) ), + array( 'name' => 'tables', 'callback' => array( $installer, 'createTables' ) ), + array( 'name' => 'interwiki', 'callback' => array( $installer, 'populateInterwikiTable' ) ), + array( 'name' => 'stats', 'callback' => array( $this, 'populateSiteStats' ) ), + array( 'name' => 'keys', 'callback' => array( $this, 'generateKeys' ) ), + array( 'name' => 'sysop', 'callback' => array( $this, 'createSysop' ) ), + array( 'name' => 'mainpage', 'callback' => array( $this, 'createMainpage' ) ), + ); + + // Build the array of install steps starting from the core install list, + // then adding any callbacks that wanted to attach after a given step + foreach( $coreInstallSteps as $step ) { + $this->installSteps[] = $step; + if( isset( $this->extraInstallSteps[ $step['name'] ] ) ) { + $this->installSteps = array_merge( + $this->installSteps, + $this->extraInstallSteps[ $step['name'] ] + ); + } + } + + // Prepend any steps that want to be at the beginning + if( isset( $this->extraInstallSteps['BEGINNING'] ) ) { + $this->installSteps = array_merge( + $this->extraInstallSteps['BEGINNING'], + $this->installSteps + ); + } + + // Extensions should always go first, chance to tie into hooks and such + if( count( $this->getVar( '_Extensions' ) ) ) { + array_unshift( $this->installSteps, + array( 'name' => 'extensions', 'callback' => array( $this, 'includeExtensions' ) ) + ); + $this->installSteps[] = array( + 'name' => 'extension-tables', + 'callback' => array( $installer, 'createExtensionTables' ) + ); + } + return $this->installSteps; + } + + /** + * Actually perform the installation. + * + * @param $startCB Array A callback array for the beginning of each step + * @param $endCB Array A callback array for the end of each step + * + * @return Array of Status objects + */ + public function performInstallation( $startCB, $endCB ) { + $installResults = array(); + $installer = $this->getDBInstaller(); + $installer->preInstall(); + $steps = $this->getInstallSteps( $installer ); + foreach( $steps as $stepObj ) { + $name = $stepObj['name']; + call_user_func_array( $startCB, array( $name ) ); + + // Perform the callback step + $status = call_user_func( $stepObj['callback'], $installer ); + + // Output and save the results + call_user_func( $endCB, $name, $status ); + $installResults[$name] = $status; + + // If we've hit some sort of fatal, we need to bail. + // Callback already had a chance to do output above. + if( !$status->isOk() ) { + break; + } + } + if( $status->isOk() ) { + $this->setVar( '_InstallDone', true ); + } + return $installResults; + } + + /** + * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of + * /dev/urandom + * + * @return Status + */ + public function generateKeys() { + $keys = array( 'wgSecretKey' => 64 ); + if ( strval( $this->getVar( 'wgUpgradeKey' ) ) === '' ) { + $keys['wgUpgradeKey'] = 16; + } + return $this->doGenerateKeys( $keys ); + } + + /** + * Generate a secret value for variables using either + * /dev/urandom or mt_rand(). Produce a warning in the later case. + * + * @param $keys Array + * @return Status + */ + protected function doGenerateKeys( $keys ) { + $status = Status::newGood(); + + wfSuppressWarnings(); + $file = fopen( "/dev/urandom", "r" ); + wfRestoreWarnings(); + + foreach ( $keys as $name => $length ) { + if ( $file ) { + $secretKey = bin2hex( fread( $file, $length / 2 ) ); + } else { + $secretKey = ''; + + for ( $i = 0; $i < $length / 8; $i++ ) { + $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) ); + } + } + + $this->setVar( $name, $secretKey ); + } + + if ( $file ) { + fclose( $file ); + } else { + $names = array_keys ( $keys ); + $names = preg_replace( '/^(.*)$/', '\$$1', $names ); + global $wgLang; + $status->warning( 'config-insecure-keys', $wgLang->listToText( $names ), count( $names ) ); + } + + return $status; + } + + /** + * Create the first user account, grant it sysop and bureaucrat rights + * + * @return Status + */ + protected function createSysop() { + $name = $this->getVar( '_AdminName' ); + $user = User::newFromName( $name ); + + if ( !$user ) { + // We should've validated this earlier anyway! + return Status::newFatal( 'config-admin-error-user', $name ); + } + + if ( $user->idForName() == 0 ) { + $user->addToDatabase(); + + try { + $user->setPassword( $this->getVar( '_AdminPassword' ) ); + } catch( PasswordError $pwe ) { + return Status::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() ); + } + + $user->addGroup( 'sysop' ); + $user->addGroup( 'bureaucrat' ); + if( $this->getVar( '_AdminEmail' ) ) { + $user->setEmail( $this->getVar( '_AdminEmail' ) ); + } + $user->saveSettings(); + + // Update user count + $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 ); + $ssUpdate->doUpdate(); + } + $status = Status::newGood(); + + if( $this->getVar( '_Subscribe' ) && $this->getVar( '_AdminEmail' ) ) { + $this->subscribeToMediaWikiAnnounce( $status ); + } + + return $status; + } + + private function subscribeToMediaWikiAnnounce( Status $s ) { + $params = array( + 'email' => $this->getVar( '_AdminEmail' ), + 'language' => 'en', + 'digest' => 0 + ); + + // Mailman doesn't support as many languages as we do, so check to make + // sure their selected language is available + $myLang = $this->getVar( '_UserLang' ); + if( in_array( $myLang, $this->mediaWikiAnnounceLanguages ) ) { + $myLang = $myLang == 'pt-br' ? 'pt_BR' : $myLang; // rewrite to Mailman's pt_BR + $params['language'] = $myLang; + } + + if( MWHttpRequest::canMakeRequests() ) { + $res = MWHttpRequest::factory( $this->mediaWikiAnnounceUrl, + array( 'method' => 'POST', 'postData' => $params ) )->execute(); + if( !$res->isOK() ) { + $s->warning( 'config-install-subscribe-fail', $res->getMessage() ); + } + } else { + $s->warning( 'config-install-subscribe-notpossible' ); + } + } + + /** + * Insert Main Page with default content. + * + * @param $installer DatabaseInstaller + * @return Status + */ + protected function createMainpage( DatabaseInstaller $installer ) { + $status = Status::newGood(); + try { + $article = new Article( Title::newMainPage() ); + $article->doEdit( wfMsgForContent( 'mainpagetext' ) . "\n\n" . + wfMsgForContent( 'mainpagedocfooter' ), + '', + EDIT_NEW, + false, + User::newFromName( 'MediaWiki default' ) ); + } catch (MWException $e) { + //using raw, because $wgShowExceptionDetails can not be set yet + $status->fatal( 'config-install-mainpage-failed', $e->getMessage() ); + } + + return $status; + } + + /** + * Override the necessary bits of the config to run an installation. + */ + public static function overrideConfig() { + define( 'MW_NO_SESSION', 1 ); + + // Don't access the database + $GLOBALS['wgUseDatabaseMessages'] = false; + // Debug-friendly + $GLOBALS['wgShowExceptionDetails'] = true; + // Don't break forms + $GLOBALS['wgExternalLinkTarget'] = '_blank'; + + // Extended debugging + $GLOBALS['wgShowSQLErrors'] = true; + $GLOBALS['wgShowDBErrorBacktrace'] = true; + + // Allow multiple ob_flush() calls + $GLOBALS['wgDisableOutputCompression'] = true; + + // Use a sensible cookie prefix (not my_wiki) + $GLOBALS['wgCookiePrefix'] = 'mw_installer'; + + // Some of the environment checks make shell requests, remove limits + $GLOBALS['wgMaxShellMemory'] = 0; + } + + /** + * Add an installation step following the given step. + * + * @param $callback Array A valid installation callback array, in this form: + * array( 'name' => 'some-unique-name', 'callback' => array( $obj, 'function' ) ); + * @param $findStep String the step to find. Omit to put the step at the beginning + */ + public function addInstallStep( $callback, $findStep = 'BEGINNING' ) { + $this->extraInstallSteps[$findStep][] = $callback; + } + + /** + * Disable the time limit for execution. + * Some long-running pages (Install, Upgrade) will want to do this + */ + protected function disableTimeLimit() { + wfSuppressWarnings(); + set_time_limit( 0 ); + wfRestoreWarnings(); + } }