list_unused_langvars.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php /* For licensing terms, see /license.txt */
  2. /**
  3. * Cron script to list unused, but defined, language variables.
  4. *
  5. * @package chamilo.cron.lang
  6. */
  7. /**
  8. * Includes and declarations.
  9. */
  10. die();
  11. require_once __DIR__.'/../../inc/global.inc.php';
  12. $path = api_get_path(SYS_LANG_PATH).'english';
  13. ini_set('memory_limit', '128M');
  14. /**
  15. * Main code.
  16. */
  17. $terms = [];
  18. $list = SubLanguageManager::get_lang_folder_files_list($path);
  19. foreach ($list as $entry) {
  20. $file = $path.'/'.$entry;
  21. if (is_file($file)) {
  22. $terms = array_merge($terms, SubLanguageManager::get_all_language_variable_in_file($file, true));
  23. }
  24. }
  25. // get only the array keys (the language variables defined in language files)
  26. $defined_terms = array_flip(array_keys($terms));
  27. $terms = null;
  28. echo count($defined_terms)." terms were found in language files<br />";
  29. // now get all terms found in all PHP files of Chamilo (this takes some
  30. // time and memory)
  31. $usedTerms = [];
  32. $l = strlen(api_get_path(SYS_PATH));
  33. $files = getAllPhpFiles(api_get_path(SYS_PATH));
  34. $files[] = api_get_path(SYS_PATH).'main/install/data.sql';
  35. // Browse files
  36. foreach ($files as $file) {
  37. //echo 'Analyzing '.$file."<br />";
  38. $shortFile = substr($file, $l);
  39. //echo 'Analyzing '.$shortFile."<br />";
  40. $lines = file($file);
  41. $isDataSQL = false;
  42. if (substr($file, -21) === 'main/install/data.sql') {
  43. $isDataSQL = true;
  44. }
  45. // Browse lines inside file $file
  46. foreach ($lines as $line) {
  47. if ($isDataSQL) {
  48. // Check main/install/data.sql
  49. // Should recognize stuff like
  50. // INSERT INTO settings_current (variable, type, category, selected_value, title, comment) VALUES ('enable_profile_user_address_geolocalization', 'radio', 'User', 'false', 'EnableProfileUsersAddressGeolocalizationTitle', 'EnableProfileUsersAddressGeolocalizationComment');
  51. // INSERT INTO settings_options (variable, value, display_text) VALUES ('enable_profile_user_address_geolocalization', 'true', 'Yes');
  52. // ('show_teacher_data',NULL,'radio','Platform','true','ShowTeacherDataTitle','ShowTeacherDataComment',NULL,NULL, 1),
  53. $res = 0;
  54. $myTerms = [];
  55. $res = preg_match_all('/\'(\w*)\',/', $line, $myTerms);
  56. if ($res > 0) {
  57. foreach ($myTerms[1] as $term) {
  58. if (substr($term, 0, 4) == 'lang') {
  59. $term = substr($term, 4);
  60. }
  61. $usedTerms[$term] = $shortFile;
  62. }
  63. }
  64. } else {
  65. $myTerms = [];
  66. $res = preg_match_all('/get_lang\(\'(\\w*)\'\)/', $line, $myTerms);
  67. if ($res > 0) {
  68. foreach ($myTerms[1] as $term) {
  69. if (substr($term, 0, 4) == 'lang') {
  70. $term = substr($term, 4);
  71. }
  72. $usedTerms[$term] = $shortFile;
  73. }
  74. } else {
  75. $res = 0;
  76. $myTerms = [];
  77. // Should catch:
  78. // {{ 'CopyTextToClipboard' | get_lang }}
  79. // {{ "HelloX" | get_lang | format(show_user_info.user_info.complete_name) }}
  80. // {{ "StudentCourseProgressX" | get_lang | format(item.student_info.progress) }}
  81. $res = preg_match_all('/\{\s*[\'"](\w*)[\'"]\s*\|\s*get_lang\s*(\|\s*\w*(\s*\([\w_\.,\s]*\))?\s*)?\}/', $line, $myTerms);
  82. if ($res > 0) {
  83. foreach ($myTerms[1] as $term) {
  84. if (substr($term, 0, 4) == 'lang') {
  85. $term = substr($term, 4);
  86. }
  87. $usedTerms[$term] = $shortFile;
  88. }
  89. }
  90. // {{ display.panel('PersonalDataResponsibleOrganizationTitle' | get_lang , personal_data.responsible ) }}
  91. // {{ display.panel('PersonalDataIntroductionTitle' | get_lang , 'PersonalDataIntroductionText' | get_lang) }}
  92. $myTerms = [];
  93. $res = preg_match_all('/\{\s*[\w\.]*\([\'"](\w*)[\'"]\s*\|\s*get_lang\s*(,\s*[\w_\.,\s\|\'"]*\s*)?\)\s*\}/', $line, $myTerms);
  94. if ($res > 0) {
  95. foreach ($myTerms[1] as $term) {
  96. if (substr($term, 0, 4) == 'lang') {
  97. $term = substr($term, 4);
  98. }
  99. $usedTerms[$term] = $shortFile;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. flush();
  106. }
  107. // Compare defined terms VS used terms. Used terms should be smaller than
  108. // defined terms, and this should prove the concept that there are much
  109. // more variables than what we really use
  110. if (count($usedTerms) < 1) {
  111. die("No used terms<br />\n");
  112. } else {
  113. echo "The following terms were defined but never used: <br />\n<table>";
  114. }
  115. $i = 1;
  116. foreach ($defined_terms as $term => $file) {
  117. // remove "lang" prefix just in case
  118. if (substr($term, 0, 4) == 'lang') {
  119. $term = substr($term, 4);
  120. }
  121. if (!isset($usedTerms[$term])) {
  122. echo "<tr><td>$i</td><td>$term</td></tr>\n";
  123. $i++;
  124. }
  125. }
  126. echo "</table>\n";