]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/website/search.php
Merge pull request #2793 from lonvia/increase-minimum-results
[nominatim.git] / lib-php / website / search.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
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');
16
17 $oDB = new Nominatim\DB(CONST_Database_DSN);
18 $oDB->connect();
19 $oParams = new Nominatim\ParameterParser();
20
21 $oGeocode = new Nominatim\Geocode($oDB);
22
23 $aLangPrefOrder = $oParams->getPreferredLanguages();
24 $oGeocode->setLanguagePreference($aLangPrefOrder);
25
26 // Format for output
27 $sOutputFormat = $oParams->getSet('format', array('xml', 'json', 'jsonv2', 'geojson', 'geocodejson'), 'jsonv2');
28 set_exception_handler_by_format($sOutputFormat);
29
30 $oGeocode->loadParamArray($oParams, null);
31
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;
42     }
43     include(CONST_LibDir.'/template/search-batch-json.php');
44     exit;
45 }
46
47 $oGeocode->setQueryFromParams($oParams);
48
49 if (!$oGeocode->getQueryString()
50     && isset($_SERVER['PATH_INFO'])
51     && strlen($_SERVER['PATH_INFO']) > 0
52     && $_SERVER['PATH_INFO'][0] == '/'
53 ) {
54     $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
55
56     // reverse order of '/' separated string
57     $aPhrases = explode('/', $sQuery);
58     $aPhrases = array_reverse($aPhrases);
59     $sQuery = join(', ', $aPhrases);
60     $oGeocode->setQuery($sQuery);
61 }
62
63 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
64
65 $aSearchResults = $oGeocode->lookup();
66
67 logEnd($oDB, $hLog, count($aSearchResults));
68
69 $sQuery = $oGeocode->getQueryString();
70
71 $aMoreParams = $oGeocode->getMoreUrlParams();
72 $aMoreParams['format'] = $sOutputFormat;
73 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
74     $aMoreParams['accept-language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
75 }
76
77 if (isset($_SERVER['REQUEST_SCHEME'])
78     && isset($_SERVER['HTTP_HOST'])
79     && isset($_SERVER['DOCUMENT_URI'])
80 ) {
81     $sMoreURL = $_SERVER['REQUEST_SCHEME'].'://'
82                 .$_SERVER['HTTP_HOST'].$_SERVER['DOCUMENT_URI'].'/?'
83                 .http_build_query($aMoreParams);
84 } else {
85     $sMoreURL = '/search.php?'.http_build_query($aMoreParams);
86 }
87
88 if (CONST_Debug) {
89     exit;
90 }
91
92 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
93 include(CONST_LibDir.'/template/search-'.$sOutputTemplate.'.php');