feat: address-change 301 (tracer)
- golem15_seo_redirects table and Redirect model - AddressChanged: old -> new 301 in the save transaction, earlier redirects pointed at the new address, self-loops dropped - the SEO tab save compares descriptor URLs with the entry URLs and answers the redirects.auto_created notice - resolve answers redirect with the absolute target for an exact source
This commit is contained in:
226
classes/addresses.go
Normal file
226
classes/addresses.go
Normal file
@@ -0,0 +1,226 @@
|
||||
package classes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/sm-seo-plugin/models"
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// AddressChange is one record's public address change in one locale: the
|
||||
// URL its SEO entry last saw (Old) and the URL its content descriptor
|
||||
// answers now (New).
|
||||
type AddressChange struct {
|
||||
TypeKey string
|
||||
ModelType string
|
||||
RecordID string
|
||||
Locale string
|
||||
Old string
|
||||
New string
|
||||
}
|
||||
|
||||
// AddressResult is what AddressChanged did: the redirect from the old to
|
||||
// the new address, and how many earlier redirects now point straight to
|
||||
// the new address.
|
||||
type AddressResult struct {
|
||||
Locale string
|
||||
Old string
|
||||
New string
|
||||
Redirect *models.Redirect
|
||||
Repointed int
|
||||
}
|
||||
|
||||
// AddressChanged applies a record's address change inside tx, the
|
||||
// caller's transaction (D-18, P-20):
|
||||
// 1. exact redirects whose source is the new address are deleted, so the
|
||||
// record is never shadowed (this also undoes an earlier change back);
|
||||
// 2. old → new is created as a 301 (origin slug), or the existing exact
|
||||
// rule of the old address is pointed at the new one;
|
||||
// 3. redirects whose target is the old address are pointed at the new
|
||||
// one, so no chain forms (A→B, then B→C makes A→C);
|
||||
// 4. redirects that now point to themselves are deleted;
|
||||
// 5. the entry's URL becomes the new address.
|
||||
//
|
||||
// Every redirect write is recorded in history with via auto. Nothing
|
||||
// happens when either address is empty or they are equal.
|
||||
func (s *Service) AddressChanged(ctx context.Context, tx *gorm.DB, change AddressChange) (AddressResult, error) {
|
||||
res := AddressResult{Locale: change.Locale, Old: strings.TrimSpace(change.Old), New: strings.TrimSpace(change.New)}
|
||||
if res.Old == "" || res.New == "" || res.Old == res.New {
|
||||
return res, nil
|
||||
}
|
||||
ctx = WithVia(ctx, models.ViaAuto)
|
||||
now := time.Now().UTC()
|
||||
|
||||
var shadowing []models.Redirect
|
||||
if err := tx.Where("match = ? AND source = ?", models.RedirectExact, res.New).Find(&shadowing).Error; err != nil {
|
||||
return res, err
|
||||
}
|
||||
for _, r := range shadowing {
|
||||
if err := s.deleteRedirect(ctx, tx, r); err != nil {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
|
||||
var existing []models.Redirect
|
||||
if err := tx.Where("match = ? AND source = ?", models.RedirectExact, res.Old).Limit(1).Find(&existing).Error; err != nil {
|
||||
return res, err
|
||||
}
|
||||
var redirect models.Redirect
|
||||
if len(existing) > 0 {
|
||||
redirect = existing[0]
|
||||
before := RedirectValues(redirect)
|
||||
redirect.Target, redirect.Status, redirect.UpdatedAt = res.New, models.Status301, &now
|
||||
if err := tx.Save(&redirect).Error; err != nil {
|
||||
return res, err
|
||||
}
|
||||
if err := s.recordRedirect(ctx, tx, redirect, before, RedirectValues(redirect)); err != nil {
|
||||
return res, err
|
||||
}
|
||||
} else {
|
||||
redirect = models.NewRedirect()
|
||||
redirect.Source, redirect.Target, redirect.Origin = res.Old, res.New, models.OriginSlug
|
||||
redirect.CreatedAt, redirect.UpdatedAt = &now, &now
|
||||
if err := tx.Create(&redirect).Error; err != nil {
|
||||
return res, err
|
||||
}
|
||||
if err := s.recordRedirect(ctx, tx, redirect, nil, RedirectValues(redirect)); err != nil {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
res.Redirect = &redirect
|
||||
|
||||
var pointing []models.Redirect
|
||||
targets := []string{res.Old}
|
||||
if site := s.SiteURL(); site != "" {
|
||||
targets = append(targets, site+res.Old)
|
||||
}
|
||||
if err := tx.Where("target IN ? AND id <> ?", targets, redirect.ID).Find(&pointing).Error; err != nil {
|
||||
return res, err
|
||||
}
|
||||
for _, r := range pointing {
|
||||
before := RedirectValues(r)
|
||||
r.Target, r.UpdatedAt = res.New, &now
|
||||
if r.Match == models.RedirectExact && s.samePath(r.Target, r.Source) {
|
||||
if err := s.deleteRedirect(ctx, tx, r); err != nil {
|
||||
return res, err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err := tx.Save(&r).Error; err != nil {
|
||||
return res, err
|
||||
}
|
||||
if err := s.recordRedirect(ctx, tx, r, before, RedirectValues(r)); err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Repointed++
|
||||
}
|
||||
|
||||
if change.ModelType != "" && change.RecordID != "" {
|
||||
if err := tx.Model(&models.Entry{}).
|
||||
Where("model_type = ? AND record_id = ? AND locale = ?", change.ModelType, change.RecordID, change.Locale).
|
||||
Updates(map[string]any{"url": res.New, "updated_at": now}).Error; err != nil {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// deleteRedirect deletes r inside tx and records it in history.
|
||||
func (s *Service) deleteRedirect(ctx context.Context, tx *gorm.DB, r models.Redirect) error {
|
||||
if err := tx.Delete(&models.Redirect{}, r.ID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return s.recordRedirect(ctx, tx, r, RedirectValues(r), nil)
|
||||
}
|
||||
|
||||
// recordRedirect writes the history row of one redirect write.
|
||||
func (s *Service) recordRedirect(ctx context.Context, tx *gorm.DB, r models.Redirect, before, after map[string]any) error {
|
||||
return s.Record(ctx, tx, Change{
|
||||
Kind: models.SubjectRedirect,
|
||||
SubjectID: strconv.FormatUint(uint64(r.ID), 10),
|
||||
What: s.redirectWhat(r),
|
||||
Before: before,
|
||||
After: after,
|
||||
})
|
||||
}
|
||||
|
||||
// AddressChanges compares the addresses a record's content descriptor
|
||||
// answers now with the URLs its SEO entries last saw, inside tx (D-18),
|
||||
// and returns the changes that need a redirect: the changed locales of a
|
||||
// published record, each to be applied with AddressChanged. A changed
|
||||
// locale of an unpublished record only takes its new URL, without a
|
||||
// redirect, and a locale without an entry gets one with its URL, so the
|
||||
// next change can be detected.
|
||||
func (s *Service) AddressChanges(ctx context.Context, tx *gorm.DB, typ pact.MenuItemType, id string) ([]AddressChange, error) {
|
||||
reg := s.Registry()
|
||||
if reg == nil || typ.Content == nil {
|
||||
return nil, nil
|
||||
}
|
||||
rec, err := reg.Describe(ctx, typ.Key, id, s.UnprefixedLocale())
|
||||
if err != nil || rec == nil {
|
||||
return nil, err
|
||||
}
|
||||
model := typ.Content.Model
|
||||
var out []AddressChange
|
||||
for _, l := range rec.Locales {
|
||||
url := strings.TrimSpace(l.URL)
|
||||
if url == "" {
|
||||
continue
|
||||
}
|
||||
entry, found, err := LoadEntry(tx, model, id, l.Locale)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch {
|
||||
case !found:
|
||||
row := models.Entry{ModelType: model, RecordID: id, Locale: l.Locale, TypeKey: typ.Key, URL: url}
|
||||
if err := tx.Create(&row).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case entry.URL == url:
|
||||
case entry.URL == "" || !rec.Published:
|
||||
if err := tx.Model(&models.Entry{}).Where("id = ?", entry.ID).Updates(map[string]any{"url": url, "type_key": typ.Key}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
out = append(out, AddressChange{
|
||||
TypeKey: typ.Key, ModelType: model, RecordID: id, Locale: l.Locale,
|
||||
Old: entry.URL, New: url,
|
||||
})
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// AddressNotices are the save notices of address changes
|
||||
// (redirects.auto_created, one per changed locale), on the owner field
|
||||
// named after a placeholder of the type's pattern that the save changed
|
||||
// (for example slug), else on fallback.
|
||||
func AddressNotices(typ pact.MenuItemType, changes []pact.RecordChange, results []AddressResult, fallback string) map[string][]pact.FormFieldNotice {
|
||||
if len(results) == 0 {
|
||||
return nil
|
||||
}
|
||||
field := fallback
|
||||
for _, c := range changes {
|
||||
for _, p := range typ.Placeholders {
|
||||
if strings.TrimPrefix(p, ":") == c.Attribute {
|
||||
field = c.Attribute
|
||||
}
|
||||
}
|
||||
if field != fallback {
|
||||
break
|
||||
}
|
||||
}
|
||||
out := map[string][]pact.FormFieldNotice{}
|
||||
for _, r := range results {
|
||||
out[field] = append(out[field], pact.FormFieldNotice{
|
||||
Message: "golem15.seo::lang.redirects.auto_created",
|
||||
Params: map[string]string{"old": r.Old, "new": r.New, "count": strconv.Itoa(r.Repointed)},
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
58
classes/redirects.go
Normal file
58
classes/redirects.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package classes
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.golem15.com/golem15/sm-seo-plugin/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// The resolve actions of the redirect stage.
|
||||
const (
|
||||
ActionRedirect = "redirect"
|
||||
ActionGone = "gone"
|
||||
)
|
||||
|
||||
// exactRedirect returns the exact redirect whose source is path, nil when
|
||||
// there is none.
|
||||
func exactRedirect(db *gorm.DB, path string) (*models.Redirect, error) {
|
||||
var rows []models.Redirect
|
||||
if err := db.Where("match = ? AND source = ?", models.RedirectExact, path).Limit(1).Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return &rows[0], nil
|
||||
}
|
||||
|
||||
// RedirectValues are a redirect's values for history.
|
||||
func RedirectValues(r models.Redirect) map[string]any {
|
||||
return map[string]any{
|
||||
"source": r.Source,
|
||||
"match": r.Match,
|
||||
"target": r.Target,
|
||||
"status": r.Status,
|
||||
"query": r.Query,
|
||||
"ignore_case": r.IgnoreCase,
|
||||
"trailing_slash": r.TrailingSlash,
|
||||
"origin": r.Origin,
|
||||
"note": r.Note,
|
||||
}
|
||||
}
|
||||
|
||||
// redirectWhat is the history description of a redirect.
|
||||
func (s *Service) redirectWhat(r models.Redirect) string {
|
||||
return s.Phrase("golem15.seo::lang.history.what_redirect", map[string]string{"url": r.Source})
|
||||
}
|
||||
|
||||
// samePath reports whether target names path on this site: the path
|
||||
// itself, or an absolute URL on the site URL with that path.
|
||||
func (s *Service) samePath(target, path string) bool {
|
||||
target = strings.TrimSpace(target)
|
||||
if target == path {
|
||||
return true
|
||||
}
|
||||
site := s.SiteURL()
|
||||
return site != "" && target == site+path
|
||||
}
|
||||
@@ -46,19 +46,22 @@ const (
|
||||
SourceDefault = "default"
|
||||
)
|
||||
|
||||
// Result is the effective SEO of one URL. Action is ok or not_found; Status
|
||||
// is the HTTP status the frontend should answer with. URL is the absolute
|
||||
// URL of the page (path and kept query on the site URL). Sources maps each
|
||||
// value name (title, description, canonical, robots, og_title,
|
||||
// og_description, og_image) to where it came from.
|
||||
// Result is the effective SEO of one URL. Action is ok, not_found,
|
||||
// redirect or gone; Status is the HTTP status the frontend should answer
|
||||
// with. URL is the absolute URL of the page (path and kept query on the
|
||||
// site URL). Sources maps each value name (title, description, canonical,
|
||||
// robots, og_title, og_description, og_image) to where it came from.
|
||||
type Result struct {
|
||||
Action string `json:"action"`
|
||||
Status int `json:"status"`
|
||||
URL string `json:"url"`
|
||||
Locale string `json:"locale"`
|
||||
Staging bool `json:"staging"`
|
||||
SEO *SEO `json:"seo"`
|
||||
Sources map[string]string `json:"-"`
|
||||
Action string `json:"action"`
|
||||
Status int `json:"status"`
|
||||
// Location is the absolute address a redirect answer sends the visitor
|
||||
// to; absent on every other action.
|
||||
Location string `json:"location,omitempty"`
|
||||
URL string `json:"url"`
|
||||
Locale string `json:"locale"`
|
||||
Staging bool `json:"staging"`
|
||||
SEO *SEO `json:"seo"`
|
||||
Sources map[string]string `json:"-"`
|
||||
// TypeKey and RecordID name the record behind the URL, "" when none.
|
||||
TypeKey string `json:"-"`
|
||||
RecordID string `json:"-"`
|
||||
@@ -357,6 +360,13 @@ func (r *Resolver) Resolve(ctx context.Context, rawURL string) (Result, error) {
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
redirect, err := exactRedirect(db, path)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
if redirect != nil {
|
||||
return r.redirectAnswer(svc.SiteURL()+path, *redirect), nil
|
||||
}
|
||||
key, match, rec, err := r.lookup(ctx, path)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
@@ -557,6 +567,19 @@ func (r *Resolver) resolveAt(ctx context.Context, db *gorm.DB, settings models.S
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// redirectAnswer is the answer of a matched redirect: gone for a 410,
|
||||
// else redirect with the rule's status and its target absolute on the
|
||||
// site URL.
|
||||
func (r *Resolver) redirectAnswer(self string, rule models.Redirect) Result {
|
||||
res := Result{URL: self, Staging: r.Service.Staging(), Sources: map[string]string{}}
|
||||
if rule.Status == models.Status410 {
|
||||
res.Action, res.Status = ActionGone, models.Status410
|
||||
return res
|
||||
}
|
||||
res.Action, res.Status, res.Location = ActionRedirect, rule.Status, AbsoluteURL(r.Service.SiteURL(), rule.Target)
|
||||
return res
|
||||
}
|
||||
|
||||
// rawQueryOf returns the raw query of the resolver input.
|
||||
func rawQueryOf(raw string) string {
|
||||
u, err := url.Parse(strings.TrimSpace(raw))
|
||||
|
||||
Reference in New Issue
Block a user