digest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_query = "last_digest_sent < NOW() - INTERVAL '1 days'";
  18. } else if (DB_TYPE == "mysql") {
  19. $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL 1 DAY)";
  20. }
  21. $result = db_query("SELECT id,email FROM ttrss_users
  22. WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
  23. while ($line = db_fetch_assoc($result)) {
  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. if ($debug) _debug("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. $mail = new ttrssMailer();
  42. $rc = $mail->quickMail($line["email"], $line["login"], DIGEST_SUBJECT, $digest, $digest_text);
  43. if (!$rc && $debug) _debug("ERROR: " . $mail->ErrorInfo);
  44. if ($debug) _debug("RC=$rc");
  45. if ($rc && $do_catchup) {
  46. if ($debug) _debug("Marking affected articles as read...");
  47. Article::catchupArticlesById($affected_ids, 0, $line["id"]);
  48. }
  49. } else {
  50. if ($debug) _debug("No headlines");
  51. }
  52. db_query("UPDATE ttrss_users SET last_digest_sent = NOW()
  53. WHERE id = " . $line["id"]);
  54. }
  55. }
  56. }
  57. if ($debug) _debug("All done.");
  58. }
  59. static function prepare_headlines_digest($user_id, $days = 1, $limit = 1000) {
  60. require_once "lib/MiniTemplator.class.php";
  61. $tpl = new MiniTemplator;
  62. $tpl_t = new MiniTemplator;
  63. $tpl->readTemplateFromFile("templates/digest_template_html.txt");
  64. $tpl_t->readTemplateFromFile("templates/digest_template.txt");
  65. $user_tz_string = get_pref('USER_TIMEZONE', $user_id);
  66. $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string);
  67. $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
  68. $tpl->setVariable('CUR_TIME', date('G:i', $local_ts));
  69. $tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
  70. $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts));
  71. $affected_ids = array();
  72. if (DB_TYPE == "pgsql") {
  73. $interval_query = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'";
  74. } else if (DB_TYPE == "mysql") {
  75. $interval_query = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)";
  76. }
  77. $result = db_query("SELECT ttrss_entries.title,
  78. ttrss_feeds.title AS feed_title,
  79. COALESCE(ttrss_feed_categories.title, '" . __('Uncategorized') . "') AS cat_title,
  80. date_updated,
  81. ttrss_user_entries.ref_id,
  82. link,
  83. score,
  84. content,
  85. " . SUBSTRING_FOR_DATE . "(last_updated,1,19) AS last_updated
  86. FROM
  87. ttrss_user_entries,ttrss_entries,ttrss_feeds
  88. LEFT JOIN
  89. ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id)
  90. WHERE
  91. ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
  92. AND include_in_digest = true
  93. AND $interval_query
  94. AND ttrss_user_entries.owner_uid = $user_id
  95. AND unread = true
  96. AND score >= 0
  97. ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC
  98. LIMIT $limit");
  99. $headlines_count = db_num_rows($result);
  100. $headlines = array();
  101. while ($line = db_fetch_assoc($result)) {
  102. array_push($headlines, $line);
  103. }
  104. for ($i = 0; $i < sizeof($headlines); $i++) {
  105. $line = $headlines[$i];
  106. array_push($affected_ids, $line["ref_id"]);
  107. $updated = make_local_datetime($line['last_updated'], false,
  108. $user_id);
  109. /* if ($line["score"] != 0) {
  110. if ($line["score"] > 0) $line["score"] = '+' . $line["score"];
  111. $line["title"] .= " (".$line['score'].")";
  112. } */
  113. if (get_pref('ENABLE_FEED_CATS', $user_id)) {
  114. $line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title'];
  115. }
  116. $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
  117. $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
  118. $tpl->setVariable('ARTICLE_LINK', $line["link"]);
  119. $tpl->setVariable('ARTICLE_UPDATED', $updated);
  120. $tpl->setVariable('ARTICLE_EXCERPT',
  121. truncate_string(strip_tags($line["content"]), 300));
  122. // $tpl->setVariable('ARTICLE_CONTENT',
  123. // strip_tags($article_content));
  124. $tpl->addBlock('article');
  125. $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
  126. $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
  127. $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
  128. $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
  129. // $tpl_t->setVariable('ARTICLE_EXCERPT',
  130. // truncate_string(strip_tags($line["excerpt"]), 100));
  131. $tpl_t->addBlock('article');
  132. if ($headlines[$i]['feed_title'] != $headlines[$i + 1]['feed_title']) {
  133. $tpl->addBlock('feed');
  134. $tpl_t->addBlock('feed');
  135. }
  136. }
  137. $tpl->addBlock('digest');
  138. $tpl->generateOutputToString($tmp);
  139. $tpl_t->addBlock('digest');
  140. $tpl_t->generateOutputToString($tmp_t);
  141. return array($tmp, $headlines_count, $affected_ids, $tmp_t);
  142. }
  143. }