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