ccache.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. class CCache {
  3. static function zero_all($owner_uid) {
  4. $pdo = Db::pdo();
  5. $sth = $pdo->prepare("UPDATE ttrss_counters_cache SET
  6. value = 0 WHERE owner_uid = ?");
  7. $sth->execute([$owner_uid]);
  8. $sth = $pdo->prepare("UPDATE ttrss_cat_counters_cache SET
  9. value = 0 WHERE owner_uid = ?");
  10. $sth->execute([$owner_uid]);
  11. }
  12. static function remove($feed_id, $owner_uid, $is_cat = false) {
  13. if (!$is_cat) {
  14. $table = "ttrss_counters_cache";
  15. } else {
  16. $table = "ttrss_cat_counters_cache";
  17. }
  18. $pdo = Db::pdo();
  19. $sth = $pdo->prepare("DELETE FROM $table WHERE
  20. feed_id = ? AND owner_uid = ?");
  21. $sth->execute([$feed_id, $owner_uid]);
  22. }
  23. static function update_all($owner_uid) {
  24. $pdo = Db::pdo();
  25. if (get_pref('ENABLE_FEED_CATS', $owner_uid)) {
  26. $sth = $pdo->prepare("SELECT feed_id FROM ttrss_cat_counters_cache
  27. WHERE feed_id > 0 AND owner_uid = ?");
  28. $sth->execute([$owner_uid]);
  29. while ($line = $sth->fetch()) {
  30. CCache::update($line["feed_id"], $owner_uid, true);
  31. }
  32. /* We have to manually include category 0 */
  33. CCache::update(0, $owner_uid, true);
  34. } else {
  35. $sth = $pdo->prepare("SELECT feed_id FROM ttrss_counters_cache
  36. WHERE feed_id > 0 AND owner_uid = ?");
  37. $sth->execute([$owner_uid]);
  38. while ($line = $sth->fetch()) {
  39. print CCache::update($line["feed_id"], $owner_uid);
  40. }
  41. }
  42. }
  43. static function find($feed_id, $owner_uid, $is_cat = false,
  44. $no_update = false) {
  45. if (!is_numeric($feed_id)) return;
  46. if (!$is_cat) {
  47. $table = "ttrss_counters_cache";
  48. } else {
  49. $table = "ttrss_cat_counters_cache";
  50. }
  51. $pdo = Db::pdo();
  52. $sth = $pdo->prepare("SELECT value FROM $table
  53. WHERE owner_uid = ? AND feed_id = ?
  54. LIMIT 1");
  55. $sth->execute([$owner_uid, $feed_id]);
  56. if ($row = $sth->fetch()) {
  57. return $row["value"];
  58. } else {
  59. if ($no_update) {
  60. return -1;
  61. } else {
  62. return CCache::update($feed_id, $owner_uid, $is_cat);
  63. }
  64. }
  65. }
  66. static function update($feed_id, $owner_uid, $is_cat = false,
  67. $update_pcat = true, $pcat_fast = false) {
  68. if (!is_numeric($feed_id)) return;
  69. $prev_unread = CCache::find($feed_id, $owner_uid, $is_cat, true);
  70. /* When updating a label, all we need to do is recalculate feed counters
  71. * because labels are not cached */
  72. if ($feed_id < 0) {
  73. CCache::update_all($owner_uid);
  74. return;
  75. }
  76. if (!$is_cat) {
  77. $table = "ttrss_counters_cache";
  78. } else {
  79. $table = "ttrss_cat_counters_cache";
  80. }
  81. $pdo = Db::pdo();
  82. if ($is_cat && $feed_id >= 0) {
  83. /* Recalculate counters for child feeds */
  84. if (!$pcat_fast) {
  85. $sth = $pdo->prepare("SELECT id FROM ttrss_feeds
  86. WHERE owner_uid = :uid AND
  87. (cat_id = :cat OR (:cat = 0 AND cat_id IS NULL))");
  88. $sth->execute([":uid" => $owner_uid, ":cat" => $feed_id]);
  89. while ($line = $sth->fetch()) {
  90. CCache::update($line["id"], $owner_uid, false, false);
  91. }
  92. }
  93. $sth = $pdo->prepare("SELECT SUM(value) AS sv
  94. FROM ttrss_counters_cache, ttrss_feeds
  95. WHERE id = feed_id AND
  96. (cat_id = :cat OR (:cat = 0 AND cat_id IS NULL)) AND
  97. ttrss_counters_cache.owner_uid = :uid AND
  98. ttrss_feeds.owner_uid = :uid");
  99. $sth->execute([":uid" => $owner_uid, ":cat" => $feed_id]);
  100. $row = $sth->fetch();
  101. $unread = (int) $row["sv"];
  102. } else {
  103. $unread = (int) Feeds::getFeedArticles($feed_id, $is_cat, true, $owner_uid);
  104. }
  105. $tr_in_progress = false;
  106. try {
  107. $pdo->beginTransaction();
  108. } catch (Exception $e) {
  109. $tr_in_progress = true;
  110. }
  111. $sth = $pdo->prepare("SELECT feed_id FROM $table
  112. WHERE owner_uid = ? AND feed_id = ? LIMIT 1");
  113. $sth->execute([$owner_uid, $feed_id]);
  114. if ($sth->fetch()) {
  115. $sth = $pdo->prepare("UPDATE $table SET
  116. value = ?, updated = NOW() WHERE
  117. feed_id = ? AND owner_uid = ?");
  118. $sth->execute([$unread, $feed_id, $owner_uid]);
  119. } else {
  120. $sth = $pdo->prepare("INSERT INTO $table
  121. (feed_id, value, owner_uid, updated)
  122. VALUES
  123. (?, ?, ?, NOW())");
  124. $sth->execute([$feed_id, $unread, $owner_uid]);
  125. }
  126. if (!$tr_in_progress) $pdo->commit();
  127. if ($feed_id > 0 && $prev_unread != $unread) {
  128. if (!$is_cat) {
  129. /* Update parent category */
  130. if ($update_pcat) {
  131. $sth = $pdo->prepare("SELECT cat_id FROM ttrss_feeds
  132. WHERE owner_uid = ? AND id = ?");
  133. $sth->execute([$owner_uid, $feed_id]);
  134. if ($row = $sth->fetch()) {
  135. CCache::update($row["cat_id"], $owner_uid, true, true, true);
  136. }
  137. }
  138. }
  139. } else if ($feed_id < 0) {
  140. CCache::update_all($owner_uid);
  141. }
  142. return $unread;
  143. }
  144. }