(bug 34289) user.options CSS loaded twice. Fixed by splitting off the CSS part of...
[lhc/web/wiklou.git] / includes / installer / Installer.php
index f757cd3..5580f17 100644 (file)
@@ -73,6 +73,7 @@ abstract class Installer {
                'postgres',
                'oracle',
                'sqlite',
+               'ibm_db2',
        );
 
        /**
@@ -96,13 +97,17 @@ abstract class Installer {
                'envCheckPCRE',
                'envCheckMemory',
                'envCheckCache',
+               'envCheckModSecurity',
                'envCheckDiff3',
                'envCheckGraphics',
+               'envCheckServer',
                'envCheckPath',
                'envCheckExtension',
                'envCheckShellLocale',
                'envCheckUploadsDirectory',
-               'envCheckLibicu'
+               'envCheckLibicu',
+               'envCheckSuhosinMaxValueLength',
+               'envCheckCtype',
        );
 
        /**
@@ -129,6 +134,7 @@ abstract class Installer {
                'wgDiff3',
                'wgImageMagickConvertCommand',
                'IP',
+               'wgServer',
                'wgScriptPath',
                'wgScriptExtension',
                'wgMetaNamespace',
@@ -140,6 +146,7 @@ abstract class Installer {
                'wgUseInstantCommons',
                'wgUpgradeKey',
                'wgDefaultSkin',
+               'wgResourceLoaderMaxQueryLength',
        );
 
        /**
@@ -158,7 +165,6 @@ abstract class Installer {
                '_UpgradeDone' => false,
                '_InstallDone' => false,
                '_Caches' => array(),
-               '_InstallUser' => 'root',
                '_InstallPassword' => '',
                '_SameAccount' => true,
                '_CreateDBAccount' => false,
@@ -200,7 +206,6 @@ abstract class Installer {
        protected $objectCaches = array(
                'xcache' => 'xcache_get',
                'apc' => 'apc_fetch',
-               'eaccel' => 'eaccelerator_get',
                'wincache' => 'wincache_ucache_get'
        );
 
@@ -235,6 +240,10 @@ abstract class Installer {
         * @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',
@@ -248,14 +257,10 @@ abstract class Installer {
                        'icon' => '{$wgStylePath}/common/images/cc-0.png',
                ),
                'pd' => array(
-                       'url' => 'http://creativecommons.org/licenses/publicdomain/',
+                       'url' => '',
                        'icon' => '{$wgStylePath}/common/images/public-domain.png',
                ),
-               'gfdl-old' => array(
-                       'url' => 'http://www.gnu.org/licenses/old-licenses/fdl-1.2.html',
-                       'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
-               ),
-               'gfdl-current' => array(
+               'gfdl' => array(
                        'url' => 'http://www.gnu.org/copyleft/fdl.html',
                        'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
                ),
@@ -291,9 +296,16 @@ abstract class Installer {
         * The parameters are like parameters to wfMsg().
         * The messages will be in wikitext format, which will be converted to an
         * output format such as HTML or text before being sent to the user.
+        * @param $msg
         */
        public abstract function showMessage( $msg /*, ... */ );
 
