Logging in and logging out.
[cocanwiki.git] / scripts / pagestyle.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: pagestyle.ml,v 1.2 2004/09/07 13:40:10 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Cocanwiki
13
14 let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) _ =
15   let page = q#param "page" in
16   let version =
17     try Some (int_of_string (q#param "version")) with Not_found -> None in
18
19   (* Get the CSS. *)
20   let sth =
21     match version with
22         None ->
23           let sth = dbh#prepare_cached
24                       "select css from pages where hostid = ? and url = ?" in
25           sth#execute [`Int hostid; `String page];
26           sth
27       | Some version ->
28           let sth = dbh#prepare_cached
29                       "select css from pages
30                         where hostid = ? and id = ? and
31                               (url = ? or url_deleted = ?)" in
32           sth#execute [`Int hostid; `Int version; `String page; `String page];
33           sth in
34   let css =
35     match sth#fetch1 () with
36         [ `Null ] -> ""
37       | [ `String css ] -> css
38       | _ -> assert false in
39
40   (* It's crucial, for speed of page delivery and rendering, to have
41    * an expires header for CSS.  Even though this means that occasionally
42    * people will need to hit [Shift]+Reload, I'm going to set a
43    * medium-length expiry time on this resource.
44    *)
45   Table.set (Request.headers_out r) "Expires" (Cgi_expires.expires_medium ());
46
47   q#header ~content_type:"text/css" ();
48   print_string r css
49
50 let () =
51   register_script run