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