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(
78 * A list of environment check methods called by doEnvironmentChecks().
79 * These may output warnings using showMessage(), and/or abort the
80 * installation process by returning false.
84 protected $envChecks = array(
86 'envCheckRegisterGlobals',
89 'envCheckMagicQuotes',
90 'envCheckMagicSybase',
102 'envCheckShellLocale',
103 'envCheckUploadsDirectory',
108 * MediaWiki configuration globals that will eventually be passed through
109 * to LocalSettings.php. The names only are given here, the defaults
110 * typically come from DefaultSettings.php.
114 protected $defaultVarNames = array(
126 'wgEmailAuthentication',
129 'wgImageMagickConvertCommand',
134 'wgDeletedDirectory',
139 'wgUseInstantCommons',
145 * Variables that are stored alongside globals, and are used for any
146 * configuration of the installation process aside from the MediaWiki
147 * configuration. Map of names to defaults.
151 protected $internalDefaults = array(
153 '_Environment' => false,
154 '_CompiledDBs' => array(),
155 '_SafeMode' => false,
156 '_RaiseMemory' => false,
157 '_UpgradeDone' => false,
158 '_InstallDone' => false,
159 '_Caches' => array(),
160 '_InstallUser' => 'root',
161 '_InstallPassword' => '',
162 '_SameAccount' => true,
163 '_CreateDBAccount' => false,
164 '_NamespaceType' => 'site-name',
165 '_AdminName' => '', // will be set later, when the user selects language
166 '_AdminPassword' => '',
167 '_AdminPassword2' => '',
169 '_Subscribe' => false,
170 '_SkipOptional' => 'continue',
171 '_RightsProfile' => 'wiki',
172 '_LicenseCode' => 'none',
174 '_Extensions' => array(),
175 '_MemCachedServers' => '',
176 '_UpgradeKeySupplied' => false,
177 '_ExistingDBSettings' => false,
181 * The actual list of installation steps. This will be initialized by getInstallSteps()
185 private $installSteps = array();
188 * Extra steps for installation, for things like DatabaseInstallers to modify
192 protected $extraInstallSteps = array();
195 * Known object cache types and the functions used to test for their existence.
199 protected $objectCaches = array(
200 'xcache' => 'xcache_get',
201 'apc' => 'apc_fetch',
202 'eaccel' => 'eaccelerator_get',
203 'wincache' => 'wincache_ucache_get'
207 * User rights profiles.
211 public $rightsProfiles = array(
214 '*' => array( 'edit' => false )
218 'createaccount' => false,
224 'createaccount' => false,
236 public $licenses = array(
238 'url' => 'http://creativecommons.org/licenses/by-sa/3.0/',
239 'icon' => '{$wgStylePath}/common/images/cc-by-sa.png',
241 'cc-by-nc-sa' => array(
242 'url' => 'http://creativecommons.org/licenses/by-nc-sa/3.0/',
243 'icon' => '{$wgStylePath}/common/images/cc-by-nc-sa.png',
246 'url' => 'http://creativecommons.org/licenses/publicdomain/',
247 'icon' => '{$wgStylePath}/common/images/public-domain.png',
250 'url' => 'http://www.gnu.org/licenses/old-licenses/fdl-1.2.html',
251 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
253 'gfdl-current' => array(
254 'url' => 'http://www.gnu.org/copyleft/fdl.html',
255 'icon' => '{$wgStylePath}/common/images/gnu-fdl.png',
262 'cc-choose' => array(
263 // Details will be filled in by the selector.
271 * URL to mediawiki-announce subscription
273 protected $mediaWikiAnnounceUrl = 'https://lists.wikimedia.org/mailman/subscribe/mediawiki-announce';
276 * Supported language codes for Mailman
278 protected $mediaWikiAnnounceLanguages = array(
279 'ca', 'cs', 'da', 'de', 'en', 'es', 'et', 'eu', 'fi', 'fr', 'hr', 'hu',
280 'it', 'ja', 'ko', 'lt', 'nl', 'no', 'pl', 'pt', 'pt-br', 'ro', 'ru',
281 'sl', 'sr', 'sv', 'tr', 'uk'
285 * UI interface for displaying a short message
286 * The parameters are like parameters to wfMsg().
287 * The messages will be in wikitext format, which will be converted to an
288 * output format such as HTML or text before being sent to the user.
290 public abstract function showMessage( $msg /*, ... */ );
293 * Show a message to the installing user by using a Status object
294 * @param $status Status
296 public abstract function showStatusMessage( Status
$status );
299 * Constructor, always call this from child classes.
301 public function __construct() {
302 global $wgExtensionMessagesFiles, $wgUser, $wgHooks;
304 // Disable the i18n cache and LoadBalancer
305 Language
::getLocalisationCache()->disableBackend();
306 LBFactory
::disableBackend();
308 // Load the installer's i18n file.
309 $wgExtensionMessagesFiles['MediawikiInstaller'] =
310 dirname( __FILE__
) . '/Installer.i18n.php';
312 // Having a user with id = 0 safeguards us from DB access via User::loadOptions().
313 $wgUser = User
::newFromId( 0 );
315 // Set our custom <doclink> hook.
316 $wgHooks['ParserFirstCallInit'][] = array( $this, 'registerDocLink' );
318 $this->settings
= $this->internalDefaults
;
320 foreach ( $this->defaultVarNames
as $var ) {
321 $this->settings
[$var] = $GLOBALS[$var];
324 foreach ( self
::getDBTypes() as $type ) {
325 $installer = $this->getDBInstaller( $type );
327 if ( !$installer->isCompiled() ) {
331 $defaults = $installer->getGlobalDefaults();
333 foreach ( $installer->getGlobalNames() as $var ) {
334 if ( isset( $defaults[$var] ) ) {
335 $this->settings
[$var] = $defaults[$var];
337 $this->settings
[$var] = $GLOBALS[$var];
342 $this->parserTitle
= Title
::newFromText( 'Installer' );
343 $this->parserOptions
= new ParserOptions
; // language will be wrong :(
344 $this->parserOptions
->setEditSection( false );
348 * Get a list of known DB types.
350 public static function getDBTypes() {
351 return self
::$dbTypes;
355 * Do initial checks of the PHP environment. Set variables according to
356 * the observed environment.
358 * It's possible that this may be called under the CLI SAPI, not the SAPI
359 * that the wiki will primarily run under. In that case, the subclass should
360 * initialise variables such as wgScriptPath, before calling this function.
362 * Under the web subclass, it can already be assumed that PHP 5+ is in use
363 * and that sessions are working.
367 public function doEnvironmentChecks() {
368 $this->showMessage( 'config-env-php', phpversion() );
372 foreach ( $this->envChecks
as $check ) {
373 $status = $this->$check();
374 if ( $status === false ) {
379 $this->setVar( '_Environment', $good );
381 return $good ? Status
::newGood() : Status
::newFatal( 'config-env-bad' );
385 * Set a MW configuration variable, or internal installer configuration variable.
387 * @param $name String
388 * @param $value Mixed
390 public function setVar( $name, $value ) {
391 $this->settings
[$name] = $value;
395 * Get an MW configuration variable, or internal installer configuration variable.
396 * The defaults come from $GLOBALS (ultimately DefaultSettings.php).
397 * Installer variables are typically prefixed by an underscore.
399 * @param $name String
400 * @param $default Mixed
404 public function getVar( $name, $default = null ) {
405 if ( !isset( $this->settings
[$name] ) ) {
408 return $this->settings
[$name];
413 * Get an instance of DatabaseInstaller for the specified DB type.
415 * @param $type Mixed: DB installer for which is needed, false to use default.
417 * @return DatabaseInstaller
419 public function getDBInstaller( $type = false ) {
421 $type = $this->getVar( 'wgDBtype' );
424 $type = strtolower( $type );
426 if ( !isset( $this->dbInstallers
[$type] ) ) {
427 $class = ucfirst( $type ). 'Installer';
428 $this->dbInstallers
[$type] = new $class( $this );
431 return $this->dbInstallers
[$type];
435 * Determine if LocalSettings.php exists. If it does, return its variables,
436 * merged with those from AdminSettings.php, as an array.
440 public function getExistingLocalSettings() {
443 wfSuppressWarnings();
444 $_lsExists = file_exists( "$IP/LocalSettings.php" );
452 require( "$IP/includes/DefaultSettings.php" );
453 require( "$IP/LocalSettings.php" );
454 if ( file_exists( "$IP/AdminSettings.php" ) ) {
455 require( "$IP/AdminSettings.php" );
457 return get_defined_vars();
461 * Get a fake password for sending back to the user in HTML.
462 * This is a security mechanism to avoid compromise of the password in the
463 * event of session ID compromise.
465 * @param $realPassword String
469 public function getFakePassword( $realPassword ) {
470 return str_repeat( '*', strlen( $realPassword ) );
474 * Set a variable which stores a password, except if the new value is a
475 * fake password in which case leave it as it is.
477 * @param $name String
478 * @param $value Mixed
480 public function setPassword( $name, $value ) {
481 if ( !preg_match( '/^\*+$/', $value ) ) {
482 $this->setVar( $name, $value );
487 * On POSIX systems return the primary group of the webserver we're running under.
488 * On other systems just returns null.
490 * This is used to advice the user that he should chgrp his config/data/images directory as the
491 * webserver user before he can install.
493 * Public because SqliteInstaller needs it, and doesn't subclass Installer.
497 public static function maybeGetWebserverPrimaryGroup() {
498 if ( !function_exists( 'posix_getegid' ) ||
!function_exists( 'posix_getpwuid' ) ) {
499 # I don't know this, this isn't UNIX.
503 # posix_getegid() *not* getmygid() because we want the group of the webserver,
504 # not whoever owns the current script.
505 $gid = posix_getegid();
506 $getpwuid = posix_getpwuid( $gid );
507 $group = $getpwuid['name'];
513 * Convert wikitext $text to HTML.
515 * This is potentially error prone since many parser features require a complete
516 * installed MW database. The solution is to just not use those features when you
517 * write your messages. This appears to work well enough. Basic formatting and
518 * external links work just fine.
520 * But in case a translator decides to throw in a #ifexist or internal link or
521 * whatever, this function is guarded to catch the attempted DB access and to present
522 * some fallback text.
524 * @param $text String
525 * @param $lineStart Boolean
528 public function parse( $text, $lineStart = false ) {
532 $out = $wgParser->parse( $text, $this->parserTitle
, $this->parserOptions
, $lineStart );
533 $html = $out->getText();
534 } catch ( DBAccessError
$e ) {
535 $html = '<!--DB access attempted during parse--> ' . htmlspecialchars( $text );
537 if ( !empty( $this->debug
) ) {
538 $html .= "<!--\n" . $e->getTraceAsString() . "\n-->";
545 public function getParserOptions() {
546 return $this->parserOptions
;
549 public function disableLinkPopups() {
550 $this->parserOptions
->setExternalLinkTarget( false );
553 public function restoreLinkPopups() {
554 global $wgExternalLinkTarget;
555 $this->parserOptions
->setExternalLinkTarget( $wgExternalLinkTarget );
559 * Install step which adds a row to the site_stats table with appropriate
562 public function populateSiteStats( DatabaseInstaller
$installer ) {
563 $status = $installer->getConnection();
564 if ( !$status->isOK() ) {
567 $status->value
->insert( 'site_stats', array(
569 'ss_total_views' => 0,
570 'ss_total_edits' => 0,
571 'ss_good_articles' => 0,
572 'ss_total_pages' => 0,
575 'ss_images' => 0 ) );
576 return Status
::newGood();
580 * Exports all wg* variables stored by the installer into global scope.
582 public function exportVars() {
583 foreach ( $this->settings
as $name => $value ) {
584 if ( substr( $name, 0, 2 ) == 'wg' ) {
585 $GLOBALS[$name] = $value;
591 * Environment check for DB types.
593 protected function envCheckDB() {
596 $compiledDBs = array();
597 $goodNames = array();
600 foreach ( self
::getDBTypes() as $name ) {
601 $db = $this->getDBInstaller( $name );
602 $readableName = wfMsg( 'config-type-' . $name );
604 if ( $db->isCompiled() ) {
605 $compiledDBs[] = $name;
606 $goodNames[] = $readableName;
609 $allNames[] = $readableName;
612 $this->setVar( '_CompiledDBs', $compiledDBs );
614 if ( !$compiledDBs ) {
615 $this->showMessage( 'config-no-db' );
616 // FIXME: this only works for the web installer!
617 $this->showHelpBox( 'config-no-db-help', $wgLang->commaList( $allNames ) );
621 // Check for FTS3 full-text search module
622 $sqlite = $this->getDBInstaller( 'sqlite' );
623 if ( $sqlite->isCompiled() ) {
624 $db = new DatabaseSqliteStandalone( ':memory:' );
625 if( $db->getFulltextSearchModule() != 'FTS3' ) {
626 $this->showMessage( 'config-no-fts3' );
632 * Environment check for register_globals.
634 protected function envCheckRegisterGlobals() {
635 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
636 $this->showMessage( 'config-register-globals' );
641 * Some versions of libxml+PHP break < and > encoding horribly
643 protected function envCheckBrokenXML() {
644 $test = new PhpXmlBugTester();
646 $this->showMessage( 'config-brokenlibxml' );
652 * Test PHP (probably 5.3.1, but it could regress again) to make sure that
653 * reference parameters to __call() are not converted to null
655 protected function envCheckPHP531() {
656 $test = new PhpRefCallBugTester
;
659 $this->showMessage( 'config-using531' );
665 * Environment check for magic_quotes_runtime.
667 protected function envCheckMagicQuotes() {
668 if( wfIniGetBool( "magic_quotes_runtime" ) ) {
669 $this->showMessage( 'config-magic-quotes-runtime' );
675 * Environment check for magic_quotes_sybase.
677 protected function envCheckMagicSybase() {
678 if ( wfIniGetBool( 'magic_quotes_sybase' ) ) {
679 $this->showMessage( 'config-magic-quotes-sybase' );
685 * Environment check for mbstring.func_overload.
687 protected function envCheckMbstring() {
688 if ( wfIniGetBool( 'mbstring.func_overload' ) ) {
689 $this->showMessage( 'config-mbstring' );
695 * Environment check for zend.ze1_compatibility_mode.
697 protected function envCheckZE1() {
698 if ( wfIniGetBool( 'zend.ze1_compatibility_mode' ) ) {
699 $this->showMessage( 'config-ze1' );
705 * Environment check for safe_mode.
707 protected function envCheckSafeMode() {
708 if ( wfIniGetBool( 'safe_mode' ) ) {
709 $this->setVar( '_SafeMode', true );
710 $this->showMessage( 'config-safe-mode' );
715 * Environment check for the XML module.
717 protected function envCheckXML() {
718 if ( !function_exists( "utf8_encode" ) ) {
719 $this->showMessage( 'config-xml-bad' );
725 * Environment check for the PCRE module.
727 protected function envCheckPCRE() {
728 if ( !function_exists( 'preg_match' ) ) {
729 $this->showMessage( 'config-pcre' );
732 wfSuppressWarnings();
733 $regexd = preg_replace( '/[\x{0400}-\x{04FF}]/u', '', '-АБВГД-' );
735 if ( $regexd != '--' ) {
736 $this->showMessage( 'config-pcre-no-utf8' );
742 * Environment check for available memory.
744 protected function envCheckMemory() {
745 $limit = ini_get( 'memory_limit' );
747 if ( !$limit ||
$limit == -1 ) {
751 $n = intval( $limit );
753 if( preg_match( '/^([0-9]+)[Mm]$/', trim( $limit ), $m ) ) {
754 $n = intval( $m[1] * ( 1024 * 1024 ) );
757 if( $n < $this->minMemorySize
* 1024 * 1024 ) {
758 $newLimit = "{$this->minMemorySize}M";
760 if( ini_set( "memory_limit", $newLimit ) === false ) {
761 $this->showMessage( 'config-memory-bad', $limit );
763 $this->showMessage( 'config-memory-raised', $limit, $newLimit );
764 $this->setVar( '_RaiseMemory', true );
772 * Environment check for compiled object cache types.
774 protected function envCheckCache() {
776 foreach ( $this->objectCaches
as $name => $function ) {
777 if ( function_exists( $function ) ) {
778 $caches[$name] = true;
783 $this->showMessage( 'config-no-cache' );
786 $this->setVar( '_Caches', $caches );
790 * Search for GNU diff3.
792 protected function envCheckDiff3() {
793 $names = array( "gdiff3", "diff3", "diff3.exe" );
794 $versionInfo = array( '$1 --version 2>&1', 'GNU diffutils' );
796 $diff3 = self
::locateExecutableInDefaultPaths( $names, $versionInfo );
799 $this->setVar( 'wgDiff3', $diff3 );
801 $this->setVar( 'wgDiff3', false );
802 $this->showMessage( 'config-diff3-bad' );
807 * Environment check for ImageMagick and GD.
809 protected function envCheckGraphics() {
810 $names = array( wfIsWindows() ?
'convert.exe' : 'convert' );
811 $convert = self
::locateExecutableInDefaultPaths( $names, array( '$1 -version', 'ImageMagick' ) );
814 $this->setVar( 'wgImageMagickConvertCommand', $convert );
815 $this->showMessage( 'config-imagemagick', $convert );
817 } elseif ( function_exists( 'imagejpeg' ) ) {
818 $this->showMessage( 'config-gd' );
821 $this->showMessage( 'no-scaling' );
826 * Environment check for setting $IP and $wgScriptPath.
828 protected function envCheckPath() {
830 $IP = dirname( dirname( dirname( __FILE__
) ) );
832 $this->setVar( 'IP', $IP );
834 // PHP_SELF isn't available sometimes, such as when PHP is CGI but
835 // cgi.fix_pathinfo is disabled. In that case, fall back to SCRIPT_NAME
836 // to get the path to the current script... hopefully it's reliable. SIGH
837 if ( !empty( $_SERVER['PHP_SELF'] ) ) {
838 $path = $_SERVER['PHP_SELF'];
839 } elseif ( !empty( $_SERVER['SCRIPT_NAME'] ) ) {
840 $path = $_SERVER['SCRIPT_NAME'];
841 } elseif ( $this->getVar( 'wgScriptPath' ) ) {
842 // Some kind soul has set it for us already (e.g. debconf)
845 $this->showMessage( 'config-no-uri' );
849 $uri = preg_replace( '{^(.*)/config.*$}', '$1', $path );
850 $this->setVar( 'wgScriptPath', $uri );
854 * Environment check for setting the preferred PHP file extension.
856 protected function envCheckExtension() {
857 // FIXME: detect this properly
858 if ( defined( 'MW_INSTALL_PHP5_EXT' ) ) {
863 $this->setVar( 'wgScriptExtension', ".$ext" );
869 protected function envCheckShellLocale() {
870 $os = php_uname( 's' );
871 $supported = array( 'Linux', 'SunOS', 'HP-UX', 'Darwin' ); # Tested these
873 if ( !in_array( $os, $supported ) ) {
877 # Get a list of available locales.
879 $lines = wfShellExec( '/usr/bin/locale -a', $ret );
885 $lines = wfArrayMap( 'trim', explode( "\n", $lines ) );
886 $candidatesByLocale = array();
887 $candidatesByLang = array();
889 foreach ( $lines as $line ) {
890 if ( $line === '' ) {
894 if ( !preg_match( '/^([a-zA-Z]+)(_[a-zA-Z]+|)\.(utf8|UTF-8)(@[a-zA-Z_]*|)$/i', $line, $m ) ) {
898 list( $all, $lang, $territory, $charset, $modifier ) = $m;
900 $candidatesByLocale[$m[0]] = $m;
901 $candidatesByLang[$lang][] = $m;
904 # Try the current value of LANG.
905 if ( isset( $candidatesByLocale[ getenv( 'LANG' ) ] ) ) {
906 $this->setVar( 'wgShellLocale', getenv( 'LANG' ) );
910 # Try the most common ones.
911 $commonLocales = array( 'en_US.UTF-8', 'en_US.utf8', 'de_DE.UTF-8', 'de_DE.utf8' );
912 foreach ( $commonLocales as $commonLocale ) {
913 if ( isset( $candidatesByLocale[$commonLocale] ) ) {
914 $this->setVar( 'wgShellLocale', $commonLocale );
919 # Is there an available locale in the Wiki's language?
920 $wikiLang = $this->getVar( 'wgLanguageCode' );
922 if ( isset( $candidatesByLang[$wikiLang] ) ) {
923 $m = reset( $candidatesByLang[$wikiLang] );
924 $this->setVar( 'wgShellLocale', $m[0] );
928 # Are there any at all?
929 if ( count( $candidatesByLocale ) ) {
930 $m = reset( $candidatesByLocale );
931 $this->setVar( 'wgShellLocale', $m[0] );
942 protected function envCheckUploadsDirectory() {
943 global $IP, $wgServer;
945 $dir = $IP . '/images/';
946 $url = $wgServer . $this->getVar( 'wgScriptPath' ) . '/images/';
947 $safe = !$this->dirIsExecutable( $dir, $url );
952 $this->showMessage( 'config-uploads-not-safe', $dir );
957 * Convert a hex string representing a Unicode code point to that code point.
961 protected function unicodeChar( $c ) {
965 } else if ($c <= 0x7FF) {
966 return chr(0xC0 |
$c >> 6) . chr(0x80 |
$c & 0x3F);
967 } else if ($c <= 0xFFFF) {
968 return chr(0xE0 |
$c >> 12) . chr(0x80 |
$c >> 6 & 0x3F)
969 . chr(0x80 |
$c & 0x3F);
970 } else if ($c <= 0x10FFFF) {
971 return chr(0xF0 |
$c >> 18) . chr(0x80 |
$c >> 12 & 0x3F)
972 . chr(0x80 |
$c >> 6 & 0x3F)
973 . chr(0x80 |
$c & 0x3F);
981 * Check the libicu version
983 protected function envCheckLibicu() {
984 $utf8 = function_exists( 'utf8_normalize' );
985 $intl = function_exists( 'normalizer_normalize' );
988 * This needs to be updated something that the latest libicu
989 * will properly normalize. This normalization was found at
990 * http://www.unicode.org/versions/Unicode5.2.0/#Character_Additions
991 * Note that we use the hex representation to create the code
992 * points in order to avoid any Unicode-destroying during transit.
994 $not_normal_c = $this->unicodeChar("FA6C");
995 $normal_c = $this->unicodeChar("242EE");
997 $useNormalizer = 'php';
998 $needsUpdate = false;
1001 * We're going to prefer the pecl extension here unless
1002 * utf8_normalize is more up to date.
1005 $useNormalizer = 'utf8';
1006 $utf8 = utf8_normalize( $not_normal_c, UNORM_NFC
);
1007 if ( $utf8 !== $normal_c ) $needsUpdate = true;
1010 $useNormalizer = 'intl';
1011 $intl = normalizer_normalize( $not_normal_c, Normalizer
::FORM_C
);
1012 if ( $intl !== $normal_c ) $needsUpdate = true;
1015 // Uses messages 'config-unicode-using-php', 'config-unicode-using-utf8', 'config-unicode-using-intl'
1016 if( $useNormalizer === 'php' ) {
1017 $this->showMessage( 'config-unicode-pure-php-warning' );
1019 $this->showMessage( 'config-unicode-using-' . $useNormalizer );
1020 if( $needsUpdate ) {
1021 $this->showMessage( 'config-unicode-update-warning' );
1027 * Get an array of likely places we can find executables. Check a bunch
1028 * of known Unix-like defaults, as well as the PATH environment variable
1029 * (which should maybe make it work for Windows?)
1033 protected static function getPossibleBinPaths() {
1035 array( '/usr/bin', '/usr/local/bin', '/opt/csw/bin',
1036 '/usr/gnu/bin', '/usr/sfw/bin', '/sw/bin', '/opt/local/bin' ),
1037 explode( PATH_SEPARATOR
, getenv( 'PATH' ) )
1042 * Search a path for any of the given executable names. Returns the
1043 * executable name if found. Also checks the version string returned
1044 * by each executable.
1046 * Used only by environment checks.
1048 * @param $path String: path to search
1049 * @param $names Array of executable names
1050 * @param $versionInfo Boolean false or array with two members:
1051 * 0 => Command to run for version check, with $1 for the full executable name
1052 * 1 => String to compare the output with
1054 * If $versionInfo is not false, only executables with a version
1055 * matching $versionInfo[1] will be returned.
1057 public static function locateExecutable( $path, $names, $versionInfo = false ) {
1058 if ( !is_array( $names ) ) {
1059 $names = array( $names );
1062 foreach ( $names as $name ) {
1063 $command = $path . DIRECTORY_SEPARATOR
. $name;
1065 wfSuppressWarnings();
1066 $file_exists = file_exists( $command );
1067 wfRestoreWarnings();
1069 if ( $file_exists ) {
1070 if ( !$versionInfo ) {
1074 $file = str_replace( '$1', wfEscapeShellArg( $command ), $versionInfo[0] );
1075 if ( strstr( wfShellExec( $file ), $versionInfo[1] ) !== false ) {
1084 * Same as locateExecutable(), but checks in getPossibleBinPaths() by default
1085 * @see locateExecutable()
1087 public static function locateExecutableInDefaultPaths( $names, $versionInfo = false ) {
1088 foreach( self
::getPossibleBinPaths() as $path ) {
1089 $exe = self
::locateExecutable( $path, $names, $versionInfo );
1090 if( $exe !== false ) {
1098 * Checks if scripts located in the given directory can be executed via the given URL.
1100 * Used only by environment checks.
1102 public function dirIsExecutable( $dir, $url ) {
1103 $scriptTypes = array(
1105 "<?php echo 'ex' . 'ec';",
1106 "#!/var/env php5\n<?php echo 'ex' . 'ec';",
1110 // it would be good to check other popular languages here, but it'll be slow.
1112 wfSuppressWarnings();
1114 foreach ( $scriptTypes as $ext => $contents ) {
1115 foreach ( $contents as $source ) {
1116 $file = 'exectest.' . $ext;
1118 if ( !file_put_contents( $dir . $file, $source ) ) {
1122 $text = Http
::get( $url . $file, array( 'timeout' => 3 ) );
1123 unlink( $dir . $file );
1125 if ( $text == 'exec' ) {
1126 wfRestoreWarnings();
1132 wfRestoreWarnings();
1138 * Register tag hook below.
1140 * @todo Move this to WebInstaller with the two things below?
1142 * @param $parser Parser
1144 public function registerDocLink( Parser
&$parser ) {
1145 $parser->setHook( 'doclink', array( $this, 'docLink' ) );
1150 * ParserOptions are constructed before we determined the language, so fix it
1152 public function setParserLanguage( $lang ) {
1153 $this->parserOptions
->setTargetLanguage( $lang );
1154 $this->parserOptions
->setUserLang( $lang->getCode() );
1158 * Extension tag hook for a documentation link.
1160 public function docLink( $linkText, $attribs, $parser ) {
1161 $url = $this->getDocUrl( $attribs['href'] );
1162 return '<a href="' . htmlspecialchars( $url ) . '">' .
1163 htmlspecialchars( $linkText ) .
1168 * Overridden by WebInstaller to provide lastPage parameters.
1170 protected function getDocUrl( $page ) {
1171 return "{$_SERVER['PHP_SELF']}?page=" . urlencode( $page );
1175 * Finds extensions that follow the format /extensions/Name/Name.php,
1176 * and returns an array containing the value for 'Name' for each found extension.
1180 public function findExtensions() {
1181 if( $this->getVar( 'IP' ) === null ) {
1186 $dir = $this->getVar( 'IP' ) . '/extensions';
1187 $dh = opendir( $dir );
1189 while ( ( $file = readdir( $dh ) ) !== false ) {
1190 if( file_exists( "$dir/$file/$file.php" ) ) {
1199 * Installs the auto-detected extensions.
1203 protected function includeExtensions() {
1204 $exts = $this->getVar( '_Extensions' );
1205 $path = $this->getVar( 'IP' ) . '/extensions';
1207 foreach( $exts as $e ) {
1208 require( "$path/$e/$e.php" );
1211 return Status
::newGood();
1215 * Get an array of install steps. Should always be in the format of
1217 * 'name' => 'someuniquename',
1218 * 'callback' => array( $obj, 'method' ),
1220 * There must be a config-install-$name message defined per step, which will
1221 * be shown on install.
1223 * @param $installer DatabaseInstaller so we can make callbacks
1226 protected function getInstallSteps( DatabaseInstaller
$installer ) {
1227 $coreInstallSteps = array(
1228 array( 'name' => 'database', 'callback' => array( $installer, 'setupDatabase' ) ),
1229 array( 'name' => 'tables', 'callback' => array( $installer, 'createTables' ) ),
1230 array( 'name' => 'interwiki', 'callback' => array( $installer, 'populateInterwikiTable' ) ),
1231 array( 'name' => 'stats', 'callback' => array( $this, 'populateSiteStats' ) ),
1232 array( 'name' => 'secretkey', 'callback' => array( $this, 'generateSecretKey' ) ),
1233 array( 'name' => 'upgradekey', 'callback' => array( $this, 'generateUpgradeKey' ) ),
1234 array( 'name' => 'sysop', 'callback' => array( $this, 'createSysop' ) ),
1235 array( 'name' => 'mainpage', 'callback' => array( $this, 'createMainpage' ) ),
1238 // Build the array of install steps starting from the core install list,
1239 // then adding any callbacks that wanted to attach after a given step
1240 foreach( $coreInstallSteps as $step ) {
1241 $this->installSteps
[] = $step;
1242 if( isset( $this->extraInstallSteps
[ $step['name'] ] ) ) {
1243 $this->installSteps
= array_merge(
1244 $this->installSteps
,
1245 $this->extraInstallSteps
[ $step['name'] ]
1250 // Prepend any steps that want to be at the beginning
1251 if( isset( $this->extraInstallSteps
['BEGINNING'] ) ) {
1252 $this->installSteps
= array_merge(
1253 $this->extraInstallSteps
['BEGINNING'],
1258 // Extensions should always go first, chance to tie into hooks and such
1259 if( count( $this->getVar( '_Extensions' ) ) ) {
1260 array_unshift( $this->installSteps
,
1261 array( 'name' => 'extensions', 'callback' => array( $this, 'includeExtensions' ) )
1264 return $this->installSteps
;
1268 * Actually perform the installation.
1270 * @param $startCB A callback array for the beginning of each step
1271 * @param $endCB A callback array for the end of each step
1273 * @return Array of Status objects
1275 public function performInstallation( $startCB, $endCB ) {
1276 $installResults = array();
1277 $installer = $this->getDBInstaller();
1278 $installer->preInstall();
1279 $installer->setupSchemaVars();
1280 $steps = $this->getInstallSteps( $installer );
1281 foreach( $steps as $stepObj ) {
1282 $name = $stepObj['name'];
1283 call_user_func_array( $startCB, array( $name ) );
1285 // Perform the callback step
1286 $status = call_user_func( $stepObj['callback'], $installer );
1288 // Output and save the results
1289 call_user_func( $endCB, $name, $status );
1290 $installResults[$name] = $status;
1292 // If we've hit some sort of fatal, we need to bail.
1293 // Callback already had a chance to do output above.
1294 if( !$status->isOk() ) {
1298 if( $status->isOk() ) {
1299 $this->setVar( '_InstallDone', true );
1301 return $installResults;
1305 * Generate $wgSecretKey. Will warn if we had to use mt_rand() instead of
1310 protected function generateSecretKey() {
1311 return $this->generateSecret( 'wgSecretKey' );
1315 * Generate a secret value for a variable using either
1316 * /dev/urandom or mt_rand() Produce a warning in the later case.
1320 protected function generateSecret( $secretName, $length = 64 ) {
1321 if ( wfIsWindows() ) {
1324 wfSuppressWarnings();
1325 $file = fopen( "/dev/urandom", "r" );
1326 wfRestoreWarnings();
1329 $status = Status
::newGood();
1332 $secretKey = bin2hex( fread( $file, $length / 2 ) );
1337 for ( $i = 0; $i < $length / 8; $i++
) {
1338 $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
1341 $status->warning( 'config-insecure-secret', '$' . $secretName );
1344 $this->setVar( $secretName, $secretKey );
1350 * Generate a default $wgUpgradeKey. Will warn if we had to use
1351 * mt_rand() instead of /dev/urandom
1355 public function generateUpgradeKey() {
1356 if ( strval( $this->getVar( 'wgUpgradeKey' ) ) === '' ) {
1357 return $this->generateSecret( 'wgUpgradeKey', 16 );
1362 * Create the first user account, grant it sysop and bureaucrat rights
1366 protected function createSysop() {
1367 $name = $this->getVar( '_AdminName' );
1368 $user = User
::newFromName( $name );
1371 // We should've validated this earlier anyway!
1372 return Status
::newFatal( 'config-admin-error-user', $name );
1375 if ( $user->idForName() == 0 ) {
1376 $user->addToDatabase();
1379 $user->setPassword( $this->getVar( '_AdminPassword' ) );
1380 } catch( PasswordError
$pwe ) {
1381 return Status
::newFatal( 'config-admin-error-password', $name, $pwe->getMessage() );
1384 $user->addGroup( 'sysop' );
1385 $user->addGroup( 'bureaucrat' );
1386 if( $this->getVar( '_AdminEmail' ) ) {
1387 $user->setEmail( $this->getVar( '_AdminEmail' ) );
1389 $user->saveSettings();
1391 // Update user count
1392 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
1393 $ssUpdate->doUpdate();
1395 $status = Status
::newGood();
1397 if( $this->getVar( '_Subscribe' ) && $this->getVar( '_AdminEmail' ) ) {
1398 $this->subscribeToMediaWikiAnnounce( $status );
1404 private function subscribeToMediaWikiAnnounce( Status
$s ) {
1406 'email' => $this->getVar( '_AdminEmail' ),
1411 // Mailman doesn't support as many languages as we do, so check to make
1412 // sure their selected language is available
1413 $myLang = $this->getVar( '_UserLang' );
1414 if( in_array( $myLang, $this->mediaWikiAnnounceLanguages
) ) {
1415 $myLang = $myLang == 'pt-br' ?
'pt_BR' : $myLang; // rewrite to Mailman's pt_BR
1416 $params['language'] = $myLang;
1419 $res = Http
::post( $this->mediaWikiAnnounceUrl
, array( 'postData' => $params ) );
1421 $s->warning( 'config-install-subscribe-fail' );
1426 * Insert Main Page with default content.
1430 protected function createMainpage( DatabaseInstaller
$installer ) {
1431 $status = Status
::newGood();
1433 // STUPID STUPID $wgTitle. PST calls getUserSig(), which joyfully
1434 // calls for a parsed message and uses $wgTitle. There isn't even
1435 // a signature in this...
1437 $wgTitle = Title
::newMainPage();
1438 $article = new Article( $wgTitle );
1439 $article->doEdit( wfMsgForContent( 'mainpagetext' ) . "\n\n" .
1440 wfMsgForContent( 'mainpagedocfooter' ),
1444 User
::newFromName( 'MediaWiki default' ) );
1445 } catch (MWException
$e) {
1446 //using raw, because $wgShowExceptionDetails can not be set yet
1447 $status->fatal( 'config-install-mainpage-failed', $e->getMessage() );
1454 * Override the necessary bits of the config to run an installation.
1456 public static function overrideConfig() {
1457 define( 'MW_NO_SESSION', 1 );
1459 // Don't access the database
1460 $GLOBALS['wgUseDatabaseMessages'] = false;
1462 $GLOBALS['wgShowExceptionDetails'] = true;
1463 // Don't break forms
1464 $GLOBALS['wgExternalLinkTarget'] = '_blank';
1466 // Extended debugging
1467 $GLOBALS['wgShowSQLErrors'] = true;
1468 $GLOBALS['wgShowDBErrorBacktrace'] = true;
1470 // Allow multiple ob_flush() calls
1471 $GLOBALS['wgDisableOutputCompression'] = true;
1473 // Use a sensible cookie prefix (not my_wiki)
1474 $GLOBALS['wgCookiePrefix'] = 'mw_installer';
1476 // Some of the environment checks make shell requests, remove limits
1477 $GLOBALS['wgMaxShellMemory'] = 0;
1481 * Add an installation step following the given step.
1483 * @param $callback Array A valid installation callback array, in this form:
1484 * array( 'name' => 'some-unique-name', 'callback' => array( $obj, 'function' ) );
1485 * @param $findStep String the step to find. Omit to put the step at the beginning
1487 public function addInstallStep( $callback, $findStep = 'BEGINNING' ) {
1488 $this->extraInstallSteps
[$findStep][] = $callback;