]> git.wh0rd.org Git - tt-rss.git/blob - functions.php
bada34c0263a83dc7072a8950cecdb00fc2aca64
[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                 $num_unread = 0;
21
22                 while ($line = pg_fetch_assoc($result)) {
23                         $num_unread += update_rss_feed($link, $line["feed_url"], $line["id"]);
24                 }
25
26                 pg_query("COMMIT");
27
28         }
29
30         function update_rss_feed($link, $feed_url, $feed) {
31
32                 error_reporting(0);
33                 $rss = fetch_rss($feed_url);
34                 error_reporting (E_ERROR | E_WARNING | E_PARSE);
35
36                 $num_unread = 0;
37         
38                 if ($rss) {
39
40                         $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
41
42                         $registered_title = pg_fetch_result($result, 0, "title");
43
44                         if (!$registered_title) {
45                                 $feed_title = $rss->channel["title"];
46                                 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
47                         }
48
49                         pg_query("BEGIN");
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"];
57         
58                                 $entry_timestamp = "";
59
60                                 $rss_2_date = $item['pubdate'];
61                                 $rss_1_date = $item['dc']['date'];
62                                 $atom_date = $item['issued'];
63                         
64                                 $no_orig_date = 'false';
65                         
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);
69 //                              if ($rss_3_date != "") $entry_timestamp = strtotime($rss_3_date);
70                                 
71                                 if ($entry_timestamp == "") {
72                                         $entry_timestamp = time();
73                                         $no_orig_date = 'true';
74                                 }
75
76                                 if (!$entry_timestamp) continue;
77
78                                 $entry_title = $item["title"];
79                                 $entry_link = $item["link"];
80
81                                 if (!$entry_title) continue;
82                                 if (!$entry_link) continue;
83
84                                 $entry_content = $item["description"];
85                                 if (!$entry_content) $entry_content = $item["content"];
86
87                                 if (!$entry_content) continue;
88
89                                 $entry_content = pg_escape_string($entry_content);
90                                 $entry_title = pg_escape_string($entry_title);
91         
92                                 $content_md5 = md5($entry_content);
93         
94                                 $result = pg_query($link, "
95                                         SELECT 
96                                                 id,unread,md5_hash,last_read,no_orig_date,title,
97                                                 EXTRACT(EPOCH FROM updated) as updated_timestamp
98                                         FROM
99                                                 ttrss_entries 
100                                         WHERE
101                                                 guid = '$entry_guid'");
102                                 
103                                 if (pg_num_rows($result) == 0) {
104         
105                                         $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
106         
107                                         $query = "INSERT INTO ttrss_entries 
108                                                         (title, guid, link, updated, content, feed_id, 
109                                                                 md5_hash, no_orig_date) 
110                                                 VALUES
111                                                         ('$entry_title', '$entry_guid', '$entry_link', 
112                                                                 '$entry_timestamp', '$entry_content', '$feed', 
113                                                                 '$content_md5', $no_orig_date)";
114         
115                                         $result = pg_query($link, $query);
116
117                                         if ($result) ++$num_unread;
118         
119                                 } else {
120         
121                                         $entry_id = pg_fetch_result($result, 0, "id");
122                                         $updated_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
123                                         $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
124                                         $last_read = pg_fetch_result($result, 0, "last_read");
125         
126                                         $unread = pg_fetch_result($result, 0, "unread");
127                                         $md5_hash = pg_fetch_result($result, 0, "md5_hash");
128                                         $no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
129                                         $orig_title = pg_fetch_result($result, 0, "title");
130
131                                         // disable update detection for posts which didn't have correct
132                                         // publishment date, because they will always register as updated
133                                         // sadly this doesn't catch feed generators which input current date 
134                                         // in posts all the time (some planets do this)
135
136                                         if ($no_orig_date != 't' && (!$last_read || $md5_hash != $content_md5)) {
137                                                 $last_read_qpart = 'last_read = null,';
138                                         } else {
139                                                 $last_read_qpart = '';
140                                         }
141
142                                         // mark post as updated on title change
143                                         // maybe we should mark it as unread instead?
144
145                                         if ($orig_title != $entry_title) {
146                                                 $last_read_qpart = 'last_read = null,';
147                                         }
148
149                                         // don't bother updating timestamps on posts with broken pubDate
150                                         
151                                         if ($no_orig_date != 't') {
152                                                 $update_timestamp_qpart = "updated = '$entry_timestamp_fmt',";
153                                         }
154
155 //                                      print "$content_md5 vs $md5_hash [$entry_title vs $orig_title, $entry_id, $feed_id]<br>";
156
157                                         if ($content_md5 != $md5_hash) {
158                                                 $update_md5_qpart = "md5_hash = '$content_md5',";
159                                         }
160
161                                         $query = "UPDATE ttrss_entries 
162                                                 SET 
163                                                         title ='$entry_title', 
164                                                         link = '$entry_link', 
165                                                         $update_timestamp_qpart
166                                                         $last_read_qpart
167                                                         $update_md5_qpart
168                                                         content = '$entry_content',
169                                                         unread = '$unread'
170                                                 WHERE
171                                                         id = '$entry_id'";
172
173 //                                      print "<pre>".htmlspecialchars($query)."</pre>";
174
175                                         $result = pg_query($link, $query);
176         
177                                         if ($result) ++$num_unread;
178         
179                                 }
180
181                         }
182
183                         if ($result) {
184                                 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
185                         }
186
187                         pg_query("COMMIT");
188
189                 }
190
191         }
192
193
194
195
196 ?>