]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
set exception handler by request format, not always HTML
[nominatim.git] / website / search.php
1 <?php
2 @define('CONST_ConnectionBucket_PageType', 'Search');
3
4 require_once(dirname(dirname(__FILE__)).'/settings/settings.php');
5 require_once(CONST_BasePath.'/lib/init-website.php');
6 require_once(CONST_BasePath.'/lib/log.php');
7 require_once(CONST_BasePath.'/lib/Geocode.php');
8 require_once(CONST_BasePath.'/lib/output.php');
9 ini_set('memory_limit', '200M');
10
11 $oDB =& getDB();
12 $oParams = new Nominatim\ParameterParser();
13
14 $oGeocode = new Nominatim\Geocode($oDB);
15
16 $aLangPrefOrder = $oParams->getPreferredLanguages();
17 $oGeocode->setLanguagePreference($aLangPrefOrder);
18
19 if (CONST_Search_ReversePlanForAll
20     || isset($aLangPrefOrder['name:de'])
21     || isset($aLangPrefOrder['name:ru'])
22     || isset($aLangPrefOrder['name:ja'])
23     || isset($aLangPrefOrder['name:pl'])
24 ) {
25     $oGeocode->setReverseInPlan(true);
26 }
27
28 // Format for output
29 $sOutputFormat = $oParams->getSet('format', array('html', 'xml', 'json', 'jsonv2', 'geojson', 'geocodejson'), 'html');
30 set_exception_handler_by_format($sOutputFormat);
31
32 $sForcedGeometry = ($sOutputFormat == 'html') ? 'geojson' : null;
33 $oGeocode->loadParamArray($oParams, $sForcedGeometry);
34
35 if (CONST_Search_BatchMode && isset($_GET['batch'])) {
36     $aBatch = json_decode($_GET['batch'], true);
37     $aBatchResults = array();
38     foreach ($aBatch as $aBatchParams) {
39         $oBatchGeocode = clone $oGeocode;
40         $oBatchParams = new Nominatim\ParameterParser($aBatchParams);
41         $oBatchGeocode->loadParamArray($oBatchParams);
42         $oBatchGeocode->setQueryFromParams($oBatchParams);
43         $aSearchResults = $oBatchGeocode->lookup();
44         $aBatchResults[] = $aSearchResults;
45     }
46     include(CONST_BasePath.'/lib/template/search-batch-json.php');
47     exit;
48 }
49
50 $oGeocode->setQueryFromParams($oParams);
51
52 if (!$oGeocode->getQueryString()
53     && isset($_SERVER['PATH_INFO'])
54     && $_SERVER['PATH_INFO'][0] == '/'
55 ) {
56     $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
57
58     // reverse order of '/' separated string
59     $aPhrases = explode('/', $sQuery);
60     $aPhrases = array_reverse($aPhrases);
61     $sQuery = join(', ', $aPhrases);
62     $oGeocode->setQuery($sQuery);
63 }
64
65 $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
66
67 $aSearchResults = $oGeocode->lookup();
68
69 if ($sOutputFormat=='html') {
70     $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
71 }
72 logEnd($oDB, $hLog, count($aSearchResults));
73
74 $sQuery = $oGeocode->getQueryString();
75
76 $aMoreParams = $oGeocode->getMoreUrlParams();
77 if ($sOutputFormat != 'html') $aMoreParams['format'] = $sOutputFormat;
78 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
79     $aMoreParams['accept-language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
80 }
81 $sMoreURL = CONST_Website_BaseURL.'search.php?'.http_build_query($aMoreParams);
82
83 if (CONST_Debug) exit;
84
85 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
86 include(CONST_BasePath.'/lib/template/search-'.$sOutputTemplate.'.php');