]> git.wh0rd.org - tt-rss.git/blob - functions.php
fixed bug in handling null last_read values on headline update
[tt-rss.git] / functions.php
1 <?
2 require_once 'config.php';
3
4 function update_all_feeds($link, $fetch) {
5
6 if (!$fetch) {
7
8 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds WHERE
9 last_updated is null OR title = '' OR
10 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) > " .
11 MIN_UPDATE_TIME);
12
13 } else {
14
15 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds");
16 }
17
18 $num_unread = 0;
19
20 while ($line = pg_fetch_assoc($result)) {
21 $num_unread += update_rss_feed($link, $line["feed_url"], $line["id"]);
22 }
23
24 }
25
26 function update_rss_feed($link, $feed_url, $feed) {
27
28 $rss = fetch_rss($feed_url);
29
30 $num_unread = 0;
31
32 if ($rss) {
33
34 $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
35
36 $registered_title = pg_fetch_result($result, 0, "title");
37
38 if (!$registered_title) {
39
40 $feed_title = $rss->channel["title"];
41 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
42 }
43
44 foreach ($rss->items as $item) {
45
46 $entry_guid = $item["id"];
47
48 if (!$entry_guid) $entry_guid = $item["guid"];
49 if (!$entry_guid) $entry_guid = $item["link"];
50
51 $entry_timestamp = "";
52
53 $rss_2_date = $item['pubdate'];
54 $rss_1_date = $item['dc']['date'];
55 $atom_date = $item['issued'];
56
57 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
58 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
59 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
60 if ($entry_timestamp == "") $entry_timestamp = 0;
61
62 if (!$entry_timestamp) continue;
63
64 $entry_title = $item["title"];
65 $entry_link = $item["link"];
66
67 if (!$entry_title) continue;
68 if (!$entry_link) continue;
69
70 $entry_content = $item["description"];
71 if (!$entry_content) $entry_content = $item["content"];
72
73 $entry_content = pg_escape_string($entry_content);
74 $entry_title = pg_escape_string($entry_title);
75
76 $content_md5 = md5($entry_content);
77
78 $result = pg_query($link, "
79 SELECT
80 id,unread,md5_hash,last_read,
81 EXTRACT(EPOCH FROM updated) as updated_timestamp
82 FROM
83 ttrss_entries
84 WHERE
85 guid = '$entry_guid' OR md5_hash = '$content_md5'");
86
87 if (pg_num_rows($result) == 0) {
88
89 $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
90
91 $query = "INSERT INTO ttrss_entries
92 (title, guid, link, updated, content, feed_id, md5_hash)
93 VALUES
94 ('$entry_title', '$entry_guid', '$entry_link',
95 '$entry_timestamp', '$entry_content', '$feed',
96 '$content_md5')";
97
98 $result = pg_query($link, $query);
99
100 if ($result) ++$num_unread;
101
102 } else {
103
104 $entry_id = pg_fetch_result($result, 0, "id");
105 $updated_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
106 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
107 $last_read = pg_fetch_result($result, 0, "last_read");
108
109 $unread = pg_fetch_result($result, 0, "unread");
110 $md5_hash = pg_fetch_result($result, 0, "md5_hash");
111
112 // if ($md5_hash != $content_md5 && CONTENT_CHECK_MD5)
113 // $unread = "true";
114
115 if (!$last_read || $md5_hash != $content_md5) {
116 $last_read = 'null';
117 } else {
118 $last_read = "'$last_read'";
119 }
120
121 // if ($unread || !CONTENT_CHECK_MD5) {
122 // $updated_query_part = "updated = '$entry_timestamp',";
123 // }
124
125 // if ($updated_timestamp > $entry_timestamp) {
126 // $unread = "true";
127 // print "$updated_timestamp : $entry_timestamp<br>";
128 // }
129
130 $query = "UPDATE ttrss_entries
131 SET
132 title ='$entry_title',
133 link = '$entry_link',
134 updated = '$entry_timestamp_fmt',
135 content = '$entry_content',
136 md5_hash = '$content_md5',
137 last_read = $last_read,
138 unread = '$unread'
139 WHERE
140 id = '$entry_id'";
141
142 $result = pg_query($link, $query);
143
144 if ($result) ++$num_unread;
145
146 }
147
148 }
149
150 if ($result) {
151 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
152 }
153
154 }
155
156 }
157
158
159
160
161 ?>