About to remove the links_to constraint from the links table.
[cocanwiki.git] / scripts / cocanwiki_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: cocanwiki_links.ml,v 1.2 2004/09/28 11:28:39 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 ExtString
23
24 let split_tags_re = Pcre.regexp ~flags:[`DOTALL] "<.*?>|[^<]+"
25 let internal_re = Pcre.regexp "class=\"internal\""
26 let href_re = Pcre.regexp "href=\"/(.*?)\""
27
28 let get_links_from_section dbh hostid content =
29   let html = Wikilib.xhtml_of_content dbh hostid content in
30
31   (* Split into attrs and non-attrs.  We end up with a list like this:
32    * [ "<ul>"; "<li>"; "Some text"; "</li>"; ... ]
33    *)
34   let html =
35     try
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
39     with
40         Not_found -> [] in
41
42   (* Only interested in the <a> tags. *)
43   let html = List.filter (fun str -> String.starts_with str "<a ") html in
44
45   (* Only interested in the tags with class="internal". *)
46   let html =
47     List.filter (fun str ->
48                    Pcre.pmatch ~rex:internal_re str
49                    && Pcre.pmatch ~rex:href_re str)
50       html in
51
52   (* Extract the URL names. *)
53   let links = List.map (fun str ->
54                           let subs =
55                             try Pcre.exec ~rex:href_re str
56                             with Not_found -> assert false in
57                           Pcre.get_substring subs 1) html in
58
59   (* Return the list of links. *)
60   links
61
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
65                                    where hostid = ?
66                                      and from_url = ? and to_url = ?" in
67     sth#execute [`Int hostid; `String from_url; `String to_url];
68
69     let exists = try sth#fetch1int () = 1 with Not_found -> false in
70
71     if not exists then (
72       let sth =
73         dbh#prepare_cached "insert into links (hostid, from_url, to_url)
74                             values (?, ?, ?)" in
75       sth#execute [`Int hostid; `String from_url; `String to_url]
76     )
77   )
78
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];
84
85   (* Get the sections from the page. *)
86   let sth = dbh#prepare_cached "select c.content from contents c, pages p
87                                  where c.pageid = p.id
88                                    and p.hostid = ?
89                                    and p.url = ?
90                                    and p.redirect is null" in
91   sth#execute [`Int hostid; `String page];
92
93   (* Get the links from each section. *)
94   sth#iter
95     (function [`String content] ->
96        let links = get_links_from_section dbh hostid content in
97        List.iter (insert_link dbh hostid page) links
98        | _ -> assert false)