[TASK] Move XLIFF handling to t3lib
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_cli.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2006-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains base class for TYPO3 cli scripts
29 *
30 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
31 */
32
33
34 /**
35 * TYPO3 cli script basis
36 *
37 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
38 * @package TYPO3
39 * @subpackage t3lib
40 */
41 class t3lib_cli {
42
43 var $cli_args = array(); // Command line arguments, exploded into key => value-array pairs
44 var $cli_options = array(
45 array('-s', 'Silent operation, will only output errors and important messages.'),
46 array('--silent', 'Same as -s'),
47 array('-ss', 'Super silent, will not even output errors or important messages.'),
48 );
49 var $cli_help = array(
50 'name' => 'CLI base class (overwrite this...)',
51 'synopsis' => '###OPTIONS###',
52 'description' => 'Class with basic functionality for CLI scripts (overwrite this...)',
53 'examples' => 'Give examples...',
54 'options' => '',
55 'license' => 'GNU GPL - free software!',
56 'author' => '[Author name]',
57 );
58 var $stdin = NULL;
59
60
61 /**
62 * Constructor
63 * Make sure child classes also call this!
64 *
65 * @return void
66 */
67 function __construct() {
68 // Loads the cli_args array with command line arguments
69 $this->cli_args = $this->cli_getArgIndex();
70 }
71
72 /**
73 * Finds the arg token (like "-s") in argv and returns the rest of argv from that point on.
74 * This should only be used in special cases since this->cli_args should already be prepared with an index of values!
75 *
76 * @param string Option string, eg. "-s"
77 * @param array Input argv array
78 * @return array Output argv array with all options AFTER the found option.
79 */
80 function cli_getArgArray($option, $argv) {
81 while (count($argv) && strcmp($argv[0], $option)) {
82 array_shift($argv);
83 }
84
85 if (!strcmp($argv[0], $option)) {
86 array_shift($argv);
87 return count($argv) ? $argv : array('');
88 }
89 }
90
91 /**
92 * Return TRUE if option is found
93 *
94 * @param string Option string, eg. "-s"
95 * @return boolean TRUE if option found
96 */
97 function cli_isArg($option) {
98 return isset($this->cli_args[$option]);
99 }
100
101 /**
102 * Return argument value
103 *
104 * @param string Option string, eg. "-s"
105 * @param integer Value index, default is 0 (zero) = the first one...
106 * @return boolean TRUE if option found
107 */
108 function cli_argValue($option, $idx = 0) {
109 return is_array($this->cli_args[$option]) ? $this->cli_args[$option][$idx] : '';
110 }
111
112 /**
113 * Will parse "_SERVER[argv]" into an index of options and values
114 * Argument names (eg. "-s") will be keys and values after (eg. "-s value1 value2 ..." or "-s=value1") will be in the array.
115 * Array is empty if no values
116 *
117 * @return array
118 */
119 function cli_getArgIndex() {
120 $cli_options = array();
121 $index = '_DEFAULT';
122 foreach ($_SERVER['argv'] as $token) {
123 if ($token{0} === '-' && !t3lib_div::testInt($token{1})) { // Options starting with a number is invalid - they could be negative values... !
124 list($index, $opt) = explode('=', $token, 2);
125 if (isset($cli_options[$index])) {
126 echo 'ERROR: Option ' . $index . ' was used twice!' . LF;
127 exit;
128 }
129 $cli_options[$index] = array();
130 if (isset($opt)) {
131 $cli_options[$index][] = $opt;
132 }
133 } else {
134 $cli_options[$index][] = $token;
135 }
136 }
137
138 return $cli_options;
139 }
140
141 /**
142 * Validates if the input arguments in this->cli_args are all listed in this->cli_options and if not, will exit with an error.
143 */
144 function cli_validateArgs() {
145 $cli_args_copy = $this->cli_args;
146 unset($cli_args_copy['_DEFAULT']);
147 $allOptions = array();
148
149 foreach ($this->cli_options as $cfg) {
150 $allOptions[] = $cfg[0];
151 $argSplit = t3lib_div::trimExplode(' ', $cfg[0], 1);
152 if (isset($cli_args_copy[$argSplit[0]])) {
153
154 foreach ($argSplit as $i => $v) {
155 $ii = $i;
156 if ($i > 0) {
157 if (!isset($cli_args_copy[$argSplit[0]][$i - 1]) && $v{0} != '[') { // Using "[]" around a paramter makes it optional
158 echo 'ERROR: Option "' . $argSplit[0] . '" requires a value ("' . $v . '") on position ' . $i . LF;
159 exit;
160 }
161 }
162 }
163
164 $ii++;
165 if (isset($cli_args_copy[$argSplit[0]][$ii - 1])) {
166 echo 'ERROR: Option "' . $argSplit[0] . '" does not support a value on position ' . $ii . LF;
167 exit;
168 }
169
170 unset($cli_args_copy[$argSplit[0]]);
171 }
172 }
173
174 if (count($cli_args_copy)) {
175 echo wordwrap('ERROR: Option ' . implode(',', array_keys($cli_args_copy)) . ' was unknown to this script!' . LF . '(Options are: ' . implode(', ', $allOptions) . ')' . LF);
176 exit;
177 }
178 }
179
180 /**
181 * Asks stdin for keyboard input and returns the line (after enter is pressed)
182 *
183 * @return string
184 */
185 function cli_keyboardInput() {
186
187 // Have to open the stdin stream only ONCE! otherwise I cannot read multiple lines from it... :
188 if (!$this->stdin) {
189 $this->stdin = fopen('php://stdin', 'r');
190 }
191
192 while (FALSE == ($line = fgets($this->stdin, 1000))) {
193 }
194
195 return trim($line);
196 }
197
198 /**
199 * Asks for Yes/No from shell and returns TRUE if "y" or "yes" is found as input.
200 *
201 * @param string String to ask before...
202 * @return boolean TRUE if "y" or "yes" is the input (case insensitive)
203 */
204 function cli_keyboardInput_yes($msg = '') {
205 echo $msg . ' (Yes/No + return): '; // ONLY makes sense to echo it out since we are awaiting keyboard input - that cannot be silenced...
206
207 return t3lib_div::inList('y,yes', strtolower($this->cli_keyboardInput()));
208 }
209
210 /**
211 * Echos strings to shell, but respective silent-modes
212 *
213 * @param string The string
214 * @param boolean If string should be written even if -s is set (-ss will subdue it!)
215 * @return boolean Returns TRUE if string was outputted.
216 */
217 function cli_echo($string = '', $force = FALSE) {
218 if (isset($this->cli_args['-ss'])) {
219 // Nothing to do...
220 } elseif (isset($this->cli_args['-s']) || isset($this->cli_args['--silent'])) {
221 if ($force) {
222 echo $string;
223 return TRUE;
224 }
225 } else {
226 echo $string;
227 return TRUE;
228 }
229
230 return FALSE;
231 }
232
233 /**
234 * Prints help-output from ->cli_help array
235 *
236 * @return void
237 */
238 function cli_help() {
239 foreach ($this->cli_help as $key => $value) {
240 $this->cli_echo(strtoupper($key) . ":\n");
241 switch ($key) {
242 case 'synopsis':
243 $optStr = '';
244 foreach ($this->cli_options as $v) {
245 $optStr .= ' [' . $v[0] . ']';
246 }
247 $this->cli_echo($this->cli_indent(str_replace('###OPTIONS###', trim($optStr), $value), 4) . "\n\n");
248 break;
249 case 'options':
250 $this->cli_echo($this->cli_indent($value, 4) . LF);
251
252 $maxLen = 0;
253 foreach ($this->cli_options as $v) {
254 if (strlen($v[0]) > $maxLen) {
255 $maxLen = strlen($v[0]);
256 }
257 }
258
259 foreach ($this->cli_options as $v) {
260 $this->cli_echo($v[0] . substr($this->cli_indent(rtrim($v[1] . LF . $v[2]), $maxLen + 4), strlen($v[0])) . LF);
261 }
262 $this->cli_echo(LF);
263 break;
264 default:
265 $this->cli_echo($this->cli_indent($value, 4) . "\n\n");
266 break;
267 }
268 }
269 }
270
271 /**
272 * Indentation function for 75 char wide lines.
273 *
274 * @param string String to break and indent.
275 * @param integer Number of space chars to indent.
276 * @return string Result
277 */
278 function cli_indent($str, $indent) {
279 $lines = explode(LF, wordwrap($str, 75 - $indent));
280 $indentStr = str_pad('', $indent, ' ');
281 foreach ($lines as $k => $v) {
282 $lines[$k] = $indentStr . $lines[$k];
283 }
284
285 return implode(LF, $lines);
286 }
287 }
288
289 ?>