Moving to GitHub.
[platal.git] / bin / csv2sql.php
CommitLineData
31874f6f 1#!/usr/bin/php5
2<?php
212aae73 3/***************************************************************************
c441aabe 4 * Copyright (C) 2003-2014 Polytechnique.org *
212aae73 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 ***************************************************************************/
31874f6f 22
50e2ba89 23require_once dirname(__FILE__) . '/../core/classes/csvimporter.php';
440b63df 24
31874f6f 25// {{{ function showHelp()
26
440b63df 27function showHelp($error = null)
28{
31874f6f 29 if ($error) {
30 echo 'Ligne de commande non-valide : ' . $error, "\n\n";
31 }
440b63df 32 echo 'csv2sql.php -t table [-du] [-k keys] [-i source] [-r phpfile]', "\n\n";
31874f6f 33 echo 'options:', "\n";
34 echo ' -t table: table in which insertion is to be done', "\n";
440b63df 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";
31874f6f 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";
41}
42
43// }}}
44// {{{ function processArgs()
45
46function processArgs()
47{
440b63df 48 global $sourceName, $table, $includedFile, $debug, $action, $keys;
49 $opts = getopt('oui:t:r:dk:');
31874f6f 50 if ($opts['i'] == '-' || empty($opts['i'])) {
51 $sourceName = 'php://stdin';
52 } else {
53 $sourceName = $opts['i'];
54 }
55
440b63df 56 if ($opts['k'] && !empty($opts['k'])) {
57 $keys = $opts['k'];
58 }
59
60 if (isset($opts['u'])) {
61 $action = CSV_UPDATE ;
62 } elseif (isset($opts['o'])) {
63 $action = CSV_UPDATEONLY;
64 }
65
66 if (isset($opts['d'])) {
67 $debug = true;
68 }
69
31874f6f 70 if ($opts['r'] && !empty($opts['r'])) {
71 $includedFile = $opts['r'];
72 }
73
74 if (!$opts['t'] || empty($opts['t'])) {
a7de4ef7 75 showHelp('Table non définie');
31874f6f 76 exit;
77 }
78 $table = $opts['t'];
79}
80
81// }}}
82
440b63df 83global $debug, $action, $keys;
eaf30d86
PH
84$debug = false;
85$action = CSV_INSERT;
86$keys = 'id';
440b63df 87
31874f6f 88processArgs();
50e2ba89 89require_once dirname(__FILE__) . '/../core/classes/xdb.php';
31874f6f 90
440b63df 91$source = file_get_contents($sourceName);
31874f6f 92$insert_relation = null;
93$update_relation = null;
31874f6f 94if (isset($includedFile)) {
50e2ba89 95 require_once $includedFile;
31874f6f 96}
97
440b63df 98$translater = new CSVImporter($table, $keys, !$debug);
31874f6f 99$translater->setCSV($source);
100$translater->run($action, $insert_relation, $update_relation);
101
448c8cdc 102// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
31874f6f 103?>