ILIAS  release_8 Revision v8.24
class.ilObjLanguageExt.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
30{
35 public function getGlobalLanguageFile(): object
36 {
37 require_once "./Services/Language/classes/class.ilLanguageFile.php";
39 }
40
46 public function setLocal(bool $a_local = true): void
47 {
48 if ($this->isInstalled()) {
49 if ($a_local) {
50 $this->setDescription("installed_local");
51 } else {
52 $this->setDescription("installed");
53 }
54 $this->update();
55 }
56 }
57
58
64 public function getLongDescription(): string
65 {
66 return $this->lng->txt($this->desc);
67 }
68
69
73 public function getDataPath(): string
74 {
75 if (!is_dir(CLIENT_DATA_DIR . "/lang_data")) {
77 }
78 return CLIENT_DATA_DIR . "/lang_data";
79 }
80
86 public function getLangPath(): string
87 {
88 return $this->lang_path;
89 }
90
96 public function getCustLangPath(): string
97 {
99 }
100
106 public function getAllRemarks(): array
107 {
108 return self::_getRemarks($this->key);
109 }
110
119 public function getAllValues(array $a_modules = array(), string $a_pattern = "", array $a_topics = array()): array
120 {
121 return self::_getValues($this->key, $a_modules, $a_topics, $a_pattern);
122 }
123
124
134 public function getChangedValues(array $a_modules = array(), string $a_pattern = "", array $a_topics = array()): array
135 {
136 return self::_getValues($this->key, $a_modules, $a_topics, $a_pattern, "changed");
137 }
138
139
146 public function getUnchangedValues(array $a_modules = array(), string $a_pattern = "", array $a_topics = array()): array
147 {
148 return self::_getValues($this->key, $a_modules, $a_topics, $a_pattern, "unchanged");
149 }
150
159 public function getAddedValues(array $a_modules = array(), string $a_pattern = '', array $a_topics = array()): array
160 {
161 $global_file_obj = $this->getGlobalLanguageFile();
162 $global_values = $global_file_obj->getAllValues();
163 $local_values = self::_getValues($this->key, $a_modules, $a_topics, $a_pattern);
164
165 return array_diff_key($local_values, $global_values);
166 }
167
168
180 public function getCommentedValues(array $a_modules = array(), string $a_pattern = "", array $a_topics = array()): array
181 {
182 $global_file_obj = $this->getGlobalLanguageFile();
183 $global_comments = $global_file_obj->getAllComments();
184 $local_values = self::_getValues($this->key, $a_modules, $a_topics, $a_pattern);
185
186 return array_intersect_key($local_values, $global_comments);
187 }
188
189
201 public function getMergedValues(): array
202 {
203 $global_file_obj = $this->getGlobalLanguageFile();
204 $global_values = $global_file_obj->getAllValues();
205 $local_values = self::_getValues($this->key);
206
207 return array_merge($global_values, $local_values);
208 }
209
221 public function getMergedRemarks(): array
222 {
223 $global_file_obj = $this->getGlobalLanguageFile();
224 $global_comments = $global_file_obj->getAllComments();
225
226 // get remarks including empty remarks for local changes
227 $local_remarks = self::_getRemarks($this->key, true);
228
229 return array_merge($global_comments, $local_remarks);
230 }
231
238 public function importLanguageFile(string $a_file, string $a_mode_existing = "keepnew"): void
239 {
240 global $DIC;
241 $ilDB = $DIC->database();
243 $ilErr = $DIC["ilErr"];
244
245 // read the new language file
246 require_once "./Services/Language/classes/class.ilLanguageFile.php";
247 $import_file_obj = new ilLanguageFile($a_file);
248 if (!$import_file_obj->read()) {
249 $ilErr->raiseError($import_file_obj->getErrorMessage(), $ilErr->MESSAGE);
250 }
251
252 switch ($a_mode_existing) {
253 // keep all existing entries
254 case "keepall":
255 $to_keep = $this->getAllValues();
256 break;
257
258 // keep existing online changes
259 case "keepnew":
260 $to_keep = $this->getChangedValues();
261 break;
262
263 // replace all existing definitions
264 case "replace":
265 $to_keep = array();
266 break;
267
268 // delete all existing entries
269 case "delete":
270 ilObjLanguage::_deleteLangData($this->key, false);
271 $ilDB->manipulate("DELETE FROM lng_modules WHERE lang_key = " .
272 $ilDB->quote($this->key, "text"));
273 $to_keep = array();
274 break;
275
276 default:
277 return;
278 }
279
280 // process the values of the import file
281 $to_save = array();
282 foreach ($import_file_obj->getAllValues() as $key => $value) {
283 if (!isset($to_keep[$key])) {
284 $to_save[$key] = $value;
285 }
286 }
287 self::_saveValues($this->key, $to_save, $import_file_obj->getAllComments());
288 }
289
296 public static function _getModules(string $a_lang_key): array
297 {
298 global $DIC;
299 $ilDB = $DIC->database();
300
301 $q = "SELECT DISTINCT module FROM lng_data WHERE " .
302 " lang_key = " . $ilDB->quote($a_lang_key, "text") . " order by module";
303 $set = $ilDB->query($q);
304
305 $modules = array();
306 while ($rec = $set->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
307 $modules[] = $rec["module"];
308 }
309 return $modules;
310 }
311
312
320 public static function _getRemarks(string $a_lang_key, bool $a_all_changed = false): array
321 {
322 global $DIC;
323 $ilDB = $DIC->database();
324 $lng = $DIC->language();
325
326 $q = "SELECT module, identifier, remarks"
327 . " FROM lng_data"
328 . " WHERE lang_key = " . $ilDB->quote($a_lang_key, "text");
329
330 if ($a_all_changed) {
331 $q .= " AND (remarks IS NOT NULL OR local_change IS NOT NULL)";
332 } else {
333 $q .= " AND remarks IS NOT NULL";
334 }
335
336 $result = $ilDB->query($q);
337
338 $remarks = array();
339 while ($row = $ilDB->fetchAssoc($result)) {
340 $remarks[$row["module"] . $lng->separator . $row["identifier"]] = $row["remarks"];
341 }
342 return $remarks;
343 }
344
345
356 public static function _getValues(
357 string $a_lang_key,
358 array $a_modules = array(),
359 array $a_topics = array(),
360 string $a_pattern = '',
361 string $a_state = ''
362 ): array {
363 global $DIC;
364 $ilDB = $DIC->database();
365 $lng = $DIC->language();
366
367 $q = "SELECT * FROM lng_data WHERE" .
368 " lang_key = " . $ilDB->quote($a_lang_key, "text") . " ";
369
370 if (is_array($a_modules) && count($a_modules) > 0) {
371 $q .= " AND " . $ilDB->in("module", $a_modules, false, "text");
372 }
373 if (is_array($a_topics) && count($a_topics) > 0) {
374 $q .= " AND " . $ilDB->in("identifier", $a_topics, false, "text");
375 }
376 if ($a_pattern) {
377 $q .= " AND " . $ilDB->like("value", "text", "%" . $a_pattern . "%");
378 }
379 if ($a_state === "changed") {
380 $q .= " AND NOT local_change IS NULL ";
381 }
382 if ($a_state === "unchanged") {
383 $q .= " AND local_change IS NULL ";
384 }
385 $q .= " ORDER BY module, identifier";
386
387 $set = $ilDB->query($q);
388
389 $values = array();
390 while ($rec = $set->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
391 $values[$rec["module"] . $lng->separator . $rec["identifier"]] = $rec["value"];
392 }
393 return $values;
394 }
395
403 public static function _saveValues(string $a_lang_key, array $a_values = array(), array $a_remarks = array()): void
404 {
405 global $DIC;
406 $ilDB = $DIC->database();
407 $lng = $DIC->language();
408
409 if (!is_array($a_values)) {
410 return;
411 }
412 $save_array = [];
413 $save_date = (new DateTime())->format("Y-m-d H:i:s");
414
415 // read and get the global values
416 $global_file_obj = ilLanguageFile::_getGlobalLanguageFile($a_lang_key);
417 $file_values = $global_file_obj->getAllValues();
418 $file_comments = $global_file_obj->getAllComments();
419 $db_values = self::_getValues($a_lang_key);
420 $db_comments = self::_getRemarks($a_lang_key);
421 $global_values = array_merge($db_values, $file_values);
422 $global_comments = array_merge($db_comments, $file_comments);
423
424 // save the single translations in lng_data
425 foreach ($a_values as $key => $value) {
426 $keys = explode($lng->separator, $key);
427
428 if (count($keys) !== 2) {
429 continue;
430 }
431
432 list($module, $topic) = $keys;
433 $save_array[$module][$topic] = $value;
434
435 $are_comments_set = array_key_exists($key, $global_comments) && array_key_exists($key, $a_remarks);
436 $are_changes_made = (isset($global_values[$key]) ? $global_values[$key] != $value : true) || (isset($db_values[$key]) ? $db_values[$key] != $value : true);
437 if ($are_changes_made || ($are_comments_set ? $global_comments[$key] != $a_remarks[$key] : $are_comments_set)) {
438 $local_change = (isset($db_values[$key]) ? $db_values[$key] == $value : true) || (isset($global_values[$key]) ? $global_values[$key] != $value : true) ? $save_date : null;
440 $module,
441 $topic,
442 $a_lang_key,
443 $value,
444 $local_change,
445 $a_remarks[$key] ?? null
446 );
447 }
448 }
449
450 // save the serialized module entries in lng_modules
451 foreach ($save_array as $module => $entries) {
452 $set = $ilDB->query(sprintf(
453 "SELECT * FROM lng_modules " .
454 "WHERE lang_key = %s AND module = %s",
455 $ilDB->quote($a_lang_key, "text"),
456 $ilDB->quote($module, "text")
457 ));
458 $row = $ilDB->fetchAssoc($set);
459
460 $arr = isset($row["lang_array"]) ? unserialize($row["lang_array"], ["allowed_classes" => false]) : "";
461 if (is_array($arr)) {
462 $entries = array_merge($arr, $entries);
463 }
464 ilObjLanguage::replaceLangModule($a_lang_key, $module, $entries);
465 }
466
467 require_once("class.ilCachedLanguage.php");
468 ilCachedLanguage::getInstance($a_lang_key)->flush();
469 }
470
477 public static function _deleteValues(string $a_lang_key, array $a_values = array()): void
478 {
479 global $DIC;
480 $ilDB = $DIC->database();
481 $lng = $DIC->language();
482
483 if (!is_array($a_values)) {
484 return;
485 }
486 $delete_array = array();
487
488 // save the single translations in lng_data
489 foreach ($a_values as $key => $value) {
490 $keys = explode($lng->separator, $key);
491 if (count($keys) === 2) {
492 $module = $keys[0];
493 $topic = $keys[1];
494 $delete_array[$module][$topic] = $value;
495
496 ilObjLanguage::deleteLangEntry($module, $topic, $a_lang_key);
497 }
498 }
499
500 // save the serialized module entries in lng_modules
501 foreach ($delete_array as $module => $entries) {
502 $set = $ilDB->query(sprintf(
503 "SELECT * FROM lng_modules " .
504 "WHERE lang_key = %s AND module = %s",
505 $ilDB->quote($a_lang_key, "text"),
506 $ilDB->quote($module, "text")
507 ));
508 $row = $ilDB->fetchAssoc($set);
509
510 $arr = unserialize($row["lang_array"], ["allowed_classes" => false]);
511 if (is_array($arr)) {
512 $entries = array_diff_key($arr, $entries);
513 }
514 ilObjLanguage::replaceLangModule($a_lang_key, $module, $entries);
515 }
516 }
517} // END class.ilObjLanguageExt
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
Class ilLanguageFile.
static _getGlobalLanguageFile(string $a_lang_key)
Read and get a global language file as a singleton object $a_lang_key language key.
Class ilObjLanguageExt.
getGlobalLanguageFile()
Read and get the global language file as an object.
setLocal(bool $a_local=true)
Set the local status of the language.
static _deleteValues(string $a_lang_key, array $a_values=array())
Delete a set of translation in the database.
getChangedValues(array $a_modules=array(), string $a_pattern="", array $a_topics=array())
Get only the changed values from the database which differ from the original language file.
static _getModules(string $a_lang_key)
Get all modules of a language.
getCommentedValues(array $a_modules=array(), string $a_pattern="", array $a_topics=array())
Get all values from the database for wich the global language file has a comment.
static _saveValues(string $a_lang_key, array $a_values=array(), array $a_remarks=array())
Save a set of translation in the database.
getAllValues(array $a_modules=array(), string $a_pattern="", array $a_topics=array())
Get all values from the database.
getLongDescription()
Get the full language description.
getMergedValues()
Get the local values merged into the values of the global language file.
getCustLangPath()
Get the customized language files path.
getDataPath()
Return the path for language data written by ILIAS.
getUnchangedValues(array $a_modules=array(), string $a_pattern="", array $a_topics=array())
Get only the unchanged values from the database which are equal to the original language file.
getAddedValues(array $a_modules=array(), string $a_pattern='', array $a_topics=array())
Get only the entries which don't exist in the global language file.
static _getRemarks(string $a_lang_key, bool $a_all_changed=false)
Get all remarks of a language.
static _getValues(string $a_lang_key, array $a_modules=array(), array $a_topics=array(), string $a_pattern='', string $a_state='')
Get the translations of specified topics.
getLangPath()
Get the language files path.
getMergedRemarks()
Get the local remarks merged into the remarks of the global language file.
getAllRemarks()
Get all remarks from the database.
Class ilObjLanguage.
static _deleteLangData(string $a_lang_key, bool $a_keep_local_change=false)
Delete languge data $a_lang_key lang key.
static deleteLangEntry(string $a_module, string $a_identifier, string $a_lang_key)
Delete lang entry.
isInstalled()
Check language object status, and return true if language is installed.
static replaceLangEntry(string $a_module, string $a_identifier, string $a_lang_key, string $a_value, string $a_local_change=null, string $a_remarks=null)
Replace lang entry.
ilLanguage $lng
setDescription(string $desc)
const CLIENT_DATA_DIR
Definition: constants.php:46
global $DIC
Definition: feed.php:28
$keys
Definition: metadata.php:204
string $key
Consumer key/client ID value.
Definition: System.php:193
$ilErr
Definition: raiseError.php:17
$lng