]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/lib.php
Merge pull request #3367 from lonvia/address-word-counts
[nominatim.git] / lib-php / lib.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 function loadSettings($sProjectDir)
12 {
13     @define('CONST_InstallDir', $sProjectDir);
14     // Temporary hack to set the directory via environment instead of
15     // the installed scripts. Neither setting is part of the official
16     // set of settings.
17     defined('CONST_ConfigDir') or define('CONST_ConfigDir', $_SERVER['NOMINATIM_CONFIGDIR']);
18 }
19
20 function getSetting($sConfName, $sDefault = null)
21 {
22     $sValue = $_SERVER['NOMINATIM_'.$sConfName];
23
24     if ($sDefault !== null && !$sValue) {
25         return $sDefault;
26     }
27
28     return $sValue;
29 }
30
31 function getSettingBool($sConfName)
32 {
33     $sVal = strtolower(getSetting($sConfName));
34
35     return strcmp($sVal, 'yes') == 0
36            || strcmp($sVal, 'true') == 0
37            || strcmp($sVal, '1') == 0;
38 }
39
40 function fail($sError, $sUserError = false)
41 {
42     if (!$sUserError) {
43         $sUserError = $sError;
44     }
45     error_log('ERROR: '.$sError);
46     var_dump($sUserError);
47     echo "\n";
48     exit(-1);
49 }
50
51
52 function getProcessorCount()
53 {
54     $sCPU = file_get_contents('/proc/cpuinfo');
55     preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
56     return count($aMatches[0]);
57 }
58
59
60 function getTotalMemoryMB()
61 {
62     $sCPU = file_get_contents('/proc/meminfo');
63     preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
64     return (int)($aMatches[1]/1024);
65 }
66
67
68 function getCacheMemoryMB()
69 {
70     $sCPU = file_get_contents('/proc/meminfo');
71     preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
72     return (int)($aMatches[1]/1024);
73 }
74
75 function getDatabaseDate(&$oDB)
76 {
77     // Find the newest node in the DB
78     $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
79     // Lookup the timestamp that node was created
80     $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
81     $sLastNodeXML = file_get_contents($sLastNodeURL);
82
83     if ($sLastNodeXML === false) {
84         return false;
85     }
86
87     preg_match('#timestamp="(([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})Z)"#', $sLastNodeXML, $aLastNodeDate);
88
89     return $aLastNodeDate[1];
90 }
91
92
93 function byImportance($a, $b)
94 {
95     if ($a['importance'] != $b['importance']) {
96         return ($a['importance'] > $b['importance']?-1:1);
97     }
98
99     return $a['foundorder'] <=> $b['foundorder'];
100 }
101
102
103 function javascript_renderData($xVal, $iOptions = 0)
104 {
105     $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
106     if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
107         // Unset, we call javascript_renderData again during exception handling
108         unset($_GET['json_callback']);
109         throw new Exception('Invalid json_callback value', 400);
110     }
111
112     $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
113     if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
114         $iOptions |= JSON_PRETTY_PRINT;
115     }
116
117     $jsonout = json_encode($xVal, $iOptions);
118
119     if ($sCallback) {
120         header('Content-Type: application/javascript; charset=UTF-8');
121         echo $_GET['json_callback'].'('.$jsonout.')';
122     } else {
123         header('Content-Type: application/json; charset=UTF-8');
124         echo $jsonout;
125     }
126 }
127
128 function addQuotes($s)
129 {
130     return "'".$s."'";
131 }
132
133 function parseLatLon($sQuery)
134 {
135     $sFound    = null;
136     $fQueryLat = null;
137     $fQueryLon = null;
138
139     if (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9.]*)[°\s]+([0-9.]+)?[′\']*[,\s]+([EW])[\s]+([0-9]+)[°\s]+([0-9]+[0-9.]*)[′\']*\\s*/', $sQuery, $aData)) {
140         /*               1          2                    3                     4          5             6
141          * degrees decimal minutes
142          * N 40 26.767, W 79 58.933
143          * N 40°26.767′, W 79°58.933′
144          */
145         $sFound    = $aData[0];
146         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
147         $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
148     } elseif (preg_match('/\\s*([0-9]+)[°\s]+([0-9]+[0-9.]*)?[′\']*[\s]+([NS])[,\s]+([0-9]+)[°\s]+([0-9]+[0-9.]*)?[′\'\s]+([EW])\\s*/', $sQuery, $aData)) {
149         /*                     1             2                          3           4             5                       6
150          * degrees decimal minutes
151          * 40 26.767 N, 79 58.933 W
152          * 40° 26.767′ N 79° 58.933′ W
153          */
154         $sFound    = $aData[0];
155         $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
156         $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
157     } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+)[°\s]+([0-9]+)[′\'\s]+([0-9]+)[″"]*[,\s]+([EW])[\s]+([0-9]+)[°\s]+([0-9]+)[′\'\s]+([0-9]+)[″"]*\\s*/', $sQuery, $aData)) {
158         /*                     1          2             3               4                  5          6             7               8
159          * degrees decimal seconds
160          * N 40 26 46 W 79 58 56
161          * N 40° 26′ 46″, W 79° 58′ 56″
162          */
163         $sFound    = $aData[0];
164         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
165         $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
166     } elseif (preg_match('/\\s*([0-9]+)[°\s]+([0-9]+)[′\'\s]+([0-9]+[0-9.]*)[″"\s]+([NS])[,\s]+([0-9]+)[°\s]+([0-9]+)[′\'\s]+([0-9]+[0-9.]*)[″"\s]+([EW])\\s*/', $sQuery, $aData)) {
167         /*                     1             2               3                     4           5             6               7                     8
168          * degrees decimal seconds
169          * 40 26 46 N 79 58 56 W
170          * 40° 26′ 46″ N, 79° 58′ 56″ W
171          * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
172          */
173         $sFound    = $aData[0];
174         $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
175         $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
176     } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
177         /*                     1          2                                3          4
178          * degrees decimal
179          * N 40.446° W 79.982°
180          */
181         $sFound    = $aData[0];
182         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
183         $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
184     } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
185         /*                     1                            2           3                            4
186          * degrees decimal
187          * 40.446° N 79.982° W
188          */
189         $sFound    = $aData[0];
190         $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
191         $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
192     } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
193         /*                 1                   2                              3                        4
194          * degrees decimal
195          * 12.34, 56.78
196          * 12.34 56.78
197          * [12.456,-78.90]
198          */
199         $sFound    = $aData[0];
200         $fQueryLat = $aData[2];
201         $fQueryLon = $aData[3];
202     } else {
203         return false;
204     }
205
206     return array($sFound, $fQueryLat, $fQueryLon);
207 }
208
209 function addressRankToGeocodeJsonType($iAddressRank)
210 {
211     if ($iAddressRank >= 29 && $iAddressRank <= 30) {
212         return 'house';
213     }
214     if ($iAddressRank >= 26 && $iAddressRank < 28) {
215         return 'street';
216     }
217     if ($iAddressRank >= 22 && $iAddressRank < 26) {
218         return 'locality';
219     }
220     if ($iAddressRank >= 17 && $iAddressRank < 22) {
221         return 'district';
222     }
223     if ($iAddressRank >= 13 && $iAddressRank < 17) {
224         return 'city';
225     }
226     if ($iAddressRank >= 10 && $iAddressRank < 13) {
227         return 'county';
228     }
229     if ($iAddressRank >= 5 && $iAddressRank < 10) {
230         return 'state';
231     }
232     if ($iAddressRank >= 4 && $iAddressRank < 5) {
233         return 'country';
234     }
235
236     return 'locality';
237 }
238
239 if (!function_exists('array_key_last')) {
240     function array_key_last(array $array)
241     {
242         if (!empty($array)) {
243             return key(array_slice($array, -1, 1, true));
244         }
245     }
246 }