]> git.wh0rd.org Git - tt-rss.git/blob - include/digest.php
Added ltrim to in fetch_file_contents to fix edge cases where a URL has one or more...
[tt-rss.git] / include / digest.php
1 <?php
2         /**
3          * Send by mail a digest of last articles.
4          *
5          * @param mixed $link The database connection.
6          * @param integer $limit The maximum number of articles by digest.
7          * @return boolean Return false if digests are not enabled.
8          */
9         function send_headlines_digests($debug = false) {
10
11                 require_once 'classes/ttrssmailer.php';
12
13                 $user_limit = 15; // amount of users to process (e.g. emails to send out)
14                 $limit = 1000; // maximum amount of headlines to include
15
16                 if ($debug) _debug("Sending digests, batch of max $user_limit users, headline limit = $limit");
17
18                 if (DB_TYPE == "pgsql") {
19                         $interval_query = "last_digest_sent < NOW() - INTERVAL '1 days'";
20                 } else if (DB_TYPE == "mysql") {
21                         $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL 1 DAY)";
22                 }
23
24                 $result = db_query("SELECT id,email FROM ttrss_users
25                                 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
26
27                 while ($line = db_fetch_assoc($result)) {
28
29                         if (@get_pref('DIGEST_ENABLE', $line['id'], false)) {
30                                 $preferred_ts = strtotime(get_pref('DIGEST_PREFERRED_TIME', $line['id'], '00:00'));
31
32                                 // try to send digests within 2 hours of preferred time
33                                 if ($preferred_ts && time() >= $preferred_ts &&
34                                                 time() - $preferred_ts <= 7200) {
35
36                                         if ($debug) _debug("Sending digest for UID:" . $line['id'] . " - " . $line["email"]);
37
38                                         $do_catchup = get_pref('DIGEST_CATCHUP', $line['id'], false);
39
40                                         global $tz_offset;
41
42                                         // reset tz_offset global to prevent tz cache clash between users
43                                         $tz_offset = -1;
44
45                                         $tuple = prepare_headlines_digest($line["id"], 1, $limit);
46                                         $digest = $tuple[0];
47                                         $headlines_count = $tuple[1];
48                                         $affected_ids = $tuple[2];
49                                         $digest_text = $tuple[3];
50
51                                         if ($headlines_count > 0) {
52
53                                                 $mail = new ttrssMailer();
54
55                                                 $rc = $mail->quickMail($line["email"], $line["login"] , DIGEST_SUBJECT, $digest, $digest_text);
56
57                                                 if (!$rc && $debug) _debug("ERROR: " . $mail->ErrorInfo);
58
59                                                 if ($debug) _debug("RC=$rc");
60
61                                                 if ($rc && $do_catchup) {
62                                                         if ($debug) _debug("Marking affected articles as read...");
63                                                         catchupArticlesById($affected_ids, 0, $line["id"]);
64                                                 }
65                                         } else {
66                                                 if ($debug) _debug("No headlines");
67                                         }
68
69                                         db_query("UPDATE ttrss_users SET last_digest_sent = NOW()
70                                                 WHERE id = " . $line["id"]);
71
72                                 }
73                         }
74                 }
75
76                 if ($debug) _debug("All done.");
77
78         }
79
80         function prepare_headlines_digest($user_id, $days = 1, $limit = 1000) {
81
82                 require_once "lib/MiniTemplator.class.php";
83
84                 $tpl = new MiniTemplator;
85                 $tpl_t = new MiniTemplator;
86
87                 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
88                 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
89
90                 $user_tz_string = get_pref('USER_TIMEZONE', $user_id);
91                 $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string);
92
93                 $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
94                 $tpl->setVariable('CUR_TIME', date('G:i', $local_ts));
95
96                 $tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
97                 $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts));
98
99                 $affected_ids = array();
100
101                 if (DB_TYPE == "pgsql") {
102                         $interval_query = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'";
103                 } else if (DB_TYPE == "mysql") {
104                         $interval_query = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)";
105                 }
106
107                 $result = db_query("SELECT ttrss_entries.title,
108                                 ttrss_feeds.title AS feed_title,
109                                 COALESCE(ttrss_feed_categories.title, '".__('Uncategorized')."') AS cat_title,
110                                 date_updated,
111                                 ttrss_user_entries.ref_id,
112                                 link,
113                                 score,
114                                 content,
115                                 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
116                         FROM
117                                 ttrss_user_entries,ttrss_entries,ttrss_feeds
118                         LEFT JOIN
119                                 ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id)
120                         WHERE
121                                 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
122                                 AND include_in_digest = true
123                                 AND $interval_query
124                                 AND ttrss_user_entries.owner_uid = $user_id
125                                 AND unread = true
126                                 AND score >= 0
127                         ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC
128                         LIMIT $limit");
129
130                 $headlines_count = db_num_rows($result);
131
132                 $headlines = array();
133
134                 while ($line = db_fetch_assoc($result)) {
135                         array_push($headlines, $line);
136                 }
137
138                 for ($i = 0; $i < sizeof($headlines); $i++) {
139
140                         $line = $headlines[$i];
141
142                         array_push($affected_ids, $line["ref_id"]);
143
144                         $updated = make_local_datetime($line['last_updated'], false,
145                                 $user_id);
146
147 /*                      if ($line["score"] != 0) {
148                                 if ($line["score"] > 0) $line["score"] = '+' . $line["score"];
149
150                                 $line["title"] .= " (".$line['score'].")";
151                         } */
152
153                         if (get_pref('ENABLE_FEED_CATS', $user_id)) {
154                                 $line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title'];
155                         }
156
157                         $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
158                         $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
159                         $tpl->setVariable('ARTICLE_LINK', $line["link"]);
160                         $tpl->setVariable('ARTICLE_UPDATED', $updated);
161                         $tpl->setVariable('ARTICLE_EXCERPT',
162                                 truncate_string(strip_tags($line["content"]), 300));
163 //                      $tpl->setVariable('ARTICLE_CONTENT',
164 //                              strip_tags($article_content));
165
166                         $tpl->addBlock('article');
167
168                         $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
169                         $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
170                         $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
171                         $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
172 //                      $tpl_t->setVariable('ARTICLE_EXCERPT',
173 //                              truncate_string(strip_tags($line["excerpt"]), 100));
174
175                         $tpl_t->addBlock('article');
176
177                         if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) {
178                                 $tpl->addBlock('feed');
179                                 $tpl_t->addBlock('feed');
180                         }
181
182                 }
183
184                 $tpl->addBlock('digest');
185                 $tpl->generateOutputToString($tmp);
186
187                 $tpl_t->addBlock('digest');
188                 $tpl_t->generateOutputToString($tmp_t);
189
190                 return array($tmp, $headlines_count, $affected_ids, $tmp_t);
191         }
192 ?>