Files
sm-seo-plugin/extensions.go
Jakub Zych e31ac7c1c6 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
2026-10-27 11:20:59 +01:00

346 lines
11 KiB
Go

package seo
import (
"context"
"fmt"
"strings"
"git.golem15.com/golem15/sm-seo-plugin/classes"
"git.golem15.com/golem15/sm-seo-plugin/controllers"
"git.golem15.com/golem15/sm-seo-plugin/models"
"git.golem15.com/golem15/summercms/modules/pact"
"git.golem15.com/golem15/summercms/modules/signpost"
)
// TabSEO is the key of the SEO tab added to every content type's form.
const TabSEO = "golem15.seo::lang.tab.seo"
// AdminActionExtensions adds the SEO tab (D-01, D-91) to the admin form of
// every content type registered in signpost with a content descriptor that
// names a controller. The content plugins import nothing from this plugin.
func (p *Plugin) AdminActionExtensions() []pact.AdminActionExtension {
reg := p.registry()
if reg == nil {
return nil
}
var out []pact.AdminActionExtension
types := reg.ContentTypes()
for _, typ := range types {
if typ.Content == nil || strings.TrimSpace(typ.Content.Controller) == "" {
continue
}
out = append(out, pact.AdminActionExtension{
Controller: typ.Content.Controller,
Form: p.seoTab(typ.Key, typ.Label),
})
}
if form := p.typeSettingsTabs(types); form != nil {
out = append(out, pact.AdminActionExtension{Controller: controllers.SettingsControllerID, Form: form})
}
out = append(out, pact.AdminActionExtension{Controller: controllers.RulesControllerID, Form: p.ruleHistory()})
return out
}
// ruleHistory is a field-less extension on the plugin's own rules
// controller: its Save sees the save's changes inside the save transaction
// and records them in history (deletes are recorded by the controller).
func (p *Plugin) ruleHistory() *pact.AdminFormExtension {
return &pact.AdminFormExtension{
Permissions: []string{controllers.PermissionManage},
Save: func(ctx context.Context, in pact.AdminFormSave) (pact.AdminFormSaveResult, error) {
svc := p.Service()
if svc == nil {
return pact.AdminFormSaveResult{}, classes.ErrNotBooted
}
before, after := classes.ChangesValues(in.Changes, in.Created)
url := ""
if r, ok := in.Record.(*models.Rule); ok && r != nil {
url = r.URL
}
return pact.AdminFormSaveResult{}, svc.Record(ctx, in.DB, classes.Change{
Kind: models.SubjectRule,
SubjectID: in.ID,
What: svc.Phrase("golem15.seo::lang.history.what_rule", map[string]string{"url": url}),
Before: before,
After: after,
})
},
}
}
func (p *Plugin) registry() *signpost.Registry {
if p == nil || p.app == nil {
return nil
}
reg, _ := p.app.Lookup[*signpost.Registry]()
return reg
}
// seoTabYAML is the SEO tab's fields document (UI-SPEC C3): the tab placed
// after the owner's tabs (no anchor, P-17) and its fields in order. The
// names seo_robots and seo_og_image are anchors Phase 33 places fields
// after; they must not be renamed.
const seoTabYAML = `tabs:
- name: golem15.seo::lang.tab.seo
fields:
seo_status:
type: partial
path: seo_status
span: full
context: update
tab: golem15.seo::lang.tab.seo
seo_snippet:
label: golem15.seo::lang.field.snippet
type: widget
widget: golem15-seo-serp
action: seo_snippet
fill: [seo_title, seo_description, seo_keyphrase]
span: full
context: [create, update]
tab: golem15.seo::lang.tab.seo
seo_title:
label: golem15.seo::lang.field.title
comment: golem15.seo::lang.field.title_comment
type: mlseotext
measure: title
span: full
tab: golem15.seo::lang.tab.seo
seo_description:
label: golem15.seo::lang.field.description
comment: golem15.seo::lang.field.description_comment
type: mlseotext
measure: description
span: full
tab: golem15.seo::lang.tab.seo
seo_keyphrase:
label: golem15.seo::lang.field.keyphrase
comment: golem15.seo::lang.field.keyphrase_comment
type: mltext
span: left
tab: golem15.seo::lang.tab.seo
seo_canonical:
label: golem15.seo::lang.field.canonical
comment: golem15.seo::lang.field.canonical_comment
type: mltext
span: right
tab: golem15.seo::lang.tab.seo
seo_robots:
label: golem15.seo::lang.field.robots
comment: golem15.seo::lang.field.robots_comment
type: mlseorobots
scope: record
span: full
tab: golem15.seo::lang.tab.seo
seo_og_title:
label: golem15.seo::lang.field.og_title
comment: golem15.seo::lang.field.og_title_comment
type: mltext
span: full
tab: golem15.seo::lang.tab.seo
seo_og_description:
label: golem15.seo::lang.field.og_description
type: mltextarea
size: small
span: full
tab: golem15.seo::lang.tab.seo
seo_og_image:
label: golem15.seo::lang.field.og_image
comment: golem15.seo::lang.field.og_image_comment
type: seoimage
span: full
tab: golem15.seo::lang.tab.seo
seo_effective:
label: golem15.seo::lang.field.effective
type: partial
path: seo_effective
span: full
context: update
tab: golem15.seo::lang.tab.seo
`
// seoTab is the form extension of content type key.
func (p *Plugin) seoTab(key, label string) *pact.AdminFormExtension {
return &pact.AdminFormExtension{
FieldsYAML: []byte(seoTabYAML),
Permissions: []string{controllers.PermissionManage},
Visible: func(ctx context.Context) bool {
return p.injected(ctx, key)
},
Dir: "extension",
Actions: []pact.AdminAction{classes.RecordSnippetAction(p.Service, key, "golem15.seo::lang.snippet.action", controllers.PermissionManage)},
PartialData: func(ctx context.Context, name string, record any) (any, error) {
return p.seoTabPartial(ctx, key, name, record)
},
Load: func(ctx context.Context, in pact.AdminFormLoad) (map[string]any, error) {
return p.loadSEOTab(ctx, key, in)
},
Save: func(ctx context.Context, in pact.AdminFormSave) (pact.AdminFormSaveResult, error) {
return p.saveSEOTab(ctx, key, in)
},
}
}
// seoTabPartial serves P1 seo_status and P2 seo_effective of a record.
func (p *Plugin) seoTabPartial(ctx context.Context, key, name string, record any) (any, error) {
svc := p.Service()
if svc == nil {
return nil, classes.ErrNotBooted
}
db, err := svc.DB(ctx)
if err != nil {
return nil, err
}
id, err := classes.RecordKey(db, record)
if err != nil {
if name == "seo_effective" {
return classes.EffectiveView{}, nil
}
return classes.StatusView{}, nil
}
switch name {
case "seo_status":
return svc.RecordStatus(ctx, key, id)
case "seo_effective":
return svc.RecordEffective(ctx, key, id)
}
return nil, nil
}
// contentType returns the registered content type key with a descriptor.
func (p *Plugin) contentType(key string) (pact.MenuItemType, error) {
reg := p.registry()
if reg == nil {
return pact.MenuItemType{}, fmt.Errorf("golem15.seo: menu item type registry is not published")
}
typ, ok := reg.ContentType(key)
if !ok {
return pact.MenuItemType{}, fmt.Errorf("golem15.seo: content type %q is not registered", key)
}
return typ, nil
}
// loadSEOTab returns the stored SEO tab values of a record, every field a
// map of locale to text.
func (p *Plugin) loadSEOTab(ctx context.Context, key string, in pact.AdminFormLoad) (map[string]any, error) {
svc := p.Service()
if svc == nil {
return nil, classes.ErrNotBooted
}
typ, err := p.contentType(key)
if err != nil {
return nil, err
}
entries, err := classes.LoadEntries(in.DB.WithContext(ctx), typ.Content.Model, in.ID)
if err != nil {
return nil, err
}
out := map[string]any{}
for column, perLocale := range classes.RecordValuesOf(entries) {
if column == classes.SharedColumn {
out[classes.FieldPrefix+column] = classes.SharedValue(entries)
continue
}
out[classes.FieldPrefix+column] = perLocale
}
return out, nil
}
// saveSEOTab stores the submitted SEO tab values of a record inside the
// record's save transaction. On an update it first compares the record's
// addresses with its entries' URLs and creates the 301 of a changed
// address (AddressChanged), answered as the redirects.auto_created notice.
func (p *Plugin) saveSEOTab(ctx context.Context, key string, in pact.AdminFormSave) (pact.AdminFormSaveResult, error) {
svc := p.Service()
if svc == nil {
return pact.AdminFormSaveResult{}, classes.ErrNotBooted
}
typ, err := p.contentType(key)
if err != nil {
return pact.AdminFormSaveResult{}, err
}
values := classes.RecordValues{}
refusals := pact.AdminFormErrors{}
for _, column := range classes.EntryColumns {
raw, ok := in.Values[classes.FieldPrefix+column]
if !ok {
continue
}
perLocale := localeMap(raw)
if column == "canonical" {
for _, v := range perLocale {
if v = strings.TrimSpace(v); v != "" && !classes.ValidURLRef(v) {
refusals[classes.FieldPrefix+column] = []string{svc.PhraseCtx(ctx, "lagoon::validation.url", map[string]string{
"attribute": svc.PhraseCtx(ctx, "golem15.seo::lang.field.canonical", nil),
})}
break
}
}
}
values[column] = perLocale
}
if len(refusals) > 0 {
return pact.AdminFormSaveResult{}, refusals
}
// The address diff runs before the values are written, because the
// write refreshes the entries' URLs (D-18, P-20).
var moved []classes.AddressResult
if !in.Created {
changes, err := svc.AddressChanges(ctx, in.DB, typ, in.ID)
if err != nil {
return pact.AdminFormSaveResult{}, err
}
for _, change := range changes {
res, err := svc.AddressChanged(ctx, in.DB, change)
if err != nil {
return pact.AdminFormSaveResult{}, err
}
if res.Redirect != nil {
moved = append(moved, res)
}
}
}
if _, _, err := svc.SaveRecord(ctx, in.DB, classes.RecordWrite{Type: typ, ID: in.ID, Values: values}); err != nil {
return pact.AdminFormSaveResult{}, err
}
return pact.AdminFormSaveResult{Notices: classes.AddressNotices(typ, in.Changes, moved, "seo_status")}, nil
}
// localeMap reads a submitted value as locale -> text: a translatable
// field's map, or a plain string under the empty locale key.
func localeMap(raw any) map[string]string {
switch v := raw.(type) {
case map[string]string:
return v
case map[string]any:
out := make(map[string]string, len(v))
for locale, value := range v {
if s, ok := value.(string); ok {
out[locale] = s
}
}
return out
case string:
return map[string]string{"": v}
}
return map[string]string{}
}
// injected reports whether the SEO tab of content type key is shown: false
// only when its settings row turns it off. It answers true when it cannot
// decide, so a database error never hides stored values.
func (p *Plugin) injected(ctx context.Context, key string) bool {
svc := p.Service()
if svc == nil {
return true
}
db, err := svc.DB(ctx)
if err != nil {
return true
}
row, err := classes.LoadTypeSetting(db, key)
if err != nil {
return true
}
return row.Inject
}