From f41cd0eb68f696abc2d2631dfa0263517572bb99 Mon Sep 17 00:00:00 2001
From: "Committo,Ergo:sum" <esj@rezo.net>
Date: Sat, 29 Apr 2006 17:04:45 +0000
Subject: [PATCH] =?UTF-8?q?Partage=20de=20code,=20table=5Fobjet=20s'=C3=A9?=
 =?UTF-8?q?tendant=20=C3=A0=20tous=20les=20tables=20principales=20afin=20d?=
 =?UTF-8?q?'etre=20utilis=C3=A9e=20dans=20inc/import.php.=20Mais=20il=20me?=
 =?UTF-8?q?=20semblerait=20utile=20d'adopter=20une=20version=5Farchive=201?=
 =?UTF-8?q?.3=20pour=20gommer=20les=20diff=C3=A9rences=20avec=20le=20nom?=
 =?UTF-8?q?=20des=20boucles=20qui=20fait=20qu'il=20existe=20une=20liste=20?=
 =?UTF-8?q?de=20tables=20presque=20identique=20dans=20le=20compilateur.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 ecrire/inc/import.php | 87 +++++++++++++++++++++----------------------
 ecrire/inc/utils.php  | 25 ++++++++++---
 2 files changed, 61 insertions(+), 51 deletions(-)

