]>
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']; | |
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 = time(); | |
71ad3959 | 61 | |
40d13c28 AD |
62 | $entry_title = $item["title"]; |
63 | $entry_link = $item["link"]; | |
71ad3959 AD |
64 | |
65 | if (!$entry_title) continue; | |
66 | if (!$entry_link) continue; | |
67 | ||
40d13c28 AD |
68 | $entry_content = $item["description"]; |
69 | if (!$entry_content) $entry_content = $item["content"]; | |
70 | ||
71 | $entry_content = pg_escape_string($entry_content); | |
72 | $entry_title = pg_escape_string($entry_title); | |
73 | ||
74 | $content_md5 = md5($entry_content); | |
75 | ||
76 | $result = pg_query($link, " | |
77 | SELECT | |
78 | id,unread,md5_hash | |
79 | FROM | |
80 | ttrss_entries | |
81 | WHERE | |
82 | guid = '$entry_guid' OR md5_hash = '$content_md5'"); | |
83 | ||
84 | if (pg_num_rows($result) == 0) { | |
85 | ||
86 | $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
87 | ||
88 | $query = "INSERT INTO ttrss_entries | |
89 | (title, guid, link, updated, content, feed_id, md5_hash) | |
90 | VALUES | |
91 | ('$entry_title', '$entry_guid', '$entry_link', | |
92 | '$entry_timestamp', '$entry_content', '$feed', | |
93 | '$content_md5')"; | |
94 | ||
76798ff3 AD |
95 | $result = pg_query($link, $query); |
96 | ||
97 | if ($result) ++$num_unread; | |
40d13c28 AD |
98 | |
99 | } else { | |
100 | ||
101 | $entry_id = pg_fetch_result($result, 0, "id"); | |
102 | $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
103 | ||
104 | $unread = pg_fetch_result($result, 0, "unread"); | |
105 | $md5_hash = pg_fetch_result($result, 0, "md5_hash"); | |
57865bb1 AD |
106 | |
107 | if ($md5_hash != $content_md5 && CONTENT_CHECK_MD5) | |
ac53063a AD |
108 | $unread = "true"; |
109 | ||
d482c58c | 110 | if ($unread || !CONTENT_CHECK_MD5) { |
ac53063a AD |
111 | $updated_query_part = "updated = '$entry_timestamp',"; |
112 | } | |
40d13c28 AD |
113 | |
114 | $query = "UPDATE ttrss_entries | |
115 | SET | |
116 | title ='$entry_title', | |
117 | link = '$entry_link', | |
ac53063a | 118 | $updated_query_part |
40d13c28 AD |
119 | content = '$entry_content', |
120 | md5_hash = '$content_md5', | |
121 | unread = '$unread' | |
122 | WHERE | |
123 | id = '$entry_id'"; | |
124 | ||
125 | $result = pg_query($link, $query); | |
126 | ||
76798ff3 | 127 | if ($result) ++$num_unread; |
40d13c28 AD |
128 | |
129 | } | |
130 | ||
131 | } | |
132 | ||
76798ff3 AD |
133 | if ($result) { |
134 | $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()"); | |
135 | } | |
40d13c28 AD |
136 | |
137 | } | |
138 | ||
139 | } | |
140 | ||
141 | ||
142 | ||
143 | ||
144 | ?> |