3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2014 *
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);
61 $requete['SELECT'][] = "t.".$_id_table;
64 foreach ($champs as $champ => $poids) {
65 if (is_array($champ)){
66 spip_log("requetes imbriquees interdites");
68 if (strpos($champ,".")===FALSE)
70 $requete['SELECT'][] = $champ;
71 $a[] = $champ.' '.$methode.' '.$q;
74 if ($a) $requete['WHERE'][] = join(" OR ", $a);
75 $requete['FROM'][] = table_objet_sql($table).' AS t';
80 $requete['SELECT'], $requete['FROM'], $requete['WHERE'],
81 implode(" ",$requete['GROUPBY']),
82 $requete['ORDERBY'], $requete['LIMIT'],
83 $requete['HAVING'], $serveur
86 while ($t = sql_fetch($s,$serveur)
87 AND (!isset($t['score']) OR $t['score']>0)) {
88 $id = intval($t[$_id_table]);
90 if ($options['toutvoir']
91 OR autoriser('voir', $table, $id)) {
92 // indiquer les champs concernes
93 $champs_vus = array();
98 foreach ($champs as $champ => $poids) {
99 $champ = explode('.',$champ);
100 $champ = end($champ);
101 // translitteration_rapide uniquement si on est deja en utf-8
102 $value = ($GLOBALS['meta']['charset']=='utf-8' ?
translitteration_rapide($t[$champ]) : translitteration($t[$champ]));
104 ($options['score'] ||
$options['matches'])
105 ?
preg_match_all($preg, $value, $regs, PREG_SET_ORDER
)
106 : preg_match($preg, $value)
110 if ($options['champs'])
111 $champs_vus[$champ] = $t[$champ];
112 if ($options['score'])
113 $score +
= $n * $poids;
114 if ($options['matches'])
115 $matches[$champ] = $regs;
117 if (!$options['champs']
118 AND !$options['score']
119 AND !$options['matches'])
125 if (!isset($results))
127 $results[$id] = array();
129 $results[$id]['champs'] = $champs_vus;
131 $results[$id]['score'] = $score;
133 $results[$id]['matches'] = $matches;
139 // Gerer les donnees associees
140 // ici on est un peu naze : pas capables de reconstruire une jointure complexe
141 // on ne sait passer que par table de laison en 1 coup
142 if (isset($jointures[$table])
143 AND $joints = recherche_en_base(
146 array_merge($options, array('jointures' => false))
149 include_spip('action/editer_liens');
150 $trouver_table = charger_fonction('trouver_table','base');
151 $cle_depart = id_table_objet($table);
152 $table_depart = table_objet($table,$serveur);
153 $desc_depart = $trouver_table($table_depart,$serveur);
154 $depart_associable = objet_associable($table);
155 foreach ($joints as $table_liee => $ids_trouves) {
156 // on peut definir une fonction de recherche jointe pour regler les cas particuliers
159 $rechercher_joints = charger_fonction("rechercher_joints_${table}_${table_liee}","inc",true)
160 or $rechercher_joints = charger_fonction("rechercher_joints_objet_${table_liee}","inc",true)
161 or $rechercher_joints = charger_fonction("rechercher_joints_${table}_objet_lie","inc",true)
164 $cle_arrivee = id_table_objet($table_liee);
165 $table_arrivee = table_objet($table_liee,$serveur);
166 $desc_arrivee = $trouver_table($table_arrivee,$serveur);
167 // cas simple : $cle_depart dans la table_liee
168 if (isset($desc_arrivee['field'][$cle_depart])){
169 $s = sql_select("$cle_depart, $cle_arrivee", $desc_arrivee['table_sql'], sql_in($cle_arrivee, array_keys($ids_trouves)), '','','','',$serveur);
171 // cas simple : $cle_arrivee dans la table
172 elseif (isset($desc_depart['field'][$cle_arrivee])){
173 $s = sql_select("$cle_depart, $cle_arrivee", $desc_depart['table_sql'], sql_in($cle_arrivee, array_keys($ids_trouves)), '','','','',$serveur);
175 // sinon cherchons une table de liaison
176 // cas recherche principale article, objet lie document : passer par spip_documents_liens
177 elseif ($l = objet_associable($table_liee)){
178 list($primary, $table_liens) = $l;
179 $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);
181 // cas recherche principale auteur, objet lie article: passer par spip_auteurs_liens
182 elseif ($l = $depart_associable){
183 list($primary, $table_liens) = $l;
184 $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);
186 // cas table de liaison generique spip_xxx_yyy
187 elseif($t=$trouver_table($table_arrivee."_".$table_depart,$serveur)
188 OR $t=$trouver_table($table_depart."_".$table_arrivee,$serveur)){
189 $s = sql_select("$cle_depart,$cle_arrivee", $t["table_sql"], sql_in($cle_arrivee, array_keys($ids_trouves)), '','','','',$serveur);
193 list($cle_depart,$cle_arrivee,$s) = $rechercher_joints($table,$table_liee,array_keys($ids_trouves), $serveur);
195 while ($t = is_array($s)?
array_shift($s):sql_fetch($s)) {
196 $id = $t[$cle_depart];
197 $joint = $ids_trouves[$t[$cle_arrivee]];
198 if (!isset($results))
200 if (!isset($results[$id]))
201 $results[$id] = array();
202 if (isset($joint['score']) and $joint['score']) {
203 $results[$id]['score'] +
= $joint['score'];
205 if (isset($joint['champs']) and $joint['champs']) {
206 foreach($joint['champs'] as $c => $val) {
207 $results[$id]['champs'][$table_liee.'.'.$c] = $val;
210 if (isset($joint['matches']) and $joint['matches']) {
211 foreach($joint['matches'] as $c => $val) {
212 $results[$id]['matches'][$table_liee.'.'.$c] = $val;