ccache.php 4.79 KB
Newer Older
1
<?php
2 3
class CCache {
	static function zero_all($owner_uid) {
Andrew Dolgov's avatar
Andrew Dolgov committed
4 5 6 7 8
		$pdo = Db::pdo();

		$sth = $pdo->prepare("UPDATE ttrss_counters_cache SET
			value = 0 WHERE owner_uid = ?");
		$sth->execute([$owner_uid]);
9

Andrew Dolgov's avatar
Andrew Dolgov committed
10 11 12
		$sth = $pdo->prepare("UPDATE ttrss_cat_counters_cache SET
			value = 0 WHERE owner_uid = ?");
		$sth->execute([$owner_uid]);
13 14
	}

15
	static function remove($feed_id, $owner_uid, $is_cat = false) {
16

17 18
		$feed_id = (int) $feed_id;

19 20 21 22 23 24
		if (!$is_cat) {
			$table = "ttrss_counters_cache";
		} else {
			$table = "ttrss_cat_counters_cache";
		}

Andrew Dolgov's avatar
Andrew Dolgov committed
25 26 27 28 29
		$pdo = Db::pdo();

		$sth = $pdo->prepare("DELETE FROM $table WHERE
			feed_id = ? AND owner_uid = ?");
		$sth->execute([$feed_id, $owner_uid]);
30 31 32

	}

33
	static function update_all($owner_uid) {
34

Andrew Dolgov's avatar
Andrew Dolgov committed
35 36
		$pdo = Db::pdo();

37
		if (get_pref('ENABLE_FEED_CATS', $owner_uid)) {
38

Andrew Dolgov's avatar
Andrew Dolgov committed
39 40 41
			$sth = $pdo->prepare("SELECT feed_id FROM ttrss_cat_counters_cache
				WHERE feed_id > 0 AND owner_uid = ?");
			$sth->execute([$owner_uid]);
42

Andrew Dolgov's avatar
Andrew Dolgov committed
43
			while ($line = $sth->fetch()) {
44
				CCache::update($line["feed_id"], $owner_uid, true);
45 46 47 48
			}

			/* We have to manually include category 0 */

49
			CCache::update(0, $owner_uid, true);
50 51

		} else {
Andrew Dolgov's avatar
Andrew Dolgov committed
52 53 54
			$sth = $pdo->prepare("SELECT feed_id FROM ttrss_counters_cache
				WHERE feed_id > 0 AND owner_uid = ?");
			$sth->execute([$owner_uid]);
55

Andrew Dolgov's avatar
Andrew Dolgov committed
56
			while ($line = $sth->fetch()) {
57
				print CCache::update($line["feed_id"], $owner_uid);
58 59 60 61 62 63

			}

		}
	}

64 65
	static function find($feed_id, $owner_uid, $is_cat = false,
						 $no_update = false) {
66

67 68 69 70 71 72
		// "" (null) is valid and should be cast to 0 (uncategorized)
		// everything else i.e. tags are not
		if (!is_numeric($feed_id) && $feed_id)
			return;

		$feed_id = (int) $feed_id;
73 74 75 76 77 78 79

		if (!$is_cat) {
			$table = "ttrss_counters_cache";
		} else {
			$table = "ttrss_cat_counters_cache";
		}

Andrew Dolgov's avatar
Andrew Dolgov committed
80
		$pdo = Db::pdo();
81

Andrew Dolgov's avatar
Andrew Dolgov committed
82 83
		$sth = $pdo->prepare("SELECT value FROM $table
			WHERE owner_uid = ? AND feed_id = ?
84 85
			LIMIT 1");

Andrew Dolgov's avatar
Andrew Dolgov committed
86 87 88 89
		$sth->execute([$owner_uid, $feed_id]);

		if ($row = $sth->fetch()) {
			return $row["value"];
90 91 92 93
		} else {
			if ($no_update) {
				return -1;
			} else {
94
				return CCache::update($feed_id, $owner_uid, $is_cat);
95 96 97 98 99
			}
		}

	}

100 101
	static function update($feed_id, $owner_uid, $is_cat = false,
						   $update_pcat = true, $pcat_fast = false) {
102

103 104 105 106 107 108
		// "" (null) is valid and should be cast to 0 (uncategorized)
		// everything else i.e. tags are not
		if (!is_numeric($feed_id) && $feed_id)
			return;

		$feed_id = (int) $feed_id;
109

110
		$prev_unread = CCache::find($feed_id, $owner_uid, $is_cat, true);
111 112 113 114 115

		/* When updating a label, all we need to do is recalculate feed counters
		 * because labels are not cached */

		if ($feed_id < 0) {
116
			CCache::update_all($owner_uid);
117 118 119 120 121 122 123 124 125
			return;
		}

		if (!$is_cat) {
			$table = "ttrss_counters_cache";
		} else {
			$table = "ttrss_cat_counters_cache";
		}

Andrew Dolgov's avatar
Andrew Dolgov committed
126
		$pdo = Db::pdo();
127

Andrew Dolgov's avatar
Andrew Dolgov committed
128
		if ($is_cat && $feed_id >= 0) {
129 130
			/* Recalculate counters for child feeds */

131
			if (!$pcat_fast) {
Andrew Dolgov's avatar
Andrew Dolgov committed
132
				$sth = $pdo->prepare("SELECT id FROM ttrss_feeds
Andrew Dolgov's avatar
Andrew Dolgov committed
133 134
						WHERE owner_uid = :uid AND 
							(cat_id = :cat OR (:cat = 0 AND cat_id IS NULL))");
Andrew Dolgov's avatar
Andrew Dolgov committed
135
				$sth->execute([":uid" => $owner_uid, ":cat" => $feed_id]);
136

Andrew Dolgov's avatar
Andrew Dolgov committed
137
				while ($line = $sth->fetch()) {
138
					CCache::update((int)$line["id"], $owner_uid, false, false);
139
				}
140
			}
141

Andrew Dolgov's avatar
Andrew Dolgov committed
142
			$sth = $pdo->prepare("SELECT SUM(value) AS sv
143
				FROM ttrss_counters_cache, ttrss_feeds
Andrew Dolgov's avatar
Andrew Dolgov committed
144
				WHERE id = feed_id AND 
Andrew Dolgov's avatar
Andrew Dolgov committed
145
				(cat_id = :cat OR (:cat = 0 AND cat_id IS NULL)) AND
Andrew Dolgov's avatar
Andrew Dolgov committed
146 147 148 149
				ttrss_counters_cache.owner_uid = :uid AND
				ttrss_feeds.owner_uid = :uid");
			$sth->execute([":uid" => $owner_uid, ":cat" => $feed_id]);
			$row = $sth->fetch();
150

Andrew Dolgov's avatar
Andrew Dolgov committed
151
			$unread = (int) $row["sv"];
152 153

		} else {
Andrew Dolgov's avatar
Andrew Dolgov committed
154
			$unread = (int) Feeds::getFeedArticles($feed_id, $is_cat, true, $owner_uid);
155 156
		}

Andrew Dolgov's avatar
Andrew Dolgov committed
157 158 159 160 161 162 163
		$tr_in_progress = false;

		try {
			$pdo->beginTransaction();
		} catch (Exception $e) {
			$tr_in_progress = true;
		}
164

Andrew Dolgov's avatar
Andrew Dolgov committed
165 166 167
		$sth = $pdo->prepare("SELECT feed_id FROM $table
			WHERE owner_uid = ? AND feed_id = ? LIMIT 1");
		$sth->execute([$owner_uid, $feed_id]);
168

Andrew Dolgov's avatar
Andrew Dolgov committed
169 170 171 172 173 174 175
		if ($sth->fetch()) {

			$sth = $pdo->prepare("UPDATE $table SET
				value = ?, updated = NOW() WHERE
				feed_id = ? AND owner_uid = ?");

			$sth->execute([$unread, $feed_id, $owner_uid]);
176 177

		} else {
Andrew Dolgov's avatar
Andrew Dolgov committed
178
			$sth = $pdo->prepare("INSERT INTO $table
179 180
				(feed_id, value, owner_uid, updated)
				VALUES
Andrew Dolgov's avatar
Andrew Dolgov committed
181 182
				(?, ?, ?, NOW())");
			$sth->execute([$feed_id, $unread, $owner_uid]);
183 184
		}

Andrew Dolgov's avatar
Andrew Dolgov committed
185
		if (!$tr_in_progress) $pdo->commit();
186 187 188 189 190 191 192 193 194

		if ($feed_id > 0 && $prev_unread != $unread) {

			if (!$is_cat) {

				/* Update parent category */

				if ($update_pcat) {

Andrew Dolgov's avatar
Andrew Dolgov committed
195 196 197
					$sth = $pdo->prepare("SELECT cat_id FROM ttrss_feeds
						WHERE owner_uid = ? AND id = ?");
					$sth->execute([$owner_uid, $feed_id]);
198

Andrew Dolgov's avatar
Andrew Dolgov committed
199
					if ($row = $sth->fetch()) {
200
						CCache::update((int)$row["cat_id"], $owner_uid, true, true, true);
Andrew Dolgov's avatar
Andrew Dolgov committed
201
					}
202 203 204
				}
			}
		} else if ($feed_id < 0) {
205
			CCache::update_all($owner_uid);
206 207 208 209 210
		}

		return $unread;
	}

211
}