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_links.ml,v 1.2 2004/09/28 11:28:39 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.
24 let split_tags_re = Pcre.regexp ~flags:[`DOTALL] "<.*?>|[^<]+"
25 let internal_re = Pcre.regexp "class=\"internal\""
26 let href_re = Pcre.regexp "href=\"/(.*?)\""
28 let get_links_from_section dbh hostid content =
29 let html = Wikilib.xhtml_of_content dbh hostid content in
31 (* Split into attrs and non-attrs. We end up with a list like this:
32 * [ "<ul>"; "<li>"; "Some text"; "</li>"; ... ]
36 let html = Pcre.extract_all ~rex:split_tags_re html in
37 let html = Array.to_list html in
38 List.map (function [| a |] -> a | _ -> assert false) html
42 (* Only interested in the <a> tags. *)
43 let html = List.filter (fun str -> String.starts_with str "<a ") html in
45 (* Only interested in the tags with class="internal". *)
47 List.filter (fun str ->
48 Pcre.pmatch ~rex:internal_re str
49 && Pcre.pmatch ~rex:href_re str)
52 (* Extract the URL names. *)
53 let links = List.map (fun str ->
55 try Pcre.exec ~rex:href_re str
56 with Not_found -> assert false in
57 Pcre.get_substring subs 1) html in
59 (* Return the list of links. *)
62 let insert_link dbh hostid from_url to_url =
63 if from_url <> to_url then (
64 let sth = dbh#prepare_cached "select 1 from links
66 and from_url = ? and to_url = ?" in
67 sth#execute [`Int hostid; `String from_url; `String to_url];
69 let exists = try sth#fetch1int () = 1 with Not_found -> false in
73 dbh#prepare_cached "insert into links (hostid, from_url, to_url)
75 sth#execute [`Int hostid; `String from_url; `String to_url]
79 let update_links_for_page dbh hostid page =
80 (* Delete entries in the old links table. *)
81 let sth = dbh#prepare_cached "delete from links
82 where hostid = ? and from_url = ?" in
83 sth#execute [`Int hostid; `String page];
85 (* Get the sections from the page. *)
86 let sth = dbh#prepare_cached "select c.content from contents c, pages p
90 and p.redirect is null" in
91 sth#execute [`Int hostid; `String page];
93 (* Get the links from each section. *)
95 (function [`String content] ->
96 let links = get_links_from_section dbh hostid content in
97 List.iter (insert_link dbh hostid page) links