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 $
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.
28 open Cocanwiki_template
30 let keys h = Hashtbl.fold (fun key _ xs -> key :: xs) h []
32 let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
33 let template = get_template dbh hostid "broken_links.html" in
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
43 "select l.from_url, p.title, l.to_url
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];
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
63 let keys = List.sort compare (keys h) in
67 let values = Hashtbl.find h to_url in
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
76 template#table "links" table;
81 register_script ~restrict:[CanView] run