[SPIP] ~maj v3.0.14-->v3.0.17
[ptitvelo/web/www.git] / www / ecrire / inc / recherche_to_array.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2014 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
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 \***************************************************************************/
12
13
14 if (!defined('_ECRIRE_INC_VERSION')) return;
15
16
17 // methodes sql
18 function inc_recherche_to_array_dist($recherche, $options = array()) {
19
20 // options par defaut
21 $options = array_merge(
22 array(
23 'score' => true,
24 'champs' => false,
25 'toutvoir' => false,
26 'matches' => false,
27 'jointures' => false
28 ),
29 $options
30 );
31
32 include_spip('inc/rechercher');
33 include_spip('inc/autoriser');
34
35 $requete = array(
36 "SELECT"=>array(),
37 "FROM"=>array(),
38 "WHERE"=>array(),
39 "GROUPBY"=>array(),
40 "ORDERBY"=>array(),
41 "LIMIT"=>"",
42 "HAVING"=>array()
43 );
44
45 $table = sinon($options['table'], 'article');
46 if ($options['champs'])
47 $champs = $options['champs'];
48 else {
49 $l = liste_des_champs();
50 $champs = $l['article'];
51 }
52 $serveur = $options['serveur'];
53
54 list($methode, $q, $preg) = expression_recherche($recherche, $options);
55
56 $jointures = $options['jointures']
57 ? liste_des_jointures()
58 : array();
59
60 $_id_table = id_table_objet($table);
61
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);
67 }
68
69 $requete['SELECT'][] = "t.".$_id_table;
70 $a = array();
71 // Recherche fulltext
72 foreach ($champs as $champ => $poids) {
73 if (is_array($champ)){
74 spip_log("requetes imbriquees interdites");
75 } else {
76 if (strpos($champ,".")===FALSE)
77 $champ = "t.$champ";
78 $requete['SELECT'][] = $champ;
79 $a[] = $champ.' '.$methode.' '.$q;
80 }
81 }
82 if ($a) $requete['WHERE'][] = join(" OR ", $a);
83 $requete['FROM'][] = table_objet_sql($table).' AS t';
84
85 $results = array();
86
87 $s = sql_select(
88 $requete['SELECT'], $requete['FROM'], $requete['WHERE'],
89 implode(" ",$requete['GROUPBY']),
90 $requete['ORDERBY'], $requete['LIMIT'],
91 $requete['HAVING'], $serveur
92 );
93
94 while ($t = sql_fetch($s,$serveur)
95 AND (!isset($t['score']) OR $t['score']>0)) {
96 $id = intval($t[$_id_table]);
97
98 if ($options['toutvoir']
99 OR autoriser('voir', $table, $id)) {
100 // indiquer les champs concernes
101 $champs_vus = array();
102 $score = 0;
103 $matches = array();
104
105 $vu = false;
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]));
111 if ($n =
112 ($options['score'] || $options['matches'])
113 ? preg_match_all($preg, $value, $regs, PREG_SET_ORDER)
114 : preg_match($preg, $value)
115 ) {
116 $vu = true;
117
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;
124
125 if (!$options['champs']
126 AND !$options['score']
127 AND !$options['matches'])
128 break;
129 }
130 }
131
132 if ($vu) {
133 if (!isset($results))
134 $results = array();
135 $results[$id] = array();
136 if ($champs_vus)
137 $results[$id]['champs'] = $champs_vus;
138 if ($score)
139 $results[$id]['score'] = $score;
140 if ($matches)
141 $results[$id]['matches'] = $matches;
142 }
143 }
144 }
145
146
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(
152 $recherche,
153 $jointures[$table],
154 array_merge($options, array('jointures' => false))
155 )
156 ) {
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
165 if (
166 !(
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)
170 )
171 ){
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);
178 }
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);
182 }
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);
188 }
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);
193 }
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);
198 }
199 }
200 else
201 list($cle_depart,$cle_arrivee,$s) = $rechercher_joints($table,$table_liee,array_keys($ids_trouves), $serveur);
202
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))
207 $results = array();
208 if (!isset($results[$id]))
209 $results[$id] = array();
210 if (isset($joint['score']) and $joint['score']) {
211 $results[$id]['score'] += $joint['score'];
212 }
213 if (isset($joint['champs']) and $joint['champs']) {
214 foreach($joint['champs'] as $c => $val) {
215 $results[$id]['champs'][$table_liee.'.'.$c] = $val;
216 }
217 }
218 if (isset($joint['matches']) and $joint['matches']) {
219 foreach($joint['matches'] as $c => $val) {
220 $results[$id]['matches'][$table_liee.'.'.$c] = $val;
221 }
222 }
223 }
224 }
225 }
226
227 return $results;
228 }
229
230
231 ?>