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");
22 while ($line = pg_fetch_assoc($result)) {
23 $num_unread +
= update_rss_feed($link, $line["feed_url"], $line["id"]);
30 function update_rss_feed($link, $feed_url, $feed) {
33 $rss = fetch_rss($feed_url);
34 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
40 $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
42 $registered_title = pg_fetch_result($result, 0, "title");
44 if (!$registered_title) {
45 $feed_title = $rss->channel
["title"];
46 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
51 foreach ($rss->items
as $item) {
53 $entry_guid = $item["id"];
55 if (!$entry_guid) $entry_guid = $item["guid"];
56 if (!$entry_guid) $entry_guid = $item["link"];
58 $entry_timestamp = "";
60 $rss_2_date = $item['pubdate'];
61 $rss_1_date = $item['dc']['date'];
62 $atom_date = $item['issued'];
64 $no_orig_date = 'false';
66 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
67 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
68 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
69 // if ($rss_3_date != "") $entry_timestamp = strtotime($rss_3_date);
71 if ($entry_timestamp == "") {
72 $entry_timestamp = time();
73 $no_orig_date = 'true';
76 if (!$entry_timestamp) continue;
78 $entry_title = $item["title"];
79 $entry_link = $item["link"];
81 if (!$entry_title) continue;
82 if (!$entry_link) continue;
84 $entry_content = $item["description"];
85 if (!$entry_content) $entry_content = $item["content"];
87 if (!$entry_content) continue;
89 $entry_content = pg_escape_string($entry_content);
90 $entry_title = pg_escape_string($entry_title);
92 $content_md5 = md5($entry_content);
94 $result = pg_query($link, "
96 id,unread,md5_hash,last_read,no_orig_date,title,
97 EXTRACT(EPOCH FROM updated) as updated_timestamp
101 guid = '$entry_guid'");
103 if (pg_num_rows($result) == 0) {
105 $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
107 $query = "INSERT INTO ttrss_entries
108 (title, guid, link, updated, content, feed_id,
109 md5_hash, no_orig_date)
111 ('$entry_title', '$entry_guid', '$entry_link',
112 '$entry_timestamp', '$entry_content', '$feed',
113 '$content_md5', $no_orig_date)";
115 $result = pg_query($link, $query);
117 if ($result) ++
$num_unread;
121 $entry_id = pg_fetch_result($result, 0, "id");
122 $updated_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
123 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
124 $last_read = pg_fetch_result($result, 0, "last_read");
126 $unread = pg_fetch_result($result, 0, "unread");
127 $md5_hash = pg_fetch_result($result, 0, "md5_hash");
128 $no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
129 $orig_title = pg_fetch_result($result, 0, "title");
131 // disable update detection for posts which didn't have correct
132 // publishment date, because they will always register as updated
133 // sadly this doesn't catch feed generators which input current date
134 // in posts all the time (some planets do this)
136 if ($no_orig_date != 't' && (!$last_read ||
$md5_hash != $content_md5)) {
137 $last_read_qpart = 'last_read = null,';
139 $last_read_qpart = '';
142 // mark post as updated on title change
143 // maybe we should mark it as unread instead?
145 if ($orig_title != $entry_title) {
146 $last_read_qpart = 'last_read = null,';
149 // don't bother updating timestamps on posts with broken pubDate
151 if ($no_orig_date != 't') {
152 $update_timestamp_qpart = "updated = '$entry_timestamp_fmt',";
155 // print "$content_md5 vs $md5_hash [$entry_title vs $orig_title, $entry_id, $feed_id]<br>";
157 if ($content_md5 != $md5_hash) {
158 $update_md5_qpart = "md5_hash = '$content_md5',";
161 $query = "UPDATE ttrss_entries
163 title ='$entry_title',
164 link = '$entry_link',
165 $update_timestamp_qpart
168 content = '$entry_content',
173 // print "<pre>".htmlspecialchars($query)."</pre>";
175 $result = pg_query($link, $query);
177 if ($result) ++
$num_unread;
184 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");