Merge "Revert "Adding sanity check to Title::isRedirect().""
[lhc/web/wiklou.git] / includes / installer / Installer.php
index 7b68283..12a84a1 100644 (file)
@@ -2,6 +2,21 @@
 /**
  * Base code for MediaWiki installer.
  *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup Deployment
  */
@@ -24,7 +39,7 @@
 abstract class Installer {
 
        // This is the absolute minimum PHP version we can support
-       const MINIMUM_PHP_VERSION = '5.2.3';
+       const MINIMUM_PHP_VERSION = '5.3.2';
 
        /**
         * @var array
@@ -107,6 +122,7 @@ abstract class Installer {
                'envCheckUploadsDirectory',
                'envCheckLibicu',
                'envCheckSuhosinMaxValueLength',
+               'envCheckCtype',
        );
 
        /**
@@ -205,7 +221,6 @@ abstract class Installer {
        protected $objectCaches = array(
                'xcache' => 'xcache_get',
                'apc' => 'apc_fetch',
-               'eaccel' => 'eaccelerator_get',
                'wincache' => 'wincache_ucache_get'
        );
 
@@ -296,11 +311,13 @@ 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 /*, ... */ );
 
@@ -602,7 +619,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();
@@ -621,6 +637,7 @@ abstract class Installer {
 
        /**
         * Environment check for DB types.
+        * @return bool
         */
        protected function envCheckDB() {
                global $wgLang;
@@ -631,19 +648,33 @@ abstract class Installer {
                        $allNames[] = wfMsg( "config-type-$name" );
                }
 
-               if ( !$this->getVar( '_CompiledDBs' ) ) {
-                       $this->showError( 'config-no-db', $wgLang->commaList( $allNames ) );
-                       // @todo FIXME: This only works for the web installer!
-                       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() ) {
-                       if( DatabaseSqlite::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 );
+               return true;
        }
 
        /**
@@ -657,6 +688,7 @@ abstract class Installer {
 
        /**
         * Some versions of libxml+PHP break < and > encoding horribly
+        * @return bool
         */
        protected function envCheckBrokenXML() {
                $test = new PhpXmlBugTester();
@@ -664,11 +696,13 @@ abstract class Installer {
                        $this->showError( 'config-brokenlibxml' );
                        return false;
                }
+               return true;
        }
 
        /**
         * Test PHP (probably 5.3.1, but it could regress again) to make sure that
         * reference parameters to __call() are not converted to null
+        * @return bool
         */
        protected function envCheckPHP531() {
                $test = new PhpRefCallBugTester;
@@ -677,70 +711,84 @@ abstract class Installer {
                        $this->showError( 'config-using531', phpversion() );
                        return false;
                }
+               return true;
        }
 
        /**
         * Environment check for magic_quotes_runtime.
+        * @return bool
         */
        protected function envCheckMagicQuotes() {
                if( wfIniGetBool( "magic_quotes_runtime" ) ) {
                        $this->showError( 'config-magic-quotes-runtime' );
                        return false;
                }
+               return true;
        }
 
        /**
         * Environment check for magic_quotes_sybase.
+        * @return bool
         */
        protected function envCheckMagicSybase() {
                if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
                        $this->showError( 'config-magic-quotes-sybase' );
                        return false;
                }
+               return true;
        }
 
        /**
         * Environment check for mbstring.func_overload.
+        * @return bool
         */
        protected function envCheckMbstring() {
                if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
                        $this->showError( 'config-mbstring' );
                        return false;
                }
+               return true;
        }
 
        /**
         * Environment check for zend.ze1_compatibility_mode.
+        * @return bool
         */
        protected function envCheckZE1() {
                if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
                        $this->showError( 'config-ze1' );
                        return false;
                }
