3 require_once(CONST_LibDir.'/init-website.php');
4 require_once(CONST_LibDir.'/log.php');
5 require_once(CONST_LibDir.'/Geocode.php');
6 require_once(CONST_LibDir.'/output.php');
7 ini_set('memory_limit', '200M');
9 $oDB = new Nominatim\DB(CONST_Database_DSN);
11 $oParams = new Nominatim\ParameterParser();
13 $oGeocode = new Nominatim\Geocode($oDB);
15 $aLangPrefOrder = $oParams->getPreferredLanguages();
16 $oGeocode->setLanguagePreference($aLangPrefOrder);
19 $sOutputFormat = $oParams->getSet('format', array('xml', 'json', 'jsonv2', 'geojson', 'geocodejson'), 'jsonv2');
20 set_exception_handler_by_format($sOutputFormat);
22 $oGeocode->loadParamArray($oParams, null);
24 if (CONST_Search_BatchMode && isset($_GET['batch'])) {
25 $aBatch = json_decode($_GET['batch'], true);
26 $aBatchResults = array();
27 foreach ($aBatch as $aBatchParams) {
28 $oBatchGeocode = clone $oGeocode;
29 $oBatchParams = new Nominatim\ParameterParser($aBatchParams);
30 $oBatchGeocode->loadParamArray($oBatchParams);
31 $oBatchGeocode->setQueryFromParams($oBatchParams);
32 $aSearchResults = $oBatchGeocode->lookup();
33 $aBatchResults[] = $aSearchResults;
35 include(CONST_LibDir.'/template/search-batch-json.php');
39 $oGeocode->setQueryFromParams($oParams);
41 if (!$oGeocode->getQueryString()
42 && isset($_SERVER['PATH_INFO'])
43 && strlen($_SERVER['PATH_INFO']) > 0
44 && $_SERVER['PATH_INFO'][0] == '/'
46 $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
48 // reverse order of '/' separated string
49 $aPhrases = explode('/', $sQuery);
50 $aPhrases = array_reverse($aPhrases);
51 $sQuery = join(', ', $aPhrases);
52 $oGeocode->setQuery($sQuery);
55 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
57 $aSearchResults = $oGeocode->lookup();
59 logEnd($oDB, $hLog, count($aSearchResults));
61 $sQuery = $oGeocode->getQueryString();
63 $aMoreParams = $oGeocode->getMoreUrlParams();
64 $aMoreParams['format'] = $sOutputFormat;
65 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
66 $aMoreParams['accept-language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
69 if (isset($_SERVER['REQUEST_SCHEME'])
70 && isset($_SERVER['HTTP_HOST'])
71 && isset($_SERVER['DOCUMENT_URI'])
73 $sMoreURL = $_SERVER['REQUEST_SCHEME'].'://'
74 .$_SERVER['HTTP_HOST'].$_SERVER['DOCUMENT_URI'].'/?'
75 .http_build_query($aMoreParams);
77 $sMoreURL = '/search.php'.http_build_query($aMoreParams);
80 if (CONST_Debug) exit;
82 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
83 include(CONST_LibDir.'/template/search-'.$sOutputTemplate.'.php');