/** * Load country names localized for a particular language. * * @param string $code The language to return the list in * @return an associative array of country codes and localized country names */ private static function loadLanguage($code) { if (!isset(self::$cache[$code])) { wfProfileIn(__METHOD__ . '-recache'); /* Load override for wrong or missing entries in cldr */ $override = dirname(__FILE__) . '/LocalNames/' . self::getOverrideFileName($code); if (Language::isValidBuiltInCode($code) && file_exists($override)) { $countryNames = false; require $override; if (is_array($countryNames)) { self::$cache[$code] = $countryNames; } } $filename = dirname(__FILE__) . '/CldrNames/' . self::getFileName($code); if (Language::isValidBuiltInCode($code) && file_exists($filename)) { $countryNames = false; require $filename; if (is_array($countryNames)) { if (isset(self::$cache[$code])) { // Add to existing list of localized country names self::$cache[$code] = self::$cache[$code] + $countryNames; } else { // No list exists, so create it self::$cache[$code] = $countryNames; } } } else { wfDebug(__METHOD__ . ": Unable to load country names for {$filename}\n"); } wfProfileOut(__METHOD__ . '-recache'); } return isset(self::$cache[$code]) ? self::$cache[$code] : array(); }
/** * Load time units localized for a particular language. Helper function for getUnits. * * @param string $code The language to return the list in * @return array an associative array of time unit codes and localized time units */ private static function loadLanguage($code) { if (!isset(self::$cache[$code])) { /* Load override for wrong or missing entries in cldr */ $override = __DIR__ . '/LocalNames/' . self::getOverrideFileName($code); if (Language::isValidBuiltInCode($code) && file_exists($override)) { $timeUnits = false; require $override; if (is_array($timeUnits)) { self::$cache[$code] = $timeUnits; } } $filename = __DIR__ . '/CldrNames/' . self::getFileName($code); if (Language::isValidBuiltInCode($code) && file_exists($filename)) { $timeUnits = false; require $filename; if (is_array($timeUnits)) { if (isset(self::$cache[$code])) { // Add to existing list of localized time units self::$cache[$code] = self::$cache[$code] + $timeUnits; } else { // No list exists, so create it self::$cache[$code] = $timeUnits; } } } else { wfDebug(__METHOD__ . ": Unable to load time units for {$filename}\n"); } if (!isset(self::$cache[$code])) { self::$cache[$code] = array(); } } return self::$cache[$code]; }
public function execute() { $commonContent = "{}"; $configuration = null; $this->getMain()->setCacheMode('public'); $this->getMain()->setCacheMaxAge(2419200); $params = $this->extractRequestParams(); $source = $params['from']; $target = $params['to']; if (!Language::isValidBuiltInCode($source) || !Language::isValidBuiltInCode($target)) { $this->dieUsage('Invalid language', 'invalidlanguage'); } // Read common configuraiton $commonFileName = __DIR__ . "/../modules/source/conf/common.json"; if (is_readable($commonFileName)) { $commonContent = file_get_contents($commonFileName); } $commonConfiguration = json_decode($commonContent, false); // Read configuraiton for language pair $filename = __DIR__ . "/../modules/source/conf/{$source}-{$target}.json"; if (is_readable($filename)) { $contents = file_get_contents($filename); $configuration = json_decode($contents, false); } if (!$configuration) { // No language specific configuration. $configuration = $commonConfiguration; } else { // For now, we use only templates in configuration. // If we have more keys in configuration, this must be // a separate method to merge configurations $configuration->templates = (object) array_merge((array) $commonConfiguration->templates, (array) $configuration->templates); } $this->getResult()->addValue(null, 'configuration', $configuration); }
public function execute() { $result = $this->getResult(); $params = $this->extractRequestParams(); $source = $target = null; if (isset($params['source']) && Language::isValidBuiltInCode($params['source'])) { $source = $params['source']; } if (isset($params['target']) && Language::isValidBuiltInCode($params['target'])) { $target = $params['target']; } $interval = $params['interval']; $result->addValue(array('query'), 'contenttranslationlangtrend', ContentTranslation\Translation::getTrend($source, $target, $interval)); }
public function execute() { $result = $this->getResult(); $params = $this->extractRequestParams(); $source = $target = null; if (isset($params['source']) && Language::isValidBuiltInCode($params['source'])) { $source = $params['source']; } if (isset($params['target']) && Language::isValidBuiltInCode($params['target'])) { $target = $params['target']; } $interval = $params['interval']; $data = array('translations' => Translation::getTrendByStatus($source, $target, 'published', $interval), 'drafts' => Translation::getTrendByStatus($source, $target, 'draft', $interval)); if ($target !== null) { // We can give deletion rates for only local wiki. We cannot give // deletion stats for all wikis. $data['deletions'] = Translation::getDeletionTrend($interval); } $out = $this->addMissingDates($data, $interval); $result->addValue(array('query'), 'contenttranslationlangtrend', $out); }
public function execute() { $from = $to = null; $params = $this->extractRequestParams(); $result = $this->getResult(); $user = $this->getUser(); if (isset($params['from'])) { $from = $params['from']; } if (isset($params['to'])) { $to = $params['to']; } $limit = $params['limit']; $offset = $params['offset']; if ($from !== null && !Language::isValidBuiltInCode($from)) { $this->dieUsage('Invalid language', 'invalidlanguage'); } if ($to !== null && !Language::isValidBuiltInCode($to)) { $this->dieUsage('Invalid language', 'invalidlanguage'); } $translations = ContentTranslation\Translation::getAllPublishedTranslations($from, $to, $limit, $offset); $resultSize = count($translations); $result->addValue(array('result'), 'translations', $translations); }
public static function time($parser, $format = '', $date = '', $language = '', $local = false) { global $wgLang, $wgContLang, $wgLocaltimezone; self::registerClearHook(); if (isset(self::$mTimeCache[$format][$date][$language][$local])) { return self::$mTimeCache[$format][$date][$language][$local]; } # compute the timestamp string $ts # PHP >= 5.2 can handle dates before 1970 or after 2038 using the DateTime object # PHP < 5.2 is limited to dates between 1970 and 2038 $invalidTime = false; if (class_exists('DateTime')) { # PHP >= 5.2 # the DateTime constructor must be used because it throws exceptions # when errors occur, whereas date_create appears to just output a warning # that can't really be detected from within the code try { # Determine timezone if ($local) { # convert to MediaWiki local timezone if set if (isset($wgLocaltimezone)) { $tz = new DateTimeZone($wgLocaltimezone); } else { $tz = new DateTimeZone(date_default_timezone_get()); } } else { # if local time was not requested, convert to UTC $tz = new DateTimeZone('UTC'); } # Correct for DateTime interpreting 'XXXX' as XX:XX o'clock if (preg_match('/^[0-9]{4}$/', $date)) { $date = '00:00 ' . $date; } # Parse date if ($date !== '') { $dateObject = new DateTime($date, $tz); } else { # use current date and time $dateObject = new DateTime('now', $tz); } # Generate timestamp $ts = $dateObject->format('YmdHis'); } catch (Exception $ex) { $invalidTime = true; } } else { # PHP < 5.2 if ($date !== '') { $unix = @strtotime($date); } else { $unix = time(); } if ($unix == -1 || $unix == false) { $invalidTime = true; } else { if ($local) { # Use the time zone if (isset($wgLocaltimezone)) { $oldtz = getenv('TZ'); putenv('TZ=' . $wgLocaltimezone); } wfSuppressWarnings(); // E_STRICT system time bitching $ts = date('YmdHis', $unix); wfRestoreWarnings(); if (isset($wgLocaltimezone)) { putenv('TZ=' . $oldtz); } } else { $ts = wfTimestamp(TS_MW, $unix); } } } # format the timestamp and return the result if ($invalidTime) { $result = '<strong class="error">' . wfMsgForContent('pfunc_time_error') . '</strong>'; } else { self::$mTimeChars += strlen($format); if (self::$mTimeChars > self::$mMaxTimeChars) { return '<strong class="error">' . wfMsgForContent('pfunc_time_too_long') . '</strong>'; } else { if ($ts < 100000000000000) { // Language can't deal with years after 9999 if ($language !== '' && Language::isValidBuiltInCode($language)) { // use whatever language is passed as a parameter $langObject = Language::factory($language); $result = $langObject->sprintfDate($format, $ts); } else { // use wiki's content language $result = $parser->getFunctionLang()->sprintfDate($format, $ts); } } else { return '<strong class="error">' . wfMsgForContent('pfunc_time_too_big') . '</strong>'; } } } self::$mTimeCache[$format][$date][$language][$local] = $result; return $result; }
/** * @param string $name Parameter name * @param mixed $value Parameter value * @return bool Validity */ public function validateParam($name, $value) { if (in_array($name, ['width', 'height'])) { // Reject negative heights, widths return $value > 0; } elseif ($name == 'lang') { // Validate $code if ($value === '' || !Language::isValidBuiltInCode($value)) { wfDebug("Invalid user language code\n"); return false; } return true; } // Only lang, width and height are acceptable keys return false; }
/** * @param $parser Parser * @param $frame PPFrame * @param $format string * @param $date string * @param $language string * @param $local string|bool * @return string */ public static function timeCommon($parser, $frame = null, $format = '', $date = '', $language = '', $local = false) { global $wgLocaltimezone; self::registerClearHook(); if ($date === '') { $cacheKey = $parser->getOptions()->getTimestamp(); $timestamp = new MWTimestamp($cacheKey); $date = $timestamp->getTimestamp(TS_ISO_8601); $useTTL = true; } else { $cacheKey = $date; $useTTL = false; } if (isset(self::$mTimeCache[$format][$cacheKey][$language][$local])) { $cachedVal = self::$mTimeCache[$format][$cacheKey][$language][$local]; if ($useTTL && $cachedVal[1] !== null && $frame && is_callable(array($frame, 'setTTL'))) { $frame->setTTL($cachedVal[1]); } return $cachedVal[0]; } # compute the timestamp string $ts # PHP >= 5.2 can handle dates before 1970 or after 2038 using the DateTime object $invalidTime = false; # the DateTime constructor must be used because it throws exceptions # when errors occur, whereas date_create appears to just output a warning # that can't really be detected from within the code try { # Default input timezone is UTC. $utc = new DateTimeZone('UTC'); # Correct for DateTime interpreting 'XXXX' as XX:XX o'clock if (preg_match('/^[0-9]{4}$/', $date)) { $date = '00:00 ' . $date; } # Parse date # UTC is a default input timezone. $dateObject = new DateTime($date, $utc); # Set output timezone. if ($local) { if (isset($wgLocaltimezone)) { $tz = new DateTimeZone($wgLocaltimezone); } else { $tz = new DateTimeZone(date_default_timezone_get()); } } else { $tz = $utc; } $dateObject->setTimezone($tz); # Generate timestamp $ts = $dateObject->format('YmdHis'); } catch (Exception $ex) { $invalidTime = true; } $ttl = null; # format the timestamp and return the result if ($invalidTime) { $result = '<strong class="error">' . wfMessage('pfunc_time_error')->inContentLanguage()->escaped() . '</strong>'; } else { self::$mTimeChars += strlen($format); if (self::$mTimeChars > self::$mMaxTimeChars) { return '<strong class="error">' . wfMessage('pfunc_time_too_long')->inContentLanguage()->escaped() . '</strong>'; } else { if ($ts < 0) { // Language can't deal with BC years return '<strong class="error">' . wfMessage('pfunc_time_too_small')->inContentLanguage()->escaped() . '</strong>'; } elseif ($ts < 100000000000000) { // Language can't deal with years after 9999 if ($language !== '' && Language::isValidBuiltInCode($language)) { // use whatever language is passed as a parameter $langObject = Language::factory($language); } else { // use wiki's content language $langObject = $parser->getFunctionLang(); StubObject::unstub($langObject); // $ttl is passed by reference, which doesn't work right on stub objects } $result = $langObject->sprintfDate($format, $ts, $tz, $ttl); } else { return '<strong class="error">' . wfMessage('pfunc_time_too_big')->inContentLanguage()->escaped() . '</strong>'; } } } self::$mTimeCache[$format][$cacheKey][$language][$local] = array($result, $ttl); if ($useTTL && $ttl !== null && $frame && is_callable(array($frame, 'setTTL'))) { $frame->setTTL($ttl); } return $result; }
/** * @covers Language::isValidBuiltInCode */ public function testBuiltInCodeValidationRejectUnderscore() { $this->assertFalse((bool) Language::isValidBuiltInCode('be_tarask'), "reject underscore in language code"); }
static function isValidBuiltInLanguageCode($code) { return Language::isValidBuiltInCode($code); }
/** * Initialise a language in this object. Rebuild the cache if necessary. * @param $code * @throws MWException */ protected function initLanguage($code) { if (isset($this->initialisedLangs[$code])) { return; } $this->initialisedLangs[$code] = true; # If the code is of the wrong form for a Messages*.php file, do a shallow fallback if (!Language::isValidBuiltInCode($code)) { $this->initShallowFallback($code, 'en'); return; } # Recache the data if necessary if (!$this->manualRecache && $this->isExpired($code)) { if (Language::isSupportedLanguage($code)) { $this->recache($code); } elseif ($code === 'en') { throw new MWException('MessagesEn.php is missing.'); } else { $this->initShallowFallback($code, 'en'); } return; } # Preload some stuff $preload = $this->getItem($code, 'preload'); if ($preload === null) { if ($this->manualRecache) { // No Messages*.php file. Do shallow fallback to en. if ($code === 'en') { throw new MWException('No localisation cache found for English. ' . 'Please run maintenance/rebuildLocalisationCache.php.'); } $this->initShallowFallback($code, 'en'); return; } else { throw new MWException('Invalid or missing localisation cache.'); } } $this->data[$code] = $preload; foreach ($preload as $key => $item) { if (in_array($key, self::$splitKeys)) { foreach ($item as $subkey => $subitem) { $this->loadedSubitems[$code][$key][$subkey] = true; } } else { $this->loadedItems[$code][$key] = true; } } }
/** * Get the ordered list of fallback languages. * * @since 1.19 * @param string $code Language code * @return array Non-empty array, ending in "en" */ public static function getFallbacksFor($code) { if ($code === 'en' || !Language::isValidBuiltInCode($code)) { return array(); } // For unknown languages, fallbackSequence returns an empty array, // hardcode fallback to 'en' in that case. return self::getLocalisationCache()->getItem($code, 'fallbackSequence') ?: array('en'); }
/** * main function */ public function execute() { $wikia = null; extract($this->extractRequestParams()); /** * database instance */ $db = $this->getDB(); $activeonly = false; if (isset($active)) { $activeonly = true; } /** * query builder */ $this->addTables(array('city_list')); if ($activeonly) { $this->addWhereFld('city_public', 1); } if ($wikia) { $this->addWhereFld('city_id', $wikia); } if (empty($wikia)) { if (!empty($to)) { if ($to && is_int($to)) { $this->addWhere('city_id <= ' . intval($to)); } } if (!empty($from)) { if ($from && is_int($from)) { $this->addWhere('city_id >= ' . intval($from)); } } } if (!empty($lang)) { if (!Language::isValidBuiltInCode($lang)) { // FIXME add proper error msg $this->dieUsageMsg(array('invalidtitle', $lang)); } $this->addWhereFld('city_lang', $lang); } if (isset($countonly)) { /** * query builder */ $this->addFields(array('count(*) as cnt')); $data = array(); $res = $this->select(__METHOD__); if ($row = $db->fetchObject($res)) { $data['count'] = $row->cnt; ApiResult::setContent($data, $row->cnt); } $db->freeResult($res); } else { $this->addFields(array('city_id', 'city_url', 'city_lang')); $this->addOption("ORDER BY ", "city_id"); #--- result builder $data = array(); $res = $this->select(__METHOD__); while ($row = $db->fetchObject($res)) { $domain = $row->city_url; $domain = preg_replace('/^http:\\/\\//', '', $domain); $domain = preg_replace('/\\/$/', '', $domain); if ($domain) { $data[$row->city_id] = array("id" => $row->city_id, "domain" => $domain, "lang" => $row->city_lang); ApiResult::setContent($data[$row->city_id], $domain); } } $db->freeResult($res); } $this->getResult()->setIndexedTagName($data, 'variable'); $this->getResult()->addValue('query', $this->getModuleName(), $data); }
/** * Test Language::isValidBuiltInCode() * @dataProvider provideLanguageCodes * @covers Language::isValidBuiltInCode */ public function testBuiltInCodeValidation($code, $expected, $message = '') { $this->assertEquals($expected, (bool) Language::isValidBuiltInCode($code), "validating code {$code} {$message}"); }
/** * @param $parser Parser * @param $format string * @param $date string * @param $language string * @param $local string|bool * @return string */ public static function time($parser, $format = '', $date = '', $language = '', $local = false) { global $wgLocaltimezone; self::registerClearHook(); if ($date === '') { $cacheKey = $parser->getOptions()->getTimestamp(); $date = wfTimestamp(TS_ISO_8601, $cacheKey); } else { $cacheKey = $date; } if (isset(self::$mTimeCache[$format][$cacheKey][$language][$local])) { return self::$mTimeCache[$format][$cacheKey][$language][$local]; } # compute the timestamp string $ts # PHP >= 5.2 can handle dates before 1970 or after 2038 using the DateTime object $invalidTime = false; # the DateTime constructor must be used because it throws exceptions # when errors occur, whereas date_create appears to just output a warning # that can't really be detected from within the code try { # Default input timezone is UTC. $utc = new DateTimeZone('UTC'); # Correct for DateTime interpreting 'XXXX' as XX:XX o'clock if (preg_match('/^[0-9]{4}$/', $date)) { $date = '00:00 ' . $date; } # Parse date # UTC is a default input timezone. $dateObject = new DateTime($date, $utc); # Set output timezone. if ($local) { if (isset($wgLocaltimezone)) { $tz = new DateTimeZone($wgLocaltimezone); } else { $tz = new DateTimeZone(date_default_timezone_get()); } $dateObject->setTimezone($tz); } else { $dateObject->setTimezone($utc); } # Generate timestamp $ts = $dateObject->format('YmdHis'); } catch (Exception $ex) { $invalidTime = true; } # format the timestamp and return the result if ($invalidTime) { $result = '<strong class="error">' . wfMsgForContent('pfunc_time_error') . '</strong>'; } else { self::$mTimeChars += strlen($format); if (self::$mTimeChars > self::$mMaxTimeChars) { return '<strong class="error">' . wfMsgForContent('pfunc_time_too_long') . '</strong>'; } else { if ($ts < 100000000000000) { // Language can't deal with years after 9999 if ($language !== '' && Language::isValidBuiltInCode($language)) { // use whatever language is passed as a parameter $langObject = Language::factory($language); $result = $langObject->sprintfDate($format, $ts); } else { // use wiki's content language $result = $parser->getFunctionLang()->sprintfDate($format, $ts); } } else { return '<strong class="error">' . wfMsgForContent('pfunc_time_too_big') . '</strong>'; } } } self::$mTimeCache[$format][$cacheKey][$language][$local] = $result; return $result; }
/** * Get the ordered list of fallback languages. * * @since 1.19 * @param string $code Language code * @return array */ public static function getFallbacksFor($code) { if ($code === 'en' || !Language::isValidBuiltInCode($code)) { return array(); } else { $v = self::getLocalisationCache()->getItem($code, 'fallback'); $v = array_map('trim', explode(',', $v)); if ($v[count($v) - 1] !== 'en') { $v[] = 'en'; } return $v; } }
function isValidBuiltInCode($code) { $this->checkType('isValidBuiltInCode', 1, $code, 'string'); return array((bool) Language::isValidBuiltInCode($code)); }
/** * Create a language object for a given language code * @param $code String * @return Language */ protected static function newFromCode($code) { global $IP; static $recursionLevel = 0; // Protect against path traversal below if (!Language::isValidCode($code) || strcspn($code, ":/\\") !== strlen($code)) { throw new MWException("Invalid language code \"{$code}\""); } if (!Language::isValidBuiltInCode($code)) { // It's not possible to customise this code with class files, so // just return a Language object. This is to support uselang= hacks. $lang = new Language(); $lang->setCode($code); return $lang; } if ($code == 'en') { $class = 'Language'; } else { $class = 'Language' . str_replace('-', '_', ucfirst($code)); if (!defined('MW_COMPILED')) { // Preload base classes to work around APC/PHP5 bug if (file_exists("{$IP}/languages/classes/{$class}.deps.php")) { include_once "{$IP}/languages/classes/{$class}.deps.php"; } if (file_exists("{$IP}/languages/classes/{$class}.php")) { include_once "{$IP}/languages/classes/{$class}.php"; } } } if ($recursionLevel > 5) { throw new MWException("Language fallback loop detected when creating class {$class}\n"); } if (!MWInit::classExists($class)) { $fallback = Language::getFallbackFor($code); ++$recursionLevel; $lang = Language::newFromCode($fallback); --$recursionLevel; $lang->setCode($code); } else { $lang = new $class(); } return $lang; }
/** * @return string */ public function getLanguage() { if ($this->language === null) { // Must be a valid language code after this point (T64849) // Only support uselang values that follow built-in conventions (T102058) $lang = $this->getRequest()->getRawVal('lang', ''); // Stricter version of RequestContext::sanitizeLangCode() if (!Language::isValidBuiltInCode($lang)) { wfDebug("Invalid user language code\n"); $lang = $this->getResourceLoader()->getConfig()->get('LanguageCode'); } $this->language = $lang; } return $this->language; }
/** * getLanguage * Returns a valid mediawiki language code to use for all the * DonationInterface translations. * Will only look at the currently configured language if the 'language' key * doesn't exist in the data set: Users may not have a language preference * set if we're bouncing between mediawiki instances for payments. * @param array $data A normalized DonationInterface data set. * @return string A valid mediawiki language code. */ public static function getLanguage($data) { global $wgLang; if (array_key_exists('language', $data) && Language::isValidBuiltInCode($data['language'])) { return $data['language']; } else { return $wgLang->getCode(); } }
/** * normalize helper function. * If the language has not yet been set or is not valid, pulls the language code * from the current global language object. * Also sets the premium_language as the calculated language if it's not * already set coming in (had been defaulting to english). */ protected function setLanguage() { global $wgLang; $language = false; if ($this->isSomething('uselang')) { $language = $this->getVal('uselang'); } elseif ($this->isSomething('language')) { $language = $this->getVal('language'); } if ($language == false || !Language::isValidBuiltInCode($this->normalized['language'])) { $language = $wgLang->getCode(); } $this->setVal('language', $language); $this->expunge('uselang'); if (!$this->isSomething('premium_language')) { $this->setVal('premium_language', $language); } }