first commit
This commit is contained in:
commit
8c4dcae475
89 changed files with 24623 additions and 0 deletions
194
history.go
Normal file
194
history.go
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
// history.go - opt-in, cookie-local browsing history: recently viewed
|
||||
// documents and recent searches. Both are OFF by default (see Prefs).
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
recentDocsCookie = "recent_docs"
|
||||
recentSearchesCookie = "recent_searches"
|
||||
recentDocsMax = 20
|
||||
recentSearchesMax = 15
|
||||
)
|
||||
|
||||
func readIDListCookie(r *http.Request, name string, max int) []string {
|
||||
c, err := r.Cookie(name)
|
||||
if err != nil || c.Value == "" {
|
||||
return nil
|
||||
}
|
||||
var out []string
|
||||
for _, id := range strings.Split(c.Value, "|") {
|
||||
if id != "" {
|
||||
out = append(out, id)
|
||||
if len(out) == max {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func pushFront(list []string, id string, max int) []string {
|
||||
out := make([]string, 0, max)
|
||||
out = append(out, id)
|
||||
for _, x := range list {
|
||||
if len(out) == max {
|
||||
break
|
||||
}
|
||||
if x != id {
|
||||
out = append(out, x)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func pushRecentDoc(w http.ResponseWriter, r *http.Request, id string) {
|
||||
ids := pushFront(readIDListCookie(r, recentDocsCookie, recentDocsMax), id, recentDocsMax)
|
||||
writeListCookie(w, recentDocsCookie, ids)
|
||||
}
|
||||
|
||||
type recentSearch struct {
|
||||
Label string
|
||||
URL string
|
||||
QS string
|
||||
}
|
||||
|
||||
func recordRecentSearch(w http.ResponseWriter, r *http.Request, qs string) []string {
|
||||
if len(qs) > searchQSMax {
|
||||
return nil
|
||||
}
|
||||
entry := url.QueryEscape(qs)
|
||||
|
||||
entries := readIDListCookie(r, recentSearchesCookie, recentSearchesMax)
|
||||
entries = pushFront(entries, entry, recentSearchesMax)
|
||||
return writeListCookie(w, recentSearchesCookie, entries)
|
||||
}
|
||||
|
||||
func (co *Corpus) readRecentSearches(r *http.Request) []recentSearch {
|
||||
return co.parseSearchEntries(readIDListCookie(r, recentSearchesCookie, recentSearchesMax))
|
||||
}
|
||||
|
||||
func (co *Corpus) parseSearchEntries(entries []string) []recentSearch {
|
||||
var out []recentSearch
|
||||
for _, e := range entries {
|
||||
qs, err := url.QueryUnescape(e)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
v, err := url.ParseQuery(qs)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if label, ok := co.searchLabel(v); ok {
|
||||
out = append(out, recentSearch{Label: label, URL: "/search?" + qs, QS: qs})
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
const searchQSMax = 500
|
||||
|
||||
func (co *Corpus) savableSearch(v url.Values) bool {
|
||||
if len(v.Encode()) > searchQSMax {
|
||||
return false
|
||||
}
|
||||
_, ok := co.searchLabel(v)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (co *Corpus) searchLabel(v url.Values) (label string, ok bool) {
|
||||
var parts []string
|
||||
fields := v["fqf"]
|
||||
conjs := v["fqc"]
|
||||
for i, term := range v["fqv"] {
|
||||
if term == "" {
|
||||
continue
|
||||
}
|
||||
label := term
|
||||
if i < len(fields) && fields[i] != "" && fields[i] != "TE" {
|
||||
code := fields[i]
|
||||
if f, ok := fieldByCode[code]; ok {
|
||||
label = term + " [" + f.Label + "]"
|
||||
} else {
|
||||
label = term + " [" + code + "]"
|
||||
}
|
||||
}
|
||||
if i < len(conjs) && conjs[i] == "not" {
|
||||
label = "-" + label
|
||||
}
|
||||
parts = append(parts, label)
|
||||
}
|
||||
var notes, filters []string
|
||||
if colls := v["coll"]; len(colls) > 0 && len(colls) < len(validCollections) {
|
||||
filters = append(filters, colls...)
|
||||
}
|
||||
if types := v["type"]; len(types) > 0 && (len(co.archive.Types) == 0 || len(types) < len(co.archive.Types)) {
|
||||
filters = append(filters, types...)
|
||||
}
|
||||
if len(filters) > 0 {
|
||||
notes = append(notes, "("+strings.Join(filters, ", ")+")")
|
||||
}
|
||||
if n := dateRangeNote("doc", v.Get("date_doc_start"), v.Get("date_doc_end")); n != "" {
|
||||
notes = append(notes, n)
|
||||
}
|
||||
if n := dateRangeNote("pub", v.Get("date_pub_start"), v.Get("date_pub_end")); n != "" {
|
||||
notes = append(notes, n)
|
||||
}
|
||||
switch {
|
||||
case len(parts) == 0 && len(notes) == 0:
|
||||
return "", false
|
||||
case len(parts) == 0:
|
||||
parts = []string{"all documents"}
|
||||
}
|
||||
return strings.Join(append(parts, notes...), " · "), true
|
||||
}
|
||||
|
||||
func dateRangeNote(kind, start, end string) string {
|
||||
switch {
|
||||
case start == "" && end == "":
|
||||
return ""
|
||||
case start == end || (start != "" && end == padDateEnd(start)):
|
||||
// date:2013 arrives as start "2013", end "2013-12-31": one year
|
||||
return "(" + kind + " " + start + ")"
|
||||
case start == "":
|
||||
return "(" + kind + " to " + end + ")"
|
||||
case end == "":
|
||||
return "(" + kind + " from " + start + ")"
|
||||
default:
|
||||
return "(" + kind + " " + start + " to " + end + ")"
|
||||
}
|
||||
}
|
||||
|
||||
func handleHistoryClear(w http.ResponseWriter, r *http.Request) {
|
||||
if !readPOSTForm(w, r) {
|
||||
return
|
||||
}
|
||||
switch r.FormValue("which") {
|
||||
case "docs":
|
||||
delCookie(w, recentDocsCookie)
|
||||
case "searches":
|
||||
delCookie(w, recentSearchesCookie)
|
||||
default:
|
||||
delCookie(w, recentDocsCookie)
|
||||
delCookie(w, recentSearchesCookie)
|
||||
}
|
||||
http.Redirect(w, r, safeBackPath(r.FormValue("back")), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// safeBackPath only allows local absolute paths as redirect targets.
|
||||
func safeBackPath(p string) string {
|
||||
if !strings.HasPrefix(p, "/") || strings.HasPrefix(p, "//") {
|
||||
return "/"
|
||||
}
|
||||
for i := 0; i < len(p); i++ {
|
||||
if p[i] == '\\' || p[i] < 0x20 {
|
||||
return "/"
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue