Skip to content
23 changes: 16 additions & 7 deletions index_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ func TestGetIndexesRebuildsOlderDocuments(t *testing.T) {
seedDocumentIndex(t, version)
authors := index.CreateAuthorsIndex(homeDir + authorsIndexPath)
retrieved := time.Now().UTC().Truncate(time.Second)
if err := authors.Index("existing-author", index.Author{
Slug: "existing-author", Name: "Existing Author", DataSourceID: "Q1", RetrievedOn: retrieved,
if err := authors.Index("existing-author", struct {
index.Author
DataSourceReferences string
}{
Author: index.Author{Slug: "existing-author", Name: "Existing Author", RetrievedOn: retrieved},
DataSourceReferences: `{"wikidata":"Q1"}`,
}); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -150,7 +154,7 @@ func TestGetIndexesRebuildsOlderDocuments(t *testing.T) {
t.Fatalf("library must be searchable after rebuilding: %+v, %v", book, err)
}
author, err := idx.Author("existing-author", "")
if err != nil || author.DataSourceID != "Q1" || !author.RetrievedOn.Equal(retrieved) {
if err != nil || author.DataSourceReferences["wikidata"] != "Q1" || !author.RetrievedOn.Equal(retrieved) {
t.Fatalf("rebuilding must preserve author metadata: %+v, %v", author, err)
}
if err := idx.Close(); err != nil {
Expand Down Expand Up @@ -269,14 +273,19 @@ func TestGetIndexesMigratesAuthorsWithoutReindexingDocuments(t *testing.T) {
t.Fatal(err)
}
legacyMapping := index.CreateAuthorsMapping().(*mapping.IndexMappingImpl)
delete(legacyMapping.DefaultMapping.Properties, "DataSourceLinks")
delete(legacyMapping.DefaultMapping.Properties, "DataSourceReferences")
legacyMapping.DefaultMapping.AddFieldMappingsAt("DataSourceID", bleve.NewKeywordFieldMapping())
authors, err := bleve.New(homeDir+authorsIndexPath, legacyMapping)
if err != nil {
t.Fatal(err)
}
retrieved := time.Now().UTC().Truncate(time.Second)
if err := authors.Index("alice-borchardt", index.Author{
Slug: "alice-borchardt", Name: "Alice Borchardt", DataSourceID: "Q1", RetrievedOn: retrieved,
if err := authors.Index("alice-borchardt", struct {
index.Author
DataSourceID string
}{
Author: index.Author{Slug: "alice-borchardt", Name: "Alice Borchardt", RetrievedOn: retrieved},
DataSourceID: "Q1",
}); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -305,7 +314,7 @@ func TestGetIndexesMigratesAuthorsWithoutReindexingDocuments(t *testing.T) {
t.Fatalf("author migration changed document enrichment state: %+v, %v", data, err)
}
author, err := idx.Author("alice-borchardt", "")
if err != nil || author.DataSourceID != "Q1" || !author.RetrievedOn.Equal(retrieved) {
if err != nil || author.DataSourceReferences["wikidata"] != "Q1" || !author.RetrievedOn.Equal(retrieved) {
t.Fatalf("expected existing author metadata preserved: %+v, %v", author, err)
}
if pending, err := idx.AuthorsWithoutInfo(); err != nil || len(pending) != 0 {
Expand Down
20 changes: 10 additions & 10 deletions internal/biography/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var spaceBeforePunctuation = regexp.MustCompile(` +([,.;:!?)\]])`)
// Source supplies cleaned biography HTML (b, strong, i and em, with newline-separated
// paragraphs) and identifies the profile it belongs to.
type Source interface {
LinkKey() string
ReferenceKey() string
ProfileServiceName() string
ProfileInputName() string
ProfileValue(author index.Author) string
Expand Down Expand Up @@ -108,7 +108,7 @@ func (s *Service) RefreshMetadataBio(ctx context.Context, author index.Author, l
}

func (s *Service) authorLink(author index.Author) (string, bool) {
value := author.DataSourceLinks[s.source.LinkKey()]
value := author.DataSourceReferences[s.source.ReferenceKey()]
id, err := s.source.NormalizeAuthorID(value)
return value, err == nil && id != "" && id == value
}
Expand Down Expand Up @@ -177,8 +177,8 @@ func (s *Service) RefreshBio(ctx context.Context, author index.Author, language
type storeFunc func(slug string, bio index.AuthorBio) error

func (s *Service) bioStore(author index.Author, merge bool) storeFunc {
linkKey := s.source.LinkKey()
previousLink := author.DataSourceLinks[linkKey]
linkKey := s.source.ReferenceKey()
previousLink := author.DataSourceReferences[linkKey]
return func(slug string, bio index.AuthorBio) error {
if bio.Source != previousLink {
return s.index.StoreCanonicalAuthorBio(slug, linkKey, previousLink, bio)
Expand All @@ -191,9 +191,9 @@ func (s *Service) bioStore(author index.Author, merge bool) storeFunc {
// An empty ID removes the profile and biography.
func (s *Service) UpdateBio(ctx context.Context, author index.Author, id, language string) (string, string, error) {
s = s.forLanguage(language)
linkKey := s.source.LinkKey()
linkKey := s.source.ReferenceKey()
if id == "" {
if err := s.index.StoreCanonicalAuthorBio(author.Slug, linkKey, author.DataSourceLinks[linkKey], index.AuthorBio{}); err != nil {
if err := s.index.StoreCanonicalAuthorBio(author.Slug, linkKey, author.DataSourceReferences[linkKey], index.AuthorBio{}); err != nil {
return "", "", fmt.Errorf("clearing %s author link: %w", s.ProfileServiceName(), err)
}
return "", "", nil
Expand All @@ -206,11 +206,11 @@ func (s *Service) UpdateBio(ctx context.Context, author index.Author, id, langua
return "", "", fmt.Errorf("invalid %s author ID %q", s.ProfileServiceName(), id)
}
save := s.bioStore(author, false)
author.DataSourceLinks = maps.Clone(author.DataSourceLinks)
if author.DataSourceLinks == nil {
author.DataSourceLinks = make(map[string]string)
author.DataSourceReferences = maps.Clone(author.DataSourceReferences)
if author.DataSourceReferences == nil {
author.DataSourceReferences = make(map[string]string)
}
author.DataSourceLinks[linkKey] = id
author.DataSourceReferences[linkKey] = id
author.Bio = index.AuthorBio{}
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), requestTimeout)
defer cancel()
Expand Down
10 changes: 5 additions & 5 deletions internal/biography/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ type fakeSource struct {
refreshes []bool
}

func (*fakeSource) LinkKey() string { return "catalog" }
func (*fakeSource) ReferenceKey() string { return "catalog" }
func (*fakeSource) ProfileServiceName() string { return "Example catalog" }
func (*fakeSource) ProfileInputName() string { return "catalogID" }
func (*fakeSource) ProfileValue(author index.Author) string {
return author.DataSourceLinks["catalog"]
return author.DataSourceReferences["catalog"]
}
func (*fakeSource) NormalizeAuthorID(value string) (string, error) {
if value == "invalid" {
Expand Down Expand Up @@ -114,7 +114,7 @@ func (f *fakeBioIndex) StoreAuthorBioForLink(_ string, linkKey, previousSource s
func catalogAuthor(bio index.AuthorBio) index.Author {
return index.Author{
Slug: "alice", Name: "Alice Example",
DataSourceLinks: map[string]string{"catalog": "alice"}, Bio: bio,
DataSourceReferences: map[string]string{"catalog": "alice"}, Bio: bio,
}
}

Expand Down Expand Up @@ -423,7 +423,7 @@ func TestUpdateBio(t *testing.T) {
source := &fakeSource{desc: englishBio, canonical: "primary"}
idx := &fakeBioIndex{}
provider := biography.NewService(source, &fakeTranslator{}, idx)
author := index.Author{Slug: "alice", DataSourceLinks: map[string]string{"catalog": previous}}
author := index.Author{Slug: "alice", DataSourceReferences: map[string]string{"catalog": previous}}
text, _, err := provider.UpdateBio(context.Background(), author, "variant", "es")
if err != nil || text != "[es] "+englishBio || idx.linkKey != "catalog" || idx.previousSource != previous ||
len(idx.replaced) != 1 || idx.replaced[0].Source != "primary" {
Expand All @@ -432,7 +432,7 @@ func TestUpdateBio(t *testing.T) {
if !slices.Equal(source.calls, []string{"variant"}) || !slices.Equal(source.refreshes, []bool{true}) {
t.Fatalf("profile edits must bypass source caches, got %q, refresh=%v", source.calls, source.refreshes)
}
if author.DataSourceLinks["catalog"] != previous {
if author.DataSourceReferences["catalog"] != previous {
t.Fatal("updating must not mutate the caller's author")
}
})
Expand Down
34 changes: 17 additions & 17 deletions internal/datasource/biblioreads/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ func TestEnrichStoresMatchingResult(t *testing.T) {
t.Error("expected document to be marked as BiblioReads-enriched")
}
author, err := idx.Author("alice-borchardt", "")
if err != nil || author.DataSourceLinks["goodreads"] != wantAuthorURL {
if err != nil || author.DataSourceReferences["goodreads"] != wantAuthorURL {
t.Fatalf("expected matching author URL stored, got %+v, %v", author, err)
}
other, err := idx.Author("second-author", "")
if err != nil || other.DataSourceLinks["goodreads"] != "" {
if err != nil || other.DataSourceReferences["goodreads"] != "" {
t.Fatalf("unmatched coauthor must not receive the URL, got %+v, %v", other, err)
}
if want := []string{"La Reina Dragon Alice Borchardt", "La Reina Dragon"}; !slices.Equal(source.Queries(), want) {
Expand Down Expand Up @@ -240,42 +240,42 @@ func TestEnrichStoresSecondAuthorURLAndPreservesItOnEdits(t *testing.T) {
if err != nil {
t.Fatal(err)
}
stale.DataSourceLinks = map[string]string{"other": "https://example.org/second-author"}
stale.DataSourceReferences = map[string]string{"other": "https://example.org/second-author"}
if err := idx.IndexAuthor(stale); err != nil {
t.Fatal(err)
}
if err := idx.EnrichDocuments(10); err != nil {
t.Fatal(err)
}
author, err := idx.Author("second-author", "")
if err != nil || author.DataSourceLinks["goodreads"] != "2" ||
author.DataSourceLinks["other"] != "https://example.org/second-author" {
if err != nil || author.DataSourceReferences["goodreads"] != "2" ||
author.DataSourceReferences["other"] != "https://example.org/second-author" {
t.Fatalf("expected second author URL stored, got %+v, %v", author, err)
}
first, err := idx.Author("alice-borchardt", "")
if err != nil || first.DataSourceLinks["goodreads"] != "" {
if err != nil || first.DataSourceReferences["goodreads"] != "" {
t.Fatalf("first author must not receive coauthor URL, got %+v, %v", first, err)
}
stale.DataSourceID = "Q2"
stale.DataSourceReferences["wikidata"] = "Q2"
stale.RetrievedOn = time.Now().UTC()
stale.DataSourceLinks["goodreads"] = "999.Stale"
stale.DataSourceReferences["goodreads"] = "999.Stale"
if err := idx.IndexAuthor(stale); err != nil {
t.Fatal(err)
}
if stale.DataSourceLinks["goodreads"] != "999.Stale" {
if stale.DataSourceReferences["goodreads"] != "999.Stale" {
t.Fatal("indexing an author mutated the caller's links")
}
author, err = idx.Author("second-author", "")
if err != nil || author.DataSourceLinks["goodreads"] != "2" ||
author.DataSourceLinks["other"] != "https://example.org/second-author" ||
author.DataSourceID != "Q2" || author.DocumentCount != 1 {
if err != nil || author.DataSourceReferences["goodreads"] != "2" ||
author.DataSourceReferences["other"] != "https://example.org/second-author" ||
author.DataSourceReferences["wikidata"] != "Q2" || author.DocumentCount != 1 {
t.Fatalf("stale author edit lost URL or metadata: %+v, %v", author, err)
}
if _, err := idx.NewFile("second.pdf", []byte("")); err != nil {
t.Fatal(err)
}
author, err = idx.Author("second-author", "")
if err != nil || author.DataSourceLinks["goodreads"] != "2" || author.DocumentCount != 2 {
if err != nil || author.DataSourceReferences["goodreads"] != "2" || author.DocumentCount != 2 {
t.Fatalf("incrementing document count lost author URL: %+v, %v", author, err)
}
}
Expand All @@ -292,7 +292,7 @@ func TestEnrichRejectsInvalidAuthorURL(t *testing.T) {
t.Fatal(err)
}
author, err := idx.Author("alice-borchardt", "")
if err != nil || author.DataSourceLinks["goodreads"] != "" {
if err != nil || author.DataSourceReferences["goodreads"] != "" {
t.Fatalf("invalid author paths must not be stored as author IDs: %+v, %v", author, err)
}
doc, err := idx.DocumentByID(testID)
Expand Down Expand Up @@ -700,11 +700,11 @@ func TestBookMatchesKennedyMiddleName(t *testing.T) {
t.Fatalf("expected persisted book match and rating, got %+v, %v", stored, err)
}
author, err := idx.Author("paul-kennedy", "")
if err != nil || author.DataSourceLinks["goodreads"] != "8390124.Paul_M_Kennedy" || author.Name != "Paul Kennedy" {
if err != nil || author.DataSourceReferences["goodreads"] != "8390124.Paul_M_Kennedy" || author.Name != "Paul Kennedy" {
t.Fatalf("expected author URL with original name preserved, got %+v, %v", author, err)
}
other, err := idx.Author("another-writer", "")
if err != nil || other.DataSourceLinks["goodreads"] != "" {
if err != nil || other.DataSourceReferences["goodreads"] != "" {
t.Fatalf("unmatched coauthor must not receive the URL, got %+v, %v", other, err)
}
if want := []string{title + " Paul Kennedy", title}; !slices.Equal(source.Queries(), want) {
Expand Down Expand Up @@ -1228,7 +1228,7 @@ func TestMatchesTolkienOmnibusSearchResults(t *testing.T) {
t.Fatalf("expected persisted omnibus match, got %+v", stored)
}
author, err := idx.Author("j-r-r-tolkien", "")
if err != nil || author.DataSourceLinks["goodreads"] != "656983.J_R_R_Tolkien" {
if err != nil || author.DataSourceReferences["goodreads"] != "656983.J_R_R_Tolkien" {
t.Fatalf("expected author URL on author record, got %+v, %v", author, err)
}
if want := []string{title + " J. R. R. Tolkien", title}; !slices.Equal(source.Queries(), want) {
Expand Down
4 changes: 2 additions & 2 deletions internal/datasource/biblioreads/bio.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewBioSource(client AuthorClient) *BioSource {
return &BioSource{client: client}
}

func (*BioSource) LinkKey() string { return Source }
func (*BioSource) ReferenceKey() string { return Source }

func (*BioSource) ProfileServiceName() string {
return "Goodreads"
Expand All @@ -47,7 +47,7 @@ func (*BioSource) ProfileInputName() string {
}

func (*BioSource) ProfileValue(author index.Author) string {
if id := author.DataSourceLinks[Source]; id != "" {
if id := author.DataSourceReferences[Source]; id != "" {
return goodreadsBaseURL + goodreadsAuthorPrefix + id
}
return ""
Expand Down
2 changes: 1 addition & 1 deletion internal/datasource/biblioreads/reviews.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (b *ReviewProvider) Name() string {

// AuthorLink returns the indexed author link without making a network request.
func (b *ReviewProvider) AuthorLink(author index.Author, _ string) datasourcemodel.DataSourceLink {
id := author.DataSourceLinks[Source]
id := author.DataSourceReferences[Source]
if !authorIDPattern.MatchString(id) {
return goodreadsDataSourceLink("", goodreadsAuthorPrefix)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/datasource/biblioreads/reviews_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func withBiblioReadsData(t *testing.T, document index.Document, data biblioreads
func TestBiblioReadsDataSourceLinks(t *testing.T) {
provider := biblioreads.NewReviewProvider(nil, nil)
author := provider.AuthorLink(index.Author{
DataSourceLinks: map[string]string{"goodreads": "123.Jane_Doe"},
DataSourceReferences: map[string]string{"goodreads": "123.Jane_Doe"},
}, "en")
if author.URL != "https://www.goodreads.com/author/show/123.Jane_Doe" ||
author.Source != "Goodreads" || author.Icon != "/images/goodreads.svg" || !author.IconInvertible {
Expand All @@ -42,7 +42,7 @@ func TestBiblioReadsDataSourceLinks(t *testing.T) {
{"goodreads": "123.Jane_Doe/"},
{"goodreads": "123?x=y"},
} {
if link := provider.AuthorLink(index.Author{DataSourceLinks: links}, "en"); link.URL != "" {
if link := provider.AuthorLink(index.Author{DataSourceReferences: links}, "en"); link.URL != "" {
t.Errorf("unexpected Goodreads link for %v: %+v", links, link)
}
}
Expand Down
3 changes: 1 addition & 2 deletions internal/datasource/model/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ type Author interface {
DateOfBirth() precisiondate.PrecisionDate
DateOfDeath() precisiondate.PrecisionDate
Image() string
// Links returns the current links owned by this datasource, including language-specific keys.
// Links returns source-owned profile IDs and URLs, including language-specific keys.
Links() map[string]string
// OwnsLink identifies entries replaced by a metadata refresh, even if absent from Links.
OwnsLink(key string) bool
SourceID() string
RetrievedOn() time.Time
Pseudonyms() []string
}
11 changes: 5 additions & 6 deletions internal/datasource/wikidata/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ func (a Author) DateOfDeath() precisiondate.PrecisionDate {
}

func (a Author) Links() map[string]string {
links := make(map[string]string, len(a.wikipediaLink)+1)
links := make(map[string]string, len(a.wikipediaLink)+2)
if a.wikidataEntityId != "" {
links["wikidata"] = a.wikidataEntityId
}
if a.website != "" {
links["website"] = a.website
}
Expand All @@ -96,7 +99,7 @@ func (a Author) Links() map[string]string {
}

func (a Author) OwnsLink(key string) bool {
return key == "website" || strings.HasPrefix(key, "wikipedia:")
return key == "wikidata" || key == "website" || strings.HasPrefix(key, "wikipedia:")
}

func (a Author) Image() string {
Expand All @@ -111,10 +114,6 @@ func (a Author) Gender() float64 {
return a.gender
}

func (a Author) SourceID() string {
return a.wikidataEntityId
}

func (a Author) RetrievedOn() time.Time {
return a.retrievedOn
}
Expand Down
6 changes: 3 additions & 3 deletions internal/datasource/wikidata/bio.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func (*BioSource) ForLanguage(language string) biography.Source {
return &BioSource{language: language}
}

func (s *BioSource) Language() string { return s.language }
func (s *BioSource) LinkKey() string { return "wikipedia:" + s.language }
func (s *BioSource) Language() string { return s.language }
func (s *BioSource) ReferenceKey() string { return "wikipedia:" + s.language }

func (*BioSource) ProfileServiceName() string { return "Wikipedia" }
func (*BioSource) ProfileInputName() string { return "wikipediaURL" }
func (*BioSource) MetadataServiceName() string { return "Wikidata" }

func (s *BioSource) ProfileValue(author index.Author) string {
return author.DataSourceLinks[s.LinkKey()]
return author.DataSourceReferences[s.ReferenceKey()]
}

// NormalizeAuthorID accepts article URLs, retaining their language and encoded title.
Expand Down
6 changes: 3 additions & 3 deletions internal/datasource/wikidata/bio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestBioUsesKnownLinkInCurrentLanguage(t *testing.T) {
idx := authorIndex(t)
author := index.Author{
Slug: "example", Name: "Example",
DataSourceLinks: map[string]string{
DataSourceReferences: map[string]string{
"wikipedia:en": "https://en.wikipedia.org/wiki/Example",
"wikipedia:es": "https://es.wikipedia.org/wiki/Autor_%C3%B1",
},
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestBioUsesKnownLinkInCurrentLanguage(t *testing.T) {
}}, nil
})
provider := biography.NewService(NewBioSource(), nil, idx)
if value := provider.ProfileValueForLanguage(author, "es"); value != author.DataSourceLinks["wikipedia:es"] {
if value := provider.ProfileValueForLanguage(author, "es"); value != author.DataSourceReferences["wikipedia:es"] {
t.Fatalf("profile input must show the current language's link: %q", value)
}
if _, _, pending := provider.StoredBio(author, "fr"); pending {
Expand All @@ -96,7 +96,7 @@ func TestBioUsesKnownLinkInCurrentLanguage(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if stored.Bio.Source != author.DataSourceLinks["wikipedia:es"] ||
if stored.Bio.Source != author.DataSourceReferences["wikipedia:es"] ||
stored.Bio.Language != "es" || stored.Bio.Text["es"] != want {
t.Fatalf("must persist safe emphasis and paragraph breaks with the known language: %+v", stored.Bio)
}
Expand Down
Loading