ccache.php 4.8 KB

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