1 (* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
29 let debug_prerr = if debug then prerr_endline else ignore
31 (*module HGT = Http_getter_types;;*)
32 (*module HG = Http_getter;;*)
33 (*module UM = UriManager;;*)
35 let decompile = ref (fun ~baseuri:_ -> assert false);;
36 let set_decompile_cb f = decompile := f;;
39 let strip_xpointer s = Pcre.replace ~pat:"#.*$" s ;;
41 let safe_buri_of_suri suri =
43 UM.buri_of_uri (UM.uri_of_string suri)
45 UM.IllFormedUri _ -> suri
47 let one_step_depend cache_of_processed_baseuri suri dbtype dbd =
48 assert false (* MATITA 1.0
49 let buri = safe_buri_of_suri suri in
50 if Hashtbl.mem cache_of_processed_baseuri buri then
54 Hashtbl.add cache_of_processed_baseuri buri true;
56 let buri = buri ^ "/" in
57 let buri = HSql.escape dbtype dbd buri in
58 let obj_tbl = MetadataTypes.obj_tbl () in
59 if HSql.isMysql dbtype dbd then
60 sprintf ("SELECT source, h_occurrence FROM %s WHERE "
61 ^^ "h_occurrence REGEXP '"^^
62 "^%s\\([^/]+\\|[^/]+#xpointer.*\\)$"^^"'")
66 sprintf ("SELECT source, h_occurrence FROM %s WHERE "
67 ^^ "REGEXP(h_occurrence, '"^^
68 "^%s\\([^/]+\\|[^/]+#xpointer.*\\)$"^^"')")
70 (* implementation with vanilla ocaml-sqlite3
71 HLog.debug "Warning SELECT without REGEXP";
73 ("SELECT source, h_occurrence FROM %s WHERE " ^^
74 "h_occurrence LIKE '%s%%' " ^^ HSql.escape_string_for_like)
79 let rc = HSql.exec dbtype dbd query in
83 match row.(0), row.(1) with
84 | Some uri, Some occ when
85 Filename.dirname (strip_xpointer occ) = buri ->
88 let l = List.sort Pervasives.compare !l in
91 exn -> raise exn (* no errors should be accepted *)
95 let db_uris_of_baseuri buri =
97 let dbd = LibraryDb.instance () in
99 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
102 let buri = buri ^ "/" in
103 let buri = HSql.escape dbtype dbd buri in
104 let obj_tbl = MetadataTypes.name_tbl () in
105 if HSql.isMysql dbtype dbd then
106 sprintf ("SELECT source FROM %s WHERE "
107 ^^ "source REGEXP '^%s[^/]*(#xpointer.*)?$'") obj_tbl buri
110 sprintf ("SELECT source FROM %s WHERE "
111 ^^ "REGEXP(source, '^%s[^/]*\\(#xpointer.*\\)?$')") obj_tbl buri
115 let rc = HSql.exec dbtype dbd query in
120 | Some uri when Filename.dirname (strip_xpointer uri) = buri ->
123 let l = List.sort Pervasives.compare !l in
126 exn -> raise exn (* no errors should be accepted *)
130 let close_uri_list cache_of_processed_baseuri uri_to_remove =
131 let dbd = LibraryDb.instance () in
133 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
135 (* to remove an uri you have to remove the whole script *)
138 (List.fast_sort Pervasives.compare
139 (List.map safe_buri_of_suri uri_to_remove))
141 (* cleand the already visided baseuris *)
145 if Hashtbl.mem cache_of_processed_baseuri buri then false
149 (* now calculate the list of objects that belong to these baseuris *)
154 let inhabitants = HG.ls ~local:true (buri ^ "/") in
155 let inhabitants = List.filter
156 (function HGT.Ls_object _ -> true | _ -> false)
159 let inhabitants = List.map
161 | HGT.Ls_object e -> buri ^ "/" ^ e.HGT.uri
167 with HGT.Invalid_URI u ->
168 HLog.error ("We were listing an invalid buri: " ^ u);
171 let uri_to_remove_from_db =
174 let dbu = db_uris_of_baseuri buri in
178 let uri_to_remove = uri_to_remove @ uri_to_remove_from_db in
180 HExtlib.list_uniq (List.sort Pervasives.compare uri_to_remove) in
181 (* now we want the list of all uri that depend on them *)
184 (fun acc u -> one_step_depend cache_of_processed_baseuri u dbtype dbd @ acc)
188 HExtlib.list_uniq (List.fast_sort Pervasives.compare depend)
190 uri_to_remove, depend
193 let rec close_db cache_of_processed_baseuri uris next =
197 let uris, next = close_uri_list cache_of_processed_baseuri l in
198 close_db cache_of_processed_baseuri uris next @ uris
201 let cleaned_no = ref 0;;
203 (** TODO repellent code ... *)
204 let moo_root_dir = lazy (
206 List.assoc "cic:/matita/"
210 Str.split (Str.regexp "[ \t\r\n]+") (HExtlib.trim_blanks pair)
214 (Helm_registry.get_list Helm_registry.string "getter.prefix"))
216 String.sub url 7 (String.length url - 7))
220 let close_db _cache_of_processed_baseuri uris _next =
221 uris (* MATITA 1.0 *)
224 let clean_baseuris ?verbose:(_=true) _buris =
225 (* MATITA 1.0 *) () (*
226 let cache_of_processed_baseuri = Hashtbl.create 1024 in
227 let buris = List.map Http_getter_misc.strip_trailing_slash buris in
228 debug_prerr "clean_baseuris called on:";
230 List.iter debug_prerr buris;
231 let l = close_db cache_of_processed_baseuri [] buris in
232 let l = HExtlib.list_uniq (List.fast_sort Pervasives.compare l) in
233 let l = List.map NUri.uri_of_string l in
234 debug_prerr "clean_baseuri will remove:";
236 List.iter (fun u -> debug_prerr (NUri.string_of_uri u)) l;
242 LibraryMisc.lexicon_file_of_baseuri
243 ~must_exist:false ~writable:true ~baseuri
245 HExtlib.safe_remove lexiconfile;
246 HExtlib.rmdir_descend (Filename.chop_extension lexiconfile)
247 with Http_getter_types.Key_not_found _ -> ())
248 (HExtlib.list_uniq (List.fast_sort Pervasives.compare
249 (List.map NUri.baseuri_of_uri l @ buris)))