]> git.openstreetmap.org Git - nominatim.git/blob - website/search.php
Merge pull request #490 from lonvia/streamline-class-interfaces
[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
10         ini_set('memory_limit', '200M');
11
12         $oDB =& getDB();
13
14         $oGeocode = new Geocode($oDB);
15
16         $aLangPrefOrder = 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 = getParamSet('format', array('html', 'xml', 'json', 'jsonv2'), 'html');
30
31         // Show / use polygons
32         if ($sOutputFormat == 'html')
33         {
34                 $oGeocode->setIncludePolygonAsText(getParamBool('polygon'));
35                 $bAsText = false;
36         }
37         else
38         {
39                 $bAsPoints = getParamBool('polygon');
40                 $bAsGeoJSON = getParamBool('polygon_geojson');
41                 $bAsKML = getParamBool('polygon_kml');
42                 $bAsSVG = getParamBool('polygon_svg');
43                 $bAsText = getParamBool('polygon_text');
44                 if ( ( ($bAsGeoJSON?1:0)
45                                  + ($bAsKML?1:0)
46                                  + ($bAsSVG?1:0)
47                                  + ($bAsText?1:0)
48                                  + ($bAsPoints?1:0)
49                                  ) > CONST_PolygonOutput_MaximumTypes)
50                 {
51                         if (CONST_PolygonOutput_MaximumTypes)
52                         {
53                                 userError("Select only ".CONST_PolygonOutput_MaximumTypes." polgyon output option");
54                         }
55                         else
56                         {
57                                 userError("Polygon output is disabled");
58                         }
59                         exit;
60                 }
61                 $oGeocode->setIncludePolygonAsPoints($bAsPoints);
62                 $oGeocode->setIncludePolygonAsText($bAsText);
63                 $oGeocode->setIncludePolygonAsGeoJSON($bAsGeoJSON);
64                 $oGeocode->setIncludePolygonAsKML($bAsKML);
65                 $oGeocode->setIncludePolygonAsSVG($bAsSVG);
66         }
67
68         // Polygon simplification threshold (optional)
69         $oGeocode->setPolygonSimplificationThreshold(getParamFloat('polygon_threshold', 0.0));
70
71         $oGeocode->loadParamArray($_GET);
72
73         if (CONST_Search_BatchMode && isset($_GET['batch']))
74         {
75                 $aBatch = json_decode($_GET['batch'], true);
76                 $aBatchResults = array();
77                 foreach($aBatch as $aBatchParams)
78                 {
79                         $oBatchGeocode = clone $oGeocode;
80                         $oBatchGeocode->loadParamArray($aBatchParams);
81                         $oBatchGeocode->setQueryFromParams($aBatchParams);
82                         $aSearchResults = $oBatchGeocode->lookup();
83                         $aBatchResults[] = $aSearchResults;
84                 }
85                 include(CONST_BasePath.'/lib/template/search-batch-json.php');
86                 exit;
87         }
88
89         if (!getParamString('q') && isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'][0] == '/')
90         {
91                 $sQuery = substr(rawurldecode($_SERVER['PATH_INFO']), 1);
92
93                 // reverse order of '/' separated string
94                 $aPhrases = explode('/', $sQuery);
95                 $aPhrases = array_reverse($aPhrases);
96                 $sQuery = join(', ',$aPhrases);
97                 $oGeocode->setQuery($sQuery);
98         }
99         else
100         {
101                 $oGeocode->setQueryFromParams($_GET);
102         }
103
104         $hLog = logStart($oDB, 'search', $oGeocode->getQueryString(), $aLangPrefOrder);
105
106         $aSearchResults = $oGeocode->lookup();
107         if ($aSearchResults === false) $aSearchResults = array();
108
109         if ($sOutputFormat=='html')
110         {
111                 $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate - '2 minutes'::interval,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
112         }
113         logEnd($oDB, $hLog, sizeof($aSearchResults));
114
115         $sQuery = $oGeocode->getQueryString();
116         $sViewBox = $oGeocode->getViewBoxString();
117         $bShowPolygons = (isset($_GET['polygon']) && $_GET['polygon']);
118         $aExcludePlaceIDs = $oGeocode->getExcludedPlaceIDs();
119
120         $sMoreURL = CONST_Website_BaseURL.'search.php?format='.urlencode($sOutputFormat).'&exclude_place_ids='.join(',',$aExcludePlaceIDs);
121         if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) $sMoreURL .= '&accept-language='.$_SERVER["HTTP_ACCEPT_LANGUAGE"];
122         if ($bShowPolygons) $sMoreURL .= '&polygon=1';
123         if ($oGeocode->getIncludeAddressDetails()) $sMoreURL .= '&addressdetails=1';
124         if ($oGeocode->getIncludeExtraTags()) $sMoreURL .= '&extratags=1';
125         if ($oGeocode->getIncludeNameDetails()) $sMoreURL .= '&namedetails=1';
126         if ($sViewBox) $sMoreURL .= '&viewbox='.urlencode($sViewBox);
127         if (isset($_GET['nearlat']) && isset($_GET['nearlon'])) $sMoreURL .= '&nearlat='.(float)$_GET['nearlat'].'&nearlon='.(float)$_GET['nearlon'];
128         $sMoreURL .= '&q='.urlencode($sQuery);
129
130         if (CONST_Debug) exit;
131
132         include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');