feat(17-03): full sitemap generation, settings, hooks and import commands
- generator in Definition::addItemToSet order: 50000 URL cap, lastmod from the record or the updates manifest (regular file, 5 MiB cap, fail safe), BeforeAddItem (skip) and AddItem (extra XML) festival events, tier or item priority, per-alternate expansion of list type children - priority tiers from golem15.sitemap.priority_tiers, compiled at boot, defaults equal to the PHP getPriorityForUrl rules - frontend_url rewrite of loc, xhtml:link href and the stylesheet href; GET /sitemap.xsl serves the PHP stylesheet byte for byte - golem15_sitemap_settings singleton and Settings model - sitemap:import (PHP ImportCommand) and sitemap:import-winter with the read-only Winter export SQL - urlto and full goldens generated from PHP; TestSitemapImportSmoke
This commit is contained in:
@@ -60,7 +60,7 @@ func (d *DefinitionItem) UnmarshalJSON(raw []byte) error {
|
||||
}
|
||||
item := DefinitionItem{Type: ItemTypeURL}
|
||||
if v, ok := fields["type"]; ok {
|
||||
s, err := decodeText(v)
|
||||
s, err := DecodeText(v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sitemap definition item type: %w", err)
|
||||
}
|
||||
@@ -77,7 +77,7 @@ func (d *DefinitionItem) UnmarshalJSON(raw []byte) error {
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
s, err := decodeText(v)
|
||||
s, err := DecodeText(v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sitemap definition item %s: %w", key, err)
|
||||
}
|
||||
@@ -185,9 +185,9 @@ func (l DefinitionItems) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal([]DefinitionItem(l))
|
||||
}
|
||||
|
||||
// decodeText decodes a stored text value: a string as it is, a number in
|
||||
// DecodeText decodes a stored text value: a string as it is, a number in
|
||||
// its PHP string form, a boolean as PHP casts it ("1" or ""), null as nil.
|
||||
func decodeText(raw json.RawMessage) (*string, error) {
|
||||
func DecodeText(raw json.RawMessage) (*string, error) {
|
||||
var v any
|
||||
dec := json.NewDecoder(bytes.NewReader(raw))
|
||||
dec.UseNumber()
|
||||
|
||||
87
models/settings.go
Normal file
87
models/settings.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package models
|
||||
|
||||
// The settings keys of the five priority tiers, in the order PHP checked
|
||||
// them. A tier rule names one of them.
|
||||
const (
|
||||
PriorityHomepage = "priority_homepage"
|
||||
PriorityServices = "priority_services"
|
||||
PriorityBlog = "priority_blog"
|
||||
PriorityCategories = "priority_categories"
|
||||
PriorityLegal = "priority_legal"
|
||||
)
|
||||
|
||||
// PriorityDefaults are the PHP defaults of the priority settings.
|
||||
var PriorityDefaults = map[string]string{
|
||||
PriorityHomepage: "1.0",
|
||||
PriorityServices: "0.8",
|
||||
PriorityBlog: "0.6",
|
||||
PriorityCategories: "0.4",
|
||||
PriorityLegal: "0.2",
|
||||
}
|
||||
|
||||
// Settings is the sitemap settings singleton (row id 1) on
|
||||
// golem15_sitemap_settings, the PHP Golem15\Sitemap\Models\Settings that
|
||||
// WinterCMS kept in system_settings under golem15_sitemap_settings.
|
||||
// FrontendURL replaces the backend URL in the generated XML;
|
||||
// FrontendDefaultLocale is the locale whose URLs carry no prefix;
|
||||
// UpdatesManifestPath names a JSON file of URL path to last-modified date;
|
||||
// EnablePriorityTiers turns the tier priorities on, and the priority fields
|
||||
// hold each tier's value as text. A null priority reads as its PHP default.
|
||||
type Settings struct {
|
||||
ID uint `gorm:"column:id;primaryKey"`
|
||||
FrontendURL *string `gorm:"column:frontend_url"`
|
||||
FrontendDefaultLocale *string `gorm:"column:frontend_default_locale"`
|
||||
UpdatesManifestPath *string `gorm:"column:updates_manifest_path"`
|
||||
EnablePriorityTiers bool `gorm:"column:enable_priority_tiers"`
|
||||
PriorityHomepage *string `gorm:"column:priority_homepage"`
|
||||
PriorityServices *string `gorm:"column:priority_services"`
|
||||
PriorityBlog *string `gorm:"column:priority_blog"`
|
||||
PriorityCategories *string `gorm:"column:priority_categories"`
|
||||
PriorityLegal *string `gorm:"column:priority_legal"`
|
||||
}
|
||||
|
||||
func (Settings) TableName() string { return "golem15_sitemap_settings" }
|
||||
|
||||
// Fillable lists only the settings columns.
|
||||
func (Settings) Fillable() []string {
|
||||
return []string{
|
||||
"frontend_url", "frontend_default_locale", "updates_manifest_path",
|
||||
"enable_priority_tiers", PriorityHomepage, PriorityServices,
|
||||
PriorityBlog, PriorityCategories, PriorityLegal,
|
||||
}
|
||||
}
|
||||
|
||||
// Rules is empty: the PHP settings form validated nothing.
|
||||
func (Settings) Rules() map[string]string { return map[string]string{} }
|
||||
|
||||
// DefaultSettings is the settings PHP read when nothing was saved: tiers on
|
||||
// with the default priorities.
|
||||
func DefaultSettings() Settings {
|
||||
return Settings{ID: 1, EnablePriorityTiers: true}
|
||||
}
|
||||
|
||||
// Priority returns the value of a priority setting: the stored text, or the
|
||||
// PHP default when it is null.
|
||||
func (s Settings) Priority(key string) string {
|
||||
var p *string
|
||||
switch key {
|
||||
case PriorityHomepage:
|
||||
p = s.PriorityHomepage
|
||||
case PriorityServices:
|
||||
p = s.PriorityServices
|
||||
case PriorityBlog:
|
||||
p = s.PriorityBlog
|
||||
case PriorityCategories:
|
||||
p = s.PriorityCategories
|
||||
case PriorityLegal:
|
||||
p = s.PriorityLegal
|
||||
}
|
||||
if p == nil {
|
||||
return PriorityDefaults[key]
|
||||
}
|
||||
return *p
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(Settings{})
|
||||
}
|
||||
Reference in New Issue
Block a user