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: links.ml,v 1.1 2004/10/27 21:14:05 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
31 (* This script produces the links database for the whole site (if
32 * called without the 'page' parameter), or else just for a particular
33 * page. If called with 'page' and 'type' parameters together, then it
34 * can show inbound or outbound links only. The format in all cases is a
35 * simple machine-parsable text file.
37 let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
38 let template = get_template dbh hostid "links.txt" in
40 if q#param_exists "page" then (
41 let page = q#param "page" in
42 let page = if page = "" then "index" else page in
44 if q#param_exists "type" then (
45 let type_ = q#param "type" in
47 if type_ = "inbound" then (
48 (* This is "what links here". We're interested in indirect
49 * links too, so use Cocanwiki_links.what_links_here function.
51 let links = what_links_here dbh hostid page in
52 let links = List.map fst links in
54 q#header ~content_type:"text/plain" ();
56 List.iter (fun url -> ignore (print_endline r url)) links
58 ) else if type_ = "outbound" then (
59 (* Display a list of links outbound from this page. *)
61 dbh#prepare_cached "select to_url from links
62 where hostid = ? and from_url = ?" in
64 sth#execute [`Int hostid; `String page];
66 q#header ~content_type:"text/plain" ();
68 sth#iter (function [`String url] -> ignore (print_endline r url)
72 failwith "'type' parameter should be 'inbound' or 'outbound'"
75 (* Just return the single-row "links database" relating to this
78 let sth = dbh#prepare_cached "select to_url from links
79 where hostid = ? and from_url = ?" in
80 sth#execute [`Int hostid; `String page];
83 sth#map (function [`String to_url] ->
84 [ "to", Template.VarString to_url ]
85 | _ -> assert false) in
87 [ [ "from", Template.VarString page;
88 "to", Template.VarTable table ] ] in
89 template#table "links" table;
91 q#template ~content_type:"text/plain" template
94 (* Links database for whole site in the simple format required by
95 * the TouchGraph application. We don't know anything about external
96 * links, so we don't include them. However we do include links to
97 * non-existant internal pages.
99 let h = Hashtbl.create 1024 in
100 let add_link from_url to_url =
101 let xs = try Hashtbl.find h from_url with Not_found -> [] in
102 let xs = to_url :: xs in
103 Hashtbl.replace h from_url xs
106 let sth = dbh#prepare_cached "select from_url, to_url from links
108 sth#execute [`Int hostid];
110 sth#iter (function [`String from_url; `String to_url] ->
111 add_link from_url to_url
112 | _ -> assert false);
114 (* Don't forget redirects! They're kinda like links ... *)
115 let sth = dbh#prepare_cached "select url, redirect from pages
116 where hostid = ? and url is not null
117 and redirect is not null" in
118 sth#execute [`Int hostid];
120 sth#iter (function [`String url; `String redirect] ->
121 add_link url redirect
122 | _ -> assert false);
124 let keys h = Hashtbl.fold (fun key _ xs -> key :: xs) h [] in
129 let xs = Hashtbl.find h from in
131 List.map (fun to_ -> [ "to", Template.VarString to_ ]) xs in
132 [ "from", Template.VarString from;
133 "to", Template.VarTable table ]
135 template#table "links" table;
137 q#template ~content_type:"text/plain" template