From: Andrew Dolgov Date: Thu, 4 May 2017 12:41:38 +0000 (+0300) Subject: move digest stuff to Digest class X-Git-Tag: 17.12~201 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=c2f0f24e4c4577944b563f3c0af92a8ed89b8e61;p=tt-rss.git move digest stuff to Digest class --- diff --git a/classes/backend.php b/classes/backend.php index 75fb4c86..c9a595b8 100644 --- a/classes/backend.php +++ b/classes/backend.php @@ -9,9 +9,7 @@ class Backend extends Handler { function digestTest() { header("Content-type: text/html"); - require_once "digest.php"; - - $rv = prepare_headlines_digest($_SESSION['uid'], 1, 1000); + $rv = Digest::prepare_headlines_digest($_SESSION['uid'], 1, 1000); $rv[3] = "
" . $rv[3] . "
"; diff --git a/classes/digest.php b/classes/digest.php new file mode 100644 index 00000000..5a50eb07 --- /dev/null +++ b/classes/digest.php @@ -0,0 +1,197 @@ += $preferred_ts && + time() - $preferred_ts <= 7200 + ) { + + if ($debug) _debug("Sending digest for UID:" . $line['id'] . " - " . $line["email"]); + + $do_catchup = get_pref('DIGEST_CATCHUP', $line['id'], false); + + global $tz_offset; + + // reset tz_offset global to prevent tz cache clash between users + $tz_offset = -1; + + $tuple = Digest::prepare_headlines_digest($line["id"], 1, $limit); + $digest = $tuple[0]; + $headlines_count = $tuple[1]; + $affected_ids = $tuple[2]; + $digest_text = $tuple[3]; + + if ($headlines_count > 0) { + + $mail = new ttrssMailer(); + + $rc = $mail->quickMail($line["email"], $line["login"], DIGEST_SUBJECT, $digest, $digest_text); + + if (!$rc && $debug) _debug("ERROR: " . $mail->ErrorInfo); + + if ($debug) _debug("RC=$rc"); + + if ($rc && $do_catchup) { + if ($debug) _debug("Marking affected articles as read..."); + Article::catchupArticlesById($affected_ids, 0, $line["id"]); + } + } else { + if ($debug) _debug("No headlines"); + } + + db_query("UPDATE ttrss_users SET last_digest_sent = NOW() + WHERE id = " . $line["id"]); + + } + } + } + + if ($debug) _debug("All done."); + + } + + static function prepare_headlines_digest($user_id, $days = 1, $limit = 1000) { + + 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"); + + $user_tz_string = get_pref('USER_TIMEZONE', $user_id); + $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)"; + } + + $result = db_query("SELECT ttrss_entries.title, + ttrss_feeds.title AS feed_title, + COALESCE(ttrss_feed_categories.title, '" . __('Uncategorized') . "') AS cat_title, + date_updated, + ttrss_user_entries.ref_id, + link, + score, + content, + " . SUBSTRING_FOR_DATE . "(last_updated,1,19) AS last_updated + 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"]); + + $updated = make_local_datetime($line['last_updated'], false, + $user_id); + + /* if ($line["score"] != 0) { + if ($line["score"] > 0) $line["score"] = '+' . $line["score"]; + + $line["title"] .= " (".$line['score'].")"; + } */ + + if (get_pref('ENABLE_FEED_CATS', $user_id)) { + $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'); + + if ($headlines[$i]['feed_title'] != $headlines[$i + 1]['feed_title']) { + $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); + } + +} \ No newline at end of file diff --git a/include/digest.php b/include/digest.php deleted file mode 100644 index 2c7f81cd..00000000 --- a/include/digest.php +++ /dev/null @@ -1,192 +0,0 @@ -= $preferred_ts && - time() - $preferred_ts <= 7200) { - - if ($debug) _debug("Sending digest for UID:" . $line['id'] . " - " . $line["email"]); - - $do_catchup = get_pref('DIGEST_CATCHUP', $line['id'], false); - - global $tz_offset; - - // reset tz_offset global to prevent tz cache clash between users - $tz_offset = -1; - - $tuple = prepare_headlines_digest($line["id"], 1, $limit); - $digest = $tuple[0]; - $headlines_count = $tuple[1]; - $affected_ids = $tuple[2]; - $digest_text = $tuple[3]; - - if ($headlines_count > 0) { - - $mail = new ttrssMailer(); - - $rc = $mail->quickMail($line["email"], $line["login"] , DIGEST_SUBJECT, $digest, $digest_text); - - if (!$rc && $debug) _debug("ERROR: " . $mail->ErrorInfo); - - if ($debug) _debug("RC=$rc"); - - if ($rc && $do_catchup) { - if ($debug) _debug("Marking affected articles as read..."); - Article::catchupArticlesById($affected_ids, 0, $line["id"]); - } - } else { - if ($debug) _debug("No headlines"); - } - - db_query("UPDATE ttrss_users SET last_digest_sent = NOW() - WHERE id = " . $line["id"]); - - } - } - } - - if ($debug) _debug("All done."); - - } - - function prepare_headlines_digest($user_id, $days = 1, $limit = 1000) { - - 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"); - - $user_tz_string = get_pref('USER_TIMEZONE', $user_id); - $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)"; - } - - $result = db_query("SELECT ttrss_entries.title, - ttrss_feeds.title AS feed_title, - COALESCE(ttrss_feed_categories.title, '".__('Uncategorized')."') AS cat_title, - date_updated, - ttrss_user_entries.ref_id, - link, - score, - content, - ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated - 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"]); - - $updated = make_local_datetime($line['last_updated'], false, - $user_id); - -/* if ($line["score"] != 0) { - if ($line["score"] > 0) $line["score"] = '+' . $line["score"]; - - $line["title"] .= " (".$line['score'].")"; - } */ - - if (get_pref('ENABLE_FEED_CATS', $user_id)) { - $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'); - - if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) { - $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); - } - diff --git a/include/rssfuncs.php b/include/rssfuncs.php index 5e1bdd2c..d816450f 100644 --- a/include/rssfuncs.php +++ b/include/rssfuncs.php @@ -217,10 +217,8 @@ housekeeping_user($owner_uid); } - require_once "digest.php"; - // Send feed digests by email if needed. - send_headlines_digests($debug); + Digest::send_headlines_digests($debug); return $nf;