]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
port check-for-update function to python
[nominatim.git] / 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 $aSearchResults = $oGeocode->lookup();
58
59 logEnd($oDB, $hLog, count($aSearchResults));
60
61 $sQuery = $oGeocode->getQueryString();
62
63 $aMoreParams = $oGeocode->getMoreUrlParams();
64 $aMoreParams['format'] = $sOutputFormat;
65 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
66     $aMoreParams['accept-language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
67 }
68
69 if (isset($_SERVER['REQUEST_SCHEME'])
70     && isset($_SERVER['SERVER_NAME'])
71     && isset($_SERVER['DOCUMENT_URI'])
72 ) {
73     $sMoreURL = $_SERVER['REQUEST_SCHEME'].'://'
74                 .$_SERVER['SERVER_NAME'].$_SERVER['DOCUMENT_URI'].'/?'
75                 .http_build_query($aMoreParams);
76 } else {
77     $sMoreURL = '/search.php'.http_build_query($aMoreParams);
78 }
79
80 if (CONST_Debug) exit;
81
82 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
83 include(CONST_LibDir.'/template/search-'.$sOutputTemplate.'.php');