Files
supermarket/internal/seo/scope.go
T
davidandClaude Opus 5.5 c36f786ae9 Fixes from the first round of TEST.md
SEO: a domain property (sc-domain:) covers every subdomain, so the shop,
Gitea and forum were counted as the website. What ranks, the morning
check, the Monday report, keywords, change candidates and the audit's
traffic now read only the website's own host (with or without www).
The report keeps pages worth a proposal apart from "Waiting for your
yes", explains only moves past the thresholds, and says "places up/down".

Also:
- Feed summaries drop the posts' scripts and styles.
- Spend you log can't be negative.
- Test and check buttons can't be clicked twice while they run.
- Previews show the site's web fonts and icon sprites.
- Backups are read without leaving -wal/-shm files; deleting one removes
  them, and leftovers are swept at start.
- The Overview's setup flags a CRM that Hermes can't reach.
- Import now says what came in; adding a prospect that exists says so;
  prospects can be deleted.
- Signal tags and brief addresses keep accented letters (Ü → u).
- Page errors start with a capital; clearer messages for bad uploads,
  CRM retry and cancel, keywords, and the finished audit.
- The module you're in stays unfolded in the sidebar.
- A picked keyword can be unpicked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 06:31:42 -07:00

74 lines
2.0 KiB
Go

package seo
import (
"net/url"
"strings"
)
// Scope is one website's share of a Search Console property. A domain
// property (sc-domain:example.com) covers every subdomain — a shop, a Gitea,
// a forum — so its rows are narrowed to the website's own host, with or
// without www. With no hosts it's the whole property.
type Scope struct {
Property string
Hosts []string // lower case, without www.
}
// NewScope is the scope of the website at siteURL within property. Only a
// domain property is narrowed, and only to a host within its domain: a
// URL-prefix property is one site already.
func NewScope(property, siteURL string) Scope {
s := Scope{Property: property}
domain, ok := strings.CutPrefix(strings.ToLower(property), "sc-domain:")
u, err := url.Parse(siteURL)
if !ok || err != nil {
return s
}
host := strings.TrimPrefix(strings.ToLower(u.Hostname()), "www.")
if host == domain || strings.HasSuffix(host, "."+domain) {
s.Hosts = []string{host}
}
return s
}
// Has says whether a page's address is on the website.
func (s Scope) Has(page string) bool {
if len(s.Hosts) == 0 {
return true
}
u, err := url.Parse(page)
if err != nil {
return false
}
h := strings.TrimPrefix(strings.ToLower(u.Hostname()), "www.")
for _, want := range s.Hosts {
if h == want {
return true
}
}
return false
}
// where is the SQL condition for the scope's rows of a gsc_ table, which
// has site and page columns, and its arguments.
func (s Scope) where() (string, []any) {
cond := "site = ?"
args := []any{s.Property}
if len(s.Hosts) == 0 {
return cond, args
}
var ors []string
for _, h := range s.Hosts {
h = likeEscape(h)
for _, prefix := range []string{"https://", "http://", "https://www.", "http://www."} {
ors = append(ors, `page LIKE ? ESCAPE '\'`, `page = ?`)
args = append(args, prefix+h+"/%", prefix+h)
}
}
return cond + " AND (" + strings.Join(ors, " OR ") + ")", args
}
func likeEscape(s string) string {
return strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(s)
}