]> matita.cs.unibo.it Git - helm.git/blob - helm/searchEngine/searchEngine.ml
999d858aaedede548ab5fe18917146590f2ea541
[helm.git] / helm / searchEngine / searchEngine.ml
1 (* Copyright (C) 2002, 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://cs.unibo.it/helm/.
24  *)
25
26 let debug = true;;
27 let debug_print s = if debug then prerr_endline s;;
28 (* Http_common.debug := true;; *)
29
30 open Printf;;
31
32 let postgresConnectionString =
33  try
34   Sys.getenv "POSTGRESQL_CONNECTION_STRING"
35  with
36   Not_found -> "host=mowgli.cs.unibo.it dbname=helm_mowgli_new_schema user=helm"
37 ;;
38
39 let daemon_name = "Search Engine";;
40 let default_port = 58085;;
41 let port_env_var = "SEARCH_ENGINE_PORT";;
42
43 let pages_dir =
44   try
45     Sys.getenv "SEARCH_ENGINE_HTML_DIR"
46   with Not_found -> "html"  (* relative to searchEngine's document root *)
47 ;;
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";;
51
52 exception Chat_unfinished
53
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 ;
59    !res_string
60 ;;
61
62   (** chain application of Pcre substitutions *)
63 let rec apply_substs substs line =
64   match substs with
65   | [] -> 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 =
71     try 
72       let line = input_line inchan in 
73       fold_lines' (f value line)
74     with End_of_file -> value
75   in
76   let res = (try fold_lines' init with e -> (close_in inchan; raise e)) in
77   close_in inchan;
78   res
79   (** iter like function on files *)
80 let iter_file f = fold_file (fun _ line -> f line) ()
81
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@")
88
89 let port =
90   try
91     int_of_string (Sys.getenv port_env_var)
92   with
93   | Not_found -> default_port
94   | Failure "int_of_string" ->
95       prerr_endline "Warning: invalid port, reverting to default";
96       default_port
97 in
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>"
101     res_string)
102 in
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
106 in
107 let contype = "Content-Type", "text/html" in
108
109 (* SEARCH ENGINE functions *)
110
111 let refine_constraints (x, y, z) = (x, y, z), (Some x, Some y, Some z) in
112
113 (* HTTP DAEMON CALLBACK *)
114
115 let callback (req: Http_types.request) outchan =
116   try
117     debug_print (sprintf "Received request: %s" req#path);
118     if req#path <> "/getpage" then
119       Mqint.init postgresConnectionString;
120     (match req#path with
121     | "/execute" ->
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
127         Http_daemon.respond
128           ~body:
129             (sprintf "<html><body><pre>%s</pre></body></html>" result_string)
130           ~headers:[contype] outchan
131     | "/locate" ->
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
135     | "/getpage" ->
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
140         match page with
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
155         in
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 *)
159           | [] -> keys, lookup
160           | "alias" :: key :: value :: rest ->
161               let key' = CicTextualParser0.Id key in
162                parse_tokens
163                  (key'::keys)
164                  (fun id ->
165                    if id = key' then
166                      Some
167                       (CicTextualParser0.Uri (MQueryMisc.cic_textual_parser_uri_of_string value))
168                    else lookup id)
169                  rest
170           | _ -> failwith "Can't parse aliases"
171         in
172         let parse_choices choices_raw =
173           let choices = Pcre.split ~pat:";" choices_raw in
174           List.fold_left
175             (fun f x ->
176               match Pcre.split ~pat:"\\s" x with
177               | ""::id::tail
178               | id::tail when id<>"" ->
179                   (fun id' ->
180                     if id = id' then
181                       Some (List.map (fun u -> Netencoding.Url.decode u) tail)
182                     else
183                       f id')
184               | _ -> failwith "Can't parse choices")
185             (fun _ -> None)
186             choices
187         in
188         let (id_to_uris : Disambiguate.domain_and_interpretation) =
189          parse_tokens [] (fun _ -> None) tokens in
190         let id_to_choices =
191           try
192             let choices_raw = req#param "choices" in
193             parse_choices choices_raw
194           with Http_types.Param_not_found _ -> (fun _ -> None)
195         in
196         let module Chat: Disambiguate.Callbacks =
197           struct
198
199             let get_metasenv () =
200              !CicTextualParser0.metasenv
201
202             let set_metasenv metasenv =
203               CicTextualParser0.metasenv := metasenv
204
205             let output_html = prerr_endline
206
207             let interactive_user_uri_choice
208               ~selection_mode ?ok
209               ?enable_button_for_non_vars ~(title: string) ~(msg: string)
210               ~(id: string) (choices: string list)
211               =
212                 (match id_to_choices id with
213                 | Some choices -> choices
214                 | None ->
215                   let msg = Pcre.replace ~pat:"\"" ~templ:"\\\"" msg in
216                   (match selection_mode with
217                   | `SINGLE -> assert false
218                   | `EXTENDED ->
219                       iter_file
220                         (fun line ->
221                           let formatted_choices =
222                             String.concat ","
223                               (List.map (fun uri -> sprintf "\"%s\"" uri) choices)
224                           in
225                           let processed_line =
226                             apply_substs
227                               [title_tag_RE, title;
228                                choices_tag_RE, formatted_choices;
229                                msg_tag_RE, msg;
230                                id_to_uris_RE, id_to_uris_raw;
231                                id_RE, id]
232                               line
233                           in
234                           output_string outchan processed_line)
235                         interactive_user_uri_choice_TPL;
236                       raise Chat_unfinished))
237
238             let interactive_interpretation_choice interpretations =
239               let html_interpretations_labels =
240                 String.concat ", "
241                   (List.map
242                     (fun l ->
243                       "\"" ^
244                       (String.concat "<br />"
245                         (List.map
246                           (fun (id, value) ->
247                             (sprintf "alias %s %s" id value))
248                           l)) ^
249                       "\"")
250                   interpretations)
251               in
252               let html_interpretations =
253                 String.concat ", "
254                   (List.map
255                     (fun l ->
256                       "\"" ^
257                       (String.concat " "
258                         (List.map
259                           (fun (id, value) ->
260                             (sprintf "alias %s %s"
261                               id
262                               (MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format'
263                                 value)))
264                           l)) ^
265                       "\"")
266                     interpretations)
267               in
268               iter_file
269                 (fun line ->
270                   let processed_line =
271                     apply_substs
272                       [interpretations_RE, html_interpretations;
273                        interpretations_labels_RE, html_interpretations_labels]
274                       line
275                   in
276                   output_string outchan processed_line)
277                 interactive_interpretation_choice_TPL;
278               raise Chat_unfinished
279
280             let input_or_locate_uri ~title =
281               UriManager.uri_of_string "cic:/Coq/Init/DataTypes/nat_ind.con"
282
283           end
284         in
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
289         in
290         (match metasenv' with
291         | [] ->
292             let must = MQueryLevels2.get_constraints term' in
293             let must',only = refine_constraints must in
294             let results = MQueryGenerator.searchPattern must' only in 
295             iter_file
296               (fun line ->
297                 let new_aliases =
298                   match id_to_uris' with
299                   | (domain, f) ->
300                       String.concat ", "
301                         (List.map
302                           (fun name ->
303                             sprintf "\"alias %s cic:%s\""
304                               (match name with
305                                   CicTextualParser0.Id name -> name
306                                 | _ -> assert false (*CSC: completare *))
307                               (match f name with
308                               | None -> assert false
309                               | Some (CicTextualParser0.Uri t) ->
310                                   MQueryMisc.string_of_cic_textual_parser_uri
311                                     t
312                               | _ -> assert false (*CSC: completare *)))
313                           domain)
314                 in
315                 let processed_line =
316                   apply_substs
317                     [results_RE, text_of_result results "\n";
318                      new_aliases_RE, new_aliases]
319                     line
320                 in
321                 output_string outchan processed_line)
322               final_results_TPL
323         | _ -> (* unable to instantiate some implicit variable *)
324             Http_daemon.respond
325               ~headers:[contype]
326               ~body:"some implicit variables are still unistantiated :-("
327               outchan)
328
329     | invalid_request ->
330         Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan);
331     if req#path <> "/getpage" then
332       Mqint.close ();
333     debug_print (sprintf "%s done!" req#path)
334   with
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
338   | exc ->
339       Http_daemon.respond
340         ~body:(pp_error ("Uncaught exception: " ^ (Printexc.to_string exc)))
341         outchan
342 in
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;
346 flush stdout;
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
351