]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/searchEngine/searchEngine.ml
Partial upgrade to the new disambiguating lexer/parser.
[helm.git] / helm / searchEngine / searchEngine.ml
index 7b5635704f782e4b19cdfb4db942fafc3777b3f0..7fa835d8cc098927946104a35e2166a60b6928fd 100644 (file)
@@ -1,4 +1,3 @@
-
 (* Copyright (C) 2002, HELM Team.
  * 
  * This file is part of HELM, an Hypertextual, Electronic
@@ -30,6 +29,13 @@ Http_common.debug := true;;
 
 open Printf;;
 
+let postgresConnectionString =
+ try
+  Sys.getenv "POSTGRESQL_CONNECTION_STRING"
+ with
+  Not_found -> "host=mowgli.cs.unibo.it dbname=helm_mowgli_new_schema user=helm"
+;;
+
 let daemon_name = "Search Engine";;
 let default_port = 58085;;
 let port_env_var = "SEARCH_ENGINE_PORT";;
@@ -41,6 +47,14 @@ let final_results_TPL = pages_dir ^ "/templateambigpdq3.html";;
 
 exception Chat_unfinished
 
+  (** pretty print a MathQL query result to a string *)
+let text_of_result result sep =
+ let res_string = ref "" in
+  let app = function s -> res_string := !res_string ^ s in
+   MQueryUtil.text_of_result app result sep ;
+   !res_string
+;;
+
   (** chain application of Pcre substitutions *)
 let rec apply_substs substs line =
   match substs with
@@ -78,9 +92,9 @@ let port =
       default_port
 in
 let pp_result result =
 let result_string = MQueryUtil.text_of_result result "\n" in
let res_string = text_of_result result "\n" in
   (sprintf "<html>\n<head>\n</head>\n<body>\n<pre>%s</pre>\n</body>\n</html>"
-    result_string)
+    res_string)
 in
 let pp_error = sprintf "<html><body><h1>Error: %s</h1></body></html>" in
 let bad_request body outchan =
@@ -103,7 +117,7 @@ let callback (req: Http_types.request) outchan =
         let lexbuf = Lexing.from_string query_string in
         let query = MQueryUtil.query_of_text lexbuf in
         let result = MQueryGenerator.execute_query query in
-        let result_string = MQueryUtil.text_of_result result "\n" in
+        let result_string = text_of_result result "\n" in
         Http_daemon.respond
           ~body:
             (sprintf "<html><body><pre>%s</pre></body></html>" result_string)
@@ -113,10 +127,12 @@ let callback (req: Http_types.request) outchan =
         let result = MQueryGenerator.locate id in
         Http_daemon.respond ~headers:[contype] ~body:(pp_result result) outchan
     | "/getpage" ->
+        (* TODO implement "is_permitted" *)
         (let is_permitted _ = true in
         let remove_fragment uri = Pcre.replace ~pat:"#.*" uri in
-        match req#param "url" with
-        | page when is_permitted (remove_fragment page) ->
+        let page = remove_fragment (req#param "url") in
+        match page with
+        | page when is_permitted page ->
             Http_daemon.respond_file
               ~fname:(sprintf "%s/%s" pages_dir (remove_fragment page)) outchan
         | page -> Http_daemon.respond_forbidden ~url:page outchan)
@@ -133,13 +149,15 @@ let callback (req: Http_types.request) outchan =
         let rec parse_tokens keys lookup = function (* TODO spostarla fuori *)
           | [] -> keys, lookup
           | "alias" :: key :: value :: rest ->
-              parse_tokens
-                (key::keys)
-                (fun id ->
-                  if id = key then
-                    Some (Disambiguate.cic_textual_parser_uri_of_string value)
-                  else lookup id)
-                rest
+              let key' = CicTextualParser0.Id key in
+               parse_tokens
+                 (key'::keys)
+                 (fun id ->
+                   if id = key' then
+                     Some
+                      (CicTextualParser0.Uri (MQueryMisc.cic_textual_parser_uri_of_string value))
+                   else lookup id)
+                 rest
           | _ -> failwith "Can't parse aliases"
         in
         let parse_choices choices_raw =
@@ -158,7 +176,8 @@ let callback (req: Http_types.request) outchan =
             (fun _ -> None)
             choices
         in
-        let id_to_uris = parse_tokens [] (fun _ -> None) tokens in
+        let (id_to_uris : Disambiguate.domain_and_interpretation) =
+         parse_tokens [] (fun _ -> None) tokens in
         let id_to_choices =
           try
             let choices_raw = req#param "choices" in
@@ -168,6 +187,12 @@ let callback (req: Http_types.request) outchan =
         let module Chat: Disambiguate.Callbacks =
           struct
 
+            let get_metasenv () =
+             !CicTextualParser0.metasenv
+
+            let set_metasenv metasenv =
+              CicTextualParser0.metasenv := metasenv
+
             let output_html = prerr_endline
 
             let interactive_user_uri_choice
@@ -225,7 +250,7 @@ let callback (req: Http_types.request) outchan =
                           (fun (id, value) ->
                             (sprintf "alias %s %s"
                               id
-                              (Disambiguate.wrong_xpointer_format_from_wrong_xpointer_format'
+                              (MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format'
                                 value)))
                           l)) ^
                       "\"")
@@ -258,7 +283,6 @@ let callback (req: Http_types.request) outchan =
             let must = MQueryLevels2.get_constraints term' in
             let must',only = refine_constraints must in
             let results = MQueryGenerator.searchPattern must' only in 
-            debug_print "FASE 3";
             iter_file
               (fun line ->
                 let new_aliases =
@@ -268,17 +292,20 @@ let callback (req: Http_types.request) outchan =
                         (List.map
                           (fun name ->
                             sprintf "\"alias %s cic:%s\""
-                              name
+                              (match name with
+                                  CicTextualParser0.Id name -> name
+                                | _ -> assert false (*CSC: completare *))
                               (match f name with
                               | None -> assert false
-                              | Some t ->
-                                  Disambiguate.string_of_cic_textual_parser_uri
-                                    t))
+                              | Some (CicTextualParser0.Uri t) ->
+                                  MQueryMisc.string_of_cic_textual_parser_uri
+                                    t
+                              | _ -> assert false (*CSC: completare *)))
                           domain)
                 in
                 let processed_line =
                   apply_substs
-                    [results_RE, MQueryUtil.text_of_result results "\n";
+                    [results_RE, text_of_result results "\n";
                      new_aliases_RE, new_aliases]
                     line
                 in
@@ -300,14 +327,14 @@ let callback (req: Http_types.request) outchan =
   | exc ->
       Http_daemon.respond
         ~body:(pp_error ("Uncaught exception: " ^ (Printexc.to_string exc)))
-       outchan
+        outchan
 in
 printf "%s started and listening on port %d\n" daemon_name port;
 printf "current directory is %s\n" (Sys.getcwd ());
 flush stdout;
 Unix.putenv "http_proxy" "";
 Mqint.set_database Mqint.postgres_db;
-Mqint.init "host=mowgli.cs.unibo.it dbname=helm_mowgli_new_schema user=helm";
+Mqint.init postgresConnectionString;
 Http_daemon.start' ~port callback;
 Mqint.close ();
 printf "%s is terminating, bye!\n" daemon_name