delete_old_tasks.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script removes previous tasks from disk to clear space.
  5. * Configure the date on lines 22-23 to change the dates after/before which
  6. * to delete.
  7. * This works based on sessions dates (it will not delete tasks from
  8. * base courses).
  9. * This script should be located inside the tests/scripts/ folder to work
  10. * @author Paul Patrocinio <ppatrocino@icpna.edu.pe>
  11. * @author Percy Santiago <psantiago@icpna.edu.pe>
  12. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  13. */
  14. exit(); //remove this line to execute from the command line
  15. if (PHP_SAPI !== 'cli') {
  16. die('This script can only be executed from the command line');
  17. }
  18. require __DIR__.'/../../main/inc/conf/configuration.php';
  19. // Dates
  20. $expiryDate = '2015-06-01'; //session start date must be < to be considered
  21. $fromDate = '2011-01-01'; //session start date must be > to be considered
  22. $sessionCourses = array();
  23. $coursesCodes = array();
  24. $coursesDirs = array();
  25. if (!$conexion = mysql_connect($_configuration['db_host'], $_configuration['db_user'], $_configuration['db_password'])) {
  26. echo 'Could not connect to database';
  27. exit;
  28. }
  29. if (!mysql_select_db($_configuration['main_database'], $conexion)) {
  30. echo 'Could not select database '.$_configuration['main_database'];
  31. exit;
  32. }
  33. echo "[".time()."] Querying sessions\n";
  34. $sql = "SELECT id FROM session where access_start_date < '$expiryDate' AND access_start_date > '$fromDate'";
  35. $res = mysql_query($sql, $conexion);
  36. if ($res === false) {
  37. }
  38. $countSessions = mysql_num_rows($res);
  39. $sql = "SELECT count(*) FROM session";
  40. $resc = mysql_query($sql, $conexion);
  41. if ($resc === false) {
  42. }
  43. $countAllSessions = mysql_result($resc, 0, 0);
  44. echo "[".time()."] Found $countSessions sessions between $fromDate and $expiryDate on a total of $countAllSessions sessions."."\n";
  45. while ($session = mysql_fetch_assoc($res)) {
  46. $sql2 = "SELECT c.id AS cid, c.code as ccode, c.directory as cdir
  47. FROM course c, session_rel_course s
  48. WHERE s.id_session = ".$session['id']."
  49. AND s.course_code = c.code";
  50. $res2 = mysql_query($sql2, $conexion); //Database::query($sql2);
  51. if ($res2 === false) {
  52. die("Error querying courses for session ".$session['id'].": ".mysql_error($res2)."\n");
  53. }
  54. if (mysql_num_rows($res2) > 0) {
  55. while ($course = mysql_fetch_assoc($res2)) {
  56. $sessionCourses[$session['id']] = $course['cid'];
  57. //$_SESSION['session_course'] = $sessionCourses;
  58. if (empty($coursesCodes[$course['cid']])) {
  59. $coursesCodes[$course['cid']] = $course['ccode'];
  60. }
  61. if (empty($coursesDirs[$course['cid']])) {
  62. $coursesDirs[$course['cid']] = $course['cdir'];
  63. }
  64. }
  65. }
  66. }
  67. echo "[".time()."] Filled courses arrays. Now checking tasks...\n";
  68. /**
  69. * Locate and destroy the expired tasks
  70. */
  71. //$sessionCourse = $_SESSION['session_course'];
  72. $totalSize = 0;
  73. foreach ($sessionCourses as $sid => $cid) {
  74. // Check if a folder already exists in this session
  75. // Folders are exclusive to sessions. If a folder already exists in
  76. // another session, you will not be allowed to create the same folder in
  77. // another session. As such, folders belong to one and only one session.
  78. $sql = "SELECT id, url FROM c_student_publication
  79. WHERE filetype = 'folder'
  80. AND c_id = $cid
  81. AND session_id = $sid
  82. AND active = 1
  83. AND url LIKE '%ALP%'";
  84. $resCarpetas = mysql_query($sql, $conexion); //Database::query($sql);
  85. if (mysql_num_rows($resCarpetas) > 0) {
  86. while ($rowDemo = mysql_fetch_assoc($resCarpetas)) {
  87. $carpetaAlpElimina = $_configuration['root_sys'].'courses/'.$coursesDirs[$cid].'/work'.$rowDemo['url'];
  88. //echo "rm -rf ".$carpetaAlpElimina."\n";
  89. $size = folderSize($carpetaAlpElimina);
  90. $totalSize += $size;
  91. echo "Freeing $size of a total $totalSize bytes in $carpetaAlpElimina\n";
  92. exec('rm -rf '.$carpetaAlpElimina);
  93. }
  94. $sqldel = "DELETE FROM c_student_publication
  95. WHERE
  96. c_id = $cid
  97. AND session_id = $sid AND active = 1;";
  98. $resdel = mysql_query($sqldel);
  99. if ($resdel === false) {
  100. echo "Error querying sessions";
  101. }
  102. }
  103. }
  104. echo "[".time()."] Deleted tasks from $countSessions sessions between $fromDate and $expiryDate on a total of $countAllSessions sessions."."\n";
  105. /**
  106. * Helper function to calculate size of a folder
  107. * @author See php.net comments on filesize()
  108. */
  109. function folderSize($dir) {
  110. $size = 0;
  111. $contents = glob(rtrim($dir, '/').'/*', GLOB_NOSORT);
  112. foreach ($contents as $contents_value) {
  113. if (is_file($contents_value)) {
  114. $size += filesize($contents_value);
  115. } else {
  116. $size += folderSize($contents_value);
  117. }
  118. }
  119. return $size;
  120. }