31874f6f |
1 | #!/usr/bin/php5 |
2 | <?php |
3 | |
4 | // {{{ function showHelp() |
5 | |
6 | function showHelp($error = null) { |
7 | if ($error) { |
8 | echo 'Ligne de commande non-valide : ' . $error, "\n\n"; |
9 | } |
10 | echo 'csv2sql.php -t table [-i source] [-r phpfile]', "\n\n"; |
11 | echo 'options:', "\n"; |
12 | echo ' -t table: table in which insertion is to be done', "\n"; |
13 | echo ' -i source: CSV source file (stdin if not defined or if source is \'-\'', "\n"; |
14 | echo ' -r phpfile: PHP file which define relations', "\n"; |
15 | } |
16 | |
17 | // }}} |
18 | // {{{ function processArgs() |
19 | |
20 | function processArgs() |
21 | { |
22 | global $sourceName, $table, $includedFile; |
23 | $opts = getopt('i:t:r:d:'); |
24 | if ($opts['i'] == '-' || empty($opts['i'])) { |
25 | $sourceName = 'php://stdin'; |
26 | } else { |
27 | $sourceName = $opts['i']; |
28 | } |
29 | |
30 | if ($opts['r'] && !empty($opts['r'])) { |
31 | $includedFile = $opts['r']; |
32 | } |
33 | |
34 | if (!$opts['t'] || empty($opts['t'])) { |
35 | showHelp('Table non définie'); |
36 | exit; |
37 | } |
38 | $table = $opts['t']; |
39 | } |
40 | |
41 | // }}} |
42 | |
43 | processArgs(); |
44 | require_once(dirname(__FILE__) . '/../classes/csvimporter.php'); |
45 | require_once(dirname(__FILE__) . '/../classes/xdb.php'); |
46 | |
47 | $source = file_get_contents($sourceName); |
48 | $insert_relation = null; |
49 | $update_relation = null; |
50 | $debug = false; |
51 | $action = CSV_INSERT; |
52 | if (isset($includedFile)) { |
53 | require_once($includedFile); |
54 | } |
55 | |
56 | $translater = new CSVImporter($table, $key, !$debug); |
57 | $translater->setCSV($source); |
58 | $translater->run($action, $insert_relation, $update_relation); |
59 | |
60 | ?> |