[SPIP] ~maj 3.0.10 --> 3.0.14
[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-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 $requete['SELECT'][] = "t.".$_id_table;
62 $a = array();
63 // Recherche fulltext
64 foreach ($champs as $champ => $poids) {
65 if (is_array($champ)){
66 spip_log("requetes imbriquees interdites");
67 } else {
68 if (strpos($champ,".")===FALSE)
69 $champ = "t.$champ";
70 $requete['SELECT'][] = $champ;
71 $a[] = $champ.' '.$methode.' '.$q;
72 }
73 }
74 if ($a) $requete['WHERE'][] = join(" OR ", $a);
75 $requete['FROM'][] = table_objet_sql($table).' AS t';
76
77 $results = array();
78
79 $s = sql_select(
80 $requete['SELECT'], $requete['FROM'], $requete['WHERE'],
81 implode(" ",$requete['GROUPBY']),
82 $requete['ORDERBY'], $requete['LIMIT'],
83 $requete['HAVING'], $serveur
84 );
85
86 while ($t = sql_fetch($s,$serveur)
87 AND (!isset($t['score']) OR $t['score']>0)) {
88 $id = intval($t[$_id_table]);
89
90 if ($options['toutvoir']
91 OR autoriser('voir', $table, $id)) {
92 // indiquer les champs concernes
93 $champs_vus = array();
94 $score = 0;
95 $matches = array();
96
97 $vu = false;
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]));
103 if ($n =
104 ($options['score'] || $options['matches'])
105 ? preg_match_all($preg, $value, $regs, PREG_SET_ORDER)
106 : preg_match($preg, $value)
107 ) {
108 $vu = true;
109
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;
116
117 if (!$options['champs']
118 AND !$options['score']
119 AND !$options['matches'])
120 break;
121 }
122 }
123
124 if ($vu) {
125 if (!isset($results))
126 $results = array();
127 $results[$id] = array();
128 if ($champs_vus)
129 $results[$id]['champs'] = $champs_vus;
130 if ($score)
131 $results[$id]['score'] = $score;
132 if ($matches)
133 $results[$id]['matches'] = $matches;
134 }
135 }
136 }
137
138
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(
144 $recherche,
145 $jointures[$table],
146 array_merge($options, array('jointures' => false))
147 )
148 ) {
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
157 if (
158 !(
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)
162 )
163 ){
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);
170 }
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);
174 }
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);
180 }
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);
185 }
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);
190 }
191 }
192 else
193 list($cle_depart,$cle_arrivee,$s) = $rechercher_joints($table,$table_liee,array_keys($ids_trouves), $serveur);
194
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))
199 $results = array();
200 if (!isset($results[$id]))
201 $results[$id] = array();
202 if (isset($joint['score']) and $joint['score']) {
203 $results[$id]['score'] += $joint['score'];
204 }
205 if (isset($joint['champs']) and $joint['champs']) {
206 foreach($joint['champs'] as $c => $val) {
207 $results[$id]['champs'][$table_liee.'.'.$c] = $val;
208 }
209 }
210 if (isset($joint['matches']) and $joint['matches']) {
211 foreach($joint['matches'] as $c => $val) {
212 $results[$id]['matches'][$table_liee.'.'.$c] = $val;
213 }
214 }
215 }
216 }
217 }
218
219 return $results;
220 }
221
222
223 ?>