package server import ( "crypto/sha256" "database/sql" "encoding/hex" "errors" "fmt" "net/url" "path" "strings" "pageswiki/internal/store" ) type cityAutoPlanItem struct { Path string `json:"path"` PageUUID string `json:"page_uuid"` Quartier, Street, Spot string QuartierPath, StreetPath, SpotPath string `json:",omitempty"` Undefined bool `json:"undefined_spot"` Action string `json:"action"` } func cityAutoName(raw, prefix string) string { decoded, err := url.PathUnescape(raw) if err == nil { raw = decoded } name := citySuggestionName(raw) if len(name) >= 2 { return name } sum := sha256.Sum256([]byte(raw)) return prefix + "-" + hex.EncodeToString(sum[:4]) } // Names are identifiers, not ownership. In particular Spot names are unique // across the entire Domain, even when their Streets differ. func (s *Server) availableCityAutoName(domain, kind, parent, base, boundary string) (string, error) { for attempt := 0; attempt < 32; attempt++ { name := base if attempt > 0 { sum := sha256.Sum256([]byte(fmt.Sprintf("%s:%s:%d", kind, boundary, attempt))) stem := base if len(stem) > 47 { stem = stem[:47] } name = stem + "-" + hex.EncodeToString(sum[:8]) } exists, err := s.store.CityNameExists(domain, kind, parent, name) if err != nil { return "", err } if !exists { return name, nil } } return "", errors.New("unable to allocate unique generated City name") } func (s *Server) buildCityAutoPlan(domainUUID string) ([]cityAutoPlanItem, error) { candidates, err := s.store.CityAutoCandidates(domainUUID) if err != nil { return nil, err } var out []cityAutoPlanItem // Many Pages share one boundary. Resolve each boundary once, without // retaining a database cursor while making nested repository calls. type lookupResult struct { node store.CityNodeDetail err error } cache := map[string]lookupResult{} lookup := func(kind, parent, boundary string) (store.CityNodeDetail, error) { key := kind + "\x00" + parent + "\x00" + boundary if r, ok := cache[key]; ok { return r.node, r.err } node, err := s.store.CityNodeOverlappingPath(domainUUID, kind, parent, boundary) cache[key] = lookupResult{node, err} return node, err } for _, candidate := range candidates { semanticPath, pageUUID := candidate.Path, candidate.ObjectUUID parts := strings.Split(strings.Trim(semanticPath, "/"), "/") if len(parts) < 3 || parts[0] == "system" { continue } item := cityAutoPlanItem{Path: semanticPath, PageUUID: pageUUID, Quartier: cityAutoName(parts[0], "quartier"), Street: cityAutoName(parts[1], "street"), QuartierPath: "/" + parts[0], StreetPath: "/" + parts[0] + "/" + parts[1]} if len(parts) >= 4 { item.Spot = cityAutoName(parts[2], "spot") item.SpotPath = item.StreetPath + "/" + parts[2] } else { item.Undefined = true item.Spot = item.Street + "-undefined" item.SpotPath = item.StreetPath + "/_undefined" } item.Action = "create" if q, e := lookup("quartier", "", item.QuartierPath); e == nil { item.Quartier = q.Name item.Action = "merge" if st, e := lookup("street", q.UUID, item.StreetPath); e == nil { item.Street = st.Name if spot, e := lookup("spot", st.UUID, item.SpotPath); e == nil { item.Spot = spot.Name } else if !errors.Is(e, sql.ErrNoRows) { return nil, e } } else if !errors.Is(e, sql.ErrNoRows) { return nil, e } } else if !errors.Is(e, sql.ErrNoRows) { return nil, e } out = append(out, item) } return out, nil } func (s *Server) applyCityAutoPlan(domainUUID, actor string) (int, error) { plan, err := s.buildCityAutoPlan(domainUUID) if err != nil { return 0, err } quartiers, streets, spots := map[string]string{}, map[string]string{}, map[string]string{} applied := 0 for _, item := range plan { qID := quartiers[item.QuartierPath] if qID == "" { if q, e := s.store.CityNodeOverlappingPath(domainUUID, "quartier", "", item.QuartierPath); e == nil { qID = q.UUID } else { if !errors.Is(e, sql.ErrNoRows) { return applied, e } name, err := s.availableCityAutoName(domainUUID, "quartier", "", item.Quartier, item.QuartierPath) if err != nil { return applied, err } qID, e = s.store.CreateZoneAtPath(domainUUID, actor, "quartier", "", name, item.Quartier, "Generated from URL Paths", item.QuartierPath) if e != nil { return applied, e } } quartiers[item.QuartierPath] = qID } streetKey := qID + item.StreetPath streetID := streets[streetKey] if streetID == "" { if st, e := s.store.CityNodeOverlappingPath(domainUUID, "street", qID, item.StreetPath); e == nil { streetID = st.UUID } else { if !errors.Is(e, sql.ErrNoRows) { return applied, e } name, err := s.availableCityAutoName(domainUUID, "street", qID, item.Street, item.StreetPath) if err != nil { return applied, err } streetID, e = s.store.CreateZoneAtPath(domainUUID, actor, "street", qID, name, item.Street, "", item.StreetPath) if e != nil { return applied, e } } streets[streetKey] = streetID } spotKey := streetID + item.SpotPath spotID := spots[spotKey] if spotID == "" { if spot, e := s.store.CityNodeOverlappingPath(domainUUID, "spot", streetID, item.SpotPath); e == nil { spotID = spot.UUID } else { if !errors.Is(e, sql.ErrNoRows) { return applied, e } name, err := s.availableCityAutoName(domainUUID, "spot", streetID, item.Spot, item.SpotPath) if err != nil { return applied, err } spotID, e = s.store.CreateSpotAtPath(domainUUID, actor, streetID, name, item.Spot, "Generated from URL Paths", item.SpotPath) if e != nil { return applied, e } } spots[spotKey] = spotID } if err := s.store.AddSpotPage(domainUUID, actor, spotID, item.PageUUID, item.Path, "primary", path.Base(item.Path)); err != nil && !errors.Is(err, sql.ErrNoRows) { return applied, err } applied++ } return applied, nil }