list_unused_langvars.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php /* For licensing terms, see /license.txt */
  2. /**
  3. * Cron script to list unused, but defined, language variables
  4. * @package chamilo.cron.lang
  5. */
  6. /**
  7. * Includes and declarations
  8. */
  9. die();
  10. require_once '../../inc/global.inc.php';
  11. $path = api_get_path(SYS_LANG_PATH).'english';
  12. ini_set('memory_limit', '128M');
  13. /**
  14. * Main code
  15. */
  16. $terms = array();
  17. $list = SubLanguageManager::get_lang_folder_files_list($path);
  18. foreach ($list as $entry) {
  19. $file = $path.'/'.$entry;
  20. if (is_file($file)) {
  21. $terms = array_merge($terms, SubLanguageManager::get_all_language_variable_in_file($file, true));
  22. }
  23. }
  24. // get only the array keys (the language variables defined in language files)
  25. $defined_terms = array_flip(array_keys($terms));
  26. $terms = null;
  27. echo count($defined_terms)." terms were found in language files<br />";
  28. // now get all terms found in all PHP files of Chamilo (this takes some
  29. // time and memory)
  30. $usedTerms = array();
  31. $l = strlen(api_get_path(SYS_PATH));
  32. $files = get_all_php_files(api_get_path(SYS_PATH));
  33. // Browse files
  34. foreach ($files as $file) {
  35. //echo 'Analyzing '.$file."<br />";
  36. $shortFile = substr($file, $l);
  37. //echo 'Analyzing '.$shortFile."<br />";
  38. $lines = file($file);
  39. // Browse lines inside file $file
  40. foreach ($lines as $line) {
  41. $myTerms = array();
  42. $res = preg_match_all('/get_lang\(\'(\\w*)\'\)/', $line, $myTerms);
  43. if ($res > 0) {
  44. foreach ($myTerms[1] as $term) {
  45. if (substr($term, 0, 4) == 'lang') {
  46. $term = substr($term, 4);
  47. }
  48. $usedTerms[$term] = $shortFile;
  49. }
  50. } else {
  51. $res = 0;
  52. $res = preg_match_all('/\{[\'"](\\w*)[\'"]\|get_lang\}/', $line, $myTerms);
  53. if ($res > 0) {
  54. foreach ($myTerms[1] as $term) {
  55. if (substr($term, 0, 4) == 'lang') {
  56. $term = substr($term, 4);
  57. }
  58. $usedTerms[$term] = $shortFile;
  59. }
  60. }
  61. }
  62. }
  63. flush();
  64. }
  65. // Compare defined terms VS used terms. Used terms should be smaller than
  66. // defined terms, and this should prove the concept that there are much
  67. // more variables than what we really use
  68. if (count($usedTerms) < 1) {
  69. die("No used terms<br />\n");
  70. } else {
  71. echo "The following terms were defined but never used: <br />\n<table>";
  72. }
  73. $i = 1;
  74. foreach ($defined_terms as $term => $file) {
  75. // remove "lang" prefix just in case
  76. if (substr($term, 0, 4) == 'lang') { $term = substr($term, 4); }
  77. if (!isset($usedTerms[$term])) {
  78. echo "<tr><td>$i</td><td>$term</td></tr>\n";
  79. $i++;
  80. }
  81. }
  82. echo "</table>\n";
  83. function get_all_php_files($base_path) {
  84. $list = scandir($base_path);
  85. $files = array();
  86. foreach ($list as $item) {
  87. if (substr($item, 0, 1) == '.') {continue; }
  88. $special_dirs = array(api_get_path(SYS_TEST_PATH), api_get_path(SYS_COURSE_PATH), api_get_path(SYS_LANG_PATH), api_get_path(SYS_ARCHIVE_PATH));
  89. if (in_array($base_path.$item.'/', $special_dirs)) {continue; }
  90. if (is_dir($base_path.$item)) {
  91. $files = array_merge($files, get_all_php_files($base_path.$item.'/'));
  92. } else {
  93. //only analyse php files
  94. $sub = substr($item, -4);
  95. if ($sub == '.php' or $sub == '.tpl') {
  96. $files[] = $base_path.$item;
  97. }
  98. }
  99. }
  100. $list = null;
  101. return $files;
  102. }