Permalink
Browse files

Static pages handling refactored with the new Lazy_tree

  • Loading branch information...
1 parent e24ec9d commit cacee163909542d9394e4201163d94bf74cc1340 @pdonadeo committed Nov 3, 2010
Showing with 40 additions and 69 deletions.
  1. +3 −6 src/lib/data_mapping.ml
  2. +13 −43 src/lib/db.ml
  3. +11 −10 src/views/common_fragments.ml
  4. +13 −10 src/views/pages.ml
@@ -3,6 +3,7 @@ TYPE_CONV_PATH "Data_mapping"
open BatStd
open BatMap
open Utilities
+open Lazy_tree
let (-->) = StringMap.Infix.(-->)
let (<--) = StringMap.Infix.(<--)
@@ -166,19 +167,15 @@ let map_of_post p =
type page =
{
- page_id : int;
- page_parent_id : int option;
- page_parent : (page Lazy.t) option;
- page_position : int;
- page_children : page list;
page_mtime : float;
page_title : string option;
page_url_title : string;
page_template : string;
page_password : string option;
}
-type page_db = page list * page list (* the list of roots, actually a tree; the flat list of all pages *)
+type page_db = (page, [`complete]) tree * (page, [`complete]) tree_node list
+ (* the list of roots, actually a tree; the flat list of all pages *)
type inet_addr = Unix.inet_addr
type str = string with sexp;;
View
@@ -552,54 +552,24 @@ let get_raw_data () =
List.map
(
fun record ->
- {
- page_id = !!(record-->"page_id") |> int_of_string;
- page_parent_id = (match record-->"parent_page" with None -> None | Some id -> Some (int_of_string id));
- page_parent = None; (* PLACEHOLDER *)
- page_position = !!(record-->"position") |> int_of_string;
- page_children = []; (* PLACEHOLDER *)
- page_mtime = !!(record-->"mtime") |> Netdate.parse_epoch;
- page_title = record-->"title";
- page_url_title = !!(record-->"url_title");
- page_template = !!(record-->"template");
- page_password = record-->"password";
- }
+ let id = !!(record-->"page_id") |> int_of_string in
+ let parent_id = (match record-->"parent_page" with None -> None | Some id -> Some (int_of_string id)) in
+ let pos = !!(record-->"position") |> int_of_string in
+ let data = {
+ page_mtime = !!(record-->"mtime") |> Netdate.parse_epoch;
+ page_title = record-->"title";
+ page_url_title = !!(record-->"url_title");
+ page_template = !!(record-->"template");
+ page_password = record-->"password";
+ } in
+ Lazy_tree.new_node ~id ~parent_id ~pos ~data
)
(get_all_records ())
-let build_pages_tree (raw_data : page list) : page list =
- let rec aux (raw_data : page list)
- (parent : page Lazy.t option)
- (parent_id : int option) =
- let children, rest =
- List.partition (fun el -> if el.page_parent_id = parent_id then true else false) raw_data in
-
- let children, rest =
- List.fold_left
- (
- fun (new_children, rest) page ->
- let rec new_page = lazy (
- let complete_children, rest = aux rest (Some new_page) (Some page.page_id) in
- { page with
- page_parent = parent;
- page_children = complete_children;
- }
- ) in
- (Lazy.force new_page)::new_children, rest
- )
- ([], rest)
- children in
- List.rev children, rest
- in (* END of aux *)
- aux raw_data None None |> fst
-
-let rec flat_pages (tree : page list) : page list =
- List.fold_left (fun acc page -> acc @ [page] @ (flat_pages page.page_children)) [] tree
-
let page_db : Data_mapping.page_db Lazy.t =
lazy (
- let tree = get_raw_data () |> build_pages_tree in
- let flat = flat_pages tree in
+ let tree = get_raw_data () |> Lazy_tree.build_tree in
+ let flat = Lazy_tree.flat_tree tree in
tree, flat
)
@@ -192,48 +192,49 @@ let page_menu =
let hd, tl = List.hd roots, List.tl roots in
X.div ~a:[X.a_id "main-nav"; X.a_class "col"] [
X.ul (X.li ~a:[X.a_class "page_item"] [
- X.a ~a:[X.a_rel [`bookmark]; X.a_title !!(hd.page_title); X.a_href (_u hd.page_url_title)]
- [ X.pc !!(hd.page_title) ]
+ X.a ~a:[X.a_rel [`bookmark]; X.a_title !!((Lazy_tree.data hd).page_title); X.a_href (_u (Lazy_tree.data hd).page_url_title)]
+ [ X.pc !!((Lazy_tree.data hd).page_title) ]
] )
(List.map (fun li -> X.li ~a:[X.a_class "page_item"] [
X.a ~a:[X.a_rel [`bookmark]; X.a_title !!(li.page_title); X.a_href (_u li.page_url_title)]
[ X.pc !!(li.page_title) ]
]
- ) tl)
+ ) (List.map Lazy_tree.data tl))
]
end
| Some id -> begin
let current_page = List.find
- (fun p -> if p.page_id = id then true else false)
+ (fun p -> if Lazy_tree.id p = id then true else false)
(Lazy.force Db.page_db |> snd) in
- let page_parent_id = current_page.page_parent_id in
+ let page_parent_id = Lazy_tree.parent_id current_page in
let li_list = if page_parent_id = None
then []
else
- let parent_page = Lazy.force (!!(current_page.page_parent)) in
+ let parent_page = !!(Lazy_tree.parent current_page) in
[
X.li ~a:[X.a_class "page_item"] [
- X.a ~a:[X.a_title "Go up"; X.a_href (_u parent_page.page_url_title)]
+ X.a ~a:[X.a_title "Go up"; X.a_href (_u (Lazy_tree.data parent_page).page_url_title)]
[ X.pc "Go up" ]
]
] in
let li_list = li_list @ [
- X.li ~a:[X.a_class "page_item current_page_item"] [ X.pc !!(current_page.page_title) ]
+ X.li ~a:[X.a_class "page_item current_page_item"] [ X.pc !!((Lazy_tree.data current_page).page_title) ]
] in
let li_list = li_list @ (List.map
(
fun p ->
+ let p = Lazy_tree.data p in
X.li ~a:[X.a_class "page_item"] [
X.a ~a:[X.a_title !!(p.page_title); X.a_href (_u p.page_url_title)] [ X.pc !!(p.page_title) ]
]
- ) current_page.page_children) in
+ ) (Lazy_tree.children current_page)) in
X.div ~a:[X.a_id "main-nav"; X.a_class "col"] [
X.ul (List.hd li_list) (List.tl li_list)
]
end
in (* END OF page_menu_aux *)
List.fold_left
- (fun map page -> let id = Some page.page_id in (id, page_menu_aux id)::map)
+ (fun map page -> let id = Some (Lazy_tree.id page) in (id, page_menu_aux id)::map)
[ (None, page_menu_aux None) ]
(Lazy.force Db.page_db |> snd)
) in (* menu_map *)
View
@@ -11,7 +11,7 @@ let (<--) = Template.Context.(<--)
let pages_regexps = lazy (
let _, pages = Lazy.force Db.page_db in
List.fold_left
- (fun acc page -> (Pcre.regexp ("^" ^ (page.page_url_title) ^ "/?$"), page)::acc)
+ (fun acc page -> (Pcre.regexp ("^" ^ ((Lazy_tree.data page).page_url_title) ^ "/?$"), page)::acc)
[]
pages |> List.rev
)
@@ -26,7 +26,7 @@ let selector script_name =
with Not_found -> false
)
(Lazy.force pages_regexps) |> snd in
- Some ("", BatMap.StringMap.add "page_id" (string_of_int page.page_id) BatMap.StringMap.empty)
+ Some ("", BatMap.StringMap.add "page_id" (string_of_int (Lazy_tree.id page)) BatMap.StringMap.empty)
with
| Not_found -> None
@@ -40,9 +40,11 @@ class ['g, 'p] page cgi : ['g, 'p] Types.service =
inherit ['g, 'p] view_base cgi
method private render_page ?(cache = `No_cache) ?(headers = []) page auth_ok error =
- ctx<--("top_menu", Tstr (X.string_of_element (page_menu (Some page.page_id))));
+ let page_id = Lazy_tree.id page in
+ let page = Lazy_tree.data page in
+ ctx<--("top_menu", Tstr (X.string_of_element (page_menu (Some page_id))));
ctx<--("page_title", Tstr !!(page.page_title));
- ctx<--("page_id", Tint page.page_id);
+ ctx<--("page_id", Tint page_id);
ctx<--("page_url", Tstr page.page_url_title);
ctx<--("js_object_name", Tstr "pages");
ctx<--("auth_ok", Tbool auth_ok);
@@ -62,23 +64,24 @@ class ['g, 'p] page cgi : ['g, 'p] Types.service =
self#old_browser_warning ();
self#include_google_analytics ();
let current_page = List.find
- (fun p -> if p.page_id = id then true else false)
+ (fun p -> if (Lazy_tree.id p) = id then true else false)
(Lazy.force Db.page_db |> snd) in
+ let current_page' = Lazy_tree.data current_page in
let headers, cache =
- match current_page.page_password with
+ match current_page'.page_password with
| Some _ -> [], `No_cache
- | None -> [ `Last_Modified current_page.page_mtime ], `Unspecified in
+ | None -> [ `Last_Modified current_page'.page_mtime ], `Unspecified in
let auth_ok =
- match current_page.page_password, self#get_session () with
+ match current_page'.page_password, self#get_session () with
| Some _, Blog_session.Anonymous (l, _) when (not_in_list l id) -> false
| _, _ -> true in
self#render_page ~headers ~cache current_page auth_ok false
method post ((id, password) : 'p) =
let page = List.find
- (fun p -> if p.page_id = id then true else false)
+ (fun p -> if (Lazy_tree.id p) = id then true else false)
(Lazy.force Db.page_db |> snd) in
- if page.page_password = Some password
+ if (Lazy_tree.data page).page_password = Some password
then
let new_session =
match self#get_session () with

0 comments on commit cacee16

Please sign in to comment.