]> git.wh0rd.org Git - tt-rss.git/blob - backend.php
use transactions in feed update process, better handle feeds without entry dates...
[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 == "rpc") {
17
18                 $subop = $_GET["subop"];
19
20                 if ($subop == "forceUpdateAll") {
21                         update_all_feeds($link, true);
22                 }
23
24                 if ($subop == "updateAll") {
25                         update_all_feeds($link, false);
26                 }
27                 
28                 if ($subop == "catchupPage") {
29
30                         $ids = split(",", $_GET["ids"]);
31
32                         foreach ($ids as $id) {
33
34                                 pg_query("UPDATE ttrss_entries SET unread=false,last_read = NOW()
35                                         WHERE id = '$id'");
36
37                         }
38
39                         print "Marked active page as read.";
40                 }
41
42         }
43         
44         if ($op == "feeds") {
45
46                 $subop = $_GET["subop"];
47
48                 if ($subop == "catchupAll") {
49                         pg_query("UPDATE ttrss_entries SET last_read = NOW(),unread = false");
50                 }
51
52                 update_all_feeds($link, $fetch);
53                 
54                 $result = pg_query("SELECT *,
55                         (SELECT count(id) FROM ttrss_entries 
56                                 WHERE feed_id = ttrss_feeds.id) AS total,
57                         (SELECT count(id) FROM ttrss_entries
58                                 WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
59                         FROM ttrss_feeds ORDER BY title");                      
60
61                 print "<table width=\"100%\" class=\"feeds\">";
62
63                 $lnum = 0;
64
65                 $total_unread = 0;
66
67                 while ($line = pg_fetch_assoc($result)) {
68                 
69                         $feed = $line["title"];
70                         $feed_id = $line["id"];   
71                         
72                         $total = $line["total"];
73                         $unread = $line["unread"];
74                         
75                         $class = ($lnum % 2) ? "even" : "odd";
76
77                         if ($unread > 0) $class .= "Unread";
78
79                         $total_unread += $unread;
80
81                         print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
82
83                         $feed = "<a href=\"javascript:viewfeed($feed_id, 0);\">$feed</a>";
84                         print "<td id=\"FEEDN-$feed_id\">$feed</td>";
85                         print "<td>";
86                         print "<span id=\"FEEDU-$feed_id\">$unread</span>&nbsp;/&nbsp;";
87                         print "<span id=\"FEEDT-$feed_id\">$total</span>";
88                         print "</td>";
89
90                         print "</tr>";
91                         ++$lnum;
92                 }
93
94 //              print "<tr><td class=\"footer\" colspan=\"3\">
95 //                      <a href=\"javascript:update_feed_list(false,true)\">Update all feeds</a></td></tr>";
96
97 //              print "<tr><td class=\"footer\" colspan=\"2\">&nbsp;";
98 //              print "</td></tr>";
99
100                 print "</table>";
101
102                 print "<p align=\"center\">All feeds: 
103                         <a class=\"button\" 
104                                 href=\"javascript:updateFeedList(false,true)\">Update</a>";
105
106                 print "&nbsp;<a class=\"button\" 
107                                 href=\"javascript:catchupAllFeeds()\">Mark as read</a></p>";
108
109                 print "<div class=\"invisible\" id=\"FEEDTU\">$total_unread</div>";
110
111         }
112
113         if ($op == "view") {
114
115                 $id = $_GET["id"];
116
117                 $result = pg_query("UPDATE ttrss_entries SET unread = false,last_read = NOW() WHERE id = '$id'");
118
119                 $result = pg_query("SELECT title,link,content FROM ttrss_entries
120                         WHERE   id = '$id'");
121
122                 if ($result) {
123
124                         $line = pg_fetch_assoc($result);
125
126                         print "<table class=\"feedOverview\">";
127                         print "<tr><td><b>Title:</b></td><td>".$line["title"]."</td></tr>";
128                         print "<tr><td><b>Link:</b></td><td><a href=\"".$line["link"]."\">".$line["link"]."</a></td></tr>";
129
130                         print "</table>";
131
132                         print $line["content"];
133
134
135                 }
136         }
137
138         if ($op == "viewfeed") {
139
140                 $feed = $_GET["feed"];
141                 $skip = $_GET["skip"];
142                 $subop = $_GET["subop"];
143
144                 if (!$skip) $skip = 0;
145
146                 if ($subop == "undefined") $subop = "";
147
148                 // FIXME: check for null value here
149
150                 $result = pg_query("SELECT *,
151                         EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) as update_timeout
152                         FROM ttrss_feeds WHERE id = '$feed'");
153
154                 if ($result) {
155
156                         $line = pg_fetch_assoc($result);
157
158                         if ($subop == "ForceUpdate" || 
159                                 (!$subop && $line["update_timeout"] > MIN_UPDATE_TIME)) {
160                                 
161                                 update_rss_feed($link, $line["feed_url"], $feed);
162
163                         } else {
164
165                                 if ($subop == "MarkAllRead")  {
166
167                                         pg_query("UPDATE ttrss_entries SET unread = false,last_read = NOW() 
168                                                 WHERE feed_id = '$feed'");
169                                 }
170
171                                 if ($subop == "MarkPageRead")  {
172
173 //                                      pg_query("UPDATE ttrss_entries SET unread = false 
174 //                                              WHERE feed_id = '$feed' ORDER BY updated OFFSET $skip LIMIT 1");
175                                 }
176
177                         }
178                 }
179
180                 print "<table class=\"headlinesList\" id=\"headlinesList\" width=\"100%\">";
181
182                 print "<tr><td class=\"search\" colspan=\"3\">
183                         Search: <input onchange=\"javascript:search($feed,this);\"></td></tr>"; 
184                 print "<tr><td colspan=\"3\" class=\"title\">" . $line["title"] . "</td></tr>"; 
185
186                 if ($ext == "SEARCH") {
187                         $search = $_GET["search"];
188                         $search_query_part = "(upper(title) LIKE upper('%$search%') 
189                                 OR content LIKE '%$search%') AND";
190                 }
191
192                 $result = pg_query("SELECT count(id) AS total_entries 
193                         FROM ttrss_entries WHERE feed_id = '$feed'");
194
195                 $total_entries = pg_fetch_result($result, 0, "total_entries");
196
197                 $result = pg_query("SELECT 
198                                 id,title,updated,unread,feed_id,
199                                 EXTRACT(EPOCH FROM last_read) AS last_read_ts,
200                                 EXTRACT(EPOCH FROM updated) AS updated_ts
201                         FROM
202                                 ttrss_entries 
203                         WHERE
204                         $search_query_part
205                         feed_id = '$feed' ORDER BY updated DESC LIMIT ".HEADLINES_PER_PAGE." OFFSET $skip");
206
207                 $lnum = 0;
208
209                 while ($line = pg_fetch_assoc($result)) {
210
211                         $class = ($lnum % 2) ? "even" : "odd";
212
213                         if ($line["last_read_ts"] < $line["updated_ts"] && $line["unread"] == "f") {
214                                 $update_pic = "<img src=\"updated.png\" alt=\"Updated\">";
215                         } else {
216                                 $update_pic = "&nbsp;";
217                         }
218
219                         if ($line["unread"] == "t") 
220                                 $class .= "Unread";
221
222                         $id = $line["id"];
223                         $feed_id = $line["feed_id"];
224
225                         $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
226                                 $line["title"] . "</a>";
227                                 
228                         print "<tr class='$class' id='RROW-$id'>";
229
230                         print "<td id='FUPDPIC-$id' valign='center' class='headlineUpdateMark'>$update_pic</td>";
231
232                         print "<td class='headlineUpdated'>".$line["updated"]."</td>";
233                         print "<td class='headlineTitle'>$content_link</td>";
234
235                         print "</tr>";
236
237                         ++$lnum;
238                 }
239
240                 if ($lnum == 0) {
241                         print "<tr><td align='center'>No entries found.</td></tr>";
242
243                 }
244
245                 print "<tr><td colspan=\"3\" class=\"headlineToolbar\">";
246
247                 $next_skip = $skip + HEADLINES_PER_PAGE;
248                 $prev_skip = $skip - HEADLINES_PER_PAGE;
249         
250                 print "Navigate: ";
251
252                 if ($prev_skip >= 0) {
253                         print "<a class=\"button\" 
254                                 href=\"javascript:viewfeed($feed, $prev_skip);\">Previous Page</a>";
255                 } else {
256                         print "<a class=\"disabledButton\">Previous Page</a>";
257                 }
258                 print "&nbsp;";
259
260                 if ($next_skip < $total_entries) {              
261                         print "<a class=\"button\" 
262                                 href=\"javascript:viewfeed($feed, $next_skip);\">Next Page</a>";
263                 } else {
264                         print "<a class=\"disabledButton\">Next Page</a>";
265                 }                       
266                 print "&nbsp;&nbsp;Feed: ";
267 //              print "<a class=\"button\" 
268 //                      href=\"javascript:viewfeed($feed, $skip, '');\">Refresh Page</a>";
269 //              print "&nbsp;";
270
271                 print "<a class=\"button\" 
272                         href=\"javascript:viewfeed($feed, 0, 'ForceUpdate');\">Update</a>";
273                 
274                 print "&nbsp;&nbsp;Mark as read: ";
275                 
276                 print "<a class=\"button\" 
277                         href=\"javascript:catchupPage($feed);\">This Page</a>";
278                 print "&nbsp;";
279                 print "<a class=\"button\" 
280                         href=\"javascript:viewfeed($feed, $skip, 'MarkAllRead');\">All Posts</a>";
281
282                 print "</td></tr>";
283                 print "</table>";
284
285                 $result = pg_query("SELECT id, (SELECT count(id) FROM ttrss_entries 
286                         WHERE feed_id = ttrss_feeds.id) AS total,
287                 (SELECT count(id) FROM ttrss_entries
288                         WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
289                 FROM ttrss_feeds WHERE id = '$feed'");                  
290
291                 $total = pg_fetch_result($result, 0, "total");
292                 $unread = pg_fetch_result($result, 0, "unread");
293
294                 print "<div class=\"invisible\" id=\"FACTIVE\">$feed</div>";
295                 print "<div class=\"invisible\" id=\"FTOTAL\">$total</div>";
296                 print "<div class=\"invisible\" id=\"FUNREAD\">$unread</div>";
297
298         }
299
300         if ($op == "pref-rpc") {
301
302                 $subop = $_GET["subop"];
303
304                 if ($subop == "unread") {
305                         $ids = split(",", $_GET["ids"]);
306                         foreach ($ids as $id) {
307                                 pg_query("UPDATE ttrss_entries SET unread = true WHERE feed_id = '$id'");
308                         }
309
310                         print "Marked selected feeds as read.";
311                 }
312
313                 if ($subop == "read") {
314                         $ids = split(",", $_GET["ids"]);
315                         foreach ($ids as $id) {
316                                 pg_query("UPDATE ttrss_entries 
317                                         SET unread = false,last_read = NOW() WHERE feed_id = '$id'");
318                         }
319
320                         print "Marked selected feeds as unread.";
321
322                 }
323
324         }
325
326         if ($op == "pref-feeds") {
327         
328                 $subop = $_GET["subop"];
329
330                 if ($subop == "edit") {
331                         print "<p>[Edit feed placeholder]</p>";
332                 }
333
334                 if ($subop == "remove") {
335                         $ids = split(",", $_GET["ids"]);
336
337                         foreach ($ids as $id) {
338                                 pg_query("BEGIN");
339                                 pg_query("DELETE FROM ttrss_entries WHERE feed_id = '$id'");
340                                 pg_query("DELETE FROM ttrss_feeds WHERE id = '$id'");
341                                 pg_query("COMMIT");
342
343                         }
344                 }
345
346                 if ($subop == "add") {
347                         $feed_link = pg_escape_string($_GET["link"]);
348                                 
349                         $result = pg_query(
350                                 "INSERT INTO ttrss_feeds (feed_url,title) VALUES ('$feed_link', '')");
351
352                         $result = pg_query("SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'");
353
354                         $feed_id = pg_fetch_result($result, 0, "id");
355
356                         if ($feed_id) {
357                                 update_rss_feed($link, $feed_link, $feed_id);
358                         }
359
360                 }
361         
362                 $result = pg_query("SELECT * FROM ttrss_feeds ORDER by title");
363
364                 print "<p><table width=\"100%\" class=\"prefFeedList\" id=\"prefFeedList\">";
365                 print "<tr class=\"title\">
366                                         <td>Select</td><td>Title</td><td>Link</td><td>Last Updated</td></tr>";
367                 
368                 $lnum = 0;
369                 
370                 while ($line = pg_fetch_assoc($result)) {
371
372                         $class = ($lnum % 2) ? "even" : "odd";
373                         
374                         $feed_id = $line["id"];
375                         
376                         print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
377
378                         print "<td><input onclick='toggleSelectRow(this);' 
379                                 type=\"checkbox\" id=\"FRCHK-".$line["id"]."\"></td>";
380                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
381                                 $line["title"] . "</td>";               
382                         print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
383                                 $line["feed_url"] . "</td>";            
384                                 
385                         print "<td>" . $line["last_updated"] . "</td>";
386                         print "</tr>";
387
388                         ++$lnum;
389                 }
390
391                 print "</table>";
392
393         }
394
395         pg_close($link);
396 ?>