9e37e63bcce26942f30f4c03c78872e6339d1315
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2017 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
14 if (!defined('_ECRIRE_INC_VERSION')) {
20 function inc_recherche_to_array_dist($recherche, $options = array()) {
23 $options = array_merge(
34 include_spip('inc/rechercher');
35 include_spip('inc/autoriser');
47 $table = sinon($options['table'], 'article');
48 if ($options['champs']) {
49 $champs = $options['champs'];
51 $l = liste_des_champs();
52 $champs = $l['article'];
54 $serveur = $options['serveur'];
56 list($methode, $q, $preg) = expression_recherche($recherche, $options);
58 $jointures = $options['jointures']
59 ?
liste_des_jointures()
62 $_id_table = id_table_objet($table);
64 // c'est un pis-aller : ca a peu de chance de marcher, mais mieux quand meme que en conservant la ','
65 // (aka ca marche au moins dans certains cas comme avec spip_formulaires_reponses_champs)
66 if (strpos($_id_table, ",") !== false) {
67 $_id_table = explode(',', $_id_table);
68 $_id_table = reset($_id_table);
71 $requete['SELECT'][] = "t." . $_id_table;
74 foreach ($champs as $champ => $poids) {
75 if (is_array($champ)) {
76 spip_log("requetes imbriquees interdites");
78 if (strpos($champ, ".") === false) {
81 $requete['SELECT'][] = $champ;
82 $a[] = $champ . ' ' . $methode . ' ' . $q;
86 $requete['WHERE'][] = join(" OR ", $a);
88 $requete['FROM'][] = table_objet_sql($table) . ' AS t';
93 $requete['SELECT'], $requete['FROM'], $requete['WHERE'],
94 implode(" ", $requete['GROUPBY']),
95 $requete['ORDERBY'], $requete['LIMIT'],
96 $requete['HAVING'], $serveur
99 while ($t = sql_fetch($s, $serveur)
100 and (!isset($t['score']) or $t['score'] > 0)) {
101 $id = intval($t[$_id_table]);
103 if ($options['toutvoir']
104 or autoriser('voir', $table, $id)
106 // indiquer les champs concernes
107 $champs_vus = array();
112 foreach ($champs as $champ => $poids) {
113 $champ = explode('.', $champ);
114 $champ = end($champ);
115 // translitteration_rapide uniquement si on est deja en utf-8
116 $value = ($GLOBALS['meta']['charset'] == 'utf-8' ?
translitteration_rapide($t[$champ]) : translitteration($t[$champ]));
118 ($options['score'] ||
$options['matches'])
119 ?
preg_match_all($preg, $value, $regs, PREG_SET_ORDER
)
120 : preg_match($preg, $value)
124 if ($options['champs']) {
125 $champs_vus[$champ] = $t[$champ];
127 if ($options['score']) {
128 $score +
= $n * $poids;
130 if ($options['matches']) {
131 $matches[$champ] = $regs;
134 if (!$options['champs']
135 and !$options['score']
136 and !$options['matches']
144 if (!isset($results)) {
147 $results[$id] = array();
149 $results[$id]['champs'] = $champs_vus;
152 $results[$id]['score'] = $score;
155 $results[$id]['matches'] = $matches;
162 // Gerer les donnees associees
163 // ici on est un peu naze : pas capables de reconstruire une jointure complexe
164 // on ne sait passer que par table de laison en 1 coup
165 if (isset($jointures[$table])
166 and $joints = recherche_en_base(
169 array_merge($options, array('jointures' => false))
172 include_spip('action/editer_liens');
173 $trouver_table = charger_fonction('trouver_table', 'base');
174 $cle_depart = id_table_objet($table);
175 $table_depart = table_objet($table, $serveur);
176 $desc_depart = $trouver_table($table_depart, $serveur);
177 $depart_associable = objet_associable($table);
178 foreach ($joints as $table_liee => $ids_trouves) {
179 // on peut definir une fonction de recherche jointe pour regler les cas particuliers
182 $rechercher_joints = charger_fonction("rechercher_joints_${table}_${table_liee}", "inc", true)
183 or $rechercher_joints = charger_fonction("rechercher_joints_objet_${table_liee}", "inc", true)
184 or $rechercher_joints = charger_fonction("rechercher_joints_${table}_objet_lie", "inc", true)
187 $cle_arrivee = id_table_objet($table_liee);
188 $table_arrivee = table_objet($table_liee, $serveur);
189 $desc_arrivee = $trouver_table($table_arrivee, $serveur);
190 // cas simple : $cle_depart dans la table_liee
191 if (isset($desc_arrivee['field'][$cle_depart])) {
192 $s = sql_select("$cle_depart, $cle_arrivee", $desc_arrivee['table_sql'],
193 sql_in($cle_arrivee, array_keys($ids_trouves)), '', '', '', '', $serveur);
194 } // cas simple : $cle_arrivee dans la table
195 elseif (isset($desc_depart['field'][$cle_arrivee])) {
196 $s = sql_select("$cle_depart, $cle_arrivee", $desc_depart['table_sql'],
197 sql_in($cle_arrivee, array_keys($ids_trouves)), '', '', '', '', $serveur);
199 // sinon cherchons une table de liaison
200 // cas recherche principale article, objet lie document : passer par spip_documents_liens
201 elseif ($l = objet_associable($table_liee)) {
202 list($primary, $table_liens) = $l;
203 $s = sql_select("id_objet as $cle_depart, $primary as $cle_arrivee", $table_liens,
204 array("objet='$table'", sql_in($primary, array_keys($ids_trouves))), '', '', '', '', $serveur);
205 } // cas recherche principale auteur, objet lie article: passer par spip_auteurs_liens
206 elseif ($l = $depart_associable) {
207 list($primary, $table_liens) = $l;
208 $s = sql_select("$primary as $cle_depart, id_objet as $cle_arrivee", $table_liens,
209 array("objet='$table_liee'", sql_in('id_objet', array_keys($ids_trouves))), '', '', '', '', $serveur);
210 } // cas table de liaison generique spip_xxx_yyy
211 elseif ($t = $trouver_table($table_arrivee . "_" . $table_depart, $serveur)
212 or $t = $trouver_table($table_depart . "_" . $table_arrivee, $serveur)
214 $s = sql_select("$cle_depart,$cle_arrivee", $t["table_sql"], sql_in($cle_arrivee, array_keys($ids_trouves)),
215 '', '', '', '', $serveur);
218 list($cle_depart, $cle_arrivee, $s) = $rechercher_joints($table, $table_liee, array_keys($ids_trouves),
222 while ($t = is_array($s) ?
array_shift($s) : sql_fetch($s)) {
223 $id = $t[$cle_depart];
224 $joint = $ids_trouves[$t[$cle_arrivee]];
225 if (!isset($results)) {
228 if (!isset($results[$id])) {
229 $results[$id] = array();
231 if (isset($joint['score']) and $joint['score']) {
232 if (!isset($results[$id]['score'])) {
233 $results[$id]['score'] = 0;
235 $results[$id]['score'] +
= $joint['score'];
237 if (isset($joint['champs']) and $joint['champs']) {
238 foreach ($joint['champs'] as $c => $val) {
239 $results[$id]['champs'][$table_liee . '.' . $c] = $val;
242 if (isset($joint['matches']) and $joint['matches']) {
243 foreach ($joint['matches'] as $c => $val) {
244 $results[$id]['matches'][$table_liee . '.' . $c] = $val;