]> git.wh0rd.org - tt-rss.git/blob - functions.php
headline update uses transactions once again
[tt-rss.git] / functions.php
1 <?
2 require_once 'config.php';
3
4 function update_all_feeds($link, $fetch) {
5
6 pg_query("BEGIN");
7
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 }
19
20 while ($line = pg_fetch_assoc($result)) {
21 update_rss_feed($link, $line["feed_url"], $line["id"]);
22 }
23
24 pg_query("COMMIT");
25
26 }
27
28 function update_rss_feed($link, $feed_url, $feed) {
29
30 error_reporting(0);
31 $rss = fetch_rss($feed_url);
32 error_reporting (E_ERROR | E_WARNING | E_PARSE);
33
34 pg_query("BEGIN");
35
36 if ($rss) {
37
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) {
43 $feed_title = $rss->channel["title"];
44 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
45 }
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"];
53
54 if (!$entry_guid) continue;
55
56 $entry_timestamp = "";
57
58 $rss_2_date = $item['pubdate'];
59 $rss_1_date = $item['dc']['date'];
60 $atom_date = $item['issued'];
61
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);
65
66 if ($entry_timestamp == "") {
67 $entry_timestamp = time();
68 $no_orig_date = 'true';
69 } else {
70 $no_orig_date = 'false';
71 }
72
73 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
74
75 $entry_title = $item["title"];
76 $entry_link = $item["link"];
77
78 if (!$entry_title) continue;
79 if (!$entry_link) continue;
80
81 $entry_content = $item["description"];
82 if (!$entry_content) $entry_content = $item["content:escaped"];
83 if (!$entry_content) $entry_content = $item["content"];
84
85 if (!$entry_content) continue;
86
87 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
88
89 $result = pg_query($link, "
90 SELECT
91 id,last_read,no_orig_date,title,feed_id,content_hash,
92 EXTRACT(EPOCH FROM updated) as updated_timestamp
93 FROM
94 ttrss_entries
95 WHERE
96 guid = '$entry_guid'");
97
98 if (pg_num_rows($result) == 0) {
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)
114 VALUES
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
124 $result = pg_query($link, $query);
125
126 } else {
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");
140 $orig_title = pg_fetch_result($result, 0, "title");
141
142 if ($orig_title != $entry_title) {
143 $last_read_qpart = 'last_read = null,';
144 }
145
146 if ($orig_content_hash != $content_hash) {
147 $last_read_qpart = 'last_read = null,';
148 }
149
150 // if ($orig_timestamp < $entry_timestamp) {
151 // $last_read_qpart = 'last_read = null,';
152 // }
153
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
158 $query = "UPDATE ttrss_entries
159 SET
160 $last_read_qpart
161 title = '$entry_title',
162 link = '$entry_link',
163 updated = '$entry_timestamp_fmt',
164 content = '$entry_content',
165 content_hash = '$content_hash'
166 WHERE
167 id = '$orig_entry_id'";
168
169 $result = pg_query($link, $query);
170
171 }
172 }
173
174 if ($result) {
175 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
176 }
177
178 }
179
180 pg_query("COMMIT");
181
182 }
183
184
185
186
187 ?>