Add account consultation statistics to weekly crons.
[platal.git] / bin / csv2sql.php
... / ...
CommitLineData
1#!/usr/bin/php5
2<?php
3/***************************************************************************
4 * Copyright (C) 2003-2011 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
23require_once dirname(__FILE__) . '/../core/classes/csvimporter.php';
24
25// {{{ function showHelp()
26
27function showHelp($error = null)
28{
29 if ($error) {
30 echo 'Ligne de commande non-valide : ' . $error, "\n\n";
31 }
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";
41}
42
43// }}}
44// {{{ function processArgs()
45
46function processArgs()
47{
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';
52 } else {
53 $sourceName = $opts['i'];
54 }
55
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
70 if ($opts['r'] && !empty($opts['r'])) {
71 $includedFile = $opts['r'];
72 }
73
74 if (!$opts['t'] || empty($opts['t'])) {
75 showHelp('Table non définie');
76 exit;
77 }
78 $table = $opts['t'];
79}
80
81// }}}
82
83global $debug, $action, $keys;
84$debug = false;
85$action = CSV_INSERT;
86$keys = 'id';
87
88processArgs();
89require_once dirname(__FILE__) . '/../core/classes/xdb.php';
90
91$source = file_get_contents($sourceName);
92$insert_relation = null;
93$update_relation = null;
94if (isset($includedFile)) {
95 require_once $includedFile;
96}
97
98$translater = new CSVImporter($table, $keys, !$debug);
99$translater->setCSV($source);
100$translater->run($action, $insert_relation, $update_relation);
101
102// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
103?>