ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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. More...
 
 create ($config, $context, $code=false)
 Creates a language object, handles class fallbacks. More...
 
 getFallbackFor ($code)
 Returns the fallback language for language. More...
 
 loadLanguage ($code)
 Loads language into the cache, handles message file and fallbacks. More...
 

Static Public Member Functions

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

Data Fields

 $cache
 Cache of language code information used to load HTMLPurifier_Language objects. More...
 
 $keys = array('fallback', 'messages', 'errorNames')
 Valid keys in the HTMLPurifier_Language object. More...
 

Protected Attributes

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

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

◆ create()

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

Creates a language object, handles class fallbacks.

Parameters
HTMLPurifier_Config$config
HTMLPurifier_Context$context
bool | string$codeCode to override configuration with. Private parameter.
Returns
HTMLPurifier_Language

Definition at line 88 of file LanguageFactory.php.

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

89  {
90  // validate language code
91  if ($code === false) {
92  $code = $this->validator->validate(
93  $config->get('Core.Language'),
94  $config,
95  $context
96  );
97  } else {
98  $code = $this->validator->validate($code, $config, $context);
99  }
100  if ($code === false) {
101  $code = 'en'; // malformed code becomes English
102  }
103 
104  $pcode = str_replace('-', '_', $code); // make valid PHP classname
105  static $depth = 0; // recursion protection
106 
107  if ($code == 'en') {
108  $lang = new HTMLPurifier_Language($config, $context);
109  } else {
110  $class = 'HTMLPurifier_Language_' . $pcode;
111  $file = $this->dir . '/Language/classes/' . $code . '.php';
112  if (file_exists($file) || class_exists($class, false)) {
113  $lang = new $class($config, $context);
114  } else {
115  // Go fallback
116  $raw_fallback = $this->getFallbackFor($code);
117  $fallback = $raw_fallback ? $raw_fallback : 'en';
118  $depth++;
119  $lang = $this->create($config, $context, $fallback);
120  if (!$raw_fallback) {
121  $lang->error = true;
122  }
123  $depth--;
124  }
125  }
126  $lang->code = $code;
127  return $lang;
128  }
Represents a language and defines localizable string formatting and other functions, as well as the localized messages for HTML Purifier.
Definition: Language.php:7
getFallbackFor($code)
Returns the fallback language for language.
$code
Definition: example_050.php:99
$fallback
Definition: en-x-test.php:5
create($config, $context, $code=false)
Creates a language object, handles class fallbacks.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
+ Here is the call graph for this function:

◆ getFallbackFor()

HTMLPurifier_LanguageFactory::getFallbackFor (   $code)

Returns the fallback language for language.

Note
Loads the original language into cache
Parameters
string$codelanguage code
Returns
string|bool

Definition at line 136 of file LanguageFactory.php.

References $code, and loadLanguage().

Referenced by create().

137  {
138  $this->loadLanguage($code);
139  return $this->cache[$code]['fallback'];
140  }
$code
Definition: example_050.php:99
loadLanguage($code)
Loads language into the cache, handles message file and fallbacks.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ instance()

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

Retrieve sole instance of the factory.

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

Definition at line 59 of file LanguageFactory.php.

Referenced by HTMLPurifier_Language\load(), and HTMLPurifier\purify().

60  {
61  static $instance = null;
62  if ($prototype !== null) {
63  $instance = $prototype;
64  } elseif ($instance === null || $prototype == true) {
65  $instance = new HTMLPurifier_LanguageFactory();
66  $instance->setup();
67  }
68  return $instance;
69  }
Class responsible for generating HTMLPurifier_Language objects, managing caching and fallbacks...
+ Here is the caller graph for this function:

◆ loadLanguage()

HTMLPurifier_LanguageFactory::loadLanguage (   $code)

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

Parameters
string$codelanguage code

Definition at line 146 of file LanguageFactory.php.

References $cache, $code, $fallback, $filename, $key, and array.

Referenced by getFallbackFor().

147  {
148  static $languages_seen = array(); // recursion guard
149 
150  // abort if we've already loaded it
151  if (isset($this->cache[$code])) {
152  return;
153  }
154 
155  // generate filename
156  $filename = $this->dir . '/Language/messages/' . $code . '.php';
157 
158  // default fallback : may be overwritten by the ensuing include
159  $fallback = ($code != 'en') ? 'en' : false;
160 
161  // load primary localisation
162  if (!file_exists($filename)) {
163  // skip the include: will rely solely on fallback
164  $filename = $this->dir . '/Language/messages/en.php';
165  $cache = array();
166  } else {
167  include $filename;
168  $cache = compact($this->keys);
169  }
170 
171  // load fallback localisation
172  if (!empty($fallback)) {
173 
174  // infinite recursion guard
175  if (isset($languages_seen[$code])) {
176  trigger_error(
177  'Circular fallback reference in language ' .
178  $code,
179  E_USER_ERROR
180  );
181  $fallback = 'en';
182  }
183  $language_seen[$code] = true;
184 
185  // load the fallback recursively
186  $this->loadLanguage($fallback);
187  $fallback_cache = $this->cache[$fallback];
188 
189  // merge fallback with current language
190  foreach ($this->keys as $key) {
191  if (isset($cache[$key]) && isset($fallback_cache[$key])) {
192  if (isset($this->mergeable_keys_map[$key])) {
193  $cache[$key] = $cache[$key] + $fallback_cache[$key];
194  } elseif (isset($this->mergeable_keys_list[$key])) {
195  $cache[$key] = array_merge($fallback_cache[$key], $cache[$key]);
196  }
197  } else {
198  $cache[$key] = $fallback_cache[$key];
199  }
200  }
201  }
202 
203  // save to cache for later retrieval
204  $this->cache[$code] = $cache;
205  return;
206  }
$code
Definition: example_050.php:99
$fallback
Definition: en-x-test.php:5
loadLanguage($code)
Loads language into the cache, handles message file and fallbacks.
$cache
Cache of language code information used to load HTMLPurifier_Language objects.
Create styles array
The data for the language used.
$key
Definition: croninfo.php:18
+ Here is the caller graph for this function:

◆ setup()

HTMLPurifier_LanguageFactory::setup ( )

Sets up the singleton, much like a constructor.

Note
Prevents people from getting this outside of the singleton

Definition at line 75 of file LanguageFactory.php.

76  {
77  $this->validator = new HTMLPurifier_AttrDef_Lang();
78  $this->dir = HTMLPURIFIER_PREFIX . '/HTMLPurifier';
79  }
Validates the HTML attribute lang, effectively a language code.
Definition: Lang.php:7

Field Documentation

◆ $cache

HTMLPurifier_LanguageFactory::$cache

Cache of language code information used to load HTMLPurifier_Language objects.

Structure is: $factory->cache[$language_code][$key] = $value array

Definition at line 18 of file LanguageFactory.php.

Referenced by loadLanguage().

◆ $dir

HTMLPurifier_LanguageFactory::$dir
protected

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

string

Definition at line 39 of file LanguageFactory.php.

◆ $keys

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

Definition at line 25 of file LanguageFactory.php.

◆ $mergeable_keys_list

HTMLPurifier_LanguageFactory::$mergeable_keys_list = array()
protected

Keys whose contents are a list and can be merged.

array lookup

Definition at line 51 of file LanguageFactory.php.

◆ $mergeable_keys_map

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

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

array

Definition at line 45 of file LanguageFactory.php.

◆ $validator

HTMLPurifier_LanguageFactory::$validator
protected

Instance to validate language codes.

HTMLPurifier_AttrDef_Lang

Definition at line 32 of file LanguageFactory.php.


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