3 * Send by mail a digest of last articles.
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.
9 function send_headlines_digests($debug = false) {
11 require_once 'classes/ttrssmailer.php';
13 $user_limit = 15; // amount of users to process (e.g. emails to send out)
14 $limit = 1000; // maximum amount of headlines to include
16 if ($debug) _debug("Sending digests, batch of max $user_limit users, headline limit = $limit");
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)";
24 $result = db_query("SELECT id,email FROM ttrss_users
25 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
27 while ($line = db_fetch_assoc($result)) {
29 if (@get_pref('DIGEST_ENABLE', $line['id'], false)) {
30 $preferred_ts = strtotime(get_pref('DIGEST_PREFERRED_TIME', $line['id'], '00:00'));
32 // try to send digests within 2 hours of preferred time
33 if ($preferred_ts && time() >= $preferred_ts &&
34 time() - $preferred_ts <= 7200) {
36 if ($debug) _debug("Sending digest for UID:" . $line['id'] . " - " . $line["email"]);
38 $do_catchup = get_pref('DIGEST_CATCHUP', $line['id'], false);
42 // reset tz_offset global to prevent tz cache clash between users
45 $tuple = prepare_headlines_digest($line["id"], 1, $limit);
47 $headlines_count = $tuple[1];
48 $affected_ids = $tuple[2];
49 $digest_text = $tuple[3];
51 if ($headlines_count > 0) {
53 $mail = new ttrssMailer();
55 $rc = $mail->quickMail($line["email"], $line["login"] , DIGEST_SUBJECT, $digest, $digest_text);
57 if (!$rc && $debug) _debug("ERROR: " . $mail->ErrorInfo);
59 if ($debug) _debug("RC=$rc");
61 if ($rc && $do_catchup) {
62 if ($debug) _debug("Marking affected articles as read...");
63 catchupArticlesById($affected_ids, 0, $line["id"]);
66 if ($debug) _debug("No headlines");
69 db_query("UPDATE ttrss_users SET last_digest_sent = NOW()
70 WHERE id = " . $line["id"]);
76 if ($debug) _debug("All done.");
80 function prepare_headlines_digest($user_id, $days = 1, $limit = 1000) {
82 require_once "lib/MiniTemplator.class.php";
84 $tpl = new MiniTemplator;
85 $tpl_t = new MiniTemplator;
87 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
88 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
90 $user_tz_string = get_pref('USER_TIMEZONE', $user_id);
91 $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string);
93 $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
94 $tpl->setVariable('CUR_TIME', date('G:i', $local_ts));
96 $tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
97 $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts));
99 $affected_ids = array();
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)";
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,
111 ttrss_user_entries.ref_id,
115 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
117 ttrss_user_entries,ttrss_entries,ttrss_feeds
119 ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id)
121 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
122 AND include_in_digest = true
124 AND ttrss_user_entries.owner_uid = $user_id
127 ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC
130 $headlines_count = db_num_rows($result);
132 $headlines = array();
134 while ($line = db_fetch_assoc($result)) {
135 array_push($headlines, $line);
138 for ($i = 0; $i < sizeof($headlines); $i++) {
140 $line = $headlines[$i];
142 array_push($affected_ids, $line["ref_id"]);
144 $updated = make_local_datetime($line['last_updated'], false,
147 /* if ($line["score"] != 0) {
148 if ($line["score"] > 0) $line["score"] = '+' . $line["score"];
150 $line["title"] .= " (".$line['score'].")";
153 if (get_pref('ENABLE_FEED_CATS', $user_id)) {
154 $line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title'];
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));
166 $tpl->addBlock('article');
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));
175 $tpl_t->addBlock('article');
177 if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) {
178 $tpl->addBlock('feed');
179 $tpl_t->addBlock('feed');
184 $tpl->addBlock('digest');
185 $tpl->generateOutputToString($tmp);
187 $tpl_t->addBlock('digest');
188 $tpl_t->generateOutputToString($tmp_t);
190 return array($tmp, $headlines_count, $affected_ids, $tmp_t);