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 // Ignore requests from a faulty app.
66 if ($oGeocode->getQueryString() === '-1,-1') {
67 $aSearchResults = array();
69 $aSearchResults = $oGeocode->lookup();
72 logEnd($oDB, $hLog, count($aSearchResults));
74 $sQuery = $oGeocode->getQueryString();
76 $aMoreParams = $oGeocode->getMoreUrlParams();
77 $aMoreParams['format'] = $sOutputFormat;
78 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
79 $aMoreParams['accept-language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
82 if (isset($_SERVER['REQUEST_SCHEME'])
83 && isset($_SERVER['HTTP_HOST'])
84 && isset($_SERVER['DOCUMENT_URI'])
86 $sMoreURL = $_SERVER['REQUEST_SCHEME'].'://'
87 .$_SERVER['HTTP_HOST'].$_SERVER['DOCUMENT_URI'].'/?'
88 .http_build_query($aMoreParams);
90 $sMoreURL = '/search.php?'.http_build_query($aMoreParams);
97 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
98 include(CONST_LibDir.'/template/search-'.$sOutputTemplate.'.php');