]> git.openstreetmap.org Git - nominatim.git/blob - lib/cmd.php
increase weight for country tokens
[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 -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
152     if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
153         $sCMD .= ' -h ' . $aDSNInfo['hostspec'];
154     }
155     if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
156         $sCMD .= ' -U ' . $aDSNInfo['username'];
157     }
158     $aProcEnv = null;
159     if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
160         $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
161     }
162     if (!$bVerbose) {
163         $sCMD .= ' -q';
164     }
165     if ($bfatal && !$bIgnoreErrors) {
166         $sCMD .= ' -v ON_ERROR_STOP=1';
167     }
168     $aDescriptors = array(
169                      0 => array('pipe', 'r'),
170                      1 => STDOUT,
171                      2 => STDERR
172                     );
173     $ahPipes = null;
174     $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes, null, $aProcEnv);
175     if (!is_resource($hProcess)) {
176         fail('unable to start pgsql');
177     }
178
179     if (!$bVerbose) {
180         fwrite($ahPipes[0], 'set client_min_messages to WARNING;');
181     }
182
183     while (strlen($sScript)) {
184         $iWritten = fwrite($ahPipes[0], $sScript);
185         if ($iWritten <= 0) break;
186         $sScript = substr($sScript, $iWritten);
187     }
188     fclose($ahPipes[0]);
189     $iReturn = proc_close($hProcess);
190     if ($bfatal && $iReturn > 0) {
191         fail("pgsql returned with error code ($iReturn)");
192     }
193 }
194
195
196 function runWithEnv($sCmd, $aEnv)
197 {
198     $aFDs = array(
199              0 => array('pipe', 'r'),
200              1 => STDOUT,
201              2 => STDERR
202             );
203     $aPipes = null;
204     $hProc = @proc_open($sCmd, $aFDs, $aPipes, null, $aEnv);
205     if (!is_resource($hProc)) {
206         fail('unable to run command:' . $sCmd);
207     }
208
209     fclose($aPipes[0]); // no stdin
210
211     $iStat = proc_close($hProc);
212     return $iStat;
213 }