4 function getCmdOpt($aArg, $aSpec, &$aResult, $bExitOnError = false, $bExitOnUnknown = false)
9 foreach ($aSpec as $aLine) {
10 if (is_array($aLine)) {
11 if ($aLine[0]) $aQuick['--'.$aLine[0]] = $aLine;
12 if ($aLine[1]) $aQuick['-'.$aLine[1]] = $aLine;
13 $aCounts[$aLine[0]] = 0;
19 $iSize = count($aArg);
20 for ($i = 1; $i < $iSize; $i++) {
21 if (isset($aQuick[$aArg[$i]])) {
22 $aLine = $aQuick[$aArg[$i]];
23 $aCounts[$aLine[0]]++;
25 if ($aLine[4] == $aLine[5]) {
28 for ($n = $aLine[4]; $i < $iSize && $n; $n--) {
30 if ($i >= $iSize || $aArg[$i][0] == '-') showUsage($aSpec, $bExitOnError, 'Parameter of \''.$aLine[0].'\' is missing');
34 $xVal[] = realpath($aArg[$i]);
37 $sPath = realpath(dirname($aArg[$i]));
39 $xVal[] = $sPath . '/' . basename($aArg[$i]);
45 $xVal[] = (bool)$aArg[$i];
48 $xVal[] = (int)$aArg[$i];
51 $xVal[] = (float)$aArg[$i];
58 if ($aLine[4] == 1) $xVal = $xVal[0];
63 fail('Variable numbers of params not yet supported');
67 if (!array_key_exists($aLine[0], $aResult)) $aResult[$aLine[0]] = array();
68 $aResult[$aLine[0]][] = $xVal;
70 $aResult[$aLine[0]] = $xVal;
73 $bUnknown = $aArg[$i];
77 if (array_key_exists('help', $aResult)) showUsage($aSpec);
78 if ($bUnknown && $bExitOnUnknown) showUsage($aSpec, $bExitOnError, 'Unknown option \''.$bUnknown.'\'');
80 foreach ($aSpec as $aLine) {
81 if (is_array($aLine)) {
82 if ($aCounts[$aLine[0]] < $aLine[2]) showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is missing');
83 if ($aCounts[$aLine[0]] > $aLine[3]) showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is pressent too many times');
86 if (!array_key_exists($aLine[0], $aResult))
87 $aResult[$aLine[0]] = false;
95 function showUsage($aSpec, $bExit = false, $sError = false)
98 echo basename($_SERVER['argv'][0]).': '.$sError."\n";
99 echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
102 echo 'Usage: '.basename($_SERVER['argv'][0])."\n";
104 foreach ($aSpec as $aLine) {
105 if (is_array($aLine)) {
111 if ($aLine[1]) $aNames[] = '-'.$aLine[1];
112 if ($aLine[0]) $aNames[] = '--'.$aLine[0];
113 $sName = join(', ', $aNames);
114 echo ' '.$sName.str_repeat(' ', 30-strlen($sName)).$aLine[7]."\n";
123 function chksql($oSql, $sMsg = false)
130 echo date('Y-m-d H:i:s == ').$sMsg."\n";
133 $aWarnings = array();
138 $GLOBALS['aWarnings'][] = $sMsg;
139 echo date('Y-m-d H:i:s == ').'WARNING: '.$sMsg."\n";
143 function repeatWarnings()
145 foreach ($GLOBALS['aWarnings'] as $sMsg) {
146 echo ' * ',$sMsg."\n";
151 function runSQLScript($sScript, $bfatal = true, $bVerbose = false, $bIgnoreErrors = false)
153 // Convert database DSN to psql parameters
154 $aDSNInfo = \Nominatim\DB::parseDSN(CONST_Database_DSN);
155 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
156 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
157 if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
158 $sCMD .= ' -h ' . $aDSNInfo['hostspec'];
160 if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
161 $sCMD .= ' -U ' . $aDSNInfo['username'];
164 if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
165 $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
170 if ($bfatal && !$bIgnoreErrors) {
171 $sCMD .= ' -v ON_ERROR_STOP=1';
173 $aDescriptors = array(
174 0 => array('pipe', 'r'),
179 $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes, null, $aProcEnv);
180 if (!is_resource($hProcess)) {
181 fail('unable to start pgsql');
185 fwrite($ahPipes[0], 'set client_min_messages to WARNING;');
188 while (strlen($sScript)) {
189 $iWritten = fwrite($ahPipes[0], $sScript);
190 if ($iWritten <= 0) break;
191 $sScript = substr($sScript, $iWritten);
194 $iReturn = proc_close($hProcess);
195 if ($bfatal && $iReturn > 0) {
196 fail("pgsql returned with error code ($iReturn)");
201 function runWithEnv($sCmd, $aEnv)
204 0 => array('pipe', 'r'),
209 $hProc = @proc_open($sCmd, $aFDs, $aPipes, null, $aEnv);
210 if (!is_resource($hProc)) {
211 fail('unable to run command:' . $sCmd);
214 fclose($aPipes[0]); // no stdin
216 $iStat = proc_close($hProc);