]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
query the last updated timestamp only if output format is HTML
[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 (CONST_Search_ReversePlanForAll
23                 || isset($aLangPrefOrder['name:de'])
24                 || isset($aLangPrefOrder['name:ru'])
25                 || isset($aLangPrefOrder['name:ja'])
26                 || isset($aLangPrefOrder['name:pl']))
27         {
28                 $oGeocode->setReverseInPlan(true);
29         }
30
31         // Format for output
32         $sOutputFormat = 'html';
33         if (isset($_GET['format']) && ($_GET['format'] == 'html' || $_GET['format'] == 'xml' || $_GET['format'] == 'json' ||  $_GET['format'] == 'jsonv2'))
34         {
35                 $sOutputFormat = $_GET['format'];
36         }
37
38         // Show / use polygons
39         if ($sOutputFormat == 'html')
40         {
41                 if (isset($_GET['polygon'])) $oGeocode->setIncludePolygonAsText((bool)$_GET['polygon']);
42         }
43         else
44         {
45                 $bAsPoints = (boolean)isset($_GET['polygon']) && $_GET['polygon'];
46                 $bAsGeoJSON = (boolean)isset($_GET['polygon_geojson']) && $_GET['polygon_geojson'];
47                 $bAsKML = (boolean)isset($_GET['polygon_kml']) && $_GET['polygon_kml'];
48                 $bAsSVG = (boolean)isset($_GET['polygon_svg']) && $_GET['polygon_svg'];
49                 $bAsText = (boolean)isset($_GET['polygon_text']) && $_GET['polygon_text'];
50                 if ( ( ($bAsGeoJSON?1:0)
51                                  + ($bAsKML?1:0)
52                                  + ($bAsSVG?1:0)
53                                  + ($bAsText?1:0)
54                                  + ($bAsPoints?1:0)
55                                  ) > CONST_PolygonOutput_MaximumTypes)
56                 {
57                         if (CONST_PolygonOutput_MaximumTypes)
58                         {
59                                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
60                         }
61                         else
62                         {
63                                 userError("Polygon output is disabled");
64                         }
65                         exit;
66                 }
67                 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
68                 $oGeocode->setIncludePolygonAsText($bAsText);
69                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
70                 $oGeocode->setIncludePolygonAsKML($bAsKML);
71                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
72         }
73
74         // Polygon simplification threshold (optional)
75         $fThreshold = 0.0;
76         if (isset($_GET['polygon_threshold'])) $fThreshold = (float)$_GET['polygon_threshold'];
77         $oGeocode->setPolygonSimplificationThreshold($fThreshold);
78
79         $oGeocode->loadParamArray($_GET);
80
81         if (CONST_Search_BatchMode && isset($_GET['batch']))
82         {
83                 $aBatch = json_decode($_GET['batch'], true);
84                 $aBatchResults = array();
85                 foreach($aBatch as $aBatchParams)
86                 {
87                         $oBatchGeocode = clone $oGeocode;
88                         $oBatchGeocode->loadParamArray($aBatchParams);
89                         $oBatchGeocode->setQueryFromParams($aBatchParams);
90                         $aSearchResults = $oBatchGeocode->lookup();
91                         $aBatchResults[] = $aSearchResults;
92                 }
93                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
94                 exit;
95         }
96         else
97         {
98                 if (!(isset($_GET['q']) && $_GET['q']) && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
99                 {
100                         $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
101
102                         // reverse order of '/' separated string
103                         $aPhrases = explode('/', $sQuery);
104                         $aPhrases = array_reverse($aPhrases);
105                         $sQuery = join(', ',$aPhrases);
106                         $oGeocode->setQuery($sQuery);
107                 }
108                 else
109                 {
110                         $oGeocode->setQueryFromParams($_GET);
111                 }
112         }
113
114         $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
115
116         $aSearchResults = $oGeocode->lookup();
117         if ($aSearchResults === false) $aSearchResults = array();
118
119         if ($sOutputFormat=='html')
120         {
121                 $sDataDate = $oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1");
122                 $sTileURL = CONST_Map_Tile_URL;
123                 $sTileAttribution = CONST_Map_Tile_Attribution;
124         }
125         logEnd($oDB, $hLog, sizeof($aSearchResults));
126
127         $bAsText = $oGeocode->getIncludePolygonAsText();
128         $sQuery = $oGeocode->getQueryString();
129         $sViewBox = $oGeocode->getViewBoxString();
130         $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
131         $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
132
133         $sMoreURL = CONST_Website_BaseURL.'search.php?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$oGeocode->getExcludedPlaceIDs());
134         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
135         if ($bShowPolygons) $sMoreURL .= '&polygon=1';
136         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
137         if ($oGeocode->getIncludeExtraTags()) $sMoreURL .= '&extratags=1';
138         if ($oGeocode->getIncludeNameDetails()) $sMoreURL .= '&namedetails=1';
139         if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
140         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
141         $sMoreURL .= '&q='.urlencode($sQuery);
142
143         if (CONST_Debug) exit;
144
145         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');