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