3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2016 *
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')) return;
18 function inc_recherche_to_array_dist($recherche, $options = array()) {
21 $options = array_merge(
32 include_spip('inc/rechercher');
33 include_spip('inc/autoriser');
45 $table = sinon($options['table'], 'article');
46 if ($options['champs'])
47 $champs = $options['champs'];
49 $l = liste_des_champs();
50 $champs = $l['article'];
52 $serveur = $options['serveur'];
54 list($methode, $q, $preg) = expression_recherche($recherche, $options);
56 $jointures = $options['jointures']
57 ?
liste_des_jointures()
60 $_id_table = id_table_objet($table);
62 // c'est un pis-aller : ca a peu de chance de marcher, mais mieux quand meme que en conservant la ','
63 // (aka ca marche au moins dans certains cas comme avec spip_formulaires_reponses_champs)
64 if (strpos($_id_table,",")!==false){
65 $_id_table = explode(',',$_id_table);
66 $_id_table = reset($_id_table);
69 $requete['SELECT'][] = "t.".$_id_table;
72 foreach ($champs as $champ => $poids) {
73 if (is_array($champ)){
74 spip_log("requetes imbriquees interdites");
76 if (strpos($champ,".")===FALSE)
78 $requete['SELECT'][] = $champ;
79 $a[] = $champ.' '.$methode.' '.$q;
82 if ($a) $requete['WHERE'][] = join(" OR ", $a);
83 $requete['FROM'][] = table_objet_sql($table).' AS t';
88 $requete['SELECT'], $requete['FROM'], $requete['WHERE'],
89 implode(" ",$requete['GROUPBY']),
90 $requete['ORDERBY'], $requete['LIMIT'],
91 $requete['HAVING'], $serveur
94 while ($t = sql_fetch($s,$serveur)
95 AND (!isset($t['score']) OR $t['score']>0)) {
96 $id = intval($t[$_id_table]);
98 if ($options['toutvoir']
99 OR autoriser('voir', $table, $id)) {
100 // indiquer les champs concernes
101 $champs_vus = array();
106 foreach ($champs as $champ => $poids) {
107 $champ = explode('.',$champ);
108 $champ = end($champ);
109 // translitteration_rapide uniquement si on est deja en utf-8
110 $value = ($GLOBALS['meta']['charset']=='utf-8' ?
translitteration_rapide($t[$champ]) : translitteration($t[$champ]));
112 ($options['score'] ||
$options['matches'])
113 ?
preg_match_all($preg, $value, $regs, PREG_SET_ORDER
)
114 : preg_match($preg, $value)
118 if ($options['champs'])
119 $champs_vus[$champ] = $t[$champ];
120 if ($options['score'])
121 $score +
= $n * $poids;
122 if ($options['matches'])
123 $matches[$champ] = $regs;
125 if (!$options['champs']
126 AND !$options['score']
127 AND !$options['matches'])
133 if (!isset($results))
135 $results[$id] = array();
137 $results[$id]['champs'] = $champs_vus;
139 $results[$id]['score'] = $score;
141 $results[$id]['matches'] = $matches;
147 // Gerer les donnees associees
148 // ici on est un peu naze : pas capables de reconstruire une jointure complexe
149 // on ne sait passer que par table de laison en 1 coup
150 if (isset($jointures[$table])
151 AND $joints = recherche_en_base(
154 array_merge($options, array('jointures' => false))
157 include_spip('action/editer_liens');
158 $trouver_table = charger_fonction('trouver_table','base');
159 $cle_depart = id_table_objet($table);
160 $table_depart = table_objet($table,$serveur);
161 $desc_depart = $trouver_table($table_depart,$serveur);
162 $depart_associable = objet_associable($table);
163 foreach ($joints as $table_liee => $ids_trouves) {
164 // on peut definir une fonction de recherche jointe pour regler les cas particuliers
167 $rechercher_joints = charger_fonction("rechercher_joints_${table}_${table_liee}","inc",true)
168 or $rechercher_joints = charger_fonction("rechercher_joints_objet_${table_liee}","inc",true)
169 or $rechercher_joints = charger_fonction("rechercher_joints_${table}_objet_lie","inc",true)
172 $cle_arrivee = id_table_objet($table_liee);
173 $table_arrivee = table_objet($table_liee,$serveur);
174 $desc_arrivee = $trouver_table($table_arrivee,$serveur);
175 // cas simple : $cle_depart dans la table_liee
176 if (isset($desc_arrivee['field'][$cle_depart])){
177 $s = sql_select("$cle_depart, $cle_arrivee", $desc_arrivee['table_sql'], sql_in($cle_arrivee, array_keys($ids_trouves)), '','','','',$serveur);
179 // cas simple : $cle_arrivee dans la table
180 elseif (isset($desc_depart['field'][$cle_arrivee])){
181 $s = sql_select("$cle_depart, $cle_arrivee", $desc_depart['table_sql'], sql_in($cle_arrivee, array_keys($ids_trouves)), '','','','',$serveur);
183 // sinon cherchons une table de liaison
184 // cas recherche principale article, objet lie document : passer par spip_documents_liens
185 elseif ($l = objet_associable($table_liee)){
186 list($primary, $table_liens) = $l;
187 $s = sql_select("id_objet as $cle_depart, $primary as $cle_arrivee", $table_liens, array("objet='$table'",sql_in($primary, array_keys($ids_trouves))), '','','','',$serveur);
189 // cas recherche principale auteur, objet lie article: passer par spip_auteurs_liens
190 elseif ($l = $depart_associable){
191 list($primary, $table_liens) = $l;
192 $s = sql_select("$primary as $cle_depart, id_objet as $cle_arrivee", $table_liens, array("objet='$table_liee'",sql_in('id_objet', array_keys($ids_trouves))), '','','','',$serveur);
194 // cas table de liaison generique spip_xxx_yyy
195 elseif($t=$trouver_table($table_arrivee."_".$table_depart,$serveur)
196 OR $t=$trouver_table($table_depart."_".$table_arrivee,$serveur)){
197 $s = sql_select("$cle_depart,$cle_arrivee", $t["table_sql"], sql_in($cle_arrivee, array_keys($ids_trouves)), '','','','',$serveur);
201 list($cle_depart,$cle_arrivee,$s) = $rechercher_joints($table,$table_liee,array_keys($ids_trouves), $serveur);
203 while ($t = is_array($s)?
array_shift($s):sql_fetch($s)) {
204 $id = $t[$cle_depart];
205 $joint = $ids_trouves[$t[$cle_arrivee]];
206 if (!isset($results))
208 if (!isset($results[$id]))
209 $results[$id] = array();
210 if (isset($joint['score']) and $joint['score']) {
211 $results[$id]['score'] +
= $joint['score'];
213 if (isset($joint['champs']) and $joint['champs']) {
214 foreach($joint['champs'] as $c => $val) {
215 $results[$id]['champs'][$table_liee.'.'.$c] = $val;
218 if (isset($joint['matches']) and $joint['matches']) {
219 foreach($joint['matches'] as $c => $val) {
220 $results[$id]['matches'][$table_liee.'.'.$c] = $val;