1
|
|
|
<?php |
2
|
|
|
class CCache { |
3
|
|
|
public static function zero_all($owner_uid) { |
4
|
|
|
$pdo = Db::pdo(); |
5
|
|
|
|
6
|
|
|
$sth = $pdo->prepare("UPDATE ttrss_counters_cache SET |
7
|
|
|
value = 0 WHERE owner_uid = ?"); |
8
|
|
|
$sth->execute([$owner_uid]); |
9
|
|
|
|
10
|
|
|
$sth = $pdo->prepare("UPDATE ttrss_cat_counters_cache SET |
11
|
|
|
value = 0 WHERE owner_uid = ?"); |
12
|
|
|
$sth->execute([$owner_uid]); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public static function remove($feed_id, $owner_uid, $is_cat = false) { |
16
|
|
|
|
17
|
|
|
$feed_id = (int) $feed_id; |
18
|
|
|
|
19
|
|
|
if (!$is_cat) { |
20
|
|
|
$table = "ttrss_counters_cache"; |
21
|
|
|
} else { |
22
|
|
|
$table = "ttrss_cat_counters_cache"; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$pdo = Db::pdo(); |
26
|
|
|
|
27
|
|
|
$sth = $pdo->prepare("DELETE FROM $table WHERE |
28
|
|
|
feed_id = ? AND owner_uid = ?"); |
29
|
|
|
$sth->execute([$feed_id, $owner_uid]); |
30
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function update_all($owner_uid) { |
34
|
|
|
|
35
|
|
|
$pdo = Db::pdo(); |
36
|
|
|
|
37
|
|
|
if (get_pref('ENABLE_FEED_CATS', $owner_uid)) { |
38
|
|
|
|
39
|
|
|
$sth = $pdo->prepare("SELECT feed_id FROM ttrss_cat_counters_cache |
40
|
|
|
WHERE feed_id > 0 AND owner_uid = ?"); |
41
|
|
|
$sth->execute([$owner_uid]); |
42
|
|
|
|
43
|
|
|
while ($line = $sth->fetch()) { |
44
|
|
|
CCache::update($line["feed_id"], $owner_uid, true); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/* We have to manually include category 0 */ |
48
|
|
|
|
49
|
|
|
CCache::update(0, $owner_uid, true); |
50
|
|
|
|
51
|
|
|
} else { |
52
|
|
|
$sth = $pdo->prepare("SELECT feed_id FROM ttrss_counters_cache |
53
|
|
|
WHERE feed_id > 0 AND owner_uid = ?"); |
54
|
|
|
$sth->execute([$owner_uid]); |
55
|
|
|
|
56
|
|
|
while ($line = $sth->fetch()) { |
57
|
|
|
print CCache::update($line["feed_id"], $owner_uid); |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function find($feed_id, $owner_uid, $is_cat = false, |
65
|
|
|
$no_update = false) { |
66
|
|
|
|
67
|
|
|
// "" (null) is valid and should be cast to 0 (uncategorized) |
68
|
|
|
// everything else i.e. tags are not |
69
|
|
|
if (!is_numeric($feed_id) && $feed_id) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$feed_id = (int) $feed_id; |
74
|
|
|
|
75
|
|
|
if (!$is_cat) { |
76
|
|
|
$table = "ttrss_counters_cache"; |
77
|
|
|
} else { |
78
|
|
|
$table = "ttrss_cat_counters_cache"; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$pdo = Db::pdo(); |
82
|
|
|
|
83
|
|
|
$sth = $pdo->prepare("SELECT value FROM $table |
84
|
|
|
WHERE owner_uid = ? AND feed_id = ? |
85
|
|
|
LIMIT 1"); |
86
|
|
|
|
87
|
|
|
$sth->execute([$owner_uid, $feed_id]); |
88
|
|
|
|
89
|
|
|
if ($row = $sth->fetch()) { |
90
|
|
|
return $row["value"]; |
91
|
|
|
} else { |
92
|
|
|
if ($no_update) { |
93
|
|
|
return -1; |
94
|
|
|
} else { |
95
|
|
|
return CCache::update($feed_id, $owner_uid, $is_cat); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public static function update($feed_id, $owner_uid, $is_cat = false, |
102
|
|
|
$update_pcat = true, $pcat_fast = false) { |
103
|
|
|
|
104
|
|
|
// "" (null) is valid and should be cast to 0 (uncategorized) |
105
|
|
|
// everything else i.e. tags are not |
106
|
|
|
if (!is_numeric($feed_id) && $feed_id) { |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$feed_id = (int) $feed_id; |
111
|
|
|
|
112
|
|
|
$prev_unread = CCache::find($feed_id, $owner_uid, $is_cat, true); |
113
|
|
|
|
114
|
|
|
/* When updating a label, all we need to do is recalculate feed counters |
115
|
|
|
* because labels are not cached */ |
116
|
|
|
|
117
|
|
|
if ($feed_id < 0) { |
118
|
|
|
CCache::update_all($owner_uid); |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (!$is_cat) { |
123
|
|
|
$table = "ttrss_counters_cache"; |
124
|
|
|
} else { |
125
|
|
|
$table = "ttrss_cat_counters_cache"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$pdo = Db::pdo(); |
129
|
|
|
|
130
|
|
|
if ($is_cat && $feed_id >= 0) { |
131
|
|
|
/* Recalculate counters for child feeds */ |
132
|
|
|
|
133
|
|
|
if (!$pcat_fast) { |
134
|
|
|
$sth = $pdo->prepare("SELECT id FROM ttrss_feeds |
135
|
|
|
WHERE owner_uid = :uid AND |
136
|
|
|
(cat_id = :cat OR (:cat = 0 AND cat_id IS NULL))"); |
137
|
|
|
$sth->execute([":uid" => $owner_uid, ":cat" => $feed_id]); |
138
|
|
|
|
139
|
|
|
while ($line = $sth->fetch()) { |
140
|
|
|
CCache::update((int) $line["id"], $owner_uid, false, false); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$sth = $pdo->prepare("SELECT SUM(value) AS sv |
145
|
|
|
FROM ttrss_counters_cache, ttrss_feeds |
146
|
|
|
WHERE ttrss_feeds.id = feed_id AND |
147
|
|
|
(cat_id = :cat OR (:cat = 0 AND cat_id IS NULL)) AND |
148
|
|
|
ttrss_counters_cache.owner_uid = :uid AND |
149
|
|
|
ttrss_feeds.owner_uid = :uid"); |
150
|
|
|
$sth->execute([":uid" => $owner_uid, ":cat" => $feed_id]); |
151
|
|
|
$row = $sth->fetch(); |
152
|
|
|
|
153
|
|
|
$unread = (int) $row["sv"]; |
154
|
|
|
|
155
|
|
|
} else { |
156
|
|
|
$unread = (int) Feeds::getFeedArticles($feed_id, $is_cat, true, $owner_uid); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$tr_in_progress = false; |
160
|
|
|
|
161
|
|
|
try { |
162
|
|
|
$pdo->beginTransaction(); |
163
|
|
|
} catch (Exception $e) { |
164
|
|
|
$tr_in_progress = true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$sth = $pdo->prepare("SELECT feed_id FROM $table |
168
|
|
|
WHERE owner_uid = ? AND feed_id = ? LIMIT 1"); |
169
|
|
|
$sth->execute([$owner_uid, $feed_id]); |
170
|
|
|
|
171
|
|
|
if ($sth->fetch()) { |
172
|
|
|
|
173
|
|
|
$sth = $pdo->prepare("update $table SET |
174
|
|
|
value = ?, updated = NOW() WHERE |
175
|
|
|
feed_id = ? AND owner_uid = ?"); |
176
|
|
|
|
177
|
|
|
$sth->execute([$unread, $feed_id, $owner_uid]); |
178
|
|
|
|
179
|
|
|
} else { |
180
|
|
|
$sth = $pdo->prepare("INSERT INTO $table |
181
|
|
|
(feed_id, value, owner_uid, updated) |
182
|
|
|
VALUES |
183
|
|
|
(?, ?, ?, NOW())"); |
184
|
|
|
$sth->execute([$feed_id, $unread, $owner_uid]); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (!$tr_in_progress) { |
188
|
|
|
$pdo->commit(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if ($feed_id > 0 && $prev_unread != $unread) { |
192
|
|
|
|
193
|
|
|
if (!$is_cat) { |
194
|
|
|
|
195
|
|
|
/* Update parent category */ |
196
|
|
|
|
197
|
|
|
if ($update_pcat) { |
198
|
|
|
|
199
|
|
|
$sth = $pdo->prepare("SELECT cat_id FROM ttrss_feeds |
200
|
|
|
WHERE owner_uid = ? AND id = ?"); |
201
|
|
|
$sth->execute([$owner_uid, $feed_id]); |
202
|
|
|
|
203
|
|
|
if ($row = $sth->fetch()) { |
204
|
|
|
CCache::update((int) $row["cat_id"], $owner_uid, true, true, true); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} else if ($feed_id < 0) { |
209
|
|
|
CCache::update_all($owner_uid); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $unread; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|