ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
HTMLPurifier_LanguageFactory Class Reference

Class responsible for generating HTMLPurifier_Language objects, managing caching and fallbacks. More...

+ Collaboration diagram for HTMLPurifier_LanguageFactory:

Public Member Functions

 setup ()
 Sets up the singleton, much like a constructor.
 create ($config, $context, $code=false)
 Creates a language object, handles class fallbacks.
 getFallbackFor ($code)
 Returns the fallback language for language.
 loadLanguage ($code)
 Loads language into the cache, handles message file and fallbacks.

Static Public Member Functions

static instance ($prototype=null)
 Retrieve sole instance of the factory.

Data Fields

 $cache
 Cache of language code information used to load HTMLPurifier_Language objects Structure is: $factory->cache[$language_code][$key] = $value array map.
 $keys = array('fallback', 'messages', 'errorNames')
 Valid keys in the HTMLPurifier_Language object.

Protected Attributes

 $validator
 Instance of HTMLPurifier_AttrDef_Lang to validate language codes object HTMLPurifier_AttrDef_Lang.
 $dir
 Cached copy of dirname(FILE), directory of current file without trailing slash string filename.
 $mergeable_keys_map = array('messages' => true, 'errorNames' => true)
 Keys whose contents are a hash map and can be merged array lookup.
 $mergeable_keys_list = array()
 Keys whose contents are a list and can be merged array lookup.

Detailed Description

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

Definition at line 10 of file LanguageFactory.php.

Member Function Documentation

HTMLPurifier_LanguageFactory::create (   $config,
  $context,
  $code = false 
)

Creates a language object, handles class fallbacks.

Parameters
$configInstance of HTMLPurifier_Config
$contextInstance of HTMLPurifier_Context
$codeCode to override configuration with. Private parameter.

Definition at line 83 of file LanguageFactory.php.

References $fallback, $file, $lang, and getFallbackFor().

{
// validate language code
if ($code === false) {
$code = $this->validator->validate(
$config->get('Core.Language'), $config, $context
);
} else {
$code = $this->validator->validate($code, $config, $context);
}
if ($code === false) $code = 'en'; // malformed code becomes English
$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;
}

+ Here is the call graph for this function:

HTMLPurifier_LanguageFactory::getFallbackFor (   $code)

Returns the fallback language for language.

Note
Loads the original language into cache
Parameters
$codestring language code

Definition at line 129 of file LanguageFactory.php.

References loadLanguage().

Referenced by create().

{
$this->loadLanguage($code);
return $this->cache[$code]['fallback'];
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static HTMLPurifier_LanguageFactory::instance (   $prototype = null)
static

Retrieve sole instance of the factory.

Parameters
$prototypeOptional prototype to overload sole instance with, or bool true to reset to default factory.

Definition at line 57 of file LanguageFactory.php.

Referenced by HTMLPurifier_Language\load().

{
static $instance = null;
if ($prototype !== null) {
$instance = $prototype;
} elseif ($instance === null || $prototype == true) {
$instance = new HTMLPurifier_LanguageFactory();
$instance->setup();
}
return $instance;
}

+ Here is the caller graph for this function:

HTMLPurifier_LanguageFactory::loadLanguage (   $code)

Loads language into the cache, handles message file and fallbacks.

Parameters
$codestring language code

Definition at line 138 of file LanguageFactory.php.

References $cache, $fallback, and $filename.

Referenced by getFallbackFor().

{
static $languages_seen = array(); // recursion guard
// abort if we've already loaded it
if (isset($this->cache[$code])) return;
// 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])) {
trigger_error('Circular fallback reference in language ' .
$code, E_USER_ERROR);
$fallback = 'en';
}
$language_seen[$code] = true;
// load the fallback recursively
$fallback_cache = $this->cache[$fallback];
// merge fallback with current language
foreach ( $this->keys as $key ) {
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])) {
$cache[$key] = array_merge( $fallback_cache[$key], $cache[$key] );
}
} else {
$cache[$key] = $fallback_cache[$key];
}
}
}
// save to cache for later retrieval
$this->cache[$code] = $cache;
return;
}

+ Here is the caller graph for this function:

HTMLPurifier_LanguageFactory::setup ( )

Sets up the singleton, much like a constructor.

Note
Prevents people from getting this outside of the singleton

Definition at line 72 of file LanguageFactory.php.

{
$this->validator = new HTMLPurifier_AttrDef_Lang();
$this->dir = HTMLPURIFIER_PREFIX . '/HTMLPurifier';
}

Field Documentation

HTMLPurifier_LanguageFactory::$cache

Cache of language code information used to load HTMLPurifier_Language objects Structure is: $factory->cache[$language_code][$key] = $value array map.

Definition at line 18 of file LanguageFactory.php.

Referenced by loadLanguage().

HTMLPurifier_LanguageFactory::$dir
protected

Cached copy of dirname(FILE), directory of current file without trailing slash string filename.

Definition at line 38 of file LanguageFactory.php.

HTMLPurifier_LanguageFactory::$keys = array('fallback', 'messages', 'errorNames')

Valid keys in the HTMLPurifier_Language object.

Designates which variables to slurp out of a message file. array list

Definition at line 25 of file LanguageFactory.php.

HTMLPurifier_LanguageFactory::$mergeable_keys_list = array()
protected

Keys whose contents are a list and can be merged array lookup.

Definition at line 50 of file LanguageFactory.php.

HTMLPurifier_LanguageFactory::$mergeable_keys_map = array('messages' => true, 'errorNames' => true)
protected

Keys whose contents are a hash map and can be merged array lookup.

Definition at line 44 of file LanguageFactory.php.

HTMLPurifier_LanguageFactory::$validator
protected

Instance of HTMLPurifier_AttrDef_Lang to validate language codes object HTMLPurifier_AttrDef_Lang.

Definition at line 31 of file LanguageFactory.php.


The documentation for this class was generated from the following file: