]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/website/lookup.php
Merge pull request #2391 from lonvia/fix-sonar-issues
[nominatim.git] / lib-php / website / lookup.php
1 <?php
2
3 require_once(CONST_LibDir.'/init-website.php');
4 require_once(CONST_LibDir.'/log.php');
5 require_once(CONST_LibDir.'/PlaceLookup.php');
6 require_once(CONST_LibDir.'/output.php');
7 ini_set('memory_limit', '200M');
8
9 $oParams = new Nominatim\ParameterParser();
10
11 // Format for output
12 $sOutputFormat = $oParams->getSet('format', array('xml', 'json', 'jsonv2', 'geojson', 'geocodejson'), 'xml');
13 set_exception_handler_by_format($sOutputFormat);
14
15 // Preferred language
16 $aLangPrefOrder = $oParams->getPreferredLanguages();
17
18 $oDB = new Nominatim\DB(CONST_Database_DSN);
19 $oDB->connect();
20
21 $hLog = logStart($oDB, 'place', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
22
23 $aSearchResults = array();
24 $aCleanedQueryParts = array();
25
26 $oPlaceLookup = new Nominatim\PlaceLookup($oDB);
27 $oPlaceLookup->loadParamArray($oParams);
28 $oPlaceLookup->setIncludeAddressDetails($oParams->getBool('addressdetails', true));
29
30 $aOsmIds = explode(',', $oParams->getString('osm_ids', ''));
31
32 if (count($aOsmIds) > CONST_Places_Max_ID_count) {
33     userError('Bulk User: Only ' . CONST_Places_Max_ID_count . ' ids are allowed in one request.');
34 }
35
36 foreach ($aOsmIds as $sItem) {
37     // Skip empty sItem
38     if (empty($sItem)) {
39         continue;
40     }
41
42     $sType = $sItem[0];
43     $iId = (int) substr($sItem, 1);
44     if ($iId > 0 && ($sType == 'N' || $sType == 'W' || $sType == 'R')) {
45         $aCleanedQueryParts[] = $sType . $iId;
46         $oPlace = $oPlaceLookup->lookupOSMID($sType, $iId);
47         if ($oPlace) {
48             // we want to use the search-* output templates, so we need to fill
49             // $aSearchResults and slightly change the (reverse search) oPlace
50             // key names
51             $oResult = $oPlace;
52             unset($oResult['aAddress']);
53             if (isset($oPlace['aAddress'])) {
54                 $oResult['address'] = $oPlace['aAddress'];
55             }
56             if ($sOutputFormat != 'geocodejson') {
57                 unset($oResult['langaddress']);
58                 $oResult['name'] = $oPlace['langaddress'];
59             }
60
61             $aOutlineResult = $oPlaceLookup->getOutlines(
62                 $oPlace['place_id'],
63                 $oPlace['lon'],
64                 $oPlace['lat'],
65                 Nominatim\ClassTypes\getDefRadius($oPlace)
66             );
67
68             if ($aOutlineResult) {
69                 $oResult = array_merge($oResult, $aOutlineResult);
70             }
71
72             $aSearchResults[] = $oResult;
73         }
74     }
75 }
76
77
78 if (CONST_Debug) {
79     exit;
80 }
81
82 $sXmlRootTag = 'lookupresults';
83 $sQuery = join(',', $aCleanedQueryParts);
84 // we initialize these to avoid warnings in our logfile
85 $sViewBox = '';
86 $bShowPolygons = '';
87 $aExcludePlaceIDs = array();
88 $sMoreURL = '';
89
90 logEnd($oDB, $hLog, 1);
91
92 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
93 include(CONST_LibDir.'/template/search-'.$sOutputTemplate.'.php');