digest.php 5.9 KB

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