1 (* Copyright (C) 2002-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://cs.unibo.it/helm/.
29 let debug_print s = if debug then prerr_endline s
30 let _ = Http_common.debug := false
32 exception Chat_unfinished
33 exception Unbound_identifier of string
34 exception Invalid_action of string (* invalid action for "/search" method *)
36 let daemon_name = "Moogle"
37 let configuration_file =
38 "/projects/helm/daemons/searchEngine.debug/moogle.conf.xml"
41 "ACTION"; "ADVANCED"; "ADVANCED_CHECKED"; "CHOICES"; "CURRENT_CHOICES";
42 "EXPRESSION"; "ID"; "IDEN"; "ID_TO_URIS"; "INTERPRETATIONS";
43 "INTERPRETATIONS_LABELS"; "MSG"; "NEW_ALIASES"; "NEXT_LINK"; "NO_CHOICES";
44 "PAGE"; "PAGES"; "PREV_LINK"; "QUERY_KIND"; "QUERY_SUMMARY"; "RESULTS";
45 "SEARCH_ENGINE_URL"; "SIMPLE_CHECKED"; "TITLE";
49 let regexps = Hashtbl.create 25 in
51 (fun tag -> Hashtbl.add regexps tag (Pcre.regexp (sprintf "@%s@" tag)))
55 Hashtbl.find regexps name
56 with Not_found -> assert false
58 (* First of all we load the configuration *)
59 let _ = Helm_registry.load_from configuration_file
60 let port = Helm_registry.get_int "search_engine.port"
61 let pages_dir = Helm_registry.get "search_engine.html_dir"
63 let interactive_interpretation_choice_TPL = pages_dir ^ "/moogle_chat2.html"
64 let moogle_TPL = pages_dir ^ "/moogle.html"
67 let ic = Unix.open_process_in "hostname -f" in
68 let hostname = input_line ic in
69 ignore (Unix.close_process_in ic);
70 sprintf "http://%s:%d" hostname port
71 let _ = Helm_registry.set "search_engine.my_own_url" my_own_url
73 let bad_request body outchan =
74 Http_daemon.respond_error ~code:(`Status (`Client_error `Bad_request)) ~body
77 (** chain application of Pcre substitutions *)
78 let rec apply_substs substs line =
81 | (rex, templ) :: rest -> apply_substs rest (Pcre.replace ~rex ~templ line)
82 (** fold like function on files *)
83 let fold_file f init fname =
84 let inchan = open_in fname in
85 let rec fold_lines' value =
87 let line = input_line inchan in
88 fold_lines' (f value line)
89 with End_of_file -> value
91 let res = (try fold_lines' init with e -> (close_in inchan; raise e)) in
94 (** iter like function on files *)
95 let iter_file f = fold_file (fun _ line -> f line) ()
96 let javascript_quote s =
97 let rex = Pcre.regexp "'" in
98 let rex' = Pcre.regexp "\"" in
99 Pcre.replace ~rex ~templ:"\\'"
100 (Pcre.replace ~rex:rex' ~templ:"\\\"" s)
102 let len = String.length s in
103 String.sub s 1 (len-1)
105 let len = String.length s in
106 let suffix = String.sub s (len-4) 4 in
107 not (suffix = ".var")
109 let add_param_substs params =
112 let key' = (Pcre.extract ~pat:"param\\.(.*)" key).(1) in
113 Pcre.regexp ("@" ^ key' ^ "@"), value)
115 (fun ((key,_) as p) -> Pcre.pmatch ~pat:"^param\\." key)
118 let page_RE = Pcre.regexp "¶m\\.page=\\d+"
120 let query_kind_of_req (req: Http_types.request) =
122 | "/match" -> "Match"
124 | "/locate" -> "Locate"
128 let send_results results
129 ?(id_to_uris = CicTextualParser2.EnvironmentP3.of_string "")
130 (req: Http_types.request) outchan
132 let query_kind = query_kind_of_req req in
133 let page_link anchor page =
135 let this = req#param "this" in
137 if Pcre.pmatch ~rex:page_RE this then
138 Pcre.replace ~rex:page_RE ~templ:(sprintf "¶m.page=%d" page)
141 sprintf "%s¶m.page=%d" this page
143 let target = Pcre.replace ~pat:"&" ~templ:"&" target in
144 sprintf "<a href=\"%s\">%s</a>" target anchor
145 with Http_types.Param_not_found _ -> ""
147 Http_daemon.send_basic_headers ~code:(`Code 200) outchan ;
148 Http_daemon.send_header "Content-Type" "text/xml" outchan;
149 Http_daemon.send_CRLF outchan ;
152 | `Results results ->
153 let page = try int_of_string (req#param "page") with _ -> 1 in
154 let results_no = List.length results in
155 let results_per_page =
156 Helm_registry.get_int "search_engine.results_per_page"
159 if results_no mod results_per_page = 0 then
160 results_no / results_per_page
162 results_no / results_per_page + 1
164 let pages = if pages = 0 then 1 else pages in
165 let (summary, results) = MooglePp.theory_of_result page results in
166 [ tag "PAGE", string_of_int page;
167 tag "PAGES", string_of_int pages;
168 tag "PREV_LINK", (if page > 1 then page_link "Prev" (page-1) else "");
170 (if page < pages then page_link "Next" (page+1) else "");
171 tag "QUERY_KIND", query_kind;
172 tag "QUERY_SUMMARY", summary;
173 tag "RESULTS", results ]
179 tag "QUERY_KIND", query_kind;
180 tag "QUERY_SUMMARY", "error";
186 with Http_types.Param_not_found _ -> "no"
189 (tag "SEARCH_ENGINE_URL", my_own_url) ::
190 (tag "ADVANCED", advanced) ::
191 (tag "EXPRESSION", req#param "expression") ::
192 add_param_substs req#params @
193 (if advanced = "no" then
194 [ tag "SIMPLE_CHECKED", "checked='true'";
195 tag "ADVANCED_CHECKED", "" ]
197 [ tag "SIMPLE_CHECKED", "";
198 tag "ADVANCED_CHECKED", "checked='true'" ]) @
204 CicTextualParser2.EnvironmentP3.to_string id_to_uris
208 (* CSC: Bug here: this is a string, not an array! *)
209 ((tag "NEW_ALIASES", "'" ^ javascript_quote new_aliases ^ "'") ::
213 output_string outchan (processed_line ^ "\n"))
216 let exec_action dbd (req: Http_types.request) outchan =
217 let term_str = req#param "expression" in
218 let (context, metasenv) = ([], []) in
220 try req#param "aliases"
221 with Http_types.Param_not_found _ -> ""
223 let parse_interpretation_choices choices =
224 List.map int_of_string (Pcre.split ~pat:" " choices) in
225 let parse_choices choices_raw =
226 let choices = Pcre.split ~pat:";" choices_raw in
229 match Pcre.split ~pat:"\\s" x with
231 | id::tail when id<>"" ->
234 Some (List.map (fun u -> Netencoding.Url.decode u) tail)
237 | _ -> failwith "Can't parse choices")
241 let id_to_uris = CicTextualParser2.EnvironmentP3.of_string id_to_uris_raw in
244 parse_choices (req#param "choices")
245 with Http_types.Param_not_found _ -> (fun _ -> None)
247 let interpretation_choices =
249 let choices_raw = req#param "interpretation_choices" in
250 if choices_raw = "" then None
251 else Some (parse_interpretation_choices choices_raw)
252 with Http_types.Param_not_found _ -> None
254 let module Chat: DisambiguateTypes.Callbacks =
256 let interactive_user_uri_choice ~selection_mode ?ok
257 ?enable_button_for_non_vars ~(title: string) ~(msg: string)
258 ~(id: string) (choices: string list)
260 match id_to_choices id with
261 | Some choices -> choices
262 | None -> List.filter nonvar choices
264 let interactive_interpretation_choice interpretations =
265 match interpretation_choices with
268 let html_interpretations =
269 MooglePp.html_of_interpretations interpretations
271 Http_daemon.send_basic_headers ~code:(`Code 200) outchan ;
272 Http_daemon.send_CRLF outchan ;
276 with Http_types.Param_not_found _ -> "no"
282 [tag "ADVANCED", advanced;
283 tag "INTERPRETATIONS", html_interpretations;
284 tag "CURRENT_CHOICES", req#param "choices";
285 tag "EXPRESSION", req#param "expression";
286 tag "ACTION", string_tail req#path ]
289 output_string outchan (processed_line ^ "\n"))
290 interactive_interpretation_choice_TPL;
291 raise Chat_unfinished
293 let input_or_locate_uri ~title ?id () =
295 | Some id -> raise (Unbound_identifier id)
296 | None -> assert false
299 let module Disambiguate' = Disambiguate.Make(Chat) in
300 let ast = CicTextualParser2.parse_term (Stream.of_string term_str) in
301 let (id_to_uris, metasenv, term) =
303 Disambiguate'.disambiguate_term dbd context metasenv ast id_to_uris
305 | [id_to_uris,metasenv,term,_] -> id_to_uris,metasenv,term
310 | "/match" -> MetadataQuery.match_term ~dbd term
312 let status = ProofEngineTypes.initial_status term metasenv in
313 let intros = PrimitiveTactics.intros_tac () in
314 let subgoals = ProofEngineTypes.apply_tactic intros status in
317 let (uri,metasenv,bo,ty) = proof in
318 List.map fst (MetadataQuery.hint ~dbd (proof, goal))
323 | Cic.MutInd (uri, typeno, _) ->
324 UriManager.string_of_uriref (uri, [typeno])
327 MetadataQuery.elim ~dbd uri
330 send_results ~id_to_uris (`Results uris) req outchan
332 let callback dbd (req: Http_types.request) outchan =
334 debug_print (sprintf "Received request: %s" req#path);
337 (* TODO implement "is_permitted" *)
338 (let is_permitted _ = true in
339 let page = req#param "url" in
342 bool_of_string (req#param "preprocess")
343 with Invalid_argument _ | Http_types.Param_not_found _ -> false)
346 | page when is_permitted page ->
347 (let fname = sprintf "%s/%s" pages_dir page in
348 Http_daemon.send_basic_headers ~code:(`Code 200) outchan;
349 Http_daemon.send_header "Content-Type" "text/html" outchan;
350 Http_daemon.send_CRLF outchan;
351 if preprocess then begin
354 output_string outchan
356 ((tag "SEARCH_ENGINE_URL", my_own_url) ::
357 (tag "ADVANCED", "no") ::
358 (tag "RESULTS", "") ::
359 add_param_substs req#params)
364 Http_daemon.send_file ~src:(Http_types.FileSrc fname) outchan)
365 | page -> Http_daemon.respond_forbidden ~url:page outchan))
366 | "/help" -> Http_daemon.respond ~body:daemon_name outchan
368 let initial_expression =
369 try req#param "expression" with Http_types.Param_not_found _ -> ""
372 Pcre.replace ~pat:"\\s*$"
373 (Pcre.replace ~pat:"^\\s*" initial_expression)
375 if expression = "" then
376 send_results (`Results []) req outchan
378 let results = MetadataQuery.locate ~dbd expression in
379 send_results (`Results results) req outchan
383 | "/match" -> exec_action dbd req outchan
385 Http_daemon.respond_error ~code:(`Status (`Client_error `Bad_request))
387 debug_print (sprintf "%s done!" req#path)
389 | Chat_unfinished -> ()
390 | Http_types.Param_not_found attr_name ->
391 bad_request (sprintf "Parameter '%s' is missing" attr_name) outchan
392 | CicTextualParser2.Parse_error (_, msg) ->
393 send_results (`Error (MooglePp.pp_error "Parse_error" msg)) req outchan
394 | Unbound_identifier id ->
395 send_results (`Error (MooglePp.pp_error "Unbound identifier" id)) req
398 let exn_string = Printexc.to_string exn in
399 debug_print exn_string;
400 let msg = MooglePp.pp_error "Uncaught exception" exn_string in
401 send_results (`Error msg) req outchan
404 printf "%s started and listening on port %d\n" daemon_name port;
405 printf "Current directory is %s\n" (Sys.getcwd ());
406 printf "HTML directory is %s\n" pages_dir;
408 Unix.putenv "http_proxy" "";
411 ~host:(Helm_registry.get "db.host")
412 ~database:(Helm_registry.get "db.database")
413 ~user:(Helm_registry.get "db.user")
416 Http_daemon.start' ~port (callback dbd);
417 printf "%s is terminating, bye!\n" daemon_name