Commits

Luke Plant committed b9b31a4

Implemented rendering of arrays to PHP file

Comments (0)

Files changed (3)

 import qualified DB
 import qualified Formats
 import Database.HDBC
-import List (sortBy)
+import List (sortBy, intersperse)
 import qualified Data.Map as Map
 import Data.Maybe (fromJust)
 import Data.Ord (comparing)
 
 utf8 = UTF8.fromString
 
+makePHPMap amap = "array(" ++
+                  (concat $ intersperse ",\n" $ map mkPair $ Map.toList amap)
+                  ++ ")"
+    where mkPair (a,b) = (show a) ++ " => " ++ (show b)
+
 createRedirectFile postUrlMap categoryUrlMap = do
     tpl <- readTemplate Settings.redirect_file_template
-    let ctx = Map.fromList ([(utf8 "postIdsToUrls", utf8 "test1"),
-                             (utf8 "categoryIdsToUrls", utf8 "test2")])
+    let ctx = Map.fromList ([(utf8 "postIdsToUrls", utf8 $ makePHPMap postUrlMap),
+                             (utf8 "categoryIdsToUrls", utf8 $ makePHPMap categoryUrlMap)])
     renderToFile Settings.redirect_file_output tpl ctx
 
+-- TODO - a better way of generating this, something like Routes
+makePostUrl p = Settings.root_url ++ "posts/" ++ (P.slug p) ++ "/"
+makeCategoryUrl c = Settings.root_url ++ "categories/" ++ (C.slug c) ++ "/"
+
 main = handleSqlError $ do
   cn <- DB.connect
-  cats <- readCategories
-  writeItems cn addCategory cats
+  origCats <- readCategories
+  newCats <- writeItems cn addCategory origCats
   origPosts <- readPosts
   newPosts <- writeItems cn addPost origPosts
   -- we need the new IDs to rewrite comments tables
   let postCategories = correctIds postCategories' id_map
   writeItems cn addPostCategory postCategories
 
-  createRedirectFile () ()
+  let postUrlMap = Map.fromList $ zip (map (show . P.id) origPosts) (map makePostUrl newPosts)
+  let categoryUrlMap = Map.fromList $ zip (map (show . C.id) origCats) (map makeCategoryUrl newCats)
+  createRedirectFile postUrlMap categoryUrlMap
   commit cn
   return ()
 
   // template language uses dollar substitutions, and dollar signs
   // must be escaped by doubling.
 
-$$postMap = array($postIdsToUrls);
+$$postMap = ${postIdsToUrls};
 
-$$catMap = array($categoryIdsToUrls);
+$$catMap = ${categoryIdsToUrls};
 
 
-?>
+?>
 module Settings where
 
 sqlite_path = "/home/luke/httpd/lukeplant.me.uk/web/cgi-bin/data/test1.db"
-
+root_url = "/blog/"
 
 
 -- Migration time settings: