X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Rakka%2FResource%2FSearch.hs;h=c46d40152c34f4ab25e8c43f8e2afe8c9af81e07;hb=cac96112c79075ff03dd38616a314dd293699170;hp=e4456e878387ef79e5bd414b1fe7c374bc4c8c7e;hpb=8d621fced0d1b3f66d38b16cd732fd2342622c5d;p=Rakka.git diff --git a/Rakka/Resource/Search.hs b/Rakka/Resource/Search.hs index e4456e8..c46d401 100644 --- a/Rakka/Resource/Search.hs +++ b/Rakka/Resource/Search.hs @@ -11,7 +11,9 @@ import Control.Arrow.ArrowList import Control.Arrow.ArrowTree import Control.Monad.Trans import Data.Maybe +import Data.Time import Network.HTTP.Lucu +import Network.HTTP.Lucu.RFC1123DateTime import Network.URI hiding (query, fragment) import Rakka.Environment import Rakka.Page @@ -19,6 +21,7 @@ import Rakka.Resource import Rakka.Storage import Rakka.SystemConfig import Rakka.Utils +import Rakka.W3CDateTime import Rakka.Wiki.Engine import System.FilePath import Text.HyperEstraier hiding (getText) @@ -55,7 +58,7 @@ maxSectionWindowSize = 10 to="5" total="5"> - + aaa foo bbb @@ -67,20 +70,26 @@ handleSearch env = do params <- getQueryForm let query = UTF8.decodeString $ fromMaybe "" $ lookup "q" params + order = fmap UTF8.decodeString (lookup "order" params) from = fromMaybe 0 $ fmap read $ lookup "from" params to = fromMaybe (from + resultsPerSection) $ fmap read $ lookup "to" params - cond <- liftIO $ mkCond query from to + cond <- liftIO $ mkCond query order from to result <- searchPages (envStorage env) cond let to' = min (from + length (srPages result)) to - runIdempotentA $ proc () + BaseURI baseURI <- getSysConf (envSysConf env) + runIdempotentA baseURI $ proc () -> do tree <- ( eelem "/" += ( eelem "searchResult" += sattr "query" query + += ( case order of + Just o -> sattr "order" o + Nothing -> none + ) += sattr "from" (show from) += sattr "to" (show to') += sattr "total" (show $ srTotal result) @@ -92,30 +101,40 @@ handleSearch env ) -< () returnA -< outputXmlPage' tree (searchResultToXHTML env) where - mkCond :: String -> Int -> Int -> IO Condition - mkCond query from to + mkCond :: String -> Maybe String -> Int -> Int -> IO Condition + mkCond query order from to = do cond <- newCondition setPhrase cond query + case order of + Just o -> setOrder cond o + Nothing -> return () setSkip cond from - setMax cond (to - from) + setMax cond (to - from + 1) return cond - mkPageElem :: ArrowXml a => a HitPage XmlTree + mkPageElem :: (ArrowChoice a, ArrowXml a, ArrowIO a) => a HitPage XmlTree mkPageElem = ( eelem "page" += attr "name" (arr hpPageName >>> mkText) + += attr "lastModified" ( arrIO (utcToLocalZonedTime . hpLastMod) + >>> + arr formatW3CDateTime + >>> + mkText + ) += ( arrL hpSnippet >>> mkSnippetTree ) ) - mkSnippetTree :: ArrowXml a => a SnippetFragment XmlTree + mkSnippetTree :: (ArrowChoice a, ArrowXml a) => a SnippetFragment XmlTree mkSnippetTree = proc fragment -> case fragment of - Boundary -> eelem "boundary" - NormalText t -> txt t - HighlightedWord w -> eelem "hit" += txt w - -<< () + Boundary -> eelem "boundary" -< () + NormalText t -> mkText -< t + HighlightedWord w -> ( eelem "hit" + += mkText + ) -< w searchResultToXHTML :: (ArrowXml a, ArrowChoice a, ArrowIO a) => Environment -> a XmlTree XmlTree @@ -129,9 +148,9 @@ searchResultToXHTML env let cssHref = [uriToString id (mkObjectURI baseURI styleSheet) ""] scriptSrc = [uriToString id (baseURI { uriPath = uriPath baseURI "js" }) ""] - pageTitle <- listA (readSubPage env) -< (Nothing, Nothing, "PageTitle") - leftSideBar <- listA (readSubPage env) -< (Nothing, Nothing, "SideBar/Left") - rightSideBar <- listA (readSubPage env) -< (Nothing, Nothing, "SideBar/Right") + pageTitle <- listA (readSubPage env) -< "PageTitle" + leftSideBar <- listA (readSubPage env) -< "SideBar/Left" + rightSideBar <- listA (readSubPage env) -< "SideBar/Right" ( eelem "/" += ( eelem "html" @@ -197,6 +216,11 @@ searchResultToXHTML env getText ) &&& + maybeA ( getXPathTreesInDoc "/searchResult/@order/text()" + >>> + getText + ) + &&& ( getXPathTreesInDoc "/searchResult/@from/text()" >>> getText @@ -208,11 +232,11 @@ searchResultToXHTML env >>> getText >>> - arr ((+ 1) . (`div` resultsPerSection) . read) + arr ((+ 1) . (`div` resultsPerSection) . (\ x -> x - 1) . read) ) ) >>> - ( ((> 1) . snd . snd) + ( ((> 1) . snd . snd . snd) `guardsP` formatPager baseURI ) @@ -241,7 +265,7 @@ searchResultToXHTML env uniqueNamespacesFromDeclAndQNames ) ) -<< tree where - formatItem :: (ArrowXml a, ArrowChoice a) => URI -> a XmlTree XmlTree + formatItem :: (ArrowXml a, ArrowChoice a, ArrowIO a) => URI -> a XmlTree XmlTree formatItem baseURI = ( eelem "div" += sattr "class" "searchResult" @@ -254,6 +278,19 @@ searchResultToXHTML env ) += (getAttrValue "name" >>> mkText) ) + += ( eelem "div" + += sattr "class" "date" + += ( getAttrValue "lastModified" + >>> + arr (zonedTimeToUTC . fromJust . parseW3CDateTime) + >>> + arrIO utcToLocalZonedTime + >>> + arr formatRFC1123DateTime + >>> + mkText + ) + ) += ( eelem "p" += ( getChildren >>> @@ -268,7 +305,7 @@ searchResultToXHTML env ) ) - formatPager :: (ArrowChoice a, ArrowXml a) => URI -> a (String, (Int, Int)) XmlTree + formatPager :: (ArrowChoice a, ArrowXml a) => URI -> a (String, (Maybe String, (Int, Int))) XmlTree formatPager baseURI = ( eelem "div" += sattr "class" "pager" @@ -277,13 +314,15 @@ searchResultToXHTML env &&& arr (fst . snd) &&& - ( arr snd + arr (fst . snd . snd) + &&& + ( arr (snd . snd) >>> mkSectionWindow ) ) >>> - proc (query, (currentSection, section)) + proc (query, (order, (currentSection, section))) -> if currentSection == section then ( txt " " <+> @@ -299,8 +338,8 @@ searchResultToXHTML env >>> uriToText ) - += (arr (show . snd) >>> mkText) - ) -< (query, section) + += (arr (show . snd . snd) >>> mkText) + ) -< (query, (order, section)) ) ) @@ -323,38 +362,31 @@ searchResultToXHTML env arrL id -< [begin .. end] - mkSectionURI :: Arrow a => URI -> a (String, Int) URI + mkSectionURI :: Arrow a => URI -> a (String, (Maybe String, Int)) URI mkSectionURI baseURI - = arr $ \ (query, section) + = arr $ \ (query, (order, section)) -> baseURI { - uriPath = uriPath baseURI "search" - , uriQuery = '?' : mkQueryString [ ("q" , query) - , ("from", show $ section * resultsPerSection) - , ("to" , show $ (section + 1) * resultsPerSection - 1) - ] + uriPath = uriPath baseURI "search.html" + , uriQuery = '?' : mkQueryString ( [ ("q" , query) + , ("from", show $ section * resultsPerSection) + , ("to" , show $ (section + 1) * resultsPerSection - 1) + ] + ++ + case order of + Just o -> [("order", o)] + Nothing -> [] + ) } uriToText :: ArrowXml a => a URI XmlTree uriToText = arr (\ uri -> uriToString id uri "") >>> mkText - mkQueryString :: [(String, String)] -> String - mkQueryString [] = "" - mkQueryString ((k, v) : xs) = encode k ++ "=" ++ encode v ++ - if xs == [] then - "" - else - ';' : mkQueryString(xs) - - encode :: String -> String - encode = escapeURIString isSafeChar . UTF8.encodeString - +-- FIXME: localize readSubPage :: (ArrowXml a, ArrowChoice a, ArrowIO a) => - Environment - -> a (Maybe PageName, Maybe XmlTree, PageName) XmlTree + Environment -> a PageName XmlTree readSubPage env - = proc (mainPageName, mainPage, subPageName) -> + = proc (subPageName) -> do subPage <- getPageA (envStorage env) >>> arr fromJust >>> xmlizePage -< (subPageName, Nothing) - subXHTML <- makeSubXHTML (envStorage env) (envSysConf env) (envInterpTable env) - -< (mainPageName, mainPage, subPage) + subXHTML <- makeSubXHTML (envStorage env) (envSysConf env) (envInterpTable env) -< (Nothing, Nothing, subPage) returnA -< subXHTML