feat: image sitemap files (tracer)

- migration 202610280004: image_file_no, kind, published_at and parent_refs
  on the slot table; the image, News, IndexNow and HTML sitemap settings
- the rebuild stores each page's featured and body images (absolute on the
  site, deduplicated, at most 1000) and numbers the rows with images into
  sitemap-images-<n>.xml with the per-type files' stable rules
- the index lists the image files while images_enabled is on; the files
  route serves them with image:image and image:loc only
This commit is contained in:
Jakub Zych
2026-11-04 12:19:05 +01:00
parent 64453d2a91
commit 5bf56fde57
9 changed files with 587 additions and 25 deletions

View File

@@ -0,0 +1,87 @@
package updates
import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)
// mediaURLColumns are the slot table columns of the image and News
// sitemaps and the HTML sitemap (P33-13): the image file number places the
// rows with images in the sitemap-images files (stable, like file_no),
// published_at and kind feed the News sitemap, parent_refs (the record's
// parents as [{type, id}]) the News sources and the HTML sitemap's
// category tree.
var mediaURLColumns = []string{
"image_file_no INTEGER NULL",
"published_at TIMESTAMPTZ NULL",
"kind TEXT NOT NULL DEFAULT ''",
"parent_refs JSONB NOT NULL DEFAULT '[]'",
}
// mediaSettingsColumns are the settings of the image and News sitemaps,
// IndexNow and the HTML sitemap (UI-SPEC C26). html_paths is the HTML
// sitemap's address per locale. The JSONB columns take NULL, like
// extra_urls: a lagoon.Jsonable that is not valid writes NULL.
var mediaSettingsColumns = []string{
"images_enabled BOOLEAN NOT NULL DEFAULT TRUE",
"news_enabled BOOLEAN NOT NULL DEFAULT FALSE",
"news_publication TEXT NULL",
"news_sources JSONB NULL DEFAULT '[]'",
"indexnow_enabled BOOLEAN NOT NULL DEFAULT FALSE",
"indexnow_key TEXT NULL",
"indexnow_last_at TIMESTAMPTZ NULL",
"indexnow_last_count INTEGER NULL",
"indexnow_last_status INTEGER NULL",
"html_enabled BOOLEAN NOT NULL DEFAULT TRUE",
`html_paths JSONB NULL DEFAULT '{"pl":"/mapa-strony","en":"/sitemap"}'`,
"html_page_size INTEGER NOT NULL DEFAULT 150",
}
// columnName is the name part of a column definition.
func columnName(def string) string {
for i, r := range def {
if r == ' ' {
return def[:i]
}
}
return def
}
var addMedia = []*gormigrate.Migration{
{
ID: "202610280004_add_golem15_sitemap_media_news_indexnow",
Migrate: func(tx *gorm.DB) error {
for _, column := range mediaURLColumns {
if err := tx.Exec(`ALTER TABLE golem15_sitemap_urls ADD COLUMN ` + column).Error; err != nil {
return err
}
}
for _, column := range mediaSettingsColumns {
if err := tx.Exec(`ALTER TABLE golem15_sitemap_settings ADD COLUMN ` + column).Error; err != nil {
return err
}
}
return tx.Exec(`CREATE INDEX golem15_sitemap_urls_image_file_index ON golem15_sitemap_urls (` + columnName(mediaURLColumns[0]) + `)`).Error
},
Rollback: func(tx *gorm.DB) error {
if err := tx.Exec(`DROP INDEX IF EXISTS golem15_sitemap_urls_image_file_index`).Error; err != nil {
return err
}
for _, column := range mediaURLColumns {
if err := tx.Exec(`ALTER TABLE golem15_sitemap_urls DROP COLUMN IF EXISTS ` + columnName(column)).Error; err != nil {
return err
}
}
for _, column := range mediaSettingsColumns {
if err := tx.Exec(`ALTER TABLE golem15_sitemap_settings DROP COLUMN IF EXISTS ` + columnName(column)).Error; err != nil {
return err
}
}
return nil
},
},
}
func init() {
Register(addMedia...)
}