]>
Commit | Line | Data |
---|---|---|
40d13c28 AD |
1 | <? |
2 | require_once 'config.php'; | |
3 | ||
9c9c7e6b | 4 | function update_all_feeds($link, $fetch) { |
40d13c28 | 5 | |
9c9c7e6b AD |
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 | } | |
40d13c28 | 17 | |
76798ff3 AD |
18 | $num_unread = 0; |
19 | ||
40d13c28 | 20 | while ($line = pg_fetch_assoc($result)) { |
76798ff3 | 21 | $num_unread += update_rss_feed($link, $line["feed_url"], $line["id"]); |
40d13c28 AD |
22 | } |
23 | ||
24 | } | |
25 | ||
26 | function update_rss_feed($link, $feed_url, $feed) { | |
27 | ||
28 | $rss = fetch_rss($feed_url); | |
76798ff3 AD |
29 | |
30 | $num_unread = 0; | |
40d13c28 AD |
31 | |
32 | if ($rss) { | |
331900c6 AD |
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 | } | |
40d13c28 AD |
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 | ||
9c9c7e6b AD |
51 | $entry_timestamp = ""; |
52 | ||
53 | $rss_2_date = $item['pubdate']; | |
54 | $rss_1_date = $item['dc']['date']; | |
55 | $atom_date = $item['issued']; | |
b197f117 | 56 | |
9c9c7e6b AD |
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); | |
b197f117 AD |
60 | if ($entry_timestamp == "") $entry_timestamp = 0; |
61 | ||
62 | if (!$entry_timestamp) continue; | |
71ad3959 | 63 | |
40d13c28 AD |
64 | $entry_title = $item["title"]; |
65 | $entry_link = $item["link"]; | |
71ad3959 AD |
66 | |
67 | if (!$entry_title) continue; | |
68 | if (!$entry_link) continue; | |
69 | ||
40d13c28 AD |
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 | |
cac95b8d | 80 | id,unread,md5_hash,last_read, |
b197f117 | 81 | EXTRACT(EPOCH FROM updated) as updated_timestamp |
40d13c28 AD |
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 | ||
76798ff3 AD |
98 | $result = pg_query($link, $query); |
99 | ||
100 | if ($result) ++$num_unread; | |
40d13c28 AD |
101 | |
102 | } else { | |
103 | ||
104 | $entry_id = pg_fetch_result($result, 0, "id"); | |
b197f117 AD |
105 | $updated_timestamp = pg_fetch_result($result, 0, "updated_timestamp"); |
106 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
cac95b8d | 107 | $last_read = pg_fetch_result($result, 0, "last_read"); |
40d13c28 AD |
108 | |
109 | $unread = pg_fetch_result($result, 0, "unread"); | |
110 | $md5_hash = pg_fetch_result($result, 0, "md5_hash"); | |
57865bb1 | 111 | |
cac95b8d AD |
112 | // if ($md5_hash != $content_md5 && CONTENT_CHECK_MD5) |
113 | // $unread = "true"; | |
114 | ||
829396cc | 115 | if (!$last_read || $md5_hash != $content_md5) { |
cac95b8d AD |
116 | $last_read = 'null'; |
117 | } else { | |
118 | $last_read = "'$last_read'"; | |
119 | } | |
120 | ||
b197f117 AD |
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 | ||
40d13c28 AD |
130 | $query = "UPDATE ttrss_entries |
131 | SET | |
132 | title ='$entry_title', | |
133 | link = '$entry_link', | |
b197f117 | 134 | updated = '$entry_timestamp_fmt', |
40d13c28 AD |
135 | content = '$entry_content', |
136 | md5_hash = '$content_md5', | |
cac95b8d | 137 | last_read = $last_read, |
40d13c28 AD |
138 | unread = '$unread' |
139 | WHERE | |
140 | id = '$entry_id'"; | |
141 | ||
142 | $result = pg_query($link, $query); | |
143 | ||
76798ff3 | 144 | if ($result) ++$num_unread; |
40d13c28 AD |
145 | |
146 | } | |
147 | ||
148 | } | |
149 | ||
76798ff3 AD |
150 | if ($result) { |
151 | $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()"); | |
152 | } | |
40d13c28 AD |
153 | |
154 | } | |
155 | ||
156 | } | |
157 | ||
158 | ||
159 | ||
160 | ||
161 | ?> |