]> git.wh0rd.org - tt-rss.git/blame - classes/counters.php
pngcrush.sh
[tt-rss.git] / classes / counters.php
CommitLineData
65af3b2c
AD
1<?php
2class Counters {
3
4 static function getAllCounters() {
5 $data = Counters::getGlobalCounters();
6
7 $data = array_merge($data, Counters::getVirtCounters());
8 $data = array_merge($data, Counters::getLabelCounters());
9 $data = array_merge($data, Counters::getFeedCounters());
10 $data = array_merge($data, Counters::getCategoryCounters());
11
12 return $data;
13 }
14
15 static function getCategoryCounters() {
16 $ret_arr = array();
17
18 /* Labels category */
19
20 $cv = array("id" => -2, "kind" => "cat",
21 "counter" => Feeds::getCategoryUnread(-2));
22
23 array_push($ret_arr, $cv);
24
a25ac0d7
AD
25 $pdo = DB::pdo();
26
27 $sth = $pdo->prepare("SELECT id AS cat_id, value AS unread,
65af3b2c
AD
28 (SELECT COUNT(id) FROM ttrss_feed_categories AS c2
29 WHERE c2.parent_cat = ttrss_feed_categories.id) AS num_children
30 FROM ttrss_feed_categories, ttrss_cat_counters_cache
31 WHERE ttrss_cat_counters_cache.feed_id = id AND
32 ttrss_cat_counters_cache.owner_uid = ttrss_feed_categories.owner_uid AND
a25ac0d7
AD
33 ttrss_feed_categories.owner_uid = ?");
34 $sth->execute([$_SESSION['uid']]);
65af3b2c 35
a25ac0d7 36 while ($line = $sth->fetch()) {
65af3b2c
AD
37 $line["cat_id"] = (int) $line["cat_id"];
38
39 if ($line["num_children"] > 0) {
40 $child_counter = Feeds::getCategoryChildrenUnread($line["cat_id"], $_SESSION["uid"]);
41 } else {
42 $child_counter = 0;
43 }
44
45 $cv = array("id" => $line["cat_id"], "kind" => "cat",
46 "counter" => $line["unread"] + $child_counter);
47
48 array_push($ret_arr, $cv);
49 }
50
51 /* Special case: NULL category doesn't actually exist in the DB */
52
53 $cv = array("id" => 0, "kind" => "cat",
54 "counter" => (int) CCache::find(0, $_SESSION["uid"], true));
55
56 array_push($ret_arr, $cv);
57
58 return $ret_arr;
59 }
60
61 static function getGlobalCounters($global_unread = -1) {
62 $ret_arr = array();
63
64 if ($global_unread == -1) {
65 $global_unread = Feeds::getGlobalUnread();
66 }
67
68 $cv = array("id" => "global-unread",
69 "counter" => (int) $global_unread);
70
71 array_push($ret_arr, $cv);
72
a25ac0d7
AD
73 $pdo = Db::pdo();
74
75 $sth = $pdo->prepare("SELECT COUNT(id) AS fn FROM
76 ttrss_feeds WHERE owner_uid = ?");
77 $sth->execute([$_SESSION['uid']]);
78 $row = $sth->fetch();
65af3b2c 79
a25ac0d7 80 $subscribed_feeds = $row["fn"];
65af3b2c
AD
81
82 $cv = array("id" => "subscribed-feeds",
83 "counter" => (int) $subscribed_feeds);
84
85 array_push($ret_arr, $cv);
86
87 return $ret_arr;
88 }
89
90 static function getVirtCounters() {
91
92 $ret_arr = array();
93
94 for ($i = 0; $i >= -4; $i--) {
95
96 $count = getFeedUnread($i);
97
98 if ($i == 0 || $i == -1 || $i == -2)
99 $auxctr = Feeds::getFeedArticles($i, false);
100 else
101 $auxctr = 0;
102
103 $cv = array("id" => $i,
104 "counter" => (int) $count,
105 "auxcounter" => (int) $auxctr);
106
107// if (get_pref('EXTENDED_FEEDLIST'))
108// $cv["xmsg"] = getFeedArticles($i)." ".__("total");
109
110 array_push($ret_arr, $cv);
111 }
112
113 $feeds = PluginHost::getInstance()->get_feeds(-1);
114
115 if (is_array($feeds)) {
116 foreach ($feeds as $feed) {
117 $cv = array("id" => PluginHost::pfeed_to_feed_id($feed['id']),
118 "counter" => $feed['sender']->get_unread($feed['id']));
119
120 if (method_exists($feed['sender'], 'get_total'))
121 $cv["auxcounter"] = $feed['sender']->get_total($feed['id']);
122
123 array_push($ret_arr, $cv);
124 }
125 }
126
127 return $ret_arr;
128 }
129
130 static function getLabelCounters($descriptions = false) {
131
132 $ret_arr = array();
133
a25ac0d7 134 $pdo = Db::pdo();
65af3b2c 135
a25ac0d7 136 $sth = $pdo->prepare("SELECT id,caption,SUM(CASE WHEN u1.unread = true THEN 1 ELSE 0 END) AS unread, COUNT(u1.unread) AS total
65af3b2c
AD
137 FROM ttrss_labels2 LEFT JOIN ttrss_user_labels2 ON
138 (ttrss_labels2.id = label_id)
139 LEFT JOIN ttrss_user_entries AS u1 ON u1.ref_id = article_id
a25ac0d7 140 WHERE ttrss_labels2.owner_uid = :uid AND u1.owner_uid = :uid
65af3b2c
AD
141 GROUP BY ttrss_labels2.id,
142 ttrss_labels2.caption");
a25ac0d7 143 $sth->execute([":uid" => $_SESSION['uid']]);
65af3b2c 144
a25ac0d7 145 while ($line = $sth->fetch()) {
65af3b2c
AD
146
147 $id = Labels::label_to_feed_id($line["id"]);
148
149 $cv = array("id" => $id,
150 "counter" => (int) $line["unread"],
151 "auxcounter" => (int) $line["total"]);
152
153 if ($descriptions)
154 $cv["description"] = $line["caption"];
155
156 array_push($ret_arr, $cv);
157 }
158
159 return $ret_arr;
160 }
161
162 static function getFeedCounters($active_feed = false) {
163
164 $ret_arr = array();
165
a25ac0d7
AD
166 $pdo = Db::pdo();
167
168 $sth = $pdo->prepare("SELECT ttrss_feeds.id,
65af3b2c
AD
169 ttrss_feeds.title,
170 ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
171 last_error, value AS count
172 FROM ttrss_feeds, ttrss_counters_cache
a25ac0d7 173 WHERE ttrss_feeds.owner_uid = ?
65af3b2c 174 AND ttrss_counters_cache.owner_uid = ttrss_feeds.owner_uid
a25ac0d7
AD
175 AND ttrss_counters_cache.feed_id = id");
176 $sth->execute([$_SESSION['uid']]);
65af3b2c 177
a25ac0d7 178 while ($line = $sth->fetch()) {
65af3b2c
AD
179
180 $id = $line["id"];
181 $count = $line["count"];
182 $last_error = htmlspecialchars($line["last_error"]);
183
184 $last_updated = make_local_datetime($line['last_updated'], false);
185
fa3bcfa3
AD
186 if (Feeds::feedHasIcon($id)) {
187 $has_img = filemtime(Feeds::getIconFile($id));
188 } else {
189 $has_img = false;
190 }
65af3b2c
AD
191
192 if (date('Y') - date('Y', strtotime($line['last_updated'])) > 2)
193 $last_updated = '';
194
195 $cv = array("id" => $id,
196 "updated" => $last_updated,
197 "counter" => (int) $count,
198 "has_img" => (int) $has_img);
199
200 if ($last_error)
201 $cv["error"] = $last_error;
202
203// if (get_pref('EXTENDED_FEEDLIST'))
204// $cv["xmsg"] = getFeedArticles($id)." ".__("total");
205
206 if ($active_feed && $id == $active_feed)
207 $cv["title"] = truncate_string($line["title"], 30);
208
209 array_push($ret_arr, $cv);
210
211 }
212
213 return $ret_arr;
214 }
215
216}