From 10f4de729dbd97169454938cc3abf800bccdd2a9 Mon Sep 17 00:00:00 2001
From: "Committo,Ergo:sum" <esj@rezo.net>
Date: Tue, 2 Oct 2007 11:31:04 +0000
Subject: [PATCH] =?UTF-8?q?La=20fonction=20'''trouver=5Ftable'''=20devient?=
 =?UTF-8?q?=20surchargeable=20sous=20le=20nom=20de=20'''base=5Ftrouver=5Ft?=
 =?UTF-8?q?able''',=20ce=20qui=20devrait=20permettre=20de=20r=C3=A9soudre?=
 =?UTF-8?q?=20proprement=20le=20probl=C3=A8me=20ddes=20tables=20nomm=C3=A9?=
 =?UTF-8?q?es=20bizarrement=20=C3=A9voqu=C3=A9=20dans=20http://archives.re?=
 =?UTF-8?q?zo.net/spip-dev.mbox/200709.mbox/%3C46E5795D.60704@math.univ-mo?=
 =?UTF-8?q?ntp2.fr%3E.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitattributes                |  1 +
 ecrire/base/abstract_sql.php  | 63 ++---------------------------
 ecrire/base/trouver_table.php | 74 +++++++++++++++++++++++++++++++++++
 ecrire/public/compiler.php    |  5 ++-
 ecrire/public/criteres.php    | 18 +++++++--
 ecrire/public/references.php  |  6 ++-
 6 files changed, 102 insertions(+), 65 deletions(-)
 create mode 100644 ecrire/base/trouver_table.php

diff --git a/.gitattributes b/.gitattributes
index 6baf50f2ee..0826c38c8f 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -426,6 +426,7 @@ ecrire/base/delete_all.php -text
 ecrire/base/delete_statistiques.php -text
 ecrire/base/import_all.php -text
 ecrire/base/index.php -text
+ecrire/base/trouver_table.php -text
 ecrire/charsets/cp1250.php -text
 ecrire/charsets/cp1251.php -text
 ecrire/charsets/cp1256.php -text
diff --git a/ecrire/base/abstract_sql.php b/ecrire/base/abstract_sql.php
index 7f15312ec0..53f8722a0e 100644
--- a/ecrire/base/abstract_sql.php
+++ b/ecrire/base/abstract_sql.php
@@ -311,65 +311,6 @@ function test_sql_int($type)
 	OR strpos($type, 'tinyint') === 0);
 }
 
-// Trouve la description d'une table, en particulier celle d'une boucle
-// Si on ne la trouve pas, on demande au serveur SQL
-// retourne False si lui non plus  ne la trouve pas.
-// Si on la trouve, le tableau resultat a les entrees:
-// field (comme dans serial.php)
-// key (comme dans serial.php)
-// table = nom SQL de la table (avec le prefixe spip_ pour les stds)
-// id_table = nom SPIP de la table (i.e. type de boucle)
-// le compilateur produit  FROM $r['table'] AS $r['id_table']
-// Cette fonction intervient a la compilation, 
-// mais aussi pour la balise contextuelle EXPOSE.
-
-// http://doc.spip.org/@trouver_table
-function trouver_table($nom, $serveur='')
-{
-	global $tables_principales, $tables_auxiliaires, $table_des_tables, $connexions;
-
-	if (!spip_connect($serveur)) return null;
-	$s = $serveur ? $serveur : 0;
-	$nom_sql = $nom;
-
-	if ($connexions[$s]['spip_connect_version']) {
-		// base sous SPIP, le nom SQL peut etre autre
-		if (isset($table_des_tables[$nom])) {
-		  // indirection (table principale avec nom!=type)
-			$t = $table_des_tables[$nom];
-			$nom_sql = 'spip_' . $t;
-			if (!isset($connexions[$s]['tables'][$nom_sql])) {
-				include_spip('base/serial');
-				$connexions[$s]['tables'][$nom_sql] = $tables_principales[$nom_sql];
-				$connexions[$s]['tables'][$nom_sql]['table']= $nom_sql;
-				$connexions[$s]['tables'][$nom_sql]['id_table']= $t;
-			} # table principale deja vue, ok.
-		} else {
-			include_spip('base/auxiliaires');
-			if (isset($tables_auxiliaires['spip_' .$nom])) {
-				$nom_sql = 'spip_' . $nom;
-				if (!isset($connexions[$s]['tables'][$nom_sql])) {
-					$connexions[$s]['tables'][$nom_sql] = $tables_auxiliaires[$nom_sql];
-					$connexions[$s]['tables'][$nom_sql]['table']= $nom_sql;
-					$connexions[$s]['tables'][$nom_sql]['id_table']= $nom;
-				} # table locale a cote de SPIP: noms egaux
-			} # auxiliaire deja vue, ok.
-		}
-	}
-
-	if (isset($connexions[$s]['tables'][$nom_sql]))
-		return $connexions[$s]['tables'][$nom_sql];
-
-	$desc = sql_showtable($nom_sql, $serveur, ($nom_sql != $nom));
-	if (!$desc OR !$desc['field']) {
-		  spip_log("table inconnue $serveur $nom");
-		  return null;
-	} else {
-		$desc['table']= $nom_sql;
-		$desc['id_table']= $nom;
-	}
-	return	$connexions[$s]['tables'][$nom_sql] = $desc;
-}
 
 // Cette fonction est vouee a disparaitre
 
