/upload/includes/cron/jobs/clean_dlstat.php

http://torrentpier2.googlecode.com/ · PHP · 119 lines · 98 code · 17 blank · 4 comment · 4 complexity · 05e787c4b7350afea41c46816daccbd1 MD5 · raw file

  1. <?php
  2. if (!defined('BB_ROOT')) die(basename(__FILE__));
  3. define('BUF_DLSTATUS_TABLE', 'tmp_buf_dlstatus');
  4. // Move new dl-status records to main table
  5. DB()->query("
  6. CREATE TEMPORARY TABLE ". BUF_DLSTATUS_TABLE ." (
  7. user_id mediumint(9) NOT NULL default '0',
  8. topic_id mediumint(8) unsigned NOT NULL default '0',
  9. user_status tinyint(1) NOT NULL default '0',
  10. PRIMARY KEY (user_id, topic_id)
  11. ) ENGINE = MyISAM
  12. ");
  13. DB()->query("
  14. INSERT INTO ". BUF_DLSTATUS_TABLE ."
  15. (user_id, topic_id, user_status)
  16. SELECT
  17. user_id, topic_id, user_status
  18. FROM
  19. ". BB_BT_DLSTATUS ."
  20. WHERE
  21. last_modified_dlstatus < DATE_SUB(NOW(), INTERVAL 1 DAY)
  22. ");
  23. DB()->query("
  24. REPLACE INTO ". BB_BT_DLSTATUS ."
  25. (user_id, topic_id, user_status)
  26. SELECT
  27. user_id, topic_id, user_status
  28. FROM ". BUF_DLSTATUS_TABLE ."
  29. ");
  30. DB()->query("
  31. DELETE dl
  32. FROM ". BUF_DLSTATUS_TABLE ." buf
  33. INNER JOIN ". BB_BT_DLSTATUS ." dl USING(user_id, topic_id)
  34. ");
  35. DB()->query("DROP TEMPORARY TABLE ". BUF_DLSTATUS_TABLE);
  36. // Delete staled dl-status records
  37. $keeping_dlstat = array(
  38. DL_STATUS_WILL => (int) $bb_cfg['dl_will_days_keep'],
  39. DL_STATUS_DOWN => (int) $bb_cfg['dl_down_days_keep'],
  40. DL_STATUS_COMPLETE => (int) $bb_cfg['dl_complete_days_keep'],
  41. DL_STATUS_CANCEL => (int) $bb_cfg['dl_cancel_days_keep'],
  42. );
  43. $delete_dlstat_sql = array();
  44. foreach ($keeping_dlstat as $dl_status => $days_to_keep)
  45. {
  46. if ($days_to_keep)
  47. {
  48. $delete_dlstat_sql[] = "
  49. user_status = $dl_status
  50. AND
  51. last_modified_dlstatus < DATE_SUB(NOW(), INTERVAL $days_to_keep DAY)
  52. ";
  53. }
  54. }
  55. if ($delete_dlstat_sql = join(') OR (', $delete_dlstat_sql))
  56. {
  57. DB()->query("DELETE QUICK FROM ". BB_BT_DLSTATUS ." WHERE ($delete_dlstat_sql)");
  58. }
  59. // Delete orphans
  60. DB()->query("
  61. DELETE QUICK dl
  62. FROM ". BB_BT_DLSTATUS ." dl
  63. LEFT JOIN ". BB_USERS ." u USING(user_id)
  64. WHERE u.user_id IS NULL
  65. ");
  66. DB()->query("
  67. DELETE QUICK dl
  68. FROM ". BB_BT_DLSTATUS ." dl
  69. LEFT JOIN ". BB_TOPICS ." t USING(topic_id)
  70. WHERE t.topic_id IS NULL
  71. ");
  72. // Tor-Stats cleanup
  73. if ($torstat_days_keep = intval($bb_cfg['torstat_days_keep']))
  74. {
  75. DB()->query("DELETE QUICK FROM ". BB_BT_TORSTAT ." WHERE last_modified_torstat < DATE_SUB(NOW(), INTERVAL $torstat_days_keep DAY)");
  76. }
  77. DB()->query("
  78. DELETE QUICK tst
  79. FROM ". BB_BT_TORSTAT ." tst
  80. LEFT JOIN ". BB_BT_TORRENTS ." tor USING(topic_id)
  81. WHERE tor.topic_id IS NULL
  82. ");
  83. DB()->query("
  84. UPDATE
  85. ". BB_BT_USERS ."
  86. SET
  87. up_yesterday = up_today,
  88. down_yesterday = down_today,
  89. up_release_yesterday = up_release_today,
  90. up_bonus_yesterday = up_bonus_today,
  91. points_yesterday = points_today
  92. ");
  93. DB()->query("
  94. UPDATE
  95. ". BB_BT_USERS ."
  96. SET
  97. up_today = 0,
  98. down_today = 0,
  99. up_release_today = 0,
  100. up_bonus_today = 0,
  101. points_today = 0
  102. ");