PageRenderTime 32ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

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