+               return true;
        }
 
        /**
         * Environment check for safe_mode.
+        * @return bool
         */
        protected function envCheckSafeMode() {
                if ( wfIniGetBool( 'safe_mode' ) ) {
                        $this->setVar( '_SafeMode', true );
                        $this->showMessage( 'config-safe-mode' );
                }
+               return true;
        }
 
        /**
         * Environment check for the XML module.
+        * @return bool
         */
        protected function envCheckXML() {
                if ( !function_exists( "utf8_encode" ) ) {
                        $this->showError( 'config-xml-bad' );
                        return false;
                }
+               return true;
        }
 
        /**
         * Environment check for the PCRE module.
+        * @return bool
         */
        protected function envCheckPCRE() {
                if ( !function_exists( 'preg_match' ) ) {
@@ -754,10 +802,12 @@ abstract class Installer {
                        $this->showError( 'config-pcre-no-utf8' );
                        return false;
                }
+               return true;
        }
 
        /**
         * Environment check for available memory.
+        * @return bool
         */
        protected function envCheckMemory() {
                $limit = ini_get( 'memory_limit' );
@@ -777,9 +827,8 @@ abstract class Installer {
                                $this->showMessage( 'config-memory-raised', $limit, $newLimit );
                                $this->setVar( '_RaiseMemory', true );
                        }
-               } else {
-                       return true;
                }
+               return true;
        }
 
        /**
@@ -805,15 +854,18 @@ abstract class Installer {
 
        /**
         * Scare user to death if they have mod_security
+        * @return bool
         */
        protected function envCheckModSecurity() {
                if ( self::apacheModulePresent( 'mod_security' ) ) {
                        $this->showMessage( 'config-mod-security' );
                }
+               return true;
        }
 
        /**
         * Search for GNU diff3.
+        * @return bool
         */
        protected function envCheckDiff3() {
                $names = array( "gdiff3", "diff3", "diff3.exe" );
@@ -827,10 +879,12 @@ abstract class Installer {
                        $this->setVar( 'wgDiff3', false );
                        $this->showMessage( 'config-diff3-bad' );
                }
+               return true;
        }
 
        /**
         * Environment check for ImageMagick and GD.
+        * @return bool
         */
        protected function envCheckGraphics() {
                $names = array( wfIsWindows() ? 'convert.exe' : 'convert' );
@@ -843,23 +897,32 @@ abstract class Installer {
                        return true;
                } elseif ( function_exists( 'imagejpeg' ) ) {
                        $this->showMessage( 'config-gd' );
-                       return true;
+
                } else {
                        $this->showMessage( 'config-no-scaling' );
                }
+               return true;
        }
 
        /**
         * Environment check for the server hostname.
         */
        protected function envCheckServer() {
-               $server = WebRequest::detectServer();
+               $server = $this->envGetDefaultServer();
                $this->showMessage( 'config-using-server', $server );
                $this->setVar( 'wgServer', $server );
+               return true;
        }
 
