ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilObjLanguage.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once "./Services/Object/classes/class.ilObject.php";
5 
14 class ilObjLanguage extends ilObject
15 {
23  public $separator;
25  public $lang_default;
26  public $lang_user;
27  public $lang_path;
28 
29  public $key;
30  public $status;
31 
32 
40  public function __construct($a_id = 0, $a_call_by_reference = false)
41  {
42  global $DIC;
43  $lng = $DIC->language();
44 
45  $this->type = "lng";
46  parent::__construct($a_id, $a_call_by_reference);
47 
48  $this->type = "lng";
49  $this->key = $this->title;
50  $this->status = $this->desc;
51  $this->lang_default = $lng->lang_default;
52  $this->lang_user = $lng->lang_user;
53  $this->lang_path = $lng->lang_path;
54  $this->cust_lang_path = $lng->cust_lang_path;
55  $this->separator = $lng->separator;
56  $this->comment_separator = $lng->comment_separator;
57  }
58 
59 
64  public static function getInstalledLanguages()
65  {
66  $objects = array();
67  $languages = ilObject::_getObjectsByType("lng");
68  foreach ($languages as $lang) {
69  $langObj = new ilObjLanguage($lang["obj_id"], false);
70  if ($langObj->isInstalled()) {
71  $objects[] = $langObj;
72  } else {
73  unset($langObj);
74  }
75  }
76  return $objects;
77  }
78 
79 
85  public function getKey()
86  {
87  return $this->key;
88  }
89 
95  public function getStatus()
96  {
97  return $this->status;
98  }
99 
103  public function isSystemLanguage()
104  {
105  if ($this->key == $this->lang_default) {
106  return true;
107  } else {
108  return false;
109  }
110  }
111 
115  public function isUserLanguage()
116  {
117  if ($this->key == $this->lang_user) {
118  return true;
119  } else {
120  return false;
121  }
122  }
123 
124 
130  public function isInstalled()
131  {
132  if (substr($this->getStatus(), 0, 9) == "installed") {
133  return true;
134  } else {
135  return false;
136  }
137  }
138 
145  public function isLocal()
146  {
147  if (substr($this->getStatus(), 10) == "local") {
148  return true;
149  } else {
150  return false;
151  }
152  }
153 
160  public function install($scope = '')
161  {
162  if (!empty($scope)) {
163  if ($scope == 'global') {
164  $scope = '';
165  } else {
166  $scopeExtension = '.' . $scope;
167  }
168  }
169 
170  if (($this->isInstalled() == false) ||
171  ($this->isInstalled() == true && $this->isLocal() == false && !empty($scope))) {
172  if ($this->check($scope)) {
173  // lang-file is ok. Flush data in db and...
174  if (empty($scope)) {
175  $this->flush('keep_local');
176  }
177 
178  // ...re-insert data from lang-file
179  $this->insert($scope);
180 
181  // update information in db-table about available/installed languages
182  if (empty($scope)) {
183  $newDesc = 'installed';
184  } elseif ($scope == 'local') {
185  $newDesc = 'installed_local';
186  }
187  $this->setDescription($newDesc);
188  $this->update();
189  return $this->getKey();
190  }
191  }
192  return "";
193  }
194 
195 
201  public function uninstall()
202  {
203  if ((substr($this->status, 0, 9) == "installed") && ($this->key != $this->lang_default) && ($this->key != $this->lang_user)) {
204  $this->flush('all');
205  $this->setTitle($this->key);
206  $this->setDescription("not_installed");
207  $this->update();
208  $this->resetUserLanguage($this->key);
209 
210  return $this->key;
211  }
212  return "";
213  }
214 
215 
220  public function refresh()
221  {
222  if ($this->isInstalled() == true) {
223  if ($this->check()) {
224  $this->flush('keep_local');
225  $this->insert();
226  $this->setTitle($this->getKey());
227  $this->setDescription($this->getStatus());
228  $this->update();
229 
230  if ($this->isLocal() == true) {
231  if ($this->check('local')) {
232  $this->insert('local');
233  $this->setTitle($this->getKey());
234  $this->setDescription($this->getStatus());
235  $this->update();
236  }
237  }
238  return true;
239  }
240  }
241  return false;
242  }
243 
247  public static function refreshAll()
248  {
249  $languages = ilObject::_getObjectsByType("lng");
250  $refreshed = array();
251 
252  foreach ($languages as $lang) {
253  $langObj = new ilObjLanguage($lang["obj_id"], false);
254  if ($langObj->refresh()) {
255  $refreshed[] = $langObj->getKey();
256  }
257  unset($langObj);
258  }
259 
260  self::refreshPlugins($refreshed);
261  }
262 
263 
268  public static function refreshPlugins($a_lang_keys = null)
269  {
270  global $DIC;
271  $ilPluginAdmin = $DIC['ilPluginAdmin'];
272 
273  // refresh languages of activated plugins
274  include_once("./Services/Component/classes/class.ilPluginSlot.php");
275  $slots = ilPluginSlot::getAllSlots();
276  foreach ($slots as $slot) {
277  $act_plugins = $ilPluginAdmin->getActivePluginsForSlot(
278  $slot["component_type"],
279  $slot["component_name"],
280  $slot["slot_id"]
281  );
282  foreach ($act_plugins as $plugin) {
283  include_once("./Services/Component/classes/class.ilPlugin.php");
285  $slot["component_type"],
286  $slot["component_name"],
287  $slot["slot_id"],
288  $plugin
289  );
290  if (is_object($pl)) {
291  $pl->updateLanguages($a_lang_keys);
292  }
293  }
294  }
295  }
296 
297 
303  public static function _deleteLangData($a_lang_key, $a_keep_local_change = false)
304  {
305  global $DIC;
306  $ilDB = $DIC->database();
307 
308  if (!$a_keep_local_change) {
309  $ilDB->manipulate("DELETE FROM lng_data WHERE lang_key = " .
310  $ilDB->quote($a_lang_key, "text"));
311  } else {
312  $ilDB->manipulate("DELETE FROM lng_data WHERE lang_key = " .
313  $ilDB->quote($a_lang_key, "text") .
314  " AND local_change IS NULL");
315  }
316  }
317 
322  public function flush($a_mode = 'all')
323  {
324  global $DIC;
325  $ilDB = $DIC->database();
326 
327  ilObjLanguage::_deleteLangData($this->key, ($a_mode == 'keep_local'));
328 
329  if ($a_mode == 'all') {
330  $ilDB->manipulate("DELETE FROM lng_modules WHERE lang_key = " .
331  $ilDB->quote($this->key, "text"));
332  }
333  }
334 
335 
342  public function getLocalChanges($a_min_date = "", $a_max_date = "")
343  {
344  global $DIC;
345  $ilDB = $DIC->database();
346 
347  if ($a_min_date == "") {
348  $a_min_date = "1980-01-01 00:00:00";
349  }
350  if ($a_max_date == "") {
351  $a_max_date = "2200-01-01 00:00:00";
352  }
353 
354  $q = sprintf(
355  "SELECT * FROM lng_data WHERE lang_key = %s " .
356  "AND local_change >= %s AND local_change <= %s",
357  $ilDB->quote($this->key, "text"),
358  $ilDB->quote($a_min_date, "timestamp"),
359  $ilDB->quote($a_max_date, "timestamp")
360  );
361  $result = $ilDB->query($q);
362 
363  $changes = array();
364  while ($row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
365  $changes[$row["module"]][$row["identifier"]] = $row["value"];
366  }
367  return $changes;
368  }
369 
370 
376  public static function _getLastLocalChange($a_key)
377  {
378  global $DIC;
379  $ilDB = $DIC->database();
380 
381  $q = sprintf(
382  "SELECT MAX(local_change) last_change FROM lng_data " .
383  "WHERE lang_key = %s AND local_change IS NOT NULL",
384  $ilDB->quote($a_key, "text")
385  );
386  $result = $ilDB->query($q);
387 
388  if ($row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
389  return $row['last_change'];
390  } else {
391  return "";
392  }
393  }
394 
395 
402  public static function _getLocalChangesByModule($a_key, $a_module)
403  {
404  global $DIC;
405  $ilDB = $DIC->database();
406 
407  $changes = array();
408  $result = $ilDB->queryF(
409  "SELECT * FROM lng_data WHERE lang_key = %s AND module = %s AND local_change IS NOT NULL",
410  array('text', 'text'),
411  array($a_key, $a_module)
412  );
413 
414  while ($row = $ilDB->fetchAssoc($result)) {
415  $changes[$row['identifier']] = $row['value'];
416  }
417  return $changes;
418  }
419 
420 
426  public function insert($scope = '')
427  {
428  global $DIC;
429  $ilDB = $DIC->database();
430  $scopeExtension = "";
431  if (!empty($scope)) {
432  if ($scope == 'global') {
433  $scope = '';
434  } else {
435  $scopeExtension = '.' . $scope;
436  }
437  }
438 
439  $path = $this->lang_path;
440  if ($scope == "local") {
441  $path = $this->cust_lang_path;
442  }
443 
444  $lang_file = $path . "/ilias_" . $this->key . ".lang" . $scopeExtension;
445 
446  if (is_file($lang_file)) {
447  // initialize the array for updating lng_modules below
448  $lang_array = array();
449  $lang_array["common"] = array();
450 
451  // remove header first
452  if ($content = self::cut_header(file($lang_file))) {
453  $local_changes = null;
454  if (empty($scope)) {
455  // get all local changes for a global file
456  $local_changes = $this->getLocalChanges();
457  } elseif ($scope == 'local') {
458  // get the modification date of the local file
459  // get the newer local changes for a local file
460  $min_date = date("Y-m-d H:i:s", filemtime($lang_file));
461  $local_changes = $this->getLocalChanges($min_date);
462  }
463  $dbAccess = new ilObjLanguageDBAccess($ilDB, $this->key, $content, $local_changes, $scope);
464  $lang_array = $dbAccess->insertLangEntries($lang_file);
465  $dbAccess->replaceLangModules($lang_array);
466  }
467  }
468  }
469 
473  final public static function replaceLangModule($a_key, $a_module, $a_array)
474  {
475  global $DIC;
476  $ilDB = $DIC->database();
477 
478  // avoid flushing the whole cache (see mantis #28818)
479  ilCachedLanguage::getInstance($a_key)->deleteInCache();
480 
481  $ilDB->manipulate(sprintf(
482  "DELETE FROM lng_modules WHERE lang_key = %s AND module = %s",
483  $ilDB->quote($a_key, "text"),
484  $ilDB->quote($a_module, "text")
485  ));
486 
487  /*$ilDB->manipulate(sprintf("INSERT INTO lng_modules (lang_key, module, lang_array) VALUES ".
488  "(%s,%s,%s)", $ilDB->quote($a_key, "text"),
489  $ilDB->quote($a_module, "text"),
490  $ilDB->quote(serialize($a_array), "clob")));*/
491  $ilDB->insert("lng_modules", array(
492  "lang_key" => array("text", $a_key),
493  "module" => array("text", $a_module),
494  "lang_array" => array("clob", serialize((array) $a_array))
495  ));
496 
497  // check if the module is correctly saved
498  // see mantis #20046 and #19140
499  $result = $ilDB->queryF(
500  "SELECT lang_array FROM lng_modules WHERE lang_key = %s AND module = %s",
501  array('text','text'),
502  array($a_key, $a_module)
503  );
504  $row = $ilDB->fetchAssoc($result);
505 
506  $unserialied = unserialize($row['lang_array']);
507  if (!is_array($unserialied)) {
509  $ilErr = $DIC['ilErr'];
510  $ilErr->raiseError(
511  "Data for module '" . $a_module . "' of language '" . $a_key . "' is not correctly saved. " .
512  "Please check the collation of your database tables lng_data and lng_modules. It must be utf8_unicode_ci.",
513  $ilErr->MESSAGE
514  );
515  }
516  }
517 
521  final public static function replaceLangEntry(
522  $a_module,
523  $a_identifier,
524  $a_lang_key,
525  $a_value,
526  $a_local_change = null,
527  $a_remarks = null
528  ) {
529  global $DIC;
530  $ilDB = $DIC->database();
531 
532  // avoid a cache flush here (see mantis #28818)
533  // ilGlobalCache::flushAll();
534 
535  if (isset($a_remarks)) {
536  $a_remarks = substr($a_remarks, 0, 250);
537  }
538  if ($a_remarks == '') {
539  $a_remarks = null;
540  }
541 
542  if (isset($a_value)) {
543  $a_value = substr($a_value, 0, 4000);
544  }
545  if ($a_value == '') {
546  $a_value = null;
547  }
548 
549  $ilDB->replace(
550  'lng_data',
551  array(
552  'module' => array('text',$a_module),
553  'identifier' => array('text',$a_identifier),
554  'lang_key' => array('text',$a_lang_key)
555  ),
556  array(
557  'value' => array('text',$a_value),
558  'local_change' => array('timestamp',$a_local_change),
559  'remarks' => array('text', $a_remarks)
560  )
561  );
562  return true;
563 
564  /*
565  $ilDB->manipulate(sprintf("DELETE FROM lng_data WHERE module = %s AND ".
566  "identifier = %s AND lang_key = %s",
567  $ilDB->quote($a_module, "text"), $ilDB->quote($a_identifier, "text"),
568  $ilDB->quote($a_lang_key, "text")));
569 
570 
571  $ilDB->manipulate(sprintf("INSERT INTO lng_data " .
572  "(module, identifier, lang_key, value, local_change) " .
573  "VALUES (%s,%s,%s,%s,%s)",
574  $ilDB->quote($a_module, "text"), $ilDB->quote($a_identifier, "text"),
575  $ilDB->quote($a_lang_key, "text"), $ilDB->quote($a_value, "text"),
576  $ilDB->quote($a_local_change, "timestamp")));
577  */
578  }
579 
583  final public static function updateLangEntry(
584  $a_module,
585  $a_identifier,
586  $a_lang_key,
587  $a_value,
588  $a_local_change = null,
589  $a_remarks = null
590  ) {
591  global $DIC;
592  $ilDB = $DIC->database();
593 
594  if (isset($a_remarks)) {
595  $a_remarks = substr($a_remarks, 0, 250);
596  }
597  if ($a_remarks == '') {
598  unset($a_remarks);
599  }
600 
601  if (isset($a_value)) {
602  $a_value = substr($a_value, 0, 4000);
603  }
604  if ($a_value == '') {
605  unset($a_value);
606  }
607 
608  $ilDB->manipulate(sprintf(
609  "UPDATE lng_data " .
610  "SET value = %s, local_change = %s, remarks = %s " .
611  "WHERE module = %s AND identifier = %s AND lang_key = %s ",
612  $ilDB->quote($a_value, "text"),
613  $ilDB->quote($a_local_change, "timestamp"),
614  $ilDB->quote($a_remarks, "text"),
615  $ilDB->quote($a_module, "text"),
616  $ilDB->quote($a_identifier, "text"),
617  $ilDB->quote($a_lang_key, "text")
618  ));
619  }
620 
621 
625  final public static function deleteLangEntry($a_module, $a_identifier, $a_lang_key)
626  {
627  global $DIC;
628  $ilDB = $DIC->database();
629 
630  $ilDB->manipulate(sprintf(
631  "DELETE FROM lng_data " .
632  "WHERE module = %s AND identifier = %s AND lang_key = %s ",
633  $ilDB->quote($a_module, "text"),
634  $ilDB->quote($a_identifier, "text"),
635  $ilDB->quote($a_lang_key, "text")
636  ));
637 
638  return true;
639  }
640 
641 
648  public function resetUserLanguage($lang_key)
649  {
650  global $DIC;
651  $ilDB = $DIC->database();
652 
653  $query = "UPDATE usr_pref SET " .
654  "value = " . $ilDB->quote($this->lang_default, "text") . " " .
655  "WHERE keyword = " . $ilDB->quote('language', "text") . " " .
656  "AND value = " . $ilDB->quote($lang_key, "text");
657  $ilDB->manipulate($query);
658  }
659 
668  public static function cut_header($content)
669  {
670  foreach ($content as $key => $val) {
671  if (trim($val) == "<!-- language file start -->") {
672  return array_slice($content, $key + 1);
673  }
674  }
675 
676  return false;
677  }
678 
685  public function optimizeData()
686  {
687  // Mantis #22313: removed table optimization
688  return true;
689  }
690 
700  public function check($scope = '')
701  {
702  include_once("./Services/Utilities/classes/class.ilStr.php");
703 
704  if (!empty($scope)) {
705  if ($scope == 'global') {
706  $scope = '';
707  } else {
708  $scopeExtension = '.' . $scope;
709  }
710  }
711 
712  $path = $this->lang_path;
713  if ($scope == "local") {
714  $path = $this->cust_lang_path;
715  }
716 
717  $tmpPath = getcwd();
718 
719  // dir check
720  if (!is_dir($path)) {
721  $this->ilias->raiseError("Directory not found: " . $path, $this->ilias->error_obj->MESSAGE);
722  }
723 
724  chdir($path);
725 
726  // compute lang-file name format
727  $lang_file = "ilias_" . $this->key . ".lang" . $scopeExtension;
728 
729  // file check
730  if (!is_file($lang_file)) {
731  $this->ilias->raiseError("File not found: " . $lang_file, $this->ilias->error_obj->MESSAGE);
732  }
733 
734  // header check
735  $content = $this->cut_header(file($lang_file));
736  if ($content === false) {
737  $this->ilias->raiseError("Wrong Header in " . $lang_file, $this->ilias->error_obj->MESSAGE);
738  }
739 
740  // check (counting) elements of each lang-entry
741  $line = 0;
742  foreach ($content as $key => $val) {
743  $separated = explode($this->separator, trim($val));
744  $num = count($separated);
745  ++$n;
746  if ($num != 3) {
747  $line = $n + 36;
748  $this->ilias->raiseError("Wrong parameter count in " . $lang_file . " in line $line (Value: $val)! Please check your language file!", $this->ilias->error_obj->MESSAGE);
749  }
750  if (!ilStr::isUtf8($separated[2])) {
751  $this->ilias->raiseError("Non UTF8 character found in " . $lang_file . " in line $line (Value: $val)! Please check your language file!", $this->ilias->error_obj->MESSAGE);
752  }
753  }
754 
755  chdir($tmpPath);
756 
757  // no error occured
758  return true;
759  }
760 
764  public static function countUsers($a_lang)
765  {
766  global $DIC;
767  $ilDB = $DIC->database();
768  $lng = $DIC->language();
769 
770  $set = $ilDB->query("SELECT COUNT(*) cnt FROM usr_data ud JOIN usr_pref up" .
771  " ON ud.usr_id = up.usr_id " .
772  " WHERE up.value = " . $ilDB->quote($a_lang, "text") .
773  " AND up.keyword = " . $ilDB->quote("language", "text"));
774  $rec = $ilDB->fetchAssoc($set);
775 
776  // add users with no usr_pref set to default language
777  if ($a_lang == $lng->lang_default) {
778  $set2 = $ilDB->query("SELECT COUNT(*) cnt FROM usr_data ud LEFT JOIN usr_pref up" .
779  " ON (ud.usr_id = up.usr_id AND up.keyword = " . $ilDB->quote("language", "text") . ")" .
780  " WHERE up.value IS NULL ");
781  $rec2 = $ilDB->fetchAssoc($set2);
782  }
783 
784  return (int) $rec["cnt"] + (int) $rec2["cnt"];
785  }
786 } // END class.LanguageObject
optimizeData()
optimizes the db-table langdata
static getPluginObject(string $a_ctype, string $a_cname, string $a_slot_id, string $a_pname)
isUserLanguage()
check if language is system language
$result
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilObjLanguage.
static getInstalledLanguages()
Get the language objects of the installed languages.
static _getObjectsByType($a_obj_type="", $a_owner="")
Get objects by type.
static countUsers($a_lang)
Count number of users that use a language.
isInstalled()
Check language object status, and return true if language is installed.
insert($scope='')
insert language data from file into database
__construct($a_id=0, $a_call_by_reference=false)
Constructor.
$ilErr
Definition: raiseError.php:18
setTitle($a_title)
set object title
getStatus()
get language status
uninstall()
uninstall current language
check($scope='')
Validate the logical structure of a lang file.
static _getLocalChangesByModule($a_key, $a_module)
Get the local changes of a language module.
static _deleteLangData($a_lang_key, $a_keep_local_change=false)
Delete languge data.
getLocalChanges($a_min_date="", $a_max_date="")
get locally changed language entries
isSystemLanguage()
check if language is system language
global $DIC
Definition: goto.php:24
static deleteLangEntry($a_module, $a_identifier, $a_lang_key)
Delete lang entry.
refresh()
refresh current language
resetUserLanguage($lang_key)
search ILIAS for users which have selected &#39;$lang_key&#39; as their prefered language and reset them to d...
redirection script todo: (a better solution should control the processing via a xml file) ...
$query
$n
Definition: RandomTest.php:85
install($scope='')
install current language
$lang
Definition: xapiexit.php:8
flush($a_mode='all')
remove language data from database
__construct(Container $dic, ilPlugin $plugin)
global $ilDB
static refreshAll()
Refresh all installed languages.
isLocal()
Check language object status, and return true if a local language file is installed.
setDescription($a_desc)
set object description
static updateLangEntry( $a_module, $a_identifier, $a_lang_key, $a_value, $a_local_change=null, $a_remarks=null)
Replace lang entry.
update()
update object in db
static _getLastLocalChange($a_key)
get the date of the last local change
static refreshPlugins($a_lang_keys=null)
static replaceLangEntry( $a_module, $a_identifier, $a_lang_key, $a_value, $a_local_change=null, $a_remarks=null)
Replace lang entry.
static getAllSlots()
Get all plugin slots.
static cut_header($content)
remove lang-file haeder information from &#39;$content&#39; This function seeks for a special keyword where t...
static isUtf8($a_str)
Check whether string is utf-8.
getKey()
get language key