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