]> git.openstreetmap.org Git - nominatim.git/blob - lib/cmd.php
Merge pull request #1758 from krahulreddy/advanced-installations
[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 info($sMsg)
124 {
125     echo date('Y-m-d H:i:s == ').$sMsg."\n";
126 }
127
128 $aWarnings = array();
129
130
131 function warn($sMsg)
132 {
133     $GLOBALS['aWarnings'][] = $sMsg;
134     echo date('Y-m-d H:i:s == ').'WARNING: '.$sMsg."\n";
135 }
136
137
138 function repeatWarnings()
139 {
140     foreach ($GLOBALS['aWarnings'] as $sMsg) {
141         echo '  * ',$sMsg."\n";
142     }
143 }
144
145
146 function runSQLScript($sScript, $bfatal = true, $bVerbose = false, $bIgnoreErrors = false)
147 {
148     // Convert database DSN to psql parameters
149     $aDSNInfo = \Nominatim\DB::parseDSN(CONST_Database_DSN);
150     if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
151     $sCMD = 'psql'
152         .' -p '.escapeshellarg($aDSNInfo['port'])
153         .' -d '.escapeshellarg($aDSNInfo['database']);
154     if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
155         $sCMD .= ' -h ' . escapeshellarg($aDSNInfo['hostspec']);
156     }
157     if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
158         $sCMD .= ' -U ' . escapeshellarg($aDSNInfo['username']);
159     }
160     $aProcEnv = null;
161     if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
162         $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
163     }
164     if (!$bVerbose) {
165         $sCMD .= ' -q';
166     }
167     if ($bfatal && !$bIgnoreErrors) {
168         $sCMD .= ' -v ON_ERROR_STOP=1';
169     }
170     $aDescriptors = array(
171                      0 => array('pipe', 'r'),
172                      1 => STDOUT,
173                      2 => STDERR
174                     );
175     $ahPipes = null;
176     $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes, null, $aProcEnv);
177     if (!is_resource($hProcess)) {
178         fail('unable to start pgsql');
179     }
180
181     if (!$bVerbose) {
182         fwrite($ahPipes[0], 'set client_min_messages to WARNING;');
183     }
184
185     while (strlen($sScript)) {
186         $iWritten = fwrite($ahPipes[0], $sScript);
187         if ($iWritten <= 0) break;
188         $sScript = substr($sScript, $iWritten);
189     }
190     fclose($ahPipes[0]);
191     $iReturn = proc_close($hProcess);
192     if ($bfatal && $iReturn > 0) {
193         fail("pgsql returned with error code ($iReturn)");
194     }
195 }
196
197
198 function runWithEnv($sCmd, $aEnv)
199 {
200     $aFDs = array(
201              0 => array('pipe', 'r'),
202              1 => STDOUT,
203              2 => STDERR
204             );
205     $aPipes = null;
206     $hProc = @proc_open($sCmd, $aFDs, $aPipes, null, $aEnv);
207     if (!is_resource($hProc)) {
208         fail('unable to run command:' . $sCmd);
209     }
210
211     fclose($aPipes[0]); // no stdin
212
213     $iStat = proc_close($hProc);
214     return $iStat;
215 }