digest.php 6.5 KB

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