]> git.wh0rd.org - tt-rss.git/blame - functions.php
cleaned up headline update process, fixed bug mentioned in previous patch
[tt-rss.git] / functions.php
CommitLineData
40d13c28
AD
1<?
2 require_once 'config.php';
3
9c9c7e6b 4 function update_all_feeds($link, $fetch) {
40d13c28 5
b82af8c3
AD
6 pg_query("BEGIN");
7
9c9c7e6b
AD
8 if (!$fetch) {
9
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) > " .
13 MIN_UPDATE_TIME);
14
15 } else {
16
17 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds");
18 }
40d13c28
AD
19
20 while ($line = pg_fetch_assoc($result)) {
466001c4 21 update_rss_feed($link, $line["feed_url"], $line["id"]);
40d13c28
AD
22 }
23
b82af8c3
AD
24 pg_query("COMMIT");
25
40d13c28
AD
26 }
27
28 function update_rss_feed($link, $feed_url, $feed) {
29
3ad5aa85 30 error_reporting(0);
40d13c28 31 $rss = fetch_rss($feed_url);
3ad5aa85 32 error_reporting (E_ERROR | E_WARNING | E_PARSE);
76798ff3 33
40d13c28
AD
34
35 if ($rss) {
b82af8c3 36
331900c6
AD
37 $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
38
39 $registered_title = pg_fetch_result($result, 0, "title");
40
41 if (!$registered_title) {
331900c6
AD
42 $feed_title = $rss->channel["title"];
43 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
44 }
40d13c28
AD
45
46 foreach ($rss->items as $item) {
47
48 $entry_guid = $item["id"];
49
50 if (!$entry_guid) $entry_guid = $item["guid"];
51 if (!$entry_guid) $entry_guid = $item["link"];
466001c4
AD
52
53 if (!$entry_guid) continue;
40d13c28 54
9c9c7e6b 55 $entry_timestamp = "";
b82af8c3 56
9c9c7e6b
AD
57 $rss_2_date = $item['pubdate'];
58 $rss_1_date = $item['dc']['date'];
59 $atom_date = $item['issued'];
b197f117 60
9c9c7e6b
AD
61 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
62 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
63 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
b82af8c3
AD
64
65 if ($entry_timestamp == "") {
66 $entry_timestamp = time();
67 $no_orig_date = 'true';
466001c4
AD
68 } else {
69 $no_orig_date = 'false';
b82af8c3 70 }
b197f117 71
466001c4 72 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
71ad3959 73
40d13c28
AD
74 $entry_title = $item["title"];
75 $entry_link = $item["link"];
71ad3959
AD
76
77 if (!$entry_title) continue;
78 if (!$entry_link) continue;
79
40d13c28 80 $entry_content = $item["description"];
466001c4 81 if (!$entry_content) $entry_content = $item["content:escaped"];
40d13c28 82 if (!$entry_content) $entry_content = $item["content"];
a2015351
AD
83
84 if (!$entry_content) continue;
85
466001c4 86 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
cb0bd8bd 87
40d13c28
AD
88 $result = pg_query($link, "
89 SELECT
466001c4 90 id,last_read,no_orig_date,title,feed_id,content_hash,
b197f117 91 EXTRACT(EPOCH FROM updated) as updated_timestamp
40d13c28
AD
92 FROM
93 ttrss_entries
94 WHERE
a2015351 95 guid = '$entry_guid'");
466001c4 96
40d13c28 97 if (pg_num_rows($result) == 0) {
466001c4
AD
98
99 $entry_content = pg_escape_string($entry_content);
100 $entry_title = pg_escape_string($entry_title);
101 $entry_link = pg_escape_string($entry_link);
102
103 $query = "INSERT
104 INTO ttrss_entries
105 (title,
106 guid,
107 link,
108 updated,
109 content,
110 content_hash,
111 feed_id,
112 no_orig_date)
40d13c28 113 VALUES
466001c4
AD
114 ('$entry_title',
115 '$entry_guid',
116 '$entry_link',
117 '$entry_timestamp_fmt',
118 '$entry_content',
119 '$content_hash',
120 '$feed',
121 $no_orig_date)";
122
76798ff3
AD
123 $result = pg_query($link, $query);
124
40d13c28 125 } else {
466001c4
AD
126
127 $orig_entry_id = pg_fetch_result($result, 0, "id");
128 $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
129
130 if ($orig_feed_id != $feed) {
131// print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
132 continue;
133 }
134
135 $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
136 $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
137 $orig_last_read = pg_fetch_result($result, 0, "last_read");
138 $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
b82af8c3 139 $orig_title = pg_fetch_result($result, 0, "title");
cac95b8d 140
466001c4
AD
141 if ($orig_title != $entry_title) {
142 $last_read_qpart = 'last_read = null,';
143 }
144
145 if ($orig_content_hash != $content_hash) {
b82af8c3 146 $last_read_qpart = 'last_read = null,';
cac95b8d
AD
147 }
148
466001c4
AD
149 if ($orig_timestamp < $entry_timestamp) {
150 $last_read_qpart = 'last_read = null,';
a2015351
AD
151 }
152
466001c4
AD
153 $entry_content = pg_escape_string($entry_content);
154 $entry_title = pg_escape_string($entry_title);
155 $entry_link = pg_escape_string($entry_link);
156
40d13c28
AD
157 $query = "UPDATE ttrss_entries
158 SET
466001c4
AD
159 $last_read_qpart
160 title = '$entry_title',
40d13c28 161 link = '$entry_link',
466001c4
AD
162 updated = '$entry_timestamp_fmt',
163 content = '$entry_content',
164 content_hash = '$content_hash'
40d13c28 165 WHERE
466001c4 166 id = '$orig_entry_id'";
a2015351 167
40d13c28 168 $result = pg_query($link, $query);
a2015351 169
466001c4 170 }
40d13c28
AD
171 }
172
76798ff3
AD
173 if ($result) {
174 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
175 }
40d13c28
AD
176
177 }
178
179 }
180
181
182
183
184?>