Files
sm-seo-plugin/models/autolink.go
Jakub Zych 6203aec2bc feat: auto-link phrases in the resolve answer (tracer)
- golem15_seo_autolinks (migration 202610280007) and the settings column
  autolink_max (Links tab, 0 to 20, default 3)
- classes/autolinks.go: checked save and delete with history, ForPage
- ok resolve answers carry autolinks for the page's locale without
  disabled phrases and links to the page itself, and autolink_max
2026-11-04 16:22:23 +01:00

114 lines
3.4 KiB
Go

package models
import (
"strconv"
"time"
"gorm.io/gorm"
)
// The origins of an auto-link phrase: entered in the admin, added from a
// record's focus keyphrase, or created through MCP.
const (
AutolinkOriginManual = "manual"
AutolinkOriginKeyphrase = "keyphrase"
AutolinkOriginMCP = "mcp"
)
// AutolinkOrigins lists the origins in filter order.
var AutolinkOrigins = []string{AutolinkOriginManual, AutolinkOriginKeyphrase, AutolinkOriginMCP}
// The limits of an auto-link phrase and of the settings' autolink_max
// (UI-SPEC C20, C22).
const (
MinAutolinkPhrase = 3
MaxAutolinkPhrase = 200
MaxAutolinkURL = 2048
DefaultAutolinkMax = 3
MinAutolinkMax = 0
MaxAutolinkMax = 20
)
// Autolink is one phrase of the auto-link table on golem15_seo_autolinks
// (D-82, P33-45): texts link the phrase's first occurrence to URL (a path
// or an absolute http(s) address). Locale is the content locale it applies
// in, "" for every locale; a disabled phrase is kept but never linked.
type Autolink struct {
ID uint `gorm:"column:id;primaryKey"`
Phrase string `gorm:"column:phrase"`
URL string `gorm:"column:url"`
Locale string `gorm:"column:locale"`
Enabled bool `gorm:"column:enabled"`
Origin string `gorm:"column:origin"`
Note string `gorm:"column:note"`
CreatedAt *time.Time `gorm:"column:created_at"`
UpdatedAt *time.Time `gorm:"column:updated_at"`
}
func (Autolink) TableName() string { return "golem15_seo_autolinks" }
// MorphName names an auto-link in history and the admin.
func (Autolink) MorphName() string { return `Golem15\SEO\Models\Autolink` }
// Fillable lists the form's columns.
func (*Autolink) Fillable() []string {
return []string{"phrase", "url", "locale", "enabled", "note"}
}
// Rules is empty: the Auto-links controller and the service check a
// phrase before a save (classes.AutolinkProblems).
func (*Autolink) Rules() map[string]string { return map[string]string{} }
// NewAutolink is a phrase with the form's defaults: every locale, active,
// entered by hand.
func NewAutolink() Autolink {
return Autolink{Enabled: true, Origin: AutolinkOriginManual}
}
// IDText is the auto-link's id as text.
func (a Autolink) IDText() string { return strconv.FormatUint(uint64(a.ID), 10) }
// The filter scopes of the Auto-links list (UI-SPEC C22).
const (
FilterAutolinkLocale = "FilterAutolinkLocale"
FilterAutolinkOrigin = "FilterAutolinkOrigin"
// AutolinkLocaleAll is the locale filter's value of the phrases that
// apply in every locale.
AutolinkLocaleAll = "all"
)
// FilterScopes lists the Auto-links list's filter scopes.
func (Autolink) FilterScopes() []string {
return []string{FilterAutolinkLocale, FilterAutolinkOrigin}
}
// FilterScope narrows the Auto-links list by locale ("all" for the
// phrases of every locale) or origin; an unknown origin matches nothing.
// The active filter is a switch on the enabled column.
func (Autolink) FilterScope(name string, db *gorm.DB, value any) *gorm.DB {
if db == nil {
return db
}
switch name {
case FilterAutolinkLocale:
s, _ := value.(string)
if s == AutolinkLocaleAll {
s = ""
}
return db.Where("golem15_seo_autolinks.locale = ?", s)
case FilterAutolinkOrigin:
s, _ := value.(string)
for _, o := range AutolinkOrigins {
if o == s {
return db.Where("golem15_seo_autolinks.origin = ?", s)
}
}
return db.Where("1 = 0")
}
return db
}
func init() {
Register(Autolink{})
}