+       /**
+        * 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;
@@ -881,10 +944,12 @@ abstract class Installer {
                        $ext = 'php';
                }
                $this->setVar( 'wgScriptExtension', ".$ext" );
+               return true;
        }
 
        /**
         * TODO: document
+        * @return bool
         */
        protected function envCheckShellLocale() {
                $os = php_uname( 's' );
@@ -958,6 +1023,7 @@ abstract class Installer {
 
        /**
         * TODO: document
+        * @return bool
         */
        protected function envCheckUploadsDirectory() {
                global $IP;
@@ -966,17 +1032,17 @@ abstract class Installer {
                $url = $this->getVar( 'wgServer' ) . $this->getVar( 'wgScriptPath' ) . '/images/';
                $safe = !$this->dirIsExecutable( $dir, $url );
 
-               if ( $safe ) {
-                       return true;
-               } else {
+               if ( !$safe ) {
                        $this->showMessage( 'config-uploads-not-safe', $dir );
                }
+               return true;
        }
 
        /**
         * Checks if suhosin.get.max_value_length is set, and if so, sets
         * $wgResourceLoaderMaxQueryLength to that value in the generated
         * LocalSettings file
+        * @return bool
         */
        protected function envCheckSuhosinMaxValueLength() {
                $maxValueLength = ini_get( 'suhosin.get.max_value_length' );
@@ -989,6 +1055,7 @@ abstract class Installer {
                        $maxValueLength = -1;
                }
                $this->setVar( 'wgResourceLoaderMaxQueryLength', $maxValueLength );
+               return true;
        }
 
        /**
@@ -1042,12 +1109,16 @@ abstract class Installer {
                if( $utf8 ) {
                        $useNormalizer = 'utf8';
                        $utf8 = utf8_normalize( $not_normal_c, UtfNormal::UNORM_NFC );
-                       if ( $utf8 !== $normal_c ) $needsUpdate = true;
+                       if ( $utf8 !== $normal_c ) {
+                               $needsUpdate = true;
+                       }
                }
                if( $intl ) {
                        $useNormalizer = 'intl';
                        $intl = normalizer_normalize( $not_normal_c, Normalizer::FORM_C );
-                       if ( $intl !== $normal_c ) $needsUpdate = true;
+                       if ( $intl !== $normal_c ) {
+                               $needsUpdate = true;
+                       }
                }
 
                // Uses messages 'config-unicode-using-php', 'config-unicode-using-utf8', 'config-unicode-using-intl'
@@ -1061,6 +1132,17 @@ abstract class Installer {
                }
        }
 
+       /**
+        * @return bool
+        */
+       protected function envCheckCtype() {
+               if ( !function_exists( 'ctype_digit' ) ) {
+                       $this->showError( 'config-ctype' );
+                       return false;
+               }
+               return true;
+       }
+
        /**
         * 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
@@ -1091,6 +1173,7 @@ abstract class Installer {
         *
         * If $versionInfo is not false, only executables with a version
         * matching $versionInfo[1] will be returned.
+        * @return bool|string
         */
        public static function locateExecutable( $path, $names, $versionInfo = false ) {
                if ( !is_array( $names ) ) {
@@ -1139,6 +1222,9 @@ abstract class Installer {
         * Checks if scripts located in the given directory can be executed via the given URL.
         *
         * Used only by environment checks.
+        * @param $dir string
+        * @param $url string
+        * @return bool|int|string
         */
        public function dirIsExecutable( $dir, $url ) {
                $scriptTypes = array(
@@ -1210,7 +1296,7 @@ abstract class Installer {
 
        /**
         * Overridden by WebInstaller to provide lastPage parameters.
-        * @param $page stirng
+        * @param $page string
         * @return string
         */
        protected function getDocUrl( $page ) {
@@ -1240,6 +1326,7 @@ abstract class Installer {
                                $exts[] = $file;
                        }
                }
+               natcasesort( $exts );
 
                return $exts;
        }
@@ -1374,8 +1461,7 @@ abstract class Installer {
        }
 
        /**
-        * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of
-        * /dev/urandom
+        * Generate $wgSecretKey. Will warn if we had to use an insecure random source.
         *
         * @return Status
         */
@@ -1388,8 +1474,8 @@ abstract class Installer {
        }
 
        /**
-        * Generate a secret value for variables using either
-        * /dev/urandom or mt_rand(). Produce a warning in the later case.
+        * Generate a secret value for variables using our CryptRand generator.
+        * Produce a warning if the random source was insecure.
         *
         * @param $keys Array
         * @return Status
@@ -1397,28 +1483,18 @@ abstract class Installer {
        protected function doGenerateKeys( $keys ) {
                $status = Status::newGood();
 
-               wfSuppressWarnings();
-               $file = fopen( "/dev/urandom", "r" );
-               wfRestoreWarnings();
-
+               $strong = true;
                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 ) );
-                               }
+                       $secretKey = MWCryptRand::generateHex( $length, true );
+                       if ( !MWCryptRand::wasStrong() ) {
+                               $strong = false;
                        }
 
                        $this->setVar( $name, $secretKey );
                }
 
-               if ( $file ) {
-                       fclose( $file );
-               } else {
-                       $names = array_keys ( $keys );
+               if ( !$strong ) {
+                       $names = array_keys( $keys );
                        $names = preg_replace( '/^(.*)$/', '\$$1', $names );
                        global $wgLang;
                        $status->warning( 'config-insecure-keys', $wgLang->listToText( $names ), count( $names ) );
@@ -1470,6 +1546,9 @@ abstract class Installer {
                return $status;
        }
 
+       /**
+        * @param $s Status
+        */
        private function subscribeToMediaWikiAnnounce( Status $s ) {
                $params = array(
                        'email'    => $this->getVar( '_AdminEmail' ),
@@ -1505,13 +1584,13 @@ abstract class Installer {
        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() );
@@ -1528,6 +1607,8 @@ abstract class Installer {
 
                // Don't access the database
                $GLOBALS['wgUseDatabaseMessages'] = false;
+               // Don't cache langconv tables
+               $GLOBALS['wgLanguageConverterCacheType'] = CACHE_NONE;
                // Debug-friendly
                $GLOBALS['wgShowExceptionDetails'] = true;
                // Don't break forms