Serial columns now 64 bits.
[cocanwiki.git] / scripts / broken_links.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: broken_links.ml,v 1.1 2004/10/09 16:25:08 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 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_template
29
30 let keys h = Hashtbl.fold (fun key _ xs -> key :: xs) h []
31
32 let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
33   let template = get_template dbh hostid "broken_links.html" in
34
35   (* The links table (to_url) field can now point to a non-existant
36    * page.  It's from this observation that we are able to retrieve
37    * the complete list of broken links.  Links to empty template pages
38    * aren't broken links either because some of the content is
39    * synthesized.
40    *)
41   let sth =
42     dbh#prepare_cached
43       "select l.from_url, p.title, l.to_url
44          from links l, pages p
45         where l.hostid = ?
46           and l.hostid = p.hostid and l.from_url = p.url
47           and p.redirect is null
48           and not exists (select id from pages
49                            where hostid = l.hostid and url = l.to_url)
50           and not exists (select id from templates
51                            where l.to_url ~ url_regexp)
52         order by 3, 1 desc" in
53   sth#execute [`Int hostid];
54
55   (* Group the links together. *)
56   let h = Hashtbl.create 32 in
57   sth#iter (function [`String from_url; `String from_title; `String to_url] ->
58               let a = try Hashtbl.find h to_url with Not_found -> [] in
59               let a = (from_url, from_title) :: a in
60               Hashtbl.replace h to_url a
61               | _ -> assert false);
62
63   let keys = List.sort compare (keys h) in
64   let table =
65     List.map
66       (fun to_url ->
67          let values = Hashtbl.find h to_url in
68          let table =
69            List.map
70              (fun (from_url, from_title) ->
71                 [ "page", Template.VarString from_url;
72                   "title", Template.VarString from_title ]) values in
73          [ "url", Template.VarString to_url;
74            "references", Template.VarTable table ]) keys in
75
76   template#table "links" table;
77
78   q#template template
79
80 let () =
81   register_script ~restrict:[CanView] run