list_used_img.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if (PHP_SAPI!='cli') { die('Run this script through the command line or comment this line in the code'); }
  10. require_once '../../inc/global.inc.php';
  11. $path = api_get_path(SYS_CODE_PATH).'img/';
  12. ini_set('memory_limit','128M');
  13. ini_set('max_execution_time','240');
  14. /**
  15. * Main code
  16. */
  17. $terms = array();
  18. $found_img = get_img_files($path);
  19. // now get all terms found in all PHP files of Chamilo (this takes some time and memory)
  20. $unexisting_img = array();
  21. $l = strlen(api_get_path(SYS_PATH));
  22. $files = get_all_php_files(api_get_path(SYS_PATH));
  23. $counter = 0;
  24. foreach ($files as $file) {
  25. $shortfile = substr($file,$l);
  26. $lines = file($file);
  27. foreach ($lines as $line) {
  28. $res3 = preg_match_all('/([\w\d-_]+\.png)/',$line,$myterms3);
  29. $res4 = preg_match_all('/([\w\d-_]+\.jpg)/',$line,$myterms4);
  30. $res5 = preg_match_all('/([\w\d-_]+\.jpeg)/',$line,$myterms5);
  31. $res6 = preg_match_all('/([\w\d-_]+\.gif)/',$line,$myterms6);
  32. $myterms = array_merge($myterms3,$myterms4,$myterms5,$myterms6);
  33. if (count($myterms)>0) {
  34. foreach ($myterms as $mytermsentry) {
  35. if (count($mytermsentry)==0) { continue; }
  36. foreach ($mytermsentry as $term) {
  37. if (!isset($found_img[$term])) {
  38. $unexisting_img[$term] = $shortfile;
  39. } else {
  40. $used_icons[$term][] = $shortfile;
  41. }
  42. }
  43. }
  44. }
  45. }
  46. flush();
  47. $counter++;
  48. }
  49. echo '<table>';
  50. /*if (count($unexisting_img)<1) { die("No missing image<br />\n"); } else { echo "The following images were nowhere to be found: <br />\n<table>"; }
  51. foreach ($unexisting_img as $term => $file) {
  52. echo "<tr><td>$term</td><td>in $file</td></tr>\n";
  53. }*/
  54. echo '<tr><td colspan="2">Existing images('.count($found_img).'), used('.count($used_icons).') and unused</td></tr>'."\n";
  55. echo '<tr><td>Image file</td><td>Img path</td><td>Used in...</td></tr>'."\n";
  56. $r = ksort($found_img);
  57. foreach ($found_img as $term => $path) {
  58. if (isset($used_icons[$term])) {
  59. echo '<tr>';
  60. echo '<td bgcolor="#55ff55">'.$term.'</td>';
  61. echo '<td bgcolor="#55ff55">'.($path=='/'?'/':$path.'/').$term.'</td>';
  62. $st = '';
  63. foreach ($used_icons[$term] as $entry) {
  64. $st .= $entry."\n";
  65. }
  66. echo '<td bgcolor="#55ff55"><pre>'.$st.'</pre></td>';
  67. echo '</tr>'."\n";
  68. } else {
  69. echo '<tr>';
  70. echo '<td bgcolor="#ff5555">'.$term.'</td>';
  71. echo '<td bgcolor="#ff5555">'.($path=='/'?'/':$path.'/').$term.'</td>';
  72. echo '<td bgcolor="#ff5555">-</td>';
  73. echo '</tr>'."\n";
  74. }
  75. }
  76. echo "</table>\n";
  77. echo "Analysed files:<br />\n";
  78. print_r($files);
  79. function get_all_php_files($base_path) {
  80. $list = scandir($base_path);
  81. $files = array();
  82. foreach ($list as $item) {
  83. if (substr($item,0,1)=='.') {continue;}
  84. $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));
  85. if (in_array($base_path.$item.'/',$special_dirs)) {continue;}
  86. if (is_dir($base_path.$item)) {
  87. $files = array_merge($files,get_all_php_files($base_path.$item.'/'));
  88. } else {
  89. //only analyse php files
  90. $ext = substr($item,-4);
  91. if (in_array($ext,array('.php','html','.htm','.css'))) {
  92. $files[] = $base_path.$item;
  93. }
  94. }
  95. }
  96. $list = null;
  97. return $files;
  98. }
  99. function get_img_files($path) {
  100. $files = array();
  101. //We know there are max 3 levels
  102. $list = scandir($path);
  103. foreach ($list as $entry) {
  104. if (substr($entry,0,1)=='.') { continue; }
  105. if (is_dir($path.$entry)) {
  106. $sublist = scandir($path.$entry);
  107. foreach ($sublist as $subentry) {
  108. if (substr($subentry,0,1)=='.') { continue; }
  109. if (is_dir($path.$entry.'/'.$subentry)) {
  110. $subsublist = scandir($path.$entry.'/'.$subentry);
  111. foreach ($subsublist as $subsubentry) {
  112. if (substr($subsubentry,0,1)=='.') { continue; }
  113. if (is_file($path.$entry.'/'.$subentry.'/'.$subsubentry)) {
  114. $files[$subsubentry] = '/'.$entry.'/'.$subentry;
  115. }
  116. }
  117. } elseif (is_file($path.$entry.'/'.$subentry)) {
  118. $files[$subentry] = '/'.$entry;
  119. }
  120. }
  121. } elseif (is_file($path.$entry)) {
  122. $files[$entry] = '/';
  123. }
  124. }
  125. return $files;
  126. }