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.9 2006/08/04 12:45:33 rich Exp $
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.
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.
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.
25 open Cocanwiki_strings
27 type pt = Page of string | Title of string
30 string option * string option * string option * string
31 (* (sectionname, divname, jsgo, content) *)
34 id : int32; (* Original page ID (0 = none). *)
35 pt : pt; (* Page of title (only used if id=0) *)
36 description : string; (* Description. *)
37 keywords : string option; (* Keywords. *)
38 redirect : string option; (* Redirect to. *)
39 (* NB. Don't call this 'contents' because that clashes with the
40 * Pervasives.contents fields of the ref type.
42 contents_ : section list;
45 exception SaveURLError
46 exception SaveConflict of int32 * int32 * string * string option
52 | Title title -> title in
54 let model = { id = 0l;
56 description = description;
62 let new_page_with_title title =
63 (* Initial page contents. *)
64 let contents = [ None, None, None, "<b>" ^ title ^ "</b> is " ] in
65 let model = { id = 0l;
70 contents_ = contents } in
73 let load_page dbh hostid ~url ?version () =
74 (* Pull out the page itself from the database. *)
78 PGSQL(dbh) "select id, title, description, keywords, redirect
80 where hostid = $hostid and url = $url"
82 PGSQL(dbh) "select id, title, description, keywords, redirect
84 where hostid = $hostid and id = $version and
85 (url = $url or url_deleted = $url)" in
87 let pageid, title, description, keywords, redirect =
90 | _ -> raise Not_found in
92 (* Get the sections. *)
93 let contents = PGSQL(dbh)
94 "select sectionname, divname, jsgo, content
96 where pageid = $pageid
99 let model = { id = pageid;
101 description = description;
104 contents_ = contents } in
107 let save_page r dbh hostid ?user model =
108 (* Logging information, if available. *)
114 | User (id, _, _, _) -> Some id
118 try Some (Connection.remote_ip (Request.connection r))
119 with Not_found -> None in
122 (* Creating a new page (id = 0)? If so, we're just going to insert
123 * a new row, which is easy.
125 if model.id = 0l then (
126 (* Create the page title or URL. *)
131 match Wikilib.generate_url_of_title r dbh hostid title with
132 Wikilib.GenURL_OK url -> url, title
134 raise SaveURLError in
136 let description = model.description in
137 let keywords = model.keywords in
138 let redirect = model.redirect in
139 PGSQL(dbh) "insert into pages (hostid, url, title,
140 description, keywords,
141 logged_ip, logged_user,
143 values ($hostid, $url, $title, $description, $?keywords,
144 $?logged_ip, $?logged_user, $?redirect)";
146 let pageid = PGOCaml.serial4 dbh "pages_id_seq" in
148 (* Create the page contents. *)
149 let ordering = ref 0 in (* Creating new ordering. *)
151 fun (sectionname, divname, jsgo, content) ->
152 incr ordering; let ordering = Int32.of_int !ordering in
154 "insert into contents (pageid, ordering, sectionname, divname,
156 values ($pageid, $ordering,
157 $?sectionname, $?divname, $?jsgo, $content)"
162 (* Otherwise it's an old page which we're updating. *)
164 (* Pull out fields from the database. *)
165 let model_id = model.id in
168 "select creation_date, coalesce (url, url_deleted),
171 where hostid = $hostid and id = $model_id" in
173 let creation_date, url, title, css =
176 | _ -> assert false in
177 let url = Option.get url in
182 | Title new_title when title <> new_title -> new_title
185 (* Has someone else edited this page in the meantime? *)
186 let max_id = Option.get (
188 PGSQL(dbh) "select max(id) from pages
189 where hostid = $hostid and url = $url"
193 let edited = max_id <> model_id in
196 raise (SaveConflict (max_id, model_id, url, css));
198 (* Defer the pages_redirect_cn constraint because that would
199 * temporarily fail on the next UPDATE.
203 pages_redirect_cn, sitemenu_url_cn,
204 page_emails_url_cn, links_from_cn, recently_visited_url_cn
207 (* Mark the old page as deleted. NB. There is a small race
208 * condition here because PostgreSQL doesn't do isolation
209 * properly. If a user tries to visit this page between the
210 * delete and the creation of the new page, then they'll get
211 * a page not found error. (XXX)
213 PGSQL(dbh) "update pages set url_deleted = url, url = null
214 where hostid = $hostid and id = $model_id";
216 let description = model.description in
217 let keywords = model.keywords in
218 let redirect = model.redirect in
220 "insert into pages (hostid, url, title,
221 description, keywords,
222 creation_date, logged_ip,
223 logged_user, redirect, css)
224 values ($hostid, $url, $title, $description, $?keywords,
226 $?logged_ip, $?logged_user, $?redirect, $?css)";
228 (* New page ID <> old page ID model.id. *)
229 let pageid = PGOCaml.serial4 dbh "pages_id_seq" in
231 (* Create the page contents. *)
232 let ordering = ref 0 in (* Creating new ordering. *)
234 fun (sectionname, divname, jsgo, content) ->
235 incr ordering; let ordering = Int32.of_int !ordering in
236 PGSQL(dbh) "insert into contents (pageid,
237 ordering, sectionname, divname, jsgo, content)
238 values ($pageid, $ordering, $?sectionname,
239 $?divname, $?jsgo, $content)"
245 (* Keep the links table in synch. *)
246 Cocanwiki_links.update_links_for_page r dbh hostid url;