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";;
43 let pages_dir = "html";; (* relative to searchEngine's document root *)
44 let interactive_user_uri_choice_TPL = pages_dir ^ "/templateambigpdq1.html";;
45 let interactive_interpretation_choice_TPL = pages_dir ^ "/templateambigpdq2.html";;
46 let final_results_TPL = pages_dir ^ "/templateambigpdq3.html";;
48 exception Chat_unfinished
50 (** pretty print a MathQL query result to a string *)
51 let text_of_result result sep =
52 let res_string = ref "" in
53 let app = function s -> res_string := !res_string ^ s in
54 MQueryUtil.text_of_result app result sep ;
58 (** chain application of Pcre substitutions *)
59 let rec apply_substs substs line =
62 | (rex, templ) :: rest -> apply_substs rest (Pcre.replace ~rex ~templ line)
63 (** fold like function on files *)
64 let fold_file f init fname =
65 let inchan = open_in fname in
66 let rec fold_lines' value =
68 let line = input_line inchan in
69 fold_lines' (f value line)
70 with End_of_file -> value
72 let res = (try fold_lines' init with e -> (close_in inchan; raise e)) in
75 (** iter like function on files *)
76 let iter_file f = fold_file (fun _ line -> f line) ()
78 let (title_tag_RE, choices_tag_RE, msg_tag_RE, id_to_uris_RE, id_RE,
79 interpretations_RE, interpretations_labels_RE, results_RE, new_aliases_RE) =
80 (Pcre.regexp "@TITLE@", Pcre.regexp "@CHOICES@", Pcre.regexp "@MSG@",
81 Pcre.regexp "@ID_TO_URIS@", Pcre.regexp "@ID@",
82 Pcre.regexp "@INTERPRETATIONS@", Pcre.regexp "@INTERPRETATIONS_LABELS@",
83 Pcre.regexp "@RESULTS@", Pcre.regexp "@NEW_ALIASES@")
87 int_of_string (Sys.getenv port_env_var)
89 | Not_found -> default_port
90 | Failure "int_of_string" ->
91 prerr_endline "Warning: invalid port, reverting to default";
94 let pp_result result =
95 let res_string = text_of_result result "\n" in
96 (sprintf "<html>\n<head>\n</head>\n<body>\n<pre>%s</pre>\n</body>\n</html>"
99 let pp_error = sprintf "<html><body><h1>Error: %s</h1></body></html>" in
100 let bad_request body outchan =
101 Http_daemon.respond_error ~status:(`Client_error `Bad_request) ~body outchan
103 let contype = "Content-Type", "text/html" in
105 (* SEARCH ENGINE functions *)
107 let refine_constraints (x, y, z) = (x, y, z), (Some x, Some y, Some z) in
109 (* HTTP DAEMON CALLBACK *)
111 let callback (req: Http_types.request) outchan =
113 debug_print (sprintf "Received request: %s" req#path);
116 let query_string = req#param "query" in
117 let lexbuf = Lexing.from_string query_string in
118 let query = MQueryUtil.query_of_text lexbuf in
119 let result = MQueryGenerator.execute_query query in
120 let result_string = text_of_result result "\n" in
123 (sprintf "<html><body><pre>%s</pre></body></html>" result_string)
124 ~headers:[contype] outchan
126 let id = req#param "id" in
127 let result = MQueryGenerator.locate id in
128 Http_daemon.respond ~headers:[contype] ~body:(pp_result result) outchan
130 (* TODO implement "is_permitted" *)
131 (let is_permitted _ = true in
132 let remove_fragment uri = Pcre.replace ~pat:"#.*" uri in
133 let page = remove_fragment (req#param "url") in
135 | page when is_permitted page ->
136 Http_daemon.respond_file
137 ~fname:(sprintf "%s/%s" pages_dir (remove_fragment page)) outchan
138 | page -> Http_daemon.respond_forbidden ~url:page outchan)
139 | "/searchPattern" ->
140 let term_string = req#param "term" in
141 let lexbuf = Lexing.from_string term_string in
142 let (context, metasenv) = ([], []) in
143 let (dom, mk_metasenv_and_expr) =
144 CicTextualParserContext.main
145 ~context ~metasenv CicTextualLexer.token lexbuf
147 let id_to_uris_raw = req#param "aliases" in
148 let tokens = Pcre.split ~pat:"\\s" id_to_uris_raw in
149 let rec parse_tokens keys lookup = function (* TODO spostarla fuori *)
151 | "alias" :: key :: value :: rest ->
156 Some (MQueryMisc.cic_textual_parser_uri_of_string value)
159 | _ -> failwith "Can't parse aliases"
161 let parse_choices choices_raw =
162 let choices = Pcre.split ~pat:";" choices_raw in
165 match Pcre.split ~pat:"\\s" x with
167 | id::tail when id<>"" ->
170 Some (List.map (fun u -> Netencoding.Url.decode u) tail)
173 | _ -> failwith "Can't parse choices")
177 let id_to_uris = parse_tokens [] (fun _ -> None) tokens in
180 let choices_raw = req#param "choices" in
181 parse_choices choices_raw
182 with Http_types.Param_not_found _ -> (fun _ -> None)
184 let module Chat: Disambiguate.Callbacks =
187 let output_html = prerr_endline
189 let interactive_user_uri_choice
191 ?enable_button_for_non_vars ~(title: string) ~(msg: string)
192 ~(id: string) (choices: string list)
194 (match id_to_choices id with
195 | Some choices -> choices
197 let msg = Pcre.replace ~pat:"\"" ~templ:"\\\"" msg in
198 (match selection_mode with
199 | `SINGLE -> assert false
203 let formatted_choices =
205 (List.map (fun uri -> sprintf "\"%s\"" uri) choices)
209 [title_tag_RE, title;
210 choices_tag_RE, formatted_choices;
212 id_to_uris_RE, id_to_uris_raw;
216 output_string outchan processed_line)
217 interactive_user_uri_choice_TPL;
218 raise Chat_unfinished))
220 let interactive_interpretation_choice interpretations =
221 let html_interpretations_labels =
226 (String.concat "<br />"
229 (sprintf "alias %s %s" id value))
234 let html_interpretations =
242 (sprintf "alias %s %s"
244 (MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format'
254 [interpretations_RE, html_interpretations;
255 interpretations_labels_RE, html_interpretations_labels]
258 output_string outchan processed_line)
259 interactive_interpretation_choice_TPL;
260 raise Chat_unfinished
262 let input_or_locate_uri ~title =
263 UriManager.uri_of_string "cic:/Coq/Init/DataTypes/nat_ind.con"
267 let module Disambiguate' = Disambiguate.Make (Chat) in
268 let (id_to_uris', metasenv', term') =
269 Disambiguate'.disambiguate_input
270 context metasenv dom mk_metasenv_and_expr id_to_uris
272 (match metasenv' with
274 let must = MQueryLevels2.get_constraints term' in
275 let must',only = refine_constraints must in
276 let results = MQueryGenerator.searchPattern must' only in
280 match id_to_uris' with
285 sprintf "\"alias %s cic:%s\""
288 | None -> assert false
290 MQueryMisc.string_of_cic_textual_parser_uri
296 [results_RE, text_of_result results "\n";
297 new_aliases_RE, new_aliases]
300 output_string outchan processed_line)
302 | _ -> (* unable to instantiate some implicit variable *)
305 ~body:"some implicit variables are still unistantiated :-("
309 Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan);
310 debug_print (sprintf "%s done!" req#path)
312 | Chat_unfinished -> prerr_endline "Chat unfinished, Try again!"
313 | Http_types.Param_not_found attr_name ->
314 bad_request (sprintf "Parameter '%s' is missing" attr_name) outchan
317 ~body:(pp_error ("Uncaught exception: " ^ (Printexc.to_string exc)))
320 printf "%s started and listening on port %d\n" daemon_name port;
321 printf "current directory is %s\n" (Sys.getcwd ());
323 Unix.putenv "http_proxy" "";
324 Mqint.set_database Mqint.postgres_db;
325 Mqint.init postgresConnectionString;
326 Http_daemon.start' ~port callback;
328 printf "%s is terminating, bye!\n" daemon_name