digest.php 5.7 KB

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