2 Backend (getCurrent,getList,get,setCurrent,update)
5 ,Markup (Text, Paragraph, Link, Bold, Emph, Heading, Url)
13 import Text.ParserCombinators.Parsec
16 --Keyword -> (Full text,date)
17 getCurrent :: a -> String -> IO (Maybe (String,String))
19 --Keyword -> [(id,date,author,comment)]
20 getList :: a -> String -> IO [(String, String, String, String)]
22 --Keyword -> id -> Full text
23 get :: a -> String -> String -> IO (Maybe String)
26 setCurrent :: a -> String -> String -> IO Bool
28 --Keyword -> Full text -> id
29 update :: a -> String -> String -> String -> String -> IO String
31 data Markup = Text String
36 | Heading Int [Markup]
39 type Document = [Markup]
41 wikiParser :: GenParser Char st Document
44 ss <- (wikiParser <|> return [])
57 pEol = char '\n' <|> do
63 l <- many1 $ noneOf ['|']
65 d <- many1 $ noneOf "]"
71 l <- many1 $ noneOf "]"
77 s <- pStringParser "**"
82 s <- pStringParser "//"
86 try (string xs >> return []) <|> (do
88 ss <- pStringParser xs
93 level <- many1 $ char '='
95 s <- pStringParser (' ':level)
96 return (Heading (length level) s)
101 s <- many1 (alphaNum <|> oneOf "?.:&-/")
102 return (Url (proto++"://"++s))
104 pMain = try (pHeading)
116 c <- oneOf "*,;.:!?[]()'\"=-%$£<>/\\|"
120 t <- many1 alphaNum--(noneOf ['\n','\r','[',']'])
124 newtype PGB = PGB DBService
126 createPGB :: String -> String -> String -> String -> IO PGB
127 createPGB host database user password = let db = createDBService host database "" user password Nothing in return (PGB db)
130 testDB = createPGB "wave" "wiki" "wiki" "12wiki34db"
132 instance Backend PGB where
134 getCurrent (PGB db) key = do
135 result <- selectReturnTuples db $ "SELECT fulltext,timestamp FROM curtexts WHERE keyword="++tov key
137 [text,date]:_ -> return (Just (text,date))
140 getList (PGB db) key = do
141 list <- selectReturnTuples db $ "SELECT id, timestamp, author, comment from fulltexts WHERE keyword = "++tov key
142 return $ map (\[id,date,author,comment] -> (id,date,author,comment)) list
144 get (PGB db) key id = do
145 list <- selectReturnTuples db $ "SELECT fulltext from fulltexts WHERE id = "++tov id
147 [s]:_ -> return (Just s)
150 setCurrent (PGB db) key id = do
151 full <- selectReturnTuples db $ "SELECT keyword FROM fulltexts WHERE keyword="++tov key++" AND id='"++id++"'"
152 cur <- selectReturnTuples db $ "SELECT keyword FROM curtexts WHERE keyword="++tov key
159 execute db $ "INSERT INTO current (keyword, id) VALUES ("++tov key++","++tov id++")"
161 execute db $ "UPDATE current SET id = "++tov id++" WHERE keyword = "++tov key
162 if rows == 1 then return True
165 update (PGB db) key text author comment = do
166 rows <- execute db $ "INSERT INTO fulltexts (keyword,fulltext, author, comment) VALUES ("++tov key++","++tov text++","++tov author++","++tov comment++")"
167 if rows == 0 then return ""
169 [[id]] <- selectReturnTuples db $ "SELECT currval('fulltexts_id_seq')"
170 setCurrent (PGB db) key id
174 tov :: String -> String
175 tov s = '\'':escapeQuery s++"'"