]> matita.cs.unibo.it Git - helm.git/blob - helm/http_getter/http_getter.ml
Ported to Helm_registry.
[helm.git] / helm / http_getter / http_getter.ml
1 (*
2  * Copyright (C) 2003-2004:
3  *    Stefano Zacchiroli <zack@cs.unibo.it>
4  *    for the HELM Team http://helm.cs.unibo.it/
5  *
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.
9  *
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.
14  *
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.
19  *
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,
23  *  MA  02111-1307, USA.
24  *
25  *  For details, see the HELM World-Wide-Web page,
26  *  http://helm.cs.unibo.it/
27  *)
28
29 open Printf
30
31 open Http_getter_common
32 open Http_getter_misc
33 open Http_getter_debugger
34 open Http_getter_types
35
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:")
43
44   (* global maps, shared by all threads *)
45
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
50
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
55
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)
62
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)
74   in
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)
81   in
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))
88   in
89   if (xml_index = None && rdf_index = None && xsl_index = None) then
90     debug_print (sprintf "Warning: useless server %s" server_url);
91   (match xml_index with
92   | Some xml_index ->
93       (log := `T "Updating XML db ...<br />" :: !log;
94       List.iter
95         (function
96           | l when is_blank_line l -> ()  (* skip blank and commented lines *)
97           | l ->
98               (try
99                 (match Pcre.split ~rex:index_line_sep_RE l with
100                 | [uri; "gz"] ->
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")
104                 | [uri] ->
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)
113   | None -> ());
114   (match rdf_index with
115   | Some rdf_index ->
116       (log := `T "Updating RDF db ..." :: !log;
117       List.iter
118         (fun l ->
119           try
120             (match Pcre.split ~rex:index_line_sep_RE l with
121             | [uri; "gz"] ->
122                 rdf_map#replace uri
123                   ((rdf_url_of_uri uri) ^ ".xml.gz")
124             | [uri] ->
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)
131   | None -> ());
132   (match xsl_index with
133   | Some xsl_index ->
134       (log := `T "Updating XSLT db ..." :: !log;
135       List.iter
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)
139   | None -> ());
140   debug_print "done with this server";
141   !log
142
143 let update_from_all_servers () =  (* use global maps *)
144   clear_maps ();
145   let log =
146     List.fold_left
147       update_from_server
148       []  (* initial logmsg: empty *)
149         (* reverse order: 1st server is the most important one *)
150       (List.map snd (List.rev (Http_getter_env.servers ())))
151   in
152   sync_maps ();
153   `Msg (`L (List.rev log))
154
155 let update_from_one_server server_url =
156   let log = update_from_server [] server_url in
157   `Msg (`L (List.rev log))
158
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
164     done;
165     cs
166   in
167   let user = try Unix.getlogin () with _ -> "" in
168   Filename.open_temp_file (user ^ flat_string uri ".-=:;!?/&" '_') ""
169   
170 (* API *)
171
172 let help () = Http_getter_const.usage_string (Http_getter_env.env_to_string ())
173
174 let resolve uri =
175   try
176     (map_of_uri uri)#resolve uri
177   with Http_getter_map.Key_not_found _ -> raise (Unresolvable_URI uri)
178
179   (* Warning: this fail if uri is already registered *)
180 let register ~uri ~url = (map_of_uri uri)#add uri url
181
182 let update () = update_from_all_servers ()
183
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;
188   close_out outchan;
189   fname
190
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;
195   close_out outchan;
196   fname
197
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;
202   close_out outchan;
203   fname
204
205 let clean_cache () = Http_getter_cache.clean ()
206
207 let list_servers () = Http_getter_env.servers ()
208
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;
215     update ()
216   end else  (* already checked bt parse_position *)
217     assert false
218
219 let remove_server position =
220   let server_name =
221     try
222       List.assoc position (Http_getter_env.servers ())
223     with Not_found ->
224       raise (Invalid_argument (sprintf "no server with position %d" position))
225   in
226   Http_getter_env.remove_server position;
227   update ()
228
229 let return_uris map filter =
230   let uris = ref [] in
231   map#iter (fun uri _ -> if filter uri then uris := uri :: !uris);
232   List.rev !uris
233
234 let getalluris () =
235   let filter uri =
236     (Pcre.pmatch ~rex:heading_cic_RE uri) &&
237     not (Pcre.pmatch ~rex:trailing_types_RE uri)
238   in
239   return_uris cic_map filter
240
241 let getallrdfuris classs =
242   let filter =
243     let base = "^helm:rdf:www\\.cs\\.unibo\\.it/helm/rdf/" in
244     match classs with
245     | `Forward -> (fun uri -> Pcre.pmatch ~pat:(base ^ "forward") uri)
246     | `Backward -> (fun uri -> Pcre.pmatch ~pat:(base ^ "backward") uri)
247   in
248   return_uris rdf_map filter
249
250 let ls =
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))
257   in
258   let basepart_RE =
259     Pcre.regexp
260       "^([^.]*\\.[^.]*)((\\.body)|(\\.proof_tree)|(\\.types))?(\\.ann)?$"
261   in
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$")
267   in
268   let (slash_RE, til_slash_RE, no_slashes_RE) =
269     (Pcre.regexp "/", Pcre.regexp "^.*/", Pcre.regexp "^[^/]*$")
270   in
271   fun lsuri ->
272     let pat =
273       "^" ^
274       (match lsuri with Cic p -> ("cic:" ^ p) | Theory p -> ("theory:" ^ p))
275     in
276     let (dir_RE, obj_RE) =
277       (Pcre.regexp (pat ^ "/"), Pcre.regexp (pat ^ "(\\.|$)"))
278     in
279     let dirs = ref StringSet.empty in
280     let objs = Hashtbl.create 17 in
281     let store_dir d =
282       dirs := StringSet.add (List.hd (Pcre.split ~rex:slash_RE d)) !dirs
283     in
284     let store_obj o =
285       let basepart = Pcre.replace ~rex:basepart_RE ~templ:"$1" o in
286       let no_flags = false, No, No, No in
287       let oldflags =
288         try
289           Hashtbl.find objs basepart
290         with Not_found -> (* no ann, no types, no body, no proof tree *)
291           no_flags
292       in
293       let newflags =
294         match o with
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)
301         | s -> no_flags
302       in
303       Hashtbl.replace objs basepart (oldflags ++ newflags)
304     in
305     cic_map#iter
306       (* BLEARGH Dbm module lacks support for fold-like functions *)
307       (fun key _ ->
308         match key with
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
312               store_obj localpart
313             else
314               store_dir localpart
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) ->
322         ls_items :=
323           Ls_object {
324             uri = uri; ann = annflag;
325             types = typesflag; body = typesflag; proof_tree = treeflag
326           } :: !ls_items)
327       objs;
328     List.rev !ls_items
329