Пример #1
0
 /**
  * TODO: document
  * @return bool
  */
 protected function envCheckShellLocale()
 {
     $os = php_uname('s');
     $supported = array('Linux', 'SunOS', 'HP-UX', 'Darwin');
     # Tested these
     if (!in_array($os, $supported)) {
         return true;
     }
     # Get a list of available locales.
     $ret = false;
     $lines = wfShellExec('/usr/bin/locale -a', $ret);
     if ($ret) {
         return true;
     }
     $lines = wfArrayMap('trim', explode("\n", $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')])) {
         $this->setVar('wgShellLocale', getenv('LANG'));
         return true;
     }
     # 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])) {
             $this->setVar('wgShellLocale', $commonLocale);
             return true;
         }
     }
     # Is there an available locale in the Wiki's language?
     $wikiLang = $this->getVar('wgLanguageCode');
     if (isset($candidatesByLang[$wikiLang])) {
         $m = reset($candidatesByLang[$wikiLang]);
         $this->setVar('wgShellLocale', $m[0]);
         return true;
     }
     # Are there any at all?
     if (count($candidatesByLocale)) {
         $m = reset($candidatesByLocale);
         $this->setVar('wgShellLocale', $m[0]);
         return true;
     }
     # Give up.
     return true;
 }
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') || !function_exists('exec')) {
        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;
    $lines = wfShellExec('/usr/bin/locale -a', $ret, true);
    if ($ret) {
        return false;
    }
    $lines = wfArrayMap('trim', explode("\n", $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;
}