feat!: remove the PHP-parity sitemap, tiers, definitions screen and XSL

BREAKING CHANGE: /sitemap.xsl is gone (404), the definitions screen and
the Phase 17 settings tile are replaced by the settings singleton, and
priority, change frequency, priority tiers and the update manifest are
no longer written or read. The config keys golem15.sitemap.cache_seconds
and golem15.sitemap.priority_tiers are removed; site_url, lastmod_ignore
and rebuild_delay_seconds are new.

- sitemap:import and sitemap:import-winter write the additional addresses
  (and site_url, default_locale) instead of the definition
- the golem15_sitemap_definitions table stays in the database, unread
This commit is contained in:
Jakub Zych
2026-11-04 11:12:51 +01:00
parent 3ba70bb69f
commit 84cb27a810
42 changed files with 582 additions and 5371 deletions

View File

@@ -1,55 +1,55 @@
package console
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"git.golem15.com/golem15/sm-sitemap-plugin/classes"
"git.golem15.com/golem15/sm-sitemap-plugin/models"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bonfire"
"gorm.io/gorm"
)
// Import is sitemap:import FILE [--theme=] [--append], the PHP
// ImportCommand: it reads a JSON array of URL entries ({"url": ...,
// "changefreq": ..., "priority": ...}; changefreq defaults to monthly,
// priority to 0.5) into the sitemap definition. Without --append the
// definition keeps its non-url items first and the imported URLs replace
// its url items; with --append the URLs are added after every existing item.
// The definition is always the one /sitemap.xml serves (theme default);
// --theme is accepted for PHP compatibility and a theme other than default
// is reported. An entry whose url is not a path or an http(s) URL
// (classes.AllowedItemURL) is refused, as the admin form refuses it.
// Import is sitemap:import FILE [--append]: it reads a JSON array of URL
// entries ({"url": "/path", "lastmod": "2026-10-28"}; lastmod optional) into
// the settings' additional addresses, in one transaction. Without --append
// the imported addresses replace the additional addresses; with --append
// they are added after them, an address already listed is skipped. An
// entry whose url is not a path or an http(s) URL (models.AllowedAddress),
// or whose lastmod is not a date, stops the import before anything is
// written. The Phase 17 keys changefreq and priority are ignored, and
// --theme is accepted for the PHP command's sake. The files follow at the
// next rebuild (sitemap:rebuild).
func Import(app *backpack.App) bonfire.Command {
return bonfire.Command{
Name: "sitemap:import",
Description: "Import sitemap URLs from a JSON manifest file",
Long: `Reads a JSON array of URL entries ({"url": "/path", "changefreq":
"monthly", "priority": "0.5"}; changefreq and priority are optional) and puts
them into the sitemap definition that /sitemap.xml serves, in one
transaction. Every entry needs a url that is a path or an http(s) URL; one
bad entry stops the import before anything is written.
Description: "Import additional sitemap addresses from a JSON manifest file",
Long: `Reads a JSON array of URL entries ({"url": "/path", "lastmod":
"2026-10-28"}; lastmod is optional) and puts them into the sitemap's
additional addresses (Settings > Sitemaps > Additional addresses), in one
transaction. Every entry needs a url that is a path or an http(s) URL; one bad
entry stops the import before anything is written. The old changefreq and
priority keys are ignored: the sitemaps no longer write them.
By default the import replaces the definition's url items and keeps its
dynamic items (pages, posts and other generated types). --append adds the
URLs and keeps every existing item. --theme is accepted for the PHP command's
sake: only the default definition is served, so the URLs always go there.
It ends with "Imported N URLs into sitemap definition for theme 'default'."`,
By default the import replaces the additional addresses. --append adds the
addresses after them and skips those already listed. --theme is accepted for
the PHP command's sake and does nothing. The files change at the next rebuild
(sitemap:rebuild, a settings save or the nightly run). It ends with
"Imported N addresses into the sitemap's additional addresses."`,
Args: []bonfire.Arg{
{Name: "file", Description: "Path to the JSON manifest (relative to the working directory or absolute)", Required: true},
},
Flags: []bonfire.Flag{
{Name: "theme", Description: "Theme code of the PHP plugin; /sitemap.xml serves only the default definition"},
{Name: "append", Description: "Add URLs without removing existing url-type items", Bare: true},
{Name: "theme", Description: "Accepted for the PHP command; ignored"},
{Name: "append", Description: "Add the addresses instead of replacing the additional addresses", Bare: true},
},
Examples: []bonfire.Example{
{Command: "sitemap:import urls.json", Description: "Replace the url items with the manifest's URLs"},
{Command: "sitemap:import urls.json --append", Description: "Add the URLs and keep every existing item"},
{Command: "sitemap:import urls.json", Description: "Replace the additional addresses with the manifest's addresses"},
{Command: "sitemap:import urls.json --append", Description: "Add the addresses and keep the existing ones"},
},
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
file, _ := in.Argument("file")
@@ -72,136 +72,67 @@ func ImportFile(ctx context.Context, db *gorm.DB, file, theme string, appendMode
}
return fmt.Errorf("Cannot read %s: %w", file, err)
}
entries, labels, ok := orderedEntries(raw)
entries, labels, ok := models.LegacyEntries(raw)
if !ok {
return errors.New("Invalid JSON: expected an array of URL entries")
}
newItems := make([]models.DefinitionItem, 0, len(entries))
rows := make([]models.ExtraURL, 0, len(entries))
for i, entry := range entries {
var fields map[string]json.RawMessage
_ = json.Unmarshal(entry, &fields)
url, err := models.DecodeText(fields["url"])
if fields["url"] == nil || err != nil || phpEmpty(fields["url"]) {
if fields["url"] == nil || err != nil || strings.TrimSpace(models.Text(url)) == "" {
return fmt.Errorf("Entry %s is missing a 'url' field", labels[i])
}
if !classes.AllowedItemURL(models.Text(url)) {
u := strings.TrimSpace(models.Text(url))
if !models.AllowedAddress(u) {
return fmt.Errorf("Entry %s has a url that is not a path or an http(s) URL", labels[i])
}
item := models.DefinitionItem{Type: models.ItemTypeURL, URL: url}
item.Changefreq = textOr(fields["changefreq"], "monthly")
item.Priority = textOr(fields["priority"], "0.5")
newItems = append(newItems, item)
row := models.ExtraURL{URL: u}
if raw, ok := fields["lastmod"]; ok {
mod, err := models.DecodeText(raw)
if err != nil || (strings.TrimSpace(models.Text(mod)) != "" && !models.ValidDate(models.Text(mod))) {
return fmt.Errorf("Entry %s has a lastmod that is not a date (YYYY-MM-DD)", labels[i])
}
row.LastMod = strings.TrimSpace(models.Text(mod))
}
rows = append(rows, row)
}
var kept int
var kept, added int
err = db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
def, err := singletonDefinition(tx)
s, err := loadSettingsForUpdate(tx)
if err != nil {
return err
}
existing := def.Items()
if !appendMode {
var dynamic []models.DefinitionItem
for _, item := range existing {
if item.Type != models.ItemTypeURL {
dynamic = append(dynamic, item)
}
next := []models.ExtraURL{}
seen := map[string]bool{}
if appendMode {
for _, e := range s.Extra() {
next = append(next, e)
seen[e.URL] = true
}
existing = dynamic
kept = len(next)
}
kept = len(existing)
def.SetItems(append(append([]models.DefinitionItem{}, existing...), newItems...))
return tx.Save(&def).Error
for _, r := range rows {
if seen[r.URL] {
continue
}
seen[r.URL] = true
next = append(next, r)
added++
}
return saveExtra(tx, next)
})
if err != nil {
return err
}
out.Success(fmt.Sprintf("Imported %d URLs into sitemap definition for theme '%s'.", len(newItems), models.DefinitionTheme))
out.Success(fmt.Sprintf("Imported %d addresses into the sitemap's additional addresses.", added))
if kept > 0 {
label := "existing non-URL (dynamic)"
if appendMode {
label = "existing"
}
out.Info(fmt.Sprintf("Kept %d %s items.", kept, label))
out.Info(fmt.Sprintf("Kept %d existing addresses.", kept))
}
if theme != "" && theme != models.DefinitionTheme {
out.Warning(fmt.Sprintf("Theme '%s' was given, but /sitemap.xml serves only the '%s' definition, so the URLs were imported there.", theme, models.DefinitionTheme))
if theme != "" {
out.Warning(fmt.Sprintf("Theme '%s' was given; the sitemap has one set of additional addresses, so the addresses were imported there.", theme))
}
out.Info("Run sitemap:rebuild (or save the sitemap settings) to update the files now.")
return nil
}
// orderedEntries splits a JSON array, or a JSON object in key order (a PHP
// array with keys), into its values with their PHP labels (index or key).
func orderedEntries(raw []byte) ([]json.RawMessage, []string, bool) {
raw = bytes.TrimSpace(raw)
if len(raw) == 0 {
return nil, nil, false
}
switch raw[0] {
case '[':
var list []json.RawMessage
if err := json.Unmarshal(raw, &list); err != nil {
return nil, nil, false
}
labels := make([]string, len(list))
for i := range list {
labels[i] = fmt.Sprint(i)
}
return list, labels, true
case '{':
dec := json.NewDecoder(bytes.NewReader(raw))
if _, err := dec.Token(); err != nil {
return nil, nil, false
}
var list []json.RawMessage
var labels []string
for dec.More() {
tok, err := dec.Token()
if err != nil {
return nil, nil, false
}
key, _ := tok.(string)
var v json.RawMessage
if err := dec.Decode(&v); err != nil {
return nil, nil, false
}
list = append(list, v)
labels = append(labels, key)
}
return list, labels, true
}
return nil, nil, false
}
// phpEmpty is PHP empty() of a decoded JSON value.
func phpEmpty(raw json.RawMessage) bool {
var v any
if err := json.Unmarshal(raw, &v); err != nil {
return true
}
switch t := v.(type) {
case nil:
return true
case string:
return t == "" || t == "0"
case bool:
return !t
case float64:
return t == 0
case []any:
return len(t) == 0
case map[string]any:
return len(t) == 0
}
return false
}
// textOr is PHP `$entry[key] ?? fallback` as text: a missing or null value
// is the fallback.
func textOr(raw json.RawMessage, fallback string) *string {
if raw != nil {
if s, err := models.DecodeText(raw); err == nil && s != nil {
return s
}
}
return &fallback
}