]> git.wh0rd.org - tt-rss.git/blob - classes/ccache.php
ccache: 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 cat_id = :cat OR (:cat = 0 AND cat_id IS NULL)");
122 $sth->execute([":uid" => $owner_uid, ":cat" => $feed_id]);
123
124 while ($line = $sth->fetch()) {
125 CCache::update($line["id"], $owner_uid, false, false);
126 }
127 }
128
129 $sth = $pdo->prepare("SELECT SUM(value) AS sv
130 FROM ttrss_counters_cache, ttrss_feeds
131 WHERE id = feed_id AND
132 cat_id = :cat OR (:cat IS NULL AND cat_id IS NULL) AND
133 ttrss_counters_cache.owner_uid = :uid AND
134 ttrss_feeds.owner_uid = :uid");
135 $sth->execute([":uid" => $owner_uid, ":cat" => $feed_id]);
136 $row = $sth->fetch();
137
138 $unread = (int) $row["sv"];
139
140 } else {
141 $unread = (int) Feeds::getFeedArticles($feed_id, $is_cat, true, $owner_uid);
142 }
143
144 $pdo->beginTransaction();
145
146 $sth = $pdo->prepare("SELECT feed_id FROM $table
147 WHERE owner_uid = ? AND feed_id = ? LIMIT 1");
148 $sth->execute([$owner_uid, $feed_id]);
149
150 if ($sth->fetch()) {
151
152 $sth = $pdo->prepare("UPDATE $table SET
153 value = ?, updated = NOW() WHERE
154 feed_id = ? AND owner_uid = ?");
155
156 $sth->execute([$unread, $feed_id, $owner_uid]);
157
158 } else {
159 $sth = $pdo->prepare("INSERT INTO $table
160 (feed_id, value, owner_uid, updated)
161 VALUES
162 (?, ?, ?, NOW())");
163 $sth->execute([$feed_id, $unread, $owner_uid]);
164 }
165
166 $pdo->commit();
167
168 if ($feed_id > 0 && $prev_unread != $unread) {
169
170 if (!$is_cat) {
171
172 /* Update parent category */
173
174 if ($update_pcat) {
175
176 $sth = $pdo->prepare("SELECT cat_id FROM ttrss_feeds
177 WHERE owner_uid = ? AND id = ?");
178 $sth->execute([$owner_uid, $feed_id]);
179
180 if ($row = $sth->fetch()) {
181 CCache::update($row["cat_id"], $owner_uid, true, true, true);
182 }
183 }
184 }
185 } else if ($feed_id < 0) {
186 CCache::update_all($owner_uid);
187 }
188
189 return $unread;
190 }
191
192 }