Files
sm-seo-plugin/classes/redirects.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

59 lines
1.5 KiB
Go

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
}