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