]> git.wh0rd.org Git - tt-rss.git/blob - include/ccache.php
tweak selected feed display a bit
[tt-rss.git] / include / ccache.php
1 <?php
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'");
6         } */
7
8         function ccache_zero_all($owner_uid) {
9                 db_query("UPDATE ttrss_counters_cache SET
10                         value = 0 WHERE owner_uid = '$owner_uid'");
11
12                 db_query("UPDATE ttrss_cat_counters_cache SET
13                         value = 0 WHERE owner_uid = '$owner_uid'");
14         }
15
16         function ccache_remove($feed_id, $owner_uid, $is_cat = false) {
17
18                 if (!$is_cat) {
19                         $table = "ttrss_counters_cache";
20                 } else {
21                         $table = "ttrss_cat_counters_cache";
22                 }
23
24                 db_query("DELETE FROM $table WHERE
25                         feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
26
27         }
28
29         function ccache_update_all($owner_uid) {
30
31                 if (get_pref('ENABLE_FEED_CATS', $owner_uid)) {
32
33                         $result = db_query("SELECT feed_id FROM ttrss_cat_counters_cache
34                                 WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
35
36                         while ($line = db_fetch_assoc($result)) {
37                                 ccache_update($line["feed_id"], $owner_uid, true);
38                         }
39
40                         /* We have to manually include category 0 */
41
42                         ccache_update(0, $owner_uid, true);
43
44                 } else {
45                         $result = db_query("SELECT feed_id FROM ttrss_counters_cache
46                                 WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
47
48                         while ($line = db_fetch_assoc($result)) {
49                                 print ccache_update($line["feed_id"], $owner_uid);
50
51                         }
52
53                 }
54         }
55
56         function ccache_find($feed_id, $owner_uid, $is_cat = false,
57                 $no_update = false) {
58
59                 if (!is_numeric($feed_id)) return;
60
61                 if (!$is_cat) {
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");
67                         } */
68                 } else {
69                         $table = "ttrss_cat_counters_cache";
70                 }
71
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)";
76                 }
77
78                 $result = db_query("SELECT value FROM $table
79                         WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id'
80                         LIMIT 1");
81
82                 if (db_num_rows($result) == 1) {
83                         return db_fetch_result($result, 0, "value");
84                 } else {
85                         if ($no_update) {
86                                 return -1;
87                         } else {
88                                 return ccache_update($feed_id, $owner_uid, $is_cat);
89                         }
90                 }
91
92         }
93
94         function ccache_update($feed_id, $owner_uid, $is_cat = false,
95                 $update_pcat = true, $pcat_fast = false) {
96
97                 if (!is_numeric($feed_id)) return;
98
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");
103                 } */
104
105                 $prev_unread = ccache_find($feed_id, $owner_uid, $is_cat, true);
106
107                 /* When updating a label, all we need to do is recalculate feed counters
108                  * because labels are not cached */
109
110                 if ($feed_id < 0) {
111                         ccache_update_all($owner_uid);
112                         return;
113                 }
114
115                 if (!$is_cat) {
116                         $table = "ttrss_counters_cache";
117                 } else {
118                         $table = "ttrss_cat_counters_cache";
119                 }
120
121                 if ($is_cat && $feed_id >= 0) {
122                         if ($feed_id != 0) {
123                                 $cat_qpart = "cat_id = '$feed_id'";
124                         } else {
125                                 $cat_qpart = "cat_id IS NULL";
126                         }
127
128                         /* Recalculate counters for child feeds */
129
130                         if (!$pcat_fast) {
131                                 $result = db_query("SELECT id FROM ttrss_feeds
132                                                 WHERE owner_uid = '$owner_uid' AND $cat_qpart");
133
134                                 while ($line = db_fetch_assoc($result)) {
135                                         ccache_update($line["id"], $owner_uid, false, false);
136                                 }
137                         }
138
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'");
143
144                         $unread = (int) db_fetch_result($result, 0, "sv");
145
146                 } else {
147                         $unread = (int) getFeedArticles($feed_id, $is_cat, true, $owner_uid);
148                 }
149
150                 db_query("BEGIN");
151
152                 $result = db_query("SELECT feed_id FROM $table
153                         WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id' LIMIT 1");
154
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'");
159
160                 } else {
161                         db_query("INSERT INTO $table
162                                 (feed_id, value, owner_uid, updated)
163                                 VALUES
164                                 ($feed_id, $unread, $owner_uid, NOW())");
165                 }
166
167                 db_query("COMMIT");
168
169                 if ($feed_id > 0 && $prev_unread != $unread) {
170
171                         if (!$is_cat) {
172
173                                 /* Update parent category */
174
175                                 if ($update_pcat) {
176
177                                         $result = db_query("SELECT cat_id FROM ttrss_feeds
178                                                 WHERE owner_uid = '$owner_uid' AND id = '$feed_id'");
179
180                                         $cat_id = (int) db_fetch_result($result, 0, "cat_id");
181
182                                         ccache_update($cat_id, $owner_uid, true, true, true);
183
184                                 }
185                         }
186                 } else if ($feed_id < 0) {
187                         ccache_update_all($owner_uid);
188                 }
189
190                 return $unread;
191         }
192
193         /* function ccache_cleanup($owner_uid) {
194
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'");
200
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'");
205                 } else {
206                         db_query("DELETE c1 FROM
207                                         ttrss_counters_cache AS c1,
208                                         ttrss_counters_cache AS c2
209                                 WHERE
210                                         c1.owner_uid = '$owner_uid' AND
211                                         c1.owner_uid = c2.owner_uid AND
212                                         c1.feed_id = c2.feed_id");
213
214                         db_query("DELETE c1 FROM
215                                         ttrss_cat_counters_cache AS c1,
216                                         ttrss_cat_counters_cache AS c2
217                                 WHERE
218                                         c1.owner_uid = '$owner_uid' AND
219                                         c1.owner_uid = c2.owner_uid AND
220                                         c1.feed_id = c2.feed_id");
221
222                 }
223         } */
224 ?>