]> git.wh0rd.org - tt-rss.git/blob - classes/digest.php
pngcrush.sh
[tt-rss.git] / classes / digest.php
1 <?php
2 class Digest
3 {
4
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 */
12 static function send_headlines_digests() {
13
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 Debug::log("Sending digests, batch of max $user_limit users, headline limit = $limit");
18
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)";
23 }
24
25 $pdo = Db::pdo();
26
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()) {
31
32 if (@get_pref('DIGEST_ENABLE', $line['id'], false)) {
33 $preferred_ts = strtotime(get_pref('DIGEST_PREFERRED_TIME', $line['id'], '00:00'));
34
35 // try to send digests within 2 hours of preferred time
36 if ($preferred_ts && time() >= $preferred_ts &&
37 time() - $preferred_ts <= 7200
38 ) {
39
40 Debug::log("Sending digest for UID:" . $line['id'] . " - " . $line["email"]);
41
42 $do_catchup = get_pref('DIGEST_CATCHUP', $line['id'], false);
43
44 global $tz_offset;
45
46 // reset tz_offset global to prevent tz cache clash between users
47 $tz_offset = -1;
48
49 $tuple = Digest::prepare_headlines_digest($line["id"], 1, $limit);
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
57 $mailer = new Mailer();
58
59 //$rc = $mail->quickMail($line["email"], $line["login"], DIGEST_SUBJECT, $digest, $digest_text);
60
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]);
66
67 //if (!$rc && $debug) Debug::log("ERROR: " . $mailer->lastError());
68
69 Debug::log("RC=$rc");
70
71 if ($rc && $do_catchup) {
72 Debug::log("Marking affected articles as read...");
73 Article::catchupArticlesById($affected_ids, 0, $line["id"]);
74 }
75 } else {
76 Debug::log("No headlines");
77 }
78
79 $sth = $pdo->prepare("UPDATE ttrss_users SET last_digest_sent = NOW()
80 WHERE id = ?");
81 $sth->execute([$line["id"]]);
82
83 }
84 }
85 }
86
87 Debug::log("All done.");
88
89 }
90
91 static function prepare_headlines_digest($user_id, $days = 1, $limit = 1000) {
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
101 $user_tz_string = get_pref('USER_TIMEZONE', $user_id);
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
112 $days = (int) $days;
113
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)";
118 }
119
120 $pdo = Db::pdo();
121
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,
125 date_updated,
126 ttrss_user_entries.ref_id,
127 link,
128 score,
129 content,
130 " . SUBSTRING_FOR_DATE . "(last_updated,1,19) AS last_updated
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
138 AND $interval_qpart
139 AND ttrss_user_entries.owner_uid = :user_id
140 AND unread = true
141 AND score >= 0
142 ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC
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();
147
148 $headlines_count = 0;
149 $headlines = array();
150
151 while ($line = $sth->fetch()) {
152 array_push($headlines, $line);
153 $headlines_count++;
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
162 $updated = make_local_datetime($line['last_updated'], false,
163 $user_id);
164
165 if (get_pref('ENABLE_FEED_CATS', $user_id)) {
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
189 if ($headlines[$i]['feed_title'] != $headlines[$i + 1]['feed_title']) {
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 }
204
205 }