]> gitweb @ CieloNegro.org - Rakka.git/blobdiff - Rakka/Resource/Search.hs
/search.xml should contain lastMod
[Rakka.git] / Rakka / Resource / Search.hs
index af90bfa3844395a6d70392767a07c7fe130c36c1..20f51c6540931d79ae3c1cae1e1059d5f331a79d 100644 (file)
@@ -11,6 +11,7 @@ import           Control.Arrow.ArrowList
 import           Control.Arrow.ArrowTree
 import           Control.Monad.Trans
 import           Data.Maybe
+import           Data.Time
 import           Network.HTTP.Lucu
 import           Network.URI hiding (query, fragment)
 import           Rakka.Environment
@@ -19,9 +20,10 @@ 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
+import           Text.HyperEstraier hiding (getText)
 import           Text.XML.HXT.Arrow.Namespace
 import           Text.XML.HXT.Arrow.XmlArrow
 import           Text.XML.HXT.Arrow.XmlNodeSet
@@ -41,8 +43,12 @@ resSearch env
       }
 
 
-defaultResultsPerPage :: Int
-defaultResultsPerPage = 20
+resultsPerSection :: Int
+resultsPerSection = 10
+
+
+maxSectionWindowSize :: Int
+maxSectionWindowSize = 10
 
 
 {-
@@ -51,7 +57,7 @@ defaultResultsPerPage = 20
                 to="5"
                 total="5">
 
-    <page name="Page/1">
+    <page name="Page/1" lastModified="2000-01-01T00:00:00">
       aaa <hit>foo</hit> bbb
     </page>
 
@@ -65,7 +71,7 @@ handleSearch env
          let query = UTF8.decodeString $ fromMaybe ""  $ lookup "q" params
              from  = fromMaybe 0
                      $ fmap read $ lookup "from" params
-             to    = fromMaybe defaultResultsPerPage
+             to    = fromMaybe (from + resultsPerSection)
                      $ fmap read $ lookup "to"   params
 
          cond   <- liftIO $ mkCond query from to
@@ -73,7 +79,8 @@ handleSearch env
 
          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
@@ -96,22 +103,29 @@ handleSearch env
                setMax    cond (to - from)
                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
@@ -186,29 +200,32 @@ searchResultToXHTML env
                                    )
                                 += ( getXPathTreesInDoc "/searchResult/page"
                                      >>>
-                                     eelem "div"
-                                     += sattr "class" "searchResult"
-                                     += ( eelem "a"
-                                          += attr "href" ( getAttrValue "name"
-                                                           >>>
-                                                           arr (\ x -> uriToString id (mkPageURI baseURI x) "")
-                                                           >>>
-                                                           mkText
-                                                         )
-                                          += (getAttrValue "name" >>> mkText)
-                                        )
-                                     += ( eelem "p"
-                                          += ( getChildren
-                                               >>>
-                                               choiceA [ isText             :-> this
-                                                       , hasName "boundary" :-> txt " ... "
-                                                       , hasName "hit"      :-> ( eelem "span"
-                                                                                  += sattr "class" "highlighted"
-                                                                                  += getChildren
-                                                                                )
-                                                       ]
-                                             )
-                                        )
+                                     formatItem baseURI
+                                   )
+                                += ( ( ( getXPathTreesInDoc "/searchResult/@query/text()"
+                                         >>>
+                                         getText
+                                       )
+                                       &&&
+                                       ( getXPathTreesInDoc "/searchResult/@from/text()"
+                                         >>>
+                                         getText
+                                         >>>
+                                         arr ((`div` resultsPerSection) . read)
+                                       )
+                                       &&&
+                                       ( getXPathTreesInDoc "/searchResult/@total/text()"
+                                         >>>
+                                         getText
+                                         >>>
+                                         arr ((+ 1) . (`div` resultsPerSection) . read)
+                                       )
+                                     )
+                                     >>>
+                                     ( ((> 1) . snd . snd)
+                                       `guardsP`
+                                       formatPager baseURI
+                                     )
                                    )
                               )
                          )
@@ -233,6 +250,113 @@ searchResultToXHTML env
                  >>>
                  uniqueNamespacesFromDeclAndQNames
                ) ) -<< tree
+    where
+      formatItem :: (ArrowXml a, ArrowChoice a) => URI -> a XmlTree XmlTree
+      formatItem baseURI
+          = ( eelem "div"
+              += sattr "class" "searchResult"
+              += ( eelem "a"
+                   += attr "href" ( getAttrValue "name"
+                                    >>>
+                                    arr (\ x -> uriToString id (mkPageURI baseURI x) "")
+                                    >>>
+                                    mkText
+                                  )
+                   += (getAttrValue "name" >>> mkText)
+                 )
+              += ( eelem "p"
+                   += ( getChildren
+                        >>>
+                        choiceA [ isText             :-> this
+                                , hasName "boundary" :-> txt " ... "
+                                , hasName "hit"      :-> ( eelem "span"
+                                                           += sattr "class" "highlighted"
+                                                           += getChildren
+                                                         )
+                                ]
+                      )
+                 )
+            )
+
+      formatPager :: (ArrowChoice a, ArrowXml a) => URI -> a (String, (Int, Int)) XmlTree
+      formatPager baseURI
+          = ( eelem "div"
+              += sattr "class" "pager"
+              += txt "Page."
+              += ( ( arr fst
+                     &&&
+                     arr (fst . snd)
+                     &&&
+                     ( arr snd
+                       >>>
+                       mkSectionWindow
+                     )
+                   )
+                   >>>
+                   proc (query, (currentSection, section))
+                       -> if currentSection == section then
+                              ( txt " "
+                                <+> 
+                                eelem "span"
+                                += sattr "class" "currentSection"
+                                += (arr show >>> mkText)
+                              ) -< section
+                          else
+                              ( txt " "
+                                <+>
+                                eelem "a"
+                                += attr "href" ( mkSectionURI baseURI
+                                                 >>>
+                                                 uriToText
+                                               )
+                                += (arr (show . snd) >>> mkText)
+                              ) -< (query, section)
+                 )
+            )
+
+      mkSectionWindow :: ArrowList a => a (Int, Int) Int
+      mkSectionWindow 
+          = proc (currentSection, totalSections)
+          -> let windowWidth  = min maxSectionWindowSize totalSections
+                 windowBegin  = currentSection - (windowWidth `div` 2)
+                 (begin, end) = if windowBegin < 0 then
+                                    -- 左に溢れた
+                                    (0, windowWidth - 1)
+                                else
+                                    if windowBegin + windowWidth >= totalSections then
+                                        -- 右に溢れた
+                                        (totalSections - windowWidth, totalSections - 1)
+                                    else
+                                        -- どちらにも溢れない
+                                        (windowBegin, windowBegin + windowWidth - 1)
+             in
+               arrL id -< [begin .. end]
+                       
+
+      mkSectionURI :: Arrow a => URI -> a (String, Int) URI
+      mkSectionURI baseURI
+          = arr $ \ (query, section)
+          -> baseURI {
+               uriPath  = uriPath baseURI </> "search"
+             , uriQuery = '?' : mkQueryString [ ("q"   , query)
+                                              , ("from", show $ section * resultsPerSection)
+                                              , ("to"  , show $ (section + 1) * resultsPerSection - 1)
+                                              ]
+             }
+
+      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
 
 
 readSubPage :: (ArrowXml a, ArrowChoice a, ArrowIO a) =>