3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2012 *
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;
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() {
22 if (is_null($liste)) {
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'];
32 // puis passer dans le pipeline
33 $liste = pipeline('rechercher_liste_des_champs', $liste);
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() {
44 if (is_null($liste)) {
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'];
54 // puis passer dans le pipeline
55 $liste = pipeline('rechercher_liste_des_jointures', $liste);
60 function expression_recherche($recherche, $options) {
61 $u = $GLOBALS['meta']['pcre_u'];
62 include_spip('inc/charsets');
63 $recherche = trim(translitteration($recherche));
66 if (substr($recherche,0,1)=='/' AND substr($recherche,-1,1)=='/'){
68 $preg = $recherche.$options['preg_flags'];
72 // s'il y a plusieurs mots il faut les chercher tous : oblige REGEXP
73 if (preg_match(",\s+,".$u, $recherche)){
75 $recherche = preg_replace(',\s+,'.$u, '|', $recherche);
78 $preg = '/'.str_replace('/', '\\/', $recherche).'/' . $options['preg_flags'];
81 // Si la chaine est inactive, on va utiliser LIKE pour aller plus vite
82 // ou si l'expression reguliere est invalide
84 OR (@preg_match
($preg,'')===FALSE) ) {
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('\(','\)','[?]', '\[', '\]', '\+', '\*', '\/'),
92 $recherche_mod = $recherche;
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);
112 . preg_replace(",\s+,".$u, "%", $q)
116 $preg = '/'.preg_replace(",\s+,".$u, ".+", trim($recherche_mod)).'/' . $options['preg_flags'];
120 $q = sql_quote(trim($recherche, '/'));
123 return array($methode, $q, $preg);
127 // Effectue une recherche sur toutes les tables de la base de donnees
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');
138 if (!is_array($tables)) {
139 $liste = liste_des_champs();
141 if (is_string($tables)
144 foreach(explode(',', $tables) as $t)
145 if (isset($liste[$t]))
146 $toutes[$t] = $liste[$t];
153 if (!strlen($recherche) OR !count($tables))
156 include_spip('inc/autoriser');
158 // options par defaut
159 $options = array_merge(array(
160 'preg_flags' => 'UimsS',
165 'jointures' => false,
166 'serveur' => $serveur
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 => { } },
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);
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))
193 ##var_dump($results[$table]);
196 spip_log("recherche $table ($recherche) : ".count($results[$table])." resultats ".spip_timer('rech'),'recherche');
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');
211 // options par defaut
212 $options = array_merge(array(
213 'preg_flags' => 'UimsS',
214 'toutmodifier' => false
218 $options['champs'] = true;
221 if (!is_array($tables))
222 $tables = liste_des_champs();
224 $results = recherche_en_base($recherche, $tables, $options);
226 $preg = '/'.str_replace('/', '\\/', $recherche).'/' . $options['preg_flags'];
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)) {
234 foreach ($x['champs'] as $key => $val) {
235 if ($key == $_id_table) next
;
236 $repl = preg_replace($preg, $remplace, $val);
238 $modifs[$key] = $repl;
241 objet_modifier_champs($table, $id,
243 'champs' => array_keys($modifs),