Disable category, file, search pages in offline mode

This commit is contained in:
Cadence Ember 2023-11-15 23:46:40 +13:00
parent b8ccd6cc3e
commit 9611b8c164
3 changed files with 156 additions and 138 deletions

View File

@ -65,64 +65,70 @@
(define (page-category req) (define (page-category req)
(response-handler (response-handler
(define wikiname (path/param-path (first (url-path (request-uri req))))) (cond
(define prefixed-category (string-join (map path/param-path (cddr (url-path (request-uri req)))) "/")) [(config-true? 'feature_offline::only)
(define origin (format "https://~a.fandom.com" wikiname)) (response/output #:code 503
(define source-url (format "~a/wiki/~a" origin prefixed-category)) #:headers (build-headers always-headers)
(λ (out) (write-html '(p "Sorry, category pages are temporarily disabled. I hope to have them back soon.") out)))]
[else
(define wikiname (path/param-path (first (url-path (request-uri req)))))
(define prefixed-category (string-join (map path/param-path (cddr (url-path (request-uri req)))) "/"))
(define origin (format "https://~a.fandom.com" wikiname))
(define source-url (format "~a/wiki/~a" origin prefixed-category))
(define-values (members-data page-data siteinfo) (define-values (members-data page-data siteinfo)
(thread-values (thread-values
(λ () (λ ()
(define dest-url (define dest-url
(format "~a/api.php?~a" (format "~a/api.php?~a"
origin origin
(params->query `(("action" . "query") (params->query `(("action" . "query")
("list" . "categorymembers") ("list" . "categorymembers")
("cmtitle" . ,prefixed-category) ("cmtitle" . ,prefixed-category)
("cmlimit" . "max") ("cmlimit" . "max")
("formatversion" . "2") ("formatversion" . "2")
("format" . "json"))))) ("format" . "json")))))
(log-outgoing dest-url) (log-outgoing dest-url)
(define dest-res (easy:get dest-url #:timeouts timeouts)) (define dest-res (easy:get dest-url #:timeouts timeouts))
(easy:response-json dest-res)) (easy:response-json dest-res))
(λ () (λ ()
(define dest-url (define dest-url
(format "~a/api.php?~a" (format "~a/api.php?~a"
origin origin
(params->query `(("action" . "parse") (params->query `(("action" . "parse")
("page" . ,prefixed-category) ("page" . ,prefixed-category)
("prop" . "text|headhtml|langlinks") ("prop" . "text|headhtml|langlinks")
("formatversion" . "2") ("formatversion" . "2")
("format" . "json"))))) ("format" . "json")))))
(log-outgoing dest-url) (log-outgoing dest-url)
(define dest-res (easy:get dest-url #:timeouts timeouts)) (define dest-res (easy:get dest-url #:timeouts timeouts))
(easy:response-json dest-res)) (easy:response-json dest-res))
(λ () (λ ()
(siteinfo-fetch wikiname)))) (siteinfo-fetch wikiname))))
(define title (preprocess-html-wiki (jp "/parse/title" page-data prefixed-category))) (define title (preprocess-html-wiki (jp "/parse/title" page-data prefixed-category)))
(define page-html (preprocess-html-wiki (jp "/parse/text" page-data ""))) (define page-html (preprocess-html-wiki (jp "/parse/text" page-data "")))
(define page (html->xexp page-html)) (define page (html->xexp page-html))
(define head-data ((head-data-getter wikiname) page-data)) (define head-data ((head-data-getter wikiname) page-data))
(define body (generate-results-page (define body (generate-results-page
#:req req #:req req
#:source-url source-url #:source-url source-url
#:wikiname wikiname #:wikiname wikiname
#:title title #:title title
#:members-data members-data #:members-data members-data
#:page page #:page page
#:head-data head-data #:head-data head-data
#:siteinfo siteinfo)) #:siteinfo siteinfo))
(when (config-true? 'debug) (when (config-true? 'debug)
; used for its side effects ; used for its side effects
; convert to string with error checking, error will be raised if xexp is invalid ; convert to string with error checking, error will be raised if xexp is invalid
(xexp->html body)) (xexp->html body))
(response/output (response/output
#:code 200 #:code 200
#:headers (build-headers always-headers) #:headers (build-headers always-headers)
(λ (out) (λ (out)
(write-html body out))))) (write-html body out)))])))
(module+ test (module+ test
(check-not-false ((query-selector (attribute-selector 'href "/test/wiki/Ankle_Monitor") (check-not-false ((query-selector (attribute-selector 'href "/test/wiki/Ankle_Monitor")
(generate-results-page (generate-results-page

View File

@ -104,50 +104,56 @@
(define (page-file req) (define (page-file req)
(response-handler (response-handler
(define wikiname (path/param-path (first (url-path (request-uri req))))) (cond
(define prefixed-title (path/param-path (caddr (url-path (request-uri req))))) [(config-true? 'feature_offline::only)
(define origin (format "https://~a.fandom.com" wikiname)) (response/output #:code 503
(define source-url (format "~a/wiki/~a" origin prefixed-title)) #:headers (build-headers always-headers)
(λ (out) (write-html '(p "Sorry, file pages are temporarily disabled. I hope to have them back soon.") out)))]
[else
(define wikiname (path/param-path (first (url-path (request-uri req)))))
(define prefixed-title (path/param-path (caddr (url-path (request-uri req)))))
(define origin (format "https://~a.fandom.com" wikiname))
(define source-url (format "~a/wiki/~a" origin prefixed-title))
(define-values (media-detail siteinfo) (define-values (media-detail siteinfo)
(thread-values (thread-values
(λ () (λ ()
(define dest-url (define dest-url
(format "~a/wikia.php?~a" (format "~a/wikia.php?~a"
origin origin
(params->query `(("format" . "json") ("controller" . "Lightbox") (params->query `(("format" . "json") ("controller" . "Lightbox")
("method" . "getMediaDetail") ("method" . "getMediaDetail")
("fileTitle" . ,prefixed-title))))) ("fileTitle" . ,prefixed-title)))))
(log-outgoing dest-url) (log-outgoing dest-url)
(define dest-res (easy:get dest-url #:timeouts timeouts)) (define dest-res (easy:get dest-url #:timeouts timeouts))
(easy:response-json dest-res)) (easy:response-json dest-res))
(λ () (λ ()
(siteinfo-fetch wikiname)))) (siteinfo-fetch wikiname))))
(if (not (jp "/exists" media-detail #f)) (if (not (jp "/exists" media-detail #f))
(next-dispatcher) (next-dispatcher)
(response-handler (response-handler
(define file-title (jp "/fileTitle" media-detail "")) (define file-title (jp "/fileTitle" media-detail ""))
(define title (define title
(if (non-empty-string? file-title) (format "File:~a" file-title) prefixed-title)) (if (non-empty-string? file-title) (format "File:~a" file-title) prefixed-title))
(define image-content-type (define image-content-type
(if (non-empty-string? (jp "/videoEmbedCode" media-detail "")) (if (non-empty-string? (jp "/videoEmbedCode" media-detail ""))
#f #f
(url-content-type (jp "/imageUrl" media-detail)))) (url-content-type (jp "/imageUrl" media-detail))))
(define body (define body
(generate-results-page #:req req (generate-results-page #:req req
#:source-url source-url #:source-url source-url
#:wikiname wikiname #:wikiname wikiname
#:title title #:title title
#:media-detail media-detail #:media-detail media-detail
#:image-content-type image-content-type #:image-content-type image-content-type
#:siteinfo siteinfo)) #:siteinfo siteinfo))
(when (config-true? 'debug) (when (config-true? 'debug)
; used for its side effects ; used for its side effects
; convert to string with error checking, error will be raised if xexp is invalid ; convert to string with error checking, error will be raised if xexp is invalid
(xexp->html body)) (xexp->html body))
(response/output #:code 200 (response/output #:code 200
#:headers (build-headers always-headers) #:headers (build-headers always-headers)
(λ (out) (write-html body out))))))) (λ (out) (write-html body out)))))])))
(module+ test (module+ test
(parameterize ([(config-parameter 'strict_proxy) "true"]) (parameterize ([(config-parameter 'strict_proxy) "true"])
(check-equal? (get-media-html "https://static.wikia.nocookie.net/a" "image/jpeg") (check-equal? (get-media-html "https://static.wikia.nocookie.net/a" "image/jpeg")

View File

@ -68,48 +68,54 @@
(define (page-search req) (define (page-search req)
;; this just means, catch any errors and display them in the browser. it's a function somewhere else ;; this just means, catch any errors and display them in the browser. it's a function somewhere else
(response-handler (response-handler
;; the URL will look like "/minecraft/wiki/Special:Search?q=Spawner" (cond
;; grab the first part to use as the wikiname, in this case, "minecraft" [(config-true? 'feature_offline::only)
(define wikiname (path/param-path (first (url-path (request-uri req))))) (response/output #:code 503
;; grab the part after ?q= which is the search terms #:headers (build-headers always-headers)
(define query (dict-ref (url-query (request-uri req)) 'q #f)) (λ (out) (write-html '(body (p "Sorry, full search is temporarily broken, but I have a plan to fix it.") (p "In the meantime, please use the popup search suggestions below the search box.")) out)))]
;; constructing the URL where I want to get fandom data from... [else
(define origin (format "https://~a.fandom.com" wikiname)) ;; the URL will look like "/minecraft/wiki/Special:Search?q=Spawner"
;; the dest-URL will look something like https://minecraft.fandom.com/api.php?action=query&list=search&srsearch=Spawner&formatversion=2&format=json ;; grab the first part to use as the wikiname, in this case, "minecraft"
(define dest-url (define wikiname (path/param-path (first (url-path (request-uri req)))))
(format "~a/api.php?~a" ;; grab the part after ?q= which is the search terms
origin (define query (dict-ref (url-query (request-uri req)) 'q #f))
(params->query `(("action" . "query") ;; constructing the URL where I want to get fandom data from...
("list" . "search") (define origin (format "https://~a.fandom.com" wikiname))
("srsearch" . ,query) ;; the dest-URL will look something like https://minecraft.fandom.com/api.php?action=query&list=search&srsearch=Spawner&formatversion=2&format=json
("formatversion" . "2") (define dest-url
("format" . "json"))))) (format "~a/api.php?~a"
origin
(params->query `(("action" . "query")
("list" . "search")
("srsearch" . ,query)
("formatversion" . "2")
("format" . "json")))))
;; simultaneously get the search results from the fandom API, as well as information about the wiki as a whole (its license, icon, name) ;; simultaneously get the search results from the fandom API, as well as information about the wiki as a whole (its license, icon, name)
(define-values (dest-res siteinfo) (define-values (dest-res siteinfo)
(thread-values (thread-values
(λ () (λ ()
(log-outgoing dest-url) (log-outgoing dest-url)
(easy:get dest-url #:timeouts timeouts)) ;; HTTP request to dest-url for search results (easy:get dest-url #:timeouts timeouts)) ;; HTTP request to dest-url for search results
(λ () (λ ()
(siteinfo-fetch wikiname)))) ;; helper function in another file to get information about the wiki (siteinfo-fetch wikiname)))) ;; helper function in another file to get information about the wiki
;; search results are a JSON string. parse JSON into racket data structures ;; search results are a JSON string. parse JSON into racket data structures
(define data (easy:response-json dest-res)) (define data (easy:response-json dest-res))
;; calling my generate-results-page function with the information so far in order to get a big fat x-expression ;; 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 ;; big fat x-expression goes into the body variable
(define body (generate-results-page req dest-url wikiname query data #:siteinfo siteinfo)) (define body (generate-results-page req dest-url wikiname query data #:siteinfo siteinfo))
;; error checking ;; error checking
(when (config-true? 'debug) (when (config-true? 'debug)
; used for its side effects ; used for its side effects
; convert to string with error checking, error will be raised if xexp is invalid ; convert to string with error checking, error will be raised if xexp is invalid
(xexp->html body)) (xexp->html body))
;; convert body to HTML and send to browser ;; convert body to HTML and send to browser
(response/output (response/output
#:code 200 #:code 200
#:headers (build-headers always-headers) #:headers (build-headers always-headers)
(λ (out) (λ (out)
(write-html body out))))) (write-html body out)))])))
(module+ test (module+ test
(parameterize ([(config-parameter 'feature_offline::only) "false"]) (parameterize ([(config-parameter 'feature_offline::only) "false"])
(check-not-false ((query-selector (attribute-selector 'href "/test/wiki/Gacha_Capsule") (check-not-false ((query-selector (attribute-selector 'href "/test/wiki/Gacha_Capsule")