]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
prefs RPC optimizations
[tt-rss.git] / backend.php
1 <?
2         header("Content-Type: application/xml");
3
4         require_once "config.php";
5         require_once "functions.php";
6         require_once "magpierss/rss_fetch.inc";
7
8
9         $link = pg_connect(DB_CONN);    
10
11         pg_query("set client_encoding = 'utf-8'");
12
13         $op = $_GET["op"];
14         $fetch = $_GET["fetch"];
15                 
16         if ($op == "feeds") {
17
18                 if ($fetch) update_all_feeds($link);
19
20                 $result = pg_query("SELECT *,
21                         (SELECT count(id) FROM ttrss_entries 
22                                 WHERE feed_id = ttrss_feeds.id) AS total,
23                         (SELECT count(id) FROM ttrss_entries
24                                 WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
25                         FROM ttrss_feeds ORDER BY title");                      
26
27                 print "<table width=\"100%\" class=\"feeds\">";
28
29                 $lnum = 0;
30
31                 while ($line = pg_fetch_assoc($result)) {
32                 
33                         $feed = $line["title"];
34                         $feed_id = $line["id"];   
35                         
36                         $total = $line["total"];
37                         $unread = $line["unread"];
38                         
39                         $class = ($lnum % 2) ? "even" : "odd";
40
41                         if ($unread > 0) $class .= "Unread";
42
43                         print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
44
45                         $feed = "<a href=\"javascript:viewfeed($feed_id, 0);\">$feed</a>";
46                         print "<td id=\"FEEDN-$feed_id\">$feed</td>";
47                         print "<td>";
48                         print "<span id=\"FEEDU-$feed_id\">$unread</span>&nbsp;/&nbsp;";
49                         print "<span id=\"FEEDT-$feed_id\">$total</span>";
50                         print "</td>";
51
52                         print "</tr>";
53                         ++$lnum;
54                 }
55
56                 print "<tr><td class=\"footer\" colspan=\"3\">
57                         <a href=\"javascript:update_feed_list(false,true)\">Update all feeds</a></td></tr>";
58
59                 print "</table>";
60
61         }
62
63         if ($op == "view") {
64
65                 $id = $_GET["id"];
66
67                 $result = pg_query("UPDATE ttrss_entries SET unread = false WHERE id = '$id'");
68
69                 $result = pg_query("SELECT title,link,content FROM ttrss_entries
70                         WHERE   id = '$id'");
71
72                 if ($result) {
73
74                         $line = pg_fetch_assoc($result);
75
76                         print "<table class=\"feedOverview\">";
77                         print "<tr><td><b>Title:</b></td><td>".$line["title"]."</td></tr>";
78                         print "<tr><td><b>Link:</b></td><td><a href=\"".$line["link"]."\">".$line["link"]."</a></td></tr>";
79
80                         print "</table>";
81
82                         print $line["content"];
83
84
85                 }
86         }
87
88         if ($op == "viewfeed") {
89
90                 $feed = $_GET["feed"];
91                 $skip = $_GET["skip"];
92                 $ext = $_GET["ext"];
93
94                 if (!$skip) $skip = 0;
95
96                 if ($ext == "undefined") $ext = "";
97
98                 // FIXME: check for null value here
99
100                 $result = pg_query("SELECT *,
101                         EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) as update_timeout
102                         FROM ttrss_feeds WHERE id = '$feed'");
103
104                 if ($result) {
105
106                         $line = pg_fetch_assoc($result);
107
108                         if (!$ext && $line["update_timeout"] > MIN_UPDATE_TIME) {
109                                 
110                                 update_rss_feed($link, $line["feed_url"], $feed);
111
112                         } else {
113
114                                 if ($ext == "MarkAllRead")  {
115
116                                         pg_query("UPDATE ttrss_entries SET unread = false 
117                                                 WHERE feed_id = '$feed'");
118                                 }
119
120                                 if ($ext == "MarkPageRead")  {
121
122 //                                      pg_query("UPDATE ttrss_entries SET unread = false 
123 //                                              WHERE feed_id = '$feed' ORDER BY updated OFFSET $skip LIMIT 1");
124                                 }
125
126                         }
127                 }
128
129                 print "<table class=\"headlines\" width=\"100%\">";
130
131                 print "<tr><td class=\"search\" colspan=\"2\">
132                         Search: <input onchange=\"javascript:search($feed,this);\"></td></tr>"; 
133                 print "<tr><td colspan=\"2\" class=\"title\">" . $line["title"] . "</td></tr>"; 
134
135                 if ($ext == "SEARCH") {
136                         $search = $_GET["search"];
137                         $search_query_part = "(upper(title) LIKE upper('%$search%') 
138                                 OR content LIKE '%$search%') AND";
139                 }
140
141                 $result = pg_query("SELECT id,title,updated,unread,feed_id FROM
142                         ttrss_entries WHERE
143                         $search_query_part
144                         feed_id = '$feed' ORDER BY updated DESC LIMIT ".HEADLINES_PER_PAGE." OFFSET $skip");
145
146                 $lnum = 0;
147
148                 while ($line = pg_fetch_assoc($result)) {
149
150                         $class = ($lnum % 2) ? "even" : "odd";
151
152                         if ($line["unread"] == "t") 
153                                 $class .= "Unread";
154
155                         $content_link = "<a href=\"javascript:view(".$line["id"].",".$line["feed_id"].");\">".$line["title"]."</a>";
156                         
157                         print "<tr class='$class' id='RROW-".$line["id"]."'>";
158                         print "<td class='headlineUpdated'>".$line["updated"]."</td>";
159                         print "<td class='headlineTitle'>$content_link</td>";
160
161                         print "</tr>";
162
163                         ++$lnum;
164                 }
165
166                 if ($lnum == 0) {
167                         print "<tr><td align='center'>No entries found.</td></tr>";
168
169                 }
170
171                 print "<tr><td colspan=\"2\" class=\"headlineToolbar\">";
172
173                 $next_skip = $skip + HEADLINES_PER_PAGE;
174                 $prev_skip = $skip - HEADLINES_PER_PAGE;
175         
176                 print "Navigate: ";
177                 print "<a class=\"button\" 
178                         href=\"javascript:viewfeed($feed, $prev_skip);\">Previous Page</a>";
179                 print "&nbsp;";
180                 print "<a class=\"button\" 
181                         href=\"javascript:viewfeed($feed, $next_skip);\">Next Page</a>";
182                 print "&nbsp;";
183                 print "<a class=\"button\" 
184                         href=\"javascript:viewfeed($feed, $skip, '');\">Refresh</a>";
185                 print "&nbsp;&nbsp;Mark as read: ";
186                 print "<a class=\"button\" 
187                         href=\"javascript:viewfeed($feed, $skip, 'MarkPageRead');\">This Page</a>";
188                 print "&nbsp;";
189                 print "<a class=\"button\" 
190                         href=\"javascript:viewfeed($feed, $skip, 'MarkAllRead');\">All Posts</a>";
191
192                 print "</td></tr>";
193                 print "</table>";
194
195                 $result = pg_query("SELECT id, (SELECT count(id) FROM ttrss_entries 
196                         WHERE feed_id = ttrss_feeds.id) AS total,
197                 (SELECT count(id) FROM ttrss_entries
198                         WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
199                 FROM ttrss_feeds WHERE id = '$feed'");                  
200
201                 $total = pg_fetch_result($result, 0, "total");
202                 $unread = pg_fetch_result($result, 0, "unread");
203
204                 print "<div class=\"invisible\" id=\"FACTIVE\">$feed</div>";
205                 print "<div class=\"invisible\" id=\"FTOTAL\">$total</div>";
206                 print "<div class=\"invisible\" id=\"FUNREAD\">$unread</div>";
207
208         }
209
210         if ($op == "pref-rpc") {
211
212                 $subop = $_GET["subop"];
213
214                 if ($subop == "unread") {
215                         $ids = split(",", $_GET["ids"]);
216                         foreach ($ids as $id) {
217                                 pg_query("UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
218                         }
219
220                         print "Marked selected feeds as read.";
221                 }
222
223                 if ($subop == "read") {
224                         $ids = split(",", $_GET["ids"]);
225                         foreach ($ids as $id) {
226                                 pg_query("UPDATE ttrss_entries SET unread = false WHERE feed_id = '$id'");
227                         }
228
229                         print "Marked selected feeds as unread.";
230
231                 }
232
233         }
234
235         if ($op == "pref-feeds") {
236         
237                 $subop = $_GET["subop"];
238
239                 if ($subop == "edit") {
240                         print "<p>[Edit feed placeholder]</p>";
241                 }
242
243                 if ($subop == "remove") {
244                         $ids = split(",", $_GET["ids"]);
245
246                         foreach ($ids as $id) {
247                                 pg_query("BEGIN");
248                                 pg_query("DELETE FROM ttrss_entries WHERE feed_id = '$id'");
249                                 pg_query("DELETE FROM ttrss_feeds WHERE id = '$id'");
250                                 pg_query("COMMIT");
251
252                         }
253                 }
254
255                 if ($subop == "add") {
256                         $feed_link = pg_escape_string($_GET["link"]);
257                                 
258                         $result = pg_query(
259                                 "INSERT INTO ttrss_feeds (feed_url,title) VALUES ('$feed_link', '')");
260
261                         $result = pg_query("SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
262
263                         $feed_id = pg_fetch_result($result, 0, "id");
264
265                         if ($feed_id) {
266                                 update_rss_feed($link, $feed_link, $feed_id);
267                         }
268
269                 }
270         
271                 $result = pg_query("SELECT * FROM ttrss_feeds ORDER by title");
272
273                 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
274                 print "<tr class=\"title\">
275                                         <td>Select</td><td>Title</td><td>Link</td><td>Last Updated</td></tr>";
276                 
277                 $lnum = 0;
278                 
279                 while ($line = pg_fetch_assoc($result)) {
280
281                         $class = ($lnum % 2) ? "even" : "odd";
282                         
283                         $feed_id = $line["id"];
284                         
285                         print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
286
287                         print "<td><input onclick='toggleSelectRow(this);' 
288                                 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
289                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
290                                 $line["title"] . "</td>";               
291                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
292                                 $line["feed_url"] . "</td>";            
293                                 
294                         print "<td>" . $line["last_updated"] . "</td>";
295                         print "</tr>";
296
297                         ++$lnum;
298                 }
299
300                 print "</table>";
301
302         }
303
304         pg_close($link);
305 ?>