1 (* Copyright (C) 2002, 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/.
27 let debug_print s = if debug then prerr_endline s;;
28 (* Http_common.debug := true;; *)
32 let postgresConnectionString =
34 Sys.getenv "POSTGRESQL_CONNECTION_STRING"
36 Not_found -> "host=mowgli.cs.unibo.it dbname=helm_mowgli_new_schema user=helm"
39 let daemon_name = "Search Engine";;
40 let default_port = 58085;;
41 let port_env_var = "SEARCH_ENGINE_PORT";;
45 Sys.getenv "SEARCH_ENGINE_HTML_DIR"
46 with Not_found -> "html" (* relative to searchEngine's document root *)
48 let interactive_user_uri_choice_TPL = pages_dir ^ "/templateambigpdq1.html";;
49 let interactive_interpretation_choice_TPL = pages_dir ^ "/templateambigpdq2.html";;
50 let final_results_TPL = pages_dir ^ "/templateambigpdq3.html";;
52 exception Chat_unfinished
54 (** pretty print a MathQL query result to a string *)
55 let text_of_result result sep =
56 let res_string = ref "" in
57 let app = function s -> res_string := !res_string ^ s in
58 MQueryUtil.text_of_result app result sep ;
62 (** chain application of Pcre substitutions *)
63 let rec apply_substs substs line =
66 | (rex, templ) :: rest -> apply_substs rest (Pcre.replace ~rex ~templ line)
67 (** fold like function on files *)
68 let fold_file f init fname =
69 let inchan = open_in fname in
70 let rec fold_lines' value =
72 let line = input_line inchan in
73 fold_lines' (f value line)
74 with End_of_file -> value
76 let res = (try fold_lines' init with e -> (close_in inchan; raise e)) in
79 (** iter like function on files *)
80 let iter_file f = fold_file (fun _ line -> f line) ()
82 let (title_tag_RE, choices_tag_RE, msg_tag_RE, id_to_uris_RE, id_RE,
83 interpretations_RE, interpretations_labels_RE, results_RE, new_aliases_RE) =
84 (Pcre.regexp "@TITLE@", Pcre.regexp "@CHOICES@", Pcre.regexp "@MSG@",
85 Pcre.regexp "@ID_TO_URIS@", Pcre.regexp "@ID@",
86 Pcre.regexp "@INTERPRETATIONS@", Pcre.regexp "@INTERPRETATIONS_LABELS@",
87 Pcre.regexp "@RESULTS@", Pcre.regexp "@NEW_ALIASES@")
91 int_of_string (Sys.getenv port_env_var)
93 | Not_found -> default_port
94 | Failure "int_of_string" ->
95 prerr_endline "Warning: invalid port, reverting to default";
98 let pp_result result =
99 let res_string = text_of_result result "\n" in
100 (sprintf "<html>\n<head>\n</head>\n<body>\n<pre>%s</pre>\n</body>\n</html>"
103 let pp_error = sprintf "<html><body><h1>Error: %s</h1></body></html>" in
104 let bad_request body outchan =
105 Http_daemon.respond_error ~status:(`Client_error `Bad_request) ~body outchan
107 let contype = "Content-Type", "text/html" in
109 (* SEARCH ENGINE functions *)
111 let refine_constraints (x, y, z) = (x, y, z), (Some x, Some y, Some z) in
113 (* HTTP DAEMON CALLBACK *)
115 let callback (req: Http_types.request) outchan =
117 debug_print (sprintf "Received request: %s" req#path);
118 if req#path <> "/getpage" then
119 Mqint.init postgresConnectionString;
122 let query_string = req#param "query" in
123 let lexbuf = Lexing.from_string query_string in
124 let query = MQueryUtil.query_of_text lexbuf in
125 let result = MQueryGenerator.execute_query query in
126 let result_string = text_of_result result "\n" in
129 (sprintf "<html><body><pre>%s</pre></body></html>" result_string)
130 ~headers:[contype] outchan
132 let id = req#param "id" in
133 let result = MQueryGenerator.locate id in
134 Http_daemon.respond ~headers:[contype] ~body:(pp_result result) outchan
136 (* TODO implement "is_permitted" *)
137 (let is_permitted _ = true in
138 let remove_fragment uri = Pcre.replace ~pat:"#.*" uri in
139 let page = remove_fragment (req#param "url") in
141 | page when is_permitted page ->
142 let fname = sprintf "%s/%s" pages_dir (remove_fragment page) in
143 debug_print (sprintf "Returning file: %s" fname);
144 Http_daemon.send_basic_headers ~code:200 outchan;
145 Http_daemon.send_CRLF outchan;
146 Http_daemon.send_file ~name:fname outchan
147 | page -> Http_daemon.respond_forbidden ~url:page outchan)
148 | "/searchPattern" ->
149 let term_string = req#param "term" in
150 let lexbuf = Lexing.from_string term_string in
151 let (context, metasenv) = ([], []) in
152 let (dom, mk_metasenv_and_expr) =
153 CicTextualParserContext.main
154 ~context ~metasenv CicTextualLexer.token lexbuf
156 let id_to_uris_raw = req#param "aliases" in
157 let tokens = Pcre.split ~pat:"\\s" id_to_uris_raw in
158 let rec parse_tokens keys lookup = function (* TODO spostarla fuori *)
160 | "alias" :: key :: value :: rest ->
161 let key' = CicTextualParser0.Id key in
167 (CicTextualParser0.Uri (MQueryMisc.cic_textual_parser_uri_of_string value))
170 | _ -> failwith "Can't parse aliases"
172 let parse_choices choices_raw =
173 let choices = Pcre.split ~pat:";" choices_raw in
176 match Pcre.split ~pat:"\\s" x with
178 | id::tail when id<>"" ->
181 Some (List.map (fun u -> Netencoding.Url.decode u) tail)
184 | _ -> failwith "Can't parse choices")
188 let (id_to_uris : Disambiguate.domain_and_interpretation) =
189 parse_tokens [] (fun _ -> None) tokens in
192 let choices_raw = req#param "choices" in
193 parse_choices choices_raw
194 with Http_types.Param_not_found _ -> (fun _ -> None)
196 let module Chat: Disambiguate.Callbacks =
199 let get_metasenv () =
200 !CicTextualParser0.metasenv
202 let set_metasenv metasenv =
203 CicTextualParser0.metasenv := metasenv
205 let output_html = prerr_endline
207 let interactive_user_uri_choice
209 ?enable_button_for_non_vars ~(title: string) ~(msg: string)
210 ~(id: string) (choices: string list)
212 (match id_to_choices id with
213 | Some choices -> choices
215 let msg = Pcre.replace ~pat:"\"" ~templ:"\\\"" msg in
216 (match selection_mode with
217 | `SINGLE -> assert false
221 let formatted_choices =
223 (List.map (fun uri -> sprintf "\"%s\"" uri) choices)
227 [title_tag_RE, title;
228 choices_tag_RE, formatted_choices;
230 id_to_uris_RE, id_to_uris_raw;
234 output_string outchan processed_line)
235 interactive_user_uri_choice_TPL;
236 raise Chat_unfinished))
238 let interactive_interpretation_choice interpretations =
239 let html_interpretations_labels =
244 (String.concat "<br />"
247 (sprintf "alias %s %s" id value))
252 let html_interpretations =
260 (sprintf "alias %s %s"
262 (MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format'
272 [interpretations_RE, html_interpretations;
273 interpretations_labels_RE, html_interpretations_labels]
276 output_string outchan processed_line)
277 interactive_interpretation_choice_TPL;
278 raise Chat_unfinished
280 let input_or_locate_uri ~title =
281 UriManager.uri_of_string "cic:/Coq/Init/DataTypes/nat_ind.con"
285 let module Disambiguate' = Disambiguate.Make (Chat) in
286 let (id_to_uris', metasenv', term') =
287 Disambiguate'.disambiguate_input
288 context metasenv dom mk_metasenv_and_expr id_to_uris
290 (match metasenv' with
292 let must = MQueryLevels2.get_constraints term' in
293 let must',only = refine_constraints must in
294 let results = MQueryGenerator.searchPattern must' only in
298 match id_to_uris' with
303 sprintf "\"alias %s cic:%s\""
305 CicTextualParser0.Id name -> name
306 | _ -> assert false (*CSC: completare *))
308 | None -> assert false
309 | Some (CicTextualParser0.Uri t) ->
310 MQueryMisc.string_of_cic_textual_parser_uri
312 | _ -> assert false (*CSC: completare *)))
317 [results_RE, text_of_result results "\n";
318 new_aliases_RE, new_aliases]
321 output_string outchan processed_line)
323 | _ -> (* unable to instantiate some implicit variable *)
326 ~body:"some implicit variables are still unistantiated :-("
330 Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan);
331 if req#path <> "/getpage" then
333 debug_print (sprintf "%s done!" req#path)
335 | Chat_unfinished -> prerr_endline "Chat unfinished, Try again!"
336 | Http_types.Param_not_found attr_name ->
337 bad_request (sprintf "Parameter '%s' is missing" attr_name) outchan
340 ~body:(pp_error ("Uncaught exception: " ^ (Printexc.to_string exc)))
343 printf "%s started and listening on port %d\n" daemon_name port;
344 printf "Current directory is %s\n" (Sys.getcwd ());
345 printf "HTML directory is %s\n" pages_dir;
347 Unix.putenv "http_proxy" "";
348 Mqint.set_database Mqint.postgres_db;
349 Http_daemon.start' ~port callback;
350 printf "%s is terminating, bye!\n" daemon_name