list_unused_langvars.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. require_once api_get_path(SYS_CODE_PATH).'admin/sub_language.class.php';
  12. $path = api_get_path(SYS_LANG_PATH).'english';
  13. ini_set('memory_limit','128M');
  14. /**
  15. * Main code
  16. */
  17. $terms = array();
  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. $used_terms = array();
  32. $l = strlen(api_get_path(SYS_PATH));
  33. $files = get_all_php_files(api_get_path(SYS_PATH));
  34. // Browse files
  35. foreach ($files as $file) {
  36. //echo 'Analyzing '.$file."<br />";
  37. $shortfile = substr($file,$l);
  38. //echo 'Analyzing '.$shortfile."<br />";
  39. $lines = file($file);
  40. // Browse lines inside file $file
  41. foreach ($lines as $line) {
  42. $myterms = array();
  43. $res = preg_match_all('/get_lang\(\'(\\w*)\'\)/',$line,$myterms);
  44. if ($res > 0) {
  45. foreach($myterms[1] as $term) {
  46. if (substr($term,0,4)=='lang') { $term = substr($term,4); }
  47. $used_terms[$term] = $shortfile;
  48. }
  49. } else {
  50. $res = 0;
  51. $res = preg_match_all('/\{[\'"](\\w*)[\'"]\|get_lang\}/',$line,$myterms);
  52. if ($res > 0) {
  53. foreach($myterms[1] as $term) {
  54. if (substr($term,0,4)=='lang') { $term = substr($term,4); }
  55. $used_terms[$term] = $shortfile;
  56. }
  57. }
  58. }
  59. }
  60. flush();
  61. }
  62. // Compare defined terms VS used terms. Used terms should be smaller than
  63. // defined terms, and this should prove the concept that there are much
  64. // more variables than what we really use
  65. if (count($used_terms)<1) {
  66. die("No used terms<br />\n");
  67. } else {
  68. echo "The following terms were defined but never used: <br />\n<table>";
  69. }
  70. $i = 1;
  71. foreach ($defined_terms as $term => $file) {
  72. // remove "lang" prefix just in case
  73. if (substr($term,0,4)=='lang') { $term = substr($term,4); }
  74. if (!isset($used_terms[$term])) {
  75. echo "<tr><td>$i</td><td>$term</td></tr>\n";
  76. $i++;
  77. }
  78. }
  79. echo "</table>\n";
  80. function get_all_php_files($base_path) {
  81. $list = scandir($base_path);
  82. $files = array();
  83. foreach ($list as $item) {
  84. if (substr($item,0,1)=='.') {continue;}
  85. $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));
  86. if (in_array($base_path.$item.'/',$special_dirs)) {continue;}
  87. if (is_dir($base_path.$item)) {
  88. $files = array_merge($files,get_all_php_files($base_path.$item.'/'));
  89. } else {
  90. //only analyse php files
  91. $sub = substr($item,-4);
  92. if ($sub == '.php' or $sub == '.tpl') {
  93. $files[] = $base_path.$item;
  94. }
  95. }
  96. }
  97. $list = null;
  98. return $files;
  99. }