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/
31 let debug_prerr = if debug then prerr_endline else ignore
33 module HGT = Http_getter_types;;
34 module HG = Http_getter;;
35 module UM = UriManager;;
37 let decompile = ref (fun ~baseuri -> assert false);;
38 let set_decompile_cb f = decompile := f;;
40 let strip_xpointer s = Pcre.replace ~pat:"#.*$" s ;;
42 let safe_buri_of_suri suri =
44 UM.buri_of_uri (UM.uri_of_string suri)
46 UM.IllFormedUri _ -> suri
48 let one_step_depend cache_of_processed_baseuri suri dbtype dbd =
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 *)
94 let db_uris_of_baseuri buri =
95 let dbd = LibraryDb.instance () in
97 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
100 let buri = buri ^ "/" in
101 let buri = HSql.escape dbtype dbd buri in
102 let obj_tbl = MetadataTypes.name_tbl () in
103 if HSql.isMysql dbtype dbd then
104 sprintf ("SELECT source FROM %s WHERE "
105 ^^ "source REGEXP '^%s[^/]*(#xpointer.*)?$'") obj_tbl buri
108 sprintf ("SELECT source FROM %s WHERE "
109 ^^ "REGEXP(source, '^%s[^/]*\\(#xpointer.*\\)?$')") obj_tbl buri
113 let rc = HSql.exec dbtype dbd query in
118 | Some uri when Filename.dirname (strip_xpointer uri) = buri ->
121 let l = List.sort Pervasives.compare !l in
124 exn -> raise exn (* no errors should be accepted *)
127 let close_uri_list cache_of_processed_baseuri uri_to_remove =
128 let dbd = LibraryDb.instance () in
130 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
132 (* to remove an uri you have to remove the whole script *)
135 (List.fast_sort Pervasives.compare
136 (List.map safe_buri_of_suri uri_to_remove))
138 (* cleand the already visided baseuris *)
142 if Hashtbl.mem cache_of_processed_baseuri buri then false
146 (* now calculate the list of objects that belong to these baseuris *)
151 let inhabitants = HG.ls ~local:true (buri ^ "/") in
152 let inhabitants = List.filter
153 (function HGT.Ls_object _ -> true | _ -> false)
156 let inhabitants = List.map
158 | HGT.Ls_object e -> buri ^ "/" ^ e.HGT.uri
164 with HGT.Invalid_URI u ->
165 HLog.error ("We were listing an invalid buri: " ^ u);
168 let uri_to_remove_from_db =
171 let dbu = db_uris_of_baseuri buri in
175 let uri_to_remove = uri_to_remove @ uri_to_remove_from_db in
177 HExtlib.list_uniq (List.sort Pervasives.compare uri_to_remove) in
178 (* now we want the list of all uri that depend on them *)
181 (fun acc u -> one_step_depend cache_of_processed_baseuri u dbtype dbd @ acc)
185 HExtlib.list_uniq (List.fast_sort Pervasives.compare depend)
187 uri_to_remove, depend
190 let rec close_db cache_of_processed_baseuri uris next =
194 let uris, next = close_uri_list cache_of_processed_baseuri l in
195 close_db cache_of_processed_baseuri uris next @ uris
198 let cleaned_no = ref 0;;
200 (** TODO repellent code ... *)
201 let moo_root_dir = lazy (
203 List.assoc "cic:/matita/"
207 Str.split (Str.regexp "[ \t\r\n]+") (HExtlib.trim_blanks pair)
211 (Helm_registry.get_list Helm_registry.string "getter.prefix"))
213 String.sub url 7 (String.length url - 7))
216 let clean_baseuris ?(verbose=true) buris =
217 let cache_of_processed_baseuri = Hashtbl.create 1024 in
218 let dbd = LibraryDb.instance () in
220 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
222 Hashtbl.clear cache_of_processed_baseuri;
223 let buris = List.map Http_getter_misc.strip_trailing_slash buris in
224 debug_prerr "clean_baseuris called on:";
226 List.iter debug_prerr buris;
227 let l = close_db cache_of_processed_baseuri [] buris in
228 let l = HExtlib.list_uniq (List.fast_sort Pervasives.compare l) in
229 let l = List.map UriManager.uri_of_string l in
230 debug_prerr "clean_baseuri will remove:";
232 List.iter (fun u -> debug_prerr (UriManager.string_of_uri u)) l;
238 LibraryMisc.obj_file_of_baseuri ~must_exist:false ~writable:true ~baseuri
240 HExtlib.safe_remove obj_file ;
242 (LibraryMisc.lexicon_file_of_baseuri
243 ~must_exist:false ~writable:true ~baseuri) ;
244 HExtlib.rmdir_descend (Filename.chop_extension obj_file)
245 with Http_getter_types.Key_not_found _ -> ())
246 (HExtlib.list_uniq (List.fast_sort Pervasives.compare
247 (List.map (UriManager.buri_of_uri) l @ buris)));
249 (let last_baseuri = ref "" in
251 let buri = UriManager.buri_of_uri uri in
252 if buri <> !last_baseuri then
254 if not (Helm_registry.get_bool "matita.verbose") then
255 (print_endline ("matitaclean " ^ buri ^ "/");flush stdout)
257 HLog.message ("Removing: " ^ buri ^ "/*");
260 LibrarySync.remove_obj uri
262 if HSql.isMysql dbtype dbd then
264 cleaned_no := !cleaned_no + List.length l;
265 if !cleaned_no > 30 && HSql.isMysql dbtype dbd then
270 ignore (HSql.exec dbtype dbd ("OPTIMIZE TABLE " ^ table)))
271 [MetadataTypes.name_tbl (); MetadataTypes.rel_tbl ();
272 MetadataTypes.sort_tbl (); MetadataTypes.obj_tbl();
273 MetadataTypes.count_tbl()]