]> git.wh0rd.org - tt-rss.git/blame - functions.php
basic functionality pass 3
[tt-rss.git] / functions.php
CommitLineData
40d13c28
AD
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
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 foreach ($rss->items as $item) {
24
25 $entry_guid = $item["id"];
26
27 if (!$entry_guid) $entry_guid = $item["guid"];
28 if (!$entry_guid) $entry_guid = $item["link"];
29
30 $entry_timestamp = $item["pubdate"];
31 if (!$entry_timestamp) $entry_timestamp = $item["modified"];
32 if (!$entry_timestamp) $entry_timestamp = $item["updated"];
33
34 $entry_timestamp = strtotime($entry_timestamp);
35
36 $entry_title = $item["title"];
37 $entry_link = $item["link"];
38
39 $entry_content = $item["description"];
40 if (!$entry_content) $entry_content = $item["content"];
41
42 $entry_content = pg_escape_string($entry_content);
43 $entry_title = pg_escape_string($entry_title);
44
45 $content_md5 = md5($entry_content);
46
47 $result = pg_query($link, "
48 SELECT
49 id,unread,md5_hash
50 FROM
51 ttrss_entries
52 WHERE
53 guid = '$entry_guid' OR md5_hash = '$content_md5'");
54
55 if (pg_num_rows($result) == 0) {
56
57 $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
58
59 $query = "INSERT INTO ttrss_entries
60 (title, guid, link, updated, content, feed_id, md5_hash)
61 VALUES
62 ('$entry_title', '$entry_guid', '$entry_link',
63 '$entry_timestamp', '$entry_content', '$feed',
64 '$content_md5')";
65
66 pg_query($link, $query);
67
68 } else {
69
70 $entry_id = pg_fetch_result($result, 0, "id");
71 $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
72
73 $unread = pg_fetch_result($result, 0, "unread");
74 $md5_hash = pg_fetch_result($result, 0, "md5_hash");
75
76 if ($md5_hash != $content_md5)
77 $unread = "false";
78
79 $query = "UPDATE ttrss_entries
80 SET
81 title ='$entry_title',
82 link = '$entry_link',
83 updated = '$entry_timestamp',
84 content = '$entry_content',
85 md5_hash = '$content_md5',
86 unread = '$unread'
87 WHERE
88 id = '$entry_id'";
89
90 $result = pg_query($link, $query);
91
92
93 // print "$entry_guid - $entry_timestamp - $entry_title -
94 // $entry_link - $entry_id<br>";
95
96 }
97
98 }
99
100 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
101
102 }
103
104 }
105
106
107
108
109?>