]> git.wh0rd.org - tt-rss.git/blob - functions.php
basic functionality pass 10
[tt-rss.git] / functions.php
1 <?
2 require_once 'config.php';
3
4 function update_all_feeds($link) {
5
6 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds WHERE
7 last_updated is null OR title = '' OR
8 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) > " .
9 MIN_UPDATE_TIME);
10
11 while ($line = pg_fetch_assoc($result)) {
12 update_rss_feed($link, $line["feed_url"], $line["id"]);
13 }
14
15 }
16
17 function update_rss_feed($link, $feed_url, $feed) {
18
19 $rss = fetch_rss($feed_url);
20
21 if ($rss) {
22
23 $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
24
25 $registered_title = pg_fetch_result($result, 0, "title");
26
27 if (!$registered_title) {
28
29 $feed_title = $rss->channel["title"];
30 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
31 }
32
33 foreach ($rss->items as $item) {
34
35 $entry_guid = $item["id"];
36
37 if (!$entry_guid) $entry_guid = $item["guid"];
38 if (!$entry_guid) $entry_guid = $item["link"];
39
40 $entry_timestamp = $item["pubdate"];
41 if (!$entry_timestamp) $entry_timestamp = $item["modified"];
42 if (!$entry_timestamp) $entry_timestamp = $item["updated"];
43
44 if (!$entry_timestamp) continue;
45
46 $entry_timestamp = strtotime($entry_timestamp);
47
48 if (!$entry_timestamp) continue;
49
50 $entry_title = $item["title"];
51 $entry_link = $item["link"];
52
53 if (!$entry_title) continue;
54 if (!$entry_link) continue;
55
56 $entry_content = $item["description"];
57 if (!$entry_content) $entry_content = $item["content"];
58
59 $entry_content = pg_escape_string($entry_content);
60 $entry_title = pg_escape_string($entry_title);
61
62 $content_md5 = md5($entry_content);
63
64 $result = pg_query($link, "
65 SELECT
66 id,unread,md5_hash
67 FROM
68 ttrss_entries
69 WHERE
70 guid = '$entry_guid' OR md5_hash = '$content_md5'");
71
72 if (pg_num_rows($result) == 0) {
73
74 $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
75
76 $query = "INSERT INTO ttrss_entries
77 (title, guid, link, updated, content, feed_id, md5_hash)
78 VALUES
79 ('$entry_title', '$entry_guid', '$entry_link',
80 '$entry_timestamp', '$entry_content', '$feed',
81 '$content_md5')";
82
83 pg_query($link, $query);
84
85 } else {
86
87 $entry_id = pg_fetch_result($result, 0, "id");
88 $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
89
90 $unread = pg_fetch_result($result, 0, "unread");
91 $md5_hash = pg_fetch_result($result, 0, "md5_hash");
92
93 if ($md5_hash != $content_md5 && CONTENT_CHECK_MD5)
94 $unread = "true";
95
96 if ($unread || !CONTENT_CHECK_MD5) {
97 $updated_query_part = "updated = '$entry_timestamp',";
98 }
99
100 $query = "UPDATE ttrss_entries
101 SET
102 title ='$entry_title',
103 link = '$entry_link',
104 $updated_query_part
105 content = '$entry_content',
106 md5_hash = '$content_md5',
107 unread = '$unread'
108 WHERE
109 id = '$entry_id'";
110
111 $result = pg_query($link, $query);
112
113
114 // print "$entry_guid - $entry_timestamp - $entry_title -
115 // $entry_link - $entry_id<br>";
116
117 }
118
119 }
120
121 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
122
123 }
124
125 }
126
127
128
129
130 ?>