@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | // securité |
| 24 | 24 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 25 | - return; |
|
| 25 | + return; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | // se faciliter la lecture du charset |
@@ -32,19 +32,19 @@ discard block |
||
| 32 | 32 | * Initialisation |
| 33 | 33 | */ |
| 34 | 34 | function init_charset(): void { |
| 35 | - // Initialisation |
|
| 36 | - $GLOBALS['CHARSET'] = []; |
|
| 37 | - |
|
| 38 | - // noter a l'occasion dans la meta pcre_u notre capacite a utiliser le flag /u |
|
| 39 | - // dans les preg_replace pour ne pas casser certaines lettres accentuees : |
|
| 40 | - // en utf-8 chr(195).chr(160) = a` alors qu'en iso-latin chr(160) = nbsp |
|
| 41 | - if ( |
|
| 42 | - !isset($GLOBALS['meta']['pcre_u']) |
|
| 43 | - or (isset($_GET['var_mode']) and !isset($_GET['var_profile'])) |
|
| 44 | - ) { |
|
| 45 | - include_spip('inc/meta'); |
|
| 46 | - ecrire_meta('pcre_u', (lire_config('charset', _DEFAULT_CHARSET) === 'utf-8') ? 'u' : ''); |
|
| 47 | - } |
|
| 35 | + // Initialisation |
|
| 36 | + $GLOBALS['CHARSET'] = []; |
|
| 37 | + |
|
| 38 | + // noter a l'occasion dans la meta pcre_u notre capacite a utiliser le flag /u |
|
| 39 | + // dans les preg_replace pour ne pas casser certaines lettres accentuees : |
|
| 40 | + // en utf-8 chr(195).chr(160) = a` alors qu'en iso-latin chr(160) = nbsp |
|
| 41 | + if ( |
|
| 42 | + !isset($GLOBALS['meta']['pcre_u']) |
|
| 43 | + or (isset($_GET['var_mode']) and !isset($_GET['var_profile'])) |
|
| 44 | + ) { |
|
| 45 | + include_spip('inc/meta'); |
|
| 46 | + ecrire_meta('pcre_u', (lire_config('charset', _DEFAULT_CHARSET) === 'utf-8') ? 'u' : ''); |
|
| 47 | + } |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | // TODO: code d’exécution en dehors du fichier. |
@@ -65,39 +65,39 @@ discard block |
||
| 65 | 65 | * - false si le charset n'est pas décrit dans le répertoire charsets/ |
| 66 | 66 | **/ |
| 67 | 67 | function load_charset($charset = 'AUTO') { |
| 68 | - if ($charset == 'AUTO') { |
|
| 69 | - $charset = $GLOBALS['meta']['charset']; |
|
| 70 | - } |
|
| 71 | - $charset = trim(strtolower($charset)); |
|
| 72 | - if (isset($GLOBALS['CHARSET'][$charset])) { |
|
| 73 | - return $charset; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - if ($charset == 'utf-8') { |
|
| 77 | - $GLOBALS['CHARSET'][$charset] = []; |
|
| 78 | - |
|
| 79 | - return $charset; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - // Quelques synonymes |
|
| 83 | - if ($charset === '') { |
|
| 84 | - $charset = 'iso-8859-1'; |
|
| 85 | - } elseif ($charset === 'windows-1250') { |
|
| 86 | - $charset = 'cp1250'; |
|
| 87 | - } elseif ($charset === 'windows-1251') { |
|
| 88 | - $charset = 'cp1251'; |
|
| 89 | - } elseif ($charset === 'windows-1256') { |
|
| 90 | - $charset = 'cp1256'; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - if (find_in_path($charset . '.php', 'charsets/', true)) { |
|
| 94 | - return $charset; |
|
| 95 | - } else { |
|
| 96 | - spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'"); |
|
| 97 | - $GLOBALS['CHARSET'][$charset] = []; |
|
| 98 | - |
|
| 99 | - return false; |
|
| 100 | - } |
|
| 68 | + if ($charset == 'AUTO') { |
|
| 69 | + $charset = $GLOBALS['meta']['charset']; |
|
| 70 | + } |
|
| 71 | + $charset = trim(strtolower($charset)); |
|
| 72 | + if (isset($GLOBALS['CHARSET'][$charset])) { |
|
| 73 | + return $charset; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + if ($charset == 'utf-8') { |
|
| 77 | + $GLOBALS['CHARSET'][$charset] = []; |
|
| 78 | + |
|
| 79 | + return $charset; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + // Quelques synonymes |
|
| 83 | + if ($charset === '') { |
|
| 84 | + $charset = 'iso-8859-1'; |
|
| 85 | + } elseif ($charset === 'windows-1250') { |
|
| 86 | + $charset = 'cp1250'; |
|
| 87 | + } elseif ($charset === 'windows-1251') { |
|
| 88 | + $charset = 'cp1251'; |
|
| 89 | + } elseif ($charset === 'windows-1256') { |
|
| 90 | + $charset = 'cp1256'; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + if (find_in_path($charset . '.php', 'charsets/', true)) { |
|
| 94 | + return $charset; |
|
| 95 | + } else { |
|
| 96 | + spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'"); |
|
| 97 | + $GLOBALS['CHARSET'][$charset] = []; |
|
| 98 | + |
|
| 99 | + return false; |
|
| 100 | + } |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | |
@@ -110,19 +110,19 @@ discard block |
||
| 110 | 110 | * true si notre charset est utilisable par mb_strsing |
| 111 | 111 | **/ |
| 112 | 112 | function init_mb_string(): bool { |
| 113 | - static $mb; |
|
| 114 | - |
|
| 115 | - // verifier que le charset interne est connu de mb_string |
|
| 116 | - if (!$mb) { |
|
| 117 | - if (mb_detect_order(lire_config('charset', _DEFAULT_CHARSET))) { |
|
| 118 | - mb_internal_encoding('utf-8'); |
|
| 119 | - $mb = 1; |
|
| 120 | - } else { |
|
| 121 | - $mb = -1; |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - return ($mb === 1); |
|
| 113 | + static $mb; |
|
| 114 | + |
|
| 115 | + // verifier que le charset interne est connu de mb_string |
|
| 116 | + if (!$mb) { |
|
| 117 | + if (mb_detect_order(lire_config('charset', _DEFAULT_CHARSET))) { |
|
| 118 | + mb_internal_encoding('utf-8'); |
|
| 119 | + $mb = 1; |
|
| 120 | + } else { |
|
| 121 | + $mb = -1; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + return ($mb === 1); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | /** |
@@ -137,21 +137,21 @@ discard block |
||
| 137 | 137 | * true si iconv fonctionne correctement |
| 138 | 138 | **/ |
| 139 | 139 | function test_iconv(): bool { |
| 140 | - static $iconv_ok; |
|
| 141 | - |
|
| 142 | - if (!$iconv_ok) { |
|
| 143 | - if (!function_exists('iconv')) { |
|
| 144 | - $iconv_ok = -1; |
|
| 145 | - } else { |
|
| 146 | - if (utf_32_to_unicode(@iconv('utf-8', 'utf-32', 'chaine de test')) === 'chaine de test') { |
|
| 147 | - $iconv_ok = 1; |
|
| 148 | - } else { |
|
| 149 | - $iconv_ok = -1; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - return ($iconv_ok === 1); |
|
| 140 | + static $iconv_ok; |
|
| 141 | + |
|
| 142 | + if (!$iconv_ok) { |
|
| 143 | + if (!function_exists('iconv')) { |
|
| 144 | + $iconv_ok = -1; |
|
| 145 | + } else { |
|
| 146 | + if (utf_32_to_unicode(@iconv('utf-8', 'utf-32', 'chaine de test')) === 'chaine de test') { |
|
| 147 | + $iconv_ok = 1; |
|
| 148 | + } else { |
|
| 149 | + $iconv_ok = -1; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + return ($iconv_ok === 1); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -171,75 +171,75 @@ discard block |
||
| 171 | 171 | * texte corrigé |
| 172 | 172 | **/ |
| 173 | 173 | function corriger_caracteres_windows($texte, $charset = 'AUTO', $charset_cible = 'unicode') { |
| 174 | - static $trans; |
|
| 175 | - |
|
| 176 | - if (is_array($texte)) { |
|
| 177 | - return array_map('corriger_caracteres_windows', $texte); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - if ($charset == 'AUTO') { |
|
| 181 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 182 | - } |
|
| 183 | - if ($charset == 'utf-8') { |
|
| 184 | - $p = chr(194); |
|
| 185 | - if (strpos($texte, $p) == false) { |
|
| 186 | - return $texte; |
|
| 187 | - } |
|
| 188 | - } else { |
|
| 189 | - if ($charset == 'iso-8859-1') { |
|
| 190 | - $p = ''; |
|
| 191 | - } else { |
|
| 192 | - return $texte; |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - if (!isset($trans[$charset][$charset_cible])) { |
|
| 197 | - $trans[$charset][$charset_cible] = [ |
|
| 198 | - $p . chr(128) => '€', |
|
| 199 | - $p . chr(129) => ' ', # pas affecte |
|
| 200 | - $p . chr(130) => '‚', |
|
| 201 | - $p . chr(131) => 'ƒ', |
|
| 202 | - $p . chr(132) => '„', |
|
| 203 | - $p . chr(133) => '…', |
|
| 204 | - $p . chr(134) => '†', |
|
| 205 | - $p . chr(135) => '‡', |
|
| 206 | - $p . chr(136) => 'ˆ', |
|
| 207 | - $p . chr(137) => '‰', |
|
| 208 | - $p . chr(138) => 'Š', |
|
| 209 | - $p . chr(139) => '‹', |
|
| 210 | - $p . chr(140) => 'Œ', |
|
| 211 | - $p . chr(141) => ' ', # pas affecte |
|
| 212 | - $p . chr(142) => 'Ž', |
|
| 213 | - $p . chr(143) => ' ', # pas affecte |
|
| 214 | - $p . chr(144) => ' ', # pas affecte |
|
| 215 | - $p . chr(145) => '‘', |
|
| 216 | - $p . chr(146) => '’', |
|
| 217 | - $p . chr(147) => '“', |
|
| 218 | - $p . chr(148) => '”', |
|
| 219 | - $p . chr(149) => '•', |
|
| 220 | - $p . chr(150) => '–', |
|
| 221 | - $p . chr(151) => '—', |
|
| 222 | - $p . chr(152) => '˜', |
|
| 223 | - $p . chr(153) => '™', |
|
| 224 | - $p . chr(154) => 'š', |
|
| 225 | - $p . chr(155) => '›', |
|
| 226 | - $p . chr(156) => 'œ', |
|
| 227 | - $p . chr(157) => ' ', # pas affecte |
|
| 228 | - $p . chr(158) => 'ž', |
|
| 229 | - $p . chr(159) => 'Ÿ', |
|
| 230 | - ]; |
|
| 231 | - if ($charset_cible != 'unicode') { |
|
| 232 | - foreach ($trans[$charset][$charset_cible] as $k => $c) { |
|
| 233 | - $trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - return @str_replace( |
|
| 239 | - array_keys($trans[$charset][$charset_cible]), |
|
| 240 | - array_values($trans[$charset][$charset_cible]), |
|
| 241 | - $texte |
|
| 242 | - ); |
|
| 174 | + static $trans; |
|
| 175 | + |
|
| 176 | + if (is_array($texte)) { |
|
| 177 | + return array_map('corriger_caracteres_windows', $texte); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + if ($charset == 'AUTO') { |
|
| 181 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 182 | + } |
|
| 183 | + if ($charset == 'utf-8') { |
|
| 184 | + $p = chr(194); |
|
| 185 | + if (strpos($texte, $p) == false) { |
|
| 186 | + return $texte; |
|
| 187 | + } |
|
| 188 | + } else { |
|
| 189 | + if ($charset == 'iso-8859-1') { |
|
| 190 | + $p = ''; |
|
| 191 | + } else { |
|
| 192 | + return $texte; |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + if (!isset($trans[$charset][$charset_cible])) { |
|
| 197 | + $trans[$charset][$charset_cible] = [ |
|
| 198 | + $p . chr(128) => '€', |
|
| 199 | + $p . chr(129) => ' ', # pas affecte |
|
| 200 | + $p . chr(130) => '‚', |
|
| 201 | + $p . chr(131) => 'ƒ', |
|
| 202 | + $p . chr(132) => '„', |
|
| 203 | + $p . chr(133) => '…', |
|
| 204 | + $p . chr(134) => '†', |
|
| 205 | + $p . chr(135) => '‡', |
|
| 206 | + $p . chr(136) => 'ˆ', |
|
| 207 | + $p . chr(137) => '‰', |
|
| 208 | + $p . chr(138) => 'Š', |
|
| 209 | + $p . chr(139) => '‹', |
|
| 210 | + $p . chr(140) => 'Œ', |
|
| 211 | + $p . chr(141) => ' ', # pas affecte |
|
| 212 | + $p . chr(142) => 'Ž', |
|
| 213 | + $p . chr(143) => ' ', # pas affecte |
|
| 214 | + $p . chr(144) => ' ', # pas affecte |
|
| 215 | + $p . chr(145) => '‘', |
|
| 216 | + $p . chr(146) => '’', |
|
| 217 | + $p . chr(147) => '“', |
|
| 218 | + $p . chr(148) => '”', |
|
| 219 | + $p . chr(149) => '•', |
|
| 220 | + $p . chr(150) => '–', |
|
| 221 | + $p . chr(151) => '—', |
|
| 222 | + $p . chr(152) => '˜', |
|
| 223 | + $p . chr(153) => '™', |
|
| 224 | + $p . chr(154) => 'š', |
|
| 225 | + $p . chr(155) => '›', |
|
| 226 | + $p . chr(156) => 'œ', |
|
| 227 | + $p . chr(157) => ' ', # pas affecte |
|
| 228 | + $p . chr(158) => 'ž', |
|
| 229 | + $p . chr(159) => 'Ÿ', |
|
| 230 | + ]; |
|
| 231 | + if ($charset_cible != 'unicode') { |
|
| 232 | + foreach ($trans[$charset][$charset_cible] as $k => $c) { |
|
| 233 | + $trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + return @str_replace( |
|
| 239 | + array_keys($trans[$charset][$charset_cible]), |
|
| 240 | + array_values($trans[$charset][$charset_cible]), |
|
| 241 | + $texte |
|
| 242 | + ); |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | |
@@ -256,26 +256,26 @@ discard block |
||
| 256 | 256 | * texte converti |
| 257 | 257 | **/ |
| 258 | 258 | function html2unicode($texte, $secure = false) { |
| 259 | - if (strpos($texte, '&') === false) { |
|
| 260 | - return $texte; |
|
| 261 | - } |
|
| 262 | - static $trans = []; |
|
| 263 | - if (!$trans) { |
|
| 264 | - load_charset('html'); |
|
| 265 | - foreach ($GLOBALS['CHARSET']['html'] as $key => $val) { |
|
| 266 | - $trans["&$key;"] = $val; |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - if ($secure) { |
|
| 271 | - return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 272 | - } else { |
|
| 273 | - return str_replace( |
|
| 274 | - ['&', '"', '<', '>'], |
|
| 275 | - ['&', '"', '<', '>'], |
|
| 276 | - str_replace(array_keys($trans), array_values($trans), $texte) |
|
| 277 | - ); |
|
| 278 | - } |
|
| 259 | + if (strpos($texte, '&') === false) { |
|
| 260 | + return $texte; |
|
| 261 | + } |
|
| 262 | + static $trans = []; |
|
| 263 | + if (!$trans) { |
|
| 264 | + load_charset('html'); |
|
| 265 | + foreach ($GLOBALS['CHARSET']['html'] as $key => $val) { |
|
| 266 | + $trans["&$key;"] = $val; |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + if ($secure) { |
|
| 271 | + return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 272 | + } else { |
|
| 273 | + return str_replace( |
|
| 274 | + ['&', '"', '<', '>'], |
|
| 275 | + ['&', '"', '<', '>'], |
|
| 276 | + str_replace(array_keys($trans), array_values($trans), $texte) |
|
| 277 | + ); |
|
| 278 | + } |
|
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | |
@@ -290,16 +290,16 @@ discard block |
||
| 290 | 290 | * texte converti |
| 291 | 291 | **/ |
| 292 | 292 | function mathml2unicode($texte) { |
| 293 | - static $trans; |
|
| 294 | - if (!$trans) { |
|
| 295 | - load_charset('mathml'); |
|
| 293 | + static $trans; |
|
| 294 | + if (!$trans) { |
|
| 295 | + load_charset('mathml'); |
|
| 296 | 296 | |
| 297 | - foreach ($GLOBALS['CHARSET']['mathml'] as $key => $val) { |
|
| 298 | - $trans["&$key;"] = $val; |
|
| 299 | - } |
|
| 300 | - } |
|
| 297 | + foreach ($GLOBALS['CHARSET']['mathml'] as $key => $val) { |
|
| 298 | + $trans["&$key;"] = $val; |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | - return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 302 | + return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | |
@@ -321,75 +321,75 @@ discard block |
||
| 321 | 321 | * texte converti en unicode |
| 322 | 322 | **/ |
| 323 | 323 | function charset2unicode($texte, $charset = 'AUTO' /* $forcer: obsolete*/) { |
| 324 | - static $trans; |
|
| 325 | - |
|
| 326 | - if ($charset === 'AUTO') { |
|
| 327 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - if ($charset === '') { |
|
| 331 | - $charset = 'iso-8859-1'; |
|
| 332 | - } |
|
| 333 | - $charset = strtolower($charset); |
|
| 334 | - |
|
| 335 | - switch ($charset) { |
|
| 336 | - case 'utf-8': |
|
| 337 | - case 'utf8': |
|
| 338 | - return utf_8_to_unicode($texte); |
|
| 339 | - |
|
| 340 | - case 'iso-8859-1': |
|
| 341 | - $texte = corriger_caracteres_windows($texte, 'iso-8859-1'); |
|
| 342 | - // pas de break; ici, on suit sur default: |
|
| 343 | - |
|
| 344 | - default: |
|
| 345 | - // mbstring presente ? |
|
| 346 | - if (init_mb_string()) { |
|
| 347 | - $order = mb_detect_order(); |
|
| 348 | - try { |
|
| 349 | - # mb_string connait-il $charset? |
|
| 350 | - if ($order and mb_detect_order($charset)) { |
|
| 351 | - $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 352 | - if ($s && $s != $texte) { |
|
| 353 | - return utf_8_to_unicode($s); |
|
| 354 | - } |
|
| 355 | - } |
|
| 356 | - } catch (\Error $e) { |
|
| 357 | - // Le charset n'existe probablement pas |
|
| 358 | - } finally { |
|
| 359 | - mb_detect_order($order); # remettre comme precedemment |
|
| 360 | - } |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 364 | - if (!isset($trans[$charset])) { |
|
| 365 | - if ( |
|
| 366 | - $cset = load_charset($charset) |
|
| 367 | - and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 368 | - ) { |
|
| 369 | - foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 370 | - $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - } |
|
| 374 | - if (isset($trans[$charset]) and is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 375 | - return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - // Sinon demander a iconv (malgre le fait qu'il coupe quand un |
|
| 379 | - // caractere n'appartient pas au charset, mais c'est un probleme |
|
| 380 | - // surtout en utf-8, gere ci-dessus) |
|
| 381 | - if (test_iconv()) { |
|
| 382 | - $s = iconv($charset, 'utf-32le', $texte); |
|
| 383 | - if ($s) { |
|
| 384 | - return utf_32_to_unicode($s); |
|
| 385 | - } |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - // Au pire ne rien faire |
|
| 389 | - spip_log("erreur charset '$charset' non supporte"); |
|
| 390 | - |
|
| 391 | - return $texte; |
|
| 392 | - } |
|
| 324 | + static $trans; |
|
| 325 | + |
|
| 326 | + if ($charset === 'AUTO') { |
|
| 327 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + if ($charset === '') { |
|
| 331 | + $charset = 'iso-8859-1'; |
|
| 332 | + } |
|
| 333 | + $charset = strtolower($charset); |
|
| 334 | + |
|
| 335 | + switch ($charset) { |
|
| 336 | + case 'utf-8': |
|
| 337 | + case 'utf8': |
|
| 338 | + return utf_8_to_unicode($texte); |
|
| 339 | + |
|
| 340 | + case 'iso-8859-1': |
|
| 341 | + $texte = corriger_caracteres_windows($texte, 'iso-8859-1'); |
|
| 342 | + // pas de break; ici, on suit sur default: |
|
| 343 | + |
|
| 344 | + default: |
|
| 345 | + // mbstring presente ? |
|
| 346 | + if (init_mb_string()) { |
|
| 347 | + $order = mb_detect_order(); |
|
| 348 | + try { |
|
| 349 | + # mb_string connait-il $charset? |
|
| 350 | + if ($order and mb_detect_order($charset)) { |
|
| 351 | + $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 352 | + if ($s && $s != $texte) { |
|
| 353 | + return utf_8_to_unicode($s); |
|
| 354 | + } |
|
| 355 | + } |
|
| 356 | + } catch (\Error $e) { |
|
| 357 | + // Le charset n'existe probablement pas |
|
| 358 | + } finally { |
|
| 359 | + mb_detect_order($order); # remettre comme precedemment |
|
| 360 | + } |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 364 | + if (!isset($trans[$charset])) { |
|
| 365 | + if ( |
|
| 366 | + $cset = load_charset($charset) |
|
| 367 | + and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 368 | + ) { |
|
| 369 | + foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 370 | + $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + } |
|
| 374 | + if (isset($trans[$charset]) and is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 375 | + return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + // Sinon demander a iconv (malgre le fait qu'il coupe quand un |
|
| 379 | + // caractere n'appartient pas au charset, mais c'est un probleme |
|
| 380 | + // surtout en utf-8, gere ci-dessus) |
|
| 381 | + if (test_iconv()) { |
|
| 382 | + $s = iconv($charset, 'utf-32le', $texte); |
|
| 383 | + if ($s) { |
|
| 384 | + return utf_32_to_unicode($s); |
|
| 385 | + } |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + // Au pire ne rien faire |
|
| 389 | + spip_log("erreur charset '$charset' non supporte"); |
|
| 390 | + |
|
| 391 | + return $texte; |
|
| 392 | + } |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | |
@@ -408,43 +408,43 @@ discard block |
||
| 408 | 408 | * texte transformé dans le charset souhaité |
| 409 | 409 | **/ |
| 410 | 410 | function unicode2charset($texte, $charset = 'AUTO') { |
| 411 | - static $CHARSET_REVERSE = []; |
|
| 412 | - static $trans = []; |
|
| 413 | - |
|
| 414 | - if ($charset == 'AUTO') { |
|
| 415 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - switch ($charset) { |
|
| 419 | - case 'utf-8': |
|
| 420 | - return unicode_to_utf_8($texte); |
|
| 421 | - |
|
| 422 | - default: |
|
| 423 | - $charset = load_charset($charset); |
|
| 424 | - |
|
| 425 | - if (empty($CHARSET_REVERSE[$charset])) { |
|
| 426 | - $CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]); |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - if (!isset($trans[$charset])) { |
|
| 430 | - $trans[$charset] = []; |
|
| 431 | - $t = &$trans[$charset]; |
|
| 432 | - for ($e = 128; $e < 255; $e++) { |
|
| 433 | - $h = dechex($e); |
|
| 434 | - if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
|
| 435 | - $s = $CHARSET_REVERSE[$charset][$e]; |
|
| 436 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 437 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 438 | - } else { |
|
| 439 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 440 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 441 | - } |
|
| 442 | - } |
|
| 443 | - } |
|
| 444 | - $texte = str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 445 | - |
|
| 446 | - return $texte; |
|
| 447 | - } |
|
| 411 | + static $CHARSET_REVERSE = []; |
|
| 412 | + static $trans = []; |
|
| 413 | + |
|
| 414 | + if ($charset == 'AUTO') { |
|
| 415 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + switch ($charset) { |
|
| 419 | + case 'utf-8': |
|
| 420 | + return unicode_to_utf_8($texte); |
|
| 421 | + |
|
| 422 | + default: |
|
| 423 | + $charset = load_charset($charset); |
|
| 424 | + |
|
| 425 | + if (empty($CHARSET_REVERSE[$charset])) { |
|
| 426 | + $CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]); |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + if (!isset($trans[$charset])) { |
|
| 430 | + $trans[$charset] = []; |
|
| 431 | + $t = &$trans[$charset]; |
|
| 432 | + for ($e = 128; $e < 255; $e++) { |
|
| 433 | + $h = dechex($e); |
|
| 434 | + if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
|
| 435 | + $s = $CHARSET_REVERSE[$charset][$e]; |
|
| 436 | + $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 437 | + $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 438 | + } else { |
|
| 439 | + $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 440 | + $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | + } |
|
| 444 | + $texte = str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 445 | + |
|
| 446 | + return $texte; |
|
| 447 | + } |
|
| 448 | 448 | } |
| 449 | 449 | |
| 450 | 450 | |
@@ -462,40 +462,40 @@ discard block |
||
| 462 | 462 | * texte transformé dans le charset site |
| 463 | 463 | **/ |
| 464 | 464 | function importer_charset($texte, $charset = 'AUTO') { |
| 465 | - $s = null; |
|
| 466 | - static $trans = []; |
|
| 467 | - // on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite ! |
|
| 468 | - if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset'] == 'utf-8')) { |
|
| 469 | - $texte = corriger_caracteres_windows($texte, 'iso-8859-1', $GLOBALS['meta']['charset']); |
|
| 470 | - if (init_mb_string()) { |
|
| 471 | - if ( |
|
| 472 | - $order = mb_detect_order() # mb_string connait-il $charset? |
|
| 473 | - and mb_detect_order($charset) |
|
| 474 | - ) { |
|
| 475 | - $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 476 | - } |
|
| 477 | - mb_detect_order($order); # remettre comme precedemment |
|
| 478 | - return $s; |
|
| 479 | - } |
|
| 480 | - // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 481 | - if (!isset($trans[$charset])) { |
|
| 482 | - if ( |
|
| 483 | - $cset = load_charset($charset) |
|
| 484 | - and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 485 | - ) { |
|
| 486 | - foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 487 | - $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 488 | - } |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - if (is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 492 | - return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - return $texte; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - return unicode2charset(charset2unicode($texte, $charset)); |
|
| 465 | + $s = null; |
|
| 466 | + static $trans = []; |
|
| 467 | + // on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite ! |
|
| 468 | + if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset'] == 'utf-8')) { |
|
| 469 | + $texte = corriger_caracteres_windows($texte, 'iso-8859-1', $GLOBALS['meta']['charset']); |
|
| 470 | + if (init_mb_string()) { |
|
| 471 | + if ( |
|
| 472 | + $order = mb_detect_order() # mb_string connait-il $charset? |
|
| 473 | + and mb_detect_order($charset) |
|
| 474 | + ) { |
|
| 475 | + $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 476 | + } |
|
| 477 | + mb_detect_order($order); # remettre comme precedemment |
|
| 478 | + return $s; |
|
| 479 | + } |
|
| 480 | + // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 481 | + if (!isset($trans[$charset])) { |
|
| 482 | + if ( |
|
| 483 | + $cset = load_charset($charset) |
|
| 484 | + and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 485 | + ) { |
|
| 486 | + foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 487 | + $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 488 | + } |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + if (is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 492 | + return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + return $texte; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + return unicode2charset(charset2unicode($texte, $charset)); |
|
| 499 | 499 | } |
| 500 | 500 | |
| 501 | 501 | |
@@ -511,92 +511,92 @@ discard block |
||
| 511 | 511 | **/ |
| 512 | 512 | function utf_8_to_unicode($source) { |
| 513 | 513 | |
| 514 | - // mb_string : methode rapide |
|
| 515 | - if (init_mb_string()) { |
|
| 516 | - $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 517 | - |
|
| 518 | - return mb_encode_numericentity($source, $convmap, 'UTF-8'); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - // Sinon methode pas a pas |
|
| 522 | - static $decrement; |
|
| 523 | - static $shift; |
|
| 524 | - |
|
| 525 | - // Cf. php.net, par Ronen. Adapte pour compatibilite < php4 |
|
| 526 | - if (!is_array($decrement)) { |
|
| 527 | - // array used to figure what number to decrement from character order value |
|
| 528 | - // according to number of characters used to map unicode to ascii by utf-8 |
|
| 529 | - $decrement[4] = 240; |
|
| 530 | - $decrement[3] = 224; |
|
| 531 | - $decrement[2] = 192; |
|
| 532 | - $decrement[1] = 0; |
|
| 533 | - // the number of bits to shift each charNum by |
|
| 534 | - $shift[1][0] = 0; |
|
| 535 | - $shift[2][0] = 6; |
|
| 536 | - $shift[2][1] = 0; |
|
| 537 | - $shift[3][0] = 12; |
|
| 538 | - $shift[3][1] = 6; |
|
| 539 | - $shift[3][2] = 0; |
|
| 540 | - $shift[4][0] = 18; |
|
| 541 | - $shift[4][1] = 12; |
|
| 542 | - $shift[4][2] = 6; |
|
| 543 | - $shift[4][3] = 0; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - $pos = 0; |
|
| 547 | - $len = strlen($source); |
|
| 548 | - $encodedString = ''; |
|
| 549 | - while ($pos < $len) { |
|
| 550 | - $char = ''; |
|
| 551 | - $ischar = false; |
|
| 552 | - $asciiPos = ord(substr($source, $pos, 1)); |
|
| 553 | - if (($asciiPos >= 240) && ($asciiPos <= 255)) { |
|
| 554 | - // 4 chars representing one unicode character |
|
| 555 | - $thisLetter = substr($source, $pos, 4); |
|
| 556 | - $pos += 4; |
|
| 557 | - } else { |
|
| 558 | - if (($asciiPos >= 224) && ($asciiPos <= 239)) { |
|
| 559 | - // 3 chars representing one unicode character |
|
| 560 | - $thisLetter = substr($source, $pos, 3); |
|
| 561 | - $pos += 3; |
|
| 562 | - } else { |
|
| 563 | - if (($asciiPos >= 192) && ($asciiPos <= 223)) { |
|
| 564 | - // 2 chars representing one unicode character |
|
| 565 | - $thisLetter = substr($source, $pos, 2); |
|
| 566 | - $pos += 2; |
|
| 567 | - } else { |
|
| 568 | - // 1 char (lower ascii) |
|
| 569 | - $thisLetter = substr($source, $pos, 1); |
|
| 570 | - $pos += 1; |
|
| 571 | - $char = $thisLetter; |
|
| 572 | - $ischar = true; |
|
| 573 | - } |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - if ($ischar) { |
|
| 578 | - $encodedString .= $char; |
|
| 579 | - } else { // process the string representing the letter to a unicode entity |
|
| 580 | - $thisLen = strlen($thisLetter); |
|
| 581 | - $thisPos = 0; |
|
| 582 | - $decimalCode = 0; |
|
| 583 | - while ($thisPos < $thisLen) { |
|
| 584 | - $thisCharOrd = ord(substr($thisLetter, $thisPos, 1)); |
|
| 585 | - if ($thisPos == 0) { |
|
| 586 | - $charNum = intval($thisCharOrd - $decrement[$thisLen]); |
|
| 587 | - $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 588 | - } else { |
|
| 589 | - $charNum = intval($thisCharOrd - 128); |
|
| 590 | - $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 591 | - } |
|
| 592 | - $thisPos++; |
|
| 593 | - } |
|
| 594 | - $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 595 | - $encodedString .= $encodedLetter; |
|
| 596 | - } |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - return $encodedString; |
|
| 514 | + // mb_string : methode rapide |
|
| 515 | + if (init_mb_string()) { |
|
| 516 | + $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 517 | + |
|
| 518 | + return mb_encode_numericentity($source, $convmap, 'UTF-8'); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + // Sinon methode pas a pas |
|
| 522 | + static $decrement; |
|
| 523 | + static $shift; |
|
| 524 | + |
|
| 525 | + // Cf. php.net, par Ronen. Adapte pour compatibilite < php4 |
|
| 526 | + if (!is_array($decrement)) { |
|
| 527 | + // array used to figure what number to decrement from character order value |
|
| 528 | + // according to number of characters used to map unicode to ascii by utf-8 |
|
| 529 | + $decrement[4] = 240; |
|
| 530 | + $decrement[3] = 224; |
|
| 531 | + $decrement[2] = 192; |
|
| 532 | + $decrement[1] = 0; |
|
| 533 | + // the number of bits to shift each charNum by |
|
| 534 | + $shift[1][0] = 0; |
|
| 535 | + $shift[2][0] = 6; |
|
| 536 | + $shift[2][1] = 0; |
|
| 537 | + $shift[3][0] = 12; |
|
| 538 | + $shift[3][1] = 6; |
|
| 539 | + $shift[3][2] = 0; |
|
| 540 | + $shift[4][0] = 18; |
|
| 541 | + $shift[4][1] = 12; |
|
| 542 | + $shift[4][2] = 6; |
|
| 543 | + $shift[4][3] = 0; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + $pos = 0; |
|
| 547 | + $len = strlen($source); |
|
| 548 | + $encodedString = ''; |
|
| 549 | + while ($pos < $len) { |
|
| 550 | + $char = ''; |
|
| 551 | + $ischar = false; |
|
| 552 | + $asciiPos = ord(substr($source, $pos, 1)); |
|
| 553 | + if (($asciiPos >= 240) && ($asciiPos <= 255)) { |
|
| 554 | + // 4 chars representing one unicode character |
|
| 555 | + $thisLetter = substr($source, $pos, 4); |
|
| 556 | + $pos += 4; |
|
| 557 | + } else { |
|
| 558 | + if (($asciiPos >= 224) && ($asciiPos <= 239)) { |
|
| 559 | + // 3 chars representing one unicode character |
|
| 560 | + $thisLetter = substr($source, $pos, 3); |
|
| 561 | + $pos += 3; |
|
| 562 | + } else { |
|
| 563 | + if (($asciiPos >= 192) && ($asciiPos <= 223)) { |
|
| 564 | + // 2 chars representing one unicode character |
|
| 565 | + $thisLetter = substr($source, $pos, 2); |
|
| 566 | + $pos += 2; |
|
| 567 | + } else { |
|
| 568 | + // 1 char (lower ascii) |
|
| 569 | + $thisLetter = substr($source, $pos, 1); |
|
| 570 | + $pos += 1; |
|
| 571 | + $char = $thisLetter; |
|
| 572 | + $ischar = true; |
|
| 573 | + } |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + if ($ischar) { |
|
| 578 | + $encodedString .= $char; |
|
| 579 | + } else { // process the string representing the letter to a unicode entity |
|
| 580 | + $thisLen = strlen($thisLetter); |
|
| 581 | + $thisPos = 0; |
|
| 582 | + $decimalCode = 0; |
|
| 583 | + while ($thisPos < $thisLen) { |
|
| 584 | + $thisCharOrd = ord(substr($thisLetter, $thisPos, 1)); |
|
| 585 | + if ($thisPos == 0) { |
|
| 586 | + $charNum = intval($thisCharOrd - $decrement[$thisLen]); |
|
| 587 | + $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 588 | + } else { |
|
| 589 | + $charNum = intval($thisCharOrd - 128); |
|
| 590 | + $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 591 | + } |
|
| 592 | + $thisPos++; |
|
| 593 | + } |
|
| 594 | + $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 595 | + $encodedString .= $encodedLetter; |
|
| 596 | + } |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + return $encodedString; |
|
| 600 | 600 | } |
| 601 | 601 | |
| 602 | 602 | /** |
@@ -615,32 +615,32 @@ discard block |
||
| 615 | 615 | **/ |
| 616 | 616 | function utf_32_to_unicode($source) { |
| 617 | 617 | |
| 618 | - // mb_string : methode rapide |
|
| 619 | - if (init_mb_string()) { |
|
| 620 | - $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 621 | - $source = mb_encode_numericentity($source, $convmap, 'UTF-32LE'); |
|
| 622 | - |
|
| 623 | - return str_replace(chr(0), '', $source); |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - // Sinon methode lente |
|
| 627 | - $texte = ''; |
|
| 628 | - while ($source) { |
|
| 629 | - $words = unpack('V*', substr($source, 0, 1024)); |
|
| 630 | - $source = substr($source, 1024); |
|
| 631 | - foreach ($words as $word) { |
|
| 632 | - if ($word < 128) { |
|
| 633 | - $texte .= chr($word); |
|
| 634 | - } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
|
| 635 | - else { |
|
| 636 | - if ($word != 65279) { |
|
| 637 | - $texte .= '&#' . $word . ';'; |
|
| 638 | - } |
|
| 639 | - } |
|
| 640 | - } |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - return $texte; |
|
| 618 | + // mb_string : methode rapide |
|
| 619 | + if (init_mb_string()) { |
|
| 620 | + $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 621 | + $source = mb_encode_numericentity($source, $convmap, 'UTF-32LE'); |
|
| 622 | + |
|
| 623 | + return str_replace(chr(0), '', $source); |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + // Sinon methode lente |
|
| 627 | + $texte = ''; |
|
| 628 | + while ($source) { |
|
| 629 | + $words = unpack('V*', substr($source, 0, 1024)); |
|
| 630 | + $source = substr($source, 1024); |
|
| 631 | + foreach ($words as $word) { |
|
| 632 | + if ($word < 128) { |
|
| 633 | + $texte .= chr($word); |
|
| 634 | + } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
|
| 635 | + else { |
|
| 636 | + if ($word != 65279) { |
|
| 637 | + $texte .= '&#' . $word . ';'; |
|
| 638 | + } |
|
| 639 | + } |
|
| 640 | + } |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + return $texte; |
|
| 644 | 644 | } |
| 645 | 645 | |
| 646 | 646 | |
@@ -657,21 +657,21 @@ discard block |
||
| 657 | 657 | * Caractère utf8 si trouvé, '' sinon |
| 658 | 658 | **/ |
| 659 | 659 | function caractere_utf_8($num) { |
| 660 | - $num = intval($num); |
|
| 661 | - if ($num < 128) { |
|
| 662 | - return chr($num); |
|
| 663 | - } |
|
| 664 | - if ($num < 2048) { |
|
| 665 | - return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 666 | - } |
|
| 667 | - if ($num < 65536) { |
|
| 668 | - return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 669 | - } |
|
| 670 | - if ($num < 1_114_112) { |
|
| 671 | - return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - return ''; |
|
| 660 | + $num = intval($num); |
|
| 661 | + if ($num < 128) { |
|
| 662 | + return chr($num); |
|
| 663 | + } |
|
| 664 | + if ($num < 2048) { |
|
| 665 | + return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 666 | + } |
|
| 667 | + if ($num < 65536) { |
|
| 668 | + return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 669 | + } |
|
| 670 | + if ($num < 1_114_112) { |
|
| 671 | + return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + return ''; |
|
| 675 | 675 | } |
| 676 | 676 | |
| 677 | 677 | /** |
@@ -684,42 +684,42 @@ discard block |
||
| 684 | 684 | **/ |
| 685 | 685 | function unicode_to_utf_8($texte) { |
| 686 | 686 | |
| 687 | - // 1. Entites € et suivantes |
|
| 688 | - $vu = []; |
|
| 689 | - if ( |
|
| 690 | - preg_match_all( |
|
| 691 | - ',�*([1-9][0-9][0-9]+);,S', |
|
| 692 | - $texte, |
|
| 693 | - $regs, |
|
| 694 | - PREG_SET_ORDER |
|
| 695 | - ) |
|
| 696 | - ) { |
|
| 697 | - foreach ($regs as $reg) { |
|
| 698 | - if ($reg[1] > 127 and !isset($vu[$reg[0]])) { |
|
| 699 | - $vu[$reg[0]] = caractere_utf_8($reg[1]); |
|
| 700 | - } |
|
| 701 | - } |
|
| 702 | - } |
|
| 703 | - //$texte = str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 704 | - |
|
| 705 | - // 2. Entites > ÿ |
|
| 706 | - //$vu = array(); |
|
| 707 | - if ( |
|
| 708 | - preg_match_all( |
|
| 709 | - ',�*([1-9a-f][0-9a-f][0-9a-f]+);,iS', |
|
| 710 | - $texte, |
|
| 711 | - $regs, |
|
| 712 | - PREG_SET_ORDER |
|
| 713 | - ) |
|
| 714 | - ) { |
|
| 715 | - foreach ($regs as $reg) { |
|
| 716 | - if (!isset($vu[$reg[0]])) { |
|
| 717 | - $vu[$reg[0]] = caractere_utf_8(hexdec($reg[1])); |
|
| 718 | - } |
|
| 719 | - } |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - return str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 687 | + // 1. Entites € et suivantes |
|
| 688 | + $vu = []; |
|
| 689 | + if ( |
|
| 690 | + preg_match_all( |
|
| 691 | + ',�*([1-9][0-9][0-9]+);,S', |
|
| 692 | + $texte, |
|
| 693 | + $regs, |
|
| 694 | + PREG_SET_ORDER |
|
| 695 | + ) |
|
| 696 | + ) { |
|
| 697 | + foreach ($regs as $reg) { |
|
| 698 | + if ($reg[1] > 127 and !isset($vu[$reg[0]])) { |
|
| 699 | + $vu[$reg[0]] = caractere_utf_8($reg[1]); |
|
| 700 | + } |
|
| 701 | + } |
|
| 702 | + } |
|
| 703 | + //$texte = str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 704 | + |
|
| 705 | + // 2. Entites > ÿ |
|
| 706 | + //$vu = array(); |
|
| 707 | + if ( |
|
| 708 | + preg_match_all( |
|
| 709 | + ',�*([1-9a-f][0-9a-f][0-9a-f]+);,iS', |
|
| 710 | + $texte, |
|
| 711 | + $regs, |
|
| 712 | + PREG_SET_ORDER |
|
| 713 | + ) |
|
| 714 | + ) { |
|
| 715 | + foreach ($regs as $reg) { |
|
| 716 | + if (!isset($vu[$reg[0]])) { |
|
| 717 | + $vu[$reg[0]] = caractere_utf_8(hexdec($reg[1])); |
|
| 718 | + } |
|
| 719 | + } |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + return str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 723 | 723 | } |
| 724 | 724 | |
| 725 | 725 | /** |
@@ -731,15 +731,15 @@ discard block |
||
| 731 | 731 | * texte converti |
| 732 | 732 | **/ |
| 733 | 733 | function unicode_to_javascript($texte) { |
| 734 | - $vu = []; |
|
| 735 | - while (preg_match(',�*([0-9]+);,S', $texte, $regs) and !isset($vu[$regs[1]])) { |
|
| 736 | - $num = $regs[1]; |
|
| 737 | - $vu[$num] = true; |
|
| 738 | - $s = '\u' . sprintf('%04x', $num); |
|
| 739 | - $texte = str_replace($regs[0], $s, $texte); |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - return $texte; |
|
| 734 | + $vu = []; |
|
| 735 | + while (preg_match(',�*([0-9]+);,S', $texte, $regs) and !isset($vu[$regs[1]])) { |
|
| 736 | + $num = $regs[1]; |
|
| 737 | + $vu[$num] = true; |
|
| 738 | + $s = '\u' . sprintf('%04x', $num); |
|
| 739 | + $texte = str_replace($regs[0], $s, $texte); |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + return $texte; |
|
| 743 | 743 | } |
| 744 | 744 | |
| 745 | 745 | /** |
@@ -751,11 +751,11 @@ discard block |
||
| 751 | 751 | * texte converti |
| 752 | 752 | **/ |
| 753 | 753 | function javascript_to_unicode($texte) { |
| 754 | - while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 755 | - $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 756 | - } |
|
| 754 | + while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 755 | + $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 756 | + } |
|
| 757 | 757 | |
| 758 | - return $texte; |
|
| 758 | + return $texte; |
|
| 759 | 759 | } |
| 760 | 760 | |
| 761 | 761 | /** |
@@ -767,11 +767,11 @@ discard block |
||
| 767 | 767 | * texte converti |
| 768 | 768 | **/ |
| 769 | 769 | function javascript_to_binary($texte) { |
| 770 | - while (preg_match(',%([0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 771 | - $texte = str_replace($regs[0], chr(hexdec($regs[1])), $texte); |
|
| 772 | - } |
|
| 770 | + while (preg_match(',%([0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 771 | + $texte = str_replace($regs[0], chr(hexdec($regs[1])), $texte); |
|
| 772 | + } |
|
| 773 | 773 | |
| 774 | - return $texte; |
|
| 774 | + return $texte; |
|
| 775 | 775 | } |
| 776 | 776 | |
| 777 | 777 | |
@@ -789,26 +789,26 @@ discard block |
||
| 789 | 789 | * @return string |
| 790 | 790 | */ |
| 791 | 791 | function translitteration_rapide($texte, $charset = 'AUTO', $complexe = ''): string { |
| 792 | - static $trans = []; |
|
| 793 | - if ($charset == 'AUTO') { |
|
| 794 | - $charset = $GLOBALS['meta']['charset']; |
|
| 795 | - } |
|
| 796 | - if (!strlen($texte)) { |
|
| 797 | - return $texte; |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - $table_translit = 'translit' . $complexe; |
|
| 801 | - |
|
| 802 | - // 2. Translitterer grace a la table predefinie |
|
| 803 | - if (!isset($trans[$complexe])) { |
|
| 804 | - $trans[$complexe] = []; |
|
| 805 | - load_charset($table_translit); |
|
| 806 | - foreach ($GLOBALS['CHARSET'][$table_translit] as $key => $val) { |
|
| 807 | - $trans[$complexe][caractere_utf_8($key)] = $val; |
|
| 808 | - } |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - return str_replace(array_keys($trans[$complexe]), array_values($trans[$complexe]), $texte); |
|
| 792 | + static $trans = []; |
|
| 793 | + if ($charset == 'AUTO') { |
|
| 794 | + $charset = $GLOBALS['meta']['charset']; |
|
| 795 | + } |
|
| 796 | + if (!strlen($texte)) { |
|
| 797 | + return $texte; |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + $table_translit = 'translit' . $complexe; |
|
| 801 | + |
|
| 802 | + // 2. Translitterer grace a la table predefinie |
|
| 803 | + if (!isset($trans[$complexe])) { |
|
| 804 | + $trans[$complexe] = []; |
|
| 805 | + load_charset($table_translit); |
|
| 806 | + foreach ($GLOBALS['CHARSET'][$table_translit] as $key => $val) { |
|
| 807 | + $trans[$complexe][caractere_utf_8($key)] = $val; |
|
| 808 | + } |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + return str_replace(array_keys($trans[$complexe]), array_values($trans[$complexe]), $texte); |
|
| 812 | 812 | } |
| 813 | 813 | |
| 814 | 814 | /** |
@@ -831,14 +831,14 @@ discard block |
||
| 831 | 831 | * @return string |
| 832 | 832 | */ |
| 833 | 833 | function translitteration($texte, $charset = 'AUTO', $complexe = ''): string { |
| 834 | - // 0. Supprimer les caracteres illegaux |
|
| 835 | - include_spip('inc/filtres'); |
|
| 836 | - $texte = corriger_caracteres($texte); |
|
| 834 | + // 0. Supprimer les caracteres illegaux |
|
| 835 | + include_spip('inc/filtres'); |
|
| 836 | + $texte = corriger_caracteres($texte); |
|
| 837 | 837 | |
| 838 | - // 1. Passer le charset et les é en utf-8 |
|
| 839 | - $texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset))); |
|
| 838 | + // 1. Passer le charset et les é en utf-8 |
|
| 839 | + $texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset))); |
|
| 840 | 840 | |
| 841 | - return translitteration_rapide($texte, $charset, $complexe); |
|
| 841 | + return translitteration_rapide($texte, $charset, $complexe); |
|
| 842 | 842 | } |
| 843 | 843 | |
| 844 | 844 | /** |
@@ -853,17 +853,17 @@ discard block |
||
| 853 | 853 | * @return string |
| 854 | 854 | */ |
| 855 | 855 | function translitteration_complexe($texte, $chiffres = false): string { |
| 856 | - $texte = translitteration($texte, 'AUTO', 'complexe'); |
|
| 856 | + $texte = translitteration($texte, 'AUTO', 'complexe'); |
|
| 857 | 857 | |
| 858 | - if ($chiffres) { |
|
| 859 | - $texte = preg_replace_callback( |
|
| 860 | - "/[aeiuoyd]['`?~.^+(-]{1,2}/S", |
|
| 861 | - fn($m) => translitteration_chiffree($m[0]), |
|
| 862 | - $texte |
|
| 863 | - ); |
|
| 864 | - } |
|
| 858 | + if ($chiffres) { |
|
| 859 | + $texte = preg_replace_callback( |
|
| 860 | + "/[aeiuoyd]['`?~.^+(-]{1,2}/S", |
|
| 861 | + fn($m) => translitteration_chiffree($m[0]), |
|
| 862 | + $texte |
|
| 863 | + ); |
|
| 864 | + } |
|
| 865 | 865 | |
| 866 | - return $texte; |
|
| 866 | + return $texte; |
|
| 867 | 867 | } |
| 868 | 868 | |
| 869 | 869 | /** |
@@ -875,7 +875,7 @@ discard block |
||
| 875 | 875 | * @return string |
| 876 | 876 | */ |
| 877 | 877 | function translitteration_chiffree($car): string { |
| 878 | - return strtr($car, "'`?~.^+(-", '123456789'); |
|
| 878 | + return strtr($car, "'`?~.^+(-", '123456789'); |
|
| 879 | 879 | } |
| 880 | 880 | |
| 881 | 881 | |
@@ -888,7 +888,7 @@ discard block |
||
| 888 | 888 | * true s'il a un BOM |
| 889 | 889 | **/ |
| 890 | 890 | function bom_utf8($texte): bool { |
| 891 | - return (substr($texte, 0, 3) === chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 891 | + return (substr($texte, 0, 3) === chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 892 | 892 | } |
| 893 | 893 | |
| 894 | 894 | /** |
@@ -905,21 +905,21 @@ discard block |
||
| 905 | 905 | * true si c'est le cas |
| 906 | 906 | **/ |
| 907 | 907 | function is_utf8($string): bool { |
| 908 | - return !strlen( |
|
| 909 | - preg_replace( |
|
| 910 | - ',[\x09\x0A\x0D\x20-\x7E]' # ASCII |
|
| 911 | - . '|[\xC2-\xDF][\x80-\xBF]' # non-overlong 2-byte |
|
| 912 | - . '|\xE0[\xA0-\xBF][\x80-\xBF]' # excluding overlongs |
|
| 913 | - . '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' # straight 3-byte |
|
| 914 | - . '|\xED[\x80-\x9F][\x80-\xBF]' # excluding surrogates |
|
| 915 | - . '|\xF0[\x90-\xBF][\x80-\xBF]{2}' # planes 1-3 |
|
| 916 | - . '|[\xF1-\xF3][\x80-\xBF]{3}' # planes 4-15 |
|
| 917 | - . '|\xF4[\x80-\x8F][\x80-\xBF]{2}' # plane 16 |
|
| 918 | - . ',sS', |
|
| 919 | - '', |
|
| 920 | - $string |
|
| 921 | - ) |
|
| 922 | - ); |
|
| 908 | + return !strlen( |
|
| 909 | + preg_replace( |
|
| 910 | + ',[\x09\x0A\x0D\x20-\x7E]' # ASCII |
|
| 911 | + . '|[\xC2-\xDF][\x80-\xBF]' # non-overlong 2-byte |
|
| 912 | + . '|\xE0[\xA0-\xBF][\x80-\xBF]' # excluding overlongs |
|
| 913 | + . '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' # straight 3-byte |
|
| 914 | + . '|\xED[\x80-\x9F][\x80-\xBF]' # excluding surrogates |
|
| 915 | + . '|\xF0[\x90-\xBF][\x80-\xBF]{2}' # planes 1-3 |
|
| 916 | + . '|[\xF1-\xF3][\x80-\xBF]{3}' # planes 4-15 |
|
| 917 | + . '|\xF4[\x80-\x8F][\x80-\xBF]{2}' # plane 16 |
|
| 918 | + . ',sS', |
|
| 919 | + '', |
|
| 920 | + $string |
|
| 921 | + ) |
|
| 922 | + ); |
|
| 923 | 923 | } |
| 924 | 924 | |
| 925 | 925 | /** |
@@ -931,13 +931,13 @@ discard block |
||
| 931 | 931 | * true si c'est le cas |
| 932 | 932 | **/ |
| 933 | 933 | function is_ascii($string): bool { |
| 934 | - return !strlen( |
|
| 935 | - preg_replace( |
|
| 936 | - ',[\x09\x0A\x0D\x20-\x7E],sS', |
|
| 937 | - '', |
|
| 938 | - $string |
|
| 939 | - ) |
|
| 940 | - ); |
|
| 934 | + return !strlen( |
|
| 935 | + preg_replace( |
|
| 936 | + ',[\x09\x0A\x0D\x20-\x7E],sS', |
|
| 937 | + '', |
|
| 938 | + $string |
|
| 939 | + ) |
|
| 940 | + ); |
|
| 941 | 941 | } |
| 942 | 942 | |
| 943 | 943 | /** |
@@ -956,53 +956,53 @@ discard block |
||
| 956 | 956 | **/ |
| 957 | 957 | function transcoder_page($texte, $headers = ''): string { |
| 958 | 958 | |
| 959 | - // Si tout est < 128 pas la peine d'aller plus loin |
|
| 960 | - if (is_ascii($texte)) { |
|
| 961 | - #spip_log('charset: ascii'); |
|
| 962 | - return $texte; |
|
| 963 | - } |
|
| 964 | - |
|
| 965 | - if (bom_utf8($texte)) { |
|
| 966 | - // Reconnaitre le BOM utf-8 (0xEFBBBF) |
|
| 967 | - $charset = 'utf-8'; |
|
| 968 | - $texte = substr($texte, 3); |
|
| 969 | - } elseif (preg_match(',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) { |
|
| 970 | - // charset precise par le contenu (xml) |
|
| 971 | - $charset = trim(strtolower($regs[1])); |
|
| 972 | - } elseif ( |
|
| 973 | - // charset precise par le contenu (html) |
|
| 974 | - preg_match(',<(meta|html|body)[^>]*charset[^>]*=[^>]*([#-_a-z0-9]+?),UimsS', $texte, $regs) |
|
| 975 | - # eviter toute balise SPIP tel que #CHARSET ou #CONFIG d'un squelette |
|
| 976 | - and !str_contains($regs[2], '#') |
|
| 977 | - and $tmp = trim(strtolower($regs[2])) |
|
| 978 | - ) { |
|
| 979 | - $charset = $tmp; |
|
| 980 | - } elseif (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) { |
|
| 981 | - // charset de la reponse http |
|
| 982 | - $charset = trim(strtolower($regs[1])); |
|
| 983 | - } else { |
|
| 984 | - $charset = ''; |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - |
|
| 988 | - // normaliser les noms du shif-jis japonais |
|
| 989 | - if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) { |
|
| 990 | - $charset = 'shift-jis'; |
|
| 991 | - } |
|
| 992 | - |
|
| 993 | - if ($charset) { |
|
| 994 | - spip_log("charset: $charset"); |
|
| 995 | - } else { |
|
| 996 | - // valeur par defaut |
|
| 997 | - if (is_utf8($texte)) { |
|
| 998 | - $charset = 'utf-8'; |
|
| 999 | - } else { |
|
| 1000 | - $charset = 'iso-8859-1'; |
|
| 1001 | - } |
|
| 1002 | - spip_log("charset probable: $charset"); |
|
| 1003 | - } |
|
| 1004 | - |
|
| 1005 | - return importer_charset($texte, $charset); |
|
| 959 | + // Si tout est < 128 pas la peine d'aller plus loin |
|
| 960 | + if (is_ascii($texte)) { |
|
| 961 | + #spip_log('charset: ascii'); |
|
| 962 | + return $texte; |
|
| 963 | + } |
|
| 964 | + |
|
| 965 | + if (bom_utf8($texte)) { |
|
| 966 | + // Reconnaitre le BOM utf-8 (0xEFBBBF) |
|
| 967 | + $charset = 'utf-8'; |
|
| 968 | + $texte = substr($texte, 3); |
|
| 969 | + } elseif (preg_match(',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) { |
|
| 970 | + // charset precise par le contenu (xml) |
|
| 971 | + $charset = trim(strtolower($regs[1])); |
|
| 972 | + } elseif ( |
|
| 973 | + // charset precise par le contenu (html) |
|
| 974 | + preg_match(',<(meta|html|body)[^>]*charset[^>]*=[^>]*([#-_a-z0-9]+?),UimsS', $texte, $regs) |
|
| 975 | + # eviter toute balise SPIP tel que #CHARSET ou #CONFIG d'un squelette |
|
| 976 | + and !str_contains($regs[2], '#') |
|
| 977 | + and $tmp = trim(strtolower($regs[2])) |
|
| 978 | + ) { |
|
| 979 | + $charset = $tmp; |
|
| 980 | + } elseif (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) { |
|
| 981 | + // charset de la reponse http |
|
| 982 | + $charset = trim(strtolower($regs[1])); |
|
| 983 | + } else { |
|
| 984 | + $charset = ''; |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + |
|
| 988 | + // normaliser les noms du shif-jis japonais |
|
| 989 | + if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) { |
|
| 990 | + $charset = 'shift-jis'; |
|
| 991 | + } |
|
| 992 | + |
|
| 993 | + if ($charset) { |
|
| 994 | + spip_log("charset: $charset"); |
|
| 995 | + } else { |
|
| 996 | + // valeur par defaut |
|
| 997 | + if (is_utf8($texte)) { |
|
| 998 | + $charset = 'utf-8'; |
|
| 999 | + } else { |
|
| 1000 | + $charset = 'iso-8859-1'; |
|
| 1001 | + } |
|
| 1002 | + spip_log("charset probable: $charset"); |
|
| 1003 | + } |
|
| 1004 | + |
|
| 1005 | + return importer_charset($texte, $charset); |
|
| 1006 | 1006 | } |
| 1007 | 1007 | |
| 1008 | 1008 | |
@@ -1026,19 +1026,19 @@ discard block |
||
| 1026 | 1026 | * Le texte coupé |
| 1027 | 1027 | **/ |
| 1028 | 1028 | function spip_substr($c, $start = 0, $length = null) { |
| 1029 | - if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1030 | - if ($length) { |
|
| 1031 | - return substr($c, $start, $length); |
|
| 1032 | - } else { |
|
| 1033 | - return substr($c, $start); |
|
| 1034 | - } |
|
| 1035 | - } |
|
| 1036 | - |
|
| 1037 | - if ($length) { |
|
| 1038 | - return mb_substr($c, $start, $length); |
|
| 1039 | - } else { |
|
| 1040 | - return mb_substr($c, $start); |
|
| 1041 | - } |
|
| 1029 | + if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1030 | + if ($length) { |
|
| 1031 | + return substr($c, $start, $length); |
|
| 1032 | + } else { |
|
| 1033 | + return substr($c, $start); |
|
| 1034 | + } |
|
| 1035 | + } |
|
| 1036 | + |
|
| 1037 | + if ($length) { |
|
| 1038 | + return mb_substr($c, $start, $length); |
|
| 1039 | + } else { |
|
| 1040 | + return mb_substr($c, $start); |
|
| 1041 | + } |
|
| 1042 | 1042 | } |
| 1043 | 1043 | |
| 1044 | 1044 | /** |
@@ -1052,13 +1052,13 @@ discard block |
||
| 1052 | 1052 | * La chaîne avec une majuscule sur le premier mot |
| 1053 | 1053 | */ |
| 1054 | 1054 | function spip_ucfirst($c) { |
| 1055 | - if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1056 | - return ucfirst($c); |
|
| 1057 | - } |
|
| 1055 | + if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1056 | + return ucfirst($c); |
|
| 1057 | + } |
|
| 1058 | 1058 | |
| 1059 | - $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
|
| 1059 | + $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
|
| 1060 | 1060 | |
| 1061 | - return $lettre1 . spip_substr($c, 1); |
|
| 1061 | + return $lettre1 . spip_substr($c, 1); |
|
| 1062 | 1062 | } |
| 1063 | 1063 | |
| 1064 | 1064 | /** |
@@ -1072,11 +1072,11 @@ discard block |
||
| 1072 | 1072 | * La chaîne en minuscules |
| 1073 | 1073 | */ |
| 1074 | 1074 | function spip_strtolower($c) { |
| 1075 | - if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1076 | - return strtolower($c); |
|
| 1077 | - } |
|
| 1075 | + if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1076 | + return strtolower($c); |
|
| 1077 | + } |
|
| 1078 | 1078 | |
| 1079 | - return mb_strtolower($c); |
|
| 1079 | + return mb_strtolower($c); |
|
| 1080 | 1080 | } |
| 1081 | 1081 | |
| 1082 | 1082 | /** |
@@ -1090,15 +1090,15 @@ discard block |
||
| 1090 | 1090 | * Longueur de la chaîne |
| 1091 | 1091 | */ |
| 1092 | 1092 | function spip_strlen($c) { |
| 1093 | - // On transforme les sauts de ligne pour ne pas compter deux caractères |
|
| 1094 | - $c = str_replace("\r\n", "\n", $c); |
|
| 1093 | + // On transforme les sauts de ligne pour ne pas compter deux caractères |
|
| 1094 | + $c = str_replace("\r\n", "\n", $c); |
|
| 1095 | 1095 | |
| 1096 | - // Si ce n'est pas utf-8, utiliser strlen |
|
| 1097 | - if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1098 | - return strlen($c); |
|
| 1099 | - } |
|
| 1096 | + // Si ce n'est pas utf-8, utiliser strlen |
|
| 1097 | + if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1098 | + return strlen($c); |
|
| 1099 | + } |
|
| 1100 | 1100 | |
| 1101 | - return mb_strlen($c); |
|
| 1101 | + return mb_strlen($c); |
|
| 1102 | 1102 | } |
| 1103 | 1103 | |
| 1104 | 1104 | /** |
@@ -1113,17 +1113,17 @@ discard block |
||
| 1113 | 1113 | * en unicode : 💩 |
| 1114 | 1114 | */ |
| 1115 | 1115 | function utf8_noplanes($x): string { |
| 1116 | - $regexp_utf8_4bytes = '/( |
|
| 1116 | + $regexp_utf8_4bytes = '/( |
|
| 1117 | 1117 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 1118 | 1118 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 1119 | 1119 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 1120 | 1120 | )/xS'; |
| 1121 | - if (preg_match_all($regexp_utf8_4bytes, $x, $z, PREG_PATTERN_ORDER)) { |
|
| 1122 | - foreach ($z[0] as $k) { |
|
| 1123 | - $ku = utf_8_to_unicode($k); |
|
| 1124 | - $x = str_replace($k, $ku, $x); |
|
| 1125 | - } |
|
| 1126 | - } |
|
| 1127 | - |
|
| 1128 | - return $x; |
|
| 1121 | + if (preg_match_all($regexp_utf8_4bytes, $x, $z, PREG_PATTERN_ORDER)) { |
|
| 1122 | + foreach ($z[0] as $k) { |
|
| 1123 | + $ku = utf_8_to_unicode($k); |
|
| 1124 | + $x = str_replace($k, $ku, $x); |
|
| 1125 | + } |
|
| 1126 | + } |
|
| 1127 | + |
|
| 1128 | + return $x; |
|
| 1129 | 1129 | } |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | $charset = 'cp1256'; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - if (find_in_path($charset . '.php', 'charsets/', true)) { |
|
| 93 | + if (find_in_path($charset.'.php', 'charsets/', true)) { |
|
| 94 | 94 | return $charset; |
| 95 | 95 | } else { |
| 96 | 96 | spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'"); |
@@ -195,38 +195,38 @@ discard block |
||
| 195 | 195 | |
| 196 | 196 | if (!isset($trans[$charset][$charset_cible])) { |
| 197 | 197 | $trans[$charset][$charset_cible] = [ |
| 198 | - $p . chr(128) => '€', |
|
| 199 | - $p . chr(129) => ' ', # pas affecte |
|
| 200 | - $p . chr(130) => '‚', |
|
| 201 | - $p . chr(131) => 'ƒ', |
|
| 202 | - $p . chr(132) => '„', |
|
| 203 | - $p . chr(133) => '…', |
|
| 204 | - $p . chr(134) => '†', |
|
| 205 | - $p . chr(135) => '‡', |
|
| 206 | - $p . chr(136) => 'ˆ', |
|
| 207 | - $p . chr(137) => '‰', |
|
| 208 | - $p . chr(138) => 'Š', |
|
| 209 | - $p . chr(139) => '‹', |
|
| 210 | - $p . chr(140) => 'Œ', |
|
| 211 | - $p . chr(141) => ' ', # pas affecte |
|
| 212 | - $p . chr(142) => 'Ž', |
|
| 213 | - $p . chr(143) => ' ', # pas affecte |
|
| 214 | - $p . chr(144) => ' ', # pas affecte |
|
| 215 | - $p . chr(145) => '‘', |
|
| 216 | - $p . chr(146) => '’', |
|
| 217 | - $p . chr(147) => '“', |
|
| 218 | - $p . chr(148) => '”', |
|
| 219 | - $p . chr(149) => '•', |
|
| 220 | - $p . chr(150) => '–', |
|
| 221 | - $p . chr(151) => '—', |
|
| 222 | - $p . chr(152) => '˜', |
|
| 223 | - $p . chr(153) => '™', |
|
| 224 | - $p . chr(154) => 'š', |
|
| 225 | - $p . chr(155) => '›', |
|
| 226 | - $p . chr(156) => 'œ', |
|
| 227 | - $p . chr(157) => ' ', # pas affecte |
|
| 228 | - $p . chr(158) => 'ž', |
|
| 229 | - $p . chr(159) => 'Ÿ', |
|
| 198 | + $p.chr(128) => '€', |
|
| 199 | + $p.chr(129) => ' ', # pas affecte |
|
| 200 | + $p.chr(130) => '‚', |
|
| 201 | + $p.chr(131) => 'ƒ', |
|
| 202 | + $p.chr(132) => '„', |
|
| 203 | + $p.chr(133) => '…', |
|
| 204 | + $p.chr(134) => '†', |
|
| 205 | + $p.chr(135) => '‡', |
|
| 206 | + $p.chr(136) => 'ˆ', |
|
| 207 | + $p.chr(137) => '‰', |
|
| 208 | + $p.chr(138) => 'Š', |
|
| 209 | + $p.chr(139) => '‹', |
|
| 210 | + $p.chr(140) => 'Œ', |
|
| 211 | + $p.chr(141) => ' ', # pas affecte |
|
| 212 | + $p.chr(142) => 'Ž', |
|
| 213 | + $p.chr(143) => ' ', # pas affecte |
|
| 214 | + $p.chr(144) => ' ', # pas affecte |
|
| 215 | + $p.chr(145) => '‘', |
|
| 216 | + $p.chr(146) => '’', |
|
| 217 | + $p.chr(147) => '“', |
|
| 218 | + $p.chr(148) => '”', |
|
| 219 | + $p.chr(149) => '•', |
|
| 220 | + $p.chr(150) => '–', |
|
| 221 | + $p.chr(151) => '—', |
|
| 222 | + $p.chr(152) => '˜', |
|
| 223 | + $p.chr(153) => '™', |
|
| 224 | + $p.chr(154) => 'š', |
|
| 225 | + $p.chr(155) => '›', |
|
| 226 | + $p.chr(156) => 'œ', |
|
| 227 | + $p.chr(157) => ' ', # pas affecte |
|
| 228 | + $p.chr(158) => 'ž', |
|
| 229 | + $p.chr(159) => 'Ÿ', |
|
| 230 | 230 | ]; |
| 231 | 231 | if ($charset_cible != 'unicode') { |
| 232 | 232 | foreach ($trans[$charset][$charset_cible] as $k => $c) { |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | and is_array($GLOBALS['CHARSET'][$cset]) |
| 368 | 368 | ) { |
| 369 | 369 | foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
| 370 | - $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 370 | + $trans[$charset][chr($key)] = '&#'.$val.';'; |
|
| 371 | 371 | } |
| 372 | 372 | } |
| 373 | 373 | } |
@@ -433,11 +433,11 @@ discard block |
||
| 433 | 433 | $h = dechex($e); |
| 434 | 434 | if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
| 435 | 435 | $s = $CHARSET_REVERSE[$charset][$e]; |
| 436 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 437 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 436 | + $t['&#'.$e.';'] = $t['�'.$e.';'] = $t['�'.$e.';'] = chr($s); |
|
| 437 | + $t['&#x'.$h.';'] = $t['�'.$h.';'] = $t['�'.$h.';'] = chr($s); |
|
| 438 | 438 | } else { |
| 439 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 440 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 439 | + $t['&#'.$e.';'] = $t['�'.$e.';'] = $t['�'.$e.';'] = chr($e); |
|
| 440 | + $t['&#x'.$h.';'] = $t['�'.$h.';'] = $t['�'.$h.';'] = chr($e); |
|
| 441 | 441 | } |
| 442 | 442 | } |
| 443 | 443 | } |
@@ -484,7 +484,7 @@ discard block |
||
| 484 | 484 | and is_array($GLOBALS['CHARSET'][$cset]) |
| 485 | 485 | ) { |
| 486 | 486 | foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
| 487 | - $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 487 | + $trans[$charset][chr($key)] = unicode2charset('&#'.$val.';'); |
|
| 488 | 488 | } |
| 489 | 489 | } |
| 490 | 490 | } |
@@ -591,7 +591,7 @@ discard block |
||
| 591 | 591 | } |
| 592 | 592 | $thisPos++; |
| 593 | 593 | } |
| 594 | - $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 594 | + $encodedLetter = '&#'.preg_replace('/^0+/', '', $decimalCode).';'; |
|
| 595 | 595 | $encodedString .= $encodedLetter; |
| 596 | 596 | } |
| 597 | 597 | } |
@@ -634,7 +634,7 @@ discard block |
||
| 634 | 634 | } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
| 635 | 635 | else { |
| 636 | 636 | if ($word != 65279) { |
| 637 | - $texte .= '&#' . $word . ';'; |
|
| 637 | + $texte .= '&#'.$word.';'; |
|
| 638 | 638 | } |
| 639 | 639 | } |
| 640 | 640 | } |
@@ -662,13 +662,13 @@ discard block |
||
| 662 | 662 | return chr($num); |
| 663 | 663 | } |
| 664 | 664 | if ($num < 2048) { |
| 665 | - return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 665 | + return chr(($num >> 6) + 192).chr(($num & 63) + 128); |
|
| 666 | 666 | } |
| 667 | 667 | if ($num < 65536) { |
| 668 | - return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 668 | + return chr(($num >> 12) + 224).chr((($num >> 6) & 63) + 128).chr(($num & 63) + 128); |
|
| 669 | 669 | } |
| 670 | 670 | if ($num < 1_114_112) { |
| 671 | - return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 671 | + return chr(($num >> 18) + 240).chr((($num >> 12) & 63) + 128).chr((($num >> 6) & 63) + 128).chr(($num & 63) + 128); |
|
| 672 | 672 | } |
| 673 | 673 | |
| 674 | 674 | return ''; |
@@ -735,7 +735,7 @@ discard block |
||
| 735 | 735 | while (preg_match(',�*([0-9]+);,S', $texte, $regs) and !isset($vu[$regs[1]])) { |
| 736 | 736 | $num = $regs[1]; |
| 737 | 737 | $vu[$num] = true; |
| 738 | - $s = '\u' . sprintf('%04x', $num); |
|
| 738 | + $s = '\u'.sprintf('%04x', $num); |
|
| 739 | 739 | $texte = str_replace($regs[0], $s, $texte); |
| 740 | 740 | } |
| 741 | 741 | |
@@ -752,7 +752,7 @@ discard block |
||
| 752 | 752 | **/ |
| 753 | 753 | function javascript_to_unicode($texte) { |
| 754 | 754 | while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
| 755 | - $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 755 | + $texte = str_replace($regs[0], '&#'.hexdec($regs[1]).';', $texte); |
|
| 756 | 756 | } |
| 757 | 757 | |
| 758 | 758 | return $texte; |
@@ -797,7 +797,7 @@ discard block |
||
| 797 | 797 | return $texte; |
| 798 | 798 | } |
| 799 | 799 | |
| 800 | - $table_translit = 'translit' . $complexe; |
|
| 800 | + $table_translit = 'translit'.$complexe; |
|
| 801 | 801 | |
| 802 | 802 | // 2. Translitterer grace a la table predefinie |
| 803 | 803 | if (!isset($trans[$complexe])) { |
@@ -888,7 +888,7 @@ discard block |
||
| 888 | 888 | * true s'il a un BOM |
| 889 | 889 | **/ |
| 890 | 890 | function bom_utf8($texte): bool { |
| 891 | - return (substr($texte, 0, 3) === chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 891 | + return (substr($texte, 0, 3) === chr(0xEF).chr(0xBB).chr(0xBF)); |
|
| 892 | 892 | } |
| 893 | 893 | |
| 894 | 894 | /** |
@@ -1058,7 +1058,7 @@ discard block |
||
| 1058 | 1058 | |
| 1059 | 1059 | $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
| 1060 | 1060 | |
| 1061 | - return $lettre1 . spip_substr($c, 1); |
|
| 1061 | + return $lettre1.spip_substr($c, 1); |
|
| 1062 | 1062 | } |
| 1063 | 1063 | |
| 1064 | 1064 | /** |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | $res = admin_repair_tables(); |
| 40 | 40 | if (!$res) { |
| 41 | - $res = "<div class='error'>" . _T('avis_erreur_mysql') . ' ' . sql_errno() . ': ' . sql_error() . "</div>\n"; |
|
| 41 | + $res = "<div class='error'>"._T('avis_erreur_mysql').' '.sql_errno().': '.sql_error()."</div>\n"; |
|
| 42 | 42 | } else { |
| 43 | 43 | include_spip('inc/rubriques'); |
| 44 | 44 | calculer_rubriques(); |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | $res .= pipeline('base_admin_repair', $res); |
| 49 | 49 | echo minipres( |
| 50 | 50 | _T('texte_tentative_recuperation'), |
| 51 | - $res . generer_form_ecrire('accueil', '', '', _T('public:accueil_site')) |
|
| 51 | + $res.generer_form_ecrire('accueil', '', '', _T('public:accueil_site')) |
|
| 52 | 52 | ); |
| 53 | 53 | } |
| 54 | 54 | |
@@ -91,12 +91,12 @@ discard block |
||
| 91 | 91 | $count = sql_countsel($tab); |
| 92 | 92 | |
| 93 | 93 | if ($count > 1) { |
| 94 | - $m .= '(' . _T('texte_compte_elements', ['count' => $count]) . ")\n"; |
|
| 94 | + $m .= '('._T('texte_compte_elements', ['count' => $count]).")\n"; |
|
| 95 | 95 | } else { |
| 96 | 96 | if ($count == 1) { |
| 97 | - $m .= '(' . _T('texte_compte_element', ['count' => $count]) . ")\n"; |
|
| 97 | + $m .= '('._T('texte_compte_element', ['count' => $count]).")\n"; |
|
| 98 | 98 | } else { |
| 99 | - $m .= '(' . _T('texte_vide') . ")\n"; |
|
| 99 | + $m .= '('._T('texte_vide').")\n"; |
|
| 100 | 100 | } |
| 101 | 101 | } |
| 102 | 102 | |
@@ -105,13 +105,13 @@ discard block |
||
| 105 | 105 | and $msg = join( |
| 106 | 106 | ' ', |
| 107 | 107 | (is_resource($result_repair) or is_object($result_repair)) ? sql_fetch($result_repair) : $result_repair |
| 108 | - ) . ' ' |
|
| 108 | + ).' ' |
|
| 109 | 109 | and !str_contains($msg, ' OK ') |
| 110 | 110 | ) { |
| 111 | 111 | $class = " class='notice'"; |
| 112 | - $m .= '<br /><tt>' . spip_htmlentities($msg) . "</tt>\n"; |
|
| 112 | + $m .= '<br /><tt>'.spip_htmlentities($msg)."</tt>\n"; |
|
| 113 | 113 | } else { |
| 114 | - $m .= ' ' . _T('texte_table_ok'); |
|
| 114 | + $m .= ' '._T('texte_table_ok'); |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | $res .= "<div$class>$m</div>"; |
@@ -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 | /** |
@@ -36,20 +36,20 @@ discard block |
||
| 36 | 36 | **/ |
| 37 | 37 | function base_repair_dist($titre = '', $reprise = '') { |
| 38 | 38 | |
| 39 | - $res = admin_repair_tables(); |
|
| 40 | - if (!$res) { |
|
| 41 | - $res = "<div class='error'>" . _T('avis_erreur_mysql') . ' ' . sql_errno() . ': ' . sql_error() . "</div>\n"; |
|
| 42 | - } else { |
|
| 43 | - include_spip('inc/rubriques'); |
|
| 44 | - calculer_rubriques(); |
|
| 45 | - propager_les_secteurs(); |
|
| 46 | - } |
|
| 47 | - include_spip('inc/minipres'); |
|
| 48 | - $res .= pipeline('base_admin_repair', $res); |
|
| 49 | - echo minipres( |
|
| 50 | - _T('texte_tentative_recuperation'), |
|
| 51 | - $res . generer_form_ecrire('accueil', '', '', _T('public:accueil_site')) |
|
| 52 | - ); |
|
| 39 | + $res = admin_repair_tables(); |
|
| 40 | + if (!$res) { |
|
| 41 | + $res = "<div class='error'>" . _T('avis_erreur_mysql') . ' ' . sql_errno() . ': ' . sql_error() . "</div>\n"; |
|
| 42 | + } else { |
|
| 43 | + include_spip('inc/rubriques'); |
|
| 44 | + calculer_rubriques(); |
|
| 45 | + propager_les_secteurs(); |
|
| 46 | + } |
|
| 47 | + include_spip('inc/minipres'); |
|
| 48 | + $res .= pipeline('base_admin_repair', $res); |
|
| 49 | + echo minipres( |
|
| 50 | + _T('texte_tentative_recuperation'), |
|
| 51 | + $res . generer_form_ecrire('accueil', '', '', _T('public:accueil_site')) |
|
| 52 | + ); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
@@ -63,60 +63,60 @@ discard block |
||
| 63 | 63 | **/ |
| 64 | 64 | function admin_repair_tables() { |
| 65 | 65 | |
| 66 | - $repair = sql_serveur('repair', '', true); |
|
| 66 | + $repair = sql_serveur('repair', '', true); |
|
| 67 | 67 | |
| 68 | - // recreer les tables manquantes eventuelles |
|
| 69 | - include_spip('base/create'); |
|
| 70 | - creer_base(); |
|
| 71 | - $tables = sql_alltable(); |
|
| 68 | + // recreer les tables manquantes eventuelles |
|
| 69 | + include_spip('base/create'); |
|
| 70 | + creer_base(); |
|
| 71 | + $tables = sql_alltable(); |
|
| 72 | 72 | |
| 73 | - $res = ''; |
|
| 74 | - foreach ($tables as $tab) { |
|
| 75 | - $class = ''; |
|
| 76 | - $m = "<strong>$tab</strong> "; |
|
| 77 | - spip_log("Repare $tab", _LOG_INFO_IMPORTANTE); |
|
| 78 | - // supprimer la meta avant de lancer la reparation |
|
| 79 | - // car le repair peut etre long ; on ne veut pas boucler |
|
| 80 | - effacer_meta('admin_repair'); |
|
| 81 | - if ($repair) { |
|
| 82 | - $result_repair = sql_repair($tab); |
|
| 83 | - if (!$result_repair) { |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - } |
|
| 73 | + $res = ''; |
|
| 74 | + foreach ($tables as $tab) { |
|
| 75 | + $class = ''; |
|
| 76 | + $m = "<strong>$tab</strong> "; |
|
| 77 | + spip_log("Repare $tab", _LOG_INFO_IMPORTANTE); |
|
| 78 | + // supprimer la meta avant de lancer la reparation |
|
| 79 | + // car le repair peut etre long ; on ne veut pas boucler |
|
| 80 | + effacer_meta('admin_repair'); |
|
| 81 | + if ($repair) { |
|
| 82 | + $result_repair = sql_repair($tab); |
|
| 83 | + if (!$result_repair) { |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - // essayer de maj la table (creation de champs manquants) |
|
| 89 | - maj_tables($tab); |
|
| 88 | + // essayer de maj la table (creation de champs manquants) |
|
| 89 | + maj_tables($tab); |
|
| 90 | 90 | |
| 91 | - $count = sql_countsel($tab); |
|
| 91 | + $count = sql_countsel($tab); |
|
| 92 | 92 | |
| 93 | - if ($count > 1) { |
|
| 94 | - $m .= '(' . _T('texte_compte_elements', ['count' => $count]) . ")\n"; |
|
| 95 | - } else { |
|
| 96 | - if ($count == 1) { |
|
| 97 | - $m .= '(' . _T('texte_compte_element', ['count' => $count]) . ")\n"; |
|
| 98 | - } else { |
|
| 99 | - $m .= '(' . _T('texte_vide') . ")\n"; |
|
| 100 | - } |
|
| 101 | - } |
|
| 93 | + if ($count > 1) { |
|
| 94 | + $m .= '(' . _T('texte_compte_elements', ['count' => $count]) . ")\n"; |
|
| 95 | + } else { |
|
| 96 | + if ($count == 1) { |
|
| 97 | + $m .= '(' . _T('texte_compte_element', ['count' => $count]) . ")\n"; |
|
| 98 | + } else { |
|
| 99 | + $m .= '(' . _T('texte_vide') . ")\n"; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - if ( |
|
| 104 | - $repair |
|
| 105 | - and $result_repair |
|
| 106 | - and $msg = join( |
|
| 107 | - ' ', |
|
| 108 | - (is_resource($result_repair) or is_object($result_repair)) ? sql_fetch($result_repair) : $result_repair |
|
| 109 | - ) . ' ' |
|
| 110 | - and !str_contains($msg, ' OK ') |
|
| 111 | - ) { |
|
| 112 | - $class = " class='notice'"; |
|
| 113 | - $m .= '<br /><tt>' . spip_htmlentities($msg) . "</tt>\n"; |
|
| 114 | - } else { |
|
| 115 | - $m .= ' ' . _T('texte_table_ok'); |
|
| 116 | - } |
|
| 103 | + if ( |
|
| 104 | + $repair |
|
| 105 | + and $result_repair |
|
| 106 | + and $msg = join( |
|
| 107 | + ' ', |
|
| 108 | + (is_resource($result_repair) or is_object($result_repair)) ? sql_fetch($result_repair) : $result_repair |
|
| 109 | + ) . ' ' |
|
| 110 | + and !str_contains($msg, ' OK ') |
|
| 111 | + ) { |
|
| 112 | + $class = " class='notice'"; |
|
| 113 | + $m .= '<br /><tt>' . spip_htmlentities($msg) . "</tt>\n"; |
|
| 114 | + } else { |
|
| 115 | + $m .= ' ' . _T('texte_table_ok'); |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - $res .= "<div$class>$m</div>"; |
|
| 119 | - } |
|
| 118 | + $res .= "<div$class>$m</div>"; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - return $res; |
|
| 121 | + return $res; |
|
| 122 | 122 | } |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | \***************************************************************************/ |
| 12 | 12 | |
| 13 | 13 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 14 | - return; |
|
| 14 | + return; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | include_spip('inc/charsets'); |
@@ -24,222 +24,222 @@ discard block |
||
| 24 | 24 | * @return string |
| 25 | 25 | */ |
| 26 | 26 | function xml_entites_html($texte) { |
| 27 | - if ( |
|
| 28 | - !is_string($texte) or !$texte |
|
| 29 | - or strpbrk($texte, "&\"'<>") == false |
|
| 30 | - ) { |
|
| 31 | - return $texte; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - if (!function_exists('spip_htmlspecialchars')) { |
|
| 35 | - include_spip('inc/filtres_mini'); |
|
| 36 | - } |
|
| 37 | - $texte = spip_htmlspecialchars($texte, ENT_QUOTES); |
|
| 38 | - |
|
| 39 | - return $texte; |
|
| 27 | + if ( |
|
| 28 | + !is_string($texte) or !$texte |
|
| 29 | + or strpbrk($texte, "&\"'<>") == false |
|
| 30 | + ) { |
|
| 31 | + return $texte; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + if (!function_exists('spip_htmlspecialchars')) { |
|
| 35 | + include_spip('inc/filtres_mini'); |
|
| 36 | + } |
|
| 37 | + $texte = spip_htmlspecialchars($texte, ENT_QUOTES); |
|
| 38 | + |
|
| 39 | + return $texte; |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | function xml_debutElement($phraseur, $name, $attrs) { |
| 43 | - $depth = $phraseur->depth; |
|
| 44 | - |
|
| 45 | - $t = $phraseur->ouvrant[$depth] ?? ' '; |
|
| 46 | - // espace initial signifie: deja integree au resultat |
|
| 47 | - if ($t[0] != ' ') { |
|
| 48 | - $phraseur->res .= '<' . $t . '>'; |
|
| 49 | - $phraseur->ouvrant[$depth] = ' ' . $t; |
|
| 50 | - } |
|
| 51 | - $t = $phraseur->contenu[$depth]; |
|
| 52 | - // n'indenter que s'il y a un separateur avant |
|
| 53 | - $phraseur->res .= preg_replace("/[\n\t ]+$/", "\n$depth", $t); |
|
| 54 | - $phraseur->contenu[$depth] = ''; |
|
| 55 | - $att = ''; |
|
| 56 | - $sep = ' '; |
|
| 57 | - foreach ($attrs as $k => $v) { |
|
| 58 | - $delim = !str_contains($v, "'") ? "'" : '"'; |
|
| 59 | - $val = xml_entites_html($v); |
|
| 60 | - $att .= $sep . $k . '=' . $delim |
|
| 61 | - . ($delim !== '"' ? str_replace('"', '"', $val) : $val) |
|
| 62 | - . $delim; |
|
| 63 | - $sep = "\n $depth"; |
|
| 64 | - } |
|
| 65 | - $phraseur->depth .= ' '; |
|
| 66 | - $phraseur->contenu[$phraseur->depth] = ''; |
|
| 67 | - $phraseur->ouvrant[$phraseur->depth] = $name . $att; |
|
| 68 | - $phraseur->reperes[$phraseur->depth] = xml_get_current_line_number($phraseur->sax); |
|
| 43 | + $depth = $phraseur->depth; |
|
| 44 | + |
|
| 45 | + $t = $phraseur->ouvrant[$depth] ?? ' '; |
|
| 46 | + // espace initial signifie: deja integree au resultat |
|
| 47 | + if ($t[0] != ' ') { |
|
| 48 | + $phraseur->res .= '<' . $t . '>'; |
|
| 49 | + $phraseur->ouvrant[$depth] = ' ' . $t; |
|
| 50 | + } |
|
| 51 | + $t = $phraseur->contenu[$depth]; |
|
| 52 | + // n'indenter que s'il y a un separateur avant |
|
| 53 | + $phraseur->res .= preg_replace("/[\n\t ]+$/", "\n$depth", $t); |
|
| 54 | + $phraseur->contenu[$depth] = ''; |
|
| 55 | + $att = ''; |
|
| 56 | + $sep = ' '; |
|
| 57 | + foreach ($attrs as $k => $v) { |
|
| 58 | + $delim = !str_contains($v, "'") ? "'" : '"'; |
|
| 59 | + $val = xml_entites_html($v); |
|
| 60 | + $att .= $sep . $k . '=' . $delim |
|
| 61 | + . ($delim !== '"' ? str_replace('"', '"', $val) : $val) |
|
| 62 | + . $delim; |
|
| 63 | + $sep = "\n $depth"; |
|
| 64 | + } |
|
| 65 | + $phraseur->depth .= ' '; |
|
| 66 | + $phraseur->contenu[$phraseur->depth] = ''; |
|
| 67 | + $phraseur->ouvrant[$phraseur->depth] = $name . $att; |
|
| 68 | + $phraseur->reperes[$phraseur->depth] = xml_get_current_line_number($phraseur->sax); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | function xml_finElement($phraseur, $name, $fusion_bal = false) { |
| 72 | - $ouv = $phraseur->ouvrant[$phraseur->depth]; |
|
| 73 | - |
|
| 74 | - if ($ouv[0] != ' ') { |
|
| 75 | - $phraseur->ouvrant[$phraseur->depth] = ' ' . $ouv; |
|
| 76 | - } else { |
|
| 77 | - $ouv = ''; |
|
| 78 | - } |
|
| 79 | - $t = $phraseur->contenu[$phraseur->depth]; |
|
| 80 | - $phraseur->depth = substr($phraseur->depth, 2); |
|
| 81 | - $t = preg_replace("/[\n\t ]+$/", "\n" . $phraseur->depth, $t); |
|
| 82 | - |
|
| 83 | - // fusion <balise></balise> en <balise />. |
|
| 84 | - // ATTENTION, certains clients http croient que fusion ==> pas d'atttributs |
|
| 85 | - // en particulier pour les balises Script et A. |
|
| 86 | - // en presence d'attributs ne le faire que si la DTD est dispo et d'accord |
|
| 87 | - // (param fusion_bal) |
|
| 88 | - |
|
| 89 | - if ($t || (($ouv != $name) and !$fusion_bal)) { |
|
| 90 | - $phraseur->res .= ($ouv ? ('<' . $ouv . '>') : '') . $t . '</' . $name . '>'; |
|
| 91 | - } else { |
|
| 92 | - $phraseur->res .= ($ouv ? ('<' . $ouv . ' />') : ('</' . $name . '>')); |
|
| 93 | - } |
|
| 72 | + $ouv = $phraseur->ouvrant[$phraseur->depth]; |
|
| 73 | + |
|
| 74 | + if ($ouv[0] != ' ') { |
|
| 75 | + $phraseur->ouvrant[$phraseur->depth] = ' ' . $ouv; |
|
| 76 | + } else { |
|
| 77 | + $ouv = ''; |
|
| 78 | + } |
|
| 79 | + $t = $phraseur->contenu[$phraseur->depth]; |
|
| 80 | + $phraseur->depth = substr($phraseur->depth, 2); |
|
| 81 | + $t = preg_replace("/[\n\t ]+$/", "\n" . $phraseur->depth, $t); |
|
| 82 | + |
|
| 83 | + // fusion <balise></balise> en <balise />. |
|
| 84 | + // ATTENTION, certains clients http croient que fusion ==> pas d'atttributs |
|
| 85 | + // en particulier pour les balises Script et A. |
|
| 86 | + // en presence d'attributs ne le faire que si la DTD est dispo et d'accord |
|
| 87 | + // (param fusion_bal) |
|
| 88 | + |
|
| 89 | + if ($t || (($ouv != $name) and !$fusion_bal)) { |
|
| 90 | + $phraseur->res .= ($ouv ? ('<' . $ouv . '>') : '') . $t . '</' . $name . '>'; |
|
| 91 | + } else { |
|
| 92 | + $phraseur->res .= ($ouv ? ('<' . $ouv . ' />') : ('</' . $name . '>')); |
|
| 93 | + } |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | function xml_textElement($phraseur, $data) { |
| 97 | - $depth = $phraseur->depth; |
|
| 98 | - $phraseur->contenu[$depth] .= preg_match('/^script/', $phraseur->ouvrant[$depth]) |
|
| 99 | - ? $data |
|
| 100 | - : xml_entites_html($data); |
|
| 97 | + $depth = $phraseur->depth; |
|
| 98 | + $phraseur->contenu[$depth] .= preg_match('/^script/', $phraseur->ouvrant[$depth]) |
|
| 99 | + ? $data |
|
| 100 | + : xml_entites_html($data); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | function xml_piElement($phraseur, $target, $data) { |
| 104 | - $depth = $phraseur->depth; |
|
| 105 | - |
|
| 106 | - if (strtolower($target) != 'php') { |
|
| 107 | - $phraseur->contenu[$depth] .= $data; |
|
| 108 | - } else { |
|
| 109 | - ob_start(); |
|
| 110 | - eval($data); |
|
| 111 | - $data = ob_get_contents(); |
|
| 112 | - ob_end_clean(); |
|
| 113 | - $phraseur->contenu[$depth] .= $data; |
|
| 114 | - } |
|
| 104 | + $depth = $phraseur->depth; |
|
| 105 | + |
|
| 106 | + if (strtolower($target) != 'php') { |
|
| 107 | + $phraseur->contenu[$depth] .= $data; |
|
| 108 | + } else { |
|
| 109 | + ob_start(); |
|
| 110 | + eval($data); |
|
| 111 | + $data = ob_get_contents(); |
|
| 112 | + ob_end_clean(); |
|
| 113 | + $phraseur->contenu[$depth] .= $data; |
|
| 114 | + } |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | |
| 118 | 118 | function xml_defaultElement($phraseur, $data) { |
| 119 | - $depth = $phraseur->depth; |
|
| 119 | + $depth = $phraseur->depth; |
|
| 120 | 120 | |
| 121 | - if (!isset($phraseur->contenu[$depth])) { |
|
| 122 | - $phraseur->contenu[$depth] = ''; |
|
| 123 | - } |
|
| 124 | - $phraseur->contenu[$depth] .= $data; |
|
| 121 | + if (!isset($phraseur->contenu[$depth])) { |
|
| 122 | + $phraseur->contenu[$depth] = ''; |
|
| 123 | + } |
|
| 124 | + $phraseur->contenu[$depth] .= $data; |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | function xml_parsestring($phraseur, $data) { |
| 128 | - $phraseur->contenu[$phraseur->depth] = ''; |
|
| 129 | - |
|
| 130 | - if (!xml_parse($phraseur->sax, $data, true)) { |
|
| 131 | - coordonnees_erreur( |
|
| 132 | - $phraseur, |
|
| 133 | - xml_error_string(xml_get_error_code($phraseur->sax)) |
|
| 134 | - . "<br />\n" . |
|
| 135 | - (!$phraseur->depth ? '' : |
|
| 136 | - ('(' . |
|
| 137 | - _T('erreur_balise_non_fermee') . |
|
| 138 | - ' <tt>' . |
|
| 139 | - $phraseur->ouvrant[$phraseur->depth] . |
|
| 140 | - '</tt> ' . |
|
| 141 | - _T('ligne') . |
|
| 142 | - ' ' . |
|
| 143 | - $phraseur->reperes[$phraseur->depth] . |
|
| 144 | - ") <br />\n")) |
|
| 145 | - ); |
|
| 146 | - } |
|
| 128 | + $phraseur->contenu[$phraseur->depth] = ''; |
|
| 129 | + |
|
| 130 | + if (!xml_parse($phraseur->sax, $data, true)) { |
|
| 131 | + coordonnees_erreur( |
|
| 132 | + $phraseur, |
|
| 133 | + xml_error_string(xml_get_error_code($phraseur->sax)) |
|
| 134 | + . "<br />\n" . |
|
| 135 | + (!$phraseur->depth ? '' : |
|
| 136 | + ('(' . |
|
| 137 | + _T('erreur_balise_non_fermee') . |
|
| 138 | + ' <tt>' . |
|
| 139 | + $phraseur->ouvrant[$phraseur->depth] . |
|
| 140 | + '</tt> ' . |
|
| 141 | + _T('ligne') . |
|
| 142 | + ' ' . |
|
| 143 | + $phraseur->reperes[$phraseur->depth] . |
|
| 144 | + ") <br />\n")) |
|
| 145 | + ); |
|
| 146 | + } |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | function coordonnees_erreur($phraseur, $msg) { |
| 150 | - $entete_length = substr_count($phraseur->entete, "\n"); |
|
| 151 | - $phraseur->err[] = [ |
|
| 152 | - $msg, |
|
| 153 | - xml_get_current_line_number($phraseur->sax) + $entete_length, |
|
| 154 | - xml_get_current_column_number($phraseur->sax) |
|
| 155 | - ]; |
|
| 150 | + $entete_length = substr_count($phraseur->entete, "\n"); |
|
| 151 | + $phraseur->err[] = [ |
|
| 152 | + $msg, |
|
| 153 | + xml_get_current_line_number($phraseur->sax) + $entete_length, |
|
| 154 | + xml_get_current_column_number($phraseur->sax) |
|
| 155 | + ]; |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | function xml_sax_dist($page, $apply = false, $phraseur = null, $doctype = '', $charset = null) { |
| 159 | - if (is_null($charset)) { |
|
| 160 | - $charset = $GLOBALS['meta']['charset']; |
|
| 161 | - } |
|
| 162 | - if ($apply) { |
|
| 163 | - ob_start(); |
|
| 164 | - if (is_array($apply)) { |
|
| 165 | - $r = $page(...$apply); |
|
| 166 | - } else { |
|
| 167 | - $r = $page(); |
|
| 168 | - } |
|
| 169 | - $page = ob_get_contents(); |
|
| 170 | - ob_end_clean(); |
|
| 171 | - // fonction sans aucun "echo", ca doit etre le resultat |
|
| 172 | - if (!$page) { |
|
| 173 | - $page = $r; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if (!$page) { |
|
| 178 | - return ''; |
|
| 179 | - } |
|
| 180 | - // charger la DTD et transcoder les entites, |
|
| 181 | - // et escamoter le doctype que sax mange en php5 mais pas en php4 |
|
| 182 | - if (!$doctype) { |
|
| 183 | - if (!$r = analyser_doctype($page)) { |
|
| 184 | - $page = _MESSAGE_DOCTYPE . _DOCTYPE_ECRIRE |
|
| 185 | - . preg_replace(_REGEXP_DOCTYPE, '', $page); |
|
| 186 | - $r = analyser_doctype($page); |
|
| 187 | - } |
|
| 188 | - [$entete, $avail, $grammaire, $rotlvl] = array_pad($r, 4, null); |
|
| 189 | - $page = substr($page, strlen($entete)); |
|
| 190 | - } else { |
|
| 191 | - $avail = 'SYSTEM'; |
|
| 192 | - $grammaire = $doctype; |
|
| 193 | - $rotlvl = basename($grammaire); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - include_spip('xml/analyser_dtd'); |
|
| 197 | - $dtc = charger_dtd($grammaire, $avail, $rotlvl); |
|
| 198 | - $page = sax_bug($page, $dtc, $charset); |
|
| 199 | - |
|
| 200 | - // compatibilite Tidy espace public |
|
| 201 | - if (!$phraseur) { |
|
| 202 | - $indenter_xml = charger_fonction('indenter', 'xml'); |
|
| 203 | - |
|
| 204 | - return $indenter_xml($page, $apply); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - $xml_parser = xml_parser_create($charset); |
|
| 208 | - |
|
| 209 | - xml_set_element_handler( |
|
| 210 | - $xml_parser, |
|
| 211 | - [$phraseur, 'debutElement'], |
|
| 212 | - [$phraseur, 'finElement'] |
|
| 213 | - ); |
|
| 214 | - |
|
| 215 | - xml_set_character_data_handler( |
|
| 216 | - $xml_parser, |
|
| 217 | - [$phraseur, 'textElement'] |
|
| 218 | - ); |
|
| 219 | - |
|
| 220 | - xml_set_processing_instruction_handler( |
|
| 221 | - $xml_parser, |
|
| 222 | - [$phraseur, 'piElement'] |
|
| 223 | - ); |
|
| 224 | - |
|
| 225 | - xml_set_default_handler( |
|
| 226 | - $xml_parser, |
|
| 227 | - [$phraseur, 'defaultElement'] |
|
| 228 | - ); |
|
| 229 | - |
|
| 230 | - xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); |
|
| 231 | - |
|
| 232 | - $phraseur->sax = $xml_parser; |
|
| 233 | - if (isset($entete)) { |
|
| 234 | - $phraseur->entete = $entete; |
|
| 235 | - } |
|
| 236 | - $phraseur->page = $page; |
|
| 237 | - $phraseur->dtc = $dtc; |
|
| 238 | - $phraseur->phraserTout($xml_parser, $page); |
|
| 239 | - xml_parser_free($xml_parser); |
|
| 240 | - $phraseur->sax = ''; |
|
| 241 | - |
|
| 242 | - return $phraseur; |
|
| 159 | + if (is_null($charset)) { |
|
| 160 | + $charset = $GLOBALS['meta']['charset']; |
|
| 161 | + } |
|
| 162 | + if ($apply) { |
|
| 163 | + ob_start(); |
|
| 164 | + if (is_array($apply)) { |
|
| 165 | + $r = $page(...$apply); |
|
| 166 | + } else { |
|
| 167 | + $r = $page(); |
|
| 168 | + } |
|
| 169 | + $page = ob_get_contents(); |
|
| 170 | + ob_end_clean(); |
|
| 171 | + // fonction sans aucun "echo", ca doit etre le resultat |
|
| 172 | + if (!$page) { |
|
| 173 | + $page = $r; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if (!$page) { |
|
| 178 | + return ''; |
|
| 179 | + } |
|
| 180 | + // charger la DTD et transcoder les entites, |
|
| 181 | + // et escamoter le doctype que sax mange en php5 mais pas en php4 |
|
| 182 | + if (!$doctype) { |
|
| 183 | + if (!$r = analyser_doctype($page)) { |
|
| 184 | + $page = _MESSAGE_DOCTYPE . _DOCTYPE_ECRIRE |
|
| 185 | + . preg_replace(_REGEXP_DOCTYPE, '', $page); |
|
| 186 | + $r = analyser_doctype($page); |
|
| 187 | + } |
|
| 188 | + [$entete, $avail, $grammaire, $rotlvl] = array_pad($r, 4, null); |
|
| 189 | + $page = substr($page, strlen($entete)); |
|
| 190 | + } else { |
|
| 191 | + $avail = 'SYSTEM'; |
|
| 192 | + $grammaire = $doctype; |
|
| 193 | + $rotlvl = basename($grammaire); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + include_spip('xml/analyser_dtd'); |
|
| 197 | + $dtc = charger_dtd($grammaire, $avail, $rotlvl); |
|
| 198 | + $page = sax_bug($page, $dtc, $charset); |
|
| 199 | + |
|
| 200 | + // compatibilite Tidy espace public |
|
| 201 | + if (!$phraseur) { |
|
| 202 | + $indenter_xml = charger_fonction('indenter', 'xml'); |
|
| 203 | + |
|
| 204 | + return $indenter_xml($page, $apply); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + $xml_parser = xml_parser_create($charset); |
|
| 208 | + |
|
| 209 | + xml_set_element_handler( |
|
| 210 | + $xml_parser, |
|
| 211 | + [$phraseur, 'debutElement'], |
|
| 212 | + [$phraseur, 'finElement'] |
|
| 213 | + ); |
|
| 214 | + |
|
| 215 | + xml_set_character_data_handler( |
|
| 216 | + $xml_parser, |
|
| 217 | + [$phraseur, 'textElement'] |
|
| 218 | + ); |
|
| 219 | + |
|
| 220 | + xml_set_processing_instruction_handler( |
|
| 221 | + $xml_parser, |
|
| 222 | + [$phraseur, 'piElement'] |
|
| 223 | + ); |
|
| 224 | + |
|
| 225 | + xml_set_default_handler( |
|
| 226 | + $xml_parser, |
|
| 227 | + [$phraseur, 'defaultElement'] |
|
| 228 | + ); |
|
| 229 | + |
|
| 230 | + xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); |
|
| 231 | + |
|
| 232 | + $phraseur->sax = $xml_parser; |
|
| 233 | + if (isset($entete)) { |
|
| 234 | + $phraseur->entete = $entete; |
|
| 235 | + } |
|
| 236 | + $phraseur->page = $page; |
|
| 237 | + $phraseur->dtc = $dtc; |
|
| 238 | + $phraseur->phraserTout($xml_parser, $page); |
|
| 239 | + xml_parser_free($xml_parser); |
|
| 240 | + $phraseur->sax = ''; |
|
| 241 | + |
|
| 242 | + return $phraseur; |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | // SAX ne dit pas si une Entite est dans un attribut ou non. |
@@ -250,24 +250,24 @@ discard block |
||
| 250 | 250 | // sinon on se rabat sur ce qu'en connait SPIP en standard. |
| 251 | 251 | |
| 252 | 252 | function sax_bug($data, $dtc, $charset = null) { |
| 253 | - if (is_null($charset)) { |
|
| 254 | - $charset = $GLOBALS['meta']['charset']; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - if ($dtc) { |
|
| 258 | - $trans = []; |
|
| 259 | - |
|
| 260 | - foreach ($dtc->entites as $k => $v) { |
|
| 261 | - if (!strpos(' amp lt gt quot ', (string) $k)) { |
|
| 262 | - $trans["&$k;"] = $v; |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - $data = strtr($data, $trans); |
|
| 266 | - } else { |
|
| 267 | - $data = html2unicode($data, true); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - return unicode2charset($data, $charset); |
|
| 253 | + if (is_null($charset)) { |
|
| 254 | + $charset = $GLOBALS['meta']['charset']; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + if ($dtc) { |
|
| 258 | + $trans = []; |
|
| 259 | + |
|
| 260 | + foreach ($dtc->entites as $k => $v) { |
|
| 261 | + if (!strpos(' amp lt gt quot ', (string) $k)) { |
|
| 262 | + $trans["&$k;"] = $v; |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + $data = strtr($data, $trans); |
|
| 266 | + } else { |
|
| 267 | + $data = html2unicode($data, true); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + return unicode2charset($data, $charset); |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | // Retirer < ? xml... ? > et autre PI, ainsi que les commentaires en debut |
@@ -277,52 +277,52 @@ discard block |
||
| 277 | 277 | // les autres formats RSS n'ont pas de DTD, |
| 278 | 278 | // mais un XML Schema que SPIP ne fait pas encore lire. |
| 279 | 279 | function analyser_doctype($data) { |
| 280 | - if (!preg_match(_REGEXP_DOCTYPE, $data, $page)) { |
|
| 281 | - if (preg_match(_REGEXP_XML, $data, $page)) { |
|
| 282 | - [, $entete, $topelement] = $page; |
|
| 283 | - if ($topelement == 'rss') { |
|
| 284 | - return [ |
|
| 285 | - $entete, |
|
| 286 | - 'PUBLIC', |
|
| 287 | - _DOCTYPE_RSS, |
|
| 288 | - 'rss-0.91.dtd' |
|
| 289 | - ]; |
|
| 290 | - } else { |
|
| 291 | - $dtd = $topelement . '.dtd'; |
|
| 292 | - $f = find_in_path($dtd); |
|
| 293 | - if (file_exists($f)) { |
|
| 294 | - return [$entete, 'SYSTEM', $f, $dtd]; |
|
| 295 | - } |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - spip_log('Dtd pas vu pour ' . substr($data, 0, 100)); |
|
| 299 | - |
|
| 300 | - return []; |
|
| 301 | - } |
|
| 302 | - [$entete, , $topelement, $avail, $suite] = $page; |
|
| 303 | - |
|
| 304 | - if (!preg_match('/^"([^"]*)"\s*(.*)$/', $suite, $r)) { |
|
| 305 | - if (!preg_match("/^'([^']*)'\s*(.*)$/", $suite, $r)) { |
|
| 306 | - return []; |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - [, $rotlvl, $suite] = $r; |
|
| 310 | - |
|
| 311 | - if (!$suite) { |
|
| 312 | - if ($avail != 'SYSTEM') { |
|
| 313 | - return []; |
|
| 314 | - } |
|
| 315 | - $grammaire = $rotlvl; |
|
| 316 | - $rotlvl = ''; |
|
| 317 | - } else { |
|
| 318 | - if (!preg_match('/^"([^"]*)"\s*$/', $suite, $r)) { |
|
| 319 | - if (!preg_match("/^'([^']*)'\s*$/", $suite, $r)) { |
|
| 320 | - return []; |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $grammaire = $r[1]; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - return [$entete, $avail, $grammaire, $rotlvl]; |
|
| 280 | + if (!preg_match(_REGEXP_DOCTYPE, $data, $page)) { |
|
| 281 | + if (preg_match(_REGEXP_XML, $data, $page)) { |
|
| 282 | + [, $entete, $topelement] = $page; |
|
| 283 | + if ($topelement == 'rss') { |
|
| 284 | + return [ |
|
| 285 | + $entete, |
|
| 286 | + 'PUBLIC', |
|
| 287 | + _DOCTYPE_RSS, |
|
| 288 | + 'rss-0.91.dtd' |
|
| 289 | + ]; |
|
| 290 | + } else { |
|
| 291 | + $dtd = $topelement . '.dtd'; |
|
| 292 | + $f = find_in_path($dtd); |
|
| 293 | + if (file_exists($f)) { |
|
| 294 | + return [$entete, 'SYSTEM', $f, $dtd]; |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + spip_log('Dtd pas vu pour ' . substr($data, 0, 100)); |
|
| 299 | + |
|
| 300 | + return []; |
|
| 301 | + } |
|
| 302 | + [$entete, , $topelement, $avail, $suite] = $page; |
|
| 303 | + |
|
| 304 | + if (!preg_match('/^"([^"]*)"\s*(.*)$/', $suite, $r)) { |
|
| 305 | + if (!preg_match("/^'([^']*)'\s*(.*)$/", $suite, $r)) { |
|
| 306 | + return []; |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + [, $rotlvl, $suite] = $r; |
|
| 310 | + |
|
| 311 | + if (!$suite) { |
|
| 312 | + if ($avail != 'SYSTEM') { |
|
| 313 | + return []; |
|
| 314 | + } |
|
| 315 | + $grammaire = $rotlvl; |
|
| 316 | + $rotlvl = ''; |
|
| 317 | + } else { |
|
| 318 | + if (!preg_match('/^"([^"]*)"\s*$/', $suite, $r)) { |
|
| 319 | + if (!preg_match("/^'([^']*)'\s*$/", $suite, $r)) { |
|
| 320 | + return []; |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $grammaire = $r[1]; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + return [$entete, $avail, $grammaire, $rotlvl]; |
|
| 328 | 328 | } |
@@ -45,8 +45,8 @@ discard block |
||
| 45 | 45 | $t = $phraseur->ouvrant[$depth] ?? ' '; |
| 46 | 46 | // espace initial signifie: deja integree au resultat |
| 47 | 47 | if ($t[0] != ' ') { |
| 48 | - $phraseur->res .= '<' . $t . '>'; |
|
| 49 | - $phraseur->ouvrant[$depth] = ' ' . $t; |
|
| 48 | + $phraseur->res .= '<'.$t.'>'; |
|
| 49 | + $phraseur->ouvrant[$depth] = ' '.$t; |
|
| 50 | 50 | } |
| 51 | 51 | $t = $phraseur->contenu[$depth]; |
| 52 | 52 | // n'indenter que s'il y a un separateur avant |
@@ -57,14 +57,14 @@ discard block |
||
| 57 | 57 | foreach ($attrs as $k => $v) { |
| 58 | 58 | $delim = !str_contains($v, "'") ? "'" : '"'; |
| 59 | 59 | $val = xml_entites_html($v); |
| 60 | - $att .= $sep . $k . '=' . $delim |
|
| 60 | + $att .= $sep.$k.'='.$delim |
|
| 61 | 61 | . ($delim !== '"' ? str_replace('"', '"', $val) : $val) |
| 62 | 62 | . $delim; |
| 63 | 63 | $sep = "\n $depth"; |
| 64 | 64 | } |
| 65 | 65 | $phraseur->depth .= ' '; |
| 66 | 66 | $phraseur->contenu[$phraseur->depth] = ''; |
| 67 | - $phraseur->ouvrant[$phraseur->depth] = $name . $att; |
|
| 67 | + $phraseur->ouvrant[$phraseur->depth] = $name.$att; |
|
| 68 | 68 | $phraseur->reperes[$phraseur->depth] = xml_get_current_line_number($phraseur->sax); |
| 69 | 69 | } |
| 70 | 70 | |
@@ -72,13 +72,13 @@ discard block |
||
| 72 | 72 | $ouv = $phraseur->ouvrant[$phraseur->depth]; |
| 73 | 73 | |
| 74 | 74 | if ($ouv[0] != ' ') { |
| 75 | - $phraseur->ouvrant[$phraseur->depth] = ' ' . $ouv; |
|
| 75 | + $phraseur->ouvrant[$phraseur->depth] = ' '.$ouv; |
|
| 76 | 76 | } else { |
| 77 | 77 | $ouv = ''; |
| 78 | 78 | } |
| 79 | 79 | $t = $phraseur->contenu[$phraseur->depth]; |
| 80 | 80 | $phraseur->depth = substr($phraseur->depth, 2); |
| 81 | - $t = preg_replace("/[\n\t ]+$/", "\n" . $phraseur->depth, $t); |
|
| 81 | + $t = preg_replace("/[\n\t ]+$/", "\n".$phraseur->depth, $t); |
|
| 82 | 82 | |
| 83 | 83 | // fusion <balise></balise> en <balise />. |
| 84 | 84 | // ATTENTION, certains clients http croient que fusion ==> pas d'atttributs |
@@ -87,9 +87,9 @@ discard block |
||
| 87 | 87 | // (param fusion_bal) |
| 88 | 88 | |
| 89 | 89 | if ($t || (($ouv != $name) and !$fusion_bal)) { |
| 90 | - $phraseur->res .= ($ouv ? ('<' . $ouv . '>') : '') . $t . '</' . $name . '>'; |
|
| 90 | + $phraseur->res .= ($ouv ? ('<'.$ouv.'>') : '').$t.'</'.$name.'>'; |
|
| 91 | 91 | } else { |
| 92 | - $phraseur->res .= ($ouv ? ('<' . $ouv . ' />') : ('</' . $name . '>')); |
|
| 92 | + $phraseur->res .= ($ouv ? ('<'.$ouv.' />') : ('</'.$name.'>')); |
|
| 93 | 93 | } |
| 94 | 94 | } |
| 95 | 95 | |
@@ -131,16 +131,15 @@ discard block |
||
| 131 | 131 | coordonnees_erreur( |
| 132 | 132 | $phraseur, |
| 133 | 133 | xml_error_string(xml_get_error_code($phraseur->sax)) |
| 134 | - . "<br />\n" . |
|
| 135 | - (!$phraseur->depth ? '' : |
|
| 136 | - ('(' . |
|
| 137 | - _T('erreur_balise_non_fermee') . |
|
| 138 | - ' <tt>' . |
|
| 139 | - $phraseur->ouvrant[$phraseur->depth] . |
|
| 140 | - '</tt> ' . |
|
| 141 | - _T('ligne') . |
|
| 142 | - ' ' . |
|
| 143 | - $phraseur->reperes[$phraseur->depth] . |
|
| 134 | + . "<br />\n". |
|
| 135 | + (!$phraseur->depth ? '' : ('('. |
|
| 136 | + _T('erreur_balise_non_fermee'). |
|
| 137 | + ' <tt>'. |
|
| 138 | + $phraseur->ouvrant[$phraseur->depth]. |
|
| 139 | + '</tt> '. |
|
| 140 | + _T('ligne'). |
|
| 141 | + ' '. |
|
| 142 | + $phraseur->reperes[$phraseur->depth]. |
|
| 144 | 143 | ") <br />\n")) |
| 145 | 144 | ); |
| 146 | 145 | } |
@@ -181,7 +180,7 @@ discard block |
||
| 181 | 180 | // et escamoter le doctype que sax mange en php5 mais pas en php4 |
| 182 | 181 | if (!$doctype) { |
| 183 | 182 | if (!$r = analyser_doctype($page)) { |
| 184 | - $page = _MESSAGE_DOCTYPE . _DOCTYPE_ECRIRE |
|
| 183 | + $page = _MESSAGE_DOCTYPE._DOCTYPE_ECRIRE |
|
| 185 | 184 | . preg_replace(_REGEXP_DOCTYPE, '', $page); |
| 186 | 185 | $r = analyser_doctype($page); |
| 187 | 186 | } |
@@ -288,18 +287,18 @@ discard block |
||
| 288 | 287 | 'rss-0.91.dtd' |
| 289 | 288 | ]; |
| 290 | 289 | } else { |
| 291 | - $dtd = $topelement . '.dtd'; |
|
| 290 | + $dtd = $topelement.'.dtd'; |
|
| 292 | 291 | $f = find_in_path($dtd); |
| 293 | 292 | if (file_exists($f)) { |
| 294 | 293 | return [$entete, 'SYSTEM', $f, $dtd]; |
| 295 | 294 | } |
| 296 | 295 | } |
| 297 | 296 | } |
| 298 | - spip_log('Dtd pas vu pour ' . substr($data, 0, 100)); |
|
| 297 | + spip_log('Dtd pas vu pour '.substr($data, 0, 100)); |
|
| 299 | 298 | |
| 300 | 299 | return []; |
| 301 | 300 | } |
| 302 | - [$entete, , $topelement, $avail, $suite] = $page; |
|
| 301 | + [$entete,, $topelement, $avail, $suite] = $page; |
|
| 303 | 302 | |
| 304 | 303 | if (!preg_match('/^"([^"]*)"\s*(.*)$/', $suite, $r)) { |
| 305 | 304 | if (!preg_match("/^'([^']*)'\s*(.*)$/", $suite, $r)) { |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | if (!$link) { |
| 65 | 65 | $erreurs[] = pg_last_error(); |
| 66 | 66 | foreach ($erreurs as $e) { |
| 67 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 67 | + spip_log('Echec pg_connect. Erreur : '.$e, 'pg.'._LOG_HS); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | return false; |
@@ -82,8 +82,8 @@ discard block |
||
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | spip_log( |
| 85 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 86 | - 'pg.' . _LOG_DEBUG |
|
| 85 | + "Connexion vers $host, base $db, prefixe $prefixe ".($link ? 'operationnelle' : 'impossible'), |
|
| 86 | + 'pg.'._LOG_DEBUG |
|
| 87 | 87 | ); |
| 88 | 88 | |
| 89 | 89 | return !$link ? false : [ |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | } else { |
| 172 | 172 | $suite = ''; |
| 173 | 173 | } |
| 174 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 174 | + $query = preg_replace('/([,\s])spip_/', '\1'.$prefixe.'_', $query).$suite; |
|
| 175 | 175 | |
| 176 | 176 | // renvoyer la requete inerte si demandee |
| 177 | 177 | if (!$requeter) { |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | // ou revoir l'api de sql_alter en creant un |
| 240 | 240 | // sql_alter_table($table,array($actions)); |
| 241 | 241 | if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
| 242 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 242 | + spip_log("$query mal comprise", 'pg.'._LOG_ERREUR); |
|
| 243 | 243 | |
| 244 | 244 | return false; |
| 245 | 245 | } |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | $i = 0; |
| 253 | 253 | $ouverte = false; |
| 254 | 254 | while ($do = array_shift($todo)) { |
| 255 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 255 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i].','.$do : $do; |
|
| 256 | 256 | $o = (str_contains($do, '(')); |
| 257 | 257 | $f = (str_contains($do, ')')); |
| 258 | 258 | if ($o and !$f) { |
@@ -265,33 +265,33 @@ discard block |
||
| 265 | 265 | } |
| 266 | 266 | } |
| 267 | 267 | $todo = $todo2; |
| 268 | - $query = $debut . ' ' . array_shift($todo); |
|
| 268 | + $query = $debut.' '.array_shift($todo); |
|
| 269 | 269 | |
| 270 | 270 | if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
| 271 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 271 | + spip_log("$query incompris", 'pg.'._LOG_ERREUR); |
|
| 272 | 272 | } else { |
| 273 | 273 | if ($r[1]) { |
| 274 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 274 | + spip_log("j'ignore IGNORE dans $query", 'pg.'._LOG_AVERTISSEMENT); |
|
| 275 | 275 | } |
| 276 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 276 | + $f = 'spip_pg_alter_'.strtolower($r[3]); |
|
| 277 | 277 | if (function_exists($f)) { |
| 278 | 278 | $f($r[2], $r[4], $serveur, $requeter); |
| 279 | 279 | } else { |
| 280 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 280 | + spip_log("$query non prevu", 'pg.'._LOG_ERREUR); |
|
| 281 | 281 | } |
| 282 | 282 | } |
| 283 | 283 | // Alter a plusieurs args. Faudrait optimiser. |
| 284 | 284 | if ($todo) { |
| 285 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 285 | + spip_pg_alter("TABLE $table ".join(',', $todo)); |
|
| 286 | 286 | } |
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 290 | 290 | if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
| 291 | - spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 291 | + spip_log("alter change: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 292 | 292 | } else { |
| 293 | 293 | [, $old, $new, $type, $default, $null, $def2] = $r; |
| 294 | - $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 294 | + $actions = ["ALTER $old TYPE ".mysql2pg_type($type)]; |
|
| 295 | 295 | if ($null) { |
| 296 | 296 | $actions[] = "ALTER $old SET NOT NULL"; |
| 297 | 297 | } else { |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | $actions[] = "ALTER $old DROP DEFAULT"; |
| 305 | 305 | } |
| 306 | 306 | |
| 307 | - spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 307 | + spip_pg_query("ALTER TABLE $table ".join(', ', $actions)); |
|
| 308 | 308 | |
| 309 | 309 | if ($old != $new) { |
| 310 | 310 | spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
@@ -315,7 +315,7 @@ discard block |
||
| 315 | 315 | function spip_pg_alter_add($table, $arg, $serveur = '', $requeter = true) { |
| 316 | 316 | $nom_index = null; |
| 317 | 317 | if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
| 318 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 318 | + spip_log("alter add $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 319 | 319 | |
| 320 | 320 | return null; |
| 321 | 321 | } |
@@ -325,14 +325,14 @@ discard block |
||
| 325 | 325 | $m[2] = $n[1]; |
| 326 | 326 | } |
| 327 | 327 | |
| 328 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 328 | + return spip_pg_query("ALTER TABLE $table ADD ".$m[1].' '.mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 329 | 329 | } elseif ($r[1][0] == 'P') { |
| 330 | 330 | // la primary peut etre sur plusieurs champs |
| 331 | 331 | $r[2] = trim(str_replace('`', '', $r[2])); |
| 332 | 332 | $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
| 333 | 333 | |
| 334 | 334 | return spip_pg_query( |
| 335 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 335 | + "ALTER TABLE $table ADD CONSTRAINT $table".'_pkey PRIMARY KEY ('.$m.')', |
|
| 336 | 336 | $serveur, |
| 337 | 337 | $requeter |
| 338 | 338 | ); |
@@ -351,7 +351,7 @@ discard block |
||
| 351 | 351 | $colonnes = substr($m[1], 1, -1); |
| 352 | 352 | if (str_contains(',', $colonnes)) { |
| 353 | 353 | spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
| 354 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 354 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.'._LOG_ERREUR); |
|
| 355 | 355 | } else { |
| 356 | 356 | $nom_index = $colonnes; |
| 357 | 357 | } |
@@ -367,23 +367,23 @@ discard block |
||
| 367 | 367 | |
| 368 | 368 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 369 | 369 | if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
| 370 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 370 | + spip_log("alter drop: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 371 | 371 | } else { |
| 372 | 372 | if (!$r[1] or $r[1] == 'COLUMN') { |
| 373 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 373 | + return spip_pg_query("ALTER TABLE $table DROP ".$r[2], $serveur); |
|
| 374 | 374 | } elseif ($r[1][0] == 'P') { |
| 375 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 375 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table".'_pkey', $serveur); |
|
| 376 | 376 | } else { |
| 377 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 377 | + return spip_pg_query('DROP INDEX '.$table.'_'.$r[2], $serveur); |
|
| 378 | 378 | } |
| 379 | 379 | } |
| 380 | 380 | } |
| 381 | 381 | |
| 382 | 382 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 383 | 383 | if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
| 384 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 384 | + spip_log("alter modify: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 385 | 385 | } else { |
| 386 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 386 | + return spip_pg_alter_change($table, $r[1].' '.$arg, $serveur = '', $requeter = true); |
|
| 387 | 387 | } |
| 388 | 388 | } |
| 389 | 389 | |
@@ -399,7 +399,7 @@ discard block |
||
| 399 | 399 | } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
| 400 | 400 | $rename = $r[2]; |
| 401 | 401 | } else { |
| 402 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 402 | + spip_log("alter rename: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
@@ -420,8 +420,8 @@ discard block |
||
| 420 | 420 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 421 | 421 | if (!($nom or $table or $champs)) { |
| 422 | 422 | spip_log( |
| 423 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 424 | - 'pg.' . _LOG_ERREUR |
|
| 423 | + "Champ manquant pour creer un index pg ($nom, $table, (".@join(',', $champs).'))', |
|
| 424 | + 'pg.'._LOG_ERREUR |
|
| 425 | 425 | ); |
| 426 | 426 | |
| 427 | 427 | return false; |
@@ -432,7 +432,7 @@ discard block |
||
| 432 | 432 | |
| 433 | 433 | // PG ne differentie pas noms des index en fonction des tables |
| 434 | 434 | // il faut donc creer des noms uniques d'index pour une base pg |
| 435 | - $nom = $table . '_' . $nom; |
|
| 435 | + $nom = $table.'_'.$nom; |
|
| 436 | 436 | // enlever d'eventuelles parentheses deja presentes sur champs |
| 437 | 437 | if (!is_array($champs)) { |
| 438 | 438 | if ($champs[0] == '(') { |
@@ -440,7 +440,7 @@ discard block |
||
| 440 | 440 | } |
| 441 | 441 | $champs = [$champs]; |
| 442 | 442 | } |
| 443 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 443 | + $query = "CREATE INDEX $nom ON $table (".join(',', $champs).')'; |
|
| 444 | 444 | if (!$requeter) { |
| 445 | 445 | return $query; |
| 446 | 446 | } |
@@ -463,7 +463,7 @@ discard block |
||
| 463 | 463 | } else { |
| 464 | 464 | $suite = ''; |
| 465 | 465 | } |
| 466 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 466 | + $query = 'EXPLAIN '.preg_replace('/([,\s])spip_/', '\1'.$prefixe.'_', $query).$suite; |
|
| 467 | 467 | |
| 468 | 468 | if (!$requeter) { |
| 469 | 469 | return $query; |
@@ -556,16 +556,16 @@ discard block |
||
| 556 | 556 | } |
| 557 | 557 | } |
| 558 | 558 | $from = spip_pg_from($from, $prefixe); |
| 559 | - $query = 'SELECT ' . $select |
|
| 559 | + $query = 'SELECT '.$select |
|
| 560 | 560 | . (!$from ? '' : "\nFROM $from") |
| 561 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 561 | + . (!$where ? '' : ("\nWHERE ".(!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 562 | 562 | "\n\tAND ", |
| 563 | 563 | array_map('calculer_pg_where', $where) |
| 564 | 564 | ))))) |
| 565 | 565 | . spip_pg_groupby($groupby, $from, $select) |
| 566 | 566 | . (!$having ? '' : "\nHAVING $having") |
| 567 | 567 | . ($orderby ? ("\nORDER BY $orderby") : '') |
| 568 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 568 | + . (!$limit ? '' : (" LIMIT $count".(!$offset ? '' : " OFFSET $offset"))); |
|
| 569 | 569 | |
| 570 | 570 | // renvoyer la requete inerte si demandee |
| 571 | 571 | if ($requeter === false) { |
@@ -586,7 +586,7 @@ discard block |
||
| 586 | 586 | $from = spip_pg_select_as($from); |
| 587 | 587 | } |
| 588 | 588 | |
| 589 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 589 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1'.$prefixe.'_', $from); |
|
| 590 | 590 | } |
| 591 | 591 | |
| 592 | 592 | function spip_pg_orderby($order, $select) { |
@@ -594,7 +594,7 @@ discard block |
||
| 594 | 594 | $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
| 595 | 595 | |
| 596 | 596 | foreach ($arg as $v) { |
| 597 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 597 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+'.$v.'/', $select, $m)) { |
|
| 598 | 598 | $res[] = $m[1]; |
| 599 | 599 | } else { |
| 600 | 600 | $res[] = $v; |
@@ -772,7 +772,7 @@ discard block |
||
| 772 | 772 | $n++; |
| 773 | 773 | $res .= "\nwhen $index=$v then $n"; |
| 774 | 774 | } |
| 775 | - $arg = $m[1] . "case $res else 0 end " |
|
| 775 | + $arg = $m[1]."case $res else 0 end " |
|
| 776 | 776 | . substr($arg, strlen($m[0])); |
| 777 | 777 | } |
| 778 | 778 | |
@@ -815,9 +815,9 @@ discard block |
||
| 815 | 815 | } |
| 816 | 816 | |
| 817 | 817 | if (strtoupper($join) === 'AND') { |
| 818 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 818 | + return $exp.join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 819 | 819 | } else { |
| 820 | - return $exp . join($join, $v); |
|
| 820 | + return $exp.join($join, $v); |
|
| 821 | 821 | } |
| 822 | 822 | } |
| 823 | 823 | |
@@ -827,7 +827,7 @@ discard block |
||
| 827 | 827 | if (substr($k, -1) == '@') { |
| 828 | 828 | // c'est une jointure qui se refere au from precedent |
| 829 | 829 | // pas de virgule |
| 830 | - $argsas .= ' ' . $v; |
|
| 830 | + $argsas .= ' '.$v; |
|
| 831 | 831 | } else { |
| 832 | 832 | $as = ''; |
| 833 | 833 | // spip_log("$k : $v", _LOG_DEBUG); |
@@ -837,7 +837,7 @@ discard block |
||
| 837 | 837 | } elseif ($v != $k) { |
| 838 | 838 | $p = strpos($v, ' '); |
| 839 | 839 | if ($p) { |
| 840 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 840 | + $v = substr($v, 0, $p)." AS $k".substr($v, $p); |
|
| 841 | 841 | } else { |
| 842 | 842 | $as = " AS $k"; |
| 843 | 843 | } |
@@ -845,7 +845,7 @@ discard block |
||
| 845 | 845 | } |
| 846 | 846 | // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
| 847 | 847 | // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
| 848 | - $argsas .= ', ' . $v . $as; |
|
| 848 | + $argsas .= ', '.$v.$as; |
|
| 849 | 849 | } |
| 850 | 850 | } |
| 851 | 851 | |
@@ -876,7 +876,7 @@ discard block |
||
| 876 | 876 | $serveur = '', |
| 877 | 877 | $requeter = true |
| 878 | 878 | ) { |
| 879 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 879 | + $c = !$groupby ? '*' : ('DISTINCT '.(is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 880 | 880 | $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
| 881 | 881 | if (!$requeter) { |
| 882 | 882 | return $r; |
@@ -977,8 +977,8 @@ discard block |
||
| 977 | 977 | |
| 978 | 978 | return spip_pg_insert( |
| 979 | 979 | $table, |
| 980 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 981 | - '(' . join(',', $couples) . ')', |
|
| 980 | + '('.join(',', array_keys($couples)).')', |
|
| 981 | + '('.join(',', $couples).')', |
|
| 982 | 982 | $desc, |
| 983 | 983 | $serveur, |
| 984 | 984 | $requeter |
@@ -1001,7 +1001,7 @@ discard block |
||
| 1001 | 1001 | $c = $tab_couples[0] ?? []; |
| 1002 | 1002 | $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
| 1003 | 1003 | |
| 1004 | - $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1004 | + $cles = '('.join(',', array_keys($les_cles)).')'; |
|
| 1005 | 1005 | $valeurs = []; |
| 1006 | 1006 | foreach ($tab_couples as $couples) { |
| 1007 | 1007 | foreach ($couples as $champ => $val) { |
@@ -1010,7 +1010,7 @@ discard block |
||
| 1010 | 1010 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
| 1011 | 1011 | $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
| 1012 | 1012 | |
| 1013 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1013 | + $valeurs[] = '('.join(',', $couples).')'; |
|
| 1014 | 1014 | } |
| 1015 | 1015 | $valeurs = implode(', ', $valeurs); |
| 1016 | 1016 | |
@@ -1031,7 +1031,7 @@ discard block |
||
| 1031 | 1031 | |
| 1032 | 1032 | $set = []; |
| 1033 | 1033 | foreach ($couples as $champ => $val) { |
| 1034 | - $set[] = $champ . '=' . $val; |
|
| 1034 | + $set[] = $champ.'='.$val; |
|
| 1035 | 1035 | } |
| 1036 | 1036 | |
| 1037 | 1037 | $query = calculer_pg_expression('UPDATE', $table, ',') |
@@ -1066,7 +1066,7 @@ discard block |
||
| 1066 | 1066 | |
| 1067 | 1067 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1068 | 1068 | if (!$values) { |
| 1069 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1069 | + spip_log("replace vide $table", 'pg.'._LOG_AVERTISSEMENT); |
|
| 1070 | 1070 | |
| 1071 | 1071 | return 0; |
| 1072 | 1072 | } |
@@ -1100,8 +1100,8 @@ discard block |
||
| 1100 | 1100 | if (!$where) { |
| 1101 | 1101 | return spip_pg_insert( |
| 1102 | 1102 | $table, |
| 1103 | - '(' . join(',', array_keys($values)) . ')', |
|
| 1104 | - '(' . join(',', $values) . ')', |
|
| 1103 | + '('.join(',', array_keys($values)).')', |
|
| 1104 | + '('.join(',', $values).')', |
|
| 1105 | 1105 | $desc, |
| 1106 | 1106 | $serveur |
| 1107 | 1107 | ); |
@@ -1122,12 +1122,11 @@ discard block |
||
| 1122 | 1122 | $couples = pg_affected_rows($couples); |
| 1123 | 1123 | } |
| 1124 | 1124 | if (!$couples) { |
| 1125 | - $ret = !$seq ? '' : |
|
| 1126 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1127 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1125 | + $ret = !$seq ? '' : (" RETURNING nextval('$seq') < $prim"); |
|
| 1126 | + $connexion['last'] = $q = "INSERT INTO $table (".join(',', array_keys($values)).') VALUES ('.join( |
|
| 1128 | 1127 | ',', |
| 1129 | 1128 | $values |
| 1130 | - ) . ")$ret"; |
|
| 1129 | + ).")$ret"; |
|
| 1131 | 1130 | $couples = spip_pg_query_simple($link, $q); |
| 1132 | 1131 | if (!$couples) { |
| 1133 | 1132 | return false; |
@@ -1175,7 +1174,7 @@ discard block |
||
| 1175 | 1174 | ) { |
| 1176 | 1175 | return ''; |
| 1177 | 1176 | } else { |
| 1178 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1177 | + return $raw ? $prim : $table.'_'.$prim.'_seq'; |
|
| 1179 | 1178 | } |
| 1180 | 1179 | } |
| 1181 | 1180 | |
@@ -1192,29 +1191,29 @@ discard block |
||
| 1192 | 1191 | return spip_pg_frommysql($v); |
| 1193 | 1192 | } else { |
| 1194 | 1193 | if (strncmp($v, '0000', 4) == 0) { |
| 1195 | - $v = '0001' . substr($v, 4); |
|
| 1194 | + $v = '0001'.substr($v, 4); |
|
| 1196 | 1195 | } |
| 1197 | 1196 | if (strpos($v, '-00-00') === 4) { |
| 1198 | - $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1197 | + $v = substr($v, 0, 4).'-01-01'.substr($v, 10); |
|
| 1199 | 1198 | } |
| 1200 | 1199 | |
| 1201 | 1200 | return "timestamp '$v'"; |
| 1202 | 1201 | } |
| 1203 | 1202 | } elseif (!sql_test_int($t)) { |
| 1204 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1203 | + return ("'".pg_escape_string($v)."'"); |
|
| 1205 | 1204 | } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
| 1206 | 1205 | return $v; |
| 1207 | 1206 | } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
| 1208 | 1207 | return substr($v, 1); |
| 1209 | 1208 | } else { |
| 1210 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1209 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.'._LOG_AVERTISSEMENT); |
|
| 1211 | 1210 | |
| 1212 | 1211 | return intval($v); |
| 1213 | 1212 | } |
| 1214 | 1213 | } |
| 1215 | 1214 | |
| 1216 | 1215 | function spip_pg_hex($v) { |
| 1217 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1216 | + return "CAST(x'".$v."' as bigint)"; |
|
| 1218 | 1217 | } |
| 1219 | 1218 | |
| 1220 | 1219 | function spip_pg_quote($v, $type = '') { |
@@ -1252,15 +1251,15 @@ discard block |
||
| 1252 | 1251 | return $not ? '0=0' : '0=1'; |
| 1253 | 1252 | } |
| 1254 | 1253 | if (strpos($valeurs, "CAST(x'") !== false) { |
| 1255 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1254 | + return "($val=".join("OR $val=", explode(',', $valeurs)).')'; |
|
| 1256 | 1255 | } |
| 1257 | 1256 | $n = $i = 0; |
| 1258 | 1257 | $in_sql = ''; |
| 1259 | 1258 | while ($n = strpos($valeurs, ',', $n + 1)) { |
| 1260 | 1259 | if ((++$i) >= 255) { |
| 1261 | - $in_sql .= "($val $not IN (" . |
|
| 1262 | - substr($valeurs, 0, $n) . |
|
| 1263 | - "))\n" . |
|
| 1260 | + $in_sql .= "($val $not IN (". |
|
| 1261 | + substr($valeurs, 0, $n). |
|
| 1262 | + "))\n". |
|
| 1264 | 1263 | ($not ? "AND\t" : "OR\t"); |
| 1265 | 1264 | $valeurs = substr($valeurs, $n + 1); |
| 1266 | 1265 | $i = $n = 0; |
@@ -1276,7 +1275,7 @@ discard block |
||
| 1276 | 1275 | $s = $link ? pg_last_error($link) : pg_last_error(); |
| 1277 | 1276 | if ($s) { |
| 1278 | 1277 | $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
| 1279 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1278 | + spip_log("$s - $query", 'pg.'._LOG_ERREUR); |
|
| 1280 | 1279 | } |
| 1281 | 1280 | |
| 1282 | 1281 | return $s; |
@@ -1324,7 +1323,7 @@ discard block |
||
| 1324 | 1323 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1325 | 1324 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 1326 | 1325 | $link = $connexion['link']; |
| 1327 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1326 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE '._q($match); |
|
| 1328 | 1327 | |
| 1329 | 1328 | return spip_pg_query_simple($link, $q); |
| 1330 | 1329 | } |
@@ -1332,7 +1331,7 @@ discard block |
||
| 1332 | 1331 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1333 | 1332 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 1334 | 1333 | $link = $connexion['link']; |
| 1335 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1334 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE '._q($nom_table); |
|
| 1336 | 1335 | |
| 1337 | 1336 | $res = spip_pg_query_simple($link, $q); |
| 1338 | 1337 | if (!$res) { |
@@ -1343,15 +1342,15 @@ discard block |
||
| 1343 | 1342 | // il faut en tenir compte dans le return |
| 1344 | 1343 | $fields = []; |
| 1345 | 1344 | while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
| 1346 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1345 | + $fields[$field[0]] = $field[2].(!$field[1] ? '' : (' DEFAULT '.$field[1])); |
|
| 1347 | 1346 | } |
| 1348 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1347 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE '._q($nom_table); |
|
| 1349 | 1348 | $res = spip_pg_query_simple($link, $q); |
| 1350 | 1349 | $keys = []; |
| 1351 | 1350 | while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
| 1352 | 1351 | if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
| 1353 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1354 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1352 | + $nom = str_replace($nom_table.'_', '', $r[2]); |
|
| 1353 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY '.$nom))] = $r[3]; |
|
| 1355 | 1354 | } |
| 1356 | 1355 | } |
| 1357 | 1356 | |
@@ -1383,16 +1382,16 @@ discard block |
||
| 1383 | 1382 | if (strpos($k, 'KEY ') === 0) { |
| 1384 | 1383 | $n = str_replace('`', '', $k); |
| 1385 | 1384 | $v = str_replace('`', '"', $v); |
| 1386 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1385 | + $i = $nom.preg_replace('/KEY +/', '_', $n); |
|
| 1387 | 1386 | if ($k != $n) { |
| 1388 | 1387 | $i = "\"$i\""; |
| 1389 | 1388 | } |
| 1390 | 1389 | $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
| 1391 | 1390 | } elseif (strpos($k, 'UNIQUE ') === 0) { |
| 1392 | 1391 | $k = preg_replace('/^UNIQUE +/', '', $k); |
| 1393 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1392 | + $prim .= "$s\n\t\tCONSTRAINT ".str_replace('`', '"', $k)." UNIQUE ($v)"; |
|
| 1394 | 1393 | } else { |
| 1395 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1394 | + $prim .= "$s\n\t\t".str_replace('`', '"', $k)." ($v)"; |
|
| 1396 | 1395 | } |
| 1397 | 1396 | if ($k == 'PRIMARY KEY') { |
| 1398 | 1397 | $prim_name = $v; |
@@ -1403,17 +1402,17 @@ discard block |
||
| 1403 | 1402 | |
| 1404 | 1403 | $character_set = ''; |
| 1405 | 1404 | if (@$GLOBALS['meta']['charset_sql_base']) { |
| 1406 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1405 | + $character_set .= ' CHARACTER SET '.$GLOBALS['meta']['charset_sql_base']; |
|
| 1407 | 1406 | } |
| 1408 | 1407 | if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
| 1409 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1408 | + $character_set .= ' COLLATE '.$GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1410 | 1409 | } |
| 1411 | 1410 | |
| 1412 | 1411 | foreach ($champs as $k => $v) { |
| 1413 | 1412 | $k = str_replace('`', '"', $k); |
| 1414 | 1413 | if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
| 1415 | 1414 | if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
| 1416 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1415 | + $v = $defs[1].$character_set.' '.substr($v, strlen($defs[1])); |
|
| 1417 | 1416 | } |
| 1418 | 1417 | } |
| 1419 | 1418 | |
@@ -1428,7 +1427,7 @@ discard block |
||
| 1428 | 1427 | |
| 1429 | 1428 | // En l'absence de "if not exists" en PG, on neutralise les erreurs |
| 1430 | 1429 | |
| 1431 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1430 | + $q = "CREATE $temporary TABLE $nom ($query".($prim ? ",$prim" : '').')'. |
|
| 1432 | 1431 | ($character_set ? " DEFAULT $character_set" : '') |
| 1433 | 1432 | . "\n"; |
| 1434 | 1433 | |
@@ -1439,7 +1438,7 @@ discard block |
||
| 1439 | 1438 | $r = @pg_query($link, $q); |
| 1440 | 1439 | |
| 1441 | 1440 | if (!$r) { |
| 1442 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1441 | + spip_log("Impossible de creer cette table: $q", 'pg.'._LOG_ERREUR); |
|
| 1443 | 1442 | } else { |
| 1444 | 1443 | foreach ($keys as $index) { |
| 1445 | 1444 | pg_query($link, $index); |
@@ -1462,20 +1461,20 @@ discard block |
||
| 1462 | 1461 | // vue deja presente |
| 1463 | 1462 | if (sql_showtable($nom, false, $serveur)) { |
| 1464 | 1463 | if ($requeter) { |
| 1465 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1464 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.'._LOG_ERREUR); |
|
| 1466 | 1465 | } |
| 1467 | 1466 | |
| 1468 | 1467 | return false; |
| 1469 | 1468 | } |
| 1470 | 1469 | |
| 1471 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1470 | + $query = "CREATE VIEW $nom AS ".$query_select; |
|
| 1472 | 1471 | |
| 1473 | 1472 | return spip_pg_query($query, $serveur, $requeter); |
| 1474 | 1473 | } |
| 1475 | 1474 | |
| 1476 | 1475 | |
| 1477 | 1476 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1478 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1477 | + spip_log('changement de charset sql a ecrire en PG', 'pg.'._LOG_ERREUR); |
|
| 1479 | 1478 | } |
| 1480 | 1479 | |
| 1481 | 1480 | |
@@ -1488,7 +1487,7 @@ discard block |
||
| 1488 | 1487 | * @return bool|string true / false / requete |
| 1489 | 1488 | **/ |
| 1490 | 1489 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1491 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1490 | + return spip_pg_query('VACUUM '.$table, $serveur, $requeter); |
|
| 1492 | 1491 | } |
| 1493 | 1492 | |
| 1494 | 1493 | // Selectionner la sous-chaine dans $objet |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - return; |
|
| 21 | + return; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | define('_DEFAULT_DB', 'spip'); |
@@ -31,159 +31,159 @@ discard block |
||
| 31 | 31 | // si ca ne marche toujours pas, echec. |
| 32 | 32 | |
| 33 | 33 | function req_pg_dist($addr, $port, $login, $pass, $db = '', $prefixe = '') { |
| 34 | - static $last_connect = []; |
|
| 35 | - if (!extension_loaded('pgsql')) { |
|
| 36 | - return false; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - // si provient de selectdb |
|
| 40 | - if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 41 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 42 | - ${$a} = $last_connect[$a]; |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - [$host, $p] = array_pad(explode(';', $addr), 2, null); |
|
| 46 | - if ($p > 0) { |
|
| 47 | - $port = " port=$p"; |
|
| 48 | - } else { |
|
| 49 | - $port = ''; |
|
| 50 | - } |
|
| 51 | - $erreurs = []; |
|
| 52 | - if ($db) { |
|
| 53 | - @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 54 | - } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 55 | - $erreurs[] = pg_last_error(); |
|
| 56 | - if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 57 | - $db = $login; |
|
| 58 | - } else { |
|
| 59 | - $erreurs[] = pg_last_error(); |
|
| 60 | - $db = _DEFAULT_DB; |
|
| 61 | - $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - if (!$link) { |
|
| 65 | - $erreurs[] = pg_last_error(); |
|
| 66 | - foreach ($erreurs as $e) { |
|
| 67 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return false; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - if ($link) { |
|
| 74 | - $last_connect = [ |
|
| 75 | - 'addr' => $addr, |
|
| 76 | - 'port' => $port, |
|
| 77 | - 'login' => $login, |
|
| 78 | - 'pass' => $pass, |
|
| 79 | - 'db' => $db, |
|
| 80 | - 'prefixe' => $prefixe, |
|
| 81 | - ]; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - spip_log( |
|
| 85 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 86 | - 'pg.' . _LOG_DEBUG |
|
| 87 | - ); |
|
| 88 | - |
|
| 89 | - return !$link ? false : [ |
|
| 90 | - 'db' => $db, |
|
| 91 | - 'prefixe' => $prefixe ?: $db, |
|
| 92 | - 'link' => $link, |
|
| 93 | - ]; |
|
| 34 | + static $last_connect = []; |
|
| 35 | + if (!extension_loaded('pgsql')) { |
|
| 36 | + return false; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + // si provient de selectdb |
|
| 40 | + if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 41 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 42 | + ${$a} = $last_connect[$a]; |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + [$host, $p] = array_pad(explode(';', $addr), 2, null); |
|
| 46 | + if ($p > 0) { |
|
| 47 | + $port = " port=$p"; |
|
| 48 | + } else { |
|
| 49 | + $port = ''; |
|
| 50 | + } |
|
| 51 | + $erreurs = []; |
|
| 52 | + if ($db) { |
|
| 53 | + @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 54 | + } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 55 | + $erreurs[] = pg_last_error(); |
|
| 56 | + if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 57 | + $db = $login; |
|
| 58 | + } else { |
|
| 59 | + $erreurs[] = pg_last_error(); |
|
| 60 | + $db = _DEFAULT_DB; |
|
| 61 | + $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + if (!$link) { |
|
| 65 | + $erreurs[] = pg_last_error(); |
|
| 66 | + foreach ($erreurs as $e) { |
|
| 67 | + spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + if ($link) { |
|
| 74 | + $last_connect = [ |
|
| 75 | + 'addr' => $addr, |
|
| 76 | + 'port' => $port, |
|
| 77 | + 'login' => $login, |
|
| 78 | + 'pass' => $pass, |
|
| 79 | + 'db' => $db, |
|
| 80 | + 'prefixe' => $prefixe, |
|
| 81 | + ]; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + spip_log( |
|
| 85 | + "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 86 | + 'pg.' . _LOG_DEBUG |
|
| 87 | + ); |
|
| 88 | + |
|
| 89 | + return !$link ? false : [ |
|
| 90 | + 'db' => $db, |
|
| 91 | + 'prefixe' => $prefixe ?: $db, |
|
| 92 | + 'link' => $link, |
|
| 93 | + ]; |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | $GLOBALS['spip_pg_functions_1'] = [ |
| 97 | - 'alter' => 'spip_pg_alter', |
|
| 98 | - 'count' => 'spip_pg_count', |
|
| 99 | - 'countsel' => 'spip_pg_countsel', |
|
| 100 | - 'create' => 'spip_pg_create', |
|
| 101 | - 'create_base' => 'spip_pg_create_base', |
|
| 102 | - 'create_view' => 'spip_pg_create_view', |
|
| 103 | - 'date_proche' => 'spip_pg_date_proche', |
|
| 104 | - 'delete' => 'spip_pg_delete', |
|
| 105 | - 'drop_table' => 'spip_pg_drop_table', |
|
| 106 | - 'drop_view' => 'spip_pg_drop_view', |
|
| 107 | - 'errno' => 'spip_pg_errno', |
|
| 108 | - 'error' => 'spip_pg_error', |
|
| 109 | - 'explain' => 'spip_pg_explain', |
|
| 110 | - 'fetch' => 'spip_pg_fetch', |
|
| 111 | - 'seek' => 'spip_pg_seek', |
|
| 112 | - 'free' => 'spip_pg_free', |
|
| 113 | - 'hex' => 'spip_pg_hex', |
|
| 114 | - 'in' => 'spip_pg_in', |
|
| 115 | - 'insert' => 'spip_pg_insert', |
|
| 116 | - 'insertq' => 'spip_pg_insertq', |
|
| 117 | - 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 118 | - 'listdbs' => 'spip_pg_listdbs', |
|
| 119 | - 'multi' => 'spip_pg_multi', |
|
| 120 | - 'optimize' => 'spip_pg_optimize', |
|
| 121 | - 'query' => 'spip_pg_query', |
|
| 122 | - 'quote' => 'spip_pg_quote', |
|
| 123 | - 'replace' => 'spip_pg_replace', |
|
| 124 | - 'replace_multi' => 'spip_pg_replace_multi', |
|
| 125 | - 'select' => 'spip_pg_select', |
|
| 126 | - 'selectdb' => 'spip_pg_selectdb', |
|
| 127 | - 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 128 | - 'showbase' => 'spip_pg_showbase', |
|
| 129 | - 'showtable' => 'spip_pg_showtable', |
|
| 130 | - 'update' => 'spip_pg_update', |
|
| 131 | - 'updateq' => 'spip_pg_updateq', |
|
| 97 | + 'alter' => 'spip_pg_alter', |
|
| 98 | + 'count' => 'spip_pg_count', |
|
| 99 | + 'countsel' => 'spip_pg_countsel', |
|
| 100 | + 'create' => 'spip_pg_create', |
|
| 101 | + 'create_base' => 'spip_pg_create_base', |
|
| 102 | + 'create_view' => 'spip_pg_create_view', |
|
| 103 | + 'date_proche' => 'spip_pg_date_proche', |
|
| 104 | + 'delete' => 'spip_pg_delete', |
|
| 105 | + 'drop_table' => 'spip_pg_drop_table', |
|
| 106 | + 'drop_view' => 'spip_pg_drop_view', |
|
| 107 | + 'errno' => 'spip_pg_errno', |
|
| 108 | + 'error' => 'spip_pg_error', |
|
| 109 | + 'explain' => 'spip_pg_explain', |
|
| 110 | + 'fetch' => 'spip_pg_fetch', |
|
| 111 | + 'seek' => 'spip_pg_seek', |
|
| 112 | + 'free' => 'spip_pg_free', |
|
| 113 | + 'hex' => 'spip_pg_hex', |
|
| 114 | + 'in' => 'spip_pg_in', |
|
| 115 | + 'insert' => 'spip_pg_insert', |
|
| 116 | + 'insertq' => 'spip_pg_insertq', |
|
| 117 | + 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 118 | + 'listdbs' => 'spip_pg_listdbs', |
|
| 119 | + 'multi' => 'spip_pg_multi', |
|
| 120 | + 'optimize' => 'spip_pg_optimize', |
|
| 121 | + 'query' => 'spip_pg_query', |
|
| 122 | + 'quote' => 'spip_pg_quote', |
|
| 123 | + 'replace' => 'spip_pg_replace', |
|
| 124 | + 'replace_multi' => 'spip_pg_replace_multi', |
|
| 125 | + 'select' => 'spip_pg_select', |
|
| 126 | + 'selectdb' => 'spip_pg_selectdb', |
|
| 127 | + 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 128 | + 'showbase' => 'spip_pg_showbase', |
|
| 129 | + 'showtable' => 'spip_pg_showtable', |
|
| 130 | + 'update' => 'spip_pg_update', |
|
| 131 | + 'updateq' => 'spip_pg_updateq', |
|
| 132 | 132 | ]; |
| 133 | 133 | |
| 134 | 134 | // Par ou ca passe une fois les traductions faites |
| 135 | 135 | function spip_pg_trace_query($query, $serveur = '') { |
| 136 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 137 | - $prefixe = $connexion['prefixe']; |
|
| 138 | - $link = $connexion['link']; |
|
| 139 | - $db = $connexion['db']; |
|
| 140 | - |
|
| 141 | - if (isset($_GET['var_profile'])) { |
|
| 142 | - include_spip('public/tracer'); |
|
| 143 | - $t = trace_query_start(); |
|
| 144 | - $e = ''; |
|
| 145 | - } else { |
|
| 146 | - $t = 0; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $connexion['last'] = $query; |
|
| 150 | - $r = spip_pg_query_simple($link, $query); |
|
| 151 | - |
|
| 152 | - // Log de l'erreur eventuelle |
|
| 153 | - if ($e = spip_pg_errno($serveur)) { |
|
| 154 | - $e .= spip_pg_error($query, $serveur); |
|
| 155 | - } // et du fautif |
|
| 156 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 136 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 137 | + $prefixe = $connexion['prefixe']; |
|
| 138 | + $link = $connexion['link']; |
|
| 139 | + $db = $connexion['db']; |
|
| 140 | + |
|
| 141 | + if (isset($_GET['var_profile'])) { |
|
| 142 | + include_spip('public/tracer'); |
|
| 143 | + $t = trace_query_start(); |
|
| 144 | + $e = ''; |
|
| 145 | + } else { |
|
| 146 | + $t = 0; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $connexion['last'] = $query; |
|
| 150 | + $r = spip_pg_query_simple($link, $query); |
|
| 151 | + |
|
| 152 | + // Log de l'erreur eventuelle |
|
| 153 | + if ($e = spip_pg_errno($serveur)) { |
|
| 154 | + $e .= spip_pg_error($query, $serveur); |
|
| 155 | + } // et du fautif |
|
| 156 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | // Fonction de requete generale quand on est sur que c'est SQL standard. |
| 160 | 160 | // Elle change juste le noms des tables ($table_prefix) dans le FROM etc |
| 161 | 161 | |
| 162 | 162 | function spip_pg_query($query, $serveur = '', $requeter = true) { |
| 163 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 164 | - $prefixe = $connexion['prefixe']; |
|
| 165 | - $link = $connexion['link']; |
|
| 166 | - $db = $connexion['db']; |
|
| 167 | - |
|
| 168 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 169 | - $suite = strstr($query, (string) $regs[0]); |
|
| 170 | - $query = substr($query, 0, -strlen($suite)); |
|
| 171 | - } else { |
|
| 172 | - $suite = ''; |
|
| 173 | - } |
|
| 174 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 175 | - |
|
| 176 | - // renvoyer la requete inerte si demandee |
|
| 177 | - if (!$requeter) { |
|
| 178 | - return $query; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - return spip_pg_trace_query($query, $serveur); |
|
| 163 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 164 | + $prefixe = $connexion['prefixe']; |
|
| 165 | + $link = $connexion['link']; |
|
| 166 | + $db = $connexion['db']; |
|
| 167 | + |
|
| 168 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 169 | + $suite = strstr($query, (string) $regs[0]); |
|
| 170 | + $query = substr($query, 0, -strlen($suite)); |
|
| 171 | + } else { |
|
| 172 | + $suite = ''; |
|
| 173 | + } |
|
| 174 | + $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 175 | + |
|
| 176 | + // renvoyer la requete inerte si demandee |
|
| 177 | + if (!$requeter) { |
|
| 178 | + return $query; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + return spip_pg_trace_query($query, $serveur); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | function spip_pg_query_simple($link, $query) { |
| 185 | - #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 186 | - return pg_query($link, $query); |
|
| 185 | + #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 186 | + return pg_query($link, $query); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | /* |
@@ -195,196 +195,196 @@ discard block |
||
| 195 | 195 | * de requetes showtable intempestives |
| 196 | 196 | */ |
| 197 | 197 | function spip_pg_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 198 | - static $tables = []; |
|
| 199 | - |
|
| 200 | - if (!isset($tables[$table])) { |
|
| 201 | - if (!$desc) { |
|
| 202 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 203 | - $desc = $trouver_table($table, $serveur); |
|
| 204 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 205 | - if (!$desc) { |
|
| 206 | - return $couples; |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 211 | - // cependant, il faudra peut etre etendre |
|
| 212 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 213 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 214 | - $tables[$table] = []; |
|
| 215 | - foreach ($desc['field'] as $k => $v) { |
|
| 216 | - $v = strtolower(ltrim($v)); |
|
| 217 | - // ne pas ajouter de timestamp now() si un default est specifie |
|
| 218 | - if (strpos($v, 'timestamp') === 0 and !str_contains($v, 'default')) { |
|
| 219 | - $tables[$table][] = $k; |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - // ajout des champs type 'timestamp' absents |
|
| 225 | - foreach ($tables[$table] as $maj) { |
|
| 226 | - if (!array_key_exists($maj, $couples)) { |
|
| 227 | - $couples[$maj] = 'NOW()'; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - return $couples; |
|
| 198 | + static $tables = []; |
|
| 199 | + |
|
| 200 | + if (!isset($tables[$table])) { |
|
| 201 | + if (!$desc) { |
|
| 202 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 203 | + $desc = $trouver_table($table, $serveur); |
|
| 204 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 205 | + if (!$desc) { |
|
| 206 | + return $couples; |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 211 | + // cependant, il faudra peut etre etendre |
|
| 212 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 213 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 214 | + $tables[$table] = []; |
|
| 215 | + foreach ($desc['field'] as $k => $v) { |
|
| 216 | + $v = strtolower(ltrim($v)); |
|
| 217 | + // ne pas ajouter de timestamp now() si un default est specifie |
|
| 218 | + if (strpos($v, 'timestamp') === 0 and !str_contains($v, 'default')) { |
|
| 219 | + $tables[$table][] = $k; |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + // ajout des champs type 'timestamp' absents |
|
| 225 | + foreach ($tables[$table] as $maj) { |
|
| 226 | + if (!array_key_exists($maj, $couples)) { |
|
| 227 | + $couples[$maj] = 'NOW()'; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + return $couples; |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | |
| 235 | 235 | // Alter en PG ne traite pas les index |
| 236 | 236 | function spip_pg_alter($query, $serveur = '', $requeter = true) { |
| 237 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 238 | - // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 239 | - // ou revoir l'api de sql_alter en creant un |
|
| 240 | - // sql_alter_table($table,array($actions)); |
|
| 241 | - if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 242 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 243 | - |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - $debut = $regs[1]; |
|
| 247 | - $table = $regs[3]; |
|
| 248 | - $suite = $regs[4]; |
|
| 249 | - $todo = explode(',', $suite); |
|
| 250 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 251 | - $todo2 = []; |
|
| 252 | - $i = 0; |
|
| 253 | - $ouverte = false; |
|
| 254 | - while ($do = array_shift($todo)) { |
|
| 255 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 256 | - $o = (str_contains($do, '(')); |
|
| 257 | - $f = (str_contains($do, ')')); |
|
| 258 | - if ($o and !$f) { |
|
| 259 | - $ouverte = true; |
|
| 260 | - } elseif ($f) { |
|
| 261 | - $ouverte = false; |
|
| 262 | - } |
|
| 263 | - if (!$ouverte) { |
|
| 264 | - $i++; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - $todo = $todo2; |
|
| 268 | - $query = $debut . ' ' . array_shift($todo); |
|
| 269 | - |
|
| 270 | - if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 271 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 272 | - } else { |
|
| 273 | - if ($r[1]) { |
|
| 274 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 275 | - } |
|
| 276 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 277 | - if (function_exists($f)) { |
|
| 278 | - $f($r[2], $r[4], $serveur, $requeter); |
|
| 279 | - } else { |
|
| 280 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - // Alter a plusieurs args. Faudrait optimiser. |
|
| 284 | - if ($todo) { |
|
| 285 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 286 | - } |
|
| 237 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 238 | + // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 239 | + // ou revoir l'api de sql_alter en creant un |
|
| 240 | + // sql_alter_table($table,array($actions)); |
|
| 241 | + if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 242 | + spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 243 | + |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + $debut = $regs[1]; |
|
| 247 | + $table = $regs[3]; |
|
| 248 | + $suite = $regs[4]; |
|
| 249 | + $todo = explode(',', $suite); |
|
| 250 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 251 | + $todo2 = []; |
|
| 252 | + $i = 0; |
|
| 253 | + $ouverte = false; |
|
| 254 | + while ($do = array_shift($todo)) { |
|
| 255 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 256 | + $o = (str_contains($do, '(')); |
|
| 257 | + $f = (str_contains($do, ')')); |
|
| 258 | + if ($o and !$f) { |
|
| 259 | + $ouverte = true; |
|
| 260 | + } elseif ($f) { |
|
| 261 | + $ouverte = false; |
|
| 262 | + } |
|
| 263 | + if (!$ouverte) { |
|
| 264 | + $i++; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + $todo = $todo2; |
|
| 268 | + $query = $debut . ' ' . array_shift($todo); |
|
| 269 | + |
|
| 270 | + if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 271 | + spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 272 | + } else { |
|
| 273 | + if ($r[1]) { |
|
| 274 | + spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 275 | + } |
|
| 276 | + $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 277 | + if (function_exists($f)) { |
|
| 278 | + $f($r[2], $r[4], $serveur, $requeter); |
|
| 279 | + } else { |
|
| 280 | + spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + // Alter a plusieurs args. Faudrait optimiser. |
|
| 284 | + if ($todo) { |
|
| 285 | + spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 286 | + } |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 290 | - if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 291 | - spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 292 | - } else { |
|
| 293 | - [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 294 | - $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 295 | - if ($null) { |
|
| 296 | - $actions[] = "ALTER $old SET NOT NULL"; |
|
| 297 | - } else { |
|
| 298 | - $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - if ($d = ($default ?: $def2)) { |
|
| 302 | - $actions[] = "ALTER $old SET $d"; |
|
| 303 | - } else { |
|
| 304 | - $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 308 | - |
|
| 309 | - if ($old != $new) { |
|
| 310 | - spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 311 | - } |
|
| 312 | - } |
|
| 290 | + if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 291 | + spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 292 | + } else { |
|
| 293 | + [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 294 | + $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 295 | + if ($null) { |
|
| 296 | + $actions[] = "ALTER $old SET NOT NULL"; |
|
| 297 | + } else { |
|
| 298 | + $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + if ($d = ($default ?: $def2)) { |
|
| 302 | + $actions[] = "ALTER $old SET $d"; |
|
| 303 | + } else { |
|
| 304 | + $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 308 | + |
|
| 309 | + if ($old != $new) { |
|
| 310 | + spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | function spip_pg_alter_add($table, $arg, $serveur = '', $requeter = true) { |
| 316 | - $nom_index = null; |
|
| 317 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 318 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 319 | - |
|
| 320 | - return null; |
|
| 321 | - } |
|
| 322 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 323 | - preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 324 | - if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 325 | - $m[2] = $n[1]; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 329 | - } elseif ($r[1][0] == 'P') { |
|
| 330 | - // la primary peut etre sur plusieurs champs |
|
| 331 | - $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 332 | - $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 333 | - |
|
| 334 | - return spip_pg_query( |
|
| 335 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 336 | - $serveur, |
|
| 337 | - $requeter |
|
| 338 | - ); |
|
| 339 | - } else { |
|
| 340 | - preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 341 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 342 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 343 | - // |
|
| 344 | - // nom_index (colonnes) |
|
| 345 | - if ($m[2]) { |
|
| 346 | - $colonnes = substr($m[2], 1, -1); |
|
| 347 | - $nom_index = $m[1]; |
|
| 348 | - } else { |
|
| 349 | - // (colonne) |
|
| 350 | - if ($m[1][0] == '(') { |
|
| 351 | - $colonnes = substr($m[1], 1, -1); |
|
| 352 | - if (str_contains(',', $colonnes)) { |
|
| 353 | - spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 354 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 355 | - } else { |
|
| 356 | - $nom_index = $colonnes; |
|
| 357 | - } |
|
| 358 | - } // nom_index |
|
| 359 | - else { |
|
| 360 | - $nom_index = $colonnes = $m[1]; |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 365 | - } |
|
| 316 | + $nom_index = null; |
|
| 317 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 318 | + spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 319 | + |
|
| 320 | + return null; |
|
| 321 | + } |
|
| 322 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 323 | + preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 324 | + if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 325 | + $m[2] = $n[1]; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 329 | + } elseif ($r[1][0] == 'P') { |
|
| 330 | + // la primary peut etre sur plusieurs champs |
|
| 331 | + $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 332 | + $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 333 | + |
|
| 334 | + return spip_pg_query( |
|
| 335 | + "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 336 | + $serveur, |
|
| 337 | + $requeter |
|
| 338 | + ); |
|
| 339 | + } else { |
|
| 340 | + preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 341 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 342 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 343 | + // |
|
| 344 | + // nom_index (colonnes) |
|
| 345 | + if ($m[2]) { |
|
| 346 | + $colonnes = substr($m[2], 1, -1); |
|
| 347 | + $nom_index = $m[1]; |
|
| 348 | + } else { |
|
| 349 | + // (colonne) |
|
| 350 | + if ($m[1][0] == '(') { |
|
| 351 | + $colonnes = substr($m[1], 1, -1); |
|
| 352 | + if (str_contains(',', $colonnes)) { |
|
| 353 | + spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 354 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 355 | + } else { |
|
| 356 | + $nom_index = $colonnes; |
|
| 357 | + } |
|
| 358 | + } // nom_index |
|
| 359 | + else { |
|
| 360 | + $nom_index = $colonnes = $m[1]; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 365 | + } |
|
| 366 | 366 | } |
| 367 | 367 | |
| 368 | 368 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 369 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 370 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 371 | - } else { |
|
| 372 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 373 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 374 | - } elseif ($r[1][0] == 'P') { |
|
| 375 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 376 | - } else { |
|
| 377 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 378 | - } |
|
| 379 | - } |
|
| 369 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 370 | + spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 371 | + } else { |
|
| 372 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 373 | + return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 374 | + } elseif ($r[1][0] == 'P') { |
|
| 375 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 376 | + } else { |
|
| 377 | + return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | 380 | } |
| 381 | 381 | |
| 382 | 382 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 383 | - if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 384 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 385 | - } else { |
|
| 386 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 387 | - } |
|
| 383 | + if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 384 | + spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 385 | + } else { |
|
| 386 | + return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 387 | + } |
|
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | // attention (en pg) : |
@@ -392,17 +392,17 @@ discard block |
||
| 392 | 392 | // - alter table A rename X to Y = changer le nom de la colonne X en Y |
| 393 | 393 | // pour l'instant, traiter simplement RENAME TO X |
| 394 | 394 | function spip_pg_alter_rename($table, $arg, $serveur = '', $requeter = true) { |
| 395 | - $rename = ''; |
|
| 396 | - // si TO, mais pas au debut |
|
| 397 | - if (!stripos($arg, 'TO ')) { |
|
| 398 | - $rename = $arg; |
|
| 399 | - } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 400 | - $rename = $r[2]; |
|
| 401 | - } else { |
|
| 402 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 395 | + $rename = ''; |
|
| 396 | + // si TO, mais pas au debut |
|
| 397 | + if (!stripos($arg, 'TO ')) { |
|
| 398 | + $rename = $arg; |
|
| 399 | + } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 400 | + $rename = $r[2]; |
|
| 401 | + } else { |
|
| 402 | + spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | |
@@ -418,59 +418,59 @@ discard block |
||
| 418 | 418 | * @return bool ou requete |
| 419 | 419 | */ |
| 420 | 420 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 421 | - if (!($nom or $table or $champs)) { |
|
| 422 | - spip_log( |
|
| 423 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 424 | - 'pg.' . _LOG_ERREUR |
|
| 425 | - ); |
|
| 426 | - |
|
| 427 | - return false; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - $nom = str_replace('`', '', $nom); |
|
| 431 | - $champs = str_replace('`', '', $champs); |
|
| 432 | - |
|
| 433 | - // PG ne differentie pas noms des index en fonction des tables |
|
| 434 | - // il faut donc creer des noms uniques d'index pour une base pg |
|
| 435 | - $nom = $table . '_' . $nom; |
|
| 436 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 437 | - if (!is_array($champs)) { |
|
| 438 | - if ($champs[0] == '(') { |
|
| 439 | - $champs = substr($champs, 1, -1); |
|
| 440 | - } |
|
| 441 | - $champs = [$champs]; |
|
| 442 | - } |
|
| 443 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 444 | - if (!$requeter) { |
|
| 445 | - return $query; |
|
| 446 | - } |
|
| 447 | - $res = spip_pg_query($query, $serveur, $requeter); |
|
| 448 | - |
|
| 449 | - return $res; |
|
| 421 | + if (!($nom or $table or $champs)) { |
|
| 422 | + spip_log( |
|
| 423 | + "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 424 | + 'pg.' . _LOG_ERREUR |
|
| 425 | + ); |
|
| 426 | + |
|
| 427 | + return false; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + $nom = str_replace('`', '', $nom); |
|
| 431 | + $champs = str_replace('`', '', $champs); |
|
| 432 | + |
|
| 433 | + // PG ne differentie pas noms des index en fonction des tables |
|
| 434 | + // il faut donc creer des noms uniques d'index pour une base pg |
|
| 435 | + $nom = $table . '_' . $nom; |
|
| 436 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 437 | + if (!is_array($champs)) { |
|
| 438 | + if ($champs[0] == '(') { |
|
| 439 | + $champs = substr($champs, 1, -1); |
|
| 440 | + } |
|
| 441 | + $champs = [$champs]; |
|
| 442 | + } |
|
| 443 | + $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 444 | + if (!$requeter) { |
|
| 445 | + return $query; |
|
| 446 | + } |
|
| 447 | + $res = spip_pg_query($query, $serveur, $requeter); |
|
| 448 | + |
|
| 449 | + return $res; |
|
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | |
| 453 | 453 | function spip_pg_explain($query, $serveur = '', $requeter = true) { |
| 454 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 455 | - return []; |
|
| 456 | - } |
|
| 457 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 458 | - $prefixe = $connexion['prefixe']; |
|
| 459 | - $link = $connexion['link']; |
|
| 460 | - if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 461 | - $suite = strstr($query, (string) $regs[0]); |
|
| 462 | - $query = substr($query, 0, -strlen($suite)); |
|
| 463 | - } else { |
|
| 464 | - $suite = ''; |
|
| 465 | - } |
|
| 466 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 467 | - |
|
| 468 | - if (!$requeter) { |
|
| 469 | - return $query; |
|
| 470 | - } |
|
| 471 | - $r = spip_pg_query_simple($link, $query); |
|
| 472 | - |
|
| 473 | - return spip_pg_fetch($r, null, $serveur); |
|
| 454 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 455 | + return []; |
|
| 456 | + } |
|
| 457 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 458 | + $prefixe = $connexion['prefixe']; |
|
| 459 | + $link = $connexion['link']; |
|
| 460 | + if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 461 | + $suite = strstr($query, (string) $regs[0]); |
|
| 462 | + $query = substr($query, 0, -strlen($suite)); |
|
| 463 | + } else { |
|
| 464 | + $suite = ''; |
|
| 465 | + } |
|
| 466 | + $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 467 | + |
|
| 468 | + if (!$requeter) { |
|
| 469 | + return $query; |
|
| 470 | + } |
|
| 471 | + $r = spip_pg_query_simple($link, $query); |
|
| 472 | + |
|
| 473 | + return spip_pg_fetch($r, null, $serveur); |
|
| 474 | 474 | } |
| 475 | 475 | |
| 476 | 476 | |
@@ -489,92 +489,92 @@ discard block |
||
| 489 | 489 | * - False en cas d'erreur. |
| 490 | 490 | **/ |
| 491 | 491 | function spip_pg_selectdb($db, $serveur = '', $requeter = true) { |
| 492 | - // se connecter a la base indiquee |
|
| 493 | - // avec les identifiants connus |
|
| 494 | - $index = $serveur ? strtolower($serveur) : 0; |
|
| 492 | + // se connecter a la base indiquee |
|
| 493 | + // avec les identifiants connus |
|
| 494 | + $index = $serveur ? strtolower($serveur) : 0; |
|
| 495 | 495 | |
| 496 | - if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 497 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 498 | - return $db; |
|
| 499 | - } |
|
| 500 | - } |
|
| 496 | + if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 497 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 498 | + return $db; |
|
| 499 | + } |
|
| 500 | + } |
|
| 501 | 501 | |
| 502 | - return false; |
|
| 502 | + return false; |
|
| 503 | 503 | } |
| 504 | 504 | |
| 505 | 505 | // Qu'une seule base pour le moment |
| 506 | 506 | |
| 507 | 507 | function spip_pg_listdbs($serveur) { |
| 508 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 509 | - $link = $connexion['link']; |
|
| 510 | - $dbs = []; |
|
| 511 | - $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 512 | - while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 513 | - $dbs[] = reset($row); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - return $dbs; |
|
| 508 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 509 | + $link = $connexion['link']; |
|
| 510 | + $dbs = []; |
|
| 511 | + $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 512 | + while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 513 | + $dbs[] = reset($row); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + return $dbs; |
|
| 517 | 517 | } |
| 518 | 518 | |
| 519 | 519 | function spip_pg_select( |
| 520 | - $select, |
|
| 521 | - $from, |
|
| 522 | - $where = '', |
|
| 523 | - $groupby = [], |
|
| 524 | - $orderby = '', |
|
| 525 | - $limit = '', |
|
| 526 | - $having = '', |
|
| 527 | - $serveur = '', |
|
| 528 | - $requeter = true |
|
| 520 | + $select, |
|
| 521 | + $from, |
|
| 522 | + $where = '', |
|
| 523 | + $groupby = [], |
|
| 524 | + $orderby = '', |
|
| 525 | + $limit = '', |
|
| 526 | + $having = '', |
|
| 527 | + $serveur = '', |
|
| 528 | + $requeter = true |
|
| 529 | 529 | ) { |
| 530 | 530 | |
| 531 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 532 | - $prefixe = $connexion['prefixe']; |
|
| 533 | - $link = $connexion['link']; |
|
| 534 | - $db = $connexion['db']; |
|
| 535 | - |
|
| 536 | - $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 537 | - if ($limit) { |
|
| 538 | - $offset = $limatch[2]; |
|
| 539 | - $count = $limatch[3]; |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - $select = spip_pg_frommysql($select); |
|
| 543 | - |
|
| 544 | - // si pas de tri explicitement demande, le GROUP BY ne |
|
| 545 | - // contient que la clef primaire. |
|
| 546 | - // lui ajouter alors le champ de tri par defaut |
|
| 547 | - if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 548 | - $groupby[] = $groupbyplus[1]; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - $orderby = spip_pg_orderby($orderby, $select); |
|
| 552 | - |
|
| 553 | - if ($having) { |
|
| 554 | - if (is_array($having)) { |
|
| 555 | - $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 556 | - } |
|
| 557 | - } |
|
| 558 | - $from = spip_pg_from($from, $prefixe); |
|
| 559 | - $query = 'SELECT ' . $select |
|
| 560 | - . (!$from ? '' : "\nFROM $from") |
|
| 561 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 562 | - "\n\tAND ", |
|
| 563 | - array_map('calculer_pg_where', $where) |
|
| 564 | - ))))) |
|
| 565 | - . spip_pg_groupby($groupby, $from, $select) |
|
| 566 | - . (!$having ? '' : "\nHAVING $having") |
|
| 567 | - . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 568 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 569 | - |
|
| 570 | - // renvoyer la requete inerte si demandee |
|
| 571 | - if ($requeter === false) { |
|
| 572 | - return $query; |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - $r = spip_pg_trace_query($query, $serveur); |
|
| 576 | - |
|
| 577 | - return $r ?: $query; |
|
| 531 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 532 | + $prefixe = $connexion['prefixe']; |
|
| 533 | + $link = $connexion['link']; |
|
| 534 | + $db = $connexion['db']; |
|
| 535 | + |
|
| 536 | + $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 537 | + if ($limit) { |
|
| 538 | + $offset = $limatch[2]; |
|
| 539 | + $count = $limatch[3]; |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + $select = spip_pg_frommysql($select); |
|
| 543 | + |
|
| 544 | + // si pas de tri explicitement demande, le GROUP BY ne |
|
| 545 | + // contient que la clef primaire. |
|
| 546 | + // lui ajouter alors le champ de tri par defaut |
|
| 547 | + if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 548 | + $groupby[] = $groupbyplus[1]; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + $orderby = spip_pg_orderby($orderby, $select); |
|
| 552 | + |
|
| 553 | + if ($having) { |
|
| 554 | + if (is_array($having)) { |
|
| 555 | + $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 556 | + } |
|
| 557 | + } |
|
| 558 | + $from = spip_pg_from($from, $prefixe); |
|
| 559 | + $query = 'SELECT ' . $select |
|
| 560 | + . (!$from ? '' : "\nFROM $from") |
|
| 561 | + . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 562 | + "\n\tAND ", |
|
| 563 | + array_map('calculer_pg_where', $where) |
|
| 564 | + ))))) |
|
| 565 | + . spip_pg_groupby($groupby, $from, $select) |
|
| 566 | + . (!$having ? '' : "\nHAVING $having") |
|
| 567 | + . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 568 | + . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 569 | + |
|
| 570 | + // renvoyer la requete inerte si demandee |
|
| 571 | + if ($requeter === false) { |
|
| 572 | + return $query; |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + $r = spip_pg_trace_query($query, $serveur); |
|
| 576 | + |
|
| 577 | + return $r ?: $query; |
|
| 578 | 578 | ; |
| 579 | 579 | } |
| 580 | 580 | |
@@ -582,26 +582,26 @@ discard block |
||
| 582 | 582 | // car le reste de la requete utilise les alias (AS) systematiquement |
| 583 | 583 | |
| 584 | 584 | function spip_pg_from($from, $prefixe) { |
| 585 | - if (is_array($from)) { |
|
| 586 | - $from = spip_pg_select_as($from); |
|
| 587 | - } |
|
| 585 | + if (is_array($from)) { |
|
| 586 | + $from = spip_pg_select_as($from); |
|
| 587 | + } |
|
| 588 | 588 | |
| 589 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 589 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 590 | 590 | } |
| 591 | 591 | |
| 592 | 592 | function spip_pg_orderby($order, $select) { |
| 593 | - $res = []; |
|
| 594 | - $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 595 | - |
|
| 596 | - foreach ($arg as $v) { |
|
| 597 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 598 | - $res[] = $m[1]; |
|
| 599 | - } else { |
|
| 600 | - $res[] = $v; |
|
| 601 | - } |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - return spip_pg_frommysql(join(',', $res)); |
|
| 593 | + $res = []; |
|
| 594 | + $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 595 | + |
|
| 596 | + foreach ($arg as $v) { |
|
| 597 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 598 | + $res[] = $m[1]; |
|
| 599 | + } else { |
|
| 600 | + $res[] = $v; |
|
| 601 | + } |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + return spip_pg_frommysql(join(',', $res)); |
|
| 605 | 605 | } |
| 606 | 606 | |
| 607 | 607 | // Conversion a l'arrach' des jointures MySQL en jointures PG |
@@ -609,56 +609,56 @@ discard block |
||
| 609 | 609 | // et pour enlever les repetitions (sans incidence de perf, mais ca fait sale) |
| 610 | 610 | |
| 611 | 611 | function spip_pg_groupby($groupby, $from, $select) { |
| 612 | - $join = strpos($from, ','); |
|
| 613 | - // ismplifier avant de decouper |
|
| 614 | - if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 615 | - $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - if ($join or $groupby) { |
|
| 619 | - $join = is_array($select) ? $select : explode(', ', $select); |
|
| 620 | - } |
|
| 621 | - if ($join) { |
|
| 622 | - // enlever les 0 as points, '', ... |
|
| 623 | - foreach ($join as $k => $v) { |
|
| 624 | - $v = str_replace('DISTINCT ', '', $v); |
|
| 625 | - // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 626 | - $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 627 | - $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 628 | - // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 629 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 630 | - // idem sans AS (fetch numerique) |
|
| 631 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 632 | - // des AS simples : on garde le cote droit du AS |
|
| 633 | - $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 634 | - // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 635 | - if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 636 | - unset($join[$k]); |
|
| 637 | - } else { |
|
| 638 | - $join[$k] = trim($v); |
|
| 639 | - } |
|
| 640 | - } |
|
| 641 | - $join = array_diff($join, ['']); |
|
| 642 | - $join = implode(',', $join); |
|
| 643 | - } |
|
| 644 | - if (is_array($groupby)) { |
|
| 645 | - $groupby = join(',', $groupby); |
|
| 646 | - } |
|
| 647 | - if ($join) { |
|
| 648 | - $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 649 | - } |
|
| 650 | - if (!$groupby) { |
|
| 651 | - return ''; |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - $groupby = spip_pg_frommysql($groupby); |
|
| 655 | - // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 656 | - // issue de prepare_recherche |
|
| 657 | - $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 658 | - $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 659 | - $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 660 | - |
|
| 661 | - return "\nGROUP BY $groupby"; |
|
| 612 | + $join = strpos($from, ','); |
|
| 613 | + // ismplifier avant de decouper |
|
| 614 | + if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 615 | + $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + if ($join or $groupby) { |
|
| 619 | + $join = is_array($select) ? $select : explode(', ', $select); |
|
| 620 | + } |
|
| 621 | + if ($join) { |
|
| 622 | + // enlever les 0 as points, '', ... |
|
| 623 | + foreach ($join as $k => $v) { |
|
| 624 | + $v = str_replace('DISTINCT ', '', $v); |
|
| 625 | + // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 626 | + $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 627 | + $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 628 | + // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 629 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 630 | + // idem sans AS (fetch numerique) |
|
| 631 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 632 | + // des AS simples : on garde le cote droit du AS |
|
| 633 | + $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 634 | + // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 635 | + if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 636 | + unset($join[$k]); |
|
| 637 | + } else { |
|
| 638 | + $join[$k] = trim($v); |
|
| 639 | + } |
|
| 640 | + } |
|
| 641 | + $join = array_diff($join, ['']); |
|
| 642 | + $join = implode(',', $join); |
|
| 643 | + } |
|
| 644 | + if (is_array($groupby)) { |
|
| 645 | + $groupby = join(',', $groupby); |
|
| 646 | + } |
|
| 647 | + if ($join) { |
|
| 648 | + $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 649 | + } |
|
| 650 | + if (!$groupby) { |
|
| 651 | + return ''; |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + $groupby = spip_pg_frommysql($groupby); |
|
| 655 | + // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 656 | + // issue de prepare_recherche |
|
| 657 | + $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 658 | + $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 659 | + $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 660 | + |
|
| 661 | + return "\nGROUP BY $groupby"; |
|
| 662 | 662 | } |
| 663 | 663 | |
| 664 | 664 | // Conversion des operateurs MySQL en PG |
@@ -669,492 +669,492 @@ discard block |
||
| 669 | 669 | // A ameliorer. |
| 670 | 670 | |
| 671 | 671 | function spip_pg_frommysql($arg) { |
| 672 | - if (is_array($arg)) { |
|
| 673 | - $arg = join(', ', $arg); |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - $res = spip_pg_fromfield($arg); |
|
| 677 | - |
|
| 678 | - $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 679 | - |
|
| 680 | - $res = preg_replace( |
|
| 681 | - '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 682 | - 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 683 | - $res |
|
| 684 | - ); |
|
| 685 | - $res = preg_replace( |
|
| 686 | - '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 687 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 688 | - $res |
|
| 689 | - ); |
|
| 690 | - $res = preg_replace( |
|
| 691 | - '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 692 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 693 | - $res |
|
| 694 | - ); |
|
| 695 | - |
|
| 696 | - $res = preg_replace( |
|
| 697 | - '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 698 | - ' EXTRACT(epoch FROM NOW())', |
|
| 699 | - $res |
|
| 700 | - ); |
|
| 701 | - |
|
| 702 | - // la fonction md5(integer) n'est pas connu en pg |
|
| 703 | - // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 704 | - $res = preg_replace( |
|
| 705 | - '/md5\s*[(]([^)]*)[)]/i', |
|
| 706 | - 'MD5(CAST(\1 AS text))', |
|
| 707 | - $res |
|
| 708 | - ); |
|
| 709 | - |
|
| 710 | - $res = preg_replace( |
|
| 711 | - '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 712 | - ' EXTRACT(epoch FROM \1)', |
|
| 713 | - $res |
|
| 714 | - ); |
|
| 715 | - |
|
| 716 | - $res = preg_replace( |
|
| 717 | - '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 718 | - ' EXTRACT(day FROM \1)', |
|
| 719 | - $res |
|
| 720 | - ); |
|
| 721 | - |
|
| 722 | - $res = preg_replace( |
|
| 723 | - '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 724 | - ' EXTRACT(month FROM \1)', |
|
| 725 | - $res |
|
| 726 | - ); |
|
| 727 | - |
|
| 728 | - $res = preg_replace( |
|
| 729 | - '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 730 | - ' EXTRACT(year FROM \1)', |
|
| 731 | - $res |
|
| 732 | - ); |
|
| 733 | - |
|
| 734 | - $res = preg_replace( |
|
| 735 | - '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 736 | - ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 737 | - $res |
|
| 738 | - ); |
|
| 739 | - |
|
| 740 | - $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 741 | - |
|
| 742 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 743 | - |
|
| 744 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 745 | - |
|
| 746 | - $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 747 | - $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 748 | - $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 749 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 750 | - $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 751 | - |
|
| 752 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 753 | - $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 754 | - |
|
| 755 | - $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 756 | - $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 672 | + if (is_array($arg)) { |
|
| 673 | + $arg = join(', ', $arg); |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + $res = spip_pg_fromfield($arg); |
|
| 677 | + |
|
| 678 | + $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 679 | + |
|
| 680 | + $res = preg_replace( |
|
| 681 | + '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 682 | + 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 683 | + $res |
|
| 684 | + ); |
|
| 685 | + $res = preg_replace( |
|
| 686 | + '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 687 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 688 | + $res |
|
| 689 | + ); |
|
| 690 | + $res = preg_replace( |
|
| 691 | + '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 692 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 693 | + $res |
|
| 694 | + ); |
|
| 695 | + |
|
| 696 | + $res = preg_replace( |
|
| 697 | + '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 698 | + ' EXTRACT(epoch FROM NOW())', |
|
| 699 | + $res |
|
| 700 | + ); |
|
| 701 | + |
|
| 702 | + // la fonction md5(integer) n'est pas connu en pg |
|
| 703 | + // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 704 | + $res = preg_replace( |
|
| 705 | + '/md5\s*[(]([^)]*)[)]/i', |
|
| 706 | + 'MD5(CAST(\1 AS text))', |
|
| 707 | + $res |
|
| 708 | + ); |
|
| 709 | + |
|
| 710 | + $res = preg_replace( |
|
| 711 | + '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 712 | + ' EXTRACT(epoch FROM \1)', |
|
| 713 | + $res |
|
| 714 | + ); |
|
| 715 | + |
|
| 716 | + $res = preg_replace( |
|
| 717 | + '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 718 | + ' EXTRACT(day FROM \1)', |
|
| 719 | + $res |
|
| 720 | + ); |
|
| 721 | + |
|
| 722 | + $res = preg_replace( |
|
| 723 | + '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 724 | + ' EXTRACT(month FROM \1)', |
|
| 725 | + $res |
|
| 726 | + ); |
|
| 727 | + |
|
| 728 | + $res = preg_replace( |
|
| 729 | + '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 730 | + ' EXTRACT(year FROM \1)', |
|
| 731 | + $res |
|
| 732 | + ); |
|
| 733 | + |
|
| 734 | + $res = preg_replace( |
|
| 735 | + '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 736 | + ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 737 | + $res |
|
| 738 | + ); |
|
| 739 | + |
|
| 740 | + $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 741 | + |
|
| 742 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 743 | + |
|
| 744 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 745 | + |
|
| 746 | + $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 747 | + $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 748 | + $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 749 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 750 | + $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 751 | + |
|
| 752 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 753 | + $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 754 | + |
|
| 755 | + $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 756 | + $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 757 | 757 | # correct en theorie mais produit des debordements arithmetiques |
| 758 | 758 | # $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)(timestamp *'[^']*' *[+-] *timestamp *'[^']*') *[)]/", '\2', $res); |
| 759 | - $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 760 | - $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 759 | + $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 760 | + $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 761 | 761 | |
| 762 | - return str_replace('REGEXP', '~', $res); |
|
| 762 | + return str_replace('REGEXP', '~', $res); |
|
| 763 | 763 | } |
| 764 | 764 | |
| 765 | 765 | function spip_pg_fromfield($arg) { |
| 766 | - while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 767 | - preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 768 | - $res = ''; |
|
| 769 | - $n = 0; |
|
| 770 | - $index = $m[2]; |
|
| 771 | - foreach ($r[1] as $v) { |
|
| 772 | - $n++; |
|
| 773 | - $res .= "\nwhen $index=$v then $n"; |
|
| 774 | - } |
|
| 775 | - $arg = $m[1] . "case $res else 0 end " |
|
| 776 | - . substr($arg, strlen($m[0])); |
|
| 777 | - } |
|
| 778 | - |
|
| 779 | - return $arg; |
|
| 766 | + while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 767 | + preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 768 | + $res = ''; |
|
| 769 | + $n = 0; |
|
| 770 | + $index = $m[2]; |
|
| 771 | + foreach ($r[1] as $v) { |
|
| 772 | + $n++; |
|
| 773 | + $res .= "\nwhen $index=$v then $n"; |
|
| 774 | + } |
|
| 775 | + $arg = $m[1] . "case $res else 0 end " |
|
| 776 | + . substr($arg, strlen($m[0])); |
|
| 777 | + } |
|
| 778 | + |
|
| 779 | + return $arg; |
|
| 780 | 780 | } |
| 781 | 781 | |
| 782 | 782 | function calculer_pg_where($v) { |
| 783 | - if (!is_array($v)) { |
|
| 784 | - return spip_pg_frommysql($v); |
|
| 785 | - } |
|
| 786 | - |
|
| 787 | - $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 788 | - if (!($n = count($v))) { |
|
| 789 | - return $op; |
|
| 790 | - } else { |
|
| 791 | - $arg = calculer_pg_where(array_shift($v)); |
|
| 792 | - if ($n == 1) { |
|
| 793 | - return "$op($arg)"; |
|
| 794 | - } else { |
|
| 795 | - $arg2 = calculer_pg_where(array_shift($v)); |
|
| 796 | - if ($n == 2) { |
|
| 797 | - return "($arg $op $arg2)"; |
|
| 798 | - } else { |
|
| 799 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 800 | - } |
|
| 801 | - } |
|
| 802 | - } |
|
| 783 | + if (!is_array($v)) { |
|
| 784 | + return spip_pg_frommysql($v); |
|
| 785 | + } |
|
| 786 | + |
|
| 787 | + $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 788 | + if (!($n = count($v))) { |
|
| 789 | + return $op; |
|
| 790 | + } else { |
|
| 791 | + $arg = calculer_pg_where(array_shift($v)); |
|
| 792 | + if ($n == 1) { |
|
| 793 | + return "$op($arg)"; |
|
| 794 | + } else { |
|
| 795 | + $arg2 = calculer_pg_where(array_shift($v)); |
|
| 796 | + if ($n == 2) { |
|
| 797 | + return "($arg $op $arg2)"; |
|
| 798 | + } else { |
|
| 799 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 800 | + } |
|
| 801 | + } |
|
| 802 | + } |
|
| 803 | 803 | } |
| 804 | 804 | |
| 805 | 805 | |
| 806 | 806 | function calculer_pg_expression($expression, $v, $join = 'AND') { |
| 807 | - if (empty($v)) { |
|
| 808 | - return ''; |
|
| 809 | - } |
|
| 807 | + if (empty($v)) { |
|
| 808 | + return ''; |
|
| 809 | + } |
|
| 810 | 810 | |
| 811 | - $exp = "\n$expression "; |
|
| 811 | + $exp = "\n$expression "; |
|
| 812 | 812 | |
| 813 | - if (!is_array($v)) { |
|
| 814 | - $v = [$v]; |
|
| 815 | - } |
|
| 813 | + if (!is_array($v)) { |
|
| 814 | + $v = [$v]; |
|
| 815 | + } |
|
| 816 | 816 | |
| 817 | - if (strtoupper($join) === 'AND') { |
|
| 818 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 819 | - } else { |
|
| 820 | - return $exp . join($join, $v); |
|
| 821 | - } |
|
| 817 | + if (strtoupper($join) === 'AND') { |
|
| 818 | + return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 819 | + } else { |
|
| 820 | + return $exp . join($join, $v); |
|
| 821 | + } |
|
| 822 | 822 | } |
| 823 | 823 | |
| 824 | 824 | function spip_pg_select_as($args) { |
| 825 | - $argsas = ''; |
|
| 826 | - foreach ($args as $k => $v) { |
|
| 827 | - if (substr($k, -1) == '@') { |
|
| 828 | - // c'est une jointure qui se refere au from precedent |
|
| 829 | - // pas de virgule |
|
| 830 | - $argsas .= ' ' . $v; |
|
| 831 | - } else { |
|
| 832 | - $as = ''; |
|
| 833 | - // spip_log("$k : $v", _LOG_DEBUG); |
|
| 834 | - if (!is_numeric($k)) { |
|
| 835 | - if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 836 | - $v = $k; |
|
| 837 | - } elseif ($v != $k) { |
|
| 838 | - $p = strpos($v, ' '); |
|
| 839 | - if ($p) { |
|
| 840 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 841 | - } else { |
|
| 842 | - $as = " AS $k"; |
|
| 843 | - } |
|
| 844 | - } |
|
| 845 | - } |
|
| 846 | - // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 847 | - // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 848 | - $argsas .= ', ' . $v . $as; |
|
| 849 | - } |
|
| 850 | - } |
|
| 851 | - |
|
| 852 | - return substr($argsas, 2); |
|
| 825 | + $argsas = ''; |
|
| 826 | + foreach ($args as $k => $v) { |
|
| 827 | + if (substr($k, -1) == '@') { |
|
| 828 | + // c'est une jointure qui se refere au from precedent |
|
| 829 | + // pas de virgule |
|
| 830 | + $argsas .= ' ' . $v; |
|
| 831 | + } else { |
|
| 832 | + $as = ''; |
|
| 833 | + // spip_log("$k : $v", _LOG_DEBUG); |
|
| 834 | + if (!is_numeric($k)) { |
|
| 835 | + if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 836 | + $v = $k; |
|
| 837 | + } elseif ($v != $k) { |
|
| 838 | + $p = strpos($v, ' '); |
|
| 839 | + if ($p) { |
|
| 840 | + $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 841 | + } else { |
|
| 842 | + $as = " AS $k"; |
|
| 843 | + } |
|
| 844 | + } |
|
| 845 | + } |
|
| 846 | + // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 847 | + // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 848 | + $argsas .= ', ' . $v . $as; |
|
| 849 | + } |
|
| 850 | + } |
|
| 851 | + |
|
| 852 | + return substr($argsas, 2); |
|
| 853 | 853 | } |
| 854 | 854 | |
| 855 | 855 | function spip_pg_fetch($res, $t = '', $serveur = '', $requeter = true) { |
| 856 | 856 | |
| 857 | - if ($res) { |
|
| 858 | - $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 859 | - } |
|
| 857 | + if ($res) { |
|
| 858 | + $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 859 | + } |
|
| 860 | 860 | |
| 861 | - return $res; |
|
| 861 | + return $res; |
|
| 862 | 862 | } |
| 863 | 863 | |
| 864 | 864 | function spip_pg_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 865 | - if ($r) { |
|
| 866 | - return pg_result_seek($r, $row_number); |
|
| 867 | - } |
|
| 865 | + if ($r) { |
|
| 866 | + return pg_result_seek($r, $row_number); |
|
| 867 | + } |
|
| 868 | 868 | } |
| 869 | 869 | |
| 870 | 870 | |
| 871 | 871 | function spip_pg_countsel( |
| 872 | - $from = [], |
|
| 873 | - $where = [], |
|
| 874 | - $groupby = [], |
|
| 875 | - $having = [], |
|
| 876 | - $serveur = '', |
|
| 877 | - $requeter = true |
|
| 872 | + $from = [], |
|
| 873 | + $where = [], |
|
| 874 | + $groupby = [], |
|
| 875 | + $having = [], |
|
| 876 | + $serveur = '', |
|
| 877 | + $requeter = true |
|
| 878 | 878 | ) { |
| 879 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 880 | - $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 881 | - if (!$requeter) { |
|
| 882 | - return $r; |
|
| 883 | - } |
|
| 884 | - if (!is_resource($r)) { |
|
| 885 | - return 0; |
|
| 886 | - } |
|
| 887 | - [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 888 | - |
|
| 889 | - return $c; |
|
| 879 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 880 | + $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 881 | + if (!$requeter) { |
|
| 882 | + return $r; |
|
| 883 | + } |
|
| 884 | + if (!is_resource($r)) { |
|
| 885 | + return 0; |
|
| 886 | + } |
|
| 887 | + [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 888 | + |
|
| 889 | + return $c; |
|
| 890 | 890 | } |
| 891 | 891 | |
| 892 | 892 | function spip_pg_count($res, $serveur = '', $requeter = true) { |
| 893 | - return !$res ? 0 : pg_numrows($res); |
|
| 893 | + return !$res ? 0 : pg_numrows($res); |
|
| 894 | 894 | } |
| 895 | 895 | |
| 896 | 896 | function spip_pg_free($res, $serveur = '', $requeter = true) { |
| 897 | - // rien a faire en postgres |
|
| 897 | + // rien a faire en postgres |
|
| 898 | 898 | } |
| 899 | 899 | |
| 900 | 900 | function spip_pg_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 901 | 901 | |
| 902 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 903 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 902 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 903 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 904 | 904 | |
| 905 | - $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 906 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 905 | + $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 906 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 907 | 907 | |
| 908 | - // renvoyer la requete inerte si demandee |
|
| 909 | - if (!$requeter) { |
|
| 910 | - return $query; |
|
| 911 | - } |
|
| 908 | + // renvoyer la requete inerte si demandee |
|
| 909 | + if (!$requeter) { |
|
| 910 | + return $query; |
|
| 911 | + } |
|
| 912 | 912 | |
| 913 | - $res = spip_pg_trace_query($query, $serveur); |
|
| 914 | - if ($res) { |
|
| 915 | - return pg_affected_rows($res); |
|
| 916 | - } else { |
|
| 917 | - return false; |
|
| 918 | - } |
|
| 913 | + $res = spip_pg_trace_query($query, $serveur); |
|
| 914 | + if ($res) { |
|
| 915 | + return pg_affected_rows($res); |
|
| 916 | + } else { |
|
| 917 | + return false; |
|
| 918 | + } |
|
| 919 | 919 | } |
| 920 | 920 | |
| 921 | 921 | function spip_pg_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 922 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 923 | - $prefixe = $connexion['prefixe']; |
|
| 924 | - $link = $connexion['link']; |
|
| 925 | - |
|
| 926 | - if (!$desc) { |
|
| 927 | - $desc = description_table($table, $serveur); |
|
| 928 | - } |
|
| 929 | - $seq = spip_pg_sequence($table, true); |
|
| 930 | - // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 931 | - if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 932 | - $seq = spip_pg_sequence($table); |
|
| 933 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 934 | - $seq = "currval('$seq')"; |
|
| 935 | - } |
|
| 936 | - |
|
| 937 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 938 | - $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 939 | - $ins = (strlen($champs) < 3) |
|
| 940 | - ? ' DEFAULT VALUES' |
|
| 941 | - : "$champs VALUES $valeurs"; |
|
| 942 | - $q = "INSERT INTO $table $ins $ret"; |
|
| 943 | - if (!$requeter) { |
|
| 944 | - return $q; |
|
| 945 | - } |
|
| 946 | - $connexion['last'] = $q; |
|
| 947 | - $r = spip_pg_query_simple($link, $q); |
|
| 922 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 923 | + $prefixe = $connexion['prefixe']; |
|
| 924 | + $link = $connexion['link']; |
|
| 925 | + |
|
| 926 | + if (!$desc) { |
|
| 927 | + $desc = description_table($table, $serveur); |
|
| 928 | + } |
|
| 929 | + $seq = spip_pg_sequence($table, true); |
|
| 930 | + // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 931 | + if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 932 | + $seq = spip_pg_sequence($table); |
|
| 933 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 934 | + $seq = "currval('$seq')"; |
|
| 935 | + } |
|
| 936 | + |
|
| 937 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 938 | + $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 939 | + $ins = (strlen($champs) < 3) |
|
| 940 | + ? ' DEFAULT VALUES' |
|
| 941 | + : "$champs VALUES $valeurs"; |
|
| 942 | + $q = "INSERT INTO $table $ins $ret"; |
|
| 943 | + if (!$requeter) { |
|
| 944 | + return $q; |
|
| 945 | + } |
|
| 946 | + $connexion['last'] = $q; |
|
| 947 | + $r = spip_pg_query_simple($link, $q); |
|
| 948 | 948 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 949 | - if ($r) { |
|
| 950 | - if (!$ret) { |
|
| 951 | - return 0; |
|
| 952 | - } |
|
| 953 | - if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 954 | - return $r2[0]; |
|
| 955 | - } |
|
| 956 | - } |
|
| 957 | - |
|
| 958 | - return false; |
|
| 949 | + if ($r) { |
|
| 950 | + if (!$ret) { |
|
| 951 | + return 0; |
|
| 952 | + } |
|
| 953 | + if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 954 | + return $r2[0]; |
|
| 955 | + } |
|
| 956 | + } |
|
| 957 | + |
|
| 958 | + return false; |
|
| 959 | 959 | } |
| 960 | 960 | |
| 961 | 961 | function spip_pg_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 962 | 962 | |
| 963 | - if (!$desc) { |
|
| 964 | - $desc = description_table($table, $serveur); |
|
| 965 | - } |
|
| 966 | - if (!$desc) { |
|
| 967 | - die("$table insertion sans description"); |
|
| 968 | - } |
|
| 969 | - $fields = $desc['field']; |
|
| 970 | - |
|
| 971 | - foreach ($couples as $champ => $val) { |
|
| 972 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 973 | - } |
|
| 974 | - |
|
| 975 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 976 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 977 | - |
|
| 978 | - return spip_pg_insert( |
|
| 979 | - $table, |
|
| 980 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 981 | - '(' . join(',', $couples) . ')', |
|
| 982 | - $desc, |
|
| 983 | - $serveur, |
|
| 984 | - $requeter |
|
| 985 | - ); |
|
| 963 | + if (!$desc) { |
|
| 964 | + $desc = description_table($table, $serveur); |
|
| 965 | + } |
|
| 966 | + if (!$desc) { |
|
| 967 | + die("$table insertion sans description"); |
|
| 968 | + } |
|
| 969 | + $fields = $desc['field']; |
|
| 970 | + |
|
| 971 | + foreach ($couples as $champ => $val) { |
|
| 972 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 973 | + } |
|
| 974 | + |
|
| 975 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 976 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 977 | + |
|
| 978 | + return spip_pg_insert( |
|
| 979 | + $table, |
|
| 980 | + '(' . join(',', array_keys($couples)) . ')', |
|
| 981 | + '(' . join(',', $couples) . ')', |
|
| 982 | + $desc, |
|
| 983 | + $serveur, |
|
| 984 | + $requeter |
|
| 985 | + ); |
|
| 986 | 986 | } |
| 987 | 987 | |
| 988 | 988 | |
| 989 | 989 | function spip_pg_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 990 | 990 | |
| 991 | - if (!$desc) { |
|
| 992 | - $desc = description_table($table, $serveur); |
|
| 993 | - } |
|
| 994 | - if (!$desc) { |
|
| 995 | - die("$table insertion sans description"); |
|
| 996 | - } |
|
| 997 | - $fields = $desc['field'] ?? []; |
|
| 998 | - |
|
| 999 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1000 | - // une premiere fois pour ajouter maj dans les cles |
|
| 1001 | - $c = $tab_couples[0] ?? []; |
|
| 1002 | - $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1003 | - |
|
| 1004 | - $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1005 | - $valeurs = []; |
|
| 1006 | - foreach ($tab_couples as $couples) { |
|
| 1007 | - foreach ($couples as $champ => $val) { |
|
| 1008 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1009 | - } |
|
| 1010 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1011 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1012 | - |
|
| 1013 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1014 | - } |
|
| 1015 | - $valeurs = implode(', ', $valeurs); |
|
| 1016 | - |
|
| 1017 | - return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 991 | + if (!$desc) { |
|
| 992 | + $desc = description_table($table, $serveur); |
|
| 993 | + } |
|
| 994 | + if (!$desc) { |
|
| 995 | + die("$table insertion sans description"); |
|
| 996 | + } |
|
| 997 | + $fields = $desc['field'] ?? []; |
|
| 998 | + |
|
| 999 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1000 | + // une premiere fois pour ajouter maj dans les cles |
|
| 1001 | + $c = $tab_couples[0] ?? []; |
|
| 1002 | + $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1003 | + |
|
| 1004 | + $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1005 | + $valeurs = []; |
|
| 1006 | + foreach ($tab_couples as $couples) { |
|
| 1007 | + foreach ($couples as $champ => $val) { |
|
| 1008 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1009 | + } |
|
| 1010 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1011 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1012 | + |
|
| 1013 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1014 | + } |
|
| 1015 | + $valeurs = implode(', ', $valeurs); |
|
| 1016 | + |
|
| 1017 | + return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1018 | 1018 | } |
| 1019 | 1019 | |
| 1020 | 1020 | |
| 1021 | 1021 | function spip_pg_update($table, $couples, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1022 | 1022 | |
| 1023 | - if (!$couples) { |
|
| 1024 | - return; |
|
| 1025 | - } |
|
| 1026 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1027 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1023 | + if (!$couples) { |
|
| 1024 | + return; |
|
| 1025 | + } |
|
| 1026 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1027 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1028 | 1028 | |
| 1029 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1030 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1029 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1030 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1031 | 1031 | |
| 1032 | - $set = []; |
|
| 1033 | - foreach ($couples as $champ => $val) { |
|
| 1034 | - $set[] = $champ . '=' . $val; |
|
| 1035 | - } |
|
| 1032 | + $set = []; |
|
| 1033 | + foreach ($couples as $champ => $val) { |
|
| 1034 | + $set[] = $champ . '=' . $val; |
|
| 1035 | + } |
|
| 1036 | 1036 | |
| 1037 | - $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1038 | - . calculer_pg_expression('SET', $set, ',') |
|
| 1039 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1037 | + $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1038 | + . calculer_pg_expression('SET', $set, ',') |
|
| 1039 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1040 | 1040 | |
| 1041 | - // renvoyer la requete inerte si demandee |
|
| 1042 | - if (!$requeter) { |
|
| 1043 | - return $query; |
|
| 1044 | - } |
|
| 1041 | + // renvoyer la requete inerte si demandee |
|
| 1042 | + if (!$requeter) { |
|
| 1043 | + return $query; |
|
| 1044 | + } |
|
| 1045 | 1045 | |
| 1046 | - return spip_pg_trace_query($query, $serveur); |
|
| 1046 | + return spip_pg_trace_query($query, $serveur); |
|
| 1047 | 1047 | } |
| 1048 | 1048 | |
| 1049 | 1049 | // idem, mais les valeurs sont des constantes a mettre entre apostrophes |
| 1050 | 1050 | // sauf les expressions de date lorsqu'il s'agit de fonctions SQL (NOW etc) |
| 1051 | 1051 | function spip_pg_updateq($table, $couples, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1052 | - if (!$couples) { |
|
| 1053 | - return; |
|
| 1054 | - } |
|
| 1055 | - if (!$desc) { |
|
| 1056 | - $desc = description_table($table, $serveur); |
|
| 1057 | - } |
|
| 1058 | - $fields = $desc['field']; |
|
| 1059 | - foreach ($couples as $k => $val) { |
|
| 1060 | - $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1052 | + if (!$couples) { |
|
| 1053 | + return; |
|
| 1054 | + } |
|
| 1055 | + if (!$desc) { |
|
| 1056 | + $desc = description_table($table, $serveur); |
|
| 1057 | + } |
|
| 1058 | + $fields = $desc['field']; |
|
| 1059 | + foreach ($couples as $k => $val) { |
|
| 1060 | + $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1064 | 1064 | } |
| 1065 | 1065 | |
| 1066 | 1066 | |
| 1067 | 1067 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1068 | - if (!$values) { |
|
| 1069 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1070 | - |
|
| 1071 | - return 0; |
|
| 1072 | - } |
|
| 1073 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1074 | - $prefixe = $connexion['prefixe']; |
|
| 1075 | - $link = $connexion['link']; |
|
| 1076 | - |
|
| 1077 | - if (!$desc) { |
|
| 1078 | - $desc = description_table($table, $serveur); |
|
| 1079 | - } |
|
| 1080 | - if (!$desc) { |
|
| 1081 | - die("$table insertion sans description"); |
|
| 1082 | - } |
|
| 1083 | - $prim = $desc['key']['PRIMARY KEY']; |
|
| 1084 | - $ids = preg_split('/,\s*/', $prim); |
|
| 1085 | - $noprims = $prims = []; |
|
| 1086 | - foreach ($values as $k => $v) { |
|
| 1087 | - $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1088 | - |
|
| 1089 | - if (!in_array($k, $ids)) { |
|
| 1090 | - $noprims[$k] = "$k=$v"; |
|
| 1091 | - } else { |
|
| 1092 | - $prims[$k] = "$k=$v"; |
|
| 1093 | - } |
|
| 1094 | - } |
|
| 1095 | - |
|
| 1096 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1097 | - $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1098 | - |
|
| 1099 | - $where = join(' AND ', $prims); |
|
| 1100 | - if (!$where) { |
|
| 1101 | - return spip_pg_insert( |
|
| 1102 | - $table, |
|
| 1103 | - '(' . join(',', array_keys($values)) . ')', |
|
| 1104 | - '(' . join(',', $values) . ')', |
|
| 1105 | - $desc, |
|
| 1106 | - $serveur |
|
| 1107 | - ); |
|
| 1108 | - } |
|
| 1109 | - $couples = join(',', $noprims); |
|
| 1110 | - |
|
| 1111 | - $seq = spip_pg_sequence($table); |
|
| 1112 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 1113 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1114 | - |
|
| 1115 | - $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1116 | - if ($couples) { |
|
| 1117 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1068 | + if (!$values) { |
|
| 1069 | + spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1070 | + |
|
| 1071 | + return 0; |
|
| 1072 | + } |
|
| 1073 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1074 | + $prefixe = $connexion['prefixe']; |
|
| 1075 | + $link = $connexion['link']; |
|
| 1076 | + |
|
| 1077 | + if (!$desc) { |
|
| 1078 | + $desc = description_table($table, $serveur); |
|
| 1079 | + } |
|
| 1080 | + if (!$desc) { |
|
| 1081 | + die("$table insertion sans description"); |
|
| 1082 | + } |
|
| 1083 | + $prim = $desc['key']['PRIMARY KEY']; |
|
| 1084 | + $ids = preg_split('/,\s*/', $prim); |
|
| 1085 | + $noprims = $prims = []; |
|
| 1086 | + foreach ($values as $k => $v) { |
|
| 1087 | + $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1088 | + |
|
| 1089 | + if (!in_array($k, $ids)) { |
|
| 1090 | + $noprims[$k] = "$k=$v"; |
|
| 1091 | + } else { |
|
| 1092 | + $prims[$k] = "$k=$v"; |
|
| 1093 | + } |
|
| 1094 | + } |
|
| 1095 | + |
|
| 1096 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1097 | + $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1098 | + |
|
| 1099 | + $where = join(' AND ', $prims); |
|
| 1100 | + if (!$where) { |
|
| 1101 | + return spip_pg_insert( |
|
| 1102 | + $table, |
|
| 1103 | + '(' . join(',', array_keys($values)) . ')', |
|
| 1104 | + '(' . join(',', $values) . ')', |
|
| 1105 | + $desc, |
|
| 1106 | + $serveur |
|
| 1107 | + ); |
|
| 1108 | + } |
|
| 1109 | + $couples = join(',', $noprims); |
|
| 1110 | + |
|
| 1111 | + $seq = spip_pg_sequence($table); |
|
| 1112 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 1113 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1114 | + |
|
| 1115 | + $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1116 | + if ($couples) { |
|
| 1117 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1118 | 1118 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 1119 | - if (!$couples) { |
|
| 1120 | - return false; |
|
| 1121 | - } |
|
| 1122 | - $couples = pg_affected_rows($couples); |
|
| 1123 | - } |
|
| 1124 | - if (!$couples) { |
|
| 1125 | - $ret = !$seq ? '' : |
|
| 1126 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1127 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1128 | - ',', |
|
| 1129 | - $values |
|
| 1130 | - ) . ")$ret"; |
|
| 1131 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1132 | - if (!$couples) { |
|
| 1133 | - return false; |
|
| 1134 | - } elseif ($ret) { |
|
| 1135 | - $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1136 | - if ($r[0]) { |
|
| 1137 | - $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1138 | - // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1139 | - // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1140 | - $r = @pg_query($link, $q); |
|
| 1141 | - } |
|
| 1142 | - } |
|
| 1143 | - } |
|
| 1144 | - |
|
| 1145 | - return $couples; |
|
| 1119 | + if (!$couples) { |
|
| 1120 | + return false; |
|
| 1121 | + } |
|
| 1122 | + $couples = pg_affected_rows($couples); |
|
| 1123 | + } |
|
| 1124 | + if (!$couples) { |
|
| 1125 | + $ret = !$seq ? '' : |
|
| 1126 | + (" RETURNING nextval('$seq') < $prim"); |
|
| 1127 | + $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1128 | + ',', |
|
| 1129 | + $values |
|
| 1130 | + ) . ")$ret"; |
|
| 1131 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1132 | + if (!$couples) { |
|
| 1133 | + return false; |
|
| 1134 | + } elseif ($ret) { |
|
| 1135 | + $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1136 | + if ($r[0]) { |
|
| 1137 | + $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1138 | + // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1139 | + // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1140 | + $r = @pg_query($link, $q); |
|
| 1141 | + } |
|
| 1142 | + } |
|
| 1143 | + } |
|
| 1144 | + |
|
| 1145 | + return $couples; |
|
| 1146 | 1146 | } |
| 1147 | 1147 | |
| 1148 | 1148 | |
| 1149 | 1149 | function spip_pg_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1150 | - $retour = null; |
|
| 1151 | - // boucler pour traiter chaque requete independemment |
|
| 1152 | - foreach ($tab_couples as $couples) { |
|
| 1153 | - $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1154 | - } |
|
| 1155 | - |
|
| 1156 | - // renvoie le dernier id |
|
| 1157 | - return $retour; |
|
| 1150 | + $retour = null; |
|
| 1151 | + // boucler pour traiter chaque requete independemment |
|
| 1152 | + foreach ($tab_couples as $couples) { |
|
| 1153 | + $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1154 | + } |
|
| 1155 | + |
|
| 1156 | + // renvoie le dernier id |
|
| 1157 | + return $retour; |
|
| 1158 | 1158 | } |
| 1159 | 1159 | |
| 1160 | 1160 | |
@@ -1163,149 +1163,149 @@ discard block |
||
| 1163 | 1163 | |
| 1164 | 1164 | function spip_pg_sequence($table, $raw = false) { |
| 1165 | 1165 | |
| 1166 | - include_spip('base/serial'); |
|
| 1167 | - if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1168 | - return false; |
|
| 1169 | - } |
|
| 1170 | - $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1171 | - $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1172 | - if ( |
|
| 1173 | - !preg_match('/^\w+$/', $prim) |
|
| 1174 | - or !str_contains($desc['field'][$prim], 'int') |
|
| 1175 | - ) { |
|
| 1176 | - return ''; |
|
| 1177 | - } else { |
|
| 1178 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1179 | - } |
|
| 1166 | + include_spip('base/serial'); |
|
| 1167 | + if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1168 | + return false; |
|
| 1169 | + } |
|
| 1170 | + $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1171 | + $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1172 | + if ( |
|
| 1173 | + !preg_match('/^\w+$/', $prim) |
|
| 1174 | + or !str_contains($desc['field'][$prim], 'int') |
|
| 1175 | + ) { |
|
| 1176 | + return ''; |
|
| 1177 | + } else { |
|
| 1178 | + return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1179 | + } |
|
| 1180 | 1180 | } |
| 1181 | 1181 | |
| 1182 | 1182 | // Explicite les conversions de Mysql d'une valeur $v de type $t |
| 1183 | 1183 | // Dans le cas d'un champ date, pas d'apostrophe, c'est une syntaxe ad hoc |
| 1184 | 1184 | |
| 1185 | 1185 | function spip_pg_cite($v, $t) { |
| 1186 | - if (is_null($v)) { |
|
| 1187 | - return 'NULL'; |
|
| 1188 | - } // null php se traduit en NULL SQL |
|
| 1189 | - |
|
| 1190 | - if (sql_test_date($t)) { |
|
| 1191 | - if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1192 | - return spip_pg_frommysql($v); |
|
| 1193 | - } else { |
|
| 1194 | - if (strncmp($v, '0000', 4) == 0) { |
|
| 1195 | - $v = '0001' . substr($v, 4); |
|
| 1196 | - } |
|
| 1197 | - if (strpos($v, '-00-00') === 4) { |
|
| 1198 | - $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1199 | - } |
|
| 1200 | - |
|
| 1201 | - return "timestamp '$v'"; |
|
| 1202 | - } |
|
| 1203 | - } elseif (!sql_test_int($t)) { |
|
| 1204 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1205 | - } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1206 | - return $v; |
|
| 1207 | - } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1208 | - return substr($v, 1); |
|
| 1209 | - } else { |
|
| 1210 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1211 | - |
|
| 1212 | - return intval($v); |
|
| 1213 | - } |
|
| 1186 | + if (is_null($v)) { |
|
| 1187 | + return 'NULL'; |
|
| 1188 | + } // null php se traduit en NULL SQL |
|
| 1189 | + |
|
| 1190 | + if (sql_test_date($t)) { |
|
| 1191 | + if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1192 | + return spip_pg_frommysql($v); |
|
| 1193 | + } else { |
|
| 1194 | + if (strncmp($v, '0000', 4) == 0) { |
|
| 1195 | + $v = '0001' . substr($v, 4); |
|
| 1196 | + } |
|
| 1197 | + if (strpos($v, '-00-00') === 4) { |
|
| 1198 | + $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1199 | + } |
|
| 1200 | + |
|
| 1201 | + return "timestamp '$v'"; |
|
| 1202 | + } |
|
| 1203 | + } elseif (!sql_test_int($t)) { |
|
| 1204 | + return ("'" . pg_escape_string($v) . "'"); |
|
| 1205 | + } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1206 | + return $v; |
|
| 1207 | + } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1208 | + return substr($v, 1); |
|
| 1209 | + } else { |
|
| 1210 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1211 | + |
|
| 1212 | + return intval($v); |
|
| 1213 | + } |
|
| 1214 | 1214 | } |
| 1215 | 1215 | |
| 1216 | 1216 | function spip_pg_hex($v) { |
| 1217 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1217 | + return "CAST(x'" . $v . "' as bigint)"; |
|
| 1218 | 1218 | } |
| 1219 | 1219 | |
| 1220 | 1220 | function spip_pg_quote($v, $type = '') { |
| 1221 | - if (!is_array($v)) { |
|
| 1222 | - return spip_pg_cite($v, $type); |
|
| 1223 | - } |
|
| 1224 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1225 | - foreach ($v as $k => $r) { |
|
| 1226 | - $v[$k] = spip_pg_quote($r, $type); |
|
| 1227 | - } |
|
| 1228 | - |
|
| 1229 | - return join(',', $v); |
|
| 1221 | + if (!is_array($v)) { |
|
| 1222 | + return spip_pg_cite($v, $type); |
|
| 1223 | + } |
|
| 1224 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1225 | + foreach ($v as $k => $r) { |
|
| 1226 | + $v[$k] = spip_pg_quote($r, $type); |
|
| 1227 | + } |
|
| 1228 | + |
|
| 1229 | + return join(',', $v); |
|
| 1230 | 1230 | } |
| 1231 | 1231 | |
| 1232 | 1232 | function spip_pg_date_proche($champ, $interval, $unite) { |
| 1233 | - return '(' |
|
| 1234 | - . $champ |
|
| 1235 | - . (($interval <= 0) ? '>' : '<') |
|
| 1236 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1237 | - . '(' |
|
| 1238 | - . sql_quote(date('Y-m-d H:i:s')) |
|
| 1239 | - . ', INTERVAL ' |
|
| 1240 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1241 | - . ' ' |
|
| 1242 | - . $unite |
|
| 1243 | - . '))'; |
|
| 1233 | + return '(' |
|
| 1234 | + . $champ |
|
| 1235 | + . (($interval <= 0) ? '>' : '<') |
|
| 1236 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1237 | + . '(' |
|
| 1238 | + . sql_quote(date('Y-m-d H:i:s')) |
|
| 1239 | + . ', INTERVAL ' |
|
| 1240 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1241 | + . ' ' |
|
| 1242 | + . $unite |
|
| 1243 | + . '))'; |
|
| 1244 | 1244 | } |
| 1245 | 1245 | |
| 1246 | 1246 | function spip_pg_in($val, $valeurs, $not = '', $serveur = '') { |
| 1247 | 1247 | // |
| 1248 | 1248 | // IN (...) souvent limite a 255 elements, d'ou cette fonction assistante |
| 1249 | 1249 | // |
| 1250 | - // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1251 | - if (!$valeurs) { |
|
| 1252 | - return $not ? '0=0' : '0=1'; |
|
| 1253 | - } |
|
| 1254 | - if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1255 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1256 | - } |
|
| 1257 | - $n = $i = 0; |
|
| 1258 | - $in_sql = ''; |
|
| 1259 | - while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1260 | - if ((++$i) >= 255) { |
|
| 1261 | - $in_sql .= "($val $not IN (" . |
|
| 1262 | - substr($valeurs, 0, $n) . |
|
| 1263 | - "))\n" . |
|
| 1264 | - ($not ? "AND\t" : "OR\t"); |
|
| 1265 | - $valeurs = substr($valeurs, $n + 1); |
|
| 1266 | - $i = $n = 0; |
|
| 1267 | - } |
|
| 1268 | - } |
|
| 1269 | - $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1270 | - |
|
| 1271 | - return "($in_sql)"; |
|
| 1250 | + // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1251 | + if (!$valeurs) { |
|
| 1252 | + return $not ? '0=0' : '0=1'; |
|
| 1253 | + } |
|
| 1254 | + if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1255 | + return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1256 | + } |
|
| 1257 | + $n = $i = 0; |
|
| 1258 | + $in_sql = ''; |
|
| 1259 | + while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1260 | + if ((++$i) >= 255) { |
|
| 1261 | + $in_sql .= "($val $not IN (" . |
|
| 1262 | + substr($valeurs, 0, $n) . |
|
| 1263 | + "))\n" . |
|
| 1264 | + ($not ? "AND\t" : "OR\t"); |
|
| 1265 | + $valeurs = substr($valeurs, $n + 1); |
|
| 1266 | + $i = $n = 0; |
|
| 1267 | + } |
|
| 1268 | + } |
|
| 1269 | + $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1270 | + |
|
| 1271 | + return "($in_sql)"; |
|
| 1272 | 1272 | } |
| 1273 | 1273 | |
| 1274 | 1274 | function spip_pg_error($query = '', $serveur = '', $requeter = true) { |
| 1275 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1276 | - $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1277 | - if ($s) { |
|
| 1278 | - $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1279 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1280 | - } |
|
| 1281 | - |
|
| 1282 | - return $s; |
|
| 1275 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1276 | + $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1277 | + if ($s) { |
|
| 1278 | + $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1279 | + spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1280 | + } |
|
| 1281 | + |
|
| 1282 | + return $s; |
|
| 1283 | 1283 | } |
| 1284 | 1284 | |
| 1285 | 1285 | function spip_pg_errno($serveur = '') { |
| 1286 | - // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1287 | - // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1288 | - return 0; |
|
| 1286 | + // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1287 | + // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1288 | + return 0; |
|
| 1289 | 1289 | } |
| 1290 | 1290 | |
| 1291 | 1291 | function spip_pg_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 1292 | - if ($exist) { |
|
| 1293 | - $exist = ' IF EXISTS'; |
|
| 1294 | - } |
|
| 1295 | - if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1296 | - return true; |
|
| 1297 | - } else { |
|
| 1298 | - return false; |
|
| 1299 | - } |
|
| 1292 | + if ($exist) { |
|
| 1293 | + $exist = ' IF EXISTS'; |
|
| 1294 | + } |
|
| 1295 | + if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1296 | + return true; |
|
| 1297 | + } else { |
|
| 1298 | + return false; |
|
| 1299 | + } |
|
| 1300 | 1300 | } |
| 1301 | 1301 | |
| 1302 | 1302 | // supprime une vue |
| 1303 | 1303 | function spip_pg_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 1304 | - if ($exist) { |
|
| 1305 | - $exist = ' IF EXISTS'; |
|
| 1306 | - } |
|
| 1304 | + if ($exist) { |
|
| 1305 | + $exist = ' IF EXISTS'; |
|
| 1306 | + } |
|
| 1307 | 1307 | |
| 1308 | - return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1308 | + return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1309 | 1309 | } |
| 1310 | 1310 | |
| 1311 | 1311 | /** |
@@ -1322,40 +1322,40 @@ discard block |
||
| 1322 | 1322 | * Ressource à utiliser avec sql_fetch() |
| 1323 | 1323 | **/ |
| 1324 | 1324 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1325 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1326 | - $link = $connexion['link']; |
|
| 1327 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1325 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1326 | + $link = $connexion['link']; |
|
| 1327 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1328 | 1328 | |
| 1329 | - return spip_pg_query_simple($link, $q); |
|
| 1329 | + return spip_pg_query_simple($link, $q); |
|
| 1330 | 1330 | } |
| 1331 | 1331 | |
| 1332 | 1332 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1333 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1334 | - $link = $connexion['link']; |
|
| 1335 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1336 | - |
|
| 1337 | - $res = spip_pg_query_simple($link, $q); |
|
| 1338 | - if (!$res) { |
|
| 1339 | - return false; |
|
| 1340 | - } |
|
| 1341 | - |
|
| 1342 | - // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1343 | - // il faut en tenir compte dans le return |
|
| 1344 | - $fields = []; |
|
| 1345 | - while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1346 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1347 | - } |
|
| 1348 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1349 | - $res = spip_pg_query_simple($link, $q); |
|
| 1350 | - $keys = []; |
|
| 1351 | - while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1352 | - if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1353 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1354 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1355 | - } |
|
| 1356 | - } |
|
| 1357 | - |
|
| 1358 | - return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1333 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1334 | + $link = $connexion['link']; |
|
| 1335 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1336 | + |
|
| 1337 | + $res = spip_pg_query_simple($link, $q); |
|
| 1338 | + if (!$res) { |
|
| 1339 | + return false; |
|
| 1340 | + } |
|
| 1341 | + |
|
| 1342 | + // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1343 | + // il faut en tenir compte dans le return |
|
| 1344 | + $fields = []; |
|
| 1345 | + while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1346 | + $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1347 | + } |
|
| 1348 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1349 | + $res = spip_pg_query_simple($link, $q); |
|
| 1350 | + $keys = []; |
|
| 1351 | + while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1352 | + if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1353 | + $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1354 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1355 | + } |
|
| 1356 | + } |
|
| 1357 | + |
|
| 1358 | + return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1359 | 1359 | } |
| 1360 | 1360 | |
| 1361 | 1361 | // Fonction de creation d'une table SQL nommee $nom |
@@ -1366,116 +1366,116 @@ discard block |
||
| 1366 | 1366 | // Le nom des index est prefixe par celui de la table pour eviter les conflits |
| 1367 | 1367 | function spip_pg_create($nom, $champs, $cles, $autoinc = false, $temporary = false, $serveur = '', $requeter = true) { |
| 1368 | 1368 | |
| 1369 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1370 | - $link = $connexion['link']; |
|
| 1371 | - $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1372 | - |
|
| 1373 | - $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1374 | - $keys = []; |
|
| 1375 | - |
|
| 1376 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1377 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1378 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 1379 | - return; |
|
| 1380 | - } |
|
| 1381 | - |
|
| 1382 | - foreach ($cles as $k => $v) { |
|
| 1383 | - if (strpos($k, 'KEY ') === 0) { |
|
| 1384 | - $n = str_replace('`', '', $k); |
|
| 1385 | - $v = str_replace('`', '"', $v); |
|
| 1386 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1387 | - if ($k != $n) { |
|
| 1388 | - $i = "\"$i\""; |
|
| 1389 | - } |
|
| 1390 | - $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1391 | - } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1392 | - $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1393 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1394 | - } else { |
|
| 1395 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1396 | - } |
|
| 1397 | - if ($k == 'PRIMARY KEY') { |
|
| 1398 | - $prim_name = $v; |
|
| 1399 | - } |
|
| 1400 | - $s = ','; |
|
| 1401 | - } |
|
| 1402 | - $s = ''; |
|
| 1403 | - |
|
| 1404 | - $character_set = ''; |
|
| 1405 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1406 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1407 | - } |
|
| 1408 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1409 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1410 | - } |
|
| 1411 | - |
|
| 1412 | - foreach ($champs as $k => $v) { |
|
| 1413 | - $k = str_replace('`', '"', $k); |
|
| 1414 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1415 | - if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1416 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1417 | - } |
|
| 1418 | - } |
|
| 1419 | - |
|
| 1420 | - $query .= "$s\n\t\t$k " |
|
| 1421 | - . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1422 | - ? ' bigserial' |
|
| 1423 | - : mysql2pg_type($v) |
|
| 1424 | - ); |
|
| 1425 | - $s = ','; |
|
| 1426 | - } |
|
| 1427 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1428 | - |
|
| 1429 | - // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1430 | - |
|
| 1431 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1432 | - ($character_set ? " DEFAULT $character_set" : '') |
|
| 1433 | - . "\n"; |
|
| 1434 | - |
|
| 1435 | - if (!$requeter) { |
|
| 1436 | - return $q; |
|
| 1437 | - } |
|
| 1438 | - $connexion['last'] = $q; |
|
| 1439 | - $r = @pg_query($link, $q); |
|
| 1440 | - |
|
| 1441 | - if (!$r) { |
|
| 1442 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1443 | - } else { |
|
| 1444 | - foreach ($keys as $index) { |
|
| 1445 | - pg_query($link, $index); |
|
| 1446 | - } |
|
| 1447 | - } |
|
| 1448 | - |
|
| 1449 | - return $r; |
|
| 1369 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1370 | + $link = $connexion['link']; |
|
| 1371 | + $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1372 | + |
|
| 1373 | + $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1374 | + $keys = []; |
|
| 1375 | + |
|
| 1376 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1377 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1378 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 1379 | + return; |
|
| 1380 | + } |
|
| 1381 | + |
|
| 1382 | + foreach ($cles as $k => $v) { |
|
| 1383 | + if (strpos($k, 'KEY ') === 0) { |
|
| 1384 | + $n = str_replace('`', '', $k); |
|
| 1385 | + $v = str_replace('`', '"', $v); |
|
| 1386 | + $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1387 | + if ($k != $n) { |
|
| 1388 | + $i = "\"$i\""; |
|
| 1389 | + } |
|
| 1390 | + $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1391 | + } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1392 | + $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1393 | + $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1394 | + } else { |
|
| 1395 | + $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1396 | + } |
|
| 1397 | + if ($k == 'PRIMARY KEY') { |
|
| 1398 | + $prim_name = $v; |
|
| 1399 | + } |
|
| 1400 | + $s = ','; |
|
| 1401 | + } |
|
| 1402 | + $s = ''; |
|
| 1403 | + |
|
| 1404 | + $character_set = ''; |
|
| 1405 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1406 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1407 | + } |
|
| 1408 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1409 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1410 | + } |
|
| 1411 | + |
|
| 1412 | + foreach ($champs as $k => $v) { |
|
| 1413 | + $k = str_replace('`', '"', $k); |
|
| 1414 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1415 | + if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1416 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1417 | + } |
|
| 1418 | + } |
|
| 1419 | + |
|
| 1420 | + $query .= "$s\n\t\t$k " |
|
| 1421 | + . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1422 | + ? ' bigserial' |
|
| 1423 | + : mysql2pg_type($v) |
|
| 1424 | + ); |
|
| 1425 | + $s = ','; |
|
| 1426 | + } |
|
| 1427 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1428 | + |
|
| 1429 | + // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1430 | + |
|
| 1431 | + $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1432 | + ($character_set ? " DEFAULT $character_set" : '') |
|
| 1433 | + . "\n"; |
|
| 1434 | + |
|
| 1435 | + if (!$requeter) { |
|
| 1436 | + return $q; |
|
| 1437 | + } |
|
| 1438 | + $connexion['last'] = $q; |
|
| 1439 | + $r = @pg_query($link, $q); |
|
| 1440 | + |
|
| 1441 | + if (!$r) { |
|
| 1442 | + spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1443 | + } else { |
|
| 1444 | + foreach ($keys as $index) { |
|
| 1445 | + pg_query($link, $index); |
|
| 1446 | + } |
|
| 1447 | + } |
|
| 1448 | + |
|
| 1449 | + return $r; |
|
| 1450 | 1450 | } |
| 1451 | 1451 | |
| 1452 | 1452 | |
| 1453 | 1453 | function spip_pg_create_base($nom, $serveur = '', $requeter = true) { |
| 1454 | - return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1454 | + return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1455 | 1455 | } |
| 1456 | 1456 | |
| 1457 | 1457 | // Fonction de creation d'une vue SQL nommee $nom |
| 1458 | 1458 | function spip_pg_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 1459 | - if (!$query_select) { |
|
| 1460 | - return false; |
|
| 1461 | - } |
|
| 1462 | - // vue deja presente |
|
| 1463 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 1464 | - if ($requeter) { |
|
| 1465 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1466 | - } |
|
| 1459 | + if (!$query_select) { |
|
| 1460 | + return false; |
|
| 1461 | + } |
|
| 1462 | + // vue deja presente |
|
| 1463 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 1464 | + if ($requeter) { |
|
| 1465 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1466 | + } |
|
| 1467 | 1467 | |
| 1468 | - return false; |
|
| 1469 | - } |
|
| 1468 | + return false; |
|
| 1469 | + } |
|
| 1470 | 1470 | |
| 1471 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1471 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1472 | 1472 | |
| 1473 | - return spip_pg_query($query, $serveur, $requeter); |
|
| 1473 | + return spip_pg_query($query, $serveur, $requeter); |
|
| 1474 | 1474 | } |
| 1475 | 1475 | |
| 1476 | 1476 | |
| 1477 | 1477 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1478 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1478 | + spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1479 | 1479 | } |
| 1480 | 1480 | |
| 1481 | 1481 | |
@@ -1488,50 +1488,50 @@ discard block |
||
| 1488 | 1488 | * @return bool|string true / false / requete |
| 1489 | 1489 | **/ |
| 1490 | 1490 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1491 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1491 | + return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1492 | 1492 | } |
| 1493 | 1493 | |
| 1494 | 1494 | // Selectionner la sous-chaine dans $objet |
| 1495 | 1495 | // correspondant a $lang. Cf balise Multi de Spip |
| 1496 | 1496 | |
| 1497 | 1497 | function spip_pg_multi($objet, $lang) { |
| 1498 | - $r = 'regexp_replace(' |
|
| 1499 | - . $objet |
|
| 1500 | - . ",'<multi>.*[[]" |
|
| 1501 | - . $lang |
|
| 1502 | - . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1498 | + $r = 'regexp_replace(' |
|
| 1499 | + . $objet |
|
| 1500 | + . ",'<multi>.*[[]" |
|
| 1501 | + . $lang |
|
| 1502 | + . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1503 | 1503 | |
| 1504 | - return $r; |
|
| 1504 | + return $r; |
|
| 1505 | 1505 | } |
| 1506 | 1506 | |
| 1507 | 1507 | // Palanquee d'idiosyncrasies MySQL dans les creations de table |
| 1508 | 1508 | // A completer par les autres, mais essayer de reduire en amont. |
| 1509 | 1509 | |
| 1510 | 1510 | function mysql2pg_type($v) { |
| 1511 | - $remplace = [ |
|
| 1512 | - '/auto_increment/i' => '', // non reconnu |
|
| 1513 | - '/bigint/i' => 'bigint', |
|
| 1514 | - '/mediumint/i' => 'mediumint', |
|
| 1515 | - '/smallint/i' => 'smallint', |
|
| 1516 | - '/tinyint/i' => 'int', |
|
| 1517 | - '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1518 | - '/longtext/i' => 'text', |
|
| 1519 | - '/mediumtext/i' => 'text', |
|
| 1520 | - '/tinytext/i' => 'text', |
|
| 1521 | - '/longblob/i' => 'text', |
|
| 1522 | - '/0000-00-00/' => '0001-01-01', |
|
| 1523 | - '/datetime/i' => 'timestamp', |
|
| 1524 | - '/unsigned/i' => '', |
|
| 1525 | - '/double/i' => 'double precision', |
|
| 1526 | - '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1527 | - '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1528 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1529 | - ]; |
|
| 1530 | - |
|
| 1531 | - return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1511 | + $remplace = [ |
|
| 1512 | + '/auto_increment/i' => '', // non reconnu |
|
| 1513 | + '/bigint/i' => 'bigint', |
|
| 1514 | + '/mediumint/i' => 'mediumint', |
|
| 1515 | + '/smallint/i' => 'smallint', |
|
| 1516 | + '/tinyint/i' => 'int', |
|
| 1517 | + '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1518 | + '/longtext/i' => 'text', |
|
| 1519 | + '/mediumtext/i' => 'text', |
|
| 1520 | + '/tinytext/i' => 'text', |
|
| 1521 | + '/longblob/i' => 'text', |
|
| 1522 | + '/0000-00-00/' => '0001-01-01', |
|
| 1523 | + '/datetime/i' => 'timestamp', |
|
| 1524 | + '/unsigned/i' => '', |
|
| 1525 | + '/double/i' => 'double precision', |
|
| 1526 | + '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1527 | + '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1528 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1529 | + ]; |
|
| 1530 | + |
|
| 1531 | + return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1532 | 1532 | } |
| 1533 | 1533 | |
| 1534 | 1534 | // Renvoie false si on n'a pas les fonctions pg (pour l'install) |
| 1535 | 1535 | function spip_versions_pg() { |
| 1536 | - return function_exists('pg_connect'); |
|
| 1536 | + return function_exists('pg_connect'); |
|
| 1537 | 1537 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | |
| 23 | 23 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 24 | - return; |
|
| 24 | + return; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | |
@@ -44,70 +44,70 @@ discard block |
||
| 44 | 44 | * @return array|string |
| 45 | 45 | */ |
| 46 | 46 | function action_inscrire_auteur_dist($statut, $mail_complet, $nom, $options = []) { |
| 47 | - if (!is_array($options)) { |
|
| 48 | - $options = ['id' => $options]; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - if (function_exists('test_inscription')) { |
|
| 52 | - $f = 'test_inscription'; |
|
| 53 | - } else { |
|
| 54 | - $f = 'test_inscription_dist'; |
|
| 55 | - } |
|
| 56 | - $desc = $f($statut, $mail_complet, $nom, $options); |
|
| 57 | - |
|
| 58 | - // erreur ? |
|
| 59 | - if (!is_array($desc)) { |
|
| 60 | - return _T($desc); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - include_spip('base/abstract_sql'); |
|
| 64 | - $res = sql_select('statut, id_auteur, login, email, nom', 'spip_auteurs', 'email=' . sql_quote($desc['email'])); |
|
| 65 | - // erreur ? |
|
| 66 | - if (!$res) { |
|
| 67 | - return _T('titre_probleme_technique'); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $row = sql_fetch($res); |
|
| 71 | - sql_free($res); |
|
| 72 | - if ($row) { |
|
| 73 | - if (isset($options['force_nouveau']) and $options['force_nouveau'] == true) { |
|
| 74 | - $desc['id_auteur'] = $row['id_auteur']; |
|
| 75 | - $desc = inscription_nouveau($desc); |
|
| 76 | - } else { |
|
| 77 | - $desc = $row; |
|
| 78 | - } |
|
| 79 | - } else // s'il n'existe pas deja, creer les identifiants |
|
| 80 | - { |
|
| 81 | - $desc = inscription_nouveau($desc); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - // erreur ? |
|
| 85 | - if (!is_array($desc)) { |
|
| 86 | - return $desc; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - |
|
| 90 | - // generer le mot de passe (ou le refaire si compte inutilise) |
|
| 91 | - $desc['pass'] = creer_pass_pour_auteur($desc['id_auteur']); |
|
| 92 | - |
|
| 93 | - // attribuer un jeton pour confirmation par clic sur un lien |
|
| 94 | - $desc['jeton'] = auteur_attribuer_jeton($desc['id_auteur']); |
|
| 95 | - |
|
| 96 | - // charger de suite cette fonction, pour ses utilitaires |
|
| 97 | - $envoyer_inscription = charger_fonction('envoyer_inscription', ''); |
|
| 98 | - [$sujet, $msg, $from, $head] = $envoyer_inscription($desc, $nom, $statut, $options); |
|
| 99 | - |
|
| 100 | - $notifications = charger_fonction('notifications', 'inc'); |
|
| 101 | - notifications_envoyer_mails($mail_complet, $msg, $sujet, $from, $head); |
|
| 102 | - |
|
| 103 | - // Notifications |
|
| 104 | - $notifications( |
|
| 105 | - 'inscription', |
|
| 106 | - $desc['id_auteur'], |
|
| 107 | - ['nom' => $desc['nom'], 'email' => $desc['email']] |
|
| 108 | - ); |
|
| 109 | - |
|
| 110 | - return $desc; |
|
| 47 | + if (!is_array($options)) { |
|
| 48 | + $options = ['id' => $options]; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + if (function_exists('test_inscription')) { |
|
| 52 | + $f = 'test_inscription'; |
|
| 53 | + } else { |
|
| 54 | + $f = 'test_inscription_dist'; |
|
| 55 | + } |
|
| 56 | + $desc = $f($statut, $mail_complet, $nom, $options); |
|
| 57 | + |
|
| 58 | + // erreur ? |
|
| 59 | + if (!is_array($desc)) { |
|
| 60 | + return _T($desc); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + include_spip('base/abstract_sql'); |
|
| 64 | + $res = sql_select('statut, id_auteur, login, email, nom', 'spip_auteurs', 'email=' . sql_quote($desc['email'])); |
|
| 65 | + // erreur ? |
|
| 66 | + if (!$res) { |
|
| 67 | + return _T('titre_probleme_technique'); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $row = sql_fetch($res); |
|
| 71 | + sql_free($res); |
|
| 72 | + if ($row) { |
|
| 73 | + if (isset($options['force_nouveau']) and $options['force_nouveau'] == true) { |
|
| 74 | + $desc['id_auteur'] = $row['id_auteur']; |
|
| 75 | + $desc = inscription_nouveau($desc); |
|
| 76 | + } else { |
|
| 77 | + $desc = $row; |
|
| 78 | + } |
|
| 79 | + } else // s'il n'existe pas deja, creer les identifiants |
|
| 80 | + { |
|
| 81 | + $desc = inscription_nouveau($desc); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + // erreur ? |
|
| 85 | + if (!is_array($desc)) { |
|
| 86 | + return $desc; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + |
|
| 90 | + // generer le mot de passe (ou le refaire si compte inutilise) |
|
| 91 | + $desc['pass'] = creer_pass_pour_auteur($desc['id_auteur']); |
|
| 92 | + |
|
| 93 | + // attribuer un jeton pour confirmation par clic sur un lien |
|
| 94 | + $desc['jeton'] = auteur_attribuer_jeton($desc['id_auteur']); |
|
| 95 | + |
|
| 96 | + // charger de suite cette fonction, pour ses utilitaires |
|
| 97 | + $envoyer_inscription = charger_fonction('envoyer_inscription', ''); |
|
| 98 | + [$sujet, $msg, $from, $head] = $envoyer_inscription($desc, $nom, $statut, $options); |
|
| 99 | + |
|
| 100 | + $notifications = charger_fonction('notifications', 'inc'); |
|
| 101 | + notifications_envoyer_mails($mail_complet, $msg, $sujet, $from, $head); |
|
| 102 | + |
|
| 103 | + // Notifications |
|
| 104 | + $notifications( |
|
| 105 | + 'inscription', |
|
| 106 | + $desc['id_auteur'], |
|
| 107 | + ['nom' => $desc['nom'], 'email' => $desc['email']] |
|
| 108 | + ); |
|
| 109 | + |
|
| 110 | + return $desc; |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | |
@@ -130,23 +130,23 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | */ |
| 132 | 132 | function test_inscription_dist($statut, $mail, $nom, $options) { |
| 133 | - include_spip('inc/filtres'); |
|
| 134 | - if (!$r = email_valide($mail)) { |
|
| 135 | - return 'info_email_invalide'; |
|
| 136 | - } |
|
| 137 | - $nom = trim(corriger_caracteres($nom)); |
|
| 138 | - $res = ['email' => $r, 'nom' => $nom, 'prefs' => $statut]; |
|
| 139 | - if (isset($options['login'])) { |
|
| 140 | - $login = trim(corriger_caracteres($options['login'])); |
|
| 141 | - if ((strlen($login) >= _LOGIN_TROP_COURT) and (strlen($nom) <= 64)) { |
|
| 142 | - $res['login'] = $login; |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - if (!isset($res['login']) and ((strlen($nom) < _LOGIN_TROP_COURT) or (strlen($nom) > 64))) { |
|
| 146 | - return 'ecrire:info_login_trop_court'; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - return $res; |
|
| 133 | + include_spip('inc/filtres'); |
|
| 134 | + if (!$r = email_valide($mail)) { |
|
| 135 | + return 'info_email_invalide'; |
|
| 136 | + } |
|
| 137 | + $nom = trim(corriger_caracteres($nom)); |
|
| 138 | + $res = ['email' => $r, 'nom' => $nom, 'prefs' => $statut]; |
|
| 139 | + if (isset($options['login'])) { |
|
| 140 | + $login = trim(corriger_caracteres($options['login'])); |
|
| 141 | + if ((strlen($login) >= _LOGIN_TROP_COURT) and (strlen($nom) <= 64)) { |
|
| 142 | + $res['login'] = $login; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + if (!isset($res['login']) and ((strlen($nom) < _LOGIN_TROP_COURT) or (strlen($nom) > 64))) { |
|
| 146 | + return 'ecrire:info_login_trop_court'; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + return $res; |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | |
@@ -159,33 +159,33 @@ discard block |
||
| 159 | 159 | * @return mixed|string |
| 160 | 160 | */ |
| 161 | 161 | function inscription_nouveau($desc) { |
| 162 | - if (!isset($desc['login']) or !strlen($desc['login'])) { |
|
| 163 | - $desc['login'] = test_login($desc['nom'], $desc['email']); |
|
| 164 | - } |
|
| 162 | + if (!isset($desc['login']) or !strlen($desc['login'])) { |
|
| 163 | + $desc['login'] = test_login($desc['nom'], $desc['email']); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - $desc['statut'] = 'nouveau'; |
|
| 167 | - include_spip('action/editer_auteur'); |
|
| 168 | - if (isset($desc['id_auteur'])) { |
|
| 169 | - $id_auteur = $desc['id_auteur']; |
|
| 170 | - } else { |
|
| 171 | - $id_auteur = auteur_inserer(); |
|
| 172 | - } |
|
| 166 | + $desc['statut'] = 'nouveau'; |
|
| 167 | + include_spip('action/editer_auteur'); |
|
| 168 | + if (isset($desc['id_auteur'])) { |
|
| 169 | + $id_auteur = $desc['id_auteur']; |
|
| 170 | + } else { |
|
| 171 | + $id_auteur = auteur_inserer(); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - if (!$id_auteur) { |
|
| 175 | - return _T('titre_probleme_technique'); |
|
| 176 | - } |
|
| 174 | + if (!$id_auteur) { |
|
| 175 | + return _T('titre_probleme_technique'); |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - $desc['lang'] = $GLOBALS['spip_lang']; |
|
| 178 | + $desc['lang'] = $GLOBALS['spip_lang']; |
|
| 179 | 179 | |
| 180 | - include_spip('inc/autoriser'); |
|
| 181 | - // lever l'autorisation pour pouvoir modifier le statut |
|
| 182 | - autoriser_exception('modifier', 'auteur', $id_auteur); |
|
| 183 | - auteur_modifier($id_auteur, $desc); |
|
| 184 | - autoriser_exception('modifier', 'auteur', $id_auteur, false); |
|
| 180 | + include_spip('inc/autoriser'); |
|
| 181 | + // lever l'autorisation pour pouvoir modifier le statut |
|
| 182 | + autoriser_exception('modifier', 'auteur', $id_auteur); |
|
| 183 | + auteur_modifier($id_auteur, $desc); |
|
| 184 | + autoriser_exception('modifier', 'auteur', $id_auteur, false); |
|
| 185 | 185 | |
| 186 | - $desc['id_auteur'] = $id_auteur; |
|
| 186 | + $desc['id_auteur'] = $id_auteur; |
|
| 187 | 187 | |
| 188 | - return $desc; |
|
| 188 | + return $desc; |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | |
@@ -202,27 +202,27 @@ discard block |
||
| 202 | 202 | * @return string |
| 203 | 203 | */ |
| 204 | 204 | function test_login($nom, $mail) { |
| 205 | - include_spip('inc/charsets'); |
|
| 206 | - $nom = strtolower(translitteration($nom)); |
|
| 207 | - $login_base = preg_replace('/[^\w\d_]/', '_', $nom); |
|
| 208 | - |
|
| 209 | - // il faut eviter que le login soit vraiment trop court |
|
| 210 | - if (strlen($login_base) < 3) { |
|
| 211 | - $mail = strtolower(translitteration(preg_replace('/@.*/', '', $mail))); |
|
| 212 | - $login_base = preg_replace('/[^\w\d]/', '_', $mail); |
|
| 213 | - } |
|
| 214 | - if (strlen($login_base) < 3) { |
|
| 215 | - $login_base = 'user'; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $login = $login_base; |
|
| 219 | - |
|
| 220 | - for ($i = 1;; $i++) { |
|
| 221 | - if (!sql_countsel('spip_auteurs', "login='$login'")) { |
|
| 222 | - return $login; |
|
| 223 | - } |
|
| 224 | - $login = $login_base . $i; |
|
| 225 | - } |
|
| 205 | + include_spip('inc/charsets'); |
|
| 206 | + $nom = strtolower(translitteration($nom)); |
|
| 207 | + $login_base = preg_replace('/[^\w\d_]/', '_', $nom); |
|
| 208 | + |
|
| 209 | + // il faut eviter que le login soit vraiment trop court |
|
| 210 | + if (strlen($login_base) < 3) { |
|
| 211 | + $mail = strtolower(translitteration(preg_replace('/@.*/', '', $mail))); |
|
| 212 | + $login_base = preg_replace('/[^\w\d]/', '_', $mail); |
|
| 213 | + } |
|
| 214 | + if (strlen($login_base) < 3) { |
|
| 215 | + $login_base = 'user'; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $login = $login_base; |
|
| 219 | + |
|
| 220 | + for ($i = 1;; $i++) { |
|
| 221 | + if (!sql_countsel('spip_auteurs', "login='$login'")) { |
|
| 222 | + return $login; |
|
| 223 | + } |
|
| 224 | + $login = $login_base . $i; |
|
| 225 | + } |
|
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | |
@@ -240,26 +240,26 @@ discard block |
||
| 240 | 240 | */ |
| 241 | 241 | function envoyer_inscription_dist($desc, $nom, $mode, $options = []) { |
| 242 | 242 | |
| 243 | - $contexte = array_merge($desc, $options); |
|
| 244 | - $contexte['nom'] = $nom; |
|
| 245 | - $contexte['mode'] = $mode; |
|
| 246 | - $contexte['url_confirm'] = generer_url_action('confirmer_inscription', '', true, true); |
|
| 247 | - $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'email', $desc['email']); |
|
| 248 | - $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'jeton', $desc['jeton']); |
|
| 249 | - // S'il y a l'option redirect, on l'ajoute directement ici |
|
| 250 | - if (isset($options['redirect'])) { |
|
| 251 | - $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'redirect', $options['redirect']); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - $modele_mail = 'modeles/mail_inscription'; |
|
| 255 | - if (isset($options['modele_mail']) and $options['modele_mail']) { |
|
| 256 | - $modele_mail = $options['modele_mail']; |
|
| 257 | - } |
|
| 258 | - $message = recuperer_fond($modele_mail, $contexte); |
|
| 259 | - $from = ($options['from'] ?? ''); |
|
| 260 | - $head = ''; |
|
| 261 | - |
|
| 262 | - return ['', $message, $from, $head]; |
|
| 243 | + $contexte = array_merge($desc, $options); |
|
| 244 | + $contexte['nom'] = $nom; |
|
| 245 | + $contexte['mode'] = $mode; |
|
| 246 | + $contexte['url_confirm'] = generer_url_action('confirmer_inscription', '', true, true); |
|
| 247 | + $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'email', $desc['email']); |
|
| 248 | + $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'jeton', $desc['jeton']); |
|
| 249 | + // S'il y a l'option redirect, on l'ajoute directement ici |
|
| 250 | + if (isset($options['redirect'])) { |
|
| 251 | + $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'redirect', $options['redirect']); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + $modele_mail = 'modeles/mail_inscription'; |
|
| 255 | + if (isset($options['modele_mail']) and $options['modele_mail']) { |
|
| 256 | + $modele_mail = $options['modele_mail']; |
|
| 257 | + } |
|
| 258 | + $message = recuperer_fond($modele_mail, $contexte); |
|
| 259 | + $from = ($options['from'] ?? ''); |
|
| 260 | + $head = ''; |
|
| 261 | + |
|
| 262 | + return ['', $message, $from, $head]; |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | |
@@ -270,12 +270,12 @@ discard block |
||
| 270 | 270 | * @return string |
| 271 | 271 | */ |
| 272 | 272 | function creer_pass_pour_auteur($id_auteur) { |
| 273 | - include_spip('inc/acces'); |
|
| 274 | - $pass = creer_pass_aleatoire(max(_PASS_LONGUEUR_MINI, 16), $id_auteur); |
|
| 275 | - include_spip('action/editer_auteur'); |
|
| 276 | - auteur_instituer($id_auteur, ['pass' => $pass]); |
|
| 273 | + include_spip('inc/acces'); |
|
| 274 | + $pass = creer_pass_aleatoire(max(_PASS_LONGUEUR_MINI, 16), $id_auteur); |
|
| 275 | + include_spip('action/editer_auteur'); |
|
| 276 | + auteur_instituer($id_auteur, ['pass' => $pass]); |
|
| 277 | 277 | |
| 278 | - return $pass; |
|
| 278 | + return $pass; |
|
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | /** |
@@ -288,17 +288,17 @@ discard block |
||
| 288 | 288 | * @return string |
| 289 | 289 | */ |
| 290 | 290 | function tester_statut_inscription($statut_tmp, $id) { |
| 291 | - include_spip('inc/autoriser'); |
|
| 292 | - if ($statut_tmp) { |
|
| 293 | - return autoriser('inscrireauteur', $statut_tmp, $id) ? $statut_tmp : ''; |
|
| 294 | - } elseif ( |
|
| 295 | - autoriser('inscrireauteur', $statut_tmp = '1comite', $id) |
|
| 296 | - or autoriser('inscrireauteur', $statut_tmp = '6forum', $id) |
|
| 297 | - ) { |
|
| 298 | - return $statut_tmp; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - return ''; |
|
| 291 | + include_spip('inc/autoriser'); |
|
| 292 | + if ($statut_tmp) { |
|
| 293 | + return autoriser('inscrireauteur', $statut_tmp, $id) ? $statut_tmp : ''; |
|
| 294 | + } elseif ( |
|
| 295 | + autoriser('inscrireauteur', $statut_tmp = '1comite', $id) |
|
| 296 | + or autoriser('inscrireauteur', $statut_tmp = '6forum', $id) |
|
| 297 | + ) { |
|
| 298 | + return $statut_tmp; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + return ''; |
|
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | |
@@ -312,35 +312,35 @@ discard block |
||
| 312 | 312 | * @return array |
| 313 | 313 | */ |
| 314 | 314 | function confirmer_statut_inscription($auteur) { |
| 315 | - // securite |
|
| 316 | - if ($auteur['statut'] != 'nouveau') { |
|
| 317 | - return $auteur; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - $s = $auteur['prefs']; |
|
| 321 | - // securite, au cas ou prefs aurait ete corrompu (ou deja ecrase par un tableau serialize) |
|
| 322 | - if (!preg_match(',^\w+$,', $s)) { |
|
| 323 | - $s = '6forum'; |
|
| 324 | - } |
|
| 325 | - include_spip('inc/autoriser'); |
|
| 326 | - if (!autoriser('inscrireauteur', $s)) { |
|
| 327 | - return $auteur; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - include_spip('inc/autoriser'); |
|
| 331 | - // accorder l'autorisation de modif du statut auteur |
|
| 332 | - autoriser_exception('modifier', 'auteur', $auteur['id_auteur']); |
|
| 333 | - include_spip('action/editer_auteur'); |
|
| 334 | - // changer le statut |
|
| 335 | - auteur_modifier($auteur['id_auteur'], ['statut' => $s]); |
|
| 336 | - unset($_COOKIE['spip_session']); // forcer la maj de la session |
|
| 337 | - // lever l'autorisation de modif du statut auteur |
|
| 338 | - autoriser_exception('modifier', 'auteur', $auteur['id_auteur'], false); |
|
| 339 | - |
|
| 340 | - // mettre a jour le statut |
|
| 341 | - $auteur['statut'] = $s; |
|
| 342 | - |
|
| 343 | - return $auteur; |
|
| 315 | + // securite |
|
| 316 | + if ($auteur['statut'] != 'nouveau') { |
|
| 317 | + return $auteur; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + $s = $auteur['prefs']; |
|
| 321 | + // securite, au cas ou prefs aurait ete corrompu (ou deja ecrase par un tableau serialize) |
|
| 322 | + if (!preg_match(',^\w+$,', $s)) { |
|
| 323 | + $s = '6forum'; |
|
| 324 | + } |
|
| 325 | + include_spip('inc/autoriser'); |
|
| 326 | + if (!autoriser('inscrireauteur', $s)) { |
|
| 327 | + return $auteur; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + include_spip('inc/autoriser'); |
|
| 331 | + // accorder l'autorisation de modif du statut auteur |
|
| 332 | + autoriser_exception('modifier', 'auteur', $auteur['id_auteur']); |
|
| 333 | + include_spip('action/editer_auteur'); |
|
| 334 | + // changer le statut |
|
| 335 | + auteur_modifier($auteur['id_auteur'], ['statut' => $s]); |
|
| 336 | + unset($_COOKIE['spip_session']); // forcer la maj de la session |
|
| 337 | + // lever l'autorisation de modif du statut auteur |
|
| 338 | + autoriser_exception('modifier', 'auteur', $auteur['id_auteur'], false); |
|
| 339 | + |
|
| 340 | + // mettre a jour le statut |
|
| 341 | + $auteur['statut'] = $s; |
|
| 342 | + |
|
| 343 | + return $auteur; |
|
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | |
@@ -354,20 +354,20 @@ discard block |
||
| 354 | 354 | * @return string |
| 355 | 355 | */ |
| 356 | 356 | function auteur_attribuer_jeton($id_auteur): string { |
| 357 | - include_spip('base/abstract_sql'); |
|
| 358 | - include_spip('inc/acces'); |
|
| 359 | - |
|
| 360 | - // s'assurer de l'unicite du jeton pour le couple (email,cookie) |
|
| 361 | - do { |
|
| 362 | - // Un morceau du jeton est lisible en bdd pour éviter de devoir déchiffrer |
|
| 363 | - // tous les jetons connus pour vérifier le jeton d’un auteur. |
|
| 364 | - $public = substr(creer_uniqid(), 0, 7) . '.'; |
|
| 365 | - $jeton = $public . creer_uniqid(); |
|
| 366 | - $jeton_chiffre_prefixe = $public . Chiffrement::chiffrer($jeton, SpipCles::secret_du_site()); |
|
| 367 | - sql_updateq('spip_auteurs', ['cookie_oubli' => $jeton_chiffre_prefixe], 'id_auteur=' . intval($id_auteur)); |
|
| 368 | - } while (sql_countsel('spip_auteurs', 'cookie_oubli=' . sql_quote($jeton_chiffre_prefixe, '', 'string')) > 1); |
|
| 369 | - |
|
| 370 | - return $jeton; |
|
| 357 | + include_spip('base/abstract_sql'); |
|
| 358 | + include_spip('inc/acces'); |
|
| 359 | + |
|
| 360 | + // s'assurer de l'unicite du jeton pour le couple (email,cookie) |
|
| 361 | + do { |
|
| 362 | + // Un morceau du jeton est lisible en bdd pour éviter de devoir déchiffrer |
|
| 363 | + // tous les jetons connus pour vérifier le jeton d’un auteur. |
|
| 364 | + $public = substr(creer_uniqid(), 0, 7) . '.'; |
|
| 365 | + $jeton = $public . creer_uniqid(); |
|
| 366 | + $jeton_chiffre_prefixe = $public . Chiffrement::chiffrer($jeton, SpipCles::secret_du_site()); |
|
| 367 | + sql_updateq('spip_auteurs', ['cookie_oubli' => $jeton_chiffre_prefixe], 'id_auteur=' . intval($id_auteur)); |
|
| 368 | + } while (sql_countsel('spip_auteurs', 'cookie_oubli=' . sql_quote($jeton_chiffre_prefixe, '', 'string')) > 1); |
|
| 369 | + |
|
| 370 | + return $jeton; |
|
| 371 | 371 | } |
| 372 | 372 | |
| 373 | 373 | /** |
@@ -381,19 +381,19 @@ discard block |
||
| 381 | 381 | * @return string|null |
| 382 | 382 | */ |
| 383 | 383 | function auteur_lire_jeton(int $id_auteur, bool $autoInit = false): ?string { |
| 384 | - include_spip('base/abstract_sql'); |
|
| 385 | - $jeton_chiffre_prefixe = sql_getfetsel('cookie_oubli', 'spip_auteurs', 'id_auteur=' . $id_auteur); |
|
| 386 | - if ($jeton_chiffre_prefixe) { |
|
| 387 | - $jeton_chiffre = substr($jeton_chiffre_prefixe, 8); |
|
| 388 | - $jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
|
| 389 | - if ($jeton) { |
|
| 390 | - return $jeton; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - if ($autoInit) { |
|
| 394 | - return auteur_attribuer_jeton($id_auteur); |
|
| 395 | - } |
|
| 396 | - return null; |
|
| 384 | + include_spip('base/abstract_sql'); |
|
| 385 | + $jeton_chiffre_prefixe = sql_getfetsel('cookie_oubli', 'spip_auteurs', 'id_auteur=' . $id_auteur); |
|
| 386 | + if ($jeton_chiffre_prefixe) { |
|
| 387 | + $jeton_chiffre = substr($jeton_chiffre_prefixe, 8); |
|
| 388 | + $jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
|
| 389 | + if ($jeton) { |
|
| 390 | + return $jeton; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + if ($autoInit) { |
|
| 394 | + return auteur_attribuer_jeton($id_auteur); |
|
| 395 | + } |
|
| 396 | + return null; |
|
| 397 | 397 | } |
| 398 | 398 | |
| 399 | 399 | /** |
@@ -403,24 +403,24 @@ discard block |
||
| 403 | 403 | * @return array|bool |
| 404 | 404 | */ |
| 405 | 405 | function auteur_verifier_jeton($jeton) { |
| 406 | - // refuser un jeton corrompu |
|
| 407 | - if (preg_match(',[^0-9a-f.],i', $jeton)) { |
|
| 408 | - return false; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - include_spip('base/abstract_sql'); |
|
| 412 | - $public = substr($jeton, 0, 8); |
|
| 413 | - |
|
| 414 | - // Les auteurs qui ont un jetons ressemblant |
|
| 415 | - $auteurs = sql_allfetsel('*', 'spip_auteurs', 'cookie_oubli LIKE ' . sql_quote($public . '%')); |
|
| 416 | - foreach ($auteurs as $auteur) { |
|
| 417 | - $jeton_chiffre = substr($auteur['cookie_oubli'], 8); |
|
| 418 | - $_jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
|
| 419 | - if ($_jeton and hash_equals($jeton, $_jeton)) { |
|
| 420 | - return $auteur; |
|
| 421 | - } |
|
| 422 | - } |
|
| 423 | - return false; |
|
| 406 | + // refuser un jeton corrompu |
|
| 407 | + if (preg_match(',[^0-9a-f.],i', $jeton)) { |
|
| 408 | + return false; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + include_spip('base/abstract_sql'); |
|
| 412 | + $public = substr($jeton, 0, 8); |
|
| 413 | + |
|
| 414 | + // Les auteurs qui ont un jetons ressemblant |
|
| 415 | + $auteurs = sql_allfetsel('*', 'spip_auteurs', 'cookie_oubli LIKE ' . sql_quote($public . '%')); |
|
| 416 | + foreach ($auteurs as $auteur) { |
|
| 417 | + $jeton_chiffre = substr($auteur['cookie_oubli'], 8); |
|
| 418 | + $_jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
|
| 419 | + if ($_jeton and hash_equals($jeton, $_jeton)) { |
|
| 420 | + return $auteur; |
|
| 421 | + } |
|
| 422 | + } |
|
| 423 | + return false; |
|
| 424 | 424 | } |
| 425 | 425 | |
| 426 | 426 | /** |
@@ -430,6 +430,6 @@ discard block |
||
| 430 | 430 | * @return bool |
| 431 | 431 | */ |
| 432 | 432 | function auteur_effacer_jeton($id_auteur) { |
| 433 | - include_spip('base/abstract_sql'); |
|
| 434 | - return sql_updateq('spip_auteurs', ['cookie_oubli' => ''], 'id_auteur=' . intval($id_auteur)); |
|
| 433 | + include_spip('base/abstract_sql'); |
|
| 434 | + return sql_updateq('spip_auteurs', ['cookie_oubli' => ''], 'id_auteur=' . intval($id_auteur)); |
|
| 435 | 435 | } |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | include_spip('base/abstract_sql'); |
| 64 | - $res = sql_select('statut, id_auteur, login, email, nom', 'spip_auteurs', 'email=' . sql_quote($desc['email'])); |
|
| 64 | + $res = sql_select('statut, id_auteur, login, email, nom', 'spip_auteurs', 'email='.sql_quote($desc['email'])); |
|
| 65 | 65 | // erreur ? |
| 66 | 66 | if (!$res) { |
| 67 | 67 | return _T('titre_probleme_technique'); |
@@ -217,11 +217,11 @@ discard block |
||
| 217 | 217 | |
| 218 | 218 | $login = $login_base; |
| 219 | 219 | |
| 220 | - for ($i = 1;; $i++) { |
|
| 220 | + for ($i = 1; ; $i++) { |
|
| 221 | 221 | if (!sql_countsel('spip_auteurs', "login='$login'")) { |
| 222 | 222 | return $login; |
| 223 | 223 | } |
| 224 | - $login = $login_base . $i; |
|
| 224 | + $login = $login_base.$i; |
|
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | |
@@ -361,11 +361,11 @@ discard block |
||
| 361 | 361 | do { |
| 362 | 362 | // Un morceau du jeton est lisible en bdd pour éviter de devoir déchiffrer |
| 363 | 363 | // tous les jetons connus pour vérifier le jeton d’un auteur. |
| 364 | - $public = substr(creer_uniqid(), 0, 7) . '.'; |
|
| 365 | - $jeton = $public . creer_uniqid(); |
|
| 366 | - $jeton_chiffre_prefixe = $public . Chiffrement::chiffrer($jeton, SpipCles::secret_du_site()); |
|
| 367 | - sql_updateq('spip_auteurs', ['cookie_oubli' => $jeton_chiffre_prefixe], 'id_auteur=' . intval($id_auteur)); |
|
| 368 | - } while (sql_countsel('spip_auteurs', 'cookie_oubli=' . sql_quote($jeton_chiffre_prefixe, '', 'string')) > 1); |
|
| 364 | + $public = substr(creer_uniqid(), 0, 7).'.'; |
|
| 365 | + $jeton = $public.creer_uniqid(); |
|
| 366 | + $jeton_chiffre_prefixe = $public.Chiffrement::chiffrer($jeton, SpipCles::secret_du_site()); |
|
| 367 | + sql_updateq('spip_auteurs', ['cookie_oubli' => $jeton_chiffre_prefixe], 'id_auteur='.intval($id_auteur)); |
|
| 368 | + } while (sql_countsel('spip_auteurs', 'cookie_oubli='.sql_quote($jeton_chiffre_prefixe, '', 'string')) > 1); |
|
| 369 | 369 | |
| 370 | 370 | return $jeton; |
| 371 | 371 | } |
@@ -382,7 +382,7 @@ discard block |
||
| 382 | 382 | */ |
| 383 | 383 | function auteur_lire_jeton(int $id_auteur, bool $autoInit = false): ?string { |
| 384 | 384 | include_spip('base/abstract_sql'); |
| 385 | - $jeton_chiffre_prefixe = sql_getfetsel('cookie_oubli', 'spip_auteurs', 'id_auteur=' . $id_auteur); |
|
| 385 | + $jeton_chiffre_prefixe = sql_getfetsel('cookie_oubli', 'spip_auteurs', 'id_auteur='.$id_auteur); |
|
| 386 | 386 | if ($jeton_chiffre_prefixe) { |
| 387 | 387 | $jeton_chiffre = substr($jeton_chiffre_prefixe, 8); |
| 388 | 388 | $jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
@@ -412,7 +412,7 @@ discard block |
||
| 412 | 412 | $public = substr($jeton, 0, 8); |
| 413 | 413 | |
| 414 | 414 | // Les auteurs qui ont un jetons ressemblant |
| 415 | - $auteurs = sql_allfetsel('*', 'spip_auteurs', 'cookie_oubli LIKE ' . sql_quote($public . '%')); |
|
| 415 | + $auteurs = sql_allfetsel('*', 'spip_auteurs', 'cookie_oubli LIKE '.sql_quote($public.'%')); |
|
| 416 | 416 | foreach ($auteurs as $auteur) { |
| 417 | 417 | $jeton_chiffre = substr($auteur['cookie_oubli'], 8); |
| 418 | 418 | $_jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
@@ -431,5 +431,5 @@ discard block |
||
| 431 | 431 | */ |
| 432 | 432 | function auteur_effacer_jeton($id_auteur) { |
| 433 | 433 | include_spip('base/abstract_sql'); |
| 434 | - return sql_updateq('spip_auteurs', ['cookie_oubli' => ''], 'id_auteur=' . intval($id_auteur)); |
|
| 434 | + return sql_updateq('spip_auteurs', ['cookie_oubli' => ''], 'id_auteur='.intval($id_auteur)); |
|
| 435 | 435 | } |
@@ -7,21 +7,21 @@ |
||
| 7 | 7 | * @internal |
| 8 | 8 | */ |
| 9 | 9 | class Collection { |
| 10 | - private array $vars = []; |
|
| 10 | + private array $vars = []; |
|
| 11 | 11 | |
| 12 | - public function add(string $var, string $value) { |
|
| 13 | - $this->vars[$var] = $value; |
|
| 14 | - } |
|
| 12 | + public function add(string $var, string $value) { |
|
| 13 | + $this->vars[$var] = $value; |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - public function getString(): string { |
|
| 17 | - $string = ''; |
|
| 18 | - foreach ($this->vars as $key => $value) { |
|
| 19 | - $string .= "$key: $value;\n"; |
|
| 20 | - } |
|
| 21 | - return $string; |
|
| 22 | - } |
|
| 16 | + public function getString(): string { |
|
| 17 | + $string = ''; |
|
| 18 | + foreach ($this->vars as $key => $value) { |
|
| 19 | + $string .= "$key: $value;\n"; |
|
| 20 | + } |
|
| 21 | + return $string; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - public function __toString(): string { |
|
| 25 | - return $this->getString(); |
|
| 26 | - } |
|
| 24 | + public function __toString(): string { |
|
| 25 | + return $this->getString(); |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -8,583 +8,583 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Decorator extends FilterIterator |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Conditions de filtrage |
|
| 13 | - * ie criteres de selection. |
|
| 14 | - * |
|
| 15 | - * @var array |
|
| 16 | - */ |
|
| 17 | - protected $filtre = []; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Fonction de filtrage compilee a partir des criteres de filtre. |
|
| 21 | - * |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - protected $func_filtre; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Critere {offset, limit}. |
|
| 28 | - * |
|
| 29 | - * @var int |
|
| 30 | - * @var int |
|
| 31 | - */ |
|
| 32 | - protected $offset; |
|
| 33 | - protected $limit; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * nombre d'elements recuperes depuis la position 0, |
|
| 37 | - * en tenant compte des filtres. |
|
| 38 | - * |
|
| 39 | - * @var int |
|
| 40 | - */ |
|
| 41 | - protected $fetched = 0; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Y a t'il une erreur ? |
|
| 45 | - * |
|
| 46 | - * @var bool |
|
| 47 | - */ |
|
| 48 | - protected $err = false; |
|
| 49 | - |
|
| 50 | - // Extension SPIP des iterateurs PHP |
|
| 51 | - /** |
|
| 52 | - * type de l'iterateur. |
|
| 53 | - * |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - protected $type; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * parametres de l'iterateur. |
|
| 60 | - * |
|
| 61 | - * @var array |
|
| 62 | - */ |
|
| 63 | - protected $command; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * infos de compilateur. |
|
| 67 | - * |
|
| 68 | - * @var array |
|
| 69 | - */ |
|
| 70 | - protected $info; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * position courante de l'iterateur. |
|
| 74 | - * |
|
| 75 | - * @var int |
|
| 76 | - */ |
|
| 77 | - protected $pos; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * nombre total resultats dans l'iterateur. |
|
| 81 | - * |
|
| 82 | - * @var int |
|
| 83 | - */ |
|
| 84 | - protected $total; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * nombre maximal de recherche pour $total |
|
| 88 | - * si l'iterateur n'implemente pas de fonction specifique. |
|
| 89 | - */ |
|
| 90 | - protected $max = 100000; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Liste des champs a inserer dans les $row |
|
| 94 | - * retournes par ->fetch(). |
|
| 95 | - */ |
|
| 96 | - protected $select = []; |
|
| 97 | - private $iter; |
|
| 98 | - |
|
| 99 | - public function __construct(Iterator $iter, $command, $info) { |
|
| 100 | - parent::__construct($iter); |
|
| 101 | - parent::rewind(); // remettre a la premiere position (bug? connu de FilterIterator) |
|
| 102 | - |
|
| 103 | - // recuperer l'iterateur transmis |
|
| 104 | - $this->iter = $this->getInnerIterator(); |
|
| 105 | - $this->command = $command; |
|
| 106 | - $this->info = $info; |
|
| 107 | - $this->pos = 0; |
|
| 108 | - $this->fetched = 0; |
|
| 109 | - |
|
| 110 | - // chercher la liste des champs a retourner par |
|
| 111 | - // fetch si l'objet ne les calcule pas tout seul |
|
| 112 | - if (!method_exists($this->iter, 'fetch')) { |
|
| 113 | - $this->calculer_select(); |
|
| 114 | - $this->calculer_filtres(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - // emptyIterator critere {si} faux n'a pas d'erreur ! |
|
| 118 | - if (isset($this->iter->err)) { |
|
| 119 | - $this->err = $this->iter->err; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // pas d'init a priori, le calcul ne sera fait qu'en cas de besoin (provoque une double requete souvent inutile en sqlite) |
|
| 123 | - //$this->total = $this->count(); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Drapeau a activer en cas d'echec |
|
| 128 | - * (select SQL errone, non chargement des DATA, etc). |
|
| 129 | - */ |
|
| 130 | - public function err() { |
|
| 131 | - if (method_exists($this->iter, 'err')) { |
|
| 132 | - return $this->iter->err(); |
|
| 133 | - } |
|
| 134 | - if (property_exists($this->iter, 'err')) { |
|
| 135 | - return $this->iter->err; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - return false; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - // recuperer la valeur d'une balise #X |
|
| 142 | - // en fonction des methodes |
|
| 143 | - // et proprietes disponibles |
|
| 144 | - public function get_select($nom) { |
|
| 145 | - if ( |
|
| 146 | - is_object($this->iter) |
|
| 147 | - and method_exists($this->iter, $nom) |
|
| 148 | - ) { |
|
| 149 | - try { |
|
| 150 | - return $this->iter->{$nom}(); |
|
| 151 | - } catch (Exception $e) { |
|
| 152 | - // #GETCHILDREN sur un fichier de DirectoryIterator ... |
|
| 153 | - spip_log("Methode {$nom} en echec sur " . get_class($this->iter)); |
|
| 154 | - spip_log("Cela peut être normal : retour d'une ligne de resultat ne pouvant pas calculer cette methode"); |
|
| 155 | - |
|
| 156 | - return ''; |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - /* |
|
| 11 | + /** |
|
| 12 | + * Conditions de filtrage |
|
| 13 | + * ie criteres de selection. |
|
| 14 | + * |
|
| 15 | + * @var array |
|
| 16 | + */ |
|
| 17 | + protected $filtre = []; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Fonction de filtrage compilee a partir des criteres de filtre. |
|
| 21 | + * |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + protected $func_filtre; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Critere {offset, limit}. |
|
| 28 | + * |
|
| 29 | + * @var int |
|
| 30 | + * @var int |
|
| 31 | + */ |
|
| 32 | + protected $offset; |
|
| 33 | + protected $limit; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * nombre d'elements recuperes depuis la position 0, |
|
| 37 | + * en tenant compte des filtres. |
|
| 38 | + * |
|
| 39 | + * @var int |
|
| 40 | + */ |
|
| 41 | + protected $fetched = 0; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Y a t'il une erreur ? |
|
| 45 | + * |
|
| 46 | + * @var bool |
|
| 47 | + */ |
|
| 48 | + protected $err = false; |
|
| 49 | + |
|
| 50 | + // Extension SPIP des iterateurs PHP |
|
| 51 | + /** |
|
| 52 | + * type de l'iterateur. |
|
| 53 | + * |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + protected $type; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * parametres de l'iterateur. |
|
| 60 | + * |
|
| 61 | + * @var array |
|
| 62 | + */ |
|
| 63 | + protected $command; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * infos de compilateur. |
|
| 67 | + * |
|
| 68 | + * @var array |
|
| 69 | + */ |
|
| 70 | + protected $info; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * position courante de l'iterateur. |
|
| 74 | + * |
|
| 75 | + * @var int |
|
| 76 | + */ |
|
| 77 | + protected $pos; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * nombre total resultats dans l'iterateur. |
|
| 81 | + * |
|
| 82 | + * @var int |
|
| 83 | + */ |
|
| 84 | + protected $total; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * nombre maximal de recherche pour $total |
|
| 88 | + * si l'iterateur n'implemente pas de fonction specifique. |
|
| 89 | + */ |
|
| 90 | + protected $max = 100000; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Liste des champs a inserer dans les $row |
|
| 94 | + * retournes par ->fetch(). |
|
| 95 | + */ |
|
| 96 | + protected $select = []; |
|
| 97 | + private $iter; |
|
| 98 | + |
|
| 99 | + public function __construct(Iterator $iter, $command, $info) { |
|
| 100 | + parent::__construct($iter); |
|
| 101 | + parent::rewind(); // remettre a la premiere position (bug? connu de FilterIterator) |
|
| 102 | + |
|
| 103 | + // recuperer l'iterateur transmis |
|
| 104 | + $this->iter = $this->getInnerIterator(); |
|
| 105 | + $this->command = $command; |
|
| 106 | + $this->info = $info; |
|
| 107 | + $this->pos = 0; |
|
| 108 | + $this->fetched = 0; |
|
| 109 | + |
|
| 110 | + // chercher la liste des champs a retourner par |
|
| 111 | + // fetch si l'objet ne les calcule pas tout seul |
|
| 112 | + if (!method_exists($this->iter, 'fetch')) { |
|
| 113 | + $this->calculer_select(); |
|
| 114 | + $this->calculer_filtres(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + // emptyIterator critere {si} faux n'a pas d'erreur ! |
|
| 118 | + if (isset($this->iter->err)) { |
|
| 119 | + $this->err = $this->iter->err; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + // pas d'init a priori, le calcul ne sera fait qu'en cas de besoin (provoque une double requete souvent inutile en sqlite) |
|
| 123 | + //$this->total = $this->count(); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Drapeau a activer en cas d'echec |
|
| 128 | + * (select SQL errone, non chargement des DATA, etc). |
|
| 129 | + */ |
|
| 130 | + public function err() { |
|
| 131 | + if (method_exists($this->iter, 'err')) { |
|
| 132 | + return $this->iter->err(); |
|
| 133 | + } |
|
| 134 | + if (property_exists($this->iter, 'err')) { |
|
| 135 | + return $this->iter->err; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + return false; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + // recuperer la valeur d'une balise #X |
|
| 142 | + // en fonction des methodes |
|
| 143 | + // et proprietes disponibles |
|
| 144 | + public function get_select($nom) { |
|
| 145 | + if ( |
|
| 146 | + is_object($this->iter) |
|
| 147 | + and method_exists($this->iter, $nom) |
|
| 148 | + ) { |
|
| 149 | + try { |
|
| 150 | + return $this->iter->{$nom}(); |
|
| 151 | + } catch (Exception $e) { |
|
| 152 | + // #GETCHILDREN sur un fichier de DirectoryIterator ... |
|
| 153 | + spip_log("Methode {$nom} en echec sur " . get_class($this->iter)); |
|
| 154 | + spip_log("Cela peut être normal : retour d'une ligne de resultat ne pouvant pas calculer cette methode"); |
|
| 155 | + |
|
| 156 | + return ''; |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + /* |
|
| 160 | 160 | if (property_exists($this->iter, $nom)) { |
| 161 | 161 | return $this->iter->$nom; |
| 162 | 162 | }*/ |
| 163 | - // cle et valeur par defaut |
|
| 164 | - // ICI PLANTAGE SI ON NE CONTROLE PAS $nom |
|
| 165 | - if ( |
|
| 166 | - in_array($nom, ['cle', 'valeur']) |
|
| 167 | - and method_exists($this, $nom) |
|
| 168 | - ) { |
|
| 169 | - return $this->{$nom}(); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - // Par defaut chercher en xpath dans la valeur() |
|
| 173 | - return table_valeur($this->valeur(), $nom, null); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - public function next(): void { |
|
| 177 | - ++$this->pos; |
|
| 178 | - parent::next(); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * revient au depart. |
|
| 183 | - */ |
|
| 184 | - public function rewind(): void { |
|
| 185 | - $this->pos = 0; |
|
| 186 | - $this->fetched = 0; |
|
| 187 | - parent::rewind(); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * aller a la position absolue n, |
|
| 192 | - * comptee depuis le debut. |
|
| 193 | - * |
|
| 194 | - * @param int $n |
|
| 195 | - * absolute pos |
|
| 196 | - * @param string $continue |
|
| 197 | - * param for sql_ api |
|
| 198 | - * |
|
| 199 | - * @return bool |
|
| 200 | - * success or fail if not implemented |
|
| 201 | - */ |
|
| 202 | - public function seek($n = 0, $continue = null) { |
|
| 203 | - if ($this->func_filtre or !method_exists($this->iter, 'seek') or !$this->iter->seek($n)) { |
|
| 204 | - $this->seek_loop($n); |
|
| 205 | - } |
|
| 206 | - $this->pos = $n; |
|
| 207 | - $this->fetched = $n; |
|
| 208 | - |
|
| 209 | - return true; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Avancer de $saut pas. |
|
| 214 | - * |
|
| 215 | - * @param $saut |
|
| 216 | - * @param $max |
|
| 217 | - * |
|
| 218 | - * @return int |
|
| 219 | - */ |
|
| 220 | - public function skip($saut, $max = null) { |
|
| 221 | - // pas de saut en arriere autorise pour cette fonction |
|
| 222 | - if (($saut = intval($saut)) <= 0) { |
|
| 223 | - return $this->pos; |
|
| 224 | - } |
|
| 225 | - $seek = $this->pos + $saut; |
|
| 226 | - // si le saut fait depasser le maxi, on libere la resource |
|
| 227 | - // et on sort |
|
| 228 | - if (is_null($max)) { |
|
| 229 | - $max = $this->count(); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - if ($seek >= $max or $seek >= $this->count()) { |
|
| 233 | - // sortie plus rapide que de faire next() jusqu'a la fin ! |
|
| 234 | - $this->free(); |
|
| 235 | - |
|
| 236 | - return $max; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - $this->seek($seek); |
|
| 240 | - |
|
| 241 | - return $this->pos; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Renvoyer un tableau des donnees correspondantes |
|
| 246 | - * a la position courante de l'iterateur |
|
| 247 | - * en controlant si on respecte le filtre |
|
| 248 | - * Appliquer aussi le critere {offset,limit}. |
|
| 249 | - * |
|
| 250 | - * @return array|bool |
|
| 251 | - */ |
|
| 252 | - public function fetch() { |
|
| 253 | - if (method_exists($this->iter, 'fetch')) { |
|
| 254 | - return $this->iter->fetch(); |
|
| 255 | - } |
|
| 256 | - while ( |
|
| 257 | - $this->valid() |
|
| 258 | - and ( |
|
| 259 | - !$this->accept() |
|
| 260 | - or (isset($this->offset) and $this->fetched++ < $this->offset) |
|
| 261 | - ) |
|
| 262 | - ) { |
|
| 263 | - $this->next(); |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - if (!$this->valid()) { |
|
| 267 | - return false; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - if ( |
|
| 271 | - isset($this->limit) |
|
| 272 | - and $this->fetched > $this->offset + $this->limit |
|
| 273 | - ) { |
|
| 274 | - return false; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - $r = []; |
|
| 278 | - foreach ($this->select as $nom) { |
|
| 279 | - $r[$nom] = $this->get_select($nom); |
|
| 280 | - } |
|
| 281 | - $this->next(); |
|
| 282 | - |
|
| 283 | - return $r; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - // retourner la cle pour #CLE |
|
| 287 | - public function cle() { |
|
| 288 | - return $this->key(); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - // retourner la valeur pour #VALEUR |
|
| 292 | - public function valeur() { |
|
| 293 | - return $this->current(); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Accepte-t-on l'entree courante lue ? |
|
| 298 | - * On execute les filtres pour le savoir. |
|
| 299 | - */ |
|
| 300 | - public function accept(): bool { |
|
| 301 | - if ($f = $this->func_filtre) { |
|
| 302 | - return $f(); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - return true; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * liberer la ressource. |
|
| 310 | - * |
|
| 311 | - * @return bool |
|
| 312 | - */ |
|
| 313 | - public function free() { |
|
| 314 | - if (method_exists($this->iter, 'free')) { |
|
| 315 | - $this->iter->free(); |
|
| 316 | - } |
|
| 317 | - $this->pos = $this->total = 0; |
|
| 318 | - |
|
| 319 | - return true; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Compter le nombre total de resultats |
|
| 324 | - * pour #TOTAL_BOUCLE. |
|
| 325 | - * |
|
| 326 | - * @return int |
|
| 327 | - */ |
|
| 328 | - public function count() { |
|
| 329 | - if (is_null($this->total)) { |
|
| 330 | - if ( |
|
| 331 | - method_exists($this->iter, 'count') |
|
| 332 | - and !$this->func_filtre |
|
| 333 | - ) { |
|
| 334 | - return $this->total = $this->iter->count(); |
|
| 335 | - } |
|
| 336 | - // compter les lignes et rembobiner |
|
| 337 | - $total = 0; |
|
| 338 | - $pos = $this->pos; // sauver la position |
|
| 339 | - $this->rewind(); |
|
| 340 | - while ($this->fetch() and $total < $this->max) { |
|
| 341 | - ++$total; |
|
| 342 | - } |
|
| 343 | - $this->seek($pos); |
|
| 344 | - $this->total = $total; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - return $this->total; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Assembler le tableau de filtres traduits depuis les conditions SQL |
|
| 352 | - * les filtres vides ou null sont ignores. |
|
| 353 | - * |
|
| 354 | - * @param $filtres |
|
| 355 | - * @param string $operateur |
|
| 356 | - * |
|
| 357 | - * @return null|string |
|
| 358 | - */ |
|
| 359 | - protected function assembler_filtres($filtres, $operateur = 'AND') { |
|
| 360 | - $filtres_string = []; |
|
| 361 | - foreach ($filtres as $k => $v) { |
|
| 362 | - // si c'est un tableau de OR/AND + 2 sous-filtres, on recurse pour transformer en chaine |
|
| 363 | - if (is_array($v) and in_array(reset($v), ['OR', 'AND'])) { |
|
| 364 | - $op = array_shift($v); |
|
| 365 | - $v = $this->assembler_filtres($v, $op); |
|
| 366 | - } |
|
| 367 | - if (is_null($v) or !is_string($v) or empty($v)) { |
|
| 368 | - continue; |
|
| 369 | - } |
|
| 370 | - $filtres_string[] = $v; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - if (!count($filtres_string)) { |
|
| 374 | - return null; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - return '(' . implode(") {$operateur} (", $filtres_string) . ')'; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Traduire un element du tableau where SQL en un filtre. |
|
| 382 | - * |
|
| 383 | - * @param $v |
|
| 384 | - * |
|
| 385 | - * @return null|array|string |
|
| 386 | - */ |
|
| 387 | - protected function traduire_condition_sql_en_filtre($v) { |
|
| 388 | - if (is_array($v)) { |
|
| 389 | - if ((count($v) >= 2) && ('REGEXP' == $v[0]) && ("'.*'" == $v[2])) { |
|
| 390 | - return 'true'; |
|
| 391 | - } |
|
| 392 | - if ((count($v) >= 2) && ('LIKE' == $v[0]) && ("'%'" == $v[2])) { |
|
| 393 | - return 'true'; |
|
| 394 | - } |
|
| 395 | - $op = $v[0] ?: $v; |
|
| 396 | - } else { |
|
| 397 | - $op = $v; |
|
| 398 | - } |
|
| 399 | - if ((!$op) or (1 == $op) or ('0=0' == $op)) { |
|
| 400 | - return 'true'; |
|
| 401 | - } |
|
| 402 | - if ('0=1' === $op) { |
|
| 403 | - return 'false'; |
|
| 404 | - } |
|
| 405 | - // traiter {cle IN a,b} ou {valeur !IN a,b} |
|
| 406 | - if (preg_match(',^\(([\w/]+)(\s+NOT)?\s+IN\s+(\(.*\))\)$,', $op, $regs)) { |
|
| 407 | - return $this->composer_filtre($regs[1], 'IN', $regs[3], $regs[2]); |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - // 3 possibilites : count($v) = |
|
| 411 | - // * 1 : {x y} ; on recoit $v[0] = y |
|
| 412 | - // * 2 : {x !op y} ; on recoit $v[0] = 'NOT', $v[1] = array() // array du type {x op y} |
|
| 413 | - // * 3 : {x op y} ; on recoit $v[0] = 'op', $v[1] = x, $v[2] = y |
|
| 414 | - |
|
| 415 | - // 1 : forcement traite par un critere, on passe |
|
| 416 | - if (!$v or !is_array($v) or 1 == count($v)) { |
|
| 417 | - return null; // sera ignore |
|
| 418 | - } |
|
| 419 | - if (2 == count($v) and is_array($v[1])) { |
|
| 420 | - return $this->composer_filtre($v[1][1], $v[1][0], $v[1][2], 'NOT'); |
|
| 421 | - } |
|
| 422 | - if (3 == count($v)) { |
|
| 423 | - // traiter le OR/AND suivi de 2 valeurs |
|
| 424 | - if (in_array($op, ['OR', 'AND'])) { |
|
| 425 | - array_shift($v); |
|
| 426 | - foreach (array_keys($v) as $k) { |
|
| 427 | - $v[$k] = $this->traduire_condition_sql_en_filtre($v[$k]); |
|
| 428 | - if (null === $v[$k]) { |
|
| 429 | - unset($v[$k]); |
|
| 430 | - } elseif ('true' === $v[$k]) { |
|
| 431 | - if ('OR' === $op) { |
|
| 432 | - return 'true'; |
|
| 433 | - } |
|
| 434 | - if ('AND' === $op) { |
|
| 435 | - unset($v[$k]); |
|
| 436 | - } |
|
| 437 | - } elseif ('false' === $v[$k]) { |
|
| 438 | - if ('OR' === $op) { |
|
| 439 | - unset($v[$k]); |
|
| 440 | - } |
|
| 441 | - if ('AND' === $op) { |
|
| 442 | - return 'false'; |
|
| 443 | - } |
|
| 444 | - } |
|
| 445 | - } |
|
| 446 | - if (!count($v)) { |
|
| 447 | - return null; |
|
| 448 | - } |
|
| 449 | - if (1 === count($v)) { |
|
| 450 | - return reset($v); |
|
| 451 | - } |
|
| 452 | - array_unshift($v, $op); |
|
| 453 | - |
|
| 454 | - return $v; |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - return $this->composer_filtre($v[1], $v[0], $v[2]); |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - return null; // sera ignore |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * Calculer un filtre sur un champ du tableau. |
|
| 465 | - * |
|
| 466 | - * @param $cle |
|
| 467 | - * @param $op |
|
| 468 | - * @param $valeur |
|
| 469 | - * @param false $not |
|
| 470 | - * |
|
| 471 | - * @return null|string |
|
| 472 | - */ |
|
| 473 | - protected function composer_filtre($cle, $op, $valeur, $not = false) { |
|
| 474 | - if (method_exists($this->iter, 'exception_des_criteres')) { |
|
| 475 | - if (in_array($cle, $this->iter->exception_des_criteres())) { |
|
| 476 | - return null; |
|
| 477 | - } |
|
| 478 | - } |
|
| 479 | - // TODO: analyser le filtre pour refuser ce qu'on ne sait pas traiter ? |
|
| 480 | - // mais c'est normalement deja opere par calculer_critere_infixe() |
|
| 481 | - // qui regarde la description 'desc' (en casse reelle d'ailleurs : {isDir=1} |
|
| 482 | - // ne sera pas vu si l'on a defini desc['field']['isdir'] pour que #ISDIR soit present. |
|
| 483 | - // il faudrait peut etre definir les 2 champs isDir et isdir... a reflechir... |
|
| 484 | - |
|
| 485 | - // if (!in_array($cle, array('cle', 'valeur'))) |
|
| 486 | - // return; |
|
| 487 | - |
|
| 488 | - $a = '$this->get_select(\'' . $cle . '\')'; |
|
| 489 | - |
|
| 490 | - $filtre = ''; |
|
| 491 | - |
|
| 492 | - if ('REGEXP' == $op) { |
|
| 493 | - $filtre = 'filtrer("match", ' . $a . ', ' . str_replace('\"', '"', $valeur) . ')'; |
|
| 494 | - $op = ''; |
|
| 495 | - } else { |
|
| 496 | - if ('LIKE' == $op) { |
|
| 497 | - $valeur = str_replace(['\"', '_', '%'], ['"', '.', '.*'], preg_quote($valeur)); |
|
| 498 | - $filtre = 'filtrer("match", ' . $a . ', ' . $valeur . ')'; |
|
| 499 | - $op = ''; |
|
| 500 | - } else { |
|
| 501 | - if ('=' == $op) { |
|
| 502 | - $op = '=='; |
|
| 503 | - } else { |
|
| 504 | - if ('IN' == $op) { |
|
| 505 | - $filtre = 'in_array(' . $a . ', array' . $valeur . ')'; |
|
| 506 | - $op = ''; |
|
| 507 | - } else { |
|
| 508 | - if (!in_array($op, ['<', '<=', '>', '>='])) { |
|
| 509 | - spip_log('operateur non reconnu ' . $op); // [todo] mettre une erreur de squelette |
|
| 510 | - $op = ''; |
|
| 511 | - } |
|
| 512 | - } |
|
| 513 | - } |
|
| 514 | - } |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - if ($op) { |
|
| 518 | - $filtre = $a . $op . str_replace('\"', '"', $valeur); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - if ($not) { |
|
| 522 | - $filtre = "!({$filtre})"; |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - return $filtre; |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - // calcule les elements a retournes par fetch() |
|
| 529 | - // enleve les elements inutiles du select() |
|
| 530 | - // |
|
| 531 | - private function calculer_select() { |
|
| 532 | - if ($select = &$this->command['select']) { |
|
| 533 | - foreach ($select as $s) { |
|
| 534 | - // /!\ $s = '.nom' |
|
| 535 | - if ('.' == $s[0]) { |
|
| 536 | - $s = substr($s, 1); |
|
| 537 | - } |
|
| 538 | - $this->select[] = $s; |
|
| 539 | - } |
|
| 540 | - } |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - private function calculer_filtres() { |
|
| 544 | - // Issu de calculer_select() de public/composer L.519 |
|
| 545 | - // TODO: externaliser... |
|
| 546 | - // |
|
| 547 | - // retirer les criteres vides: |
|
| 548 | - // {X ?} avec X absent de l'URL |
|
| 549 | - // {par #ENV{X}} avec X absent de l'URL |
|
| 550 | - // IN sur collection vide (ce dernier devrait pouvoir etre fait a la compil) |
|
| 551 | - if ($where = &$this->command['where']) { |
|
| 552 | - foreach ($where as $k => $v) { |
|
| 553 | - $this->filtre[] = $this->traduire_condition_sql_en_filtre($v); |
|
| 554 | - } |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - // critere {2,7} |
|
| 558 | - if (isset($this->command['limit']) and $this->command['limit']) { |
|
| 559 | - $limit = explode(',', $this->command['limit']); |
|
| 560 | - $this->offset = $limit[0]; |
|
| 561 | - $this->limit = $limit[1]; |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - // Creer la fonction de filtrage sur $this |
|
| 565 | - if ($this->filtre) { |
|
| 566 | - if ($filtres = $this->assembler_filtres($this->filtre)) { |
|
| 567 | - $filtres = 'return ' . $filtres . ';'; |
|
| 568 | - $this->func_filtre = fn () => eval($filtres); |
|
| 569 | - } else { |
|
| 570 | - $this->func_filtre = null; |
|
| 571 | - } |
|
| 572 | - } |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - /* |
|
| 163 | + // cle et valeur par defaut |
|
| 164 | + // ICI PLANTAGE SI ON NE CONTROLE PAS $nom |
|
| 165 | + if ( |
|
| 166 | + in_array($nom, ['cle', 'valeur']) |
|
| 167 | + and method_exists($this, $nom) |
|
| 168 | + ) { |
|
| 169 | + return $this->{$nom}(); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + // Par defaut chercher en xpath dans la valeur() |
|
| 173 | + return table_valeur($this->valeur(), $nom, null); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + public function next(): void { |
|
| 177 | + ++$this->pos; |
|
| 178 | + parent::next(); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * revient au depart. |
|
| 183 | + */ |
|
| 184 | + public function rewind(): void { |
|
| 185 | + $this->pos = 0; |
|
| 186 | + $this->fetched = 0; |
|
| 187 | + parent::rewind(); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * aller a la position absolue n, |
|
| 192 | + * comptee depuis le debut. |
|
| 193 | + * |
|
| 194 | + * @param int $n |
|
| 195 | + * absolute pos |
|
| 196 | + * @param string $continue |
|
| 197 | + * param for sql_ api |
|
| 198 | + * |
|
| 199 | + * @return bool |
|
| 200 | + * success or fail if not implemented |
|
| 201 | + */ |
|
| 202 | + public function seek($n = 0, $continue = null) { |
|
| 203 | + if ($this->func_filtre or !method_exists($this->iter, 'seek') or !$this->iter->seek($n)) { |
|
| 204 | + $this->seek_loop($n); |
|
| 205 | + } |
|
| 206 | + $this->pos = $n; |
|
| 207 | + $this->fetched = $n; |
|
| 208 | + |
|
| 209 | + return true; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Avancer de $saut pas. |
|
| 214 | + * |
|
| 215 | + * @param $saut |
|
| 216 | + * @param $max |
|
| 217 | + * |
|
| 218 | + * @return int |
|
| 219 | + */ |
|
| 220 | + public function skip($saut, $max = null) { |
|
| 221 | + // pas de saut en arriere autorise pour cette fonction |
|
| 222 | + if (($saut = intval($saut)) <= 0) { |
|
| 223 | + return $this->pos; |
|
| 224 | + } |
|
| 225 | + $seek = $this->pos + $saut; |
|
| 226 | + // si le saut fait depasser le maxi, on libere la resource |
|
| 227 | + // et on sort |
|
| 228 | + if (is_null($max)) { |
|
| 229 | + $max = $this->count(); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + if ($seek >= $max or $seek >= $this->count()) { |
|
| 233 | + // sortie plus rapide que de faire next() jusqu'a la fin ! |
|
| 234 | + $this->free(); |
|
| 235 | + |
|
| 236 | + return $max; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + $this->seek($seek); |
|
| 240 | + |
|
| 241 | + return $this->pos; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Renvoyer un tableau des donnees correspondantes |
|
| 246 | + * a la position courante de l'iterateur |
|
| 247 | + * en controlant si on respecte le filtre |
|
| 248 | + * Appliquer aussi le critere {offset,limit}. |
|
| 249 | + * |
|
| 250 | + * @return array|bool |
|
| 251 | + */ |
|
| 252 | + public function fetch() { |
|
| 253 | + if (method_exists($this->iter, 'fetch')) { |
|
| 254 | + return $this->iter->fetch(); |
|
| 255 | + } |
|
| 256 | + while ( |
|
| 257 | + $this->valid() |
|
| 258 | + and ( |
|
| 259 | + !$this->accept() |
|
| 260 | + or (isset($this->offset) and $this->fetched++ < $this->offset) |
|
| 261 | + ) |
|
| 262 | + ) { |
|
| 263 | + $this->next(); |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + if (!$this->valid()) { |
|
| 267 | + return false; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + if ( |
|
| 271 | + isset($this->limit) |
|
| 272 | + and $this->fetched > $this->offset + $this->limit |
|
| 273 | + ) { |
|
| 274 | + return false; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + $r = []; |
|
| 278 | + foreach ($this->select as $nom) { |
|
| 279 | + $r[$nom] = $this->get_select($nom); |
|
| 280 | + } |
|
| 281 | + $this->next(); |
|
| 282 | + |
|
| 283 | + return $r; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + // retourner la cle pour #CLE |
|
| 287 | + public function cle() { |
|
| 288 | + return $this->key(); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + // retourner la valeur pour #VALEUR |
|
| 292 | + public function valeur() { |
|
| 293 | + return $this->current(); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Accepte-t-on l'entree courante lue ? |
|
| 298 | + * On execute les filtres pour le savoir. |
|
| 299 | + */ |
|
| 300 | + public function accept(): bool { |
|
| 301 | + if ($f = $this->func_filtre) { |
|
| 302 | + return $f(); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + return true; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * liberer la ressource. |
|
| 310 | + * |
|
| 311 | + * @return bool |
|
| 312 | + */ |
|
| 313 | + public function free() { |
|
| 314 | + if (method_exists($this->iter, 'free')) { |
|
| 315 | + $this->iter->free(); |
|
| 316 | + } |
|
| 317 | + $this->pos = $this->total = 0; |
|
| 318 | + |
|
| 319 | + return true; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Compter le nombre total de resultats |
|
| 324 | + * pour #TOTAL_BOUCLE. |
|
| 325 | + * |
|
| 326 | + * @return int |
|
| 327 | + */ |
|
| 328 | + public function count() { |
|
| 329 | + if (is_null($this->total)) { |
|
| 330 | + if ( |
|
| 331 | + method_exists($this->iter, 'count') |
|
| 332 | + and !$this->func_filtre |
|
| 333 | + ) { |
|
| 334 | + return $this->total = $this->iter->count(); |
|
| 335 | + } |
|
| 336 | + // compter les lignes et rembobiner |
|
| 337 | + $total = 0; |
|
| 338 | + $pos = $this->pos; // sauver la position |
|
| 339 | + $this->rewind(); |
|
| 340 | + while ($this->fetch() and $total < $this->max) { |
|
| 341 | + ++$total; |
|
| 342 | + } |
|
| 343 | + $this->seek($pos); |
|
| 344 | + $this->total = $total; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + return $this->total; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Assembler le tableau de filtres traduits depuis les conditions SQL |
|
| 352 | + * les filtres vides ou null sont ignores. |
|
| 353 | + * |
|
| 354 | + * @param $filtres |
|
| 355 | + * @param string $operateur |
|
| 356 | + * |
|
| 357 | + * @return null|string |
|
| 358 | + */ |
|
| 359 | + protected function assembler_filtres($filtres, $operateur = 'AND') { |
|
| 360 | + $filtres_string = []; |
|
| 361 | + foreach ($filtres as $k => $v) { |
|
| 362 | + // si c'est un tableau de OR/AND + 2 sous-filtres, on recurse pour transformer en chaine |
|
| 363 | + if (is_array($v) and in_array(reset($v), ['OR', 'AND'])) { |
|
| 364 | + $op = array_shift($v); |
|
| 365 | + $v = $this->assembler_filtres($v, $op); |
|
| 366 | + } |
|
| 367 | + if (is_null($v) or !is_string($v) or empty($v)) { |
|
| 368 | + continue; |
|
| 369 | + } |
|
| 370 | + $filtres_string[] = $v; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + if (!count($filtres_string)) { |
|
| 374 | + return null; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + return '(' . implode(") {$operateur} (", $filtres_string) . ')'; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Traduire un element du tableau where SQL en un filtre. |
|
| 382 | + * |
|
| 383 | + * @param $v |
|
| 384 | + * |
|
| 385 | + * @return null|array|string |
|
| 386 | + */ |
|
| 387 | + protected function traduire_condition_sql_en_filtre($v) { |
|
| 388 | + if (is_array($v)) { |
|
| 389 | + if ((count($v) >= 2) && ('REGEXP' == $v[0]) && ("'.*'" == $v[2])) { |
|
| 390 | + return 'true'; |
|
| 391 | + } |
|
| 392 | + if ((count($v) >= 2) && ('LIKE' == $v[0]) && ("'%'" == $v[2])) { |
|
| 393 | + return 'true'; |
|
| 394 | + } |
|
| 395 | + $op = $v[0] ?: $v; |
|
| 396 | + } else { |
|
| 397 | + $op = $v; |
|
| 398 | + } |
|
| 399 | + if ((!$op) or (1 == $op) or ('0=0' == $op)) { |
|
| 400 | + return 'true'; |
|
| 401 | + } |
|
| 402 | + if ('0=1' === $op) { |
|
| 403 | + return 'false'; |
|
| 404 | + } |
|
| 405 | + // traiter {cle IN a,b} ou {valeur !IN a,b} |
|
| 406 | + if (preg_match(',^\(([\w/]+)(\s+NOT)?\s+IN\s+(\(.*\))\)$,', $op, $regs)) { |
|
| 407 | + return $this->composer_filtre($regs[1], 'IN', $regs[3], $regs[2]); |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + // 3 possibilites : count($v) = |
|
| 411 | + // * 1 : {x y} ; on recoit $v[0] = y |
|
| 412 | + // * 2 : {x !op y} ; on recoit $v[0] = 'NOT', $v[1] = array() // array du type {x op y} |
|
| 413 | + // * 3 : {x op y} ; on recoit $v[0] = 'op', $v[1] = x, $v[2] = y |
|
| 414 | + |
|
| 415 | + // 1 : forcement traite par un critere, on passe |
|
| 416 | + if (!$v or !is_array($v) or 1 == count($v)) { |
|
| 417 | + return null; // sera ignore |
|
| 418 | + } |
|
| 419 | + if (2 == count($v) and is_array($v[1])) { |
|
| 420 | + return $this->composer_filtre($v[1][1], $v[1][0], $v[1][2], 'NOT'); |
|
| 421 | + } |
|
| 422 | + if (3 == count($v)) { |
|
| 423 | + // traiter le OR/AND suivi de 2 valeurs |
|
| 424 | + if (in_array($op, ['OR', 'AND'])) { |
|
| 425 | + array_shift($v); |
|
| 426 | + foreach (array_keys($v) as $k) { |
|
| 427 | + $v[$k] = $this->traduire_condition_sql_en_filtre($v[$k]); |
|
| 428 | + if (null === $v[$k]) { |
|
| 429 | + unset($v[$k]); |
|
| 430 | + } elseif ('true' === $v[$k]) { |
|
| 431 | + if ('OR' === $op) { |
|
| 432 | + return 'true'; |
|
| 433 | + } |
|
| 434 | + if ('AND' === $op) { |
|
| 435 | + unset($v[$k]); |
|
| 436 | + } |
|
| 437 | + } elseif ('false' === $v[$k]) { |
|
| 438 | + if ('OR' === $op) { |
|
| 439 | + unset($v[$k]); |
|
| 440 | + } |
|
| 441 | + if ('AND' === $op) { |
|
| 442 | + return 'false'; |
|
| 443 | + } |
|
| 444 | + } |
|
| 445 | + } |
|
| 446 | + if (!count($v)) { |
|
| 447 | + return null; |
|
| 448 | + } |
|
| 449 | + if (1 === count($v)) { |
|
| 450 | + return reset($v); |
|
| 451 | + } |
|
| 452 | + array_unshift($v, $op); |
|
| 453 | + |
|
| 454 | + return $v; |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + return $this->composer_filtre($v[1], $v[0], $v[2]); |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + return null; // sera ignore |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * Calculer un filtre sur un champ du tableau. |
|
| 465 | + * |
|
| 466 | + * @param $cle |
|
| 467 | + * @param $op |
|
| 468 | + * @param $valeur |
|
| 469 | + * @param false $not |
|
| 470 | + * |
|
| 471 | + * @return null|string |
|
| 472 | + */ |
|
| 473 | + protected function composer_filtre($cle, $op, $valeur, $not = false) { |
|
| 474 | + if (method_exists($this->iter, 'exception_des_criteres')) { |
|
| 475 | + if (in_array($cle, $this->iter->exception_des_criteres())) { |
|
| 476 | + return null; |
|
| 477 | + } |
|
| 478 | + } |
|
| 479 | + // TODO: analyser le filtre pour refuser ce qu'on ne sait pas traiter ? |
|
| 480 | + // mais c'est normalement deja opere par calculer_critere_infixe() |
|
| 481 | + // qui regarde la description 'desc' (en casse reelle d'ailleurs : {isDir=1} |
|
| 482 | + // ne sera pas vu si l'on a defini desc['field']['isdir'] pour que #ISDIR soit present. |
|
| 483 | + // il faudrait peut etre definir les 2 champs isDir et isdir... a reflechir... |
|
| 484 | + |
|
| 485 | + // if (!in_array($cle, array('cle', 'valeur'))) |
|
| 486 | + // return; |
|
| 487 | + |
|
| 488 | + $a = '$this->get_select(\'' . $cle . '\')'; |
|
| 489 | + |
|
| 490 | + $filtre = ''; |
|
| 491 | + |
|
| 492 | + if ('REGEXP' == $op) { |
|
| 493 | + $filtre = 'filtrer("match", ' . $a . ', ' . str_replace('\"', '"', $valeur) . ')'; |
|
| 494 | + $op = ''; |
|
| 495 | + } else { |
|
| 496 | + if ('LIKE' == $op) { |
|
| 497 | + $valeur = str_replace(['\"', '_', '%'], ['"', '.', '.*'], preg_quote($valeur)); |
|
| 498 | + $filtre = 'filtrer("match", ' . $a . ', ' . $valeur . ')'; |
|
| 499 | + $op = ''; |
|
| 500 | + } else { |
|
| 501 | + if ('=' == $op) { |
|
| 502 | + $op = '=='; |
|
| 503 | + } else { |
|
| 504 | + if ('IN' == $op) { |
|
| 505 | + $filtre = 'in_array(' . $a . ', array' . $valeur . ')'; |
|
| 506 | + $op = ''; |
|
| 507 | + } else { |
|
| 508 | + if (!in_array($op, ['<', '<=', '>', '>='])) { |
|
| 509 | + spip_log('operateur non reconnu ' . $op); // [todo] mettre une erreur de squelette |
|
| 510 | + $op = ''; |
|
| 511 | + } |
|
| 512 | + } |
|
| 513 | + } |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + if ($op) { |
|
| 518 | + $filtre = $a . $op . str_replace('\"', '"', $valeur); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + if ($not) { |
|
| 522 | + $filtre = "!({$filtre})"; |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + return $filtre; |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + // calcule les elements a retournes par fetch() |
|
| 529 | + // enleve les elements inutiles du select() |
|
| 530 | + // |
|
| 531 | + private function calculer_select() { |
|
| 532 | + if ($select = &$this->command['select']) { |
|
| 533 | + foreach ($select as $s) { |
|
| 534 | + // /!\ $s = '.nom' |
|
| 535 | + if ('.' == $s[0]) { |
|
| 536 | + $s = substr($s, 1); |
|
| 537 | + } |
|
| 538 | + $this->select[] = $s; |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + private function calculer_filtres() { |
|
| 544 | + // Issu de calculer_select() de public/composer L.519 |
|
| 545 | + // TODO: externaliser... |
|
| 546 | + // |
|
| 547 | + // retirer les criteres vides: |
|
| 548 | + // {X ?} avec X absent de l'URL |
|
| 549 | + // {par #ENV{X}} avec X absent de l'URL |
|
| 550 | + // IN sur collection vide (ce dernier devrait pouvoir etre fait a la compil) |
|
| 551 | + if ($where = &$this->command['where']) { |
|
| 552 | + foreach ($where as $k => $v) { |
|
| 553 | + $this->filtre[] = $this->traduire_condition_sql_en_filtre($v); |
|
| 554 | + } |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + // critere {2,7} |
|
| 558 | + if (isset($this->command['limit']) and $this->command['limit']) { |
|
| 559 | + $limit = explode(',', $this->command['limit']); |
|
| 560 | + $this->offset = $limit[0]; |
|
| 561 | + $this->limit = $limit[1]; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + // Creer la fonction de filtrage sur $this |
|
| 565 | + if ($this->filtre) { |
|
| 566 | + if ($filtres = $this->assembler_filtres($this->filtre)) { |
|
| 567 | + $filtres = 'return ' . $filtres . ';'; |
|
| 568 | + $this->func_filtre = fn () => eval($filtres); |
|
| 569 | + } else { |
|
| 570 | + $this->func_filtre = null; |
|
| 571 | + } |
|
| 572 | + } |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + /* |
|
| 576 | 576 | * aller a la position $n en parcourant |
| 577 | 577 | * un par un tous les elements |
| 578 | 578 | */ |
| 579 | - private function seek_loop($n) { |
|
| 580 | - if ($this->pos > $n) { |
|
| 581 | - $this->rewind(); |
|
| 582 | - } |
|
| 579 | + private function seek_loop($n) { |
|
| 580 | + if ($this->pos > $n) { |
|
| 581 | + $this->rewind(); |
|
| 582 | + } |
|
| 583 | 583 | |
| 584 | - while ($this->pos < $n and $this->valid()) { |
|
| 585 | - $this->next(); |
|
| 586 | - } |
|
| 584 | + while ($this->pos < $n and $this->valid()) { |
|
| 585 | + $this->next(); |
|
| 586 | + } |
|
| 587 | 587 | |
| 588 | - return true; |
|
| 589 | - } |
|
| 588 | + return true; |
|
| 589 | + } |
|
| 590 | 590 | } |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | return $this->iter->{$nom}(); |
| 151 | 151 | } catch (Exception $e) { |
| 152 | 152 | // #GETCHILDREN sur un fichier de DirectoryIterator ... |
| 153 | - spip_log("Methode {$nom} en echec sur " . get_class($this->iter)); |
|
| 153 | + spip_log("Methode {$nom} en echec sur ".get_class($this->iter)); |
|
| 154 | 154 | spip_log("Cela peut être normal : retour d'une ligne de resultat ne pouvant pas calculer cette methode"); |
| 155 | 155 | |
| 156 | 156 | return ''; |
@@ -374,7 +374,7 @@ discard block |
||
| 374 | 374 | return null; |
| 375 | 375 | } |
| 376 | 376 | |
| 377 | - return '(' . implode(") {$operateur} (", $filtres_string) . ')'; |
|
| 377 | + return '('.implode(") {$operateur} (", $filtres_string).')'; |
|
| 378 | 378 | } |
| 379 | 379 | |
| 380 | 380 | /** |
@@ -457,7 +457,7 @@ discard block |
||
| 457 | 457 | return $this->composer_filtre($v[1], $v[0], $v[2]); |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | - return null; // sera ignore |
|
| 460 | + return null; // sera ignore |
|
| 461 | 461 | } |
| 462 | 462 | |
| 463 | 463 | /** |
@@ -485,28 +485,28 @@ discard block |
||
| 485 | 485 | // if (!in_array($cle, array('cle', 'valeur'))) |
| 486 | 486 | // return; |
| 487 | 487 | |
| 488 | - $a = '$this->get_select(\'' . $cle . '\')'; |
|
| 488 | + $a = '$this->get_select(\''.$cle.'\')'; |
|
| 489 | 489 | |
| 490 | 490 | $filtre = ''; |
| 491 | 491 | |
| 492 | 492 | if ('REGEXP' == $op) { |
| 493 | - $filtre = 'filtrer("match", ' . $a . ', ' . str_replace('\"', '"', $valeur) . ')'; |
|
| 493 | + $filtre = 'filtrer("match", '.$a.', '.str_replace('\"', '"', $valeur).')'; |
|
| 494 | 494 | $op = ''; |
| 495 | 495 | } else { |
| 496 | 496 | if ('LIKE' == $op) { |
| 497 | 497 | $valeur = str_replace(['\"', '_', '%'], ['"', '.', '.*'], preg_quote($valeur)); |
| 498 | - $filtre = 'filtrer("match", ' . $a . ', ' . $valeur . ')'; |
|
| 498 | + $filtre = 'filtrer("match", '.$a.', '.$valeur.')'; |
|
| 499 | 499 | $op = ''; |
| 500 | 500 | } else { |
| 501 | 501 | if ('=' == $op) { |
| 502 | 502 | $op = '=='; |
| 503 | 503 | } else { |
| 504 | 504 | if ('IN' == $op) { |
| 505 | - $filtre = 'in_array(' . $a . ', array' . $valeur . ')'; |
|
| 505 | + $filtre = 'in_array('.$a.', array'.$valeur.')'; |
|
| 506 | 506 | $op = ''; |
| 507 | 507 | } else { |
| 508 | 508 | if (!in_array($op, ['<', '<=', '>', '>='])) { |
| 509 | - spip_log('operateur non reconnu ' . $op); // [todo] mettre une erreur de squelette |
|
| 509 | + spip_log('operateur non reconnu '.$op); // [todo] mettre une erreur de squelette |
|
| 510 | 510 | $op = ''; |
| 511 | 511 | } |
| 512 | 512 | } |
@@ -515,7 +515,7 @@ discard block |
||
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | if ($op) { |
| 518 | - $filtre = $a . $op . str_replace('\"', '"', $valeur); |
|
| 518 | + $filtre = $a.$op.str_replace('\"', '"', $valeur); |
|
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | if ($not) { |
@@ -564,7 +564,7 @@ discard block |
||
| 564 | 564 | // Creer la fonction de filtrage sur $this |
| 565 | 565 | if ($this->filtre) { |
| 566 | 566 | if ($filtres = $this->assembler_filtres($this->filtre)) { |
| 567 | - $filtres = 'return ' . $filtres . ';'; |
|
| 567 | + $filtres = 'return '.$filtres.';'; |
|
| 568 | 568 | $this->func_filtre = fn () => eval($filtres); |
| 569 | 569 | } else { |
| 570 | 570 | $this->func_filtre = null; |
@@ -9,12 +9,12 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Condition extends Data |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Obtenir les données de la boucle CONDITION. |
|
| 14 | - * |
|
| 15 | - * @param array $command |
|
| 16 | - */ |
|
| 17 | - protected function select($command) { |
|
| 18 | - $this->tableau = [0 => 1]; |
|
| 19 | - } |
|
| 12 | + /** |
|
| 13 | + * Obtenir les données de la boucle CONDITION. |
|
| 14 | + * |
|
| 15 | + * @param array $command |
|
| 16 | + */ |
|
| 17 | + protected function select($command) { |
|
| 18 | + $this->tableau = [0 => 1]; |
|
| 19 | + } |
|
| 20 | 20 | } |
@@ -12,193 +12,193 @@ |
||
| 12 | 12 | class Sql extends AbstractIterateur implements Iterator |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Ressource sql. |
|
| 17 | - * |
|
| 18 | - * @var bool|object |
|
| 19 | - */ |
|
| 20 | - protected $sqlresult = false; |
|
| 15 | + /** |
|
| 16 | + * Ressource sql. |
|
| 17 | + * |
|
| 18 | + * @var bool|object |
|
| 19 | + */ |
|
| 20 | + protected $sqlresult = false; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * row sql courante. |
|
| 24 | - * |
|
| 25 | - * @var null|array |
|
| 26 | - */ |
|
| 27 | - protected $row; |
|
| 22 | + /** |
|
| 23 | + * row sql courante. |
|
| 24 | + * |
|
| 25 | + * @var null|array |
|
| 26 | + */ |
|
| 27 | + protected $row; |
|
| 28 | 28 | |
| 29 | - protected bool $firstseek = false; |
|
| 29 | + protected bool $firstseek = false; |
|
| 30 | 30 | |
| 31 | - protected int $pos = -1; |
|
| 31 | + protected int $pos = -1; |
|
| 32 | 32 | |
| 33 | - /* |
|
| 33 | + /* |
|
| 34 | 34 | * array command: les commandes d'initialisation |
| 35 | 35 | * array info: les infos sur le squelette |
| 36 | 36 | */ |
| 37 | - public function __construct(array $command, array $info = []) { |
|
| 38 | - $this->type = 'SQL'; |
|
| 39 | - parent::__construct($command, $info); |
|
| 40 | - |
|
| 41 | - $this->select(); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Rembobiner. |
|
| 46 | - * |
|
| 47 | - * @return bool |
|
| 48 | - */ |
|
| 49 | - public function rewind(): void { |
|
| 50 | - if ($this->pos > 0) { |
|
| 51 | - $this->seek(0); |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Verifier l'etat de l'iterateur. |
|
| 57 | - */ |
|
| 58 | - public function valid(): bool { |
|
| 59 | - if ($this->err) { |
|
| 60 | - return false; |
|
| 61 | - } |
|
| 62 | - if (!$this->firstseek) { |
|
| 63 | - $this->next(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - return is_array($this->row); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Valeurs sur la position courante. |
|
| 71 | - * |
|
| 72 | - * @return array |
|
| 73 | - */ |
|
| 74 | - #[\ReturnTypeWillChange] |
|
| 75 | - public function current() { |
|
| 76 | - return $this->row; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - #[\ReturnTypeWillChange] |
|
| 80 | - public function key() { |
|
| 81 | - return $this->pos; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Sauter a une position absolue. |
|
| 86 | - * |
|
| 87 | - * @param int $n |
|
| 88 | - * @param null|string $continue |
|
| 89 | - * |
|
| 90 | - * @return bool |
|
| 91 | - */ |
|
| 92 | - public function seek($n = 0, $continue = null) { |
|
| 93 | - if (!sql_seek($this->sqlresult, $n, $this->command['connect'], $continue)) { |
|
| 94 | - // SQLite ne sait pas seek(), il faut relancer la query |
|
| 95 | - // si la position courante est apres la position visee |
|
| 96 | - // il faut relancer la requete |
|
| 97 | - if ($this->pos > $n) { |
|
| 98 | - $this->free(); |
|
| 99 | - $this->select(); |
|
| 100 | - $this->valid(); |
|
| 101 | - } |
|
| 102 | - // et utiliser la methode par defaut pour se deplacer au bon endroit |
|
| 103 | - // (sera fait en cas d'echec de cette fonction) |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 106 | - $this->row = sql_fetch($this->sqlresult, $this->command['connect']); |
|
| 107 | - $this->pos = min($n, $this->count()); |
|
| 108 | - |
|
| 109 | - return true; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Avancer d'un cran. |
|
| 114 | - */ |
|
| 115 | - public function next(): void { |
|
| 116 | - $this->row = sql_fetch($this->sqlresult, $this->command['connect']); |
|
| 117 | - ++$this->pos; |
|
| 118 | - $this->firstseek |= true; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Avancer et retourner les donnees pour le nouvel element. |
|
| 123 | - * |
|
| 124 | - * @return null|array|bool |
|
| 125 | - */ |
|
| 126 | - public function fetch() { |
|
| 127 | - if ($this->valid()) { |
|
| 128 | - $r = $this->current(); |
|
| 129 | - $this->next(); |
|
| 130 | - } else { |
|
| 131 | - $r = false; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return $r; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * liberer les ressources. |
|
| 139 | - * |
|
| 140 | - * @return bool |
|
| 141 | - */ |
|
| 142 | - public function free() { |
|
| 143 | - if (!$this->sqlresult) { |
|
| 144 | - return true; |
|
| 145 | - } |
|
| 146 | - $a = sql_free($this->sqlresult, $this->command['connect']); |
|
| 147 | - $this->sqlresult = null; |
|
| 148 | - |
|
| 149 | - return $a; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Compter le nombre de resultats. |
|
| 154 | - * |
|
| 155 | - * @return int |
|
| 156 | - */ |
|
| 157 | - public function count() { |
|
| 158 | - if (is_null($this->total)) { |
|
| 159 | - if (!$this->sqlresult) { |
|
| 160 | - $this->total = 0; |
|
| 161 | - } else { |
|
| 162 | - // cas count(*) |
|
| 163 | - if (in_array('count(*)', $this->command['select'])) { |
|
| 164 | - $this->valid(); |
|
| 165 | - $s = $this->current(); |
|
| 166 | - $this->total = $s['count(*)']; |
|
| 167 | - } else { |
|
| 168 | - $this->total = sql_count($this->sqlresult, $this->command['connect']); |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - return $this->total; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * selectionner les donnees, ie faire la requete SQL. |
|
| 178 | - */ |
|
| 179 | - protected function select() { |
|
| 180 | - $this->row = null; |
|
| 181 | - $v = &$this->command; |
|
| 182 | - $this->sqlresult = calculer_select( |
|
| 183 | - $v['select'], |
|
| 184 | - $v['from'], |
|
| 185 | - $v['type'], |
|
| 186 | - $v['where'], |
|
| 187 | - $v['join'], |
|
| 188 | - $v['groupby'], |
|
| 189 | - $v['orderby'], |
|
| 190 | - $v['limit'], |
|
| 191 | - $v['having'], |
|
| 192 | - $v['table'], |
|
| 193 | - $v['id'], |
|
| 194 | - $v['connect'], |
|
| 195 | - $this->info |
|
| 196 | - ); |
|
| 197 | - $this->err = !$this->sqlresult; |
|
| 198 | - $this->firstseek = false; |
|
| 199 | - $this->pos = -1; |
|
| 200 | - |
|
| 201 | - // pas d'init a priori, le calcul ne sera fait qu'en cas de besoin (provoque une double requete souvent inutile en sqlite) |
|
| 202 | - //$this->total = $this->count(); |
|
| 203 | - } |
|
| 37 | + public function __construct(array $command, array $info = []) { |
|
| 38 | + $this->type = 'SQL'; |
|
| 39 | + parent::__construct($command, $info); |
|
| 40 | + |
|
| 41 | + $this->select(); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Rembobiner. |
|
| 46 | + * |
|
| 47 | + * @return bool |
|
| 48 | + */ |
|
| 49 | + public function rewind(): void { |
|
| 50 | + if ($this->pos > 0) { |
|
| 51 | + $this->seek(0); |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Verifier l'etat de l'iterateur. |
|
| 57 | + */ |
|
| 58 | + public function valid(): bool { |
|
| 59 | + if ($this->err) { |
|
| 60 | + return false; |
|
| 61 | + } |
|
| 62 | + if (!$this->firstseek) { |
|
| 63 | + $this->next(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + return is_array($this->row); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Valeurs sur la position courante. |
|
| 71 | + * |
|
| 72 | + * @return array |
|
| 73 | + */ |
|
| 74 | + #[\ReturnTypeWillChange] |
|
| 75 | + public function current() { |
|
| 76 | + return $this->row; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + #[\ReturnTypeWillChange] |
|
| 80 | + public function key() { |
|
| 81 | + return $this->pos; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Sauter a une position absolue. |
|
| 86 | + * |
|
| 87 | + * @param int $n |
|
| 88 | + * @param null|string $continue |
|
| 89 | + * |
|
| 90 | + * @return bool |
|
| 91 | + */ |
|
| 92 | + public function seek($n = 0, $continue = null) { |
|
| 93 | + if (!sql_seek($this->sqlresult, $n, $this->command['connect'], $continue)) { |
|
| 94 | + // SQLite ne sait pas seek(), il faut relancer la query |
|
| 95 | + // si la position courante est apres la position visee |
|
| 96 | + // il faut relancer la requete |
|
| 97 | + if ($this->pos > $n) { |
|
| 98 | + $this->free(); |
|
| 99 | + $this->select(); |
|
| 100 | + $this->valid(); |
|
| 101 | + } |
|
| 102 | + // et utiliser la methode par defaut pour se deplacer au bon endroit |
|
| 103 | + // (sera fait en cas d'echec de cette fonction) |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | + $this->row = sql_fetch($this->sqlresult, $this->command['connect']); |
|
| 107 | + $this->pos = min($n, $this->count()); |
|
| 108 | + |
|
| 109 | + return true; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Avancer d'un cran. |
|
| 114 | + */ |
|
| 115 | + public function next(): void { |
|
| 116 | + $this->row = sql_fetch($this->sqlresult, $this->command['connect']); |
|
| 117 | + ++$this->pos; |
|
| 118 | + $this->firstseek |= true; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Avancer et retourner les donnees pour le nouvel element. |
|
| 123 | + * |
|
| 124 | + * @return null|array|bool |
|
| 125 | + */ |
|
| 126 | + public function fetch() { |
|
| 127 | + if ($this->valid()) { |
|
| 128 | + $r = $this->current(); |
|
| 129 | + $this->next(); |
|
| 130 | + } else { |
|
| 131 | + $r = false; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return $r; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * liberer les ressources. |
|
| 139 | + * |
|
| 140 | + * @return bool |
|
| 141 | + */ |
|
| 142 | + public function free() { |
|
| 143 | + if (!$this->sqlresult) { |
|
| 144 | + return true; |
|
| 145 | + } |
|
| 146 | + $a = sql_free($this->sqlresult, $this->command['connect']); |
|
| 147 | + $this->sqlresult = null; |
|
| 148 | + |
|
| 149 | + return $a; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Compter le nombre de resultats. |
|
| 154 | + * |
|
| 155 | + * @return int |
|
| 156 | + */ |
|
| 157 | + public function count() { |
|
| 158 | + if (is_null($this->total)) { |
|
| 159 | + if (!$this->sqlresult) { |
|
| 160 | + $this->total = 0; |
|
| 161 | + } else { |
|
| 162 | + // cas count(*) |
|
| 163 | + if (in_array('count(*)', $this->command['select'])) { |
|
| 164 | + $this->valid(); |
|
| 165 | + $s = $this->current(); |
|
| 166 | + $this->total = $s['count(*)']; |
|
| 167 | + } else { |
|
| 168 | + $this->total = sql_count($this->sqlresult, $this->command['connect']); |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + return $this->total; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * selectionner les donnees, ie faire la requete SQL. |
|
| 178 | + */ |
|
| 179 | + protected function select() { |
|
| 180 | + $this->row = null; |
|
| 181 | + $v = &$this->command; |
|
| 182 | + $this->sqlresult = calculer_select( |
|
| 183 | + $v['select'], |
|
| 184 | + $v['from'], |
|
| 185 | + $v['type'], |
|
| 186 | + $v['where'], |
|
| 187 | + $v['join'], |
|
| 188 | + $v['groupby'], |
|
| 189 | + $v['orderby'], |
|
| 190 | + $v['limit'], |
|
| 191 | + $v['having'], |
|
| 192 | + $v['table'], |
|
| 193 | + $v['id'], |
|
| 194 | + $v['connect'], |
|
| 195 | + $this->info |
|
| 196 | + ); |
|
| 197 | + $this->err = !$this->sqlresult; |
|
| 198 | + $this->firstseek = false; |
|
| 199 | + $this->pos = -1; |
|
| 200 | + |
|
| 201 | + // pas d'init a priori, le calcul ne sera fait qu'en cas de besoin (provoque une double requete souvent inutile en sqlite) |
|
| 202 | + //$this->total = $this->count(); |
|
| 203 | + } |
|
| 204 | 204 | } |