]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
try moving logs to ssd
[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         $sSuggestionURL = false;
17
18         $oGeocode =& new Geocode($oDB);
19
20         $aLangPrefOrder = getPreferredLanguages();
21         $oGeocode->setLanguagePreference($aLangPrefOrder);
22
23     /*
24         if (isset($aLangPrefOrder['name:de'])) $oGeocode->setReverseInPlan(true);
25         if (isset($aLangPrefOrder['name:ru'])) $oGeocode->setReverseInPlan(true);
26         if (isset($aLangPrefOrder['name:ja'])) $oGeocode->setReverseInPlan(true);
27         if (isset($aLangPrefOrder['name:pl'])) $oGeocode->setReverseInPlan(true);
28     */
29
30         function loadParamsToGeocode($oGeocode, $aParams, $bBatch = false)
31         {
32                 if (isset($aParams['addressdetails'])) $oGeocode->setIncludeAddressDetails((bool)$aParams['addressdetails']);
33                 if (isset($aParams['bounded'])) $oGeocode->setBounded((bool)$aParams['bounded']);
34                 if (isset($aParams['dedupe'])) $oGeocode->setDedupe((bool)$aParams['dedupe']);
35
36                 if (isset($aParams['limit'])) $oGeocode->setLimit((int)$aParams['limit']);
37                 if (isset($aParams['offset'])) $oGeocode->setOffset((int)$aParams['offset']);
38
39                 if (isset($aParams['fallback'])) $oGeocode->setFallback((int)$aParams['fallback']);
40
41                 // List of excluded Place IDs - used for more acurate pageing
42                 if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids'])
43                 {
44                         foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID)
45                         {
46                                 $iExcludedPlaceID = (int)$iExcludedPlaceID;
47                                 if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID;
48                         }
49                         $oGeocode->setExcludedPlaceIds($aExcludePlaceIDs);
50                 }
51
52                 // Only certain ranks of feature
53                 if (isset($aParams['featureType'])) $oGeocode->setFeatureType($aParams['featureType']);
54                 if (isset($aParams['featuretype'])) $oGeocode->setFeatureType($aParams['featuretype']);
55
56                 // Country code list
57                 if (isset($aParams['countrycodes']))
58                 {
59                         $aCountryCodes = array();
60                         foreach(explode(',',$aParams['countrycodes']) as $sCountryCode)
61                         {
62                                 if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode))
63                                 {
64                                         $aCountryCodes[] = strtolower($sCountryCode);
65                                 }
66                         }
67                         $oGeocode->setCountryCodesList($aCountryCodes);
68                 }
69
70                 if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt'])
71                 {
72                         $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']);
73                         $oGeocode->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]);
74                 }
75                 else if (isset($aParams['viewbox']) && $aParams['viewbox'])
76                 {
77                         $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']);
78                         $oGeocode->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]);
79                 }
80
81                 if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth'])
82                 {
83                         $aPoints = explode(',',$aParams['route']);
84                         if (sizeof($aPoints) % 2 != 0)
85                         {
86                                 userError("Uneven number of points");
87                                 exit;
88                         }
89                         $fPrevCoord = false;
90                         $aRoute = array();
91                         foreach($aPoints as $i => $fPoint)
92                         {
93                                 if ($i%2)
94                                 {
95                                         $aRoute[] = array((float)$fPoint, $fPrevCoord);
96                                 }
97                                 else
98                                 {
99                                         $fPrevCoord = (float)$fPoint;
100                                 }
101                         }
102                         $oGeocode->setRoute($aRoute);
103                 }
104
105                 // Search query
106                 $sQuery = (isset($aParams['q'])?trim($aParams['q']):'');
107                 if (!$sQuery && !$bBatch && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
108                 {
109                         $sQuery = substr($_SERVER['PATH_INFO'], 1);
110
111                         // reverse order of '/' separated string
112                         $aPhrases = explode('/', $sQuery);
113                         $aPhrases = array_reverse($aPhrases);
114                         $sQuery = join(', ',$aPhrases);
115                 }
116                 if (!$sQuery)
117                 {
118                         $oGeocode->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']);
119                         $oGeocode->setReverseInPlan(false);
120                 }
121                 else
122                 {
123                         $oGeocode->setQuery($sQuery);
124                 }
125
126         }
127
128         // Format for output
129         $sOutputFormat = 'html';
130         if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' ||  $_GET['format'] == 'jsonv2'))
131         {
132                 $sOutputFormat = $_GET['format'];
133         }
134
135         // Show / use polygons
136         if ($sOutputFormat == 'html')
137         {
138                 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
139         }
140         else
141         {
142                 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
143                 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
144                 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
145                 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
146                 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
147                 if ( ( ($bAsGeoJSON?1:0)
148                      + ($bAsKML?1:0)
149                      + ($bAsSVG?1:0)
150                      + ($bAsText?1:0)
151                      + ($bAsPoints?1:0)
152                      ) > CONST_PolygonOutput_MaximumTypes)
153                 {
154                         if (CONST_PolygonOutput_MaximumTypes)
155                         {
156                                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
157                         }
158                         else
159                         {
160                                 userError("Polygon output is disabled");
161                         }
162                         exit;
163                 }
164                 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
165                 $oGeocode->setIncludePolygonAsText($bAsText);
166                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
167                 $oGeocode->setIncludePolygonAsKML($bAsKML);
168                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
169         }
170
171         loadParamsToGeocode($oGeocode, $_GET, false);
172
173         if (CONST_Search_BatchMode && isset($_GET['batch']))
174         {
175                 $aBatch = json_decode($_GET['batch'], true);
176                 $aBatchResults = array();
177                 foreach($aBatch as $aBatchParams)
178                 {
179                         $oBatchGeocode = clone $oGeocode;
180                         loadParamsToGeocode($oBatchGeocode, $aBatchParams, true);
181                         $aSearchResults = $oBatchGeocode->lookup();
182                         $aBatchResults[] = $aSearchResults;
183                 }
184                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
185                 exit;
186         }
187
188         $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
189
190         $aSearchResults = $oGeocode->lookup();
191         if ($aSearchResults === false) $aSearchResults = array();
192
193         $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
194
195         logEnd($oDB, $hLog, sizeof($aSearchResults));
196
197         $bAsText = $oGeocode->getIncludePolygonAsText();
198         $sQuery = $oGeocode->getQueryString();
199         $sViewBox = $oGeocode->getViewBoxString();
200         $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
201         $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
202
203         $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
204         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
205         if ($bShowPolygons) $sMoreURL .= '&polygon=1';
206         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
207         if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
208         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
209         $sMoreURL .= '&q='.urlencode($sQuery);
210
211         if (CONST_Debug) exit;
212
213         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');