list_unused_img.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php /* For licensing terms, see /license.txt */
  2. /**
  3. * Cron script to list unused images
  4. * @package chamilo.cron
  5. */
  6. /**
  7. * Includes and declarations
  8. */
  9. if (PHP_SAPI!='cli') {
  10. die('Run this script through the command line or comment this line in the code');
  11. }
  12. require_once __DIR__.'/../../../main/inc/global.inc.php';
  13. $path = api_get_path(SYS_CODE_PATH).'img/';
  14. ini_set('memory_limit', '128M');
  15. ini_set('max_execution_time', '240');
  16. $unused = array();
  17. global $_configuration;
  18. /**
  19. * Main code
  20. */
  21. // get all the available images and their directory
  22. $found_img = get_img_files($path);
  23. // Now, for each image, check if there is at least one reference
  24. chdir($_configuration['root_sys']);
  25. foreach ($found_img as $i => $p) {
  26. $j = 0;
  27. $output = @shell_exec('rgrep '.$i.' main/');
  28. $outputs = explode('\n', $output);
  29. foreach ($outputs as $line) {
  30. if (substr($line, 0, 5)=='rgrep') {
  31. //this means a permission error, ignore
  32. } else {
  33. $j++;
  34. }
  35. }
  36. if ($j === 0) {
  37. $unused[$i] = $p;
  38. }
  39. }
  40. echo '<table>';
  41. /*
  42. if (count($unexisting_img)<1) { die("No missing image<br />\n"); } else { echo "The following images were nowhere to be found: <br />\n<table>"; }
  43. foreach ($unexisting_img as $term => $file) {
  44. echo "<tr><td>$term</td><td>in $file</td></tr>\n";
  45. }
  46. */
  47. echo '<tr><td colspan="2">Existing images('.count($found_img).'), unused('.count($unused).')</td></tr>'."\n";
  48. echo '<tr><td>Image file</td><td>Used x times</td></tr>'."\n";
  49. $r = ksort($found_img);
  50. foreach ($unused as $term => $path) {
  51. if (isset($unused[$term])) {
  52. echo '<tr>';
  53. echo '<td bgcolor="#55ff55">'.$term.'</td>';
  54. echo '<td bgcolor="#55ff55">'.($path=='/'?'/':$path.'/').$term.'</td>';
  55. echo '</tr>'."\n";
  56. } else {
  57. echo '<tr>';
  58. echo '<td bgcolor="#ff5555">'.$term.'</td>';
  59. echo '<td bgcolor="#ff5555">'.($path=='/'?'/':$path.'/').$term.'</td>';
  60. echo '</tr>'."\n";
  61. }
  62. }
  63. echo "</table>\n";
  64. /**
  65. * Get the list of available images
  66. * @param string $path The path to start the scan from
  67. * @return array The files list
  68. */
  69. function get_img_files($path)
  70. {
  71. $files = array();
  72. //We know there are max 3 levels, so don't bother going recursive
  73. $list = scandir($path);
  74. foreach ($list as $entry) {
  75. if (substr($entry, 0, 1)=='.') {
  76. continue;
  77. }
  78. if (is_dir($path.$entry)) {
  79. $sublist = scandir($path.$entry);
  80. foreach ($sublist as $subentry) {
  81. if (substr($subentry, 0, 1)=='.') {
  82. continue;
  83. }
  84. if (is_dir($path.$entry.'/'.$subentry)) {
  85. $subsublist = scandir($path.$entry.'/'.$subentry);
  86. foreach ($subsublist as $subsubentry) {
  87. if (substr($subsubentry, 0, 1)=='.') {
  88. continue;
  89. }
  90. if (is_file($path.$entry.'/'.$subentry.'/'.$subsubentry)) {
  91. $files[$subsubentry] = '/'.$entry.'/'.$subentry;
  92. }
  93. }
  94. } elseif (is_file($path.$entry.'/'.$subentry)) {
  95. $files[$subentry] = '/'.$entry;
  96. }
  97. }
  98. } elseif (is_file($path.$entry)) {
  99. $files[$entry] = '/';
  100. }
  101. }
  102. return $files;
  103. }