delete_old_tasks.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. //die("Error querying sessions: ".Database::error($res)."\n");
  38. }
  39. $countSessions = mysql_num_rows($res);
  40. $sql = "SELECT count(*) FROM session";
  41. $resc = mysql_query($sql, $conexion);
  42. if ($resc === false) {
  43. //die("Error querying sessions: ".Database::error($res)."\n");
  44. }
  45. $countAllSessions = mysql_result($resc, 0, 0);
  46. echo "[".time()."] Found $countSessions sessions between $fromDate and $expiryDate on a total of $countAllSessions sessions."."\n";
  47. while ($session = mysql_fetch_assoc($res)) {
  48. $sql2 = "SELECT c.id AS cid, c.code as ccode, c.directory as cdir
  49. FROM course c, session_rel_course s
  50. WHERE s.id_session = ".$session['id']."
  51. AND s.course_code = c.code";
  52. $res2 = mysql_query($sql2, $conexion); //Database::query($sql2);
  53. if ($res2 === false) {
  54. die("Error querying courses for session ".$session['id'].": ".mysql_error($res2)."\n");
  55. }
  56. if (mysql_num_rows($res2) > 0) {
  57. while ($course = mysql_fetch_assoc($res2)) {
  58. $sessionCourses[$session['id']] = $course['cid'];
  59. //$_SESSION['session_course'] = $sessionCourses;
  60. if (empty($coursesCodes[$course['cid']])) {
  61. $coursesCodes[$course['cid']] = $course['ccode'];
  62. }
  63. if (empty($coursesDirs[$course['cid']])) {
  64. $coursesDirs[$course['cid']] = $course['cdir'];
  65. }
  66. }
  67. }
  68. }
  69. echo "[".time()."] Filled courses arrays. Now checking tasks...\n";
  70. /**
  71. * Locate and destroy the expired tasks
  72. */
  73. //$sessionCourse = $_SESSION['session_course'];
  74. $totalSize = 0;
  75. foreach ($sessionCourses as $sid => $cid) {
  76. // Check if a folder already exists in this session
  77. // Folders are exclusive to sessions. If a folder already exists in
  78. // another session, you will not be allowed to create the same folder in
  79. // another session. As such, folders belong to one and only one session.
  80. $sql = "SELECT id, url FROM c_student_publication
  81. WHERE filetype = 'folder'
  82. AND c_id = $cid
  83. AND session_id = $sid
  84. AND active = 1
  85. AND url LIKE '%ALP%'";
  86. $resCarpetas = mysql_query($sql, $conexion); //Database::query($sql);
  87. if (mysql_num_rows($resCarpetas) > 0) {
  88. while ($rowDemo = mysql_fetch_assoc($resCarpetas)) {
  89. $carpetaAlpElimina = $_configuration['root_sys'].'courses/'.$coursesDirs[$cid].'/work'.$rowDemo['url'];
  90. //echo "rm -rf ".$carpetaAlpElimina."\n";
  91. $size = folderSize($carpetaAlpElimina);
  92. $totalSize += $size;
  93. echo "Freeing $size of a total $totalSize bytes in $carpetaAlpElimina\n";
  94. exec('rm -rf '.$carpetaAlpElimina);
  95. }
  96. $sqldel = "DELETE FROM c_student_publication
  97. WHERE
  98. c_id = $cid
  99. AND session_id = $sid AND active = 1;";
  100. $resdel = mysql_query($sqldel);
  101. if ($resdel === false) {
  102. echo "Error querying sessions: ".Database::error($resdel)."\n";
  103. }
  104. }
  105. }
  106. echo "[".time()."] Deleted tasks from $countSessions sessions between $fromDate and $expiryDate on a total of $countAllSessions sessions."."\n";
  107. /**
  108. * Helper function to calculate size of a folder
  109. * @author See php.net comments on filesize()
  110. */
  111. function folderSize($dir) {
  112. $size = 0;
  113. $contents = glob(rtrim($dir, '/').'/*', GLOB_NOSORT);
  114. foreach ($contents as $contents_value) {
  115. if (is_file($contents_value)) {
  116. $size += filesize($contents_value);
  117. } else {
  118. $size += folderSize($contents_value);
  119. }
  120. }
  121. return $size;
  122. }