diff --git a/internal/browser/firefox/extract_bookmark.go b/internal/browser/firefox/extract_bookmark.go index 286d2e6..cc9535b 100644 --- a/internal/browser/firefox/extract_bookmark.go +++ b/internal/browser/firefox/extract_bookmark.go @@ -9,22 +9,27 @@ import ( "github.com/moond4rk/hackbrowserdata/internal/timeconv" ) -const firefoxBookmarkQuery = `SELECT id, url, type, dateAdded, COALESCE(title, '') - FROM (SELECT * FROM moz_bookmarks INNER JOIN moz_places ON moz_bookmarks.fk=moz_places.id)` +// Folders and separators have no place, so the place join keeps only bookmarks; the parent join names their folder. +const firefoxBookmarkQuery = `SELECT b.id, p.url, b.type, b.dateAdded, COALESCE(b.title, ''), COALESCE(f.title, '') + FROM moz_bookmarks b + INNER JOIN moz_places p ON b.fk = p.id + LEFT JOIN moz_bookmarks f ON b.parent = f.id` func extractBookmarks(path string, out browser.Collector) error { bookmarks, err := sqlitex.QueryRows(path, true, firefoxBookmarkQuery, func(rows *sql.Rows) (record.Bookmark, error) { var id, dateAdded int64 - var url, title string + var url, title, folder string var bt int64 - if err := rows.Scan(&id, &url, &bt, &dateAdded, &title); err != nil { + if err := rows.Scan(&id, &url, &bt, &dateAdded, &title, &folder); err != nil { return record.Bookmark{}, err } return record.Bookmark{ + ID: id, Name: title, + Type: bookmarkType(bt), URL: url, - Folder: bookmarkType(bt), + Folder: folder, CreatedAt: timeconv.UnixMicro(dateAdded), }, nil }) diff --git a/internal/browser/firefox/extract_bookmark_test.go b/internal/browser/firefox/extract_bookmark_test.go index 0fb5f61..914173a 100644 --- a/internal/browser/firefox/extract_bookmark_test.go +++ b/internal/browser/firefox/extract_bookmark_test.go @@ -34,6 +34,35 @@ func TestExtractBookmarks(t *testing.T) { // Verify field mapping assert.Equal(t, "https://github.com", got[1].URL) - assert.Equal(t, "url", got[1].Folder) // type=1 → "url" + assert.Equal(t, int64(2), got[1].ID) + assert.Equal(t, "url", got[1].Type) + assert.Empty(t, got[1].Folder, "this fixture's bookmarks have no parent row") assert.False(t, got[1].CreatedAt.IsZero()) } + +// Before, every bookmark came out with folder "url", an empty type, and id 0. +func TestExtractBookmarks_FolderTypeAndIDFromTheTree(t *testing.T) { + path := createTestDB(t, "places.sqlite", []string{mozPlacesSchema, mozBookmarksSchema}, + insertMozPlace(1, "https://go.dev", "Go", 0, 0), + insertMozPlace(2, "https://github.com", "GitHub", 0, 0), + `INSERT INTO moz_bookmarks (id, type, fk, parent, position, title, dateAdded, guid) VALUES + (1, 2, NULL, 0, 0, '', 1, 'root________'), + (3, 2, NULL, 1, 1, 'toolbar', 1, 'toolbar_____'), + (7, 1, 1, 3, 0, 'Go', 1710000000000000, 'bookmark0007'), + (8, 2, NULL, 3, 1, 'Reading', 1, 'folder000008'), + (9, 1, 2, 8, 0, 'GitHub', 1700000000000000, 'bookmark0009'), + (10, 3, NULL, 3, 2, NULL, 1, 'separator010')`, + ) + + var out testCollector + require.NoError(t, extractBookmarks(path, &out)) + byID := map[int64]record.Bookmark{} + for _, b := range collected[record.Bookmark](t, &out) { + byID[b.ID] = b + } + require.Len(t, byID, 2, "folders and separators are not bookmarks") + want := record.Bookmark{ID: 7, Name: "Go", Type: "url", URL: "https://go.dev", Folder: "toolbar", CreatedAt: byID[7].CreatedAt} + assert.Equal(t, want, byID[7]) + assert.Equal(t, "Reading", byID[9].Folder) + assert.Equal(t, "url", byID[9].Type) +} diff --git a/internal/browser/safari/extract_bookmark.go b/internal/browser/safari/extract_bookmark.go index cc543a8..ec0ad78 100644 --- a/internal/browser/safari/extract_bookmark.go +++ b/internal/browser/safari/extract_bookmark.go @@ -47,9 +47,10 @@ func extractBookmarks(path string, out browser.Collector) error { return nil } -// walkBookmarks recursively traverses the bookmark tree, collecting leaf entries. +// walkBookmarks collects the leaves in tree order. A leaf's ID is its position among them, counting from 1, because +// the plist stores no number and an index within one folder repeats across folders. func walkBookmarks(nodes []safariBookmark, folder string, out *[]record.Bookmark) { - for i, node := range nodes { + for _, node := range nodes { switch node.Type { case bookmarkTypeLeaf: title := node.URIDictionary.Title @@ -60,7 +61,7 @@ func walkBookmarks(nodes []safariBookmark, folder string, out *[]record.Bookmark continue } *out = append(*out, record.Bookmark{ - ID: int64(i), + ID: int64(len(*out) + 1), Name: title, URL: node.URLString, Folder: folder, diff --git a/internal/browser/safari/extract_bookmark_test.go b/internal/browser/safari/extract_bookmark_test.go index d2d81b4..cc36d8c 100644 --- a/internal/browser/safari/extract_bookmark_test.go +++ b/internal/browser/safari/extract_bookmark_test.go @@ -174,3 +174,32 @@ func TestExtractBookmarks_Empty(t *testing.T) { require.NoError(t, err) assert.Empty(t, bookmarks) } + +// Before, each ID was the index among its siblings, so the first bookmark of every folder was 0. +func TestExtractBookmarks_IDsAreUniqueAcrossFolders(t *testing.T) { + leaf := func(url string) safariBookmark { + return safariBookmark{Type: bookmarkTypeLeaf, URLString: url, URIDictionary: uriDictionary{Title: url}} + } + path := buildTestBookmarksPlist(t, safariBookmark{ + Type: bookmarkTypeList, + Children: []safariBookmark{ + {Type: bookmarkTypeList, Title: "BookmarksBar", Children: []safariBookmark{ + leaf("https://a.test"), + {Type: bookmarkTypeList, Title: "Nested", Children: []safariBookmark{leaf("https://b.test")}}, + leaf("https://c.test"), + }}, + {Type: bookmarkTypeList, Title: "BookmarksMenu", Children: []safariBookmark{leaf("https://d.test")}}, + }, + }) + + var out testCollector + require.NoError(t, extractBookmarks(path, &out)) + var ids []int64 + var urls []string + for _, b := range collected[record.Bookmark](t, &out) { + ids = append(ids, b.ID) + urls = append(urls, b.URL) + } + assert.Equal(t, []int64{1, 2, 3, 4}, ids) + assert.Equal(t, []string{"https://a.test", "https://b.test", "https://c.test", "https://d.test"}, urls) +} diff --git a/testdata/extract-firefox-csv.golden b/testdata/extract-firefox-csv.golden index 23169c2..66768a5 100644 --- a/testdata/extract-firefox-csv.golden +++ b/testdata/extract-firefox-csv.golden @@ -1,13 +1,13 @@ --- bookmark.csv (860 bytes) -- +-- bookmark.csv (914 bytes) -- browser,profile,id,name,type,url,folder,created_at -Firefox,abcd1234.default-release,0,,,https://www.home.firefox.test/,url,2024-03-01T20:00:00Z -Firefox,abcd1234.default-release,0,Café menu,,https://shop.home.firefox.test/menu,url,2024-03-01T19:00:00Z -Firefox,abcd1234.default-release,0,Long read & tags,,https://news.home.firefox.test/long-read,url,2024-03-01T18:00:00Z -Firefox,abcd1234.default-release,0,Docs,,https://docs.home.firefox.test/,url,2024-03-01T17:00:00Z -Firefox,wxyz5678.work,0,,,https://www.work.firefox.test/,url,2024-06-01T20:00:00Z -Firefox,wxyz5678.work,0,Café menu,,https://shop.work.firefox.test/menu,url,2024-06-01T19:00:00Z -Firefox,wxyz5678.work,0,Long read & tags,,https://news.work.firefox.test/long-read,url,2024-06-01T18:00:00Z -Firefox,wxyz5678.work,0,Docs,,https://docs.work.firefox.test/,url,2024-06-01T17:00:00Z +Firefox,abcd1234.default-release,12,,url,https://www.home.firefox.test/,unfiled,2024-03-01T20:00:00Z +Firefox,abcd1234.default-release,11,Café menu,url,https://shop.home.firefox.test/menu,menu,2024-03-01T19:00:00Z +Firefox,abcd1234.default-release,9,Long read & tags,url,https://news.home.firefox.test/long-read,Reading,2024-03-01T18:00:00Z +Firefox,abcd1234.default-release,7,Docs,url,https://docs.home.firefox.test/,toolbar,2024-03-01T17:00:00Z +Firefox,wxyz5678.work,12,,url,https://www.work.firefox.test/,unfiled,2024-06-01T20:00:00Z +Firefox,wxyz5678.work,11,Café menu,url,https://shop.work.firefox.test/menu,menu,2024-06-01T19:00:00Z +Firefox,wxyz5678.work,9,Long read & tags,url,https://news.work.firefox.test/long-read,Reading,2024-06-01T18:00:00Z +Firefox,wxyz5678.work,7,Docs,url,https://docs.work.firefox.test/,toolbar,2024-06-01T17:00:00Z -- cookie.csv (1211 bytes) -- browser,profile,host,path,name,value,is_secure,is_http_only,has_expire,is_persistent,expire_at,created_at,same_site Firefox,abcd1234.default-release,app.home.firefox.test,/,session,ephemeral,true,true,false,false,,2024-03-01T09:40:00Z,unspecified diff --git a/testdata/extract-firefox-json.golden b/testdata/extract-firefox-json.golden index d9e6ab4..e08a484 100644 --- a/testdata/extract-firefox-json.golden +++ b/testdata/extract-firefox-json.golden @@ -1,83 +1,83 @@ --- bookmark.json (1935 bytes) -- +-- bookmark.json (1989 bytes) -- [ { "browser": "Firefox", "profile": "abcd1234.default-release", - "id": 0, + "id": 12, "name": "", - "type": "", + "type": "url", "url": "https://www.home.firefox.test/", - "folder": "url", + "folder": "unfiled", "created_at": "2024-03-01T20:00:00Z" }, { "browser": "Firefox", "profile": "abcd1234.default-release", - "id": 0, + "id": 11, "name": "Café menu", - "type": "", + "type": "url", "url": "https://shop.home.firefox.test/menu", - "folder": "url", + "folder": "menu", "created_at": "2024-03-01T19:00:00Z" }, { "browser": "Firefox", "profile": "abcd1234.default-release", - "id": 0, + "id": 9, "name": "Long read \u003cwith\u003e \u0026 tags", - "type": "", + "type": "url", "url": "https://news.home.firefox.test/long-read", - "folder": "url", + "folder": "Reading", "created_at": "2024-03-01T18:00:00Z" }, { "browser": "Firefox", "profile": "abcd1234.default-release", - "id": 0, + "id": 7, "name": "Docs", - "type": "", + "type": "url", "url": "https://docs.home.firefox.test/", - "folder": "url", + "folder": "toolbar", "created_at": "2024-03-01T17:00:00Z" }, { "browser": "Firefox", "profile": "wxyz5678.work", - "id": 0, + "id": 12, "name": "", - "type": "", + "type": "url", "url": "https://www.work.firefox.test/", - "folder": "url", + "folder": "unfiled", "created_at": "2024-06-01T20:00:00Z" }, { "browser": "Firefox", "profile": "wxyz5678.work", - "id": 0, + "id": 11, "name": "Café menu", - "type": "", + "type": "url", "url": "https://shop.work.firefox.test/menu", - "folder": "url", + "folder": "menu", "created_at": "2024-06-01T19:00:00Z" }, { "browser": "Firefox", "profile": "wxyz5678.work", - "id": 0, + "id": 9, "name": "Long read \u003cwith\u003e \u0026 tags", - "type": "", + "type": "url", "url": "https://news.work.firefox.test/long-read", - "folder": "url", + "folder": "Reading", "created_at": "2024-06-01T18:00:00Z" }, { "browser": "Firefox", "profile": "wxyz5678.work", - "id": 0, + "id": 7, "name": "Docs", - "type": "", + "type": "url", "url": "https://docs.work.firefox.test/", - "folder": "url", + "folder": "toolbar", "created_at": "2024-06-01T17:00:00Z" } ] diff --git a/testdata/extract-safari-csv.golden b/testdata/extract-safari-csv.golden index 93c5c02..7cc7328 100644 --- a/testdata/extract-safari-csv.golden +++ b/testdata/extract-safari-csv.golden @@ -1,11 +1,11 @@ -- bookmark.csv (566 bytes) -- browser,profile,id,name,type,url,folder,created_at -Safari,default,0,Docs,bookmark,https://docs.home.safari.test/,BookmarksBar, -Safari,default,0,Long read & tags,bookmark,https://news.home.safari.test/long-read,Reading, -Safari,default,2,Home,bookmark,https://www.home.safari.test/,BookmarksBar, -Safari,default,0,Café menu,bookmark,https://shop.home.safari.test/menu,BookmarksMenu, -Safari,default,0,Saved post,bookmark,https://blog.home.safari.test/post,ReadingList, -Safari,default,1,Untitled entry,bookmark,https://blog.home.safari.test/draft,ReadingList, +Safari,default,1,Docs,bookmark,https://docs.home.safari.test/,BookmarksBar, +Safari,default,2,Long read & tags,bookmark,https://news.home.safari.test/long-read,Reading, +Safari,default,3,Home,bookmark,https://www.home.safari.test/,BookmarksBar, +Safari,default,4,Café menu,bookmark,https://shop.home.safari.test/menu,BookmarksMenu, +Safari,default,5,Saved post,bookmark,https://blog.home.safari.test/post,ReadingList, +Safari,default,6,Untitled entry,bookmark,https://blog.home.safari.test/draft,ReadingList, -- cookie.csv (831 bytes) -- browser,profile,host,path,name,value,is_secure,is_http_only,has_expire,is_persistent,expire_at,created_at,same_site Safari,default,app.home.safari.test,/,csrf,token-123,false,true,true,true,2024-03-02T09:00:00Z,2024-03-01T09:40:00Z, diff --git a/testdata/extract-safari-json.golden b/testdata/extract-safari-json.golden index 390f023..513edba 100644 --- a/testdata/extract-safari-json.golden +++ b/testdata/extract-safari-json.golden @@ -3,7 +3,7 @@ { "browser": "Safari", "profile": "default", - "id": 0, + "id": 1, "name": "Docs", "type": "bookmark", "url": "https://docs.home.safari.test/", @@ -13,7 +13,7 @@ { "browser": "Safari", "profile": "default", - "id": 0, + "id": 2, "name": "Long read \u003cwith\u003e \u0026 tags", "type": "bookmark", "url": "https://news.home.safari.test/long-read", @@ -23,7 +23,7 @@ { "browser": "Safari", "profile": "default", - "id": 2, + "id": 3, "name": "Home", "type": "bookmark", "url": "https://www.home.safari.test/", @@ -33,7 +33,7 @@ { "browser": "Safari", "profile": "default", - "id": 0, + "id": 4, "name": "Café menu", "type": "bookmark", "url": "https://shop.home.safari.test/menu", @@ -43,7 +43,7 @@ { "browser": "Safari", "profile": "default", - "id": 0, + "id": 5, "name": "Saved post", "type": "bookmark", "url": "https://blog.home.safari.test/post", @@ -53,7 +53,7 @@ { "browser": "Safari", "profile": "default", - "id": 1, + "id": 6, "name": "Untitled entry", "type": "bookmark", "url": "https://blog.home.safari.test/draft",