Added external functions allowing the markup to be extended.
[cocanwiki.git] / scripts / lib / cocanwiki_pages.ml
1 (* COCANWIKI - a wiki written in Objective CAML.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: cocanwiki_pages.ml,v 1.7 2006/07/27 16:46:55 rich Exp $
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *)
21
22 open Apache
23
24 open Cocanwiki
25 open Cocanwiki_strings
26
27 type pt = Page of string | Title of string
28
29 type model = {
30   id : int32;                           (* Original page ID (0 = none). *)
31   pt : pt;                              (* Page of title (only used if id=0) *)
32   description : string;                 (* Description. *)
33   redirect : string option;             (* Redirect to. *)
34   (* NB. Don't call this 'contents' because that clashes with the
35    * Pervasives.contents fields of the ref type.
36    *)
37   contents_ : (string option * string option * string option * string) list;
38           (* (sectionname, divname, jsgo, content) for each section. *)
39 }
40
41 exception SaveURLError
42 exception SaveConflict of int32 * int32 * string * string
43
44 let new_page pt =
45   let description =
46     match pt with
47         Page page -> page
48       | Title title -> title in
49
50   let model = { id = 0l;
51                 pt = pt;
52                 description = description;
53                 redirect = None;
54                 contents_ = [] } in
55   model
56
57 let new_page_with_title title =
58   (* Initial page contents. *)
59   let contents = [ None, None, None, "<b>" ^ title ^ "</b> is " ] in
60   let model = { id = 0l;
61                 pt = Title title;
62                 description = title;
63                 redirect = None;
64                 contents_ = contents } in
65   model
66
67 let load_page dbh hostid ~url ?version () =
68   (* Pull out the page itself from the database. *)
69   let rows =
70     match version with
71     | None ->
72         PGSQL(dbh) "select id, title, description, redirect
73                       from pages
74                      where hostid = $hostid and url = $url"
75     | Some version ->
76         PGSQL(dbh) "select id, title, description, redirect
77                       from pages
78                      where hostid = $hostid and id = $version and
79                            (url = $url or url_deleted = $url)" in
80
81   let pageid, title, description, redirect =
82     match rows with
83     | [row] -> row
84     | _ -> raise Not_found in
85
86   (* Get the sections. *)
87   let contents = PGSQL(dbh)
88     "select sectionname, divname, jsgo, content
89        from contents
90       where pageid = $pageid
91       order by ordering" in
92
93   let model = { id = pageid;
94                 pt = Page url;
95                 description = description;
96                 redirect = redirect;
97                 contents_ = contents } in
98   model
99
100 let save_page r dbh hostid ?user model =
101   (* Logging information, if available. *)
102   let logged_user =
103     match user with
104         None -> None
105       | Some user ->
106           match user with
107             | User (id, _, _, _) -> Some id
108             | _ -> None in
109
110   let logged_ip =
111     try Some (Connection.remote_ip (Request.connection r))
112     with Not_found -> None in
113
114   let url, pageid =
115     (* Creating a new page (id = 0)?  If so, we're just going to insert
116      * a new row, which is easy.
117      *)
118     if model.id = 0l then (
119       (* Create the page title or URL. *)
120       let url, title =
121         match model.pt with
122             Page url -> url, url
123           | Title title ->
124               match Wikilib.generate_url_of_title r dbh hostid title with
125                   Wikilib.GenURL_OK url -> url, title
126                 | _ ->
127                     raise SaveURLError in
128
129       let description = model.description in
130       let redirect = model.redirect in
131       PGSQL(dbh) "insert into pages (hostid, url, title,
132                                      description, logged_ip, logged_user,
133                                      redirect)
134                   values ($hostid, $url, $title, $description,
135                           $?logged_ip, $?logged_user, $?redirect)";
136
137       let pageid = PGOCaml.serial4 dbh "pages_id_seq" in
138
139       (* Create the page contents. *)
140       let ordering = ref 0 in   (* Creating new ordering. *)
141       List.iter (
142         fun (sectionname, divname, jsgo, content) ->
143           incr ordering; let ordering = Int32.of_int !ordering in
144           PGSQL(dbh)
145             "insert into contents (pageid, ordering, sectionname, divname,
146                                    jsgo, content)
147              values ($pageid, $ordering,
148                      $?sectionname, $?divname, $?jsgo, $content)"
149       ) model.contents_;
150
151       url, pageid
152     )
153       (* Otherwise it's an old page which we're updating. *)
154     else (
155       (* Pull out fields from the database. *)
156       let model_id = model.id in
157       let rows =
158         PGSQL(dbh)
159         "select creation_date, coalesce (url, url_deleted),
160                 title, css
161            from pages
162           where hostid = $hostid and id = $model_id" in
163
164       let creation_date, url, title, css =
165         match rows with
166         | [ row ] -> row
167         | _ -> assert false in
168       let url = Option.get url in
169
170       (* Title changed? *)
171       let title =
172         match model.pt with
173         | Title new_title when title <> new_title -> new_title
174         | _ -> title in
175
176       (* Has someone else edited this page in the meantime? *)
177       let max_id = Option.get (
178         List.hd (
179           PGSQL(dbh) "select max(id) from pages
180                        where hostid = $hostid and url = $url"
181         )
182       ) in
183
184       let edited = max_id <> model_id in
185
186       if edited then (
187         let css = match css with None -> "" | Some css -> css in
188         raise (SaveConflict (max_id, model_id, url, css))
189       );
190
191       (* Defer the pages_redirect_cn constraint because that would
192        * temporarily fail on the next UPDATE.
193        *)
194       PGSQL(dbh)
195         "set constraints
196                pages_redirect_cn, sitemenu_url_cn,
197                page_emails_url_cn, links_from_cn, recently_visited_url_cn
198              deferred";
199
200       (* Mark the old page as deleted.  NB. There is a small race
201        * condition here because PostgreSQL doesn't do isolation
202        * properly.  If a user tries to visit this page between the
203        * delete and the creation of the new page, then they'll get
204        * a page not found error. (XXX)
205        *)
206       PGSQL(dbh) "update pages set url_deleted = url, url = null
207                    where hostid = $hostid and id = $model_id";
208
209       let description = model.description in
210       let redirect = model.redirect in
211       PGSQL(dbh)
212         "insert into pages (hostid, url, title,
213                             description, creation_date, logged_ip,
214                             logged_user, redirect, css)
215          values ($hostid, $url, $title, $description, $creation_date,
216                  $?logged_ip, $?logged_user, $?redirect, $?css)";
217
218       (* New page ID <> old page ID model.id. *)
219       let pageid = PGOCaml.serial4 dbh "pages_id_seq" in
220
221       (* Create the page contents. *)
222       let ordering = ref 0 in   (* Creating new ordering. *)
223       List.iter (
224         fun (sectionname, divname, jsgo, content) ->
225           incr ordering; let ordering = Int32.of_int !ordering in
226           PGSQL(dbh) "insert into contents (pageid,
227                          ordering, sectionname, divname, jsgo, content)
228                       values ($pageid, $ordering, $?sectionname,
229                               $?divname, $?jsgo, $content)"
230       ) model.contents_;
231
232       url, pageid
233     ) in
234
235   (* Keep the links table in synch. *)
236   Cocanwiki_links.update_links_for_page r dbh hostid url;
237
238   url, pageid