-
Notifications
You must be signed in to change notification settings - Fork 144
/
cli.php
448 lines (387 loc) · 12.5 KB
/
cli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
require_once 'Converter.php';
/**
* CLI Interface.
*
* @category CLI
*
* @author Andrey Hristov <[email protected]>, Ulf Wendel <[email protected]>
* @copyright 1997-2006 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
*
* @version CVS: $Id:$, Release: @package_version@
*
* @link http://www.mysql.com
* @since Available since Release 1.0
*/
/**
* Parse the command line options.
*
* @param int
* @param array
*
* @return array
*/
function parseOptions($argc, $argv)
{
$options = array();
$error = null;
if ($argc < 2) {
$error = 'No options given';
return array($options, $error);
}
reset($argv);
// skip $argv[0] - program name
next($argv);
while ($k = key($argv)) {
$arg = trim(current($argv));
next($argv);
switch ($arg) {
case '-f':
if ($k = key($argv)) {
$arg = trim(current($argv));
next($argv);
if (substr($arg, 0, 1) == '-') {
$error = '-f needs a file name';
break 2;
} elseif (!file_exists($arg)) {
$error = sprintf('"%s" does not exist', $arg);
break 2;
} elseif (!is_file($arg)) {
$error = sprintf('"%s" is not a file', $arg);
break 2;
} elseif (!is_readable($arg)) {
$error = sprintf('"%s" is not reabale', $arg);
break 2;
} else {
$options['files'][$arg] = $arg;
}
} else {
$error = sprintf('-f needs a file name');
break 2;
}
break;
case '-d':
if ($k = key($argv)) {
$arg = trim(current($argv));
next($argv);
if (substr($arg, 0, 1) == '-') {
$error = '-d needs a directory name';
break 2;
} elseif (!file_exists($arg)) {
$error = sprintf('"%s" does not exist', $arg);
break 2;
} elseif (!is_dir($arg)) {
$error = sprintf('"%s" is not a directory', $arg);
break 2;
} elseif (!is_readable($arg)) {
$error = sprintf('"%s" is not readable', $arg);
break 2;
} else {
$options['directories'][$arg] = $arg;
}
} else {
$error = sprintf('-d needs a directory name');
break 2;
}
break;
case '-s':
if ($k = key($argv)) {
$arg = trim(current($argv));
next($argv);
if ('' == $arg) {
$error = '-s expects a code snippet to follow the option';
break 2;
}
$options['snippet'] = $arg;
} else {
$error = '-s expects a code snippet to follow the option';
break 2;
}
break;
case '-h':
case '--help':
$options['help'] = true;
case '-u':
$options['update'] = true;
break;
case '-b':
$options['backup'] = true;
$options['update'] = true;
break;
case '-v':
if (isset($options['quiet'])) {
$error = 'You cannot use -v with -q';
break 2;
}
$options['verbose'] = true;
break;
case '-q':
if (isset($options['verbose'])) {
$error = 'You cannot use -q with -v';
break 2;
}
$options['quiet'] = true;
break;
case '-w':
$options['warnings'] = true;
break;
case '-p':
if ($k = key($argv)) {
$arg = trim(current($argv));
next($argv);
if ('' == $arg) {
$error = '-p needs a search pattern';
break 2;
} else {
$options['pattern'] = $arg;
}
} else {
$error = '-p needs a search pattern';
break 2;
}
break;
default:
$error = sprintf('Invalid option "%s"', $arg);
break;
}
}
return array($options, $error);
} // end func parseOptions
/**
* Prints the help message with usage instructions.
*
* @param array
*/
function printHelp($argv)
{
printf("\n");
printf("Usage of %s :\n\n", $argv[0]);
printf("-f <file> Convert file\n");
printf("-d <directory> Convert directory\n");
printf('-p <pattern> File name pattern for -d, e.g. -p "*.php,*.php3". Default: *');
printf("\n");
printf("-s <code> Convert code snippet\n");
printf("\n");
printf("-u Update (modify) input file during the conversion\n");
printf("-b Backup files to [original_name].org before they get updated\n\n");
printf("-v verbose - print conversion details\n");
printf("-w warnings - print errors/warnings, if any\n");
printf("-q quiet - don't print the generated code\n");
printf("\n\n");
} // end func printHelp
/**
* Returns a status description (OK, Error, Warning) based on the conversion result.
*
* @param array
*
* @return string
*/
function getConversionStatus($conv_result)
{
$status = null;
if (($conv_result['found'] == $conv_result['converted']) && (count($conv_result['errors']) == 0)) {
$status = 'OK';
} elseif (($conv_result['found'] == $conv_result['converted']) && (count($conv_result['errors']) > 0)) {
$status = 'Warning';
} elseif (($conv_result['found'] != $conv_result['converted']) && (count($conv_result['errors']) > 0)) {
$status = 'Error';
}
return $status;
} // end func getConversionStatus
/**
* Prints the overview summary with conversion details.
*
* @param array
* @param string
* @param mixed
*/
function printConversionHeader($conv_result, $status, $file = null)
{
if (!is_null($file)) {
echo "\n";
printSeperator("File $file");
echo "\n";
}
echo "\n";
printSeperator('Summary', '-');
echo "\n";
printf("Status: %s\n", $status);
printf("Number of mysql_-functions found: %d\n", $conv_result['found']);
printf("Number of mysql_-functions converted: %d\n", $conv_result['converted']);
printf("Warnings/Errors: %d\n", count($conv_result['errors']));
printf("Code length: %d Bytes\n", strlen($conv_result['output']));
} // end func printConversionHeader
/**
* Prints the conversion errors.
*
* @param array
*/
function printConversionErrors($conv_result)
{
echo "\n";
echo "\n";
printSeperator('Warnings/Errors', '-');
echo "\n";
foreach ($conv_result['errors'] as $k => $error) {
printSeperator(sprintf('Warning/Error on line %d', $error['line']), ' ');
echo $error['msg'];
echo "\n";
}
} // end func printConversionErrors
/**
* Prints the generated source code.
*
* @param array
*/
function printConversionOutput($conv_result, $verbose, $quiet)
{
if ($verbose) {
echo "\n";
echo "\n";
printSeperator('Generated code', '-');
echo "\n";
}
if (!$quiet) {
print $conv_result['output'];
}
if ($verbose) {
echo "\n";
echo "\n";
printSeperator('End of code', '-');
echo "\n";
}
} // end func printConversionOutput
/**
* Converts a file.
*
* @param string
* @param bool
*/
function convertFile($file, $verbose, $quiet, $update, $backup, $warnings, $seperator = '#')
{
$conv = new MySQLConverterTool_Converter();
$ret = $conv->convertFile($file);
$status = getConversionStatus($ret);
if ($quiet) {
printSeperator(sprintf('[ %-7s ] %s', $status, $file), $seperator);
}
if ($verbose) {
printConversionHeader($ret, $status, $file);
}
if ($backup) {
$ffile = $file.'.org';
if (file_exists($ffile) && !unlink($ffile)) {
printf("Error:\n");
printf("Cannot unlink old backup file '%s'. Check the file permissions.\n\n", $ffile);
return;
}
if (!rename($file, $ffile)) {
printf("Error:\n");
printf("Cannot rename '%s' to %s'. Check the file permissions.\n\n", $file, $ffile);
return;
}
if ($verbose) {
printf("Backup created.\n", $ffile);
}
}
if ($update) {
if (!$fp = fopen($file, 'w')) {
printf("Error:\n");
printf("Cannot modify file '%s'. Check the file permissions.\n\n", $file);
return;
}
fwrite($fp, $ret['output']);
fclose($fp);
if ($verbose) {
printf("File updated/modified.\n", $file);
}
}
if (($verbose || $warnings) && count($ret['errors']) > 0) {
printConversionErrors($ret);
}
printConversionOutput($ret, $verbose, $quiet);
} // end func convertFile
/**
*
*/
function convertSnippet($code, $verbose, $quiet, $warnings, $seperator = '#')
{
$conv = new MySQLConverterTool_Converter();
$ret = $conv->convertString($code);
$status = getConversionStatus($ret);
if ($quiet) {
printSeperator('Snippet', $seperator);
}
if ($verbose) {
printConversionHeader($ret, $status, null);
}
if (($verbose || $warnings) && count($ret['errors']) > 0) {
printConversionErrors($ret);
}
printConversionOutput($ret, $verbose, $quiet);
} // end func convertSnippet
/**
* Helper: prints a line with a title.
*
* @param string
* @param string
*/
function printSeperator($title, $seperator = '#')
{
$len = strlen($title) + 2;
$num = 1;
// $num = max(floor((80 - $len) / 2), 0);
for ($i = 0; $i < $num; ++$i) {
print $seperator;
}
printf(' %s ', $title);
if ($num > 0) {
$num = 80 - $num - $len;
for ($i = 0; $i < $num; ++$i) {
print $seperator;
}
}
echo "\n";
} // end func printSeperator
if (!isset($argc) || !isset($argv)) {
// redirect to the web gui
// or maybe die("This is the command line interface, use php -f cli.php to run it! Open index.php for the web interface."); ?
header('Location: GUI/index.php');
exit(0);
}
list($options, $error) = parseOptions($argc, $argv);
if (!is_null($error) || empty($options) || isset($options['help'])) {
// some sort of trouble or help output requested
if (!is_null($error)) {
printf("\n");
printf("Error: %s\n", $error);
}
printHelp($argv);
} else {
echo "\n";
if (!empty($options['files'])) {
foreach ($options['files'] as $k => $file) {
convertFile($file, isset($options['verbose']), isset($options['quiet']), isset($options['update']), isset($options['backup']), isset($options['warnings']));
}
}
if (!empty($options['directories'])) {
$conv = new MySQLConverterTool_Converter();
foreach ($options['directories'] as $k => $directory) {
$files = $conv->getFilesOfDirectory($directory, (isset($options['pattern'])) ? $options['pattern'] : '');
if (empty($files)) {
printSeperator(sprintf('No files found in "%s"', $directory), '*');
echo "\n";
continue;
}
printSeperator(sprintf('Directory "%s"', $directory), '*');
echo "\n";
foreach ($files as $k => $file) {
convertFile($file, isset($options['verbose']), isset($options['quiet']), isset($options['update']), isset($options['backup']), isset($options['warnings']), ' ');
}
}
}
if (isset($options['snippet'])) {
convertSnippet($options['snippet'], isset($options['verbose']), isset($options['quiet']), isset($options['warnings']));
}
}