]>
git.wh0rd.org - tt-rss.git/blob - classes/digest.php
6 * Send by mail a digest of last articles.
8 * @param mixed $link The database connection.
9 * @param integer $limit The maximum number of articles by digest.
10 * @return boolean Return false if digests are not enabled.
12 static function send_headlines_digests($debug = false) {
14 $user_limit = 15; // amount of users to process (e.g. emails to send out)
15 $limit = 1000; // maximum amount of headlines to include
17 if ($debug) _debug("Sending digests, batch of max $user_limit users, headline limit = $limit");
19 if (DB_TYPE
== "pgsql") {
20 $interval_qpart = "last_digest_sent < NOW() - INTERVAL '1 days'";
21 } else if (DB_TYPE
== "mysql") {
22 $interval_qpart = "last_digest_sent < DATE_SUB(NOW(), INTERVAL 1 DAY)";
27 $res = $pdo->query("SELECT id,email FROM ttrss_users
28 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_qpart)");
30 while ($line = $res->fetch()) {
32 if (@get_pref
('DIGEST_ENABLE', $line['id'], false)) {
33 $preferred_ts = strtotime(get_pref('DIGEST_PREFERRED_TIME', $line['id'], '00:00'));
35 // try to send digests within 2 hours of preferred time
36 if ($preferred_ts && time() >= $preferred_ts &&
37 time() - $preferred_ts <= 7200
40 if ($debug) _debug("Sending digest for UID:" . $line['id'] . " - " . $line["email"]);
42 $do_catchup = get_pref('DIGEST_CATCHUP', $line['id'], false);
46 // reset tz_offset global to prevent tz cache clash between users
49 $tuple = Digest
::prepare_headlines_digest($line["id"], 1, $limit);
51 $headlines_count = $tuple[1];
52 $affected_ids = $tuple[2];
53 $digest_text = $tuple[3];
55 if ($headlines_count > 0) {
57 $mailer = new Mailer();
59 //$rc = $mail->quickMail($line["email"], $line["login"], DIGEST_SUBJECT, $digest, $digest_text);
61 $rc = $mailer->mail(["to_name" => $line["login"],
62 "to_address" => $line["email"],
63 "subject" => DIGEST_SUBJECT
,
64 "message" => $digest_text,
65 "message_html" => $digest]);
67 //if (!$rc && $debug) _debug("ERROR: " . $mailer->lastError());
69 if ($debug) _debug("RC=$rc");
71 if ($rc && $do_catchup) {
72 if ($debug) _debug("Marking affected articles as read...");
73 Article
::catchupArticlesById($affected_ids, 0, $line["id"]);
76 if ($debug) _debug("No headlines");
79 $sth = $pdo->prepare("UPDATE ttrss_users SET last_digest_sent = NOW()
81 $sth->execute([$line["id"]]);
87 if ($debug) _debug("All done.");
91 static function prepare_headlines_digest($user_id, $days = 1, $limit = 1000) {
93 require_once "lib/MiniTemplator.class.php";
95 $tpl = new MiniTemplator
;
96 $tpl_t = new MiniTemplator
;
98 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
99 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
101 $user_tz_string = get_pref('USER_TIMEZONE', $user_id);
102 $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string);
104 $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
105 $tpl->setVariable('CUR_TIME', date('G:i', $local_ts));
107 $tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
108 $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts));
110 $affected_ids = array();
114 if (DB_TYPE
== "pgsql") {
115 $interval_qpart = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'";
116 } else if (DB_TYPE
== "mysql") {
117 $interval_qpart = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)";
122 $sth = $pdo->prepare("SELECT ttrss_entries.title,
123 ttrss_feeds.title AS feed_title,
124 COALESCE(ttrss_feed_categories.title, '" . __('Uncategorized') . "') AS cat_title,
126 ttrss_user_entries.ref_id,
130 " . SUBSTRING_FOR_DATE
. "(last_updated,1,19) AS last_updated
132 ttrss_user_entries,ttrss_entries,ttrss_feeds
134 ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id)
136 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
137 AND include_in_digest = true
139 AND ttrss_user_entries.owner_uid = :user_id
142 ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC
144 $sth->bindParam(':user_id', intval($user_id, 10), \PDO
::PARAM_INT
);
145 $sth->bindParam(':limit', intval($limit, 10), \PDO
::PARAM_INT
);
148 $headlines_count = 0;
149 $headlines = array();
151 while ($line = $sth->fetch()) {
152 array_push($headlines, $line);
156 for ($i = 0; $i < sizeof($headlines); $i++
) {
158 $line = $headlines[$i];
160 array_push($affected_ids, $line["ref_id"]);
162 $updated = make_local_datetime($line['last_updated'], false,
165 if (get_pref('ENABLE_FEED_CATS', $user_id)) {
166 $line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title'];
169 $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
170 $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
171 $tpl->setVariable('ARTICLE_LINK', $line["link"]);
172 $tpl->setVariable('ARTICLE_UPDATED', $updated);
173 $tpl->setVariable('ARTICLE_EXCERPT',
174 truncate_string(strip_tags($line["content"]), 300));
175 // $tpl->setVariable('ARTICLE_CONTENT',
176 // strip_tags($article_content));
178 $tpl->addBlock('article');
180 $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
181 $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
182 $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
183 $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
184 // $tpl_t->setVariable('ARTICLE_EXCERPT',
185 // truncate_string(strip_tags($line["excerpt"]), 100));
187 $tpl_t->addBlock('article');
189 if ($headlines[$i]['feed_title'] != $headlines[$i +
1]['feed_title']) {
190 $tpl->addBlock('feed');
191 $tpl_t->addBlock('feed');
196 $tpl->addBlock('digest');
197 $tpl->generateOutputToString($tmp);
199 $tpl_t->addBlock('digest');
200 $tpl_t->generateOutputToString($tmp_t);
202 return array($tmp, $headlines_count, $affected_ids, $tmp_t);