]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
factor out parameter parsing into its own class
[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         ini_set('memory_limit', '200M');
10
11         $oDB =& getDB();
12         $oParams = new ParameterParser();
13
14         $oGeocode = new Geocode($oDB);
15
16         $aLangPrefOrder = $oParams->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 = $oParams->getSet('format', array('html', 'xml', 'json', 'jsonv2'), 'html');
30
31         // Show / use polygons
32         if ($sOutputFormat == 'html')
33         {
34                 $oGeocode->setIncludePolygonAsText($oParams->getBool('polygon'));
35                 $bAsText = false;
36         }
37         else
38         {
39                 $bAsPoints = $oParams->getBool('polygon');
40                 $bAsGeoJSON = $oParams->getBool('polygon_geojson');
41                 $bAsKML = $oParams->getBool('polygon_kml');
42                 $bAsSVG = $oParams->getBool('polygon_svg');
43                 $bAsText = $oParams->getBool('polygon_text');
44                 if ( ( ($bAsGeoJSON?1:0)
45                                  + ($bAsKML?1:0)
46                                  + ($bAsSVG?1:0)
47                                  + ($bAsText?1:0)
48                                  + ($bAsPoints?1:0)
49                                  ) > CONST_PolygonOutput_MaximumTypes)
50                 {
51                         if (CONST_PolygonOutput_MaximumTypes)
52                         {
53                                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
54                         }
55                         else
56                         {
57                                 userError("Polygon output is disabled");
58                         }
59                         exit;
60                 }
61                 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
62                 $oGeocode->setIncludePolygonAsText($bAsText);
63                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
64                 $oGeocode->setIncludePolygonAsKML($bAsKML);
65                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
66         }
67
68         // Polygon simplification threshold (optional)
69         $oGeocode->setPolygonSimplificationThreshold($oParams->getFloat('polygon_threshold', 0.0));
70
71         $oGeocode->loadParamArray($oParams);
72
73         if (CONST_Search_BatchMode && isset($_GET['batch']))
74         {
75                 $aBatch = json_decode($_GET['batch'], true);
76                 $aBatchResults = array();
77                 foreach($aBatch as $aBatchParams)
78                 {
79                         $oBatchGeocode = clone $oGeocode;
80                         $oBatchParams = new ParameterParser($aBatchParams);
81                         $oBatchGeocode->loadParamArray($oBatchParams);
82                         $oBatchGeocode->setQueryFromParams($oBatchParams);
83                         $aSearchResults = $oBatchGeocode->lookup();
84                         $aBatchResults[] = $aSearchResults;
85                 }
86                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
87                 exit;
88         }
89
90         $oGeocode->setQueryFromParams($oParams);
91
92         if (!$oGeocode->getQueryString()
93             && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
94         {
95                 $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
96
97                 // reverse order of '/' separated string
98                 $aPhrases = explode('/', $sQuery);
99                 $aPhrases = array_reverse($aPhrases);
100                 $sQuery = join(', ',$aPhrases);
101                 $oGeocode->setQuery($sQuery);
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 = chksql($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         $sQuery = $oGeocode->getQueryString();
116         $sViewBox = $oGeocode->getViewBoxString();
117         $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
118         $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
119
120         $sMoreURL = CONST_Website_BaseURL.'search.php?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$aExcludePlaceIDs);
121         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
122         if ($bShowPolygons) $sMoreURL .= '&polygon=1';
123         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
124         if ($oGeocode->getIncludeExtraTags()) $sMoreURL .= '&extratags=1';
125         if ($oGeocode->getIncludeNameDetails()) $sMoreURL .= '&namedetails=1';
126         if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
127         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
128         $sMoreURL .= '&q='.urlencode($sQuery);
129
130         if (CONST_Debug) exit;
131
132         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');