]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/cmd.php
Merge pull request #2427 from lonvia/remove-us-states-special-casing
[nominatim.git] / lib-php / cmd.php
1 <?php
2
3 require_once(CONST_LibDir.'/Shell.php');
4
5 function getCmdOpt($aArg, $aSpec, &$aResult, $bExitOnError = false, $bExitOnUnknown = false)
6 {
7     $aQuick = array();
8     $aCounts = array();
9
10     foreach ($aSpec as $aLine) {
11         if (is_array($aLine)) {
12             if ($aLine[0]) {
13                 $aQuick['--'.$aLine[0]] = $aLine;
14             }
15             if ($aLine[1]) {
16                 $aQuick['-'.$aLine[1]] = $aLine;
17             }
18             $aCounts[$aLine[0]] = 0;
19         }
20     }
21
22     $aResult = array();
23     $bUnknown = false;
24     $iSize = count($aArg);
25     for ($i = 1; $i < $iSize; $i++) {
26         if (isset($aQuick[$aArg[$i]])) {
27             $aLine = $aQuick[$aArg[$i]];
28             $aCounts[$aLine[0]]++;
29             $xVal = null;
30             if ($aLine[4] == $aLine[5]) {
31                 if ($aLine[4]) {
32                     $xVal = array();
33                     for ($n = $aLine[4]; $i < $iSize && $n; $n--) {
34                         $i++;
35                         if ($i >= $iSize || $aArg[$i][0] == '-') {
36                             showUsage($aSpec, $bExitOnError, 'Parameter of  \''.$aLine[0].'\' is missing');
37                         }
38
39                         switch ($aLine[6]) {
40                             case 'realpath':
41                                 $xVal[] = realpath($aArg[$i]);
42                                 break;
43                             case 'realdir':
44                                 $sPath = realpath(dirname($aArg[$i]));
45                                 if ($sPath) {
46                                     $xVal[] = $sPath . '/' . basename($aArg[$i]);
47                                 } else {
48                                     $xVal[] = $sPath;
49                                 }
50                                 break;
51                             case 'bool':
52                                 $xVal[] = (bool)$aArg[$i];
53                                 break;
54                             case 'int':
55                                 $xVal[] = (int)$aArg[$i];
56                                 break;
57                             case 'float':
58                                 $xVal[] = (float)$aArg[$i];
59                                 break;
60                             default:
61                                 $xVal[] = $aArg[$i];
62                                 break;
63                         }
64                     }
65                     if ($aLine[4] == 1) {
66                         $xVal = $xVal[0];
67                     }
68                 } else {
69                     $xVal = true;
70                 }
71             } else {
72                 fail('Variable numbers of params not yet supported');
73             }
74
75             if ($aLine[3] > 1) {
76                 if (!array_key_exists($aLine[0], $aResult)) {
77                     $aResult[$aLine[0]] = array();
78                 }
79                 $aResult[$aLine[0]][] = $xVal;
80             } else {
81                 $aResult[$aLine[0]] = $xVal;
82             }
83         } else {
84             $bUnknown = $aArg[$i];
85         }
86     }
87
88     if (array_key_exists('help', $aResult)) {
89         showUsage($aSpec);
90     }
91     if ($bUnknown && $bExitOnUnknown) {
92         showUsage($aSpec, $bExitOnError, 'Unknown option \''.$bUnknown.'\'');
93     }
94
95     foreach ($aSpec as $aLine) {
96         if (is_array($aLine)) {
97             if ($aCounts[$aLine[0]] < $aLine[2]) {
98                 showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is missing');
99             }
100             if ($aCounts[$aLine[0]] > $aLine[3]) {
101                 showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is pressent too many times');
102             }
103             if ($aLine[6] == 'bool' && !array_key_exists($aLine[0], $aResult)) {
104                 $aResult[$aLine[0]] = false;
105             }
106         }
107     }
108     return $bUnknown;
109 }
110
111 function showUsage($aSpec, $bExit = false, $sError = false)
112 {
113     if ($sError) {
114         echo basename($_SERVER['argv'][0]).': '.$sError."\n";
115         echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
116         exit;
117     }
118     echo 'Usage: '.basename($_SERVER['argv'][0])."\n";
119     $bFirst = true;
120     foreach ($aSpec as $aLine) {
121         if (is_array($aLine)) {
122             if ($bFirst) {
123                 $bFirst = false;
124                 echo "\n";
125             }
126             $aNames = array();
127             if ($aLine[1]) {
128                 $aNames[] = '-'.$aLine[1];
129             }
130             if ($aLine[0]) {
131                 $aNames[] = '--'.$aLine[0];
132             }
133             $sName = join(', ', $aNames);
134             echo '  '.$sName.str_repeat(' ', 30-strlen($sName)).$aLine[7]."\n";
135         } else {
136             echo $aLine."\n";
137         }
138     }
139     echo "\n";
140     exit;
141 }
142
143 function info($sMsg)
144 {
145     echo date('Y-m-d H:i:s == ').$sMsg."\n";
146 }
147
148 $aWarnings = array();
149
150
151 function warn($sMsg)
152 {
153     $GLOBALS['aWarnings'][] = $sMsg;
154     echo date('Y-m-d H:i:s == ').'WARNING: '.$sMsg."\n";
155 }
156
157
158 function repeatWarnings()
159 {
160     foreach ($GLOBALS['aWarnings'] as $sMsg) {
161         echo '  * ',$sMsg."\n";
162     }
163 }
164
165
166 function setupHTTPProxy()
167 {
168     if (!getSettingBool('HTTP_PROXY')) {
169         return;
170     }
171
172     $sProxy = 'tcp://'.getSetting('HTTP_PROXY_HOST').':'.getSetting('HTTP_PROXY_PROT');
173     $aHeaders = array();
174
175     $sLogin = getSetting('HTTP_PROXY_LOGIN');
176     $sPassword = getSetting('HTTP_PROXY_PASSWORD');
177
178     if ($sLogin && $sPassword) {
179         $sAuth = base64_encode($sLogin.':'.$sPassword);
180         $aHeaders = array('Proxy-Authorization: Basic '.$sAuth);
181     }
182
183     $aProxyHeader = array(
184                      'proxy' => $sProxy,
185                      'request_fulluri' => true,
186                      'header' => $aHeaders
187                     );
188
189     $aContext = array('http' => $aProxyHeader, 'https' => $aProxyHeader);
190     stream_context_set_default($aContext);
191 }