da408cf668aa73f370e2b591b35140f4e3111d18
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
{
27 * TODO: make protected?
34 * Cached DB installer instances, access using getDBInstaller().
38 protected $dbInstallers = array();
41 * Minimum memory size in MB.
45 protected $minMemorySize = 50;
48 * Cached Title, used by parse().
52 protected $parserTitle;
55 * Cached ParserOptions, used by parse().
59 protected $parserOptions;
62 * Known database types. These correspond to the class names <type>Installer,
63 * and are also MediaWiki database types valid for $wgDBtype.
65 * To add a new type, create a <type>Installer class and a Database<type>
66 * class, and add a config-type-<type> message to MessagesEn.php.
70 protected static $dbTypes = array(
77 * A list of environment check methods called by doEnvironmentChecks().
78 * These may output warnings using showMessage(), and/or abort the
79 * installation process by returning false.
83 protected $envChecks = array(
86 'envCheckRegisterGlobals',
87 'envCheckMagicQuotes',
88 'envCheckMagicSybase',
99 'envCheckWriteableDir',
101 'envCheckShellLocale',
102 'envCheckUploadsDirectory',
107 * UI interface for displaying a short message
108 * The parameters are like parameters to wfMsg().
109 * The messages will be in wikitext format, which will be converted to an
110 * output format such as HTML or text before being sent to the user.
112 public abstract function showMessage( $msg /*, ... */ );
115 * Constructor, always call this from child classes.
117 public function __construct() {
118 // Disable the i18n cache and LoadBalancer
119 Language
::getLocalisationCache()->disableBackend();
120 LBFactory
::disableBackend();
124 * Get a list of known DB types.
126 public static function getDBTypes() {
127 return self
::$dbTypes;
131 * Do initial checks of the PHP environment. Set variables according to
132 * the observed environment.
134 * It's possible that this may be called under the CLI SAPI, not the SAPI
135 * that the wiki will primarily run under. In that case, the subclass should
136 * initialise variables such as wgScriptPath, before calling this function.
138 * Under the web subclass, it can already be assumed that PHP 5+ is in use
139 * and that sessions are working.
143 public function doEnvironmentChecks() {
144 $this->showMessage( 'config-env-php', phpversion() );
148 foreach ( $this->envChecks
as $check ) {
149 $status = $this->$check();
150 if ( $status === false ) {
155 $this->setVar( '_Environment', $good );
158 $this->showMessage( 'config-env-good' );
160 $this->showMessage( 'config-env-bad' );
167 * Set a MW configuration variable, or internal installer configuration variable.
169 * @param $name String
170 * @param $value Mixed
172 public function setVar( $name, $value ) {
173 $this->settings
[$name] = $value;
177 * Get an MW configuration variable, or internal installer configuration variable.
178 * The defaults come from $GLOBALS (ultimately DefaultSettings.php).
179 * Installer variables are typically prefixed by an underscore.
181 * @param $name String
182 * @param $default Mixed
186 public function getVar( $name, $default = null ) {
187 if ( !isset( $this->settings
[$name] ) ) {
190 return $this->settings
[$name];
195 * Get an instance of DatabaseInstaller for the specified DB type.
197 * @param $type Mixed: DB installer for which is needed, false to use default.
199 * @return DatabaseInstaller
201 public function getDBInstaller( $type = false ) {
203 $type = $this->getVar( 'wgDBtype' );
206 $type = strtolower( $type );
208 if ( !isset( $this->dbInstallers
[$type] ) ) {
209 $class = ucfirst( $type ). 'Installer';
210 $this->dbInstallers
[$type] = new $class( $this );
213 return $this->dbInstallers
[$type];
217 * Determine if LocalSettings exists. If it does, return an appropriate
218 * status for whether we should can upgrade or not.
222 public function getLocalSettingsStatus() {
225 $status = Status
::newGood();
227 wfSuppressWarnings();
228 $ls = file_exists( "$IP/LocalSettings.php" );
232 if( $this->getDBInstaller()->needsUpgrade() ) {
233 $status->warning( 'config-localsettings-upgrade' );
236 $status->fatal( 'config-localsettings-noupgrade' );
244 * Get a fake password for sending back to the user in HTML.
245 * This is a security mechanism to avoid compromise of the password in the
246 * event of session ID compromise.
248 * @param $realPassword String
252 public function getFakePassword( $realPassword ) {
253 return str_repeat( '*', strlen( $realPassword ) );
257 * Set a variable which stores a password, except if the new value is a
258 * fake password in which case leave it as it is.
260 * @param $name String
261 * @param $value Mixed
263 public function setPassword( $name, $value ) {
264 if ( !preg_match( '/^\*+$/', $value ) ) {
265 $this->setVar( $name, $value );
270 * On POSIX systems return the primary group of the webserver we're running under.
271 * On other systems just returns null.
273 * This is used to advice the user that he should chgrp his config/data/images directory as the
274 * webserver user before he can install.
276 * Public because SqliteInstaller needs it, and doesn't subclass Installer.
280 public static function maybeGetWebserverPrimaryGroup() {
281 if ( !function_exists( 'posix_getegid' ) ||
!function_exists( 'posix_getpwuid' ) ) {
282 # I don't know this, this isn't UNIX.
286 # posix_getegid() *not* getmygid() because we want the group of the webserver,
287 # not whoever owns the current script.
288 $gid = posix_getegid();
289 $getpwuid = posix_getpwuid( $gid );
290 $group = $getpwuid['name'];
296 * Convert wikitext $text to HTML.
298 * This is potentially error prone since many parser features require a complete
299 * installed MW database. The solution is to just not use those features when you
300 * write your messages. This appears to work well enough. Basic formatting and
301 * external links work just fine.
303 * But in case a translator decides to throw in a #ifexist or internal link or
304 * whatever, this function is guarded to catch attempted DB access and to present
305 * some fallback text.
307 * @param $text String
308 * @param $lineStart Boolean
311 public function parse( $text, $lineStart = false ) {
315 $out = $wgParser->parse( $text, $this->parserTitle
, $this->parserOptions
, $lineStart );
316 $html = $out->getText();
317 } catch ( DBAccessError
$e ) {
318 $html = '<!--DB access attempted during parse--> ' . htmlspecialchars( $text );
320 if ( !empty( $this->debug
) ) {
321 $html .= "<!--\n" . $e->getTraceAsString() . "\n-->";
331 * @param $installer DatabaseInstaller
335 public function installDatabase( DatabaseInstaller
&$installer ) {
337 $type = $this->getVar( 'wgDBtype' );
338 $status = Status
::newFatal( "config-no-db", $type );
340 $status = $installer->setupDatabase();
349 * @param $installer DatabaseInstaller
353 public function installTables( DatabaseInstaller
&$installer ) {
354 $status = $installer->createTables();
356 if( $status->isOK() ) {
357 LBFactory
::enableBackend();
366 * @param $installer DatabaseInstaller
370 public function installInterwiki( DatabaseInstaller
&$installer ) {
371 return $installer->populateInterwikiTable();
375 * Exports all wg* variables stored by the installer into global scope.
377 public function exportVars() {
378 foreach ( $this->settings
as $name => $value ) {
379 if ( substr( $name, 0, 2 ) == 'wg' ) {
380 $GLOBALS[$name] = $value;
386 * Check if we're installing the latest version.
388 public function envLatestVersion() {
391 $repository = wfGetRepository();
392 $currentVersion = $repository->getLatestCoreVersion();
394 $this->setVar( '_ExternalHTTP', true );
396 if ( $currentVersion === false ) {
397 # For when the request is successful but there's e.g. some silly man in
398 # the middle firewall blocking us, e.g. one of those annoying airport ones
399 $this->showMessage( 'config-env-latest-can-not-check', $repository->getLocation() );
403 if( version_compare( $wgVersion, $currentVersion, '<' ) ) {
404 $this->showMessage( 'config-env-latest-old' );
405 // FIXME: this only works for the web installer!
406 $this->showHelpBox( 'config-env-latest-help', $wgVersion, $currentVersion );
407 } elseif( version_compare( $wgVersion, $currentVersion, '>' ) ) {
408 $this->showMessage( 'config-env-latest-new' );
411 $this->showMessage( 'config-env-latest-ok' );
415 * Environment check for DB types.
417 public function envCheckDB() {
420 $compiledDBs = array();
421 $goodNames = array();
424 foreach ( self
::getDBTypes() as $name ) {
425 $db = $this->getDBInstaller( $name );
426 $readableName = wfMsg( 'config-type-' . $name );
428 if ( $db->isCompiled() ) {
429 $compiledDBs[] = $name;
430 $goodNames[] = $readableName;
433 $allNames[] = $readableName;
436 $this->setVar( '_CompiledDBs', $compiledDBs );
438 if ( !$compiledDBs ) {
439 $this->showMessage( 'config-no-db' );
440 // FIXME: this only works for the web installer!
441 $this->showHelpBox( 'config-no-db-help', $wgLang->commaList( $allNames ) );
445 $this->showMessage( 'config-have-db', $wgLang->listToText( $goodNames ), count( $goodNames ) );
447 // Check for FTS3 full-text search module
448 $sqlite = $this->getDBInstaller( 'sqlite' );
449 if ( $sqlite->isCompiled() ) {
450 $db = new DatabaseSqliteStandalone( ':memory:' );
451 $this->showMessage( $db->getFulltextSearchModule() == 'FTS3'
459 * Environment check for register_globals.
461 public function envCheckRegisterGlobals() {
462 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
463 $this->showMessage( 'config-register-globals' );
468 * Environment check for magic_quotes_runtime.
470 public function envCheckMagicQuotes() {
471 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
472 $this->showMessage( 'config-magic-quotes-runtime' );
478 * Environment check for magic_quotes_sybase.
480 public function envCheckMagicSybase() {
481 if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
482 $this->showMessage( 'config-magic-quotes-sybase' );
488 * Environment check for mbstring.func_overload.
490 public function envCheckMbstring() {
491 if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
492 $this->showMessage( 'config-mbstring' );
498 * Environment check for zend.ze1_compatibility_mode.
500 public function envCheckZE1() {
501 if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
502 $this->showMessage( 'config-ze1' );
508 * Environment check for safe_mode.
510 public function envCheckSafeMode() {
511 if ( wfIniGetBool( 'safe_mode' ) ) {
512 $this->setVar( '_SafeMode', true );
513 $this->showMessage( 'config-safe-mode' );
518 * Environment check for the XML module.
520 public function envCheckXML() {
521 if ( !function_exists( "utf8_encode" ) ) {
522 $this->showMessage( 'config-xml-bad' );
525 $this->showMessage( 'config-xml-good' );
529 * Environment check for the PCRE module.
531 public function envCheckPCRE() {
532 if ( !function_exists( 'preg_match' ) ) {
533 $this->showMessage( 'config-pcre' );
539 * Environment check for available memory.
541 public function envCheckMemory() {
542 $limit = ini_get( 'memory_limit' );
544 if ( !$limit ||
$limit == -1 ) {
545 $this->showMessage( 'config-memory-none' );
549 $n = intval( $limit );
551 if( preg_match( '/^([0-9]+)[Mm]$/', trim( $limit ), $m ) ) {
552 $n = intval( $m[1] * ( 1024 * 1024 ) );
555 if( $n < $this->minMemorySize
* 1024 * 1024 ) {
556 $newLimit = "{$this->minMemorySize}M";
558 if( ini_set( "memory_limit", $newLimit ) === false ) {
559 $this->showMessage( 'config-memory-bad', $limit );
561 $this->showMessage( 'config-memory-raised', $limit, $newLimit );
562 $this->setVar( '_RaiseMemory', true );
565 $this->showMessage( 'config-memory-ok', $limit );
570 * Environment check for compiled object cache types.
572 public function envCheckCache() {
575 foreach ( $this->objectCaches
as $name => $function ) {
576 if ( function_exists( $function ) ) {
577 $caches[$name] = true;
578 $this->showMessage( 'config-' . $name );
583 $this->showMessage( 'config-no-cache' );
586 $this->setVar( '_Caches', $caches );
590 * Search for GNU diff3.
592 public function envCheckDiff3() {
593 $names = array( "gdiff3", "diff3", "diff3.exe" );
594 $versionInfo = array( '$1 --version 2>&1', 'diff3 (GNU diffutils)' );
596 $diff3 = $this->locateExecutableInDefaultPaths( $names, $versionInfo );
599 $this->showMessage( 'config-diff3-good', $diff3 );
600 $this->setVar( 'wgDiff3', $diff3 );
602 $this->setVar( 'wgDiff3', false );
603 $this->showMessage( 'config-diff3-bad' );
608 * Environment check for ImageMagick and GD.
610 public function envCheckGraphics() {
611 $names = array( 'convert', 'convert.exe' );
612 $convert = $this->locateExecutableInDefaultPaths( $names );
615 $this->setVar( 'wgImageMagickConvertCommand', $convert );
616 $this->showMessage( 'config-imagemagick', $convert );
618 } elseif ( function_exists( 'imagejpeg' ) ) {
619 $this->showMessage( 'config-gd' );
622 $this->showMessage( 'no-scaling' );
627 * Environment check for setting $IP and $wgScriptPath.
629 public function envCheckPath() {
631 $IP = dirname( dirname( dirname( __FILE__
) ) );
633 $this->setVar( 'IP', $IP );
634 $this->showMessage( 'config-dir', $IP );
636 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
637 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
638 // to get the path to the current script... hopefully it's reliable. SIGH
639 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
640 $path = $_SERVER['PHP_SELF'];
641 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
642 $path = $_SERVER['SCRIPT_NAME'];
643 } elseif ( $this->getVar( 'wgScriptPath' ) ) {
644 // Some kind soul has set it for us already (e.g. debconf)
647 $this->showMessage( 'config-no-uri' );
651 $uri = preg_replace( '{^(.*)/config.*$}', '$1', $path );
652 $this->setVar( 'wgScriptPath', $uri );
653 $this->showMessage( 'config-uri', $uri );
657 * Environment check for writable config/ directory.
659 public function envCheckWriteableDir() {
660 $ipDir = $this->getVar( 'IP' );
661 $configDir = $ipDir . '/config';
663 if( !is_writeable( $configDir ) ) {
664 $webserverGroup = self
::maybeGetWebserverPrimaryGroup();
666 if ( $webserverGroup !== null ) {
667 $this->showMessage( 'config-dir-not-writable-group', $ipDir, $webserverGroup );
669 $this->showMessage( 'config-dir-not-writable-nogroup', $ipDir, $webserverGroup );
677 * Environment check for setting the preferred PHP file extension.
679 public function envCheckExtension() {
680 // FIXME: detect this properly
681 if ( defined( 'MW_INSTALL_PHP5_EXT' ) ) {
687 $this->setVar( 'wgScriptExtension', ".$ext" );
688 $this->showMessage( 'config-file-extension', $ext );
694 public function envCheckShellLocale() {
695 $os = php_uname( 's' );
696 $supported = array( 'Linux', 'SunOS', 'HP-UX', 'Darwin' ); # Tested these
698 if ( !in_array( $os, $supported ) ) {
702 # Get a list of available locales.
703 $lines = $ret = false;
704 $lines = wfShellExec( '/usr/bin/locale -a', $ret );
710 $lines = wfArrayMap( 'trim', explode( "\n", $lines ) );
711 $candidatesByLocale = array();
712 $candidatesByLang = array();
714 foreach ( $lines as $line ) {
715 if ( $line === '' ) {
719 if ( !preg_match( '/^([a-zA-Z]+)(_[a-zA-Z]+|)\.(utf8|UTF-8)(@[a-zA-Z_]*|)$/i', $line, $m ) ) {
723 list( $all, $lang, $territory, $charset, $modifier ) = $m;
725 $candidatesByLocale[$m[0]] = $m;
726 $candidatesByLang[$lang][] = $m;
729 # Try the current value of LANG.
730 if ( isset( $candidatesByLocale[ getenv( 'LANG' ) ] ) ) {
731 $this->setVar( 'wgShellLocale', getenv( 'LANG' ) );
732 $this->showMessage( 'config-shell-locale', getenv( 'LANG' ) );
736 # Try the most common ones.
737 $commonLocales = array( 'en_US.UTF-8', 'en_US.utf8', 'de_DE.UTF-8', 'de_DE.utf8' );
738 foreach ( $commonLocales as $commonLocale ) {
739 if ( isset( $candidatesByLocale[$commonLocale] ) ) {
740 $this->setVar( 'wgShellLocale', $commonLocale );
741 $this->showMessage( 'config-shell-locale', $commonLocale );
746 # Is there an available locale in the Wiki's language?
747 $wikiLang = $this->getVar( 'wgLanguageCode' );
749 if ( isset( $candidatesByLang[$wikiLang] ) ) {
750 $m = reset( $candidatesByLang[$wikiLang] );
751 $this->setVar( 'wgShellLocale', $m[0] );
752 $this->showMessage( 'config-shell-locale', $m[0] );
756 # Are there any at all?
757 if ( count( $candidatesByLocale ) ) {
758 $m = reset( $candidatesByLocale );
759 $this->setVar( 'wgShellLocale', $m[0] );
760 $this->showMessage( 'config-shell-locale', $m[0] );
771 public function envCheckUploadsDirectory() {
772 global $IP, $wgServer;
774 $dir = $IP . '/images/';
775 $url = $wgServer . $this->getVar( 'wgScriptPath' ) . '/images/';
776 $safe = !$this->dirIsExecutable( $dir, $url );
779 $this->showMessage( 'config-uploads-safe' );
781 $this->showMessage( 'config-uploads-not-safe', $dir );
786 * Convert a hex string representing a Unicode code point to that code point.
790 protected function unicodeChar( $c ) {
794 } else if ($c <= 0x7FF) {
795 return chr(0xC0 |
$c >> 6) . chr(0x80 |
$c & 0x3F);
796 } else if ($c <= 0xFFFF) {
797 return chr(0xE0 |
$c >> 12) . chr(0x80 |
$c >> 6 & 0x3F)
798 . chr(0x80 |
$c & 0x3F);
799 } else if ($c <= 0x10FFFF) {
800 return chr(0xF0 |
$c >> 18) . chr(0x80 |
$c >> 12 & 0x3F)
801 . chr(0x80 |
$c >> 6 & 0x3F)
802 . chr(0x80 |
$c & 0x3F);
810 * Check the libicu version
812 public function envCheckLibicu() {
813 $utf8 = function_exists( 'utf8_normalize' );
814 $intl = function_exists( 'normalizer_normalize' );
817 * This needs to be updated something that the latest libicu
818 * will properly normalize. This normalization was found at
819 * http://www.unicode.org/versions/Unicode5.2.0/#Character_Additions
820 * Note that we use the hex representation to create the code
821 * points in order to avoid any Unicode-destroying during transit.
823 $not_normal_c = $this->unicodeChar("FA6C");
824 $normal_c = $this->unicodeChar("242EE");
826 $useNormalizer = 'php';
827 $needsUpdate = false;
830 * We're going to prefer the pecl extension here unless
831 * utf8_normalize is more up to date.
834 $useNormalizer = 'utf8';
835 $utf8 = utf8_normalize( $not_normal_c, UNORM_NFC
);
836 if ( $utf8 !== $normal_c ) $needsUpdate = true;
839 $useNormalizer = 'intl';
840 $intl = normalizer_normalize( $not_normal_c, Normalizer
::FORM_C
);
841 if ( $intl !== $normal_c ) $needsUpdate = true;
844 // Uses messages 'config-unicode-using-php', 'config-unicode-using-utf8', 'config-unicode-using-intl'
845 $this->showMessage( 'config-unicode-using-' . $useNormalizer );
846 if( $useNormalizer === 'php' ) {
847 $this->showMessage( 'config-unicode-pure-php-warning' );
848 } elseif( $needsUpdate ) {
849 $this->showMessage( 'config-unicode-update-warning' );
854 * Get an array of likely places we can find executables. Check a bunch
855 * of known Unix-like defaults, as well as the PATH environment variable
856 * (which should maybe make it work for Windows?)
860 protected function getPossibleBinPaths() {
862 array( '/usr/bin', '/usr/local/bin', '/opt/csw/bin',
863 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ),
864 explode( PATH_SEPARATOR
, getenv( 'PATH' ) )
869 * Search a path for any of the given executable names. Returns the
870 * executable name if found. Also checks the version string returned
871 * by each executable.
873 * Used only by environment checks.
875 * @param $path String: path to search
876 * @param $names Array of executable names
877 * @param $versionInfo Boolean false or array with two members:
878 * 0 => Command to run for version check, with $1 for the path
879 * 1 => String to compare the output with
881 * If $versionInfo is not false, only executables with a version
882 * matching $versionInfo[1] will be returned.
884 protected function locateExecutable( $path, $names, $versionInfo = false ) {
885 if ( !is_array( $names ) ) {
886 $names = array( $names );
889 foreach ( $names as $name ) {
890 $command = "$path/$name";
892 wfSuppressWarnings();
893 $file_exists = file_exists( $command );
896 if ( $file_exists ) {
897 if ( !$versionInfo ) {
901 $file = str_replace( '$1', $command, $versionInfo[0] );
902 if ( strstr( wfShellExec( $file ), $versionInfo[1]) !== false ) {
911 * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
912 * @see locateExecutable()
914 protected function locateExecutableInDefaultPaths( $names, $versionInfo = false ) {
915 foreach( $this->getPossibleBinPaths() as $path ) {
916 $exe = $this->locateExecutable( $path, $names, $versionInfo );
917 if( $exe !== false ) {
925 * Checks if scripts located in the given directory can be executed via the given URL.
927 * Used only by environment checks.
929 public function dirIsExecutable( $dir, $url ) {
930 $scriptTypes = array(
932 "<?php echo 'ex' . 'ec';",
933 "#!/var/env php5\n<?php echo 'ex' . 'ec';",
937 // it would be good to check other popular languages here, but it'll be slow.
939 wfSuppressWarnings();
941 foreach ( $scriptTypes as $ext => $contents ) {
942 foreach ( $contents as $source ) {
943 $file = 'exectest.' . $ext;
945 if ( !file_put_contents( $dir . $file, $source ) ) {
949 $text = Http
::get( $url . $file );
950 unlink( $dir . $file );
952 if ( $text == 'exec' ) {