list_undefined_langvars.php 3.2 KB

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