2 require_once 'config.php';
4 function update_all_feeds($link, $fetch) {
10 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds WHERE
11 last_updated is null OR title = '' OR
12 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) > " .
17 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds");
20 while ($line = pg_fetch_assoc($result)) {
21 update_rss_feed($link, $line["feed_url"], $line["id"]);
28 function update_rss_feed($link, $feed_url, $feed) {
31 $rss = fetch_rss($feed_url);
32 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
37 $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
39 $registered_title = pg_fetch_result($result, 0, "title");
41 if (!$registered_title) {
42 $feed_title = $rss->channel
["title"];
43 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
46 foreach ($rss->items
as $item) {
48 $entry_guid = $item["id"];
50 if (!$entry_guid) $entry_guid = $item["guid"];
51 if (!$entry_guid) $entry_guid = $item["link"];
53 if (!$entry_guid) continue;
55 $entry_timestamp = "";
57 $rss_2_date = $item['pubdate'];
58 $rss_1_date = $item['dc']['date'];
59 $atom_date = $item['issued'];
61 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
62 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
63 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
65 if ($entry_timestamp == "") {
66 $entry_timestamp = time();
67 $no_orig_date = 'true';
69 $no_orig_date = 'false';
72 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
74 $entry_title = $item["title"];
75 $entry_link = $item["link"];
77 if (!$entry_title) continue;
78 if (!$entry_link) continue;
80 $entry_content = $item["description"];
81 if (!$entry_content) $entry_content = $item["content:escaped"];
82 if (!$entry_content) $entry_content = $item["content"];
84 if (!$entry_content) continue;
86 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
88 $result = pg_query($link, "
90 id,last_read,no_orig_date,title,feed_id,content_hash,
91 EXTRACT(EPOCH FROM updated) as updated_timestamp
95 guid = '$entry_guid'");
97 if (pg_num_rows($result) == 0) {
99 $entry_content = pg_escape_string($entry_content);
100 $entry_title = pg_escape_string($entry_title);
101 $entry_link = pg_escape_string($entry_link);
117 '$entry_timestamp_fmt',
123 $result = pg_query($link, $query);
127 $orig_entry_id = pg_fetch_result($result, 0, "id");
128 $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
130 if ($orig_feed_id != $feed) {
131 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
135 $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
136 $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
137 $orig_last_read = pg_fetch_result($result, 0, "last_read");
138 $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
139 $orig_title = pg_fetch_result($result, 0, "title");
141 if ($orig_title != $entry_title) {
142 $last_read_qpart = 'last_read = null,';
145 if ($orig_content_hash != $content_hash) {
146 $last_read_qpart = 'last_read = null,';
149 if ($orig_timestamp < $entry_timestamp) {
150 $last_read_qpart = 'last_read = null,';
153 $entry_content = pg_escape_string($entry_content);
154 $entry_title = pg_escape_string($entry_title);
155 $entry_link = pg_escape_string($entry_link);
157 $query = "UPDATE ttrss_entries
160 title = '$entry_title',
161 link = '$entry_link',
162 updated = '$entry_timestamp_fmt',
163 content = '$entry_content',
164 content_hash = '$content_hash'
166 id = '$orig_entry_id'";
168 $result = pg_query($link, $query);
174 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");