ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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.

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 }
print $file
create($config, $context, $code=false)
Creates a language object, handles class fallbacks.
getFallbackFor($code)
Returns the fallback language for language.
Represents a language and defines localizable string formatting and other functions,...
Definition: Language.php:8
$code
Definition: example_050.php:99
$fallback
Definition: en-x-test.php:5

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

Referenced by create().

+ Here is the call graph for this function:
+ Here is the caller 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.

137 {
138 $this->loadLanguage($code);
139 return $this->cache[$code]['fallback'];
140 }
loadLanguage($code)
Loads language into the cache, handles message file and fallbacks.

References $code, and loadLanguage().

Referenced by create().

+ 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.

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.

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

+ 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.

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 }
$filename
Definition: buildRTE.php:89
$cache
Cache of language code information used to load HTMLPurifier_Language objects.

References $cache, $code, $fallback, $filename, and loadLanguage().

Referenced by getFallbackFor(), and loadLanguage().

+ Here is the call graph for this function:
+ 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:8

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 @type 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.

@type 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. @type 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.

@value 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.

@type array

Definition at line 45 of file LanguageFactory.php.

◆ $validator

HTMLPurifier_LanguageFactory::$validator
protected

Instance to validate language codes.

@type HTMLPurifier_AttrDef_Lang

Definition at line 32 of file LanguageFactory.php.


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