@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | **/ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | defined('_RECHERCHE_LOCK_KEY') || define('_RECHERCHE_LOCK_KEY', 'fulltext'); |
@@ -35,22 +35,22 @@ discard block |
||
| 35 | 35 | * @return array Couples (type d'objet => Couples (champ => score)) |
| 36 | 36 | */ |
| 37 | 37 | function liste_des_champs() { |
| 38 | - static $liste = null; |
|
| 39 | - if (is_null($liste)) { |
|
| 40 | - $liste = array(); |
|
| 41 | - // recuperer les tables_objets_sql declarees |
|
| 42 | - include_spip('base/objets'); |
|
| 43 | - $tables_objets = lister_tables_objets_sql(); |
|
| 44 | - foreach ($tables_objets as $t => $infos) { |
|
| 45 | - if ($infos['rechercher_champs']) { |
|
| 46 | - $liste[$infos['type']] = $infos['rechercher_champs']; |
|
| 47 | - } |
|
| 48 | - } |
|
| 49 | - // puis passer dans le pipeline |
|
| 50 | - $liste = pipeline('rechercher_liste_des_champs', $liste); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - return $liste; |
|
| 38 | + static $liste = null; |
|
| 39 | + if (is_null($liste)) { |
|
| 40 | + $liste = array(); |
|
| 41 | + // recuperer les tables_objets_sql declarees |
|
| 42 | + include_spip('base/objets'); |
|
| 43 | + $tables_objets = lister_tables_objets_sql(); |
|
| 44 | + foreach ($tables_objets as $t => $infos) { |
|
| 45 | + if ($infos['rechercher_champs']) { |
|
| 46 | + $liste[$infos['type']] = $infos['rechercher_champs']; |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | + // puis passer dans le pipeline |
|
| 50 | + $liste = pipeline('rechercher_liste_des_champs', $liste); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + return $liste; |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | |
@@ -58,174 +58,174 @@ discard block |
||
| 58 | 58 | // en ne regardant que le titre ou le nom |
| 59 | 59 | // https://code.spip.net/@liste_des_jointures |
| 60 | 60 | function liste_des_jointures() { |
| 61 | - static $liste = null; |
|
| 62 | - if (is_null($liste)) { |
|
| 63 | - $liste = array(); |
|
| 64 | - // recuperer les tables_objets_sql declarees |
|
| 65 | - include_spip('base/objets'); |
|
| 66 | - $tables_objets = lister_tables_objets_sql(); |
|
| 67 | - foreach ($tables_objets as $t => $infos) { |
|
| 68 | - if ($infos['rechercher_jointures']) { |
|
| 69 | - $liste[$infos['type']] = $infos['rechercher_jointures']; |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - // puis passer dans le pipeline |
|
| 73 | - $liste = pipeline('rechercher_liste_des_jointures', $liste); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - return $liste; |
|
| 61 | + static $liste = null; |
|
| 62 | + if (is_null($liste)) { |
|
| 63 | + $liste = array(); |
|
| 64 | + // recuperer les tables_objets_sql declarees |
|
| 65 | + include_spip('base/objets'); |
|
| 66 | + $tables_objets = lister_tables_objets_sql(); |
|
| 67 | + foreach ($tables_objets as $t => $infos) { |
|
| 68 | + if ($infos['rechercher_jointures']) { |
|
| 69 | + $liste[$infos['type']] = $infos['rechercher_jointures']; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + // puis passer dans le pipeline |
|
| 73 | + $liste = pipeline('rechercher_liste_des_jointures', $liste); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + return $liste; |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | function expression_recherche($recherche, $options) { |
| 80 | - // ne calculer qu'une seule fois l'expression par hit |
|
| 81 | - // (meme si utilisee dans plusieurs boucles) |
|
| 82 | - static $expression = array(); |
|
| 83 | - $key = serialize(array($recherche, $options['preg_flags'])); |
|
| 84 | - if (isset($expression[$key])) { |
|
| 85 | - return $expression[$key]; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - $u = $GLOBALS['meta']['pcre_u']; |
|
| 89 | - if ($u and strpos($options['preg_flags'], $u) === false) { |
|
| 90 | - $options['preg_flags'] .= $u; |
|
| 91 | - } |
|
| 92 | - include_spip('inc/charsets'); |
|
| 93 | - $recherche = trim($recherche); |
|
| 94 | - |
|
| 95 | - // retirer les + de +truc et les * de truc* |
|
| 96 | - $recherche = preg_replace(',(^|\s)\+(\w),Uims', '$1$2', $recherche); |
|
| 97 | - $recherche = preg_replace(',(\w)\*($|\s),Uims', '$1$2', $recherche); |
|
| 98 | - |
|
| 99 | - $is_preg = false; |
|
| 100 | - if (substr($recherche, 0, 1) == '/' and substr($recherche, -1, 1) == '/' and strlen($recherche) > 2) { |
|
| 101 | - // c'est une preg |
|
| 102 | - $recherche_trans = translitteration($recherche); |
|
| 103 | - $preg = $recherche_trans . $options['preg_flags']; |
|
| 104 | - $is_preg = true; |
|
| 105 | - } else { |
|
| 106 | - // s'il y a plusieurs mots il faut les chercher tous : oblige REGEXP, |
|
| 107 | - // sauf ceux de moins de 4 lettres (on supprime ainsi 'le', 'les', 'un', |
|
| 108 | - // 'une', 'des' ...) |
|
| 109 | - |
|
| 110 | - // attention : plusieurs mots entre guillemets sont a rechercher tels quels |
|
| 111 | - $recherche_trans = $recherche_mod = $recherche_org = $recherche; |
|
| 112 | - |
|
| 113 | - // les expressions entre " " sont un mot a chercher tel quel |
|
| 114 | - // -> on remplace les espaces par un \x1 et on enleve les guillemets |
|
| 115 | - if (preg_match(',["][^"]+["],Uims', $recherche_mod, $matches)) { |
|
| 116 | - foreach ($matches as $match) { |
|
| 117 | - $word = preg_replace(",\s+,Uims", "\x1", $match); |
|
| 118 | - $word = trim($word, '"'); |
|
| 119 | - $recherche_mod = str_replace($match, $word, $recherche_mod); |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - if (preg_match(",\s+," . $u, $recherche_mod)) { |
|
| 124 | - $is_preg = true; |
|
| 125 | - |
|
| 126 | - $recherche_inter = '|'; |
|
| 127 | - $recherche_mots = explode(' ', $recherche_mod); |
|
| 128 | - $min_long = defined('_RECHERCHE_MIN_CAR') ? _RECHERCHE_MIN_CAR : 4; |
|
| 129 | - $petits_mots = true; |
|
| 130 | - foreach ($recherche_mots as $mot) { |
|
| 131 | - if (strlen($mot) >= $min_long) { |
|
| 132 | - // echapper les caracteres de regexp qui sont eventuellement dans la recherche |
|
| 133 | - $recherche_inter .= preg_quote($mot) . ' '; |
|
| 134 | - $petits_mots = false; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - $recherche_inter = str_replace("\x1", '\s', $recherche_inter); |
|
| 138 | - |
|
| 139 | - // mais on cherche quand même l'expression complète, même si elle |
|
| 140 | - // comporte des mots de moins de quatre lettres |
|
| 141 | - $recherche = trim(preg_replace(',\s+,' . $u, '|', $recherche_inter), '|'); |
|
| 142 | - if (!$recherche or $petits_mots){ |
|
| 143 | - $recherche = preg_quote($recherche_org); |
|
| 144 | - } |
|
| 145 | - $recherche_trans = translitteration($recherche); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $preg = '/' . str_replace('/', '\\/', $recherche_trans) . '/' . $options['preg_flags']; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - // Si la chaine est inactive, on va utiliser LIKE pour aller plus vite |
|
| 152 | - // ou si l'expression reguliere est invalide |
|
| 153 | - if (!$is_preg |
|
| 154 | - or (@preg_match($preg, '') === false) |
|
| 155 | - ) { |
|
| 156 | - $methode = 'LIKE'; |
|
| 157 | - $u = $GLOBALS['meta']['pcre_u']; |
|
| 158 | - |
|
| 159 | - // echapper les % et _ |
|
| 160 | - $q = str_replace(array('%', '_'), array('\%', '\_'), trim($recherche)); |
|
| 161 | - |
|
| 162 | - // 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 |
|
| 163 | - $recherche = preg_quote($recherche, '/'); |
|
| 164 | - $recherche_trans = translitteration($recherche); |
|
| 165 | - $recherche_mod = $recherche_trans; |
|
| 166 | - |
|
| 167 | - // les expressions entre " " sont un mot a chercher tel quel |
|
| 168 | - // -> on remplace les espaces par un _ et on enleve les guillemets |
|
| 169 | - // corriger le like dans le $q |
|
| 170 | - if (preg_match(',["][^"]+["],Uims', $q, $matches)) { |
|
| 171 | - foreach ($matches as $match) { |
|
| 172 | - $word = preg_replace(",\s+,Uims", "_", $match); |
|
| 173 | - $word = trim($word, '"'); |
|
| 174 | - $q = str_replace($match, $word, $q); |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - // corriger la regexp |
|
| 178 | - if (preg_match(',["][^"]+["],Uims', $recherche_mod, $matches)) { |
|
| 179 | - foreach ($matches as $match) { |
|
| 180 | - $word = preg_replace(",\s+,Uims", "[\s]", $match); |
|
| 181 | - $word = trim($word, '"'); |
|
| 182 | - $recherche_mod = str_replace($match, $word, $recherche_mod); |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - $q = sql_quote( |
|
| 186 | - "%" |
|
| 187 | - . preg_replace(",\s+," . $u, "%", $q) |
|
| 188 | - . "%" |
|
| 189 | - ); |
|
| 190 | - |
|
| 191 | - $preg = '/' . preg_replace(",\s+," . $u, ".+", trim($recherche_mod)) . '/' . $options['preg_flags']; |
|
| 192 | - |
|
| 193 | - } else { |
|
| 194 | - $methode = 'REGEXP'; |
|
| 195 | - $q = sql_quote(trim($recherche, '/')); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - // tous les caracteres transliterables de $q sont remplaces par un joker |
|
| 199 | - // permet de matcher en SQL meme si on est sensible aux accents (SQLite) |
|
| 200 | - $q_t = $q; |
|
| 201 | - for ($i = 0; $i < spip_strlen($q); $i++) { |
|
| 202 | - $char = spip_substr($q, $i, 1); |
|
| 203 | - if (!is_ascii($char) |
|
| 204 | - and $char_t = translitteration($char) |
|
| 205 | - and $char_t !== $char |
|
| 206 | - ) { |
|
| 207 | - // on utilise ..?.? car le char utf peut etre encode sur 1, 2 ou 3 bytes |
|
| 208 | - // mais c'est un pis aller cf #4354 |
|
| 209 | - $q_t = str_replace($char, $is_preg ? "..?.?" : "_", $q_t); |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - $q = $q_t; |
|
| 214 | - |
|
| 215 | - // fix : SQLite 3 est sensible aux accents, on jokerise les caracteres |
|
| 216 | - // les plus frequents qui peuvent etre accentues |
|
| 217 | - // (oui c'est tres dicustable...) |
|
| 218 | - if (isset($GLOBALS['connexions'][$options['serveur'] ? $options['serveur'] : 0]['type']) |
|
| 219 | - and strncmp($GLOBALS['connexions'][$options['serveur'] ? $options['serveur'] : 0]['type'], 'sqlite', 6) == 0 |
|
| 220 | - ) { |
|
| 221 | - $q_t = strtr($q, "aeuioc", $is_preg ? "......" : "______"); |
|
| 222 | - // si il reste au moins un char significatif... |
|
| 223 | - if (preg_match(",[^'%_.],", $q_t)) { |
|
| 224 | - $q = $q_t; |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - return $expression[$key] = array($methode, $q, $preg); |
|
| 80 | + // ne calculer qu'une seule fois l'expression par hit |
|
| 81 | + // (meme si utilisee dans plusieurs boucles) |
|
| 82 | + static $expression = array(); |
|
| 83 | + $key = serialize(array($recherche, $options['preg_flags'])); |
|
| 84 | + if (isset($expression[$key])) { |
|
| 85 | + return $expression[$key]; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + $u = $GLOBALS['meta']['pcre_u']; |
|
| 89 | + if ($u and strpos($options['preg_flags'], $u) === false) { |
|
| 90 | + $options['preg_flags'] .= $u; |
|
| 91 | + } |
|
| 92 | + include_spip('inc/charsets'); |
|
| 93 | + $recherche = trim($recherche); |
|
| 94 | + |
|
| 95 | + // retirer les + de +truc et les * de truc* |
|
| 96 | + $recherche = preg_replace(',(^|\s)\+(\w),Uims', '$1$2', $recherche); |
|
| 97 | + $recherche = preg_replace(',(\w)\*($|\s),Uims', '$1$2', $recherche); |
|
| 98 | + |
|
| 99 | + $is_preg = false; |
|
| 100 | + if (substr($recherche, 0, 1) == '/' and substr($recherche, -1, 1) == '/' and strlen($recherche) > 2) { |
|
| 101 | + // c'est une preg |
|
| 102 | + $recherche_trans = translitteration($recherche); |
|
| 103 | + $preg = $recherche_trans . $options['preg_flags']; |
|
| 104 | + $is_preg = true; |
|
| 105 | + } else { |
|
| 106 | + // s'il y a plusieurs mots il faut les chercher tous : oblige REGEXP, |
|
| 107 | + // sauf ceux de moins de 4 lettres (on supprime ainsi 'le', 'les', 'un', |
|
| 108 | + // 'une', 'des' ...) |
|
| 109 | + |
|
| 110 | + // attention : plusieurs mots entre guillemets sont a rechercher tels quels |
|
| 111 | + $recherche_trans = $recherche_mod = $recherche_org = $recherche; |
|
| 112 | + |
|
| 113 | + // les expressions entre " " sont un mot a chercher tel quel |
|
| 114 | + // -> on remplace les espaces par un \x1 et on enleve les guillemets |
|
| 115 | + if (preg_match(',["][^"]+["],Uims', $recherche_mod, $matches)) { |
|
| 116 | + foreach ($matches as $match) { |
|
| 117 | + $word = preg_replace(",\s+,Uims", "\x1", $match); |
|
| 118 | + $word = trim($word, '"'); |
|
| 119 | + $recherche_mod = str_replace($match, $word, $recherche_mod); |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + if (preg_match(",\s+," . $u, $recherche_mod)) { |
|
| 124 | + $is_preg = true; |
|
| 125 | + |
|
| 126 | + $recherche_inter = '|'; |
|
| 127 | + $recherche_mots = explode(' ', $recherche_mod); |
|
| 128 | + $min_long = defined('_RECHERCHE_MIN_CAR') ? _RECHERCHE_MIN_CAR : 4; |
|
| 129 | + $petits_mots = true; |
|
| 130 | + foreach ($recherche_mots as $mot) { |
|
| 131 | + if (strlen($mot) >= $min_long) { |
|
| 132 | + // echapper les caracteres de regexp qui sont eventuellement dans la recherche |
|
| 133 | + $recherche_inter .= preg_quote($mot) . ' '; |
|
| 134 | + $petits_mots = false; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + $recherche_inter = str_replace("\x1", '\s', $recherche_inter); |
|
| 138 | + |
|
| 139 | + // mais on cherche quand même l'expression complète, même si elle |
|
| 140 | + // comporte des mots de moins de quatre lettres |
|
| 141 | + $recherche = trim(preg_replace(',\s+,' . $u, '|', $recherche_inter), '|'); |
|
| 142 | + if (!$recherche or $petits_mots){ |
|
| 143 | + $recherche = preg_quote($recherche_org); |
|
| 144 | + } |
|
| 145 | + $recherche_trans = translitteration($recherche); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $preg = '/' . str_replace('/', '\\/', $recherche_trans) . '/' . $options['preg_flags']; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + // Si la chaine est inactive, on va utiliser LIKE pour aller plus vite |
|
| 152 | + // ou si l'expression reguliere est invalide |
|
| 153 | + if (!$is_preg |
|
| 154 | + or (@preg_match($preg, '') === false) |
|
| 155 | + ) { |
|
| 156 | + $methode = 'LIKE'; |
|
| 157 | + $u = $GLOBALS['meta']['pcre_u']; |
|
| 158 | + |
|
| 159 | + // echapper les % et _ |
|
| 160 | + $q = str_replace(array('%', '_'), array('\%', '\_'), trim($recherche)); |
|
| 161 | + |
|
| 162 | + // 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 |
|
| 163 | + $recherche = preg_quote($recherche, '/'); |
|
| 164 | + $recherche_trans = translitteration($recherche); |
|
| 165 | + $recherche_mod = $recherche_trans; |
|
| 166 | + |
|
| 167 | + // les expressions entre " " sont un mot a chercher tel quel |
|
| 168 | + // -> on remplace les espaces par un _ et on enleve les guillemets |
|
| 169 | + // corriger le like dans le $q |
|
| 170 | + if (preg_match(',["][^"]+["],Uims', $q, $matches)) { |
|
| 171 | + foreach ($matches as $match) { |
|
| 172 | + $word = preg_replace(",\s+,Uims", "_", $match); |
|
| 173 | + $word = trim($word, '"'); |
|
| 174 | + $q = str_replace($match, $word, $q); |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + // corriger la regexp |
|
| 178 | + if (preg_match(',["][^"]+["],Uims', $recherche_mod, $matches)) { |
|
| 179 | + foreach ($matches as $match) { |
|
| 180 | + $word = preg_replace(",\s+,Uims", "[\s]", $match); |
|
| 181 | + $word = trim($word, '"'); |
|
| 182 | + $recherche_mod = str_replace($match, $word, $recherche_mod); |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + $q = sql_quote( |
|
| 186 | + "%" |
|
| 187 | + . preg_replace(",\s+," . $u, "%", $q) |
|
| 188 | + . "%" |
|
| 189 | + ); |
|
| 190 | + |
|
| 191 | + $preg = '/' . preg_replace(",\s+," . $u, ".+", trim($recherche_mod)) . '/' . $options['preg_flags']; |
|
| 192 | + |
|
| 193 | + } else { |
|
| 194 | + $methode = 'REGEXP'; |
|
| 195 | + $q = sql_quote(trim($recherche, '/')); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + // tous les caracteres transliterables de $q sont remplaces par un joker |
|
| 199 | + // permet de matcher en SQL meme si on est sensible aux accents (SQLite) |
|
| 200 | + $q_t = $q; |
|
| 201 | + for ($i = 0; $i < spip_strlen($q); $i++) { |
|
| 202 | + $char = spip_substr($q, $i, 1); |
|
| 203 | + if (!is_ascii($char) |
|
| 204 | + and $char_t = translitteration($char) |
|
| 205 | + and $char_t !== $char |
|
| 206 | + ) { |
|
| 207 | + // on utilise ..?.? car le char utf peut etre encode sur 1, 2 ou 3 bytes |
|
| 208 | + // mais c'est un pis aller cf #4354 |
|
| 209 | + $q_t = str_replace($char, $is_preg ? "..?.?" : "_", $q_t); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + $q = $q_t; |
|
| 214 | + |
|
| 215 | + // fix : SQLite 3 est sensible aux accents, on jokerise les caracteres |
|
| 216 | + // les plus frequents qui peuvent etre accentues |
|
| 217 | + // (oui c'est tres dicustable...) |
|
| 218 | + if (isset($GLOBALS['connexions'][$options['serveur'] ? $options['serveur'] : 0]['type']) |
|
| 219 | + and strncmp($GLOBALS['connexions'][$options['serveur'] ? $options['serveur'] : 0]['type'], 'sqlite', 6) == 0 |
|
| 220 | + ) { |
|
| 221 | + $q_t = strtr($q, "aeuioc", $is_preg ? "......" : "______"); |
|
| 222 | + // si il reste au moins un char significatif... |
|
| 223 | + if (preg_match(",[^'%_.],", $q_t)) { |
|
| 224 | + $q = $q_t; |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + return $expression[$key] = array($methode, $q, $preg); |
|
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | |
@@ -252,133 +252,133 @@ discard block |
||
| 252 | 252 | * @return array |
| 253 | 253 | */ |
| 254 | 254 | function recherche_en_base($recherche = '', $tables = null, $options = array(), $serveur = '') { |
| 255 | - include_spip('base/abstract_sql'); |
|
| 256 | - |
|
| 257 | - if (!is_array($tables)) { |
|
| 258 | - $liste = liste_des_champs(); |
|
| 259 | - |
|
| 260 | - if (is_string($tables) |
|
| 261 | - and $tables != '' |
|
| 262 | - ) { |
|
| 263 | - $toutes = array(); |
|
| 264 | - foreach (explode(',', $tables) as $t) { |
|
| 265 | - $t = trim($t); |
|
| 266 | - if (isset($liste[$t])) { |
|
| 267 | - $toutes[$t] = $liste[$t]; |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - $tables = $toutes; |
|
| 271 | - unset($toutes); |
|
| 272 | - } else { |
|
| 273 | - $tables = $liste; |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - if (!strlen($recherche) or !count($tables)) { |
|
| 278 | - return array(); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - include_spip('inc/autoriser'); |
|
| 282 | - |
|
| 283 | - // options par defaut |
|
| 284 | - $options = array_merge(array( |
|
| 285 | - 'preg_flags' => 'UimsS', |
|
| 286 | - 'toutvoir' => false, |
|
| 287 | - 'champs' => false, |
|
| 288 | - 'score' => false, |
|
| 289 | - 'matches' => false, |
|
| 290 | - 'jointures' => false, |
|
| 291 | - 'serveur' => $serveur |
|
| 292 | - ), |
|
| 293 | - $options |
|
| 294 | - ); |
|
| 295 | - |
|
| 296 | - $results = array(); |
|
| 297 | - |
|
| 298 | - // Utiliser l'iterateur (DATA:recherche) |
|
| 299 | - // pour recuperer les couples (id_objet, score) |
|
| 300 | - // Le resultat est au format { |
|
| 301 | - // id1 = { 'score' => x, attrs => { } }, |
|
| 302 | - // id2 = { 'score' => x, attrs => { } }, |
|
| 303 | - // } |
|
| 304 | - |
|
| 305 | - include_spip('inc/recherche_to_array'); |
|
| 306 | - |
|
| 307 | - foreach ($tables as $table => $champs) { |
|
| 308 | - # lock via memoization, si dispo |
|
| 309 | - if (function_exists('cache_lock')) { |
|
| 310 | - cache_lock($lock = _RECHERCHE_LOCK_KEY . ' ' . $table . ' ' . $recherche); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - spip_timer('rech'); |
|
| 314 | - |
|
| 315 | - # TODO : ici plutot charger un iterateur via l'API iterateurs |
|
| 316 | - $to_array = charger_fonction('recherche_to_array', 'inc'); |
|
| 317 | - $results[$table] = $to_array($recherche, |
|
| 318 | - array_merge($options, array('table' => $table, 'champs' => $champs)) |
|
| 319 | - ); |
|
| 320 | - ##var_dump($results[$table]); |
|
| 321 | - |
|
| 322 | - |
|
| 323 | - spip_log("recherche $table ($recherche) : " . count($results[$table]) . " resultats " . spip_timer('rech'), |
|
| 324 | - 'recherche'); |
|
| 325 | - |
|
| 326 | - if (isset($lock)) { |
|
| 327 | - cache_unlock($lock); |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - return $results; |
|
| 255 | + include_spip('base/abstract_sql'); |
|
| 256 | + |
|
| 257 | + if (!is_array($tables)) { |
|
| 258 | + $liste = liste_des_champs(); |
|
| 259 | + |
|
| 260 | + if (is_string($tables) |
|
| 261 | + and $tables != '' |
|
| 262 | + ) { |
|
| 263 | + $toutes = array(); |
|
| 264 | + foreach (explode(',', $tables) as $t) { |
|
| 265 | + $t = trim($t); |
|
| 266 | + if (isset($liste[$t])) { |
|
| 267 | + $toutes[$t] = $liste[$t]; |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + $tables = $toutes; |
|
| 271 | + unset($toutes); |
|
| 272 | + } else { |
|
| 273 | + $tables = $liste; |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + if (!strlen($recherche) or !count($tables)) { |
|
| 278 | + return array(); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + include_spip('inc/autoriser'); |
|
| 282 | + |
|
| 283 | + // options par defaut |
|
| 284 | + $options = array_merge(array( |
|
| 285 | + 'preg_flags' => 'UimsS', |
|
| 286 | + 'toutvoir' => false, |
|
| 287 | + 'champs' => false, |
|
| 288 | + 'score' => false, |
|
| 289 | + 'matches' => false, |
|
| 290 | + 'jointures' => false, |
|
| 291 | + 'serveur' => $serveur |
|
| 292 | + ), |
|
| 293 | + $options |
|
| 294 | + ); |
|
| 295 | + |
|
| 296 | + $results = array(); |
|
| 297 | + |
|
| 298 | + // Utiliser l'iterateur (DATA:recherche) |
|
| 299 | + // pour recuperer les couples (id_objet, score) |
|
| 300 | + // Le resultat est au format { |
|
| 301 | + // id1 = { 'score' => x, attrs => { } }, |
|
| 302 | + // id2 = { 'score' => x, attrs => { } }, |
|
| 303 | + // } |
|
| 304 | + |
|
| 305 | + include_spip('inc/recherche_to_array'); |
|
| 306 | + |
|
| 307 | + foreach ($tables as $table => $champs) { |
|
| 308 | + # lock via memoization, si dispo |
|
| 309 | + if (function_exists('cache_lock')) { |
|
| 310 | + cache_lock($lock = _RECHERCHE_LOCK_KEY . ' ' . $table . ' ' . $recherche); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + spip_timer('rech'); |
|
| 314 | + |
|
| 315 | + # TODO : ici plutot charger un iterateur via l'API iterateurs |
|
| 316 | + $to_array = charger_fonction('recherche_to_array', 'inc'); |
|
| 317 | + $results[$table] = $to_array($recherche, |
|
| 318 | + array_merge($options, array('table' => $table, 'champs' => $champs)) |
|
| 319 | + ); |
|
| 320 | + ##var_dump($results[$table]); |
|
| 321 | + |
|
| 322 | + |
|
| 323 | + spip_log("recherche $table ($recherche) : " . count($results[$table]) . " resultats " . spip_timer('rech'), |
|
| 324 | + 'recherche'); |
|
| 325 | + |
|
| 326 | + if (isset($lock)) { |
|
| 327 | + cache_unlock($lock); |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + return $results; |
|
| 332 | 332 | } |
| 333 | 333 | |
| 334 | 334 | |
| 335 | 335 | // Effectue une recherche sur toutes les tables de la base de donnees |
| 336 | 336 | // https://code.spip.net/@remplace_en_base |
| 337 | 337 | function remplace_en_base($recherche = '', $remplace = null, $tables = null, $options = array()) { |
| 338 | - include_spip('inc/modifier'); |
|
| 339 | - |
|
| 340 | - // options par defaut |
|
| 341 | - $options = array_merge(array( |
|
| 342 | - 'preg_flags' => 'UimsS', |
|
| 343 | - 'toutmodifier' => false |
|
| 344 | - ), |
|
| 345 | - $options |
|
| 346 | - ); |
|
| 347 | - $options['champs'] = true; |
|
| 348 | - |
|
| 349 | - |
|
| 350 | - if (!is_array($tables)) { |
|
| 351 | - $tables = liste_des_champs(); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - $results = recherche_en_base($recherche, $tables, $options); |
|
| 355 | - |
|
| 356 | - $preg = '/' . str_replace('/', '\\/', $recherche) . '/' . $options['preg_flags']; |
|
| 357 | - |
|
| 358 | - foreach ($results as $table => $r) { |
|
| 359 | - $_id_table = id_table_objet($table); |
|
| 360 | - foreach ($r as $id => $x) { |
|
| 361 | - if ($options['toutmodifier'] |
|
| 362 | - or autoriser('modifier', $table, $id) |
|
| 363 | - ) { |
|
| 364 | - $modifs = array(); |
|
| 365 | - foreach ($x['champs'] as $key => $val) { |
|
| 366 | - if ($key == $_id_table) { |
|
| 367 | - continue; |
|
| 368 | - } |
|
| 369 | - $repl = preg_replace($preg, $remplace, $val); |
|
| 370 | - if ($repl <> $val) { |
|
| 371 | - $modifs[$key] = $repl; |
|
| 372 | - } |
|
| 373 | - } |
|
| 374 | - if ($modifs) { |
|
| 375 | - objet_modifier_champs($table, $id, |
|
| 376 | - array( |
|
| 377 | - 'champs' => array_keys($modifs), |
|
| 378 | - ), |
|
| 379 | - $modifs); |
|
| 380 | - } |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - } |
|
| 338 | + include_spip('inc/modifier'); |
|
| 339 | + |
|
| 340 | + // options par defaut |
|
| 341 | + $options = array_merge(array( |
|
| 342 | + 'preg_flags' => 'UimsS', |
|
| 343 | + 'toutmodifier' => false |
|
| 344 | + ), |
|
| 345 | + $options |
|
| 346 | + ); |
|
| 347 | + $options['champs'] = true; |
|
| 348 | + |
|
| 349 | + |
|
| 350 | + if (!is_array($tables)) { |
|
| 351 | + $tables = liste_des_champs(); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + $results = recherche_en_base($recherche, $tables, $options); |
|
| 355 | + |
|
| 356 | + $preg = '/' . str_replace('/', '\\/', $recherche) . '/' . $options['preg_flags']; |
|
| 357 | + |
|
| 358 | + foreach ($results as $table => $r) { |
|
| 359 | + $_id_table = id_table_objet($table); |
|
| 360 | + foreach ($r as $id => $x) { |
|
| 361 | + if ($options['toutmodifier'] |
|
| 362 | + or autoriser('modifier', $table, $id) |
|
| 363 | + ) { |
|
| 364 | + $modifs = array(); |
|
| 365 | + foreach ($x['champs'] as $key => $val) { |
|
| 366 | + if ($key == $_id_table) { |
|
| 367 | + continue; |
|
| 368 | + } |
|
| 369 | + $repl = preg_replace($preg, $remplace, $val); |
|
| 370 | + if ($repl <> $val) { |
|
| 371 | + $modifs[$key] = $repl; |
|
| 372 | + } |
|
| 373 | + } |
|
| 374 | + if ($modifs) { |
|
| 375 | + objet_modifier_champs($table, $id, |
|
| 376 | + array( |
|
| 377 | + 'champs' => array_keys($modifs), |
|
| 378 | + ), |
|
| 379 | + $modifs); |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + } |
|
| 384 | 384 | } |