list_undefined_langvars.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php /* For licensing terms, see /license.txt */
  2. /**
  3. * Cron script to list used, but undefined, language variables
  4. * @package chamilo.cron
  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. // now get all terms found in all PHP files of Chamilo (this takes some time and memory)
  28. $undefined_terms = array();
  29. $l = strlen(api_get_path(SYS_PATH));
  30. $files = get_all_php_files(api_get_path(SYS_PATH));
  31. foreach ($files as $file) {
  32. //echo 'Analyzing '.$file."<br />";
  33. $shortfile = substr($file,$l);
  34. $lines = file($file);
  35. foreach ($lines as $line) {
  36. $myterms = array();
  37. $res = preg_match_all('/get_lang\(\'(\\w*)\'\)/',$line,$myterms);
  38. if ($res > 0) {
  39. foreach($myterms[1] as $term) {
  40. if (!isset($defined_terms[$term]) && !isset($defined_terms['lang'.$term])) {
  41. $undefined_terms[$term] = $shortfile;
  42. //echo "Undefined: $term<br />";
  43. }
  44. }
  45. }
  46. $res = 0;
  47. $res = preg_match_all('/\{[\'"](\\w*)[\'"]\|get_lang\}/',$line,$myterms);
  48. if ($res > 0) {
  49. foreach($myterms[1] as $term) {
  50. if (!isset($defined_terms[$term]) && !isset($defined_terms['lang'.$term])) {
  51. $undefined_terms[$term] = $shortfile;
  52. //echo "Undefined: $term<br />";
  53. }
  54. }
  55. }
  56. }
  57. flush();
  58. }
  59. //$undefined_terms = array_flip($undefined_terms);
  60. if (count($undefined_terms)<1) { die("No missing terms<br />\n"); } else { echo "The following terms were nowhere to be found: <br />\n<table>"; }
  61. foreach ($undefined_terms as $term => $file) {
  62. echo "<tr><td>$term</td><td>in $file";
  63. if (substr($file,0,7)=='plugin/') {
  64. echo " <span style=\"color: #00ff00;\">(this one should be taken care of by the plugin's language files)</span>";
  65. }
  66. echo "</td></tr>\n";
  67. }
  68. echo "</table>\n";
  69. function get_all_php_files($base_path) {
  70. $list = scandir($base_path);
  71. $files = array();
  72. foreach ($list as $item) {
  73. if (substr($item,0,1)=='.') {continue;}
  74. $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));
  75. if (in_array($base_path.$item.'/',$special_dirs)) {continue;}
  76. if (is_dir($base_path.$item)) {
  77. $files = array_merge($files,get_all_php_files($base_path.$item.'/'));
  78. } else {
  79. //only analyse php files
  80. $sub = substr($item,-4);
  81. if ($sub == '.php' or $sub == '.tpl') {
  82. $files[] = $base_path.$item;
  83. }
  84. }
  85. }
  86. $list = null;
  87. return $files;
  88. }