You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
197 lines
5.8 KiB
PHTML
197 lines
5.8 KiB
PHTML
12 years ago
|
<?php
|
||
8 years ago
|
class Digest
|
||
|
{
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Send by mail a digest of last articles.
|
||
|
*
|
||
|
* @param mixed $link The database connection.
|
||
|
* @param integer $limit The maximum number of articles by digest.
|
||
|
* @return boolean Return false if digests are not enabled.
|
||
|
*/
|
||
8 years ago
|
static function send_headlines_digests($debug = false) {
|
||
12 years ago
|
|
||
12 years ago
|
require_once 'classes/ttrssmailer.php';
|
||
12 years ago
|
|
||
|
$user_limit = 15; // amount of users to process (e.g. emails to send out)
|
||
|
$limit = 1000; // maximum amount of headlines to include
|
||
|
|
||
|
if ($debug) _debug("Sending digests, batch of max $user_limit users, headline limit = $limit");
|
||
|
|
||
|
if (DB_TYPE == "pgsql") {
|
||
|
$interval_query = "last_digest_sent < NOW() - INTERVAL '1 days'";
|
||
|
} else if (DB_TYPE == "mysql") {
|
||
|
$interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL 1 DAY)";
|
||
|
}
|
||
|
|
||
12 years ago
|
$result = db_query("SELECT id,email FROM ttrss_users
|
||
12 years ago
|
WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
|
||
|
|
||
|
while ($line = db_fetch_assoc($result)) {
|
||
|
|
||
12 years ago
|
if (@get_pref('DIGEST_ENABLE', $line['id'], false)) {
|
||
12 years ago
|
$preferred_ts = strtotime(get_pref('DIGEST_PREFERRED_TIME', $line['id'], '00:00'));
|
||
12 years ago
|
|
||
|
// try to send digests within 2 hours of preferred time
|
||
|
if ($preferred_ts && time() >= $preferred_ts &&
|
||
8 years ago
|
time() - $preferred_ts <= 7200
|
||
|
) {
|
||
12 years ago
|
|
||
12 years ago
|
if ($debug) _debug("Sending digest for UID:" . $line['id'] . " - " . $line["email"]);
|
||
12 years ago
|
|
||
12 years ago
|
$do_catchup = get_pref('DIGEST_CATCHUP', $line['id'], false);
|
||
12 years ago
|
|
||
|
global $tz_offset;
|
||
|
|
||
|
// reset tz_offset global to prevent tz cache clash between users
|
||
|
$tz_offset = -1;
|
||
|
|
||
8 years ago
|
$tuple = Digest::prepare_headlines_digest($line["id"], 1, $limit);
|
||
12 years ago
|
$digest = $tuple[0];
|
||
|
$headlines_count = $tuple[1];
|
||
|
$affected_ids = $tuple[2];
|
||
|
$digest_text = $tuple[3];
|
||
|
|
||
|
if ($headlines_count > 0) {
|
||
|
|
||
12 years ago
|
$mail = new ttrssMailer();
|
||
12 years ago
|
|
||
8 years ago
|
$rc = $mail->quickMail($line["email"], $line["login"], DIGEST_SUBJECT, $digest, $digest_text);
|
||
12 years ago
|
|
||
12 years ago
|
if (!$rc && $debug) _debug("ERROR: " . $mail->ErrorInfo);
|
||
12 years ago
|
|
||
12 years ago
|
if ($debug) _debug("RC=$rc");
|
||
12 years ago
|
|
||
|
if ($rc && $do_catchup) {
|
||
12 years ago
|
if ($debug) _debug("Marking affected articles as read...");
|
||
8 years ago
|
Article::catchupArticlesById($affected_ids, 0, $line["id"]);
|
||
12 years ago
|
}
|
||
|
} else {
|
||
12 years ago
|
if ($debug) _debug("No headlines");
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
db_query("UPDATE ttrss_users SET last_digest_sent = NOW()
|
||
12 years ago
|
WHERE id = " . $line["id"]);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ($debug) _debug("All done.");
|
||
|
|
||
|
}
|
||
|
|
||
8 years ago
|
static function prepare_headlines_digest($user_id, $days = 1, $limit = 1000) {
|
||
12 years ago
|
|
||
|
require_once "lib/MiniTemplator.class.php";
|
||
|
|
||
|
$tpl = new MiniTemplator;
|
||
|
$tpl_t = new MiniTemplator;
|
||
|
|
||
|
$tpl->readTemplateFromFile("templates/digest_template_html.txt");
|
||
|
$tpl_t->readTemplateFromFile("templates/digest_template.txt");
|
||
|
|
||
12 years ago
|
$user_tz_string = get_pref('USER_TIMEZONE', $user_id);
|
||
12 years ago
|
$local_ts = convert_timestamp(time(), 'UTC', $user_tz_string);
|
||
|
|
||
|
$tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
|
||
|
$tpl->setVariable('CUR_TIME', date('G:i', $local_ts));
|
||
|
|
||
|
$tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
|
||
|
$tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts));
|
||
|
|
||
|
$affected_ids = array();
|
||
|
|
||
|
if (DB_TYPE == "pgsql") {
|
||
|
$interval_query = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'";
|
||
|
} else if (DB_TYPE == "mysql") {
|
||
|
$interval_query = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)";
|
||
|
}
|
||
|
|
||
12 years ago
|
$result = db_query("SELECT ttrss_entries.title,
|
||
12 years ago
|
ttrss_feeds.title AS feed_title,
|
||
8 years ago
|
COALESCE(ttrss_feed_categories.title, '" . __('Uncategorized') . "') AS cat_title,
|
||
12 years ago
|
date_updated,
|
||
|
ttrss_user_entries.ref_id,
|
||
|
link,
|
||
|
score,
|
||
|
content,
|
||
8 years ago
|
" . SUBSTRING_FOR_DATE . "(last_updated,1,19) AS last_updated
|
||
12 years ago
|
FROM
|
||
|
ttrss_user_entries,ttrss_entries,ttrss_feeds
|
||
|
LEFT JOIN
|
||
|
ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id)
|
||
|
WHERE
|
||
|
ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
|
||
|
AND include_in_digest = true
|
||
|
AND $interval_query
|
||
|
AND ttrss_user_entries.owner_uid = $user_id
|
||
|
AND unread = true
|
||
|
AND score >= 0
|
||
|
ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC
|
||
|
LIMIT $limit");
|
||
|
|
||
|
$headlines_count = db_num_rows($result);
|
||
|
|
||
|
$headlines = array();
|
||
|
|
||
|
while ($line = db_fetch_assoc($result)) {
|
||
|
array_push($headlines, $line);
|
||
|
}
|
||
|
|
||
|
for ($i = 0; $i < sizeof($headlines); $i++) {
|
||
|
|
||
|
$line = $headlines[$i];
|
||
|
|
||
|
array_push($affected_ids, $line["ref_id"]);
|
||
|
|
||
12 years ago
|
$updated = make_local_datetime($line['last_updated'], false,
|
||
12 years ago
|
$user_id);
|
||
|
|
||
8 years ago
|
/* if ($line["score"] != 0) {
|
||
|
if ($line["score"] > 0) $line["score"] = '+' . $line["score"];
|
||
12 years ago
|
|
||
8 years ago
|
$line["title"] .= " (".$line['score'].")";
|
||
|
} */
|
||
12 years ago
|
|
||
12 years ago
|
if (get_pref('ENABLE_FEED_CATS', $user_id)) {
|
||
12 years ago
|
$line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title'];
|
||
|
}
|
||
|
|
||
|
$tpl->setVariable('FEED_TITLE', $line["feed_title"]);
|
||
|
$tpl->setVariable('ARTICLE_TITLE', $line["title"]);
|
||
|
$tpl->setVariable('ARTICLE_LINK', $line["link"]);
|
||
|
$tpl->setVariable('ARTICLE_UPDATED', $updated);
|
||
|
$tpl->setVariable('ARTICLE_EXCERPT',
|
||
|
truncate_string(strip_tags($line["content"]), 300));
|
||
|
// $tpl->setVariable('ARTICLE_CONTENT',
|
||
|
// strip_tags($article_content));
|
||
|
|
||
|
$tpl->addBlock('article');
|
||
|
|
||
|
$tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
|
||
|
$tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
|
||
|
$tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
|
||
|
$tpl_t->setVariable('ARTICLE_UPDATED', $updated);
|
||
|
// $tpl_t->setVariable('ARTICLE_EXCERPT',
|
||
|
// truncate_string(strip_tags($line["excerpt"]), 100));
|
||
|
|
||
|
$tpl_t->addBlock('article');
|
||
|
|
||
8 years ago
|
if ($headlines[$i]['feed_title'] != $headlines[$i + 1]['feed_title']) {
|
||
12 years ago
|
$tpl->addBlock('feed');
|
||
|
$tpl_t->addBlock('feed');
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
$tpl->addBlock('digest');
|
||
|
$tpl->generateOutputToString($tmp);
|
||
|
|
||
|
$tpl_t->addBlock('digest');
|
||
|
$tpl_t->generateOutputToString($tmp_t);
|
||
|
|
||
|
return array($tmp, $headlines_count, $affected_ids, $tmp_t);
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
}
|