Files
supermarket/internal/web/links.go
T
davidandClaude Opus 5.5 a1409d91ad Phase 8 part 3: lost links
Each known linking page (from Bing, Plausible or added by hand) is
opened once a week, at most 100 a day, to check it still links to the
site: kept, gone (the page no longer has the link), page gone (404, 410
or moved elsewhere) or pointing at a 404 (our page it links to is gone).
SEO → Links lists the lost ones with what happened, when last seen and
what the site brought; links can be added by hand (checked at once),
checked now, or forgotten. What to write next suggests a redirect for a
link to a 404, and asking for a lost link back when it brought visitors.
Links and What to write next now work for websites without Search
Console too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 08:43:30 -07:00

200 lines
5.6 KiB
Go

package web
import (
"context"
"net/http"
"strconv"
"strings"
"time"
"supermarket/internal/backlinks"
"supermarket/internal/profile"
"supermarket/internal/seo"
"supermarket/internal/tracking"
)
// SEO → Links: links from other sites to yours.
// linkDays is the period the Links page covers, as What ranks does.
const linkDays = 90
func (s *Server) seoLinksPage(w http.ResponseWriter, r *http.Request) {
s.renderLinks(w, r, "")
}
func (s *Server) renderLinks(w http.ResponseWriter, r *http.Request, errMsg string) {
// Links don't need Search Console: every website with an address.
sites, cur := s.auditSites(r)
data := map[string]any{"Sites": sites, "Current": cur, "Connected": true}
data["Path"] = "/seo/links"
data["Plausible"] = s.plausible() != nil
data["Brought"] = map[string]tracking.Referrer{}
if cur != nil {
if p, _ := profile.Load(s.DB); p != nil {
for _, web := range p.Websites {
if web.URL != cur.URL {
continue
}
list, ok, err := tracking.Referrers(s.DB, web, linkDays)
if err != nil {
s.serverError(w, err)
return
}
data["Read"] = ok
var links []tracking.Referrer
others := map[string]int{}
for _, ref := range list {
if ref.Kind == tracking.RefLink {
links = append(links, ref)
} else {
others[ref.Kind] += ref.Visitors
}
}
data["Links"], data["Others"] = links, others
brought := map[string]tracking.Referrer{}
for _, ref := range list {
brought[strings.TrimPrefix(strings.ToLower(ref.Source), "www.")] = ref
}
data["Brought"] = brought
}
}
var known int
_ = s.DB.QueryRow(`SELECT count(*) FROM backlinks WHERE site = ?`, cur.URL).Scan(&known)
data["Known"] = known
}
if cur != nil {
data["BingKey"] = s.bing() != nil
data["BingSite"] = s.bingSite(*cur)
st, err := backlinks.LastBing(s.DB, cur.URL)
if err != nil {
s.serverError(w, err)
return
}
data["Bing"] = st
links, err := backlinks.Links(s.DB, cur.URL)
if err != nil {
s.serverError(w, err)
return
}
first := ""
if st != nil {
first = st.First
}
sites := backlinks.Sites(links, first)
var lost []backlinks.Link
counts := map[string]int{}
for _, l := range links {
counts[l.Status]++
if l.Lost() {
lost = append(lost, l)
}
}
data["Lost"], data["Counts"] = lost, counts
data["LinkingSites"], data["LinkCount"] = sites, len(links)
if len(sites) > 30 {
data["MoreSites"], data["LinkingSites"] = len(sites)-30, sites[:30]
}
}
last, _ := tracking.Last(s.DB, "referrers")
data["Last"] = last
s.render(w, r, "seo-links", page{Title: "Links", Nav: "seo/links", Error: errMsg, Data: data})
}
// seoLinksBing reads the site's links from Bing now.
func (s *Server) seoLinksBing(w http.ResponseWriter, r *http.Request) {
_, cur := s.auditSites(r)
c := s.bing()
if cur == nil || c == nil {
redirect(w, r, "/seo/links", "")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Minute)
defer cancel()
back := "/seo/links?site=" + strconv.Itoa(cur.Index) + "#bing"
if _, err := backlinks.SyncBing(ctx, s.DB, c, cur.URL, s.bingSite(*cur), time.Now()); err != nil {
redirect(w, r, back, "") // the card shows the error
return
}
redirect(w, r, back, "bing-read")
}
// weeklyBingLinks reads each website's links from Bing once a week.
func (s *Server) weeklyBingLinks(ctx context.Context) {
c := s.bing()
if c == nil {
return
}
now := time.Now()
for _, site := range s.seoSites() {
if site.URL == "" || !backlinks.DueBing(s.DB, site.URL, now) {
continue
}
bctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
if _, err := backlinks.SyncBing(bctx, s.DB, c, site.URL, s.bingSite(site), now); err != nil {
s.Log.Warn("backlinks: reading Bing", "site", site.URL, "err", err)
}
cancel()
}
}
// linkFetcher reads other sites' pages for the link checks.
func (s *Server) linkFetcher() backlinks.Fetcher {
return seo.NewFetcher("Supermarket/" + s.Version + " (link check)")
}
// seoLinksAction adds a link by hand, checks links now, or forgets one.
func (s *Server) seoLinksAction(w http.ResponseWriter, r *http.Request) {
_, cur := s.auditSites(r)
if cur == nil {
http.NotFound(w, r)
return
}
back := "/seo/links?site=" + strconv.Itoa(cur.Index) + "#lost"
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Minute)
defer cancel()
now := time.Now()
switch r.PathValue("action") {
case "add":
l, err := backlinks.Add(s.DB, cur.URL, r.PostFormValue("source"), r.PostFormValue("target"), now)
if err != nil {
s.renderLinks(w, r, err.Error())
return
}
if err := backlinks.CheckOne(ctx, s.DB, s.linkFetcher(), cur.URL, l, now); err != nil {
s.serverError(w, err)
return
}
redirect(w, r, back, "link-added")
case "check":
n, err := backlinks.CheckDue(ctx, s.DB, s.linkFetcher(), cur.URL, 20, now)
if err != nil {
s.serverError(w, err)
return
}
redirectCounts(w, r, back, "links-checked", n)
case "forget":
id, _ := strconv.ParseInt(r.PostFormValue("id"), 10, 64)
if err := backlinks.Remove(s.DB, cur.URL, id); err != nil {
s.serverError(w, err)
return
}
redirect(w, r, back, "link-forgotten")
default:
http.NotFound(w, r)
}
}
// dailyLinkChecks checks the links due, within the day's limit.
func (s *Server) dailyLinkChecks(ctx context.Context) {
for _, site := range s.seoSites() {
if site.URL == "" {
continue
}
cctx, cancel := context.WithTimeout(ctx, 20*time.Minute)
if _, err := backlinks.CheckDue(cctx, s.DB, s.linkFetcher(), site.URL, 0, time.Now()); err != nil {
s.Log.Warn("backlinks: checking links", "site", site.URL, "err", err)
}
cancel()
}
}