diff --git a/ecrire/inc/import.php b/ecrire/inc/import.php
index f70a48a8d9..034225834f 100644
--- a/ecrire/inc/import.php
+++ b/ecrire/inc/import.php
@@ -85,7 +85,7 @@ function import_debut($f, $gz=false) {
 //
 
 function import_objet_1_2($f, $gz=false) {
-	global $import_ok, $pos, $abs_pos;
+	global $import_ok;
 	static $time_javascript;
 
 	if (time() - $time_javascript > 3) {	// 3 secondes
@@ -93,36 +93,24 @@ function import_objet_1_2($f, $gz=false) {
 		$time_javascript = time();
 	}
 
-	static $tables;
-	if (!$tables) $tables = array(
-		'article' => 'spip_articles',
-		'auteur' => 'spip_auteurs',
-		'breve' => 'spip_breves',
-		'document' => 'spip_documents',
-		'forum' => 'spip_forum',
-		'groupe_mots' => 'spip_groupes_mots',
-		'message' => 'spip_messages',
-		'mot' => 'spip_mots',
-		'petition' => 'spip_petitions',
-		'rubrique' => 'spip_rubriques',
-		'signature' => 'spip_signatures',
-		'syndic' => 'spip_syndic',
-		'syndic_article' => 'spip_syndic_articles',
-		'type_document' => 'spip_types_documents'
-	);
-
-	$import_ok = false;
+	
 	$b = '';
 	// Lire le type d'objet
-	if (!($type = xml_fetch_tag($f, $b, $gz))) return false;
+	if (!($type = xml_fetch_tag($f, $b, $gz))) return ($import_ok = false);
 	if ($type == '/SPIP') return !($import_ok = true);
+	return ($import_ok = import_objet_1_2_boucle($type, $f, $gz));
+}
+
+function import_objet_1_2_boucle($type, $f, $gz) {
+	global $pos, $abs_pos;
+
 	$id = "id_$type";
 	$id_objet = 0;
+	$liens = array();
 
 	// Lire les champs de l'objet
 	for (;;) {
-		$b = '';
-		if (!($col = xml_fetch_tag($f, $b, $gz))) return false;
+		if (!($col = xml_fetch_tag($f, $value, $gz))) return false;
 		if ($col == '/'.$type) break;
 		$value = '';
 		if (!xml_fetch_tag($f, $value, $gz)) return false;
@@ -139,7 +127,7 @@ function import_objet_1_2($f, $gz=false) {
 					echo "--><br><font color='red'><b>"._T('avis_erreur_sauvegarde', array('type' => $type, 'id_objet' => $id_objet))."</b></font>\n<font color='black'>"._T('avis_colonne_inexistante', array('col' => $col));
 					if ($col == 'images') echo _T('info_verifier_image');
 					echo "</font>\n<!--";
-					$GLOBALS['erreur_restauration'] = true;
+					$GLOBALS['erreur_restauration']= true;
 				}
 			}
 			else {
@@ -149,14 +137,40 @@ function import_objet_1_2($f, $gz=false) {
 			}
 		}
 	}
-
-	$table = $tables[$type];
-	
-	if (!spip_query("REPLACE $table (" . join(',', $cols) . ') VALUES (' . join(',', $values) . ')')) {
+	$table = table_objet($type);
+	if ($table) 
+		$table = "spip_$table";
+	else {
+		$table = $type;
+		// Table non Spip, on accepte.
+		// Si c'est vraiment n'importe quoi le test suivant le dira
+		echo "\nTable externe: $type";
+	}
+	$n = spip_query("REPLACE " . $table . "(" . join(',', $cols) . ') VALUES (' . join(',', $values) . ')');
+	if(!$n) {
 		echo "--><br><font color='red'><b>"._T('avis_erreur_mysql')."</b></font>\n<font color='black'><tt>".spip_sql_error()."</tt></font>\n<!--";
 		$GLOBALS['erreur_restauration'] = true;
 	}
 
+	supprime_anciens_liens($type, $id_objet);
+	$sens = ($type == 'auteur' OR $type == 'mot' OR $type == 'document');
+	$type .= 's';
+	foreach($liens as $type_lien => $t) {
+		if (!$sens)
+			$table_lien = $type_lien.'s_'.$type;
+		else
+			$table_lien = $type.'_'.$type_lien .
+			  (($type_lien == 'syndic' OR $type_lien == 'forum') ? '' : 's');
+		spip_abstract_insert('spip_' . $table_lien, "($id, id_$type_lien)", join(',', $t));
+	}
+	
+	ecrire_meta("status_restauration", strval($pos + $abs_pos));
+
+	return true;
+}
+
+function supprime_anciens_liens($type, $id_objet)
+{
 	if ($type == 'article') {
 		spip_query("DELETE FROM spip_auteurs_articles WHERE id_article=$id_objet");
 		spip_query("DELETE FROM spip_documents_articles WHERE id_article=$id_objet");
@@ -181,25 +195,8 @@ function import_objet_1_2($f, $gz=false) {
 	else if ($type == 'message') {
 		spip_query("DELETE FROM spip_auteurs_messages WHERE id_message=$id_objet");
 	}
-	if ($liens) {
-		reset($liens);
-		while (list($type_lien, $t) = each($liens)) {
-			if ($type == 'auteur' OR $type == 'mot' OR $type == 'document')
-				if ($type_lien == 'syndic' OR $type_lien == 'forum') $table_lien = 'spip_'.$type.'s_'.$type_lien;
-				else $table_lien = 'spip_'.$type.'s_'.$type_lien.'s';
-			else
-				$table_lien = 'spip_'.$type_lien.'s_'.$type.'s';
-			spip_abstract_insert($table_lien, "($id, id_$type_lien)", join(',', $t));
-		}
-	}
-
-	$p = $pos + $abs_pos;
-	ecrire_meta("status_restauration", "$p");
-
-	return $import_ok = true;
 }
 
-
 function import_objet_0_0($f, $gz=false) {
 	global $import_ok, $pos, $abs_pos;
 
diff --git a/ecrire/inc/utils.php b/ecrire/inc/utils.php
index 780bcaa738..990bfdb3cc 100644
--- a/ecrire/inc/utils.php
+++ b/ecrire/inc/utils.php
@@ -411,13 +411,26 @@ function _L($text, $args=array()) {
 
 // Nommage bizarre des tables d'objets
 function table_objet($type) {
-	if ($type == 'site' OR $type == 'syndic')
-		return 'syndic';
-	else if ($type == 'forum')
-		return 'forum';
-	else
-		return $type.'s';
+	static $surnoms = array(
+                'article' => 'articles',
+                'auteur' => 'auteurs',
+                'breve' => 'breves',
+                'document' => 'documents',
+                'forum' => 'forum', # hum
+                'groupe_mots' => 'groupes_mots', # hum
+                'message' => 'messages',
+                'mot' => 'mots',
+                'petition' => 'petitions',
+                'rubrique' => 'rubriques',
+                'signature' => 'signatures',
+                'syndic' => 'syndic',
+                'site' => 'syndic', # hum hum
+                'syndic_article' => 'syndic_articles',
+                'type_document' => 'types_documents' # hum
+		);
+	return $surnoms[$type];
 }
+
 function id_table_objet($type) {
 	if ($type == 'site' OR $type == 'syndic')
 		return 'id_syndic';
-- 
GitLab