+       /**
+        * Same as showMessage(), but for displaying errors
+        * @param $msg
+        */
+       public abstract function showError( $msg /*, ... */ );
+
        /**
         * Show a message to the installing user by using a Status object
         * @param $status Status
@@ -323,12 +335,14 @@ abstract class Installer {
                        $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();
 
@@ -340,6 +354,7 @@ abstract class Installer {
                                }
                        }
                }
+               $this->setVar( '_CompiledDBs', $compiledDBs );
 
                $this->parserTitle = Title::newFromText( 'Installer' );
                $this->parserOptions = new ParserOptions; // language will  be wrong :(
@@ -348,6 +363,8 @@ abstract class Installer {
 
        /**
         * Get a list of known DB types.
+        *
+        * @return array
         */
        public static function getDBTypes() {
                return self::$dbTypes;
@@ -446,7 +463,7 @@ abstract class Installer {
         *
         * @return Array
         */
-       public function getExistingLocalSettings() {
+       public static function getExistingLocalSettings() {
                global $IP;
 
                wfSuppressWarnings();
@@ -551,6 +568,9 @@ abstract class Installer {
                return $html;
        }
 
+       /**
+        * @return ParserOptions
+        */
        public function getParserOptions() {
                return $this->parserOptions;
        }
@@ -567,6 +587,10 @@ abstract class Installer {
        /**
         * Install step which adds a row to the site_stats table with appropriate
         * initial values.
+        *
+        * @param $installer DatabaseInstaller
+        *
+        * @return Status
         */
        public function populateSiteStats( DatabaseInstaller $installer ) {
                $status = $installer->getConnection();
@@ -580,7 +604,6 @@ abstract class Installer {
                        'ss_good_articles' => 0,
                        'ss_total_pages' => 0,
                        'ss_users' => 0,
-                       'ss_admins' => 0,
                        'ss_images' => 0 ),
                        __METHOD__, 'IGNORE' );
                return Status::newGood();
@@ -599,44 +622,50 @@ abstract class Installer {
 
        /**
         * Environment check for DB types.
+        * @return bool
         */
        protected function envCheckDB() {
                global $wgLang;
 
-               $compiledDBs = array();
                $allNames = array();
 
                foreach ( self::getDBTypes() as $name ) {
-                       if ( $this->getDBInstaller( $name )->isCompiled() ) {
-                               $compiledDBs[] = $name;
-                       }
-                       $allNames[] = wfMsg( 'config-type-' . $name );;
+                       $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 ) );
-                       return false;
+               // cache initially available databases to make sure that everything will be displayed correctly
+               // after a refresh on env checks page
+               $databases = $this->getVar( '_CompiledDBs-preFilter' );
+               if ( !$databases ) {
+                       $databases = $this->getVar( '_CompiledDBs' );
+                       $this->setVar( '_CompiledDBs-preFilter', $databases );
                }
 
-               // Check for FTS3 full-text search module
-               $sqlite = $this->getDBInstaller( 'sqlite' );
-               if ( $sqlite->isCompiled() ) {
-                       $db = new DatabaseSqliteStandalone( ':memory:' );
-                       if( $db->getFulltextSearchModule() != 'FTS3' ) {
-                               $this->showMessage( 'config-no-fts3' );
+               $databases = array_flip ( $databases );
+               foreach ( array_keys( $databases ) as $db ) {
+                       $installer = $this->getDBInstaller( $db );
+                       $status = $installer->checkPrerequisites();
+                       if ( !$status->isGood() ) {
+                               $this->showStatusMessage( $status );
                        }
+                       if ( !$status->isOK() ) {
+                               unset( $databases[$db] );
+                       }
+               }
+               $databases = array_flip( $databases );
+               if ( !$databases ) {
+                       $this->showError( 'config-no-db', $wgLang->commaList( $allNames ) );
+                       // @todo FIXME: This only works for the web installer!
+                       return false;
                }
+               $this->setVar( '_CompiledDBs', $databases );
        }
 
        /**
         * Environment check for register_globals.
         */
        protected function envCheckRegisterGlobals() {
-               if( wfIniGetBool( "magic_quotes_runtime" ) ) {
+               if( wfIniGetBool( 'register_globals' ) ) {
                        $this->showMessage( 'config-register-globals' );
                }
        }
@@ -647,7 +676,7 @@ abstract class Installer {
        protected function envCheckBrokenXML() {
                $test = new PhpXmlBugTester();
                if ( !$test->ok ) {
-                       $this->showMessage( 'config-brokenlibxml' );
+                       $this->showError( 'config-brokenlibxml' );
                        return false;
                }
        }
@@ -660,7 +689,7 @@ abstract class Installer {
                $test = new PhpRefCallBugTester;
                $test->execute();
                if ( !$test->ok ) {
-                       $this->showMessage( 'config-using531', phpversion() );
+                       $this->showError( 'config-using531', phpversion() );
                        return false;
                }
        }
@@ -670,7 +699,7 @@ abstract class Installer {
         */
        protected function envCheckMagicQuotes() {
                if( wfIniGetBool( "magic_quotes_runtime" ) ) {
-                       $this->showMessage( 'config-magic-quotes-runtime' );
+                       $this->showError( 'config-magic-quotes-runtime' );
                        return false;
                }
        }
@@ -680,7 +709,7 @@ abstract class Installer {
         */
        protected function envCheckMagicSybase() {
                if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
-                       $this->showMessage( 'config-magic-quotes-sybase' );
+                       $this->showError( 'config-magic-quotes-sybase' );
                        return false;
                }
        }
@@ -690,7 +719,7 @@ abstract class Installer {
         */
        protected function envCheckMbstring() {
                if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
-                       $this->showMessage( 'config-mbstring' );
+                       $this->showError( 'config-mbstring' );
                        return false;
                }
        }
@@ -700,7 +729,7 @@ abstract class Installer {
         */
        protected function envCheckZE1() {
                if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
-                       $this->showMessage( 'config-ze1' );
+                       $this->showError( 'config-ze1' );
                        return false;
                }
        }
@@ -720,7 +749,7 @@ abstract class Installer {
         */
        protected function envCheckXML() {
                if ( !function_exists( "utf8_encode" ) ) {
-                       $this->showMessage( 'config-xml-bad' );
+                       $this->showError( 'config-xml-bad' );
                        return false;
                }
        }
@@ -730,14 +759,14 @@ abstract class Installer {
         */
        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->showMessage( 'config-pcre-no-utf8' );
+                       $this->showError( 'config-pcre-no-utf8' );
                        return false;
                }
        }
@@ -775,6 +804,9 @@ abstract class Installer {
                $caches = array();
                foreach ( $this->objectCaches as $name => $function ) {
                        if ( function_exists( $function ) ) {
+                               if ( $name == 'xcache' && !wfIniGetBool( 'xcache.var_size' ) ) {
+                                       continue;
+                               }
                                $caches[$name] = true;
                        }
                }
@@ -786,6 +818,15 @@ 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.
         */
@@ -823,39 +864,39 @@ abstract class Installer {
                }
        }
 
+       /**
+        * Environment check for the server hostname.
+        */
+       protected function envCheckServer() {
+               $server = $this->envGetDefaultServer();
+               $this->showMessage( 'config-using-server', $server );
+               $this->setVar( 'wgServer', $server );
+       }
+
+       /**
+        * Helper function to be called from envCheckServer()
+        * @return String
+        */
+       protected abstract function envGetDefaultServer();
+
        /**
         * Environment check for setting $IP and $wgScriptPath.
+        * @return bool
         */
        protected function envCheckPath() {
                global $IP;
                $IP = dirname( dirname( dirname( __FILE__ ) ) );
-
                $this->setVar( 'IP', $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( '{^(.*)/(mw-)?config.*$}', '$1', $path );
-               $this->setVar( 'wgScriptPath', $uri );
+               $this->showMessage( 'config-using-uri', $this->getVar( 'wgServer' ), $this->getVar( 'wgScriptPath' ) );
+               return true;
        }
 
        /**
         * Environment check for setting the preferred PHP file extension.
         */
        protected function envCheckExtension() {
-               // FIXME: detect this properly
+               // @todo FIXME: Detect this properly
                if ( defined( 'MW_INSTALL_PHP5_EXT' ) ) {
                        $ext = 'php5';
                } else {
@@ -866,6 +907,7 @@ abstract class Installer {
 
        /**
         * TODO: document
+        * @return bool
         */
        protected function envCheckShellLocale() {
                $os = php_uname( 's' );
@@ -941,10 +983,10 @@ abstract class Installer {
         * TODO: document
         */
        protected function envCheckUploadsDirectory() {
-               global $IP, $wgServer;
+               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 ) {
@@ -954,6 +996,24 @@ abstract class Installer {
                }
        }
 
+       /**
+        * 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
@@ -963,12 +1023,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);
@@ -1004,7 +1064,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 ) {
@@ -1024,6 +1084,13 @@ abstract class Installer {
                }
        }
 
+       protected function envCheckCtype() {
+               if ( !function_exists( 'ctype_digit' ) ) {
+                       $this->showError( 'config-ctype' );
+                       return false;
+               }
+       }
+
        /**
         * Get an array of likely places we can find executables. Check a bunch
         * of known Unix-like defaults, as well as the PATH environment variable
@@ -1084,6 +1151,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 ) {
@@ -1120,7 +1190,13 @@ abstract class Installer {
                                        break;
                                }
 
-                               $text = Http::get( $url . $file, array( 'timeout' => 3 ) );
+                               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' ) {
@@ -1135,16 +1211,37 @@ 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->getCode() );
+               $this->parserOptions->setUserLang( $lang );
        }
 
        /**
         * Overridden by WebInstaller to provide lastPage parameters.
+        * @param $page string
+        * @return string
         */
        protected function getDocUrl( $page ) {
                return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
@@ -1162,14 +1259,18 @@ abstract class Installer {
                }
 
                $exts = array();
-               $dir = $this->getVar( 'IP' ) . '/extensions';
-               $dh = opendir( $dir );
+               $extDir = $this->getVar( 'IP' ) . '/extensions';
+               $dh = opendir( $extDir );
 
                while ( ( $file = readdir( $dh ) ) !== false ) {
-                       if( file_exists( "$dir/$file/$file.php" ) ) {
+                       if( !is_dir( "$extDir/$file" ) ) {
+                               continue;
+                       }
+                       if( file_exists( "$extDir/$file/$file.php" ) ) {
                                $exts[] = $file;
                        }
                }
+               natcasesort( $exts );
 
                return $exts;
        }
@@ -1183,7 +1284,6 @@ abstract class Installer {
                global $IP;
                $exts = $this->getVar( '_Extensions' );
                $IP = $this->getVar( 'IP' );
-               $path = $IP . '/extensions';
 
                /**
                 * We need to include DefaultSettings before including extensions to avoid
@@ -1194,10 +1294,12 @@ abstract class Installer {
                 * @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( "$path/$e/$e.php" );
+                       require_once( "$IP/extensions/$e/$e.php" );
                }
 
                $hooksWeWant = isset( $wgHooks['LoadExtensionSchemaUpdates'] ) ?
@@ -1399,6 +1501,9 @@ abstract class Installer {
                return $status;
        }
 
+       /**
+        * @param $s Status
+        */
        private function subscribeToMediaWikiAnnounce( Status $s ) {
                $params = array(
                        'email'    => $this->getVar( '_AdminEmail' ),
@@ -1414,27 +1519,33 @@ abstract class Installer {
                        $params['language'] = $myLang;
                }
 
-               $res = Http::post( $this->mediaWikiAnnounceUrl, array( 'postData' => $params ) );
-               if( !$res ) {
-                       $s->warning( 'config-install-subscribe-fail' );
+               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' ) );
+                       $page = WikiPage::factory( Title::newMainPage() );
+                       $page->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() );
@@ -1480,4 +1591,14 @@ abstract class Installer {
        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();
+       }
 }