2022-08-23 09:57:42 +00:00
|
|
|
#lang racket/base
|
|
|
|
(require racket/dict
|
|
|
|
racket/list
|
|
|
|
racket/string
|
|
|
|
(prefix-in easy: net/http-easy)
|
|
|
|
; html libs
|
|
|
|
html-writing
|
|
|
|
; web server libs
|
|
|
|
net/url
|
|
|
|
web-server/http
|
|
|
|
(only-in web-server/dispatchers/dispatch next-dispatcher)
|
|
|
|
#;(only-in web-server/http/redirect redirect-to)
|
|
|
|
"application-globals.rkt"
|
2022-09-16 13:56:03 +00:00
|
|
|
"config.rkt"
|
|
|
|
"data.rkt"
|
2023-12-06 00:08:20 +00:00
|
|
|
"search-provider-fandom.rkt"
|
|
|
|
"search-provider-solr.rkt"
|
2023-02-05 04:56:15 +00:00
|
|
|
"../lib/syntax.rkt"
|
2023-02-12 00:55:33 +00:00
|
|
|
"../lib/thread-utils.rkt"
|
2023-02-05 04:56:15 +00:00
|
|
|
"../lib/url-utils.rkt"
|
|
|
|
"whole-utils.rkt"
|
|
|
|
"../lib/xexpr-utils.rkt")
|
2022-08-23 09:57:42 +00:00
|
|
|
|
|
|
|
(provide
|
|
|
|
page-search)
|
|
|
|
|
2023-12-06 00:08:20 +00:00
|
|
|
(define search-providers
|
|
|
|
(hash "fandom" generate-results-content-fandom
|
|
|
|
"solr" generate-results-content-solr))
|
2022-08-23 09:57:42 +00:00
|
|
|
|
2023-05-27 11:48:08 +00:00
|
|
|
;; this takes the info we gathered from fandom and makes the big fat x-expression page
|
2023-12-06 00:08:20 +00:00
|
|
|
(define (generate-results-page req source-url wikiname query results-content #:siteinfo [siteinfo #f])
|
2023-05-27 11:48:08 +00:00
|
|
|
;; this is *another* helper that builds the wiki page UI and lets me put the search results (or whatever else) in the middle
|
2022-08-23 09:57:42 +00:00
|
|
|
(generate-wiki-page
|
2023-05-27 11:48:08 +00:00
|
|
|
;; so I provide my helper function with the necessary context...
|
2022-11-29 11:03:54 +00:00
|
|
|
#:req req
|
2023-12-06 00:08:20 +00:00
|
|
|
#:source-url source-url
|
2022-09-16 12:56:05 +00:00
|
|
|
#:wikiname wikiname
|
2022-10-30 11:03:13 +00:00
|
|
|
#:title query
|
2022-10-09 09:50:50 +00:00
|
|
|
#:siteinfo siteinfo
|
2023-05-27 11:48:08 +00:00
|
|
|
;; and here's the actual results to display in the wiki page layout
|
2023-12-06 00:08:20 +00:00
|
|
|
results-content))
|
2022-08-23 09:57:42 +00:00
|
|
|
|
2023-05-27 11:48:08 +00:00
|
|
|
;; will be called when the web browser asks to load the page
|
2022-08-23 09:57:42 +00:00
|
|
|
(define (page-search req)
|
2023-05-27 11:48:08 +00:00
|
|
|
;; this just means, catch any errors and display them in the browser. it's a function somewhere else
|
2022-08-23 09:57:42 +00:00
|
|
|
(response-handler
|
2023-05-27 11:48:08 +00:00
|
|
|
;; the URL will look like "/minecraft/wiki/Special:Search?q=Spawner"
|
|
|
|
;; grab the first part to use as the wikiname, in this case, "minecraft"
|
2022-08-23 09:57:42 +00:00
|
|
|
(define wikiname (path/param-path (first (url-path (request-uri req)))))
|
2023-12-06 00:08:20 +00:00
|
|
|
;; grab a dict of url search params
|
|
|
|
(define params (url-query (request-uri req)))
|
2023-05-27 11:48:08 +00:00
|
|
|
;; grab the part after ?q= which is the search terms
|
2023-12-06 00:08:20 +00:00
|
|
|
(define query (dict-ref params 'q #f))
|
|
|
|
;; figure out which search provider we're going to use
|
|
|
|
(define search-provider (hash-ref search-providers (config-get 'feature_offline::search)))
|
2022-09-16 13:56:03 +00:00
|
|
|
|
2023-12-06 00:08:20 +00:00
|
|
|
;; external special:search url to link at the bottom of the page as the upstream source
|
|
|
|
(define external-search-url
|
|
|
|
(format "https://~a.fandom.com/wiki/Special:Search?~a"
|
|
|
|
wikiname
|
|
|
|
(params->query `(("query" . ,query)
|
|
|
|
("search" . "internal")))))
|
|
|
|
|
|
|
|
;; simultaneously get the search results, as well as information about the wiki as a whole (its license, icon, name)
|
|
|
|
(define-values (results-content siteinfo)
|
2023-02-12 00:55:33 +00:00
|
|
|
(thread-values
|
|
|
|
(λ ()
|
2023-12-06 00:08:20 +00:00
|
|
|
(search-provider wikiname query params)) ;; call the search provider (see file "search-provider-fandom.rkt")
|
2023-02-12 00:55:33 +00:00
|
|
|
(λ ()
|
2023-05-27 11:48:08 +00:00
|
|
|
(siteinfo-fetch wikiname)))) ;; helper function in another file to get information about the wiki
|
2022-08-23 09:57:42 +00:00
|
|
|
|
2023-05-27 11:48:08 +00:00
|
|
|
;; calling my generate-results-page function with the information so far in order to get a big fat x-expression
|
|
|
|
;; big fat x-expression goes into the body variable
|
2023-12-06 00:08:20 +00:00
|
|
|
(define body (generate-results-page req external-search-url wikiname query results-content #:siteinfo siteinfo))
|
2023-05-27 11:48:08 +00:00
|
|
|
;; error checking
|
2023-02-12 00:55:33 +00:00
|
|
|
(when (config-true? 'debug)
|
|
|
|
; used for its side effects
|
|
|
|
; convert to string with error checking, error will be raised if xexp is invalid
|
|
|
|
(xexp->html body))
|
2023-05-27 11:48:08 +00:00
|
|
|
;; convert body to HTML and send to browser
|
2023-02-12 00:55:33 +00:00
|
|
|
(response/output
|
|
|
|
#:code 200
|
|
|
|
#:headers (build-headers always-headers)
|
|
|
|
(λ (out)
|
|
|
|
(write-html body out)))))
|
2023-12-06 00:08:20 +00:00
|
|
|
|