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