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