3 * SPDX-License-Identifier: GPL-2.0-only
5 * This file is part of Nominatim. (https://nominatim.org)
7 * Copyright (C) 2022 by the Nominatim developer community.
8 * For a full list of authors see the git log.
11 require_once(CONST_LibDir.'/init-website.php');
12 require_once(CONST_LibDir.'/log.php');
13 require_once(CONST_LibDir.'/Geocode.php');
14 require_once(CONST_LibDir.'/output.php');
15 ini_set('memory_limit', '200M');
17 $oDB = new Nominatim\DB(CONST_Database_DSN);
19 $oParams = new Nominatim\ParameterParser();
21 $oGeocode = new Nominatim\Geocode($oDB);
23 $aLangPrefOrder = $oParams->getPreferredLanguages();
24 $oGeocode->setLanguagePreference($aLangPrefOrder);
27 $sOutputFormat = $oParams->getSet('format', array('xml', 'json', 'jsonv2', 'geojson', 'geocodejson'), 'jsonv2');
28 set_exception_handler_by_format($sOutputFormat);
30 $oGeocode->loadParamArray($oParams, null);
32 if (CONST_Search_BatchMode && isset($_GET['batch'])) {
33 $aBatch = json_decode($_GET['batch'], true);
34 $aBatchResults = array();
35 foreach ($aBatch as $aBatchParams) {
36 $oBatchGeocode = clone $oGeocode;
37 $oBatchParams = new Nominatim\ParameterParser($aBatchParams);
38 $oBatchGeocode->loadParamArray($oBatchParams);
39 $oBatchGeocode->setQueryFromParams($oBatchParams);
40 $aSearchResults = $oBatchGeocode->lookup();
41 $aBatchResults[] = $aSearchResults;
43 include(CONST_LibDir.'/template/search-batch-json.php');
47 $oGeocode->setQueryFromParams($oParams);
49 if (!$oGeocode->getQueryString()
50 && isset($_SERVER['PATH_INFO'])
51 && strlen($_SERVER['PATH_INFO']) > 0
52 && $_SERVER['PATH_INFO'][0] == '/'
54 $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
56 // reverse order of '/' separated string
57 $aPhrases = explode('/', $sQuery);
58 $aPhrases = array_reverse($aPhrases);
59 $sQuery = join(', ', $aPhrases);
60 $oGeocode->setQuery($sQuery);
63 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
65 $aSearchResults = $oGeocode->lookup();
67 logEnd($oDB, $hLog, count($aSearchResults));
69 $sQuery = $oGeocode->getQueryString();
71 $aMoreParams = $oGeocode->getMoreUrlParams();
72 $aMoreParams['format'] = $sOutputFormat;
73 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
74 $aMoreParams['accept-language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
77 if (isset($_SERVER['REQUEST_SCHEME'])
78 && isset($_SERVER['HTTP_HOST'])
79 && isset($_SERVER['DOCUMENT_URI'])
81 $sMoreURL = $_SERVER['REQUEST_SCHEME'].'://'
82 .$_SERVER['HTTP_HOST'].$_SERVER['DOCUMENT_URI'].'/?'
83 .http_build_query($aMoreParams);
85 $sMoreURL = '/search.php?'.http_build_query($aMoreParams);
92 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
93 include(CONST_LibDir.'/template/search-'.$sOutputTemplate.'.php');