delete_old_courses.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * This deletes courses that were created but no file was ever uploaded, and
  4. * that were created previous to a specific date and last used previous to
  5. * another specific date (see $creation and $access)
  6. * Use this script with caution, as it will completely remove any trace of the
  7. * deleted courses.
  8. * Please note that this is not written with the inclusion of the concept ot
  9. * sessions. As such, it might delete courses but leave the course reference
  10. * in the session, which would cause issues.
  11. * Launch from the command line.
  12. * Usage: php delete_old_courses.php
  13. */
  14. exit;
  15. $creation = '2014-01-01';
  16. $access = '2014-07-01';
  17. require_once '../../main/inc/global.inc.php';
  18. if (PHP_SAPI !== 'cli') {
  19. die('This script can only be executed from the command line');
  20. }
  21. $tableExercise = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  22. $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
  23. $sql = "SELECT
  24. id, code, directory, db_name, creation_date, last_visit
  25. FROM $tableCourse c
  26. WHERE creation_date < '$creation' AND last_visit < '$access'
  27. ORDER by code
  28. ";
  29. echo $sql.PHP_EOL;
  30. $result = Database::query($sql);
  31. $items = Database::store_result($result, 'ASSOC');
  32. $total = 0;
  33. $count = 0;
  34. if (!empty($items)) {
  35. foreach ($items as $item) {
  36. $size = exec('du -sh '.__DIR__.'/../../courses/'.$item['directory']);
  37. echo "Course ".$item['code'].'('.$item['id'].') created on '.$item['creation_date'].' and last used on '.$item['last_visit'].' uses '.substr($size, 0, 8).PHP_EOL;
  38. // Check if it's 160K or 9.1M in size, which is the case for 'empty'
  39. // courses (created without or with example content)
  40. if (substr($size, 0, 4) == '160K' or substr($size, 0, 4) == '9,1M') {
  41. CourseManager::delete_course($item['code']);
  42. // The normal procedure moves the course directory to archive, so
  43. // delete it there as well
  44. echo('rm -rf '.__DIR__.'/../../archive/'.$item['directory'].'_*').PHP_EOL;
  45. exec('rm -rf '.__DIR__.'/../../archive/'.$item['directory'].'_*');
  46. // The normal procedure also created a database dump, but it is
  47. // stored in the course folder, so no issue there...
  48. if (substr($size, 0, 4) == '160K') {
  49. $total += 160;
  50. }
  51. if (substr($size, 0, 4) == '9,1M') {
  52. $total += 9100;
  53. }
  54. $count ++;
  55. if ($count%100 == 0) {
  56. // Print progressive information about the freed space
  57. echo '### Until now: '.$total.'K in '.$count.' courses'.PHP_EOL;
  58. }
  59. }
  60. }
  61. }
  62. // Print final information about the freed space
  63. echo $total.'K in '.$count.' courses'.PHP_EOL;