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