feat(17-05): sitemap:import-winter --page-url copies a CMS page item as a url item
- a Winter cms-page or static-page item whose reference is named by --page-url reference=/path becomes a url item with that URL (reported as remapped) instead of being dropped - smoke test and README cover the flag
This commit is contained in:
@@ -33,13 +33,16 @@ var droppedTypes = map[string]bool{
|
||||
// of scripts/winter-export-settings.sql (the JSON object of the
|
||||
// golem15_sitemap_settings system setting). Theme picks a definition when
|
||||
// the export holds several. Maps rename item types (old to new) before they
|
||||
// are checked. Force replaces a definition that has items; DryRun reports
|
||||
// without writing.
|
||||
// are checked. PageURLs turns a static or CMS page item into a url item:
|
||||
// its key is the item's reference (the PHP page file name, for example
|
||||
// blog), its value the page's URL (/blog). Force replaces a definition that
|
||||
// has items; DryRun reports without writing.
|
||||
type WinterOptions struct {
|
||||
DefinitionsFile string
|
||||
SettingsFile string
|
||||
Theme string
|
||||
Maps map[string]string
|
||||
PageURLs map[string]string
|
||||
Force bool
|
||||
DryRun bool
|
||||
}
|
||||
@@ -58,7 +61,8 @@ type WinterReport struct {
|
||||
// ImportWinter is sitemap:import-winter: it copies a WinterCMS site's
|
||||
// sitemap definition and settings into the definition /sitemap.xml serves
|
||||
// (theme default) and the settings row. Item types are mapped (url kept,
|
||||
// static and CMS page types dropped, --map old=new renames applied first),
|
||||
// static and CMS page types dropped unless --page-url reference=/path names
|
||||
// their URL, --map old=new renames applied first),
|
||||
// an item of a registry type that takes references is dropped when its
|
||||
// reference is not among the type's current references, an item of an
|
||||
// unknown type is dropped, and every dropped or remapped item is printed. A
|
||||
@@ -73,6 +77,7 @@ func ImportWinter(app *backpack.App) bonfire.Command {
|
||||
{Name: "settings", Description: "Settings export (the JSON object from scripts/winter-export-settings.sql)"},
|
||||
{Name: "theme", Description: "The PHP theme whose definition to copy when the export holds several"},
|
||||
{Name: "map", Description: "Rename an item type, old=new (repeatable)", Repeatable: true},
|
||||
{Name: "page-url", Description: "Copy a static or CMS page item as a url item, reference=/path (repeatable)", Repeatable: true},
|
||||
{Name: "force", Description: "Replace a definition that already has items", Bare: true},
|
||||
{Name: "dry-run", Description: "Report what would be copied without writing", Bare: true},
|
||||
},
|
||||
@@ -81,11 +86,16 @@ func ImportWinter(app *backpack.App) bonfire.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pageURLs, err := parsePageURLs(in.Flags("page-url"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts := WinterOptions{
|
||||
DefinitionsFile: flagValue(in, "definitions"),
|
||||
SettingsFile: flagValue(in, "settings"),
|
||||
Theme: flagValue(in, "theme"),
|
||||
Maps: maps,
|
||||
PageURLs: pageURLs,
|
||||
Force: flagOn(in, "force"),
|
||||
DryRun: flagOn(in, "dry-run"),
|
||||
}
|
||||
@@ -118,6 +128,25 @@ func parseMaps(values []string) (map[string]string, error) {
|
||||
return maps, nil
|
||||
}
|
||||
|
||||
// parsePageURLs reads --page-url reference=/path values. The URL must be a
|
||||
// root-relative path or an http(s) URL.
|
||||
func parsePageURLs(values []string) (map[string]string, error) {
|
||||
urls := map[string]string{}
|
||||
for _, v := range values {
|
||||
ref, url, ok := strings.Cut(v, "=")
|
||||
ref, url = strings.TrimSpace(ref), strings.TrimSpace(url)
|
||||
if !ok || ref == "" || url == "" {
|
||||
return nil, fmt.Errorf("--page-url %q: use reference=/path", v)
|
||||
}
|
||||
lower := strings.ToLower(url)
|
||||
if !strings.HasPrefix(url, "/") && !strings.HasPrefix(lower, "http://") && !strings.HasPrefix(lower, "https://") {
|
||||
return nil, fmt.Errorf("--page-url %q: the URL must start with / or http(s)://", v)
|
||||
}
|
||||
urls[ref] = url
|
||||
}
|
||||
return urls, nil
|
||||
}
|
||||
|
||||
// winterDefinition is one exported golem15_sitemap_definitions row.
|
||||
type winterDefinition struct {
|
||||
ID json.RawMessage `json:"id"`
|
||||
@@ -142,7 +171,7 @@ func ImportWinterFiles(ctx context.Context, db *gorm.DB, reg *signpost.Registry,
|
||||
return report, err
|
||||
}
|
||||
out.Info(fmt.Sprintf("Copying definition %s (theme '%s') into the '%s' definition.", strings.TrimSpace(string(row.ID)), models.Text(row.Theme), models.DefinitionTheme))
|
||||
items, err = mapItems(ctx, reg, row.Data, opts.Maps, &report)
|
||||
items, err = mapItems(ctx, reg, row.Data, opts.Maps, opts.PageURLs, &report)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
@@ -277,7 +306,7 @@ func exportedJSON(raw json.RawMessage) json.RawMessage {
|
||||
}
|
||||
|
||||
// mapItems decodes the exported data and maps its items.
|
||||
func mapItems(ctx context.Context, reg *signpost.Registry, data json.RawMessage, maps map[string]string, report *WinterReport) ([]models.DefinitionItem, error) {
|
||||
func mapItems(ctx context.Context, reg *signpost.Registry, data json.RawMessage, maps, pageURLs map[string]string, report *WinterReport) ([]models.DefinitionItem, error) {
|
||||
data = exportedJSON(data)
|
||||
if len(data) == 0 || bytes.Equal(data, []byte("null")) {
|
||||
return []models.DefinitionItem{}, nil
|
||||
@@ -299,6 +328,16 @@ func mapItems(ctx context.Context, reg *signpost.Registry, data json.RawMessage,
|
||||
if page, ok := fields["cmsPage"].(string); ok && page != "" {
|
||||
report.Lines = append(report.Lines, fmt.Sprintf("%s: cmsPage '%s' dropped (CMS pages are not supported)", label, page))
|
||||
}
|
||||
if url, ok := pageURLs[models.Text(item.Reference)]; ok && droppedTypes[item.Type] {
|
||||
report.Lines = append(report.Lines, fmt.Sprintf("%s: copied as type 'url' with url '%s'", label, url))
|
||||
report.Remapped++
|
||||
item.Type = models.ItemTypeURL
|
||||
item.URL = &url
|
||||
item.Reference = nil
|
||||
out = append(out, item)
|
||||
report.Kept++
|
||||
continue
|
||||
}
|
||||
if to, ok := maps[item.Type]; ok && to != item.Type {
|
||||
report.Lines = append(report.Lines, fmt.Sprintf("%s: type '%s' mapped to '%s'", label, item.Type, to))
|
||||
report.Remapped++
|
||||
|
||||
Reference in New Issue
Block a user