[SPIP] +installation version 3.0.10
[lhc/web/www.git] / www / ecrire / inc / rechercher.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2012 *
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 // Donne la liste des champs/tables ou l'on sait chercher/remplacer
18 // avec un poids pour le score
19 // http://doc.spip.org/@liste_des_champs
20 function liste_des_champs() {
21 static $liste=null;
22 if (is_null($liste)) {
23 $liste = array();
24 // recuperer les tables_objets_sql declarees
25 include_spip('base/objets');
26 $tables_objets = lister_tables_objets_sql();
27 foreach($tables_objets as $t=>$infos){
28 if ($infos['rechercher_champs']){
29 $liste[$infos['type']] = $infos['rechercher_champs'];
30 }
31 }
32 // puis passer dans le pipeline
33 $liste = pipeline('rechercher_liste_des_champs', $liste);
34 }
35 return $liste;
36 }
37
38
39 // Recherche des auteurs et mots-cles associes
40 // en ne regardant que le titre ou le nom
41 // http://doc.spip.org/@liste_des_jointures
42 function liste_des_jointures() {
43 static $liste=null;
44 if (is_null($liste)) {
45 $liste = array();
46 // recuperer les tables_objets_sql declarees
47 include_spip('base/objets');
48 $tables_objets = lister_tables_objets_sql();
49 foreach($tables_objets as $t=>$infos){
50 if ($infos['rechercher_jointures']){
51 $liste[$infos['type']] = $infos['rechercher_jointures'];
52 }
53 }
54 // puis passer dans le pipeline
55 $liste = pipeline('rechercher_liste_des_jointures', $liste);
56 }
57 return $liste;
58 }
59
60 function expression_recherche($recherche, $options) {
61 $u = $GLOBALS['meta']['pcre_u'];
62 include_spip('inc/charsets');
63 $recherche = trim(translitteration($recherche));
64
65 $is_preg = false;
66 if (substr($recherche,0,1)=='/' AND substr($recherche,-1,1)=='/'){
67 // c'est une preg
68 $preg = $recherche.$options['preg_flags'];
69 $is_preg = true;
70 }
71 else{
72 // s'il y a plusieurs mots il faut les chercher tous : oblige REGEXP
73 if (preg_match(",\s+,".$u, $recherche)){
74 $is_preg = true;
75 $recherche = preg_replace(',\s+,'.$u, '|', $recherche);
76 }
77
78 $preg = '/'.str_replace('/', '\\/', $recherche).'/' . $options['preg_flags'];
79 }
80
81 // Si la chaine est inactive, on va utiliser LIKE pour aller plus vite
82 // ou si l'expression reguliere est invalide
83 if (!$is_preg
84 OR (@preg_match($preg,'')===FALSE) ) {
85 $methode = 'LIKE';
86 $u = $GLOBALS['meta']['pcre_u'];
87 // eviter les parentheses et autres caractères qui interferent avec pcre par la suite (dans le preg_match_all) s'il y a des reponses
88 $recherche = str_replace(
89 array('(',')','?','[', ']', '+', '*', '/'),
90 array('\(','\)','[?]', '\[', '\]', '\+', '\*', '\/'),
91 $recherche);
92 $recherche_mod = $recherche;
93
94 // echapper les % et _
95 $q = str_replace(array('%','_'), array('\%', '\_'), trim($recherche));
96 // les expressions entre " " sont un mot a chercher tel quel
97 // -> on remplace les espaces par un _ et on enleve les guillemets
98 if (preg_match(',["][^"]+["],Uims',$q,$matches)){
99 foreach($matches as $match){
100 // corriger le like dans le $q
101 $word = preg_replace(",\s+,Uims","_",$match);
102 $word = trim($word,'"');
103 $q = str_replace($match,$word,$q);
104 // corriger la regexp
105 $word = preg_replace(",\s+,Uims","[\s]",$match);
106 $word = trim($word,'"');
107 $recherche_mod = str_replace($match,$word,$recherche_mod);
108 }
109 }
110 $q = sql_quote(
111 "%"
112 . preg_replace(",\s+,".$u, "%", $q)
113 . "%"
114 );
115
116 $preg = '/'.preg_replace(",\s+,".$u, ".+", trim($recherche_mod)).'/' . $options['preg_flags'];
117
118 } else {
119 $methode = 'REGEXP';
120 $q = sql_quote(trim($recherche, '/'));
121 }
122
123 return array($methode, $q, $preg);
124 }
125
126
127 // Effectue une recherche sur toutes les tables de la base de donnees
128 // options :
129 // - toutvoir pour eviter autoriser(voir)
130 // - flags pour eviter les flags regexp par defaut (UimsS)
131 // - champs pour retourner les champs concernes
132 // - score pour retourner un score
133 // On peut passer les tables, ou une chaine listant les tables souhaitees
134 // http://doc.spip.org/@recherche_en_base
135 function recherche_en_base($recherche='', $tables=NULL, $options=array(), $serveur='') {
136 include_spip('base/abstract_sql');
137
138 if (!is_array($tables)) {
139 $liste = liste_des_champs();
140
141 if (is_string($tables)
142 AND $tables != '') {
143 $toutes = array();
144 foreach(explode(',', $tables) as $t)
145 if (isset($liste[$t]))
146 $toutes[$t] = $liste[$t];
147 $tables = $toutes;
148 unset($toutes);
149 } else
150 $tables = $liste;
151 }
152
153 if (!strlen($recherche) OR !count($tables))
154 return array();
155
156 include_spip('inc/autoriser');
157
158 // options par defaut
159 $options = array_merge(array(
160 'preg_flags' => 'UimsS',
161 'toutvoir' => false,
162 'champs' => false,
163 'score' => false,
164 'matches' => false,
165 'jointures' => false,
166 'serveur' => $serveur
167 ),
168 $options
169 );
170
171 $results = array();
172
173 // Utiliser l'iterateur (DATA:recherche)
174 // pour recuperer les couples (id_objet, score)
175 // Le resultat est au format {
176 // id1 = { 'score' => x, attrs => { } },
177 // id2 = { 'score' => x, attrs => { } },
178 // }
179 include_spip('inc/memoization');
180 foreach ($tables as $table => $champs) {
181 # lock via memoization, si dispo
182 if (function_exists('cache_lock'))
183 cache_lock($lock = 'recherche '.$table.' '.$recherche);
184
185 spip_timer('rech');
186
187 // TODO: ici plutot charger un iterateur via l'API iterateurs
188 include_spip('inc/recherche_to_array');
189 $to_array = charger_fonction('recherche_to_array', 'inc');
190 $results[$table] = $to_array($recherche,
191 array_merge($options, array('table' => $table, 'champs' => $champs))
192 );
193 ##var_dump($results[$table]);
194
195
196 spip_log("recherche $table ($recherche) : ".count($results[$table])." resultats ".spip_timer('rech'),'recherche');
197
198 if (isset($lock))
199 cache_unlock($lock);
200 }
201
202 return $results;
203 }
204
205
206 // Effectue une recherche sur toutes les tables de la base de donnees
207 // http://doc.spip.org/@remplace_en_base
208 function remplace_en_base($recherche='', $remplace=NULL, $tables=NULL, $options=array()) {
209 include_spip('inc/modifier');
210
211 // options par defaut
212 $options = array_merge(array(
213 'preg_flags' => 'UimsS',
214 'toutmodifier' => false
215 ),
216 $options
217 );
218 $options['champs'] = true;
219
220
221 if (!is_array($tables))
222 $tables = liste_des_champs();
223
224 $results = recherche_en_base($recherche, $tables, $options);
225
226 $preg = '/'.str_replace('/', '\\/', $recherche).'/' . $options['preg_flags'];
227
228 foreach ($results as $table => $r) {
229 $_id_table = id_table_objet($table);
230 foreach ($r as $id => $x) {
231 if ($options['toutmodifier']
232 OR autoriser('modifier', $table, $id)) {
233 $modifs = array();
234 foreach ($x['champs'] as $key => $val) {
235 if ($key == $_id_table) next;
236 $repl = preg_replace($preg, $remplace, $val);
237 if ($repl <> $val)
238 $modifs[$key] = $repl;
239 }
240 if ($modifs)
241 objet_modifier_champs($table, $id,
242 array(
243 'champs' => array_keys($modifs),
244 ),
245 $modifs);
246 }
247 }
248 }
249 }
250
251 ?>