]> git.wh0rd.org Git - tt-rss.git/blob - functions.php
cleaned up headline update process, fixed bug mentioned in previous patch
[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         
35                 if ($rss) {
36
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) {
42                                 $feed_title = $rss->channel["title"];
43                                 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
44                         }
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"];
52
53                                 if (!$entry_guid) continue;
54         
55                                 $entry_timestamp = "";
56
57                                 $rss_2_date = $item['pubdate'];
58                                 $rss_1_date = $item['dc']['date'];
59                                 $atom_date = $item['issued'];
60                         
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);
64                                 
65                                 if ($entry_timestamp == "") {
66                                         $entry_timestamp = time();
67                                         $no_orig_date = 'true';
68                                 } else {
69                                         $no_orig_date = 'false';
70                                 }
71
72                                 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
73
74                                 $entry_title = $item["title"];
75                                 $entry_link = $item["link"];
76
77                                 if (!$entry_title) continue;
78                                 if (!$entry_link) continue;
79
80                                 $entry_content = $item["description"];
81                                 if (!$entry_content) $entry_content = $item["content:escaped"];
82                                 if (!$entry_content) $entry_content = $item["content"];
83
84                                 if (!$entry_content) continue;
85
86                                 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
87
88                                 $result = pg_query($link, "
89                                         SELECT 
90                                                 id,last_read,no_orig_date,title,feed_id,content_hash,
91                                                 EXTRACT(EPOCH FROM updated) as updated_timestamp
92                                         FROM
93                                                 ttrss_entries 
94                                         WHERE
95                                                 guid = '$entry_guid'");
96
97                                 if (pg_num_rows($result) == 0) {
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) 
113                                                 VALUES
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
123                                         $result = pg_query($link, $query);
124
125                                 } else {
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");
139                                         $orig_title = pg_fetch_result($result, 0, "title");
140
141                                         if ($orig_title != $entry_title) {
142                                                 $last_read_qpart = 'last_read = null,';
143                                         }
144
145                                         if ($orig_content_hash != $content_hash) {
146                                                 $last_read_qpart = 'last_read = null,';
147                                         }
148
149                                         if ($orig_timestamp < $entry_timestamp) {
150                                                 $last_read_qpart = 'last_read = null,';
151                                         }
152
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
157                                         $query = "UPDATE ttrss_entries 
158                                                 SET 
159                                                         $last_read_qpart 
160                                                         title = '$entry_title',
161                                                         link = '$entry_link', 
162                                                         updated = '$entry_timestamp_fmt',
163                                                         content = '$entry_content',
164                                                         content_hash = '$content_hash'
165                                                 WHERE
166                                                         id = '$orig_entry_id'";
167
168                                         $result = pg_query($link, $query);
169
170                                 }
171                         }
172
173                         if ($result) {
174                                 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
175                         }
176
177                 }
178
179         }
180
181
182
183
184 ?>