2 @define('CONST_ConnectionBucket_PageType', 'Search');
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');
10 ini_set('memory_limit', '200M');
14 $oGeocode = new Geocode($oDB);
16 $aLangPrefOrder = getPreferredLanguages();
17 $oGeocode->setLanguagePreference($aLangPrefOrder);
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']))
25 $oGeocode->setReverseInPlan(true);
29 $sOutputFormat = getParamSet('format', array('html', 'xml', 'json', 'jsonv2'), 'html');
31 // Show / use polygons
32 if ($sOutputFormat == 'html')
34 $oGeocode->setIncludePolygonAsText(getParamBool('polygon'));
39 $bAsPoints = getParamBool('polygon');
40 $bAsGeoJSON = getParamBool('polygon_geojson');
41 $bAsKML = getParamBool('polygon_kml');
42 $bAsSVG = getParamBool('polygon_svg');
43 $bAsText = getParamBool('polygon_text');
44 if ( ( ($bAsGeoJSON?1:0)
49 ) > CONST_PolygonOutput_MaximumTypes)
51 if (CONST_PolygonOutput_MaximumTypes)
53 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
57 userError("Polygon output is disabled");
61 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
62 $oGeocode->setIncludePolygonAsText($bAsText);
63 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
64 $oGeocode->setIncludePolygonAsKML($bAsKML);
65 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
68 // Polygon simplification threshold (optional)
69 $oGeocode->setPolygonSimplificationThreshold(getParamFloat('polygon_threshold', 0.0));
71 $oGeocode->loadParamArray($_GET);
73 if (CONST_Search_BatchMode && isset($_GET['batch']))
75 $aBatch = json_decode($_GET['batch'], true);
76 $aBatchResults = array();
77 foreach($aBatch as $aBatchParams)
79 $oBatchGeocode = clone $oGeocode;
80 $oBatchGeocode->loadParamArray($aBatchParams);
81 $oBatchGeocode->setQueryFromParams($aBatchParams);
82 $aSearchResults = $oBatchGeocode->lookup();
83 $aBatchResults[] = $aSearchResults;
85 include(CONST_BasePath.'/lib/template/search-batch-json.php');
89 if (!getParamString('q') && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
91 $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
93 // reverse order of '/' separated string
94 $aPhrases = explode('/', $sQuery);
95 $aPhrases = array_reverse($aPhrases);
96 $sQuery = join(', ',$aPhrases);
97 $oGeocode->setQuery($sQuery);
101 $oGeocode->setQueryFromParams($_GET);
104 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
106 $aSearchResults = $oGeocode->lookup();
107 if ($aSearchResults === false) $aSearchResults = array();
109 if ($sOutputFormat=='html')
111 $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
113 logEnd($oDB, $hLog, sizeof($aSearchResults));
115 $sQuery = $oGeocode->getQueryString();
116 $sViewBox = $oGeocode->getViewBoxString();
117 $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
118 $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
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);
130 if (CONST_Debug) exit;
132 include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');