Files
sm-sitemap-plugin/console/helpers.go
Jakub Zych 4a82240429 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
2026-10-12 17:07:39 +02:00

76 lines
2.2 KiB
Go

// Package console holds the sitemap plugin's commands: sitemap:import (the
// PHP ImportCommand) and sitemap:import-winter (a copy of a WinterCMS
// site's definition and settings).
package console
import (
"context"
"fmt"
"strings"
"git.golem15.com/golem15/sm-sitemap-plugin/models"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bonfire"
"git.golem15.com/golem15/summercms/modules/lagoon"
"gorm.io/gorm"
)
func flagValue(in bonfire.Input, name string) string {
v, ok := in.Flag(name)
if !ok {
return ""
}
return strings.TrimSpace(v)
}
func flagOn(in bonfire.Input, name string) bool {
switch strings.ToLower(flagValue(in, name)) {
case "true", "1", "yes":
return true
}
return false
}
// lookupDB returns the app database. The generated main.go publishes no
// database for plugin commands, so when none is published it opens
// database.dsn and publishes it the way lagoon's migrate does. The returned
// func closes a pool this call opened; it is a no-op otherwise.
func lookupDB(ctx context.Context, app *backpack.App) (*gorm.DB, func(), error) {
if app == nil {
return nil, nil, fmt.Errorf("golem15.sitemap: app is nil")
}
if db, ok := app.Lookup[*gorm.DB](); ok && db != nil {
return db, func() {}, nil
}
if lagoon.DSN(app.Config) == "" {
return nil, nil, fmt.Errorf("golem15.sitemap: database is not configured (set SUMMER_DATABASE__DSN)")
}
sqlDB, gdb, err := lagoon.OpenFromApp(ctx, app)
if err != nil {
return nil, nil, err
}
if err := lagoon.Publish(app, sqlDB, gdb); err != nil {
_ = sqlDB.Close()
return nil, nil, err
}
return gdb, func() { _ = sqlDB.Close() }, nil
}
// singletonDefinition loads the definition with the fixed theme key,
// creating it when there is none (PHP Definition::firstOrCreate).
func singletonDefinition(tx *gorm.DB) (models.Definition, error) {
var defs []models.Definition
if err := tx.Where("theme = ?", models.DefinitionTheme).Order("id ASC").Limit(1).Find(&defs).Error; err != nil {
return models.Definition{}, err
}
if len(defs) > 0 {
return defs[0], nil
}
theme := models.DefinitionTheme
def := models.Definition{Theme: &theme}
if err := tx.Create(&def).Error; err != nil {
return models.Definition{}, err
}
return def, nil
}