3 require_once(CONST_LibDir.'/Shell.php');
5 function getCmdOpt($aArg, $aSpec, &$aResult, $bExitOnError = false, $bExitOnUnknown = false)
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;
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]]++;
26 if ($aLine[4] == $aLine[5]) {
29 for ($n = $aLine[4]; $i < $iSize && $n; $n--) {
31 if ($i >= $iSize || $aArg[$i][0] == '-') showUsage($aSpec, $bExitOnError, 'Parameter of \''.$aLine[0].'\' is missing');
35 $xVal[] = realpath($aArg[$i]);
38 $sPath = realpath(dirname($aArg[$i]));
40 $xVal[] = $sPath . '/' . basename($aArg[$i]);
46 $xVal[] = (bool)$aArg[$i];
49 $xVal[] = (int)$aArg[$i];
52 $xVal[] = (float)$aArg[$i];
59 if ($aLine[4] == 1) $xVal = $xVal[0];
64 fail('Variable numbers of params not yet supported');
68 if (!array_key_exists($aLine[0], $aResult)) $aResult[$aLine[0]] = array();
69 $aResult[$aLine[0]][] = $xVal;
71 $aResult[$aLine[0]] = $xVal;
74 $bUnknown = $aArg[$i];
78 if (array_key_exists('help', $aResult)) showUsage($aSpec);
79 if ($bUnknown && $bExitOnUnknown) showUsage($aSpec, $bExitOnError, 'Unknown option \''.$bUnknown.'\'');
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');
87 if (!array_key_exists($aLine[0], $aResult))
88 $aResult[$aLine[0]] = false;
96 function showUsage($aSpec, $bExit = false, $sError = false)
99 echo basename($_SERVER['argv'][0]).': '.$sError."\n";
100 echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
103 echo 'Usage: '.basename($_SERVER['argv'][0])."\n";
105 foreach ($aSpec as $aLine) {
106 if (is_array($aLine)) {
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";
126 echo date('Y-m-d H:i:s == ').$sMsg."\n";
129 $aWarnings = array();
134 $GLOBALS['aWarnings'][] = $sMsg;
135 echo date('Y-m-d H:i:s == ').'WARNING: '.$sMsg."\n";
139 function repeatWarnings()
141 foreach ($GLOBALS['aWarnings'] as $sMsg) {
142 echo ' * ',$sMsg."\n";
147 function runSQLScript($sScript, $bfatal = true, $bVerbose = false, $bIgnoreErrors = false)
149 // Convert database DSN to psql parameters
150 $aDSNInfo = \Nominatim\DB::parseDSN(CONST_Database_DSN);
151 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
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']);
159 if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
160 $oCmd->addParams('--username', $aDSNInfo['username']);
162 if (isset($aDSNInfo['password'])) {
163 $oCmd->addEnvPair('PGPASSWORD', $aDSNInfo['password']);
166 $oCmd->addParams('--quiet');
168 if ($bfatal && !$bIgnoreErrors) {
169 $oCmd->addParams('-v', 'ON_ERROR_STOP=1');
172 $aDescriptors = array(
173 0 => array('pipe', 'r'),
178 $hProcess = @proc_open($oCmd->escapedCmd(), $aDescriptors, $ahPipes, null, $oCmd->aEnv);
179 if (!is_resource($hProcess)) {
180 fail('unable to start pgsql');
184 fwrite($ahPipes[0], 'set client_min_messages to WARNING;');
187 while (strlen($sScript)) {
188 $iWritten = fwrite($ahPipes[0], $sScript);
189 if ($iWritten <= 0) break;
190 $sScript = substr($sScript, $iWritten);
193 $iReturn = proc_close($hProcess);
194 if ($bfatal && $iReturn > 0) {
195 fail("pgsql returned with error code ($iReturn)");
199 function setupHTTPProxy()
201 if (!CONST_HTTP_Proxy) {
205 $sProxy = 'tcp://'.CONST_HTTP_Proxy_Host.':'.CONST_HTTP_Proxy_Port;
208 if (CONST_HTTP_Proxy_Login != null
209 && CONST_HTTP_Proxy_Login != ''
210 && CONST_HTTP_Proxy_Password != null
211 && CONST_HTTP_Proxy_Password != ''
213 $sAuth = base64_encode(CONST_HTTP_Proxy_Login.':'.CONST_HTTP_Proxy_Password);
214 $aHeaders = array('Proxy-Authorization: Basic '.$sAuth);
217 $aProxyHeader = array(
219 'request_fulluri' => true,
220 'header' => $aHeaders
223 $aContext = array('http' => $aProxyHeader, 'https' => $aProxyHeader);
224 stream_context_set_default($aContext);