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