From 3be75e05c0d037f80704550770447b5c21e597c4 Mon Sep 17 00:00:00 2001 From: Bogdan Abaev Date: Mon, 21 Aug 2023 14:23:04 +0000 Subject: [PATCH 1/3] include annotation tags for /collections/:key/tags Fixes: #162 --- model/Collection.inc.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/model/Collection.inc.php b/model/Collection.inc.php index e51af75f..06e775b2 100644 --- a/model/Collection.inc.php +++ b/model/Collection.inc.php @@ -629,6 +629,24 @@ public function getTagItemCounts() { foreach ($rows as $row) { $counts[$row['tagID']] = $row['numItems']; } + // Fetch the tags of annotations as well + $annotationsSql = "SELECT tagID, COUNT(*) AS numItems FROM tags JOIN itemTags USING (tagID) + JOIN itemAnnotations USING (itemID) + JOIN itemAttachments ON itemAttachments.itemID = itemAnnotations.parentItemID + JOIN collectionItems ON collectionItems.itemID = itemAttachments.sourceItemID + WHERE collectionID=? GROUP BY tagID;"; + + $rows = Zotero_DB::query($annotationsSql, $this->id, Zotero_Shards::getByLibraryID($this->libraryID)); + if (!$rows) { + return $counts; + } + // Add numItems into the same array. + foreach ($rows as $row) { + if (!array_key_exists($row['tagID'], $counts)) { + $counts[$row['tagID']] = 0; + } + $counts[$row['tagID']] += $row['numItems']; + } return $counts; } From e3a44386fdadbf0fe2c33e6f671cfd2d86439153 Mon Sep 17 00:00:00 2001 From: Bogdan Abaev Date: Tue, 22 Aug 2023 18:15:02 +0000 Subject: [PATCH 2/3] in /top mode, use top level itemID in tag filter In Zoteri_Items::search, if /top items are requested, fetch the top item's itemID during tag filtering as opposed to the actual item's itemID. That way, if an attachment has a tag, the top level attachment would be matches on that tag search query. --- model/Items.inc.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/model/Items.inc.php b/model/Items.inc.php index 76c99634..f079e08d 100644 --- a/model/Items.inc.php +++ b/model/Items.inc.php @@ -431,7 +431,12 @@ public static function search($libraryID, $onlyTopLevel = false, array $params = $tagIDs = array_unique($tagIDs); - $tmpSQL = "SELECT itemID FROM items JOIN itemTags USING (itemID) " + // If /top items are requested, fetch itemIDs of top level items whose + // children match the tag query + $tmpSelect = $onlyTopLevel ? "COALESCE(topLevelItemID, itemID)" : "itemID"; + $tmpJoin = $onlyTopLevel ? "LEFT JOIN itemTopLevel USING (itemID) " : " "; + $tmpSQL = "SELECT $tmpSelect FROM items JOIN itemTags USING (itemID) " + . $tmpJoin . "WHERE tagID IN (" . implode(',', array_fill(0, sizeOf($tagIDs), '?')) . ")"; $ids = Zotero_DB::columnQuery($tmpSQL, $tagIDs, $shardID); From 7453f3ce523b59c235a3f793a1b1ef38883a8206 Mon Sep 17 00:00:00 2001 From: Bogdan Abaev Date: Tue, 22 Aug 2023 18:28:57 +0000 Subject: [PATCH 3/3] minor syntax fixes --- model/Collection.inc.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/model/Collection.inc.php b/model/Collection.inc.php index 06e775b2..703d75c8 100644 --- a/model/Collection.inc.php +++ b/model/Collection.inc.php @@ -630,11 +630,11 @@ public function getTagItemCounts() { $counts[$row['tagID']] = $row['numItems']; } // Fetch the tags of annotations as well - $annotationsSql = "SELECT tagID, COUNT(*) AS numItems FROM tags JOIN itemTags USING (tagID) + $annotationsSql = "SELECT tagID, COUNT(*) AS numItems FROM itemTags JOIN itemAnnotations USING (itemID) - JOIN itemAttachments ON itemAttachments.itemID = itemAnnotations.parentItemID - JOIN collectionItems ON collectionItems.itemID = itemAttachments.sourceItemID - WHERE collectionID=? GROUP BY tagID;"; + JOIN itemAttachments ON (itemAttachments.itemID = itemAnnotations.parentItemID) + JOIN collectionItems ON (collectionItems.itemID = itemAttachments.sourceItemID) + WHERE collectionID=? GROUP BY tagID"; $rows = Zotero_DB::query($annotationsSql, $this->id, Zotero_Shards::getByLibraryID($this->libraryID)); if (!$rows) {