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 cache_of_processed_baseuri = Hashtbl.create 1024
39 let one_step_depend suri dbtype dbd =
42 UM.buri_of_uri (UM.uri_of_string suri)
43 with UM.IllFormedUri _ -> suri
45 if Hashtbl.mem cache_of_processed_baseuri buri then
49 Hashtbl.add cache_of_processed_baseuri buri true;
51 let buri = buri ^ "/" in
52 let buri = HSql.escape dbtype dbd buri in
53 let obj_tbl = MetadataTypes.obj_tbl () in
54 if HSql.isMysql dbtype dbd then
55 sprintf ("SELECT source, h_occurrence FROM %s WHERE "
56 ^^ "h_occurrence REGEXP '^%s[^/]*$'") obj_tbl buri
59 sprintf ("SELECT source, h_occurrence FROM %s WHERE "
60 ^^ "REGEXP(h_occurrence, '^%s[^/]*$')") obj_tbl buri
61 (* implementation with vanilla ocaml-sqlite3
62 HLog.debug "Warning SELECT without REGEXP";
64 ("SELECT source, h_occurrence FROM %s WHERE " ^^
65 "h_occurrence LIKE '%s%%' " ^^ HSql.escape_string_for_like)
70 let rc = HSql.exec dbtype dbd query in
74 match row.(0), row.(1) with
75 | Some uri, Some occ when Filename.dirname occ = buri ->
78 let l = List.sort Pervasives.compare !l in
81 exn -> raise exn (* no errors should be accepted *)
84 let safe_buri_of_suri suri =
86 UM.buri_of_uri (UM.uri_of_string suri)
88 UM.IllFormedUri _ -> suri
90 let db_uris_of_baseuri buri =
91 let dbd = LibraryDb.instance () in
93 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
96 let buri = buri ^ "/" in
97 let buri = HSql.escape dbtype dbd buri in
98 let obj_tbl = MetadataTypes.name_tbl () in
99 if HSql.isMysql dbtype dbd then
100 sprintf ("SELECT source FROM %s WHERE "
101 ^^ "source REGEXP '^%s[^/]*(#xpointer.*)?$'") obj_tbl buri
104 sprintf ("SELECT source FROM %s WHERE "
105 ^^ "REGEXP(source, '^%s[^/]*\\(#xpointer.*\\)?$')") obj_tbl buri
109 let rc = HSql.exec dbtype dbd query in
110 let strip_xpointer s = Pcre.replace ~pat:"#.*$" s in
115 | Some uri when Filename.dirname (strip_xpointer uri) = buri ->
119 let l = List.sort Pervasives.compare !l in
122 exn -> raise exn (* no errors should be accepted *)
125 let close_uri_list uri_to_remove =
126 let dbd = LibraryDb.instance () in
128 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
130 (* to remove an uri you have to remove the whole script *)
133 (List.fast_sort Pervasives.compare
134 (List.map safe_buri_of_suri uri_to_remove))
136 (* cleand the already visided baseuris *)
140 if Hashtbl.mem cache_of_processed_baseuri buri then false
144 (* now calculate the list of objects that belong to these baseuris *)
149 let inhabitants = HG.ls ~local:true (buri ^ "/") in
150 let inhabitants = List.filter
151 (function HGT.Ls_object _ -> true | _ -> false)
154 let inhabitants = List.map
156 | HGT.Ls_object e -> buri ^ "/" ^ e.HGT.uri
162 with HGT.Invalid_URI u ->
163 HLog.error ("We were listing an invalid buri: " ^ u);
166 let uri_to_remove_from_db =
169 let dbu = db_uris_of_baseuri buri in
173 let uri_to_remove = uri_to_remove @ uri_to_remove_from_db in
175 HExtlib.list_uniq (List.sort Pervasives.compare uri_to_remove) in
176 (* now we want the list of all uri that depend on them *)
179 (fun acc u -> one_step_depend u dbtype dbd @ acc) [] uri_to_remove
182 HExtlib.list_uniq (List.fast_sort Pervasives.compare depend)
184 uri_to_remove, depend
186 let rec close_db uris next =
189 | l -> let uris, next = close_uri_list l in close_db uris next @ uris
191 let cleaned_no = ref 0;;
193 (** TODO repellent code ... *)
194 let moo_root_dir = lazy (
196 List.assoc "cic:/matita/"
200 Str.split (Str.regexp "[ \t\r\n]+") (HExtlib.trim_blanks pair)
204 (Helm_registry.get_list Helm_registry.string "getter.prefix"))
206 String.sub url 7 (String.length url - 7) (* remove heading "file:///" *)
209 let clean_baseuris ?(verbose=true) buris =
210 let dbd = LibraryDb.instance () in
212 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
214 Hashtbl.clear cache_of_processed_baseuri;
215 let buris = List.map Http_getter_misc.strip_trailing_slash buris in
216 debug_prerr "clean_baseuris called on:";
218 List.iter debug_prerr buris;
219 let l = close_db [] buris in
220 let l = HExtlib.list_uniq (List.fast_sort Pervasives.compare l) in
221 let l = List.map UriManager.uri_of_string l in
222 debug_prerr "clean_baseuri will remove:";
224 List.iter (fun u -> debug_prerr (UriManager.string_of_uri u)) l;
229 LibraryMisc.obj_file_of_baseuri ~must_exist:false ~writable:true ~baseuri
231 HExtlib.safe_remove obj_file ;
233 (LibraryMisc.lexicon_file_of_baseuri
234 ~must_exist:false ~writable:true ~baseuri) ;
235 HExtlib.rmdir_descend (Filename.chop_extension obj_file)
236 with Http_getter_types.Key_not_found _ -> ())
237 (HExtlib.list_uniq (List.fast_sort Pervasives.compare
238 (List.map (UriManager.buri_of_uri) l @ buris)));
240 (let last_baseuri = ref "" in
242 let buri = UriManager.buri_of_uri uri in
243 if buri <> !last_baseuri then
245 if Helm_registry.get_bool "matita.bench" then
246 (print_endline ("matitaclean " ^ buri ^ "/");flush stdout)
248 HLog.message ("Removing: " ^ buri ^ "/*");
251 LibrarySync.remove_obj uri
253 if HSql.isMysql dbtype dbd then
255 cleaned_no := !cleaned_no + List.length l;
256 if !cleaned_no > 30 && HSql.isMysql dbtype dbd then
261 ignore (HSql.exec dbtype dbd ("OPTIMIZE TABLE " ^ table)))
262 [MetadataTypes.name_tbl (); MetadataTypes.rel_tbl ();
263 MetadataTypes.sort_tbl (); MetadataTypes.obj_tbl();
264 MetadataTypes.count_tbl()]