9e37e63bcce26942f30f4c03c78872e6339d1315
[lhc/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-2017 *
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')) {
15 return;
16 }
17
18
19 // methodes sql
20 function inc_recherche_to_array_dist($recherche, $options = array()) {
21
22 // options par defaut
23 $options = array_merge(
24 array(
25 'score' => true,
26 'champs' => false,
27 'toutvoir' => false,
28 'matches' => false,
29 'jointures' => false
30 ),
31 $options
32 );
33
34 include_spip('inc/rechercher');
35 include_spip('inc/autoriser');
36
37 $requete = array(
38 "SELECT" => array(),
39 "FROM" => array(),
40 "WHERE" => array(),
41 "GROUPBY" => array(),
42 "ORDERBY" => array(),
43 "LIMIT" => "",
44 "HAVING" => array()
45 );
46
47 $table = sinon($options['table'], 'article');
48 if ($options['champs']) {
49 $champs = $options['champs'];
50 } else {
51 $l = liste_des_champs();
52 $champs = $l['article'];
53 }
54 $serveur = $options['serveur'];
55
56 list($methode, $q, $preg) = expression_recherche($recherche, $options);
57
58 $jointures = $options['jointures']
59 ? liste_des_jointures()
60 : array();
61
62 $_id_table = id_table_objet($table);
63
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);
69 }
70
71 $requete['SELECT'][] = "t." . $_id_table;
72 $a = array();
73 // Recherche fulltext
74 foreach ($champs as $champ => $poids) {
75 if (is_array($champ)) {
76 spip_log("requetes imbriquees interdites");
77 } else {
78 if (strpos($champ, ".") === false) {
79 $champ = "t.$champ";
80 }
81 $requete['SELECT'][] = $champ;
82 $a[] = $champ . ' ' . $methode . ' ' . $q;
83 }
84 }
85 if ($a) {
86 $requete['WHERE'][] = join(" OR ", $a);
87 }
88 $requete['FROM'][] = table_objet_sql($table) . ' AS t';
89
90 $results = array();
91
92 $s = sql_select(
93 $requete['SELECT'], $requete['FROM'], $requete['WHERE'],
94 implode(" ", $requete['GROUPBY']),
95 $requete['ORDERBY'], $requete['LIMIT'],
96 $requete['HAVING'], $serveur
97 );
98
99 while ($t = sql_fetch($s, $serveur)
100 and (!isset($t['score']) or $t['score'] > 0)) {
101 $id = intval($t[$_id_table]);
102
103 if ($options['toutvoir']
104 or autoriser('voir', $table, $id)
105 ) {
106 // indiquer les champs concernes
107 $champs_vus = array();
108 $score = 0;
109 $matches = array();
110
111 $vu = false;
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]));
117 if ($n =
118 ($options['score'] || $options['matches'])
119 ? preg_match_all($preg, $value, $regs, PREG_SET_ORDER)
120 : preg_match($preg, $value)
121 ) {
122 $vu = true;
123
124 if ($options['champs']) {
125 $champs_vus[$champ] = $t[$champ];
126 }
127 if ($options['score']) {
128 $score += $n * $poids;
129 }
130 if ($options['matches']) {
131 $matches[$champ] = $regs;
132 }
133
134 if (!$options['champs']
135 and !$options['score']
136 and !$options['matches']
137 ) {
138 break;
139 }
140 }
141 }
142
143 if ($vu) {
144 if (!isset($results)) {
145 $results = array();
146 }
147 $results[$id] = array();
148 if ($champs_vus) {
149 $results[$id]['champs'] = $champs_vus;
150 }
151 if ($score) {
152 $results[$id]['score'] = $score;
153 }
154 if ($matches) {
155 $results[$id]['matches'] = $matches;
156 }
157 }
158 }
159 }
160
161
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(
167 $recherche,
168 $jointures[$table],
169 array_merge($options, array('jointures' => false))
170 )
171 ) {
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
180 if (
181 !(
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)
185 )
186 ) {
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);
198 }
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)
213 ) {
214 $s = sql_select("$cle_depart,$cle_arrivee", $t["table_sql"], sql_in($cle_arrivee, array_keys($ids_trouves)),
215 '', '', '', '', $serveur);
216 }
217 } else {
218 list($cle_depart, $cle_arrivee, $s) = $rechercher_joints($table, $table_liee, array_keys($ids_trouves),
219 $serveur);
220 }
221
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)) {
226 $results = array();
227 }
228 if (!isset($results[$id])) {
229 $results[$id] = array();
230 }
231 if (isset($joint['score']) and $joint['score']) {
232 if (!isset($results[$id]['score'])) {
233 $results[$id]['score'] = 0;
234 }
235 $results[$id]['score'] += $joint['score'];
236 }
237 if (isset($joint['champs']) and $joint['champs']) {
238 foreach ($joint['champs'] as $c => $val) {
239 $results[$id]['champs'][$table_liee . '.' . $c] = $val;
240 }
241 }
242 if (isset($joint['matches']) and $joint['matches']) {
243 foreach ($joint['matches'] as $c => $val) {
244 $results[$id]['matches'][$table_liee . '.' . $c] = $val;
245 }
246 }
247 }
248 }
249 }
250
251 return $results;
252 }