]> git.openstreetmap.org Git - nominatim.git/blob - lib/cmd.php
9efe5653b735ca45dd12a62da471d824436dc4ee
[nominatim.git] / lib / cmd.php
1 <?php
2
3
4 function getCmdOpt($aArg, $aSpec, &$aResult, $bExitOnError = false, $bExitOnUnknown = false)
5 {
6     $aQuick = array();
7     $aCounts = array();
8
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;
14         }
15     }
16
17     $aResult = array();
18     $bUnknown = false;
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]]++;
24             $xVal = null;
25             if ($aLine[4] == $aLine[5]) {
26                 if ($aLine[4]) {
27                     $xVal = array();
28                     for ($n = $aLine[4]; $i < $iSize && $n; $n--) {
29                         $i++;
30                         if ($i >= $iSize || $aArg[$i][0] == '-') showUsage($aSpec, $bExitOnError, 'Parameter of  \''.$aLine[0].'\' is missing');
31
32                         switch ($aLine[6]) {
33                             case 'realpath':
34                                 $xVal[] = realpath($aArg[$i]);
35                                 break;
36                             case 'realdir':
37                                 $sPath = realpath(dirname($aArg[$i]));
38                                 if ($sPath) {
39                                     $xVal[] = $sPath . '/' . basename($aArg[$i]);
40                                 } else {
41                                     $xVal[] = $sPath;
42                                 }
43                                 break;
44                             case 'bool':
45                                 $xVal[] = (bool)$aArg[$i];
46                                 break;
47                             case 'int':
48                                 $xVal[] = (int)$aArg[$i];
49                                 break;
50                             case 'float':
51                                 $xVal[] = (float)$aArg[$i];
52                                 break;
53                             default:
54                                 $xVal[] = $aArg[$i];
55                                 break;
56                         }
57                     }
58                     if ($aLine[4] == 1) $xVal = $xVal[0];
59                 } else {
60                     $xVal = true;
61                 }
62             } else {
63                 fail('Variable numbers of params not yet supported');
64             }
65
66             if ($aLine[3] > 1) {
67                 if (!array_key_exists($aLine[0], $aResult)) $aResult[$aLine[0]] = array();
68                 $aResult[$aLine[0]][] = $xVal;
69             } else {
70                 $aResult[$aLine[0]] = $xVal;
71             }
72         } else {
73             $bUnknown = $aArg[$i];
74         }
75     }
76
77     if (array_key_exists('help', $aResult)) showUsage($aSpec);
78     if ($bUnknown && $bExitOnUnknown) showUsage($aSpec, $bExitOnError, 'Unknown option \''.$bUnknown.'\'');
79
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');
84             switch ($aLine[6]) {
85                 case 'bool':
86                     if (!array_key_exists($aLine[0], $aResult))
87                         $aResult[$aLine[0]] = false;
88                     break;
89             }
90         }
91     }
92     return $bUnknown;
93 }
94
95 function showUsage($aSpec, $bExit = false, $sError = false)
96 {
97     if ($sError) {
98         echo basename($_SERVER['argv'][0]).': '.$sError."\n";
99         echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
100         exit;
101     }
102     echo 'Usage: '.basename($_SERVER['argv'][0])."\n";
103     $bFirst = true;
104     foreach ($aSpec as $aLine) {
105         if (is_array($aLine)) {
106             if ($bFirst) {
107                 $bFirst = false;
108                 echo "\n";
109             }
110             $aNames = array();
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";
115         } else {
116             echo $aLine."\n";
117         }
118     }
119     echo "\n";
120     exit;
121 }
122
123 function chksql($oSql, $sMsg = false)
124 {
125     if (PEAR::isError($oSql)) {
126         fail($sMsg || $oSql->getMessage(), $oSql->userinfo);
127     }
128
129     return $oSql;
130 }
131
132 function info($sMsg)
133 {
134     echo date('Y-m-d H:i:s == ').$sMsg."\n";
135 }
136
137 $aWarnings = array();
138
139
140 function warn($sMsg)
141 {
142     $GLOBALS['aWarnings'][] = $sMsg;
143     echo date('Y-m-d H:i:s == ').'WARNING: '.$sMsg."\n";
144 }
145
146
147 function repeatWarnings()
148 {
149     foreach ($GLOBALS['aWarnings'] as $sMsg) {
150         echo '  * ',$sMsg."\n";
151     }
152 }
153
154
155 function runSQLScript($sScript, $bfatal = true, $bVerbose = false, $bIgnoreErrors = false)
156 {
157     // Convert database DSN to psql parameters
158     $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
159     if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
160     $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
161     if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
162         $sCMD .= ' -h ' . $aDSNInfo['hostspec'];
163     }
164     if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
165         $sCMD .= ' -U ' . $aDSNInfo['username'];
166     }
167     $aProcEnv = null;
168     if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
169         $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
170     }
171     if (!$bVerbose) {
172         $sCMD .= ' -q';
173     }
174     if ($bfatal && !$bIgnoreErrors) {
175         $sCMD .= ' -v ON_ERROR_STOP=1';
176     }
177     $aDescriptors = array(
178                      0 => array('pipe', 'r'),
179                      1 => STDOUT,
180                      2 => STDERR
181                     );
182     $ahPipes = null;
183     $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes, null, $aProcEnv);
184     if (!is_resource($hProcess)) {
185         fail('unable to start pgsql');
186     }
187
188     while (strlen($sScript)) {
189         $iWritten = fwrite($ahPipes[0], $sScript);
190         if ($iWritten <= 0) break;
191         $sScript = substr($sScript, $iWritten);
192     }
193     fclose($ahPipes[0]);
194     $iReturn = proc_close($hProcess);
195     if ($bfatal && $iReturn > 0) {
196         fail("pgsql returned with error code ($iReturn)");
197     }
198 }
199
200
201 function runWithEnv($sCmd, $aEnv)
202 {
203     $aFDs = array(
204              0 => array('pipe', 'r'),
205              1 => STDOUT,
206              2 => STDERR
207             );
208     $aPipes = null;
209     $hProc = @proc_open($sCmd, $aFDs, $aPipes, null, $aEnv);
210     if (!is_resource($hProc)) {
211         fail('unable to run command:' . $sCmd);
212     }
213
214     fclose($aPipes[0]); // no stdin
215
216     $iStat = proc_close($hProc);
217     return $iStat;
218 }