]> git.openstreetmap.org Git - nominatim.git/blob - lib/cmd.php
switch remaining settings to dotenv format
[nominatim.git] / lib / cmd.php
1 <?php
2
3 require_once(CONST_LibDir.'/Shell.php');
4
5 function getCmdOpt($aArg, $aSpec, &$aResult, $bExitOnError = false, $bExitOnUnknown = false)
6 {
7     $aQuick = array();
8     $aCounts = array();
9
10     foreach ($aSpec as $aLine) {
11         if (is_array($aLine)) {
12             if ($aLine[0]) $aQuick['--'.$aLine[0]] = $aLine;
13             if ($aLine[1]) $aQuick['-'.$aLine[1]] = $aLine;
14             $aCounts[$aLine[0]] = 0;
15         }
16     }
17
18     $aResult = array();
19     $bUnknown = false;
20     $iSize = count($aArg);
21     for ($i = 1; $i < $iSize; $i++) {
22         if (isset($aQuick[$aArg[$i]])) {
23             $aLine = $aQuick[$aArg[$i]];
24             $aCounts[$aLine[0]]++;
25             $xVal = null;
26             if ($aLine[4] == $aLine[5]) {
27                 if ($aLine[4]) {
28                     $xVal = array();
29                     for ($n = $aLine[4]; $i < $iSize && $n; $n--) {
30                         $i++;
31                         if ($i >= $iSize || $aArg[$i][0] == '-') showUsage($aSpec, $bExitOnError, 'Parameter of  \''.$aLine[0].'\' is missing');
32
33                         switch ($aLine[6]) {
34                             case 'realpath':
35                                 $xVal[] = realpath($aArg[$i]);
36                                 break;
37                             case 'realdir':
38                                 $sPath = realpath(dirname($aArg[$i]));
39                                 if ($sPath) {
40                                     $xVal[] = $sPath . '/' . basename($aArg[$i]);
41                                 } else {
42                                     $xVal[] = $sPath;
43                                 }
44                                 break;
45                             case 'bool':
46                                 $xVal[] = (bool)$aArg[$i];
47                                 break;
48                             case 'int':
49                                 $xVal[] = (int)$aArg[$i];
50                                 break;
51                             case 'float':
52                                 $xVal[] = (float)$aArg[$i];
53                                 break;
54                             default:
55                                 $xVal[] = $aArg[$i];
56                                 break;
57                         }
58                     }
59                     if ($aLine[4] == 1) $xVal = $xVal[0];
60                 } else {
61                     $xVal = true;
62                 }
63             } else {
64                 fail('Variable numbers of params not yet supported');
65             }
66
67             if ($aLine[3] > 1) {
68                 if (!array_key_exists($aLine[0], $aResult)) $aResult[$aLine[0]] = array();
69                 $aResult[$aLine[0]][] = $xVal;
70             } else {
71                 $aResult[$aLine[0]] = $xVal;
72             }
73         } else {
74             $bUnknown = $aArg[$i];
75         }
76     }
77
78     if (array_key_exists('help', $aResult)) showUsage($aSpec);
79     if ($bUnknown && $bExitOnUnknown) showUsage($aSpec, $bExitOnError, 'Unknown option \''.$bUnknown.'\'');
80
81     foreach ($aSpec as $aLine) {
82         if (is_array($aLine)) {
83             if ($aCounts[$aLine[0]] < $aLine[2]) showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is missing');
84             if ($aCounts[$aLine[0]] > $aLine[3]) showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is pressent too many times');
85             switch ($aLine[6]) {
86                 case 'bool':
87                     if (!array_key_exists($aLine[0], $aResult))
88                         $aResult[$aLine[0]] = false;
89                     break;
90             }
91         }
92     }
93     return $bUnknown;
94 }
95
96 function showUsage($aSpec, $bExit = false, $sError = false)
97 {
98     if ($sError) {
99         echo basename($_SERVER['argv'][0]).': '.$sError."\n";
100         echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
101         exit;
102     }
103     echo 'Usage: '.basename($_SERVER['argv'][0])."\n";
104     $bFirst = true;
105     foreach ($aSpec as $aLine) {
106         if (is_array($aLine)) {
107             if ($bFirst) {
108                 $bFirst = false;
109                 echo "\n";
110             }
111             $aNames = array();
112             if ($aLine[1]) $aNames[] = '-'.$aLine[1];
113             if ($aLine[0]) $aNames[] = '--'.$aLine[0];
114             $sName = join(', ', $aNames);
115             echo '  '.$sName.str_repeat(' ', 30-strlen($sName)).$aLine[7]."\n";
116         } else {
117             echo $aLine."\n";
118         }
119     }
120     echo "\n";
121     exit;
122 }
123
124 function info($sMsg)
125 {
126     echo date('Y-m-d H:i:s == ').$sMsg."\n";
127 }
128
129 $aWarnings = array();
130
131
132 function warn($sMsg)
133 {
134     $GLOBALS['aWarnings'][] = $sMsg;
135     echo date('Y-m-d H:i:s == ').'WARNING: '.$sMsg."\n";
136 }
137
138
139 function repeatWarnings()
140 {
141     foreach ($GLOBALS['aWarnings'] as $sMsg) {
142         echo '  * ',$sMsg."\n";
143     }
144 }
145
146
147 function runSQLScript($sScript, $bfatal = true, $bVerbose = false, $bIgnoreErrors = false)
148 {
149     // Convert database DSN to psql parameters
150     $aDSNInfo = \Nominatim\DB::parseDSN(getSetting('DATABASE_DSN'));
151     if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
152
153     $oCmd = new \Nominatim\Shell('psql');
154     $oCmd->addParams('--port', $aDSNInfo['port']);
155     $oCmd->addParams('--dbname', $aDSNInfo['database']);
156     if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
157         $oCmd->addParams('--host', $aDSNInfo['hostspec']);
158     }
159     if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
160         $oCmd->addParams('--username', $aDSNInfo['username']);
161     }
162     if (isset($aDSNInfo['password'])) {
163         $oCmd->addEnvPair('PGPASSWORD', $aDSNInfo['password']);
164     }
165     if (!$bVerbose) {
166         $oCmd->addParams('--quiet');
167     }
168     if ($bfatal && !$bIgnoreErrors) {
169         $oCmd->addParams('-v', 'ON_ERROR_STOP=1');
170     }
171
172     $aDescriptors = array(
173                      0 => array('pipe', 'r'),
174                      1 => STDOUT,
175                      2 => STDERR
176                     );
177     $ahPipes = null;
178     $hProcess = @proc_open($oCmd->escapedCmd(), $aDescriptors, $ahPipes, null, $oCmd->aEnv);
179     if (!is_resource($hProcess)) {
180         fail('unable to start pgsql');
181     }
182
183     if (!$bVerbose) {
184         fwrite($ahPipes[0], 'set client_min_messages to WARNING;');
185     }
186
187     while (strlen($sScript)) {
188         $iWritten = fwrite($ahPipes[0], $sScript);
189         if ($iWritten <= 0) break;
190         $sScript = substr($sScript, $iWritten);
191     }
192     fclose($ahPipes[0]);
193     $iReturn = proc_close($hProcess);
194     if ($bfatal && $iReturn > 0) {
195         fail("pgsql returned with error code ($iReturn)");
196     }
197 }
198
199 function setupHTTPProxy()
200 {
201     if (!getSettingBool('HTTP_PROXY')) {
202         return;
203     }
204
205     $sProxy = 'tcp://'.getSetting('HTTP_PROXY_HOST').':'.getSetting('HTTP_PROXY_PROT');
206     $aHeaders = array();
207
208     $sLogin = getSetting('HTTP_PROXY_LOGIN');
209     $sPassword = getSetting('HTTP_PROXY_PASSWORD');
210
211     if ($sLogin && $sPassword) {
212         $sAuth = base64_encode($sLogin.':'.$sPassword);
213         $aHeaders = array('Proxy-Authorization: Basic '.$sAuth);
214     }
215
216     $aProxyHeader = array(
217                      'proxy' => $sProxy,
218                      'request_fulluri' => true,
219                      'header' => $aHeaders
220                     );
221
222     $aContext = array('http' => $aProxyHeader, 'https' => $aProxyHeader);
223     stream_context_set_default($aContext);
224 }