]>
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") { | |
22 | $interval_query = "last_digest_sent < NOW() - INTERVAL '1 days'"; | |
23 | } else if (DB_TYPE == "mysql") { | |
24 | $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL 1 DAY)"; | |
25 | } | |
26 | ||
a42c55f0 | 27 | $result = db_query("SELECT id,email FROM ttrss_users |
f1611683 AD |
28 | WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)"); |
29 | ||
30 | while ($line = db_fetch_assoc($result)) { | |
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 | ||
1b2afd2b | 57 | $mail = new ttrssMailer(); |
f1611683 | 58 | |
c2f0f24e | 59 | $rc = $mail->quickMail($line["email"], $line["login"], DIGEST_SUBJECT, $digest, $digest_text); |
f1611683 | 60 | |
9bfda43e | 61 | if (!$rc && $debug) _debug("ERROR: " . $mail->ErrorInfo); |
f1611683 | 62 | |
9bfda43e | 63 | if ($debug) _debug("RC=$rc"); |
f1611683 AD |
64 | |
65 | if ($rc && $do_catchup) { | |
9bfda43e | 66 | if ($debug) _debug("Marking affected articles as read..."); |
aeb1abed | 67 | Article::catchupArticlesById($affected_ids, 0, $line["id"]); |
f1611683 AD |
68 | } |
69 | } else { | |
9bfda43e | 70 | if ($debug) _debug("No headlines"); |
f1611683 AD |
71 | } |
72 | ||
a42c55f0 | 73 | db_query("UPDATE ttrss_users SET last_digest_sent = NOW() |
f1611683 AD |
74 | WHERE id = " . $line["id"]); |
75 | ||
76 | } | |
77 | } | |
78 | } | |
79 | ||
80 | if ($debug) _debug("All done."); | |
81 | ||
82 | } | |
83 | ||
c2f0f24e | 84 | static function prepare_headlines_digest($user_id, $days = 1, $limit = 1000) { |
f1611683 AD |
85 | |
86 | require_once "lib/MiniTemplator.class.php"; | |
87 | ||
88 | $tpl = new MiniTemplator; | |
89 | $tpl_t = new MiniTemplator; | |
90 | ||
91 | $tpl->readTemplateFromFile("templates/digest_template_html.txt"); | |
92 | $tpl_t->readTemplateFromFile("templates/digest_template.txt"); | |
93 | ||
a42c55f0 | 94 | $user_tz_string = get_pref('USER_TIMEZONE', $user_id); |
f1611683 AD |
95 | $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string); |
96 | ||
97 | $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts)); | |
98 | $tpl->setVariable('CUR_TIME', date('G:i', $local_ts)); | |
99 | ||
100 | $tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts)); | |
101 | $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts)); | |
102 | ||
103 | $affected_ids = array(); | |
104 | ||
105 | if (DB_TYPE == "pgsql") { | |
106 | $interval_query = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'"; | |
107 | } else if (DB_TYPE == "mysql") { | |
108 | $interval_query = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
109 | } | |
110 | ||
a42c55f0 | 111 | $result = db_query("SELECT ttrss_entries.title, |
f1611683 | 112 | ttrss_feeds.title AS feed_title, |
c2f0f24e | 113 | COALESCE(ttrss_feed_categories.title, '" . __('Uncategorized') . "') AS cat_title, |
f1611683 AD |
114 | date_updated, |
115 | ttrss_user_entries.ref_id, | |
116 | link, | |
117 | score, | |
118 | content, | |
c2f0f24e | 119 | " . SUBSTRING_FOR_DATE . "(last_updated,1,19) AS last_updated |
f1611683 AD |
120 | FROM |
121 | ttrss_user_entries,ttrss_entries,ttrss_feeds | |
122 | LEFT JOIN | |
123 | ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id) | |
124 | WHERE | |
125 | ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id | |
126 | AND include_in_digest = true | |
127 | AND $interval_query | |
128 | AND ttrss_user_entries.owner_uid = $user_id | |
129 | AND unread = true | |
130 | AND score >= 0 | |
131 | ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC | |
132 | LIMIT $limit"); | |
133 | ||
f1611683 AD |
134 | $headlines_count = db_num_rows($result); |
135 | ||
136 | $headlines = array(); | |
137 | ||
138 | while ($line = db_fetch_assoc($result)) { | |
139 | array_push($headlines, $line); | |
140 | } | |
141 | ||
142 | for ($i = 0; $i < sizeof($headlines); $i++) { | |
143 | ||
144 | $line = $headlines[$i]; | |
145 | ||
146 | array_push($affected_ids, $line["ref_id"]); | |
147 | ||
a42c55f0 | 148 | $updated = make_local_datetime($line['last_updated'], false, |
f1611683 AD |
149 | $user_id); |
150 | ||
c2f0f24e AD |
151 | /* if ($line["score"] != 0) { |
152 | if ($line["score"] > 0) $line["score"] = '+' . $line["score"]; | |
f1611683 | 153 | |
c2f0f24e AD |
154 | $line["title"] .= " (".$line['score'].")"; |
155 | } */ | |
f1611683 | 156 | |
a42c55f0 | 157 | if (get_pref('ENABLE_FEED_CATS', $user_id)) { |
f1611683 AD |
158 | $line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title']; |
159 | } | |
160 | ||
161 | $tpl->setVariable('FEED_TITLE', $line["feed_title"]); | |
162 | $tpl->setVariable('ARTICLE_TITLE', $line["title"]); | |
163 | $tpl->setVariable('ARTICLE_LINK', $line["link"]); | |
164 | $tpl->setVariable('ARTICLE_UPDATED', $updated); | |
165 | $tpl->setVariable('ARTICLE_EXCERPT', | |
166 | truncate_string(strip_tags($line["content"]), 300)); | |
167 | // $tpl->setVariable('ARTICLE_CONTENT', | |
168 | // strip_tags($article_content)); | |
169 | ||
170 | $tpl->addBlock('article'); | |
171 | ||
172 | $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]); | |
173 | $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]); | |
174 | $tpl_t->setVariable('ARTICLE_LINK', $line["link"]); | |
175 | $tpl_t->setVariable('ARTICLE_UPDATED', $updated); | |
176 | // $tpl_t->setVariable('ARTICLE_EXCERPT', | |
177 | // truncate_string(strip_tags($line["excerpt"]), 100)); | |
178 | ||
179 | $tpl_t->addBlock('article'); | |
180 | ||
c2f0f24e | 181 | if ($headlines[$i]['feed_title'] != $headlines[$i + 1]['feed_title']) { |
f1611683 AD |
182 | $tpl->addBlock('feed'); |
183 | $tpl_t->addBlock('feed'); | |
184 | } | |
185 | ||
186 | } | |
187 | ||
188 | $tpl->addBlock('digest'); | |
189 | $tpl->generateOutputToString($tmp); | |
190 | ||
191 | $tpl_t->addBlock('digest'); | |
192 | $tpl_t->generateOutputToString($tmp_t); | |
193 | ||
194 | return array($tmp, $headlines_count, $affected_ids, $tmp_t); | |
195 | } | |
ea79a0e0 | 196 | |
c2f0f24e | 197 | } |