+# Determine a suitable value for $wgShellLocale
+function getShellLocale( $wikiLang ) {
+ # Give up now if we're in safe mode or open_basedir
+ # It's theoretically possible but tricky to work with
+ if ( wfIniGetBool( "safe_mode" ) || ini_get( 'open_basedir' ) ) {
+ return false;
+ }
+
+ $os = php_uname( 's' );
+ $supported = array( 'Linux', 'SunOS', 'HP-UX' ); # Tested these
+ if ( !in_array( $os, $supported ) ) {
+ return false;
+ }
+
+ # Get a list of available locales
+ $lines = $ret = false;
+ exec( '/usr/bin/locale -a', $lines, $ret );
+ if ( $ret ) {
+ return false;
+ }
+
+ $lines = array_map( 'trim', $lines );
+ $candidatesByLocale = array();
+ $candidatesByLang = array();
+ foreach ( $lines as $line ) {
+ if ( $line === '' ) {
+ continue;
+ }
+ if ( !preg_match( '/^([a-zA-Z]+)(_[a-zA-Z]+|)\.(utf8|UTF-8)(@[a-zA-Z_]*|)$/i', $line, $m ) ) {
+ continue;
+ }
+ list( $all, $lang, $territory, $charset, $modifier ) = $m;
+ $candidatesByLocale[$m[0]] = $m;
+ $candidatesByLang[$lang][] = $m;
+ }
+
+ # Try the current value of LANG
+ if ( isset( $candidatesByLocale[ getenv( 'LANG' ) ] ) ) {
+ return getenv( 'LANG' );
+ }
+
+ # Try the most common ones
+ $commonLocales = array( 'en_US.UTF-8', 'en_US.utf8', 'de_DE.UTF-8', 'de_DE.utf8' );
+ foreach ( $commonLocales as $commonLocale ) {
+ if ( isset( $candidatesByLocale[$commonLocale] ) ) {
+ return $commonLocale;
+ }
+ }
+
+ # Is there an available locale in the Wiki's language?
+ if ( isset( $candidatesByLang[$wikiLang] ) ) {
+ $m = reset( $candidatesByLang[$wikiLang] );
+ return $m[0];
+ }
+
+ # Are there any at all?
+ if ( count( $candidatesByLocale ) ) {
+ $m = reset( $candidatesByLocale );
+ return $m[0];
+ }
+
+ # Give up
+ return false;
+}
+