@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | |
| 24 | 24 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 25 | - return; |
|
| 25 | + return; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
@@ -43,90 +43,90 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | function _sqlite_init_functions(&$sqlite) { |
| 45 | 45 | |
| 46 | - if (!$sqlite) { |
|
| 47 | - return false; |
|
| 48 | - } |
|
| 46 | + if (!$sqlite) { |
|
| 47 | + return false; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | 50 | |
| 51 | - $fonctions = [ |
|
| 52 | - // A |
|
| 53 | - 'ACOS' => ['acos', 1], |
|
| 54 | - 'ASIN' => ['asin', 1], |
|
| 55 | - 'ATAN' => ['atan', 1], // mysql accepte 2 params comme atan2… hum ? |
|
| 56 | - 'ATAN2' => ['atan2', 2], |
|
| 51 | + $fonctions = [ |
|
| 52 | + // A |
|
| 53 | + 'ACOS' => ['acos', 1], |
|
| 54 | + 'ASIN' => ['asin', 1], |
|
| 55 | + 'ATAN' => ['atan', 1], // mysql accepte 2 params comme atan2… hum ? |
|
| 56 | + 'ATAN2' => ['atan2', 2], |
|
| 57 | 57 | |
| 58 | - // C |
|
| 59 | - 'CEIL' => ['_sqlite_func_ceil', 1], |
|
| 60 | - 'CONCAT' => ['_sqlite_func_concat', -1], |
|
| 61 | - 'COS' => ['cos', 1], |
|
| 58 | + // C |
|
| 59 | + 'CEIL' => ['_sqlite_func_ceil', 1], |
|
| 60 | + 'CONCAT' => ['_sqlite_func_concat', -1], |
|
| 61 | + 'COS' => ['cos', 1], |
|
| 62 | 62 | |
| 63 | - // D |
|
| 64 | - 'DATE_FORMAT' => ['_sqlite_func_date_format', 2], // équivalent a strftime avec args inversés |
|
| 65 | - 'DAYOFMONTH' => ['_sqlite_func_dayofmonth', 1], |
|
| 66 | - 'DEGREES' => ['rad2deg', 1], |
|
| 63 | + // D |
|
| 64 | + 'DATE_FORMAT' => ['_sqlite_func_date_format', 2], // équivalent a strftime avec args inversés |
|
| 65 | + 'DAYOFMONTH' => ['_sqlite_func_dayofmonth', 1], |
|
| 66 | + 'DEGREES' => ['rad2deg', 1], |
|
| 67 | 67 | |
| 68 | - // E |
|
| 69 | - 'EXTRAIRE_MULTI' => ['_sqlite_func_extraire_multi', 2], // specifique a SPIP/sql_multi() |
|
| 70 | - 'EXP' => ['exp', 1], |
|
| 68 | + // E |
|
| 69 | + 'EXTRAIRE_MULTI' => ['_sqlite_func_extraire_multi', 2], // specifique a SPIP/sql_multi() |
|
| 70 | + 'EXP' => ['exp', 1], |
|
| 71 | 71 | |
| 72 | - // F |
|
| 73 | - 'FIND_IN_SET' => ['_sqlite_func_find_in_set', 2], |
|
| 74 | - 'FLOOR' => ['_sqlite_func_floor', 1], |
|
| 72 | + // F |
|
| 73 | + 'FIND_IN_SET' => ['_sqlite_func_find_in_set', 2], |
|
| 74 | + 'FLOOR' => ['_sqlite_func_floor', 1], |
|
| 75 | 75 | |
| 76 | - // G |
|
| 77 | - 'GREATEST' => ['_sqlite_func_greatest', -1], |
|
| 76 | + // G |
|
| 77 | + 'GREATEST' => ['_sqlite_func_greatest', -1], |
|
| 78 | 78 | |
| 79 | - // I |
|
| 80 | - 'IF' => ['_sqlite_func_if', 3], |
|
| 81 | - 'INSERT' => ['_sqlite_func_insert', 4], |
|
| 82 | - 'INSTR' => ['_sqlite_func_instr', 2], |
|
| 79 | + // I |
|
| 80 | + 'IF' => ['_sqlite_func_if', 3], |
|
| 81 | + 'INSERT' => ['_sqlite_func_insert', 4], |
|
| 82 | + 'INSTR' => ['_sqlite_func_instr', 2], |
|
| 83 | 83 | |
| 84 | - // L |
|
| 85 | - 'LEAST' => ['_sqlite_func_least', -1], |
|
| 86 | - '_LEFT' => ['_sqlite_func_left', 2], |
|
| 84 | + // L |
|
| 85 | + 'LEAST' => ['_sqlite_func_least', -1], |
|
| 86 | + '_LEFT' => ['_sqlite_func_left', 2], |
|
| 87 | 87 | |
| 88 | - // N |
|
| 89 | - 'NOW' => ['_sqlite_func_now', 0], |
|
| 88 | + // N |
|
| 89 | + 'NOW' => ['_sqlite_func_now', 0], |
|
| 90 | 90 | |
| 91 | - // M |
|
| 92 | - 'MD5' => ['md5', 1], |
|
| 93 | - 'MONTH' => ['_sqlite_func_month', 1], |
|
| 91 | + // M |
|
| 92 | + 'MD5' => ['md5', 1], |
|
| 93 | + 'MONTH' => ['_sqlite_func_month', 1], |
|
| 94 | 94 | |
| 95 | - // P |
|
| 96 | - 'PREG_REPLACE' => ['_sqlite_func_preg_replace', 3], |
|
| 95 | + // P |
|
| 96 | + 'PREG_REPLACE' => ['_sqlite_func_preg_replace', 3], |
|
| 97 | 97 | |
| 98 | - // R |
|
| 99 | - 'RADIANS' => ['deg2rad', 1], |
|
| 100 | - 'RAND' => ['_sqlite_func_rand', 0], // sinon random() v2.4 |
|
| 101 | - 'REGEXP' => ['_sqlite_func_regexp_match', 2], // critere REGEXP supporte a partir de v3.3.2 |
|
| 102 | - 'RIGHT' => ['_sqlite_func_right', 2], |
|
| 98 | + // R |
|
| 99 | + 'RADIANS' => ['deg2rad', 1], |
|
| 100 | + 'RAND' => ['_sqlite_func_rand', 0], // sinon random() v2.4 |
|
| 101 | + 'REGEXP' => ['_sqlite_func_regexp_match', 2], // critere REGEXP supporte a partir de v3.3.2 |
|
| 102 | + 'RIGHT' => ['_sqlite_func_right', 2], |
|
| 103 | 103 | |
| 104 | - // S |
|
| 105 | - 'SETTYPE' => ['settype', 2], // CAST present en v3.2.3 |
|
| 106 | - 'SIN' => ['sin', 1], |
|
| 107 | - 'SQRT' => ['sqrt', 1], |
|
| 108 | - 'SUBSTRING' => ['_sqlite_func_substring' /*, 3*/], // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0 |
|
| 104 | + // S |
|
| 105 | + 'SETTYPE' => ['settype', 2], // CAST present en v3.2.3 |
|
| 106 | + 'SIN' => ['sin', 1], |
|
| 107 | + 'SQRT' => ['sqrt', 1], |
|
| 108 | + 'SUBSTRING' => ['_sqlite_func_substring' /*, 3*/], // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0 |
|
| 109 | 109 | |
| 110 | - // T |
|
| 111 | - 'TAN' => ['tan', 1], |
|
| 112 | - 'TIMESTAMPDIFF' => ['_sqlite_timestampdiff' /*, 3*/], |
|
| 113 | - 'TO_DAYS' => ['_sqlite_func_to_days', 1], |
|
| 110 | + // T |
|
| 111 | + 'TAN' => ['tan', 1], |
|
| 112 | + 'TIMESTAMPDIFF' => ['_sqlite_timestampdiff' /*, 3*/], |
|
| 113 | + 'TO_DAYS' => ['_sqlite_func_to_days', 1], |
|
| 114 | 114 | |
| 115 | - // U |
|
| 116 | - 'UNIX_TIMESTAMP' => ['_sqlite_func_unix_timestamp', 1], |
|
| 115 | + // U |
|
| 116 | + 'UNIX_TIMESTAMP' => ['_sqlite_func_unix_timestamp', 1], |
|
| 117 | 117 | |
| 118 | - // V |
|
| 119 | - 'VIDE' => ['_sqlite_func_vide', 0], // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide() |
|
| 118 | + // V |
|
| 119 | + 'VIDE' => ['_sqlite_func_vide', 0], // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide() |
|
| 120 | 120 | |
| 121 | - // Y |
|
| 122 | - 'YEAR' => ['_sqlite_func_year', 1] |
|
| 123 | - ]; |
|
| 121 | + // Y |
|
| 122 | + 'YEAR' => ['_sqlite_func_year', 1] |
|
| 123 | + ]; |
|
| 124 | 124 | |
| 125 | - foreach ($fonctions as $f => $r) { |
|
| 126 | - _sqlite_add_function($sqlite, $f, $r); |
|
| 127 | - } |
|
| 125 | + foreach ($fonctions as $f => $r) { |
|
| 126 | + _sqlite_add_function($sqlite, $f, $r); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - #spip_log('functions sqlite chargees ','sqlite.'._LOG_DEBUG); |
|
| 129 | + #spip_log('functions sqlite chargees ','sqlite.'._LOG_DEBUG); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | |
@@ -147,9 +147,9 @@ discard block |
||
| 147 | 147 | * |
| 148 | 148 | **/ |
| 149 | 149 | function _sqlite_add_function(&$sqlite, &$f, &$r) { |
| 150 | - isset($r[1]) |
|
| 151 | - ? $sqlite->sqliteCreateFunction($f, $r[0], $r[1]) |
|
| 152 | - : $sqlite->sqliteCreateFunction($f, $r[0]); |
|
| 150 | + isset($r[1]) |
|
| 151 | + ? $sqlite->sqliteCreateFunction($f, $r[0], $r[1]) |
|
| 152 | + : $sqlite->sqliteCreateFunction($f, $r[0]); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | /** |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | * @return int |
| 160 | 160 | */ |
| 161 | 161 | function _sqlite_func_ceil($a) { |
| 162 | - return ceil($a); |
|
| 162 | + return ceil($a); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | * @return string |
| 170 | 170 | */ |
| 171 | 171 | function _sqlite_func_concat(...$args) { |
| 172 | - return join('', $args); |
|
| 172 | + return join('', $args); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | * @return int |
| 183 | 183 | */ |
| 184 | 184 | function _sqlite_func_dayofmonth($d) { |
| 185 | - return _sqlite_func_date('d', $d); |
|
| 185 | + return _sqlite_func_date('d', $d); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | |
@@ -194,15 +194,15 @@ discard block |
||
| 194 | 194 | * @return int |
| 195 | 195 | */ |
| 196 | 196 | function _sqlite_func_find_in_set($num, $set) { |
| 197 | - $rank = 0; |
|
| 198 | - foreach (explode(',', $set) as $v) { |
|
| 199 | - if ($v == $num) { |
|
| 200 | - return (++$rank); |
|
| 201 | - } |
|
| 202 | - $rank++; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - return 0; |
|
| 197 | + $rank = 0; |
|
| 198 | + foreach (explode(',', $set) as $v) { |
|
| 199 | + if ($v == $num) { |
|
| 200 | + return (++$rank); |
|
| 201 | + } |
|
| 202 | + $rank++; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + return 0; |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | /** |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | * @return int |
| 213 | 213 | */ |
| 214 | 214 | function _sqlite_func_floor($a) { |
| 215 | - return floor($a); |
|
| 215 | + return floor($a); |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | * @return mixed |
| 226 | 226 | */ |
| 227 | 227 | function _sqlite_func_if($bool, $oui, $non) { |
| 228 | - return ($bool) ? $oui : $non; |
|
| 228 | + return ($bool) ? $oui : $non; |
|
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | |
@@ -242,10 +242,10 @@ discard block |
||
| 242 | 242 | * @return string |
| 243 | 243 | */ |
| 244 | 244 | function _sqlite_func_insert($s, $index, $longueur, $chaine) { |
| 245 | - return |
|
| 246 | - substr($s, 0, $index) |
|
| 247 | - . $chaine |
|
| 248 | - . substr(substr($s, $index), $longueur); |
|
| 245 | + return |
|
| 246 | + substr($s, 0, $index) |
|
| 247 | + . $chaine |
|
| 248 | + . substr(substr($s, $index), $longueur); |
|
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | * @return int |
| 258 | 258 | */ |
| 259 | 259 | function _sqlite_func_instr($s, $search) { |
| 260 | - return strpos($s, $search); |
|
| 260 | + return strpos($s, $search); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | |
@@ -268,7 +268,7 @@ discard block |
||
| 268 | 268 | * @return int |
| 269 | 269 | */ |
| 270 | 270 | function _sqlite_func_least(...$args) { |
| 271 | - return min($args); |
|
| 271 | + return min($args); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | * @return int |
| 280 | 280 | */ |
| 281 | 281 | function _sqlite_func_greatest(...$args) { |
| 282 | - return max($args); |
|
| 282 | + return max($args); |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | |
@@ -291,7 +291,7 @@ discard block |
||
| 291 | 291 | * @return string |
| 292 | 292 | */ |
| 293 | 293 | function _sqlite_func_left($s, $lenght) { |
| 294 | - return substr($s, $lenght); |
|
| 294 | + return substr($s, $lenght); |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | /** |
@@ -301,13 +301,13 @@ discard block |
||
| 301 | 301 | * @return string |
| 302 | 302 | */ |
| 303 | 303 | function _sqlite_func_now($force_refresh = false) { |
| 304 | - static $now = null; |
|
| 305 | - if (is_null($now) or $force_refresh) { |
|
| 306 | - $now = date('Y-m-d H:i:s'); |
|
| 307 | - } |
|
| 304 | + static $now = null; |
|
| 305 | + if (is_null($now) or $force_refresh) { |
|
| 306 | + $now = date('Y-m-d H:i:s'); |
|
| 307 | + } |
|
| 308 | 308 | |
| 309 | - #spip_log("Passage avec NOW : $now | ".time(),'sqlite.'._LOG_DEBUG); |
|
| 310 | - return $now; |
|
| 309 | + #spip_log("Passage avec NOW : $now | ".time(),'sqlite.'._LOG_DEBUG); |
|
| 310 | + return $now; |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | * @return int |
| 321 | 321 | */ |
| 322 | 322 | function _sqlite_func_month($d) { |
| 323 | - return _sqlite_func_date('m', $d); |
|
| 323 | + return _sqlite_func_date('m', $d); |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | |
@@ -333,10 +333,10 @@ discard block |
||
| 333 | 333 | * @return string |
| 334 | 334 | */ |
| 335 | 335 | function _sqlite_func_preg_replace($quoi, $cherche, $remplace) { |
| 336 | - $return = preg_replace('%' . $cherche . '%', $remplace, $quoi); |
|
| 336 | + $return = preg_replace('%' . $cherche . '%', $remplace, $quoi); |
|
| 337 | 337 | |
| 338 | - #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 339 | - return $return; |
|
| 338 | + #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 339 | + return $return; |
|
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | /** |
@@ -349,26 +349,26 @@ discard block |
||
| 349 | 349 | * @return string, l'extrait trouve. |
| 350 | 350 | **/ |
| 351 | 351 | function _sqlite_func_extraire_multi($quoi, $lang) { |
| 352 | - if (!defined('_EXTRAIRE_MULTI')) { |
|
| 353 | - include_spip('inc/filtres'); |
|
| 354 | - } |
|
| 355 | - if (!function_exists('approcher_langue')) { |
|
| 356 | - include_spip('inc/lang'); |
|
| 357 | - } |
|
| 358 | - if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) { |
|
| 359 | - foreach ($regs as $reg) { |
|
| 360 | - // chercher la version de la langue courante |
|
| 361 | - $trads = extraire_trads($reg[1]); |
|
| 362 | - if ($l = approcher_langue($trads, $lang)) { |
|
| 363 | - $trad = $trads[$l]; |
|
| 364 | - } else { |
|
| 365 | - $trad = reset($trads); |
|
| 366 | - } |
|
| 367 | - $quoi = str_replace($reg[0], $trad, $quoi); |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return $quoi; |
|
| 352 | + if (!defined('_EXTRAIRE_MULTI')) { |
|
| 353 | + include_spip('inc/filtres'); |
|
| 354 | + } |
|
| 355 | + if (!function_exists('approcher_langue')) { |
|
| 356 | + include_spip('inc/lang'); |
|
| 357 | + } |
|
| 358 | + if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) { |
|
| 359 | + foreach ($regs as $reg) { |
|
| 360 | + // chercher la version de la langue courante |
|
| 361 | + $trads = extraire_trads($reg[1]); |
|
| 362 | + if ($l = approcher_langue($trads, $lang)) { |
|
| 363 | + $trad = $trads[$l]; |
|
| 364 | + } else { |
|
| 365 | + $trad = reset($trads); |
|
| 366 | + } |
|
| 367 | + $quoi = str_replace($reg[0], $trad, $quoi); |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return $quoi; |
|
| 372 | 372 | } |
| 373 | 373 | |
| 374 | 374 | |
@@ -378,7 +378,7 @@ discard block |
||
| 378 | 378 | * @return float |
| 379 | 379 | */ |
| 380 | 380 | function _sqlite_func_rand() { |
| 381 | - return random_int(0, mt_getrandmax()); |
|
| 381 | + return random_int(0, mt_getrandmax()); |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | |
@@ -390,7 +390,7 @@ discard block |
||
| 390 | 390 | * @return string |
| 391 | 391 | */ |
| 392 | 392 | function _sqlite_func_right($s, $length) { |
| 393 | - return substr($s, 0 - $length); |
|
| 393 | + return substr($s, 0 - $length); |
|
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | |
@@ -402,17 +402,17 @@ discard block |
||
| 402 | 402 | * @return bool |
| 403 | 403 | */ |
| 404 | 404 | function _sqlite_func_regexp_match($cherche, $quoi) { |
| 405 | - // optimiser un cas tres courant avec les requetes en base |
|
| 406 | - if (!$quoi and !strlen($quoi)) { |
|
| 407 | - return false; |
|
| 408 | - } |
|
| 409 | - // il faut enlever un niveau d'echappement pour être homogène à mysql |
|
| 410 | - $cherche = str_replace('\\\\', '\\', $cherche); |
|
| 411 | - $u = $GLOBALS['meta']['pcre_u'] ?? 'u'; |
|
| 412 | - $return = preg_match('%' . $cherche . '%imsS' . $u, $quoi); |
|
| 413 | - |
|
| 414 | - #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 415 | - return $return; |
|
| 405 | + // optimiser un cas tres courant avec les requetes en base |
|
| 406 | + if (!$quoi and !strlen($quoi)) { |
|
| 407 | + return false; |
|
| 408 | + } |
|
| 409 | + // il faut enlever un niveau d'echappement pour être homogène à mysql |
|
| 410 | + $cherche = str_replace('\\\\', '\\', $cherche); |
|
| 411 | + $u = $GLOBALS['meta']['pcre_u'] ?? 'u'; |
|
| 412 | + $return = preg_match('%' . $cherche . '%imsS' . $u, $quoi); |
|
| 413 | + |
|
| 414 | + #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 415 | + return $return; |
|
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | |
@@ -427,8 +427,8 @@ discard block |
||
| 427 | 427 | * @return string |
| 428 | 428 | */ |
| 429 | 429 | function _sqlite_func_date_format($date, $conv) { |
| 430 | - $conv = _sqlite_func_strftime_format_converter($conv); |
|
| 431 | - return strftime($conv, is_int($date) ? $date : strtotime($date)); |
|
| 430 | + $conv = _sqlite_func_strftime_format_converter($conv); |
|
| 431 | + return strftime($conv, is_int($date) ? $date : strtotime($date)); |
|
| 432 | 432 | } |
| 433 | 433 | |
| 434 | 434 | /** |
@@ -444,28 +444,28 @@ discard block |
||
| 444 | 444 | * @return void |
| 445 | 445 | */ |
| 446 | 446 | function _sqlite_func_strftime_format_converter(string $conv): string { |
| 447 | - // ok : %a %b %d %e %H %I %l %j %k %m %p %r %S %T %w %y %Y |
|
| 448 | - // on ne sait pas en gérer certains... |
|
| 449 | - static $mysql_to_strftime_not_ok = ['%c', '%D', '%f', '%U', '%V', '%W', '%X']; |
|
| 450 | - static $mysql_to_strftime = [ |
|
| 451 | - '%h' => '%I', |
|
| 452 | - '%i' => '%M', |
|
| 453 | - '%M' => '%B', |
|
| 454 | - '%s' => '%S', |
|
| 455 | - '%u' => '%U', |
|
| 456 | - '%v' => '%V', |
|
| 457 | - '%x' => '%G', |
|
| 458 | - ]; |
|
| 459 | - static $to_strftime = []; |
|
| 460 | - if (!isset($to_strftime[$conv])) { |
|
| 461 | - $count = 0; |
|
| 462 | - str_replace($mysql_to_strftime_not_ok, '', $conv, $count); |
|
| 463 | - if ($count > 0) { |
|
| 464 | - spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_ERREUR); |
|
| 465 | - } |
|
| 466 | - $to_strftime[$conv] = str_replace(array_keys($mysql_to_strftime), $mysql_to_strftime, $conv); |
|
| 467 | - } |
|
| 468 | - return $to_strftime[$conv]; |
|
| 447 | + // ok : %a %b %d %e %H %I %l %j %k %m %p %r %S %T %w %y %Y |
|
| 448 | + // on ne sait pas en gérer certains... |
|
| 449 | + static $mysql_to_strftime_not_ok = ['%c', '%D', '%f', '%U', '%V', '%W', '%X']; |
|
| 450 | + static $mysql_to_strftime = [ |
|
| 451 | + '%h' => '%I', |
|
| 452 | + '%i' => '%M', |
|
| 453 | + '%M' => '%B', |
|
| 454 | + '%s' => '%S', |
|
| 455 | + '%u' => '%U', |
|
| 456 | + '%v' => '%V', |
|
| 457 | + '%x' => '%G', |
|
| 458 | + ]; |
|
| 459 | + static $to_strftime = []; |
|
| 460 | + if (!isset($to_strftime[$conv])) { |
|
| 461 | + $count = 0; |
|
| 462 | + str_replace($mysql_to_strftime_not_ok, '', $conv, $count); |
|
| 463 | + if ($count > 0) { |
|
| 464 | + spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_ERREUR); |
|
| 465 | + } |
|
| 466 | + $to_strftime[$conv] = str_replace(array_keys($mysql_to_strftime), $mysql_to_strftime, $conv); |
|
| 467 | + } |
|
| 468 | + return $to_strftime[$conv]; |
|
| 469 | 469 | } |
| 470 | 470 | |
| 471 | 471 | /** |
@@ -479,11 +479,11 @@ discard block |
||
| 479 | 479 | * @return int |
| 480 | 480 | */ |
| 481 | 481 | function _sqlite_func_to_days($d) { |
| 482 | - static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01 |
|
| 483 | - $result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600)); |
|
| 482 | + static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01 |
|
| 483 | + $result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600)); |
|
| 484 | 484 | |
| 485 | - #spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG); |
|
| 486 | - return $result; |
|
| 485 | + #spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG); |
|
| 486 | + return $result; |
|
| 487 | 487 | } |
| 488 | 488 | |
| 489 | 489 | /** |
@@ -495,13 +495,13 @@ discard block |
||
| 495 | 495 | * @return string |
| 496 | 496 | */ |
| 497 | 497 | function _sqlite_func_substring($string, $start, $len = null) { |
| 498 | - // SQL compte a partir de 1, php a partir de 0 |
|
| 499 | - $start = ($start > 0) ? $start - 1 : $start; |
|
| 500 | - if (is_null($len)) { |
|
| 501 | - return substr($string, $start); |
|
| 502 | - } else { |
|
| 503 | - return substr($string, $start, $len); |
|
| 504 | - } |
|
| 498 | + // SQL compte a partir de 1, php a partir de 0 |
|
| 499 | + $start = ($start > 0) ? $start - 1 : $start; |
|
| 500 | + if (is_null($len)) { |
|
| 501 | + return substr($string, $start); |
|
| 502 | + } else { |
|
| 503 | + return substr($string, $start, $len); |
|
| 504 | + } |
|
| 505 | 505 | } |
| 506 | 506 | |
| 507 | 507 | /** |
@@ -517,33 +517,33 @@ discard block |
||
| 517 | 517 | * @return int |
| 518 | 518 | */ |
| 519 | 519 | function _sqlite_timestampdiff($unit, $date1, $date2) { |
| 520 | - $d1 = date_create($date1); |
|
| 521 | - $d2 = date_create($date2); |
|
| 522 | - $diff = date_diff($d1, $d2); |
|
| 523 | - $inv = $diff->invert ? -1 : 1; |
|
| 524 | - switch ($unit) { |
|
| 525 | - case 'YEAR': |
|
| 526 | - return $inv * $diff->y; |
|
| 527 | - case 'QUARTER': |
|
| 528 | - return $inv * (4 * $diff->y + intval(floor($diff->m / 3))); |
|
| 529 | - case 'MONTH': |
|
| 530 | - return $inv * (12 * $diff->y + $diff->m); |
|
| 531 | - case 'WEEK': |
|
| 532 | - return $inv * intval(floor($diff->days / 7)); |
|
| 533 | - case 'DAY': |
|
| 534 | - #var_dump($inv*$diff->days); |
|
| 535 | - return $inv * $diff->days; |
|
| 536 | - case 'HOUR': |
|
| 537 | - return $inv * (24 * $diff->days + $diff->h); |
|
| 538 | - case 'MINUTE': |
|
| 539 | - return $inv * ((24 * $diff->days + $diff->h) * 60 + $diff->i); |
|
| 540 | - case 'SECOND': |
|
| 541 | - return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s); |
|
| 542 | - case 'MICROSECOND': |
|
| 543 | - return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s) * 1_000_000; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - return 0; |
|
| 520 | + $d1 = date_create($date1); |
|
| 521 | + $d2 = date_create($date2); |
|
| 522 | + $diff = date_diff($d1, $d2); |
|
| 523 | + $inv = $diff->invert ? -1 : 1; |
|
| 524 | + switch ($unit) { |
|
| 525 | + case 'YEAR': |
|
| 526 | + return $inv * $diff->y; |
|
| 527 | + case 'QUARTER': |
|
| 528 | + return $inv * (4 * $diff->y + intval(floor($diff->m / 3))); |
|
| 529 | + case 'MONTH': |
|
| 530 | + return $inv * (12 * $diff->y + $diff->m); |
|
| 531 | + case 'WEEK': |
|
| 532 | + return $inv * intval(floor($diff->days / 7)); |
|
| 533 | + case 'DAY': |
|
| 534 | + #var_dump($inv*$diff->days); |
|
| 535 | + return $inv * $diff->days; |
|
| 536 | + case 'HOUR': |
|
| 537 | + return $inv * (24 * $diff->days + $diff->h); |
|
| 538 | + case 'MINUTE': |
|
| 539 | + return $inv * ((24 * $diff->days + $diff->h) * 60 + $diff->i); |
|
| 540 | + case 'SECOND': |
|
| 541 | + return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s); |
|
| 542 | + case 'MICROSECOND': |
|
| 543 | + return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s) * 1_000_000; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + return 0; |
|
| 547 | 547 | } |
| 548 | 548 | |
| 549 | 549 | /** |
@@ -553,24 +553,24 @@ discard block |
||
| 553 | 553 | * @return int |
| 554 | 554 | */ |
| 555 | 555 | function _sqlite_func_unix_timestamp($d) { |
| 556 | - static $mem = []; |
|
| 557 | - static $n = 0; |
|
| 558 | - if (isset($mem[$d])) { |
|
| 559 | - return $mem[$d]; |
|
| 560 | - } |
|
| 561 | - if ($n++ > 100) { |
|
| 562 | - $mem = []; |
|
| 563 | - $n = 0; |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - //2005-12-02 20:53:53 |
|
| 567 | - #spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG); |
|
| 568 | - if (!$d) { |
|
| 569 | - return $mem[$d] = time(); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - // une pile plus grosse n'accelere pas le calcul |
|
| 573 | - return $mem[$d] = strtotime($d); |
|
| 556 | + static $mem = []; |
|
| 557 | + static $n = 0; |
|
| 558 | + if (isset($mem[$d])) { |
|
| 559 | + return $mem[$d]; |
|
| 560 | + } |
|
| 561 | + if ($n++ > 100) { |
|
| 562 | + $mem = []; |
|
| 563 | + $n = 0; |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + //2005-12-02 20:53:53 |
|
| 567 | + #spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG); |
|
| 568 | + if (!$d) { |
|
| 569 | + return $mem[$d] = time(); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + // une pile plus grosse n'accelere pas le calcul |
|
| 573 | + return $mem[$d] = strtotime($d); |
|
| 574 | 574 | } |
| 575 | 575 | |
| 576 | 576 | |
@@ -583,7 +583,7 @@ discard block |
||
| 583 | 583 | * @return int |
| 584 | 584 | */ |
| 585 | 585 | function _sqlite_func_year($d) { |
| 586 | - return _sqlite_func_date('Y', $d); |
|
| 586 | + return _sqlite_func_date('Y', $d); |
|
| 587 | 587 | } |
| 588 | 588 | |
| 589 | 589 | /** |
@@ -596,20 +596,20 @@ discard block |
||
| 596 | 596 | * @return int |
| 597 | 597 | */ |
| 598 | 598 | function _sqlite_func_date($quoi, $d) { |
| 599 | - static $mem = []; |
|
| 600 | - static $n = 0; |
|
| 601 | - if (isset($mem[$d])) { |
|
| 602 | - return $mem[$d][$quoi]; |
|
| 603 | - } |
|
| 604 | - if ($n++ > 100) { |
|
| 605 | - $mem = []; |
|
| 606 | - $n = 0; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - $dec = date('Y-m-d', _sqlite_func_unix_timestamp($d)); |
|
| 610 | - $mem[$d] = ['Y' => substr($dec, 0, 4), 'm' => substr($dec, 5, 2), 'd' => substr($dec, 8, 2)]; |
|
| 611 | - |
|
| 612 | - return $mem[$d][$quoi]; |
|
| 599 | + static $mem = []; |
|
| 600 | + static $n = 0; |
|
| 601 | + if (isset($mem[$d])) { |
|
| 602 | + return $mem[$d][$quoi]; |
|
| 603 | + } |
|
| 604 | + if ($n++ > 100) { |
|
| 605 | + $mem = []; |
|
| 606 | + $n = 0; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + $dec = date('Y-m-d', _sqlite_func_unix_timestamp($d)); |
|
| 610 | + $mem[$d] = ['Y' => substr($dec, 0, 4), 'm' => substr($dec, 5, 2), 'd' => substr($dec, 8, 2)]; |
|
| 611 | + |
|
| 612 | + return $mem[$d][$quoi]; |
|
| 613 | 613 | } |
| 614 | 614 | |
| 615 | 615 | /** |
@@ -618,5 +618,5 @@ discard block |
||
| 618 | 618 | * @return void |
| 619 | 619 | */ |
| 620 | 620 | function _sqlite_func_vide() { |
| 621 | - return; |
|
| 621 | + return; |
|
| 622 | 622 | } |
@@ -20,11 +20,11 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | |
| 22 | 22 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 23 | - return; |
|
| 23 | + return; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | if (!defined('_MYSQL_NOPLANES')) { |
| 27 | - define('_MYSQL_NOPLANES', true); |
|
| 27 | + define('_MYSQL_NOPLANES', true); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
@@ -41,112 +41,112 @@ discard block |
||
| 41 | 41 | * - tableau décrivant la connexion sinon |
| 42 | 42 | */ |
| 43 | 43 | function req_mysql_dist($host, $port, $login, $pass, $db = '', $prefixe = '') { |
| 44 | - if (!extension_loaded(\mysqli::class)) { |
|
| 45 | - return false; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - // si port est fourni mais pas host, c'est un socket -> compat avec vieille syntaxe de mysql_connect() et anciens fichiers connect.php |
|
| 49 | - try { |
|
| 50 | - if ( |
|
| 51 | - $port and !is_numeric($socket = $port) |
|
| 52 | - and (!$host or $host === 'localhost') |
|
| 53 | - ) { |
|
| 54 | - $link = @mysqli_connect($host, $login, $pass, '', null, $socket); |
|
| 55 | - } elseif ($port) { |
|
| 56 | - $link = @mysqli_connect($host, $login, $pass, '', $port); |
|
| 57 | - } else { |
|
| 58 | - $link = @mysqli_connect($host, $login, $pass); |
|
| 59 | - } |
|
| 60 | - } catch (\mysqli_sql_exception $e) { |
|
| 61 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 62 | - $link = false; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - if (!$link) { |
|
| 66 | - spip_log('Echec mysqli_connect. Erreur : ' . mysqli_connect_error(), 'mysql.' . _LOG_HS); |
|
| 67 | - |
|
| 68 | - return false; |
|
| 69 | - } |
|
| 70 | - $last = ''; |
|
| 71 | - if (!$db) { |
|
| 72 | - $ok = $link; |
|
| 73 | - $db = 'spip'; |
|
| 74 | - } else { |
|
| 75 | - $ok = mysqli_select_db($link, $db); |
|
| 76 | - if ( |
|
| 77 | - defined('_MYSQL_SET_SQL_MODE') |
|
| 78 | - or defined('_MYSQL_SQL_MODE_TEXT_NOT_NULL') // compatibilite |
|
| 79 | - ) { |
|
| 80 | - mysqli_query($link, $last = "set sql_mode=''"); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - spip_log( |
|
| 85 | - "Connexion MySQLi vers $host, base $db, prefixe $prefixe " . ($ok ? 'operationnelle' : 'impossible'), |
|
| 86 | - _LOG_DEBUG |
|
| 87 | - ); |
|
| 88 | - |
|
| 89 | - return !$ok ? false : [ |
|
| 90 | - 'db' => $db, |
|
| 91 | - 'last' => $last, |
|
| 92 | - 'prefixe' => $prefixe ?: $db, |
|
| 93 | - 'link' => $link, |
|
| 94 | - 'total_requetes' => 0, |
|
| 95 | - ]; |
|
| 44 | + if (!extension_loaded(\mysqli::class)) { |
|
| 45 | + return false; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + // si port est fourni mais pas host, c'est un socket -> compat avec vieille syntaxe de mysql_connect() et anciens fichiers connect.php |
|
| 49 | + try { |
|
| 50 | + if ( |
|
| 51 | + $port and !is_numeric($socket = $port) |
|
| 52 | + and (!$host or $host === 'localhost') |
|
| 53 | + ) { |
|
| 54 | + $link = @mysqli_connect($host, $login, $pass, '', null, $socket); |
|
| 55 | + } elseif ($port) { |
|
| 56 | + $link = @mysqli_connect($host, $login, $pass, '', $port); |
|
| 57 | + } else { |
|
| 58 | + $link = @mysqli_connect($host, $login, $pass); |
|
| 59 | + } |
|
| 60 | + } catch (\mysqli_sql_exception $e) { |
|
| 61 | + spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 62 | + $link = false; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + if (!$link) { |
|
| 66 | + spip_log('Echec mysqli_connect. Erreur : ' . mysqli_connect_error(), 'mysql.' . _LOG_HS); |
|
| 67 | + |
|
| 68 | + return false; |
|
| 69 | + } |
|
| 70 | + $last = ''; |
|
| 71 | + if (!$db) { |
|
| 72 | + $ok = $link; |
|
| 73 | + $db = 'spip'; |
|
| 74 | + } else { |
|
| 75 | + $ok = mysqli_select_db($link, $db); |
|
| 76 | + if ( |
|
| 77 | + defined('_MYSQL_SET_SQL_MODE') |
|
| 78 | + or defined('_MYSQL_SQL_MODE_TEXT_NOT_NULL') // compatibilite |
|
| 79 | + ) { |
|
| 80 | + mysqli_query($link, $last = "set sql_mode=''"); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + spip_log( |
|
| 85 | + "Connexion MySQLi vers $host, base $db, prefixe $prefixe " . ($ok ? 'operationnelle' : 'impossible'), |
|
| 86 | + _LOG_DEBUG |
|
| 87 | + ); |
|
| 88 | + |
|
| 89 | + return !$ok ? false : [ |
|
| 90 | + 'db' => $db, |
|
| 91 | + 'last' => $last, |
|
| 92 | + 'prefixe' => $prefixe ?: $db, |
|
| 93 | + 'link' => $link, |
|
| 94 | + 'total_requetes' => 0, |
|
| 95 | + ]; |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | |
| 99 | 99 | $GLOBALS['spip_mysql_functions_1'] = [ |
| 100 | - 'alter' => 'spip_mysql_alter', |
|
| 101 | - 'count' => 'spip_mysql_count', |
|
| 102 | - 'countsel' => 'spip_mysql_countsel', |
|
| 103 | - 'create' => 'spip_mysql_create', |
|
| 104 | - 'create_base' => 'spip_mysql_create_base', |
|
| 105 | - 'create_view' => 'spip_mysql_create_view', |
|
| 106 | - 'date_proche' => 'spip_mysql_date_proche', |
|
| 107 | - 'delete' => 'spip_mysql_delete', |
|
| 108 | - 'drop_table' => 'spip_mysql_drop_table', |
|
| 109 | - 'drop_view' => 'spip_mysql_drop_view', |
|
| 110 | - 'errno' => 'spip_mysql_errno', |
|
| 111 | - 'error' => 'spip_mysql_error', |
|
| 112 | - 'explain' => 'spip_mysql_explain', |
|
| 113 | - 'fetch' => 'spip_mysql_fetch', |
|
| 114 | - 'seek' => 'spip_mysql_seek', |
|
| 115 | - 'free' => 'spip_mysql_free', |
|
| 116 | - 'hex' => 'spip_mysql_hex', |
|
| 117 | - 'in' => 'spip_mysql_in', |
|
| 118 | - 'insert' => 'spip_mysql_insert', |
|
| 119 | - 'insertq' => 'spip_mysql_insertq', |
|
| 120 | - 'insertq_multi' => 'spip_mysql_insertq_multi', |
|
| 121 | - 'listdbs' => 'spip_mysql_listdbs', |
|
| 122 | - 'multi' => 'spip_mysql_multi', |
|
| 123 | - 'optimize' => 'spip_mysql_optimize', |
|
| 124 | - 'query' => 'spip_mysql_query', |
|
| 125 | - 'quote' => 'spip_mysql_quote', |
|
| 126 | - 'replace' => 'spip_mysql_replace', |
|
| 127 | - 'replace_multi' => 'spip_mysql_replace_multi', |
|
| 128 | - 'repair' => 'spip_mysql_repair', |
|
| 129 | - 'select' => 'spip_mysql_select', |
|
| 130 | - 'selectdb' => 'spip_mysql_selectdb', |
|
| 131 | - 'set_charset' => 'spip_mysql_set_charset', |
|
| 132 | - 'get_charset' => 'spip_mysql_get_charset', |
|
| 133 | - 'showbase' => 'spip_mysql_showbase', |
|
| 134 | - 'showtable' => 'spip_mysql_showtable', |
|
| 135 | - 'table_exists' => 'spip_mysql_table_exists', |
|
| 136 | - 'update' => 'spip_mysql_update', |
|
| 137 | - 'updateq' => 'spip_mysql_updateq', |
|
| 138 | - |
|
| 139 | - // association de chaque nom http d'un charset aux couples MySQL |
|
| 140 | - 'charsets' => [ |
|
| 141 | - 'cp1250' => ['charset' => 'cp1250', 'collation' => 'cp1250_general_ci'], |
|
| 142 | - 'cp1251' => ['charset' => 'cp1251', 'collation' => 'cp1251_general_ci'], |
|
| 143 | - 'cp1256' => ['charset' => 'cp1256', 'collation' => 'cp1256_general_ci'], |
|
| 144 | - 'iso-8859-1' => ['charset' => 'latin1', 'collation' => 'latin1_swedish_ci'], |
|
| 100 | + 'alter' => 'spip_mysql_alter', |
|
| 101 | + 'count' => 'spip_mysql_count', |
|
| 102 | + 'countsel' => 'spip_mysql_countsel', |
|
| 103 | + 'create' => 'spip_mysql_create', |
|
| 104 | + 'create_base' => 'spip_mysql_create_base', |
|
| 105 | + 'create_view' => 'spip_mysql_create_view', |
|
| 106 | + 'date_proche' => 'spip_mysql_date_proche', |
|
| 107 | + 'delete' => 'spip_mysql_delete', |
|
| 108 | + 'drop_table' => 'spip_mysql_drop_table', |
|
| 109 | + 'drop_view' => 'spip_mysql_drop_view', |
|
| 110 | + 'errno' => 'spip_mysql_errno', |
|
| 111 | + 'error' => 'spip_mysql_error', |
|
| 112 | + 'explain' => 'spip_mysql_explain', |
|
| 113 | + 'fetch' => 'spip_mysql_fetch', |
|
| 114 | + 'seek' => 'spip_mysql_seek', |
|
| 115 | + 'free' => 'spip_mysql_free', |
|
| 116 | + 'hex' => 'spip_mysql_hex', |
|
| 117 | + 'in' => 'spip_mysql_in', |
|
| 118 | + 'insert' => 'spip_mysql_insert', |
|
| 119 | + 'insertq' => 'spip_mysql_insertq', |
|
| 120 | + 'insertq_multi' => 'spip_mysql_insertq_multi', |
|
| 121 | + 'listdbs' => 'spip_mysql_listdbs', |
|
| 122 | + 'multi' => 'spip_mysql_multi', |
|
| 123 | + 'optimize' => 'spip_mysql_optimize', |
|
| 124 | + 'query' => 'spip_mysql_query', |
|
| 125 | + 'quote' => 'spip_mysql_quote', |
|
| 126 | + 'replace' => 'spip_mysql_replace', |
|
| 127 | + 'replace_multi' => 'spip_mysql_replace_multi', |
|
| 128 | + 'repair' => 'spip_mysql_repair', |
|
| 129 | + 'select' => 'spip_mysql_select', |
|
| 130 | + 'selectdb' => 'spip_mysql_selectdb', |
|
| 131 | + 'set_charset' => 'spip_mysql_set_charset', |
|
| 132 | + 'get_charset' => 'spip_mysql_get_charset', |
|
| 133 | + 'showbase' => 'spip_mysql_showbase', |
|
| 134 | + 'showtable' => 'spip_mysql_showtable', |
|
| 135 | + 'table_exists' => 'spip_mysql_table_exists', |
|
| 136 | + 'update' => 'spip_mysql_update', |
|
| 137 | + 'updateq' => 'spip_mysql_updateq', |
|
| 138 | + |
|
| 139 | + // association de chaque nom http d'un charset aux couples MySQL |
|
| 140 | + 'charsets' => [ |
|
| 141 | + 'cp1250' => ['charset' => 'cp1250', 'collation' => 'cp1250_general_ci'], |
|
| 142 | + 'cp1251' => ['charset' => 'cp1251', 'collation' => 'cp1251_general_ci'], |
|
| 143 | + 'cp1256' => ['charset' => 'cp1256', 'collation' => 'cp1256_general_ci'], |
|
| 144 | + 'iso-8859-1' => ['charset' => 'latin1', 'collation' => 'latin1_swedish_ci'], |
|
| 145 | 145 | //'iso-8859-6'=>array('charset'=>'latin1','collation'=>'latin1_swedish_ci'), |
| 146 | - 'iso-8859-9' => ['charset' => 'latin5', 'collation' => 'latin5_turkish_ci'], |
|
| 146 | + 'iso-8859-9' => ['charset' => 'latin5', 'collation' => 'latin5_turkish_ci'], |
|
| 147 | 147 | //'iso-8859-15'=>array('charset'=>'latin1','collation'=>'latin1_swedish_ci'), |
| 148 | - 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'] |
|
| 149 | - ] |
|
| 148 | + 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'] |
|
| 149 | + ] |
|
| 150 | 150 | ]; |
| 151 | 151 | |
| 152 | 152 | |
@@ -157,9 +157,9 @@ discard block |
||
| 157 | 157 | * @return Object Information de connexion pour mysqli |
| 158 | 158 | */ |
| 159 | 159 | function _mysql_link($serveur = '') { |
| 160 | - $link = &$GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 160 | + $link = &$GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 161 | 161 | |
| 162 | - return $link; |
|
| 162 | + return $link; |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | |
@@ -172,10 +172,10 @@ discard block |
||
| 172 | 172 | * @return resource Ressource de résultats pour fetch() |
| 173 | 173 | */ |
| 174 | 174 | function spip_mysql_set_charset($charset, $serveur = '', $requeter = true) { |
| 175 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 176 | - spip_log('changement de charset sql : ' . 'SET NAMES ' . _q($charset), _LOG_DEBUG); |
|
| 175 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 176 | + spip_log('changement de charset sql : ' . 'SET NAMES ' . _q($charset), _LOG_DEBUG); |
|
| 177 | 177 | |
| 178 | - return mysqli_query($connexion['link'], $connexion['last'] = 'SET NAMES ' . _q($charset)); |
|
| 178 | + return mysqli_query($connexion['link'], $connexion['last'] = 'SET NAMES ' . _q($charset)); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | |
@@ -188,11 +188,11 @@ discard block |
||
| 188 | 188 | * @return array Description du charset (son nom est dans 'charset') |
| 189 | 189 | */ |
| 190 | 190 | function spip_mysql_get_charset($charset = [], $serveur = '', $requeter = true) { |
| 191 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 192 | - $connexion['last'] = $c = 'SHOW CHARACTER SET' |
|
| 193 | - . (!$charset ? '' : (' LIKE ' . _q($charset['charset']))); |
|
| 191 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 192 | + $connexion['last'] = $c = 'SHOW CHARACTER SET' |
|
| 193 | + . (!$charset ? '' : (' LIKE ' . _q($charset['charset']))); |
|
| 194 | 194 | |
| 195 | - return spip_mysql_fetch(mysqli_query($connexion['link'], $c), null, $serveur); |
|
| 195 | + return spip_mysql_fetch(mysqli_query($connexion['link'], $c), null, $serveur); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -208,72 +208,72 @@ discard block |
||
| 208 | 208 | */ |
| 209 | 209 | function spip_mysql_query($query, $serveur = '', $requeter = true) { |
| 210 | 210 | |
| 211 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 212 | - $prefixe = $connexion['prefixe']; |
|
| 213 | - $link = $connexion['link']; |
|
| 214 | - $db = $connexion['db']; |
|
| 215 | - |
|
| 216 | - $query = _mysql_traite_query($query, $db, $prefixe); |
|
| 217 | - |
|
| 218 | - // renvoyer la requete inerte si demandee |
|
| 219 | - if (!$requeter) { |
|
| 220 | - return $query; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - if (isset($_GET['var_profile']) or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 224 | - include_spip('public/tracer'); |
|
| 225 | - $t = trace_query_start(); |
|
| 226 | - } else { |
|
| 227 | - $t = 0; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $connexion['last'] = $query; |
|
| 231 | - $connexion['total_requetes']++; |
|
| 232 | - |
|
| 233 | - // ajouter un debug utile dans log/mysql-slow.log ? |
|
| 234 | - $debug = ''; |
|
| 235 | - if (defined('_DEBUG_SLOW_QUERIES') and _DEBUG_SLOW_QUERIES) { |
|
| 236 | - if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 237 | - [, $id, , $infos] = $GLOBALS['debug']['aucasou']; |
|
| 238 | - $debug .= "BOUCLE$id @ " . ($infos[0] ?? '') . ' | '; |
|
| 239 | - } |
|
| 240 | - if (isset($_SERVER['REQUEST_URI'])) { |
|
| 241 | - $debug .= $_SERVER['REQUEST_URI']; |
|
| 242 | - } |
|
| 243 | - if (!empty($GLOBALS['ip'])) { |
|
| 244 | - $debug .= ' + ' . $GLOBALS['ip']; |
|
| 245 | - } |
|
| 246 | - $debug = ' /* ' . mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)) . ' */'; |
|
| 247 | - } |
|
| 248 | - try { |
|
| 249 | - $r = mysqli_query($link, $query . $debug); |
|
| 250 | - } catch (\mysqli_sql_exception $e) { |
|
| 251 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 252 | - $r = false; |
|
| 253 | - // Todo: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 254 | - // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - //Eviter de propager le GoneAway sur les autres requetes d'un même processus PHP |
|
| 258 | - if ($e = spip_mysql_errno($serveur)) { // Log d'un Gone Away |
|
| 259 | - if ($e == 2006) { //Si Gone Away on relance une connexion vierge |
|
| 260 | - //Fermer la connexion defaillante |
|
| 261 | - mysqli_close($connexion['link']); |
|
| 262 | - unset($GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]); |
|
| 263 | - //Relancer une connexion vierge |
|
| 264 | - spip_connect($serveur); |
|
| 265 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 266 | - $link = $connexion['link']; |
|
| 267 | - //On retente au cas où |
|
| 268 | - $r = mysqli_query($link, $query . $debug); |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - // Log de l'erreur eventuelle |
|
| 273 | - if ($e = spip_mysql_errno($serveur)) { |
|
| 274 | - $e .= spip_mysql_error($query, $serveur); |
|
| 275 | - } // et du fautif |
|
| 276 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 211 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 212 | + $prefixe = $connexion['prefixe']; |
|
| 213 | + $link = $connexion['link']; |
|
| 214 | + $db = $connexion['db']; |
|
| 215 | + |
|
| 216 | + $query = _mysql_traite_query($query, $db, $prefixe); |
|
| 217 | + |
|
| 218 | + // renvoyer la requete inerte si demandee |
|
| 219 | + if (!$requeter) { |
|
| 220 | + return $query; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + if (isset($_GET['var_profile']) or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 224 | + include_spip('public/tracer'); |
|
| 225 | + $t = trace_query_start(); |
|
| 226 | + } else { |
|
| 227 | + $t = 0; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $connexion['last'] = $query; |
|
| 231 | + $connexion['total_requetes']++; |
|
| 232 | + |
|
| 233 | + // ajouter un debug utile dans log/mysql-slow.log ? |
|
| 234 | + $debug = ''; |
|
| 235 | + if (defined('_DEBUG_SLOW_QUERIES') and _DEBUG_SLOW_QUERIES) { |
|
| 236 | + if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 237 | + [, $id, , $infos] = $GLOBALS['debug']['aucasou']; |
|
| 238 | + $debug .= "BOUCLE$id @ " . ($infos[0] ?? '') . ' | '; |
|
| 239 | + } |
|
| 240 | + if (isset($_SERVER['REQUEST_URI'])) { |
|
| 241 | + $debug .= $_SERVER['REQUEST_URI']; |
|
| 242 | + } |
|
| 243 | + if (!empty($GLOBALS['ip'])) { |
|
| 244 | + $debug .= ' + ' . $GLOBALS['ip']; |
|
| 245 | + } |
|
| 246 | + $debug = ' /* ' . mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)) . ' */'; |
|
| 247 | + } |
|
| 248 | + try { |
|
| 249 | + $r = mysqli_query($link, $query . $debug); |
|
| 250 | + } catch (\mysqli_sql_exception $e) { |
|
| 251 | + spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 252 | + $r = false; |
|
| 253 | + // Todo: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 254 | + // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + //Eviter de propager le GoneAway sur les autres requetes d'un même processus PHP |
|
| 258 | + if ($e = spip_mysql_errno($serveur)) { // Log d'un Gone Away |
|
| 259 | + if ($e == 2006) { //Si Gone Away on relance une connexion vierge |
|
| 260 | + //Fermer la connexion defaillante |
|
| 261 | + mysqli_close($connexion['link']); |
|
| 262 | + unset($GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]); |
|
| 263 | + //Relancer une connexion vierge |
|
| 264 | + spip_connect($serveur); |
|
| 265 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 266 | + $link = $connexion['link']; |
|
| 267 | + //On retente au cas où |
|
| 268 | + $r = mysqli_query($link, $query . $debug); |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + // Log de l'erreur eventuelle |
|
| 273 | + if ($e = spip_mysql_errno($serveur)) { |
|
| 274 | + $e .= spip_mysql_error($query, $serveur); |
|
| 275 | + } // et du fautif |
|
| 276 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | /** |
@@ -288,12 +288,12 @@ discard block |
||
| 288 | 288 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 289 | 289 | */ |
| 290 | 290 | function spip_mysql_alter($query, $serveur = '', $requeter = true) { |
| 291 | - // ici on supprime les ` entourant le nom de table pour permettre |
|
| 292 | - // la transposition du prefixe, compte tenu que les plugins ont la mauvaise habitude |
|
| 293 | - // d'utiliser ceux-ci, copie-colle de phpmyadmin |
|
| 294 | - $query = preg_replace(',^TABLE\s*`([^`]*)`,i', "TABLE \\1", $query); |
|
| 291 | + // ici on supprime les ` entourant le nom de table pour permettre |
|
| 292 | + // la transposition du prefixe, compte tenu que les plugins ont la mauvaise habitude |
|
| 293 | + // d'utiliser ceux-ci, copie-colle de phpmyadmin |
|
| 294 | + $query = preg_replace(',^TABLE\s*`([^`]*)`,i', "TABLE \\1", $query); |
|
| 295 | 295 | |
| 296 | - return spip_mysql_query('ALTER ' . $query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 296 | + return spip_mysql_query('ALTER ' . $query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | |
@@ -306,9 +306,9 @@ discard block |
||
| 306 | 306 | * @return bool Toujours true |
| 307 | 307 | */ |
| 308 | 308 | function spip_mysql_optimize($table, $serveur = '', $requeter = true) { |
| 309 | - spip_mysql_query('OPTIMIZE TABLE ' . $table); |
|
| 309 | + spip_mysql_query('OPTIMIZE TABLE ' . $table); |
|
| 310 | 310 | |
| 311 | - return true; |
|
| 311 | + return true; |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | |
@@ -321,18 +321,18 @@ discard block |
||
| 321 | 321 | * @return array Tableau de l'explication |
| 322 | 322 | */ |
| 323 | 323 | function spip_mysql_explain($query, $serveur = '', $requeter = true) { |
| 324 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 325 | - return []; |
|
| 326 | - } |
|
| 327 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 328 | - $prefixe = $connexion['prefixe']; |
|
| 329 | - $link = $connexion['link']; |
|
| 330 | - $db = $connexion['db']; |
|
| 331 | - |
|
| 332 | - $query = 'EXPLAIN ' . _mysql_traite_query($query, $db, $prefixe); |
|
| 333 | - $r = mysqli_query($link, $query); |
|
| 334 | - |
|
| 335 | - return spip_mysql_fetch($r, null, $serveur); |
|
| 324 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 325 | + return []; |
|
| 326 | + } |
|
| 327 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 328 | + $prefixe = $connexion['prefixe']; |
|
| 329 | + $link = $connexion['link']; |
|
| 330 | + $db = $connexion['db']; |
|
| 331 | + |
|
| 332 | + $query = 'EXPLAIN ' . _mysql_traite_query($query, $db, $prefixe); |
|
| 333 | + $r = mysqli_query($link, $query); |
|
| 334 | + |
|
| 335 | + return spip_mysql_fetch($r, null, $serveur); |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | |
@@ -361,35 +361,35 @@ discard block |
||
| 361 | 361 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 362 | 362 | */ |
| 363 | 363 | function spip_mysql_select( |
| 364 | - $select, |
|
| 365 | - $from, |
|
| 366 | - $where = '', |
|
| 367 | - $groupby = '', |
|
| 368 | - $orderby = '', |
|
| 369 | - $limit = '', |
|
| 370 | - $having = '', |
|
| 371 | - $serveur = '', |
|
| 372 | - $requeter = true |
|
| 364 | + $select, |
|
| 365 | + $from, |
|
| 366 | + $where = '', |
|
| 367 | + $groupby = '', |
|
| 368 | + $orderby = '', |
|
| 369 | + $limit = '', |
|
| 370 | + $having = '', |
|
| 371 | + $serveur = '', |
|
| 372 | + $requeter = true |
|
| 373 | 373 | ) { |
| 374 | 374 | |
| 375 | 375 | |
| 376 | - $from = (!is_array($from) ? $from : spip_mysql_select_as($from)); |
|
| 377 | - $query = |
|
| 378 | - calculer_mysql_expression('SELECT', $select, ', ') |
|
| 379 | - . calculer_mysql_expression('FROM', $from, ', ') |
|
| 380 | - . calculer_mysql_expression('WHERE', $where) |
|
| 381 | - . calculer_mysql_expression('GROUP BY', $groupby, ',') |
|
| 382 | - . calculer_mysql_expression('HAVING', $having) |
|
| 383 | - . ($orderby ? ("\nORDER BY " . spip_mysql_order($orderby)) : '') |
|
| 384 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 376 | + $from = (!is_array($from) ? $from : spip_mysql_select_as($from)); |
|
| 377 | + $query = |
|
| 378 | + calculer_mysql_expression('SELECT', $select, ', ') |
|
| 379 | + . calculer_mysql_expression('FROM', $from, ', ') |
|
| 380 | + . calculer_mysql_expression('WHERE', $where) |
|
| 381 | + . calculer_mysql_expression('GROUP BY', $groupby, ',') |
|
| 382 | + . calculer_mysql_expression('HAVING', $having) |
|
| 383 | + . ($orderby ? ("\nORDER BY " . spip_mysql_order($orderby)) : '') |
|
| 384 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 385 | 385 | |
| 386 | - // renvoyer la requete inerte si demandee |
|
| 387 | - if ($requeter === false) { |
|
| 388 | - return $query; |
|
| 389 | - } |
|
| 390 | - $r = spip_mysql_query($query, $serveur, $requeter); |
|
| 386 | + // renvoyer la requete inerte si demandee |
|
| 387 | + if ($requeter === false) { |
|
| 388 | + return $query; |
|
| 389 | + } |
|
| 390 | + $r = spip_mysql_query($query, $serveur, $requeter); |
|
| 391 | 391 | |
| 392 | - return $r ?: $query; |
|
| 392 | + return $r ?: $query; |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | |
@@ -406,7 +406,7 @@ discard block |
||
| 406 | 406 | * @return string Texte du orderby préparé |
| 407 | 407 | */ |
| 408 | 408 | function spip_mysql_order($orderby) { |
| 409 | - return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 409 | + return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | |
@@ -429,26 +429,26 @@ discard block |
||
| 429 | 429 | * Contrainte pour clause WHERE |
| 430 | 430 | */ |
| 431 | 431 | function calculer_mysql_where($v) { |
| 432 | - if (!is_array($v)) { |
|
| 433 | - return $v; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - $op = array_shift($v); |
|
| 437 | - if (!($n = count($v))) { |
|
| 438 | - return $op; |
|
| 439 | - } else { |
|
| 440 | - $arg = calculer_mysql_where(array_shift($v)); |
|
| 441 | - if ($n == 1) { |
|
| 442 | - return "$op($arg)"; |
|
| 443 | - } else { |
|
| 444 | - $arg2 = calculer_mysql_where(array_shift($v)); |
|
| 445 | - if ($n == 2) { |
|
| 446 | - return "($arg $op $arg2)"; |
|
| 447 | - } else { |
|
| 448 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 449 | - } |
|
| 450 | - } |
|
| 451 | - } |
|
| 432 | + if (!is_array($v)) { |
|
| 433 | + return $v; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + $op = array_shift($v); |
|
| 437 | + if (!($n = count($v))) { |
|
| 438 | + return $op; |
|
| 439 | + } else { |
|
| 440 | + $arg = calculer_mysql_where(array_shift($v)); |
|
| 441 | + if ($n == 1) { |
|
| 442 | + return "$op($arg)"; |
|
| 443 | + } else { |
|
| 444 | + $arg2 = calculer_mysql_where(array_shift($v)); |
|
| 445 | + if ($n == 2) { |
|
| 446 | + return "($arg $op $arg2)"; |
|
| 447 | + } else { |
|
| 448 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 449 | + } |
|
| 450 | + } |
|
| 451 | + } |
|
| 452 | 452 | } |
| 453 | 453 | |
| 454 | 454 | /** |
@@ -463,21 +463,21 @@ discard block |
||
| 463 | 463 | * @return string Texte de l'expression, une partie donc, du texte la requête. |
| 464 | 464 | */ |
| 465 | 465 | function calculer_mysql_expression($expression, $v, $join = 'AND') { |
| 466 | - if (empty($v)) { |
|
| 467 | - return ''; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - $exp = "\n$expression "; |
|
| 471 | - |
|
| 472 | - if (!is_array($v)) { |
|
| 473 | - return $exp . $v; |
|
| 474 | - } else { |
|
| 475 | - if (strtoupper($join) === 'AND') { |
|
| 476 | - return $exp . join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 477 | - } else { |
|
| 478 | - return $exp . join($join, $v); |
|
| 479 | - } |
|
| 480 | - } |
|
| 466 | + if (empty($v)) { |
|
| 467 | + return ''; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + $exp = "\n$expression "; |
|
| 471 | + |
|
| 472 | + if (!is_array($v)) { |
|
| 473 | + return $exp . $v; |
|
| 474 | + } else { |
|
| 475 | + if (strtoupper($join) === 'AND') { |
|
| 476 | + return $exp . join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 477 | + } else { |
|
| 478 | + return $exp . join($join, $v); |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | 481 | } |
| 482 | 482 | |
| 483 | 483 | |
@@ -488,26 +488,26 @@ discard block |
||
| 488 | 488 | * @return string Sélection de colonnes pour une clause SELECT |
| 489 | 489 | */ |
| 490 | 490 | function spip_mysql_select_as($args) { |
| 491 | - $res = ''; |
|
| 492 | - foreach ($args as $k => $v) { |
|
| 493 | - if (substr($k, -1) == '@') { |
|
| 494 | - // c'est une jointure qui se refere au from precedent |
|
| 495 | - // pas de virgule |
|
| 496 | - $res .= ' ' . $v; |
|
| 497 | - } else { |
|
| 498 | - if (!is_numeric($k)) { |
|
| 499 | - $p = strpos($v, ' '); |
|
| 500 | - if ($p) { |
|
| 501 | - $v = substr($v, 0, $p) . " AS `$k`" . substr($v, $p); |
|
| 502 | - } else { |
|
| 503 | - $v .= " AS `$k`"; |
|
| 504 | - } |
|
| 505 | - } |
|
| 506 | - $res .= ', ' . $v; |
|
| 507 | - } |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - return substr($res, 2); |
|
| 491 | + $res = ''; |
|
| 492 | + foreach ($args as $k => $v) { |
|
| 493 | + if (substr($k, -1) == '@') { |
|
| 494 | + // c'est une jointure qui se refere au from precedent |
|
| 495 | + // pas de virgule |
|
| 496 | + $res .= ' ' . $v; |
|
| 497 | + } else { |
|
| 498 | + if (!is_numeric($k)) { |
|
| 499 | + $p = strpos($v, ' '); |
|
| 500 | + if ($p) { |
|
| 501 | + $v = substr($v, 0, $p) . " AS `$k`" . substr($v, $p); |
|
| 502 | + } else { |
|
| 503 | + $v .= " AS `$k`"; |
|
| 504 | + } |
|
| 505 | + } |
|
| 506 | + $res .= ', ' . $v; |
|
| 507 | + } |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + return substr($res, 2); |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | |
@@ -532,58 +532,58 @@ discard block |
||
| 532 | 532 | */ |
| 533 | 533 | function _mysql_traite_query($query, $db = '', $prefixe = '', $echappe_textes = true) { |
| 534 | 534 | |
| 535 | - if ($GLOBALS['mysql_rappel_nom_base'] and $db) { |
|
| 536 | - $pref = '`' . $db . '`.'; |
|
| 537 | - } else { |
|
| 538 | - $pref = ''; |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - if ($prefixe) { |
|
| 542 | - $pref .= $prefixe . '_'; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - if (!preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 546 | - $suite = ''; |
|
| 547 | - } else { |
|
| 548 | - $suite = strstr($query, (string) $regs[0]); |
|
| 549 | - $query = substr($query, 0, -strlen($suite)); |
|
| 550 | - // propager le prefixe en cas de requete imbriquee |
|
| 551 | - // il faut alors echapper les chaine avant de le faire, pour ne pas risquer de |
|
| 552 | - // modifier une requete qui est en fait juste du texte dans un champ |
|
| 553 | - if (stripos($suite, 'SELECT') !== false) { |
|
| 554 | - if ($echappe_textes) { |
|
| 555 | - [$suite_echap, $textes] = query_echappe_textes($suite); |
|
| 556 | - } |
|
| 557 | - else { |
|
| 558 | - $suite_echap = $suite; |
|
| 559 | - } |
|
| 560 | - if (preg_match('/^(.*?)([(]\s*SELECT\b.*)$/si', $suite_echap, $r)) { |
|
| 561 | - $suite_echap = $r[1] . _mysql_traite_query($r[2], $db, $prefixe, false); |
|
| 562 | - if ($echappe_textes) { |
|
| 563 | - $suite = query_reinjecte_textes($suite_echap, $textes); |
|
| 564 | - } |
|
| 565 | - else { |
|
| 566 | - $suite = $suite_echap; |
|
| 567 | - } |
|
| 568 | - } |
|
| 569 | - } |
|
| 570 | - } |
|
| 571 | - $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1' . $pref, $query) . $suite; |
|
| 572 | - |
|
| 573 | - // en option, remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 574 | - // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 575 | - if ( |
|
| 576 | - defined('_MYSQL_NOPLANES') |
|
| 577 | - and _MYSQL_NOPLANES |
|
| 578 | - and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 579 | - and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 580 | - ) { |
|
| 581 | - include_spip('inc/charsets'); |
|
| 582 | - $r = utf8_noplanes($r); |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - #spip_log("_mysql_traite_query: " . substr($r,0, 50) . ".... $db, $prefixe", _LOG_DEBUG); |
|
| 586 | - return $r; |
|
| 535 | + if ($GLOBALS['mysql_rappel_nom_base'] and $db) { |
|
| 536 | + $pref = '`' . $db . '`.'; |
|
| 537 | + } else { |
|
| 538 | + $pref = ''; |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + if ($prefixe) { |
|
| 542 | + $pref .= $prefixe . '_'; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + if (!preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 546 | + $suite = ''; |
|
| 547 | + } else { |
|
| 548 | + $suite = strstr($query, (string) $regs[0]); |
|
| 549 | + $query = substr($query, 0, -strlen($suite)); |
|
| 550 | + // propager le prefixe en cas de requete imbriquee |
|
| 551 | + // il faut alors echapper les chaine avant de le faire, pour ne pas risquer de |
|
| 552 | + // modifier une requete qui est en fait juste du texte dans un champ |
|
| 553 | + if (stripos($suite, 'SELECT') !== false) { |
|
| 554 | + if ($echappe_textes) { |
|
| 555 | + [$suite_echap, $textes] = query_echappe_textes($suite); |
|
| 556 | + } |
|
| 557 | + else { |
|
| 558 | + $suite_echap = $suite; |
|
| 559 | + } |
|
| 560 | + if (preg_match('/^(.*?)([(]\s*SELECT\b.*)$/si', $suite_echap, $r)) { |
|
| 561 | + $suite_echap = $r[1] . _mysql_traite_query($r[2], $db, $prefixe, false); |
|
| 562 | + if ($echappe_textes) { |
|
| 563 | + $suite = query_reinjecte_textes($suite_echap, $textes); |
|
| 564 | + } |
|
| 565 | + else { |
|
| 566 | + $suite = $suite_echap; |
|
| 567 | + } |
|
| 568 | + } |
|
| 569 | + } |
|
| 570 | + } |
|
| 571 | + $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1' . $pref, $query) . $suite; |
|
| 572 | + |
|
| 573 | + // en option, remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 574 | + // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 575 | + if ( |
|
| 576 | + defined('_MYSQL_NOPLANES') |
|
| 577 | + and _MYSQL_NOPLANES |
|
| 578 | + and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 579 | + and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 580 | + ) { |
|
| 581 | + include_spip('inc/charsets'); |
|
| 582 | + $r = utf8_noplanes($r); |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + #spip_log("_mysql_traite_query: " . substr($r,0, 50) . ".... $db, $prefixe", _LOG_DEBUG); |
|
| 586 | + return $r; |
|
| 587 | 587 | } |
| 588 | 588 | |
| 589 | 589 | /** |
@@ -601,13 +601,13 @@ discard block |
||
| 601 | 601 | * - False en cas d'erreur. |
| 602 | 602 | **/ |
| 603 | 603 | function spip_mysql_selectdb($db, $serveur = '', $requeter = true) { |
| 604 | - $link = _mysql_link($serveur); |
|
| 605 | - $ok = mysqli_select_db($link, $db); |
|
| 606 | - if (!$ok) { |
|
| 607 | - spip_log('Echec mysqli_selectdb. Erreur : ' . mysqli_error($link), 'mysql.' . _LOG_CRITIQUE); |
|
| 608 | - } |
|
| 604 | + $link = _mysql_link($serveur); |
|
| 605 | + $ok = mysqli_select_db($link, $db); |
|
| 606 | + if (!$ok) { |
|
| 607 | + spip_log('Echec mysqli_selectdb. Erreur : ' . mysqli_error($link), 'mysql.' . _LOG_CRITIQUE); |
|
| 608 | + } |
|
| 609 | 609 | |
| 610 | - return $ok; |
|
| 610 | + return $ok; |
|
| 611 | 611 | } |
| 612 | 612 | |
| 613 | 613 | |
@@ -628,14 +628,14 @@ discard block |
||
| 628 | 628 | * Liste de noms de bases de données |
| 629 | 629 | **/ |
| 630 | 630 | function spip_mysql_listdbs($serveur = '', $requeter = true) { |
| 631 | - $dbs = []; |
|
| 632 | - if ($res = spip_mysql_query('SHOW DATABASES', $serveur)) { |
|
| 633 | - while ($row = mysqli_fetch_assoc($res)) { |
|
| 634 | - $dbs[] = $row['Database']; |
|
| 635 | - } |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - return $dbs; |
|
| 631 | + $dbs = []; |
|
| 632 | + if ($res = spip_mysql_query('SHOW DATABASES', $serveur)) { |
|
| 633 | + while ($row = mysqli_fetch_assoc($res)) { |
|
| 634 | + $dbs[] = $row['Database']; |
|
| 635 | + } |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + return $dbs; |
|
| 639 | 639 | } |
| 640 | 640 | |
| 641 | 641 | |
@@ -658,73 +658,73 @@ discard block |
||
| 658 | 658 | * - true si la requête réussie, false sinon. |
| 659 | 659 | */ |
| 660 | 660 | function spip_mysql_create( |
| 661 | - $nom, |
|
| 662 | - $champs, |
|
| 663 | - $cles, |
|
| 664 | - $autoinc = false, |
|
| 665 | - $temporary = false, |
|
| 666 | - $serveur = '', |
|
| 667 | - $requeter = true |
|
| 661 | + $nom, |
|
| 662 | + $champs, |
|
| 663 | + $cles, |
|
| 664 | + $autoinc = false, |
|
| 665 | + $temporary = false, |
|
| 666 | + $serveur = '', |
|
| 667 | + $requeter = true |
|
| 668 | 668 | ) { |
| 669 | 669 | |
| 670 | - $query = ''; |
|
| 671 | - $keys = ''; |
|
| 672 | - $s = ''; |
|
| 673 | - $p = ''; |
|
| 674 | - |
|
| 675 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 676 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 677 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 678 | - return; |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - $res = spip_mysql_query('SELECT version() as v', $serveur); |
|
| 682 | - if (($row = mysqli_fetch_array($res)) && (version_compare($row['v'], '5.0', '>='))) { |
|
| 683 | - spip_mysql_query("SET sql_mode=''", $serveur); |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - foreach ($cles as $k => $v) { |
|
| 687 | - $keys .= "$s\n\t\t$k ($v)"; |
|
| 688 | - if ($k == 'PRIMARY KEY') { |
|
| 689 | - $p = $v; |
|
| 690 | - } |
|
| 691 | - $s = ','; |
|
| 692 | - } |
|
| 693 | - $s = ''; |
|
| 694 | - |
|
| 695 | - $character_set = ''; |
|
| 696 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 697 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 698 | - } |
|
| 699 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 700 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 701 | - } |
|
| 702 | - |
|
| 703 | - foreach ($champs as $k => $v) { |
|
| 704 | - $v = _mysql_remplacements_definitions_table($v); |
|
| 705 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 706 | - if ( |
|
| 707 | - preg_match(',(char|text),i', $defs[1]) |
|
| 708 | - and !preg_match(',(binary|CHARACTER|COLLATE),i', $v) |
|
| 709 | - ) { |
|
| 710 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 711 | - } |
|
| 712 | - } |
|
| 713 | - |
|
| 714 | - $query .= "$s\n\t\t$k $v" |
|
| 715 | - . (($autoinc && ($p == $k) && preg_match(',\b(big|small|medium)?int\b,i', $v)) |
|
| 716 | - ? ' auto_increment' |
|
| 717 | - : '' |
|
| 718 | - ); |
|
| 719 | - $s = ','; |
|
| 720 | - } |
|
| 721 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 722 | - $q = "CREATE $temporary TABLE IF NOT EXISTS $nom ($query" . ($keys ? ",$keys" : '') . ')' |
|
| 723 | - . ' ENGINE=MyISAM' |
|
| 724 | - . ($character_set ? " DEFAULT $character_set" : '') |
|
| 725 | - . "\n"; |
|
| 726 | - |
|
| 727 | - return spip_mysql_query($q, $serveur); |
|
| 670 | + $query = ''; |
|
| 671 | + $keys = ''; |
|
| 672 | + $s = ''; |
|
| 673 | + $p = ''; |
|
| 674 | + |
|
| 675 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 676 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 677 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 678 | + return; |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + $res = spip_mysql_query('SELECT version() as v', $serveur); |
|
| 682 | + if (($row = mysqli_fetch_array($res)) && (version_compare($row['v'], '5.0', '>='))) { |
|
| 683 | + spip_mysql_query("SET sql_mode=''", $serveur); |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + foreach ($cles as $k => $v) { |
|
| 687 | + $keys .= "$s\n\t\t$k ($v)"; |
|
| 688 | + if ($k == 'PRIMARY KEY') { |
|
| 689 | + $p = $v; |
|
| 690 | + } |
|
| 691 | + $s = ','; |
|
| 692 | + } |
|
| 693 | + $s = ''; |
|
| 694 | + |
|
| 695 | + $character_set = ''; |
|
| 696 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 697 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 698 | + } |
|
| 699 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 700 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 701 | + } |
|
| 702 | + |
|
| 703 | + foreach ($champs as $k => $v) { |
|
| 704 | + $v = _mysql_remplacements_definitions_table($v); |
|
| 705 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 706 | + if ( |
|
| 707 | + preg_match(',(char|text),i', $defs[1]) |
|
| 708 | + and !preg_match(',(binary|CHARACTER|COLLATE),i', $v) |
|
| 709 | + ) { |
|
| 710 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 711 | + } |
|
| 712 | + } |
|
| 713 | + |
|
| 714 | + $query .= "$s\n\t\t$k $v" |
|
| 715 | + . (($autoinc && ($p == $k) && preg_match(',\b(big|small|medium)?int\b,i', $v)) |
|
| 716 | + ? ' auto_increment' |
|
| 717 | + : '' |
|
| 718 | + ); |
|
| 719 | + $s = ','; |
|
| 720 | + } |
|
| 721 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 722 | + $q = "CREATE $temporary TABLE IF NOT EXISTS $nom ($query" . ($keys ? ",$keys" : '') . ')' |
|
| 723 | + . ' ENGINE=MyISAM' |
|
| 724 | + . ($character_set ? " DEFAULT $character_set" : '') |
|
| 725 | + . "\n"; |
|
| 726 | + |
|
| 727 | + return spip_mysql_query($q, $serveur); |
|
| 728 | 728 | } |
| 729 | 729 | |
| 730 | 730 | |
@@ -737,25 +737,25 @@ discard block |
||
| 737 | 737 | * Définition SQL adaptée pour MySQL d'un champ de table |
| 738 | 738 | */ |
| 739 | 739 | function _mysql_remplacements_definitions_table($query) { |
| 740 | - // quelques remplacements |
|
| 741 | - $num = '(\s*\([0-9]*\))?'; |
|
| 742 | - $enum = '(\s*\([^\)]*\))?'; |
|
| 743 | - |
|
| 744 | - $remplace = [ |
|
| 745 | - '/VARCHAR(\s*[^\s\(])/is' => 'VARCHAR(255)\\1', |
|
| 746 | - '/^TIMESTAMP($| NULL DEFAULT NULL)/is' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', |
|
| 747 | - ]; |
|
| 748 | - |
|
| 749 | - if (is_string($query)) { |
|
| 750 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 751 | - } elseif (is_array($query)) { |
|
| 752 | - $keys = array_keys($remplace); |
|
| 753 | - foreach ($query as $k => $q) { |
|
| 754 | - $query[$k] = preg_replace($keys, $remplace, $q); |
|
| 755 | - } |
|
| 756 | - } |
|
| 757 | - |
|
| 758 | - return $query; |
|
| 740 | + // quelques remplacements |
|
| 741 | + $num = '(\s*\([0-9]*\))?'; |
|
| 742 | + $enum = '(\s*\([^\)]*\))?'; |
|
| 743 | + |
|
| 744 | + $remplace = [ |
|
| 745 | + '/VARCHAR(\s*[^\s\(])/is' => 'VARCHAR(255)\\1', |
|
| 746 | + '/^TIMESTAMP($| NULL DEFAULT NULL)/is' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', |
|
| 747 | + ]; |
|
| 748 | + |
|
| 749 | + if (is_string($query)) { |
|
| 750 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 751 | + } elseif (is_array($query)) { |
|
| 752 | + $keys = array_keys($remplace); |
|
| 753 | + foreach ($query as $k => $q) { |
|
| 754 | + $query[$k] = preg_replace($keys, $remplace, $q); |
|
| 755 | + } |
|
| 756 | + } |
|
| 757 | + |
|
| 758 | + return $query; |
|
| 759 | 759 | } |
| 760 | 760 | |
| 761 | 761 | |
@@ -768,7 +768,7 @@ discard block |
||
| 768 | 768 | * @return bool true si la base est créee. |
| 769 | 769 | **/ |
| 770 | 770 | function spip_mysql_create_base($nom, $serveur = '', $requeter = true) { |
| 771 | - return spip_mysql_query("CREATE DATABASE `$nom`", $serveur, $requeter); |
|
| 771 | + return spip_mysql_query("CREATE DATABASE `$nom`", $serveur, $requeter); |
|
| 772 | 772 | } |
| 773 | 773 | |
| 774 | 774 | |
@@ -789,19 +789,19 @@ discard block |
||
| 789 | 789 | * - string texte de la requête si $requeter vaut false |
| 790 | 790 | */ |
| 791 | 791 | function spip_mysql_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 792 | - if (!$query_select) { |
|
| 793 | - return false; |
|
| 794 | - } |
|
| 795 | - // vue deja presente |
|
| 796 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 797 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", _LOG_ERREUR); |
|
| 792 | + if (!$query_select) { |
|
| 793 | + return false; |
|
| 794 | + } |
|
| 795 | + // vue deja presente |
|
| 796 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 797 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", _LOG_ERREUR); |
|
| 798 | 798 | |
| 799 | - return false; |
|
| 800 | - } |
|
| 799 | + return false; |
|
| 800 | + } |
|
| 801 | 801 | |
| 802 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 802 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 803 | 803 | |
| 804 | - return spip_mysql_query($query, $serveur, $requeter); |
|
| 804 | + return spip_mysql_query($query, $serveur, $requeter); |
|
| 805 | 805 | } |
| 806 | 806 | |
| 807 | 807 | |
@@ -817,11 +817,11 @@ discard block |
||
| 817 | 817 | * - true si la requête a réussie, false sinon |
| 818 | 818 | */ |
| 819 | 819 | function spip_mysql_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 820 | - if ($exist) { |
|
| 821 | - $exist = ' IF EXISTS'; |
|
| 822 | - } |
|
| 820 | + if ($exist) { |
|
| 821 | + $exist = ' IF EXISTS'; |
|
| 822 | + } |
|
| 823 | 823 | |
| 824 | - return spip_mysql_query("DROP TABLE$exist $table", $serveur, $requeter); |
|
| 824 | + return spip_mysql_query("DROP TABLE$exist $table", $serveur, $requeter); |
|
| 825 | 825 | } |
| 826 | 826 | |
| 827 | 827 | /** |
@@ -836,11 +836,11 @@ discard block |
||
| 836 | 836 | * - true si la requête a réussie, false sinon |
| 837 | 837 | */ |
| 838 | 838 | function spip_mysql_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 839 | - if ($exist) { |
|
| 840 | - $exist = ' IF EXISTS'; |
|
| 841 | - } |
|
| 839 | + if ($exist) { |
|
| 840 | + $exist = ' IF EXISTS'; |
|
| 841 | + } |
|
| 842 | 842 | |
| 843 | - return spip_mysql_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 843 | + return spip_mysql_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 844 | 844 | } |
| 845 | 845 | |
| 846 | 846 | /** |
@@ -857,7 +857,7 @@ discard block |
||
| 857 | 857 | * Ressource à utiliser avec sql_fetch() |
| 858 | 858 | **/ |
| 859 | 859 | function spip_mysql_showbase($match, $serveur = '', $requeter = true) { |
| 860 | - return spip_mysql_query('SHOW TABLES LIKE ' . _q($match), $serveur, $requeter); |
|
| 860 | + return spip_mysql_query('SHOW TABLES LIKE ' . _q($match), $serveur, $requeter); |
|
| 861 | 861 | } |
| 862 | 862 | |
| 863 | 863 | /** |
@@ -873,7 +873,7 @@ discard block |
||
| 873 | 873 | * - true si la requête a réussie, false sinon |
| 874 | 874 | */ |
| 875 | 875 | function spip_mysql_repair($table, $serveur = '', $requeter = true) { |
| 876 | - return spip_mysql_query("REPAIR TABLE `$table`", $serveur, $requeter); |
|
| 876 | + return spip_mysql_query("REPAIR TABLE `$table`", $serveur, $requeter); |
|
| 877 | 877 | } |
| 878 | 878 | |
| 879 | 879 | /** |
@@ -890,12 +890,12 @@ discard block |
||
| 890 | 890 | * Ressource à utiliser avec sql_fetch() |
| 891 | 891 | **/ |
| 892 | 892 | function spip_mysql_table_exists(string $table, $serveur = '', $requeter = true) { |
| 893 | - $r = spip_mysql_query('SHOW TABLES LIKE ' . _q($table), $serveur, $requeter); |
|
| 894 | - if (!$requeter) { |
|
| 895 | - return $r; |
|
| 896 | - } |
|
| 897 | - $res = spip_mysql_fetch($r); |
|
| 898 | - return (bool) $res; |
|
| 893 | + $r = spip_mysql_query('SHOW TABLES LIKE ' . _q($table), $serveur, $requeter); |
|
| 894 | + if (!$requeter) { |
|
| 895 | + return $r; |
|
| 896 | + } |
|
| 897 | + $res = spip_mysql_fetch($r); |
|
| 898 | + return (bool) $res; |
|
| 899 | 899 | } |
| 900 | 900 | |
| 901 | 901 | define('_MYSQL_RE_SHOW_TABLE', '/^[^(),]*\(((?:[^()]*\((?:[^()]*\([^()]*\))?[^()]*\)[^()]*)*[^()]*)\)[^()]*$/'); |
@@ -918,86 +918,86 @@ discard block |
||
| 918 | 918 | * - array description de la table sinon |
| 919 | 919 | */ |
| 920 | 920 | function spip_mysql_showtable($nom_table, $serveur = '', $requeter = true) { |
| 921 | - $s = spip_mysql_query("SHOW CREATE TABLE `$nom_table`", $serveur, $requeter); |
|
| 922 | - if (!$s) { |
|
| 923 | - return ''; |
|
| 924 | - } |
|
| 925 | - if (!$requeter) { |
|
| 926 | - return $s; |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - [, $a] = mysqli_fetch_array($s, MYSQLI_NUM); |
|
| 930 | - if (preg_match(_MYSQL_RE_SHOW_TABLE, $a, $r)) { |
|
| 931 | - $desc = $r[1]; |
|
| 932 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 933 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 934 | - if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 935 | - $namedkeys = $r[2]; |
|
| 936 | - $desc = $r[1]; |
|
| 937 | - } else { |
|
| 938 | - $namedkeys = ''; |
|
| 939 | - } |
|
| 940 | - |
|
| 941 | - $fields = []; |
|
| 942 | - foreach (preg_split('/,\s*`/', $desc) as $v) { |
|
| 943 | - preg_match('/^\s*`?([^`]*)`\s*(.*)/', $v, $r); |
|
| 944 | - $fields[strtolower($r[1])] = $r[2]; |
|
| 945 | - } |
|
| 946 | - $keys = []; |
|
| 947 | - |
|
| 948 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 949 | - if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 950 | - $k = str_replace('`', '', trim($r[1])); |
|
| 951 | - $t = strtolower(str_replace('`', '', $r[2])); |
|
| 952 | - if ($k && !isset($keys[$k])) { |
|
| 953 | - $keys[$k] = $t; |
|
| 954 | - } else { |
|
| 955 | - $keys[] = $t; |
|
| 956 | - } |
|
| 957 | - } |
|
| 958 | - } |
|
| 959 | - spip_mysql_free($s); |
|
| 960 | - |
|
| 961 | - return ['field' => $fields, 'key' => $keys]; |
|
| 962 | - } |
|
| 963 | - |
|
| 964 | - $res = spip_mysql_query("SHOW COLUMNS FROM `$nom_table`", $serveur); |
|
| 965 | - if ($res) { |
|
| 966 | - $nfields = []; |
|
| 967 | - $nkeys = []; |
|
| 968 | - while ($val = spip_mysql_fetch($res)) { |
|
| 969 | - $nfields[$val['Field']] = $val['Type']; |
|
| 970 | - if ($val['Null'] == 'NO') { |
|
| 971 | - $nfields[$val['Field']] .= ' NOT NULL'; |
|
| 972 | - } |
|
| 973 | - if ($val['Default'] === '0' || $val['Default']) { |
|
| 974 | - if (preg_match('/[A-Z_]/', $val['Default'])) { |
|
| 975 | - $nfields[$val['Field']] .= ' DEFAULT ' . $val['Default']; |
|
| 976 | - } else { |
|
| 977 | - $nfields[$val['Field']] .= " DEFAULT '" . $val['Default'] . "'"; |
|
| 978 | - } |
|
| 979 | - } |
|
| 980 | - if ($val['Extra']) { |
|
| 981 | - $nfields[$val['Field']] .= ' ' . $val['Extra']; |
|
| 982 | - } |
|
| 983 | - if ($val['Key'] == 'PRI') { |
|
| 984 | - $nkeys['PRIMARY KEY'] = $val['Field']; |
|
| 985 | - } else { |
|
| 986 | - if ($val['Key'] == 'MUL') { |
|
| 987 | - $nkeys['KEY ' . $val['Field']] = $val['Field']; |
|
| 988 | - } else { |
|
| 989 | - if ($val['Key'] == 'UNI') { |
|
| 990 | - $nkeys['UNIQUE KEY ' . $val['Field']] = $val['Field']; |
|
| 991 | - } |
|
| 992 | - } |
|
| 993 | - } |
|
| 994 | - } |
|
| 995 | - spip_mysql_free($res); |
|
| 996 | - |
|
| 997 | - return ['field' => $nfields, 'key' => $nkeys]; |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - return ''; |
|
| 921 | + $s = spip_mysql_query("SHOW CREATE TABLE `$nom_table`", $serveur, $requeter); |
|
| 922 | + if (!$s) { |
|
| 923 | + return ''; |
|
| 924 | + } |
|
| 925 | + if (!$requeter) { |
|
| 926 | + return $s; |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + [, $a] = mysqli_fetch_array($s, MYSQLI_NUM); |
|
| 930 | + if (preg_match(_MYSQL_RE_SHOW_TABLE, $a, $r)) { |
|
| 931 | + $desc = $r[1]; |
|
| 932 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 933 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 934 | + if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 935 | + $namedkeys = $r[2]; |
|
| 936 | + $desc = $r[1]; |
|
| 937 | + } else { |
|
| 938 | + $namedkeys = ''; |
|
| 939 | + } |
|
| 940 | + |
|
| 941 | + $fields = []; |
|
| 942 | + foreach (preg_split('/,\s*`/', $desc) as $v) { |
|
| 943 | + preg_match('/^\s*`?([^`]*)`\s*(.*)/', $v, $r); |
|
| 944 | + $fields[strtolower($r[1])] = $r[2]; |
|
| 945 | + } |
|
| 946 | + $keys = []; |
|
| 947 | + |
|
| 948 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 949 | + if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 950 | + $k = str_replace('`', '', trim($r[1])); |
|
| 951 | + $t = strtolower(str_replace('`', '', $r[2])); |
|
| 952 | + if ($k && !isset($keys[$k])) { |
|
| 953 | + $keys[$k] = $t; |
|
| 954 | + } else { |
|
| 955 | + $keys[] = $t; |
|
| 956 | + } |
|
| 957 | + } |
|
| 958 | + } |
|
| 959 | + spip_mysql_free($s); |
|
| 960 | + |
|
| 961 | + return ['field' => $fields, 'key' => $keys]; |
|
| 962 | + } |
|
| 963 | + |
|
| 964 | + $res = spip_mysql_query("SHOW COLUMNS FROM `$nom_table`", $serveur); |
|
| 965 | + if ($res) { |
|
| 966 | + $nfields = []; |
|
| 967 | + $nkeys = []; |
|
| 968 | + while ($val = spip_mysql_fetch($res)) { |
|
| 969 | + $nfields[$val['Field']] = $val['Type']; |
|
| 970 | + if ($val['Null'] == 'NO') { |
|
| 971 | + $nfields[$val['Field']] .= ' NOT NULL'; |
|
| 972 | + } |
|
| 973 | + if ($val['Default'] === '0' || $val['Default']) { |
|
| 974 | + if (preg_match('/[A-Z_]/', $val['Default'])) { |
|
| 975 | + $nfields[$val['Field']] .= ' DEFAULT ' . $val['Default']; |
|
| 976 | + } else { |
|
| 977 | + $nfields[$val['Field']] .= " DEFAULT '" . $val['Default'] . "'"; |
|
| 978 | + } |
|
| 979 | + } |
|
| 980 | + if ($val['Extra']) { |
|
| 981 | + $nfields[$val['Field']] .= ' ' . $val['Extra']; |
|
| 982 | + } |
|
| 983 | + if ($val['Key'] == 'PRI') { |
|
| 984 | + $nkeys['PRIMARY KEY'] = $val['Field']; |
|
| 985 | + } else { |
|
| 986 | + if ($val['Key'] == 'MUL') { |
|
| 987 | + $nkeys['KEY ' . $val['Field']] = $val['Field']; |
|
| 988 | + } else { |
|
| 989 | + if ($val['Key'] == 'UNI') { |
|
| 990 | + $nkeys['UNIQUE KEY ' . $val['Field']] = $val['Field']; |
|
| 991 | + } |
|
| 992 | + } |
|
| 993 | + } |
|
| 994 | + } |
|
| 995 | + spip_mysql_free($res); |
|
| 996 | + |
|
| 997 | + return ['field' => $nfields, 'key' => $nkeys]; |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + return ''; |
|
| 1001 | 1001 | } |
| 1002 | 1002 | |
| 1003 | 1003 | |
@@ -1013,12 +1013,12 @@ discard block |
||
| 1013 | 1013 | * @return array Ligne de résultat |
| 1014 | 1014 | */ |
| 1015 | 1015 | function spip_mysql_fetch($r, $t = '', $serveur = '', $requeter = true) { |
| 1016 | - if (!$t) { |
|
| 1017 | - $t = MYSQLI_ASSOC; |
|
| 1018 | - } |
|
| 1019 | - if ($r) { |
|
| 1020 | - return mysqli_fetch_array($r, $t); |
|
| 1021 | - } |
|
| 1016 | + if (!$t) { |
|
| 1017 | + $t = MYSQLI_ASSOC; |
|
| 1018 | + } |
|
| 1019 | + if ($r) { |
|
| 1020 | + return mysqli_fetch_array($r, $t); |
|
| 1021 | + } |
|
| 1022 | 1022 | } |
| 1023 | 1023 | |
| 1024 | 1024 | /** |
@@ -1031,9 +1031,9 @@ discard block |
||
| 1031 | 1031 | * @return bool True si déplacement réussi, false sinon. |
| 1032 | 1032 | **/ |
| 1033 | 1033 | function spip_mysql_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 1034 | - if ($r and mysqli_num_rows($r)) { |
|
| 1035 | - return mysqli_data_seek($r, $row_number); |
|
| 1036 | - } |
|
| 1034 | + if ($r and mysqli_num_rows($r)) { |
|
| 1035 | + return mysqli_data_seek($r, $row_number); |
|
| 1036 | + } |
|
| 1037 | 1037 | } |
| 1038 | 1038 | |
| 1039 | 1039 | |
@@ -1051,26 +1051,26 @@ discard block |
||
| 1051 | 1051 | * - int Nombre de lignes (0 si la requête n'a pas réussie) |
| 1052 | 1052 | **/ |
| 1053 | 1053 | function spip_mysql_countsel( |
| 1054 | - $from = [], |
|
| 1055 | - $where = [], |
|
| 1056 | - $groupby = '', |
|
| 1057 | - $having = [], |
|
| 1058 | - $serveur = '', |
|
| 1059 | - $requeter = true |
|
| 1054 | + $from = [], |
|
| 1055 | + $where = [], |
|
| 1056 | + $groupby = '', |
|
| 1057 | + $having = [], |
|
| 1058 | + $serveur = '', |
|
| 1059 | + $requeter = true |
|
| 1060 | 1060 | ) { |
| 1061 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 1062 | - |
|
| 1063 | - $r = spip_mysql_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 1064 | - if (!$requeter) { |
|
| 1065 | - return $r; |
|
| 1066 | - } |
|
| 1067 | - if (!$r instanceof mysqli_result) { |
|
| 1068 | - return 0; |
|
| 1069 | - } |
|
| 1070 | - [$c] = mysqli_fetch_array($r, MYSQLI_NUM); |
|
| 1071 | - mysqli_free_result($r); |
|
| 1072 | - |
|
| 1073 | - return $c; |
|
| 1061 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 1062 | + |
|
| 1063 | + $r = spip_mysql_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 1064 | + if (!$requeter) { |
|
| 1065 | + return $r; |
|
| 1066 | + } |
|
| 1067 | + if (!$r instanceof mysqli_result) { |
|
| 1068 | + return 0; |
|
| 1069 | + } |
|
| 1070 | + [$c] = mysqli_fetch_array($r, MYSQLI_NUM); |
|
| 1071 | + mysqli_free_result($r); |
|
| 1072 | + |
|
| 1073 | + return $c; |
|
| 1074 | 1074 | } |
| 1075 | 1075 | |
| 1076 | 1076 | |
@@ -1093,16 +1093,16 @@ discard block |
||
| 1093 | 1093 | * Erreur eventuelle |
| 1094 | 1094 | **/ |
| 1095 | 1095 | function spip_mysql_error($query = '', $serveur = '', $requeter = true) { |
| 1096 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1097 | - $s = mysqli_error($link); |
|
| 1098 | - if ($s) { |
|
| 1099 | - $trace = debug_backtrace(); |
|
| 1100 | - if ($trace[0]['function'] != 'spip_mysql_error') { |
|
| 1101 | - spip_log("$s - $query - " . sql_error_backtrace(), 'mysql.' . _LOG_ERREUR); |
|
| 1102 | - } |
|
| 1103 | - } |
|
| 1104 | - |
|
| 1105 | - return $s; |
|
| 1096 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1097 | + $s = mysqli_error($link); |
|
| 1098 | + if ($s) { |
|
| 1099 | + $trace = debug_backtrace(); |
|
| 1100 | + if ($trace[0]['function'] != 'spip_mysql_error') { |
|
| 1101 | + spip_log("$s - $query - " . sql_error_backtrace(), 'mysql.' . _LOG_ERREUR); |
|
| 1102 | + } |
|
| 1103 | + } |
|
| 1104 | + |
|
| 1105 | + return $s; |
|
| 1106 | 1106 | } |
| 1107 | 1107 | |
| 1108 | 1108 | |
@@ -1117,18 +1117,18 @@ discard block |
||
| 1117 | 1117 | * 0, pas d'erreur. Autre, numéro de l'erreur. |
| 1118 | 1118 | **/ |
| 1119 | 1119 | function spip_mysql_errno($serveur = '', $requeter = true) { |
| 1120 | - $link = $GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 1121 | - $s = mysqli_errno($link); |
|
| 1122 | - // 2006 MySQL server has gone away |
|
| 1123 | - // 2013 Lost connection to MySQL server during query |
|
| 1124 | - if (in_array($s, [2006, 2013])) { |
|
| 1125 | - define('spip_interdire_cache', true); |
|
| 1126 | - } |
|
| 1127 | - if ($s) { |
|
| 1128 | - spip_log("Erreur mysql $s", _LOG_ERREUR); |
|
| 1129 | - } |
|
| 1130 | - |
|
| 1131 | - return $s; |
|
| 1120 | + $link = $GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 1121 | + $s = mysqli_errno($link); |
|
| 1122 | + // 2006 MySQL server has gone away |
|
| 1123 | + // 2013 Lost connection to MySQL server during query |
|
| 1124 | + if (in_array($s, [2006, 2013])) { |
|
| 1125 | + define('spip_interdire_cache', true); |
|
| 1126 | + } |
|
| 1127 | + if ($s) { |
|
| 1128 | + spip_log("Erreur mysql $s", _LOG_ERREUR); |
|
| 1129 | + } |
|
| 1130 | + |
|
| 1131 | + return $s; |
|
| 1132 | 1132 | } |
| 1133 | 1133 | |
| 1134 | 1134 | |
@@ -1142,9 +1142,9 @@ discard block |
||
| 1142 | 1142 | * @return int Nombre de lignes |
| 1143 | 1143 | */ |
| 1144 | 1144 | function spip_mysql_count($r, $serveur = '', $requeter = true) { |
| 1145 | - if ($r) { |
|
| 1146 | - return mysqli_num_rows($r); |
|
| 1147 | - } |
|
| 1145 | + if ($r) { |
|
| 1146 | + return mysqli_num_rows($r); |
|
| 1147 | + } |
|
| 1148 | 1148 | } |
| 1149 | 1149 | |
| 1150 | 1150 | |
@@ -1160,7 +1160,7 @@ discard block |
||
| 1160 | 1160 | * @return bool True si réussi |
| 1161 | 1161 | */ |
| 1162 | 1162 | function spip_mysql_free($r, $serveur = '', $requeter = true) { |
| 1163 | - return (($r instanceof mysqli_result) ? mysqli_free_result($r) : false); |
|
| 1163 | + return (($r instanceof mysqli_result) ? mysqli_free_result($r) : false); |
|
| 1164 | 1164 | } |
| 1165 | 1165 | |
| 1166 | 1166 | |
@@ -1188,50 +1188,50 @@ discard block |
||
| 1188 | 1188 | **/ |
| 1189 | 1189 | function spip_mysql_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 1190 | 1190 | |
| 1191 | - $e = null; |
|
| 1192 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1193 | - $link = $connexion['link']; |
|
| 1194 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1195 | - |
|
| 1196 | - // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 1197 | - if ( |
|
| 1198 | - defined('_MYSQL_NOPLANES') |
|
| 1199 | - and _MYSQL_NOPLANES |
|
| 1200 | - and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 1201 | - and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 1202 | - ) { |
|
| 1203 | - include_spip('inc/charsets'); |
|
| 1204 | - $valeurs = utf8_noplanes($valeurs); |
|
| 1205 | - } |
|
| 1206 | - |
|
| 1207 | - $query = "INSERT INTO $table $champs VALUES $valeurs"; |
|
| 1208 | - if (!$requeter) { |
|
| 1209 | - return $query; |
|
| 1210 | - } |
|
| 1211 | - |
|
| 1212 | - if (isset($_GET['var_profile'])) { |
|
| 1213 | - include_spip('public/tracer'); |
|
| 1214 | - $t = trace_query_start(); |
|
| 1215 | - $e = ''; |
|
| 1216 | - } else { |
|
| 1217 | - $t = 0; |
|
| 1218 | - } |
|
| 1219 | - |
|
| 1220 | - $connexion['last'] = $query; |
|
| 1221 | - #spip_log($query, 'mysql.'._LOG_DEBUG); |
|
| 1222 | - $r = false; |
|
| 1223 | - if (mysqli_query($link, $query)) { |
|
| 1224 | - $r = mysqli_insert_id($link); |
|
| 1225 | - } else { |
|
| 1226 | - // Log de l'erreur eventuelle |
|
| 1227 | - if ($e = spip_mysql_errno($serveur)) { |
|
| 1228 | - $e .= spip_mysql_error($query, $serveur); |
|
| 1229 | - } // et du fautif |
|
| 1230 | - } |
|
| 1231 | - |
|
| 1232 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 1233 | - |
|
| 1234 | - // return $r ? $r : (($r===0) ? -1 : 0); pb avec le multi-base. |
|
| 1191 | + $e = null; |
|
| 1192 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1193 | + $link = $connexion['link']; |
|
| 1194 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1195 | + |
|
| 1196 | + // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 1197 | + if ( |
|
| 1198 | + defined('_MYSQL_NOPLANES') |
|
| 1199 | + and _MYSQL_NOPLANES |
|
| 1200 | + and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 1201 | + and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 1202 | + ) { |
|
| 1203 | + include_spip('inc/charsets'); |
|
| 1204 | + $valeurs = utf8_noplanes($valeurs); |
|
| 1205 | + } |
|
| 1206 | + |
|
| 1207 | + $query = "INSERT INTO $table $champs VALUES $valeurs"; |
|
| 1208 | + if (!$requeter) { |
|
| 1209 | + return $query; |
|
| 1210 | + } |
|
| 1211 | + |
|
| 1212 | + if (isset($_GET['var_profile'])) { |
|
| 1213 | + include_spip('public/tracer'); |
|
| 1214 | + $t = trace_query_start(); |
|
| 1215 | + $e = ''; |
|
| 1216 | + } else { |
|
| 1217 | + $t = 0; |
|
| 1218 | + } |
|
| 1219 | + |
|
| 1220 | + $connexion['last'] = $query; |
|
| 1221 | + #spip_log($query, 'mysql.'._LOG_DEBUG); |
|
| 1222 | + $r = false; |
|
| 1223 | + if (mysqli_query($link, $query)) { |
|
| 1224 | + $r = mysqli_insert_id($link); |
|
| 1225 | + } else { |
|
| 1226 | + // Log de l'erreur eventuelle |
|
| 1227 | + if ($e = spip_mysql_errno($serveur)) { |
|
| 1228 | + $e .= spip_mysql_error($query, $serveur); |
|
| 1229 | + } // et du fautif |
|
| 1230 | + } |
|
| 1231 | + |
|
| 1232 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 1233 | + |
|
| 1234 | + // return $r ? $r : (($r===0) ? -1 : 0); pb avec le multi-base. |
|
| 1235 | 1235 | } |
| 1236 | 1236 | |
| 1237 | 1237 | /** |
@@ -1256,26 +1256,26 @@ discard block |
||
| 1256 | 1256 | **/ |
| 1257 | 1257 | function spip_mysql_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1258 | 1258 | |
| 1259 | - if (!$desc) { |
|
| 1260 | - $desc = description_table($table, $serveur); |
|
| 1261 | - } |
|
| 1262 | - if (!$desc) { |
|
| 1263 | - $couples = []; |
|
| 1264 | - } |
|
| 1265 | - $fields = $desc['field'] ?? []; |
|
| 1266 | - |
|
| 1267 | - foreach ($couples as $champ => $val) { |
|
| 1268 | - $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1269 | - } |
|
| 1270 | - |
|
| 1271 | - return spip_mysql_insert( |
|
| 1272 | - $table, |
|
| 1273 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 1274 | - '(' . join(',', $couples) . ')', |
|
| 1275 | - $desc, |
|
| 1276 | - $serveur, |
|
| 1277 | - $requeter |
|
| 1278 | - ); |
|
| 1259 | + if (!$desc) { |
|
| 1260 | + $desc = description_table($table, $serveur); |
|
| 1261 | + } |
|
| 1262 | + if (!$desc) { |
|
| 1263 | + $couples = []; |
|
| 1264 | + } |
|
| 1265 | + $fields = $desc['field'] ?? []; |
|
| 1266 | + |
|
| 1267 | + foreach ($couples as $champ => $val) { |
|
| 1268 | + $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1269 | + } |
|
| 1270 | + |
|
| 1271 | + return spip_mysql_insert( |
|
| 1272 | + $table, |
|
| 1273 | + '(' . join(',', array_keys($couples)) . ')', |
|
| 1274 | + '(' . join(',', $couples) . ')', |
|
| 1275 | + $desc, |
|
| 1276 | + $serveur, |
|
| 1277 | + $requeter |
|
| 1278 | + ); |
|
| 1279 | 1279 | } |
| 1280 | 1280 | |
| 1281 | 1281 | |
@@ -1300,34 +1300,34 @@ discard block |
||
| 1300 | 1300 | **/ |
| 1301 | 1301 | function spip_mysql_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1302 | 1302 | |
| 1303 | - if (!$desc) { |
|
| 1304 | - $desc = description_table($table, $serveur); |
|
| 1305 | - } |
|
| 1306 | - if (!$desc) { |
|
| 1307 | - $tab_couples = []; |
|
| 1308 | - } |
|
| 1309 | - $fields = $desc['field'] ?? []; |
|
| 1310 | - |
|
| 1311 | - $cles = '(' . join(',', array_keys(reset($tab_couples))) . ')'; |
|
| 1312 | - $valeurs = []; |
|
| 1313 | - $r = false; |
|
| 1314 | - |
|
| 1315 | - // Quoter et Inserer par groupes de 100 max pour eviter un debordement de pile |
|
| 1316 | - foreach ($tab_couples as $couples) { |
|
| 1317 | - foreach ($couples as $champ => $val) { |
|
| 1318 | - $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1319 | - } |
|
| 1320 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1321 | - if (count($valeurs) >= 100) { |
|
| 1322 | - $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1323 | - $valeurs = []; |
|
| 1324 | - } |
|
| 1325 | - } |
|
| 1326 | - if (count($valeurs)) { |
|
| 1327 | - $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1328 | - } |
|
| 1329 | - |
|
| 1330 | - return $r; // dans le cas d'une table auto_increment, le dernier insert_id |
|
| 1303 | + if (!$desc) { |
|
| 1304 | + $desc = description_table($table, $serveur); |
|
| 1305 | + } |
|
| 1306 | + if (!$desc) { |
|
| 1307 | + $tab_couples = []; |
|
| 1308 | + } |
|
| 1309 | + $fields = $desc['field'] ?? []; |
|
| 1310 | + |
|
| 1311 | + $cles = '(' . join(',', array_keys(reset($tab_couples))) . ')'; |
|
| 1312 | + $valeurs = []; |
|
| 1313 | + $r = false; |
|
| 1314 | + |
|
| 1315 | + // Quoter et Inserer par groupes de 100 max pour eviter un debordement de pile |
|
| 1316 | + foreach ($tab_couples as $couples) { |
|
| 1317 | + foreach ($couples as $champ => $val) { |
|
| 1318 | + $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1319 | + } |
|
| 1320 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1321 | + if (count($valeurs) >= 100) { |
|
| 1322 | + $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1323 | + $valeurs = []; |
|
| 1324 | + } |
|
| 1325 | + } |
|
| 1326 | + if (count($valeurs)) { |
|
| 1327 | + $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1328 | + } |
|
| 1329 | + |
|
| 1330 | + return $r; // dans le cas d'une table auto_increment, le dernier insert_id |
|
| 1331 | 1331 | } |
| 1332 | 1332 | |
| 1333 | 1333 | /** |
@@ -1352,19 +1352,19 @@ discard block |
||
| 1352 | 1352 | * - array Tableau décrivant la requête et son temps d'exécution si var_profile est actif |
| 1353 | 1353 | */ |
| 1354 | 1354 | function spip_mysql_update($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1355 | - $set = []; |
|
| 1356 | - foreach ($champs as $champ => $val) { |
|
| 1357 | - $set[] = $champ . "=$val"; |
|
| 1358 | - } |
|
| 1359 | - if (!empty($set)) { |
|
| 1360 | - return spip_mysql_query( |
|
| 1361 | - calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1362 | - . calculer_mysql_expression('SET', $set, ',') |
|
| 1363 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1364 | - $serveur, |
|
| 1365 | - $requeter |
|
| 1366 | - ); |
|
| 1367 | - } |
|
| 1355 | + $set = []; |
|
| 1356 | + foreach ($champs as $champ => $val) { |
|
| 1357 | + $set[] = $champ . "=$val"; |
|
| 1358 | + } |
|
| 1359 | + if (!empty($set)) { |
|
| 1360 | + return spip_mysql_query( |
|
| 1361 | + calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1362 | + . calculer_mysql_expression('SET', $set, ',') |
|
| 1363 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1364 | + $serveur, |
|
| 1365 | + $requeter |
|
| 1366 | + ); |
|
| 1367 | + } |
|
| 1368 | 1368 | } |
| 1369 | 1369 | |
| 1370 | 1370 | /** |
@@ -1397,29 +1397,29 @@ discard block |
||
| 1397 | 1397 | */ |
| 1398 | 1398 | function spip_mysql_updateq($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1399 | 1399 | |
| 1400 | - if (!$champs) { |
|
| 1401 | - return; |
|
| 1402 | - } |
|
| 1403 | - if (!$desc) { |
|
| 1404 | - $desc = description_table($table, $serveur); |
|
| 1405 | - } |
|
| 1406 | - if (!$desc) { |
|
| 1407 | - $champs = []; |
|
| 1408 | - } else { |
|
| 1409 | - $fields = $desc['field']; |
|
| 1410 | - } |
|
| 1411 | - $set = []; |
|
| 1412 | - foreach ($champs as $champ => $val) { |
|
| 1413 | - $set[] = $champ . '=' . spip_mysql_cite($val, @$fields[$champ]); |
|
| 1414 | - } |
|
| 1415 | - |
|
| 1416 | - return spip_mysql_query( |
|
| 1417 | - calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1418 | - . calculer_mysql_expression('SET', $set, ',') |
|
| 1419 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1420 | - $serveur, |
|
| 1421 | - $requeter |
|
| 1422 | - ); |
|
| 1400 | + if (!$champs) { |
|
| 1401 | + return; |
|
| 1402 | + } |
|
| 1403 | + if (!$desc) { |
|
| 1404 | + $desc = description_table($table, $serveur); |
|
| 1405 | + } |
|
| 1406 | + if (!$desc) { |
|
| 1407 | + $champs = []; |
|
| 1408 | + } else { |
|
| 1409 | + $fields = $desc['field']; |
|
| 1410 | + } |
|
| 1411 | + $set = []; |
|
| 1412 | + foreach ($champs as $champ => $val) { |
|
| 1413 | + $set[] = $champ . '=' . spip_mysql_cite($val, @$fields[$champ]); |
|
| 1414 | + } |
|
| 1415 | + |
|
| 1416 | + return spip_mysql_query( |
|
| 1417 | + calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1418 | + . calculer_mysql_expression('SET', $set, ',') |
|
| 1419 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1420 | + $serveur, |
|
| 1421 | + $requeter |
|
| 1422 | + ); |
|
| 1423 | 1423 | } |
| 1424 | 1424 | |
| 1425 | 1425 | /** |
@@ -1435,22 +1435,22 @@ discard block |
||
| 1435 | 1435 | * - False en cas d'erreur. |
| 1436 | 1436 | **/ |
| 1437 | 1437 | function spip_mysql_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 1438 | - $res = spip_mysql_query( |
|
| 1439 | - calculer_mysql_expression('DELETE FROM', $table, ',') |
|
| 1440 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1441 | - $serveur, |
|
| 1442 | - $requeter |
|
| 1443 | - ); |
|
| 1444 | - if (!$requeter) { |
|
| 1445 | - return $res; |
|
| 1446 | - } |
|
| 1447 | - if ($res) { |
|
| 1448 | - $link = _mysql_link($serveur); |
|
| 1449 | - |
|
| 1450 | - return mysqli_affected_rows($link); |
|
| 1451 | - } else { |
|
| 1452 | - return false; |
|
| 1453 | - } |
|
| 1438 | + $res = spip_mysql_query( |
|
| 1439 | + calculer_mysql_expression('DELETE FROM', $table, ',') |
|
| 1440 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1441 | + $serveur, |
|
| 1442 | + $requeter |
|
| 1443 | + ); |
|
| 1444 | + if (!$requeter) { |
|
| 1445 | + return $res; |
|
| 1446 | + } |
|
| 1447 | + if ($res) { |
|
| 1448 | + $link = _mysql_link($serveur); |
|
| 1449 | + |
|
| 1450 | + return mysqli_affected_rows($link); |
|
| 1451 | + } else { |
|
| 1452 | + return false; |
|
| 1453 | + } |
|
| 1454 | 1454 | } |
| 1455 | 1455 | |
| 1456 | 1456 | |
@@ -1479,10 +1479,10 @@ discard block |
||
| 1479 | 1479 | * - False en cas d'erreur. |
| 1480 | 1480 | **/ |
| 1481 | 1481 | function spip_mysql_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) { |
| 1482 | - return spip_mysql_query("REPLACE $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1483 | - ',', |
|
| 1484 | - array_map('_q', $couples) |
|
| 1485 | - ) . ')', $serveur, $requeter); |
|
| 1482 | + return spip_mysql_query("REPLACE $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1483 | + ',', |
|
| 1484 | + array_map('_q', $couples) |
|
| 1485 | + ) . ')', $serveur, $requeter); |
|
| 1486 | 1486 | } |
| 1487 | 1487 | |
| 1488 | 1488 | |
@@ -1511,14 +1511,14 @@ discard block |
||
| 1511 | 1511 | * - False en cas d'erreur. |
| 1512 | 1512 | **/ |
| 1513 | 1513 | function spip_mysql_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1514 | - $cles = '(' . join(',', array_keys($tab_couples[0])) . ')'; |
|
| 1515 | - $valeurs = []; |
|
| 1516 | - foreach ($tab_couples as $couples) { |
|
| 1517 | - $valeurs[] = '(' . join(',', array_map('_q', $couples)) . ')'; |
|
| 1518 | - } |
|
| 1519 | - $valeurs = implode(', ', $valeurs); |
|
| 1520 | - |
|
| 1521 | - return spip_mysql_query("REPLACE $table $cles VALUES $valeurs", $serveur, $requeter); |
|
| 1514 | + $cles = '(' . join(',', array_keys($tab_couples[0])) . ')'; |
|
| 1515 | + $valeurs = []; |
|
| 1516 | + foreach ($tab_couples as $couples) { |
|
| 1517 | + $valeurs[] = '(' . join(',', array_map('_q', $couples)) . ')'; |
|
| 1518 | + } |
|
| 1519 | + $valeurs = implode(', ', $valeurs); |
|
| 1520 | + |
|
| 1521 | + return spip_mysql_query("REPLACE $table $cles VALUES $valeurs", $serveur, $requeter); |
|
| 1522 | 1522 | } |
| 1523 | 1523 | |
| 1524 | 1524 | |
@@ -1533,32 +1533,32 @@ discard block |
||
| 1533 | 1533 | * @return string Texte de sélection pour la requête |
| 1534 | 1534 | */ |
| 1535 | 1535 | function spip_mysql_multi($objet, $lang) { |
| 1536 | - $lengthlang = strlen("[$lang]"); |
|
| 1537 | - $posmulti = 'INSTR(' . $objet . ", '<multi>')"; |
|
| 1538 | - $posfinmulti = 'INSTR(' . $objet . ", '</multi>')"; |
|
| 1539 | - $debutchaine = 'LEFT(' . $objet . ", $posmulti-1)"; |
|
| 1540 | - $finchaine = 'RIGHT(' . $objet . ', CHAR_LENGTH(' . $objet . ") -(7+$posfinmulti))"; |
|
| 1541 | - $chainemulti = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1542 | - $poslang = "INSTR($chainemulti,'[" . $lang . "]')"; |
|
| 1543 | - $poslang = "IF($poslang=0,INSTR($chainemulti,']')+1,$poslang+$lengthlang)"; |
|
| 1544 | - $chainelang = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1545 | - $posfinlang = 'INSTR(' . $chainelang . ", '[')"; |
|
| 1546 | - $chainelang = "IF($posfinlang>0,LEFT($chainelang,$posfinlang-1),$chainelang)"; |
|
| 1547 | - //$chainelang = "LEFT($chainelang,$posfinlang-1)"; |
|
| 1548 | - $retour = "(TRIM(IF($posmulti = 0 , " . |
|
| 1549 | - ' TRIM(' . $objet . '), ' . |
|
| 1550 | - ' CONCAT( ' . |
|
| 1551 | - " $debutchaine, " . |
|
| 1552 | - ' IF( ' . |
|
| 1553 | - " $poslang = 0, " . |
|
| 1554 | - " $chainemulti, " . |
|
| 1555 | - " $chainelang" . |
|
| 1556 | - ' ), ' . |
|
| 1557 | - " $finchaine" . |
|
| 1558 | - ' ) ' . |
|
| 1559 | - '))) AS multi'; |
|
| 1560 | - |
|
| 1561 | - return $retour; |
|
| 1536 | + $lengthlang = strlen("[$lang]"); |
|
| 1537 | + $posmulti = 'INSTR(' . $objet . ", '<multi>')"; |
|
| 1538 | + $posfinmulti = 'INSTR(' . $objet . ", '</multi>')"; |
|
| 1539 | + $debutchaine = 'LEFT(' . $objet . ", $posmulti-1)"; |
|
| 1540 | + $finchaine = 'RIGHT(' . $objet . ', CHAR_LENGTH(' . $objet . ") -(7+$posfinmulti))"; |
|
| 1541 | + $chainemulti = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1542 | + $poslang = "INSTR($chainemulti,'[" . $lang . "]')"; |
|
| 1543 | + $poslang = "IF($poslang=0,INSTR($chainemulti,']')+1,$poslang+$lengthlang)"; |
|
| 1544 | + $chainelang = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1545 | + $posfinlang = 'INSTR(' . $chainelang . ", '[')"; |
|
| 1546 | + $chainelang = "IF($posfinlang>0,LEFT($chainelang,$posfinlang-1),$chainelang)"; |
|
| 1547 | + //$chainelang = "LEFT($chainelang,$posfinlang-1)"; |
|
| 1548 | + $retour = "(TRIM(IF($posmulti = 0 , " . |
|
| 1549 | + ' TRIM(' . $objet . '), ' . |
|
| 1550 | + ' CONCAT( ' . |
|
| 1551 | + " $debutchaine, " . |
|
| 1552 | + ' IF( ' . |
|
| 1553 | + " $poslang = 0, " . |
|
| 1554 | + " $chainemulti, " . |
|
| 1555 | + " $chainelang" . |
|
| 1556 | + ' ), ' . |
|
| 1557 | + " $finchaine" . |
|
| 1558 | + ' ) ' . |
|
| 1559 | + '))) AS multi'; |
|
| 1560 | + |
|
| 1561 | + return $retour; |
|
| 1562 | 1562 | } |
| 1563 | 1563 | |
| 1564 | 1564 | /** |
@@ -1572,7 +1572,7 @@ discard block |
||
| 1572 | 1572 | * Valeur hexadécimale pour MySQL |
| 1573 | 1573 | **/ |
| 1574 | 1574 | function spip_mysql_hex($v) { |
| 1575 | - return '0x' . $v; |
|
| 1575 | + return '0x' . $v; |
|
| 1576 | 1576 | } |
| 1577 | 1577 | |
| 1578 | 1578 | /** |
@@ -1588,15 +1588,15 @@ discard block |
||
| 1588 | 1588 | * Donnée prête à être utilisée par le gestionnaire SQL |
| 1589 | 1589 | */ |
| 1590 | 1590 | function spip_mysql_quote($v, $type = '') { |
| 1591 | - if (!is_array($v)) { |
|
| 1592 | - return spip_mysql_cite($v, $type); |
|
| 1593 | - } |
|
| 1594 | - |
|
| 1595 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1596 | - foreach ($v as $k => $r) { |
|
| 1597 | - $v[$k] = spip_mysql_quote($r, $type); |
|
| 1598 | - } |
|
| 1599 | - return implode(',', $v); |
|
| 1591 | + if (!is_array($v)) { |
|
| 1592 | + return spip_mysql_cite($v, $type); |
|
| 1593 | + } |
|
| 1594 | + |
|
| 1595 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1596 | + foreach ($v as $k => $r) { |
|
| 1597 | + $v[$k] = spip_mysql_quote($r, $type); |
|
| 1598 | + } |
|
| 1599 | + return implode(',', $v); |
|
| 1600 | 1600 | } |
| 1601 | 1601 | |
| 1602 | 1602 | /** |
@@ -1612,18 +1612,18 @@ discard block |
||
| 1612 | 1612 | * Expression SQL |
| 1613 | 1613 | **/ |
| 1614 | 1614 | function spip_mysql_date_proche($champ, $interval, $unite) { |
| 1615 | - $use_now = ( ($champ === 'maj' or strpos($champ, '.maj')) ? true : false ); |
|
| 1616 | - return '(' |
|
| 1617 | - . $champ |
|
| 1618 | - . (($interval <= 0) ? '>' : '<') |
|
| 1619 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1620 | - . '(' |
|
| 1621 | - . ($use_now ? 'NOW()' : sql_quote(date('Y-m-d H:i:s'))) |
|
| 1622 | - . ', INTERVAL ' |
|
| 1623 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1624 | - . ' ' |
|
| 1625 | - . $unite |
|
| 1626 | - . '))'; |
|
| 1615 | + $use_now = ( ($champ === 'maj' or strpos($champ, '.maj')) ? true : false ); |
|
| 1616 | + return '(' |
|
| 1617 | + . $champ |
|
| 1618 | + . (($interval <= 0) ? '>' : '<') |
|
| 1619 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1620 | + . '(' |
|
| 1621 | + . ($use_now ? 'NOW()' : sql_quote(date('Y-m-d H:i:s'))) |
|
| 1622 | + . ', INTERVAL ' |
|
| 1623 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1624 | + . ' ' |
|
| 1625 | + . $unite |
|
| 1626 | + . '))'; |
|
| 1627 | 1627 | } |
| 1628 | 1628 | |
| 1629 | 1629 | |
@@ -1647,7 +1647,7 @@ discard block |
||
| 1647 | 1647 | * Expression de requête SQL |
| 1648 | 1648 | **/ |
| 1649 | 1649 | function spip_mysql_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) { |
| 1650 | - return "($val $not IN ($valeurs))"; |
|
| 1650 | + return "($val $not IN ($valeurs))"; |
|
| 1651 | 1651 | } |
| 1652 | 1652 | |
| 1653 | 1653 | |
@@ -1659,39 +1659,39 @@ discard block |
||
| 1659 | 1659 | * @return string|number Texte ou nombre échappé |
| 1660 | 1660 | */ |
| 1661 | 1661 | function spip_mysql_cite($v, $type) { |
| 1662 | - if (!$type) { |
|
| 1663 | - if (is_bool($v)) { |
|
| 1664 | - return strval(intval($v)); |
|
| 1665 | - } elseif (is_numeric($v)) { |
|
| 1666 | - return strval($v); |
|
| 1667 | - } elseif ($v === null) { |
|
| 1668 | - return "''"; |
|
| 1669 | - } |
|
| 1670 | - return "'" . addslashes($v) . "'"; |
|
| 1671 | - } |
|
| 1672 | - |
|
| 1673 | - if ($v === null) { |
|
| 1674 | - if (stripos($type, 'NOT NULL') === false) { |
|
| 1675 | - // null php se traduit en NULL SQL |
|
| 1676 | - return 'NULL'; |
|
| 1677 | - } else { |
|
| 1678 | - return "''"; |
|
| 1679 | - } |
|
| 1680 | - } elseif (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 1681 | - return $v; |
|
| 1682 | - } elseif (sql_test_int($type)) { |
|
| 1683 | - if ( |
|
| 1684 | - is_numeric($v) |
|
| 1685 | - or ($v and ctype_xdigit(substr($v, 2)) and $v[0] === '0' and $v[1] === 'x') |
|
| 1686 | - ) { |
|
| 1687 | - return $v; |
|
| 1688 | - } else { |
|
| 1689 | - // si pas numerique, forcer le intval |
|
| 1690 | - return intval($v); |
|
| 1691 | - } |
|
| 1692 | - } |
|
| 1693 | - |
|
| 1694 | - return ("'" . addslashes($v) . "'"); |
|
| 1662 | + if (!$type) { |
|
| 1663 | + if (is_bool($v)) { |
|
| 1664 | + return strval(intval($v)); |
|
| 1665 | + } elseif (is_numeric($v)) { |
|
| 1666 | + return strval($v); |
|
| 1667 | + } elseif ($v === null) { |
|
| 1668 | + return "''"; |
|
| 1669 | + } |
|
| 1670 | + return "'" . addslashes($v) . "'"; |
|
| 1671 | + } |
|
| 1672 | + |
|
| 1673 | + if ($v === null) { |
|
| 1674 | + if (stripos($type, 'NOT NULL') === false) { |
|
| 1675 | + // null php se traduit en NULL SQL |
|
| 1676 | + return 'NULL'; |
|
| 1677 | + } else { |
|
| 1678 | + return "''"; |
|
| 1679 | + } |
|
| 1680 | + } elseif (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 1681 | + return $v; |
|
| 1682 | + } elseif (sql_test_int($type)) { |
|
| 1683 | + if ( |
|
| 1684 | + is_numeric($v) |
|
| 1685 | + or ($v and ctype_xdigit(substr($v, 2)) and $v[0] === '0' and $v[1] === 'x') |
|
| 1686 | + ) { |
|
| 1687 | + return $v; |
|
| 1688 | + } else { |
|
| 1689 | + // si pas numerique, forcer le intval |
|
| 1690 | + return intval($v); |
|
| 1691 | + } |
|
| 1692 | + } |
|
| 1693 | + |
|
| 1694 | + return ("'" . addslashes($v) . "'"); |
|
| 1695 | 1695 | } |
| 1696 | 1696 | |
| 1697 | 1697 | /** |
@@ -1701,7 +1701,7 @@ discard block |
||
| 1701 | 1701 | * True si on a les fonctions, false sinon |
| 1702 | 1702 | */ |
| 1703 | 1703 | function spip_versions_mysql() { |
| 1704 | - return function_exists('mysqli_query'); |
|
| 1704 | + return function_exists('mysqli_query'); |
|
| 1705 | 1705 | } |
| 1706 | 1706 | |
| 1707 | 1707 | |
@@ -1714,20 +1714,20 @@ discard block |
||
| 1714 | 1714 | * - chaîne : code compilé pour le faire désactiver par SPIP sinon |
| 1715 | 1715 | */ |
| 1716 | 1716 | function test_rappel_nom_base_mysql($server_db) { |
| 1717 | - $GLOBALS['mysql_rappel_nom_base'] = true; |
|
| 1718 | - sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1719 | - $ok = spip_query("INSERT INTO spip_meta (nom,valeur) VALUES ('mysql_rappel_nom_base', 'test')", $server_db); |
|
| 1717 | + $GLOBALS['mysql_rappel_nom_base'] = true; |
|
| 1718 | + sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1719 | + $ok = spip_query("INSERT INTO spip_meta (nom,valeur) VALUES ('mysql_rappel_nom_base', 'test')", $server_db); |
|
| 1720 | 1720 | |
| 1721 | - if ($ok) { |
|
| 1722 | - sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1721 | + if ($ok) { |
|
| 1722 | + sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1723 | 1723 | |
| 1724 | - return ''; |
|
| 1725 | - } else { |
|
| 1726 | - $GLOBALS['mysql_rappel_nom_base'] = false; |
|
| 1724 | + return ''; |
|
| 1725 | + } else { |
|
| 1726 | + $GLOBALS['mysql_rappel_nom_base'] = false; |
|
| 1727 | 1727 | |
| 1728 | - return "\$GLOBALS['mysql_rappel_nom_base'] = false; " . |
|
| 1729 | - "/* echec de test_rappel_nom_base_mysql a l'installation. */\n"; |
|
| 1730 | - } |
|
| 1728 | + return "\$GLOBALS['mysql_rappel_nom_base'] = false; " . |
|
| 1729 | + "/* echec de test_rappel_nom_base_mysql a l'installation. */\n"; |
|
| 1730 | + } |
|
| 1731 | 1731 | } |
| 1732 | 1732 | |
| 1733 | 1733 | /** |
@@ -1741,13 +1741,13 @@ discard block |
||
| 1741 | 1741 | * - chaîne : code compilé pour l'indiquer le résultat du test à SPIP |
| 1742 | 1742 | */ |
| 1743 | 1743 | function test_sql_mode_mysql($server_db) { |
| 1744 | - $res = sql_select('version() as v', '', '', '', '', '', '', $server_db); |
|
| 1745 | - $row = sql_fetch($res, $server_db); |
|
| 1746 | - if (version_compare($row['v'], '5.0.0', '>=')) { |
|
| 1747 | - defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE', true); |
|
| 1744 | + $res = sql_select('version() as v', '', '', '', '', '', '', $server_db); |
|
| 1745 | + $row = sql_fetch($res, $server_db); |
|
| 1746 | + if (version_compare($row['v'], '5.0.0', '>=')) { |
|
| 1747 | + defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE', true); |
|
| 1748 | 1748 | |
| 1749 | - return "defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE',true);\n"; |
|
| 1750 | - } |
|
| 1749 | + return "defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE',true);\n"; |
|
| 1750 | + } |
|
| 1751 | 1751 | |
| 1752 | - return ''; |
|
| 1752 | + return ''; |
|
| 1753 | 1753 | } |
@@ -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'); |
@@ -32,130 +32,130 @@ discard block |
||
| 32 | 32 | |
| 33 | 33 | // https://code.spip.net/@req_pg_dist |
| 34 | 34 | function req_pg_dist($addr, $port, $login, $pass, $db = '', $prefixe = '') { |
| 35 | - static $last_connect = []; |
|
| 36 | - if (!extension_loaded('pgsql')) { |
|
| 37 | - return false; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - // si provient de selectdb |
|
| 41 | - if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 42 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 43 | - ${$a} = $last_connect[$a]; |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - @[$host, $p] = explode(';', $addr); |
|
| 47 | - if ($p > 0) { |
|
| 48 | - $port = " port=$p"; |
|
| 49 | - } else { |
|
| 50 | - $port = ''; |
|
| 51 | - } |
|
| 52 | - $erreurs = []; |
|
| 53 | - if ($db) { |
|
| 54 | - @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 55 | - } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 56 | - $erreurs[] = pg_last_error(); |
|
| 57 | - if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 58 | - $db = $login; |
|
| 59 | - } else { |
|
| 60 | - $erreurs[] = pg_last_error(); |
|
| 61 | - $db = _DEFAULT_DB; |
|
| 62 | - $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - if (!$link) { |
|
| 66 | - $erreurs[] = pg_last_error(); |
|
| 67 | - foreach ($erreurs as $e) { |
|
| 68 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - return false; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - if ($link) { |
|
| 75 | - $last_connect = [ |
|
| 76 | - 'addr' => $addr, |
|
| 77 | - 'port' => $port, |
|
| 78 | - 'login' => $login, |
|
| 79 | - 'pass' => $pass, |
|
| 80 | - 'db' => $db, |
|
| 81 | - 'prefixe' => $prefixe, |
|
| 82 | - ]; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - spip_log( |
|
| 86 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 87 | - 'pg.' . _LOG_DEBUG |
|
| 88 | - ); |
|
| 89 | - |
|
| 90 | - return !$link ? false : [ |
|
| 91 | - 'db' => $db, |
|
| 92 | - 'prefixe' => $prefixe ?: $db, |
|
| 93 | - 'link' => $link, |
|
| 94 | - ]; |
|
| 35 | + static $last_connect = []; |
|
| 36 | + if (!extension_loaded('pgsql')) { |
|
| 37 | + return false; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + // si provient de selectdb |
|
| 41 | + if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 42 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 43 | + ${$a} = $last_connect[$a]; |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + @[$host, $p] = explode(';', $addr); |
|
| 47 | + if ($p > 0) { |
|
| 48 | + $port = " port=$p"; |
|
| 49 | + } else { |
|
| 50 | + $port = ''; |
|
| 51 | + } |
|
| 52 | + $erreurs = []; |
|
| 53 | + if ($db) { |
|
| 54 | + @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 55 | + } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 56 | + $erreurs[] = pg_last_error(); |
|
| 57 | + if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 58 | + $db = $login; |
|
| 59 | + } else { |
|
| 60 | + $erreurs[] = pg_last_error(); |
|
| 61 | + $db = _DEFAULT_DB; |
|
| 62 | + $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + if (!$link) { |
|
| 66 | + $erreurs[] = pg_last_error(); |
|
| 67 | + foreach ($erreurs as $e) { |
|
| 68 | + spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + return false; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + if ($link) { |
|
| 75 | + $last_connect = [ |
|
| 76 | + 'addr' => $addr, |
|
| 77 | + 'port' => $port, |
|
| 78 | + 'login' => $login, |
|
| 79 | + 'pass' => $pass, |
|
| 80 | + 'db' => $db, |
|
| 81 | + 'prefixe' => $prefixe, |
|
| 82 | + ]; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + spip_log( |
|
| 86 | + "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 87 | + 'pg.' . _LOG_DEBUG |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + return !$link ? false : [ |
|
| 91 | + 'db' => $db, |
|
| 92 | + 'prefixe' => $prefixe ?: $db, |
|
| 93 | + 'link' => $link, |
|
| 94 | + ]; |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | $GLOBALS['spip_pg_functions_1'] = [ |
| 98 | - 'alter' => 'spip_pg_alter', |
|
| 99 | - 'count' => 'spip_pg_count', |
|
| 100 | - 'countsel' => 'spip_pg_countsel', |
|
| 101 | - 'create' => 'spip_pg_create', |
|
| 102 | - 'create_base' => 'spip_pg_create_base', |
|
| 103 | - 'create_view' => 'spip_pg_create_view', |
|
| 104 | - 'date_proche' => 'spip_pg_date_proche', |
|
| 105 | - 'delete' => 'spip_pg_delete', |
|
| 106 | - 'drop_table' => 'spip_pg_drop_table', |
|
| 107 | - 'drop_view' => 'spip_pg_drop_view', |
|
| 108 | - 'errno' => 'spip_pg_errno', |
|
| 109 | - 'error' => 'spip_pg_error', |
|
| 110 | - 'explain' => 'spip_pg_explain', |
|
| 111 | - 'fetch' => 'spip_pg_fetch', |
|
| 112 | - 'seek' => 'spip_pg_seek', |
|
| 113 | - 'free' => 'spip_pg_free', |
|
| 114 | - 'hex' => 'spip_pg_hex', |
|
| 115 | - 'in' => 'spip_pg_in', |
|
| 116 | - 'insert' => 'spip_pg_insert', |
|
| 117 | - 'insertq' => 'spip_pg_insertq', |
|
| 118 | - 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 119 | - 'listdbs' => 'spip_pg_listdbs', |
|
| 120 | - 'multi' => 'spip_pg_multi', |
|
| 121 | - 'optimize' => 'spip_pg_optimize', |
|
| 122 | - 'query' => 'spip_pg_query', |
|
| 123 | - 'quote' => 'spip_pg_quote', |
|
| 124 | - 'replace' => 'spip_pg_replace', |
|
| 125 | - 'replace_multi' => 'spip_pg_replace_multi', |
|
| 126 | - 'select' => 'spip_pg_select', |
|
| 127 | - 'selectdb' => 'spip_pg_selectdb', |
|
| 128 | - 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 129 | - 'showbase' => 'spip_pg_showbase', |
|
| 130 | - 'showtable' => 'spip_pg_showtable', |
|
| 131 | - 'update' => 'spip_pg_update', |
|
| 132 | - 'updateq' => 'spip_pg_updateq', |
|
| 98 | + 'alter' => 'spip_pg_alter', |
|
| 99 | + 'count' => 'spip_pg_count', |
|
| 100 | + 'countsel' => 'spip_pg_countsel', |
|
| 101 | + 'create' => 'spip_pg_create', |
|
| 102 | + 'create_base' => 'spip_pg_create_base', |
|
| 103 | + 'create_view' => 'spip_pg_create_view', |
|
| 104 | + 'date_proche' => 'spip_pg_date_proche', |
|
| 105 | + 'delete' => 'spip_pg_delete', |
|
| 106 | + 'drop_table' => 'spip_pg_drop_table', |
|
| 107 | + 'drop_view' => 'spip_pg_drop_view', |
|
| 108 | + 'errno' => 'spip_pg_errno', |
|
| 109 | + 'error' => 'spip_pg_error', |
|
| 110 | + 'explain' => 'spip_pg_explain', |
|
| 111 | + 'fetch' => 'spip_pg_fetch', |
|
| 112 | + 'seek' => 'spip_pg_seek', |
|
| 113 | + 'free' => 'spip_pg_free', |
|
| 114 | + 'hex' => 'spip_pg_hex', |
|
| 115 | + 'in' => 'spip_pg_in', |
|
| 116 | + 'insert' => 'spip_pg_insert', |
|
| 117 | + 'insertq' => 'spip_pg_insertq', |
|
| 118 | + 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 119 | + 'listdbs' => 'spip_pg_listdbs', |
|
| 120 | + 'multi' => 'spip_pg_multi', |
|
| 121 | + 'optimize' => 'spip_pg_optimize', |
|
| 122 | + 'query' => 'spip_pg_query', |
|
| 123 | + 'quote' => 'spip_pg_quote', |
|
| 124 | + 'replace' => 'spip_pg_replace', |
|
| 125 | + 'replace_multi' => 'spip_pg_replace_multi', |
|
| 126 | + 'select' => 'spip_pg_select', |
|
| 127 | + 'selectdb' => 'spip_pg_selectdb', |
|
| 128 | + 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 129 | + 'showbase' => 'spip_pg_showbase', |
|
| 130 | + 'showtable' => 'spip_pg_showtable', |
|
| 131 | + 'update' => 'spip_pg_update', |
|
| 132 | + 'updateq' => 'spip_pg_updateq', |
|
| 133 | 133 | ]; |
| 134 | 134 | |
| 135 | 135 | // Par ou ca passe une fois les traductions faites |
| 136 | 136 | // https://code.spip.net/@spip_pg_trace_query |
| 137 | 137 | function spip_pg_trace_query($query, $serveur = '') { |
| 138 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 139 | - $prefixe = $connexion['prefixe']; |
|
| 140 | - $link = $connexion['link']; |
|
| 141 | - $db = $connexion['db']; |
|
| 142 | - |
|
| 143 | - if (isset($_GET['var_profile'])) { |
|
| 144 | - include_spip('public/tracer'); |
|
| 145 | - $t = trace_query_start(); |
|
| 146 | - $e = ''; |
|
| 147 | - } else { |
|
| 148 | - $t = 0; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $connexion['last'] = $query; |
|
| 152 | - $r = spip_pg_query_simple($link, $query); |
|
| 153 | - |
|
| 154 | - // Log de l'erreur eventuelle |
|
| 155 | - if ($e = spip_pg_errno($serveur)) { |
|
| 156 | - $e .= spip_pg_error($query, $serveur); |
|
| 157 | - } // et du fautif |
|
| 158 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 138 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 139 | + $prefixe = $connexion['prefixe']; |
|
| 140 | + $link = $connexion['link']; |
|
| 141 | + $db = $connexion['db']; |
|
| 142 | + |
|
| 143 | + if (isset($_GET['var_profile'])) { |
|
| 144 | + include_spip('public/tracer'); |
|
| 145 | + $t = trace_query_start(); |
|
| 146 | + $e = ''; |
|
| 147 | + } else { |
|
| 148 | + $t = 0; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $connexion['last'] = $query; |
|
| 152 | + $r = spip_pg_query_simple($link, $query); |
|
| 153 | + |
|
| 154 | + // Log de l'erreur eventuelle |
|
| 155 | + if ($e = spip_pg_errno($serveur)) { |
|
| 156 | + $e .= spip_pg_error($query, $serveur); |
|
| 157 | + } // et du fautif |
|
| 158 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | // Fonction de requete generale quand on est sur que c'est SQL standard. |
@@ -163,30 +163,30 @@ discard block |
||
| 163 | 163 | |
| 164 | 164 | // https://code.spip.net/@spip_pg_query |
| 165 | 165 | function spip_pg_query($query, $serveur = '', $requeter = true) { |
| 166 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 167 | - $prefixe = $connexion['prefixe']; |
|
| 168 | - $link = $connexion['link']; |
|
| 169 | - $db = $connexion['db']; |
|
| 170 | - |
|
| 171 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 172 | - $suite = strstr($query, (string) $regs[0]); |
|
| 173 | - $query = substr($query, 0, -strlen($suite)); |
|
| 174 | - } else { |
|
| 175 | - $suite = ''; |
|
| 176 | - } |
|
| 177 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 178 | - |
|
| 179 | - // renvoyer la requete inerte si demandee |
|
| 180 | - if (!$requeter) { |
|
| 181 | - return $query; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - return spip_pg_trace_query($query, $serveur); |
|
| 166 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 167 | + $prefixe = $connexion['prefixe']; |
|
| 168 | + $link = $connexion['link']; |
|
| 169 | + $db = $connexion['db']; |
|
| 170 | + |
|
| 171 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 172 | + $suite = strstr($query, (string) $regs[0]); |
|
| 173 | + $query = substr($query, 0, -strlen($suite)); |
|
| 174 | + } else { |
|
| 175 | + $suite = ''; |
|
| 176 | + } |
|
| 177 | + $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 178 | + |
|
| 179 | + // renvoyer la requete inerte si demandee |
|
| 180 | + if (!$requeter) { |
|
| 181 | + return $query; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + return spip_pg_trace_query($query, $serveur); |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | function spip_pg_query_simple($link, $query) { |
| 188 | - #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 189 | - return pg_query($link, $query); |
|
| 188 | + #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 189 | + return pg_query($link, $query); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | /* |
@@ -198,200 +198,200 @@ discard block |
||
| 198 | 198 | * de requetes showtable intempestives |
| 199 | 199 | */ |
| 200 | 200 | function spip_pg_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 201 | - static $tables = []; |
|
| 202 | - |
|
| 203 | - if (!isset($tables[$table])) { |
|
| 204 | - if (!$desc) { |
|
| 205 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 206 | - $desc = $trouver_table($table, $serveur); |
|
| 207 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 208 | - if (!$desc) { |
|
| 209 | - return $couples; |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 214 | - // cependant, il faudra peut etre etendre |
|
| 215 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 216 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 217 | - $tables[$table] = []; |
|
| 218 | - foreach ($desc['field'] as $k => $v) { |
|
| 219 | - $v = strtolower(ltrim($v)); |
|
| 220 | - // ne pas ajouter de timestamp now() si un default est specifie |
|
| 221 | - if (strpos($v, 'timestamp') === 0 and strpos($v, 'default') === false) { |
|
| 222 | - $tables[$table][] = $k; |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // ajout des champs type 'timestamp' absents |
|
| 228 | - foreach ($tables[$table] as $maj) { |
|
| 229 | - if (!array_key_exists($maj, $couples)) { |
|
| 230 | - $couples[$maj] = 'NOW()'; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - return $couples; |
|
| 201 | + static $tables = []; |
|
| 202 | + |
|
| 203 | + if (!isset($tables[$table])) { |
|
| 204 | + if (!$desc) { |
|
| 205 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 206 | + $desc = $trouver_table($table, $serveur); |
|
| 207 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 208 | + if (!$desc) { |
|
| 209 | + return $couples; |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 214 | + // cependant, il faudra peut etre etendre |
|
| 215 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 216 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 217 | + $tables[$table] = []; |
|
| 218 | + foreach ($desc['field'] as $k => $v) { |
|
| 219 | + $v = strtolower(ltrim($v)); |
|
| 220 | + // ne pas ajouter de timestamp now() si un default est specifie |
|
| 221 | + if (strpos($v, 'timestamp') === 0 and strpos($v, 'default') === false) { |
|
| 222 | + $tables[$table][] = $k; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // ajout des champs type 'timestamp' absents |
|
| 228 | + foreach ($tables[$table] as $maj) { |
|
| 229 | + if (!array_key_exists($maj, $couples)) { |
|
| 230 | + $couples[$maj] = 'NOW()'; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + return $couples; |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | |
| 238 | 238 | // Alter en PG ne traite pas les index |
| 239 | 239 | // https://code.spip.net/@spip_pg_alter |
| 240 | 240 | function spip_pg_alter($query, $serveur = '', $requeter = true) { |
| 241 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 242 | - // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 243 | - // ou revoir l'api de sql_alter en creant un |
|
| 244 | - // sql_alter_table($table,array($actions)); |
|
| 245 | - if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 246 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 247 | - |
|
| 248 | - return false; |
|
| 249 | - } |
|
| 250 | - $debut = $regs[1]; |
|
| 251 | - $table = $regs[3]; |
|
| 252 | - $suite = $regs[4]; |
|
| 253 | - $todo = explode(',', $suite); |
|
| 254 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 255 | - $todo2 = []; |
|
| 256 | - $i = 0; |
|
| 257 | - $ouverte = false; |
|
| 258 | - while ($do = array_shift($todo)) { |
|
| 259 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 260 | - $o = (false !== strpos($do, '(')); |
|
| 261 | - $f = (false !== strpos($do, ')')); |
|
| 262 | - if ($o and !$f) { |
|
| 263 | - $ouverte = true; |
|
| 264 | - } elseif ($f) { |
|
| 265 | - $ouverte = false; |
|
| 266 | - } |
|
| 267 | - if (!$ouverte) { |
|
| 268 | - $i++; |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - $todo = $todo2; |
|
| 272 | - $query = $debut . ' ' . array_shift($todo); |
|
| 273 | - |
|
| 274 | - if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 275 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 276 | - } else { |
|
| 277 | - if ($r[1]) { |
|
| 278 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 279 | - } |
|
| 280 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 281 | - if (function_exists($f)) { |
|
| 282 | - $f($r[2], $r[4], $serveur, $requeter); |
|
| 283 | - } else { |
|
| 284 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - // Alter a plusieurs args. Faudrait optimiser. |
|
| 288 | - if ($todo) { |
|
| 289 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 290 | - } |
|
| 241 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 242 | + // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 243 | + // ou revoir l'api de sql_alter en creant un |
|
| 244 | + // sql_alter_table($table,array($actions)); |
|
| 245 | + if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 246 | + spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 247 | + |
|
| 248 | + return false; |
|
| 249 | + } |
|
| 250 | + $debut = $regs[1]; |
|
| 251 | + $table = $regs[3]; |
|
| 252 | + $suite = $regs[4]; |
|
| 253 | + $todo = explode(',', $suite); |
|
| 254 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 255 | + $todo2 = []; |
|
| 256 | + $i = 0; |
|
| 257 | + $ouverte = false; |
|
| 258 | + while ($do = array_shift($todo)) { |
|
| 259 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 260 | + $o = (false !== strpos($do, '(')); |
|
| 261 | + $f = (false !== strpos($do, ')')); |
|
| 262 | + if ($o and !$f) { |
|
| 263 | + $ouverte = true; |
|
| 264 | + } elseif ($f) { |
|
| 265 | + $ouverte = false; |
|
| 266 | + } |
|
| 267 | + if (!$ouverte) { |
|
| 268 | + $i++; |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + $todo = $todo2; |
|
| 272 | + $query = $debut . ' ' . array_shift($todo); |
|
| 273 | + |
|
| 274 | + if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 275 | + spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 276 | + } else { |
|
| 277 | + if ($r[1]) { |
|
| 278 | + spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 279 | + } |
|
| 280 | + $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 281 | + if (function_exists($f)) { |
|
| 282 | + $f($r[2], $r[4], $serveur, $requeter); |
|
| 283 | + } else { |
|
| 284 | + spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + // Alter a plusieurs args. Faudrait optimiser. |
|
| 288 | + if ($todo) { |
|
| 289 | + spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 290 | + } |
|
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | // https://code.spip.net/@spip_pg_alter_change |
| 294 | 294 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 295 | - if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 296 | - spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 297 | - } else { |
|
| 298 | - [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 299 | - $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 300 | - if ($null) { |
|
| 301 | - $actions[] = "ALTER $old SET NOT NULL"; |
|
| 302 | - } else { |
|
| 303 | - $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - if ($d = ($default ?: $def2)) { |
|
| 307 | - $actions[] = "ALTER $old SET $d"; |
|
| 308 | - } else { |
|
| 309 | - $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 313 | - |
|
| 314 | - if ($old != $new) { |
|
| 315 | - spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 316 | - } |
|
| 317 | - } |
|
| 295 | + if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 296 | + spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 297 | + } else { |
|
| 298 | + [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 299 | + $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 300 | + if ($null) { |
|
| 301 | + $actions[] = "ALTER $old SET NOT NULL"; |
|
| 302 | + } else { |
|
| 303 | + $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + if ($d = ($default ?: $def2)) { |
|
| 307 | + $actions[] = "ALTER $old SET $d"; |
|
| 308 | + } else { |
|
| 309 | + $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 313 | + |
|
| 314 | + if ($old != $new) { |
|
| 315 | + spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | // https://code.spip.net/@spip_pg_alter_add |
| 321 | 321 | function spip_pg_alter_add($table, $arg, $serveur = '', $requeter = true) { |
| 322 | - $nom_index = null; |
|
| 323 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 324 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 325 | - |
|
| 326 | - return null; |
|
| 327 | - } |
|
| 328 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 329 | - preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 330 | - if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 331 | - $m[2] = $n[1]; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 335 | - } elseif ($r[1][0] == 'P') { |
|
| 336 | - // la primary peut etre sur plusieurs champs |
|
| 337 | - $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 338 | - $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 339 | - |
|
| 340 | - return spip_pg_query( |
|
| 341 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 342 | - $serveur, |
|
| 343 | - $requeter |
|
| 344 | - ); |
|
| 345 | - } else { |
|
| 346 | - preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 347 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 348 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 349 | - // |
|
| 350 | - // nom_index (colonnes) |
|
| 351 | - if ($m[2]) { |
|
| 352 | - $colonnes = substr($m[2], 1, -1); |
|
| 353 | - $nom_index = $m[1]; |
|
| 354 | - } else { |
|
| 355 | - // (colonne) |
|
| 356 | - if ($m[1][0] == '(') { |
|
| 357 | - $colonnes = substr($m[1], 1, -1); |
|
| 358 | - if (false !== strpos(',', $colonnes)) { |
|
| 359 | - spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 360 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 361 | - } else { |
|
| 362 | - $nom_index = $colonnes; |
|
| 363 | - } |
|
| 364 | - } // nom_index |
|
| 365 | - else { |
|
| 366 | - $nom_index = $colonnes = $m[1]; |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 371 | - } |
|
| 322 | + $nom_index = null; |
|
| 323 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 324 | + spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 325 | + |
|
| 326 | + return null; |
|
| 327 | + } |
|
| 328 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 329 | + preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 330 | + if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 331 | + $m[2] = $n[1]; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 335 | + } elseif ($r[1][0] == 'P') { |
|
| 336 | + // la primary peut etre sur plusieurs champs |
|
| 337 | + $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 338 | + $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 339 | + |
|
| 340 | + return spip_pg_query( |
|
| 341 | + "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 342 | + $serveur, |
|
| 343 | + $requeter |
|
| 344 | + ); |
|
| 345 | + } else { |
|
| 346 | + preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 347 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 348 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 349 | + // |
|
| 350 | + // nom_index (colonnes) |
|
| 351 | + if ($m[2]) { |
|
| 352 | + $colonnes = substr($m[2], 1, -1); |
|
| 353 | + $nom_index = $m[1]; |
|
| 354 | + } else { |
|
| 355 | + // (colonne) |
|
| 356 | + if ($m[1][0] == '(') { |
|
| 357 | + $colonnes = substr($m[1], 1, -1); |
|
| 358 | + if (false !== strpos(',', $colonnes)) { |
|
| 359 | + spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 360 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 361 | + } else { |
|
| 362 | + $nom_index = $colonnes; |
|
| 363 | + } |
|
| 364 | + } // nom_index |
|
| 365 | + else { |
|
| 366 | + $nom_index = $colonnes = $m[1]; |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 371 | + } |
|
| 372 | 372 | } |
| 373 | 373 | |
| 374 | 374 | // https://code.spip.net/@spip_pg_alter_drop |
| 375 | 375 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 376 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 377 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 378 | - } else { |
|
| 379 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 380 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 381 | - } elseif ($r[1][0] == 'P') { |
|
| 382 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 383 | - } else { |
|
| 384 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 385 | - } |
|
| 386 | - } |
|
| 376 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 377 | + spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 378 | + } else { |
|
| 379 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 380 | + return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 381 | + } elseif ($r[1][0] == 'P') { |
|
| 382 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 383 | + } else { |
|
| 384 | + return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 385 | + } |
|
| 386 | + } |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 390 | - if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 391 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 392 | - } else { |
|
| 393 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 394 | - } |
|
| 390 | + if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 391 | + spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 392 | + } else { |
|
| 393 | + return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 394 | + } |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | // attention (en pg) : |
@@ -399,17 +399,17 @@ discard block |
||
| 399 | 399 | // - alter table A rename X to Y = changer le nom de la colonne X en Y |
| 400 | 400 | // pour l'instant, traiter simplement RENAME TO X |
| 401 | 401 | function spip_pg_alter_rename($table, $arg, $serveur = '', $requeter = true) { |
| 402 | - $rename = ''; |
|
| 403 | - // si TO, mais pas au debut |
|
| 404 | - if (!stripos($arg, 'TO ')) { |
|
| 405 | - $rename = $arg; |
|
| 406 | - } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 407 | - $rename = $r[2]; |
|
| 408 | - } else { |
|
| 409 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 402 | + $rename = ''; |
|
| 403 | + // si TO, mais pas au debut |
|
| 404 | + if (!stripos($arg, 'TO ')) { |
|
| 405 | + $rename = $arg; |
|
| 406 | + } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 407 | + $rename = $r[2]; |
|
| 408 | + } else { |
|
| 409 | + spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | |
@@ -425,60 +425,60 @@ discard block |
||
| 425 | 425 | * @return bool ou requete |
| 426 | 426 | */ |
| 427 | 427 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 428 | - if (!($nom or $table or $champs)) { |
|
| 429 | - spip_log( |
|
| 430 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 431 | - 'pg.' . _LOG_ERREUR |
|
| 432 | - ); |
|
| 433 | - |
|
| 434 | - return false; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - $nom = str_replace('`', '', $nom); |
|
| 438 | - $champs = str_replace('`', '', $champs); |
|
| 439 | - |
|
| 440 | - // PG ne differentie pas noms des index en fonction des tables |
|
| 441 | - // il faut donc creer des noms uniques d'index pour une base pg |
|
| 442 | - $nom = $table . '_' . $nom; |
|
| 443 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 444 | - if (!is_array($champs)) { |
|
| 445 | - if ($champs[0] == '(') { |
|
| 446 | - $champs = substr($champs, 1, -1); |
|
| 447 | - } |
|
| 448 | - $champs = [$champs]; |
|
| 449 | - } |
|
| 450 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 451 | - if (!$requeter) { |
|
| 452 | - return $query; |
|
| 453 | - } |
|
| 454 | - $res = spip_pg_query($query, $serveur, $requeter); |
|
| 455 | - |
|
| 456 | - return $res; |
|
| 428 | + if (!($nom or $table or $champs)) { |
|
| 429 | + spip_log( |
|
| 430 | + "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 431 | + 'pg.' . _LOG_ERREUR |
|
| 432 | + ); |
|
| 433 | + |
|
| 434 | + return false; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + $nom = str_replace('`', '', $nom); |
|
| 438 | + $champs = str_replace('`', '', $champs); |
|
| 439 | + |
|
| 440 | + // PG ne differentie pas noms des index en fonction des tables |
|
| 441 | + // il faut donc creer des noms uniques d'index pour une base pg |
|
| 442 | + $nom = $table . '_' . $nom; |
|
| 443 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 444 | + if (!is_array($champs)) { |
|
| 445 | + if ($champs[0] == '(') { |
|
| 446 | + $champs = substr($champs, 1, -1); |
|
| 447 | + } |
|
| 448 | + $champs = [$champs]; |
|
| 449 | + } |
|
| 450 | + $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 451 | + if (!$requeter) { |
|
| 452 | + return $query; |
|
| 453 | + } |
|
| 454 | + $res = spip_pg_query($query, $serveur, $requeter); |
|
| 455 | + |
|
| 456 | + return $res; |
|
| 457 | 457 | } |
| 458 | 458 | |
| 459 | 459 | |
| 460 | 460 | // https://code.spip.net/@spip_pg_explain |
| 461 | 461 | function spip_pg_explain($query, $serveur = '', $requeter = true) { |
| 462 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 463 | - return []; |
|
| 464 | - } |
|
| 465 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 466 | - $prefixe = $connexion['prefixe']; |
|
| 467 | - $link = $connexion['link']; |
|
| 468 | - if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 469 | - $suite = strstr($query, (string) $regs[0]); |
|
| 470 | - $query = substr($query, 0, -strlen($suite)); |
|
| 471 | - } else { |
|
| 472 | - $suite = ''; |
|
| 473 | - } |
|
| 474 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 475 | - |
|
| 476 | - if (!$requeter) { |
|
| 477 | - return $query; |
|
| 478 | - } |
|
| 479 | - $r = spip_pg_query_simple($link, $query); |
|
| 480 | - |
|
| 481 | - return spip_pg_fetch($r, null, $serveur); |
|
| 462 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 463 | + return []; |
|
| 464 | + } |
|
| 465 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 466 | + $prefixe = $connexion['prefixe']; |
|
| 467 | + $link = $connexion['link']; |
|
| 468 | + if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 469 | + $suite = strstr($query, (string) $regs[0]); |
|
| 470 | + $query = substr($query, 0, -strlen($suite)); |
|
| 471 | + } else { |
|
| 472 | + $suite = ''; |
|
| 473 | + } |
|
| 474 | + $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 475 | + |
|
| 476 | + if (!$requeter) { |
|
| 477 | + return $query; |
|
| 478 | + } |
|
| 479 | + $r = spip_pg_query_simple($link, $query); |
|
| 480 | + |
|
| 481 | + return spip_pg_fetch($r, null, $serveur); |
|
| 482 | 482 | } |
| 483 | 483 | |
| 484 | 484 | |
@@ -497,94 +497,94 @@ discard block |
||
| 497 | 497 | * - False en cas d'erreur. |
| 498 | 498 | **/ |
| 499 | 499 | function spip_pg_selectdb($db, $serveur = '', $requeter = true) { |
| 500 | - // se connecter a la base indiquee |
|
| 501 | - // avec les identifiants connus |
|
| 502 | - $index = $serveur ? strtolower($serveur) : 0; |
|
| 503 | - |
|
| 504 | - if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 505 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 506 | - return $db; |
|
| 507 | - } |
|
| 508 | - } else { |
|
| 509 | - return false; |
|
| 510 | - } |
|
| 500 | + // se connecter a la base indiquee |
|
| 501 | + // avec les identifiants connus |
|
| 502 | + $index = $serveur ? strtolower($serveur) : 0; |
|
| 503 | + |
|
| 504 | + if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 505 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 506 | + return $db; |
|
| 507 | + } |
|
| 508 | + } else { |
|
| 509 | + return false; |
|
| 510 | + } |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | // Qu'une seule base pour le moment |
| 514 | 514 | |
| 515 | 515 | // https://code.spip.net/@spip_pg_listdbs |
| 516 | 516 | function spip_pg_listdbs($serveur) { |
| 517 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 518 | - $link = $connexion['link']; |
|
| 519 | - $dbs = []; |
|
| 520 | - $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 521 | - while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 522 | - $dbs[] = reset($row); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - return $dbs; |
|
| 517 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 518 | + $link = $connexion['link']; |
|
| 519 | + $dbs = []; |
|
| 520 | + $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 521 | + while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 522 | + $dbs[] = reset($row); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + return $dbs; |
|
| 526 | 526 | } |
| 527 | 527 | |
| 528 | 528 | // https://code.spip.net/@spip_pg_select |
| 529 | 529 | function spip_pg_select( |
| 530 | - $select, |
|
| 531 | - $from, |
|
| 532 | - $where = '', |
|
| 533 | - $groupby = [], |
|
| 534 | - $orderby = '', |
|
| 535 | - $limit = '', |
|
| 536 | - $having = '', |
|
| 537 | - $serveur = '', |
|
| 538 | - $requeter = true |
|
| 530 | + $select, |
|
| 531 | + $from, |
|
| 532 | + $where = '', |
|
| 533 | + $groupby = [], |
|
| 534 | + $orderby = '', |
|
| 535 | + $limit = '', |
|
| 536 | + $having = '', |
|
| 537 | + $serveur = '', |
|
| 538 | + $requeter = true |
|
| 539 | 539 | ) { |
| 540 | 540 | |
| 541 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 542 | - $prefixe = $connexion['prefixe']; |
|
| 543 | - $link = $connexion['link']; |
|
| 544 | - $db = $connexion['db']; |
|
| 545 | - |
|
| 546 | - $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 547 | - if ($limit) { |
|
| 548 | - $offset = $limatch[2]; |
|
| 549 | - $count = $limatch[3]; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - $select = spip_pg_frommysql($select); |
|
| 553 | - |
|
| 554 | - // si pas de tri explicitement demande, le GROUP BY ne |
|
| 555 | - // contient que la clef primaire. |
|
| 556 | - // lui ajouter alors le champ de tri par defaut |
|
| 557 | - if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 558 | - $groupby[] = $groupbyplus[1]; |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - $orderby = spip_pg_orderby($orderby, $select); |
|
| 562 | - |
|
| 563 | - if ($having) { |
|
| 564 | - if (is_array($having)) { |
|
| 565 | - $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 566 | - } |
|
| 567 | - } |
|
| 568 | - $from = spip_pg_from($from, $prefixe); |
|
| 569 | - $query = 'SELECT ' . $select |
|
| 570 | - . (!$from ? '' : "\nFROM $from") |
|
| 571 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 572 | - "\n\tAND ", |
|
| 573 | - array_map('calculer_pg_where', $where) |
|
| 574 | - ))))) |
|
| 575 | - . spip_pg_groupby($groupby, $from, $select) |
|
| 576 | - . (!$having ? '' : "\nHAVING $having") |
|
| 577 | - . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 578 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 579 | - |
|
| 580 | - // renvoyer la requete inerte si demandee |
|
| 581 | - if ($requeter === false) { |
|
| 582 | - return $query; |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - $r = spip_pg_trace_query($query, $serveur); |
|
| 586 | - |
|
| 587 | - return $r ?: $query; |
|
| 541 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 542 | + $prefixe = $connexion['prefixe']; |
|
| 543 | + $link = $connexion['link']; |
|
| 544 | + $db = $connexion['db']; |
|
| 545 | + |
|
| 546 | + $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 547 | + if ($limit) { |
|
| 548 | + $offset = $limatch[2]; |
|
| 549 | + $count = $limatch[3]; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + $select = spip_pg_frommysql($select); |
|
| 553 | + |
|
| 554 | + // si pas de tri explicitement demande, le GROUP BY ne |
|
| 555 | + // contient que la clef primaire. |
|
| 556 | + // lui ajouter alors le champ de tri par defaut |
|
| 557 | + if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 558 | + $groupby[] = $groupbyplus[1]; |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + $orderby = spip_pg_orderby($orderby, $select); |
|
| 562 | + |
|
| 563 | + if ($having) { |
|
| 564 | + if (is_array($having)) { |
|
| 565 | + $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 566 | + } |
|
| 567 | + } |
|
| 568 | + $from = spip_pg_from($from, $prefixe); |
|
| 569 | + $query = 'SELECT ' . $select |
|
| 570 | + . (!$from ? '' : "\nFROM $from") |
|
| 571 | + . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 572 | + "\n\tAND ", |
|
| 573 | + array_map('calculer_pg_where', $where) |
|
| 574 | + ))))) |
|
| 575 | + . spip_pg_groupby($groupby, $from, $select) |
|
| 576 | + . (!$having ? '' : "\nHAVING $having") |
|
| 577 | + . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 578 | + . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 579 | + |
|
| 580 | + // renvoyer la requete inerte si demandee |
|
| 581 | + if ($requeter === false) { |
|
| 582 | + return $query; |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + $r = spip_pg_trace_query($query, $serveur); |
|
| 586 | + |
|
| 587 | + return $r ?: $query; |
|
| 588 | 588 | ; |
| 589 | 589 | } |
| 590 | 590 | |
@@ -593,27 +593,27 @@ discard block |
||
| 593 | 593 | |
| 594 | 594 | // https://code.spip.net/@spip_pg_from |
| 595 | 595 | function spip_pg_from($from, $prefixe) { |
| 596 | - if (is_array($from)) { |
|
| 597 | - $from = spip_pg_select_as($from); |
|
| 598 | - } |
|
| 596 | + if (is_array($from)) { |
|
| 597 | + $from = spip_pg_select_as($from); |
|
| 598 | + } |
|
| 599 | 599 | |
| 600 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 600 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 601 | 601 | } |
| 602 | 602 | |
| 603 | 603 | // https://code.spip.net/@spip_pg_orderby |
| 604 | 604 | function spip_pg_orderby($order, $select) { |
| 605 | - $res = []; |
|
| 606 | - $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 607 | - |
|
| 608 | - foreach ($arg as $v) { |
|
| 609 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 610 | - $res[] = $m[1]; |
|
| 611 | - } else { |
|
| 612 | - $res[] = $v; |
|
| 613 | - } |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - return spip_pg_frommysql(join(',', $res)); |
|
| 605 | + $res = []; |
|
| 606 | + $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 607 | + |
|
| 608 | + foreach ($arg as $v) { |
|
| 609 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 610 | + $res[] = $m[1]; |
|
| 611 | + } else { |
|
| 612 | + $res[] = $v; |
|
| 613 | + } |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + return spip_pg_frommysql(join(',', $res)); |
|
| 617 | 617 | } |
| 618 | 618 | |
| 619 | 619 | // Conversion a l'arrach' des jointures MySQL en jointures PG |
@@ -622,56 +622,56 @@ discard block |
||
| 622 | 622 | |
| 623 | 623 | // https://code.spip.net/@spip_pg_groupby |
| 624 | 624 | function spip_pg_groupby($groupby, $from, $select) { |
| 625 | - $join = strpos($from, ','); |
|
| 626 | - // ismplifier avant de decouper |
|
| 627 | - if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 628 | - $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - if ($join or $groupby) { |
|
| 632 | - $join = is_array($select) ? $select : explode(', ', $select); |
|
| 633 | - } |
|
| 634 | - if ($join) { |
|
| 635 | - // enlever les 0 as points, '', ... |
|
| 636 | - foreach ($join as $k => $v) { |
|
| 637 | - $v = str_replace('DISTINCT ', '', $v); |
|
| 638 | - // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 639 | - $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 640 | - $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 641 | - // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 642 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 643 | - // idem sans AS (fetch numerique) |
|
| 644 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 645 | - // des AS simples : on garde le cote droit du AS |
|
| 646 | - $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 647 | - // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 648 | - if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 649 | - unset($join[$k]); |
|
| 650 | - } else { |
|
| 651 | - $join[$k] = trim($v); |
|
| 652 | - } |
|
| 653 | - } |
|
| 654 | - $join = array_diff($join, ['']); |
|
| 655 | - $join = implode(',', $join); |
|
| 656 | - } |
|
| 657 | - if (is_array($groupby)) { |
|
| 658 | - $groupby = join(',', $groupby); |
|
| 659 | - } |
|
| 660 | - if ($join) { |
|
| 661 | - $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 662 | - } |
|
| 663 | - if (!$groupby) { |
|
| 664 | - return ''; |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - $groupby = spip_pg_frommysql($groupby); |
|
| 668 | - // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 669 | - // issue de prepare_recherche |
|
| 670 | - $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 671 | - $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 672 | - $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 673 | - |
|
| 674 | - return "\nGROUP BY $groupby"; |
|
| 625 | + $join = strpos($from, ','); |
|
| 626 | + // ismplifier avant de decouper |
|
| 627 | + if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 628 | + $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + if ($join or $groupby) { |
|
| 632 | + $join = is_array($select) ? $select : explode(', ', $select); |
|
| 633 | + } |
|
| 634 | + if ($join) { |
|
| 635 | + // enlever les 0 as points, '', ... |
|
| 636 | + foreach ($join as $k => $v) { |
|
| 637 | + $v = str_replace('DISTINCT ', '', $v); |
|
| 638 | + // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 639 | + $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 640 | + $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 641 | + // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 642 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 643 | + // idem sans AS (fetch numerique) |
|
| 644 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 645 | + // des AS simples : on garde le cote droit du AS |
|
| 646 | + $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 647 | + // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 648 | + if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 649 | + unset($join[$k]); |
|
| 650 | + } else { |
|
| 651 | + $join[$k] = trim($v); |
|
| 652 | + } |
|
| 653 | + } |
|
| 654 | + $join = array_diff($join, ['']); |
|
| 655 | + $join = implode(',', $join); |
|
| 656 | + } |
|
| 657 | + if (is_array($groupby)) { |
|
| 658 | + $groupby = join(',', $groupby); |
|
| 659 | + } |
|
| 660 | + if ($join) { |
|
| 661 | + $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 662 | + } |
|
| 663 | + if (!$groupby) { |
|
| 664 | + return ''; |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + $groupby = spip_pg_frommysql($groupby); |
|
| 668 | + // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 669 | + // issue de prepare_recherche |
|
| 670 | + $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 671 | + $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 672 | + $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 673 | + |
|
| 674 | + return "\nGROUP BY $groupby"; |
|
| 675 | 675 | } |
| 676 | 676 | |
| 677 | 677 | // Conversion des operateurs MySQL en PG |
@@ -683,508 +683,508 @@ discard block |
||
| 683 | 683 | |
| 684 | 684 | // https://code.spip.net/@spip_pg_frommysql |
| 685 | 685 | function spip_pg_frommysql($arg) { |
| 686 | - if (is_array($arg)) { |
|
| 687 | - $arg = join(', ', $arg); |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - $res = spip_pg_fromfield($arg); |
|
| 691 | - |
|
| 692 | - $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 693 | - |
|
| 694 | - $res = preg_replace( |
|
| 695 | - '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 696 | - 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 697 | - $res |
|
| 698 | - ); |
|
| 699 | - $res = preg_replace( |
|
| 700 | - '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 701 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 702 | - $res |
|
| 703 | - ); |
|
| 704 | - $res = preg_replace( |
|
| 705 | - '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 706 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 707 | - $res |
|
| 708 | - ); |
|
| 709 | - |
|
| 710 | - $res = preg_replace( |
|
| 711 | - '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 712 | - ' EXTRACT(epoch FROM NOW())', |
|
| 713 | - $res |
|
| 714 | - ); |
|
| 715 | - |
|
| 716 | - // la fonction md5(integer) n'est pas connu en pg |
|
| 717 | - // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 718 | - $res = preg_replace( |
|
| 719 | - '/md5\s*[(]([^)]*)[)]/i', |
|
| 720 | - 'MD5(CAST(\1 AS text))', |
|
| 721 | - $res |
|
| 722 | - ); |
|
| 723 | - |
|
| 724 | - $res = preg_replace( |
|
| 725 | - '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 726 | - ' EXTRACT(epoch FROM \1)', |
|
| 727 | - $res |
|
| 728 | - ); |
|
| 729 | - |
|
| 730 | - $res = preg_replace( |
|
| 731 | - '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 732 | - ' EXTRACT(day FROM \1)', |
|
| 733 | - $res |
|
| 734 | - ); |
|
| 735 | - |
|
| 736 | - $res = preg_replace( |
|
| 737 | - '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 738 | - ' EXTRACT(month FROM \1)', |
|
| 739 | - $res |
|
| 740 | - ); |
|
| 741 | - |
|
| 742 | - $res = preg_replace( |
|
| 743 | - '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 744 | - ' EXTRACT(year FROM \1)', |
|
| 745 | - $res |
|
| 746 | - ); |
|
| 747 | - |
|
| 748 | - $res = preg_replace( |
|
| 749 | - '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 750 | - ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 751 | - $res |
|
| 752 | - ); |
|
| 753 | - |
|
| 754 | - $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 755 | - |
|
| 756 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 757 | - |
|
| 758 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 759 | - |
|
| 760 | - $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 761 | - $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 762 | - $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 763 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 764 | - $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 765 | - |
|
| 766 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 767 | - $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 768 | - |
|
| 769 | - $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 770 | - $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 686 | + if (is_array($arg)) { |
|
| 687 | + $arg = join(', ', $arg); |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + $res = spip_pg_fromfield($arg); |
|
| 691 | + |
|
| 692 | + $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 693 | + |
|
| 694 | + $res = preg_replace( |
|
| 695 | + '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 696 | + 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 697 | + $res |
|
| 698 | + ); |
|
| 699 | + $res = preg_replace( |
|
| 700 | + '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 701 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 702 | + $res |
|
| 703 | + ); |
|
| 704 | + $res = preg_replace( |
|
| 705 | + '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 706 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 707 | + $res |
|
| 708 | + ); |
|
| 709 | + |
|
| 710 | + $res = preg_replace( |
|
| 711 | + '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 712 | + ' EXTRACT(epoch FROM NOW())', |
|
| 713 | + $res |
|
| 714 | + ); |
|
| 715 | + |
|
| 716 | + // la fonction md5(integer) n'est pas connu en pg |
|
| 717 | + // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 718 | + $res = preg_replace( |
|
| 719 | + '/md5\s*[(]([^)]*)[)]/i', |
|
| 720 | + 'MD5(CAST(\1 AS text))', |
|
| 721 | + $res |
|
| 722 | + ); |
|
| 723 | + |
|
| 724 | + $res = preg_replace( |
|
| 725 | + '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 726 | + ' EXTRACT(epoch FROM \1)', |
|
| 727 | + $res |
|
| 728 | + ); |
|
| 729 | + |
|
| 730 | + $res = preg_replace( |
|
| 731 | + '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 732 | + ' EXTRACT(day FROM \1)', |
|
| 733 | + $res |
|
| 734 | + ); |
|
| 735 | + |
|
| 736 | + $res = preg_replace( |
|
| 737 | + '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 738 | + ' EXTRACT(month FROM \1)', |
|
| 739 | + $res |
|
| 740 | + ); |
|
| 741 | + |
|
| 742 | + $res = preg_replace( |
|
| 743 | + '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 744 | + ' EXTRACT(year FROM \1)', |
|
| 745 | + $res |
|
| 746 | + ); |
|
| 747 | + |
|
| 748 | + $res = preg_replace( |
|
| 749 | + '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 750 | + ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 751 | + $res |
|
| 752 | + ); |
|
| 753 | + |
|
| 754 | + $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 755 | + |
|
| 756 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 757 | + |
|
| 758 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 759 | + |
|
| 760 | + $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 761 | + $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 762 | + $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 763 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 764 | + $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 765 | + |
|
| 766 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 767 | + $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 768 | + |
|
| 769 | + $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 770 | + $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 771 | 771 | # correct en theorie mais produit des debordements arithmetiques |
| 772 | 772 | # $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)(timestamp *'[^']*' *[+-] *timestamp *'[^']*') *[)]/", '\2', $res); |
| 773 | - $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 774 | - $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 773 | + $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 774 | + $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 775 | 775 | |
| 776 | - return str_replace('REGEXP', '~', $res); |
|
| 776 | + return str_replace('REGEXP', '~', $res); |
|
| 777 | 777 | } |
| 778 | 778 | |
| 779 | 779 | // https://code.spip.net/@spip_pg_fromfield |
| 780 | 780 | function spip_pg_fromfield($arg) { |
| 781 | - while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 782 | - preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 783 | - $res = ''; |
|
| 784 | - $n = 0; |
|
| 785 | - $index = $m[2]; |
|
| 786 | - foreach ($r[1] as $v) { |
|
| 787 | - $n++; |
|
| 788 | - $res .= "\nwhen $index=$v then $n"; |
|
| 789 | - } |
|
| 790 | - $arg = $m[1] . "case $res else 0 end " |
|
| 791 | - . substr($arg, strlen($m[0])); |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - return $arg; |
|
| 781 | + while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 782 | + preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 783 | + $res = ''; |
|
| 784 | + $n = 0; |
|
| 785 | + $index = $m[2]; |
|
| 786 | + foreach ($r[1] as $v) { |
|
| 787 | + $n++; |
|
| 788 | + $res .= "\nwhen $index=$v then $n"; |
|
| 789 | + } |
|
| 790 | + $arg = $m[1] . "case $res else 0 end " |
|
| 791 | + . substr($arg, strlen($m[0])); |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + return $arg; |
|
| 795 | 795 | } |
| 796 | 796 | |
| 797 | 797 | // https://code.spip.net/@calculer_pg_where |
| 798 | 798 | function calculer_pg_where($v) { |
| 799 | - if (!is_array($v)) { |
|
| 800 | - return spip_pg_frommysql($v); |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 804 | - if (!($n = count($v))) { |
|
| 805 | - return $op; |
|
| 806 | - } else { |
|
| 807 | - $arg = calculer_pg_where(array_shift($v)); |
|
| 808 | - if ($n == 1) { |
|
| 809 | - return "$op($arg)"; |
|
| 810 | - } else { |
|
| 811 | - $arg2 = calculer_pg_where(array_shift($v)); |
|
| 812 | - if ($n == 2) { |
|
| 813 | - return "($arg $op $arg2)"; |
|
| 814 | - } else { |
|
| 815 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 816 | - } |
|
| 817 | - } |
|
| 818 | - } |
|
| 799 | + if (!is_array($v)) { |
|
| 800 | + return spip_pg_frommysql($v); |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 804 | + if (!($n = count($v))) { |
|
| 805 | + return $op; |
|
| 806 | + } else { |
|
| 807 | + $arg = calculer_pg_where(array_shift($v)); |
|
| 808 | + if ($n == 1) { |
|
| 809 | + return "$op($arg)"; |
|
| 810 | + } else { |
|
| 811 | + $arg2 = calculer_pg_where(array_shift($v)); |
|
| 812 | + if ($n == 2) { |
|
| 813 | + return "($arg $op $arg2)"; |
|
| 814 | + } else { |
|
| 815 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 816 | + } |
|
| 817 | + } |
|
| 818 | + } |
|
| 819 | 819 | } |
| 820 | 820 | |
| 821 | 821 | |
| 822 | 822 | // https://code.spip.net/@calculer_pg_expression |
| 823 | 823 | function calculer_pg_expression($expression, $v, $join = 'AND') { |
| 824 | - if (empty($v)) { |
|
| 825 | - return ''; |
|
| 826 | - } |
|
| 824 | + if (empty($v)) { |
|
| 825 | + return ''; |
|
| 826 | + } |
|
| 827 | 827 | |
| 828 | - $exp = "\n$expression "; |
|
| 828 | + $exp = "\n$expression "; |
|
| 829 | 829 | |
| 830 | - if (!is_array($v)) { |
|
| 831 | - $v = [$v]; |
|
| 832 | - } |
|
| 830 | + if (!is_array($v)) { |
|
| 831 | + $v = [$v]; |
|
| 832 | + } |
|
| 833 | 833 | |
| 834 | - if (strtoupper($join) === 'AND') { |
|
| 835 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 836 | - } else { |
|
| 837 | - return $exp . join($join, $v); |
|
| 838 | - } |
|
| 834 | + if (strtoupper($join) === 'AND') { |
|
| 835 | + return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 836 | + } else { |
|
| 837 | + return $exp . join($join, $v); |
|
| 838 | + } |
|
| 839 | 839 | } |
| 840 | 840 | |
| 841 | 841 | // https://code.spip.net/@spip_pg_select_as |
| 842 | 842 | function spip_pg_select_as($args) { |
| 843 | - $argsas = ''; |
|
| 844 | - foreach ($args as $k => $v) { |
|
| 845 | - if (substr($k, -1) == '@') { |
|
| 846 | - // c'est une jointure qui se refere au from precedent |
|
| 847 | - // pas de virgule |
|
| 848 | - $argsas .= ' ' . $v; |
|
| 849 | - } else { |
|
| 850 | - $as = ''; |
|
| 851 | - // spip_log("$k : $v", _LOG_DEBUG); |
|
| 852 | - if (!is_numeric($k)) { |
|
| 853 | - if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 854 | - $v = $k; |
|
| 855 | - } elseif ($v != $k) { |
|
| 856 | - $p = strpos($v, ' '); |
|
| 857 | - if ($p) { |
|
| 858 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 859 | - } else { |
|
| 860 | - $as = " AS $k"; |
|
| 861 | - } |
|
| 862 | - } |
|
| 863 | - } |
|
| 864 | - // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 865 | - // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 866 | - $argsas .= ', ' . $v . $as; |
|
| 867 | - } |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - return substr($argsas, 2); |
|
| 843 | + $argsas = ''; |
|
| 844 | + foreach ($args as $k => $v) { |
|
| 845 | + if (substr($k, -1) == '@') { |
|
| 846 | + // c'est une jointure qui se refere au from precedent |
|
| 847 | + // pas de virgule |
|
| 848 | + $argsas .= ' ' . $v; |
|
| 849 | + } else { |
|
| 850 | + $as = ''; |
|
| 851 | + // spip_log("$k : $v", _LOG_DEBUG); |
|
| 852 | + if (!is_numeric($k)) { |
|
| 853 | + if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 854 | + $v = $k; |
|
| 855 | + } elseif ($v != $k) { |
|
| 856 | + $p = strpos($v, ' '); |
|
| 857 | + if ($p) { |
|
| 858 | + $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 859 | + } else { |
|
| 860 | + $as = " AS $k"; |
|
| 861 | + } |
|
| 862 | + } |
|
| 863 | + } |
|
| 864 | + // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 865 | + // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 866 | + $argsas .= ', ' . $v . $as; |
|
| 867 | + } |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + return substr($argsas, 2); |
|
| 871 | 871 | } |
| 872 | 872 | |
| 873 | 873 | // https://code.spip.net/@spip_pg_fetch |
| 874 | 874 | function spip_pg_fetch($res, $t = '', $serveur = '', $requeter = true) { |
| 875 | 875 | |
| 876 | - if ($res) { |
|
| 877 | - $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 878 | - } |
|
| 876 | + if ($res) { |
|
| 877 | + $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 878 | + } |
|
| 879 | 879 | |
| 880 | - return $res; |
|
| 880 | + return $res; |
|
| 881 | 881 | } |
| 882 | 882 | |
| 883 | 883 | function spip_pg_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 884 | - if ($r) { |
|
| 885 | - return pg_result_seek($r, $row_number); |
|
| 886 | - } |
|
| 884 | + if ($r) { |
|
| 885 | + return pg_result_seek($r, $row_number); |
|
| 886 | + } |
|
| 887 | 887 | } |
| 888 | 888 | |
| 889 | 889 | |
| 890 | 890 | // https://code.spip.net/@spip_pg_countsel |
| 891 | 891 | function spip_pg_countsel( |
| 892 | - $from = [], |
|
| 893 | - $where = [], |
|
| 894 | - $groupby = [], |
|
| 895 | - $having = [], |
|
| 896 | - $serveur = '', |
|
| 897 | - $requeter = true |
|
| 892 | + $from = [], |
|
| 893 | + $where = [], |
|
| 894 | + $groupby = [], |
|
| 895 | + $having = [], |
|
| 896 | + $serveur = '', |
|
| 897 | + $requeter = true |
|
| 898 | 898 | ) { |
| 899 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 900 | - $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 901 | - if (!$requeter) { |
|
| 902 | - return $r; |
|
| 903 | - } |
|
| 904 | - if (!is_resource($r)) { |
|
| 905 | - return 0; |
|
| 906 | - } |
|
| 907 | - [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 908 | - |
|
| 909 | - return $c; |
|
| 899 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 900 | + $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 901 | + if (!$requeter) { |
|
| 902 | + return $r; |
|
| 903 | + } |
|
| 904 | + if (!is_resource($r)) { |
|
| 905 | + return 0; |
|
| 906 | + } |
|
| 907 | + [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 908 | + |
|
| 909 | + return $c; |
|
| 910 | 910 | } |
| 911 | 911 | |
| 912 | 912 | // https://code.spip.net/@spip_pg_count |
| 913 | 913 | function spip_pg_count($res, $serveur = '', $requeter = true) { |
| 914 | - return !$res ? 0 : pg_numrows($res); |
|
| 914 | + return !$res ? 0 : pg_numrows($res); |
|
| 915 | 915 | } |
| 916 | 916 | |
| 917 | 917 | // https://code.spip.net/@spip_pg_free |
| 918 | 918 | function spip_pg_free($res, $serveur = '', $requeter = true) { |
| 919 | - // rien a faire en postgres |
|
| 919 | + // rien a faire en postgres |
|
| 920 | 920 | } |
| 921 | 921 | |
| 922 | 922 | // https://code.spip.net/@spip_pg_delete |
| 923 | 923 | function spip_pg_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 924 | 924 | |
| 925 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 926 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 925 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 926 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 927 | 927 | |
| 928 | - $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 929 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 928 | + $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 929 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 930 | 930 | |
| 931 | - // renvoyer la requete inerte si demandee |
|
| 932 | - if (!$requeter) { |
|
| 933 | - return $query; |
|
| 934 | - } |
|
| 931 | + // renvoyer la requete inerte si demandee |
|
| 932 | + if (!$requeter) { |
|
| 933 | + return $query; |
|
| 934 | + } |
|
| 935 | 935 | |
| 936 | - $res = spip_pg_trace_query($query, $serveur); |
|
| 937 | - if ($res) { |
|
| 938 | - return pg_affected_rows($res); |
|
| 939 | - } else { |
|
| 940 | - return false; |
|
| 941 | - } |
|
| 936 | + $res = spip_pg_trace_query($query, $serveur); |
|
| 937 | + if ($res) { |
|
| 938 | + return pg_affected_rows($res); |
|
| 939 | + } else { |
|
| 940 | + return false; |
|
| 941 | + } |
|
| 942 | 942 | } |
| 943 | 943 | |
| 944 | 944 | // https://code.spip.net/@spip_pg_insert |
| 945 | 945 | function spip_pg_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 946 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 947 | - $prefixe = $connexion['prefixe']; |
|
| 948 | - $link = $connexion['link']; |
|
| 949 | - |
|
| 950 | - if (!$desc) { |
|
| 951 | - $desc = description_table($table, $serveur); |
|
| 952 | - } |
|
| 953 | - $seq = spip_pg_sequence($table, true); |
|
| 954 | - // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 955 | - if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 956 | - $seq = spip_pg_sequence($table); |
|
| 957 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 958 | - $seq = "currval('$seq')"; |
|
| 959 | - } |
|
| 960 | - |
|
| 961 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 962 | - $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 963 | - $ins = (strlen($champs) < 3) |
|
| 964 | - ? ' DEFAULT VALUES' |
|
| 965 | - : "$champs VALUES $valeurs"; |
|
| 966 | - $q = "INSERT INTO $table $ins $ret"; |
|
| 967 | - if (!$requeter) { |
|
| 968 | - return $q; |
|
| 969 | - } |
|
| 970 | - $connexion['last'] = $q; |
|
| 971 | - $r = spip_pg_query_simple($link, $q); |
|
| 946 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 947 | + $prefixe = $connexion['prefixe']; |
|
| 948 | + $link = $connexion['link']; |
|
| 949 | + |
|
| 950 | + if (!$desc) { |
|
| 951 | + $desc = description_table($table, $serveur); |
|
| 952 | + } |
|
| 953 | + $seq = spip_pg_sequence($table, true); |
|
| 954 | + // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 955 | + if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 956 | + $seq = spip_pg_sequence($table); |
|
| 957 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 958 | + $seq = "currval('$seq')"; |
|
| 959 | + } |
|
| 960 | + |
|
| 961 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 962 | + $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 963 | + $ins = (strlen($champs) < 3) |
|
| 964 | + ? ' DEFAULT VALUES' |
|
| 965 | + : "$champs VALUES $valeurs"; |
|
| 966 | + $q = "INSERT INTO $table $ins $ret"; |
|
| 967 | + if (!$requeter) { |
|
| 968 | + return $q; |
|
| 969 | + } |
|
| 970 | + $connexion['last'] = $q; |
|
| 971 | + $r = spip_pg_query_simple($link, $q); |
|
| 972 | 972 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 973 | - if ($r) { |
|
| 974 | - if (!$ret) { |
|
| 975 | - return 0; |
|
| 976 | - } |
|
| 977 | - if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 978 | - return $r2[0]; |
|
| 979 | - } |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - return false; |
|
| 973 | + if ($r) { |
|
| 974 | + if (!$ret) { |
|
| 975 | + return 0; |
|
| 976 | + } |
|
| 977 | + if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 978 | + return $r2[0]; |
|
| 979 | + } |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + return false; |
|
| 983 | 983 | } |
| 984 | 984 | |
| 985 | 985 | // https://code.spip.net/@spip_pg_insertq |
| 986 | 986 | function spip_pg_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 987 | 987 | |
| 988 | - if (!$desc) { |
|
| 989 | - $desc = description_table($table, $serveur); |
|
| 990 | - } |
|
| 991 | - if (!$desc) { |
|
| 992 | - die("$table insertion sans description"); |
|
| 993 | - } |
|
| 994 | - $fields = $desc['field']; |
|
| 995 | - |
|
| 996 | - foreach ($couples as $champ => $val) { |
|
| 997 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1001 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1002 | - |
|
| 1003 | - return spip_pg_insert( |
|
| 1004 | - $table, |
|
| 1005 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 1006 | - '(' . join(',', $couples) . ')', |
|
| 1007 | - $desc, |
|
| 1008 | - $serveur, |
|
| 1009 | - $requeter |
|
| 1010 | - ); |
|
| 988 | + if (!$desc) { |
|
| 989 | + $desc = description_table($table, $serveur); |
|
| 990 | + } |
|
| 991 | + if (!$desc) { |
|
| 992 | + die("$table insertion sans description"); |
|
| 993 | + } |
|
| 994 | + $fields = $desc['field']; |
|
| 995 | + |
|
| 996 | + foreach ($couples as $champ => $val) { |
|
| 997 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1001 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1002 | + |
|
| 1003 | + return spip_pg_insert( |
|
| 1004 | + $table, |
|
| 1005 | + '(' . join(',', array_keys($couples)) . ')', |
|
| 1006 | + '(' . join(',', $couples) . ')', |
|
| 1007 | + $desc, |
|
| 1008 | + $serveur, |
|
| 1009 | + $requeter |
|
| 1010 | + ); |
|
| 1011 | 1011 | } |
| 1012 | 1012 | |
| 1013 | 1013 | |
| 1014 | 1014 | // https://code.spip.net/@spip_pg_insertq_multi |
| 1015 | 1015 | function spip_pg_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1016 | 1016 | |
| 1017 | - if (!$desc) { |
|
| 1018 | - $desc = description_table($table, $serveur); |
|
| 1019 | - } |
|
| 1020 | - if (!$desc) { |
|
| 1021 | - die("$table insertion sans description"); |
|
| 1022 | - } |
|
| 1023 | - $fields = $desc['field'] ?? []; |
|
| 1024 | - |
|
| 1025 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1026 | - // une premiere fois pour ajouter maj dans les cles |
|
| 1027 | - $c = $tab_couples[0] ?? []; |
|
| 1028 | - $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1029 | - |
|
| 1030 | - $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1031 | - $valeurs = []; |
|
| 1032 | - foreach ($tab_couples as $couples) { |
|
| 1033 | - foreach ($couples as $champ => $val) { |
|
| 1034 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1035 | - } |
|
| 1036 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1037 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1038 | - |
|
| 1039 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1040 | - } |
|
| 1041 | - $valeurs = implode(', ', $valeurs); |
|
| 1042 | - |
|
| 1043 | - return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1017 | + if (!$desc) { |
|
| 1018 | + $desc = description_table($table, $serveur); |
|
| 1019 | + } |
|
| 1020 | + if (!$desc) { |
|
| 1021 | + die("$table insertion sans description"); |
|
| 1022 | + } |
|
| 1023 | + $fields = $desc['field'] ?? []; |
|
| 1024 | + |
|
| 1025 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1026 | + // une premiere fois pour ajouter maj dans les cles |
|
| 1027 | + $c = $tab_couples[0] ?? []; |
|
| 1028 | + $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1029 | + |
|
| 1030 | + $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1031 | + $valeurs = []; |
|
| 1032 | + foreach ($tab_couples as $couples) { |
|
| 1033 | + foreach ($couples as $champ => $val) { |
|
| 1034 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1035 | + } |
|
| 1036 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1037 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1038 | + |
|
| 1039 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1040 | + } |
|
| 1041 | + $valeurs = implode(', ', $valeurs); |
|
| 1042 | + |
|
| 1043 | + return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1044 | 1044 | } |
| 1045 | 1045 | |
| 1046 | 1046 | |
| 1047 | 1047 | // https://code.spip.net/@spip_pg_update |
| 1048 | 1048 | function spip_pg_update($table, $couples, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1049 | 1049 | |
| 1050 | - if (!$couples) { |
|
| 1051 | - return; |
|
| 1052 | - } |
|
| 1053 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1054 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1050 | + if (!$couples) { |
|
| 1051 | + return; |
|
| 1052 | + } |
|
| 1053 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1054 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1055 | 1055 | |
| 1056 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1057 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1056 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1057 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1058 | 1058 | |
| 1059 | - $set = []; |
|
| 1060 | - foreach ($couples as $champ => $val) { |
|
| 1061 | - $set[] = $champ . '=' . $val; |
|
| 1062 | - } |
|
| 1059 | + $set = []; |
|
| 1060 | + foreach ($couples as $champ => $val) { |
|
| 1061 | + $set[] = $champ . '=' . $val; |
|
| 1062 | + } |
|
| 1063 | 1063 | |
| 1064 | - $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1065 | - . calculer_pg_expression('SET', $set, ',') |
|
| 1066 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1064 | + $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1065 | + . calculer_pg_expression('SET', $set, ',') |
|
| 1066 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1067 | 1067 | |
| 1068 | - // renvoyer la requete inerte si demandee |
|
| 1069 | - if (!$requeter) { |
|
| 1070 | - return $query; |
|
| 1071 | - } |
|
| 1068 | + // renvoyer la requete inerte si demandee |
|
| 1069 | + if (!$requeter) { |
|
| 1070 | + return $query; |
|
| 1071 | + } |
|
| 1072 | 1072 | |
| 1073 | - return spip_pg_trace_query($query, $serveur); |
|
| 1073 | + return spip_pg_trace_query($query, $serveur); |
|
| 1074 | 1074 | } |
| 1075 | 1075 | |
| 1076 | 1076 | // idem, mais les valeurs sont des constantes a mettre entre apostrophes |
| 1077 | 1077 | // sauf les expressions de date lorsqu'il s'agit de fonctions SQL (NOW etc) |
| 1078 | 1078 | // https://code.spip.net/@spip_pg_updateq |
| 1079 | 1079 | function spip_pg_updateq($table, $couples, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1080 | - if (!$couples) { |
|
| 1081 | - return; |
|
| 1082 | - } |
|
| 1083 | - if (!$desc) { |
|
| 1084 | - $desc = description_table($table, $serveur); |
|
| 1085 | - } |
|
| 1086 | - $fields = $desc['field']; |
|
| 1087 | - foreach ($couples as $k => $val) { |
|
| 1088 | - $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1080 | + if (!$couples) { |
|
| 1081 | + return; |
|
| 1082 | + } |
|
| 1083 | + if (!$desc) { |
|
| 1084 | + $desc = description_table($table, $serveur); |
|
| 1085 | + } |
|
| 1086 | + $fields = $desc['field']; |
|
| 1087 | + foreach ($couples as $k => $val) { |
|
| 1088 | + $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1092 | 1092 | } |
| 1093 | 1093 | |
| 1094 | 1094 | |
| 1095 | 1095 | // https://code.spip.net/@spip_pg_replace |
| 1096 | 1096 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1097 | - if (!$values) { |
|
| 1098 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1099 | - |
|
| 1100 | - return 0; |
|
| 1101 | - } |
|
| 1102 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1103 | - $prefixe = $connexion['prefixe']; |
|
| 1104 | - $link = $connexion['link']; |
|
| 1105 | - |
|
| 1106 | - if (!$desc) { |
|
| 1107 | - $desc = description_table($table, $serveur); |
|
| 1108 | - } |
|
| 1109 | - if (!$desc) { |
|
| 1110 | - die("$table insertion sans description"); |
|
| 1111 | - } |
|
| 1112 | - $prim = $desc['key']['PRIMARY KEY']; |
|
| 1113 | - $ids = preg_split('/,\s*/', $prim); |
|
| 1114 | - $noprims = $prims = []; |
|
| 1115 | - foreach ($values as $k => $v) { |
|
| 1116 | - $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1117 | - |
|
| 1118 | - if (!in_array($k, $ids)) { |
|
| 1119 | - $noprims[$k] = "$k=$v"; |
|
| 1120 | - } else { |
|
| 1121 | - $prims[$k] = "$k=$v"; |
|
| 1122 | - } |
|
| 1123 | - } |
|
| 1124 | - |
|
| 1125 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1126 | - $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1127 | - |
|
| 1128 | - $where = join(' AND ', $prims); |
|
| 1129 | - if (!$where) { |
|
| 1130 | - return spip_pg_insert( |
|
| 1131 | - $table, |
|
| 1132 | - '(' . join(',', array_keys($values)) . ')', |
|
| 1133 | - '(' . join(',', $values) . ')', |
|
| 1134 | - $desc, |
|
| 1135 | - $serveur |
|
| 1136 | - ); |
|
| 1137 | - } |
|
| 1138 | - $couples = join(',', $noprims); |
|
| 1139 | - |
|
| 1140 | - $seq = spip_pg_sequence($table); |
|
| 1141 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 1142 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1143 | - |
|
| 1144 | - $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1145 | - if ($couples) { |
|
| 1146 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1097 | + if (!$values) { |
|
| 1098 | + spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1099 | + |
|
| 1100 | + return 0; |
|
| 1101 | + } |
|
| 1102 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1103 | + $prefixe = $connexion['prefixe']; |
|
| 1104 | + $link = $connexion['link']; |
|
| 1105 | + |
|
| 1106 | + if (!$desc) { |
|
| 1107 | + $desc = description_table($table, $serveur); |
|
| 1108 | + } |
|
| 1109 | + if (!$desc) { |
|
| 1110 | + die("$table insertion sans description"); |
|
| 1111 | + } |
|
| 1112 | + $prim = $desc['key']['PRIMARY KEY']; |
|
| 1113 | + $ids = preg_split('/,\s*/', $prim); |
|
| 1114 | + $noprims = $prims = []; |
|
| 1115 | + foreach ($values as $k => $v) { |
|
| 1116 | + $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1117 | + |
|
| 1118 | + if (!in_array($k, $ids)) { |
|
| 1119 | + $noprims[$k] = "$k=$v"; |
|
| 1120 | + } else { |
|
| 1121 | + $prims[$k] = "$k=$v"; |
|
| 1122 | + } |
|
| 1123 | + } |
|
| 1124 | + |
|
| 1125 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1126 | + $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1127 | + |
|
| 1128 | + $where = join(' AND ', $prims); |
|
| 1129 | + if (!$where) { |
|
| 1130 | + return spip_pg_insert( |
|
| 1131 | + $table, |
|
| 1132 | + '(' . join(',', array_keys($values)) . ')', |
|
| 1133 | + '(' . join(',', $values) . ')', |
|
| 1134 | + $desc, |
|
| 1135 | + $serveur |
|
| 1136 | + ); |
|
| 1137 | + } |
|
| 1138 | + $couples = join(',', $noprims); |
|
| 1139 | + |
|
| 1140 | + $seq = spip_pg_sequence($table); |
|
| 1141 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 1142 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1143 | + |
|
| 1144 | + $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1145 | + if ($couples) { |
|
| 1146 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1147 | 1147 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 1148 | - if (!$couples) { |
|
| 1149 | - return false; |
|
| 1150 | - } |
|
| 1151 | - $couples = pg_affected_rows($couples); |
|
| 1152 | - } |
|
| 1153 | - if (!$couples) { |
|
| 1154 | - $ret = !$seq ? '' : |
|
| 1155 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1156 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1157 | - ',', |
|
| 1158 | - $values |
|
| 1159 | - ) . ")$ret"; |
|
| 1160 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1161 | - if (!$couples) { |
|
| 1162 | - return false; |
|
| 1163 | - } elseif ($ret) { |
|
| 1164 | - $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1165 | - if ($r[0]) { |
|
| 1166 | - $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1167 | - // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1168 | - // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1169 | - $r = @pg_query($link, $q); |
|
| 1170 | - } |
|
| 1171 | - } |
|
| 1172 | - } |
|
| 1173 | - |
|
| 1174 | - return $couples; |
|
| 1148 | + if (!$couples) { |
|
| 1149 | + return false; |
|
| 1150 | + } |
|
| 1151 | + $couples = pg_affected_rows($couples); |
|
| 1152 | + } |
|
| 1153 | + if (!$couples) { |
|
| 1154 | + $ret = !$seq ? '' : |
|
| 1155 | + (" RETURNING nextval('$seq') < $prim"); |
|
| 1156 | + $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1157 | + ',', |
|
| 1158 | + $values |
|
| 1159 | + ) . ")$ret"; |
|
| 1160 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1161 | + if (!$couples) { |
|
| 1162 | + return false; |
|
| 1163 | + } elseif ($ret) { |
|
| 1164 | + $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1165 | + if ($r[0]) { |
|
| 1166 | + $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1167 | + // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1168 | + // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1169 | + $r = @pg_query($link, $q); |
|
| 1170 | + } |
|
| 1171 | + } |
|
| 1172 | + } |
|
| 1173 | + |
|
| 1174 | + return $couples; |
|
| 1175 | 1175 | } |
| 1176 | 1176 | |
| 1177 | 1177 | |
| 1178 | 1178 | // https://code.spip.net/@spip_pg_replace_multi |
| 1179 | 1179 | function spip_pg_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1180 | - $retour = null; |
|
| 1181 | - // boucler pour traiter chaque requete independemment |
|
| 1182 | - foreach ($tab_couples as $couples) { |
|
| 1183 | - $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - // renvoie le dernier id |
|
| 1187 | - return $retour; |
|
| 1180 | + $retour = null; |
|
| 1181 | + // boucler pour traiter chaque requete independemment |
|
| 1182 | + foreach ($tab_couples as $couples) { |
|
| 1183 | + $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + // renvoie le dernier id |
|
| 1187 | + return $retour; |
|
| 1188 | 1188 | } |
| 1189 | 1189 | |
| 1190 | 1190 | |
@@ -1194,20 +1194,20 @@ discard block |
||
| 1194 | 1194 | // https://code.spip.net/@spip_pg_sequence |
| 1195 | 1195 | function spip_pg_sequence($table, $raw = false) { |
| 1196 | 1196 | |
| 1197 | - include_spip('base/serial'); |
|
| 1198 | - if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1199 | - return false; |
|
| 1200 | - } |
|
| 1201 | - $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1202 | - $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1203 | - if ( |
|
| 1204 | - !preg_match('/^\w+$/', $prim) |
|
| 1205 | - or strpos($desc['field'][$prim], 'int') === false |
|
| 1206 | - ) { |
|
| 1207 | - return ''; |
|
| 1208 | - } else { |
|
| 1209 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1210 | - } |
|
| 1197 | + include_spip('base/serial'); |
|
| 1198 | + if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1199 | + return false; |
|
| 1200 | + } |
|
| 1201 | + $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1202 | + $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1203 | + if ( |
|
| 1204 | + !preg_match('/^\w+$/', $prim) |
|
| 1205 | + or strpos($desc['field'][$prim], 'int') === false |
|
| 1206 | + ) { |
|
| 1207 | + return ''; |
|
| 1208 | + } else { |
|
| 1209 | + return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1210 | + } |
|
| 1211 | 1211 | } |
| 1212 | 1212 | |
| 1213 | 1213 | // Explicite les conversions de Mysql d'une valeur $v de type $t |
@@ -1215,65 +1215,65 @@ discard block |
||
| 1215 | 1215 | |
| 1216 | 1216 | // https://code.spip.net/@spip_pg_cite |
| 1217 | 1217 | function spip_pg_cite($v, $t) { |
| 1218 | - if (is_null($v)) { |
|
| 1219 | - return 'NULL'; |
|
| 1220 | - } // null php se traduit en NULL SQL |
|
| 1221 | - |
|
| 1222 | - if (sql_test_date($t)) { |
|
| 1223 | - if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1224 | - return spip_pg_frommysql($v); |
|
| 1225 | - } else { |
|
| 1226 | - if (strncmp($v, '0000', 4) == 0) { |
|
| 1227 | - $v = '0001' . substr($v, 4); |
|
| 1228 | - } |
|
| 1229 | - if (strpos($v, '-00-00') === 4) { |
|
| 1230 | - $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1231 | - } |
|
| 1232 | - |
|
| 1233 | - return "timestamp '$v'"; |
|
| 1234 | - } |
|
| 1235 | - } elseif (!sql_test_int($t)) { |
|
| 1236 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1237 | - } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1238 | - return $v; |
|
| 1239 | - } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1240 | - return substr($v, 1); |
|
| 1241 | - } else { |
|
| 1242 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1243 | - |
|
| 1244 | - return intval($v); |
|
| 1245 | - } |
|
| 1218 | + if (is_null($v)) { |
|
| 1219 | + return 'NULL'; |
|
| 1220 | + } // null php se traduit en NULL SQL |
|
| 1221 | + |
|
| 1222 | + if (sql_test_date($t)) { |
|
| 1223 | + if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1224 | + return spip_pg_frommysql($v); |
|
| 1225 | + } else { |
|
| 1226 | + if (strncmp($v, '0000', 4) == 0) { |
|
| 1227 | + $v = '0001' . substr($v, 4); |
|
| 1228 | + } |
|
| 1229 | + if (strpos($v, '-00-00') === 4) { |
|
| 1230 | + $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1231 | + } |
|
| 1232 | + |
|
| 1233 | + return "timestamp '$v'"; |
|
| 1234 | + } |
|
| 1235 | + } elseif (!sql_test_int($t)) { |
|
| 1236 | + return ("'" . pg_escape_string($v) . "'"); |
|
| 1237 | + } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1238 | + return $v; |
|
| 1239 | + } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1240 | + return substr($v, 1); |
|
| 1241 | + } else { |
|
| 1242 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1243 | + |
|
| 1244 | + return intval($v); |
|
| 1245 | + } |
|
| 1246 | 1246 | } |
| 1247 | 1247 | |
| 1248 | 1248 | // https://code.spip.net/@spip_pg_hex |
| 1249 | 1249 | function spip_pg_hex($v) { |
| 1250 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1250 | + return "CAST(x'" . $v . "' as bigint)"; |
|
| 1251 | 1251 | } |
| 1252 | 1252 | |
| 1253 | 1253 | function spip_pg_quote($v, $type = '') { |
| 1254 | - if (!is_array($v)) { |
|
| 1255 | - return spip_pg_cite($v, $type); |
|
| 1256 | - } |
|
| 1257 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1258 | - foreach ($v as $k => $r) { |
|
| 1259 | - $v[$k] = spip_pg_quote($r, $type); |
|
| 1260 | - } |
|
| 1261 | - |
|
| 1262 | - return join(',', $v); |
|
| 1254 | + if (!is_array($v)) { |
|
| 1255 | + return spip_pg_cite($v, $type); |
|
| 1256 | + } |
|
| 1257 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1258 | + foreach ($v as $k => $r) { |
|
| 1259 | + $v[$k] = spip_pg_quote($r, $type); |
|
| 1260 | + } |
|
| 1261 | + |
|
| 1262 | + return join(',', $v); |
|
| 1263 | 1263 | } |
| 1264 | 1264 | |
| 1265 | 1265 | function spip_pg_date_proche($champ, $interval, $unite) { |
| 1266 | - return '(' |
|
| 1267 | - . $champ |
|
| 1268 | - . (($interval <= 0) ? '>' : '<') |
|
| 1269 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1270 | - . '(' |
|
| 1271 | - . sql_quote(date('Y-m-d H:i:s')) |
|
| 1272 | - . ', INTERVAL ' |
|
| 1273 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1274 | - . ' ' |
|
| 1275 | - . $unite |
|
| 1276 | - . '))'; |
|
| 1266 | + return '(' |
|
| 1267 | + . $champ |
|
| 1268 | + . (($interval <= 0) ? '>' : '<') |
|
| 1269 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1270 | + . '(' |
|
| 1271 | + . sql_quote(date('Y-m-d H:i:s')) |
|
| 1272 | + . ', INTERVAL ' |
|
| 1273 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1274 | + . ' ' |
|
| 1275 | + . $unite |
|
| 1276 | + . '))'; |
|
| 1277 | 1277 | } |
| 1278 | 1278 | |
| 1279 | 1279 | // https://code.spip.net/@spip_pg_in |
@@ -1281,69 +1281,69 @@ discard block |
||
| 1281 | 1281 | // |
| 1282 | 1282 | // IN (...) souvent limite a 255 elements, d'ou cette fonction assistante |
| 1283 | 1283 | // |
| 1284 | - // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1285 | - if (!$valeurs) { |
|
| 1286 | - return $not ? '0=0' : '0=1'; |
|
| 1287 | - } |
|
| 1288 | - if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1289 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1290 | - } |
|
| 1291 | - $n = $i = 0; |
|
| 1292 | - $in_sql = ''; |
|
| 1293 | - while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1294 | - if ((++$i) >= 255) { |
|
| 1295 | - $in_sql .= "($val $not IN (" . |
|
| 1296 | - substr($valeurs, 0, $n) . |
|
| 1297 | - "))\n" . |
|
| 1298 | - ($not ? "AND\t" : "OR\t"); |
|
| 1299 | - $valeurs = substr($valeurs, $n + 1); |
|
| 1300 | - $i = $n = 0; |
|
| 1301 | - } |
|
| 1302 | - } |
|
| 1303 | - $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1304 | - |
|
| 1305 | - return "($in_sql)"; |
|
| 1284 | + // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1285 | + if (!$valeurs) { |
|
| 1286 | + return $not ? '0=0' : '0=1'; |
|
| 1287 | + } |
|
| 1288 | + if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1289 | + return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1290 | + } |
|
| 1291 | + $n = $i = 0; |
|
| 1292 | + $in_sql = ''; |
|
| 1293 | + while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1294 | + if ((++$i) >= 255) { |
|
| 1295 | + $in_sql .= "($val $not IN (" . |
|
| 1296 | + substr($valeurs, 0, $n) . |
|
| 1297 | + "))\n" . |
|
| 1298 | + ($not ? "AND\t" : "OR\t"); |
|
| 1299 | + $valeurs = substr($valeurs, $n + 1); |
|
| 1300 | + $i = $n = 0; |
|
| 1301 | + } |
|
| 1302 | + } |
|
| 1303 | + $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1304 | + |
|
| 1305 | + return "($in_sql)"; |
|
| 1306 | 1306 | } |
| 1307 | 1307 | |
| 1308 | 1308 | // https://code.spip.net/@spip_pg_error |
| 1309 | 1309 | function spip_pg_error($query = '', $serveur = '', $requeter = true) { |
| 1310 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1311 | - $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1312 | - if ($s) { |
|
| 1313 | - $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1314 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1315 | - } |
|
| 1316 | - |
|
| 1317 | - return $s; |
|
| 1310 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1311 | + $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1312 | + if ($s) { |
|
| 1313 | + $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1314 | + spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1315 | + } |
|
| 1316 | + |
|
| 1317 | + return $s; |
|
| 1318 | 1318 | } |
| 1319 | 1319 | |
| 1320 | 1320 | // https://code.spip.net/@spip_pg_errno |
| 1321 | 1321 | function spip_pg_errno($serveur = '') { |
| 1322 | - // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1323 | - // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1324 | - return 0; |
|
| 1322 | + // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1323 | + // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1324 | + return 0; |
|
| 1325 | 1325 | } |
| 1326 | 1326 | |
| 1327 | 1327 | // https://code.spip.net/@spip_pg_drop_table |
| 1328 | 1328 | function spip_pg_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 1329 | - if ($exist) { |
|
| 1330 | - $exist = ' IF EXISTS'; |
|
| 1331 | - } |
|
| 1332 | - if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1333 | - return true; |
|
| 1334 | - } else { |
|
| 1335 | - return false; |
|
| 1336 | - } |
|
| 1329 | + if ($exist) { |
|
| 1330 | + $exist = ' IF EXISTS'; |
|
| 1331 | + } |
|
| 1332 | + if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1333 | + return true; |
|
| 1334 | + } else { |
|
| 1335 | + return false; |
|
| 1336 | + } |
|
| 1337 | 1337 | } |
| 1338 | 1338 | |
| 1339 | 1339 | // supprime une vue |
| 1340 | 1340 | // https://code.spip.net/@spip_pg_drop_view |
| 1341 | 1341 | function spip_pg_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 1342 | - if ($exist) { |
|
| 1343 | - $exist = ' IF EXISTS'; |
|
| 1344 | - } |
|
| 1342 | + if ($exist) { |
|
| 1343 | + $exist = ' IF EXISTS'; |
|
| 1344 | + } |
|
| 1345 | 1345 | |
| 1346 | - return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1346 | + return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1347 | 1347 | } |
| 1348 | 1348 | |
| 1349 | 1349 | /** |
@@ -1360,41 +1360,41 @@ discard block |
||
| 1360 | 1360 | * Ressource à utiliser avec sql_fetch() |
| 1361 | 1361 | **/ |
| 1362 | 1362 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1363 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1364 | - $link = $connexion['link']; |
|
| 1365 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1363 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1364 | + $link = $connexion['link']; |
|
| 1365 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1366 | 1366 | |
| 1367 | - return spip_pg_query_simple($link, $q); |
|
| 1367 | + return spip_pg_query_simple($link, $q); |
|
| 1368 | 1368 | } |
| 1369 | 1369 | |
| 1370 | 1370 | // https://code.spip.net/@spip_pg_showtable |
| 1371 | 1371 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1372 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1373 | - $link = $connexion['link']; |
|
| 1374 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1375 | - |
|
| 1376 | - $res = spip_pg_query_simple($link, $q); |
|
| 1377 | - if (!$res) { |
|
| 1378 | - return false; |
|
| 1379 | - } |
|
| 1380 | - |
|
| 1381 | - // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1382 | - // il faut en tenir compte dans le return |
|
| 1383 | - $fields = []; |
|
| 1384 | - while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1385 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1386 | - } |
|
| 1387 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1388 | - $res = spip_pg_query_simple($link, $q); |
|
| 1389 | - $keys = []; |
|
| 1390 | - while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1391 | - if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1392 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1393 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1394 | - } |
|
| 1395 | - } |
|
| 1396 | - |
|
| 1397 | - return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1372 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1373 | + $link = $connexion['link']; |
|
| 1374 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1375 | + |
|
| 1376 | + $res = spip_pg_query_simple($link, $q); |
|
| 1377 | + if (!$res) { |
|
| 1378 | + return false; |
|
| 1379 | + } |
|
| 1380 | + |
|
| 1381 | + // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1382 | + // il faut en tenir compte dans le return |
|
| 1383 | + $fields = []; |
|
| 1384 | + while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1385 | + $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1386 | + } |
|
| 1387 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1388 | + $res = spip_pg_query_simple($link, $q); |
|
| 1389 | + $keys = []; |
|
| 1390 | + while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1391 | + if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1392 | + $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1393 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1394 | + } |
|
| 1395 | + } |
|
| 1396 | + |
|
| 1397 | + return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1398 | 1398 | } |
| 1399 | 1399 | |
| 1400 | 1400 | // Fonction de creation d'une table SQL nommee $nom |
@@ -1406,118 +1406,118 @@ discard block |
||
| 1406 | 1406 | // https://code.spip.net/@spip_pg_create |
| 1407 | 1407 | function spip_pg_create($nom, $champs, $cles, $autoinc = false, $temporary = false, $serveur = '', $requeter = true) { |
| 1408 | 1408 | |
| 1409 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1410 | - $link = $connexion['link']; |
|
| 1411 | - $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1412 | - |
|
| 1413 | - $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1414 | - $keys = []; |
|
| 1415 | - |
|
| 1416 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1417 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1418 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 1419 | - return; |
|
| 1420 | - } |
|
| 1421 | - |
|
| 1422 | - foreach ($cles as $k => $v) { |
|
| 1423 | - if (strpos($k, 'KEY ') === 0) { |
|
| 1424 | - $n = str_replace('`', '', $k); |
|
| 1425 | - $v = str_replace('`', '"', $v); |
|
| 1426 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1427 | - if ($k != $n) { |
|
| 1428 | - $i = "\"$i\""; |
|
| 1429 | - } |
|
| 1430 | - $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1431 | - } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1432 | - $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1433 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1434 | - } else { |
|
| 1435 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1436 | - } |
|
| 1437 | - if ($k == 'PRIMARY KEY') { |
|
| 1438 | - $prim_name = $v; |
|
| 1439 | - } |
|
| 1440 | - $s = ','; |
|
| 1441 | - } |
|
| 1442 | - $s = ''; |
|
| 1443 | - |
|
| 1444 | - $character_set = ''; |
|
| 1445 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1446 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1447 | - } |
|
| 1448 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1449 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1450 | - } |
|
| 1451 | - |
|
| 1452 | - foreach ($champs as $k => $v) { |
|
| 1453 | - $k = str_replace('`', '"', $k); |
|
| 1454 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1455 | - if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1456 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1457 | - } |
|
| 1458 | - } |
|
| 1459 | - |
|
| 1460 | - $query .= "$s\n\t\t$k " |
|
| 1461 | - . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1462 | - ? ' bigserial' |
|
| 1463 | - : mysql2pg_type($v) |
|
| 1464 | - ); |
|
| 1465 | - $s = ','; |
|
| 1466 | - } |
|
| 1467 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1468 | - |
|
| 1469 | - // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1470 | - |
|
| 1471 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1472 | - ($character_set ? " DEFAULT $character_set" : '') |
|
| 1473 | - . "\n"; |
|
| 1474 | - |
|
| 1475 | - if (!$requeter) { |
|
| 1476 | - return $q; |
|
| 1477 | - } |
|
| 1478 | - $connexion['last'] = $q; |
|
| 1479 | - $r = @pg_query($link, $q); |
|
| 1480 | - |
|
| 1481 | - if (!$r) { |
|
| 1482 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1483 | - } else { |
|
| 1484 | - foreach ($keys as $index) { |
|
| 1485 | - pg_query($link, $index); |
|
| 1486 | - } |
|
| 1487 | - } |
|
| 1488 | - |
|
| 1489 | - return $r; |
|
| 1409 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1410 | + $link = $connexion['link']; |
|
| 1411 | + $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1412 | + |
|
| 1413 | + $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1414 | + $keys = []; |
|
| 1415 | + |
|
| 1416 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1417 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1418 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 1419 | + return; |
|
| 1420 | + } |
|
| 1421 | + |
|
| 1422 | + foreach ($cles as $k => $v) { |
|
| 1423 | + if (strpos($k, 'KEY ') === 0) { |
|
| 1424 | + $n = str_replace('`', '', $k); |
|
| 1425 | + $v = str_replace('`', '"', $v); |
|
| 1426 | + $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1427 | + if ($k != $n) { |
|
| 1428 | + $i = "\"$i\""; |
|
| 1429 | + } |
|
| 1430 | + $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1431 | + } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1432 | + $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1433 | + $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1434 | + } else { |
|
| 1435 | + $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1436 | + } |
|
| 1437 | + if ($k == 'PRIMARY KEY') { |
|
| 1438 | + $prim_name = $v; |
|
| 1439 | + } |
|
| 1440 | + $s = ','; |
|
| 1441 | + } |
|
| 1442 | + $s = ''; |
|
| 1443 | + |
|
| 1444 | + $character_set = ''; |
|
| 1445 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1446 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1447 | + } |
|
| 1448 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1449 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1450 | + } |
|
| 1451 | + |
|
| 1452 | + foreach ($champs as $k => $v) { |
|
| 1453 | + $k = str_replace('`', '"', $k); |
|
| 1454 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1455 | + if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1456 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1457 | + } |
|
| 1458 | + } |
|
| 1459 | + |
|
| 1460 | + $query .= "$s\n\t\t$k " |
|
| 1461 | + . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1462 | + ? ' bigserial' |
|
| 1463 | + : mysql2pg_type($v) |
|
| 1464 | + ); |
|
| 1465 | + $s = ','; |
|
| 1466 | + } |
|
| 1467 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1468 | + |
|
| 1469 | + // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1470 | + |
|
| 1471 | + $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1472 | + ($character_set ? " DEFAULT $character_set" : '') |
|
| 1473 | + . "\n"; |
|
| 1474 | + |
|
| 1475 | + if (!$requeter) { |
|
| 1476 | + return $q; |
|
| 1477 | + } |
|
| 1478 | + $connexion['last'] = $q; |
|
| 1479 | + $r = @pg_query($link, $q); |
|
| 1480 | + |
|
| 1481 | + if (!$r) { |
|
| 1482 | + spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1483 | + } else { |
|
| 1484 | + foreach ($keys as $index) { |
|
| 1485 | + pg_query($link, $index); |
|
| 1486 | + } |
|
| 1487 | + } |
|
| 1488 | + |
|
| 1489 | + return $r; |
|
| 1490 | 1490 | } |
| 1491 | 1491 | |
| 1492 | 1492 | |
| 1493 | 1493 | function spip_pg_create_base($nom, $serveur = '', $requeter = true) { |
| 1494 | - return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1494 | + return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1495 | 1495 | } |
| 1496 | 1496 | |
| 1497 | 1497 | // Fonction de creation d'une vue SQL nommee $nom |
| 1498 | 1498 | // https://code.spip.net/@spip_pg_create_view |
| 1499 | 1499 | function spip_pg_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 1500 | - if (!$query_select) { |
|
| 1501 | - return false; |
|
| 1502 | - } |
|
| 1503 | - // vue deja presente |
|
| 1504 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 1505 | - if ($requeter) { |
|
| 1506 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1507 | - } |
|
| 1500 | + if (!$query_select) { |
|
| 1501 | + return false; |
|
| 1502 | + } |
|
| 1503 | + // vue deja presente |
|
| 1504 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 1505 | + if ($requeter) { |
|
| 1506 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1507 | + } |
|
| 1508 | 1508 | |
| 1509 | - return false; |
|
| 1510 | - } |
|
| 1509 | + return false; |
|
| 1510 | + } |
|
| 1511 | 1511 | |
| 1512 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1512 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1513 | 1513 | |
| 1514 | - return spip_pg_query($query, $serveur, $requeter); |
|
| 1514 | + return spip_pg_query($query, $serveur, $requeter); |
|
| 1515 | 1515 | } |
| 1516 | 1516 | |
| 1517 | 1517 | |
| 1518 | 1518 | // https://code.spip.net/@spip_pg_set_connect_charset |
| 1519 | 1519 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1520 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1520 | + spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1521 | 1521 | } |
| 1522 | 1522 | |
| 1523 | 1523 | |
@@ -1531,7 +1531,7 @@ discard block |
||
| 1531 | 1531 | **/ |
| 1532 | 1532 | // https://code.spip.net/@spip_sqlite_optimize |
| 1533 | 1533 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1534 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1534 | + return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1535 | 1535 | } |
| 1536 | 1536 | |
| 1537 | 1537 | // Selectionner la sous-chaine dans $objet |
@@ -1539,13 +1539,13 @@ discard block |
||
| 1539 | 1539 | |
| 1540 | 1540 | // https://code.spip.net/@spip_pg_multi |
| 1541 | 1541 | function spip_pg_multi($objet, $lang) { |
| 1542 | - $r = 'regexp_replace(' |
|
| 1543 | - . $objet |
|
| 1544 | - . ",'<multi>.*[[]" |
|
| 1545 | - . $lang |
|
| 1546 | - . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1542 | + $r = 'regexp_replace(' |
|
| 1543 | + . $objet |
|
| 1544 | + . ",'<multi>.*[[]" |
|
| 1545 | + . $lang |
|
| 1546 | + . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1547 | 1547 | |
| 1548 | - return $r; |
|
| 1548 | + return $r; |
|
| 1549 | 1549 | } |
| 1550 | 1550 | |
| 1551 | 1551 | // Palanquee d'idiosyncrasies MySQL dans les creations de table |
@@ -1553,31 +1553,31 @@ discard block |
||
| 1553 | 1553 | |
| 1554 | 1554 | // https://code.spip.net/@mysql2pg_type |
| 1555 | 1555 | function mysql2pg_type($v) { |
| 1556 | - $remplace = [ |
|
| 1557 | - '/auto_increment/i' => '', // non reconnu |
|
| 1558 | - '/bigint/i' => 'bigint', |
|
| 1559 | - '/mediumint/i' => 'mediumint', |
|
| 1560 | - '/smallint/i' => 'smallint', |
|
| 1561 | - '/tinyint/i' => 'int', |
|
| 1562 | - '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1563 | - '/longtext/i' => 'text', |
|
| 1564 | - '/mediumtext/i' => 'text', |
|
| 1565 | - '/tinytext/i' => 'text', |
|
| 1566 | - '/longblob/i' => 'text', |
|
| 1567 | - '/0000-00-00/' => '0001-01-01', |
|
| 1568 | - '/datetime/i' => 'timestamp', |
|
| 1569 | - '/unsigned/i' => '', |
|
| 1570 | - '/double/i' => 'double precision', |
|
| 1571 | - '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1572 | - '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1573 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1574 | - ]; |
|
| 1575 | - |
|
| 1576 | - return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1556 | + $remplace = [ |
|
| 1557 | + '/auto_increment/i' => '', // non reconnu |
|
| 1558 | + '/bigint/i' => 'bigint', |
|
| 1559 | + '/mediumint/i' => 'mediumint', |
|
| 1560 | + '/smallint/i' => 'smallint', |
|
| 1561 | + '/tinyint/i' => 'int', |
|
| 1562 | + '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1563 | + '/longtext/i' => 'text', |
|
| 1564 | + '/mediumtext/i' => 'text', |
|
| 1565 | + '/tinytext/i' => 'text', |
|
| 1566 | + '/longblob/i' => 'text', |
|
| 1567 | + '/0000-00-00/' => '0001-01-01', |
|
| 1568 | + '/datetime/i' => 'timestamp', |
|
| 1569 | + '/unsigned/i' => '', |
|
| 1570 | + '/double/i' => 'double precision', |
|
| 1571 | + '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1572 | + '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1573 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1574 | + ]; |
|
| 1575 | + |
|
| 1576 | + return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1577 | 1577 | } |
| 1578 | 1578 | |
| 1579 | 1579 | // Renvoie false si on n'a pas les fonctions pg (pour l'install) |
| 1580 | 1580 | // https://code.spip.net/@spip_versions_pg |
| 1581 | 1581 | function spip_versions_pg() { |
| 1582 | - return function_exists('pg_connect'); |
|
| 1582 | + return function_exists('pg_connect'); |
|
| 1583 | 1583 | } |
@@ -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 | /** |
@@ -23,333 +23,333 @@ discard block |
||
| 23 | 23 | **/ |
| 24 | 24 | class ValidateurXML { |
| 25 | 25 | |
| 26 | - // https://code.spip.net/@validerElement |
|
| 27 | - public function validerElement($phraseur, $name, $attrs) { |
|
| 28 | - if (!($p = isset($this->dtc->elements[$name]))) { |
|
| 29 | - if ($p = strpos($name, ':')) { |
|
| 30 | - $name = substr($name, $p + 1); |
|
| 31 | - $p = isset($this->dtc->elements[$name]); |
|
| 32 | - } |
|
| 33 | - if (!$p) { |
|
| 34 | - coordonnees_erreur($this, " <b>$name</b> : " |
|
| 35 | - . _T('zxml_inconnu_balise')); |
|
| 36 | - |
|
| 37 | - return; |
|
| 38 | - } |
|
| 39 | - } |
|
| 40 | - // controler les filles illegitimes, ca suffit |
|
| 41 | - $depth = $this->depth; |
|
| 42 | - $ouvrant = $this->ouvrant; |
|
| 43 | - #spip_log("trouve $name apres " . $ouvrant[$depth]); |
|
| 44 | - if (isset($ouvrant[$depth])) { |
|
| 45 | - if (preg_match('/^\s*(\w+)/', $ouvrant[$depth], $r)) { |
|
| 46 | - $pere = $r[1]; |
|
| 47 | - #spip_log("pere $pere"); |
|
| 48 | - if (isset($this->dtc->elements[$pere])) { |
|
| 49 | - $fils = $this->dtc->elements[$pere]; |
|
| 50 | - #spip_log("rejeton $name fils " . @join(',',$fils)); |
|
| 51 | - if (!($p = @in_array($name, $fils))) { |
|
| 52 | - if ($p = strpos($name, ':')) { |
|
| 53 | - $p = substr($name, $p + 1); |
|
| 54 | - $p = @in_array($p, $fils); |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - if (!$p) { |
|
| 58 | - $bons_peres = @join('</b>, <b>', $this->dtc->peres[$name]); |
|
| 59 | - coordonnees_erreur($this, " <b>$name</b> " |
|
| 60 | - . _T('zxml_non_fils') |
|
| 61 | - . ' <b>' |
|
| 62 | - . $pere |
|
| 63 | - . '</b>' |
|
| 64 | - . (!$bons_peres ? '' |
|
| 65 | - : ('<p style="font-size: 80%"> ' . _T('zxml_mais_de') . ' <b>' . $bons_peres . '</b></p>'))); |
|
| 66 | - } elseif ($this->dtc->regles[$pere][0] == '/') { |
|
| 67 | - $frat = substr($depth, 2); |
|
| 68 | - if (!isset($this->fratrie[$frat])) { |
|
| 69 | - $this->fratrie[$frat] = ''; |
|
| 70 | - } |
|
| 71 | - $this->fratrie[$frat] .= "$name "; |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - // Init de la suite des balises a memoriser si regle difficile |
|
| 77 | - if ($this->dtc->regles[$name] and $this->dtc->regles[$name][0] == '/') { |
|
| 78 | - $this->fratrie[$depth] = ''; |
|
| 79 | - } |
|
| 80 | - if (isset($this->dtc->attributs[$name])) { |
|
| 81 | - foreach ($this->dtc->attributs[$name] as $n => $v) { |
|
| 82 | - if (($v[1] == '#REQUIRED') and (!isset($attrs[$n]))) { |
|
| 83 | - coordonnees_erreur($this, " <b>$n</b>" |
|
| 84 | - . ' : ' |
|
| 85 | - . _T('zxml_obligatoire_attribut') |
|
| 86 | - . " <b>$name</b>"); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - // https://code.spip.net/@validerAttribut |
|
| 93 | - public function validerAttribut($phraseur, $name, $val, $bal) { |
|
| 94 | - // Si la balise est inconnue, eviter d'insister |
|
| 95 | - if (!isset($this->dtc->attributs[$bal])) { |
|
| 96 | - return; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $a = $this->dtc->attributs[$bal]; |
|
| 100 | - if (!isset($a[$name])) { |
|
| 101 | - $bons = join(', ', array_keys($a)); |
|
| 102 | - if ($bons) { |
|
| 103 | - $bons = " title=' " . |
|
| 104 | - _T('zxml_connus_attributs') . |
|
| 105 | - ' : ' . |
|
| 106 | - $bons . |
|
| 107 | - "'"; |
|
| 108 | - } |
|
| 109 | - $bons .= " style='font-weight: bold'"; |
|
| 110 | - coordonnees_erreur($this, " <b>$name</b> " |
|
| 111 | - . _T('zxml_inconnu_attribut') . ' ' . _T('zxml_de') |
|
| 112 | - . " <a$bons>$bal</a> (" |
|
| 113 | - . _T('zxml_survoler') |
|
| 114 | - . ')'); |
|
| 115 | - } else { |
|
| 116 | - $type = $a[$name][0]; |
|
| 117 | - if (!preg_match('/^\w+$/', $type)) { |
|
| 118 | - $this->valider_motif($phraseur, $name, $val, $bal, $type); |
|
| 119 | - } else { |
|
| 120 | - if (method_exists($this, $f = 'validerAttribut_' . $type)) { |
|
| 121 | - $this->$f($phraseur, $name, $val, $bal); |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - # else spip_log("$type type d'attribut inconnu"); |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - public function validerAttribut_NMTOKEN($phraseur, $name, $val, $bal) { |
|
| 129 | - $this->valider_motif($phraseur, $name, $val, $bal, _REGEXP_NMTOKEN); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - public function validerAttribut_NMTOKENS($phraseur, $name, $val, $bal) { |
|
| 133 | - $this->valider_motif($phraseur, $name, $val, $bal, _REGEXP_NMTOKENS); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // https://code.spip.net/@validerAttribut_ID |
|
| 137 | - public function validerAttribut_ID($phraseur, $name, $val, $bal) { |
|
| 138 | - if (isset($this->ids[$val])) { |
|
| 139 | - [$l, $c] = $this->ids[$val]; |
|
| 140 | - coordonnees_erreur($this, " <p><b>$val</b> " |
|
| 141 | - . _T('zxml_valeur_attribut') |
|
| 142 | - . " <b>$name</b> " |
|
| 143 | - . _T('zxml_de') |
|
| 144 | - . " <b>$bal</b> " |
|
| 145 | - . _T('zxml_vu') |
|
| 146 | - . " (L$l,C$c)"); |
|
| 147 | - } else { |
|
| 148 | - $this->valider_motif($phraseur, $name, $val, $bal, _REGEXP_ID); |
|
| 149 | - $this->ids[$val] = [xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur)]; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - // https://code.spip.net/@validerAttribut_IDREF |
|
| 154 | - public function validerAttribut_IDREF($phraseur, $name, $val, $bal) { |
|
| 155 | - $this->idrefs[] = [$val, xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur)]; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - // https://code.spip.net/@validerAttribut_IDREFS |
|
| 159 | - public function validerAttribut_IDREFS($phraseur, $name, $val, $bal) { |
|
| 160 | - $this->idrefss[] = [$val, xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur)]; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // https://code.spip.net/@valider_motif |
|
| 164 | - public function valider_motif($phraseur, $name, $val, $bal, $motif) { |
|
| 165 | - if (!preg_match($motif, $val)) { |
|
| 166 | - coordonnees_erreur($this, "<b>$val</b> " |
|
| 167 | - . _T('zxml_valeur_attribut') |
|
| 168 | - . " <b>$name</b> " |
|
| 169 | - . _T('zxml_de') |
|
| 170 | - . " <b>$bal</b> " |
|
| 171 | - . _T('zxml_non_conforme') |
|
| 172 | - . '</p><p>' |
|
| 173 | - . '<b>' . $motif . '</b>'); |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - // https://code.spip.net/@valider_idref |
|
| 178 | - public function valider_idref($nom, $ligne, $col) { |
|
| 179 | - if (!isset($this->ids[$nom])) { |
|
| 180 | - $this->err[] = [" <p><b>$nom</b> " . _T('zxml_inconnu_id'), $ligne, $col]; |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - // https://code.spip.net/@valider_passe2 |
|
| 185 | - public function valider_passe2() { |
|
| 186 | - if (!$this->err) { |
|
| 187 | - foreach ($this->idrefs as $idref) { |
|
| 188 | - [$nom, $ligne, $col] = $idref; |
|
| 189 | - $this->valider_idref($nom, $ligne, $col); |
|
| 190 | - } |
|
| 191 | - foreach ($this->idrefss as $idref) { |
|
| 192 | - [$noms, $ligne, $col] = $idref; |
|
| 193 | - foreach (preg_split('/\s+/', $noms) as $nom) { |
|
| 194 | - $this->valider_idref($nom, $ligne, $col); |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // https://code.spip.net/@debutElement |
|
| 201 | - public function debutElement($phraseur, $name, $attrs) { |
|
| 202 | - if ($this->dtc->elements) { |
|
| 203 | - $this->validerElement($phraseur, $name, $attrs); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - if ($f = $this->process['debut']) { |
|
| 207 | - $f($this, $name, $attrs); |
|
| 208 | - } |
|
| 209 | - $depth = $this->depth; |
|
| 210 | - $this->debuts[$depth] = strlen($this->res); |
|
| 211 | - foreach ($attrs as $k => $v) { |
|
| 212 | - $this->validerAttribut($phraseur, $k, $v, $name); |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - // https://code.spip.net/@finElement |
|
| 217 | - public function finElement($phraseur, $name) { |
|
| 218 | - $depth = $this->depth; |
|
| 219 | - $contenu = $this->contenu; |
|
| 220 | - |
|
| 221 | - $n = strlen($this->res); |
|
| 222 | - $c = strlen(trim($contenu[$depth])); |
|
| 223 | - $k = $this->debuts[$depth]; |
|
| 224 | - |
|
| 225 | - $regle = $this->dtc->regles[$name] ?? false; |
|
| 226 | - $vide = ($regle == 'EMPTY'); |
|
| 227 | - // controler que les balises devant etre vides le sont |
|
| 228 | - if ($vide) { |
|
| 229 | - if ($n <> ($k + $c)) { |
|
| 230 | - coordonnees_erreur($this, " <p><b>$name</b> " . _T('zxml_nonvide_balise')); |
|
| 231 | - } |
|
| 232 | - // pour les regles PCDATA ou iteration de disjonction, tout est fait |
|
| 233 | - } elseif ($regle and ($regle != '*')) { |
|
| 234 | - if ($regle == '+') { |
|
| 235 | - // iteration de disjonction non vide: 1 balise au - |
|
| 236 | - if ($n == $k) { |
|
| 237 | - coordonnees_erreur($this, "<p>\n<b>$name</b> " |
|
| 238 | - . _T('zxml_vide_balise')); |
|
| 239 | - } |
|
| 240 | - } else { |
|
| 241 | - $f = $this->fratrie[substr($depth, 2)] ?? null; |
|
| 242 | - if (is_null($f) or !preg_match($regle, $f)) { |
|
| 243 | - coordonnees_erreur( |
|
| 244 | - $this, |
|
| 245 | - " <p>\n<b>$name</b> " |
|
| 246 | - . _T('zxml_succession_fils_incorrecte') |
|
| 247 | - . ' : <b>' |
|
| 248 | - . $f |
|
| 249 | - . '</b>' |
|
| 250 | - ); |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - if ($f = $this->process['fin']) { |
|
| 255 | - $f($this, $name, $vide); |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - // https://code.spip.net/@textElement |
|
| 260 | - public function textElement($phraseur, $data) { |
|
| 261 | - if (trim($data)) { |
|
| 262 | - $d = $this->depth; |
|
| 263 | - $d = $this->ouvrant[$d]; |
|
| 264 | - preg_match('/^\s*(\S+)/', $d, $m); |
|
| 265 | - if (isset($this->dtc->pcdata[$m[1]]) and ($this->dtc->pcdata[$m[1]])) { |
|
| 266 | - coordonnees_erreur($this, ' <p><b>' . $m[1] . '</b> ' |
|
| 267 | - . _T('zxml_nonvide_balise')); // message a affiner |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - if ($f = $this->process['text']) { |
|
| 271 | - $f($this, $data); |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - public function piElement($phraseur, $target, $data) { |
|
| 276 | - if ($f = $this->process['pi']) { |
|
| 277 | - $f($this, $target, $data); |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - // Denonciation des entitees XML inconnues |
|
| 282 | - // Pour contourner le bug de conception de SAX qui ne signale pas si elles |
|
| 283 | - // sont dans un attribut, les entites les plus frequentes ont ete |
|
| 284 | - // transcodees au prealable (sauf & < > " que SAX traite correctement). |
|
| 285 | - // On ne les verra donc pas passer a cette etape, contrairement a ce que |
|
| 286 | - // le source de la page laisse legitimement supposer. |
|
| 287 | - |
|
| 288 | - // https://code.spip.net/@defautElement |
|
| 289 | - public function defaultElement($phraseur, $data) { |
|
| 290 | - if ( |
|
| 291 | - !preg_match('/^<!--/', $data) |
|
| 292 | - and (preg_match_all('/&([^;]*)?/', $data, $r, PREG_SET_ORDER)) |
|
| 293 | - ) { |
|
| 294 | - foreach ($r as $m) { |
|
| 295 | - [$t, $e] = $m; |
|
| 296 | - if (!isset($this->dtc->entites[$e])) { |
|
| 297 | - coordonnees_erreur($this, " <b>$e</b> " |
|
| 298 | - . _T('zxml_inconnu_entite') |
|
| 299 | - . ' '); |
|
| 300 | - } |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - if (isset($this->process['default']) and ($f = $this->process['default'])) { |
|
| 304 | - $f($this, $data); |
|
| 305 | - } |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - // https://code.spip.net/@phraserTout |
|
| 309 | - public function phraserTout($phraseur, $data) { |
|
| 310 | - xml_parsestring($this, $data); |
|
| 311 | - |
|
| 312 | - if (!$this->dtc or preg_match(',^' . _MESSAGE_DOCTYPE . ',', $data)) { |
|
| 313 | - $this->err[] = ['DOCTYPE ?', 0, 0]; |
|
| 314 | - } else { |
|
| 315 | - $this->valider_passe2(); |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Constructeur |
|
| 321 | - * |
|
| 322 | - * @param array $process ? |
|
| 323 | - **/ |
|
| 324 | - public function __construct($process = []) { |
|
| 325 | - if (is_array($process)) { |
|
| 326 | - $this->process = $process; |
|
| 327 | - } |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - public $ids = []; |
|
| 331 | - public $idrefs = []; |
|
| 332 | - public $idrefss = []; |
|
| 333 | - public $debuts = []; |
|
| 334 | - public $fratrie = []; |
|
| 335 | - |
|
| 336 | - public $dtc = null; |
|
| 337 | - public $sax = null; |
|
| 338 | - public $depth = ''; |
|
| 339 | - public $entete = ''; |
|
| 340 | - public $page = ''; |
|
| 341 | - public $res = ''; |
|
| 342 | - public $err = []; |
|
| 343 | - public $contenu = []; |
|
| 344 | - public $ouvrant = []; |
|
| 345 | - public $reperes = []; |
|
| 346 | - public $process = [ |
|
| 347 | - 'debut' => 'xml_debutElement', |
|
| 348 | - 'fin' => 'xml_finElement', |
|
| 349 | - 'text' => 'xml_textElement', |
|
| 350 | - 'pi' => 'xml_piElement', |
|
| 351 | - 'default' => 'xml_defaultElement' |
|
| 352 | - ]; |
|
| 26 | + // https://code.spip.net/@validerElement |
|
| 27 | + public function validerElement($phraseur, $name, $attrs) { |
|
| 28 | + if (!($p = isset($this->dtc->elements[$name]))) { |
|
| 29 | + if ($p = strpos($name, ':')) { |
|
| 30 | + $name = substr($name, $p + 1); |
|
| 31 | + $p = isset($this->dtc->elements[$name]); |
|
| 32 | + } |
|
| 33 | + if (!$p) { |
|
| 34 | + coordonnees_erreur($this, " <b>$name</b> : " |
|
| 35 | + . _T('zxml_inconnu_balise')); |
|
| 36 | + |
|
| 37 | + return; |
|
| 38 | + } |
|
| 39 | + } |
|
| 40 | + // controler les filles illegitimes, ca suffit |
|
| 41 | + $depth = $this->depth; |
|
| 42 | + $ouvrant = $this->ouvrant; |
|
| 43 | + #spip_log("trouve $name apres " . $ouvrant[$depth]); |
|
| 44 | + if (isset($ouvrant[$depth])) { |
|
| 45 | + if (preg_match('/^\s*(\w+)/', $ouvrant[$depth], $r)) { |
|
| 46 | + $pere = $r[1]; |
|
| 47 | + #spip_log("pere $pere"); |
|
| 48 | + if (isset($this->dtc->elements[$pere])) { |
|
| 49 | + $fils = $this->dtc->elements[$pere]; |
|
| 50 | + #spip_log("rejeton $name fils " . @join(',',$fils)); |
|
| 51 | + if (!($p = @in_array($name, $fils))) { |
|
| 52 | + if ($p = strpos($name, ':')) { |
|
| 53 | + $p = substr($name, $p + 1); |
|
| 54 | + $p = @in_array($p, $fils); |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + if (!$p) { |
|
| 58 | + $bons_peres = @join('</b>, <b>', $this->dtc->peres[$name]); |
|
| 59 | + coordonnees_erreur($this, " <b>$name</b> " |
|
| 60 | + . _T('zxml_non_fils') |
|
| 61 | + . ' <b>' |
|
| 62 | + . $pere |
|
| 63 | + . '</b>' |
|
| 64 | + . (!$bons_peres ? '' |
|
| 65 | + : ('<p style="font-size: 80%"> ' . _T('zxml_mais_de') . ' <b>' . $bons_peres . '</b></p>'))); |
|
| 66 | + } elseif ($this->dtc->regles[$pere][0] == '/') { |
|
| 67 | + $frat = substr($depth, 2); |
|
| 68 | + if (!isset($this->fratrie[$frat])) { |
|
| 69 | + $this->fratrie[$frat] = ''; |
|
| 70 | + } |
|
| 71 | + $this->fratrie[$frat] .= "$name "; |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + // Init de la suite des balises a memoriser si regle difficile |
|
| 77 | + if ($this->dtc->regles[$name] and $this->dtc->regles[$name][0] == '/') { |
|
| 78 | + $this->fratrie[$depth] = ''; |
|
| 79 | + } |
|
| 80 | + if (isset($this->dtc->attributs[$name])) { |
|
| 81 | + foreach ($this->dtc->attributs[$name] as $n => $v) { |
|
| 82 | + if (($v[1] == '#REQUIRED') and (!isset($attrs[$n]))) { |
|
| 83 | + coordonnees_erreur($this, " <b>$n</b>" |
|
| 84 | + . ' : ' |
|
| 85 | + . _T('zxml_obligatoire_attribut') |
|
| 86 | + . " <b>$name</b>"); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + // https://code.spip.net/@validerAttribut |
|
| 93 | + public function validerAttribut($phraseur, $name, $val, $bal) { |
|
| 94 | + // Si la balise est inconnue, eviter d'insister |
|
| 95 | + if (!isset($this->dtc->attributs[$bal])) { |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $a = $this->dtc->attributs[$bal]; |
|
| 100 | + if (!isset($a[$name])) { |
|
| 101 | + $bons = join(', ', array_keys($a)); |
|
| 102 | + if ($bons) { |
|
| 103 | + $bons = " title=' " . |
|
| 104 | + _T('zxml_connus_attributs') . |
|
| 105 | + ' : ' . |
|
| 106 | + $bons . |
|
| 107 | + "'"; |
|
| 108 | + } |
|
| 109 | + $bons .= " style='font-weight: bold'"; |
|
| 110 | + coordonnees_erreur($this, " <b>$name</b> " |
|
| 111 | + . _T('zxml_inconnu_attribut') . ' ' . _T('zxml_de') |
|
| 112 | + . " <a$bons>$bal</a> (" |
|
| 113 | + . _T('zxml_survoler') |
|
| 114 | + . ')'); |
|
| 115 | + } else { |
|
| 116 | + $type = $a[$name][0]; |
|
| 117 | + if (!preg_match('/^\w+$/', $type)) { |
|
| 118 | + $this->valider_motif($phraseur, $name, $val, $bal, $type); |
|
| 119 | + } else { |
|
| 120 | + if (method_exists($this, $f = 'validerAttribut_' . $type)) { |
|
| 121 | + $this->$f($phraseur, $name, $val, $bal); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + # else spip_log("$type type d'attribut inconnu"); |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + public function validerAttribut_NMTOKEN($phraseur, $name, $val, $bal) { |
|
| 129 | + $this->valider_motif($phraseur, $name, $val, $bal, _REGEXP_NMTOKEN); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + public function validerAttribut_NMTOKENS($phraseur, $name, $val, $bal) { |
|
| 133 | + $this->valider_motif($phraseur, $name, $val, $bal, _REGEXP_NMTOKENS); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // https://code.spip.net/@validerAttribut_ID |
|
| 137 | + public function validerAttribut_ID($phraseur, $name, $val, $bal) { |
|
| 138 | + if (isset($this->ids[$val])) { |
|
| 139 | + [$l, $c] = $this->ids[$val]; |
|
| 140 | + coordonnees_erreur($this, " <p><b>$val</b> " |
|
| 141 | + . _T('zxml_valeur_attribut') |
|
| 142 | + . " <b>$name</b> " |
|
| 143 | + . _T('zxml_de') |
|
| 144 | + . " <b>$bal</b> " |
|
| 145 | + . _T('zxml_vu') |
|
| 146 | + . " (L$l,C$c)"); |
|
| 147 | + } else { |
|
| 148 | + $this->valider_motif($phraseur, $name, $val, $bal, _REGEXP_ID); |
|
| 149 | + $this->ids[$val] = [xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur)]; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + // https://code.spip.net/@validerAttribut_IDREF |
|
| 154 | + public function validerAttribut_IDREF($phraseur, $name, $val, $bal) { |
|
| 155 | + $this->idrefs[] = [$val, xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur)]; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + // https://code.spip.net/@validerAttribut_IDREFS |
|
| 159 | + public function validerAttribut_IDREFS($phraseur, $name, $val, $bal) { |
|
| 160 | + $this->idrefss[] = [$val, xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur)]; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // https://code.spip.net/@valider_motif |
|
| 164 | + public function valider_motif($phraseur, $name, $val, $bal, $motif) { |
|
| 165 | + if (!preg_match($motif, $val)) { |
|
| 166 | + coordonnees_erreur($this, "<b>$val</b> " |
|
| 167 | + . _T('zxml_valeur_attribut') |
|
| 168 | + . " <b>$name</b> " |
|
| 169 | + . _T('zxml_de') |
|
| 170 | + . " <b>$bal</b> " |
|
| 171 | + . _T('zxml_non_conforme') |
|
| 172 | + . '</p><p>' |
|
| 173 | + . '<b>' . $motif . '</b>'); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + // https://code.spip.net/@valider_idref |
|
| 178 | + public function valider_idref($nom, $ligne, $col) { |
|
| 179 | + if (!isset($this->ids[$nom])) { |
|
| 180 | + $this->err[] = [" <p><b>$nom</b> " . _T('zxml_inconnu_id'), $ligne, $col]; |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + // https://code.spip.net/@valider_passe2 |
|
| 185 | + public function valider_passe2() { |
|
| 186 | + if (!$this->err) { |
|
| 187 | + foreach ($this->idrefs as $idref) { |
|
| 188 | + [$nom, $ligne, $col] = $idref; |
|
| 189 | + $this->valider_idref($nom, $ligne, $col); |
|
| 190 | + } |
|
| 191 | + foreach ($this->idrefss as $idref) { |
|
| 192 | + [$noms, $ligne, $col] = $idref; |
|
| 193 | + foreach (preg_split('/\s+/', $noms) as $nom) { |
|
| 194 | + $this->valider_idref($nom, $ligne, $col); |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // https://code.spip.net/@debutElement |
|
| 201 | + public function debutElement($phraseur, $name, $attrs) { |
|
| 202 | + if ($this->dtc->elements) { |
|
| 203 | + $this->validerElement($phraseur, $name, $attrs); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + if ($f = $this->process['debut']) { |
|
| 207 | + $f($this, $name, $attrs); |
|
| 208 | + } |
|
| 209 | + $depth = $this->depth; |
|
| 210 | + $this->debuts[$depth] = strlen($this->res); |
|
| 211 | + foreach ($attrs as $k => $v) { |
|
| 212 | + $this->validerAttribut($phraseur, $k, $v, $name); |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + // https://code.spip.net/@finElement |
|
| 217 | + public function finElement($phraseur, $name) { |
|
| 218 | + $depth = $this->depth; |
|
| 219 | + $contenu = $this->contenu; |
|
| 220 | + |
|
| 221 | + $n = strlen($this->res); |
|
| 222 | + $c = strlen(trim($contenu[$depth])); |
|
| 223 | + $k = $this->debuts[$depth]; |
|
| 224 | + |
|
| 225 | + $regle = $this->dtc->regles[$name] ?? false; |
|
| 226 | + $vide = ($regle == 'EMPTY'); |
|
| 227 | + // controler que les balises devant etre vides le sont |
|
| 228 | + if ($vide) { |
|
| 229 | + if ($n <> ($k + $c)) { |
|
| 230 | + coordonnees_erreur($this, " <p><b>$name</b> " . _T('zxml_nonvide_balise')); |
|
| 231 | + } |
|
| 232 | + // pour les regles PCDATA ou iteration de disjonction, tout est fait |
|
| 233 | + } elseif ($regle and ($regle != '*')) { |
|
| 234 | + if ($regle == '+') { |
|
| 235 | + // iteration de disjonction non vide: 1 balise au - |
|
| 236 | + if ($n == $k) { |
|
| 237 | + coordonnees_erreur($this, "<p>\n<b>$name</b> " |
|
| 238 | + . _T('zxml_vide_balise')); |
|
| 239 | + } |
|
| 240 | + } else { |
|
| 241 | + $f = $this->fratrie[substr($depth, 2)] ?? null; |
|
| 242 | + if (is_null($f) or !preg_match($regle, $f)) { |
|
| 243 | + coordonnees_erreur( |
|
| 244 | + $this, |
|
| 245 | + " <p>\n<b>$name</b> " |
|
| 246 | + . _T('zxml_succession_fils_incorrecte') |
|
| 247 | + . ' : <b>' |
|
| 248 | + . $f |
|
| 249 | + . '</b>' |
|
| 250 | + ); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + if ($f = $this->process['fin']) { |
|
| 255 | + $f($this, $name, $vide); |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + // https://code.spip.net/@textElement |
|
| 260 | + public function textElement($phraseur, $data) { |
|
| 261 | + if (trim($data)) { |
|
| 262 | + $d = $this->depth; |
|
| 263 | + $d = $this->ouvrant[$d]; |
|
| 264 | + preg_match('/^\s*(\S+)/', $d, $m); |
|
| 265 | + if (isset($this->dtc->pcdata[$m[1]]) and ($this->dtc->pcdata[$m[1]])) { |
|
| 266 | + coordonnees_erreur($this, ' <p><b>' . $m[1] . '</b> ' |
|
| 267 | + . _T('zxml_nonvide_balise')); // message a affiner |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + if ($f = $this->process['text']) { |
|
| 271 | + $f($this, $data); |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + public function piElement($phraseur, $target, $data) { |
|
| 276 | + if ($f = $this->process['pi']) { |
|
| 277 | + $f($this, $target, $data); |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + // Denonciation des entitees XML inconnues |
|
| 282 | + // Pour contourner le bug de conception de SAX qui ne signale pas si elles |
|
| 283 | + // sont dans un attribut, les entites les plus frequentes ont ete |
|
| 284 | + // transcodees au prealable (sauf & < > " que SAX traite correctement). |
|
| 285 | + // On ne les verra donc pas passer a cette etape, contrairement a ce que |
|
| 286 | + // le source de la page laisse legitimement supposer. |
|
| 287 | + |
|
| 288 | + // https://code.spip.net/@defautElement |
|
| 289 | + public function defaultElement($phraseur, $data) { |
|
| 290 | + if ( |
|
| 291 | + !preg_match('/^<!--/', $data) |
|
| 292 | + and (preg_match_all('/&([^;]*)?/', $data, $r, PREG_SET_ORDER)) |
|
| 293 | + ) { |
|
| 294 | + foreach ($r as $m) { |
|
| 295 | + [$t, $e] = $m; |
|
| 296 | + if (!isset($this->dtc->entites[$e])) { |
|
| 297 | + coordonnees_erreur($this, " <b>$e</b> " |
|
| 298 | + . _T('zxml_inconnu_entite') |
|
| 299 | + . ' '); |
|
| 300 | + } |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + if (isset($this->process['default']) and ($f = $this->process['default'])) { |
|
| 304 | + $f($this, $data); |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + // https://code.spip.net/@phraserTout |
|
| 309 | + public function phraserTout($phraseur, $data) { |
|
| 310 | + xml_parsestring($this, $data); |
|
| 311 | + |
|
| 312 | + if (!$this->dtc or preg_match(',^' . _MESSAGE_DOCTYPE . ',', $data)) { |
|
| 313 | + $this->err[] = ['DOCTYPE ?', 0, 0]; |
|
| 314 | + } else { |
|
| 315 | + $this->valider_passe2(); |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Constructeur |
|
| 321 | + * |
|
| 322 | + * @param array $process ? |
|
| 323 | + **/ |
|
| 324 | + public function __construct($process = []) { |
|
| 325 | + if (is_array($process)) { |
|
| 326 | + $this->process = $process; |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + public $ids = []; |
|
| 331 | + public $idrefs = []; |
|
| 332 | + public $idrefss = []; |
|
| 333 | + public $debuts = []; |
|
| 334 | + public $fratrie = []; |
|
| 335 | + |
|
| 336 | + public $dtc = null; |
|
| 337 | + public $sax = null; |
|
| 338 | + public $depth = ''; |
|
| 339 | + public $entete = ''; |
|
| 340 | + public $page = ''; |
|
| 341 | + public $res = ''; |
|
| 342 | + public $err = []; |
|
| 343 | + public $contenu = []; |
|
| 344 | + public $ouvrant = []; |
|
| 345 | + public $reperes = []; |
|
| 346 | + public $process = [ |
|
| 347 | + 'debut' => 'xml_debutElement', |
|
| 348 | + 'fin' => 'xml_finElement', |
|
| 349 | + 'text' => 'xml_textElement', |
|
| 350 | + 'pi' => 'xml_piElement', |
|
| 351 | + 'default' => 'xml_defaultElement' |
|
| 352 | + ]; |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | |
@@ -359,8 +359,8 @@ discard block |
||
| 359 | 359 | * |
| 360 | 360 | **/ |
| 361 | 361 | function xml_valider_dist($page, $apply = false, $process = false, $doctype = '', $charset = null) { |
| 362 | - $f = new ValidateurXML($process); |
|
| 363 | - $sax = charger_fonction('sax', 'xml'); |
|
| 362 | + $f = new ValidateurXML($process); |
|
| 363 | + $sax = charger_fonction('sax', 'xml'); |
|
| 364 | 364 | |
| 365 | - return $sax($page, $apply, $f, $doctype, $charset); |
|
| 365 | + return $sax($page, $apply, $f, $doctype, $charset); |
|
| 366 | 366 | } |
@@ -11,61 +11,61 @@ |
||
| 11 | 11 | \***************************************************************************/ |
| 12 | 12 | |
| 13 | 13 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 14 | - return; |
|
| 14 | + return; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | class IndenteurXML { |
| 18 | 18 | |
| 19 | - // https://code.spip.net/@debutElement |
|
| 20 | - public function debutElement($phraseur, $name, $attrs) { |
|
| 21 | - xml_debutElement($this, $name, $attrs); |
|
| 22 | - } |
|
| 19 | + // https://code.spip.net/@debutElement |
|
| 20 | + public function debutElement($phraseur, $name, $attrs) { |
|
| 21 | + xml_debutElement($this, $name, $attrs); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - // https://code.spip.net/@finElement |
|
| 25 | - public function finElement($phraseur, $name) { |
|
| 26 | - xml_finElement($this, $name); |
|
| 27 | - } |
|
| 24 | + // https://code.spip.net/@finElement |
|
| 25 | + public function finElement($phraseur, $name) { |
|
| 26 | + xml_finElement($this, $name); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - // https://code.spip.net/@textElement |
|
| 30 | - public function textElement($phraseur, $data) { |
|
| 31 | - xml_textElement($this, $data); |
|
| 32 | - } |
|
| 29 | + // https://code.spip.net/@textElement |
|
| 30 | + public function textElement($phraseur, $data) { |
|
| 31 | + xml_textElement($this, $data); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - public function piElement($phraseur, $target, $data) { |
|
| 35 | - xml_PiElement($this, $target, $data); |
|
| 36 | - } |
|
| 34 | + public function piElement($phraseur, $target, $data) { |
|
| 35 | + xml_PiElement($this, $target, $data); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - // https://code.spip.net/@defautElement |
|
| 39 | - public function defaultElement($phraseur, $data) { |
|
| 40 | - xml_defaultElement($this, $data); |
|
| 41 | - } |
|
| 38 | + // https://code.spip.net/@defautElement |
|
| 39 | + public function defaultElement($phraseur, $data) { |
|
| 40 | + xml_defaultElement($this, $data); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - // https://code.spip.net/@phraserTout |
|
| 44 | - public function phraserTout($phraseur, $data) { |
|
| 45 | - xml_parsestring($this, $data); |
|
| 46 | - } |
|
| 43 | + // https://code.spip.net/@phraserTout |
|
| 44 | + public function phraserTout($phraseur, $data) { |
|
| 45 | + xml_parsestring($this, $data); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - public $depth = ''; |
|
| 49 | - public $res = ''; |
|
| 50 | - public $err = []; |
|
| 51 | - public $contenu = []; |
|
| 52 | - public $ouvrant = []; |
|
| 53 | - public $reperes = []; |
|
| 54 | - public $entete = ''; |
|
| 55 | - public $page = ''; |
|
| 56 | - public $dtc = null; |
|
| 57 | - public $sax = null; |
|
| 48 | + public $depth = ''; |
|
| 49 | + public $res = ''; |
|
| 50 | + public $err = []; |
|
| 51 | + public $contenu = []; |
|
| 52 | + public $ouvrant = []; |
|
| 53 | + public $reperes = []; |
|
| 54 | + public $entete = ''; |
|
| 55 | + public $page = ''; |
|
| 56 | + public $dtc = null; |
|
| 57 | + public $sax = null; |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | // https://code.spip.net/@xml_indenter_dist |
| 61 | 61 | function xml_indenter_dist($page, $apply = false) { |
| 62 | - $sax = charger_fonction('sax', 'xml'); |
|
| 63 | - $f = new IndenteurXML(); |
|
| 64 | - $sax($page, $apply, $f); |
|
| 65 | - if (!$f->err) { |
|
| 66 | - return $f->entete . $f->res; |
|
| 67 | - } |
|
| 68 | - spip_log('indentation impossible ' . (is_countable($f->err) ? count($f->err) : 0) . ' erreurs de validation'); |
|
| 62 | + $sax = charger_fonction('sax', 'xml'); |
|
| 63 | + $f = new IndenteurXML(); |
|
| 64 | + $sax($page, $apply, $f); |
|
| 65 | + if (!$f->err) { |
|
| 66 | + return $f->entete . $f->res; |
|
| 67 | + } |
|
| 68 | + spip_log('indentation impossible ' . (is_countable($f->err) ? count($f->err) : 0) . ' erreurs de validation'); |
|
| 69 | 69 | |
| 70 | - return $f->entete . $f->page; |
|
| 70 | + return $f->entete . $f->page; |
|
| 71 | 71 | } |
@@ -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,229 +24,229 @@ 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 | // https://code.spip.net/@xml_debutElement |
| 43 | 43 | function xml_debutElement($phraseur, $name, $attrs) { |
| 44 | - $depth = $phraseur->depth; |
|
| 45 | - |
|
| 46 | - $t = $phraseur->ouvrant[$depth] ?? ' '; |
|
| 47 | - // espace initial signifie: deja integree au resultat |
|
| 48 | - if ($t[0] != ' ') { |
|
| 49 | - $phraseur->res .= '<' . $t . '>'; |
|
| 50 | - $phraseur->ouvrant[$depth] = ' ' . $t; |
|
| 51 | - } |
|
| 52 | - $t = $phraseur->contenu[$depth]; |
|
| 53 | - // n'indenter que s'il y a un separateur avant |
|
| 54 | - $phraseur->res .= preg_replace("/[\n\t ]+$/", "\n$depth", $t); |
|
| 55 | - $phraseur->contenu[$depth] = ''; |
|
| 56 | - $att = ''; |
|
| 57 | - $sep = ' '; |
|
| 58 | - foreach ($attrs as $k => $v) { |
|
| 59 | - $delim = strpos($v, "'") === false ? "'" : '"'; |
|
| 60 | - $val = xml_entites_html($v); |
|
| 61 | - $att .= $sep . $k . '=' . $delim |
|
| 62 | - . ($delim !== '"' ? str_replace('"', '"', $val) : $val) |
|
| 63 | - . $delim; |
|
| 64 | - $sep = "\n $depth"; |
|
| 65 | - } |
|
| 66 | - $phraseur->depth .= ' '; |
|
| 67 | - $phraseur->contenu[$phraseur->depth] = ''; |
|
| 68 | - $phraseur->ouvrant[$phraseur->depth] = $name . $att; |
|
| 69 | - $phraseur->reperes[$phraseur->depth] = xml_get_current_line_number($phraseur->sax); |
|
| 44 | + $depth = $phraseur->depth; |
|
| 45 | + |
|
| 46 | + $t = $phraseur->ouvrant[$depth] ?? ' '; |
|
| 47 | + // espace initial signifie: deja integree au resultat |
|
| 48 | + if ($t[0] != ' ') { |
|
| 49 | + $phraseur->res .= '<' . $t . '>'; |
|
| 50 | + $phraseur->ouvrant[$depth] = ' ' . $t; |
|
| 51 | + } |
|
| 52 | + $t = $phraseur->contenu[$depth]; |
|
| 53 | + // n'indenter que s'il y a un separateur avant |
|
| 54 | + $phraseur->res .= preg_replace("/[\n\t ]+$/", "\n$depth", $t); |
|
| 55 | + $phraseur->contenu[$depth] = ''; |
|
| 56 | + $att = ''; |
|
| 57 | + $sep = ' '; |
|
| 58 | + foreach ($attrs as $k => $v) { |
|
| 59 | + $delim = strpos($v, "'") === false ? "'" : '"'; |
|
| 60 | + $val = xml_entites_html($v); |
|
| 61 | + $att .= $sep . $k . '=' . $delim |
|
| 62 | + . ($delim !== '"' ? str_replace('"', '"', $val) : $val) |
|
| 63 | + . $delim; |
|
| 64 | + $sep = "\n $depth"; |
|
| 65 | + } |
|
| 66 | + $phraseur->depth .= ' '; |
|
| 67 | + $phraseur->contenu[$phraseur->depth] = ''; |
|
| 68 | + $phraseur->ouvrant[$phraseur->depth] = $name . $att; |
|
| 69 | + $phraseur->reperes[$phraseur->depth] = xml_get_current_line_number($phraseur->sax); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | // https://code.spip.net/@xml_finElement |
| 73 | 73 | function xml_finElement($phraseur, $name, $fusion_bal = false) { |
| 74 | - $ouv = $phraseur->ouvrant[$phraseur->depth]; |
|
| 75 | - |
|
| 76 | - if ($ouv[0] != ' ') { |
|
| 77 | - $phraseur->ouvrant[$phraseur->depth] = ' ' . $ouv; |
|
| 78 | - } else { |
|
| 79 | - $ouv = ''; |
|
| 80 | - } |
|
| 81 | - $t = $phraseur->contenu[$phraseur->depth]; |
|
| 82 | - $phraseur->depth = substr($phraseur->depth, 2); |
|
| 83 | - $t = preg_replace("/[\n\t ]+$/", "\n" . $phraseur->depth, $t); |
|
| 84 | - |
|
| 85 | - // fusion <balise></balise> en <balise />. |
|
| 86 | - // ATTENTION, certains clients http croient que fusion ==> pas d'atttributs |
|
| 87 | - // en particulier pour les balises Script et A. |
|
| 88 | - // en presence d'attributs ne le faire que si la DTD est dispo et d'accord |
|
| 89 | - // (param fusion_bal) |
|
| 90 | - |
|
| 91 | - if ($t || (($ouv != $name) and !$fusion_bal)) { |
|
| 92 | - $phraseur->res .= ($ouv ? ('<' . $ouv . '>') : '') . $t . '</' . $name . '>'; |
|
| 93 | - } else { |
|
| 94 | - $phraseur->res .= ($ouv ? ('<' . $ouv . ' />') : ('</' . $name . '>')); |
|
| 95 | - } |
|
| 74 | + $ouv = $phraseur->ouvrant[$phraseur->depth]; |
|
| 75 | + |
|
| 76 | + if ($ouv[0] != ' ') { |
|
| 77 | + $phraseur->ouvrant[$phraseur->depth] = ' ' . $ouv; |
|
| 78 | + } else { |
|
| 79 | + $ouv = ''; |
|
| 80 | + } |
|
| 81 | + $t = $phraseur->contenu[$phraseur->depth]; |
|
| 82 | + $phraseur->depth = substr($phraseur->depth, 2); |
|
| 83 | + $t = preg_replace("/[\n\t ]+$/", "\n" . $phraseur->depth, $t); |
|
| 84 | + |
|
| 85 | + // fusion <balise></balise> en <balise />. |
|
| 86 | + // ATTENTION, certains clients http croient que fusion ==> pas d'atttributs |
|
| 87 | + // en particulier pour les balises Script et A. |
|
| 88 | + // en presence d'attributs ne le faire que si la DTD est dispo et d'accord |
|
| 89 | + // (param fusion_bal) |
|
| 90 | + |
|
| 91 | + if ($t || (($ouv != $name) and !$fusion_bal)) { |
|
| 92 | + $phraseur->res .= ($ouv ? ('<' . $ouv . '>') : '') . $t . '</' . $name . '>'; |
|
| 93 | + } else { |
|
| 94 | + $phraseur->res .= ($ouv ? ('<' . $ouv . ' />') : ('</' . $name . '>')); |
|
| 95 | + } |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | // https://code.spip.net/@xml_textElement |
| 99 | 99 | function xml_textElement($phraseur, $data) { |
| 100 | - $depth = $phraseur->depth; |
|
| 101 | - $phraseur->contenu[$depth] .= preg_match('/^script/', $phraseur->ouvrant[$depth]) |
|
| 102 | - ? $data |
|
| 103 | - : xml_entites_html($data); |
|
| 100 | + $depth = $phraseur->depth; |
|
| 101 | + $phraseur->contenu[$depth] .= preg_match('/^script/', $phraseur->ouvrant[$depth]) |
|
| 102 | + ? $data |
|
| 103 | + : xml_entites_html($data); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | function xml_piElement($phraseur, $target, $data) { |
| 107 | - $depth = $phraseur->depth; |
|
| 108 | - |
|
| 109 | - if (strtolower($target) != 'php') { |
|
| 110 | - $phraseur->contenu[$depth] .= $data; |
|
| 111 | - } else { |
|
| 112 | - ob_start(); |
|
| 113 | - eval($data); |
|
| 114 | - $data = ob_get_contents(); |
|
| 115 | - ob_end_clean(); |
|
| 116 | - $phraseur->contenu[$depth] .= $data; |
|
| 117 | - } |
|
| 107 | + $depth = $phraseur->depth; |
|
| 108 | + |
|
| 109 | + if (strtolower($target) != 'php') { |
|
| 110 | + $phraseur->contenu[$depth] .= $data; |
|
| 111 | + } else { |
|
| 112 | + ob_start(); |
|
| 113 | + eval($data); |
|
| 114 | + $data = ob_get_contents(); |
|
| 115 | + ob_end_clean(); |
|
| 116 | + $phraseur->contenu[$depth] .= $data; |
|
| 117 | + } |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | |
| 121 | 121 | // https://code.spip.net/@xml_defautElement |
| 122 | 122 | function xml_defaultElement($phraseur, $data) { |
| 123 | - $depth = $phraseur->depth; |
|
| 123 | + $depth = $phraseur->depth; |
|
| 124 | 124 | |
| 125 | - if (!isset($phraseur->contenu[$depth])) { |
|
| 126 | - $phraseur->contenu[$depth] = ''; |
|
| 127 | - } |
|
| 128 | - $phraseur->contenu[$depth] .= $data; |
|
| 125 | + if (!isset($phraseur->contenu[$depth])) { |
|
| 126 | + $phraseur->contenu[$depth] = ''; |
|
| 127 | + } |
|
| 128 | + $phraseur->contenu[$depth] .= $data; |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // https://code.spip.net/@xml_parsestring |
| 132 | 132 | function xml_parsestring($phraseur, $data) { |
| 133 | - $phraseur->contenu[$phraseur->depth] = ''; |
|
| 134 | - |
|
| 135 | - if (!xml_parse($phraseur->sax, $data, true)) { |
|
| 136 | - coordonnees_erreur( |
|
| 137 | - $phraseur, |
|
| 138 | - xml_error_string(xml_get_error_code($phraseur->sax)) |
|
| 139 | - . "<br />\n" . |
|
| 140 | - (!$phraseur->depth ? '' : |
|
| 141 | - ('(' . |
|
| 142 | - _T('erreur_balise_non_fermee') . |
|
| 143 | - ' <tt>' . |
|
| 144 | - $phraseur->ouvrant[$phraseur->depth] . |
|
| 145 | - '</tt> ' . |
|
| 146 | - _T('ligne') . |
|
| 147 | - ' ' . |
|
| 148 | - $phraseur->reperes[$phraseur->depth] . |
|
| 149 | - ") <br />\n")) |
|
| 150 | - ); |
|
| 151 | - } |
|
| 133 | + $phraseur->contenu[$phraseur->depth] = ''; |
|
| 134 | + |
|
| 135 | + if (!xml_parse($phraseur->sax, $data, true)) { |
|
| 136 | + coordonnees_erreur( |
|
| 137 | + $phraseur, |
|
| 138 | + xml_error_string(xml_get_error_code($phraseur->sax)) |
|
| 139 | + . "<br />\n" . |
|
| 140 | + (!$phraseur->depth ? '' : |
|
| 141 | + ('(' . |
|
| 142 | + _T('erreur_balise_non_fermee') . |
|
| 143 | + ' <tt>' . |
|
| 144 | + $phraseur->ouvrant[$phraseur->depth] . |
|
| 145 | + '</tt> ' . |
|
| 146 | + _T('ligne') . |
|
| 147 | + ' ' . |
|
| 148 | + $phraseur->reperes[$phraseur->depth] . |
|
| 149 | + ") <br />\n")) |
|
| 150 | + ); |
|
| 151 | + } |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | // https://code.spip.net/@coordonnees_erreur |
| 155 | 155 | function coordonnees_erreur($phraseur, $msg) { |
| 156 | - $entete_length = substr_count($phraseur->entete, "\n"); |
|
| 157 | - $phraseur->err[] = [ |
|
| 158 | - $msg, |
|
| 159 | - xml_get_current_line_number($phraseur->sax) + $entete_length, |
|
| 160 | - xml_get_current_column_number($phraseur->sax) |
|
| 161 | - ]; |
|
| 156 | + $entete_length = substr_count($phraseur->entete, "\n"); |
|
| 157 | + $phraseur->err[] = [ |
|
| 158 | + $msg, |
|
| 159 | + xml_get_current_line_number($phraseur->sax) + $entete_length, |
|
| 160 | + xml_get_current_column_number($phraseur->sax) |
|
| 161 | + ]; |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | // https://code.spip.net/@xml_sax_dist |
| 165 | 165 | function xml_sax_dist($page, $apply = false, $phraseur = null, $doctype = '', $charset = null) { |
| 166 | - if (is_null($charset)) { |
|
| 167 | - $charset = $GLOBALS['meta']['charset']; |
|
| 168 | - } |
|
| 169 | - if ($apply) { |
|
| 170 | - ob_start(); |
|
| 171 | - if (is_array($apply)) { |
|
| 172 | - $r = call_user_func_array($page, $apply); |
|
| 173 | - } else { |
|
| 174 | - $r = $page(); |
|
| 175 | - } |
|
| 176 | - $page = ob_get_contents(); |
|
| 177 | - ob_end_clean(); |
|
| 178 | - // fonction sans aucun "echo", ca doit etre le resultat |
|
| 179 | - if (!$page) { |
|
| 180 | - $page = $r; |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - if (!$page) { |
|
| 185 | - return ''; |
|
| 186 | - } |
|
| 187 | - // charger la DTD et transcoder les entites, |
|
| 188 | - // et escamoter le doctype que sax mange en php5 mais pas en php4 |
|
| 189 | - if (!$doctype) { |
|
| 190 | - if (!$r = analyser_doctype($page)) { |
|
| 191 | - $page = _MESSAGE_DOCTYPE . _DOCTYPE_ECRIRE |
|
| 192 | - . preg_replace(_REGEXP_DOCTYPE, '', $page); |
|
| 193 | - $r = analyser_doctype($page); |
|
| 194 | - } |
|
| 195 | - [$entete, $avail, $grammaire, $rotlvl] = array_pad($r, 4, null); |
|
| 196 | - $page = substr($page, strlen($entete)); |
|
| 197 | - } else { |
|
| 198 | - $avail = 'SYSTEM'; |
|
| 199 | - $grammaire = $doctype; |
|
| 200 | - $rotlvl = basename($grammaire); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - include_spip('xml/analyser_dtd'); |
|
| 204 | - $dtc = charger_dtd($grammaire, $avail, $rotlvl); |
|
| 205 | - $page = sax_bug($page, $dtc, $charset); |
|
| 206 | - |
|
| 207 | - // compatibilite Tidy espace public |
|
| 208 | - if (!$phraseur) { |
|
| 209 | - $indenter_xml = charger_fonction('indenter', 'xml'); |
|
| 210 | - |
|
| 211 | - return $indenter_xml($page, $apply); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $xml_parser = xml_parser_create($charset); |
|
| 215 | - |
|
| 216 | - xml_set_element_handler( |
|
| 217 | - $xml_parser, |
|
| 218 | - [$phraseur, 'debutElement'], |
|
| 219 | - [$phraseur, 'finElement'] |
|
| 220 | - ); |
|
| 221 | - |
|
| 222 | - xml_set_character_data_handler( |
|
| 223 | - $xml_parser, |
|
| 224 | - [$phraseur, 'textElement'] |
|
| 225 | - ); |
|
| 226 | - |
|
| 227 | - xml_set_processing_instruction_handler( |
|
| 228 | - $xml_parser, |
|
| 229 | - [$phraseur, 'piElement'] |
|
| 230 | - ); |
|
| 231 | - |
|
| 232 | - xml_set_default_handler( |
|
| 233 | - $xml_parser, |
|
| 234 | - [$phraseur, 'defaultElement'] |
|
| 235 | - ); |
|
| 236 | - |
|
| 237 | - xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); |
|
| 238 | - |
|
| 239 | - $phraseur->sax = $xml_parser; |
|
| 240 | - if (isset($entete)) { |
|
| 241 | - $phraseur->entete = $entete; |
|
| 242 | - } |
|
| 243 | - $phraseur->page = $page; |
|
| 244 | - $phraseur->dtc = $dtc; |
|
| 245 | - $phraseur->phraserTout($xml_parser, $page); |
|
| 246 | - xml_parser_free($xml_parser); |
|
| 247 | - $phraseur->sax = ''; |
|
| 248 | - |
|
| 249 | - return $phraseur; |
|
| 166 | + if (is_null($charset)) { |
|
| 167 | + $charset = $GLOBALS['meta']['charset']; |
|
| 168 | + } |
|
| 169 | + if ($apply) { |
|
| 170 | + ob_start(); |
|
| 171 | + if (is_array($apply)) { |
|
| 172 | + $r = call_user_func_array($page, $apply); |
|
| 173 | + } else { |
|
| 174 | + $r = $page(); |
|
| 175 | + } |
|
| 176 | + $page = ob_get_contents(); |
|
| 177 | + ob_end_clean(); |
|
| 178 | + // fonction sans aucun "echo", ca doit etre le resultat |
|
| 179 | + if (!$page) { |
|
| 180 | + $page = $r; |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + if (!$page) { |
|
| 185 | + return ''; |
|
| 186 | + } |
|
| 187 | + // charger la DTD et transcoder les entites, |
|
| 188 | + // et escamoter le doctype que sax mange en php5 mais pas en php4 |
|
| 189 | + if (!$doctype) { |
|
| 190 | + if (!$r = analyser_doctype($page)) { |
|
| 191 | + $page = _MESSAGE_DOCTYPE . _DOCTYPE_ECRIRE |
|
| 192 | + . preg_replace(_REGEXP_DOCTYPE, '', $page); |
|
| 193 | + $r = analyser_doctype($page); |
|
| 194 | + } |
|
| 195 | + [$entete, $avail, $grammaire, $rotlvl] = array_pad($r, 4, null); |
|
| 196 | + $page = substr($page, strlen($entete)); |
|
| 197 | + } else { |
|
| 198 | + $avail = 'SYSTEM'; |
|
| 199 | + $grammaire = $doctype; |
|
| 200 | + $rotlvl = basename($grammaire); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + include_spip('xml/analyser_dtd'); |
|
| 204 | + $dtc = charger_dtd($grammaire, $avail, $rotlvl); |
|
| 205 | + $page = sax_bug($page, $dtc, $charset); |
|
| 206 | + |
|
| 207 | + // compatibilite Tidy espace public |
|
| 208 | + if (!$phraseur) { |
|
| 209 | + $indenter_xml = charger_fonction('indenter', 'xml'); |
|
| 210 | + |
|
| 211 | + return $indenter_xml($page, $apply); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $xml_parser = xml_parser_create($charset); |
|
| 215 | + |
|
| 216 | + xml_set_element_handler( |
|
| 217 | + $xml_parser, |
|
| 218 | + [$phraseur, 'debutElement'], |
|
| 219 | + [$phraseur, 'finElement'] |
|
| 220 | + ); |
|
| 221 | + |
|
| 222 | + xml_set_character_data_handler( |
|
| 223 | + $xml_parser, |
|
| 224 | + [$phraseur, 'textElement'] |
|
| 225 | + ); |
|
| 226 | + |
|
| 227 | + xml_set_processing_instruction_handler( |
|
| 228 | + $xml_parser, |
|
| 229 | + [$phraseur, 'piElement'] |
|
| 230 | + ); |
|
| 231 | + |
|
| 232 | + xml_set_default_handler( |
|
| 233 | + $xml_parser, |
|
| 234 | + [$phraseur, 'defaultElement'] |
|
| 235 | + ); |
|
| 236 | + |
|
| 237 | + xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); |
|
| 238 | + |
|
| 239 | + $phraseur->sax = $xml_parser; |
|
| 240 | + if (isset($entete)) { |
|
| 241 | + $phraseur->entete = $entete; |
|
| 242 | + } |
|
| 243 | + $phraseur->page = $page; |
|
| 244 | + $phraseur->dtc = $dtc; |
|
| 245 | + $phraseur->phraserTout($xml_parser, $page); |
|
| 246 | + xml_parser_free($xml_parser); |
|
| 247 | + $phraseur->sax = ''; |
|
| 248 | + |
|
| 249 | + return $phraseur; |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | // SAX ne dit pas si une Entite est dans un attribut ou non. |
@@ -258,24 +258,24 @@ discard block |
||
| 258 | 258 | |
| 259 | 259 | // https://code.spip.net/@sax_bug |
| 260 | 260 | function sax_bug($data, $dtc, $charset = null) { |
| 261 | - if (is_null($charset)) { |
|
| 262 | - $charset = $GLOBALS['meta']['charset']; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - if ($dtc) { |
|
| 266 | - $trans = []; |
|
| 267 | - |
|
| 268 | - foreach ($dtc->entites as $k => $v) { |
|
| 269 | - if (!strpos(' amp lt gt quot ', (string) $k)) { |
|
| 270 | - $trans["&$k;"] = $v; |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - $data = strtr($data, $trans); |
|
| 274 | - } else { |
|
| 275 | - $data = html2unicode($data, true); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - return unicode2charset($data, $charset); |
|
| 261 | + if (is_null($charset)) { |
|
| 262 | + $charset = $GLOBALS['meta']['charset']; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + if ($dtc) { |
|
| 266 | + $trans = []; |
|
| 267 | + |
|
| 268 | + foreach ($dtc->entites as $k => $v) { |
|
| 269 | + if (!strpos(' amp lt gt quot ', (string) $k)) { |
|
| 270 | + $trans["&$k;"] = $v; |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + $data = strtr($data, $trans); |
|
| 274 | + } else { |
|
| 275 | + $data = html2unicode($data, true); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + return unicode2charset($data, $charset); |
|
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | // Retirer < ? xml... ? > et autre PI, ainsi que les commentaires en debut |
@@ -286,52 +286,52 @@ discard block |
||
| 286 | 286 | // mais un XML Schema que SPIP ne fait pas encore lire. |
| 287 | 287 | // https://code.spip.net/@analyser_doctype |
| 288 | 288 | function analyser_doctype($data) { |
| 289 | - if (!preg_match(_REGEXP_DOCTYPE, $data, $page)) { |
|
| 290 | - if (preg_match(_REGEXP_XML, $data, $page)) { |
|
| 291 | - [, $entete, $topelement] = $page; |
|
| 292 | - if ($topelement == 'rss') { |
|
| 293 | - return [ |
|
| 294 | - $entete, |
|
| 295 | - 'PUBLIC', |
|
| 296 | - _DOCTYPE_RSS, |
|
| 297 | - 'rss-0.91.dtd' |
|
| 298 | - ]; |
|
| 299 | - } else { |
|
| 300 | - $dtd = $topelement . '.dtd'; |
|
| 301 | - $f = find_in_path($dtd); |
|
| 302 | - if (file_exists($f)) { |
|
| 303 | - return [$entete, 'SYSTEM', $f, $dtd]; |
|
| 304 | - } |
|
| 305 | - } |
|
| 306 | - } |
|
| 307 | - spip_log('Dtd pas vu pour ' . substr($data, 0, 100)); |
|
| 308 | - |
|
| 309 | - return []; |
|
| 310 | - } |
|
| 311 | - [$entete, , $topelement, $avail, $suite] = $page; |
|
| 312 | - |
|
| 313 | - if (!preg_match('/^"([^"]*)"\s*(.*)$/', $suite, $r)) { |
|
| 314 | - if (!preg_match("/^'([^']*)'\s*(.*)$/", $suite, $r)) { |
|
| 315 | - return []; |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - [, $rotlvl, $suite] = $r; |
|
| 319 | - |
|
| 320 | - if (!$suite) { |
|
| 321 | - if ($avail != 'SYSTEM') { |
|
| 322 | - return []; |
|
| 323 | - } |
|
| 324 | - $grammaire = $rotlvl; |
|
| 325 | - $rotlvl = ''; |
|
| 326 | - } else { |
|
| 327 | - if (!preg_match('/^"([^"]*)"\s*$/', $suite, $r)) { |
|
| 328 | - if (!preg_match("/^'([^']*)'\s*$/", $suite, $r)) { |
|
| 329 | - return []; |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - $grammaire = $r[1]; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - return [$entete, $avail, $grammaire, $rotlvl]; |
|
| 289 | + if (!preg_match(_REGEXP_DOCTYPE, $data, $page)) { |
|
| 290 | + if (preg_match(_REGEXP_XML, $data, $page)) { |
|
| 291 | + [, $entete, $topelement] = $page; |
|
| 292 | + if ($topelement == 'rss') { |
|
| 293 | + return [ |
|
| 294 | + $entete, |
|
| 295 | + 'PUBLIC', |
|
| 296 | + _DOCTYPE_RSS, |
|
| 297 | + 'rss-0.91.dtd' |
|
| 298 | + ]; |
|
| 299 | + } else { |
|
| 300 | + $dtd = $topelement . '.dtd'; |
|
| 301 | + $f = find_in_path($dtd); |
|
| 302 | + if (file_exists($f)) { |
|
| 303 | + return [$entete, 'SYSTEM', $f, $dtd]; |
|
| 304 | + } |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | + spip_log('Dtd pas vu pour ' . substr($data, 0, 100)); |
|
| 308 | + |
|
| 309 | + return []; |
|
| 310 | + } |
|
| 311 | + [$entete, , $topelement, $avail, $suite] = $page; |
|
| 312 | + |
|
| 313 | + if (!preg_match('/^"([^"]*)"\s*(.*)$/', $suite, $r)) { |
|
| 314 | + if (!preg_match("/^'([^']*)'\s*(.*)$/", $suite, $r)) { |
|
| 315 | + return []; |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + [, $rotlvl, $suite] = $r; |
|
| 319 | + |
|
| 320 | + if (!$suite) { |
|
| 321 | + if ($avail != 'SYSTEM') { |
|
| 322 | + return []; |
|
| 323 | + } |
|
| 324 | + $grammaire = $rotlvl; |
|
| 325 | + $rotlvl = ''; |
|
| 326 | + } else { |
|
| 327 | + if (!preg_match('/^"([^"]*)"\s*$/', $suite, $r)) { |
|
| 328 | + if (!preg_match("/^'([^']*)'\s*$/", $suite, $r)) { |
|
| 329 | + return []; |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + $grammaire = $r[1]; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + return [$entete, $avail, $grammaire, $rotlvl]; |
|
| 337 | 337 | } |
@@ -11,59 +11,59 @@ 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('xml/interfaces'); |
| 18 | 18 | |
| 19 | 19 | // https://code.spip.net/@charger_dtd |
| 20 | 20 | function charger_dtd($grammaire, $avail, $rotlvl) { |
| 21 | - $r = null; |
|
| 22 | - static $dtd = []; # cache bien utile pour le validateur en boucle |
|
| 23 | - |
|
| 24 | - if (isset($dtd[$grammaire])) { |
|
| 25 | - return $dtd[$grammaire]; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - if ($avail == 'SYSTEM') { |
|
| 29 | - $grammaire = find_in_path($grammaire); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - $file = _DIR_CACHE_XML . preg_replace('/[^\w.]/', '_', $rotlvl) . '.gz'; |
|
| 33 | - |
|
| 34 | - if (lire_fichier($file, $r)) { |
|
| 35 | - if (!$grammaire) { |
|
| 36 | - return []; |
|
| 37 | - } |
|
| 38 | - if (($avail == 'SYSTEM') and filemtime($file) < filemtime($grammaire)) { |
|
| 39 | - $r = false; |
|
| 40 | - } |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - if ($r) { |
|
| 44 | - $dtc = unserialize($r); |
|
| 45 | - } else { |
|
| 46 | - spip_timer('dtd'); |
|
| 47 | - $dtc = new DTC(); |
|
| 48 | - // L'analyseur retourne un booleen de reussite et modifie $dtc. |
|
| 49 | - // Retourner vide en cas d'echec |
|
| 50 | - if (!analyser_dtd($grammaire, $avail, $dtc)) { |
|
| 51 | - $dtc = []; |
|
| 52 | - } else { |
|
| 53 | - // tri final pour presenter les suggestions de corrections |
|
| 54 | - foreach ($dtc->peres as $k => $v) { |
|
| 55 | - asort($v); |
|
| 56 | - $dtc->peres[$k] = $v; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - spip_log("Analyser DTD $avail $grammaire (" . spip_timer('dtd') . ') ' . (is_countable($dtc->macros) ? count($dtc->macros) : 0) . ' macros, ' . (is_countable($dtc->elements) ? count($dtc->elements) : 0) . ' elements, ' . (is_countable($dtc->attributs) ? count($dtc->attributs) : 0) . " listes d'attributs, " . (is_countable($dtc->entites) ? count($dtc->entites) : 0) . ' entites'); |
|
| 60 | - # $r = $dtc->regles; ksort($r);foreach($r as $l => $v) {$t=array_keys($dtc->attributs[$l]);echo "<b>$l</b> '$v' ", count($t), " attributs: ", join (', ',$t);$t=$dtc->peres[$l];echo "<br />",count($t), " peres: ", @join (', ',$t), "<br />\n";}exit; |
|
| 61 | - ecrire_fichier($file, serialize($dtc), true); |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - $dtd[$grammaire] = $dtc; |
|
| 65 | - |
|
| 66 | - return $dtc; |
|
| 21 | + $r = null; |
|
| 22 | + static $dtd = []; # cache bien utile pour le validateur en boucle |
|
| 23 | + |
|
| 24 | + if (isset($dtd[$grammaire])) { |
|
| 25 | + return $dtd[$grammaire]; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + if ($avail == 'SYSTEM') { |
|
| 29 | + $grammaire = find_in_path($grammaire); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + $file = _DIR_CACHE_XML . preg_replace('/[^\w.]/', '_', $rotlvl) . '.gz'; |
|
| 33 | + |
|
| 34 | + if (lire_fichier($file, $r)) { |
|
| 35 | + if (!$grammaire) { |
|
| 36 | + return []; |
|
| 37 | + } |
|
| 38 | + if (($avail == 'SYSTEM') and filemtime($file) < filemtime($grammaire)) { |
|
| 39 | + $r = false; |
|
| 40 | + } |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + if ($r) { |
|
| 44 | + $dtc = unserialize($r); |
|
| 45 | + } else { |
|
| 46 | + spip_timer('dtd'); |
|
| 47 | + $dtc = new DTC(); |
|
| 48 | + // L'analyseur retourne un booleen de reussite et modifie $dtc. |
|
| 49 | + // Retourner vide en cas d'echec |
|
| 50 | + if (!analyser_dtd($grammaire, $avail, $dtc)) { |
|
| 51 | + $dtc = []; |
|
| 52 | + } else { |
|
| 53 | + // tri final pour presenter les suggestions de corrections |
|
| 54 | + foreach ($dtc->peres as $k => $v) { |
|
| 55 | + asort($v); |
|
| 56 | + $dtc->peres[$k] = $v; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + spip_log("Analyser DTD $avail $grammaire (" . spip_timer('dtd') . ') ' . (is_countable($dtc->macros) ? count($dtc->macros) : 0) . ' macros, ' . (is_countable($dtc->elements) ? count($dtc->elements) : 0) . ' elements, ' . (is_countable($dtc->attributs) ? count($dtc->attributs) : 0) . " listes d'attributs, " . (is_countable($dtc->entites) ? count($dtc->entites) : 0) . ' entites'); |
|
| 60 | + # $r = $dtc->regles; ksort($r);foreach($r as $l => $v) {$t=array_keys($dtc->attributs[$l]);echo "<b>$l</b> '$v' ", count($t), " attributs: ", join (', ',$t);$t=$dtc->peres[$l];echo "<br />",count($t), " peres: ", @join (', ',$t), "<br />\n";}exit; |
|
| 61 | + ecrire_fichier($file, serialize($dtc), true); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + $dtd[$grammaire] = $dtc; |
|
| 65 | + |
|
| 66 | + return $dtc; |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | // Compiler une regle de production en une Regexp qu'on appliquera sur la |
@@ -76,157 +76,157 @@ discard block |
||
| 76 | 76 | |
| 77 | 77 | // https://code.spip.net/@compilerRegle |
| 78 | 78 | function compilerRegle($val) { |
| 79 | - $x = str_replace( |
|
| 80 | - '()', |
|
| 81 | - '', |
|
| 82 | - preg_replace( |
|
| 83 | - '/\s*,\s*/', |
|
| 84 | - '', |
|
| 85 | - preg_replace( |
|
| 86 | - '/(\w+)\s*/', |
|
| 87 | - '(?:\1 )', |
|
| 88 | - preg_replace( |
|
| 89 | - '/\s*\)/', |
|
| 90 | - ')', |
|
| 91 | - preg_replace( |
|
| 92 | - '/\s*([(+*|?])\s*/', |
|
| 93 | - '\1', |
|
| 94 | - preg_replace('/\s*#\w+\s*[,|]?\s*/', '', $val) |
|
| 95 | - ) |
|
| 96 | - ) |
|
| 97 | - ) |
|
| 98 | - ) |
|
| 99 | - ); |
|
| 100 | - |
|
| 101 | - return $x; |
|
| 79 | + $x = str_replace( |
|
| 80 | + '()', |
|
| 81 | + '', |
|
| 82 | + preg_replace( |
|
| 83 | + '/\s*,\s*/', |
|
| 84 | + '', |
|
| 85 | + preg_replace( |
|
| 86 | + '/(\w+)\s*/', |
|
| 87 | + '(?:\1 )', |
|
| 88 | + preg_replace( |
|
| 89 | + '/\s*\)/', |
|
| 90 | + ')', |
|
| 91 | + preg_replace( |
|
| 92 | + '/\s*([(+*|?])\s*/', |
|
| 93 | + '\1', |
|
| 94 | + preg_replace('/\s*#\w+\s*[,|]?\s*/', '', $val) |
|
| 95 | + ) |
|
| 96 | + ) |
|
| 97 | + ) |
|
| 98 | + ) |
|
| 99 | + ); |
|
| 100 | + |
|
| 101 | + return $x; |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | |
| 105 | 105 | // https://code.spip.net/@analyser_dtd |
| 106 | 106 | function analyser_dtd($loc, $avail, &$dtc) { |
| 107 | - // creer le repertoire de cache si ce n'est fait |
|
| 108 | - // (utile aussi pour le resultat de la compil) |
|
| 109 | - $file = sous_repertoire(_DIR_CACHE_XML); |
|
| 110 | - // si DTD locale, ignorer ce repertoire pour le moment |
|
| 111 | - if ($avail == 'SYSTEM') { |
|
| 112 | - $file = $loc; |
|
| 113 | - if (_DIR_RACINE and strncmp($file, _DIR_RACINE, strlen(_DIR_RACINE)) == 0) { |
|
| 114 | - $file = substr($file, strlen(_DIR_RACINE)); |
|
| 115 | - } |
|
| 116 | - $file = find_in_path($file); |
|
| 117 | - } else { |
|
| 118 | - $file .= preg_replace('/[^\w.]/', '_', $loc); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $dtd = ''; |
|
| 122 | - if (@is_readable($file)) { |
|
| 123 | - lire_fichier($file, $dtd); |
|
| 124 | - } else { |
|
| 125 | - if ($avail == 'PUBLIC') { |
|
| 126 | - include_spip('inc/distant'); |
|
| 127 | - $dtd = recuperer_url($loc); |
|
| 128 | - $dtd = trim($dtd['page'] ?? ''); |
|
| 129 | - if ($dtd) { |
|
| 130 | - ecrire_fichier($file, $dtd, true); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $dtd = ltrim($dtd); |
|
| 136 | - if (!$dtd) { |
|
| 137 | - spip_log("DTD '$loc' ($file) inaccessible"); |
|
| 138 | - |
|
| 139 | - return false; |
|
| 140 | - } else { |
|
| 141 | - spip_log("analyse de la DTD $loc "); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - while ($dtd) { |
|
| 145 | - if ($dtd[0] != '<') { |
|
| 146 | - $r = analyser_dtd_lexeme($dtd, $dtc, $loc); |
|
| 147 | - } elseif ($dtd[1] != '!') { |
|
| 148 | - $r = analyser_dtd_pi($dtd, $dtc, $loc); |
|
| 149 | - } elseif ($dtd[2] == '[') { |
|
| 150 | - $r = analyser_dtd_data($dtd, $dtc, $loc); |
|
| 151 | - } else { |
|
| 152 | - switch ($dtd[3]) { |
|
| 153 | - case '%': |
|
| 154 | - $r = analyser_dtd_data($dtd, $dtc, $loc); |
|
| 155 | - break; |
|
| 156 | - case 'T': |
|
| 157 | - $r = analyser_dtd_attlist($dtd, $dtc, $loc); |
|
| 158 | - break; |
|
| 159 | - case 'L': |
|
| 160 | - $r = analyser_dtd_element($dtd, $dtc, $loc); |
|
| 161 | - break; |
|
| 162 | - case 'N': |
|
| 163 | - $r = analyser_dtd_entity($dtd, $dtc, $loc); |
|
| 164 | - break; |
|
| 165 | - case 'O': |
|
| 166 | - $r = analyser_dtd_notation($dtd, $dtc, $loc); |
|
| 167 | - break; |
|
| 168 | - case '-': |
|
| 169 | - $r = analyser_dtd_comment($dtd, $dtc, $loc); |
|
| 170 | - break; |
|
| 171 | - default: |
|
| 172 | - $r = -1; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - if (!is_string($r)) { |
|
| 176 | - spip_log("erreur $r dans la DTD " . substr($dtd, 0, 80) . '.....'); |
|
| 177 | - |
|
| 178 | - return false; |
|
| 179 | - } |
|
| 180 | - $dtd = $r; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - return true; |
|
| 107 | + // creer le repertoire de cache si ce n'est fait |
|
| 108 | + // (utile aussi pour le resultat de la compil) |
|
| 109 | + $file = sous_repertoire(_DIR_CACHE_XML); |
|
| 110 | + // si DTD locale, ignorer ce repertoire pour le moment |
|
| 111 | + if ($avail == 'SYSTEM') { |
|
| 112 | + $file = $loc; |
|
| 113 | + if (_DIR_RACINE and strncmp($file, _DIR_RACINE, strlen(_DIR_RACINE)) == 0) { |
|
| 114 | + $file = substr($file, strlen(_DIR_RACINE)); |
|
| 115 | + } |
|
| 116 | + $file = find_in_path($file); |
|
| 117 | + } else { |
|
| 118 | + $file .= preg_replace('/[^\w.]/', '_', $loc); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $dtd = ''; |
|
| 122 | + if (@is_readable($file)) { |
|
| 123 | + lire_fichier($file, $dtd); |
|
| 124 | + } else { |
|
| 125 | + if ($avail == 'PUBLIC') { |
|
| 126 | + include_spip('inc/distant'); |
|
| 127 | + $dtd = recuperer_url($loc); |
|
| 128 | + $dtd = trim($dtd['page'] ?? ''); |
|
| 129 | + if ($dtd) { |
|
| 130 | + ecrire_fichier($file, $dtd, true); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $dtd = ltrim($dtd); |
|
| 136 | + if (!$dtd) { |
|
| 137 | + spip_log("DTD '$loc' ($file) inaccessible"); |
|
| 138 | + |
|
| 139 | + return false; |
|
| 140 | + } else { |
|
| 141 | + spip_log("analyse de la DTD $loc "); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + while ($dtd) { |
|
| 145 | + if ($dtd[0] != '<') { |
|
| 146 | + $r = analyser_dtd_lexeme($dtd, $dtc, $loc); |
|
| 147 | + } elseif ($dtd[1] != '!') { |
|
| 148 | + $r = analyser_dtd_pi($dtd, $dtc, $loc); |
|
| 149 | + } elseif ($dtd[2] == '[') { |
|
| 150 | + $r = analyser_dtd_data($dtd, $dtc, $loc); |
|
| 151 | + } else { |
|
| 152 | + switch ($dtd[3]) { |
|
| 153 | + case '%': |
|
| 154 | + $r = analyser_dtd_data($dtd, $dtc, $loc); |
|
| 155 | + break; |
|
| 156 | + case 'T': |
|
| 157 | + $r = analyser_dtd_attlist($dtd, $dtc, $loc); |
|
| 158 | + break; |
|
| 159 | + case 'L': |
|
| 160 | + $r = analyser_dtd_element($dtd, $dtc, $loc); |
|
| 161 | + break; |
|
| 162 | + case 'N': |
|
| 163 | + $r = analyser_dtd_entity($dtd, $dtc, $loc); |
|
| 164 | + break; |
|
| 165 | + case 'O': |
|
| 166 | + $r = analyser_dtd_notation($dtd, $dtc, $loc); |
|
| 167 | + break; |
|
| 168 | + case '-': |
|
| 169 | + $r = analyser_dtd_comment($dtd, $dtc, $loc); |
|
| 170 | + break; |
|
| 171 | + default: |
|
| 172 | + $r = -1; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + if (!is_string($r)) { |
|
| 176 | + spip_log("erreur $r dans la DTD " . substr($dtd, 0, 80) . '.....'); |
|
| 177 | + |
|
| 178 | + return false; |
|
| 179 | + } |
|
| 180 | + $dtd = $r; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + return true; |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | // https://code.spip.net/@analyser_dtd_comment |
| 187 | 187 | function analyser_dtd_comment($dtd, &$dtc, $grammaire) { |
| 188 | - // ejecter les commentaires, surtout quand ils contiennent du code. |
|
| 189 | - // Option /s car sur plusieurs lignes parfois |
|
| 188 | + // ejecter les commentaires, surtout quand ils contiennent du code. |
|
| 189 | + // Option /s car sur plusieurs lignes parfois |
|
| 190 | 190 | |
| 191 | - if (!preg_match('/^<!--.*?-->\s*(.*)$/s', $dtd, $m)) { |
|
| 192 | - return -6; |
|
| 193 | - } |
|
| 191 | + if (!preg_match('/^<!--.*?-->\s*(.*)$/s', $dtd, $m)) { |
|
| 192 | + return -6; |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - return $m[1]; |
|
| 195 | + return $m[1]; |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | // https://code.spip.net/@analyser_dtd_pi |
| 199 | 199 | function analyser_dtd_pi($dtd, &$dtc, $grammaire) { |
| 200 | - if (!preg_match('/^<\?.*?>\s*(.*)$/s', $dtd, $m)) { |
|
| 201 | - return -10; |
|
| 202 | - } |
|
| 200 | + if (!preg_match('/^<\?.*?>\s*(.*)$/s', $dtd, $m)) { |
|
| 201 | + return -10; |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - return $m[1]; |
|
| 204 | + return $m[1]; |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | // https://code.spip.net/@analyser_dtd_lexeme |
| 208 | 208 | function analyser_dtd_lexeme($dtd, &$dtc, $grammaire) { |
| 209 | 209 | |
| 210 | - if (!preg_match(_REGEXP_ENTITY_DEF, $dtd, $m)) { |
|
| 211 | - return -9; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - [, $s] = $m; |
|
| 215 | - $n = $dtc->macros[$s]; |
|
| 216 | - |
|
| 217 | - if (is_array($n)) { |
|
| 218 | - // en cas d'inclusion, l'espace de nom est le meme |
|
| 219 | - // mais gaffe aux DTD dont l'URL est relative a l'engloblante |
|
| 220 | - if ( |
|
| 221 | - ($n[0] == 'PUBLIC') |
|
| 222 | - and !tester_url_absolue($n[1]) |
|
| 223 | - ) { |
|
| 224 | - $n[1] = substr($grammaire, 0, strrpos($grammaire, '/') + 1) . $n[1]; |
|
| 225 | - } |
|
| 226 | - analyser_dtd($n[1], $n[0], $dtc); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - return ltrim(substr($dtd, strlen($m[0]))); |
|
| 210 | + if (!preg_match(_REGEXP_ENTITY_DEF, $dtd, $m)) { |
|
| 211 | + return -9; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + [, $s] = $m; |
|
| 215 | + $n = $dtc->macros[$s]; |
|
| 216 | + |
|
| 217 | + if (is_array($n)) { |
|
| 218 | + // en cas d'inclusion, l'espace de nom est le meme |
|
| 219 | + // mais gaffe aux DTD dont l'URL est relative a l'engloblante |
|
| 220 | + if ( |
|
| 221 | + ($n[0] == 'PUBLIC') |
|
| 222 | + and !tester_url_absolue($n[1]) |
|
| 223 | + ) { |
|
| 224 | + $n[1] = substr($grammaire, 0, strrpos($grammaire, '/') + 1) . $n[1]; |
|
| 225 | + } |
|
| 226 | + analyser_dtd($n[1], $n[0], $dtc); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + return ltrim(substr($dtd, strlen($m[0]))); |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | // il faudrait gerer plus proprement les niveaux d'inclusion: |
@@ -235,85 +235,85 @@ discard block |
||
| 235 | 235 | // https://code.spip.net/@analyser_dtd_data |
| 236 | 236 | function analyser_dtd_data($dtd, &$dtc, $grammaire) { |
| 237 | 237 | |
| 238 | - if (!preg_match(_REGEXP_INCLUDE_USE, $dtd, $m)) { |
|
| 239 | - return -11; |
|
| 240 | - } |
|
| 241 | - if ( |
|
| 242 | - !preg_match( |
|
| 243 | - '/^((\s*<!(\[\s*%\s*[^;]*;\s*\[([^]<]*<[^>]*>)*[^]<]*\]\]>)|([^]>]*>))*[^]<]*)\]\]>\s*/s', |
|
| 244 | - $m[2], |
|
| 245 | - $r |
|
| 246 | - ) |
|
| 247 | - ) { |
|
| 248 | - return -12; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - if ($dtc->macros[$m[1]] == 'INCLUDE') { |
|
| 252 | - $retour = $r[1] . substr($m[2], strlen($r[0])); |
|
| 253 | - } else { |
|
| 254 | - $retour = substr($m[2], strlen($r[0])); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - return $retour; |
|
| 238 | + if (!preg_match(_REGEXP_INCLUDE_USE, $dtd, $m)) { |
|
| 239 | + return -11; |
|
| 240 | + } |
|
| 241 | + if ( |
|
| 242 | + !preg_match( |
|
| 243 | + '/^((\s*<!(\[\s*%\s*[^;]*;\s*\[([^]<]*<[^>]*>)*[^]<]*\]\]>)|([^]>]*>))*[^]<]*)\]\]>\s*/s', |
|
| 244 | + $m[2], |
|
| 245 | + $r |
|
| 246 | + ) |
|
| 247 | + ) { |
|
| 248 | + return -12; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + if ($dtc->macros[$m[1]] == 'INCLUDE') { |
|
| 252 | + $retour = $r[1] . substr($m[2], strlen($r[0])); |
|
| 253 | + } else { |
|
| 254 | + $retour = substr($m[2], strlen($r[0])); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + return $retour; |
|
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | // https://code.spip.net/@analyser_dtd_notation |
| 261 | 261 | function analyser_dtd_notation($dtd, &$dtc, $grammaire) { |
| 262 | - if (!preg_match('/^<!NOTATION.*?>\s*(.*)$/s', $dtd, $m)) { |
|
| 263 | - return -8; |
|
| 264 | - } |
|
| 265 | - spip_log('analyser_dtd_notation a ecrire'); |
|
| 262 | + if (!preg_match('/^<!NOTATION.*?>\s*(.*)$/s', $dtd, $m)) { |
|
| 263 | + return -8; |
|
| 264 | + } |
|
| 265 | + spip_log('analyser_dtd_notation a ecrire'); |
|
| 266 | 266 | |
| 267 | - return $m[1]; |
|
| 267 | + return $m[1]; |
|
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | // https://code.spip.net/@analyser_dtd_entity |
| 271 | 271 | function analyser_dtd_entity($dtd, &$dtc, $grammaire) { |
| 272 | - if (!preg_match(_REGEXP_ENTITY_DECL, $dtd, $m)) { |
|
| 273 | - return -2; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - [$t, $term, $nom, $type, $k1, $k2, $k3, $k4, $k5, $k6, $c, $q, $alt, $dtd] = $m; |
|
| 277 | - |
|
| 278 | - if (isset($dtc->macros[$nom]) and $dtc->macros[$nom]) { |
|
| 279 | - return $dtd; |
|
| 280 | - } |
|
| 281 | - if (isset($dtc->entites[$nom])) { |
|
| 282 | - spip_log("redefinition de l'entite $nom"); |
|
| 283 | - } |
|
| 284 | - if ($k6) { |
|
| 285 | - return $k6 . $dtd; |
|
| 286 | - } // cas du synonyme complet |
|
| 287 | - $val = expanserEntite(($k2 ? $k3 : ($k4 ? $k5 : $k6)), $dtc->macros); |
|
| 288 | - |
|
| 289 | - // cas particulier double evaluation: 'PUBLIC "..." "...."' |
|
| 290 | - if (preg_match('/(PUBLIC|SYSTEM)\s+"([^"]*)"\s*("([^"]*)")?\s*$/s', $val, $r)) { |
|
| 291 | - [$t, $type, $val, $q, $alt] = $r; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - if (!$term) { |
|
| 295 | - $dtc->entites[$nom] = $val; |
|
| 296 | - } elseif (!$type) { |
|
| 297 | - $dtc->macros[$nom] = $val; |
|
| 298 | - } else { |
|
| 299 | - if (($type == 'SYSTEM') and !$alt) { |
|
| 300 | - $alt = $val; |
|
| 301 | - } |
|
| 302 | - if (!$alt) { |
|
| 303 | - $dtc->macros[$nom] = $val; |
|
| 304 | - } else { |
|
| 305 | - if ( |
|
| 306 | - ($type == 'PUBLIC') |
|
| 307 | - and (strpos($alt, '/') === false) |
|
| 308 | - ) { |
|
| 309 | - $alt = preg_replace(',/[^/]+$,', '/', $grammaire) |
|
| 310 | - . $alt; |
|
| 311 | - } |
|
| 312 | - $dtc->macros[$nom] = [$type, $alt]; |
|
| 313 | - } |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - return $dtd; |
|
| 272 | + if (!preg_match(_REGEXP_ENTITY_DECL, $dtd, $m)) { |
|
| 273 | + return -2; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + [$t, $term, $nom, $type, $k1, $k2, $k3, $k4, $k5, $k6, $c, $q, $alt, $dtd] = $m; |
|
| 277 | + |
|
| 278 | + if (isset($dtc->macros[$nom]) and $dtc->macros[$nom]) { |
|
| 279 | + return $dtd; |
|
| 280 | + } |
|
| 281 | + if (isset($dtc->entites[$nom])) { |
|
| 282 | + spip_log("redefinition de l'entite $nom"); |
|
| 283 | + } |
|
| 284 | + if ($k6) { |
|
| 285 | + return $k6 . $dtd; |
|
| 286 | + } // cas du synonyme complet |
|
| 287 | + $val = expanserEntite(($k2 ? $k3 : ($k4 ? $k5 : $k6)), $dtc->macros); |
|
| 288 | + |
|
| 289 | + // cas particulier double evaluation: 'PUBLIC "..." "...."' |
|
| 290 | + if (preg_match('/(PUBLIC|SYSTEM)\s+"([^"]*)"\s*("([^"]*)")?\s*$/s', $val, $r)) { |
|
| 291 | + [$t, $type, $val, $q, $alt] = $r; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + if (!$term) { |
|
| 295 | + $dtc->entites[$nom] = $val; |
|
| 296 | + } elseif (!$type) { |
|
| 297 | + $dtc->macros[$nom] = $val; |
|
| 298 | + } else { |
|
| 299 | + if (($type == 'SYSTEM') and !$alt) { |
|
| 300 | + $alt = $val; |
|
| 301 | + } |
|
| 302 | + if (!$alt) { |
|
| 303 | + $dtc->macros[$nom] = $val; |
|
| 304 | + } else { |
|
| 305 | + if ( |
|
| 306 | + ($type == 'PUBLIC') |
|
| 307 | + and (strpos($alt, '/') === false) |
|
| 308 | + ) { |
|
| 309 | + $alt = preg_replace(',/[^/]+$,', '/', $grammaire) |
|
| 310 | + . $alt; |
|
| 311 | + } |
|
| 312 | + $dtc->macros[$nom] = [$type, $alt]; |
|
| 313 | + } |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + return $dtd; |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | // Dresser le tableau des filles potentielles de l'element |
@@ -327,77 +327,77 @@ discard block |
||
| 327 | 327 | |
| 328 | 328 | // https://code.spip.net/@analyser_dtd_element |
| 329 | 329 | function analyser_dtd_element($dtd, &$dtc, $grammaire) { |
| 330 | - if (!preg_match('/^<!ELEMENT\s+([^>\s]+)([^>]*)>\s*(.*)$/s', $dtd, $m)) { |
|
| 331 | - return -3; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - [, $nom, $contenu, $dtd] = $m; |
|
| 335 | - $nom = expanserEntite($nom, $dtc->macros); |
|
| 336 | - |
|
| 337 | - if (isset($dtc->elements[$nom])) { |
|
| 338 | - spip_log("redefinition de l'element $nom dans la DTD"); |
|
| 339 | - |
|
| 340 | - return -4; |
|
| 341 | - } |
|
| 342 | - $filles = []; |
|
| 343 | - $contenu = expanserEntite($contenu, $dtc->macros); |
|
| 344 | - $val = $contenu ? compilerRegle($contenu) : '(?:EMPTY )'; |
|
| 345 | - if ($val == '(?:EMPTY )') { |
|
| 346 | - $dtc->regles[$nom] = 'EMPTY'; |
|
| 347 | - } elseif ($val == '(?:ANY )') { |
|
| 348 | - $dtc->regles[$nom] = 'ANY'; |
|
| 349 | - } else { |
|
| 350 | - $last = substr($val, -1); |
|
| 351 | - if ( |
|
| 352 | - preg_match('/ \w/', $val) |
|
| 353 | - or (!empty($last) and strpos('*+?', $last) === false) |
|
| 354 | - ) { |
|
| 355 | - $dtc->regles[$nom] = "/^$val$/"; |
|
| 356 | - } else { |
|
| 357 | - $dtc->regles[$nom] = $last; |
|
| 358 | - } |
|
| 359 | - $filles = array_values(preg_split('/\W+/', $val, -1, PREG_SPLIT_NO_EMPTY)); |
|
| 360 | - |
|
| 361 | - foreach ($filles as $k) { |
|
| 362 | - if (!isset($dtc->peres[$k])) { |
|
| 363 | - $dtc->peres[$k] = []; |
|
| 364 | - } |
|
| 365 | - if (!in_array($nom, $dtc->peres[$k])) { |
|
| 366 | - $dtc->peres[$k][] = $nom; |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - $dtc->pcdata[$nom] = (strpos($contenu, '#PCDATA') === false); |
|
| 371 | - $dtc->elements[$nom] = $filles; |
|
| 372 | - |
|
| 373 | - return $dtd; |
|
| 330 | + if (!preg_match('/^<!ELEMENT\s+([^>\s]+)([^>]*)>\s*(.*)$/s', $dtd, $m)) { |
|
| 331 | + return -3; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + [, $nom, $contenu, $dtd] = $m; |
|
| 335 | + $nom = expanserEntite($nom, $dtc->macros); |
|
| 336 | + |
|
| 337 | + if (isset($dtc->elements[$nom])) { |
|
| 338 | + spip_log("redefinition de l'element $nom dans la DTD"); |
|
| 339 | + |
|
| 340 | + return -4; |
|
| 341 | + } |
|
| 342 | + $filles = []; |
|
| 343 | + $contenu = expanserEntite($contenu, $dtc->macros); |
|
| 344 | + $val = $contenu ? compilerRegle($contenu) : '(?:EMPTY )'; |
|
| 345 | + if ($val == '(?:EMPTY )') { |
|
| 346 | + $dtc->regles[$nom] = 'EMPTY'; |
|
| 347 | + } elseif ($val == '(?:ANY )') { |
|
| 348 | + $dtc->regles[$nom] = 'ANY'; |
|
| 349 | + } else { |
|
| 350 | + $last = substr($val, -1); |
|
| 351 | + if ( |
|
| 352 | + preg_match('/ \w/', $val) |
|
| 353 | + or (!empty($last) and strpos('*+?', $last) === false) |
|
| 354 | + ) { |
|
| 355 | + $dtc->regles[$nom] = "/^$val$/"; |
|
| 356 | + } else { |
|
| 357 | + $dtc->regles[$nom] = $last; |
|
| 358 | + } |
|
| 359 | + $filles = array_values(preg_split('/\W+/', $val, -1, PREG_SPLIT_NO_EMPTY)); |
|
| 360 | + |
|
| 361 | + foreach ($filles as $k) { |
|
| 362 | + if (!isset($dtc->peres[$k])) { |
|
| 363 | + $dtc->peres[$k] = []; |
|
| 364 | + } |
|
| 365 | + if (!in_array($nom, $dtc->peres[$k])) { |
|
| 366 | + $dtc->peres[$k][] = $nom; |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + $dtc->pcdata[$nom] = (strpos($contenu, '#PCDATA') === false); |
|
| 371 | + $dtc->elements[$nom] = $filles; |
|
| 372 | + |
|
| 373 | + return $dtd; |
|
| 374 | 374 | } |
| 375 | 375 | |
| 376 | 376 | |
| 377 | 377 | // https://code.spip.net/@analyser_dtd_attlist |
| 378 | 378 | function analyser_dtd_attlist($dtd, &$dtc, $grammaire) { |
| 379 | - if (!preg_match('/^<!ATTLIST\s+(\S+)\s+([^>]*)>\s*(.*)/s', $dtd, $m)) { |
|
| 380 | - return -5; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - [, $nom, $val, $dtd] = $m; |
|
| 384 | - $nom = expanserEntite($nom, $dtc->macros); |
|
| 385 | - $val = expanserEntite($val, $dtc->macros); |
|
| 386 | - if (!isset($dtc->attributs[$nom])) { |
|
| 387 | - $dtc->attributs[$nom] = []; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - if (preg_match_all("/\s*(\S+)\s+(([(][^)]*[)])|(\S+))\s+([^\s']*)(\s*'[^']*')?/", $val, $r2, PREG_SET_ORDER)) { |
|
| 391 | - foreach ($r2 as $m2) { |
|
| 392 | - $v = preg_match('/^\w+$/', $m2[2]) ? $m2[2] |
|
| 393 | - : ('/^' . preg_replace('/\s+/', '', $m2[2]) . '$/'); |
|
| 394 | - $m21 = expanserEntite($m2[1], $dtc->macros); |
|
| 395 | - $m25 = expanserEntite($m2[5], $dtc->macros); |
|
| 396 | - $dtc->attributs[$nom][$m21] = [$v, $m25]; |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - return $dtd; |
|
| 379 | + if (!preg_match('/^<!ATTLIST\s+(\S+)\s+([^>]*)>\s*(.*)/s', $dtd, $m)) { |
|
| 380 | + return -5; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + [, $nom, $val, $dtd] = $m; |
|
| 384 | + $nom = expanserEntite($nom, $dtc->macros); |
|
| 385 | + $val = expanserEntite($val, $dtc->macros); |
|
| 386 | + if (!isset($dtc->attributs[$nom])) { |
|
| 387 | + $dtc->attributs[$nom] = []; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + if (preg_match_all("/\s*(\S+)\s+(([(][^)]*[)])|(\S+))\s+([^\s']*)(\s*'[^']*')?/", $val, $r2, PREG_SET_ORDER)) { |
|
| 391 | + foreach ($r2 as $m2) { |
|
| 392 | + $v = preg_match('/^\w+$/', $m2[2]) ? $m2[2] |
|
| 393 | + : ('/^' . preg_replace('/\s+/', '', $m2[2]) . '$/'); |
|
| 394 | + $m21 = expanserEntite($m2[1], $dtc->macros); |
|
| 395 | + $m25 = expanserEntite($m2[5], $dtc->macros); |
|
| 396 | + $dtc->attributs[$nom][$m21] = [$v, $m25]; |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + return $dtd; |
|
| 401 | 401 | } |
| 402 | 402 | |
| 403 | 403 | |
@@ -413,26 +413,26 @@ discard block |
||
| 413 | 413 | * @return string|array |
| 414 | 414 | **/ |
| 415 | 415 | function expanserEntite($val, $macros = []) { |
| 416 | - static $vu = []; |
|
| 417 | - if (!is_string($val)) { |
|
| 418 | - return $vu; |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - if (preg_match_all(_REGEXP_ENTITY_USE, $val, $r, PREG_SET_ORDER)) { |
|
| 422 | - foreach ($r as $m) { |
|
| 423 | - $ent = $m[1]; |
|
| 424 | - // il peut valoir "" |
|
| 425 | - if (!isset($macros[$ent])) { |
|
| 426 | - spip_log("Entite $ent inconnu"); |
|
| 427 | - } else { |
|
| 428 | - if (!isset($vu[$ent])) { |
|
| 429 | - $vu[$ent] = 0; |
|
| 430 | - } |
|
| 431 | - ++$vu[$ent]; |
|
| 432 | - $val = str_replace($m[0], $macros[$ent], $val); |
|
| 433 | - } |
|
| 434 | - } |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - return trim(preg_replace('/\s+/', ' ', $val)); |
|
| 416 | + static $vu = []; |
|
| 417 | + if (!is_string($val)) { |
|
| 418 | + return $vu; |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + if (preg_match_all(_REGEXP_ENTITY_USE, $val, $r, PREG_SET_ORDER)) { |
|
| 422 | + foreach ($r as $m) { |
|
| 423 | + $ent = $m[1]; |
|
| 424 | + // il peut valoir "" |
|
| 425 | + if (!isset($macros[$ent])) { |
|
| 426 | + spip_log("Entite $ent inconnu"); |
|
| 427 | + } else { |
|
| 428 | + if (!isset($vu[$ent])) { |
|
| 429 | + $vu[$ent] = 0; |
|
| 430 | + } |
|
| 431 | + ++$vu[$ent]; |
|
| 432 | + $val = str_replace($m[0], $macros[$ent], $val); |
|
| 433 | + } |
|
| 434 | + } |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + return trim(preg_replace('/\s+/', ' ', $val)); |
|
| 438 | 438 | } |
@@ -5,23 +5,23 @@ discard block |
||
| 5 | 5 | * @internal |
| 6 | 6 | */ |
| 7 | 7 | class Spip_Css_Vars_Collection { |
| 8 | - private array $vars = []; |
|
| 9 | - |
|
| 10 | - public function add(string $var, string $value) { |
|
| 11 | - $this->vars[$var] = $value; |
|
| 12 | - } |
|
| 13 | - |
|
| 14 | - public function getString(): string { |
|
| 15 | - $string = ''; |
|
| 16 | - foreach ($this->vars as $key => $value) { |
|
| 17 | - $string .= "$key: $value;\n"; |
|
| 18 | - } |
|
| 19 | - return $string; |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - public function __toString(): string { |
|
| 23 | - return $this->getString(); |
|
| 24 | - } |
|
| 8 | + private array $vars = []; |
|
| 9 | + |
|
| 10 | + public function add(string $var, string $value) { |
|
| 11 | + $this->vars[$var] = $value; |
|
| 12 | + } |
|
| 13 | + |
|
| 14 | + public function getString(): string { |
|
| 15 | + $string = ''; |
|
| 16 | + foreach ($this->vars as $key => $value) { |
|
| 17 | + $string .= "$key: $value;\n"; |
|
| 18 | + } |
|
| 19 | + return $string; |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + public function __toString(): string { |
|
| 23 | + return $this->getString(); |
|
| 24 | + } |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -30,59 +30,59 @@ discard block |
||
| 30 | 30 | * @param Pile $pile Pile |
| 31 | 31 | */ |
| 32 | 32 | function spip_generer_variables_css_typo(array $Pile): \Spip_Css_Vars_Collection { |
| 33 | - $vars = new \Spip_Css_Vars_Collection(); |
|
| 34 | - |
|
| 35 | - // Direction |
|
| 36 | - $vars->add('--spip-dir', $Pile[0]['dir']); |
|
| 37 | - $vars->add('--spip-left', $Pile[0]['left']); |
|
| 38 | - $vars->add('--spip-right', $Pile[0]['right']); |
|
| 39 | - |
|
| 40 | - // Typographie |
|
| 41 | - $vars->add('--spip-font-size', $Pile[0]['font-size']); |
|
| 42 | - $vars->add('--spip-line-height', $Pile[0]['line-height']); |
|
| 43 | - $vars->add('--spip-text-indent', $Pile[0]['text-indent']); |
|
| 44 | - $vars->add('--spip-font-family', $Pile[0]['font-family']); |
|
| 45 | - |
|
| 46 | - // Couleurs hors thème |
|
| 47 | - $vars->add('--spip-background-color', $Pile[0]['background-color']); |
|
| 48 | - $vars->add('--spip-color', $Pile[0]['color']); |
|
| 49 | - |
|
| 50 | - // Espacements pour le rythme vertical et les gouttières |
|
| 51 | - // Basés sur la hauteur d'une ligne de texte à la racine du document |
|
| 52 | - $vars->add('--spip-spacing-y', round(strmult($Pile[0]['font-size'], $Pile[0]['line-height']), 4) . 'rem'); |
|
| 53 | - $vars->add('--spip-spacing-x', round(strmult($Pile[0]['font-size'], $Pile[0]['line-height']), 4) . 'rem'); |
|
| 54 | - $vars->add('--spip-margin-bottom', $Pile[0]['margin-bottom']); // À déprécier |
|
| 55 | - |
|
| 56 | - // Bordures |
|
| 57 | - $vars->add('--spip-border-radius-mini', '0.2rem'); |
|
| 58 | - $vars->add('--spip-border-radius', '0.33rem'); |
|
| 59 | - $vars->add('--spip-border-radius-large', '0.66rem'); |
|
| 60 | - |
|
| 61 | - // Ombres portées |
|
| 62 | - $shadow_mini = |
|
| 63 | - '0 0.05em 0.1em hsla(0, 0%, 0%, 0.33),' . |
|
| 64 | - '0 0.1em 0.15em hsla(0, 0%, 0%, 0.05),' . |
|
| 65 | - '0 0.1em 0.25em hsla(0, 0%, 0%, 0.05)'; |
|
| 66 | - $shadow = |
|
| 67 | - '0 0.05em 0.15em hsla(0, 0%, 0%, 0.33),' . |
|
| 68 | - '0 0.1em 0.25em hsla(0, 0%, 0%, 0.05),' . |
|
| 69 | - '0 0.1em 0.5em hsla(0, 0%, 0%, 0.05)'; |
|
| 70 | - $shadow_large = |
|
| 71 | - '0 0.05em 0.15em hsla(0, 0%, 0%, 0.1),' . |
|
| 72 | - '0 0.2em 0.5em hsla(0, 0%, 0%, 0.1),' . |
|
| 73 | - '0 0.2em 1em hsla(0, 0%, 0%, 0.075)'; |
|
| 74 | - $shadow_huge = |
|
| 75 | - '0 0.1em 0.25em hsla(0, 0%, 0%, 0.1),' . |
|
| 76 | - '0 0.25em 1em hsla(0, 0%, 0%, 0.1),' . |
|
| 77 | - '0 0.5em 2em hsla(0, 0%, 0%, 0.075)'; |
|
| 78 | - $vars->add('--spip-box-shadow-mini', $shadow_mini); |
|
| 79 | - $vars->add('--spip-box-shadow-mini-hover', $shadow); |
|
| 80 | - $vars->add('--spip-box-shadow', $shadow); |
|
| 81 | - $vars->add('--spip-box-shadow-hover', $shadow_large); |
|
| 82 | - $vars->add('--spip-box-shadow-large', $shadow_large); |
|
| 83 | - $vars->add('--spip-box-shadow-large-hover', $shadow_huge); |
|
| 84 | - |
|
| 85 | - return $vars; |
|
| 33 | + $vars = new \Spip_Css_Vars_Collection(); |
|
| 34 | + |
|
| 35 | + // Direction |
|
| 36 | + $vars->add('--spip-dir', $Pile[0]['dir']); |
|
| 37 | + $vars->add('--spip-left', $Pile[0]['left']); |
|
| 38 | + $vars->add('--spip-right', $Pile[0]['right']); |
|
| 39 | + |
|
| 40 | + // Typographie |
|
| 41 | + $vars->add('--spip-font-size', $Pile[0]['font-size']); |
|
| 42 | + $vars->add('--spip-line-height', $Pile[0]['line-height']); |
|
| 43 | + $vars->add('--spip-text-indent', $Pile[0]['text-indent']); |
|
| 44 | + $vars->add('--spip-font-family', $Pile[0]['font-family']); |
|
| 45 | + |
|
| 46 | + // Couleurs hors thème |
|
| 47 | + $vars->add('--spip-background-color', $Pile[0]['background-color']); |
|
| 48 | + $vars->add('--spip-color', $Pile[0]['color']); |
|
| 49 | + |
|
| 50 | + // Espacements pour le rythme vertical et les gouttières |
|
| 51 | + // Basés sur la hauteur d'une ligne de texte à la racine du document |
|
| 52 | + $vars->add('--spip-spacing-y', round(strmult($Pile[0]['font-size'], $Pile[0]['line-height']), 4) . 'rem'); |
|
| 53 | + $vars->add('--spip-spacing-x', round(strmult($Pile[0]['font-size'], $Pile[0]['line-height']), 4) . 'rem'); |
|
| 54 | + $vars->add('--spip-margin-bottom', $Pile[0]['margin-bottom']); // À déprécier |
|
| 55 | + |
|
| 56 | + // Bordures |
|
| 57 | + $vars->add('--spip-border-radius-mini', '0.2rem'); |
|
| 58 | + $vars->add('--spip-border-radius', '0.33rem'); |
|
| 59 | + $vars->add('--spip-border-radius-large', '0.66rem'); |
|
| 60 | + |
|
| 61 | + // Ombres portées |
|
| 62 | + $shadow_mini = |
|
| 63 | + '0 0.05em 0.1em hsla(0, 0%, 0%, 0.33),' . |
|
| 64 | + '0 0.1em 0.15em hsla(0, 0%, 0%, 0.05),' . |
|
| 65 | + '0 0.1em 0.25em hsla(0, 0%, 0%, 0.05)'; |
|
| 66 | + $shadow = |
|
| 67 | + '0 0.05em 0.15em hsla(0, 0%, 0%, 0.33),' . |
|
| 68 | + '0 0.1em 0.25em hsla(0, 0%, 0%, 0.05),' . |
|
| 69 | + '0 0.1em 0.5em hsla(0, 0%, 0%, 0.05)'; |
|
| 70 | + $shadow_large = |
|
| 71 | + '0 0.05em 0.15em hsla(0, 0%, 0%, 0.1),' . |
|
| 72 | + '0 0.2em 0.5em hsla(0, 0%, 0%, 0.1),' . |
|
| 73 | + '0 0.2em 1em hsla(0, 0%, 0%, 0.075)'; |
|
| 74 | + $shadow_huge = |
|
| 75 | + '0 0.1em 0.25em hsla(0, 0%, 0%, 0.1),' . |
|
| 76 | + '0 0.25em 1em hsla(0, 0%, 0%, 0.1),' . |
|
| 77 | + '0 0.5em 2em hsla(0, 0%, 0%, 0.075)'; |
|
| 78 | + $vars->add('--spip-box-shadow-mini', $shadow_mini); |
|
| 79 | + $vars->add('--spip-box-shadow-mini-hover', $shadow); |
|
| 80 | + $vars->add('--spip-box-shadow', $shadow); |
|
| 81 | + $vars->add('--spip-box-shadow-hover', $shadow_large); |
|
| 82 | + $vars->add('--spip-box-shadow-large', $shadow_large); |
|
| 83 | + $vars->add('--spip-box-shadow-large-hover', $shadow_huge); |
|
| 84 | + |
|
| 85 | + return $vars; |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -91,101 +91,101 @@ discard block |
||
| 91 | 91 | * @param string $couleur Couleur hex |
| 92 | 92 | */ |
| 93 | 93 | function spip_generer_variables_css_couleurs_theme(string $couleur): \Spip_Css_Vars_Collection { |
| 94 | - $vars = new \Spip_Css_Vars_Collection(); |
|
| 95 | - |
|
| 96 | - #$vars->add('--spip-color-theme--hsl', couleur_hex_to_hsl($couleur, 'h, s, l')); // redéfini ensuite |
|
| 97 | - $vars->add('--spip-color-theme--h', couleur_hex_to_hsl($couleur, 'h')); |
|
| 98 | - $vars->add('--spip-color-theme--s', couleur_hex_to_hsl($couleur, 's')); |
|
| 99 | - $vars->add('--spip-color-theme--l', couleur_hex_to_hsl($couleur, 'l')); |
|
| 100 | - |
|
| 101 | - // un joli dégradé coloré de presque blanc à presque noir… |
|
| 102 | - $vars->add('--spip-color-theme--100', couleur_hex_to_hsl(couleur_eclaircir($couleur, .99), 'h, s, l')); |
|
| 103 | - $vars->add('--spip-color-theme--98', couleur_hex_to_hsl(couleur_eclaircir($couleur, .95), 'h, s, l')); |
|
| 104 | - $vars->add('--spip-color-theme--95', couleur_hex_to_hsl(couleur_eclaircir($couleur, .90), 'h, s, l')); |
|
| 105 | - $vars->add('--spip-color-theme--90', couleur_hex_to_hsl(couleur_eclaircir($couleur, .75), 'h, s, l')); |
|
| 106 | - $vars->add('--spip-color-theme--80', couleur_hex_to_hsl(couleur_eclaircir($couleur, .50), 'h, s, l')); |
|
| 107 | - $vars->add('--spip-color-theme--70', couleur_hex_to_hsl(couleur_eclaircir($couleur, .25), 'h, s, l')); |
|
| 108 | - $vars->add('--spip-color-theme--60', couleur_hex_to_hsl($couleur, 'h, s, l')); |
|
| 109 | - $vars->add('--spip-color-theme--50', couleur_hex_to_hsl(couleur_foncer($couleur, .125), 'h, s, l')); |
|
| 110 | - $vars->add('--spip-color-theme--40', couleur_hex_to_hsl(couleur_foncer($couleur, .25), 'h, s, l')); |
|
| 111 | - $vars->add('--spip-color-theme--30', couleur_hex_to_hsl(couleur_foncer($couleur, .375), 'h, s, l')); |
|
| 112 | - $vars->add('--spip-color-theme--20', couleur_hex_to_hsl(couleur_foncer($couleur, .50), 'h, s, l')); |
|
| 113 | - $vars->add('--spip-color-theme--10', couleur_hex_to_hsl(couleur_foncer($couleur, .75), 'h, s, l')); |
|
| 114 | - $vars->add('--spip-color-theme--00', couleur_hex_to_hsl(couleur_foncer($couleur, .98), 'h, s, l')); |
|
| 115 | - |
|
| 116 | - return $vars; |
|
| 94 | + $vars = new \Spip_Css_Vars_Collection(); |
|
| 95 | + |
|
| 96 | + #$vars->add('--spip-color-theme--hsl', couleur_hex_to_hsl($couleur, 'h, s, l')); // redéfini ensuite |
|
| 97 | + $vars->add('--spip-color-theme--h', couleur_hex_to_hsl($couleur, 'h')); |
|
| 98 | + $vars->add('--spip-color-theme--s', couleur_hex_to_hsl($couleur, 's')); |
|
| 99 | + $vars->add('--spip-color-theme--l', couleur_hex_to_hsl($couleur, 'l')); |
|
| 100 | + |
|
| 101 | + // un joli dégradé coloré de presque blanc à presque noir… |
|
| 102 | + $vars->add('--spip-color-theme--100', couleur_hex_to_hsl(couleur_eclaircir($couleur, .99), 'h, s, l')); |
|
| 103 | + $vars->add('--spip-color-theme--98', couleur_hex_to_hsl(couleur_eclaircir($couleur, .95), 'h, s, l')); |
|
| 104 | + $vars->add('--spip-color-theme--95', couleur_hex_to_hsl(couleur_eclaircir($couleur, .90), 'h, s, l')); |
|
| 105 | + $vars->add('--spip-color-theme--90', couleur_hex_to_hsl(couleur_eclaircir($couleur, .75), 'h, s, l')); |
|
| 106 | + $vars->add('--spip-color-theme--80', couleur_hex_to_hsl(couleur_eclaircir($couleur, .50), 'h, s, l')); |
|
| 107 | + $vars->add('--spip-color-theme--70', couleur_hex_to_hsl(couleur_eclaircir($couleur, .25), 'h, s, l')); |
|
| 108 | + $vars->add('--spip-color-theme--60', couleur_hex_to_hsl($couleur, 'h, s, l')); |
|
| 109 | + $vars->add('--spip-color-theme--50', couleur_hex_to_hsl(couleur_foncer($couleur, .125), 'h, s, l')); |
|
| 110 | + $vars->add('--spip-color-theme--40', couleur_hex_to_hsl(couleur_foncer($couleur, .25), 'h, s, l')); |
|
| 111 | + $vars->add('--spip-color-theme--30', couleur_hex_to_hsl(couleur_foncer($couleur, .375), 'h, s, l')); |
|
| 112 | + $vars->add('--spip-color-theme--20', couleur_hex_to_hsl(couleur_foncer($couleur, .50), 'h, s, l')); |
|
| 113 | + $vars->add('--spip-color-theme--10', couleur_hex_to_hsl(couleur_foncer($couleur, .75), 'h, s, l')); |
|
| 114 | + $vars->add('--spip-color-theme--00', couleur_hex_to_hsl(couleur_foncer($couleur, .98), 'h, s, l')); |
|
| 115 | + |
|
| 116 | + return $vars; |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | /** |
| 120 | 120 | * Génère les variables CSS de couleurs, dont celles dépendantes des couleurs du thème actif. |
| 121 | 121 | */ |
| 122 | 122 | function spip_generer_variables_css_couleurs(): \Spip_Css_Vars_Collection { |
| 123 | - $vars = new \Spip_Css_Vars_Collection(); |
|
| 124 | - |
|
| 125 | - // nos déclinaisons de couleur (basées sur le dégradé précedent, où 60 est là couleur du thème) |
|
| 126 | - $vars->add('--spip-color-theme-white--hsl', 'var(--spip-color-theme--100)'); |
|
| 127 | - $vars->add('--spip-color-theme-lightest--hsl', 'var(--spip-color-theme--95)'); |
|
| 128 | - $vars->add('--spip-color-theme-lighter--hsl', 'var(--spip-color-theme--90)'); |
|
| 129 | - $vars->add('--spip-color-theme-light--hsl', 'var(--spip-color-theme--80)'); |
|
| 130 | - $vars->add('--spip-color-theme--hsl', 'var(--spip-color-theme--60)'); |
|
| 131 | - $vars->add('--spip-color-theme-dark--hsl', 'var(--spip-color-theme--40)'); |
|
| 132 | - $vars->add('--spip-color-theme-darker--hsl', 'var(--spip-color-theme--20)'); |
|
| 133 | - $vars->add('--spip-color-theme-darkest--hsl', 'var(--spip-color-theme--10)'); |
|
| 134 | - $vars->add('--spip-color-theme-black--hsl', 'var(--spip-color-theme--00)'); |
|
| 135 | - |
|
| 136 | - $vars->add('--spip-color-theme-white', 'hsl(var(--spip-color-theme-white--hsl))'); |
|
| 137 | - $vars->add('--spip-color-theme-lightest', 'hsl(var(--spip-color-theme-lightest--hsl))'); |
|
| 138 | - $vars->add('--spip-color-theme-lighter', 'hsl(var(--spip-color-theme-lighter--hsl))'); |
|
| 139 | - $vars->add('--spip-color-theme-light', 'hsl(var(--spip-color-theme-light--hsl))'); |
|
| 140 | - $vars->add('--spip-color-theme', 'hsl(var(--spip-color-theme--hsl))'); |
|
| 141 | - $vars->add('--spip-color-theme-dark', 'hsl(var(--spip-color-theme-dark--hsl))'); |
|
| 142 | - $vars->add('--spip-color-theme-darker', 'hsl(var(--spip-color-theme-darker--hsl))'); |
|
| 143 | - $vars->add('--spip-color-theme-darkest', 'hsl(var(--spip-color-theme-darkest--hsl))'); |
|
| 144 | - $vars->add('--spip-color-theme-black', 'hsl(var(--spip-color-theme-black--hsl))'); |
|
| 145 | - |
|
| 146 | - // déclinaisons de gris (luminosité calquée sur le dégradé de couleur) |
|
| 147 | - $vars->add('--spip-color-white--hsl', '0, 0%, 100%'); |
|
| 148 | - $vars->add('--spip-color-gray-lightest--hsl', '0, 0%, 96%'); |
|
| 149 | - $vars->add('--spip-color-gray-lighter--hsl', '0, 0%, 90%'); |
|
| 150 | - $vars->add('--spip-color-gray-light--hsl', '0, 0%, 80%'); |
|
| 151 | - $vars->add('--spip-color-gray--hsl', '0, 0%, 60%'); |
|
| 152 | - $vars->add('--spip-color-gray-dark--hsl', '0, 0%, 40%'); |
|
| 153 | - $vars->add('--spip-color-gray-darker--hsl', '0, 0%, 20%'); |
|
| 154 | - $vars->add('--spip-color-gray-darkest--hsl', '0, 0%, 10%'); |
|
| 155 | - $vars->add('--spip-color-black--hsl', '0, 0%, 0%'); |
|
| 156 | - |
|
| 157 | - $vars->add('--spip-color-white', 'hsl(var(--spip-color-white--hsl))'); |
|
| 158 | - $vars->add('--spip-color-gray-lightest', 'hsl(var(--spip-color-gray-lightest--hsl))'); |
|
| 159 | - $vars->add('--spip-color-gray-lighter', 'hsl(var(--spip-color-gray-lighter--hsl))'); |
|
| 160 | - $vars->add('--spip-color-gray-light', 'hsl(var(--spip-color-gray-light--hsl))'); |
|
| 161 | - $vars->add('--spip-color-gray', 'hsl(var(--spip-color-gray--hsl))'); |
|
| 162 | - $vars->add('--spip-color-gray-dark', 'hsl(var(--spip-color-gray-dark--hsl))'); |
|
| 163 | - $vars->add('--spip-color-gray-darker', 'hsl(var(--spip-color-gray-darker--hsl))'); |
|
| 164 | - $vars->add('--spip-color-gray-darkest', 'hsl(var(--spip-color-gray-darkest--hsl))'); |
|
| 165 | - $vars->add('--spip-color-black', 'hsl(var(--spip-color-black--hsl))'); |
|
| 166 | - |
|
| 167 | - // Différents états : erreur, etc. |
|
| 168 | - $vars->add('--spip-color-success--hsl', '72, 66%, 62%'); |
|
| 169 | - $vars->add('--spip-color-success--h', '72'); |
|
| 170 | - $vars->add('--spip-color-success--s', '66%'); |
|
| 171 | - $vars->add('--spip-color-success--l', '62%'); |
|
| 172 | - $vars->add('--spip-color-error--hsl', '356, 70%, 57%'); |
|
| 173 | - $vars->add('--spip-color-error--h', '356'); |
|
| 174 | - $vars->add('--spip-color-error--s', '70%'); |
|
| 175 | - $vars->add('--spip-color-error--l', '57%'); |
|
| 176 | - $vars->add('--spip-color-notice--hsl', '47, 100%, 62%'); |
|
| 177 | - $vars->add('--spip-color-notice--h', '47'); |
|
| 178 | - $vars->add('--spip-color-notice--s', '100%'); |
|
| 179 | - $vars->add('--spip-color-notice--l', '62%'); |
|
| 180 | - $vars->add('--spip-color-info--hsl', '197, 56%, 27%'); |
|
| 181 | - $vars->add('--spip-color-info--h', '197'); |
|
| 182 | - $vars->add('--spip-color-info--s', '56%'); |
|
| 183 | - $vars->add('--spip-color-info--l', '27%'); |
|
| 184 | - |
|
| 185 | - $vars->add('--spip-color-success', 'hsl(var(--spip-color-success--hsl))'); |
|
| 186 | - $vars->add('--spip-color-error', 'hsl(var(--spip-color-error--hsl))'); |
|
| 187 | - $vars->add('--spip-color-notice', 'hsl(var(--spip-color-notice--hsl))'); |
|
| 188 | - $vars->add('--spip-color-info', 'hsl(var(--spip-color-info--hsl))'); |
|
| 189 | - |
|
| 190 | - return $vars; |
|
| 123 | + $vars = new \Spip_Css_Vars_Collection(); |
|
| 124 | + |
|
| 125 | + // nos déclinaisons de couleur (basées sur le dégradé précedent, où 60 est là couleur du thème) |
|
| 126 | + $vars->add('--spip-color-theme-white--hsl', 'var(--spip-color-theme--100)'); |
|
| 127 | + $vars->add('--spip-color-theme-lightest--hsl', 'var(--spip-color-theme--95)'); |
|
| 128 | + $vars->add('--spip-color-theme-lighter--hsl', 'var(--spip-color-theme--90)'); |
|
| 129 | + $vars->add('--spip-color-theme-light--hsl', 'var(--spip-color-theme--80)'); |
|
| 130 | + $vars->add('--spip-color-theme--hsl', 'var(--spip-color-theme--60)'); |
|
| 131 | + $vars->add('--spip-color-theme-dark--hsl', 'var(--spip-color-theme--40)'); |
|
| 132 | + $vars->add('--spip-color-theme-darker--hsl', 'var(--spip-color-theme--20)'); |
|
| 133 | + $vars->add('--spip-color-theme-darkest--hsl', 'var(--spip-color-theme--10)'); |
|
| 134 | + $vars->add('--spip-color-theme-black--hsl', 'var(--spip-color-theme--00)'); |
|
| 135 | + |
|
| 136 | + $vars->add('--spip-color-theme-white', 'hsl(var(--spip-color-theme-white--hsl))'); |
|
| 137 | + $vars->add('--spip-color-theme-lightest', 'hsl(var(--spip-color-theme-lightest--hsl))'); |
|
| 138 | + $vars->add('--spip-color-theme-lighter', 'hsl(var(--spip-color-theme-lighter--hsl))'); |
|
| 139 | + $vars->add('--spip-color-theme-light', 'hsl(var(--spip-color-theme-light--hsl))'); |
|
| 140 | + $vars->add('--spip-color-theme', 'hsl(var(--spip-color-theme--hsl))'); |
|
| 141 | + $vars->add('--spip-color-theme-dark', 'hsl(var(--spip-color-theme-dark--hsl))'); |
|
| 142 | + $vars->add('--spip-color-theme-darker', 'hsl(var(--spip-color-theme-darker--hsl))'); |
|
| 143 | + $vars->add('--spip-color-theme-darkest', 'hsl(var(--spip-color-theme-darkest--hsl))'); |
|
| 144 | + $vars->add('--spip-color-theme-black', 'hsl(var(--spip-color-theme-black--hsl))'); |
|
| 145 | + |
|
| 146 | + // déclinaisons de gris (luminosité calquée sur le dégradé de couleur) |
|
| 147 | + $vars->add('--spip-color-white--hsl', '0, 0%, 100%'); |
|
| 148 | + $vars->add('--spip-color-gray-lightest--hsl', '0, 0%, 96%'); |
|
| 149 | + $vars->add('--spip-color-gray-lighter--hsl', '0, 0%, 90%'); |
|
| 150 | + $vars->add('--spip-color-gray-light--hsl', '0, 0%, 80%'); |
|
| 151 | + $vars->add('--spip-color-gray--hsl', '0, 0%, 60%'); |
|
| 152 | + $vars->add('--spip-color-gray-dark--hsl', '0, 0%, 40%'); |
|
| 153 | + $vars->add('--spip-color-gray-darker--hsl', '0, 0%, 20%'); |
|
| 154 | + $vars->add('--spip-color-gray-darkest--hsl', '0, 0%, 10%'); |
|
| 155 | + $vars->add('--spip-color-black--hsl', '0, 0%, 0%'); |
|
| 156 | + |
|
| 157 | + $vars->add('--spip-color-white', 'hsl(var(--spip-color-white--hsl))'); |
|
| 158 | + $vars->add('--spip-color-gray-lightest', 'hsl(var(--spip-color-gray-lightest--hsl))'); |
|
| 159 | + $vars->add('--spip-color-gray-lighter', 'hsl(var(--spip-color-gray-lighter--hsl))'); |
|
| 160 | + $vars->add('--spip-color-gray-light', 'hsl(var(--spip-color-gray-light--hsl))'); |
|
| 161 | + $vars->add('--spip-color-gray', 'hsl(var(--spip-color-gray--hsl))'); |
|
| 162 | + $vars->add('--spip-color-gray-dark', 'hsl(var(--spip-color-gray-dark--hsl))'); |
|
| 163 | + $vars->add('--spip-color-gray-darker', 'hsl(var(--spip-color-gray-darker--hsl))'); |
|
| 164 | + $vars->add('--spip-color-gray-darkest', 'hsl(var(--spip-color-gray-darkest--hsl))'); |
|
| 165 | + $vars->add('--spip-color-black', 'hsl(var(--spip-color-black--hsl))'); |
|
| 166 | + |
|
| 167 | + // Différents états : erreur, etc. |
|
| 168 | + $vars->add('--spip-color-success--hsl', '72, 66%, 62%'); |
|
| 169 | + $vars->add('--spip-color-success--h', '72'); |
|
| 170 | + $vars->add('--spip-color-success--s', '66%'); |
|
| 171 | + $vars->add('--spip-color-success--l', '62%'); |
|
| 172 | + $vars->add('--spip-color-error--hsl', '356, 70%, 57%'); |
|
| 173 | + $vars->add('--spip-color-error--h', '356'); |
|
| 174 | + $vars->add('--spip-color-error--s', '70%'); |
|
| 175 | + $vars->add('--spip-color-error--l', '57%'); |
|
| 176 | + $vars->add('--spip-color-notice--hsl', '47, 100%, 62%'); |
|
| 177 | + $vars->add('--spip-color-notice--h', '47'); |
|
| 178 | + $vars->add('--spip-color-notice--s', '100%'); |
|
| 179 | + $vars->add('--spip-color-notice--l', '62%'); |
|
| 180 | + $vars->add('--spip-color-info--hsl', '197, 56%, 27%'); |
|
| 181 | + $vars->add('--spip-color-info--h', '197'); |
|
| 182 | + $vars->add('--spip-color-info--s', '56%'); |
|
| 183 | + $vars->add('--spip-color-info--l', '27%'); |
|
| 184 | + |
|
| 185 | + $vars->add('--spip-color-success', 'hsl(var(--spip-color-success--hsl))'); |
|
| 186 | + $vars->add('--spip-color-error', 'hsl(var(--spip-color-error--hsl))'); |
|
| 187 | + $vars->add('--spip-color-notice', 'hsl(var(--spip-color-notice--hsl))'); |
|
| 188 | + $vars->add('--spip-color-info', 'hsl(var(--spip-color-info--hsl))'); |
|
| 189 | + |
|
| 190 | + return $vars; |
|
| 191 | 191 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | * @package SPIP\Core\Fonctions |
| 17 | 17 | **/ |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -29,29 +29,29 @@ discard block |
||
| 29 | 29 | * @param bool $left |
| 30 | 30 | */ |
| 31 | 31 | function critere_compteur_articles_filtres_dist($idb, &$boucles, $crit, $left = false) { |
| 32 | - $boucle = &$boucles[$idb]; |
|
| 32 | + $boucle = &$boucles[$idb]; |
|
| 33 | 33 | |
| 34 | - $_statut = calculer_liste($crit->param[0], [], $boucles, $boucle->id_parent); |
|
| 34 | + $_statut = calculer_liste($crit->param[0], [], $boucles, $boucle->id_parent); |
|
| 35 | 35 | |
| 36 | - $not = ''; |
|
| 37 | - if ($crit->not) { |
|
| 38 | - $not = ", 'NOT'"; |
|
| 39 | - } |
|
| 40 | - $boucle->from['LAA'] = 'spip_auteurs_liens'; |
|
| 41 | - $boucle->from_type['LAA'] = 'left'; |
|
| 42 | - $boucle->join['LAA'] = ["'auteurs'", "'id_auteur'", "'id_auteur'", "'LAA.objet=\'article\''"]; |
|
| 36 | + $not = ''; |
|
| 37 | + if ($crit->not) { |
|
| 38 | + $not = ", 'NOT'"; |
|
| 39 | + } |
|
| 40 | + $boucle->from['LAA'] = 'spip_auteurs_liens'; |
|
| 41 | + $boucle->from_type['LAA'] = 'left'; |
|
| 42 | + $boucle->join['LAA'] = ["'auteurs'", "'id_auteur'", "'id_auteur'", "'LAA.objet=\'article\''"]; |
|
| 43 | 43 | |
| 44 | - $boucle->from['articles'] = 'spip_articles'; |
|
| 45 | - $boucle->from_type['articles'] = 'left'; |
|
| 46 | - $boucle->join['articles'] = [ |
|
| 47 | - "'LAA'", |
|
| 48 | - "'id_article'", |
|
| 49 | - "'id_objet'", |
|
| 50 | - "'(articles.statut IS NULL OR '.sql_in('articles.statut',_q($_statut)$not).')'" |
|
| 51 | - ]; |
|
| 44 | + $boucle->from['articles'] = 'spip_articles'; |
|
| 45 | + $boucle->from_type['articles'] = 'left'; |
|
| 46 | + $boucle->join['articles'] = [ |
|
| 47 | + "'LAA'", |
|
| 48 | + "'id_article'", |
|
| 49 | + "'id_objet'", |
|
| 50 | + "'(articles.statut IS NULL OR '.sql_in('articles.statut',_q($_statut)$not).')'" |
|
| 51 | + ]; |
|
| 52 | 52 | |
| 53 | - $boucle->select[] = 'COUNT(articles.id_article) AS compteur_articles'; |
|
| 54 | - $boucle->group[] = 'auteurs.id_auteur'; |
|
| 53 | + $boucle->select[] = 'COUNT(articles.id_article) AS compteur_articles'; |
|
| 54 | + $boucle->group[] = 'auteurs.id_auteur'; |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | * Pile complétée par le code à générer |
| 70 | 70 | */ |
| 71 | 71 | function balise_COMPTEUR_ARTICLES_dist($p) { |
| 72 | - return rindex_pile($p, 'compteur_articles', 'compteur_articles_filtres'); |
|
| 72 | + return rindex_pile($p, 'compteur_articles', 'compteur_articles_filtres'); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | |
@@ -85,44 +85,44 @@ discard block |
||
| 85 | 85 | * @return string |
| 86 | 86 | */ |
| 87 | 87 | function afficher_initiale($url, $initiale, $compteur, $debut, $pas) { |
| 88 | - static $memo = null; |
|
| 89 | - static $res = []; |
|
| 90 | - $out = ''; |
|
| 91 | - if ( |
|
| 92 | - !$memo |
|
| 93 | - or (!$initiale and !$url) |
|
| 94 | - or ($initiale !== $memo['initiale']) |
|
| 95 | - ) { |
|
| 96 | - $newcompt = intval(floor(($compteur - 1) / $pas) * $pas); |
|
| 97 | - // si fin de la pagination et une seule entree, ne pas l'afficher, ca ne sert a rien |
|
| 98 | - if (!$initiale and !$url and !$memo['compteur']) { |
|
| 99 | - $memo = null; |
|
| 100 | - } |
|
| 101 | - if ($memo) { |
|
| 102 | - $on = (($memo['compteur'] <= $debut) |
|
| 103 | - and ( |
|
| 104 | - $newcompt > $debut or ($newcompt == $debut and $newcompt == $memo['compteur']) |
|
| 105 | - )); |
|
| 106 | - $res[] = lien_ou_expose($memo['url'], $memo['initiale'], $on, 'lien_pagination'); |
|
| 107 | - } |
|
| 108 | - if ($initiale) { |
|
| 109 | - $memo = [ |
|
| 110 | - 'entree' => isset($memo['entree']) ? $memo['entree'] + 1 : 0, |
|
| 111 | - 'initiale' => $initiale, |
|
| 112 | - 'url' => parametre_url($url, 'i', $initiale), |
|
| 113 | - 'compteur' => $newcompt |
|
| 114 | - ]; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - if (!$initiale and !$url) { |
|
| 118 | - if ((is_countable($res) ? count($res) : 0) > 1) { |
|
| 119 | - $out = implode(' ', $res); |
|
| 120 | - } |
|
| 121 | - $memo = null; |
|
| 122 | - $res = []; |
|
| 123 | - } |
|
| 88 | + static $memo = null; |
|
| 89 | + static $res = []; |
|
| 90 | + $out = ''; |
|
| 91 | + if ( |
|
| 92 | + !$memo |
|
| 93 | + or (!$initiale and !$url) |
|
| 94 | + or ($initiale !== $memo['initiale']) |
|
| 95 | + ) { |
|
| 96 | + $newcompt = intval(floor(($compteur - 1) / $pas) * $pas); |
|
| 97 | + // si fin de la pagination et une seule entree, ne pas l'afficher, ca ne sert a rien |
|
| 98 | + if (!$initiale and !$url and !$memo['compteur']) { |
|
| 99 | + $memo = null; |
|
| 100 | + } |
|
| 101 | + if ($memo) { |
|
| 102 | + $on = (($memo['compteur'] <= $debut) |
|
| 103 | + and ( |
|
| 104 | + $newcompt > $debut or ($newcompt == $debut and $newcompt == $memo['compteur']) |
|
| 105 | + )); |
|
| 106 | + $res[] = lien_ou_expose($memo['url'], $memo['initiale'], $on, 'lien_pagination'); |
|
| 107 | + } |
|
| 108 | + if ($initiale) { |
|
| 109 | + $memo = [ |
|
| 110 | + 'entree' => isset($memo['entree']) ? $memo['entree'] + 1 : 0, |
|
| 111 | + 'initiale' => $initiale, |
|
| 112 | + 'url' => parametre_url($url, 'i', $initiale), |
|
| 113 | + 'compteur' => $newcompt |
|
| 114 | + ]; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + if (!$initiale and !$url) { |
|
| 118 | + if ((is_countable($res) ? count($res) : 0) > 1) { |
|
| 119 | + $out = implode(' ', $res); |
|
| 120 | + } |
|
| 121 | + $memo = null; |
|
| 122 | + $res = []; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - return $out; |
|
| 125 | + return $out; |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | /** |
@@ -141,24 +141,24 @@ discard block |
||
| 141 | 141 | * @return string |
| 142 | 142 | */ |
| 143 | 143 | function auteur_lien_messagerie($id_auteur, $en_ligne, $statut, $imessage, $email = '') { |
| 144 | - static $time = null; |
|
| 145 | - if (!in_array($statut, ['0minirezo', '1comite'])) { |
|
| 146 | - return ''; |
|
| 147 | - } |
|
| 144 | + static $time = null; |
|
| 145 | + if (!in_array($statut, ['0minirezo', '1comite'])) { |
|
| 146 | + return ''; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - if (is_null($time)) { |
|
| 150 | - $time = time(); |
|
| 151 | - } |
|
| 152 | - $parti = (($time - strtotime($en_ligne)) > 15 * 60); |
|
| 149 | + if (is_null($time)) { |
|
| 150 | + $time = time(); |
|
| 151 | + } |
|
| 152 | + $parti = (($time - strtotime($en_ligne)) > 15 * 60); |
|
| 153 | 153 | |
| 154 | - if ( |
|
| 155 | - $imessage != 'non' and !$parti // historique : est-ce que ca a encore un sens de limiter vu qu'on a la notification par email ? |
|
| 156 | - and $GLOBALS['meta']['messagerie_agenda'] != 'non' |
|
| 157 | - ) { |
|
| 158 | - return parametre_url(parametre_url(generer_url_ecrire('message_edit', 'new=oui'), 'to', $id_auteur), 'redirect', self()); |
|
| 159 | - } elseif (strlen($email) and autoriser('voir', 'auteur', $id_auteur)) { |
|
| 160 | - return 'mailto:' . $email; |
|
| 161 | - } else { |
|
| 162 | - return ''; |
|
| 163 | - } |
|
| 154 | + if ( |
|
| 155 | + $imessage != 'non' and !$parti // historique : est-ce que ca a encore un sens de limiter vu qu'on a la notification par email ? |
|
| 156 | + and $GLOBALS['meta']['messagerie_agenda'] != 'non' |
|
| 157 | + ) { |
|
| 158 | + return parametre_url(parametre_url(generer_url_ecrire('message_edit', 'new=oui'), 'to', $id_auteur), 'redirect', self()); |
|
| 159 | + } elseif (strlen($email) and autoriser('voir', 'auteur', $id_auteur)) { |
|
| 160 | + return 'mailto:' . $email; |
|
| 161 | + } else { |
|
| 162 | + return ''; |
|
| 163 | + } |
|
| 164 | 164 | } |