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;;
38 let strip_xpointer s = Pcre.replace ~pat:"#.*$" s ;;
40 let safe_buri_of_suri suri =
42 UM.buri_of_uri (UM.uri_of_string suri)
44 UM.IllFormedUri _ -> suri
46 let one_step_depend cache_of_processed_baseuri suri dbtype dbd =
47 let buri = safe_buri_of_suri suri in
48 if Hashtbl.mem cache_of_processed_baseuri buri then
52 Hashtbl.add cache_of_processed_baseuri buri true;
54 let buri = buri ^ "/" in
55 let buri = HSql.escape dbtype dbd buri in
56 let obj_tbl = MetadataTypes.obj_tbl () in
57 if HSql.isMysql dbtype dbd then
58 sprintf ("SELECT source, h_occurrence FROM %s WHERE "
59 ^^ "h_occurrence REGEXP '"^^
60 "^%s\\([^/]+\\|[^/]+#xpointer.*\\)$"^^"'")
64 sprintf ("SELECT source, h_occurrence FROM %s WHERE "
65 ^^ "REGEXP(h_occurrence, '"^^
66 "^%s\\([^/]+\\|[^/]+#xpointer.*\\)$"^^"')")
68 (* implementation with vanilla ocaml-sqlite3
69 HLog.debug "Warning SELECT without REGEXP";
71 ("SELECT source, h_occurrence FROM %s WHERE " ^^
72 "h_occurrence LIKE '%s%%' " ^^ HSql.escape_string_for_like)
77 let rc = HSql.exec dbtype dbd query in
81 match row.(0), row.(1) with
82 | Some uri, Some occ when
83 Filename.dirname (strip_xpointer occ) = buri ->
86 let l = List.sort Pervasives.compare !l in
89 exn -> raise exn (* no errors should be accepted *)
92 let db_uris_of_baseuri buri =
93 let dbd = LibraryDb.instance () in
95 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
98 let buri = buri ^ "/" in
99 let buri = HSql.escape dbtype dbd buri in
100 let obj_tbl = MetadataTypes.name_tbl () in
101 if HSql.isMysql dbtype dbd then
102 sprintf ("SELECT source FROM %s WHERE "
103 ^^ "source REGEXP '^%s[^/]*(#xpointer.*)?$'") obj_tbl buri
106 sprintf ("SELECT source FROM %s WHERE "
107 ^^ "REGEXP(source, '^%s[^/]*\\(#xpointer.*\\)?$')") obj_tbl buri
111 let rc = HSql.exec dbtype dbd query in
116 | 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 cache_of_processed_baseuri 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 cache_of_processed_baseuri u dbtype dbd @ acc)
183 HExtlib.list_uniq (List.fast_sort Pervasives.compare depend)
185 uri_to_remove, depend
188 let rec close_db cache_of_processed_baseuri uris next =
192 let uris, next = close_uri_list cache_of_processed_baseuri l in
193 close_db cache_of_processed_baseuri uris next @ uris
196 let cleaned_no = ref 0;;
198 (** TODO repellent code ... *)
199 let moo_root_dir = lazy (
201 List.assoc "cic:/matita/"
205 Str.split (Str.regexp "[ \t\r\n]+") (HExtlib.trim_blanks pair)
209 (Helm_registry.get_list Helm_registry.string "getter.prefix"))
211 String.sub url 7 (String.length url - 7))
214 let clean_baseuris ?(verbose=true) buris =
215 let cache_of_processed_baseuri = Hashtbl.create 1024 in
216 let dbd = LibraryDb.instance () in
218 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
220 Hashtbl.clear cache_of_processed_baseuri;
221 let buris = List.map Http_getter_misc.strip_trailing_slash buris in
222 debug_prerr "clean_baseuris called on:";
224 List.iter debug_prerr buris;
225 let l = close_db cache_of_processed_baseuri [] buris in
226 let l = HExtlib.list_uniq (List.fast_sort Pervasives.compare l) in
227 let l = List.map UriManager.uri_of_string l in
228 debug_prerr "clean_baseuri will remove:";
230 List.iter (fun u -> debug_prerr (UriManager.string_of_uri u)) l;
235 LibraryMisc.obj_file_of_baseuri ~must_exist:false ~writable:true ~baseuri
237 HExtlib.safe_remove obj_file ;
239 (LibraryMisc.lexicon_file_of_baseuri
240 ~must_exist:false ~writable:true ~baseuri) ;
241 HExtlib.rmdir_descend (Filename.chop_extension obj_file)
242 with Http_getter_types.Key_not_found _ -> ())
243 (HExtlib.list_uniq (List.fast_sort Pervasives.compare
244 (List.map (UriManager.buri_of_uri) l @ buris)));
246 (let last_baseuri = ref "" in
248 let buri = UriManager.buri_of_uri uri in
249 if buri <> !last_baseuri then
251 if not (Helm_registry.get_bool "matita.verbose") then
252 (print_endline ("matitaclean " ^ buri ^ "/");flush stdout)
254 HLog.message ("Removing: " ^ buri ^ "/*");
257 LibrarySync.remove_obj uri
259 if HSql.isMysql dbtype dbd then
261 cleaned_no := !cleaned_no + List.length l;
262 if !cleaned_no > 30 && HSql.isMysql dbtype dbd then
267 ignore (HSql.exec dbtype dbd ("OPTIMIZE TABLE " ^ table)))
268 [MetadataTypes.name_tbl (); MetadataTypes.rel_tbl ();
269 MetadataTypes.sort_tbl (); MetadataTypes.obj_tbl();
270 MetadataTypes.count_tbl()]