2 * Copyright (C) 2003-2004:
3 * Stefano Zacchiroli <zack@cs.unibo.it>
4 * for the HELM Team http://helm.cs.unibo.it/
6 * This file is part of HELM, an Hypertextual, Electronic
7 * Library of Mathematics, developed at the Computer Science
8 * Department, University of Bologna, Italy.
10 * HELM is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * HELM is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with HELM; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * For details, see the HELM World-Wide-Web page,
26 * http://helm.cs.unibo.it/
31 open Http_getter_common
33 open Http_getter_debugger
34 open Http_getter_types
36 let (index_line_sep_RE, index_sep_RE, trailing_types_RE,
37 heading_cic_RE, heading_theory_RE, heading_nuprl_RE,
38 heading_rdf_cic_RE, heading_rdf_theory_RE) =
39 (Pcre.regexp "[ \t]+", Pcre.regexp "\r\n|\r|\n",
40 Pcre.regexp "\\.types$",
41 Pcre.regexp "^cic:", Pcre.regexp "^theory:", Pcre.regexp "^nuprl:",
42 Pcre.regexp "^helm:rdf.*//cic:", Pcre.regexp "^helm:rdf.*//theory:")
44 (* global maps, shared by all threads *)
46 let cic_map = new Http_getter_map.map Http_getter_env.cic_dbm
47 let nuprl_map = new Http_getter_map.map Http_getter_env.nuprl_dbm
48 let rdf_map = new Http_getter_map.map Http_getter_env.rdf_dbm
49 let xsl_map = new Http_getter_map.map Http_getter_env.xsl_dbm
51 let maps = [ cic_map; nuprl_map; rdf_map; xsl_map ]
52 let close_maps () = List.iter (fun m -> m#close) maps
53 let clear_maps () = List.iter (fun m -> m#clear) maps
54 let sync_maps () = List.iter (fun m -> m#sync) maps
56 let map_of_uri = function
57 | uri when is_cic_uri uri -> cic_map
58 | uri when is_nuprl_uri uri -> nuprl_map
59 | uri when is_rdf_uri uri -> rdf_map
60 | uri when is_xsl_uri uri -> xsl_map
61 | uri -> raise (Unresolvable_URI uri)
63 let update_from_server logmsg server_url = (* use global maps *)
64 debug_print ("Updating information from " ^ server_url);
65 let xml_url_of_uri = function
66 (* TODO missing sanity checks on server_url, e.g. it can contains $1 *)
67 | uri when (Pcre.pmatch ~rex:heading_cic_RE uri) ->
68 Pcre.replace ~rex:heading_cic_RE ~templ:server_url uri
69 | uri when (Pcre.pmatch ~rex:heading_theory_RE uri) ->
70 Pcre.replace ~rex:heading_theory_RE ~templ:server_url uri
71 | uri when (Pcre.pmatch ~rex:heading_nuprl_RE uri) ->
72 Pcre.replace ~rex:heading_nuprl_RE ~templ:server_url uri
73 | uri -> raise (Invalid_URI uri)
75 let rdf_url_of_uri = function (* TODO as above *)
76 | uri when (Pcre.pmatch ~rex:heading_rdf_cic_RE uri) ->
77 Pcre.replace ~rex:heading_rdf_cic_RE ~templ:server_url uri
78 | uri when (Pcre.pmatch ~rex:heading_rdf_theory_RE uri) ->
79 Pcre.replace ~rex:heading_rdf_theory_RE ~templ:server_url uri
80 | uri -> raise (Invalid_URI uri)
82 let log = ref (`T ("Processing server: " ^ server_url) :: logmsg) in
83 let (xml_index, rdf_index, xsl_index) =
84 (* TODO keeps index in memory, is better to keep them on temp files? *)
85 (http_get (server_url ^ "/" ^ Http_getter_env.xml_index),
86 http_get (server_url ^ "/" ^ Http_getter_env.rdf_index),
87 http_get (server_url ^ "/" ^ Http_getter_env.xsl_index))
89 if (xml_index = None && rdf_index = None && xsl_index = None) then
90 debug_print (sprintf "Warning: useless server %s" server_url);
93 (log := `T "Updating XML db ...<br />" :: !log;
96 | l when is_blank_line l -> () (* skip blank and commented lines *)
99 (match Pcre.split ~rex:index_line_sep_RE l with
101 assert (is_cic_uri uri || is_nuprl_uri uri) ;
102 (map_of_uri uri)#replace
103 uri ((xml_url_of_uri uri) ^ ".xml.gz")
105 assert (is_cic_uri uri || is_nuprl_uri uri) ;
106 (map_of_uri uri)#replace
107 uri ((xml_url_of_uri uri) ^ ".xml")
108 | _ -> log := `T ("Ignoring invalid line: '" ^ l) :: !log)
109 with Invalid_URI uri ->
110 log := `T ("Ignoring invalid XML URI: '" ^ l) :: !log))
111 (Pcre.split ~rex:index_sep_RE xml_index); (* xml_index lines *)
112 log := `T "All done" :: !log)
114 (match rdf_index with
116 (log := `T "Updating RDF db ..." :: !log;
120 (match Pcre.split ~rex:index_line_sep_RE l with
123 ((rdf_url_of_uri uri) ^ ".xml.gz")
125 rdf_map#replace uri ((rdf_url_of_uri uri) ^ ".xml")
126 | _ -> log := `T ("Ignoring invalid line: '" ^ l) :: !log)
127 with Invalid_URI uri ->
128 log := `T ("Ignoring invalid RDF URI: '" ^ l) :: !log)
129 (Pcre.split ~rex:index_sep_RE rdf_index); (* rdf_index lines *)
130 log := `T "All done" :: !log)
132 (match xsl_index with
134 (log := `T "Updating XSLT db ..." :: !log;
136 (fun l -> xsl_map#replace l (server_url ^ "/" ^ l))
137 (Pcre.split ~rex:index_sep_RE xsl_index);
138 log := `T "All done" :: !log)
140 debug_print "done with this server";
143 let update_from_all_servers () = (* use global maps *)
148 [] (* initial logmsg: empty *)
149 (* reverse order: 1st server is the most important one *)
150 (List.map snd (List.rev (Http_getter_env.servers ())))
153 `Msg (`L (List.rev log))
155 let update_from_one_server server_url =
156 let log = update_from_server [] server_url in
157 `Msg (`L (List.rev log))
159 let temp_file_of_uri uri =
160 let flat_string s s' c =
161 let cs = String.copy s in
162 for i = 0 to (String.length s) - 1 do
163 if String.contains s' s.[i] then cs.[i] <- c
167 let user = try Unix.getlogin () with _ -> "" in
168 Filename.open_temp_file (user ^ flat_string uri ".-=:;!?/&" '_') ""
172 let help () = Http_getter_const.usage_string (Http_getter_env.env_to_string ())
176 (map_of_uri uri)#resolve uri
177 with Http_getter_map.Key_not_found _ -> raise (Unresolvable_URI uri)
179 (* Warning: this fail if uri is already registered *)
180 let register ~uri ~url = (map_of_uri uri)#add uri url
182 let update () = update_from_all_servers ()
184 let getxml ?(format = Enc_normal) ?(patch_dtd = true) uri =
185 let url = resolve uri in
186 let (fname, outchan) = temp_file_of_uri uri in
187 Http_getter_cache.respond_xml ~uri ~url ~enc:format ~patch:patch_dtd outchan;
191 let getxslt ?(patch_dtd = true) uri =
192 let url = resolve uri in
193 let (fname, outchan) = temp_file_of_uri uri in
194 Http_getter_cache.respond_xsl ~url ~patch:patch_dtd outchan;
198 let getdtd ?(patch_dtd = true) uri =
199 let url = Http_getter_env.dtd_dir ^ "/" ^ uri in
200 let (fname, outchan) = temp_file_of_uri uri in
201 Http_getter_cache.respond_dtd ~url ~patch:patch_dtd outchan;
205 let clean_cache () = Http_getter_cache.clean ()
207 let list_servers () = Http_getter_env.servers ()
209 let add_server ?(position = 0) name =
210 if position = 0 then begin
211 Http_getter_env.add_server ~position:0 name;
212 update_from_one_server name (* quick update (new server only) *)
213 end else if position > 0 then begin
214 Http_getter_env.add_server ~position name;
216 end else (* already checked bt parse_position *)
219 let remove_server position =
222 List.assoc position (Http_getter_env.servers ())
224 raise (Invalid_argument (sprintf "no server with position %d" position))
226 Http_getter_env.remove_server position;
229 let return_uris map filter =
231 map#iter (fun uri _ -> if filter uri then uris := uri :: !uris);
236 (Pcre.pmatch ~rex:heading_cic_RE uri) &&
237 not (Pcre.pmatch ~rex:trailing_types_RE uri)
239 return_uris cic_map filter
241 let getallrdfuris classs =
243 let base = "^helm:rdf:www\\.cs\\.unibo\\.it/helm/rdf/" in
245 | `Forward -> (fun uri -> Pcre.pmatch ~pat:(base ^ "forward") uri)
246 | `Backward -> (fun uri -> Pcre.pmatch ~pat:(base ^ "backward") uri)
248 return_uris rdf_map filter
251 let (++) (oldann, oldtypes, oldbody, oldtree)
252 (newann, newtypes, newbody, newtree) =
253 ((if newann > oldann then newann else oldann),
254 (if newtypes > oldtypes then newtypes else oldtypes),
255 (if newbody > oldbody then newbody else oldbody),
256 (if newtree > oldtree then newtree else oldtree))
260 "^([^.]*\\.[^.]*)((\\.body)|(\\.proof_tree)|(\\.types))?(\\.ann)?$"
262 let (types_RE, types_ann_RE, body_RE, body_ann_RE,
263 proof_tree_RE, proof_tree_ann_RE) =
264 (Pcre.regexp "\\.types$", Pcre.regexp "\\.types\\.ann$",
265 Pcre.regexp "\\.body$", Pcre.regexp "\\.body\\.ann$",
266 Pcre.regexp "\\.proof_tree$", Pcre.regexp "\\.proof_tree\\.ann$")
268 let (slash_RE, til_slash_RE, no_slashes_RE) =
269 (Pcre.regexp "/", Pcre.regexp "^.*/", Pcre.regexp "^[^/]*$")
274 (match lsuri with Cic p -> ("cic:" ^ p) | Theory p -> ("theory:" ^ p))
276 let (dir_RE, obj_RE) =
277 (Pcre.regexp (pat ^ "/"), Pcre.regexp (pat ^ "(\\.|$)"))
279 let dirs = ref StringSet.empty in
280 let objs = Hashtbl.create 17 in
282 dirs := StringSet.add (List.hd (Pcre.split ~rex:slash_RE d)) !dirs
285 let basepart = Pcre.replace ~rex:basepart_RE ~templ:"$1" o in
286 let no_flags = false, No, No, No in
289 Hashtbl.find objs basepart
290 with Not_found -> (* no ann, no types, no body, no proof tree *)
295 | s when Pcre.pmatch ~rex:types_RE s -> (false, Yes, No, No)
296 | s when Pcre.pmatch ~rex:types_ann_RE s -> (true, Ann, No, No)
297 | s when Pcre.pmatch ~rex:body_RE s -> (false, No, Yes, No)
298 | s when Pcre.pmatch ~rex:body_ann_RE s -> (true, No, Ann, No)
299 | s when Pcre.pmatch ~rex:proof_tree_RE s -> (false, No, No, Yes)
300 | s when Pcre.pmatch ~rex:proof_tree_ann_RE s -> (true, No, No, Ann)
303 Hashtbl.replace objs basepart (oldflags ++ newflags)
306 (* BLEARGH Dbm module lacks support for fold-like functions *)
309 | uri when Pcre.pmatch ~rex:dir_RE uri -> (* directory hit *)
310 let localpart = Pcre.replace ~rex:dir_RE uri in
311 if Pcre.pmatch ~rex:no_slashes_RE localpart then
315 | uri when Pcre.pmatch ~rex:obj_RE uri -> (* file hit *)
316 store_obj (Pcre.replace ~rex:til_slash_RE uri)
317 | uri -> () (* miss *));
318 let ls_items = ref [] in
319 StringSet.iter (fun dir -> ls_items := Ls_section dir :: !ls_items) !dirs;
320 Http_getter_misc.hashtbl_sorted_iter
321 (fun uri (annflag, typesflag, bodyflag, treeflag) ->
324 uri = uri; ann = annflag;
325 types = typesflag; body = typesflag; proof_tree = treeflag