]> git.wh0rd.org - tt-rss.git/blame - classes/digest.php
pngcrush.sh
[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 */
c10a4306 12 static function send_headlines_digests() {
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
c10a4306 17 Debug::log("Sending digests, batch of max $user_limit users, headline limit = $limit");
f1611683
AD
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
c10a4306 40 Debug::log("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
55bf4bc1
AD
61 $rc = $mailer->mail(["to_name" => $line["login"],
62 "to_address" => $line["email"],
57932e18
AD
63 "subject" => DIGEST_SUBJECT,
64 "message" => $digest_text,
65 "message_html" => $digest]);
f1611683 66
c10a4306 67 //if (!$rc && $debug) Debug::log("ERROR: " . $mailer->lastError());
f1611683 68
c10a4306 69 Debug::log("RC=$rc");
f1611683
AD
70
71 if ($rc && $do_catchup) {
c10a4306 72 Debug::log("Marking affected articles as read...");
aeb1abed 73 Article::catchupArticlesById($affected_ids, 0, $line["id"]);
f1611683
AD
74 }
75 } else {
c10a4306 76 Debug::log("No headlines");
f1611683
AD
77 }
78
abf94f00
AD
79 $sth = $pdo->prepare("UPDATE ttrss_users SET last_digest_sent = NOW()
80 WHERE id = ?");
81 $sth->execute([$line["id"]]);
f1611683
AD
82
83 }
84 }
85 }
86
c10a4306 87 Debug::log("All done.");
f1611683
AD
88
89 }
90
c2f0f24e 91 static function prepare_headlines_digest($user_id, $days = 1, $limit = 1000) {
f1611683
AD
92
93 require_once "lib/MiniTemplator.class.php";
94
95 $tpl = new MiniTemplator;
96 $tpl_t = new MiniTemplator;
97
98 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
99 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
100
a42c55f0 101 $user_tz_string = get_pref('USER_TIMEZONE', $user_id);
f1611683
AD
102 $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string);
103
104 $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
105 $tpl->setVariable('CUR_TIME', date('G:i', $local_ts));
106
107 $tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
108 $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts));
109
110 $affected_ids = array();
111
abf94f00
AD
112 $days = (int) $days;
113
f1611683 114 if (DB_TYPE == "pgsql") {
abf94f00 115 $interval_qpart = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'";
f1611683 116 } else if (DB_TYPE == "mysql") {
abf94f00 117 $interval_qpart = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)";
f1611683
AD
118 }
119
abf94f00
AD
120 $pdo = Db::pdo();
121
122 $sth = $pdo->prepare("SELECT ttrss_entries.title,
f1611683 123 ttrss_feeds.title AS feed_title,
c2f0f24e 124 COALESCE(ttrss_feed_categories.title, '" . __('Uncategorized') . "') AS cat_title,
f1611683
AD
125 date_updated,
126 ttrss_user_entries.ref_id,
127 link,
128 score,
129 content,
c2f0f24e 130 " . SUBSTRING_FOR_DATE . "(last_updated,1,19) AS last_updated
f1611683
AD
131 FROM
132 ttrss_user_entries,ttrss_entries,ttrss_feeds
133 LEFT JOIN
134 ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id)
135 WHERE
136 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
137 AND include_in_digest = true
abf94f00 138 AND $interval_qpart
09be7cdd 139 AND ttrss_user_entries.owner_uid = :user_id
f1611683
AD
140 AND unread = true
141 AND score >= 0
142 ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC
09be7cdd 143 LIMIT :limit");
144 $sth->bindParam(':user_id', intval($user_id, 10), \PDO::PARAM_INT);
145 $sth->bindParam(':limit', intval($limit, 10), \PDO::PARAM_INT);
146 $sth->execute();
f1611683 147
abf94f00 148 $headlines_count = 0;
f1611683
AD
149 $headlines = array();
150
abf94f00 151 while ($line = $sth->fetch()) {
f1611683 152 array_push($headlines, $line);
abf94f00 153 $headlines_count++;
f1611683
AD
154 }
155
156 for ($i = 0; $i < sizeof($headlines); $i++) {
157
158 $line = $headlines[$i];
159
160 array_push($affected_ids, $line["ref_id"]);
161
a42c55f0 162 $updated = make_local_datetime($line['last_updated'], false,
f1611683
AD
163 $user_id);
164
a42c55f0 165 if (get_pref('ENABLE_FEED_CATS', $user_id)) {
f1611683
AD
166 $line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title'];
167 }
168
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));
177
178 $tpl->addBlock('article');
179
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));
186
187 $tpl_t->addBlock('article');
188
c2f0f24e 189 if ($headlines[$i]['feed_title'] != $headlines[$i + 1]['feed_title']) {
f1611683
AD
190 $tpl->addBlock('feed');
191 $tpl_t->addBlock('feed');
192 }
193
194 }
195
196 $tpl->addBlock('digest');
197 $tpl->generateOutputToString($tmp);
198
199 $tpl_t->addBlock('digest');
200 $tpl_t->generateOutputToString($tmp_t);
201
202 return array($tmp, $headlines_count, $affected_ids, $tmp_t);
203 }
ea79a0e0 204
57932e18 205}