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
);
38 $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
40 $registered_title = pg_fetch_result($result, 0, "title");
42 if (!$registered_title) {
43 $feed_title = $rss->channel
["title"];
44 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
47 foreach ($rss->items
as $item) {
49 $entry_guid = $item["id"];
51 if (!$entry_guid) $entry_guid = $item["guid"];
52 if (!$entry_guid) $entry_guid = $item["link"];
54 if (!$entry_guid) continue;
56 $entry_timestamp = "";
58 $rss_2_date = $item['pubdate'];
59 $rss_1_date = $item['dc']['date'];
60 $atom_date = $item['issued'];
62 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
63 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
64 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
66 if ($entry_timestamp == "") {
67 $entry_timestamp = time();
68 $no_orig_date = 'true';
70 $no_orig_date = 'false';
73 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
75 $entry_title = $item["title"];
76 $entry_link = $item["link"];
78 if (!$entry_title) continue;
79 if (!$entry_link) continue;
81 $entry_content = $item["description"];
82 if (!$entry_content) $entry_content = $item["content:escaped"];
83 if (!$entry_content) $entry_content = $item["content"];
85 if (!$entry_content) continue;
87 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
89 $result = pg_query($link, "
91 id,last_read,no_orig_date,title,feed_id,content_hash,
92 EXTRACT(EPOCH FROM updated) as updated_timestamp
96 guid = '$entry_guid'");
98 if (pg_num_rows($result) == 0) {
100 $entry_content = pg_escape_string($entry_content);
101 $entry_title = pg_escape_string($entry_title);
102 $entry_link = pg_escape_string($entry_link);
118 '$entry_timestamp_fmt',
124 $result = pg_query($link, $query);
128 $orig_entry_id = pg_fetch_result($result, 0, "id");
129 $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
131 if ($orig_feed_id != $feed) {
132 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
136 $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
137 $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
138 $orig_last_read = pg_fetch_result($result, 0, "last_read");
139 $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
140 $orig_title = pg_fetch_result($result, 0, "title");
142 if ($orig_title != $entry_title) {
143 $last_read_qpart = 'last_read = null,';
146 if ($orig_content_hash != $content_hash) {
147 $last_read_qpart = 'last_read = null,';
150 // if ($orig_timestamp < $entry_timestamp) {
151 // $last_read_qpart = 'last_read = null,';
154 $entry_content = pg_escape_string($entry_content);
155 $entry_title = pg_escape_string($entry_title);
156 $entry_link = pg_escape_string($entry_link);
158 $query = "UPDATE ttrss_entries
161 title = '$entry_title',
162 link = '$entry_link',
163 updated = '$entry_timestamp_fmt',
164 content = '$entry_content',
165 content_hash = '$content_hash'
167 id = '$orig_entry_id'";
169 $result = pg_query($link, $query);
175 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");