]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/website/search.php
ICU: additional ranking by matching of normalised term
[nominatim.git] / lib-php / website / search.php
1 <?php
2
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');
8
9 $oDB = new Nominatim\DB(CONST_Database_DSN);
10 $oDB->connect();
11 $oParams = new Nominatim\ParameterParser();
12
13 $oGeocode = new Nominatim\Geocode($oDB);
14
15 $aLangPrefOrder = $oParams->getPreferredLanguages();
16 $oGeocode->setLanguagePreference($aLangPrefOrder);
17
18 // Format for output
19 $sOutputFormat = $oParams->getSet('format', array('xml', 'json', 'jsonv2', 'geojson', 'geocodejson'), 'jsonv2');
20 set_exception_handler_by_format($sOutputFormat);
21
22 $oGeocode->loadParamArray($oParams, null);
23
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;
34     }
35     include(CONST_LibDir.'/template/search-batch-json.php');
36     exit;
37 }
38
39 $oGeocode->setQueryFromParams($oParams);
40
41 if (!$oGeocode->getQueryString()
42     && isset($_SERVER['PATH_INFO'])
43     && strlen($_SERVER['PATH_INFO']) > 0
44     && $_SERVER['PATH_INFO'][0] == '/'
45 ) {
46     $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
47
48     // reverse order of '/' separated string
49     $aPhrases = explode('/', $sQuery);
50     $aPhrases = array_reverse($aPhrases);
51     $sQuery = join(', ', $aPhrases);
52     $oGeocode->setQuery($sQuery);
53 }
54
55 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
56
57 // Ignore requests from a faulty app.
58 if ($oGeocode->getQueryString() === '-1,-1') {
59   $aSearchResults = array();
60 } else {
61   $aSearchResults = $oGeocode->lookup();
62 }
63
64 logEnd($oDB, $hLog, count($aSearchResults));
65
66 $sQuery = $oGeocode->getQueryString();
67
68 $aMoreParams = $oGeocode->getMoreUrlParams();
69 $aMoreParams['format'] = $sOutputFormat;
70 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
71     $aMoreParams['accept-language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
72 }
73
74 if (isset($_SERVER['REQUEST_SCHEME'])
75     && isset($_SERVER['HTTP_HOST'])
76     && isset($_SERVER['DOCUMENT_URI'])
77 ) {
78     $sMoreURL = $_SERVER['REQUEST_SCHEME'].'://'
79                 .$_SERVER['HTTP_HOST'].$_SERVER['DOCUMENT_URI'].'/?'
80                 .http_build_query($aMoreParams);
81 } else {
82     $sMoreURL = '/search.php?'.http_build_query($aMoreParams);
83 }
84
85 if (CONST_Debug) {
86     exit;
87 }
88
89 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
90 include(CONST_LibDir.'/template/search-'.$sOutputTemplate.'.php');