2 /* function ccache_zero($feed_id, $owner_uid) {
3 db_query("UPDATE ttrss_counters_cache SET
4 value = 0, updated = NOW() WHERE
5 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
8 function ccache_zero_all($owner_uid) {
9 db_query("UPDATE ttrss_counters_cache SET
10 value = 0 WHERE owner_uid = '$owner_uid'");
12 db_query("UPDATE ttrss_cat_counters_cache SET
13 value = 0 WHERE owner_uid = '$owner_uid'");
16 function ccache_remove($feed_id, $owner_uid, $is_cat = false) {
19 $table = "ttrss_counters_cache";
21 $table = "ttrss_cat_counters_cache";
24 db_query("DELETE FROM $table WHERE
25 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
29 function ccache_update_all($owner_uid) {
31 if (get_pref('ENABLE_FEED_CATS', $owner_uid)) {
33 $result = db_query("SELECT feed_id FROM ttrss_cat_counters_cache
34 WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
36 while ($line = db_fetch_assoc($result)) {
37 ccache_update($line["feed_id"], $owner_uid, true);
40 /* We have to manually include category 0 */
42 ccache_update(0, $owner_uid, true);
45 $result = db_query("SELECT feed_id FROM ttrss_counters_cache
46 WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
48 while ($line = db_fetch_assoc($result)) {
49 print ccache_update($line["feed_id"], $owner_uid);
56 function ccache_find($feed_id, $owner_uid, $is_cat = false,
59 if (!is_numeric($feed_id)) return;
62 $table = "ttrss_counters_cache";
63 /* if ($feed_id > 0) {
64 $tmp_result = db_query("SELECT owner_uid FROM ttrss_feeds
65 WHERE id = '$feed_id'");
66 $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid");
69 $table = "ttrss_cat_counters_cache";
72 if (DB_TYPE == "pgsql") {
73 $date_qpart = "updated > NOW() - INTERVAL '15 minutes'";
74 } else if (DB_TYPE == "mysql") {
75 $date_qpart = "updated > DATE_SUB(NOW(), INTERVAL 15 MINUTE)";
78 $result = db_query("SELECT value FROM $table
79 WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id'
82 if (db_num_rows($result) == 1) {
83 return db_fetch_result($result, 0, "value");
88 return ccache_update($feed_id, $owner_uid, $is_cat);
94 function ccache_update($feed_id, $owner_uid, $is_cat = false,
95 $update_pcat = true, $pcat_fast = false) {
97 if (!is_numeric($feed_id)) return;
99 /* if (!$is_cat && $feed_id > 0) {
100 $tmp_result = db_query("SELECT owner_uid FROM ttrss_feeds
101 WHERE id = '$feed_id'");
102 $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid");
105 $prev_unread = ccache_find($feed_id, $owner_uid, $is_cat, true);
107 /* When updating a label, all we need to do is recalculate feed counters
108 * because labels are not cached */
111 ccache_update_all($owner_uid);
116 $table = "ttrss_counters_cache";
118 $table = "ttrss_cat_counters_cache";
121 if ($is_cat && $feed_id >= 0) {
123 $cat_qpart = "cat_id = '$feed_id'";
125 $cat_qpart = "cat_id IS NULL";
128 /* Recalculate counters for child feeds */
131 $result = db_query("SELECT id FROM ttrss_feeds
132 WHERE owner_uid = '$owner_uid' AND $cat_qpart");
134 while ($line = db_fetch_assoc($result)) {
135 ccache_update($line["id"], $owner_uid, false, false);
139 $result = db_query("SELECT SUM(value) AS sv
140 FROM ttrss_counters_cache, ttrss_feeds
141 WHERE id = feed_id AND $cat_qpart AND
142 ttrss_feeds.owner_uid = '$owner_uid'");
144 $unread = (int) db_fetch_result($result, 0, "sv");
147 $unread = (int) getFeedArticles($feed_id, $is_cat, true, $owner_uid);
152 $result = db_query("SELECT feed_id FROM $table
153 WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id' LIMIT 1");
155 if (db_num_rows($result) == 1) {
156 db_query("UPDATE $table SET
157 value = '$unread', updated = NOW() WHERE
158 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
161 db_query("INSERT INTO $table
162 (feed_id, value, owner_uid, updated)
164 ($feed_id, $unread, $owner_uid, NOW())");
169 if ($feed_id > 0 && $prev_unread != $unread) {
173 /* Update parent category */
177 $result = db_query("SELECT cat_id FROM ttrss_feeds
178 WHERE owner_uid = '$owner_uid' AND id = '$feed_id'");
180 $cat_id = (int) db_fetch_result($result, 0, "cat_id");
182 ccache_update($cat_id, $owner_uid, true, true, true);
186 } else if ($feed_id < 0) {
187 ccache_update_all($owner_uid);
193 /* function ccache_cleanup($owner_uid) {
195 if (DB_TYPE == "pgsql") {
196 db_query("DELETE FROM ttrss_counters_cache AS c1 WHERE
197 (SELECT count(*) FROM ttrss_counters_cache AS c2
198 WHERE c1.feed_id = c2.feed_id AND c2.owner_uid = c1.owner_uid) > 1
199 AND owner_uid = '$owner_uid'");
201 db_query("DELETE FROM ttrss_cat_counters_cache AS c1 WHERE
202 (SELECT count(*) FROM ttrss_cat_counters_cache AS c2
203 WHERE c1.feed_id = c2.feed_id AND c2.owner_uid = c1.owner_uid) > 1
204 AND owner_uid = '$owner_uid'");
206 db_query("DELETE c1 FROM
207 ttrss_counters_cache AS c1,
208 ttrss_counters_cache AS c2
210 c1.owner_uid = '$owner_uid' AND
211 c1.owner_uid = c2.owner_uid AND
212 c1.feed_id = c2.feed_id");
214 db_query("DELETE c1 FROM
215 ttrss_cat_counters_cache AS c1,
216 ttrss_cat_counters_cache AS c2
218 c1.owner_uid = '$owner_uid' AND
219 c1.owner_uid = c2.owner_uid AND
220 c1.feed_id = c2.feed_id");