e8800c0e8d1f37aad1fef9912df2f3d0972b4940
[platal.git] / bin / csv2sql.php
1 #!/usr/bin/php5
2 <?php
3 /***************************************************************************
4 * Copyright (C) 2003-2006 Polytechnique.org *
5 * http://opensource.polytechnique.org/ *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., *
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
21 ***************************************************************************/
22
23 // {{{ function showHelp()
24
25 function showHelp($error = null) {
26 if ($error) {
27 echo 'Ligne de commande non-valide : ' . $error, "\n\n";
28 }
29 echo 'csv2sql.php -t table [-i source] [-r phpfile]', "\n\n";
30 echo 'options:', "\n";
31 echo ' -t table: table in which insertion is to be done', "\n";
32 echo ' -i source: CSV source file (stdin if not defined or if source is \'-\'', "\n";
33 echo ' -r phpfile: PHP file which define relations', "\n";
34 }
35
36 // }}}
37 // {{{ function processArgs()
38
39 function processArgs()
40 {
41 global $sourceName, $table, $includedFile;
42 $opts = getopt('i:t:r:d:');
43 if ($opts['i'] == '-' || empty($opts['i'])) {
44 $sourceName = 'php://stdin';
45 } else {
46 $sourceName = $opts['i'];
47 }
48
49 if ($opts['r'] && !empty($opts['r'])) {
50 $includedFile = $opts['r'];
51 }
52
53 if (!$opts['t'] || empty($opts['t'])) {
54 showHelp('Table non définie');
55 exit;
56 }
57 $table = $opts['t'];
58 }
59
60 // }}}
61
62 processArgs();
63 require_once(dirname(__FILE__) . '/../classes/csvimporter.php');
64 require_once(dirname(__FILE__) . '/../classes/xdb.php');
65
66 $source = file_get_contents($sourceName);
67 $insert_relation = null;
68 $update_relation = null;
69 $debug = false;
70 $action = CSV_INSERT;
71 if (isset($includedFile)) {
72 require_once($includedFile);
73 }
74
75 $translater = new CSVImporter($table, $key, !$debug);
76 $translater->setCSV($source);
77 $translater->run($action, $insert_relation, $update_relation);
78
79 ?>