MDL-46585 lang: display debugging message when deprecated string is used
[moodle.git] / lib / classes / string_manager_standard.php
CommitLineData
99a9d8d9
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Standard string manager.
19 *
20 * @package core
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27
28/**
29 * Standard string_manager implementation
30 *
31 * Implements string_manager with getting and printing localised strings
32 *
33 * @package core
34 * @copyright 2010 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class core_string_manager_standard implements core_string_manager {
38 /** @var string location of all packs except 'en' */
39 protected $otherroot;
40 /** @var string location of all lang pack local modifications */
41 protected $localroot;
42 /** @var cache lang string cache - it will be optimised more later */
43 protected $cache;
44 /** @var int get_string() counter */
45 protected $countgetstring = 0;
46 /** @var bool use disk cache */
99a9d8d9 47 protected $translist;
598a82c9 48 /** @var cache stores list of available translations */
99a9d8d9 49 protected $menucache;
4b867a06
MG
50 /** @var array list of cached deprecated strings */
51 protected $cacheddeprecated;
99a9d8d9
PS
52
53 /**
54 * Create new instance of string manager
55 *
598a82c9 56 * @param string $otherroot location of downloaded lang packs - usually $CFG->dataroot/lang
99a9d8d9 57 * @param string $localroot usually the same as $otherroot
99a9d8d9 58 * @param array $translist limit list of visible translations
99a9d8d9 59 */
598a82c9 60 public function __construct($otherroot, $localroot, $translist) {
99a9d8d9
PS
61 $this->otherroot = $otherroot;
62 $this->localroot = $localroot;
598a82c9
PS
63 if ($translist) {
64 $this->translist = array_combine($translist, $translist);
65 } else {
66 $this->translist = array();
67 }
99a9d8d9 68
598a82c9 69 if ($this->get_revision() > 0) {
99a9d8d9
PS
70 // We can use a proper cache, establish the cache using the 'String cache' definition.
71 $this->cache = cache::make('core', 'string');
598a82c9 72 $this->menucache = cache::make('core', 'langmenu');
99a9d8d9
PS
73 } else {
74 // We only want a cache for the length of the request, create a static cache.
75 $options = array(
76 'simplekeys' => true,
77 'simpledata' => true
78 );
79 $this->cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core', 'string', array(), $options);
598a82c9 80 $this->menucache = cache::make_from_params(cache_store::MODE_REQUEST, 'core', 'langmenu', array(), $options);
99a9d8d9
PS
81 }
82 }
83
84 /**
85 * Returns list of all explicit parent languages for the given language.
86 *
87 * English (en) is considered as the top implicit parent of all language packs
88 * and is not included in the returned list. The language itself is appended to the
89 * end of the list. The method is aware of circular dependency risk.
90 *
91 * @see self::populate_parent_languages()
92 * @param string $lang the code of the language
93 * @return array all explicit parent languages with the lang itself appended
94 */
95 public function get_language_dependencies($lang) {
96 return $this->populate_parent_languages($lang);
97 }
98
99 /**
100 * Load all strings for one component
101 *
102 * @param string $component The module the string is associated with
103 * @param string $lang
104 * @param bool $disablecache Do not use caches, force fetching the strings from sources
105 * @param bool $disablelocal Do not use customized strings in xx_local language packs
106 * @return array of all string for given component and lang
107 */
598a82c9 108 public function load_component_strings($component, $lang, $disablecache = false, $disablelocal = false) {
99a9d8d9
PS
109 global $CFG;
110
111 list($plugintype, $pluginname) = core_component::normalize_component($component);
598a82c9 112 if ($plugintype === 'core' and is_null($pluginname)) {
99a9d8d9
PS
113 $component = 'core';
114 } else {
115 $component = $plugintype . '_' . $pluginname;
116 }
117
598a82c9 118 $cachekey = $lang.'_'.$component.'_'.$this->get_key_suffix();
99a9d8d9 119
598a82c9 120 $cachedstring = $this->cache->get($cachekey);
99a9d8d9 121 if (!$disablecache and !$disablelocal) {
598a82c9
PS
122 if ($cachedstring !== false) {
123 return $cachedstring;
99a9d8d9
PS
124 }
125 }
126
127 // No cache found - let us merge all possible sources of the strings.
128 if ($plugintype === 'core') {
129 $file = $pluginname;
130 if ($file === null) {
131 $file = 'moodle';
132 }
133 $string = array();
134 // First load english pack.
135 if (!file_exists("$CFG->dirroot/lang/en/$file.php")) {
136 return array();
137 }
138 include("$CFG->dirroot/lang/en/$file.php");
598a82c9 139 $enstring = $string;
99a9d8d9
PS
140
141 // And then corresponding local if present and allowed.
142 if (!$disablelocal and file_exists("$this->localroot/en_local/$file.php")) {
143 include("$this->localroot/en_local/$file.php");
144 }
145 // Now loop through all langs in correct order.
146 $deps = $this->get_language_dependencies($lang);
147 foreach ($deps as $dep) {
148 // The main lang string location.
149 if (file_exists("$this->otherroot/$dep/$file.php")) {
150 include("$this->otherroot/$dep/$file.php");
151 }
152 if (!$disablelocal and file_exists("$this->localroot/{$dep}_local/$file.php")) {
153 include("$this->localroot/{$dep}_local/$file.php");
154 }
155 }
156
157 } else {
158 if (!$location = core_component::get_plugin_directory($plugintype, $pluginname) or !is_dir($location)) {
159 return array();
160 }
161 if ($plugintype === 'mod') {
162 // Bloody mod hack.
163 $file = $pluginname;
164 } else {
165 $file = $plugintype . '_' . $pluginname;
166 }
167 $string = array();
168 // First load English pack.
169 if (!file_exists("$location/lang/en/$file.php")) {
170 // English pack does not exist, so do not try to load anything else.
171 return array();
172 }
173 include("$location/lang/en/$file.php");
598a82c9 174 $enstring = $string;
99a9d8d9
PS
175 // And then corresponding local english if present.
176 if (!$disablelocal and file_exists("$this->localroot/en_local/$file.php")) {
177 include("$this->localroot/en_local/$file.php");
178 }
179
180 // Now loop through all langs in correct order.
181 $deps = $this->get_language_dependencies($lang);
182 foreach ($deps as $dep) {
183 // Legacy location - used by contrib only.
184 if (file_exists("$location/lang/$dep/$file.php")) {
185 include("$location/lang/$dep/$file.php");
186 }
187 // The main lang string location.
188 if (file_exists("$this->otherroot/$dep/$file.php")) {
189 include("$this->otherroot/$dep/$file.php");
190 }
191 // Local customisations.
192 if (!$disablelocal and file_exists("$this->localroot/{$dep}_local/$file.php")) {
193 include("$this->localroot/{$dep}_local/$file.php");
194 }
195 }
196 }
197
198 // We do not want any extra strings from other languages - everything must be in en lang pack.
598a82c9 199 $string = array_intersect_key($string, $enstring);
99a9d8d9
PS
200
201 if (!$disablelocal) {
598a82c9
PS
202 // Now we have a list of strings from all possible sources,
203 // cache it in MUC cache if not already there.
204 if ($cachedstring === false) {
205 $this->cache->set($cachekey, $string);
206 }
99a9d8d9
PS
207 }
208 return $string;
209 }
210
4b867a06
MG
211 /**
212 * Parses all deprecated.txt in all plugins lang locations and returns the list of deprecated strings.
213 *
214 * Static variable is used for caching, this function is only called in dev environment.
215 *
216 * @return array of deprecated strings in the same format they appear in deprecated.txt files: "identifier,component"
217 * where component is a normalised component (i.e. "core_moodle", "mod_assign", etc.)
218 */
219 protected function load_deprecated_strings() {
220 global $CFG;
221
222 if ($this->cacheddeprecated !== null) {
223 return $this->cacheddeprecated;
224 }
225
226 $this->cacheddeprecated = array();
227 $content = '';
228 $filename = $CFG->dirroot . '/lang/en/deprecated.txt';
229 if (file_exists($filename)) {
230 $content .= file_get_contents($filename);
231 }
232 foreach (core_component::get_plugin_types() as $plugintype => $plugintypedir) {
233 foreach (core_component::get_plugin_list($plugintype) as $pluginname => $plugindir) {
234 $filename = $plugindir.'/lang/en/deprecated.txt';
235 if (file_exists($filename)) {
236 $content .= "\n". file_get_contents($filename);
237 }
238 }
239 }
240
241 $strings = preg_split('/\s*\n\s*/', $content, -1, PREG_SPLIT_NO_EMPTY);
242 $this->cacheddeprecated = array_flip($strings);
243
244 return $this->cacheddeprecated;
245 }
246
247 /**
248 * Has string been deprecated?
249 *
250 * Usually checked only inside get_string() to display debug warnings.
251 *
252 * @param string $identifier The identifier of the string to search for
253 * @param string $component The module the string is associated with
254 * @return bool true if deprecated
255 */
256 public function string_deprecated($identifier, $component) {
257 $deprecated = $this->load_deprecated_strings();
258 list($plugintype, $pluginname) = core_component::normalize_component($component);
259 return isset($deprecated[$identifier . ',' . $plugintype . '_' . $pluginname]);
260 }
261
99a9d8d9
PS
262 /**
263 * Does the string actually exist?
264 *
265 * get_string() is throwing debug warnings, sometimes we do not want them
266 * or we want to display better explanation of the problem.
267 * Note: Use with care!
268 *
269 * @param string $identifier The identifier of the string to search for
270 * @param string $component The module the string is associated with
271 * @return boot true if exists
272 */
273 public function string_exists($identifier, $component) {
274 $lang = current_language();
275 $string = $this->load_component_strings($component, $lang);
276 return isset($string[$identifier]);
277 }
278
279 /**
280 * Get String returns a requested string
281 *
282 * @param string $identifier The identifier of the string to search for
283 * @param string $component The module the string is associated with
284 * @param string|object|array $a An object, string or number that can be used
285 * within translation strings
286 * @param string $lang moodle translation language, null means use current
287 * @return string The String !
288 */
289 public function get_string($identifier, $component = '', $a = null, $lang = null) {
4b867a06
MG
290 global $CFG;
291
99a9d8d9 292 $this->countgetstring++;
598a82c9 293 // There are very many uses of these time formatting strings without the 'langconfig' component,
99a9d8d9
PS
294 // it would not be reasonable to expect that all of them would be converted during 2.0 migration.
295 static $langconfigstrs = array(
296 'strftimedate' => 1,
297 'strftimedatefullshort' => 1,
298 'strftimedateshort' => 1,
299 'strftimedatetime' => 1,
300 'strftimedatetimeshort' => 1,
301 'strftimedaydate' => 1,
302 'strftimedaydatetime' => 1,
303 'strftimedayshort' => 1,
304 'strftimedaytime' => 1,
305 'strftimemonthyear' => 1,
306 'strftimerecent' => 1,
307 'strftimerecentfull' => 1,
308 'strftimetime' => 1);
309
310 if (empty($component)) {
311 if (isset($langconfigstrs[$identifier])) {
312 $component = 'langconfig';
313 } else {
314 $component = 'moodle';
315 }
316 }
317
318 if ($lang === null) {
319 $lang = current_language();
320 }
321
322 $string = $this->load_component_strings($component, $lang);
323
324 if (!isset($string[$identifier])) {
325 if ($component === 'pix' or $component === 'core_pix') {
326 // This component contains only alt tags for emoticons, not all of them are supposed to be defined.
327 return '';
328 }
329 if ($identifier === 'parentlanguage' and ($component === 'langconfig' or $component === 'core_langconfig')) {
330 // Identifier parentlanguage is a special string, undefined means use English if not defined.
331 return 'en';
332 }
598a82c9
PS
333 // Do not rebuild caches here!
334 // Devs need to learn to purge all caches after any change or disable $CFG->langstringcache.
99a9d8d9
PS
335 if (!isset($string[$identifier])) {
336 // The string is still missing - should be fixed by developer.
4b867a06 337 if ($CFG->debugdeveloper) {
598a82c9
PS
338 list($plugintype, $pluginname) = core_component::normalize_component($component);
339 if ($plugintype === 'core') {
340 $file = "lang/en/{$component}.php";
341 } else if ($plugintype == 'mod') {
342 $file = "mod/{$pluginname}/lang/en/{$pluginname}.php";
343 } else {
344 $path = core_component::get_plugin_directory($plugintype, $pluginname);
345 $file = "{$path}/lang/en/{$plugintype}_{$pluginname}.php";
346 }
347 debugging("Invalid get_string() identifier: '{$identifier}' or component '{$component}'. " .
348 "Perhaps you are missing \$string['{$identifier}'] = ''; in {$file}?", DEBUG_DEVELOPER);
99a9d8d9 349 }
99a9d8d9
PS
350 return "[[$identifier]]";
351 }
352 }
353
354 $string = $string[$identifier];
355
356 if ($a !== null) {
357 // Process array's and objects (except lang_strings).
358 if (is_array($a) or (is_object($a) && !($a instanceof lang_string))) {
359 $a = (array)$a;
360 $search = array();
361 $replace = array();
362 foreach ($a as $key => $value) {
363 if (is_int($key)) {
364 // We do not support numeric keys - sorry!
365 continue;
366 }
367 if (is_array($value) or (is_object($value) && !($value instanceof lang_string))) {
368 // We support just string or lang_string as value.
369 continue;
370 }
371 $search[] = '{$a->'.$key.'}';
372 $replace[] = (string)$value;
373 }
374 if ($search) {
375 $string = str_replace($search, $replace, $string);
376 }
377 } else {
378 $string = str_replace('{$a}', (string)$a, $string);
379 }
380 }
381
4b867a06
MG
382 if ($CFG->debugdeveloper) {
383 // Display a debugging message if sting exists but was deprecated.
384 if ($this->string_deprecated($identifier, $component)) {
385 list($plugintype, $pluginname) = core_component::normalize_component($component);
386 debugging("String [{$identifier},{$plugintype}_{$pluginname}] is deprecated. ".
387 'Either you should no longer be using that string, or the string has been incorrectly deprecated, in which case you should report this as a bug. '.
388 'Please refer to https://docs.moodle.org/dev/String_deprecation', DEBUG_DEVELOPER);
389 }
390 }
391
99a9d8d9
PS
392 return $string;
393 }
394
395 /**
598a82c9 396 * Returns information about the core_string_manager performance.
99a9d8d9
PS
397 *
398 * @return array
399 */
400 public function get_performance_summary() {
401 return array(array(
402 'langcountgetstring' => $this->countgetstring,
403 ), array(
404 'langcountgetstring' => 'get_string calls',
405 ));
406 }
407
408 /**
409 * Returns a localised list of all country names, sorted by localised name.
410 *
411 * @param bool $returnall return all or just enabled
412 * @param string $lang moodle translation language, null means use current
413 * @return array two-letter country code => translated name.
414 */
415 public function get_list_of_countries($returnall = false, $lang = null) {
416 global $CFG;
417
418 if ($lang === null) {
419 $lang = current_language();
420 }
421
422 $countries = $this->load_component_strings('core_countries', $lang);
423 core_collator::asort($countries);
424 if (!$returnall and !empty($CFG->allcountrycodes)) {
425 $enabled = explode(',', $CFG->allcountrycodes);
426 $return = array();
427 foreach ($enabled as $c) {
428 if (isset($countries[$c])) {
429 $return[$c] = $countries[$c];
430 }
431 }
432 return $return;
433 }
434
435 return $countries;
436 }
437
438 /**
439 * Returns a localised list of languages, sorted by code keys.
440 *
441 * @param string $lang moodle translation language, null means use current
442 * @param string $standard language list standard
443 * - iso6392: three-letter language code (ISO 639-2/T) => translated name
598a82c9 444 * - iso6391: two-letter language code (ISO 639-1) => translated name
99a9d8d9
PS
445 * @return array language code => translated name
446 */
447 public function get_list_of_languages($lang = null, $standard = 'iso6391') {
448 if ($lang === null) {
449 $lang = current_language();
450 }
451
452 if ($standard === 'iso6392') {
453 $langs = $this->load_component_strings('core_iso6392', $lang);
454 ksort($langs);
455 return $langs;
456
457 } else if ($standard === 'iso6391') {
458 $langs2 = $this->load_component_strings('core_iso6392', $lang);
459 static $mapping = array('aar' => 'aa', 'abk' => 'ab', 'afr' => 'af', 'aka' => 'ak', 'sqi' => 'sq', 'amh' => 'am', 'ara' => 'ar', 'arg' => 'an', 'hye' => 'hy',
460 'asm' => 'as', 'ava' => 'av', 'ave' => 'ae', 'aym' => 'ay', 'aze' => 'az', 'bak' => 'ba', 'bam' => 'bm', 'eus' => 'eu', 'bel' => 'be', 'ben' => 'bn', 'bih' => 'bh',
461 'bis' => 'bi', 'bos' => 'bs', 'bre' => 'br', 'bul' => 'bg', 'mya' => 'my', 'cat' => 'ca', 'cha' => 'ch', 'che' => 'ce', 'zho' => 'zh', 'chu' => 'cu', 'chv' => 'cv',
462 'cor' => 'kw', 'cos' => 'co', 'cre' => 'cr', 'ces' => 'cs', 'dan' => 'da', 'div' => 'dv', 'nld' => 'nl', 'dzo' => 'dz', 'eng' => 'en', 'epo' => 'eo', 'est' => 'et',
463 'ewe' => 'ee', 'fao' => 'fo', 'fij' => 'fj', 'fin' => 'fi', 'fra' => 'fr', 'fry' => 'fy', 'ful' => 'ff', 'kat' => 'ka', 'deu' => 'de', 'gla' => 'gd', 'gle' => 'ga',
464 'glg' => 'gl', 'glv' => 'gv', 'ell' => 'el', 'grn' => 'gn', 'guj' => 'gu', 'hat' => 'ht', 'hau' => 'ha', 'heb' => 'he', 'her' => 'hz', 'hin' => 'hi', 'hmo' => 'ho',
465 'hrv' => 'hr', 'hun' => 'hu', 'ibo' => 'ig', 'isl' => 'is', 'ido' => 'io', 'iii' => 'ii', 'iku' => 'iu', 'ile' => 'ie', 'ina' => 'ia', 'ind' => 'id', 'ipk' => 'ik',
466 'ita' => 'it', 'jav' => 'jv', 'jpn' => 'ja', 'kal' => 'kl', 'kan' => 'kn', 'kas' => 'ks', 'kau' => 'kr', 'kaz' => 'kk', 'khm' => 'km', 'kik' => 'ki', 'kin' => 'rw',
467 'kir' => 'ky', 'kom' => 'kv', 'kon' => 'kg', 'kor' => 'ko', 'kua' => 'kj', 'kur' => 'ku', 'lao' => 'lo', 'lat' => 'la', 'lav' => 'lv', 'lim' => 'li', 'lin' => 'ln',
468 'lit' => 'lt', 'ltz' => 'lb', 'lub' => 'lu', 'lug' => 'lg', 'mkd' => 'mk', 'mah' => 'mh', 'mal' => 'ml', 'mri' => 'mi', 'mar' => 'mr', 'msa' => 'ms', 'mlg' => 'mg',
469 'mlt' => 'mt', 'mon' => 'mn', 'nau' => 'na', 'nav' => 'nv', 'nbl' => 'nr', 'nde' => 'nd', 'ndo' => 'ng', 'nep' => 'ne', 'nno' => 'nn', 'nob' => 'nb', 'nor' => 'no',
470 'nya' => 'ny', 'oci' => 'oc', 'oji' => 'oj', 'ori' => 'or', 'orm' => 'om', 'oss' => 'os', 'pan' => 'pa', 'fas' => 'fa', 'pli' => 'pi', 'pol' => 'pl', 'por' => 'pt',
471 'pus' => 'ps', 'que' => 'qu', 'roh' => 'rm', 'ron' => 'ro', 'run' => 'rn', 'rus' => 'ru', 'sag' => 'sg', 'san' => 'sa', 'sin' => 'si', 'slk' => 'sk', 'slv' => 'sl',
472 'sme' => 'se', 'smo' => 'sm', 'sna' => 'sn', 'snd' => 'sd', 'som' => 'so', 'sot' => 'st', 'spa' => 'es', 'srd' => 'sc', 'srp' => 'sr', 'ssw' => 'ss', 'sun' => 'su',
473 'swa' => 'sw', 'swe' => 'sv', 'tah' => 'ty', 'tam' => 'ta', 'tat' => 'tt', 'tel' => 'te', 'tgk' => 'tg', 'tgl' => 'tl', 'tha' => 'th', 'bod' => 'bo', 'tir' => 'ti',
474 'ton' => 'to', 'tsn' => 'tn', 'tso' => 'ts', 'tuk' => 'tk', 'tur' => 'tr', 'twi' => 'tw', 'uig' => 'ug', 'ukr' => 'uk', 'urd' => 'ur', 'uzb' => 'uz', 'ven' => 've',
475 'vie' => 'vi', 'vol' => 'vo', 'cym' => 'cy', 'wln' => 'wa', 'wol' => 'wo', 'xho' => 'xh', 'yid' => 'yi', 'yor' => 'yo', 'zha' => 'za', 'zul' => 'zu');
476 $langs1 = array();
477 foreach ($mapping as $c2 => $c1) {
478 $langs1[$c1] = $langs2[$c2];
479 }
480 ksort($langs1);
481 return $langs1;
482
483 } else {
484 debugging('Unsupported $standard parameter in get_list_of_languages() method: '.$standard);
485 }
486
487 return array();
488 }
489
490 /**
491 * Checks if the translation exists for the language
492 *
493 * @param string $lang moodle translation language code
494 * @param bool $includeall include also disabled translations
495 * @return bool true if exists
496 */
497 public function translation_exists($lang, $includeall = true) {
598a82c9
PS
498 $translations = $this->get_list_of_translations($includeall);
499 return isset($translations[$lang]);
99a9d8d9
PS
500 }
501
502 /**
503 * Returns localised list of installed translations
504 *
505 * @param bool $returnall return all or just enabled
506 * @return array moodle translation code => localised translation name
507 */
508 public function get_list_of_translations($returnall = false) {
509 global $CFG;
510
511 $languages = array();
512
598a82c9
PS
513 $cachekey = 'list_'.$this->get_key_suffix();
514 $cachedlist = $this->menucache->get($cachekey);
515 if ($cachedlist !== false) {
516 // The cache content is invalid.
517 if ($returnall or empty($this->translist)) {
518 return $cachedlist;
519 }
520 // Return only enabled translations.
521 foreach ($cachedlist as $langcode => $langname) {
522 if (isset($this->translist[$langcode])) {
523 $languages[$langcode] = $langname;
99a9d8d9
PS
524 }
525 }
598a82c9 526 return $languages;
99a9d8d9
PS
527 }
528
598a82c9
PS
529 // Get all languages available in system.
530 $langdirs = get_list_of_plugins('', 'en', $this->otherroot);
531 $langdirs["$CFG->dirroot/lang/en"] = 'en';
532
533 // Loop through all langs and get info.
534 foreach ($langdirs as $lang) {
535 if (strrpos($lang, '_local') !== false) {
536 // Just a local tweak of some other lang pack.
537 continue;
538 }
539 if (strrpos($lang, '_utf8') !== false) {
540 // Legacy 1.x lang pack.
541 continue;
99a9d8d9 542 }
598a82c9
PS
543 if ($lang !== clean_param($lang, PARAM_SAFEDIR)) {
544 // Invalid lang pack name!
545 continue;
546 }
547 $string = $this->load_component_strings('langconfig', $lang);
548 if (!empty($string['thislanguage'])) {
549 $languages[$lang] = $string['thislanguage'].' ('. $lang .')';
550 }
551 }
99a9d8d9 552
598a82c9 553 core_collator::asort($languages);
99a9d8d9 554
598a82c9
PS
555 // Cache the list so that it can be used next time.
556 $this->menucache->set($cachekey, $languages);
99a9d8d9 557
598a82c9
PS
558 if ($returnall or empty($this->translist)) {
559 return $languages;
560 }
99a9d8d9 561
598a82c9
PS
562 $cachedlist = $languages;
563
564 // Return only enabled translations.
565 $languages = array();
566 foreach ($cachedlist as $langcode => $langname) {
567 if (isset($this->translist[$langcode])) {
568 $languages[$langcode] = $langname;
99a9d8d9
PS
569 }
570 }
571
99a9d8d9
PS
572 return $languages;
573 }
574
575 /**
576 * Returns localised list of currencies.
577 *
578 * @param string $lang moodle translation language, null means use current
579 * @return array currency code => localised currency name
580 */
581 public function get_list_of_currencies($lang = null) {
582 if ($lang === null) {
583 $lang = current_language();
584 }
585
586 $currencies = $this->load_component_strings('core_currencies', $lang);
587 asort($currencies);
588
589 return $currencies;
590 }
591
592 /**
593 * Clears both in-memory and on-disk caches
594 * @param bool $phpunitreset true means called from our PHPUnit integration test reset
595 */
596 public function reset_caches($phpunitreset = false) {
99a9d8d9
PS
597 // Clear the on-disk disk with aggregated string files.
598 $this->cache->purge();
598a82c9 599 $this->menucache->purge();
99a9d8d9
PS
600
601 if (!$phpunitreset) {
602 // Increment the revision counter.
603 $langrev = get_config('core', 'langrev');
604 $next = time();
605 if ($langrev !== false and $next <= $langrev and $langrev - $next < 60*60) {
606 // This resolves problems when reset is requested repeatedly within 1s,
607 // the < 1h condition prevents accidental switching to future dates
608 // because we might not recover from it.
609 $next = $langrev+1;
610 }
611 set_config('langrev', $next);
612 }
613
99a9d8d9
PS
614 // Lang packs use PHP files in dataroot, it is better to invalidate opcode caches.
615 if (function_exists('opcache_reset')) {
616 opcache_reset();
617 }
618 }
619
598a82c9
PS
620 /**
621 * Returns cache key suffix, this enables us to store string + lang menu
622 * caches in local caches on cluster nodes. We can not use prefix because
623 * it would cause problems when creating subdirs in cache file store.
624 * @return string
625 */
626 protected function get_key_suffix() {
627 $rev = $this->get_revision();
628 if ($rev < 0) {
629 // Simple keys do not like minus char.
630 $rev = 0;
631 }
632
633 return $rev;
634 }
635
99a9d8d9
PS
636 /**
637 * Returns string revision counter, this is incremented after any string cache reset.
638 * @return int lang string revision counter, -1 if unknown
639 */
640 public function get_revision() {
641 global $CFG;
598a82c9
PS
642 if (empty($CFG->langstringcache)) {
643 return -1;
644 }
99a9d8d9
PS
645 if (isset($CFG->langrev)) {
646 return (int)$CFG->langrev;
647 } else {
648 return -1;
649 }
650 }
651
99a9d8d9
PS
652 /**
653 * Helper method that recursively loads all parents of the given language.
654 *
655 * @see self::get_language_dependencies()
656 * @param string $lang language code
657 * @param array $stack list of parent languages already populated in previous recursive calls
658 * @return array list of all parents of the given language with the $lang itself added as the last element
659 */
660 protected function populate_parent_languages($lang, array $stack = array()) {
661
662 // English does not have a parent language.
663 if ($lang === 'en') {
664 return $stack;
665 }
666
667 // Prevent circular dependency (and thence the infinitive recursion loop).
668 if (in_array($lang, $stack)) {
669 return $stack;
670 }
671
672 // Load language configuration and look for the explicit parent language.
673 if (!file_exists("$this->otherroot/$lang/langconfig.php")) {
674 return $stack;
675 }
676 $string = array();
677 include("$this->otherroot/$lang/langconfig.php");
678
679 if (empty($string['parentlanguage']) or $string['parentlanguage'] === 'en') {
99a9d8d9
PS
680 return array_merge(array($lang), $stack);
681
99a9d8d9 682 }
598a82c9
PS
683
684 $parentlang = $string['parentlanguage'];
685 return $this->populate_parent_languages($parentlang, array_merge(array($lang), $stack));
99a9d8d9
PS
686 }
687}