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