]> git.wh0rd.org - tt-rss.git/blob - classes/ccache.php
php: remove trailing whitespaces
[tt-rss.git] / classes / ccache.php
1 <?php
2 class CCache {
3 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 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 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 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 $feed_id = (int) $feed_id;
73
74 if (!$is_cat) {
75 $table = "ttrss_counters_cache";
76 } else {
77 $table = "ttrss_cat_counters_cache";
78 }
79
80 $pdo = Db::pdo();
81
82 $sth = $pdo->prepare("SELECT value FROM $table
83 WHERE owner_uid = ? AND feed_id = ?
84 LIMIT 1");
85
86 $sth->execute([$owner_uid, $feed_id]);
87
88 if ($row = $sth->fetch()) {
89 return $row["value"];
90 } else {
91 if ($no_update) {
92 return -1;
93 } else {
94 return CCache::update($feed_id, $owner_uid, $is_cat);
95 }
96 }
97
98 }
99
100 static function update($feed_id, $owner_uid, $is_cat = false,
101 $update_pcat = true, $pcat_fast = false) {
102
103 // "" (null) is valid and should be cast to 0 (uncategorized)
104 // everything else i.e. tags are not
105 if (!is_numeric($feed_id) && $feed_id)
106 return;
107
108 $feed_id = (int) $feed_id;
109
110 $prev_unread = CCache::find($feed_id, $owner_uid, $is_cat, true);
111
112 /* When updating a label, all we need to do is recalculate feed counters
113 * because labels are not cached */
114
115 if ($feed_id < 0) {
116 CCache::update_all($owner_uid);
117 return;
118 }
119
120 if (!$is_cat) {
121 $table = "ttrss_counters_cache";
122 } else {
123 $table = "ttrss_cat_counters_cache";
124 }
125
126 $pdo = Db::pdo();
127
128 if ($is_cat && $feed_id >= 0) {
129 /* Recalculate counters for child feeds */
130
131 if (!$pcat_fast) {
132 $sth = $pdo->prepare("SELECT id FROM ttrss_feeds
133 WHERE owner_uid = :uid AND
134 (cat_id = :cat OR (:cat = 0 AND cat_id IS NULL))");
135 $sth->execute([":uid" => $owner_uid, ":cat" => $feed_id]);
136
137 while ($line = $sth->fetch()) {
138 CCache::update((int)$line["id"], $owner_uid, false, false);
139 }
140 }
141
142 $sth = $pdo->prepare("SELECT SUM(value) AS sv
143 FROM ttrss_counters_cache, ttrss_feeds
144 WHERE id = feed_id AND
145 (cat_id = :cat OR (:cat = 0 AND cat_id IS NULL)) AND
146 ttrss_counters_cache.owner_uid = :uid AND
147 ttrss_feeds.owner_uid = :uid");
148 $sth->execute([":uid" => $owner_uid, ":cat" => $feed_id]);
149 $row = $sth->fetch();
150
151 $unread = (int) $row["sv"];
152
153 } else {
154 $unread = (int) Feeds::getFeedArticles($feed_id, $is_cat, true, $owner_uid);
155 }
156
157 $tr_in_progress = false;
158
159 try {
160 $pdo->beginTransaction();
161 } catch (Exception $e) {
162 $tr_in_progress = true;
163 }
164
165 $sth = $pdo->prepare("SELECT feed_id FROM $table
166 WHERE owner_uid = ? AND feed_id = ? LIMIT 1");
167 $sth->execute([$owner_uid, $feed_id]);
168
169 if ($sth->fetch()) {
170
171 $sth = $pdo->prepare("UPDATE $table SET
172 value = ?, updated = NOW() WHERE
173 feed_id = ? AND owner_uid = ?");
174
175 $sth->execute([$unread, $feed_id, $owner_uid]);
176
177 } else {
178 $sth = $pdo->prepare("INSERT INTO $table
179 (feed_id, value, owner_uid, updated)
180 VALUES
181 (?, ?, ?, NOW())");
182 $sth->execute([$feed_id, $unread, $owner_uid]);
183 }
184
185 if (!$tr_in_progress) $pdo->commit();
186
187 if ($feed_id > 0 && $prev_unread != $unread) {
188
189 if (!$is_cat) {
190
191 /* Update parent category */
192
193 if ($update_pcat) {
194
195 $sth = $pdo->prepare("SELECT cat_id FROM ttrss_feeds
196 WHERE owner_uid = ? AND id = ?");
197 $sth->execute([$owner_uid, $feed_id]);
198
199 if ($row = $sth->fetch()) {
200 CCache::update((int)$row["cat_id"], $owner_uid, true, true, true);
201 }
202 }
203 }
204 } else if ($feed_id < 0) {
205 CCache::update_all($owner_uid);
206 }
207
208 return $unread;
209 }
210
211 }