3 /***************************************************************************
4 * Copyright (C) 2003-2008 Polytechnique.org *
5 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
21 ***************************************************************************/
23 require_once(dirname(__FILE__
) . '/../core/classes/csvimporter.php');
25 // {{{ function showHelp()
27 function showHelp($error = null
)
30 echo 'Ligne de commande non-valide : ' . $error, "\n\n";
32 echo 'csv2sql.php -t table [-du] [-k keys] [-i source] [-r phpfile]', "\n\n";
33 echo 'options:', "\n";
34 echo ' -t table: table in which insertion is to be done', "\n";
35 echo ' -d: switch to debug mode', "\n";
36 echo ' -u: switch to update mode', "\n";
37 echo ' -o: switch to update-only mode', "\n";
38 echo ' -k keys: comma-separated list of keys', "\n";
39 echo ' -i source: CSV source file (stdin if not defined or if source is \'-\'', "\n";
40 echo ' -r phpfile: PHP file which define relations', "\n";
44 // {{{ function processArgs()
46 function processArgs()
48 global $sourceName, $table, $includedFile, $debug, $action, $keys;
49 $opts = getopt('oui:t:r:dk:');
50 if ($opts['i'] == '-' ||
empty($opts['i'])) {
51 $sourceName = 'php://stdin';
53 $sourceName = $opts['i'];
56 if ($opts['k'] && !empty($opts['k'])) {
60 if (isset($opts['u'])) {
61 $action = CSV_UPDATE
;
62 } elseif (isset($opts['o'])) {
63 $action = CSV_UPDATEONLY
;
66 if (isset($opts['d'])) {
70 if ($opts['r'] && !empty($opts['r'])) {
71 $includedFile = $opts['r'];
74 if (!$opts['t'] ||
empty($opts['t'])) {
75 showHelp('Table non définie');
83 global $debug, $action, $keys;
89 require_once(dirname(__FILE__
) . '/../core/classes/xdb.php');
91 $source = file_get_contents($sourceName);
92 $insert_relation = null
;
93 $update_relation = null
;
94 if (isset($includedFile)) {
95 require_once($includedFile);
98 $translater = new CSVImporter($table, $keys, !$debug);
99 $translater->setCSV($source);
100 $translater->run($action, $insert_relation, $update_relation);
102 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: