]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
Merge pull request #522 from mtmail/PSR2-arrays
[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     $oGeocode->setIncludePolygonAsText($oParams->getBool('polygon'));
34     $bAsText = false;
35 } else {
36     $bAsPoints = $oParams->getBool('polygon');
37     $bAsGeoJSON = $oParams->getBool('polygon_geojson');
38     $bAsKML = $oParams->getBool('polygon_kml');
39     $bAsSVG = $oParams->getBool('polygon_svg');
40     $bAsText = $oParams->getBool('polygon_text');
41     if (( ($bAsGeoJSON?1:0)
42              + ($bAsKML?1:0)
43              + ($bAsSVG?1:0)
44              + ($bAsText?1:0)
45              + ($bAsPoints?1:0)
46              ) > CONST_PolygonOutput_MaximumTypes
47     ) {
48         if (CONST_PolygonOutput_MaximumTypes) {
49             userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
50         } else {
51             userError("Polygon output is disabled");
52         }
53         exit;
54     }
55     $oGeocode->setIncludePolygonAsPoints($bAsPoints);
56     $oGeocode->setIncludePolygonAsText($bAsText);
57     $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
58     $oGeocode->setIncludePolygonAsKML($bAsKML);
59     $oGeocode->setIncludePolygonAsSVG($bAsSVG);
60 }
61
62 // Polygon simplification threshold (optional)
63 $oGeocode->setPolygonSimplificationThreshold($oParams->getFloat('polygon_threshold', 0.0));
64
65 $oGeocode->loadParamArray($oParams);
66
67 if (CONST_Search_BatchMode && isset($_GET['batch'])) {
68     $aBatch = json_decode($_GET['batch'], true);
69     $aBatchResults = array();
70     foreach ($aBatch as $aBatchParams) {
71         $oBatchGeocode = clone $oGeocode;
72         $oBatchParams = new ParameterParser($aBatchParams);
73         $oBatchGeocode->loadParamArray($oBatchParams);
74         $oBatchGeocode->setQueryFromParams($oBatchParams);
75         $aSearchResults = $oBatchGeocode->lookup();
76         $aBatchResults[] = $aSearchResults;
77     }
78     include(CONST_BasePath.'/lib/template/search-batch-json.php');
79     exit;
80 }
81
82 $oGeocode->setQueryFromParams($oParams);
83
84 if (!$oGeocode->getQueryString()
85     && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/') {
86     $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
87
88     // reverse order of '/' separated string
89     $aPhrases = explode('/', $sQuery);
90     $aPhrases = array_reverse($aPhrases);
91     $sQuery = join(', ',$aPhrases);
92     $oGeocode->setQuery($sQuery);
93 }
94
95 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
96
97 $aSearchResults = $oGeocode->lookup();
98 if ($aSearchResults === false) $aSearchResults = array();
99
100 if ($sOutputFormat=='html') {
101     $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
102 }
103 logEnd($oDB, $hLog, sizeof($aSearchResults));
104
105 $sQuery = $oGeocode->getQueryString();
106 $sViewBox = $oGeocode->getViewBoxString();
107 $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
108 $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
109
110 $sMoreURL = CONST_Website_BaseURL.'search.php?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$aExcludePlaceIDs);
111 if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
112 if ($bShowPolygons) $sMoreURL .= '&polygon=1';
113 if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
114 if ($oGeocode->getIncludeExtraTags()) $sMoreURL .= '&extratags=1';
115 if ($oGeocode->getIncludeNameDetails()) $sMoreURL .= '&namedetails=1';
116 if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
117 if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
118 $sMoreURL .= '&q='.urlencode($sQuery);
119
120 if (CONST_Debug) exit;
121
122 include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');