]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
details.php now also responsive
[nominatim.git] / website / search.php
1 <?php
2         @define('CONST_ConnectionBucket_PageType', 'Search');
3
4         require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
5         require_once(CONST_BasePath.'/lib/log.php');
6         require_once(CONST_BasePath.'/lib/Geocode.php');
7
8         ini_set('memory_limit', '200M');
9
10         $oDB =& getDB();
11
12         // Display defaults
13         $fLat = CONST_Default_Lat;
14         $fLon = CONST_Default_Lon;
15         $iZoom = CONST_Default_Zoom;
16
17         $oGeocode =& new Geocode($oDB);
18
19         $aLangPrefOrder = getPreferredLanguages();
20         $oGeocode->setLanguagePreference($aLangPrefOrder);
21
22         if (isset($aLangPrefOrder['name:de'])) $oGeocode->setReverseInPlan(true);
23         if (isset($aLangPrefOrder['name:ru'])) $oGeocode->setReverseInPlan(true);
24         if (isset($aLangPrefOrder['name:ja'])) $oGeocode->setReverseInPlan(true);
25         if (isset($aLangPrefOrder['name:pl'])) $oGeocode->setReverseInPlan(true);
26
27         // Format for output
28         $sOutputFormat = 'html';
29         if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' ||  $_GET['format'] == 'jsonv2'))
30         {
31                 $sOutputFormat = $_GET['format'];
32         }
33
34         // Show / use polygons
35         if ($sOutputFormat == 'html')
36         {
37                 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
38         }
39         else
40         {
41                 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
42                 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
43                 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
44                 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
45                 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
46                 if ( ( ($bAsGeoJSON?1:0)
47                      + ($bAsKML?1:0)
48                      + ($bAsSVG?1:0)
49                      + ($bAsText?1:0)
50                      + ($bAsPoints?1:0)
51                      ) > CONST_PolygonOutput_MaximumTypes)
52                 {
53                         if (CONST_PolygonOutput_MaximumTypes)
54                         {
55                                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
56                         }
57                         else
58                         {
59                                 userError("Polygon output is disabled");
60                         }
61                         exit;
62                 }
63                 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
64                 $oGeocode->setIncludePolygonAsText($bAsText);
65                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
66                 $oGeocode->setIncludePolygonAsKML($bAsKML);
67                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
68         }
69
70         // Polygon simplification threshold (optional)
71         $fThreshold = 0.0;
72         if (isset($_GET['polygon_threshold'])) $fThreshold = (float)$_GET['polygon_threshold'];
73         $oGeocode->setPolygonSimplificationThreshold($fThreshold);
74
75         $oGeocode->loadParamArray($_GET);
76
77         if (CONST_Search_BatchMode && isset($_GET['batch']))
78         {
79                 $aBatch = json_decode($_GET['batch'], true);
80                 $aBatchResults = array();
81                 foreach($aBatch as $aBatchParams)
82                 {
83                         $oBatchGeocode = clone $oGeocode;
84                         $oBatchGeocode->loadParamArray($aBatchParams);
85                         $oBatchGeocode->setQueryFromParams($aBatchParams);
86                         $aSearchResults = $oBatchGeocode->lookup();
87                         $aBatchResults[] = $aSearchResults;
88                 }
89                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
90                 exit;
91         } else {
92         if (!(isset($_GET['q']) && $_GET['q']) && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
93         {
94             $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
95
96             // reverse order of '/' separated string
97             $aPhrases = explode('/', $sQuery);
98             $aPhrases = array_reverse($aPhrases);
99             $sQuery = join(', ',$aPhrases);
100             $oGeocode->setQuery($sQuery);
101         }
102         else
103         {
104             $oGeocode->setQueryFromParams($_GET);
105         }
106
107     }
108
109         $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
110
111         $aSearchResults = $oGeocode->lookup();
112         if ($aSearchResults === false) $aSearchResults = array();
113
114         $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
115
116         logEnd($oDB, $hLog, sizeof($aSearchResults));
117
118         $bAsText = $oGeocode->getIncludePolygonAsText();
119         $sQuery = $oGeocode->getQueryString();
120         $sViewBox = $oGeocode->getViewBoxString();
121         $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
122         $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
123
124         $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
125         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
126         if ($bShowPolygons) $sMoreURL .= '&polygon=1';
127         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
128         if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
129         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
130         $sMoreURL .= '&q='.urlencode($sQuery);
131
132         if (CONST_Debug) exit;
133
134         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');