From 440b63df721250ec0de31687af5cb7642a2a100e Mon Sep 17 00:00:00 2001 From: x2003bruneau Date: Sat, 5 May 2007 09:56:22 +0000 Subject: [PATCH] Improvements in CSV importer bin/csv2sql.php | 41 ++++++++++++++++++++++++++++++++--------- classes/csvimporter.php | 20 +++++++++++++------- include/banana/hooks.inc.php | 5 ++--- modules/admin.php | 2 +- 4 files changed, 48 insertions(+), 20 deletions(-) git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1800 839d8a87-29fc-0310-9880-83ba4fa771e5 --- bin/csv2sql.php | 41 ++++++++++++++++++++++++++++++++--------- classes/csvimporter.php | 20 +++++++++++++------- include/banana/hooks.inc.php | 5 ++--- modules/admin.php | 2 +- 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/bin/csv2sql.php b/bin/csv2sql.php index ef7ad30..20963d6 100755 --- a/bin/csv2sql.php +++ b/bin/csv2sql.php @@ -20,15 +20,22 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ***************************************************************************/ +require_once(dirname(__FILE__) . '/../classes/csvimporter.php'); + // {{{ function showHelp() -function showHelp($error = null) { +function showHelp($error = null) +{ if ($error) { echo 'Ligne de commande non-valide : ' . $error, "\n\n"; } - echo 'csv2sql.php -t table [-i source] [-r phpfile]', "\n\n"; + echo 'csv2sql.php -t table [-du] [-k keys] [-i source] [-r phpfile]', "\n\n"; echo 'options:', "\n"; echo ' -t table: table in which insertion is to be done', "\n"; + echo ' -d: switch to debug mode', "\n"; + echo ' -u: switch to update mode', "\n"; + echo ' -o: switch to update-only mode', "\n"; + echo ' -k keys: comma-separated list of keys', "\n"; echo ' -i source: CSV source file (stdin if not defined or if source is \'-\'', "\n"; echo ' -r phpfile: PHP file which define relations', "\n"; } @@ -38,14 +45,28 @@ function showHelp($error = null) { function processArgs() { - global $sourceName, $table, $includedFile; - $opts = getopt('i:t:r:d:'); + global $sourceName, $table, $includedFile, $debug, $action, $keys; + $opts = getopt('oui:t:r:dk:'); if ($opts['i'] == '-' || empty($opts['i'])) { $sourceName = 'php://stdin'; } else { $sourceName = $opts['i']; } + if ($opts['k'] && !empty($opts['k'])) { + $keys = $opts['k']; + } + + if (isset($opts['u'])) { + $action = CSV_UPDATE ; + } elseif (isset($opts['o'])) { + $action = CSV_UPDATEONLY; + } + + if (isset($opts['d'])) { + $debug = true; + } + if ($opts['r'] && !empty($opts['r'])) { $includedFile = $opts['r']; } @@ -59,20 +80,22 @@ function processArgs() // }}} +global $debug, $action, $keys; +$debug = false; +$action = CSV_INSERT; +$keys = 'id'; + processArgs(); -require_once(dirname(__FILE__) . '/../classes/csvimporter.php'); require_once(dirname(__FILE__) . '/../classes/xdb.php'); -$source = file_get_contents($sourceName); +$source = file_get_contents($sourceName); $insert_relation = null; $update_relation = null; -$debug = false; -$action = CSV_INSERT; if (isset($includedFile)) { require_once($includedFile); } -$translater = new CSVImporter($table, $key, !$debug); +$translater = new CSVImporter($table, $keys, !$debug); $translater->setCSV($source); $translater->run($action, $insert_relation, $update_relation); diff --git a/classes/csvimporter.php b/classes/csvimporter.php index 2b76a50..6bb92ee 100644 --- a/classes/csvimporter.php +++ b/classes/csvimporter.php @@ -19,9 +19,10 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ***************************************************************************/ -define('CSV_INSERT', 'insert'); // INSERT IGNORE -define('CSV_REPLACE', 'replace'); // REPLACE -define('CSV_UPDATE', 'update'); // INSERT and UPDATE on error +define('CSV_INSERT', 'insert'); // INSERT IGNORE +define('CSV_REPLACE', 'replace'); // REPLACE +define('CSV_UPDATE', 'update'); // INSERT and UPDATE on error +define('CSV_UPDATEONLY', 'updateonly'); // UPDATE class CSVImporter { @@ -39,7 +40,7 @@ class CSVImporter public function __construct($table, $key = 'id', $do_sql = true) { $this->table = $table; - $this->key = $key; + $this->key = explode(',', $key); $this->do_sql = $do_sql; } @@ -159,11 +160,16 @@ class CSVImporter case CSV_REPLACE: $this->execute("REPLACE INTO {$this->table} SET $set"); break; - case CSV_UPDATE: - if (!$this->execute("INSERT INTO {$this->table} SET $set")) { + case CSV_UPDATE: case CSV_UPDATEONLY: + if ($action == CSV_UPDATEONLY || !$this->execute("INSERT INTO {$this->table} SET $set")) { $ops = $this->makeRequestArgs($line, $update_relation); $set = join(', ', $ops); - $this->execute("UPDATE {$this->table} SET $set WHERE {$ops[$this->key]}"); + $where = array(); + foreach ($this->key as $key) { + $where[] = $ops[$key]; + } + $where = join(' AND ', $where); + $this->execute("UPDATE {$this->table} SET $set WHERE $where"); } break; } diff --git a/include/banana/hooks.inc.php b/include/banana/hooks.inc.php index 7aab3df..9271fb4 100644 --- a/include/banana/hooks.inc.php +++ b/include/banana/hooks.inc.php @@ -154,13 +154,12 @@ function hook_makeLink($params) } $base = $globals->baseurl . '/' . $platal->ns . 'forum'; } else if (Banana::$protocole->name() == 'MLArchives') { - $base = $globals->baseurl . '/' . $platal->ns . 'lists/archives'; if ($feed) { - return $base . hook_platalRSS(MLBanana::$listname); + return $globals->baseurl . '/' . $platal->ns . hook_platalRSS(MLBanana::$listname); } elseif (php_sapi_name() == 'cli') { $base = "http://listes.polytechnique.org/archives/" . str_replace('@', '_', $params['group']); } else { - $base .= '/' . MLBanana::$listname; + $base = $globals->baseurl . '/' . $platal->ns . 'lists/archives/' . MLBanana::$listname; } } $base = $base . hook_platalMessageLink($params); diff --git a/modules/admin.php b/modules/admin.php index 7b3483a..22e6190 100644 --- a/modules/admin.php +++ b/modules/admin.php @@ -616,7 +616,7 @@ class AdminModule extends PLModule $importer->registerFunction('matricule', 'matricle Ecole vers X.org', array($this, 'getMatricule')); switch ($action) { case 'add': - $fields = array('nom', 'nom_ini', 'prenom', + $fields = array('nom', 'nom_ini', 'prenom', 'naissance_ini', 'prenom_ini', 'promo', 'promo_sortie', 'flags', 'matricule', 'matricule_ax', 'perms'); $importer->forceValue('promo', $promo); -- 2.1.4