]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
Merge remote-tracking branch 'upstream/master'
[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         // 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->setIncludePolygonAsText((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->setIncludePolygonAsPoints($bAsPoints);
67                 $oGeocode->setIncludePolygonAsText($bAsText);
68                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
69                 $oGeocode->setIncludePolygonAsKML($bAsKML);
70                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
71         }
72
73         // Polygon simplification threshold (optional)
74         $fThreshold = 0.0;
75         if (isset($_GET['polygon_threshold'])) $fThreshold = (float)$_GET['polygon_threshold'];
76         $oGeocode->setPolygonSimplificationThreshold($fThreshold);
77
78         $oGeocode->loadParamArray($_GET);
79
80         if (CONST_Search_BatchMode && isset($_GET['batch']))
81         {
82                 $aBatch = json_decode($_GET['batch'], true);
83                 $aBatchResults = array();
84                 foreach($aBatch as $aBatchParams)
85                 {
86                         $oBatchGeocode = clone $oGeocode;
87                         $oBatchGeocode->loadParamArray($aBatchParams);
88                         $oBatchGeocode->setQueryFromParams($aBatchParams);
89                         $aSearchResults = $oBatchGeocode->lookup();
90                         $aBatchResults[] = $aSearchResults;
91                 }
92                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
93                 exit;
94         } else {
95         if (!(isset($_GET['q']) && $_GET['q']) && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
96         {
97             $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
98
99             // reverse order of '/' separated string
100             $aPhrases = explode('/', $sQuery);
101             $aPhrases = array_reverse($aPhrases);
102             $sQuery = join(', ',$aPhrases);
103             $oGeocode->setQuery($sQuery);
104         }
105         else
106         {
107             $oGeocode->setQueryFromParams($_GET);
108         }
109
110     }
111
112         $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
113
114         $aSearchResults = $oGeocode->lookup();
115         if ($aSearchResults === false) $aSearchResults = array();
116
117         $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
118
119         logEnd($oDB, $hLog, sizeof($aSearchResults));
120
121         $bAsText = $oGeocode->getIncludePolygonAsText();
122         $sQuery = $oGeocode->getQueryString();
123         $sViewBox = $oGeocode->getViewBoxString();
124         $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
125         $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
126
127         $sMoreURL = CONST_Website_BaseURL.'search?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
128         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
129         if ($bShowPolygons) $sMoreURL .= '&polygon=1';
130         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
131         if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
132         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
133         $sMoreURL .= '&q='.urlencode($sQuery);
134
135         if (CONST_Debug) exit;
136
137         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');