list_used_img.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 __DIR__.'/../../../main/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 = getAllPhpFiles(api_get_path(SYS_PATH), true);
  23. $counter = 0;
  24. $used_icons = [];
  25. foreach ($files as $file) {
  26. $shortfile = substr($file,$l);
  27. $lines = file($file);
  28. foreach ($lines as $line) {
  29. $res3 = preg_match_all('/([\w\d-_]+\.png)/',$line,$myterms3);
  30. $res4 = preg_match_all('/([\w\d-_]+\.jpg)/',$line,$myterms4);
  31. $res5 = preg_match_all('/([\w\d-_]+\.jpeg)/',$line,$myterms5);
  32. $res6 = preg_match_all('/([\w\d-_]+\.gif)/',$line,$myterms6);
  33. $myterms = array_merge($myterms3,$myterms4,$myterms5,$myterms6);
  34. if (count($myterms)>0) {
  35. foreach ($myterms as $mytermsentry) {
  36. if (count($mytermsentry)==0) { continue; }
  37. foreach ($mytermsentry as $term) {
  38. if (!isset($found_img[$term])) {
  39. $unexisting_img[$term] = $shortfile;
  40. } else {
  41. $used_icons[$term][] = $shortfile;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. flush();
  48. $counter++;
  49. }
  50. echo '<table>';
  51. /*if (count($unexisting_img)<1) { die("No missing image<br />\n"); } else { echo "The following images were nowhere to be found: <br />\n<table>"; }
  52. foreach ($unexisting_img as $term => $file) {
  53. echo "<tr><td>$term</td><td>in $file</td></tr>\n";
  54. }*/
  55. echo '<tr><td colspan="2">Existing images('.count($found_img).'), used('.count($used_icons).') and unused</td></tr>'."\n";
  56. echo '<tr><td>Image file</td><td>Img path</td><td>Used in...</td></tr>'."\n";
  57. $r = ksort($found_img);
  58. foreach ($found_img as $term => $path) {
  59. if (isset($used_icons[$term])) {
  60. echo '<tr>';
  61. echo '<td bgcolor="#55ff55">'.$term.'</td>';
  62. echo '<td bgcolor="#55ff55">'.($path=='/'?'/':$path.'/').$term.'</td>';
  63. $st = '';
  64. foreach ($used_icons[$term] as $entry) {
  65. $st .= $entry."\n";
  66. }
  67. echo '<td bgcolor="#55ff55"><pre>'.$st.'</pre></td>';
  68. echo '</tr>'."\n";
  69. } else {
  70. echo '<tr>';
  71. echo '<td bgcolor="#ff5555">'.$term.'</td>';
  72. echo '<td bgcolor="#ff5555">'.($path=='/'?'/':$path.'/').$term.'</td>';
  73. echo '<td bgcolor="#ff5555">-</td>';
  74. echo '</tr>'."\n";
  75. }
  76. }
  77. echo "</table>\n";
  78. echo "Analysed files:<br />\n";
  79. print_r($files);
  80. function get_img_files($path) {
  81. $files = array();
  82. //We know there are max 3 levels
  83. $list = scandir($path);
  84. foreach ($list as $entry) {
  85. if (substr($entry,0,1)=='.') { continue; }
  86. if (is_dir($path.$entry)) {
  87. $sublist = scandir($path.$entry);
  88. foreach ($sublist as $subentry) {
  89. if (substr($subentry,0,1)=='.') { continue; }
  90. if (is_dir($path.$entry.'/'.$subentry)) {
  91. $subsublist = scandir($path.$entry.'/'.$subentry);
  92. foreach ($subsublist as $subsubentry) {
  93. if (substr($subsubentry,0,1)=='.') { continue; }
  94. if (is_file($path.$entry.'/'.$subentry.'/'.$subsubentry)) {
  95. $files[$subsubentry] = '/'.$entry.'/'.$subentry;
  96. }
  97. }
  98. } elseif (is_file($path.$entry.'/'.$subentry)) {
  99. $files[$subentry] = '/'.$entry;
  100. }
  101. }
  102. } elseif (is_file($path.$entry)) {
  103. $files[$entry] = '/';
  104. }
  105. }
  106. return $files;
  107. }