From 5fb7301ce1bc29c4bdb4e2be342daa19f29674d8 Mon Sep 17 00:00:00 2001 From: blankie Date: Sat, 8 Oct 2022 15:35:35 +0700 Subject: [PATCH] Add support for File: pages Fixes https://lists.sr.ht/~cadence/breezewiki-discuss/%3Cb2835a70-5118-4df0-90c9-4333486a4b69%40nixnetmail.com%3E --- src/dispatcher-tree.rkt | 1 + src/page-file.rkt | 117 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 src/page-file.rkt diff --git a/src/dispatcher-tree.rkt b/src/dispatcher-tree.rkt index f2c1412..b68cf9c 100644 --- a/src/dispatcher-tree.rkt +++ b/src/dispatcher-tree.rkt @@ -45,6 +45,7 @@ (pathprocedure:make "/proxy" (hash-ref ds 'page-proxy)) (pathprocedure:make "/search" (hash-ref ds 'page-global-search)) (filter:make (pregexp (format "^/~a/wiki/Category:.+$" px-wikiname)) (lift:make (hash-ref ds 'page-category))) + (filter:make (pregexp (format "^/~a/wiki/File:.+$" px-wikiname)) (lift:make (hash-ref ds 'page-file))) (filter:make (pregexp (format "^/~a/wiki/.+$" px-wikiname)) (lift:make (hash-ref ds 'page-wiki))) (filter:make (pregexp (format "^/~a/search$" px-wikiname)) (lift:make (hash-ref ds 'page-search))) (filter:make (pregexp (format "^/~a(/(wiki(/)?)?)?$" px-wikiname)) (lift:make (hash-ref ds 'redirect-wiki-home))) diff --git a/src/page-file.rkt b/src/page-file.rkt new file mode 100644 index 0000000..24dd5ae --- /dev/null +++ b/src/page-file.rkt @@ -0,0 +1,117 @@ +#lang racket/base +(require racket/dict + racket/list + racket/match + racket/string + (prefix-in easy: net/http-easy) + ; html libs + html-parsing + 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" + "config.rkt" + "data.rkt" + "page-wiki.rkt" + "syntax.rkt" + "url-utils.rkt" + "xexpr-utils.rkt") + +(provide + page-file) + +;(module+ test +; (require rackunit) +; (define category-json-data +; '#hasheq((batchcomplete . #t) (continue . #hasheq((cmcontinue . "page|4150504c45|41473") (continue . "-||"))) (query . #hasheq((categorymembers . (#hasheq((ns . 0) (pageid . 25049) (title . "Item (entity)")) #hasheq((ns . 0) (pageid . 128911) (title . "3D")) #hasheq((ns . 0) (pageid . 124018) (title . "A Very Fine Item")) #hasheq((ns . 0) (pageid . 142208) (title . "Amethyst Shard")) #hasheq((ns . 0) (pageid . 121612) (title . "Ankle Monitor"))))))))) + +(define (generate-results-page + #:source-url source-url + #:wikiname wikiname + #:title title + #:media-detail media-detail + #:license [license #f]) + (define video-embed-code (jp "/videoEmbedCode" media-detail "")) + (define raw-image-url (jp "/rawImageUrl" media-detail)) + (define image-url (jp "/imageUrl" media-detail raw-image-url)) + (define username (jp "/userName" media-detail)) + (define user-page-url (jp "/userPageUrl" media-detail)) + (define is-posted-in (jp "/isPostedIn" media-detail #f)) + (define smaller-article-list (jp "/smallerArticleList" media-detail)) + (define article-list-is-smaller (jp "/articleListIsSmaller" media-detail)) + (define image-description (jp "/imageDescription" media-detail #f)) + (generate-wiki-page + #:source-url source-url + #:wikiname wikiname + #:title title + #:license license + `(div + (p + ,(if (non-empty-string? video-embed-code) + `(span (a (@ (href (u-proxy-url raw-image-url))) "View original file") ". ") + `(return-no-element)) + "Added by " + (a (@ (href (u-proxy-url user-page-url))) username) + "." + ,(if is-posted-in + `(span " Posted in " + ,@(map + (λ (article) + (define page-path (jp "/title" result)) + (define title (jp "/titleText" result page-path)) + `(a (@ (href ,(format "/~a/wiki/~a" wikiname page-path))) + ,title)) + smaller-article-list)) + ,(if (= article-list-is-smaller 1) + "…" + ".")))) +; ,(update-tree-wiki image-description wikiname) + )) + +(define (page-file req) + (response-handler + (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)) + + (thread-let + ([media-detail (define dest-url + (format "~a/wikia.php?~a" + origin + (params->query `(("format" . "json") + ("controller" . "Lightbox") + ("method" . "getMediaDetail") + ("fileTitle" . prefixed-title))))) + (log-outgoing dest-url) + (define dest-res (easy:get dest-url #:timeouts timeouts)) + (easy:response-json dest-res)] + [license (license-auto wikiname)]) + + (define title (jp "/fileTitle" page-data media-detail))) + (define body (generate-results-page + #:source-url source-url + #:wikiname wikiname + #:title title + #:media-detail media-detail + #:license license)) + + (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)) + (response/output + #:code 200 + (λ (out) + (write-html body out)))))) +;(module+ test +; (check-not-false ((query-selector (attribute-selector 'href "/test/wiki/Ankle_Monitor") +; (generate-results-page +; #:source-url "" +; #:wikiname "test" +; #:title "Category:Items" +; #:members-data category-json-data +; #:page '(div "page text"))))))