friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/LanguageFactory.php

210 lines
6.5 KiB
PHP
Raw Normal View History

2010-09-09 03:14:17 +00:00
<?php
/**
* Class responsible for generating HTMLPurifier_Language objects, managing
* caching and fallbacks.
* @note Thanks to MediaWiki for the general logic, although this version
* has been entirely rewritten
* @todo Serialized cache for languages
*/
class HTMLPurifier_LanguageFactory
{
/**
2016-02-09 10:06:17 +00:00
* Cache of language code information used to load HTMLPurifier_Language objects.
2010-09-09 03:14:17 +00:00
* Structure is: $factory->cache[$language_code][$key] = $value
2016-02-09 10:06:17 +00:00
* @type array
2010-09-09 03:14:17 +00:00
*/
public $cache;
/**
* Valid keys in the HTMLPurifier_Language object. Designates which
* variables to slurp out of a message file.
2016-02-09 10:06:17 +00:00
* @type array
2010-09-09 03:14:17 +00:00
*/
public $keys = array('fallback', 'messages', 'errorNames');
/**
2016-02-09 10:06:17 +00:00
* Instance to validate language codes.
* @type HTMLPurifier_AttrDef_Lang
*
2010-09-09 03:14:17 +00:00
*/
protected $validator;
/**
* Cached copy of dirname(__FILE__), directory of current file without
2016-02-09 10:06:17 +00:00
* trailing slash.
* @type string
2010-09-09 03:14:17 +00:00
*/
protected $dir;
/**
2016-02-09 10:06:17 +00:00
* Keys whose contents are a hash map and can be merged.
* @type array
2010-09-09 03:14:17 +00:00
*/
protected $mergeable_keys_map = array('messages' => true, 'errorNames' => true);
/**
2016-02-09 10:06:17 +00:00
* Keys whose contents are a list and can be merged.
2010-09-09 03:14:17 +00:00
* @value array lookup
*/
protected $mergeable_keys_list = array();
/**
* Retrieve sole instance of the factory.
2016-02-09 10:06:17 +00:00
* @param HTMLPurifier_LanguageFactory $prototype Optional prototype to overload sole instance with,
2010-09-09 03:14:17 +00:00
* or bool true to reset to default factory.
2016-02-09 10:06:17 +00:00
* @return HTMLPurifier_LanguageFactory
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
public static function instance($prototype = null)
{
2010-09-09 03:14:17 +00:00
static $instance = null;
if ($prototype !== null) {
$instance = $prototype;
} elseif ($instance === null || $prototype == true) {
$instance = new HTMLPurifier_LanguageFactory();
$instance->setup();
}
return $instance;
}
/**
* Sets up the singleton, much like a constructor
* @note Prevents people from getting this outside of the singleton
*/
2016-02-09 10:06:17 +00:00
public function setup()
{
2010-09-09 03:14:17 +00:00
$this->validator = new HTMLPurifier_AttrDef_Lang();
$this->dir = HTMLPURIFIER_PREFIX . '/HTMLPurifier';
}
/**
* Creates a language object, handles class fallbacks
2016-02-09 10:06:17 +00:00
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @param bool|string $code Code to override configuration with. Private parameter.
* @return HTMLPurifier_Language
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
public function create($config, $context, $code = false)
{
2010-09-09 03:14:17 +00:00
// validate language code
if ($code === false) {
$code = $this->validator->validate(
2016-02-09 10:06:17 +00:00
$config->get('Core.Language'),
$config,
$context
2010-09-09 03:14:17 +00:00
);
} else {
$code = $this->validator->validate($code, $config, $context);
}
2016-02-09 10:06:17 +00:00
if ($code === false) {
$code = 'en'; // malformed code becomes English
}
2010-09-09 03:14:17 +00:00
$pcode = str_replace('-', '_', $code); // make valid PHP classname
static $depth = 0; // recursion protection
if ($code == 'en') {
$lang = new HTMLPurifier_Language($config, $context);
} else {
$class = 'HTMLPurifier_Language_' . $pcode;
$file = $this->dir . '/Language/classes/' . $code . '.php';
if (file_exists($file) || class_exists($class, false)) {
$lang = new $class($config, $context);
} else {
// Go fallback
$raw_fallback = $this->getFallbackFor($code);
$fallback = $raw_fallback ? $raw_fallback : 'en';
$depth++;
$lang = $this->create($config, $context, $fallback);
if (!$raw_fallback) {
$lang->error = true;
}
$depth--;
}
}
$lang->code = $code;
return $lang;
}
/**
* Returns the fallback language for language
* @note Loads the original language into cache
2016-02-09 10:06:17 +00:00
* @param string $code language code
* @return string|bool
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
public function getFallbackFor($code)
{
2010-09-09 03:14:17 +00:00
$this->loadLanguage($code);
return $this->cache[$code]['fallback'];
}
/**
* Loads language into the cache, handles message file and fallbacks
2016-02-09 10:06:17 +00:00
* @param string $code language code
2010-09-09 03:14:17 +00:00
*/
2016-02-09 10:06:17 +00:00
public function loadLanguage($code)
{
2010-09-09 03:14:17 +00:00
static $languages_seen = array(); // recursion guard
// abort if we've already loaded it
2016-02-09 10:06:17 +00:00
if (isset($this->cache[$code])) {
return;
}
2010-09-09 03:14:17 +00:00
// generate filename
$filename = $this->dir . '/Language/messages/' . $code . '.php';
// default fallback : may be overwritten by the ensuing include
$fallback = ($code != 'en') ? 'en' : false;
// load primary localisation
if (!file_exists($filename)) {
// skip the include: will rely solely on fallback
$filename = $this->dir . '/Language/messages/en.php';
$cache = array();
} else {
include $filename;
$cache = compact($this->keys);
}
// load fallback localisation
if (!empty($fallback)) {
// infinite recursion guard
if (isset($languages_seen[$code])) {
2016-02-09 10:06:17 +00:00
trigger_error(
'Circular fallback reference in language ' .
$code,
E_USER_ERROR
);
2010-09-09 03:14:17 +00:00
$fallback = 'en';
}
$language_seen[$code] = true;
// load the fallback recursively
$this->loadLanguage($fallback);
$fallback_cache = $this->cache[$fallback];
// merge fallback with current language
2016-02-09 10:06:17 +00:00
foreach ($this->keys as $key) {
2010-09-09 03:14:17 +00:00
if (isset($cache[$key]) && isset($fallback_cache[$key])) {
if (isset($this->mergeable_keys_map[$key])) {
$cache[$key] = $cache[$key] + $fallback_cache[$key];
} elseif (isset($this->mergeable_keys_list[$key])) {
2016-02-09 10:06:17 +00:00
$cache[$key] = array_merge($fallback_cache[$key], $cache[$key]);
2010-09-09 03:14:17 +00:00
}
} else {
$cache[$key] = $fallback_cache[$key];
}
}
}
// save to cache for later retrieval
$this->cache[$code] = $cache;
return;
}
}
// vim: et sw=4 sts=4