fix: list an additional address once when a content record has it
- an additional address whose absolute URL a listed record already has is left out with the new reason duplicate (models.ReasonDuplicate) - the settings screen counts it under Already listed by a content type - a record left out by the filter or the type switch does not count
This commit is contained in:
@@ -177,7 +177,8 @@ func Rebuild(ctx context.Context, app *backpack.App) (RebuildResult, error) {
|
||||
// first lastmod of a new row. The hreflang alternates of a record are its
|
||||
// listed addresses (D-55 to D-57). Each address keeps its page's images
|
||||
// (PageImages, on site), the type's kind, the record's first publication
|
||||
// and its parents.
|
||||
// and its parents. An additional address a listed record already has is
|
||||
// left out as models.ReasonDuplicate (markListedExtras).
|
||||
func collect(ctx context.Context, reg *signpost.Registry, filter contract.Filter, settings models.Settings, types map[string]models.TypeSetting, site string) (collected, error) {
|
||||
out := collected{unpublished: map[slotKey]bool{}}
|
||||
seen := map[slotKey]bool{}
|
||||
@@ -255,8 +256,19 @@ func collect(ctx context.Context, reg *signpost.Registry, filter contract.Filter
|
||||
}
|
||||
}
|
||||
}
|
||||
extras := extraRows(settings.Extra())
|
||||
markListedExtras(out.rows, extras, site)
|
||||
if err := batch(extras); err != nil {
|
||||
return out, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// extraRows turns the settings' additional addresses into rows; an address
|
||||
// that is not allowed is skipped.
|
||||
func extraRows(list []models.ExtraURL) []wanted {
|
||||
var extras []wanted
|
||||
for _, e := range settings.Extra() {
|
||||
for _, e := range list {
|
||||
u := strings.TrimSpace(e.URL)
|
||||
if !models.AllowedAddress(u) {
|
||||
continue
|
||||
@@ -271,10 +283,39 @@ func collect(ctx context.Context, reg *signpost.Registry, filter contract.Filter
|
||||
firstMod: mod,
|
||||
})
|
||||
}
|
||||
if err := batch(extras); err != nil {
|
||||
return out, err
|
||||
return extras
|
||||
}
|
||||
|
||||
// markListedExtras leaves out (models.ReasonDuplicate) every additional
|
||||
// address whose absolute URL a content record already lists, so an address
|
||||
// both in a content type and in the additional addresses (a definition
|
||||
// migrated from the old sitemap, for example) is in the files once, with
|
||||
// the record's lastmod and alternates. A record left out by the filter or
|
||||
// the type switch does not count: the editor's additional address then
|
||||
// stays listed. The rows keep their place in the slot table, so the
|
||||
// settings screen counts them by reason.
|
||||
func markListedExtras(records, extras []wanted, site string) {
|
||||
listed := map[string]bool{}
|
||||
for _, w := range records {
|
||||
if w.reason == "" && !w.extra {
|
||||
listed[addressKey(site, w.url)] = true
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
for i := range extras {
|
||||
if extras[i].reason == "" && listed[addressKey(site, extras[i].url)] {
|
||||
extras[i].reason = models.ReasonDuplicate
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// addressKey is the absolute form of an address for comparison, or the
|
||||
// address itself when it has none (a path without a site URL).
|
||||
func addressKey(site, u string) string {
|
||||
u = strings.TrimSpace(u)
|
||||
if a := Absolute(site, u); a != "" {
|
||||
return a
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
// decide asks filter about the rows not already left out (a type switched
|
||||
|
||||
51
classes/rebuild_test.go
Normal file
51
classes/rebuild_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package classes
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/sm-sitemap-plugin/contract"
|
||||
"git.golem15.com/golem15/sm-sitemap-plugin/models"
|
||||
)
|
||||
|
||||
// TestMarkListedExtras pins the de-duplication of the additional
|
||||
// addresses: an address a listed record has (as a path or as the same
|
||||
// absolute URL) is left out as duplicate; a record left out by the filter
|
||||
// or the type switch, another address and a record's other locale do not
|
||||
// count; an address already left out keeps its reason.
|
||||
func TestMarkListedExtras(t *testing.T) {
|
||||
const site = "https://example.test"
|
||||
records := []wanted{
|
||||
{key: slotKey{"page", "1", "en"}, url: "/"},
|
||||
{key: slotKey{"page", "2", "en"}, url: "/blog"},
|
||||
{key: slotKey{"page", "3", "en"}, url: "/faq", reason: contract.ReasonNoindex},
|
||||
{key: slotKey{"page", "4", "en"}, url: "/off", reason: contract.ReasonType},
|
||||
{key: slotKey{"page", "5", "pl"}, url: "/pl/o-nas"},
|
||||
}
|
||||
extras := extraRows([]models.ExtraURL{
|
||||
{URL: "/"},
|
||||
{URL: site + "/blog"},
|
||||
{URL: "/faq"},
|
||||
{URL: "/off"},
|
||||
{URL: "/kontakt"},
|
||||
{URL: "/o-nas"},
|
||||
{URL: "javascript:alert(1)"},
|
||||
})
|
||||
if len(extras) != 6 {
|
||||
t.Fatalf("extraRows kept %d addresses, want 6 (the javascript: one is not allowed)", len(extras))
|
||||
}
|
||||
extras[5].reason = contract.ReasonNoindex
|
||||
markListedExtras(records, extras, site)
|
||||
want := []string{models.ReasonDuplicate, models.ReasonDuplicate, "", "", "", contract.ReasonNoindex}
|
||||
for i, w := range extras {
|
||||
if w.reason != want[i] {
|
||||
t.Errorf("extra %s: reason %q, want %q", w.url, w.reason, want[i])
|
||||
}
|
||||
}
|
||||
|
||||
// Without a site URL the paths are compared as they are.
|
||||
extras = extraRows([]models.ExtraURL{{URL: "/blog"}, {URL: site + "/blog"}})
|
||||
markListedExtras(records, extras, "")
|
||||
if extras[0].reason != models.ReasonDuplicate || extras[1].reason != "" {
|
||||
t.Fatalf("without a site URL: %q %q, want duplicate and listed", extras[0].reason, extras[1].reason)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user