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