@@ -377,6 +318,7 @@ function trouver_table($nom, $serveur='')
 function description_table($nom){
 
         global $tables_principales, $tables_auxiliaires;
+	static $f;
 
         include_spip('base/serial');
         if (isset($tables_principales[$nom]))
@@ -386,7 +328,8 @@ function description_table($nom){
         if (isset($tables_auxiliaires[$nom]))
                 return $tables_auxiliaires[$nom];
 
-	return trouver_table($nom);
+	if (!$f) $f = charger_fonction('trouver_table', 'base');
+	return $f($nom);
 }
 
 ?>
diff --git a/ecrire/base/trouver_table.php b/ecrire/base/trouver_table.php
new file mode 100644
index 0000000000..ea40914104
--- /dev/null
+++ b/ecrire/base/trouver_table.php
@@ -0,0 +1,74 @@
+<?php
+
+/***************************************************************************\
+ *  SPIP, Systeme de publication pour l'internet                           *
+ *                                                                         *
+ *  Copyright (c) 2001-2007                                                *
+ *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
+ *                                                                         *
+ *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
+ *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
+\***************************************************************************/
+
+if (!defined("_ECRIRE_INC_VERSION")) return;
+
+// Trouve la description d'une table, en particulier celle d'une boucle
+// Si on ne la trouve pas, on demande au serveur SQL
+// retourne False si lui non plus  ne la trouve pas.
+// Si on la trouve, le tableau resultat a les entrees:
+// field (comme dans serial.php)
+// key (comme dans serial.php)
+// table = nom SQL de la table (avec le prefixe spip_ pour les stds)
+// id_table = nom SPIP de la table (i.e. type de boucle)
+// le compilateur produit  FROM $r['table'] AS $r['id_table']
+// Cette fonction intervient a la compilation, 
+// mais aussi pour la balise contextuelle EXPOSE.
+
+// http://doc.spip.org/@trouver_table
+function base_trouver_table_dist($nom, $serveur='')
+{
+	global $tables_principales, $tables_auxiliaires, $table_des_tables, $connexions;
+
+	if (!spip_connect($serveur)) return null;
+	$s = $serveur ? $serveur : 0;
+	$nom_sql = $nom;
+
+	if ($connexions[$s]['spip_connect_version']) {
+		// base sous SPIP, le nom SQL peut etre autre
+		if (isset($table_des_tables[$nom])) {
+		  // indirection (table principale avec nom!=type)
+			$t = $table_des_tables[$nom];
+			$nom_sql = 'spip_' . $t;
+			if (!isset($connexions[$s]['tables'][$nom_sql])) {
+				include_spip('base/serial');
+				$connexions[$s]['tables'][$nom_sql] = $tables_principales[$nom_sql];
+				$connexions[$s]['tables'][$nom_sql]['table']= $nom_sql;
+				$connexions[$s]['tables'][$nom_sql]['id_table']= $t;
+			} # table principale deja vue, ok.
+		} else {
+			include_spip('base/auxiliaires');
+			if (isset($tables_auxiliaires['spip_' .$nom])) {
+				$nom_sql = 'spip_' . $nom;
+				if (!isset($connexions[$s]['tables'][$nom_sql])) {
+					$connexions[$s]['tables'][$nom_sql] = $tables_auxiliaires[$nom_sql];
+					$connexions[$s]['tables'][$nom_sql]['table']= $nom_sql;
+					$connexions[$s]['tables'][$nom_sql]['id_table']= $nom;
+				} # table locale a cote de SPIP: noms egaux
+			} # auxiliaire deja vue, ok.
+		}
+	}
+
+	if (isset($connexions[$s]['tables'][$nom_sql]))
+		return $connexions[$s]['tables'][$nom_sql];
+
+	$desc = sql_showtable($nom_sql, $serveur, ($nom_sql != $nom));
+	if (!$desc OR !$desc['field']) {
+		  spip_log("table inconnue $serveur $nom");
+		  return null;
+	} else {
+		$desc['table']= $nom_sql;
+		$desc['id_table']= $nom;
+	}
+	return	$connexions[$s]['tables'][$nom_sql] = $desc;
+}
+?>
diff --git a/ecrire/public/compiler.php b/ecrire/public/compiler.php
index 31a0b4d8a0..14a395922f 100644
--- a/ecrire/public/compiler.php
+++ b/ecrire/public/compiler.php
@@ -670,6 +670,9 @@ function code_boucle(&$boucles, $id, $nom)
 // http://doc.spip.org/@public_compiler_dist
 function public_compiler_dist($squelette, $nom, $gram, $sourcefile, $connect=''){
 	global $tables_jointures;
+	static $trouver_table;
+	if (!$trouver_table)
+		$trouver_table = charger_fonction('trouver_table', 'base');
 
 	// Pre-traitement : reperer le charset du squelette, et le convertir
 	// Bonus : supprime le BOM
@@ -699,7 +702,7 @@ function public_compiler_dist($squelette, $nom, $gram, $sourcefile, $connect='')
 		if ($type != 'boucle') {
 			if (!$boucles[$id]->sql_serveur AND $connect)
 				$boucles[$id]->sql_serveur = $connect;
-			$show = trouver_table($type, $boucles[$id]->sql_serveur);
+			$show = $trouver_table($type, $boucles[$id]->sql_serveur);
 			if ($show) {
 				$boucles[$id]->show = $show;
 				// recopie des 2 infos les plus importantes
diff --git a/ecrire/public/criteres.php b/ecrire/public/criteres.php
index e5e3c5e514..0ae3af857b 100644
--- a/ecrire/public/criteres.php
+++ b/ecrire/public/criteres.php
@@ -799,7 +799,11 @@ function calculer_critere_infixe($idb, &$boucles, $crit) {
 // http://doc.spip.org/@critere_secteur_forum
 function critere_secteur_forum($idb, &$boucles, $val, $crit)
 {
-	$desc = trouver_table('articles', $boucles[$idb]->sql_serveur);
+	static $trouver_table;
+	if (!$trouver_table)
+		$trouver_table = charger_fonction('trouver_table', 'base');
+
+	$desc = $trouver_table('articles', $boucles[$idb]->sql_serveur);
 	return calculer_critere_externe_init($boucles[$idb], array($desc['table']), 'id_secteur', $desc, $crit->cond, true);
 }
 
@@ -933,6 +937,10 @@ function liste_champs_jointures($nom,$desc){
 // http://doc.spip.org/@calculer_chaine_jointures
 function calculer_chaine_jointures(&$boucle, $depart, $arrivee, $vu=array(), $milieu_prec = false)
 {
+	static $trouver_table;
+	if (!$trouver_table)
+		$trouver_table = charger_fonction('trouver_table', 'base');
+
 	list($dnom,$ddesc) = $depart;
 	list($anom,$adesc) = $arrivee;
 	if (!count($vu))
@@ -952,7 +960,7 @@ function calculer_chaine_jointures(&$boucle, $depart, $arrivee, $vu=array(), $mi
 		$new = $vu;
 		foreach($boucle->jointures as $v) {
 			if ($v && (!in_array($v,$vu)) && 
-			    ($def = trouver_table($v, $boucle->sql_serveur))) {
+			    ($def = $trouver_table($v, $boucle->sql_serveur))) {
 				$milieu = array_intersect($ddesc['key'], trouver_cles_table($def['key']));
 				$new[] = $v;
 				foreach ($milieu as $k)
@@ -991,8 +999,12 @@ function trouver_cles_table($keys)
 // http://doc.spip.org/@trouver_champ_exterieur
 function trouver_champ_exterieur($cle, $joints, &$boucle, $checkarrivee = false)
 {
+  static $trouver_table;
+  if (!$trouver_table)
+		$trouver_table = charger_fonction('trouver_table', 'base');
+
   foreach($joints as $k => $join) {
-    if ($join && $table = trouver_table($join, $boucle->sql_serveur)) {
+    if ($join && $table = $trouver_table($join, $boucle->sql_serveur)) {
       if (isset($table['field']) && array_key_exists($cle, $table['field'])
       	&& ($checkarrivee==false || $checkarrivee==$table['table'])) // si on sait ou on veut arriver, il faut que ca colle
 	return  array($table['table'], $table);
diff --git a/ecrire/public/references.php b/ecrire/public/references.php
index 64aa9cbf70..c5cab7b997 100644
--- a/ecrire/public/references.php
+++ b/ecrire/public/references.php
@@ -103,6 +103,10 @@ function index_tables_en_pile($idb, $nom_champ, &$boucles) {
 // http://doc.spip.org/@index_exception
 function index_exception(&$boucle, $desc, $nom_champ, $excep)
 {
+	static $trouver_table;
+	if (!$trouver_table)
+		$trouver_table = charger_fonction('trouver_table', 'base');
+
 	if (is_array($excep)) {
 		// permettre aux plugins de gerer eux meme des jointures derogatoire ingerables
 		$t = NULL;
@@ -114,7 +118,7 @@ function index_exception(&$boucle, $desc, $nom_champ, $excep)
 			list($e, $x) = $excep;	#PHP4 affecte de gauche a droite
 			$excep = $x;		#PHP5 de droite a gauche !
 			if (!$t = array_search($e, $boucle->from)) {
-				$j = trouver_table($e, $boucle->sql_serveur);
+				$j = $trouver_table($e, $boucle->sql_serveur);
 				if ($j) {
 					$t = 'J' . count($boucle->from);
 					$boucle->from[$t] = $j['table'];
-- 
GitLab