Added <meta keywords> on pages.
[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.9 2006/08/04 12:45:33 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 section =
30     string option * string option * string option * string
31     (* (sectionname, divname, jsgo, content) *)
32
33 type model = {
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.
41    *)
42   contents_ : section list;
43 }
44
45 exception SaveURLError
46 exception SaveConflict of int32 * int32 * string * string option
47
48 let new_page pt =
49   let description =
50     match pt with
51     | Page page -> page
52     | Title title -> title in
53
54   let model = { id = 0l;
55                 pt = pt;
56                 description = description;
57                 keywords = None;
58                 redirect = None;
59                 contents_ = [] } in
60   model
61
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;
66                 pt = Title title;
67                 description = title;
68                 keywords = None;
69                 redirect = None;
70                 contents_ = contents } in
71   model
72
73 let load_page dbh hostid ~url ?version () =
74   (* Pull out the page itself from the database. *)
75   let rows =
76     match version with
77     | None ->
78         PGSQL(dbh) "select id, title, description, keywords, redirect
79                       from pages
80                      where hostid = $hostid and url = $url"
81     | Some version ->
82         PGSQL(dbh) "select id, title, description, keywords, redirect
83                       from pages
84                      where hostid = $hostid and id = $version and
85                            (url = $url or url_deleted = $url)" in
86
87   let pageid, title, description, keywords, redirect =
88     match rows with
89     | [row] -> row
90     | _ -> raise Not_found in
91
92   (* Get the sections. *)
93   let contents = PGSQL(dbh)
94     "select sectionname, divname, jsgo, content
95        from contents
96       where pageid = $pageid
97       order by ordering" in
98
99   let model = { id = pageid;
100                 pt = Page url;
101                 description = description;
102                 keywords = keywords;
103                 redirect = redirect;
104                 contents_ = contents } in
105   model
106
107 let save_page r dbh hostid ?user model =
108   (* Logging information, if available. *)
109   let logged_user =
110     match user with
111         None -> None
112       | Some user ->
113           match user with
114             | User (id, _, _, _) -> Some id
115             | _ -> None in
116
117   let logged_ip =
118     try Some (Connection.remote_ip (Request.connection r))
119     with Not_found -> None in
120
121   let url, pageid =
122     (* Creating a new page (id = 0)?  If so, we're just going to insert
123      * a new row, which is easy.
124      *)
125     if model.id = 0l then (
126       (* Create the page title or URL. *)
127       let url, title =
128         match model.pt with
129             Page url -> url, url
130           | Title title ->
131               match Wikilib.generate_url_of_title r dbh hostid title with
132                   Wikilib.GenURL_OK url -> url, title
133                 | _ ->
134                     raise SaveURLError in
135
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,
142                                      redirect)
143                   values ($hostid, $url, $title, $description, $?keywords,
144                           $?logged_ip, $?logged_user, $?redirect)";
145
146       let pageid = PGOCaml.serial4 dbh "pages_id_seq" in
147
148       (* Create the page contents. *)
149       let ordering = ref 0 in   (* Creating new ordering. *)
150       List.iter (
151         fun (sectionname, divname, jsgo, content) ->
152           incr ordering; let ordering = Int32.of_int !ordering in
153           PGSQL(dbh)
154             "insert into contents (pageid, ordering, sectionname, divname,
155                                    jsgo, content)
156              values ($pageid, $ordering,
157                      $?sectionname, $?divname, $?jsgo, $content)"
158       ) model.contents_;
159
160       url, pageid
161     )
162       (* Otherwise it's an old page which we're updating. *)
163     else (
164       (* Pull out fields from the database. *)
165       let model_id = model.id in
166       let rows =
167         PGSQL(dbh)
168         "select creation_date, coalesce (url, url_deleted),
169                 title, css
170            from pages
171           where hostid = $hostid and id = $model_id" in
172
173       let creation_date, url, title, css =
174         match rows with
175         | [ row ] -> row
176         | _ -> assert false in
177       let url = Option.get url in
178
179       (* Title changed? *)
180       let title =
181         match model.pt with
182         | Title new_title when title <> new_title -> new_title
183         | _ -> title in
184
185       (* Has someone else edited this page in the meantime? *)
186       let max_id = Option.get (
187         List.hd (
188           PGSQL(dbh) "select max(id) from pages
189                        where hostid = $hostid and url = $url"
190         )
191       ) in
192
193       let edited = max_id <> model_id in
194
195       if edited then
196         raise (SaveConflict (max_id, model_id, url, css));
197
198       (* Defer the pages_redirect_cn constraint because that would
199        * temporarily fail on the next UPDATE.
200        *)
201       PGSQL(dbh)
202         "set constraints
203                pages_redirect_cn, sitemenu_url_cn,
204                page_emails_url_cn, links_from_cn, recently_visited_url_cn
205              deferred";
206
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)
212        *)
213       PGSQL(dbh) "update pages set url_deleted = url, url = null
214                    where hostid = $hostid and id = $model_id";
215
216       let description = model.description in
217       let keywords = model.keywords in
218       let redirect = model.redirect in
219       PGSQL(dbh)
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,
225                  $creation_date,
226                  $?logged_ip, $?logged_user, $?redirect, $?css)";
227
228       (* New page ID <> old page ID model.id. *)
229       let pageid = PGOCaml.serial4 dbh "pages_id_seq" in
230
231       (* Create the page contents. *)
232       let ordering = ref 0 in   (* Creating new ordering. *)
233       List.iter (
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)"
240       ) model.contents_;
241
242       url, pageid
243     ) in
244
245   (* Keep the links table in synch. *)
246   Cocanwiki_links.update_links_for_page r dbh hostid url;
247
248   url, pageid