X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=Wiki.hs;h=337d715eb9160b83cd94e2fa2549de67e09fd6da;hb=411547157fb221f10c8620e2a92efe8e5219e82d;hp=c4b7166f377a508ac021b55fcedfb1e024733269;hpb=e36bf63ff310b307b1f62f86fc87307e18c4677a;p=yawbih.git diff --git a/Wiki.hs b/Wiki.hs index c4b7166..337d715 100644 --- a/Wiki.hs +++ b/Wiki.hs @@ -1,20 +1,132 @@ module Wiki ( + Backend (getCurrent,getList,get,setCurrent,update) + ,PGB + ,createPGB + ,Markup (Text, Paragraph, Link) + ,Document + ,wikiParser ) where -class WikiBackend a where - --Keyword -> Full text - getCurrent :: a -> String -> IO String +import Dbconnect +import Data.Char +import Text.ParserCombinators.Parsec - --Keyword -> [(id,date)] - getList :: a -> String -> IO [(Int, String)] +class Backend a where + --Keyword -> (Full text,date) + getCurrent :: a -> String -> IO (Maybe (String,String)) + + --Keyword -> [(id,date,author,comment)] + getList :: a -> String -> IO [(String, String, String, String)] --Keyword -> id -> Full text - get :: a -> String -> Int -> IO String + get :: a -> String -> String -> IO String --Keyword -> id -> () - setCurrent :: a -> String -> Int -> IO () + setCurrent :: a -> String -> String -> IO Bool --Keyword -> Full text -> id - addNew :: String -> String -> IO Int + update :: a -> String -> String -> String -> String -> IO String + +data Markup = Text String + | Paragraph + | Link String String + +type Document = [Markup] + +wikiParser :: GenParser Char st Document +wikiParser = do + s <- (try (pPara) + <|> pSpace + <|> try (pLinkLong) + <|> try (pLink) + <|> pOtherChar + <|> pText) + ss <- (wikiParser <|> return []) + return (s:ss) + +pPara = do + pEol + pEol + return (Paragraph) + + +pSpace = do + space + return (Text " ") + +pEol = char '\n' <|> do + char '\r' + char '\n' + +pLinkLong = do + string "[[" + l <- many1 $ noneOf ['|'] + char '|' + d <- many1 $ noneOf "]" + string "]]" + return (Link l d) +pLink = do + string "[[" + l <- many1 $ noneOf "]" + string "]]" + return (Link l l) +pOtherChar = do + c <- oneOf ",;.:!?[]()\'\"=-%$£<>/\\|" + return (Text (c:[])) + +pText = do + t <- many1 alphaNum--(noneOf ['\n','\r','[',']']) + return (Text t) + + +newtype PGB = PGB DBService + +createPGB :: String -> String -> String -> String -> IO PGB +createPGB host database user password = let db = createDBService host database "" user password Nothing in return (PGB db) + + +testDB = createPGB "wave" "wiki" "wiki" "12wiki34db" + +instance Backend PGB where + + getCurrent (PGB db) key = do + result <- selectReturnTuples db $ "SELECT fulltext,timestamp FROM curtexts WHERE keyword="++tov key + case result of + [text,date]:_ -> return (Just (text,date)) + _ -> return Nothing + + getList (PGB db) key = do + list <- selectReturnTuples db $ "SELECT id, timestamp, author, comment from fulltexts WHERE keyword = "++tov key + return $ map (\[id,date,author,comment] -> (id,date,author,comment)) list + + get (PGB db) key id = return "" + + setCurrent (PGB db) key id = do + full <- selectReturnTuples db $ "SELECT keyword FROM fulltexts WHERE keyword="++tov key++" AND id='"++id++"'" + cur <- selectReturnTuples db $ "SELECT keyword FROM curtexts WHERE keyword="++tov key + case full of + [[]] -> do + return False + _ -> do + rows <- case cur of + [[]] -> do + execute db $ "INSERT INTO current (keyword, id) VALUES ("++tov key++","++tov id++")" + _ -> do + execute db $ "UPDATE current SET id = "++tov id++" WHERE keyword = "++tov key + if rows == 1 then return True + else return False + + update (PGB db) key text author comment = do + rows <- execute db $ "INSERT INTO fulltexts (keyword,fulltext, author, comment) VALUES ("++tov key++","++tov text++","++tov author++","++tov comment++")" + if rows == 0 then return "" + else do + [[id]] <- selectReturnTuples db $ "SELECT currval('fulltexts_id_seq')" + setCurrent (PGB db) key id + return id + + +tov :: String -> String +tov s = '\'':escapeQuery s++"'" +