ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules 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. 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 Structure is: $factory->cache[$language_code][$key] = $value array map. More...
 
 $keys = array('fallback', 'messages', 'errorNames')
 Valid keys in the HTMLPurifier_Language object. More...
 

Protected Attributes

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

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

Definition at line 129 of file LanguageFactory.php.

References loadLanguage().

Referenced by create().

129  {
130  $this->loadLanguage($code);
131  return $this->cache[$code]['fallback'];
132  }
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
$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(), and HTMLPurifier\purify().

57  {
58  static $instance = null;
59  if ($prototype !== null) {
60  $instance = $prototype;
61  } elseif ($instance === null || $prototype == true) {
62  $instance = new HTMLPurifier_LanguageFactory();
63  $instance->setup();
64  }
65  return $instance;
66  }
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
$codestring language code

Definition at line 138 of file LanguageFactory.php.

References $cache, $fallback, and $filename.

Referenced by getFallbackFor().

138  {
139  static $languages_seen = array(); // recursion guard
140 
141  // abort if we've already loaded it
142  if (isset($this->cache[$code])) return;
143 
144  // generate filename
145  $filename = $this->dir . '/Language/messages/' . $code . '.php';
146 
147  // default fallback : may be overwritten by the ensuing include
148  $fallback = ($code != 'en') ? 'en' : false;
149 
150  // load primary localisation
151  if (!file_exists($filename)) {
152  // skip the include: will rely solely on fallback
153  $filename = $this->dir . '/Language/messages/en.php';
154  $cache = array();
155  } else {
156  include $filename;
157  $cache = compact($this->keys);
158  }
159 
160  // load fallback localisation
161  if (!empty($fallback)) {
162 
163  // infinite recursion guard
164  if (isset($languages_seen[$code])) {
165  trigger_error('Circular fallback reference in language ' .
166  $code, E_USER_ERROR);
167  $fallback = 'en';
168  }
169  $language_seen[$code] = true;
170 
171  // load the fallback recursively
172  $this->loadLanguage($fallback);
173  $fallback_cache = $this->cache[$fallback];
174 
175  // merge fallback with current language
176  foreach ( $this->keys as $key ) {
177  if (isset($cache[$key]) && isset($fallback_cache[$key])) {
178  if (isset($this->mergeable_keys_map[$key])) {
179  $cache[$key] = $cache[$key] + $fallback_cache[$key];
180  } elseif (isset($this->mergeable_keys_list[$key])) {
181  $cache[$key] = array_merge( $fallback_cache[$key], $cache[$key] );
182  }
183  } else {
184  $cache[$key] = $fallback_cache[$key];
185  }
186  }
187 
188  }
189 
190  // save to cache for later retrieval
191  $this->cache[$code] = $cache;
192 
193  return;
194  }
$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 Structure is: $factory-...
$filename
Definition: buildRTE.php:89
+ 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 72 of file LanguageFactory.php.

72  {
73  $this->validator = new HTMLPurifier_AttrDef_Lang();
74  $this->dir = HTMLPURIFIER_PREFIX . '/HTMLPurifier';
75  }
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 map.

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

Definition at line 38 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 list

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 50 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 lookup.

Definition at line 44 of file LanguageFactory.php.

◆ $validator

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: