From 52d663ef5bfc203b7966b26b421dd0980b99adb8 Mon Sep 17 00:00:00 2001
From: "Committo,Ergo:sum" <esj@rezo.net>
Date: Wed, 15 Aug 2007 18:59:42 +0000
Subject: [PATCH] =?UTF-8?q?#209:=20adaptation=20des=20fonctions=20de=20fus?=
 =?UTF-8?q?ions=20de=20base=20de=20donn=C3=A9es=20pour=20PG.=20L'abstracti?=
 =?UTF-8?q?on=20de=20l'insertion=20dans=20'''sql=5Finsert'''=20n'a=20final?=
 =?UTF-8?q?ement=20pas=20besoin=20de=20l'argument=20Ignore=20puisque=20PG?=
 =?UTF-8?q?=20n'a=20pas=20cette=20option,=20ce=20param=C3=A8tre=20inutilis?=
 =?UTF-8?q?=C3=A9=20est=20reconverti=20comme=20recevant=20la=20description?=
 =?UTF-8?q?=20de=20la=20table=20(pas=20utilis=C3=A9=20dans=20le=20code=20p?=
 =?UTF-8?q?our=20le=20moment).=20Ca=20ne=20marche=20pas=20encore=20en=20PG?=
 =?UTF-8?q?=20car=20le=20'''RETURNING'''=20pour=20obtenir=20le=20num=C3=A9?=
 =?UTF-8?q?ro=20de=20la=20derni=C3=A8re=20n'accepte=20pas=20qu'on=20lui=20?=
 =?UTF-8?q?donne=20un=20cl=C3=A9=20primaire=20multiple,=20il=20faut=20prob?=
 =?UTF-8?q?ablement=20d=C3=A9clarer=20explicitement=20une=20''sequence''?=
 =?UTF-8?q?=20PG=20ici.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Incidemment MySQL5 et PG refusent une syntaxe que MySQL4 acceptait, c'est réparé. Et le script est un peu plus blindé en amont en refusant d'insérer les tables pour lesquelles les règles de fusion ne sont pas encore définies.
---
 ecrire/base/abstract_sql.php |  6 ++--
 ecrire/base/db_mysql.php     |  4 +--
 ecrire/base/db_pg.php        | 28 ++++++++++------
 ecrire/inc/import_insere.php | 65 ++++++++++++++++++++----------------
 4 files changed, 58 insertions(+), 45 deletions(-)

diff --git a/ecrire/base/abstract_sql.php b/ecrire/base/abstract_sql.php
index 217b20a4ca..8b5cec0c01 100644
--- a/ecrire/base/abstract_sql.php
+++ b/ecrire/base/abstract_sql.php
@@ -103,10 +103,10 @@ function sql_free($res, $serveur='')
 }
 
 // http://doc.spip.org/@sql_insert
-function sql_insert($table, $noms, $valeurs, $serveur='')
+function sql_insert($table, $noms, $valeurs, $desc=array(), $serveur='')
 {
 	$f = sql_serveur('insert', $serveur);
-	return $f($table, $noms, $valeurs);
+	return $f($table, $noms, $valeurs, $desc);
 }
 
 // http://doc.spip.org/@sql_update
@@ -157,7 +157,7 @@ function sql_multi($sel, $lang, $serveur='')
 }
 
 // http://doc.spip.org/@sql_error
-function sql_error($query, $serveur='') {
+function sql_error($query='requete inconnue', $serveur='') {
   	$f = sql_serveur('error', $serveur);
 	return $f($query);
 }
diff --git a/ecrire/base/db_mysql.php b/ecrire/base/db_mysql.php
index cc35bf3870..5efc983c76 100644
--- a/ecrire/base/db_mysql.php
+++ b/ecrire/base/db_mysql.php
@@ -352,8 +352,8 @@ function spip_mysql_free($r) {
 }
 
 // http://doc.spip.org/@spip_mysql_insert
-function spip_mysql_insert($table, $champs, $valeurs, $ignore='') {
-	if (!spip_mysql_query("INSERT $ignore INTO $table $champs VALUES $valeurs"))
+function spip_mysql_insert($table, $champs, $valeurs, $desc='') {
+	if (!spip_mysql_query("INSERT INTO $table $champs VALUES $valeurs"))
 		return 0;
 	$r = mysql_insert_id();
 	return $r ? $r : (($r===0) ? -1 : 0);
diff --git a/ecrire/base/db_pg.php b/ecrire/base/db_pg.php
index d8e51cfe86..6bfbbb74e6 100644
--- a/ecrire/base/db_pg.php
+++ b/ecrire/base/db_pg.php
@@ -297,25 +297,31 @@ function spip_pg_free($res, $serveur='') {
 }
 
 // http://doc.spip.org/@spip_pg_insert
-function spip_pg_insert($table, $champs, $valeurs, $ignore='') {
-	global $tables_principales;
+function spip_pg_insert($table, $champs, $valeurs, $desc=array()) {
+
 	global $spip_pg_link, $table_prefix;
-	include_spip('base/serial');
-	if (isset($tables_principales[$table]['key']["PRIMARY KEY"]))
-		$ret = " RETURNING "
-		. $tables_principales[$table]['key']["PRIMARY KEY"];
+	if (!$desc) {
+		global $tables_principales;
+		include_spip('base/serial');
+		$desc = @$tables_principales[$table];
+	}
+	if (isset($desc['key']["PRIMARY KEY"]))
+		$ret = " RETURNING " . $desc['key']["PRIMARY KEY"];
 	else $ret = '';
 
 	if ($GLOBALS['table_prefix'])
 		$table = preg_replace('/^spip/',
 				    $GLOBALS['table_prefix'],
 				    $table);
-	$r = pg_query("INSERT INTO $table $champs VALUES $valeurs $ret");
-	if (!$r) return 0;
-	if (!$ret) return -1;
-	$r = pg_fetch_array($r, NULL, PGSQL_NUM);
+	$r = pg_query($q="INSERT INTO $table $champs VALUES $valeurs $ret");
+	if ($r) {
+		if (!$ret) return 0;
+		if ($r = pg_fetch_array($r, NULL, PGSQL_NUM))
+			return $r[0];
+	}
+	spip_log("Erreur $q", 'pg'); // trace a minima
+	return -1;
 
-	return $r[0];
 }
 
 // http://doc.spip.org/@spip_pg_update
diff --git a/ecrire/inc/import_insere.php b/ecrire/inc/import_insere.php
index a562d26b60..be0a4bf245 100644
--- a/ecrire/inc/import_insere.php
+++ b/ecrire/inc/import_insere.php
@@ -12,22 +12,30 @@
 
 if (!defined("_ECRIRE_INC_VERSION")) return;
 
-// http://doc.spip.org/@insere_1_init
-function insere_1_init($request) {
+//  table des translations
 
-  //  preparation de la table des translations
-	$spip_translate = array(
+$spip_translate = array(
 		"type"		=> "VARCHAR(16) NOT NULL",
 		"ajout"		=> "ENUM('0', '1')",
 		"titre"		=> "text NOT NULL",
                 "id_old"	=> "BIGINT (21) DEFAULT '0' NOT NULL",
                 "id_new"	=> "BIGINT (21) DEFAULT '0' NOT NULL");
 
-	$spip_translate_key = array(
+$spip_translate_key = array(
                 "PRIMARY KEY"	=> "id_old, id_new, type",
                 "KEY id_old"	=> "id_old");
 
-	spip_mysql_create('spip_translate', $spip_translate, $spip_translate_key, true);
+// La rajouter ici car sql_insert en a besoin
+global $tables_principales;;
+$tables_principales['spip_translate'] =
+	array('field' => &$spip_translate, 'key' => &$spip_translate_key, 'join' => &$spip_signatures_join);
+
+// http://doc.spip.org/@insere_1_init
+function insere_1_init($request) {
+	global $tables_principales;
+	$v = $tables_principales['spip_translate'];
+	sql_create('spip_translate',  $v['field'], $v['key'], true);
+
 	// au cas ou la derniere fois ce serait terminee anormalement
 	spip_query("DELETE FROM spip_translate");
 	return insere_1bis_init($request);
@@ -40,6 +48,12 @@ function insere_1bis_init($request) {
 	$t = array_keys($GLOBALS['tables_principales']);
 	// ... mais pas cette table a cause de la duplication des login 
 	unset($t[array_search('spip_auteurs', $t)]);
+	// ni celle-ci, les qui est liee implicitement a la precedente
+	unset($t[array_search('spip_messages', $t)]);
+	// et pour celles-ci restent à programmer les regles
+	unset($t[array_search('spip_forum', $t)]);
+	unset($t[array_search('spip_syndic', $t)]);
+	unset($t[array_search('spip_signatures', $t)]);
 	return $t;
 }
 
@@ -87,19 +101,18 @@ function translate_init($request) {
 // http://doc.spip.org/@import_insere
 function import_insere($values, $table, $desc, $request, $atts) {
 
+	static $jesais = array();
+
 	$type_id = $desc['key']["PRIMARY KEY"];
 	// reserver une place dans les tables principales si nouveau
 	$ajout = 0;
+
 	if ((!function_exists($f = 'import_identifie_' . $type_id))
 	OR (!($n = $f($values, $table, $desc, $request)))) {
-	  // pas d'importation de types_doc (a revoir)
+          // pas d'importation de types_doc (a revoir)
 		if ($table == 'spip_types_documents') return;
-		$n = sql_insert($table, '', '()');
-		if (!$n) {
-			$GLOBALS['erreur_restauration'] = sql_error();
-			return;
-		}
-		$ajout = 1;
+		$n = sql_insert($table, '()', '()');
+		$ajout=1;
 	}
 
 	if (is_array($n))
@@ -107,7 +120,7 @@ function import_insere($values, $table, $desc, $request, $atts) {
 	else {$id = $n; $titre = "";}
 	sql_insert('spip_translate',
 				"(id_old, id_new, titre, type, ajout)",
-				     "(". $values[$type_id] .",$id, " . _q($titre) . ", '$type_id', '$ajout')");
+				     "(". $values[$type_id] .",$id, " . _q($titre) . ", '$type_id', $ajout)");
 }
 
 // Renumerotation des entites collectees
@@ -131,14 +144,13 @@ function import_translate($values, $table, $desc, $request, $atts) {
 // Une synchronisation plus fine serait preferable, cf [8004]
 
 // http://doc.spip.org/@import_inserer_translate
-function import_inserer_translate($values, $table, $desc, $request, $vals, $atts) {
+function import_inserer_translate($values, $table, $desc, $request, $atts) {
 	global $trans;
 	$p = $desc['key']["PRIMARY KEY"];
 	$v = $values[$p];
 
 	if (!isset($trans[$p]) OR !isset($trans[$p][$v]) OR $trans[$p][$v][2]){
-		spip_query("REPLACE $table (" . join(',',array_keys($values)) . ') VALUES (' .substr($vals,1) . ')');
-
+		sql_replace($table, $values, $desc);
 		$on = isset($atts['on']) ? ($atts['on']) : '';
 		$off = isset($atts['off']) ? ($atts['off']) : '';
 		if ($on OR $off) {
@@ -164,19 +176,15 @@ function import_inserer_translate($values, $table, $desc, $request, $vals, $atts
 // http://doc.spip.org/@import_translate_std
 function import_translate_std($values, $table, $desc, $request, $atts) {
 
-	$vals = '';
-
 	foreach ($values as $k => $v) {
 		if ($k=='id_parent' OR $k=='id_secteur')
 				$k = 'id_rubrique';
 		else  if (($k=='chapo') AND ($v[0]=='=') AND preg_match(_RACCOURCI_CHAPO, substr($v,1), $m))
 			$v = '=[->' . substr($v,1) . ']';
 
-		$v = importe_raccourci($k,importe_translate_maj($k, $v));
-
-		$vals .= "," . _q($v);
+		$values[$k]= importe_raccourci($k,importe_translate_maj($k, $v));
 	}
-	import_inserer_translate($values, $table, $desc, $request, $vals, $atts);
+	import_inserer_translate($values, $table, $desc, $request, $atts);
 }
 
 // http://doc.spip.org/@import_translate_spip_documents
@@ -188,14 +196,13 @@ function import_translate_spip_documents($values, $table, $desc, $request, $atts
 #	$url .= $atts['dir_img']; // deja dans la BD importee
 	$values['distant']= 'oui';
 
-	$vals = '';
 	foreach ($values as $k => $v) {
 	  if ($k=='fichier')
 	    $v = $url .$v;
 	  else $v = importe_raccourci($k,importe_translate_maj($k, $v));
-	  $vals .= "," . _q($v);
+	  $values[]= $v;
 	}
-	import_inserer_translate($values, $table, $desc, $request, $vals, $atts);
+	import_inserer_translate($values, $table, $desc, $request, $atts);
 }
 
 // Fonction de renumerotation, par delegation aux fonction specialisees
@@ -308,7 +315,7 @@ function import_identifie_parent_id_mot($id_groupe, $titre, $v)
 		$r = sql_fetch(spip_query("SELECT id_mot FROM spip_mots WHERE titre=$titre AND id_groupe=$new" ));
 		if ($r) return  (0 - $r['id_mot']);
 	}
-	$r = sql_insert('spip_mots', '', '()');
+	$r = sql_insert('spip_mots', '()', '()');
 	spip_query("REPLACE spip_translate (id_old, id_new, titre, type, ajout) VALUES ($v,$r,$titre,'id_mot',1)");
 	return $r;
 }
@@ -330,7 +337,7 @@ function import_identifie_parent_id_article($id_parent, $titre, $v)
 	$r = sql_fetch(spip_query("SELECT id_article FROM spip_articles WHERE titre=$titre AND id_rubrique=$id_parent AND statut<>'poubelle'" ));
 	if ($r) return (0 - $r['id_article']);
 
-	$r = sql_insert('spip_articles', '', '()');
+	$r = sql_insert('spip_articles', '()', '()');
 	spip_query("REPLACE spip_translate (id_old, id_new, titre, type, ajout) VALUES ($v,$r,$titre,'id_article',1)");
 	return $r;
 }
@@ -352,7 +359,7 @@ function import_identifie_parent_id_breve($id_parent, $titre, $v)
 	$r = sql_fetch(spip_query("SELECT id_breve FROM spip_breves WHERE titre=$titre AND id_rubrique=$id_parent AND statut<>'refuse'" ));
 	if ($r) return (0 - $r['id_breve']);
 
-	$r = sql_insert('spip_breves', '', '()');
+	$r = sql_insert('spip_breves', '()', '()');
 	spip_query("REPLACE spip_translate (id_old, id_new, titre, type, ajout) VALUES ($v,$r,$titre,'id_breve',1)");
 	return $r;
 }
-- 
GitLab