From e02f3fcb1347197e467e04a69d0be1bf92a2eaeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= Date: Sat, 8 Sep 2018 19:31:42 +0200 Subject: [PATCH] [Bookmarks Runner] Remove duplicate results for bookmarks Summary: An entry from the moz_places db may have several referring entries in the moz_bookmarks db, i.e. where moz_places.id = moz_bookmarks.fk. One of these entries is the "main" entry, while the other ones are used for tags. Only the main entry has a title, while the others have not. The tag entries have the same type as the main entry, even the parents have the same type (folder). Another source for duplicate URLs are manually created bookmarks, e.g. in different folders. These may have the same or different titles. To remove these duplicates, merge all entries with the same URL. If a URL has multiple entries, keep all with distinct titles, otherwise keep at least one - a bookmark may have an empty title. See also T9626 Test Plan: - Create a bookmark - Add one or more tags - Open FFs bookmarks sidebar - Copy and paste the new entry - Copy and paste the new entry again, change its title Search for the new bookmark. It should appear exactly twice, once with the original title, once with the modified one. Without patch, it appears 3 times, plus once more for each tag. Caveat: The bookmarks db has to be checkpointed to make the new entries visible in the main DB file. To force checkpointing, execute: $> sqlite3 -column -header ~/.mozilla/firefox/*.default/places.sqlite "PRAGMA wal_checkpoint" Reviewers: #plasma, broulik Reviewed By: #plasma, broulik Subscribers: broulik, davidedmundson, zzag, ngraham, plasma-devel Tags: #plasma Maniphest Tasks: T9626 Differential Revision: https://phabricator.kde.org/D15357 --- runners/bookmarks/browsers/firefox.cpp | 30 +++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/runners/bookmarks/browsers/firefox.cpp b/runners/bookmarks/browsers/firefox.cpp index ec3735d6c..b382578c9 100644 --- a/runners/bookmarks/browsers/firefox.cpp +++ b/runners/bookmarks/browsers/firefox.cpp @@ -99,6 +99,7 @@ QList< BookmarkMatch > Firefox::match(const QString& term, bool addEverything) + escapedTerm + "%')"); } QList results = m_fetchsqlite->query(query, QMap()); + QMultiMap uniqueResults; foreach(QVariantMap result, results) { const QString title = result.value(QStringLiteral("title")).toString(); const QUrl url = result.value(QStringLiteral("url")).toUrl(); @@ -109,7 +110,34 @@ QList< BookmarkMatch > Firefox::match(const QString& term, bool addEverything) continue; } - BookmarkMatch bookmarkMatch( m_favicon, term, title, url.toString()); + auto urlString = url.toString(); + // After joining we may have multiple results for each URL: + // 1) one for each bookmark folder (same or different titles) + // 2) one for each tag (no title for all but the first entry) + auto keyRange = uniqueResults.equal_range(urlString); + auto it = keyRange.first; + if (!title.isEmpty()) { + while (it != keyRange.second) { + if (*it == title) { + // same URL and title in multiple bookmark folders + break; + } + if (it->isEmpty()) { + // add a title if there was none for the URL + *it = title; + break; + } + ++it; + } + } + if (it == keyRange.second) { + // first or unique entry + uniqueResults.insert(urlString, title); + } + } + + for (auto result = uniqueResults.constKeyValueBegin(); result != uniqueResults.constKeyValueEnd(); ++result) { + BookmarkMatch bookmarkMatch(m_favicon, term, (*result).second, (*result).first); bookmarkMatch.addTo(matches, addEverything); }