digest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
  76. $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts));
  77. $affected_ids = array();
  78. $days = (int) $days;
  79. if (DB_TYPE == "pgsql") {
  80. $interval_qpart = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'";
  81. } else if (DB_TYPE == "mysql") {
  82. $interval_qpart = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)";
  83. }
  84. $pdo = Db::pdo();
  85. $sth = $pdo->prepare("SELECT ttrss_entries.title,
  86. ttrss_feeds.title AS feed_title,
  87. COALESCE(ttrss_feed_categories.title, '" . __('Uncategorized') . "') AS cat_title,
  88. date_updated,
  89. ttrss_user_entries.ref_id,
  90. link,
  91. score,
  92. content,
  93. " . SUBSTRING_FOR_DATE . "(last_updated,1,19) AS last_updated
  94. FROM
  95. ttrss_user_entries,ttrss_entries,ttrss_feeds
  96. LEFT JOIN
  97. ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id)
  98. WHERE
  99. ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
  100. AND include_in_digest = true
  101. AND $interval_qpart
  102. AND ttrss_user_entries.owner_uid = :user_id
  103. AND unread = true
  104. AND score >= 0
  105. ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC
  106. LIMIT :limit");
  107. $sth->bindParam(':user_id', intval($user_id, 10), PDO::PARAM_INT);
  108. $sth->bindParam(':limit', intval($limit, 10), PDO::PARAM_INT);
  109. $sth->execute();
  110. $headlines_count = 0;
  111. $headlines = array();
  112. while ($line = $sth->fetch()) {
  113. array_push($headlines, $line);
  114. $headlines_count++;
  115. }
  116. for ($i = 0; $i < sizeof($headlines); $i++) {
  117. $line = $headlines[$i];
  118. array_push($affected_ids, $line["ref_id"]);
  119. $updated = make_local_datetime($line['last_updated'], false,
  120. $user_id);
  121. if (get_pref('ENABLE_FEED_CATS', $user_id)) {
  122. $line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title'];
  123. }
  124. $article_labels = Article::get_article_labels($line["ref_id"], $user_id);
  125. $article_labels_formatted = "";
  126. if (is_array($article_labels) && count($article_labels) > 0) {
  127. $article_labels_formatted = implode(", ", array_map(function($a) {
  128. return $a[1];
  129. }, $article_labels));
  130. }
  131. $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
  132. $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
  133. $tpl->setVariable('ARTICLE_LINK', $line["link"]);
  134. $tpl->setVariable('ARTICLE_UPDATED', $updated);
  135. $tpl->setVariable('ARTICLE_EXCERPT',
  136. truncate_string(strip_tags($line["content"]), 300));
  137. // $tpl->setVariable('ARTICLE_CONTENT',
  138. // strip_tags($article_content));
  139. $tpl->setVariable('ARTICLE_LABELS', $article_labels_formatted, true);
  140. $tpl->addBlock('article');
  141. $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
  142. $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
  143. $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
  144. $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
  145. $tpl_t->setVariable('ARTICLE_LABELS', $article_labels_formatted, true);
  146. // $tpl_t->setVariable('ARTICLE_EXCERPT',
  147. // truncate_string(strip_tags($line["excerpt"]), 100));
  148. $tpl_t->addBlock('article');
  149. if ($headlines[$i]['feed_title'] != $headlines[$i + 1]['feed_title']) {
  150. $tpl->addBlock('feed');
  151. $tpl_t->addBlock('feed');
  152. }
  153. }
  154. $tpl->addBlock('digest');
  155. $tpl->generateOutputToString($tmp);
  156. $tpl_t->addBlock('digest');
  157. $tpl_t->generateOutputToString($tmp_t);
  158. return array($tmp, $headlines_count, $affected_ids, $tmp_t);
  159. }
  160. }