]> matita.cs.unibo.it Git - helm.git/blob - matita/components/library/libraryClean.ml
cicNotation* ==> notation*
[helm.git] / matita / components / library / libraryClean.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 open Printf
29
30 let debug = false
31 let debug_prerr = if debug then prerr_endline else ignore
32
33 module HGT = Http_getter_types;;
34 module HG = Http_getter;;
35 module UM = UriManager;;
36
37 let decompile = ref (fun ~baseuri -> assert false);;
38 let set_decompile_cb f = decompile := f;;
39
40 let strip_xpointer s = Pcre.replace ~pat:"#.*$" s ;;
41
42 let safe_buri_of_suri suri =
43   try
44     UM.buri_of_uri (UM.uri_of_string suri)
45   with
46     UM.IllFormedUri _ -> suri
47
48 let one_step_depend cache_of_processed_baseuri suri dbtype dbd =
49   assert false (* MATITA 1.0
50   let buri = safe_buri_of_suri suri in
51   if Hashtbl.mem cache_of_processed_baseuri buri then 
52     []
53   else
54     begin
55       Hashtbl.add cache_of_processed_baseuri buri true;
56       let query = 
57         let buri = buri ^ "/" in 
58         let buri = HSql.escape dbtype dbd buri in
59         let obj_tbl = MetadataTypes.obj_tbl () in
60         if HSql.isMysql dbtype dbd then        
61           sprintf ("SELECT source, h_occurrence FROM %s WHERE " 
62             ^^ "h_occurrence REGEXP '"^^
63             "^%s\\([^/]+\\|[^/]+#xpointer.*\\)$"^^"'")
64           obj_tbl buri
65         else
66           begin
67             sprintf ("SELECT source, h_occurrence FROM %s WHERE " 
68             ^^ "REGEXP(h_occurrence, '"^^
69             "^%s\\([^/]+\\|[^/]+#xpointer.*\\)$"^^"')")
70             obj_tbl buri
71             (* implementation with vanilla ocaml-sqlite3
72             HLog.debug "Warning SELECT without REGEXP";
73             sprintf
74               ("SELECT source, h_occurrence FROM %s WHERE " ^^ 
75                "h_occurrence LIKE '%s%%' " ^^ HSql.escape_string_for_like)
76                   obj_tbl buri*)
77           end
78       in
79       try 
80         let rc = HSql.exec dbtype dbd query in
81         let l = ref [] in
82         HSql.iter rc (
83           fun row -> 
84             match row.(0), row.(1) with 
85             | Some uri, Some occ when 
86                 Filename.dirname (strip_xpointer occ) = buri -> 
87                   l := uri :: !l
88             | _ -> ());
89         let l = List.sort Pervasives.compare !l in
90         HExtlib.list_uniq l
91       with
92         exn -> raise exn (* no errors should be accepted *)
93     end
94     *)
95     
96 let db_uris_of_baseuri buri =
97   [] (* MATITA 1.0
98  let dbd = LibraryDb.instance () in
99  let dbtype = 
100    if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
101  in
102  let query = 
103   let buri = buri ^ "/" in 
104   let buri = HSql.escape dbtype dbd buri in
105   let obj_tbl = MetadataTypes.name_tbl () in
106   if HSql.isMysql dbtype dbd then        
107     sprintf ("SELECT source FROM %s WHERE " 
108     ^^ "source REGEXP '^%s[^/]*(#xpointer.*)?$'") obj_tbl buri
109   else
110    begin
111     sprintf ("SELECT source FROM %s WHERE " 
112       ^^ "REGEXP(source, '^%s[^/]*\\(#xpointer.*\\)?$')") obj_tbl buri
113    end
114  in
115  try 
116   let rc = HSql.exec dbtype dbd query in
117   let l = ref [] in
118   HSql.iter rc (
119     fun row -> 
120       match row.(0) with 
121       | Some uri when Filename.dirname (strip_xpointer uri) = buri -> 
122           l := uri :: !l
123       | _ -> ());
124   let l = List.sort Pervasives.compare !l in
125   HExtlib.list_uniq l
126  with
127   exn -> raise exn (* no errors should be accepted *)
128   *)
129 ;;
130
131 let close_uri_list cache_of_processed_baseuri uri_to_remove =
132   let dbd = LibraryDb.instance () in
133   let dbtype = 
134     if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
135   in
136   (* to remove an uri you have to remove the whole script *)
137   let buri_to_remove = 
138     HExtlib.list_uniq 
139       (List.fast_sort Pervasives.compare 
140         (List.map safe_buri_of_suri uri_to_remove))
141   in
142   (* cleand the already visided baseuris *)
143   let buri_to_remove = 
144     List.filter 
145       (fun buri -> 
146         if Hashtbl.mem cache_of_processed_baseuri buri then false
147         else true)
148       buri_to_remove
149   in
150   (* now calculate the list of objects that belong to these baseuris *)
151   let uri_to_remove = 
152     try
153       List.fold_left 
154         (fun acc buri ->
155           let inhabitants = HG.ls ~local:true (buri ^ "/") in
156           let inhabitants = List.filter 
157               (function HGT.Ls_object _ -> true | _ -> false) 
158             inhabitants
159           in
160           let inhabitants = List.map 
161               (function 
162                | HGT.Ls_object e -> buri ^ "/" ^ e.HGT.uri 
163                | _ -> assert false)
164             inhabitants
165           in
166           inhabitants @ acc)
167       [] buri_to_remove 
168     with HGT.Invalid_URI u -> 
169       HLog.error ("We were listing an invalid buri: " ^ u);
170       exit 1
171   in
172   let uri_to_remove_from_db =
173    List.fold_left 
174      (fun acc buri -> 
175        let dbu = db_uris_of_baseuri buri in
176        dbu @ acc
177      ) [] buri_to_remove 
178   in
179   let uri_to_remove = uri_to_remove @ uri_to_remove_from_db in
180   let uri_to_remove =
181    HExtlib.list_uniq (List.sort Pervasives.compare uri_to_remove) in
182   (* now we want the list of all uri that depend on them *) 
183   let depend = 
184     List.fold_left
185     (fun acc u -> one_step_depend cache_of_processed_baseuri u dbtype dbd @ acc)
186     [] uri_to_remove
187   in
188   let depend = 
189     HExtlib.list_uniq (List.fast_sort Pervasives.compare depend) 
190   in
191   uri_to_remove, depend
192 ;;
193
194 let rec close_db cache_of_processed_baseuri uris next =
195   match next with
196   | [] -> uris
197   | l -> 
198      let uris, next = close_uri_list cache_of_processed_baseuri l in
199      close_db cache_of_processed_baseuri uris next @ uris
200 ;;
201   
202 let cleaned_no = ref 0;;
203
204   (** TODO repellent code ... *)
205 let moo_root_dir = lazy (
206   let url =
207     List.assoc "cic:/matita/"
208       (List.map
209         (fun pair ->
210           match
211             Str.split (Str.regexp "[ \t\r\n]+") (HExtlib.trim_blanks pair)
212           with
213           | a::b::_ -> a, b
214           | _ -> assert false)
215         (Helm_registry.get_list Helm_registry.string "getter.prefix"))
216   in
217   String.sub url 7 (String.length url - 7))
218 ;;
219
220 let clean_baseuris ?(verbose=true) buris =
221   prerr_endline "CLEAN_BASEURIS to be removed MATITA 1.0"; (* MATITA 1.0
222   let cache_of_processed_baseuri = Hashtbl.create 1024 in
223   let dbd = LibraryDb.instance () in
224   let dbtype = 
225     if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
226   in
227   Hashtbl.clear cache_of_processed_baseuri;
228   let buris = List.map Http_getter_misc.strip_trailing_slash buris in
229   debug_prerr "clean_baseuris called on:";
230   if debug then
231     List.iter debug_prerr buris; 
232   let l = close_db cache_of_processed_baseuri [] buris in
233   let l = HExtlib.list_uniq (List.fast_sort Pervasives.compare l) in
234   let l = List.map UriManager.uri_of_string l in
235   debug_prerr "clean_baseuri will remove:";
236   if debug then
237     List.iter (fun u -> debug_prerr (UriManager.string_of_uri u)) l; 
238   List.iter
239    (fun baseuri ->
240      !decompile ~baseuri;
241      try 
242       let obj_file =
243        LibraryMisc.obj_file_of_baseuri ~must_exist:false ~writable:true ~baseuri
244       in
245        HExtlib.safe_remove obj_file ;
246        HExtlib.safe_remove 
247          (LibraryMisc.lexicon_file_of_baseuri 
248            ~must_exist:false ~writable:true ~baseuri) ;
249        HExtlib.rmdir_descend (Filename.chop_extension obj_file)
250      with Http_getter_types.Key_not_found _ -> ())
251    (HExtlib.list_uniq (List.fast_sort Pervasives.compare
252      (List.map (UriManager.buri_of_uri) l @ buris)));
253   List.iter
254    (let last_baseuri = ref "" in
255     fun uri ->
256      let buri = UriManager.buri_of_uri uri in
257      if buri <> !last_baseuri then
258       begin
259         if not (Helm_registry.get_bool "matita.verbose") then
260             (print_endline ("matitaclean " ^ buri ^ "/");flush stdout)
261           else 
262             HLog.message ("Removing: " ^ buri ^ "/*");
263        last_baseuri := buri
264       end;
265      LibrarySync.remove_obj uri
266    ) l;
267   if HSql.isMysql dbtype dbd then
268    begin
269    cleaned_no := !cleaned_no + List.length l;
270    if !cleaned_no > 30 && HSql.isMysql dbtype dbd then
271     begin
272      cleaned_no := 0;
273      List.iter
274       (function table ->
275         ignore (HSql.exec dbtype dbd ("OPTIMIZE TABLE " ^ table)))
276       [MetadataTypes.name_tbl (); MetadataTypes.rel_tbl ();
277        MetadataTypes.sort_tbl (); MetadataTypes.obj_tbl();
278        MetadataTypes.count_tbl()]
279     end
280    end
281    *)