@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | **/ |
| 20 | 20 | |
| 21 | 21 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 22 | - return; |
|
| 22 | + return; |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | include_spip('iterateur/data'); |
@@ -37,10 +37,10 @@ discard block |
||
| 37 | 37 | * Description de la boucle complétée des champs |
| 38 | 38 | */ |
| 39 | 39 | function iterateur_CONDITION_dist($b) { |
| 40 | - $b->iterateur = 'CONDITION'; # designe la classe d'iterateur |
|
| 41 | - $b->show = [ |
|
| 42 | - 'field' => [] |
|
| 43 | - ]; |
|
| 40 | + $b->iterateur = 'CONDITION'; # designe la classe d'iterateur |
|
| 41 | + $b->show = [ |
|
| 42 | + 'field' => [] |
|
| 43 | + ]; |
|
| 44 | 44 | |
| 45 | - return $b; |
|
| 45 | + return $b; |
|
| 46 | 46 | } |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | **/ |
| 20 | 20 | |
| 21 | 21 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 22 | - return; |
|
| 22 | + return; |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | /** |
@@ -38,13 +38,13 @@ discard block |
||
| 38 | 38 | * Description de la boucle complétée des champs |
| 39 | 39 | */ |
| 40 | 40 | function iterateur_POUR_dist($b) { |
| 41 | - $b->iterateur = 'DATA'; # designe la classe d'iterateur |
|
| 42 | - $b->show = [ |
|
| 43 | - 'field' => [ |
|
| 44 | - 'cle' => 'STRING', |
|
| 45 | - 'valeur' => 'STRING', |
|
| 46 | - ] |
|
| 47 | - ]; |
|
| 41 | + $b->iterateur = 'DATA'; # designe la classe d'iterateur |
|
| 42 | + $b->show = [ |
|
| 43 | + 'field' => [ |
|
| 44 | + 'cle' => 'STRING', |
|
| 45 | + 'valeur' => 'STRING', |
|
| 46 | + ] |
|
| 47 | + ]; |
|
| 48 | 48 | |
| 49 | - return $b; |
|
| 49 | + return $b; |
|
| 50 | 50 | } |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | |
| 22 | 22 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 23 | - return; |
|
| 23 | + return; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | |
@@ -38,17 +38,17 @@ discard block |
||
| 38 | 38 | * Description de la boucle complétée des champs |
| 39 | 39 | */ |
| 40 | 40 | function iterateur_php_dist($b, $iteratorName) { |
| 41 | - $b->iterateur = $iteratorName; # designe la classe d'iterateur |
|
| 42 | - $b->show = [ |
|
| 43 | - 'field' => [ |
|
| 44 | - 'cle' => 'STRING', |
|
| 45 | - 'valeur' => 'STRING', |
|
| 46 | - ] |
|
| 47 | - ]; |
|
| 48 | - |
|
| 49 | - foreach (get_class_methods($iteratorName) as $method) { |
|
| 50 | - $b->show['field'][strtolower($method)] = 'METHOD'; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - return $b; |
|
| 41 | + $b->iterateur = $iteratorName; # designe la classe d'iterateur |
|
| 42 | + $b->show = [ |
|
| 43 | + 'field' => [ |
|
| 44 | + 'cle' => 'STRING', |
|
| 45 | + 'valeur' => 'STRING', |
|
| 46 | + ] |
|
| 47 | + ]; |
|
| 48 | + |
|
| 49 | + foreach (get_class_methods($iteratorName) as $method) { |
|
| 50 | + $b->show['field'][strtolower($method)] = 'METHOD'; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + return $b; |
|
| 54 | 54 | } |
@@ -16,10 +16,10 @@ discard block |
||
| 16 | 16 | * @package SPIP\Core\Pipelines |
| 17 | 17 | **/ |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | if (test_espace_prive()) { |
| 22 | - include_spip('inc/pipelines_ecrire'); |
|
| 22 | + include_spip('inc/pipelines_ecrire'); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | |
@@ -45,28 +45,28 @@ discard block |
||
| 45 | 45 | * @return string Contenu qui sera inséré dans le head HTML |
| 46 | 46 | **/ |
| 47 | 47 | function f_jQuery($texte) { |
| 48 | - $x = ''; |
|
| 49 | - $jquery_plugins = pipeline( |
|
| 50 | - 'jquery_plugins', |
|
| 51 | - [ |
|
| 52 | - 'javascript/jquery.js', |
|
| 53 | - 'javascript/jquery.form.js', |
|
| 54 | - 'javascript/jquery.autosave.js', |
|
| 55 | - 'javascript/jquery.placeholder-label.js', |
|
| 56 | - 'javascript/ajaxCallback.js', |
|
| 57 | - 'javascript/js.cookie.js', |
|
| 58 | - ] |
|
| 59 | - ); |
|
| 60 | - foreach (array_unique($jquery_plugins) as $script) { |
|
| 61 | - if ($script = find_in_path(supprimer_timestamp($script))) { |
|
| 62 | - $script = timestamp($script); |
|
| 63 | - $x .= "\n<script src=\"$script\" type=\"text/javascript\"></script>\n"; |
|
| 64 | - } |
|
| 65 | - } |
|
| 48 | + $x = ''; |
|
| 49 | + $jquery_plugins = pipeline( |
|
| 50 | + 'jquery_plugins', |
|
| 51 | + [ |
|
| 52 | + 'javascript/jquery.js', |
|
| 53 | + 'javascript/jquery.form.js', |
|
| 54 | + 'javascript/jquery.autosave.js', |
|
| 55 | + 'javascript/jquery.placeholder-label.js', |
|
| 56 | + 'javascript/ajaxCallback.js', |
|
| 57 | + 'javascript/js.cookie.js', |
|
| 58 | + ] |
|
| 59 | + ); |
|
| 60 | + foreach (array_unique($jquery_plugins) as $script) { |
|
| 61 | + if ($script = find_in_path(supprimer_timestamp($script))) { |
|
| 62 | + $script = timestamp($script); |
|
| 63 | + $x .= "\n<script src=\"$script\" type=\"text/javascript\"></script>\n"; |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $texte = $x . $texte; |
|
| 67 | + $texte = $x . $texte; |
|
| 68 | 68 | |
| 69 | - return $texte; |
|
| 69 | + return $texte; |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | |
@@ -85,28 +85,28 @@ discard block |
||
| 85 | 85 | * @return string Contenu de la page envoyée au navigateur |
| 86 | 86 | **/ |
| 87 | 87 | function f_surligne($texte) { |
| 88 | - if (!$GLOBALS['html']) { |
|
| 89 | - return $texte; |
|
| 90 | - } |
|
| 91 | - $rech = _request('var_recherche'); |
|
| 92 | - if ( |
|
| 93 | - !$rech |
|
| 94 | - and (!defined('_SURLIGNE_RECHERCHE_REFERERS') |
|
| 95 | - or !_SURLIGNE_RECHERCHE_REFERERS |
|
| 96 | - or !isset($_SERVER['HTTP_REFERER'])) |
|
| 97 | - ) { |
|
| 98 | - return $texte; |
|
| 99 | - } |
|
| 100 | - include_spip('inc/surligne'); |
|
| 88 | + if (!$GLOBALS['html']) { |
|
| 89 | + return $texte; |
|
| 90 | + } |
|
| 91 | + $rech = _request('var_recherche'); |
|
| 92 | + if ( |
|
| 93 | + !$rech |
|
| 94 | + and (!defined('_SURLIGNE_RECHERCHE_REFERERS') |
|
| 95 | + or !_SURLIGNE_RECHERCHE_REFERERS |
|
| 96 | + or !isset($_SERVER['HTTP_REFERER'])) |
|
| 97 | + ) { |
|
| 98 | + return $texte; |
|
| 99 | + } |
|
| 100 | + include_spip('inc/surligne'); |
|
| 101 | 101 | |
| 102 | - if (isset($_SERVER['HTTP_REFERER'])) { |
|
| 103 | - $_SERVER['HTTP_REFERER'] = preg_replace(',[^\w\,/#&;:-]+,', ' ', $_SERVER['HTTP_REFERER']); |
|
| 104 | - } |
|
| 105 | - if ($rech) { |
|
| 106 | - $rech = preg_replace(',[^\w\,/#&;:-]+,', ' ', $rech); |
|
| 107 | - } |
|
| 102 | + if (isset($_SERVER['HTTP_REFERER'])) { |
|
| 103 | + $_SERVER['HTTP_REFERER'] = preg_replace(',[^\w\,/#&;:-]+,', ' ', $_SERVER['HTTP_REFERER']); |
|
| 104 | + } |
|
| 105 | + if ($rech) { |
|
| 106 | + $rech = preg_replace(',[^\w\,/#&;:-]+,', ' ', $rech); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - return surligner_mots($texte, $rech); |
|
| 109 | + return surligner_mots($texte, $rech); |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
@@ -123,33 +123,33 @@ discard block |
||
| 123 | 123 | * @return string Contenu de la page envoyée au navigateur |
| 124 | 124 | **/ |
| 125 | 125 | function f_tidy($texte) { |
| 126 | - /** |
|
| 127 | - * Indentation à faire ? |
|
| 128 | - * |
|
| 129 | - * - true : actif. |
|
| 130 | - * - false par défaut. |
|
| 131 | - */ |
|
| 126 | + /** |
|
| 127 | + * Indentation à faire ? |
|
| 128 | + * |
|
| 129 | + * - true : actif. |
|
| 130 | + * - false par défaut. |
|
| 131 | + */ |
|
| 132 | 132 | |
| 133 | - if ( |
|
| 134 | - $GLOBALS['xhtml'] # tidy demande |
|
| 135 | - and $GLOBALS['html'] # verifie que la page avait l'entete text/html |
|
| 136 | - and strlen($texte) |
|
| 137 | - and !headers_sent() |
|
| 138 | - ) { |
|
| 139 | - # Compatibilite ascendante |
|
| 140 | - if (!is_string($GLOBALS['xhtml'])) { |
|
| 141 | - $GLOBALS['xhtml'] = 'tidy'; |
|
| 142 | - } |
|
| 133 | + if ( |
|
| 134 | + $GLOBALS['xhtml'] # tidy demande |
|
| 135 | + and $GLOBALS['html'] # verifie que la page avait l'entete text/html |
|
| 136 | + and strlen($texte) |
|
| 137 | + and !headers_sent() |
|
| 138 | + ) { |
|
| 139 | + # Compatibilite ascendante |
|
| 140 | + if (!is_string($GLOBALS['xhtml'])) { |
|
| 141 | + $GLOBALS['xhtml'] = 'tidy'; |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - if (!$f = charger_fonction($GLOBALS['xhtml'], 'inc', true)) { |
|
| 145 | - spip_log("tidy absent, l'indenteur SPIP le remplace"); |
|
| 146 | - $f = charger_fonction('sax', 'xml'); |
|
| 147 | - } |
|
| 144 | + if (!$f = charger_fonction($GLOBALS['xhtml'], 'inc', true)) { |
|
| 145 | + spip_log("tidy absent, l'indenteur SPIP le remplace"); |
|
| 146 | + $f = charger_fonction('sax', 'xml'); |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - return $f($texte); |
|
| 150 | - } |
|
| 149 | + return $f($texte); |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - return $texte; |
|
| 152 | + return $texte; |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | |
@@ -168,21 +168,21 @@ discard block |
||
| 168 | 168 | * @return string Contenu de la page envoyée au navigateur |
| 169 | 169 | **/ |
| 170 | 170 | function f_insert_head($texte) { |
| 171 | - if (!$GLOBALS['html']) { |
|
| 172 | - return $texte; |
|
| 173 | - } |
|
| 174 | - include_spip('public/admin'); // pour strripos |
|
| 171 | + if (!$GLOBALS['html']) { |
|
| 172 | + return $texte; |
|
| 173 | + } |
|
| 174 | + include_spip('public/admin'); // pour strripos |
|
| 175 | 175 | |
| 176 | - ($pos = stripos($texte, '</head>')) |
|
| 177 | - || ($pos = stripos($texte, '<body>')) |
|
| 178 | - || ($pos = 0); |
|
| 176 | + ($pos = stripos($texte, '</head>')) |
|
| 177 | + || ($pos = stripos($texte, '<body>')) |
|
| 178 | + || ($pos = 0); |
|
| 179 | 179 | |
| 180 | - if (!str_contains(substr($texte, 0, $pos), '<!-- insert_head -->')) { |
|
| 181 | - $insert = "\n" . pipeline('insert_head', '<!-- f_insert_head -->') . "\n"; |
|
| 182 | - $texte = substr_replace($texte, $insert, $pos, 0); |
|
| 183 | - } |
|
| 180 | + if (!str_contains(substr($texte, 0, $pos), '<!-- insert_head -->')) { |
|
| 181 | + $insert = "\n" . pipeline('insert_head', '<!-- f_insert_head -->') . "\n"; |
|
| 182 | + $texte = substr_replace($texte, $insert, $pos, 0); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - return $texte; |
|
| 185 | + return $texte; |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | |
@@ -198,34 +198,34 @@ discard block |
||
| 198 | 198 | * @return string Contenu de la page envoyée au navigateur |
| 199 | 199 | **/ |
| 200 | 200 | function f_admin($texte) { |
| 201 | - if (defined('_VAR_PREVIEW') and _VAR_PREVIEW and $GLOBALS['html']) { |
|
| 202 | - include_spip('inc/filtres'); // pour http_img_pack |
|
| 203 | - $x = "<div class='spip-previsu' " |
|
| 204 | - . http_style_background('preview-32.png', '', 32) |
|
| 205 | - . '>' |
|
| 206 | - . _T('previsualisation') |
|
| 207 | - . '</div>'; |
|
| 208 | - if (!$pos = stripos($texte, '</body>')) { |
|
| 209 | - $pos = strlen($texte); |
|
| 210 | - } |
|
| 211 | - $texte = substr_replace($texte, $x, $pos, 0); |
|
| 212 | - // pas de preview en fenetre enfant |
|
| 213 | - $x = "<script type='text/javascript'>const frameEl = window.frameElement;if (frameEl) {frameEl.sandbox='sandbox';window.location.href='" . addslashes($GLOBALS['meta']['adresse_site']) . "';}</script>"; |
|
| 214 | - if (!$pos = stripos($texte, '<head') or !$pos = strpos($texte, '>', $pos)) { |
|
| 215 | - $pos = -1; |
|
| 216 | - } |
|
| 217 | - $texte = substr_replace($texte, $x, $pos + 1, 0); |
|
| 218 | - } |
|
| 201 | + if (defined('_VAR_PREVIEW') and _VAR_PREVIEW and $GLOBALS['html']) { |
|
| 202 | + include_spip('inc/filtres'); // pour http_img_pack |
|
| 203 | + $x = "<div class='spip-previsu' " |
|
| 204 | + . http_style_background('preview-32.png', '', 32) |
|
| 205 | + . '>' |
|
| 206 | + . _T('previsualisation') |
|
| 207 | + . '</div>'; |
|
| 208 | + if (!$pos = stripos($texte, '</body>')) { |
|
| 209 | + $pos = strlen($texte); |
|
| 210 | + } |
|
| 211 | + $texte = substr_replace($texte, $x, $pos, 0); |
|
| 212 | + // pas de preview en fenetre enfant |
|
| 213 | + $x = "<script type='text/javascript'>const frameEl = window.frameElement;if (frameEl) {frameEl.sandbox='sandbox';window.location.href='" . addslashes($GLOBALS['meta']['adresse_site']) . "';}</script>"; |
|
| 214 | + if (!$pos = stripos($texte, '<head') or !$pos = strpos($texte, '>', $pos)) { |
|
| 215 | + $pos = -1; |
|
| 216 | + } |
|
| 217 | + $texte = substr_replace($texte, $x, $pos + 1, 0); |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - if (isset($GLOBALS['affiche_boutons_admin']) and $GLOBALS['affiche_boutons_admin']) { |
|
| 221 | - include_spip('public/admin'); |
|
| 222 | - $texte = affiche_boutons_admin($texte); |
|
| 223 | - } |
|
| 224 | - if (_request('var_mode') == 'noajax') { |
|
| 225 | - $texte = preg_replace(',(class=[\'"][^\'"]*)ajax([^\'"]*[\'"]),Uims', "\\1\\2", $texte); |
|
| 226 | - } |
|
| 220 | + if (isset($GLOBALS['affiche_boutons_admin']) and $GLOBALS['affiche_boutons_admin']) { |
|
| 221 | + include_spip('public/admin'); |
|
| 222 | + $texte = affiche_boutons_admin($texte); |
|
| 223 | + } |
|
| 224 | + if (_request('var_mode') == 'noajax') { |
|
| 225 | + $texte = preg_replace(',(class=[\'"][^\'"]*)ajax([^\'"]*[\'"]),Uims', "\\1\\2", $texte); |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - return $texte; |
|
| 228 | + return $texte; |
|
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | /** |
@@ -243,11 +243,11 @@ discard block |
||
| 243 | 243 | * @return array $flux Description et contenu de l'inclusion |
| 244 | 244 | **/ |
| 245 | 245 | function f_recuperer_fond($flux) { |
| 246 | - if (!test_espace_prive()) { |
|
| 247 | - return $flux; |
|
| 248 | - } |
|
| 246 | + if (!test_espace_prive()) { |
|
| 247 | + return $flux; |
|
| 248 | + } |
|
| 249 | 249 | |
| 250 | - return f_afficher_blocs_ecrire($flux); |
|
| 250 | + return f_afficher_blocs_ecrire($flux); |
|
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | /** |
@@ -261,30 +261,30 @@ discard block |
||
| 261 | 261 | * @return string Contenu de la page envoyée au navigateur |
| 262 | 262 | */ |
| 263 | 263 | function f_queue($texte) { |
| 264 | - // eviter une inclusion si rien a faire |
|
| 265 | - if ( |
|
| 266 | - _request('action') == 'cron' |
|
| 267 | - or queue_sleep_time_to_next_job() > 0 |
|
| 268 | - or defined('_DEBUG_BLOCK_QUEUE') |
|
| 269 | - ) { |
|
| 270 | - return $texte; |
|
| 271 | - } |
|
| 264 | + // eviter une inclusion si rien a faire |
|
| 265 | + if ( |
|
| 266 | + _request('action') == 'cron' |
|
| 267 | + or queue_sleep_time_to_next_job() > 0 |
|
| 268 | + or defined('_DEBUG_BLOCK_QUEUE') |
|
| 269 | + ) { |
|
| 270 | + return $texte; |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | - include_spip('inc/queue'); |
|
| 274 | - $code = queue_affichage_cron(); |
|
| 273 | + include_spip('inc/queue'); |
|
| 274 | + $code = queue_affichage_cron(); |
|
| 275 | 275 | |
| 276 | - // si rien a afficher |
|
| 277 | - // ou si on est pas dans une page html, on ne sait rien faire de mieux |
|
| 278 | - if (!$code or !isset($GLOBALS['html']) or !$GLOBALS['html']) { |
|
| 279 | - return $texte; |
|
| 280 | - } |
|
| 276 | + // si rien a afficher |
|
| 277 | + // ou si on est pas dans une page html, on ne sait rien faire de mieux |
|
| 278 | + if (!$code or !isset($GLOBALS['html']) or !$GLOBALS['html']) { |
|
| 279 | + return $texte; |
|
| 280 | + } |
|
| 281 | 281 | |
| 282 | - // inserer avant le </body> fermant si on peut, a la fin de la page sinon |
|
| 283 | - if (($p = strpos($texte, '</body>')) !== false) { |
|
| 284 | - $texte = substr($texte, 0, $p) . $code . substr($texte, $p); |
|
| 285 | - } else { |
|
| 286 | - $texte .= $code; |
|
| 287 | - } |
|
| 282 | + // inserer avant le </body> fermant si on peut, a la fin de la page sinon |
|
| 283 | + if (($p = strpos($texte, '</body>')) !== false) { |
|
| 284 | + $texte = substr($texte, 0, $p) . $code . substr($texte, $p); |
|
| 285 | + } else { |
|
| 286 | + $texte .= $code; |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - return $texte; |
|
| 289 | + return $texte; |
|
| 290 | 290 | } |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - $texte = $x . $texte; |
|
| 67 | + $texte = $x.$texte; |
|
| 68 | 68 | |
| 69 | 69 | return $texte; |
| 70 | 70 | } |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | || ($pos = 0); |
| 179 | 179 | |
| 180 | 180 | if (!str_contains(substr($texte, 0, $pos), '<!-- insert_head -->')) { |
| 181 | - $insert = "\n" . pipeline('insert_head', '<!-- f_insert_head -->') . "\n"; |
|
| 181 | + $insert = "\n".pipeline('insert_head', '<!-- f_insert_head -->')."\n"; |
|
| 182 | 182 | $texte = substr_replace($texte, $insert, $pos, 0); |
| 183 | 183 | } |
| 184 | 184 | |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | } |
| 211 | 211 | $texte = substr_replace($texte, $x, $pos, 0); |
| 212 | 212 | // pas de preview en fenetre enfant |
| 213 | - $x = "<script type='text/javascript'>const frameEl = window.frameElement;if (frameEl) {frameEl.sandbox='sandbox';window.location.href='" . addslashes($GLOBALS['meta']['adresse_site']) . "';}</script>"; |
|
| 213 | + $x = "<script type='text/javascript'>const frameEl = window.frameElement;if (frameEl) {frameEl.sandbox='sandbox';window.location.href='".addslashes($GLOBALS['meta']['adresse_site'])."';}</script>"; |
|
| 214 | 214 | if (!$pos = stripos($texte, '<head') or !$pos = strpos($texte, '>', $pos)) { |
| 215 | 215 | $pos = -1; |
| 216 | 216 | } |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | |
| 282 | 282 | // inserer avant le </body> fermant si on peut, a la fin de la page sinon |
| 283 | 283 | if (($p = strpos($texte, '</body>')) !== false) { |
| 284 | - $texte = substr($texte, 0, $p) . $code . substr($texte, $p); |
|
| 284 | + $texte = substr($texte, 0, $p).$code.substr($texte, $p); |
|
| 285 | 285 | } else { |
| 286 | 286 | $texte .= $code; |
| 287 | 287 | } |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | **/ |
| 20 | 20 | |
| 21 | 21 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 22 | - return; |
|
| 22 | + return; |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | /** |
@@ -40,20 +40,20 @@ discard block |
||
| 40 | 40 | * Liste des fichiers de langue trouvés, dans l'ordre des chemins |
| 41 | 41 | */ |
| 42 | 42 | function find_langs_in_path($file, $dirname = 'lang') { |
| 43 | - static $dirs = []; |
|
| 44 | - $liste = []; |
|
| 45 | - foreach (creer_chemin() as $dir) { |
|
| 46 | - if (!isset($dirs[$a = $dir . $dirname])) { |
|
| 47 | - $dirs[$a] = (is_dir($a) || !$a); |
|
| 48 | - } |
|
| 49 | - if ($dirs[$a]) { |
|
| 50 | - if (is_readable($a .= $file)) { |
|
| 51 | - $liste[] = $a; |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - } |
|
| 43 | + static $dirs = []; |
|
| 44 | + $liste = []; |
|
| 45 | + foreach (creer_chemin() as $dir) { |
|
| 46 | + if (!isset($dirs[$a = $dir . $dirname])) { |
|
| 47 | + $dirs[$a] = (is_dir($a) || !$a); |
|
| 48 | + } |
|
| 49 | + if ($dirs[$a]) { |
|
| 50 | + if (is_readable($a .= $file)) { |
|
| 51 | + $liste[] = $a; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - return array_reverse($liste); |
|
| 56 | + return array_reverse($liste); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
@@ -68,23 +68,23 @@ discard block |
||
| 68 | 68 | * Liste des fichiers touvés pour ce module et cette langue. |
| 69 | 69 | **/ |
| 70 | 70 | function chercher_module_lang($module, $lang = '') { |
| 71 | - if ($lang) { |
|
| 72 | - $lang = '_' . $lang; |
|
| 73 | - } |
|
| 71 | + if ($lang) { |
|
| 72 | + $lang = '_' . $lang; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - // 1) dans un repertoire nomme lang/ se trouvant sur le chemin |
|
| 76 | - if ( |
|
| 77 | - $f = ($module == 'local' |
|
| 78 | - ? find_in_path($module . $lang . '.php', 'lang/') |
|
| 79 | - : find_langs_in_path($module . $lang . '.php', 'lang/')) |
|
| 80 | - ) { |
|
| 81 | - return is_array($f) ? $f : [$f]; |
|
| 82 | - } |
|
| 75 | + // 1) dans un repertoire nomme lang/ se trouvant sur le chemin |
|
| 76 | + if ( |
|
| 77 | + $f = ($module == 'local' |
|
| 78 | + ? find_in_path($module . $lang . '.php', 'lang/') |
|
| 79 | + : find_langs_in_path($module . $lang . '.php', 'lang/')) |
|
| 80 | + ) { |
|
| 81 | + return is_array($f) ? $f : [$f]; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - // 2) directement dans le chemin (old style, uniquement pour local) |
|
| 85 | - return (($module == 'local') or strpos($module, '/')) |
|
| 86 | - ? (($f = find_in_path($module . $lang . '.php')) ? [$f] : false) |
|
| 87 | - : false; |
|
| 84 | + // 2) directement dans le chemin (old style, uniquement pour local) |
|
| 85 | + return (($module == 'local') or strpos($module, '/')) |
|
| 86 | + ? (($f = find_in_path($module . $lang . '.php')) ? [$f] : false) |
|
| 87 | + : false; |
|
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | /** |
@@ -106,33 +106,33 @@ discard block |
||
| 106 | 106 | * @return void |
| 107 | 107 | **/ |
| 108 | 108 | function charger_langue($lang, $module = 'spip') { |
| 109 | - static $langs = []; |
|
| 110 | - $var = 'i18n_' . $module . '_' . $lang; |
|
| 111 | - if (!isset($langs[$lang])) { |
|
| 112 | - $langs[$lang] = []; |
|
| 113 | - if ($lang) { |
|
| 114 | - $langs[$lang][] = $lang; |
|
| 115 | - if (strpos($lang, '_') !== false) { |
|
| 116 | - $l = explode('_', $lang); |
|
| 117 | - $langs[$lang][] = reset($l); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - $langs[$lang][] = $GLOBALS['meta']['langue_site']; |
|
| 121 | - $langs[$lang][] = _LANGUE_PAR_DEFAUT; |
|
| 122 | - } |
|
| 123 | - foreach ($langs[$lang] as $l) { |
|
| 124 | - if ($fichiers_lang = chercher_module_lang($module, $l)) { |
|
| 125 | - $GLOBALS['idx_lang'] = 'i18n_' . $module . '_' . $l; |
|
| 126 | - $GLOBALS[$GLOBALS['idx_lang']] = lire_fichier_langue(array_shift($fichiers_lang)); |
|
| 127 | - surcharger_langue($fichiers_lang); |
|
| 128 | - if ($l !== $lang) { |
|
| 129 | - $GLOBALS[$var] = &$GLOBALS['i18n_' . $module . '_' . $l]; |
|
| 130 | - } |
|
| 131 | - $GLOBALS['lang_' . $var] = $l; |
|
| 132 | - #spip_log("module de langue : {$module}_$l.php", 'traduire'); |
|
| 133 | - break; |
|
| 134 | - } |
|
| 135 | - } |
|
| 109 | + static $langs = []; |
|
| 110 | + $var = 'i18n_' . $module . '_' . $lang; |
|
| 111 | + if (!isset($langs[$lang])) { |
|
| 112 | + $langs[$lang] = []; |
|
| 113 | + if ($lang) { |
|
| 114 | + $langs[$lang][] = $lang; |
|
| 115 | + if (strpos($lang, '_') !== false) { |
|
| 116 | + $l = explode('_', $lang); |
|
| 117 | + $langs[$lang][] = reset($l); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + $langs[$lang][] = $GLOBALS['meta']['langue_site']; |
|
| 121 | + $langs[$lang][] = _LANGUE_PAR_DEFAUT; |
|
| 122 | + } |
|
| 123 | + foreach ($langs[$lang] as $l) { |
|
| 124 | + if ($fichiers_lang = chercher_module_lang($module, $l)) { |
|
| 125 | + $GLOBALS['idx_lang'] = 'i18n_' . $module . '_' . $l; |
|
| 126 | + $GLOBALS[$GLOBALS['idx_lang']] = lire_fichier_langue(array_shift($fichiers_lang)); |
|
| 127 | + surcharger_langue($fichiers_lang); |
|
| 128 | + if ($l !== $lang) { |
|
| 129 | + $GLOBALS[$var] = &$GLOBALS['i18n_' . $module . '_' . $l]; |
|
| 130 | + } |
|
| 131 | + $GLOBALS['lang_' . $var] = $l; |
|
| 132 | + #spip_log("module de langue : {$module}_$l.php", 'traduire'); |
|
| 133 | + break; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** |
@@ -145,21 +145,21 @@ discard block |
||
| 145 | 145 | * @return array<string, string> |
| 146 | 146 | */ |
| 147 | 147 | function lire_fichier_langue(string $fichier): array { |
| 148 | - $idx_lang_before = $GLOBALS['idx_lang'] ?? null; |
|
| 149 | - $idx_lang_tmp = ($GLOBALS['idx_lang'] ?? 'lang') . '@temporaire'; |
|
| 150 | - $GLOBALS['idx_lang'] = $idx_lang_tmp; |
|
| 151 | - $idx_lang = include $fichier; |
|
| 152 | - $GLOBALS['idx_lang'] = $idx_lang_before; |
|
| 153 | - if (!is_array($idx_lang)) { |
|
| 154 | - if (isset($GLOBALS[$idx_lang_tmp]) and is_array($GLOBALS[$idx_lang_tmp])) { |
|
| 155 | - $idx_lang = $GLOBALS[$idx_lang_tmp]; |
|
| 156 | - } else { |
|
| 157 | - $idx_lang = []; |
|
| 158 | - spip_log(sprintf('Fichier de langue incorrect : %s', $fichier), _LOG_ERREUR); |
|
| 159 | - } |
|
| 160 | - unset($GLOBALS[$idx_lang_tmp]); |
|
| 161 | - } |
|
| 162 | - return $idx_lang; |
|
| 148 | + $idx_lang_before = $GLOBALS['idx_lang'] ?? null; |
|
| 149 | + $idx_lang_tmp = ($GLOBALS['idx_lang'] ?? 'lang') . '@temporaire'; |
|
| 150 | + $GLOBALS['idx_lang'] = $idx_lang_tmp; |
|
| 151 | + $idx_lang = include $fichier; |
|
| 152 | + $GLOBALS['idx_lang'] = $idx_lang_before; |
|
| 153 | + if (!is_array($idx_lang)) { |
|
| 154 | + if (isset($GLOBALS[$idx_lang_tmp]) and is_array($GLOBALS[$idx_lang_tmp])) { |
|
| 155 | + $idx_lang = $GLOBALS[$idx_lang_tmp]; |
|
| 156 | + } else { |
|
| 157 | + $idx_lang = []; |
|
| 158 | + spip_log(sprintf('Fichier de langue incorrect : %s', $fichier), _LOG_ERREUR); |
|
| 159 | + } |
|
| 160 | + unset($GLOBALS[$idx_lang_tmp]); |
|
| 161 | + } |
|
| 162 | + return $idx_lang; |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
@@ -179,29 +179,29 @@ discard block |
||
| 179 | 179 | * Liste des chemins de fichiers de langue à surcharger. |
| 180 | 180 | **/ |
| 181 | 181 | function surcharger_langue($fichiers) { |
| 182 | - static $surcharges = []; |
|
| 183 | - if (!isset($GLOBALS['idx_lang'])) { |
|
| 184 | - return; |
|
| 185 | - } |
|
| 182 | + static $surcharges = []; |
|
| 183 | + if (!isset($GLOBALS['idx_lang'])) { |
|
| 184 | + return; |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - if (!is_array($fichiers)) { |
|
| 188 | - $fichiers = [$fichiers]; |
|
| 189 | - } |
|
| 190 | - if (!count($fichiers)) { |
|
| 191 | - return; |
|
| 192 | - } |
|
| 193 | - foreach ($fichiers as $fichier) { |
|
| 194 | - if (!isset($surcharges[$fichier])) { |
|
| 195 | - $surcharges[$fichier] = lire_fichier_langue($fichier); |
|
| 196 | - } |
|
| 197 | - if (is_array($surcharges[$fichier])) { |
|
| 198 | - $GLOBALS[$GLOBALS['idx_lang']] ??= []; |
|
| 199 | - $GLOBALS[$GLOBALS['idx_lang']] = array_merge( |
|
| 200 | - $GLOBALS[$GLOBALS['idx_lang']], |
|
| 201 | - $surcharges[$fichier] |
|
| 202 | - ); |
|
| 203 | - } |
|
| 204 | - } |
|
| 187 | + if (!is_array($fichiers)) { |
|
| 188 | + $fichiers = [$fichiers]; |
|
| 189 | + } |
|
| 190 | + if (!count($fichiers)) { |
|
| 191 | + return; |
|
| 192 | + } |
|
| 193 | + foreach ($fichiers as $fichier) { |
|
| 194 | + if (!isset($surcharges[$fichier])) { |
|
| 195 | + $surcharges[$fichier] = lire_fichier_langue($fichier); |
|
| 196 | + } |
|
| 197 | + if (is_array($surcharges[$fichier])) { |
|
| 198 | + $GLOBALS[$GLOBALS['idx_lang']] ??= []; |
|
| 199 | + $GLOBALS[$GLOBALS['idx_lang']] = array_merge( |
|
| 200 | + $GLOBALS[$GLOBALS['idx_lang']], |
|
| 201 | + $surcharges[$fichier] |
|
| 202 | + ); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -242,99 +242,99 @@ discard block |
||
| 242 | 242 | * - Description : traduction et description (texte, module, langue) |
| 243 | 243 | **/ |
| 244 | 244 | function inc_traduire_dist($ori, $lang, $raw = false) { |
| 245 | - static $deja_vu = []; |
|
| 246 | - static $local = []; |
|
| 245 | + static $deja_vu = []; |
|
| 246 | + static $local = []; |
|
| 247 | 247 | |
| 248 | - if (isset($deja_vu[$lang][$ori]) and (_request('var_mode') != 'traduction')) { |
|
| 249 | - return $raw ? $deja_vu[$lang][$ori] : $deja_vu[$lang][$ori]->texte; |
|
| 250 | - } |
|
| 248 | + if (isset($deja_vu[$lang][$ori]) and (_request('var_mode') != 'traduction')) { |
|
| 249 | + return $raw ? $deja_vu[$lang][$ori] : $deja_vu[$lang][$ori]->texte; |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | - // modules demandes explicitement <xxx|yyy|zzz:code> cf MODULES_IDIOMES |
|
| 253 | - if (strpos($ori, ':')) { |
|
| 254 | - [$modules, $code] = explode(':', $ori, 2); |
|
| 255 | - $modules = explode('|', $modules); |
|
| 256 | - $ori_complet = $ori; |
|
| 257 | - } else { |
|
| 258 | - $modules = ['spip', 'ecrire']; |
|
| 259 | - $code = $ori; |
|
| 260 | - $ori_complet = implode('|', $modules) . ':' . $ori; |
|
| 261 | - } |
|
| 252 | + // modules demandes explicitement <xxx|yyy|zzz:code> cf MODULES_IDIOMES |
|
| 253 | + if (strpos($ori, ':')) { |
|
| 254 | + [$modules, $code] = explode(':', $ori, 2); |
|
| 255 | + $modules = explode('|', $modules); |
|
| 256 | + $ori_complet = $ori; |
|
| 257 | + } else { |
|
| 258 | + $modules = ['spip', 'ecrire']; |
|
| 259 | + $code = $ori; |
|
| 260 | + $ori_complet = implode('|', $modules) . ':' . $ori; |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - $desc = new Description(); |
|
| 263 | + $desc = new Description(); |
|
| 264 | 264 | |
| 265 | - // parcourir tous les modules jusqu'a ce qu'on trouve |
|
| 266 | - foreach ($modules as $module) { |
|
| 267 | - $var = 'i18n_' . $module . '_' . $lang; |
|
| 265 | + // parcourir tous les modules jusqu'a ce qu'on trouve |
|
| 266 | + foreach ($modules as $module) { |
|
| 267 | + $var = 'i18n_' . $module . '_' . $lang; |
|
| 268 | 268 | |
| 269 | - if (empty($GLOBALS[$var])) { |
|
| 270 | - charger_langue($lang, $module); |
|
| 271 | - // surcharges persos -- on cherche |
|
| 272 | - // (lang/)local_xx.php et/ou (lang/)local.php ... |
|
| 273 | - if (!isset($local['local_' . $lang])) { |
|
| 274 | - // redéfinir la langue en cours pour les surcharges (chercher_langue a pu le changer) |
|
| 275 | - $GLOBALS['idx_lang'] = $var; |
|
| 276 | - // ... (lang/)local_xx.php |
|
| 277 | - $local['local_' . $lang] = chercher_module_lang('local', $lang); |
|
| 278 | - } |
|
| 279 | - if ($local['local_' . $lang]) { |
|
| 280 | - surcharger_langue($local['local_' . $lang]); |
|
| 281 | - } |
|
| 282 | - // ... puis (lang/)local.php |
|
| 283 | - if (!isset($local['local'])) { |
|
| 284 | - $local['local'] = chercher_module_lang('local'); |
|
| 285 | - } |
|
| 286 | - if ($local['local']) { |
|
| 287 | - surcharger_langue($local['local']); |
|
| 288 | - } |
|
| 289 | - } |
|
| 269 | + if (empty($GLOBALS[$var])) { |
|
| 270 | + charger_langue($lang, $module); |
|
| 271 | + // surcharges persos -- on cherche |
|
| 272 | + // (lang/)local_xx.php et/ou (lang/)local.php ... |
|
| 273 | + if (!isset($local['local_' . $lang])) { |
|
| 274 | + // redéfinir la langue en cours pour les surcharges (chercher_langue a pu le changer) |
|
| 275 | + $GLOBALS['idx_lang'] = $var; |
|
| 276 | + // ... (lang/)local_xx.php |
|
| 277 | + $local['local_' . $lang] = chercher_module_lang('local', $lang); |
|
| 278 | + } |
|
| 279 | + if ($local['local_' . $lang]) { |
|
| 280 | + surcharger_langue($local['local_' . $lang]); |
|
| 281 | + } |
|
| 282 | + // ... puis (lang/)local.php |
|
| 283 | + if (!isset($local['local'])) { |
|
| 284 | + $local['local'] = chercher_module_lang('local'); |
|
| 285 | + } |
|
| 286 | + if ($local['local']) { |
|
| 287 | + surcharger_langue($local['local']); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | 290 | |
| 291 | - if (isset($GLOBALS[$var][$code])) { |
|
| 292 | - $desc->code = $code; |
|
| 293 | - $desc->module = $module; |
|
| 294 | - $desc->langue = $GLOBALS['lang_' . $var] ?? $lang; |
|
| 295 | - $desc->texte = $GLOBALS[$var][$code]; |
|
| 296 | - break; |
|
| 297 | - } |
|
| 298 | - } |
|
| 291 | + if (isset($GLOBALS[$var][$code])) { |
|
| 292 | + $desc->code = $code; |
|
| 293 | + $desc->module = $module; |
|
| 294 | + $desc->langue = $GLOBALS['lang_' . $var] ?? $lang; |
|
| 295 | + $desc->texte = $GLOBALS[$var][$code]; |
|
| 296 | + break; |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | - if (!$desc->corrections) { |
|
| 301 | - $desc->corrections = true; |
|
| 302 | - // Retour aux sources si la chaine est absente dans la langue cible ; |
|
| 303 | - // on essaie d'abord la langue du site, puis a defaut la langue fr |
|
| 304 | - if ( |
|
| 305 | - ($desc->texte === null || !strlen($desc->texte)) |
|
| 306 | - and $lang !== _LANGUE_PAR_DEFAUT |
|
| 307 | - ) { |
|
| 308 | - if ($lang !== $GLOBALS['meta']['langue_site']) { |
|
| 309 | - $desc = inc_traduire_dist($ori, $GLOBALS['meta']['langue_site'], true); |
|
| 310 | - } else { |
|
| 311 | - $desc = inc_traduire_dist($ori, _LANGUE_PAR_DEFAUT, true); |
|
| 312 | - } |
|
| 313 | - } |
|
| 300 | + if (!$desc->corrections) { |
|
| 301 | + $desc->corrections = true; |
|
| 302 | + // Retour aux sources si la chaine est absente dans la langue cible ; |
|
| 303 | + // on essaie d'abord la langue du site, puis a defaut la langue fr |
|
| 304 | + if ( |
|
| 305 | + ($desc->texte === null || !strlen($desc->texte)) |
|
| 306 | + and $lang !== _LANGUE_PAR_DEFAUT |
|
| 307 | + ) { |
|
| 308 | + if ($lang !== $GLOBALS['meta']['langue_site']) { |
|
| 309 | + $desc = inc_traduire_dist($ori, $GLOBALS['meta']['langue_site'], true); |
|
| 310 | + } else { |
|
| 311 | + $desc = inc_traduire_dist($ori, _LANGUE_PAR_DEFAUT, true); |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | 314 | |
| 315 | - // Supprimer la mention <NEW> ou <MODIF> |
|
| 316 | - if ($desc->texte && substr($desc->texte, 0, 1) === '<') { |
|
| 317 | - $desc->texte = str_replace(['<NEW>', '<MODIF>'], [], $desc->texte); |
|
| 318 | - } |
|
| 315 | + // Supprimer la mention <NEW> ou <MODIF> |
|
| 316 | + if ($desc->texte && substr($desc->texte, 0, 1) === '<') { |
|
| 317 | + $desc->texte = str_replace(['<NEW>', '<MODIF>'], [], $desc->texte); |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - // Si on n'est pas en utf-8, la chaine peut l'etre... |
|
| 321 | - // le cas echeant on la convertit en entites html &#xxx; |
|
| 322 | - if ( |
|
| 323 | - (!isset($GLOBALS['meta']['charset']) or $GLOBALS['meta']['charset'] !== 'utf-8') |
|
| 324 | - and preg_match(',[\x7f-\xff],S', $desc->texte) |
|
| 325 | - ) { |
|
| 326 | - include_spip('inc/charsets'); |
|
| 327 | - $desc->texte = charset2unicode($desc->texte, 'utf-8'); |
|
| 328 | - } |
|
| 329 | - } |
|
| 320 | + // Si on n'est pas en utf-8, la chaine peut l'etre... |
|
| 321 | + // le cas echeant on la convertit en entites html &#xxx; |
|
| 322 | + if ( |
|
| 323 | + (!isset($GLOBALS['meta']['charset']) or $GLOBALS['meta']['charset'] !== 'utf-8') |
|
| 324 | + and preg_match(',[\x7f-\xff],S', $desc->texte) |
|
| 325 | + ) { |
|
| 326 | + include_spip('inc/charsets'); |
|
| 327 | + $desc->texte = charset2unicode($desc->texte, 'utf-8'); |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | 330 | |
| 331 | - if (_request('var_mode') == 'traduction') { |
|
| 332 | - $desc = definir_details_traduction($desc, $ori_complet); |
|
| 333 | - } else { |
|
| 334 | - $deja_vu[$lang][$ori] = $desc; |
|
| 335 | - } |
|
| 331 | + if (_request('var_mode') == 'traduction') { |
|
| 332 | + $desc = definir_details_traduction($desc, $ori_complet); |
|
| 333 | + } else { |
|
| 334 | + $deja_vu[$lang][$ori] = $desc; |
|
| 335 | + } |
|
| 336 | 336 | |
| 337 | - return $raw ? $desc : $desc->texte; |
|
| 337 | + return $raw ? $desc : $desc->texte; |
|
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | /** |
@@ -346,23 +346,23 @@ discard block |
||
| 346 | 346 | * @return Description |
| 347 | 347 | */ |
| 348 | 348 | function definir_details_traduction($desc, $modules) { |
| 349 | - if (!$desc->mode and $desc->texte) { |
|
| 350 | - // ne pas modifier 2 fois l'affichage |
|
| 351 | - $desc->mode = 'traduction'; |
|
| 352 | - $classe = 'debug-traduction' . ($desc->module == 'ecrire' ? '-prive' : ''); |
|
| 353 | - $desc->texte = '<span ' |
|
| 354 | - . 'lang=' . $desc->langue |
|
| 355 | - . ' class=' . $classe |
|
| 356 | - . ' data-module=' . $desc->module |
|
| 357 | - . ' data-code=' . $desc->code |
|
| 358 | - . ' title=' . $modules . '(' . $desc->langue . ')>' |
|
| 359 | - . $desc->texte |
|
| 360 | - . '</span>'; |
|
| 361 | - $desc->texte = str_replace( |
|
| 362 | - ["$desc->module:", "$desc->module|"], |
|
| 363 | - ["*$desc->module*:", "*$desc->module*|"], |
|
| 364 | - $desc->texte |
|
| 365 | - ); |
|
| 366 | - } |
|
| 367 | - return $desc; |
|
| 349 | + if (!$desc->mode and $desc->texte) { |
|
| 350 | + // ne pas modifier 2 fois l'affichage |
|
| 351 | + $desc->mode = 'traduction'; |
|
| 352 | + $classe = 'debug-traduction' . ($desc->module == 'ecrire' ? '-prive' : ''); |
|
| 353 | + $desc->texte = '<span ' |
|
| 354 | + . 'lang=' . $desc->langue |
|
| 355 | + . ' class=' . $classe |
|
| 356 | + . ' data-module=' . $desc->module |
|
| 357 | + . ' data-code=' . $desc->code |
|
| 358 | + . ' title=' . $modules . '(' . $desc->langue . ')>' |
|
| 359 | + . $desc->texte |
|
| 360 | + . '</span>'; |
|
| 361 | + $desc->texte = str_replace( |
|
| 362 | + ["$desc->module:", "$desc->module|"], |
|
| 363 | + ["*$desc->module*:", "*$desc->module*|"], |
|
| 364 | + $desc->texte |
|
| 365 | + ); |
|
| 366 | + } |
|
| 367 | + return $desc; |
|
| 368 | 368 | } |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | static $dirs = []; |
| 44 | 44 | $liste = []; |
| 45 | 45 | foreach (creer_chemin() as $dir) { |
| 46 | - if (!isset($dirs[$a = $dir . $dirname])) { |
|
| 46 | + if (!isset($dirs[$a = $dir.$dirname])) { |
|
| 47 | 47 | $dirs[$a] = (is_dir($a) || !$a); |
| 48 | 48 | } |
| 49 | 49 | if ($dirs[$a]) { |
@@ -69,21 +69,21 @@ discard block |
||
| 69 | 69 | **/ |
| 70 | 70 | function chercher_module_lang($module, $lang = '') { |
| 71 | 71 | if ($lang) { |
| 72 | - $lang = '_' . $lang; |
|
| 72 | + $lang = '_'.$lang; |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | // 1) dans un repertoire nomme lang/ se trouvant sur le chemin |
| 76 | 76 | if ( |
| 77 | 77 | $f = ($module == 'local' |
| 78 | - ? find_in_path($module . $lang . '.php', 'lang/') |
|
| 79 | - : find_langs_in_path($module . $lang . '.php', 'lang/')) |
|
| 78 | + ? find_in_path($module.$lang.'.php', 'lang/') |
|
| 79 | + : find_langs_in_path($module.$lang.'.php', 'lang/')) |
|
| 80 | 80 | ) { |
| 81 | 81 | return is_array($f) ? $f : [$f]; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | // 2) directement dans le chemin (old style, uniquement pour local) |
| 85 | 85 | return (($module == 'local') or strpos($module, '/')) |
| 86 | - ? (($f = find_in_path($module . $lang . '.php')) ? [$f] : false) |
|
| 86 | + ? (($f = find_in_path($module.$lang.'.php')) ? [$f] : false) |
|
| 87 | 87 | : false; |
| 88 | 88 | } |
| 89 | 89 | |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | **/ |
| 108 | 108 | function charger_langue($lang, $module = 'spip') { |
| 109 | 109 | static $langs = []; |
| 110 | - $var = 'i18n_' . $module . '_' . $lang; |
|
| 110 | + $var = 'i18n_'.$module.'_'.$lang; |
|
| 111 | 111 | if (!isset($langs[$lang])) { |
| 112 | 112 | $langs[$lang] = []; |
| 113 | 113 | if ($lang) { |
@@ -122,13 +122,13 @@ discard block |
||
| 122 | 122 | } |
| 123 | 123 | foreach ($langs[$lang] as $l) { |
| 124 | 124 | if ($fichiers_lang = chercher_module_lang($module, $l)) { |
| 125 | - $GLOBALS['idx_lang'] = 'i18n_' . $module . '_' . $l; |
|
| 125 | + $GLOBALS['idx_lang'] = 'i18n_'.$module.'_'.$l; |
|
| 126 | 126 | $GLOBALS[$GLOBALS['idx_lang']] = lire_fichier_langue(array_shift($fichiers_lang)); |
| 127 | 127 | surcharger_langue($fichiers_lang); |
| 128 | 128 | if ($l !== $lang) { |
| 129 | - $GLOBALS[$var] = &$GLOBALS['i18n_' . $module . '_' . $l]; |
|
| 129 | + $GLOBALS[$var] = &$GLOBALS['i18n_'.$module.'_'.$l]; |
|
| 130 | 130 | } |
| 131 | - $GLOBALS['lang_' . $var] = $l; |
|
| 131 | + $GLOBALS['lang_'.$var] = $l; |
|
| 132 | 132 | #spip_log("module de langue : {$module}_$l.php", 'traduire'); |
| 133 | 133 | break; |
| 134 | 134 | } |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | */ |
| 147 | 147 | function lire_fichier_langue(string $fichier): array { |
| 148 | 148 | $idx_lang_before = $GLOBALS['idx_lang'] ?? null; |
| 149 | - $idx_lang_tmp = ($GLOBALS['idx_lang'] ?? 'lang') . '@temporaire'; |
|
| 149 | + $idx_lang_tmp = ($GLOBALS['idx_lang'] ?? 'lang').'@temporaire'; |
|
| 150 | 150 | $GLOBALS['idx_lang'] = $idx_lang_tmp; |
| 151 | 151 | $idx_lang = include $fichier; |
| 152 | 152 | $GLOBALS['idx_lang'] = $idx_lang_before; |
@@ -257,27 +257,27 @@ discard block |
||
| 257 | 257 | } else { |
| 258 | 258 | $modules = ['spip', 'ecrire']; |
| 259 | 259 | $code = $ori; |
| 260 | - $ori_complet = implode('|', $modules) . ':' . $ori; |
|
| 260 | + $ori_complet = implode('|', $modules).':'.$ori; |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | $desc = new Description(); |
| 264 | 264 | |
| 265 | 265 | // parcourir tous les modules jusqu'a ce qu'on trouve |
| 266 | 266 | foreach ($modules as $module) { |
| 267 | - $var = 'i18n_' . $module . '_' . $lang; |
|
| 267 | + $var = 'i18n_'.$module.'_'.$lang; |
|
| 268 | 268 | |
| 269 | 269 | if (empty($GLOBALS[$var])) { |
| 270 | 270 | charger_langue($lang, $module); |
| 271 | 271 | // surcharges persos -- on cherche |
| 272 | 272 | // (lang/)local_xx.php et/ou (lang/)local.php ... |
| 273 | - if (!isset($local['local_' . $lang])) { |
|
| 273 | + if (!isset($local['local_'.$lang])) { |
|
| 274 | 274 | // redéfinir la langue en cours pour les surcharges (chercher_langue a pu le changer) |
| 275 | 275 | $GLOBALS['idx_lang'] = $var; |
| 276 | 276 | // ... (lang/)local_xx.php |
| 277 | - $local['local_' . $lang] = chercher_module_lang('local', $lang); |
|
| 277 | + $local['local_'.$lang] = chercher_module_lang('local', $lang); |
|
| 278 | 278 | } |
| 279 | - if ($local['local_' . $lang]) { |
|
| 280 | - surcharger_langue($local['local_' . $lang]); |
|
| 279 | + if ($local['local_'.$lang]) { |
|
| 280 | + surcharger_langue($local['local_'.$lang]); |
|
| 281 | 281 | } |
| 282 | 282 | // ... puis (lang/)local.php |
| 283 | 283 | if (!isset($local['local'])) { |
@@ -291,7 +291,7 @@ discard block |
||
| 291 | 291 | if (isset($GLOBALS[$var][$code])) { |
| 292 | 292 | $desc->code = $code; |
| 293 | 293 | $desc->module = $module; |
| 294 | - $desc->langue = $GLOBALS['lang_' . $var] ?? $lang; |
|
| 294 | + $desc->langue = $GLOBALS['lang_'.$var] ?? $lang; |
|
| 295 | 295 | $desc->texte = $GLOBALS[$var][$code]; |
| 296 | 296 | break; |
| 297 | 297 | } |
@@ -349,13 +349,13 @@ discard block |
||
| 349 | 349 | if (!$desc->mode and $desc->texte) { |
| 350 | 350 | // ne pas modifier 2 fois l'affichage |
| 351 | 351 | $desc->mode = 'traduction'; |
| 352 | - $classe = 'debug-traduction' . ($desc->module == 'ecrire' ? '-prive' : ''); |
|
| 352 | + $classe = 'debug-traduction'.($desc->module == 'ecrire' ? '-prive' : ''); |
|
| 353 | 353 | $desc->texte = '<span ' |
| 354 | - . 'lang=' . $desc->langue |
|
| 355 | - . ' class=' . $classe |
|
| 356 | - . ' data-module=' . $desc->module |
|
| 357 | - . ' data-code=' . $desc->code |
|
| 358 | - . ' title=' . $modules . '(' . $desc->langue . ')>' |
|
| 354 | + . 'lang='.$desc->langue |
|
| 355 | + . ' class='.$classe |
|
| 356 | + . ' data-module='.$desc->module |
|
| 357 | + . ' data-code='.$desc->code |
|
| 358 | + . ' title='.$modules.'('.$desc->langue.')>' |
|
| 359 | 359 | . $desc->texte |
| 360 | 360 | . '</span>'; |
| 361 | 361 | $desc->texte = str_replace( |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | // securité |
| 24 | 24 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 25 | - return; |
|
| 25 | + return; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | // se faciliter la lecture du charset |
@@ -32,19 +32,19 @@ discard block |
||
| 32 | 32 | * Initialisation |
| 33 | 33 | */ |
| 34 | 34 | function init_charset(): void { |
| 35 | - // Initialisation |
|
| 36 | - $GLOBALS['CHARSET'] = []; |
|
| 37 | - |
|
| 38 | - // noter a l'occasion dans la meta pcre_u notre capacite a utiliser le flag /u |
|
| 39 | - // dans les preg_replace pour ne pas casser certaines lettres accentuees : |
|
| 40 | - // en utf-8 chr(195).chr(160) = a` alors qu'en iso-latin chr(160) = nbsp |
|
| 41 | - if ( |
|
| 42 | - !isset($GLOBALS['meta']['pcre_u']) |
|
| 43 | - or (isset($_GET['var_mode']) and !isset($_GET['var_profile'])) |
|
| 44 | - ) { |
|
| 45 | - include_spip('inc/meta'); |
|
| 46 | - ecrire_meta('pcre_u', (lire_config('charset', _DEFAULT_CHARSET) === 'utf-8') ? 'u' : ''); |
|
| 47 | - } |
|
| 35 | + // Initialisation |
|
| 36 | + $GLOBALS['CHARSET'] = []; |
|
| 37 | + |
|
| 38 | + // noter a l'occasion dans la meta pcre_u notre capacite a utiliser le flag /u |
|
| 39 | + // dans les preg_replace pour ne pas casser certaines lettres accentuees : |
|
| 40 | + // en utf-8 chr(195).chr(160) = a` alors qu'en iso-latin chr(160) = nbsp |
|
| 41 | + if ( |
|
| 42 | + !isset($GLOBALS['meta']['pcre_u']) |
|
| 43 | + or (isset($_GET['var_mode']) and !isset($_GET['var_profile'])) |
|
| 44 | + ) { |
|
| 45 | + include_spip('inc/meta'); |
|
| 46 | + ecrire_meta('pcre_u', (lire_config('charset', _DEFAULT_CHARSET) === 'utf-8') ? 'u' : ''); |
|
| 47 | + } |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | // TODO: code d’exécution en dehors du fichier. |
@@ -65,39 +65,39 @@ discard block |
||
| 65 | 65 | * - false si le charset n'est pas décrit dans le répertoire charsets/ |
| 66 | 66 | **/ |
| 67 | 67 | function load_charset($charset = 'AUTO') { |
| 68 | - if ($charset == 'AUTO') { |
|
| 69 | - $charset = $GLOBALS['meta']['charset']; |
|
| 70 | - } |
|
| 71 | - $charset = trim(strtolower($charset)); |
|
| 72 | - if (isset($GLOBALS['CHARSET'][$charset])) { |
|
| 73 | - return $charset; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - if ($charset == 'utf-8') { |
|
| 77 | - $GLOBALS['CHARSET'][$charset] = []; |
|
| 78 | - |
|
| 79 | - return $charset; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - // Quelques synonymes |
|
| 83 | - if ($charset === '') { |
|
| 84 | - $charset = 'iso-8859-1'; |
|
| 85 | - } elseif ($charset === 'windows-1250') { |
|
| 86 | - $charset = 'cp1250'; |
|
| 87 | - } elseif ($charset === 'windows-1251') { |
|
| 88 | - $charset = 'cp1251'; |
|
| 89 | - } elseif ($charset === 'windows-1256') { |
|
| 90 | - $charset = 'cp1256'; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - if (find_in_path($charset . '.php', 'charsets/', true)) { |
|
| 94 | - return $charset; |
|
| 95 | - } else { |
|
| 96 | - spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'"); |
|
| 97 | - $GLOBALS['CHARSET'][$charset] = []; |
|
| 98 | - |
|
| 99 | - return false; |
|
| 100 | - } |
|
| 68 | + if ($charset == 'AUTO') { |
|
| 69 | + $charset = $GLOBALS['meta']['charset']; |
|
| 70 | + } |
|
| 71 | + $charset = trim(strtolower($charset)); |
|
| 72 | + if (isset($GLOBALS['CHARSET'][$charset])) { |
|
| 73 | + return $charset; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + if ($charset == 'utf-8') { |
|
| 77 | + $GLOBALS['CHARSET'][$charset] = []; |
|
| 78 | + |
|
| 79 | + return $charset; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + // Quelques synonymes |
|
| 83 | + if ($charset === '') { |
|
| 84 | + $charset = 'iso-8859-1'; |
|
| 85 | + } elseif ($charset === 'windows-1250') { |
|
| 86 | + $charset = 'cp1250'; |
|
| 87 | + } elseif ($charset === 'windows-1251') { |
|
| 88 | + $charset = 'cp1251'; |
|
| 89 | + } elseif ($charset === 'windows-1256') { |
|
| 90 | + $charset = 'cp1256'; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + if (find_in_path($charset . '.php', 'charsets/', true)) { |
|
| 94 | + return $charset; |
|
| 95 | + } else { |
|
| 96 | + spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'"); |
|
| 97 | + $GLOBALS['CHARSET'][$charset] = []; |
|
| 98 | + |
|
| 99 | + return false; |
|
| 100 | + } |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | |
@@ -110,19 +110,19 @@ discard block |
||
| 110 | 110 | * true si notre charset est utilisable par mb_strsing |
| 111 | 111 | **/ |
| 112 | 112 | function init_mb_string(): bool { |
| 113 | - static $mb; |
|
| 114 | - |
|
| 115 | - // verifier que le charset interne est connu de mb_string |
|
| 116 | - if (!$mb) { |
|
| 117 | - if (mb_detect_order(lire_config('charset', _DEFAULT_CHARSET))) { |
|
| 118 | - mb_internal_encoding('utf-8'); |
|
| 119 | - $mb = 1; |
|
| 120 | - } else { |
|
| 121 | - $mb = -1; |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - return ($mb === 1); |
|
| 113 | + static $mb; |
|
| 114 | + |
|
| 115 | + // verifier que le charset interne est connu de mb_string |
|
| 116 | + if (!$mb) { |
|
| 117 | + if (mb_detect_order(lire_config('charset', _DEFAULT_CHARSET))) { |
|
| 118 | + mb_internal_encoding('utf-8'); |
|
| 119 | + $mb = 1; |
|
| 120 | + } else { |
|
| 121 | + $mb = -1; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + return ($mb === 1); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | /** |
@@ -137,21 +137,21 @@ discard block |
||
| 137 | 137 | * true si iconv fonctionne correctement |
| 138 | 138 | **/ |
| 139 | 139 | function test_iconv(): bool { |
| 140 | - static $iconv_ok; |
|
| 141 | - |
|
| 142 | - if (!$iconv_ok) { |
|
| 143 | - if (!function_exists('iconv')) { |
|
| 144 | - $iconv_ok = -1; |
|
| 145 | - } else { |
|
| 146 | - if (utf_32_to_unicode(@iconv('utf-8', 'utf-32', 'chaine de test')) === 'chaine de test') { |
|
| 147 | - $iconv_ok = 1; |
|
| 148 | - } else { |
|
| 149 | - $iconv_ok = -1; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - return ($iconv_ok === 1); |
|
| 140 | + static $iconv_ok; |
|
| 141 | + |
|
| 142 | + if (!$iconv_ok) { |
|
| 143 | + if (!function_exists('iconv')) { |
|
| 144 | + $iconv_ok = -1; |
|
| 145 | + } else { |
|
| 146 | + if (utf_32_to_unicode(@iconv('utf-8', 'utf-32', 'chaine de test')) === 'chaine de test') { |
|
| 147 | + $iconv_ok = 1; |
|
| 148 | + } else { |
|
| 149 | + $iconv_ok = -1; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + return ($iconv_ok === 1); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -171,75 +171,75 @@ discard block |
||
| 171 | 171 | * texte corrigé |
| 172 | 172 | **/ |
| 173 | 173 | function corriger_caracteres_windows($texte, $charset = 'AUTO', $charset_cible = 'unicode') { |
| 174 | - static $trans; |
|
| 175 | - |
|
| 176 | - if (is_array($texte)) { |
|
| 177 | - return array_map('corriger_caracteres_windows', $texte); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - if ($charset == 'AUTO') { |
|
| 181 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 182 | - } |
|
| 183 | - if ($charset == 'utf-8') { |
|
| 184 | - $p = chr(194); |
|
| 185 | - if (strpos($texte, $p) == false) { |
|
| 186 | - return $texte; |
|
| 187 | - } |
|
| 188 | - } else { |
|
| 189 | - if ($charset == 'iso-8859-1') { |
|
| 190 | - $p = ''; |
|
| 191 | - } else { |
|
| 192 | - return $texte; |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - if (!isset($trans[$charset][$charset_cible])) { |
|
| 197 | - $trans[$charset][$charset_cible] = [ |
|
| 198 | - $p . chr(128) => '€', |
|
| 199 | - $p . chr(129) => ' ', # pas affecte |
|
| 200 | - $p . chr(130) => '‚', |
|
| 201 | - $p . chr(131) => 'ƒ', |
|
| 202 | - $p . chr(132) => '„', |
|
| 203 | - $p . chr(133) => '…', |
|
| 204 | - $p . chr(134) => '†', |
|
| 205 | - $p . chr(135) => '‡', |
|
| 206 | - $p . chr(136) => 'ˆ', |
|
| 207 | - $p . chr(137) => '‰', |
|
| 208 | - $p . chr(138) => 'Š', |
|
| 209 | - $p . chr(139) => '‹', |
|
| 210 | - $p . chr(140) => 'Œ', |
|
| 211 | - $p . chr(141) => ' ', # pas affecte |
|
| 212 | - $p . chr(142) => 'Ž', |
|
| 213 | - $p . chr(143) => ' ', # pas affecte |
|
| 214 | - $p . chr(144) => ' ', # pas affecte |
|
| 215 | - $p . chr(145) => '‘', |
|
| 216 | - $p . chr(146) => '’', |
|
| 217 | - $p . chr(147) => '“', |
|
| 218 | - $p . chr(148) => '”', |
|
| 219 | - $p . chr(149) => '•', |
|
| 220 | - $p . chr(150) => '–', |
|
| 221 | - $p . chr(151) => '—', |
|
| 222 | - $p . chr(152) => '˜', |
|
| 223 | - $p . chr(153) => '™', |
|
| 224 | - $p . chr(154) => 'š', |
|
| 225 | - $p . chr(155) => '›', |
|
| 226 | - $p . chr(156) => 'œ', |
|
| 227 | - $p . chr(157) => ' ', # pas affecte |
|
| 228 | - $p . chr(158) => 'ž', |
|
| 229 | - $p . chr(159) => 'Ÿ', |
|
| 230 | - ]; |
|
| 231 | - if ($charset_cible != 'unicode') { |
|
| 232 | - foreach ($trans[$charset][$charset_cible] as $k => $c) { |
|
| 233 | - $trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - return @str_replace( |
|
| 239 | - array_keys($trans[$charset][$charset_cible]), |
|
| 240 | - array_values($trans[$charset][$charset_cible]), |
|
| 241 | - $texte |
|
| 242 | - ); |
|
| 174 | + static $trans; |
|
| 175 | + |
|
| 176 | + if (is_array($texte)) { |
|
| 177 | + return array_map('corriger_caracteres_windows', $texte); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + if ($charset == 'AUTO') { |
|
| 181 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 182 | + } |
|
| 183 | + if ($charset == 'utf-8') { |
|
| 184 | + $p = chr(194); |
|
| 185 | + if (strpos($texte, $p) == false) { |
|
| 186 | + return $texte; |
|
| 187 | + } |
|
| 188 | + } else { |
|
| 189 | + if ($charset == 'iso-8859-1') { |
|
| 190 | + $p = ''; |
|
| 191 | + } else { |
|
| 192 | + return $texte; |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + if (!isset($trans[$charset][$charset_cible])) { |
|
| 197 | + $trans[$charset][$charset_cible] = [ |
|
| 198 | + $p . chr(128) => '€', |
|
| 199 | + $p . chr(129) => ' ', # pas affecte |
|
| 200 | + $p . chr(130) => '‚', |
|
| 201 | + $p . chr(131) => 'ƒ', |
|
| 202 | + $p . chr(132) => '„', |
|
| 203 | + $p . chr(133) => '…', |
|
| 204 | + $p . chr(134) => '†', |
|
| 205 | + $p . chr(135) => '‡', |
|
| 206 | + $p . chr(136) => 'ˆ', |
|
| 207 | + $p . chr(137) => '‰', |
|
| 208 | + $p . chr(138) => 'Š', |
|
| 209 | + $p . chr(139) => '‹', |
|
| 210 | + $p . chr(140) => 'Œ', |
|
| 211 | + $p . chr(141) => ' ', # pas affecte |
|
| 212 | + $p . chr(142) => 'Ž', |
|
| 213 | + $p . chr(143) => ' ', # pas affecte |
|
| 214 | + $p . chr(144) => ' ', # pas affecte |
|
| 215 | + $p . chr(145) => '‘', |
|
| 216 | + $p . chr(146) => '’', |
|
| 217 | + $p . chr(147) => '“', |
|
| 218 | + $p . chr(148) => '”', |
|
| 219 | + $p . chr(149) => '•', |
|
| 220 | + $p . chr(150) => '–', |
|
| 221 | + $p . chr(151) => '—', |
|
| 222 | + $p . chr(152) => '˜', |
|
| 223 | + $p . chr(153) => '™', |
|
| 224 | + $p . chr(154) => 'š', |
|
| 225 | + $p . chr(155) => '›', |
|
| 226 | + $p . chr(156) => 'œ', |
|
| 227 | + $p . chr(157) => ' ', # pas affecte |
|
| 228 | + $p . chr(158) => 'ž', |
|
| 229 | + $p . chr(159) => 'Ÿ', |
|
| 230 | + ]; |
|
| 231 | + if ($charset_cible != 'unicode') { |
|
| 232 | + foreach ($trans[$charset][$charset_cible] as $k => $c) { |
|
| 233 | + $trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + return @str_replace( |
|
| 239 | + array_keys($trans[$charset][$charset_cible]), |
|
| 240 | + array_values($trans[$charset][$charset_cible]), |
|
| 241 | + $texte |
|
| 242 | + ); |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | |
@@ -256,26 +256,26 @@ discard block |
||
| 256 | 256 | * texte converti |
| 257 | 257 | **/ |
| 258 | 258 | function html2unicode($texte, $secure = false) { |
| 259 | - if (strpos($texte, '&') === false) { |
|
| 260 | - return $texte; |
|
| 261 | - } |
|
| 262 | - static $trans = []; |
|
| 263 | - if (!$trans) { |
|
| 264 | - load_charset('html'); |
|
| 265 | - foreach ($GLOBALS['CHARSET']['html'] as $key => $val) { |
|
| 266 | - $trans["&$key;"] = $val; |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - if ($secure) { |
|
| 271 | - return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 272 | - } else { |
|
| 273 | - return str_replace( |
|
| 274 | - ['&', '"', '<', '>'], |
|
| 275 | - ['&', '"', '<', '>'], |
|
| 276 | - str_replace(array_keys($trans), array_values($trans), $texte) |
|
| 277 | - ); |
|
| 278 | - } |
|
| 259 | + if (strpos($texte, '&') === false) { |
|
| 260 | + return $texte; |
|
| 261 | + } |
|
| 262 | + static $trans = []; |
|
| 263 | + if (!$trans) { |
|
| 264 | + load_charset('html'); |
|
| 265 | + foreach ($GLOBALS['CHARSET']['html'] as $key => $val) { |
|
| 266 | + $trans["&$key;"] = $val; |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + if ($secure) { |
|
| 271 | + return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 272 | + } else { |
|
| 273 | + return str_replace( |
|
| 274 | + ['&', '"', '<', '>'], |
|
| 275 | + ['&', '"', '<', '>'], |
|
| 276 | + str_replace(array_keys($trans), array_values($trans), $texte) |
|
| 277 | + ); |
|
| 278 | + } |
|
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | |
@@ -290,16 +290,16 @@ discard block |
||
| 290 | 290 | * texte converti |
| 291 | 291 | **/ |
| 292 | 292 | function mathml2unicode($texte) { |
| 293 | - static $trans; |
|
| 294 | - if (!$trans) { |
|
| 295 | - load_charset('mathml'); |
|
| 293 | + static $trans; |
|
| 294 | + if (!$trans) { |
|
| 295 | + load_charset('mathml'); |
|
| 296 | 296 | |
| 297 | - foreach ($GLOBALS['CHARSET']['mathml'] as $key => $val) { |
|
| 298 | - $trans["&$key;"] = $val; |
|
| 299 | - } |
|
| 300 | - } |
|
| 297 | + foreach ($GLOBALS['CHARSET']['mathml'] as $key => $val) { |
|
| 298 | + $trans["&$key;"] = $val; |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | - return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 302 | + return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | |
@@ -321,75 +321,75 @@ discard block |
||
| 321 | 321 | * texte converti en unicode |
| 322 | 322 | **/ |
| 323 | 323 | function charset2unicode($texte, $charset = 'AUTO' /* $forcer: obsolete*/) { |
| 324 | - static $trans; |
|
| 325 | - |
|
| 326 | - if ($charset === 'AUTO') { |
|
| 327 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - if ($charset === '') { |
|
| 331 | - $charset = 'iso-8859-1'; |
|
| 332 | - } |
|
| 333 | - $charset = strtolower($charset); |
|
| 334 | - |
|
| 335 | - switch ($charset) { |
|
| 336 | - case 'utf-8': |
|
| 337 | - case 'utf8': |
|
| 338 | - return utf_8_to_unicode($texte); |
|
| 339 | - |
|
| 340 | - case 'iso-8859-1': |
|
| 341 | - $texte = corriger_caracteres_windows($texte, 'iso-8859-1'); |
|
| 342 | - // pas de break; ici, on suit sur default: |
|
| 343 | - |
|
| 344 | - default: |
|
| 345 | - // mbstring presente ? |
|
| 346 | - if (init_mb_string()) { |
|
| 347 | - $order = mb_detect_order(); |
|
| 348 | - try { |
|
| 349 | - # mb_string connait-il $charset? |
|
| 350 | - if ($order and mb_detect_order($charset)) { |
|
| 351 | - $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 352 | - if ($s && $s != $texte) { |
|
| 353 | - return utf_8_to_unicode($s); |
|
| 354 | - } |
|
| 355 | - } |
|
| 356 | - } catch (\Error $e) { |
|
| 357 | - // Le charset n'existe probablement pas |
|
| 358 | - } finally { |
|
| 359 | - mb_detect_order($order); # remettre comme precedemment |
|
| 360 | - } |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 364 | - if (!isset($trans[$charset])) { |
|
| 365 | - if ( |
|
| 366 | - $cset = load_charset($charset) |
|
| 367 | - and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 368 | - ) { |
|
| 369 | - foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 370 | - $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - } |
|
| 374 | - if (isset($trans[$charset]) and is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 375 | - return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - // Sinon demander a iconv (malgre le fait qu'il coupe quand un |
|
| 379 | - // caractere n'appartient pas au charset, mais c'est un probleme |
|
| 380 | - // surtout en utf-8, gere ci-dessus) |
|
| 381 | - if (test_iconv()) { |
|
| 382 | - $s = iconv($charset, 'utf-32le', $texte); |
|
| 383 | - if ($s) { |
|
| 384 | - return utf_32_to_unicode($s); |
|
| 385 | - } |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - // Au pire ne rien faire |
|
| 389 | - spip_log("erreur charset '$charset' non supporte"); |
|
| 390 | - |
|
| 391 | - return $texte; |
|
| 392 | - } |
|
| 324 | + static $trans; |
|
| 325 | + |
|
| 326 | + if ($charset === 'AUTO') { |
|
| 327 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + if ($charset === '') { |
|
| 331 | + $charset = 'iso-8859-1'; |
|
| 332 | + } |
|
| 333 | + $charset = strtolower($charset); |
|
| 334 | + |
|
| 335 | + switch ($charset) { |
|
| 336 | + case 'utf-8': |
|
| 337 | + case 'utf8': |
|
| 338 | + return utf_8_to_unicode($texte); |
|
| 339 | + |
|
| 340 | + case 'iso-8859-1': |
|
| 341 | + $texte = corriger_caracteres_windows($texte, 'iso-8859-1'); |
|
| 342 | + // pas de break; ici, on suit sur default: |
|
| 343 | + |
|
| 344 | + default: |
|
| 345 | + // mbstring presente ? |
|
| 346 | + if (init_mb_string()) { |
|
| 347 | + $order = mb_detect_order(); |
|
| 348 | + try { |
|
| 349 | + # mb_string connait-il $charset? |
|
| 350 | + if ($order and mb_detect_order($charset)) { |
|
| 351 | + $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 352 | + if ($s && $s != $texte) { |
|
| 353 | + return utf_8_to_unicode($s); |
|
| 354 | + } |
|
| 355 | + } |
|
| 356 | + } catch (\Error $e) { |
|
| 357 | + // Le charset n'existe probablement pas |
|
| 358 | + } finally { |
|
| 359 | + mb_detect_order($order); # remettre comme precedemment |
|
| 360 | + } |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 364 | + if (!isset($trans[$charset])) { |
|
| 365 | + if ( |
|
| 366 | + $cset = load_charset($charset) |
|
| 367 | + and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 368 | + ) { |
|
| 369 | + foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 370 | + $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + } |
|
| 374 | + if (isset($trans[$charset]) and is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 375 | + return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + // Sinon demander a iconv (malgre le fait qu'il coupe quand un |
|
| 379 | + // caractere n'appartient pas au charset, mais c'est un probleme |
|
| 380 | + // surtout en utf-8, gere ci-dessus) |
|
| 381 | + if (test_iconv()) { |
|
| 382 | + $s = iconv($charset, 'utf-32le', $texte); |
|
| 383 | + if ($s) { |
|
| 384 | + return utf_32_to_unicode($s); |
|
| 385 | + } |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + // Au pire ne rien faire |
|
| 389 | + spip_log("erreur charset '$charset' non supporte"); |
|
| 390 | + |
|
| 391 | + return $texte; |
|
| 392 | + } |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | |
@@ -408,43 +408,43 @@ discard block |
||
| 408 | 408 | * texte transformé dans le charset souhaité |
| 409 | 409 | **/ |
| 410 | 410 | function unicode2charset($texte, $charset = 'AUTO') { |
| 411 | - static $CHARSET_REVERSE = []; |
|
| 412 | - static $trans = []; |
|
| 413 | - |
|
| 414 | - if ($charset == 'AUTO') { |
|
| 415 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - switch ($charset) { |
|
| 419 | - case 'utf-8': |
|
| 420 | - return unicode_to_utf_8($texte); |
|
| 421 | - |
|
| 422 | - default: |
|
| 423 | - $charset = load_charset($charset); |
|
| 424 | - |
|
| 425 | - if (empty($CHARSET_REVERSE[$charset])) { |
|
| 426 | - $CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]); |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - if (!isset($trans[$charset])) { |
|
| 430 | - $trans[$charset] = []; |
|
| 431 | - $t = &$trans[$charset]; |
|
| 432 | - for ($e = 128; $e < 255; $e++) { |
|
| 433 | - $h = dechex($e); |
|
| 434 | - if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
|
| 435 | - $s = $CHARSET_REVERSE[$charset][$e]; |
|
| 436 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 437 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 438 | - } else { |
|
| 439 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 440 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 441 | - } |
|
| 442 | - } |
|
| 443 | - } |
|
| 444 | - $texte = str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 445 | - |
|
| 446 | - return $texte; |
|
| 447 | - } |
|
| 411 | + static $CHARSET_REVERSE = []; |
|
| 412 | + static $trans = []; |
|
| 413 | + |
|
| 414 | + if ($charset == 'AUTO') { |
|
| 415 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + switch ($charset) { |
|
| 419 | + case 'utf-8': |
|
| 420 | + return unicode_to_utf_8($texte); |
|
| 421 | + |
|
| 422 | + default: |
|
| 423 | + $charset = load_charset($charset); |
|
| 424 | + |
|
| 425 | + if (empty($CHARSET_REVERSE[$charset])) { |
|
| 426 | + $CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]); |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + if (!isset($trans[$charset])) { |
|
| 430 | + $trans[$charset] = []; |
|
| 431 | + $t = &$trans[$charset]; |
|
| 432 | + for ($e = 128; $e < 255; $e++) { |
|
| 433 | + $h = dechex($e); |
|
| 434 | + if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
|
| 435 | + $s = $CHARSET_REVERSE[$charset][$e]; |
|
| 436 | + $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 437 | + $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 438 | + } else { |
|
| 439 | + $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 440 | + $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | + } |
|
| 444 | + $texte = str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 445 | + |
|
| 446 | + return $texte; |
|
| 447 | + } |
|
| 448 | 448 | } |
| 449 | 449 | |
| 450 | 450 | |
@@ -462,40 +462,40 @@ discard block |
||
| 462 | 462 | * texte transformé dans le charset site |
| 463 | 463 | **/ |
| 464 | 464 | function importer_charset($texte, $charset = 'AUTO') { |
| 465 | - $s = null; |
|
| 466 | - static $trans = []; |
|
| 467 | - // on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite ! |
|
| 468 | - if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset'] == 'utf-8')) { |
|
| 469 | - $texte = corriger_caracteres_windows($texte, 'iso-8859-1', $GLOBALS['meta']['charset']); |
|
| 470 | - if (init_mb_string()) { |
|
| 471 | - if ( |
|
| 472 | - $order = mb_detect_order() # mb_string connait-il $charset? |
|
| 473 | - and mb_detect_order($charset) |
|
| 474 | - ) { |
|
| 475 | - $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 476 | - } |
|
| 477 | - mb_detect_order($order); # remettre comme precedemment |
|
| 478 | - return $s; |
|
| 479 | - } |
|
| 480 | - // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 481 | - if (!isset($trans[$charset])) { |
|
| 482 | - if ( |
|
| 483 | - $cset = load_charset($charset) |
|
| 484 | - and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 485 | - ) { |
|
| 486 | - foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 487 | - $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 488 | - } |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - if (is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 492 | - return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - return $texte; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - return unicode2charset(charset2unicode($texte, $charset)); |
|
| 465 | + $s = null; |
|
| 466 | + static $trans = []; |
|
| 467 | + // on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite ! |
|
| 468 | + if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset'] == 'utf-8')) { |
|
| 469 | + $texte = corriger_caracteres_windows($texte, 'iso-8859-1', $GLOBALS['meta']['charset']); |
|
| 470 | + if (init_mb_string()) { |
|
| 471 | + if ( |
|
| 472 | + $order = mb_detect_order() # mb_string connait-il $charset? |
|
| 473 | + and mb_detect_order($charset) |
|
| 474 | + ) { |
|
| 475 | + $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 476 | + } |
|
| 477 | + mb_detect_order($order); # remettre comme precedemment |
|
| 478 | + return $s; |
|
| 479 | + } |
|
| 480 | + // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 481 | + if (!isset($trans[$charset])) { |
|
| 482 | + if ( |
|
| 483 | + $cset = load_charset($charset) |
|
| 484 | + and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 485 | + ) { |
|
| 486 | + foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 487 | + $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 488 | + } |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + if (is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 492 | + return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + return $texte; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + return unicode2charset(charset2unicode($texte, $charset)); |
|
| 499 | 499 | } |
| 500 | 500 | |
| 501 | 501 | |
@@ -511,92 +511,92 @@ discard block |
||
| 511 | 511 | **/ |
| 512 | 512 | function utf_8_to_unicode($source) { |
| 513 | 513 | |
| 514 | - // mb_string : methode rapide |
|
| 515 | - if (init_mb_string()) { |
|
| 516 | - $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 517 | - |
|
| 518 | - return mb_encode_numericentity($source, $convmap, 'UTF-8'); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - // Sinon methode pas a pas |
|
| 522 | - static $decrement; |
|
| 523 | - static $shift; |
|
| 524 | - |
|
| 525 | - // Cf. php.net, par Ronen. Adapte pour compatibilite < php4 |
|
| 526 | - if (!is_array($decrement)) { |
|
| 527 | - // array used to figure what number to decrement from character order value |
|
| 528 | - // according to number of characters used to map unicode to ascii by utf-8 |
|
| 529 | - $decrement[4] = 240; |
|
| 530 | - $decrement[3] = 224; |
|
| 531 | - $decrement[2] = 192; |
|
| 532 | - $decrement[1] = 0; |
|
| 533 | - // the number of bits to shift each charNum by |
|
| 534 | - $shift[1][0] = 0; |
|
| 535 | - $shift[2][0] = 6; |
|
| 536 | - $shift[2][1] = 0; |
|
| 537 | - $shift[3][0] = 12; |
|
| 538 | - $shift[3][1] = 6; |
|
| 539 | - $shift[3][2] = 0; |
|
| 540 | - $shift[4][0] = 18; |
|
| 541 | - $shift[4][1] = 12; |
|
| 542 | - $shift[4][2] = 6; |
|
| 543 | - $shift[4][3] = 0; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - $pos = 0; |
|
| 547 | - $len = strlen($source); |
|
| 548 | - $encodedString = ''; |
|
| 549 | - while ($pos < $len) { |
|
| 550 | - $char = ''; |
|
| 551 | - $ischar = false; |
|
| 552 | - $asciiPos = ord(substr($source, $pos, 1)); |
|
| 553 | - if (($asciiPos >= 240) && ($asciiPos <= 255)) { |
|
| 554 | - // 4 chars representing one unicode character |
|
| 555 | - $thisLetter = substr($source, $pos, 4); |
|
| 556 | - $pos += 4; |
|
| 557 | - } else { |
|
| 558 | - if (($asciiPos >= 224) && ($asciiPos <= 239)) { |
|
| 559 | - // 3 chars representing one unicode character |
|
| 560 | - $thisLetter = substr($source, $pos, 3); |
|
| 561 | - $pos += 3; |
|
| 562 | - } else { |
|
| 563 | - if (($asciiPos >= 192) && ($asciiPos <= 223)) { |
|
| 564 | - // 2 chars representing one unicode character |
|
| 565 | - $thisLetter = substr($source, $pos, 2); |
|
| 566 | - $pos += 2; |
|
| 567 | - } else { |
|
| 568 | - // 1 char (lower ascii) |
|
| 569 | - $thisLetter = substr($source, $pos, 1); |
|
| 570 | - $pos += 1; |
|
| 571 | - $char = $thisLetter; |
|
| 572 | - $ischar = true; |
|
| 573 | - } |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - if ($ischar) { |
|
| 578 | - $encodedString .= $char; |
|
| 579 | - } else { // process the string representing the letter to a unicode entity |
|
| 580 | - $thisLen = strlen($thisLetter); |
|
| 581 | - $thisPos = 0; |
|
| 582 | - $decimalCode = 0; |
|
| 583 | - while ($thisPos < $thisLen) { |
|
| 584 | - $thisCharOrd = ord(substr($thisLetter, $thisPos, 1)); |
|
| 585 | - if ($thisPos == 0) { |
|
| 586 | - $charNum = intval($thisCharOrd - $decrement[$thisLen]); |
|
| 587 | - $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 588 | - } else { |
|
| 589 | - $charNum = intval($thisCharOrd - 128); |
|
| 590 | - $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 591 | - } |
|
| 592 | - $thisPos++; |
|
| 593 | - } |
|
| 594 | - $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 595 | - $encodedString .= $encodedLetter; |
|
| 596 | - } |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - return $encodedString; |
|
| 514 | + // mb_string : methode rapide |
|
| 515 | + if (init_mb_string()) { |
|
| 516 | + $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 517 | + |
|
| 518 | + return mb_encode_numericentity($source, $convmap, 'UTF-8'); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + // Sinon methode pas a pas |
|
| 522 | + static $decrement; |
|
| 523 | + static $shift; |
|
| 524 | + |
|
| 525 | + // Cf. php.net, par Ronen. Adapte pour compatibilite < php4 |
|
| 526 | + if (!is_array($decrement)) { |
|
| 527 | + // array used to figure what number to decrement from character order value |
|
| 528 | + // according to number of characters used to map unicode to ascii by utf-8 |
|
| 529 | + $decrement[4] = 240; |
|
| 530 | + $decrement[3] = 224; |
|
| 531 | + $decrement[2] = 192; |
|
| 532 | + $decrement[1] = 0; |
|
| 533 | + // the number of bits to shift each charNum by |
|
| 534 | + $shift[1][0] = 0; |
|
| 535 | + $shift[2][0] = 6; |
|
| 536 | + $shift[2][1] = 0; |
|
| 537 | + $shift[3][0] = 12; |
|
| 538 | + $shift[3][1] = 6; |
|
| 539 | + $shift[3][2] = 0; |
|
| 540 | + $shift[4][0] = 18; |
|
| 541 | + $shift[4][1] = 12; |
|
| 542 | + $shift[4][2] = 6; |
|
| 543 | + $shift[4][3] = 0; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + $pos = 0; |
|
| 547 | + $len = strlen($source); |
|
| 548 | + $encodedString = ''; |
|
| 549 | + while ($pos < $len) { |
|
| 550 | + $char = ''; |
|
| 551 | + $ischar = false; |
|
| 552 | + $asciiPos = ord(substr($source, $pos, 1)); |
|
| 553 | + if (($asciiPos >= 240) && ($asciiPos <= 255)) { |
|
| 554 | + // 4 chars representing one unicode character |
|
| 555 | + $thisLetter = substr($source, $pos, 4); |
|
| 556 | + $pos += 4; |
|
| 557 | + } else { |
|
| 558 | + if (($asciiPos >= 224) && ($asciiPos <= 239)) { |
|
| 559 | + // 3 chars representing one unicode character |
|
| 560 | + $thisLetter = substr($source, $pos, 3); |
|
| 561 | + $pos += 3; |
|
| 562 | + } else { |
|
| 563 | + if (($asciiPos >= 192) && ($asciiPos <= 223)) { |
|
| 564 | + // 2 chars representing one unicode character |
|
| 565 | + $thisLetter = substr($source, $pos, 2); |
|
| 566 | + $pos += 2; |
|
| 567 | + } else { |
|
| 568 | + // 1 char (lower ascii) |
|
| 569 | + $thisLetter = substr($source, $pos, 1); |
|
| 570 | + $pos += 1; |
|
| 571 | + $char = $thisLetter; |
|
| 572 | + $ischar = true; |
|
| 573 | + } |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + if ($ischar) { |
|
| 578 | + $encodedString .= $char; |
|
| 579 | + } else { // process the string representing the letter to a unicode entity |
|
| 580 | + $thisLen = strlen($thisLetter); |
|
| 581 | + $thisPos = 0; |
|
| 582 | + $decimalCode = 0; |
|
| 583 | + while ($thisPos < $thisLen) { |
|
| 584 | + $thisCharOrd = ord(substr($thisLetter, $thisPos, 1)); |
|
| 585 | + if ($thisPos == 0) { |
|
| 586 | + $charNum = intval($thisCharOrd - $decrement[$thisLen]); |
|
| 587 | + $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 588 | + } else { |
|
| 589 | + $charNum = intval($thisCharOrd - 128); |
|
| 590 | + $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 591 | + } |
|
| 592 | + $thisPos++; |
|
| 593 | + } |
|
| 594 | + $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 595 | + $encodedString .= $encodedLetter; |
|
| 596 | + } |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + return $encodedString; |
|
| 600 | 600 | } |
| 601 | 601 | |
| 602 | 602 | /** |
@@ -615,32 +615,32 @@ discard block |
||
| 615 | 615 | **/ |
| 616 | 616 | function utf_32_to_unicode($source) { |
| 617 | 617 | |
| 618 | - // mb_string : methode rapide |
|
| 619 | - if (init_mb_string()) { |
|
| 620 | - $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 621 | - $source = mb_encode_numericentity($source, $convmap, 'UTF-32LE'); |
|
| 622 | - |
|
| 623 | - return str_replace(chr(0), '', $source); |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - // Sinon methode lente |
|
| 627 | - $texte = ''; |
|
| 628 | - while ($source) { |
|
| 629 | - $words = unpack('V*', substr($source, 0, 1024)); |
|
| 630 | - $source = substr($source, 1024); |
|
| 631 | - foreach ($words as $word) { |
|
| 632 | - if ($word < 128) { |
|
| 633 | - $texte .= chr($word); |
|
| 634 | - } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
|
| 635 | - else { |
|
| 636 | - if ($word != 65279) { |
|
| 637 | - $texte .= '&#' . $word . ';'; |
|
| 638 | - } |
|
| 639 | - } |
|
| 640 | - } |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - return $texte; |
|
| 618 | + // mb_string : methode rapide |
|
| 619 | + if (init_mb_string()) { |
|
| 620 | + $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 621 | + $source = mb_encode_numericentity($source, $convmap, 'UTF-32LE'); |
|
| 622 | + |
|
| 623 | + return str_replace(chr(0), '', $source); |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + // Sinon methode lente |
|
| 627 | + $texte = ''; |
|
| 628 | + while ($source) { |
|
| 629 | + $words = unpack('V*', substr($source, 0, 1024)); |
|
| 630 | + $source = substr($source, 1024); |
|
| 631 | + foreach ($words as $word) { |
|
| 632 | + if ($word < 128) { |
|
| 633 | + $texte .= chr($word); |
|
| 634 | + } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
|
| 635 | + else { |
|
| 636 | + if ($word != 65279) { |
|
| 637 | + $texte .= '&#' . $word . ';'; |
|
| 638 | + } |
|
| 639 | + } |
|
| 640 | + } |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + return $texte; |
|
| 644 | 644 | } |
| 645 | 645 | |
| 646 | 646 | |
@@ -657,21 +657,21 @@ discard block |
||
| 657 | 657 | * Caractère utf8 si trouvé, '' sinon |
| 658 | 658 | **/ |
| 659 | 659 | function caractere_utf_8($num) { |
| 660 | - $num = intval($num); |
|
| 661 | - if ($num < 128) { |
|
| 662 | - return chr($num); |
|
| 663 | - } |
|
| 664 | - if ($num < 2048) { |
|
| 665 | - return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 666 | - } |
|
| 667 | - if ($num < 65536) { |
|
| 668 | - return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 669 | - } |
|
| 670 | - if ($num < 1_114_112) { |
|
| 671 | - return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - return ''; |
|
| 660 | + $num = intval($num); |
|
| 661 | + if ($num < 128) { |
|
| 662 | + return chr($num); |
|
| 663 | + } |
|
| 664 | + if ($num < 2048) { |
|
| 665 | + return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 666 | + } |
|
| 667 | + if ($num < 65536) { |
|
| 668 | + return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 669 | + } |
|
| 670 | + if ($num < 1_114_112) { |
|
| 671 | + return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + return ''; |
|
| 675 | 675 | } |
| 676 | 676 | |
| 677 | 677 | /** |
@@ -684,42 +684,42 @@ discard block |
||
| 684 | 684 | **/ |
| 685 | 685 | function unicode_to_utf_8($texte) { |
| 686 | 686 | |
| 687 | - // 1. Entites € et suivantes |
|
| 688 | - $vu = []; |
|
| 689 | - if ( |
|
| 690 | - preg_match_all( |
|
| 691 | - ',�*([1-9][0-9][0-9]+);,S', |
|
| 692 | - $texte, |
|
| 693 | - $regs, |
|
| 694 | - PREG_SET_ORDER |
|
| 695 | - ) |
|
| 696 | - ) { |
|
| 697 | - foreach ($regs as $reg) { |
|
| 698 | - if ($reg[1] > 127 and !isset($vu[$reg[0]])) { |
|
| 699 | - $vu[$reg[0]] = caractere_utf_8($reg[1]); |
|
| 700 | - } |
|
| 701 | - } |
|
| 702 | - } |
|
| 703 | - //$texte = str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 704 | - |
|
| 705 | - // 2. Entites > ÿ |
|
| 706 | - //$vu = array(); |
|
| 707 | - if ( |
|
| 708 | - preg_match_all( |
|
| 709 | - ',�*([1-9a-f][0-9a-f][0-9a-f]+);,iS', |
|
| 710 | - $texte, |
|
| 711 | - $regs, |
|
| 712 | - PREG_SET_ORDER |
|
| 713 | - ) |
|
| 714 | - ) { |
|
| 715 | - foreach ($regs as $reg) { |
|
| 716 | - if (!isset($vu[$reg[0]])) { |
|
| 717 | - $vu[$reg[0]] = caractere_utf_8(hexdec($reg[1])); |
|
| 718 | - } |
|
| 719 | - } |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - return str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 687 | + // 1. Entites € et suivantes |
|
| 688 | + $vu = []; |
|
| 689 | + if ( |
|
| 690 | + preg_match_all( |
|
| 691 | + ',�*([1-9][0-9][0-9]+);,S', |
|
| 692 | + $texte, |
|
| 693 | + $regs, |
|
| 694 | + PREG_SET_ORDER |
|
| 695 | + ) |
|
| 696 | + ) { |
|
| 697 | + foreach ($regs as $reg) { |
|
| 698 | + if ($reg[1] > 127 and !isset($vu[$reg[0]])) { |
|
| 699 | + $vu[$reg[0]] = caractere_utf_8($reg[1]); |
|
| 700 | + } |
|
| 701 | + } |
|
| 702 | + } |
|
| 703 | + //$texte = str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 704 | + |
|
| 705 | + // 2. Entites > ÿ |
|
| 706 | + //$vu = array(); |
|
| 707 | + if ( |
|
| 708 | + preg_match_all( |
|
| 709 | + ',�*([1-9a-f][0-9a-f][0-9a-f]+);,iS', |
|
| 710 | + $texte, |
|
| 711 | + $regs, |
|
| 712 | + PREG_SET_ORDER |
|
| 713 | + ) |
|
| 714 | + ) { |
|
| 715 | + foreach ($regs as $reg) { |
|
| 716 | + if (!isset($vu[$reg[0]])) { |
|
| 717 | + $vu[$reg[0]] = caractere_utf_8(hexdec($reg[1])); |
|
| 718 | + } |
|
| 719 | + } |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + return str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 723 | 723 | } |
| 724 | 724 | |
| 725 | 725 | /** |
@@ -731,15 +731,15 @@ discard block |
||
| 731 | 731 | * texte converti |
| 732 | 732 | **/ |
| 733 | 733 | function unicode_to_javascript($texte) { |
| 734 | - $vu = []; |
|
| 735 | - while (preg_match(',�*([0-9]+);,S', $texte, $regs) and !isset($vu[$regs[1]])) { |
|
| 736 | - $num = $regs[1]; |
|
| 737 | - $vu[$num] = true; |
|
| 738 | - $s = '\u' . sprintf('%04x', $num); |
|
| 739 | - $texte = str_replace($regs[0], $s, $texte); |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - return $texte; |
|
| 734 | + $vu = []; |
|
| 735 | + while (preg_match(',�*([0-9]+);,S', $texte, $regs) and !isset($vu[$regs[1]])) { |
|
| 736 | + $num = $regs[1]; |
|
| 737 | + $vu[$num] = true; |
|
| 738 | + $s = '\u' . sprintf('%04x', $num); |
|
| 739 | + $texte = str_replace($regs[0], $s, $texte); |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + return $texte; |
|
| 743 | 743 | } |
| 744 | 744 | |
| 745 | 745 | /** |
@@ -751,11 +751,11 @@ discard block |
||
| 751 | 751 | * texte converti |
| 752 | 752 | **/ |
| 753 | 753 | function javascript_to_unicode($texte) { |
| 754 | - while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 755 | - $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 756 | - } |
|
| 754 | + while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 755 | + $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 756 | + } |
|
| 757 | 757 | |
| 758 | - return $texte; |
|
| 758 | + return $texte; |
|
| 759 | 759 | } |
| 760 | 760 | |
| 761 | 761 | /** |
@@ -767,11 +767,11 @@ discard block |
||
| 767 | 767 | * texte converti |
| 768 | 768 | **/ |
| 769 | 769 | function javascript_to_binary($texte) { |
| 770 | - while (preg_match(',%([0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 771 | - $texte = str_replace($regs[0], chr(hexdec($regs[1])), $texte); |
|
| 772 | - } |
|
| 770 | + while (preg_match(',%([0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 771 | + $texte = str_replace($regs[0], chr(hexdec($regs[1])), $texte); |
|
| 772 | + } |
|
| 773 | 773 | |
| 774 | - return $texte; |
|
| 774 | + return $texte; |
|
| 775 | 775 | } |
| 776 | 776 | |
| 777 | 777 | |
@@ -789,26 +789,26 @@ discard block |
||
| 789 | 789 | * @return string |
| 790 | 790 | */ |
| 791 | 791 | function translitteration_rapide($texte, $charset = 'AUTO', $complexe = ''): string { |
| 792 | - static $trans = []; |
|
| 793 | - if ($charset == 'AUTO') { |
|
| 794 | - $charset = $GLOBALS['meta']['charset']; |
|
| 795 | - } |
|
| 796 | - if (!strlen($texte)) { |
|
| 797 | - return $texte; |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - $table_translit = 'translit' . $complexe; |
|
| 801 | - |
|
| 802 | - // 2. Translitterer grace a la table predefinie |
|
| 803 | - if (!isset($trans[$complexe])) { |
|
| 804 | - $trans[$complexe] = []; |
|
| 805 | - load_charset($table_translit); |
|
| 806 | - foreach ($GLOBALS['CHARSET'][$table_translit] as $key => $val) { |
|
| 807 | - $trans[$complexe][caractere_utf_8($key)] = $val; |
|
| 808 | - } |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - return str_replace(array_keys($trans[$complexe]), array_values($trans[$complexe]), $texte); |
|
| 792 | + static $trans = []; |
|
| 793 | + if ($charset == 'AUTO') { |
|
| 794 | + $charset = $GLOBALS['meta']['charset']; |
|
| 795 | + } |
|
| 796 | + if (!strlen($texte)) { |
|
| 797 | + return $texte; |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + $table_translit = 'translit' . $complexe; |
|
| 801 | + |
|
| 802 | + // 2. Translitterer grace a la table predefinie |
|
| 803 | + if (!isset($trans[$complexe])) { |
|
| 804 | + $trans[$complexe] = []; |
|
| 805 | + load_charset($table_translit); |
|
| 806 | + foreach ($GLOBALS['CHARSET'][$table_translit] as $key => $val) { |
|
| 807 | + $trans[$complexe][caractere_utf_8($key)] = $val; |
|
| 808 | + } |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + return str_replace(array_keys($trans[$complexe]), array_values($trans[$complexe]), $texte); |
|
| 812 | 812 | } |
| 813 | 813 | |
| 814 | 814 | /** |
@@ -831,14 +831,14 @@ discard block |
||
| 831 | 831 | * @return string |
| 832 | 832 | */ |
| 833 | 833 | function translitteration($texte, $charset = 'AUTO', $complexe = ''): string { |
| 834 | - // 0. Supprimer les caracteres illegaux |
|
| 835 | - include_spip('inc/filtres'); |
|
| 836 | - $texte = corriger_caracteres($texte); |
|
| 834 | + // 0. Supprimer les caracteres illegaux |
|
| 835 | + include_spip('inc/filtres'); |
|
| 836 | + $texte = corriger_caracteres($texte); |
|
| 837 | 837 | |
| 838 | - // 1. Passer le charset et les é en utf-8 |
|
| 839 | - $texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset))); |
|
| 838 | + // 1. Passer le charset et les é en utf-8 |
|
| 839 | + $texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset))); |
|
| 840 | 840 | |
| 841 | - return translitteration_rapide($texte, $charset, $complexe); |
|
| 841 | + return translitteration_rapide($texte, $charset, $complexe); |
|
| 842 | 842 | } |
| 843 | 843 | |
| 844 | 844 | /** |
@@ -853,17 +853,17 @@ discard block |
||
| 853 | 853 | * @return string |
| 854 | 854 | */ |
| 855 | 855 | function translitteration_complexe($texte, $chiffres = false): string { |
| 856 | - $texte = translitteration($texte, 'AUTO', 'complexe'); |
|
| 856 | + $texte = translitteration($texte, 'AUTO', 'complexe'); |
|
| 857 | 857 | |
| 858 | - if ($chiffres) { |
|
| 859 | - $texte = preg_replace_callback( |
|
| 860 | - "/[aeiuoyd]['`?~.^+(-]{1,2}/S", |
|
| 861 | - fn($m) => translitteration_chiffree($m[0]), |
|
| 862 | - $texte |
|
| 863 | - ); |
|
| 864 | - } |
|
| 858 | + if ($chiffres) { |
|
| 859 | + $texte = preg_replace_callback( |
|
| 860 | + "/[aeiuoyd]['`?~.^+(-]{1,2}/S", |
|
| 861 | + fn($m) => translitteration_chiffree($m[0]), |
|
| 862 | + $texte |
|
| 863 | + ); |
|
| 864 | + } |
|
| 865 | 865 | |
| 866 | - return $texte; |
|
| 866 | + return $texte; |
|
| 867 | 867 | } |
| 868 | 868 | |
| 869 | 869 | /** |
@@ -875,7 +875,7 @@ discard block |
||
| 875 | 875 | * @return string |
| 876 | 876 | */ |
| 877 | 877 | function translitteration_chiffree($car): string { |
| 878 | - return strtr($car, "'`?~.^+(-", '123456789'); |
|
| 878 | + return strtr($car, "'`?~.^+(-", '123456789'); |
|
| 879 | 879 | } |
| 880 | 880 | |
| 881 | 881 | |
@@ -888,7 +888,7 @@ discard block |
||
| 888 | 888 | * true s'il a un BOM |
| 889 | 889 | **/ |
| 890 | 890 | function bom_utf8($texte): bool { |
| 891 | - return (substr($texte, 0, 3) === chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 891 | + return (substr($texte, 0, 3) === chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 892 | 892 | } |
| 893 | 893 | |
| 894 | 894 | /** |
@@ -905,21 +905,21 @@ discard block |
||
| 905 | 905 | * true si c'est le cas |
| 906 | 906 | **/ |
| 907 | 907 | function is_utf8($string): bool { |
| 908 | - return !strlen( |
|
| 909 | - preg_replace( |
|
| 910 | - ',[\x09\x0A\x0D\x20-\x7E]' # ASCII |
|
| 911 | - . '|[\xC2-\xDF][\x80-\xBF]' # non-overlong 2-byte |
|
| 912 | - . '|\xE0[\xA0-\xBF][\x80-\xBF]' # excluding overlongs |
|
| 913 | - . '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' # straight 3-byte |
|
| 914 | - . '|\xED[\x80-\x9F][\x80-\xBF]' # excluding surrogates |
|
| 915 | - . '|\xF0[\x90-\xBF][\x80-\xBF]{2}' # planes 1-3 |
|
| 916 | - . '|[\xF1-\xF3][\x80-\xBF]{3}' # planes 4-15 |
|
| 917 | - . '|\xF4[\x80-\x8F][\x80-\xBF]{2}' # plane 16 |
|
| 918 | - . ',sS', |
|
| 919 | - '', |
|
| 920 | - $string |
|
| 921 | - ) |
|
| 922 | - ); |
|
| 908 | + return !strlen( |
|
| 909 | + preg_replace( |
|
| 910 | + ',[\x09\x0A\x0D\x20-\x7E]' # ASCII |
|
| 911 | + . '|[\xC2-\xDF][\x80-\xBF]' # non-overlong 2-byte |
|
| 912 | + . '|\xE0[\xA0-\xBF][\x80-\xBF]' # excluding overlongs |
|
| 913 | + . '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' # straight 3-byte |
|
| 914 | + . '|\xED[\x80-\x9F][\x80-\xBF]' # excluding surrogates |
|
| 915 | + . '|\xF0[\x90-\xBF][\x80-\xBF]{2}' # planes 1-3 |
|
| 916 | + . '|[\xF1-\xF3][\x80-\xBF]{3}' # planes 4-15 |
|
| 917 | + . '|\xF4[\x80-\x8F][\x80-\xBF]{2}' # plane 16 |
|
| 918 | + . ',sS', |
|
| 919 | + '', |
|
| 920 | + $string |
|
| 921 | + ) |
|
| 922 | + ); |
|
| 923 | 923 | } |
| 924 | 924 | |
| 925 | 925 | /** |
@@ -931,13 +931,13 @@ discard block |
||
| 931 | 931 | * true si c'est le cas |
| 932 | 932 | **/ |
| 933 | 933 | function is_ascii($string): bool { |
| 934 | - return !strlen( |
|
| 935 | - preg_replace( |
|
| 936 | - ',[\x09\x0A\x0D\x20-\x7E],sS', |
|
| 937 | - '', |
|
| 938 | - $string |
|
| 939 | - ) |
|
| 940 | - ); |
|
| 934 | + return !strlen( |
|
| 935 | + preg_replace( |
|
| 936 | + ',[\x09\x0A\x0D\x20-\x7E],sS', |
|
| 937 | + '', |
|
| 938 | + $string |
|
| 939 | + ) |
|
| 940 | + ); |
|
| 941 | 941 | } |
| 942 | 942 | |
| 943 | 943 | /** |
@@ -956,53 +956,53 @@ discard block |
||
| 956 | 956 | **/ |
| 957 | 957 | function transcoder_page($texte, $headers = ''): string { |
| 958 | 958 | |
| 959 | - // Si tout est < 128 pas la peine d'aller plus loin |
|
| 960 | - if (is_ascii($texte)) { |
|
| 961 | - #spip_log('charset: ascii'); |
|
| 962 | - return $texte; |
|
| 963 | - } |
|
| 964 | - |
|
| 965 | - if (bom_utf8($texte)) { |
|
| 966 | - // Reconnaitre le BOM utf-8 (0xEFBBBF) |
|
| 967 | - $charset = 'utf-8'; |
|
| 968 | - $texte = substr($texte, 3); |
|
| 969 | - } elseif (preg_match(',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) { |
|
| 970 | - // charset precise par le contenu (xml) |
|
| 971 | - $charset = trim(strtolower($regs[1])); |
|
| 972 | - } elseif ( |
|
| 973 | - // charset precise par le contenu (html) |
|
| 974 | - preg_match(',<(meta|html|body)[^>]*charset[^>]*=[^>]*([#-_a-z0-9]+?),UimsS', $texte, $regs) |
|
| 975 | - # eviter toute balise SPIP tel que #CHARSET ou #CONFIG d'un squelette |
|
| 976 | - and !str_contains($regs[2], '#') |
|
| 977 | - and $tmp = trim(strtolower($regs[2])) |
|
| 978 | - ) { |
|
| 979 | - $charset = $tmp; |
|
| 980 | - } elseif (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) { |
|
| 981 | - // charset de la reponse http |
|
| 982 | - $charset = trim(strtolower($regs[1])); |
|
| 983 | - } else { |
|
| 984 | - $charset = ''; |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - |
|
| 988 | - // normaliser les noms du shif-jis japonais |
|
| 989 | - if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) { |
|
| 990 | - $charset = 'shift-jis'; |
|
| 991 | - } |
|
| 992 | - |
|
| 993 | - if ($charset) { |
|
| 994 | - spip_log("charset: $charset"); |
|
| 995 | - } else { |
|
| 996 | - // valeur par defaut |
|
| 997 | - if (is_utf8($texte)) { |
|
| 998 | - $charset = 'utf-8'; |
|
| 999 | - } else { |
|
| 1000 | - $charset = 'iso-8859-1'; |
|
| 1001 | - } |
|
| 1002 | - spip_log("charset probable: $charset"); |
|
| 1003 | - } |
|
| 1004 | - |
|
| 1005 | - return importer_charset($texte, $charset); |
|
| 959 | + // Si tout est < 128 pas la peine d'aller plus loin |
|
| 960 | + if (is_ascii($texte)) { |
|
| 961 | + #spip_log('charset: ascii'); |
|
| 962 | + return $texte; |
|
| 963 | + } |
|
| 964 | + |
|
| 965 | + if (bom_utf8($texte)) { |
|
| 966 | + // Reconnaitre le BOM utf-8 (0xEFBBBF) |
|
| 967 | + $charset = 'utf-8'; |
|
| 968 | + $texte = substr($texte, 3); |
|
| 969 | + } elseif (preg_match(',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) { |
|
| 970 | + // charset precise par le contenu (xml) |
|
| 971 | + $charset = trim(strtolower($regs[1])); |
|
| 972 | + } elseif ( |
|
| 973 | + // charset precise par le contenu (html) |
|
| 974 | + preg_match(',<(meta|html|body)[^>]*charset[^>]*=[^>]*([#-_a-z0-9]+?),UimsS', $texte, $regs) |
|
| 975 | + # eviter toute balise SPIP tel que #CHARSET ou #CONFIG d'un squelette |
|
| 976 | + and !str_contains($regs[2], '#') |
|
| 977 | + and $tmp = trim(strtolower($regs[2])) |
|
| 978 | + ) { |
|
| 979 | + $charset = $tmp; |
|
| 980 | + } elseif (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) { |
|
| 981 | + // charset de la reponse http |
|
| 982 | + $charset = trim(strtolower($regs[1])); |
|
| 983 | + } else { |
|
| 984 | + $charset = ''; |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + |
|
| 988 | + // normaliser les noms du shif-jis japonais |
|
| 989 | + if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) { |
|
| 990 | + $charset = 'shift-jis'; |
|
| 991 | + } |
|
| 992 | + |
|
| 993 | + if ($charset) { |
|
| 994 | + spip_log("charset: $charset"); |
|
| 995 | + } else { |
|
| 996 | + // valeur par defaut |
|
| 997 | + if (is_utf8($texte)) { |
|
| 998 | + $charset = 'utf-8'; |
|
| 999 | + } else { |
|
| 1000 | + $charset = 'iso-8859-1'; |
|
| 1001 | + } |
|
| 1002 | + spip_log("charset probable: $charset"); |
|
| 1003 | + } |
|
| 1004 | + |
|
| 1005 | + return importer_charset($texte, $charset); |
|
| 1006 | 1006 | } |
| 1007 | 1007 | |
| 1008 | 1008 | |
@@ -1026,19 +1026,19 @@ discard block |
||
| 1026 | 1026 | * Le texte coupé |
| 1027 | 1027 | **/ |
| 1028 | 1028 | function spip_substr($c, $start = 0, $length = null) { |
| 1029 | - if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1030 | - if ($length) { |
|
| 1031 | - return substr($c, $start, $length); |
|
| 1032 | - } else { |
|
| 1033 | - return substr($c, $start); |
|
| 1034 | - } |
|
| 1035 | - } |
|
| 1036 | - |
|
| 1037 | - if ($length) { |
|
| 1038 | - return mb_substr($c, $start, $length); |
|
| 1039 | - } else { |
|
| 1040 | - return mb_substr($c, $start); |
|
| 1041 | - } |
|
| 1029 | + if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1030 | + if ($length) { |
|
| 1031 | + return substr($c, $start, $length); |
|
| 1032 | + } else { |
|
| 1033 | + return substr($c, $start); |
|
| 1034 | + } |
|
| 1035 | + } |
|
| 1036 | + |
|
| 1037 | + if ($length) { |
|
| 1038 | + return mb_substr($c, $start, $length); |
|
| 1039 | + } else { |
|
| 1040 | + return mb_substr($c, $start); |
|
| 1041 | + } |
|
| 1042 | 1042 | } |
| 1043 | 1043 | |
| 1044 | 1044 | /** |
@@ -1052,13 +1052,13 @@ discard block |
||
| 1052 | 1052 | * La chaîne avec une majuscule sur le premier mot |
| 1053 | 1053 | */ |
| 1054 | 1054 | function spip_ucfirst($c) { |
| 1055 | - if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1056 | - return ucfirst($c); |
|
| 1057 | - } |
|
| 1055 | + if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1056 | + return ucfirst($c); |
|
| 1057 | + } |
|
| 1058 | 1058 | |
| 1059 | - $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
|
| 1059 | + $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
|
| 1060 | 1060 | |
| 1061 | - return $lettre1 . spip_substr($c, 1); |
|
| 1061 | + return $lettre1 . spip_substr($c, 1); |
|
| 1062 | 1062 | } |
| 1063 | 1063 | |
| 1064 | 1064 | /** |
@@ -1072,11 +1072,11 @@ discard block |
||
| 1072 | 1072 | * La chaîne en minuscules |
| 1073 | 1073 | */ |
| 1074 | 1074 | function spip_strtolower($c) { |
| 1075 | - if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1076 | - return strtolower($c); |
|
| 1077 | - } |
|
| 1075 | + if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1076 | + return strtolower($c); |
|
| 1077 | + } |
|
| 1078 | 1078 | |
| 1079 | - return mb_strtolower($c); |
|
| 1079 | + return mb_strtolower($c); |
|
| 1080 | 1080 | } |
| 1081 | 1081 | |
| 1082 | 1082 | /** |
@@ -1090,15 +1090,15 @@ discard block |
||
| 1090 | 1090 | * Longueur de la chaîne |
| 1091 | 1091 | */ |
| 1092 | 1092 | function spip_strlen($c) { |
| 1093 | - // On transforme les sauts de ligne pour ne pas compter deux caractères |
|
| 1094 | - $c = str_replace("\r\n", "\n", $c); |
|
| 1093 | + // On transforme les sauts de ligne pour ne pas compter deux caractères |
|
| 1094 | + $c = str_replace("\r\n", "\n", $c); |
|
| 1095 | 1095 | |
| 1096 | - // Si ce n'est pas utf-8, utiliser strlen |
|
| 1097 | - if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1098 | - return strlen($c); |
|
| 1099 | - } |
|
| 1096 | + // Si ce n'est pas utf-8, utiliser strlen |
|
| 1097 | + if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1098 | + return strlen($c); |
|
| 1099 | + } |
|
| 1100 | 1100 | |
| 1101 | - return mb_strlen($c); |
|
| 1101 | + return mb_strlen($c); |
|
| 1102 | 1102 | } |
| 1103 | 1103 | |
| 1104 | 1104 | /** |
@@ -1113,17 +1113,17 @@ discard block |
||
| 1113 | 1113 | * en unicode : 💩 |
| 1114 | 1114 | */ |
| 1115 | 1115 | function utf8_noplanes($x): string { |
| 1116 | - $regexp_utf8_4bytes = '/( |
|
| 1116 | + $regexp_utf8_4bytes = '/( |
|
| 1117 | 1117 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 1118 | 1118 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 1119 | 1119 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 1120 | 1120 | )/xS'; |
| 1121 | - if (preg_match_all($regexp_utf8_4bytes, $x, $z, PREG_PATTERN_ORDER)) { |
|
| 1122 | - foreach ($z[0] as $k) { |
|
| 1123 | - $ku = utf_8_to_unicode($k); |
|
| 1124 | - $x = str_replace($k, $ku, $x); |
|
| 1125 | - } |
|
| 1126 | - } |
|
| 1127 | - |
|
| 1128 | - return $x; |
|
| 1121 | + if (preg_match_all($regexp_utf8_4bytes, $x, $z, PREG_PATTERN_ORDER)) { |
|
| 1122 | + foreach ($z[0] as $k) { |
|
| 1123 | + $ku = utf_8_to_unicode($k); |
|
| 1124 | + $x = str_replace($k, $ku, $x); |
|
| 1125 | + } |
|
| 1126 | + } |
|
| 1127 | + |
|
| 1128 | + return $x; |
|
| 1129 | 1129 | } |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | $charset = 'cp1256'; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - if (find_in_path($charset . '.php', 'charsets/', true)) { |
|
| 93 | + if (find_in_path($charset.'.php', 'charsets/', true)) { |
|
| 94 | 94 | return $charset; |
| 95 | 95 | } else { |
| 96 | 96 | spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'"); |
@@ -195,38 +195,38 @@ discard block |
||
| 195 | 195 | |
| 196 | 196 | if (!isset($trans[$charset][$charset_cible])) { |
| 197 | 197 | $trans[$charset][$charset_cible] = [ |
| 198 | - $p . chr(128) => '€', |
|
| 199 | - $p . chr(129) => ' ', # pas affecte |
|
| 200 | - $p . chr(130) => '‚', |
|
| 201 | - $p . chr(131) => 'ƒ', |
|
| 202 | - $p . chr(132) => '„', |
|
| 203 | - $p . chr(133) => '…', |
|
| 204 | - $p . chr(134) => '†', |
|
| 205 | - $p . chr(135) => '‡', |
|
| 206 | - $p . chr(136) => 'ˆ', |
|
| 207 | - $p . chr(137) => '‰', |
|
| 208 | - $p . chr(138) => 'Š', |
|
| 209 | - $p . chr(139) => '‹', |
|
| 210 | - $p . chr(140) => 'Œ', |
|
| 211 | - $p . chr(141) => ' ', # pas affecte |
|
| 212 | - $p . chr(142) => 'Ž', |
|
| 213 | - $p . chr(143) => ' ', # pas affecte |
|
| 214 | - $p . chr(144) => ' ', # pas affecte |
|
| 215 | - $p . chr(145) => '‘', |
|
| 216 | - $p . chr(146) => '’', |
|
| 217 | - $p . chr(147) => '“', |
|
| 218 | - $p . chr(148) => '”', |
|
| 219 | - $p . chr(149) => '•', |
|
| 220 | - $p . chr(150) => '–', |
|
| 221 | - $p . chr(151) => '—', |
|
| 222 | - $p . chr(152) => '˜', |
|
| 223 | - $p . chr(153) => '™', |
|
| 224 | - $p . chr(154) => 'š', |
|
| 225 | - $p . chr(155) => '›', |
|
| 226 | - $p . chr(156) => 'œ', |
|
| 227 | - $p . chr(157) => ' ', # pas affecte |
|
| 228 | - $p . chr(158) => 'ž', |
|
| 229 | - $p . chr(159) => 'Ÿ', |
|
| 198 | + $p.chr(128) => '€', |
|
| 199 | + $p.chr(129) => ' ', # pas affecte |
|
| 200 | + $p.chr(130) => '‚', |
|
| 201 | + $p.chr(131) => 'ƒ', |
|
| 202 | + $p.chr(132) => '„', |
|
| 203 | + $p.chr(133) => '…', |
|
| 204 | + $p.chr(134) => '†', |
|
| 205 | + $p.chr(135) => '‡', |
|
| 206 | + $p.chr(136) => 'ˆ', |
|
| 207 | + $p.chr(137) => '‰', |
|
| 208 | + $p.chr(138) => 'Š', |
|
| 209 | + $p.chr(139) => '‹', |
|
| 210 | + $p.chr(140) => 'Œ', |
|
| 211 | + $p.chr(141) => ' ', # pas affecte |
|
| 212 | + $p.chr(142) => 'Ž', |
|
| 213 | + $p.chr(143) => ' ', # pas affecte |
|
| 214 | + $p.chr(144) => ' ', # pas affecte |
|
| 215 | + $p.chr(145) => '‘', |
|
| 216 | + $p.chr(146) => '’', |
|
| 217 | + $p.chr(147) => '“', |
|
| 218 | + $p.chr(148) => '”', |
|
| 219 | + $p.chr(149) => '•', |
|
| 220 | + $p.chr(150) => '–', |
|
| 221 | + $p.chr(151) => '—', |
|
| 222 | + $p.chr(152) => '˜', |
|
| 223 | + $p.chr(153) => '™', |
|
| 224 | + $p.chr(154) => 'š', |
|
| 225 | + $p.chr(155) => '›', |
|
| 226 | + $p.chr(156) => 'œ', |
|
| 227 | + $p.chr(157) => ' ', # pas affecte |
|
| 228 | + $p.chr(158) => 'ž', |
|
| 229 | + $p.chr(159) => 'Ÿ', |
|
| 230 | 230 | ]; |
| 231 | 231 | if ($charset_cible != 'unicode') { |
| 232 | 232 | foreach ($trans[$charset][$charset_cible] as $k => $c) { |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | and is_array($GLOBALS['CHARSET'][$cset]) |
| 368 | 368 | ) { |
| 369 | 369 | foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
| 370 | - $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 370 | + $trans[$charset][chr($key)] = '&#'.$val.';'; |
|
| 371 | 371 | } |
| 372 | 372 | } |
| 373 | 373 | } |
@@ -433,11 +433,11 @@ discard block |
||
| 433 | 433 | $h = dechex($e); |
| 434 | 434 | if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
| 435 | 435 | $s = $CHARSET_REVERSE[$charset][$e]; |
| 436 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 437 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 436 | + $t['&#'.$e.';'] = $t['�'.$e.';'] = $t['�'.$e.';'] = chr($s); |
|
| 437 | + $t['&#x'.$h.';'] = $t['�'.$h.';'] = $t['�'.$h.';'] = chr($s); |
|
| 438 | 438 | } else { |
| 439 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 440 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 439 | + $t['&#'.$e.';'] = $t['�'.$e.';'] = $t['�'.$e.';'] = chr($e); |
|
| 440 | + $t['&#x'.$h.';'] = $t['�'.$h.';'] = $t['�'.$h.';'] = chr($e); |
|
| 441 | 441 | } |
| 442 | 442 | } |
| 443 | 443 | } |
@@ -484,7 +484,7 @@ discard block |
||
| 484 | 484 | and is_array($GLOBALS['CHARSET'][$cset]) |
| 485 | 485 | ) { |
| 486 | 486 | foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
| 487 | - $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 487 | + $trans[$charset][chr($key)] = unicode2charset('&#'.$val.';'); |
|
| 488 | 488 | } |
| 489 | 489 | } |
| 490 | 490 | } |
@@ -591,7 +591,7 @@ discard block |
||
| 591 | 591 | } |
| 592 | 592 | $thisPos++; |
| 593 | 593 | } |
| 594 | - $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 594 | + $encodedLetter = '&#'.preg_replace('/^0+/', '', $decimalCode).';'; |
|
| 595 | 595 | $encodedString .= $encodedLetter; |
| 596 | 596 | } |
| 597 | 597 | } |
@@ -634,7 +634,7 @@ discard block |
||
| 634 | 634 | } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
| 635 | 635 | else { |
| 636 | 636 | if ($word != 65279) { |
| 637 | - $texte .= '&#' . $word . ';'; |
|
| 637 | + $texte .= '&#'.$word.';'; |
|
| 638 | 638 | } |
| 639 | 639 | } |
| 640 | 640 | } |
@@ -662,13 +662,13 @@ discard block |
||
| 662 | 662 | return chr($num); |
| 663 | 663 | } |
| 664 | 664 | if ($num < 2048) { |
| 665 | - return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 665 | + return chr(($num >> 6) + 192).chr(($num & 63) + 128); |
|
| 666 | 666 | } |
| 667 | 667 | if ($num < 65536) { |
| 668 | - return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 668 | + return chr(($num >> 12) + 224).chr((($num >> 6) & 63) + 128).chr(($num & 63) + 128); |
|
| 669 | 669 | } |
| 670 | 670 | if ($num < 1_114_112) { |
| 671 | - return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 671 | + return chr(($num >> 18) + 240).chr((($num >> 12) & 63) + 128).chr((($num >> 6) & 63) + 128).chr(($num & 63) + 128); |
|
| 672 | 672 | } |
| 673 | 673 | |
| 674 | 674 | return ''; |
@@ -735,7 +735,7 @@ discard block |
||
| 735 | 735 | while (preg_match(',�*([0-9]+);,S', $texte, $regs) and !isset($vu[$regs[1]])) { |
| 736 | 736 | $num = $regs[1]; |
| 737 | 737 | $vu[$num] = true; |
| 738 | - $s = '\u' . sprintf('%04x', $num); |
|
| 738 | + $s = '\u'.sprintf('%04x', $num); |
|
| 739 | 739 | $texte = str_replace($regs[0], $s, $texte); |
| 740 | 740 | } |
| 741 | 741 | |
@@ -752,7 +752,7 @@ discard block |
||
| 752 | 752 | **/ |
| 753 | 753 | function javascript_to_unicode($texte) { |
| 754 | 754 | while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
| 755 | - $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 755 | + $texte = str_replace($regs[0], '&#'.hexdec($regs[1]).';', $texte); |
|
| 756 | 756 | } |
| 757 | 757 | |
| 758 | 758 | return $texte; |
@@ -797,7 +797,7 @@ discard block |
||
| 797 | 797 | return $texte; |
| 798 | 798 | } |
| 799 | 799 | |
| 800 | - $table_translit = 'translit' . $complexe; |
|
| 800 | + $table_translit = 'translit'.$complexe; |
|
| 801 | 801 | |
| 802 | 802 | // 2. Translitterer grace a la table predefinie |
| 803 | 803 | if (!isset($trans[$complexe])) { |
@@ -888,7 +888,7 @@ discard block |
||
| 888 | 888 | * true s'il a un BOM |
| 889 | 889 | **/ |
| 890 | 890 | function bom_utf8($texte): bool { |
| 891 | - return (substr($texte, 0, 3) === chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 891 | + return (substr($texte, 0, 3) === chr(0xEF).chr(0xBB).chr(0xBF)); |
|
| 892 | 892 | } |
| 893 | 893 | |
| 894 | 894 | /** |
@@ -1058,7 +1058,7 @@ discard block |
||
| 1058 | 1058 | |
| 1059 | 1059 | $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
| 1060 | 1060 | |
| 1061 | - return $lettre1 . spip_substr($c, 1); |
|
| 1061 | + return $lettre1.spip_substr($c, 1); |
|
| 1062 | 1062 | } |
| 1063 | 1063 | |
| 1064 | 1064 | /** |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | $res = admin_repair_tables(); |
| 40 | 40 | if (!$res) { |
| 41 | - $res = "<div class='error'>" . _T('avis_erreur_mysql') . ' ' . sql_errno() . ': ' . sql_error() . "</div>\n"; |
|
| 41 | + $res = "<div class='error'>"._T('avis_erreur_mysql').' '.sql_errno().': '.sql_error()."</div>\n"; |
|
| 42 | 42 | } else { |
| 43 | 43 | include_spip('inc/rubriques'); |
| 44 | 44 | calculer_rubriques(); |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | $res .= pipeline('base_admin_repair', $res); |
| 49 | 49 | echo minipres( |
| 50 | 50 | _T('texte_tentative_recuperation'), |
| 51 | - $res . generer_form_ecrire('accueil', '', '', _T('public:accueil_site')) |
|
| 51 | + $res.generer_form_ecrire('accueil', '', '', _T('public:accueil_site')) |
|
| 52 | 52 | ); |
| 53 | 53 | } |
| 54 | 54 | |
@@ -91,12 +91,12 @@ discard block |
||
| 91 | 91 | $count = sql_countsel($tab); |
| 92 | 92 | |
| 93 | 93 | if ($count > 1) { |
| 94 | - $m .= '(' . _T('texte_compte_elements', ['count' => $count]) . ")\n"; |
|
| 94 | + $m .= '('._T('texte_compte_elements', ['count' => $count]).")\n"; |
|
| 95 | 95 | } else { |
| 96 | 96 | if ($count == 1) { |
| 97 | - $m .= '(' . _T('texte_compte_element', ['count' => $count]) . ")\n"; |
|
| 97 | + $m .= '('._T('texte_compte_element', ['count' => $count]).")\n"; |
|
| 98 | 98 | } else { |
| 99 | - $m .= '(' . _T('texte_vide') . ")\n"; |
|
| 99 | + $m .= '('._T('texte_vide').")\n"; |
|
| 100 | 100 | } |
| 101 | 101 | } |
| 102 | 102 | |
@@ -105,13 +105,13 @@ discard block |
||
| 105 | 105 | and $msg = join( |
| 106 | 106 | ' ', |
| 107 | 107 | (is_resource($result_repair) or is_object($result_repair)) ? sql_fetch($result_repair) : $result_repair |
| 108 | - ) . ' ' |
|
| 108 | + ).' ' |
|
| 109 | 109 | and !str_contains($msg, ' OK ') |
| 110 | 110 | ) { |
| 111 | 111 | $class = " class='notice'"; |
| 112 | - $m .= '<br /><tt>' . spip_htmlentities($msg) . "</tt>\n"; |
|
| 112 | + $m .= '<br /><tt>'.spip_htmlentities($msg)."</tt>\n"; |
|
| 113 | 113 | } else { |
| 114 | - $m .= ' ' . _T('texte_table_ok'); |
|
| 114 | + $m .= ' '._T('texte_table_ok'); |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | $res .= "<div$class>$m</div>"; |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -36,20 +36,20 @@ discard block |
||
| 36 | 36 | **/ |
| 37 | 37 | function base_repair_dist($titre = '', $reprise = '') { |
| 38 | 38 | |
| 39 | - $res = admin_repair_tables(); |
|
| 40 | - if (!$res) { |
|
| 41 | - $res = "<div class='error'>" . _T('avis_erreur_mysql') . ' ' . sql_errno() . ': ' . sql_error() . "</div>\n"; |
|
| 42 | - } else { |
|
| 43 | - include_spip('inc/rubriques'); |
|
| 44 | - calculer_rubriques(); |
|
| 45 | - propager_les_secteurs(); |
|
| 46 | - } |
|
| 47 | - include_spip('inc/minipres'); |
|
| 48 | - $res .= pipeline('base_admin_repair', $res); |
|
| 49 | - echo minipres( |
|
| 50 | - _T('texte_tentative_recuperation'), |
|
| 51 | - $res . generer_form_ecrire('accueil', '', '', _T('public:accueil_site')) |
|
| 52 | - ); |
|
| 39 | + $res = admin_repair_tables(); |
|
| 40 | + if (!$res) { |
|
| 41 | + $res = "<div class='error'>" . _T('avis_erreur_mysql') . ' ' . sql_errno() . ': ' . sql_error() . "</div>\n"; |
|
| 42 | + } else { |
|
| 43 | + include_spip('inc/rubriques'); |
|
| 44 | + calculer_rubriques(); |
|
| 45 | + propager_les_secteurs(); |
|
| 46 | + } |
|
| 47 | + include_spip('inc/minipres'); |
|
| 48 | + $res .= pipeline('base_admin_repair', $res); |
|
| 49 | + echo minipres( |
|
| 50 | + _T('texte_tentative_recuperation'), |
|
| 51 | + $res . generer_form_ecrire('accueil', '', '', _T('public:accueil_site')) |
|
| 52 | + ); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
@@ -63,60 +63,60 @@ discard block |
||
| 63 | 63 | **/ |
| 64 | 64 | function admin_repair_tables() { |
| 65 | 65 | |
| 66 | - $repair = sql_serveur('repair', '', true); |
|
| 66 | + $repair = sql_serveur('repair', '', true); |
|
| 67 | 67 | |
| 68 | - // recreer les tables manquantes eventuelles |
|
| 69 | - include_spip('base/create'); |
|
| 70 | - creer_base(); |
|
| 71 | - $tables = sql_alltable(); |
|
| 68 | + // recreer les tables manquantes eventuelles |
|
| 69 | + include_spip('base/create'); |
|
| 70 | + creer_base(); |
|
| 71 | + $tables = sql_alltable(); |
|
| 72 | 72 | |
| 73 | - $res = ''; |
|
| 74 | - foreach ($tables as $tab) { |
|
| 75 | - $class = ''; |
|
| 76 | - $m = "<strong>$tab</strong> "; |
|
| 77 | - spip_log("Repare $tab", _LOG_INFO_IMPORTANTE); |
|
| 78 | - // supprimer la meta avant de lancer la reparation |
|
| 79 | - // car le repair peut etre long ; on ne veut pas boucler |
|
| 80 | - effacer_meta('admin_repair'); |
|
| 81 | - if ($repair) { |
|
| 82 | - $result_repair = sql_repair($tab); |
|
| 83 | - if (!$result_repair) { |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - } |
|
| 73 | + $res = ''; |
|
| 74 | + foreach ($tables as $tab) { |
|
| 75 | + $class = ''; |
|
| 76 | + $m = "<strong>$tab</strong> "; |
|
| 77 | + spip_log("Repare $tab", _LOG_INFO_IMPORTANTE); |
|
| 78 | + // supprimer la meta avant de lancer la reparation |
|
| 79 | + // car le repair peut etre long ; on ne veut pas boucler |
|
| 80 | + effacer_meta('admin_repair'); |
|
| 81 | + if ($repair) { |
|
| 82 | + $result_repair = sql_repair($tab); |
|
| 83 | + if (!$result_repair) { |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - // essayer de maj la table (creation de champs manquants) |
|
| 89 | - maj_tables($tab); |
|
| 88 | + // essayer de maj la table (creation de champs manquants) |
|
| 89 | + maj_tables($tab); |
|
| 90 | 90 | |
| 91 | - $count = sql_countsel($tab); |
|
| 91 | + $count = sql_countsel($tab); |
|
| 92 | 92 | |
| 93 | - if ($count > 1) { |
|
| 94 | - $m .= '(' . _T('texte_compte_elements', ['count' => $count]) . ")\n"; |
|
| 95 | - } else { |
|
| 96 | - if ($count == 1) { |
|
| 97 | - $m .= '(' . _T('texte_compte_element', ['count' => $count]) . ")\n"; |
|
| 98 | - } else { |
|
| 99 | - $m .= '(' . _T('texte_vide') . ")\n"; |
|
| 100 | - } |
|
| 101 | - } |
|
| 93 | + if ($count > 1) { |
|
| 94 | + $m .= '(' . _T('texte_compte_elements', ['count' => $count]) . ")\n"; |
|
| 95 | + } else { |
|
| 96 | + if ($count == 1) { |
|
| 97 | + $m .= '(' . _T('texte_compte_element', ['count' => $count]) . ")\n"; |
|
| 98 | + } else { |
|
| 99 | + $m .= '(' . _T('texte_vide') . ")\n"; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - if ( |
|
| 104 | - $repair |
|
| 105 | - and $result_repair |
|
| 106 | - and $msg = join( |
|
| 107 | - ' ', |
|
| 108 | - (is_resource($result_repair) or is_object($result_repair)) ? sql_fetch($result_repair) : $result_repair |
|
| 109 | - ) . ' ' |
|
| 110 | - and !str_contains($msg, ' OK ') |
|
| 111 | - ) { |
|
| 112 | - $class = " class='notice'"; |
|
| 113 | - $m .= '<br /><tt>' . spip_htmlentities($msg) . "</tt>\n"; |
|
| 114 | - } else { |
|
| 115 | - $m .= ' ' . _T('texte_table_ok'); |
|
| 116 | - } |
|
| 103 | + if ( |
|
| 104 | + $repair |
|
| 105 | + and $result_repair |
|
| 106 | + and $msg = join( |
|
| 107 | + ' ', |
|
| 108 | + (is_resource($result_repair) or is_object($result_repair)) ? sql_fetch($result_repair) : $result_repair |
|
| 109 | + ) . ' ' |
|
| 110 | + and !str_contains($msg, ' OK ') |
|
| 111 | + ) { |
|
| 112 | + $class = " class='notice'"; |
|
| 113 | + $m .= '<br /><tt>' . spip_htmlentities($msg) . "</tt>\n"; |
|
| 114 | + } else { |
|
| 115 | + $m .= ' ' . _T('texte_table_ok'); |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - $res .= "<div$class>$m</div>"; |
|
| 119 | - } |
|
| 118 | + $res .= "<div$class>$m</div>"; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - return $res; |
|
| 121 | + return $res; |
|
| 122 | 122 | } |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | \***************************************************************************/ |
| 12 | 12 | |
| 13 | 13 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 14 | - return; |
|
| 14 | + return; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | include_spip('inc/charsets'); |
@@ -24,222 +24,222 @@ discard block |
||
| 24 | 24 | * @return string |
| 25 | 25 | */ |
| 26 | 26 | function xml_entites_html($texte) { |
| 27 | - if ( |
|
| 28 | - !is_string($texte) or !$texte |
|
| 29 | - or strpbrk($texte, "&\"'<>") == false |
|
| 30 | - ) { |
|
| 31 | - return $texte; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - if (!function_exists('spip_htmlspecialchars')) { |
|
| 35 | - include_spip('inc/filtres_mini'); |
|
| 36 | - } |
|
| 37 | - $texte = spip_htmlspecialchars($texte, ENT_QUOTES); |
|
| 38 | - |
|
| 39 | - return $texte; |
|
| 27 | + if ( |
|
| 28 | + !is_string($texte) or !$texte |
|
| 29 | + or strpbrk($texte, "&\"'<>") == false |
|
| 30 | + ) { |
|
| 31 | + return $texte; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + if (!function_exists('spip_htmlspecialchars')) { |
|
| 35 | + include_spip('inc/filtres_mini'); |
|
| 36 | + } |
|
| 37 | + $texte = spip_htmlspecialchars($texte, ENT_QUOTES); |
|
| 38 | + |
|
| 39 | + return $texte; |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | function xml_debutElement($phraseur, $name, $attrs) { |
| 43 | - $depth = $phraseur->depth; |
|
| 44 | - |
|
| 45 | - $t = $phraseur->ouvrant[$depth] ?? ' '; |
|
| 46 | - // espace initial signifie: deja integree au resultat |
|
| 47 | - if ($t[0] != ' ') { |
|
| 48 | - $phraseur->res .= '<' . $t . '>'; |
|
| 49 | - $phraseur->ouvrant[$depth] = ' ' . $t; |
|
| 50 | - } |
|
| 51 | - $t = $phraseur->contenu[$depth]; |
|
| 52 | - // n'indenter que s'il y a un separateur avant |
|
| 53 | - $phraseur->res .= preg_replace("/[\n\t ]+$/", "\n$depth", $t); |
|
| 54 | - $phraseur->contenu[$depth] = ''; |
|
| 55 | - $att = ''; |
|
| 56 | - $sep = ' '; |
|
| 57 | - foreach ($attrs as $k => $v) { |
|
| 58 | - $delim = !str_contains($v, "'") ? "'" : '"'; |
|
| 59 | - $val = xml_entites_html($v); |
|
| 60 | - $att .= $sep . $k . '=' . $delim |
|
| 61 | - . ($delim !== '"' ? str_replace('"', '"', $val) : $val) |
|
| 62 | - . $delim; |
|
| 63 | - $sep = "\n $depth"; |
|
| 64 | - } |
|
| 65 | - $phraseur->depth .= ' '; |
|
| 66 | - $phraseur->contenu[$phraseur->depth] = ''; |
|
| 67 | - $phraseur->ouvrant[$phraseur->depth] = $name . $att; |
|
| 68 | - $phraseur->reperes[$phraseur->depth] = xml_get_current_line_number($phraseur->sax); |
|
| 43 | + $depth = $phraseur->depth; |
|
| 44 | + |
|
| 45 | + $t = $phraseur->ouvrant[$depth] ?? ' '; |
|
| 46 | + // espace initial signifie: deja integree au resultat |
|
| 47 | + if ($t[0] != ' ') { |
|
| 48 | + $phraseur->res .= '<' . $t . '>'; |
|
| 49 | + $phraseur->ouvrant[$depth] = ' ' . $t; |
|
| 50 | + } |
|
| 51 | + $t = $phraseur->contenu[$depth]; |
|
| 52 | + // n'indenter que s'il y a un separateur avant |
|
| 53 | + $phraseur->res .= preg_replace("/[\n\t ]+$/", "\n$depth", $t); |
|
| 54 | + $phraseur->contenu[$depth] = ''; |
|
| 55 | + $att = ''; |
|
| 56 | + $sep = ' '; |
|
| 57 | + foreach ($attrs as $k => $v) { |
|
| 58 | + $delim = !str_contains($v, "'") ? "'" : '"'; |
|
| 59 | + $val = xml_entites_html($v); |
|
| 60 | + $att .= $sep . $k . '=' . $delim |
|
| 61 | + . ($delim !== '"' ? str_replace('"', '"', $val) : $val) |
|
| 62 | + . $delim; |
|
| 63 | + $sep = "\n $depth"; |
|
| 64 | + } |
|
| 65 | + $phraseur->depth .= ' '; |
|
| 66 | + $phraseur->contenu[$phraseur->depth] = ''; |
|
| 67 | + $phraseur->ouvrant[$phraseur->depth] = $name . $att; |
|
| 68 | + $phraseur->reperes[$phraseur->depth] = xml_get_current_line_number($phraseur->sax); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | function xml_finElement($phraseur, $name, $fusion_bal = false) { |
| 72 | - $ouv = $phraseur->ouvrant[$phraseur->depth]; |
|
| 73 | - |
|
| 74 | - if ($ouv[0] != ' ') { |
|
| 75 | - $phraseur->ouvrant[$phraseur->depth] = ' ' . $ouv; |
|
| 76 | - } else { |
|
| 77 | - $ouv = ''; |
|
| 78 | - } |
|
| 79 | - $t = $phraseur->contenu[$phraseur->depth]; |
|
| 80 | - $phraseur->depth = substr($phraseur->depth, 2); |
|
| 81 | - $t = preg_replace("/[\n\t ]+$/", "\n" . $phraseur->depth, $t); |
|
| 82 | - |
|
| 83 | - // fusion <balise></balise> en <balise />. |
|
| 84 | - // ATTENTION, certains clients http croient que fusion ==> pas d'atttributs |
|
| 85 | - // en particulier pour les balises Script et A. |
|
| 86 | - // en presence d'attributs ne le faire que si la DTD est dispo et d'accord |
|
| 87 | - // (param fusion_bal) |
|
| 88 | - |
|
| 89 | - if ($t || (($ouv != $name) and !$fusion_bal)) { |
|
| 90 | - $phraseur->res .= ($ouv ? ('<' . $ouv . '>') : '') . $t . '</' . $name . '>'; |
|
| 91 | - } else { |
|
| 92 | - $phraseur->res .= ($ouv ? ('<' . $ouv . ' />') : ('</' . $name . '>')); |
|
| 93 | - } |
|
| 72 | + $ouv = $phraseur->ouvrant[$phraseur->depth]; |
|
| 73 | + |
|
| 74 | + if ($ouv[0] != ' ') { |
|
| 75 | + $phraseur->ouvrant[$phraseur->depth] = ' ' . $ouv; |
|
| 76 | + } else { |
|
| 77 | + $ouv = ''; |
|
| 78 | + } |
|
| 79 | + $t = $phraseur->contenu[$phraseur->depth]; |
|
| 80 | + $phraseur->depth = substr($phraseur->depth, 2); |
|
| 81 | + $t = preg_replace("/[\n\t ]+$/", "\n" . $phraseur->depth, $t); |
|
| 82 | + |
|
| 83 | + // fusion <balise></balise> en <balise />. |
|
| 84 | + // ATTENTION, certains clients http croient que fusion ==> pas d'atttributs |
|
| 85 | + // en particulier pour les balises Script et A. |
|
| 86 | + // en presence d'attributs ne le faire que si la DTD est dispo et d'accord |
|
| 87 | + // (param fusion_bal) |
|
| 88 | + |
|
| 89 | + if ($t || (($ouv != $name) and !$fusion_bal)) { |
|
| 90 | + $phraseur->res .= ($ouv ? ('<' . $ouv . '>') : '') . $t . '</' . $name . '>'; |
|
| 91 | + } else { |
|
| 92 | + $phraseur->res .= ($ouv ? ('<' . $ouv . ' />') : ('</' . $name . '>')); |
|
| 93 | + } |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | function xml_textElement($phraseur, $data) { |
| 97 | - $depth = $phraseur->depth; |
|
| 98 | - $phraseur->contenu[$depth] .= preg_match('/^script/', $phraseur->ouvrant[$depth]) |
|
| 99 | - ? $data |
|
| 100 | - : xml_entites_html($data); |
|
| 97 | + $depth = $phraseur->depth; |
|
| 98 | + $phraseur->contenu[$depth] .= preg_match('/^script/', $phraseur->ouvrant[$depth]) |
|
| 99 | + ? $data |
|
| 100 | + : xml_entites_html($data); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | function xml_piElement($phraseur, $target, $data) { |
| 104 | - $depth = $phraseur->depth; |
|
| 105 | - |
|
| 106 | - if (strtolower($target) != 'php') { |
|
| 107 | - $phraseur->contenu[$depth] .= $data; |
|
| 108 | - } else { |
|
| 109 | - ob_start(); |
|
| 110 | - eval($data); |
|
| 111 | - $data = ob_get_contents(); |
|
| 112 | - ob_end_clean(); |
|
| 113 | - $phraseur->contenu[$depth] .= $data; |
|
| 114 | - } |
|
| 104 | + $depth = $phraseur->depth; |
|
| 105 | + |
|
| 106 | + if (strtolower($target) != 'php') { |
|
| 107 | + $phraseur->contenu[$depth] .= $data; |
|
| 108 | + } else { |
|
| 109 | + ob_start(); |
|
| 110 | + eval($data); |
|
| 111 | + $data = ob_get_contents(); |
|
| 112 | + ob_end_clean(); |
|
| 113 | + $phraseur->contenu[$depth] .= $data; |
|
| 114 | + } |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | |
| 118 | 118 | function xml_defaultElement($phraseur, $data) { |
| 119 | - $depth = $phraseur->depth; |
|
| 119 | + $depth = $phraseur->depth; |
|
| 120 | 120 | |
| 121 | - if (!isset($phraseur->contenu[$depth])) { |
|
| 122 | - $phraseur->contenu[$depth] = ''; |
|
| 123 | - } |
|
| 124 | - $phraseur->contenu[$depth] .= $data; |
|
| 121 | + if (!isset($phraseur->contenu[$depth])) { |
|
| 122 | + $phraseur->contenu[$depth] = ''; |
|
| 123 | + } |
|
| 124 | + $phraseur->contenu[$depth] .= $data; |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | function xml_parsestring($phraseur, $data) { |
| 128 | - $phraseur->contenu[$phraseur->depth] = ''; |
|
| 129 | - |
|
| 130 | - if (!xml_parse($phraseur->sax, $data, true)) { |
|
| 131 | - coordonnees_erreur( |
|
| 132 | - $phraseur, |
|
| 133 | - xml_error_string(xml_get_error_code($phraseur->sax)) |
|
| 134 | - . "<br />\n" . |
|
| 135 | - (!$phraseur->depth ? '' : |
|
| 136 | - ('(' . |
|
| 137 | - _T('erreur_balise_non_fermee') . |
|
| 138 | - ' <tt>' . |
|
| 139 | - $phraseur->ouvrant[$phraseur->depth] . |
|
| 140 | - '</tt> ' . |
|
| 141 | - _T('ligne') . |
|
| 142 | - ' ' . |
|
| 143 | - $phraseur->reperes[$phraseur->depth] . |
|
| 144 | - ") <br />\n")) |
|
| 145 | - ); |
|
| 146 | - } |
|
| 128 | + $phraseur->contenu[$phraseur->depth] = ''; |
|
| 129 | + |
|
| 130 | + if (!xml_parse($phraseur->sax, $data, true)) { |
|
| 131 | + coordonnees_erreur( |
|
| 132 | + $phraseur, |
|
| 133 | + xml_error_string(xml_get_error_code($phraseur->sax)) |
|
| 134 | + . "<br />\n" . |
|
| 135 | + (!$phraseur->depth ? '' : |
|
| 136 | + ('(' . |
|
| 137 | + _T('erreur_balise_non_fermee') . |
|
| 138 | + ' <tt>' . |
|
| 139 | + $phraseur->ouvrant[$phraseur->depth] . |
|
| 140 | + '</tt> ' . |
|
| 141 | + _T('ligne') . |
|
| 142 | + ' ' . |
|
| 143 | + $phraseur->reperes[$phraseur->depth] . |
|
| 144 | + ") <br />\n")) |
|
| 145 | + ); |
|
| 146 | + } |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | function coordonnees_erreur($phraseur, $msg) { |
| 150 | - $entete_length = substr_count($phraseur->entete, "\n"); |
|
| 151 | - $phraseur->err[] = [ |
|
| 152 | - $msg, |
|
| 153 | - xml_get_current_line_number($phraseur->sax) + $entete_length, |
|
| 154 | - xml_get_current_column_number($phraseur->sax) |
|
| 155 | - ]; |
|
| 150 | + $entete_length = substr_count($phraseur->entete, "\n"); |
|
| 151 | + $phraseur->err[] = [ |
|
| 152 | + $msg, |
|
| 153 | + xml_get_current_line_number($phraseur->sax) + $entete_length, |
|
| 154 | + xml_get_current_column_number($phraseur->sax) |
|
| 155 | + ]; |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | function xml_sax_dist($page, $apply = false, $phraseur = null, $doctype = '', $charset = null) { |
| 159 | - if (is_null($charset)) { |
|
| 160 | - $charset = $GLOBALS['meta']['charset']; |
|
| 161 | - } |
|
| 162 | - if ($apply) { |
|
| 163 | - ob_start(); |
|
| 164 | - if (is_array($apply)) { |
|
| 165 | - $r = $page(...$apply); |
|
| 166 | - } else { |
|
| 167 | - $r = $page(); |
|
| 168 | - } |
|
| 169 | - $page = ob_get_contents(); |
|
| 170 | - ob_end_clean(); |
|
| 171 | - // fonction sans aucun "echo", ca doit etre le resultat |
|
| 172 | - if (!$page) { |
|
| 173 | - $page = $r; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if (!$page) { |
|
| 178 | - return ''; |
|
| 179 | - } |
|
| 180 | - // charger la DTD et transcoder les entites, |
|
| 181 | - // et escamoter le doctype que sax mange en php5 mais pas en php4 |
|
| 182 | - if (!$doctype) { |
|
| 183 | - if (!$r = analyser_doctype($page)) { |
|
| 184 | - $page = _MESSAGE_DOCTYPE . _DOCTYPE_ECRIRE |
|
| 185 | - . preg_replace(_REGEXP_DOCTYPE, '', $page); |
|
| 186 | - $r = analyser_doctype($page); |
|
| 187 | - } |
|
| 188 | - [$entete, $avail, $grammaire, $rotlvl] = array_pad($r, 4, null); |
|
| 189 | - $page = substr($page, strlen($entete)); |
|
| 190 | - } else { |
|
| 191 | - $avail = 'SYSTEM'; |
|
| 192 | - $grammaire = $doctype; |
|
| 193 | - $rotlvl = basename($grammaire); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - include_spip('xml/analyser_dtd'); |
|
| 197 | - $dtc = charger_dtd($grammaire, $avail, $rotlvl); |
|
| 198 | - $page = sax_bug($page, $dtc, $charset); |
|
| 199 | - |
|
| 200 | - // compatibilite Tidy espace public |
|
| 201 | - if (!$phraseur) { |
|
| 202 | - $indenter_xml = charger_fonction('indenter', 'xml'); |
|
| 203 | - |
|
| 204 | - return $indenter_xml($page, $apply); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - $xml_parser = xml_parser_create($charset); |
|
| 208 | - |
|
| 209 | - xml_set_element_handler( |
|
| 210 | - $xml_parser, |
|
| 211 | - [$phraseur, 'debutElement'], |
|
| 212 | - [$phraseur, 'finElement'] |
|
| 213 | - ); |
|
| 214 | - |
|
| 215 | - xml_set_character_data_handler( |
|
| 216 | - $xml_parser, |
|
| 217 | - [$phraseur, 'textElement'] |
|
| 218 | - ); |
|
| 219 | - |
|
| 220 | - xml_set_processing_instruction_handler( |
|
| 221 | - $xml_parser, |
|
| 222 | - [$phraseur, 'piElement'] |
|
| 223 | - ); |
|
| 224 | - |
|
| 225 | - xml_set_default_handler( |
|
| 226 | - $xml_parser, |
|
| 227 | - [$phraseur, 'defaultElement'] |
|
| 228 | - ); |
|
| 229 | - |
|
| 230 | - xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); |
|
| 231 | - |
|
| 232 | - $phraseur->sax = $xml_parser; |
|
| 233 | - if (isset($entete)) { |
|
| 234 | - $phraseur->entete = $entete; |
|
| 235 | - } |
|
| 236 | - $phraseur->page = $page; |
|
| 237 | - $phraseur->dtc = $dtc; |
|
| 238 | - $phraseur->phraserTout($xml_parser, $page); |
|
| 239 | - xml_parser_free($xml_parser); |
|
| 240 | - $phraseur->sax = ''; |
|
| 241 | - |
|
| 242 | - return $phraseur; |
|
| 159 | + if (is_null($charset)) { |
|
| 160 | + $charset = $GLOBALS['meta']['charset']; |
|
| 161 | + } |
|
| 162 | + if ($apply) { |
|
| 163 | + ob_start(); |
|
| 164 | + if (is_array($apply)) { |
|
| 165 | + $r = $page(...$apply); |
|
| 166 | + } else { |
|
| 167 | + $r = $page(); |
|
| 168 | + } |
|
| 169 | + $page = ob_get_contents(); |
|
| 170 | + ob_end_clean(); |
|
| 171 | + // fonction sans aucun "echo", ca doit etre le resultat |
|
| 172 | + if (!$page) { |
|
| 173 | + $page = $r; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if (!$page) { |
|
| 178 | + return ''; |
|
| 179 | + } |
|
| 180 | + // charger la DTD et transcoder les entites, |
|
| 181 | + // et escamoter le doctype que sax mange en php5 mais pas en php4 |
|
| 182 | + if (!$doctype) { |
|
| 183 | + if (!$r = analyser_doctype($page)) { |
|
| 184 | + $page = _MESSAGE_DOCTYPE . _DOCTYPE_ECRIRE |
|
| 185 | + . preg_replace(_REGEXP_DOCTYPE, '', $page); |
|
| 186 | + $r = analyser_doctype($page); |
|
| 187 | + } |
|
| 188 | + [$entete, $avail, $grammaire, $rotlvl] = array_pad($r, 4, null); |
|
| 189 | + $page = substr($page, strlen($entete)); |
|
| 190 | + } else { |
|
| 191 | + $avail = 'SYSTEM'; |
|
| 192 | + $grammaire = $doctype; |
|
| 193 | + $rotlvl = basename($grammaire); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + include_spip('xml/analyser_dtd'); |
|
| 197 | + $dtc = charger_dtd($grammaire, $avail, $rotlvl); |
|
| 198 | + $page = sax_bug($page, $dtc, $charset); |
|
| 199 | + |
|
| 200 | + // compatibilite Tidy espace public |
|
| 201 | + if (!$phraseur) { |
|
| 202 | + $indenter_xml = charger_fonction('indenter', 'xml'); |
|
| 203 | + |
|
| 204 | + return $indenter_xml($page, $apply); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + $xml_parser = xml_parser_create($charset); |
|
| 208 | + |
|
| 209 | + xml_set_element_handler( |
|
| 210 | + $xml_parser, |
|
| 211 | + [$phraseur, 'debutElement'], |
|
| 212 | + [$phraseur, 'finElement'] |
|
| 213 | + ); |
|
| 214 | + |
|
| 215 | + xml_set_character_data_handler( |
|
| 216 | + $xml_parser, |
|
| 217 | + [$phraseur, 'textElement'] |
|
| 218 | + ); |
|
| 219 | + |
|
| 220 | + xml_set_processing_instruction_handler( |
|
| 221 | + $xml_parser, |
|
| 222 | + [$phraseur, 'piElement'] |
|
| 223 | + ); |
|
| 224 | + |
|
| 225 | + xml_set_default_handler( |
|
| 226 | + $xml_parser, |
|
| 227 | + [$phraseur, 'defaultElement'] |
|
| 228 | + ); |
|
| 229 | + |
|
| 230 | + xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); |
|
| 231 | + |
|
| 232 | + $phraseur->sax = $xml_parser; |
|
| 233 | + if (isset($entete)) { |
|
| 234 | + $phraseur->entete = $entete; |
|
| 235 | + } |
|
| 236 | + $phraseur->page = $page; |
|
| 237 | + $phraseur->dtc = $dtc; |
|
| 238 | + $phraseur->phraserTout($xml_parser, $page); |
|
| 239 | + xml_parser_free($xml_parser); |
|
| 240 | + $phraseur->sax = ''; |
|
| 241 | + |
|
| 242 | + return $phraseur; |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | // SAX ne dit pas si une Entite est dans un attribut ou non. |
@@ -250,24 +250,24 @@ discard block |
||
| 250 | 250 | // sinon on se rabat sur ce qu'en connait SPIP en standard. |
| 251 | 251 | |
| 252 | 252 | function sax_bug($data, $dtc, $charset = null) { |
| 253 | - if (is_null($charset)) { |
|
| 254 | - $charset = $GLOBALS['meta']['charset']; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - if ($dtc) { |
|
| 258 | - $trans = []; |
|
| 259 | - |
|
| 260 | - foreach ($dtc->entites as $k => $v) { |
|
| 261 | - if (!strpos(' amp lt gt quot ', (string) $k)) { |
|
| 262 | - $trans["&$k;"] = $v; |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - $data = strtr($data, $trans); |
|
| 266 | - } else { |
|
| 267 | - $data = html2unicode($data, true); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - return unicode2charset($data, $charset); |
|
| 253 | + if (is_null($charset)) { |
|
| 254 | + $charset = $GLOBALS['meta']['charset']; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + if ($dtc) { |
|
| 258 | + $trans = []; |
|
| 259 | + |
|
| 260 | + foreach ($dtc->entites as $k => $v) { |
|
| 261 | + if (!strpos(' amp lt gt quot ', (string) $k)) { |
|
| 262 | + $trans["&$k;"] = $v; |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + $data = strtr($data, $trans); |
|
| 266 | + } else { |
|
| 267 | + $data = html2unicode($data, true); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + return unicode2charset($data, $charset); |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | // Retirer < ? xml... ? > et autre PI, ainsi que les commentaires en debut |
@@ -277,52 +277,52 @@ discard block |
||
| 277 | 277 | // les autres formats RSS n'ont pas de DTD, |
| 278 | 278 | // mais un XML Schema que SPIP ne fait pas encore lire. |
| 279 | 279 | function analyser_doctype($data) { |
| 280 | - if (!preg_match(_REGEXP_DOCTYPE, $data, $page)) { |
|
| 281 | - if (preg_match(_REGEXP_XML, $data, $page)) { |
|
| 282 | - [, $entete, $topelement] = $page; |
|
| 283 | - if ($topelement == 'rss') { |
|
| 284 | - return [ |
|
| 285 | - $entete, |
|
| 286 | - 'PUBLIC', |
|
| 287 | - _DOCTYPE_RSS, |
|
| 288 | - 'rss-0.91.dtd' |
|
| 289 | - ]; |
|
| 290 | - } else { |
|
| 291 | - $dtd = $topelement . '.dtd'; |
|
| 292 | - $f = find_in_path($dtd); |
|
| 293 | - if (file_exists($f)) { |
|
| 294 | - return [$entete, 'SYSTEM', $f, $dtd]; |
|
| 295 | - } |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - spip_log('Dtd pas vu pour ' . substr($data, 0, 100)); |
|
| 299 | - |
|
| 300 | - return []; |
|
| 301 | - } |
|
| 302 | - [$entete, , $topelement, $avail, $suite] = $page; |
|
| 303 | - |
|
| 304 | - if (!preg_match('/^"([^"]*)"\s*(.*)$/', $suite, $r)) { |
|
| 305 | - if (!preg_match("/^'([^']*)'\s*(.*)$/", $suite, $r)) { |
|
| 306 | - return []; |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - [, $rotlvl, $suite] = $r; |
|
| 310 | - |
|
| 311 | - if (!$suite) { |
|
| 312 | - if ($avail != 'SYSTEM') { |
|
| 313 | - return []; |
|
| 314 | - } |
|
| 315 | - $grammaire = $rotlvl; |
|
| 316 | - $rotlvl = ''; |
|
| 317 | - } else { |
|
| 318 | - if (!preg_match('/^"([^"]*)"\s*$/', $suite, $r)) { |
|
| 319 | - if (!preg_match("/^'([^']*)'\s*$/", $suite, $r)) { |
|
| 320 | - return []; |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $grammaire = $r[1]; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - return [$entete, $avail, $grammaire, $rotlvl]; |
|
| 280 | + if (!preg_match(_REGEXP_DOCTYPE, $data, $page)) { |
|
| 281 | + if (preg_match(_REGEXP_XML, $data, $page)) { |
|
| 282 | + [, $entete, $topelement] = $page; |
|
| 283 | + if ($topelement == 'rss') { |
|
| 284 | + return [ |
|
| 285 | + $entete, |
|
| 286 | + 'PUBLIC', |
|
| 287 | + _DOCTYPE_RSS, |
|
| 288 | + 'rss-0.91.dtd' |
|
| 289 | + ]; |
|
| 290 | + } else { |
|
| 291 | + $dtd = $topelement . '.dtd'; |
|
| 292 | + $f = find_in_path($dtd); |
|
| 293 | + if (file_exists($f)) { |
|
| 294 | + return [$entete, 'SYSTEM', $f, $dtd]; |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + spip_log('Dtd pas vu pour ' . substr($data, 0, 100)); |
|
| 299 | + |
|
| 300 | + return []; |
|
| 301 | + } |
|
| 302 | + [$entete, , $topelement, $avail, $suite] = $page; |
|
| 303 | + |
|
| 304 | + if (!preg_match('/^"([^"]*)"\s*(.*)$/', $suite, $r)) { |
|
| 305 | + if (!preg_match("/^'([^']*)'\s*(.*)$/", $suite, $r)) { |
|
| 306 | + return []; |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + [, $rotlvl, $suite] = $r; |
|
| 310 | + |
|
| 311 | + if (!$suite) { |
|
| 312 | + if ($avail != 'SYSTEM') { |
|
| 313 | + return []; |
|
| 314 | + } |
|
| 315 | + $grammaire = $rotlvl; |
|
| 316 | + $rotlvl = ''; |
|
| 317 | + } else { |
|
| 318 | + if (!preg_match('/^"([^"]*)"\s*$/', $suite, $r)) { |
|
| 319 | + if (!preg_match("/^'([^']*)'\s*$/", $suite, $r)) { |
|
| 320 | + return []; |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $grammaire = $r[1]; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + return [$entete, $avail, $grammaire, $rotlvl]; |
|
| 328 | 328 | } |
@@ -45,8 +45,8 @@ discard block |
||
| 45 | 45 | $t = $phraseur->ouvrant[$depth] ?? ' '; |
| 46 | 46 | // espace initial signifie: deja integree au resultat |
| 47 | 47 | if ($t[0] != ' ') { |
| 48 | - $phraseur->res .= '<' . $t . '>'; |
|
| 49 | - $phraseur->ouvrant[$depth] = ' ' . $t; |
|
| 48 | + $phraseur->res .= '<'.$t.'>'; |
|
| 49 | + $phraseur->ouvrant[$depth] = ' '.$t; |
|
| 50 | 50 | } |
| 51 | 51 | $t = $phraseur->contenu[$depth]; |
| 52 | 52 | // n'indenter que s'il y a un separateur avant |
@@ -57,14 +57,14 @@ discard block |
||
| 57 | 57 | foreach ($attrs as $k => $v) { |
| 58 | 58 | $delim = !str_contains($v, "'") ? "'" : '"'; |
| 59 | 59 | $val = xml_entites_html($v); |
| 60 | - $att .= $sep . $k . '=' . $delim |
|
| 60 | + $att .= $sep.$k.'='.$delim |
|
| 61 | 61 | . ($delim !== '"' ? str_replace('"', '"', $val) : $val) |
| 62 | 62 | . $delim; |
| 63 | 63 | $sep = "\n $depth"; |
| 64 | 64 | } |
| 65 | 65 | $phraseur->depth .= ' '; |
| 66 | 66 | $phraseur->contenu[$phraseur->depth] = ''; |
| 67 | - $phraseur->ouvrant[$phraseur->depth] = $name . $att; |
|
| 67 | + $phraseur->ouvrant[$phraseur->depth] = $name.$att; |
|
| 68 | 68 | $phraseur->reperes[$phraseur->depth] = xml_get_current_line_number($phraseur->sax); |
| 69 | 69 | } |
| 70 | 70 | |
@@ -72,13 +72,13 @@ discard block |
||
| 72 | 72 | $ouv = $phraseur->ouvrant[$phraseur->depth]; |
| 73 | 73 | |
| 74 | 74 | if ($ouv[0] != ' ') { |
| 75 | - $phraseur->ouvrant[$phraseur->depth] = ' ' . $ouv; |
|
| 75 | + $phraseur->ouvrant[$phraseur->depth] = ' '.$ouv; |
|
| 76 | 76 | } else { |
| 77 | 77 | $ouv = ''; |
| 78 | 78 | } |
| 79 | 79 | $t = $phraseur->contenu[$phraseur->depth]; |
| 80 | 80 | $phraseur->depth = substr($phraseur->depth, 2); |
| 81 | - $t = preg_replace("/[\n\t ]+$/", "\n" . $phraseur->depth, $t); |
|
| 81 | + $t = preg_replace("/[\n\t ]+$/", "\n".$phraseur->depth, $t); |
|
| 82 | 82 | |
| 83 | 83 | // fusion <balise></balise> en <balise />. |
| 84 | 84 | // ATTENTION, certains clients http croient que fusion ==> pas d'atttributs |
@@ -87,9 +87,9 @@ discard block |
||
| 87 | 87 | // (param fusion_bal) |
| 88 | 88 | |
| 89 | 89 | if ($t || (($ouv != $name) and !$fusion_bal)) { |
| 90 | - $phraseur->res .= ($ouv ? ('<' . $ouv . '>') : '') . $t . '</' . $name . '>'; |
|
| 90 | + $phraseur->res .= ($ouv ? ('<'.$ouv.'>') : '').$t.'</'.$name.'>'; |
|
| 91 | 91 | } else { |
| 92 | - $phraseur->res .= ($ouv ? ('<' . $ouv . ' />') : ('</' . $name . '>')); |
|
| 92 | + $phraseur->res .= ($ouv ? ('<'.$ouv.' />') : ('</'.$name.'>')); |
|
| 93 | 93 | } |
| 94 | 94 | } |
| 95 | 95 | |
@@ -131,16 +131,15 @@ discard block |
||
| 131 | 131 | coordonnees_erreur( |
| 132 | 132 | $phraseur, |
| 133 | 133 | xml_error_string(xml_get_error_code($phraseur->sax)) |
| 134 | - . "<br />\n" . |
|
| 135 | - (!$phraseur->depth ? '' : |
|
| 136 | - ('(' . |
|
| 137 | - _T('erreur_balise_non_fermee') . |
|
| 138 | - ' <tt>' . |
|
| 139 | - $phraseur->ouvrant[$phraseur->depth] . |
|
| 140 | - '</tt> ' . |
|
| 141 | - _T('ligne') . |
|
| 142 | - ' ' . |
|
| 143 | - $phraseur->reperes[$phraseur->depth] . |
|
| 134 | + . "<br />\n". |
|
| 135 | + (!$phraseur->depth ? '' : ('('. |
|
| 136 | + _T('erreur_balise_non_fermee'). |
|
| 137 | + ' <tt>'. |
|
| 138 | + $phraseur->ouvrant[$phraseur->depth]. |
|
| 139 | + '</tt> '. |
|
| 140 | + _T('ligne'). |
|
| 141 | + ' '. |
|
| 142 | + $phraseur->reperes[$phraseur->depth]. |
|
| 144 | 143 | ") <br />\n")) |
| 145 | 144 | ); |
| 146 | 145 | } |
@@ -181,7 +180,7 @@ discard block |
||
| 181 | 180 | // et escamoter le doctype que sax mange en php5 mais pas en php4 |
| 182 | 181 | if (!$doctype) { |
| 183 | 182 | if (!$r = analyser_doctype($page)) { |
| 184 | - $page = _MESSAGE_DOCTYPE . _DOCTYPE_ECRIRE |
|
| 183 | + $page = _MESSAGE_DOCTYPE._DOCTYPE_ECRIRE |
|
| 185 | 184 | . preg_replace(_REGEXP_DOCTYPE, '', $page); |
| 186 | 185 | $r = analyser_doctype($page); |
| 187 | 186 | } |
@@ -288,18 +287,18 @@ discard block |
||
| 288 | 287 | 'rss-0.91.dtd' |
| 289 | 288 | ]; |
| 290 | 289 | } else { |
| 291 | - $dtd = $topelement . '.dtd'; |
|
| 290 | + $dtd = $topelement.'.dtd'; |
|
| 292 | 291 | $f = find_in_path($dtd); |
| 293 | 292 | if (file_exists($f)) { |
| 294 | 293 | return [$entete, 'SYSTEM', $f, $dtd]; |
| 295 | 294 | } |
| 296 | 295 | } |
| 297 | 296 | } |
| 298 | - spip_log('Dtd pas vu pour ' . substr($data, 0, 100)); |
|
| 297 | + spip_log('Dtd pas vu pour '.substr($data, 0, 100)); |
|
| 299 | 298 | |
| 300 | 299 | return []; |
| 301 | 300 | } |
| 302 | - [$entete, , $topelement, $avail, $suite] = $page; |
|
| 301 | + [$entete,, $topelement, $avail, $suite] = $page; |
|
| 303 | 302 | |
| 304 | 303 | if (!preg_match('/^"([^"]*)"\s*(.*)$/', $suite, $r)) { |
| 305 | 304 | if (!preg_match("/^'([^']*)'\s*(.*)$/", $suite, $r)) { |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | if (!$link) { |
| 65 | 65 | $erreurs[] = pg_last_error(); |
| 66 | 66 | foreach ($erreurs as $e) { |
| 67 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 67 | + spip_log('Echec pg_connect. Erreur : '.$e, 'pg.'._LOG_HS); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | return false; |
@@ -82,8 +82,8 @@ discard block |
||
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | spip_log( |
| 85 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 86 | - 'pg.' . _LOG_DEBUG |
|
| 85 | + "Connexion vers $host, base $db, prefixe $prefixe ".($link ? 'operationnelle' : 'impossible'), |
|
| 86 | + 'pg.'._LOG_DEBUG |
|
| 87 | 87 | ); |
| 88 | 88 | |
| 89 | 89 | return !$link ? false : [ |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | } else { |
| 172 | 172 | $suite = ''; |
| 173 | 173 | } |
| 174 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 174 | + $query = preg_replace('/([,\s])spip_/', '\1'.$prefixe.'_', $query).$suite; |
|
| 175 | 175 | |
| 176 | 176 | // renvoyer la requete inerte si demandee |
| 177 | 177 | if (!$requeter) { |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | // ou revoir l'api de sql_alter en creant un |
| 240 | 240 | // sql_alter_table($table,array($actions)); |
| 241 | 241 | if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
| 242 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 242 | + spip_log("$query mal comprise", 'pg.'._LOG_ERREUR); |
|
| 243 | 243 | |
| 244 | 244 | return false; |
| 245 | 245 | } |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | $i = 0; |
| 253 | 253 | $ouverte = false; |
| 254 | 254 | while ($do = array_shift($todo)) { |
| 255 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 255 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i].','.$do : $do; |
|
| 256 | 256 | $o = (str_contains($do, '(')); |
| 257 | 257 | $f = (str_contains($do, ')')); |
| 258 | 258 | if ($o and !$f) { |
@@ -265,33 +265,33 @@ discard block |
||
| 265 | 265 | } |
| 266 | 266 | } |
| 267 | 267 | $todo = $todo2; |
| 268 | - $query = $debut . ' ' . array_shift($todo); |
|
| 268 | + $query = $debut.' '.array_shift($todo); |
|
| 269 | 269 | |
| 270 | 270 | if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
| 271 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 271 | + spip_log("$query incompris", 'pg.'._LOG_ERREUR); |
|
| 272 | 272 | } else { |
| 273 | 273 | if ($r[1]) { |
| 274 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 274 | + spip_log("j'ignore IGNORE dans $query", 'pg.'._LOG_AVERTISSEMENT); |
|
| 275 | 275 | } |
| 276 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 276 | + $f = 'spip_pg_alter_'.strtolower($r[3]); |
|
| 277 | 277 | if (function_exists($f)) { |
| 278 | 278 | $f($r[2], $r[4], $serveur, $requeter); |
| 279 | 279 | } else { |
| 280 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 280 | + spip_log("$query non prevu", 'pg.'._LOG_ERREUR); |
|
| 281 | 281 | } |
| 282 | 282 | } |
| 283 | 283 | // Alter a plusieurs args. Faudrait optimiser. |
| 284 | 284 | if ($todo) { |
| 285 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 285 | + spip_pg_alter("TABLE $table ".join(',', $todo)); |
|
| 286 | 286 | } |
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 290 | 290 | if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
| 291 | - spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 291 | + spip_log("alter change: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 292 | 292 | } else { |
| 293 | 293 | [, $old, $new, $type, $default, $null, $def2] = $r; |
| 294 | - $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 294 | + $actions = ["ALTER $old TYPE ".mysql2pg_type($type)]; |
|
| 295 | 295 | if ($null) { |
| 296 | 296 | $actions[] = "ALTER $old SET NOT NULL"; |
| 297 | 297 | } else { |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | $actions[] = "ALTER $old DROP DEFAULT"; |
| 305 | 305 | } |
| 306 | 306 | |
| 307 | - spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 307 | + spip_pg_query("ALTER TABLE $table ".join(', ', $actions)); |
|
| 308 | 308 | |
| 309 | 309 | if ($old != $new) { |
| 310 | 310 | spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
@@ -315,7 +315,7 @@ discard block |
||
| 315 | 315 | function spip_pg_alter_add($table, $arg, $serveur = '', $requeter = true) { |
| 316 | 316 | $nom_index = null; |
| 317 | 317 | if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
| 318 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 318 | + spip_log("alter add $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 319 | 319 | |
| 320 | 320 | return null; |
| 321 | 321 | } |
@@ -325,14 +325,14 @@ discard block |
||
| 325 | 325 | $m[2] = $n[1]; |
| 326 | 326 | } |
| 327 | 327 | |
| 328 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 328 | + return spip_pg_query("ALTER TABLE $table ADD ".$m[1].' '.mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 329 | 329 | } elseif ($r[1][0] == 'P') { |
| 330 | 330 | // la primary peut etre sur plusieurs champs |
| 331 | 331 | $r[2] = trim(str_replace('`', '', $r[2])); |
| 332 | 332 | $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
| 333 | 333 | |
| 334 | 334 | return spip_pg_query( |
| 335 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 335 | + "ALTER TABLE $table ADD CONSTRAINT $table".'_pkey PRIMARY KEY ('.$m.')', |
|
| 336 | 336 | $serveur, |
| 337 | 337 | $requeter |
| 338 | 338 | ); |
@@ -351,7 +351,7 @@ discard block |
||
| 351 | 351 | $colonnes = substr($m[1], 1, -1); |
| 352 | 352 | if (str_contains(',', $colonnes)) { |
| 353 | 353 | spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
| 354 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 354 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.'._LOG_ERREUR); |
|
| 355 | 355 | } else { |
| 356 | 356 | $nom_index = $colonnes; |
| 357 | 357 | } |
@@ -367,23 +367,23 @@ discard block |
||
| 367 | 367 | |
| 368 | 368 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 369 | 369 | if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
| 370 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 370 | + spip_log("alter drop: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 371 | 371 | } else { |
| 372 | 372 | if (!$r[1] or $r[1] == 'COLUMN') { |
| 373 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 373 | + return spip_pg_query("ALTER TABLE $table DROP ".$r[2], $serveur); |
|
| 374 | 374 | } elseif ($r[1][0] == 'P') { |
| 375 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 375 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table".'_pkey', $serveur); |
|
| 376 | 376 | } else { |
| 377 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 377 | + return spip_pg_query('DROP INDEX '.$table.'_'.$r[2], $serveur); |
|
| 378 | 378 | } |
| 379 | 379 | } |
| 380 | 380 | } |
| 381 | 381 | |
| 382 | 382 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 383 | 383 | if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
| 384 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 384 | + spip_log("alter modify: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 385 | 385 | } else { |
| 386 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 386 | + return spip_pg_alter_change($table, $r[1].' '.$arg, $serveur = '', $requeter = true); |
|
| 387 | 387 | } |
| 388 | 388 | } |
| 389 | 389 | |
@@ -399,7 +399,7 @@ discard block |
||
| 399 | 399 | } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
| 400 | 400 | $rename = $r[2]; |
| 401 | 401 | } else { |
| 402 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 402 | + spip_log("alter rename: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
@@ -420,8 +420,8 @@ discard block |
||
| 420 | 420 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 421 | 421 | if (!($nom or $table or $champs)) { |
| 422 | 422 | spip_log( |
| 423 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 424 | - 'pg.' . _LOG_ERREUR |
|
| 423 | + "Champ manquant pour creer un index pg ($nom, $table, (".@join(',', $champs).'))', |
|
| 424 | + 'pg.'._LOG_ERREUR |
|
| 425 | 425 | ); |
| 426 | 426 | |
| 427 | 427 | return false; |
@@ -432,7 +432,7 @@ discard block |
||
| 432 | 432 | |
| 433 | 433 | // PG ne differentie pas noms des index en fonction des tables |
| 434 | 434 | // il faut donc creer des noms uniques d'index pour une base pg |
| 435 | - $nom = $table . '_' . $nom; |
|
| 435 | + $nom = $table.'_'.$nom; |
|
| 436 | 436 | // enlever d'eventuelles parentheses deja presentes sur champs |
| 437 | 437 | if (!is_array($champs)) { |
| 438 | 438 | if ($champs[0] == '(') { |
@@ -440,7 +440,7 @@ discard block |
||
| 440 | 440 | } |
| 441 | 441 | $champs = [$champs]; |
| 442 | 442 | } |
| 443 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 443 | + $query = "CREATE INDEX $nom ON $table (".join(',', $champs).')'; |
|
| 444 | 444 | if (!$requeter) { |
| 445 | 445 | return $query; |
| 446 | 446 | } |
@@ -463,7 +463,7 @@ discard block |
||
| 463 | 463 | } else { |
| 464 | 464 | $suite = ''; |
| 465 | 465 | } |
| 466 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 466 | + $query = 'EXPLAIN '.preg_replace('/([,\s])spip_/', '\1'.$prefixe.'_', $query).$suite; |
|
| 467 | 467 | |
| 468 | 468 | if (!$requeter) { |
| 469 | 469 | return $query; |
@@ -556,16 +556,16 @@ discard block |
||
| 556 | 556 | } |
| 557 | 557 | } |
| 558 | 558 | $from = spip_pg_from($from, $prefixe); |
| 559 | - $query = 'SELECT ' . $select |
|
| 559 | + $query = 'SELECT '.$select |
|
| 560 | 560 | . (!$from ? '' : "\nFROM $from") |
| 561 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 561 | + . (!$where ? '' : ("\nWHERE ".(!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 562 | 562 | "\n\tAND ", |
| 563 | 563 | array_map('calculer_pg_where', $where) |
| 564 | 564 | ))))) |
| 565 | 565 | . spip_pg_groupby($groupby, $from, $select) |
| 566 | 566 | . (!$having ? '' : "\nHAVING $having") |
| 567 | 567 | . ($orderby ? ("\nORDER BY $orderby") : '') |
| 568 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 568 | + . (!$limit ? '' : (" LIMIT $count".(!$offset ? '' : " OFFSET $offset"))); |
|
| 569 | 569 | |
| 570 | 570 | // renvoyer la requete inerte si demandee |
| 571 | 571 | if ($requeter === false) { |
@@ -586,7 +586,7 @@ discard block |
||
| 586 | 586 | $from = spip_pg_select_as($from); |
| 587 | 587 | } |
| 588 | 588 | |
| 589 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 589 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1'.$prefixe.'_', $from); |
|
| 590 | 590 | } |
| 591 | 591 | |
| 592 | 592 | function spip_pg_orderby($order, $select) { |
@@ -594,7 +594,7 @@ discard block |
||
| 594 | 594 | $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
| 595 | 595 | |
| 596 | 596 | foreach ($arg as $v) { |
| 597 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 597 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+'.$v.'/', $select, $m)) { |
|
| 598 | 598 | $res[] = $m[1]; |
| 599 | 599 | } else { |
| 600 | 600 | $res[] = $v; |
@@ -772,7 +772,7 @@ discard block |
||
| 772 | 772 | $n++; |
| 773 | 773 | $res .= "\nwhen $index=$v then $n"; |
| 774 | 774 | } |
| 775 | - $arg = $m[1] . "case $res else 0 end " |
|
| 775 | + $arg = $m[1]."case $res else 0 end " |
|
| 776 | 776 | . substr($arg, strlen($m[0])); |
| 777 | 777 | } |
| 778 | 778 | |
@@ -815,9 +815,9 @@ discard block |
||
| 815 | 815 | } |
| 816 | 816 | |
| 817 | 817 | if (strtoupper($join) === 'AND') { |
| 818 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 818 | + return $exp.join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 819 | 819 | } else { |
| 820 | - return $exp . join($join, $v); |
|
| 820 | + return $exp.join($join, $v); |
|
| 821 | 821 | } |
| 822 | 822 | } |
| 823 | 823 | |
@@ -827,7 +827,7 @@ discard block |
||
| 827 | 827 | if (substr($k, -1) == '@') { |
| 828 | 828 | // c'est une jointure qui se refere au from precedent |
| 829 | 829 | // pas de virgule |
| 830 | - $argsas .= ' ' . $v; |
|
| 830 | + $argsas .= ' '.$v; |
|
| 831 | 831 | } else { |
| 832 | 832 | $as = ''; |
| 833 | 833 | // spip_log("$k : $v", _LOG_DEBUG); |
@@ -837,7 +837,7 @@ discard block |
||
| 837 | 837 | } elseif ($v != $k) { |
| 838 | 838 | $p = strpos($v, ' '); |
| 839 | 839 | if ($p) { |
| 840 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 840 | + $v = substr($v, 0, $p)." AS $k".substr($v, $p); |
|
| 841 | 841 | } else { |
| 842 | 842 | $as = " AS $k"; |
| 843 | 843 | } |
@@ -845,7 +845,7 @@ discard block |
||
| 845 | 845 | } |
| 846 | 846 | // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
| 847 | 847 | // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
| 848 | - $argsas .= ', ' . $v . $as; |
|
| 848 | + $argsas .= ', '.$v.$as; |
|
| 849 | 849 | } |
| 850 | 850 | } |
| 851 | 851 | |
@@ -876,7 +876,7 @@ discard block |
||
| 876 | 876 | $serveur = '', |
| 877 | 877 | $requeter = true |
| 878 | 878 | ) { |
| 879 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 879 | + $c = !$groupby ? '*' : ('DISTINCT '.(is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 880 | 880 | $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
| 881 | 881 | if (!$requeter) { |
| 882 | 882 | return $r; |
@@ -977,8 +977,8 @@ discard block |
||
| 977 | 977 | |
| 978 | 978 | return spip_pg_insert( |
| 979 | 979 | $table, |
| 980 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 981 | - '(' . join(',', $couples) . ')', |
|
| 980 | + '('.join(',', array_keys($couples)).')', |
|
| 981 | + '('.join(',', $couples).')', |
|
| 982 | 982 | $desc, |
| 983 | 983 | $serveur, |
| 984 | 984 | $requeter |
@@ -1001,7 +1001,7 @@ discard block |
||
| 1001 | 1001 | $c = $tab_couples[0] ?? []; |
| 1002 | 1002 | $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
| 1003 | 1003 | |
| 1004 | - $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1004 | + $cles = '('.join(',', array_keys($les_cles)).')'; |
|
| 1005 | 1005 | $valeurs = []; |
| 1006 | 1006 | foreach ($tab_couples as $couples) { |
| 1007 | 1007 | foreach ($couples as $champ => $val) { |
@@ -1010,7 +1010,7 @@ discard block |
||
| 1010 | 1010 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
| 1011 | 1011 | $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
| 1012 | 1012 | |
| 1013 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1013 | + $valeurs[] = '('.join(',', $couples).')'; |
|
| 1014 | 1014 | } |
| 1015 | 1015 | $valeurs = implode(', ', $valeurs); |
| 1016 | 1016 | |
@@ -1031,7 +1031,7 @@ discard block |
||
| 1031 | 1031 | |
| 1032 | 1032 | $set = []; |
| 1033 | 1033 | foreach ($couples as $champ => $val) { |
| 1034 | - $set[] = $champ . '=' . $val; |
|
| 1034 | + $set[] = $champ.'='.$val; |
|
| 1035 | 1035 | } |
| 1036 | 1036 | |
| 1037 | 1037 | $query = calculer_pg_expression('UPDATE', $table, ',') |
@@ -1066,7 +1066,7 @@ discard block |
||
| 1066 | 1066 | |
| 1067 | 1067 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1068 | 1068 | if (!$values) { |
| 1069 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1069 | + spip_log("replace vide $table", 'pg.'._LOG_AVERTISSEMENT); |
|
| 1070 | 1070 | |
| 1071 | 1071 | return 0; |
| 1072 | 1072 | } |
@@ -1100,8 +1100,8 @@ discard block |
||
| 1100 | 1100 | if (!$where) { |
| 1101 | 1101 | return spip_pg_insert( |
| 1102 | 1102 | $table, |
| 1103 | - '(' . join(',', array_keys($values)) . ')', |
|
| 1104 | - '(' . join(',', $values) . ')', |
|
| 1103 | + '('.join(',', array_keys($values)).')', |
|
| 1104 | + '('.join(',', $values).')', |
|
| 1105 | 1105 | $desc, |
| 1106 | 1106 | $serveur |
| 1107 | 1107 | ); |
@@ -1122,12 +1122,11 @@ discard block |
||
| 1122 | 1122 | $couples = pg_affected_rows($couples); |
| 1123 | 1123 | } |
| 1124 | 1124 | if (!$couples) { |
| 1125 | - $ret = !$seq ? '' : |
|
| 1126 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1127 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1125 | + $ret = !$seq ? '' : (" RETURNING nextval('$seq') < $prim"); |
|
| 1126 | + $connexion['last'] = $q = "INSERT INTO $table (".join(',', array_keys($values)).') VALUES ('.join( |
|
| 1128 | 1127 | ',', |
| 1129 | 1128 | $values |
| 1130 | - ) . ")$ret"; |
|
| 1129 | + ).")$ret"; |
|
| 1131 | 1130 | $couples = spip_pg_query_simple($link, $q); |
| 1132 | 1131 | if (!$couples) { |
| 1133 | 1132 | return false; |
@@ -1175,7 +1174,7 @@ discard block |
||
| 1175 | 1174 | ) { |
| 1176 | 1175 | return ''; |
| 1177 | 1176 | } else { |
| 1178 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1177 | + return $raw ? $prim : $table.'_'.$prim.'_seq'; |
|
| 1179 | 1178 | } |
| 1180 | 1179 | } |
| 1181 | 1180 | |
@@ -1192,29 +1191,29 @@ discard block |
||
| 1192 | 1191 | return spip_pg_frommysql($v); |
| 1193 | 1192 | } else { |
| 1194 | 1193 | if (strncmp($v, '0000', 4) == 0) { |
| 1195 | - $v = '0001' . substr($v, 4); |
|
| 1194 | + $v = '0001'.substr($v, 4); |
|
| 1196 | 1195 | } |
| 1197 | 1196 | if (strpos($v, '-00-00') === 4) { |
| 1198 | - $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1197 | + $v = substr($v, 0, 4).'-01-01'.substr($v, 10); |
|
| 1199 | 1198 | } |
| 1200 | 1199 | |
| 1201 | 1200 | return "timestamp '$v'"; |
| 1202 | 1201 | } |
| 1203 | 1202 | } elseif (!sql_test_int($t)) { |
| 1204 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1203 | + return ("'".pg_escape_string($v)."'"); |
|
| 1205 | 1204 | } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
| 1206 | 1205 | return $v; |
| 1207 | 1206 | } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
| 1208 | 1207 | return substr($v, 1); |
| 1209 | 1208 | } else { |
| 1210 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1209 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.'._LOG_AVERTISSEMENT); |
|
| 1211 | 1210 | |
| 1212 | 1211 | return intval($v); |
| 1213 | 1212 | } |
| 1214 | 1213 | } |
| 1215 | 1214 | |
| 1216 | 1215 | function spip_pg_hex($v) { |
| 1217 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1216 | + return "CAST(x'".$v."' as bigint)"; |
|
| 1218 | 1217 | } |
| 1219 | 1218 | |
| 1220 | 1219 | function spip_pg_quote($v, $type = '') { |
@@ -1252,15 +1251,15 @@ discard block |
||
| 1252 | 1251 | return $not ? '0=0' : '0=1'; |
| 1253 | 1252 | } |
| 1254 | 1253 | if (strpos($valeurs, "CAST(x'") !== false) { |
| 1255 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1254 | + return "($val=".join("OR $val=", explode(',', $valeurs)).')'; |
|
| 1256 | 1255 | } |
| 1257 | 1256 | $n = $i = 0; |
| 1258 | 1257 | $in_sql = ''; |
| 1259 | 1258 | while ($n = strpos($valeurs, ',', $n + 1)) { |
| 1260 | 1259 | if ((++$i) >= 255) { |
| 1261 | - $in_sql .= "($val $not IN (" . |
|
| 1262 | - substr($valeurs, 0, $n) . |
|
| 1263 | - "))\n" . |
|
| 1260 | + $in_sql .= "($val $not IN (". |
|
| 1261 | + substr($valeurs, 0, $n). |
|
| 1262 | + "))\n". |
|
| 1264 | 1263 | ($not ? "AND\t" : "OR\t"); |
| 1265 | 1264 | $valeurs = substr($valeurs, $n + 1); |
| 1266 | 1265 | $i = $n = 0; |
@@ -1276,7 +1275,7 @@ discard block |
||
| 1276 | 1275 | $s = $link ? pg_last_error($link) : pg_last_error(); |
| 1277 | 1276 | if ($s) { |
| 1278 | 1277 | $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
| 1279 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1278 | + spip_log("$s - $query", 'pg.'._LOG_ERREUR); |
|
| 1280 | 1279 | } |
| 1281 | 1280 | |
| 1282 | 1281 | return $s; |
@@ -1324,7 +1323,7 @@ discard block |
||
| 1324 | 1323 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1325 | 1324 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 1326 | 1325 | $link = $connexion['link']; |
| 1327 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1326 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE '._q($match); |
|
| 1328 | 1327 | |
| 1329 | 1328 | return spip_pg_query_simple($link, $q); |
| 1330 | 1329 | } |
@@ -1332,7 +1331,7 @@ discard block |
||
| 1332 | 1331 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1333 | 1332 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 1334 | 1333 | $link = $connexion['link']; |
| 1335 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1334 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE '._q($nom_table); |
|
| 1336 | 1335 | |
| 1337 | 1336 | $res = spip_pg_query_simple($link, $q); |
| 1338 | 1337 | if (!$res) { |
@@ -1343,15 +1342,15 @@ discard block |
||
| 1343 | 1342 | // il faut en tenir compte dans le return |
| 1344 | 1343 | $fields = []; |
| 1345 | 1344 | while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
| 1346 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1345 | + $fields[$field[0]] = $field[2].(!$field[1] ? '' : (' DEFAULT '.$field[1])); |
|
| 1347 | 1346 | } |
| 1348 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1347 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE '._q($nom_table); |
|
| 1349 | 1348 | $res = spip_pg_query_simple($link, $q); |
| 1350 | 1349 | $keys = []; |
| 1351 | 1350 | while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
| 1352 | 1351 | if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
| 1353 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1354 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1352 | + $nom = str_replace($nom_table.'_', '', $r[2]); |
|
| 1353 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY '.$nom))] = $r[3]; |
|
| 1355 | 1354 | } |
| 1356 | 1355 | } |
| 1357 | 1356 | |
@@ -1383,16 +1382,16 @@ discard block |
||
| 1383 | 1382 | if (strpos($k, 'KEY ') === 0) { |
| 1384 | 1383 | $n = str_replace('`', '', $k); |
| 1385 | 1384 | $v = str_replace('`', '"', $v); |
| 1386 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1385 | + $i = $nom.preg_replace('/KEY +/', '_', $n); |
|
| 1387 | 1386 | if ($k != $n) { |
| 1388 | 1387 | $i = "\"$i\""; |
| 1389 | 1388 | } |
| 1390 | 1389 | $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
| 1391 | 1390 | } elseif (strpos($k, 'UNIQUE ') === 0) { |
| 1392 | 1391 | $k = preg_replace('/^UNIQUE +/', '', $k); |
| 1393 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1392 | + $prim .= "$s\n\t\tCONSTRAINT ".str_replace('`', '"', $k)." UNIQUE ($v)"; |
|
| 1394 | 1393 | } else { |
| 1395 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1394 | + $prim .= "$s\n\t\t".str_replace('`', '"', $k)." ($v)"; |
|
| 1396 | 1395 | } |
| 1397 | 1396 | if ($k == 'PRIMARY KEY') { |
| 1398 | 1397 | $prim_name = $v; |
@@ -1403,17 +1402,17 @@ discard block |
||
| 1403 | 1402 | |
| 1404 | 1403 | $character_set = ''; |
| 1405 | 1404 | if (@$GLOBALS['meta']['charset_sql_base']) { |
| 1406 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1405 | + $character_set .= ' CHARACTER SET '.$GLOBALS['meta']['charset_sql_base']; |
|
| 1407 | 1406 | } |
| 1408 | 1407 | if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
| 1409 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1408 | + $character_set .= ' COLLATE '.$GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1410 | 1409 | } |
| 1411 | 1410 | |
| 1412 | 1411 | foreach ($champs as $k => $v) { |
| 1413 | 1412 | $k = str_replace('`', '"', $k); |
| 1414 | 1413 | if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
| 1415 | 1414 | if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
| 1416 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1415 | + $v = $defs[1].$character_set.' '.substr($v, strlen($defs[1])); |
|
| 1417 | 1416 | } |
| 1418 | 1417 | } |
| 1419 | 1418 | |
@@ -1428,7 +1427,7 @@ discard block |
||
| 1428 | 1427 | |
| 1429 | 1428 | // En l'absence de "if not exists" en PG, on neutralise les erreurs |
| 1430 | 1429 | |
| 1431 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1430 | + $q = "CREATE $temporary TABLE $nom ($query".($prim ? ",$prim" : '').')'. |
|
| 1432 | 1431 | ($character_set ? " DEFAULT $character_set" : '') |
| 1433 | 1432 | . "\n"; |
| 1434 | 1433 | |
@@ -1439,7 +1438,7 @@ discard block |
||
| 1439 | 1438 | $r = @pg_query($link, $q); |
| 1440 | 1439 | |
| 1441 | 1440 | if (!$r) { |
| 1442 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1441 | + spip_log("Impossible de creer cette table: $q", 'pg.'._LOG_ERREUR); |
|
| 1443 | 1442 | } else { |
| 1444 | 1443 | foreach ($keys as $index) { |
| 1445 | 1444 | pg_query($link, $index); |
@@ -1462,20 +1461,20 @@ discard block |
||
| 1462 | 1461 | // vue deja presente |
| 1463 | 1462 | if (sql_showtable($nom, false, $serveur)) { |
| 1464 | 1463 | if ($requeter) { |
| 1465 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1464 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.'._LOG_ERREUR); |
|
| 1466 | 1465 | } |
| 1467 | 1466 | |
| 1468 | 1467 | return false; |
| 1469 | 1468 | } |
| 1470 | 1469 | |
| 1471 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1470 | + $query = "CREATE VIEW $nom AS ".$query_select; |
|
| 1472 | 1471 | |
| 1473 | 1472 | return spip_pg_query($query, $serveur, $requeter); |
| 1474 | 1473 | } |
| 1475 | 1474 | |
| 1476 | 1475 | |
| 1477 | 1476 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1478 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1477 | + spip_log('changement de charset sql a ecrire en PG', 'pg.'._LOG_ERREUR); |
|
| 1479 | 1478 | } |
| 1480 | 1479 | |
| 1481 | 1480 | |
@@ -1488,7 +1487,7 @@ discard block |
||
| 1488 | 1487 | * @return bool|string true / false / requete |
| 1489 | 1488 | **/ |
| 1490 | 1489 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1491 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1490 | + return spip_pg_query('VACUUM '.$table, $serveur, $requeter); |
|
| 1492 | 1491 | } |
| 1493 | 1492 | |
| 1494 | 1493 | // Selectionner la sous-chaine dans $objet |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | define('_DEFAULT_DB', 'spip'); |
@@ -30,159 +30,159 @@ discard block |
||
| 30 | 30 | // si ca ne marche toujours pas, echec. |
| 31 | 31 | |
| 32 | 32 | function req_pg_dist($addr, $port, $login, #[\SensitiveParameter] $pass, $db = '', $prefixe = '') { |
| 33 | - static $last_connect = []; |
|
| 34 | - if (!extension_loaded('pgsql')) { |
|
| 35 | - return false; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - // si provient de selectdb |
|
| 39 | - if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 40 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 41 | - ${$a} = $last_connect[$a]; |
|
| 42 | - } |
|
| 43 | - } |
|
| 44 | - [$host, $p] = array_pad(explode(';', $addr), 2, null); |
|
| 45 | - if ($p > 0) { |
|
| 46 | - $port = " port=$p"; |
|
| 47 | - } else { |
|
| 48 | - $port = ''; |
|
| 49 | - } |
|
| 50 | - $erreurs = []; |
|
| 51 | - if ($db) { |
|
| 52 | - @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 53 | - } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 54 | - $erreurs[] = pg_last_error(); |
|
| 55 | - if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 56 | - $db = $login; |
|
| 57 | - } else { |
|
| 58 | - $erreurs[] = pg_last_error(); |
|
| 59 | - $db = _DEFAULT_DB; |
|
| 60 | - $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - if (!$link) { |
|
| 64 | - $erreurs[] = pg_last_error(); |
|
| 65 | - foreach ($erreurs as $e) { |
|
| 66 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return false; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - if ($link) { |
|
| 73 | - $last_connect = [ |
|
| 74 | - 'addr' => $addr, |
|
| 75 | - 'port' => $port, |
|
| 76 | - 'login' => $login, |
|
| 77 | - 'pass' => $pass, |
|
| 78 | - 'db' => $db, |
|
| 79 | - 'prefixe' => $prefixe, |
|
| 80 | - ]; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - spip_log( |
|
| 84 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 85 | - 'pg.' . _LOG_DEBUG |
|
| 86 | - ); |
|
| 87 | - |
|
| 88 | - return !$link ? false : [ |
|
| 89 | - 'db' => $db, |
|
| 90 | - 'prefixe' => $prefixe ?: $db, |
|
| 91 | - 'link' => $link, |
|
| 92 | - ]; |
|
| 33 | + static $last_connect = []; |
|
| 34 | + if (!extension_loaded('pgsql')) { |
|
| 35 | + return false; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + // si provient de selectdb |
|
| 39 | + if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 40 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 41 | + ${$a} = $last_connect[$a]; |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | + [$host, $p] = array_pad(explode(';', $addr), 2, null); |
|
| 45 | + if ($p > 0) { |
|
| 46 | + $port = " port=$p"; |
|
| 47 | + } else { |
|
| 48 | + $port = ''; |
|
| 49 | + } |
|
| 50 | + $erreurs = []; |
|
| 51 | + if ($db) { |
|
| 52 | + @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 53 | + } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 54 | + $erreurs[] = pg_last_error(); |
|
| 55 | + if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 56 | + $db = $login; |
|
| 57 | + } else { |
|
| 58 | + $erreurs[] = pg_last_error(); |
|
| 59 | + $db = _DEFAULT_DB; |
|
| 60 | + $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + if (!$link) { |
|
| 64 | + $erreurs[] = pg_last_error(); |
|
| 65 | + foreach ($erreurs as $e) { |
|
| 66 | + spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + if ($link) { |
|
| 73 | + $last_connect = [ |
|
| 74 | + 'addr' => $addr, |
|
| 75 | + 'port' => $port, |
|
| 76 | + 'login' => $login, |
|
| 77 | + 'pass' => $pass, |
|
| 78 | + 'db' => $db, |
|
| 79 | + 'prefixe' => $prefixe, |
|
| 80 | + ]; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + spip_log( |
|
| 84 | + "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 85 | + 'pg.' . _LOG_DEBUG |
|
| 86 | + ); |
|
| 87 | + |
|
| 88 | + return !$link ? false : [ |
|
| 89 | + 'db' => $db, |
|
| 90 | + 'prefixe' => $prefixe ?: $db, |
|
| 91 | + 'link' => $link, |
|
| 92 | + ]; |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | $GLOBALS['spip_pg_functions_1'] = [ |
| 96 | - 'alter' => 'spip_pg_alter', |
|
| 97 | - 'count' => 'spip_pg_count', |
|
| 98 | - 'countsel' => 'spip_pg_countsel', |
|
| 99 | - 'create' => 'spip_pg_create', |
|
| 100 | - 'create_base' => 'spip_pg_create_base', |
|
| 101 | - 'create_view' => 'spip_pg_create_view', |
|
| 102 | - 'date_proche' => 'spip_pg_date_proche', |
|
| 103 | - 'delete' => 'spip_pg_delete', |
|
| 104 | - 'drop_table' => 'spip_pg_drop_table', |
|
| 105 | - 'drop_view' => 'spip_pg_drop_view', |
|
| 106 | - 'errno' => 'spip_pg_errno', |
|
| 107 | - 'error' => 'spip_pg_error', |
|
| 108 | - 'explain' => 'spip_pg_explain', |
|
| 109 | - 'fetch' => 'spip_pg_fetch', |
|
| 110 | - 'seek' => 'spip_pg_seek', |
|
| 111 | - 'free' => 'spip_pg_free', |
|
| 112 | - 'hex' => 'spip_pg_hex', |
|
| 113 | - 'in' => 'spip_pg_in', |
|
| 114 | - 'insert' => 'spip_pg_insert', |
|
| 115 | - 'insertq' => 'spip_pg_insertq', |
|
| 116 | - 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 117 | - 'listdbs' => 'spip_pg_listdbs', |
|
| 118 | - 'multi' => 'spip_pg_multi', |
|
| 119 | - 'optimize' => 'spip_pg_optimize', |
|
| 120 | - 'query' => 'spip_pg_query', |
|
| 121 | - 'quote' => 'spip_pg_quote', |
|
| 122 | - 'replace' => 'spip_pg_replace', |
|
| 123 | - 'replace_multi' => 'spip_pg_replace_multi', |
|
| 124 | - 'select' => 'spip_pg_select', |
|
| 125 | - 'selectdb' => 'spip_pg_selectdb', |
|
| 126 | - 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 127 | - 'showbase' => 'spip_pg_showbase', |
|
| 128 | - 'showtable' => 'spip_pg_showtable', |
|
| 129 | - 'update' => 'spip_pg_update', |
|
| 130 | - 'updateq' => 'spip_pg_updateq', |
|
| 96 | + 'alter' => 'spip_pg_alter', |
|
| 97 | + 'count' => 'spip_pg_count', |
|
| 98 | + 'countsel' => 'spip_pg_countsel', |
|
| 99 | + 'create' => 'spip_pg_create', |
|
| 100 | + 'create_base' => 'spip_pg_create_base', |
|
| 101 | + 'create_view' => 'spip_pg_create_view', |
|
| 102 | + 'date_proche' => 'spip_pg_date_proche', |
|
| 103 | + 'delete' => 'spip_pg_delete', |
|
| 104 | + 'drop_table' => 'spip_pg_drop_table', |
|
| 105 | + 'drop_view' => 'spip_pg_drop_view', |
|
| 106 | + 'errno' => 'spip_pg_errno', |
|
| 107 | + 'error' => 'spip_pg_error', |
|
| 108 | + 'explain' => 'spip_pg_explain', |
|
| 109 | + 'fetch' => 'spip_pg_fetch', |
|
| 110 | + 'seek' => 'spip_pg_seek', |
|
| 111 | + 'free' => 'spip_pg_free', |
|
| 112 | + 'hex' => 'spip_pg_hex', |
|
| 113 | + 'in' => 'spip_pg_in', |
|
| 114 | + 'insert' => 'spip_pg_insert', |
|
| 115 | + 'insertq' => 'spip_pg_insertq', |
|
| 116 | + 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 117 | + 'listdbs' => 'spip_pg_listdbs', |
|
| 118 | + 'multi' => 'spip_pg_multi', |
|
| 119 | + 'optimize' => 'spip_pg_optimize', |
|
| 120 | + 'query' => 'spip_pg_query', |
|
| 121 | + 'quote' => 'spip_pg_quote', |
|
| 122 | + 'replace' => 'spip_pg_replace', |
|
| 123 | + 'replace_multi' => 'spip_pg_replace_multi', |
|
| 124 | + 'select' => 'spip_pg_select', |
|
| 125 | + 'selectdb' => 'spip_pg_selectdb', |
|
| 126 | + 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 127 | + 'showbase' => 'spip_pg_showbase', |
|
| 128 | + 'showtable' => 'spip_pg_showtable', |
|
| 129 | + 'update' => 'spip_pg_update', |
|
| 130 | + 'updateq' => 'spip_pg_updateq', |
|
| 131 | 131 | ]; |
| 132 | 132 | |
| 133 | 133 | // Par ou ca passe une fois les traductions faites |
| 134 | 134 | function spip_pg_trace_query($query, $serveur = '') { |
| 135 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 136 | - $prefixe = $connexion['prefixe']; |
|
| 137 | - $link = $connexion['link']; |
|
| 138 | - $db = $connexion['db']; |
|
| 139 | - |
|
| 140 | - if (isset($_GET['var_profile'])) { |
|
| 141 | - include_spip('public/tracer'); |
|
| 142 | - $t = trace_query_start(); |
|
| 143 | - $e = ''; |
|
| 144 | - } else { |
|
| 145 | - $t = 0; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $connexion['last'] = $query; |
|
| 149 | - $r = spip_pg_query_simple($link, $query); |
|
| 150 | - |
|
| 151 | - // Log de l'erreur eventuelle |
|
| 152 | - if ($e = spip_pg_errno($serveur)) { |
|
| 153 | - $e .= spip_pg_error($query, $serveur); |
|
| 154 | - } // et du fautif |
|
| 155 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 135 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 136 | + $prefixe = $connexion['prefixe']; |
|
| 137 | + $link = $connexion['link']; |
|
| 138 | + $db = $connexion['db']; |
|
| 139 | + |
|
| 140 | + if (isset($_GET['var_profile'])) { |
|
| 141 | + include_spip('public/tracer'); |
|
| 142 | + $t = trace_query_start(); |
|
| 143 | + $e = ''; |
|
| 144 | + } else { |
|
| 145 | + $t = 0; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $connexion['last'] = $query; |
|
| 149 | + $r = spip_pg_query_simple($link, $query); |
|
| 150 | + |
|
| 151 | + // Log de l'erreur eventuelle |
|
| 152 | + if ($e = spip_pg_errno($serveur)) { |
|
| 153 | + $e .= spip_pg_error($query, $serveur); |
|
| 154 | + } // et du fautif |
|
| 155 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | // Fonction de requete generale quand on est sur que c'est SQL standard. |
| 159 | 159 | // Elle change juste le noms des tables ($table_prefix) dans le FROM etc |
| 160 | 160 | |
| 161 | 161 | function spip_pg_query($query, $serveur = '', $requeter = true) { |
| 162 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 163 | - $prefixe = $connexion['prefixe']; |
|
| 164 | - $link = $connexion['link']; |
|
| 165 | - $db = $connexion['db']; |
|
| 166 | - |
|
| 167 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 168 | - $suite = strstr($query, (string) $regs[0]); |
|
| 169 | - $query = substr($query, 0, -strlen($suite)); |
|
| 170 | - } else { |
|
| 171 | - $suite = ''; |
|
| 172 | - } |
|
| 173 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 174 | - |
|
| 175 | - // renvoyer la requete inerte si demandee |
|
| 176 | - if (!$requeter) { |
|
| 177 | - return $query; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - return spip_pg_trace_query($query, $serveur); |
|
| 162 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 163 | + $prefixe = $connexion['prefixe']; |
|
| 164 | + $link = $connexion['link']; |
|
| 165 | + $db = $connexion['db']; |
|
| 166 | + |
|
| 167 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 168 | + $suite = strstr($query, (string) $regs[0]); |
|
| 169 | + $query = substr($query, 0, -strlen($suite)); |
|
| 170 | + } else { |
|
| 171 | + $suite = ''; |
|
| 172 | + } |
|
| 173 | + $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 174 | + |
|
| 175 | + // renvoyer la requete inerte si demandee |
|
| 176 | + if (!$requeter) { |
|
| 177 | + return $query; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + return spip_pg_trace_query($query, $serveur); |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | function spip_pg_query_simple($link, $query) { |
| 184 | - #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 185 | - return pg_query($link, $query); |
|
| 184 | + #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 185 | + return pg_query($link, $query); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | /* |
@@ -194,196 +194,196 @@ discard block |
||
| 194 | 194 | * de requetes showtable intempestives |
| 195 | 195 | */ |
| 196 | 196 | function spip_pg_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 197 | - static $tables = []; |
|
| 198 | - |
|
| 199 | - if (!isset($tables[$table])) { |
|
| 200 | - if (!$desc) { |
|
| 201 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 202 | - $desc = $trouver_table($table, $serveur); |
|
| 203 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 204 | - if (!$desc) { |
|
| 205 | - return $couples; |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 210 | - // cependant, il faudra peut etre etendre |
|
| 211 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 212 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 213 | - $tables[$table] = []; |
|
| 214 | - foreach ($desc['field'] as $k => $v) { |
|
| 215 | - $v = strtolower(ltrim($v)); |
|
| 216 | - // ne pas ajouter de timestamp now() si un default est specifie |
|
| 217 | - if (strpos($v, 'timestamp') === 0 and !str_contains($v, 'default')) { |
|
| 218 | - $tables[$table][] = $k; |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - // ajout des champs type 'timestamp' absents |
|
| 224 | - foreach ($tables[$table] as $maj) { |
|
| 225 | - if (!array_key_exists($maj, $couples)) { |
|
| 226 | - $couples[$maj] = 'NOW()'; |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - return $couples; |
|
| 197 | + static $tables = []; |
|
| 198 | + |
|
| 199 | + if (!isset($tables[$table])) { |
|
| 200 | + if (!$desc) { |
|
| 201 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 202 | + $desc = $trouver_table($table, $serveur); |
|
| 203 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 204 | + if (!$desc) { |
|
| 205 | + return $couples; |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 210 | + // cependant, il faudra peut etre etendre |
|
| 211 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 212 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 213 | + $tables[$table] = []; |
|
| 214 | + foreach ($desc['field'] as $k => $v) { |
|
| 215 | + $v = strtolower(ltrim($v)); |
|
| 216 | + // ne pas ajouter de timestamp now() si un default est specifie |
|
| 217 | + if (strpos($v, 'timestamp') === 0 and !str_contains($v, 'default')) { |
|
| 218 | + $tables[$table][] = $k; |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + // ajout des champs type 'timestamp' absents |
|
| 224 | + foreach ($tables[$table] as $maj) { |
|
| 225 | + if (!array_key_exists($maj, $couples)) { |
|
| 226 | + $couples[$maj] = 'NOW()'; |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + return $couples; |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | |
| 234 | 234 | // Alter en PG ne traite pas les index |
| 235 | 235 | function spip_pg_alter($query, $serveur = '', $requeter = true) { |
| 236 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 237 | - // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 238 | - // ou revoir l'api de sql_alter en creant un |
|
| 239 | - // sql_alter_table($table,array($actions)); |
|
| 240 | - if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 241 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 242 | - |
|
| 243 | - return false; |
|
| 244 | - } |
|
| 245 | - $debut = $regs[1]; |
|
| 246 | - $table = $regs[3]; |
|
| 247 | - $suite = $regs[4]; |
|
| 248 | - $todo = explode(',', $suite); |
|
| 249 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 250 | - $todo2 = []; |
|
| 251 | - $i = 0; |
|
| 252 | - $ouverte = false; |
|
| 253 | - while ($do = array_shift($todo)) { |
|
| 254 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 255 | - $o = (str_contains($do, '(')); |
|
| 256 | - $f = (str_contains($do, ')')); |
|
| 257 | - if ($o and !$f) { |
|
| 258 | - $ouverte = true; |
|
| 259 | - } elseif ($f) { |
|
| 260 | - $ouverte = false; |
|
| 261 | - } |
|
| 262 | - if (!$ouverte) { |
|
| 263 | - $i++; |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - $todo = $todo2; |
|
| 267 | - $query = $debut . ' ' . array_shift($todo); |
|
| 268 | - |
|
| 269 | - if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 270 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 271 | - } else { |
|
| 272 | - if ($r[1]) { |
|
| 273 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 274 | - } |
|
| 275 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 276 | - if (function_exists($f)) { |
|
| 277 | - $f($r[2], $r[4], $serveur, $requeter); |
|
| 278 | - } else { |
|
| 279 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - // Alter a plusieurs args. Faudrait optimiser. |
|
| 283 | - if ($todo) { |
|
| 284 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 285 | - } |
|
| 236 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 237 | + // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 238 | + // ou revoir l'api de sql_alter en creant un |
|
| 239 | + // sql_alter_table($table,array($actions)); |
|
| 240 | + if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 241 | + spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 242 | + |
|
| 243 | + return false; |
|
| 244 | + } |
|
| 245 | + $debut = $regs[1]; |
|
| 246 | + $table = $regs[3]; |
|
| 247 | + $suite = $regs[4]; |
|
| 248 | + $todo = explode(',', $suite); |
|
| 249 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 250 | + $todo2 = []; |
|
| 251 | + $i = 0; |
|
| 252 | + $ouverte = false; |
|
| 253 | + while ($do = array_shift($todo)) { |
|
| 254 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 255 | + $o = (str_contains($do, '(')); |
|
| 256 | + $f = (str_contains($do, ')')); |
|
| 257 | + if ($o and !$f) { |
|
| 258 | + $ouverte = true; |
|
| 259 | + } elseif ($f) { |
|
| 260 | + $ouverte = false; |
|
| 261 | + } |
|
| 262 | + if (!$ouverte) { |
|
| 263 | + $i++; |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + $todo = $todo2; |
|
| 267 | + $query = $debut . ' ' . array_shift($todo); |
|
| 268 | + |
|
| 269 | + if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 270 | + spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 271 | + } else { |
|
| 272 | + if ($r[1]) { |
|
| 273 | + spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 274 | + } |
|
| 275 | + $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 276 | + if (function_exists($f)) { |
|
| 277 | + $f($r[2], $r[4], $serveur, $requeter); |
|
| 278 | + } else { |
|
| 279 | + spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + // Alter a plusieurs args. Faudrait optimiser. |
|
| 283 | + if ($todo) { |
|
| 284 | + spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 285 | + } |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 289 | - if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 290 | - spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 291 | - } else { |
|
| 292 | - [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 293 | - $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 294 | - if ($null) { |
|
| 295 | - $actions[] = "ALTER $old SET NOT NULL"; |
|
| 296 | - } else { |
|
| 297 | - $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - if ($d = ($default ?: $def2)) { |
|
| 301 | - $actions[] = "ALTER $old SET $d"; |
|
| 302 | - } else { |
|
| 303 | - $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 307 | - |
|
| 308 | - if ($old != $new) { |
|
| 309 | - spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 310 | - } |
|
| 311 | - } |
|
| 289 | + if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 290 | + spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 291 | + } else { |
|
| 292 | + [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 293 | + $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 294 | + if ($null) { |
|
| 295 | + $actions[] = "ALTER $old SET NOT NULL"; |
|
| 296 | + } else { |
|
| 297 | + $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + if ($d = ($default ?: $def2)) { |
|
| 301 | + $actions[] = "ALTER $old SET $d"; |
|
| 302 | + } else { |
|
| 303 | + $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 307 | + |
|
| 308 | + if ($old != $new) { |
|
| 309 | + spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | function spip_pg_alter_add($table, $arg, $serveur = '', $requeter = true) { |
| 315 | - $nom_index = null; |
|
| 316 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 317 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 318 | - |
|
| 319 | - return null; |
|
| 320 | - } |
|
| 321 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 322 | - preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 323 | - if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 324 | - $m[2] = $n[1]; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 328 | - } elseif ($r[1][0] == 'P') { |
|
| 329 | - // la primary peut etre sur plusieurs champs |
|
| 330 | - $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 331 | - $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 332 | - |
|
| 333 | - return spip_pg_query( |
|
| 334 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 335 | - $serveur, |
|
| 336 | - $requeter |
|
| 337 | - ); |
|
| 338 | - } else { |
|
| 339 | - preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 340 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 341 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 342 | - // |
|
| 343 | - // nom_index (colonnes) |
|
| 344 | - if ($m[2]) { |
|
| 345 | - $colonnes = substr($m[2], 1, -1); |
|
| 346 | - $nom_index = $m[1]; |
|
| 347 | - } else { |
|
| 348 | - // (colonne) |
|
| 349 | - if ($m[1][0] == '(') { |
|
| 350 | - $colonnes = substr($m[1], 1, -1); |
|
| 351 | - if (str_contains(',', $colonnes)) { |
|
| 352 | - spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 353 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 354 | - } else { |
|
| 355 | - $nom_index = $colonnes; |
|
| 356 | - } |
|
| 357 | - } // nom_index |
|
| 358 | - else { |
|
| 359 | - $nom_index = $colonnes = $m[1]; |
|
| 360 | - } |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 364 | - } |
|
| 315 | + $nom_index = null; |
|
| 316 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 317 | + spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 318 | + |
|
| 319 | + return null; |
|
| 320 | + } |
|
| 321 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 322 | + preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 323 | + if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 324 | + $m[2] = $n[1]; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 328 | + } elseif ($r[1][0] == 'P') { |
|
| 329 | + // la primary peut etre sur plusieurs champs |
|
| 330 | + $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 331 | + $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 332 | + |
|
| 333 | + return spip_pg_query( |
|
| 334 | + "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 335 | + $serveur, |
|
| 336 | + $requeter |
|
| 337 | + ); |
|
| 338 | + } else { |
|
| 339 | + preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 340 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 341 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 342 | + // |
|
| 343 | + // nom_index (colonnes) |
|
| 344 | + if ($m[2]) { |
|
| 345 | + $colonnes = substr($m[2], 1, -1); |
|
| 346 | + $nom_index = $m[1]; |
|
| 347 | + } else { |
|
| 348 | + // (colonne) |
|
| 349 | + if ($m[1][0] == '(') { |
|
| 350 | + $colonnes = substr($m[1], 1, -1); |
|
| 351 | + if (str_contains(',', $colonnes)) { |
|
| 352 | + spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 353 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 354 | + } else { |
|
| 355 | + $nom_index = $colonnes; |
|
| 356 | + } |
|
| 357 | + } // nom_index |
|
| 358 | + else { |
|
| 359 | + $nom_index = $colonnes = $m[1]; |
|
| 360 | + } |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 364 | + } |
|
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 368 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 369 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 370 | - } else { |
|
| 371 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 372 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 373 | - } elseif ($r[1][0] == 'P') { |
|
| 374 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 375 | - } else { |
|
| 376 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 377 | - } |
|
| 378 | - } |
|
| 368 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 369 | + spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 370 | + } else { |
|
| 371 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 372 | + return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 373 | + } elseif ($r[1][0] == 'P') { |
|
| 374 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 375 | + } else { |
|
| 376 | + return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 382 | - if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 383 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 384 | - } else { |
|
| 385 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 386 | - } |
|
| 382 | + if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 383 | + spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 384 | + } else { |
|
| 385 | + return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 386 | + } |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | // attention (en pg) : |
@@ -391,17 +391,17 @@ discard block |
||
| 391 | 391 | // - alter table A rename X to Y = changer le nom de la colonne X en Y |
| 392 | 392 | // pour l'instant, traiter simplement RENAME TO X |
| 393 | 393 | function spip_pg_alter_rename($table, $arg, $serveur = '', $requeter = true) { |
| 394 | - $rename = ''; |
|
| 395 | - // si TO, mais pas au debut |
|
| 396 | - if (!stripos($arg, 'TO ')) { |
|
| 397 | - $rename = $arg; |
|
| 398 | - } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 399 | - $rename = $r[2]; |
|
| 400 | - } else { |
|
| 401 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 394 | + $rename = ''; |
|
| 395 | + // si TO, mais pas au debut |
|
| 396 | + if (!stripos($arg, 'TO ')) { |
|
| 397 | + $rename = $arg; |
|
| 398 | + } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 399 | + $rename = $r[2]; |
|
| 400 | + } else { |
|
| 401 | + spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 405 | 405 | } |
| 406 | 406 | |
| 407 | 407 | |
@@ -417,59 +417,59 @@ discard block |
||
| 417 | 417 | * @return bool ou requete |
| 418 | 418 | */ |
| 419 | 419 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 420 | - if (!($nom or $table or $champs)) { |
|
| 421 | - spip_log( |
|
| 422 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 423 | - 'pg.' . _LOG_ERREUR |
|
| 424 | - ); |
|
| 425 | - |
|
| 426 | - return false; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - $nom = str_replace('`', '', $nom); |
|
| 430 | - $champs = str_replace('`', '', $champs); |
|
| 431 | - |
|
| 432 | - // PG ne differentie pas noms des index en fonction des tables |
|
| 433 | - // il faut donc creer des noms uniques d'index pour une base pg |
|
| 434 | - $nom = $table . '_' . $nom; |
|
| 435 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 436 | - if (!is_array($champs)) { |
|
| 437 | - if ($champs[0] == '(') { |
|
| 438 | - $champs = substr($champs, 1, -1); |
|
| 439 | - } |
|
| 440 | - $champs = [$champs]; |
|
| 441 | - } |
|
| 442 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 443 | - if (!$requeter) { |
|
| 444 | - return $query; |
|
| 445 | - } |
|
| 446 | - $res = spip_pg_query($query, $serveur, $requeter); |
|
| 447 | - |
|
| 448 | - return $res; |
|
| 420 | + if (!($nom or $table or $champs)) { |
|
| 421 | + spip_log( |
|
| 422 | + "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 423 | + 'pg.' . _LOG_ERREUR |
|
| 424 | + ); |
|
| 425 | + |
|
| 426 | + return false; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + $nom = str_replace('`', '', $nom); |
|
| 430 | + $champs = str_replace('`', '', $champs); |
|
| 431 | + |
|
| 432 | + // PG ne differentie pas noms des index en fonction des tables |
|
| 433 | + // il faut donc creer des noms uniques d'index pour une base pg |
|
| 434 | + $nom = $table . '_' . $nom; |
|
| 435 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 436 | + if (!is_array($champs)) { |
|
| 437 | + if ($champs[0] == '(') { |
|
| 438 | + $champs = substr($champs, 1, -1); |
|
| 439 | + } |
|
| 440 | + $champs = [$champs]; |
|
| 441 | + } |
|
| 442 | + $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 443 | + if (!$requeter) { |
|
| 444 | + return $query; |
|
| 445 | + } |
|
| 446 | + $res = spip_pg_query($query, $serveur, $requeter); |
|
| 447 | + |
|
| 448 | + return $res; |
|
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | |
| 452 | 452 | function spip_pg_explain($query, $serveur = '', $requeter = true) { |
| 453 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 454 | - return []; |
|
| 455 | - } |
|
| 456 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 457 | - $prefixe = $connexion['prefixe']; |
|
| 458 | - $link = $connexion['link']; |
|
| 459 | - if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 460 | - $suite = strstr($query, (string) $regs[0]); |
|
| 461 | - $query = substr($query, 0, -strlen($suite)); |
|
| 462 | - } else { |
|
| 463 | - $suite = ''; |
|
| 464 | - } |
|
| 465 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 466 | - |
|
| 467 | - if (!$requeter) { |
|
| 468 | - return $query; |
|
| 469 | - } |
|
| 470 | - $r = spip_pg_query_simple($link, $query); |
|
| 471 | - |
|
| 472 | - return spip_pg_fetch($r, null, $serveur); |
|
| 453 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 454 | + return []; |
|
| 455 | + } |
|
| 456 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 457 | + $prefixe = $connexion['prefixe']; |
|
| 458 | + $link = $connexion['link']; |
|
| 459 | + if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 460 | + $suite = strstr($query, (string) $regs[0]); |
|
| 461 | + $query = substr($query, 0, -strlen($suite)); |
|
| 462 | + } else { |
|
| 463 | + $suite = ''; |
|
| 464 | + } |
|
| 465 | + $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 466 | + |
|
| 467 | + if (!$requeter) { |
|
| 468 | + return $query; |
|
| 469 | + } |
|
| 470 | + $r = spip_pg_query_simple($link, $query); |
|
| 471 | + |
|
| 472 | + return spip_pg_fetch($r, null, $serveur); |
|
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | |
@@ -488,92 +488,92 @@ discard block |
||
| 488 | 488 | * - False en cas d'erreur. |
| 489 | 489 | **/ |
| 490 | 490 | function spip_pg_selectdb($db, $serveur = '', $requeter = true) { |
| 491 | - // se connecter a la base indiquee |
|
| 492 | - // avec les identifiants connus |
|
| 493 | - $index = $serveur ? strtolower($serveur) : 0; |
|
| 491 | + // se connecter a la base indiquee |
|
| 492 | + // avec les identifiants connus |
|
| 493 | + $index = $serveur ? strtolower($serveur) : 0; |
|
| 494 | 494 | |
| 495 | - if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 496 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 497 | - return $db; |
|
| 498 | - } |
|
| 499 | - } |
|
| 495 | + if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 496 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 497 | + return $db; |
|
| 498 | + } |
|
| 499 | + } |
|
| 500 | 500 | |
| 501 | - return false; |
|
| 501 | + return false; |
|
| 502 | 502 | } |
| 503 | 503 | |
| 504 | 504 | // Qu'une seule base pour le moment |
| 505 | 505 | |
| 506 | 506 | function spip_pg_listdbs($serveur) { |
| 507 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 508 | - $link = $connexion['link']; |
|
| 509 | - $dbs = []; |
|
| 510 | - $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 511 | - while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 512 | - $dbs[] = reset($row); |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - return $dbs; |
|
| 507 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 508 | + $link = $connexion['link']; |
|
| 509 | + $dbs = []; |
|
| 510 | + $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 511 | + while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 512 | + $dbs[] = reset($row); |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + return $dbs; |
|
| 516 | 516 | } |
| 517 | 517 | |
| 518 | 518 | function spip_pg_select( |
| 519 | - $select, |
|
| 520 | - $from, |
|
| 521 | - $where = '', |
|
| 522 | - $groupby = [], |
|
| 523 | - $orderby = '', |
|
| 524 | - $limit = '', |
|
| 525 | - $having = '', |
|
| 526 | - $serveur = '', |
|
| 527 | - $requeter = true |
|
| 519 | + $select, |
|
| 520 | + $from, |
|
| 521 | + $where = '', |
|
| 522 | + $groupby = [], |
|
| 523 | + $orderby = '', |
|
| 524 | + $limit = '', |
|
| 525 | + $having = '', |
|
| 526 | + $serveur = '', |
|
| 527 | + $requeter = true |
|
| 528 | 528 | ) { |
| 529 | 529 | |
| 530 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 531 | - $prefixe = $connexion['prefixe']; |
|
| 532 | - $link = $connexion['link']; |
|
| 533 | - $db = $connexion['db']; |
|
| 534 | - |
|
| 535 | - $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 536 | - if ($limit) { |
|
| 537 | - $offset = $limatch[2]; |
|
| 538 | - $count = $limatch[3]; |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - $select = spip_pg_frommysql($select); |
|
| 542 | - |
|
| 543 | - // si pas de tri explicitement demande, le GROUP BY ne |
|
| 544 | - // contient que la clef primaire. |
|
| 545 | - // lui ajouter alors le champ de tri par defaut |
|
| 546 | - if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 547 | - $groupby[] = $groupbyplus[1]; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - $orderby = spip_pg_orderby($orderby, $select); |
|
| 551 | - |
|
| 552 | - if ($having) { |
|
| 553 | - if (is_array($having)) { |
|
| 554 | - $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 555 | - } |
|
| 556 | - } |
|
| 557 | - $from = spip_pg_from($from, $prefixe); |
|
| 558 | - $query = 'SELECT ' . $select |
|
| 559 | - . (!$from ? '' : "\nFROM $from") |
|
| 560 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 561 | - "\n\tAND ", |
|
| 562 | - array_map('calculer_pg_where', $where) |
|
| 563 | - ))))) |
|
| 564 | - . spip_pg_groupby($groupby, $from, $select) |
|
| 565 | - . (!$having ? '' : "\nHAVING $having") |
|
| 566 | - . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 567 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 568 | - |
|
| 569 | - // renvoyer la requete inerte si demandee |
|
| 570 | - if ($requeter === false) { |
|
| 571 | - return $query; |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - $r = spip_pg_trace_query($query, $serveur); |
|
| 575 | - |
|
| 576 | - return $r ?: $query; |
|
| 530 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 531 | + $prefixe = $connexion['prefixe']; |
|
| 532 | + $link = $connexion['link']; |
|
| 533 | + $db = $connexion['db']; |
|
| 534 | + |
|
| 535 | + $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 536 | + if ($limit) { |
|
| 537 | + $offset = $limatch[2]; |
|
| 538 | + $count = $limatch[3]; |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + $select = spip_pg_frommysql($select); |
|
| 542 | + |
|
| 543 | + // si pas de tri explicitement demande, le GROUP BY ne |
|
| 544 | + // contient que la clef primaire. |
|
| 545 | + // lui ajouter alors le champ de tri par defaut |
|
| 546 | + if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 547 | + $groupby[] = $groupbyplus[1]; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + $orderby = spip_pg_orderby($orderby, $select); |
|
| 551 | + |
|
| 552 | + if ($having) { |
|
| 553 | + if (is_array($having)) { |
|
| 554 | + $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 555 | + } |
|
| 556 | + } |
|
| 557 | + $from = spip_pg_from($from, $prefixe); |
|
| 558 | + $query = 'SELECT ' . $select |
|
| 559 | + . (!$from ? '' : "\nFROM $from") |
|
| 560 | + . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 561 | + "\n\tAND ", |
|
| 562 | + array_map('calculer_pg_where', $where) |
|
| 563 | + ))))) |
|
| 564 | + . spip_pg_groupby($groupby, $from, $select) |
|
| 565 | + . (!$having ? '' : "\nHAVING $having") |
|
| 566 | + . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 567 | + . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 568 | + |
|
| 569 | + // renvoyer la requete inerte si demandee |
|
| 570 | + if ($requeter === false) { |
|
| 571 | + return $query; |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + $r = spip_pg_trace_query($query, $serveur); |
|
| 575 | + |
|
| 576 | + return $r ?: $query; |
|
| 577 | 577 | ; |
| 578 | 578 | } |
| 579 | 579 | |
@@ -581,26 +581,26 @@ discard block |
||
| 581 | 581 | // car le reste de la requete utilise les alias (AS) systematiquement |
| 582 | 582 | |
| 583 | 583 | function spip_pg_from($from, $prefixe) { |
| 584 | - if (is_array($from)) { |
|
| 585 | - $from = spip_pg_select_as($from); |
|
| 586 | - } |
|
| 584 | + if (is_array($from)) { |
|
| 585 | + $from = spip_pg_select_as($from); |
|
| 586 | + } |
|
| 587 | 587 | |
| 588 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 588 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 589 | 589 | } |
| 590 | 590 | |
| 591 | 591 | function spip_pg_orderby($order, $select) { |
| 592 | - $res = []; |
|
| 593 | - $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 594 | - |
|
| 595 | - foreach ($arg as $v) { |
|
| 596 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 597 | - $res[] = $m[1]; |
|
| 598 | - } else { |
|
| 599 | - $res[] = $v; |
|
| 600 | - } |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - return spip_pg_frommysql(join(',', $res)); |
|
| 592 | + $res = []; |
|
| 593 | + $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 594 | + |
|
| 595 | + foreach ($arg as $v) { |
|
| 596 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 597 | + $res[] = $m[1]; |
|
| 598 | + } else { |
|
| 599 | + $res[] = $v; |
|
| 600 | + } |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + return spip_pg_frommysql(join(',', $res)); |
|
| 604 | 604 | } |
| 605 | 605 | |
| 606 | 606 | // Conversion a l'arrach' des jointures MySQL en jointures PG |
@@ -608,56 +608,56 @@ discard block |
||
| 608 | 608 | // et pour enlever les repetitions (sans incidence de perf, mais ca fait sale) |
| 609 | 609 | |
| 610 | 610 | function spip_pg_groupby($groupby, $from, $select) { |
| 611 | - $join = strpos($from, ','); |
|
| 612 | - // ismplifier avant de decouper |
|
| 613 | - if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 614 | - $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - if ($join or $groupby) { |
|
| 618 | - $join = is_array($select) ? $select : explode(', ', $select); |
|
| 619 | - } |
|
| 620 | - if ($join) { |
|
| 621 | - // enlever les 0 as points, '', ... |
|
| 622 | - foreach ($join as $k => $v) { |
|
| 623 | - $v = str_replace('DISTINCT ', '', $v); |
|
| 624 | - // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 625 | - $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 626 | - $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 627 | - // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 628 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 629 | - // idem sans AS (fetch numerique) |
|
| 630 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 631 | - // des AS simples : on garde le cote droit du AS |
|
| 632 | - $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 633 | - // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 634 | - if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 635 | - unset($join[$k]); |
|
| 636 | - } else { |
|
| 637 | - $join[$k] = trim($v); |
|
| 638 | - } |
|
| 639 | - } |
|
| 640 | - $join = array_diff($join, ['']); |
|
| 641 | - $join = implode(',', $join); |
|
| 642 | - } |
|
| 643 | - if (is_array($groupby)) { |
|
| 644 | - $groupby = join(',', $groupby); |
|
| 645 | - } |
|
| 646 | - if ($join) { |
|
| 647 | - $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 648 | - } |
|
| 649 | - if (!$groupby) { |
|
| 650 | - return ''; |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - $groupby = spip_pg_frommysql($groupby); |
|
| 654 | - // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 655 | - // issue de prepare_recherche |
|
| 656 | - $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 657 | - $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 658 | - $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 659 | - |
|
| 660 | - return "\nGROUP BY $groupby"; |
|
| 611 | + $join = strpos($from, ','); |
|
| 612 | + // ismplifier avant de decouper |
|
| 613 | + if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 614 | + $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + if ($join or $groupby) { |
|
| 618 | + $join = is_array($select) ? $select : explode(', ', $select); |
|
| 619 | + } |
|
| 620 | + if ($join) { |
|
| 621 | + // enlever les 0 as points, '', ... |
|
| 622 | + foreach ($join as $k => $v) { |
|
| 623 | + $v = str_replace('DISTINCT ', '', $v); |
|
| 624 | + // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 625 | + $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 626 | + $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 627 | + // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 628 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 629 | + // idem sans AS (fetch numerique) |
|
| 630 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 631 | + // des AS simples : on garde le cote droit du AS |
|
| 632 | + $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 633 | + // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 634 | + if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 635 | + unset($join[$k]); |
|
| 636 | + } else { |
|
| 637 | + $join[$k] = trim($v); |
|
| 638 | + } |
|
| 639 | + } |
|
| 640 | + $join = array_diff($join, ['']); |
|
| 641 | + $join = implode(',', $join); |
|
| 642 | + } |
|
| 643 | + if (is_array($groupby)) { |
|
| 644 | + $groupby = join(',', $groupby); |
|
| 645 | + } |
|
| 646 | + if ($join) { |
|
| 647 | + $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 648 | + } |
|
| 649 | + if (!$groupby) { |
|
| 650 | + return ''; |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + $groupby = spip_pg_frommysql($groupby); |
|
| 654 | + // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 655 | + // issue de prepare_recherche |
|
| 656 | + $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 657 | + $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 658 | + $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 659 | + |
|
| 660 | + return "\nGROUP BY $groupby"; |
|
| 661 | 661 | } |
| 662 | 662 | |
| 663 | 663 | // Conversion des operateurs MySQL en PG |
@@ -668,492 +668,492 @@ discard block |
||
| 668 | 668 | // A ameliorer. |
| 669 | 669 | |
| 670 | 670 | function spip_pg_frommysql($arg) { |
| 671 | - if (is_array($arg)) { |
|
| 672 | - $arg = join(', ', $arg); |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - $res = spip_pg_fromfield($arg); |
|
| 676 | - |
|
| 677 | - $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 678 | - |
|
| 679 | - $res = preg_replace( |
|
| 680 | - '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 681 | - 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 682 | - $res |
|
| 683 | - ); |
|
| 684 | - $res = preg_replace( |
|
| 685 | - '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 686 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 687 | - $res |
|
| 688 | - ); |
|
| 689 | - $res = preg_replace( |
|
| 690 | - '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 691 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 692 | - $res |
|
| 693 | - ); |
|
| 694 | - |
|
| 695 | - $res = preg_replace( |
|
| 696 | - '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 697 | - ' EXTRACT(epoch FROM NOW())', |
|
| 698 | - $res |
|
| 699 | - ); |
|
| 700 | - |
|
| 701 | - // la fonction md5(integer) n'est pas connu en pg |
|
| 702 | - // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 703 | - $res = preg_replace( |
|
| 704 | - '/md5\s*[(]([^)]*)[)]/i', |
|
| 705 | - 'MD5(CAST(\1 AS text))', |
|
| 706 | - $res |
|
| 707 | - ); |
|
| 708 | - |
|
| 709 | - $res = preg_replace( |
|
| 710 | - '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 711 | - ' EXTRACT(epoch FROM \1)', |
|
| 712 | - $res |
|
| 713 | - ); |
|
| 714 | - |
|
| 715 | - $res = preg_replace( |
|
| 716 | - '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 717 | - ' EXTRACT(day FROM \1)', |
|
| 718 | - $res |
|
| 719 | - ); |
|
| 720 | - |
|
| 721 | - $res = preg_replace( |
|
| 722 | - '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 723 | - ' EXTRACT(month FROM \1)', |
|
| 724 | - $res |
|
| 725 | - ); |
|
| 726 | - |
|
| 727 | - $res = preg_replace( |
|
| 728 | - '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 729 | - ' EXTRACT(year FROM \1)', |
|
| 730 | - $res |
|
| 731 | - ); |
|
| 732 | - |
|
| 733 | - $res = preg_replace( |
|
| 734 | - '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 735 | - ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 736 | - $res |
|
| 737 | - ); |
|
| 738 | - |
|
| 739 | - $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 740 | - |
|
| 741 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 742 | - |
|
| 743 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 744 | - |
|
| 745 | - $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 746 | - $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 747 | - $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 748 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 749 | - $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 750 | - |
|
| 751 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 752 | - $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 753 | - |
|
| 754 | - $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 755 | - $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 671 | + if (is_array($arg)) { |
|
| 672 | + $arg = join(', ', $arg); |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + $res = spip_pg_fromfield($arg); |
|
| 676 | + |
|
| 677 | + $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 678 | + |
|
| 679 | + $res = preg_replace( |
|
| 680 | + '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 681 | + 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 682 | + $res |
|
| 683 | + ); |
|
| 684 | + $res = preg_replace( |
|
| 685 | + '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 686 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 687 | + $res |
|
| 688 | + ); |
|
| 689 | + $res = preg_replace( |
|
| 690 | + '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 691 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 692 | + $res |
|
| 693 | + ); |
|
| 694 | + |
|
| 695 | + $res = preg_replace( |
|
| 696 | + '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 697 | + ' EXTRACT(epoch FROM NOW())', |
|
| 698 | + $res |
|
| 699 | + ); |
|
| 700 | + |
|
| 701 | + // la fonction md5(integer) n'est pas connu en pg |
|
| 702 | + // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 703 | + $res = preg_replace( |
|
| 704 | + '/md5\s*[(]([^)]*)[)]/i', |
|
| 705 | + 'MD5(CAST(\1 AS text))', |
|
| 706 | + $res |
|
| 707 | + ); |
|
| 708 | + |
|
| 709 | + $res = preg_replace( |
|
| 710 | + '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 711 | + ' EXTRACT(epoch FROM \1)', |
|
| 712 | + $res |
|
| 713 | + ); |
|
| 714 | + |
|
| 715 | + $res = preg_replace( |
|
| 716 | + '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 717 | + ' EXTRACT(day FROM \1)', |
|
| 718 | + $res |
|
| 719 | + ); |
|
| 720 | + |
|
| 721 | + $res = preg_replace( |
|
| 722 | + '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 723 | + ' EXTRACT(month FROM \1)', |
|
| 724 | + $res |
|
| 725 | + ); |
|
| 726 | + |
|
| 727 | + $res = preg_replace( |
|
| 728 | + '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 729 | + ' EXTRACT(year FROM \1)', |
|
| 730 | + $res |
|
| 731 | + ); |
|
| 732 | + |
|
| 733 | + $res = preg_replace( |
|
| 734 | + '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 735 | + ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 736 | + $res |
|
| 737 | + ); |
|
| 738 | + |
|
| 739 | + $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 740 | + |
|
| 741 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 742 | + |
|
| 743 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 744 | + |
|
| 745 | + $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 746 | + $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 747 | + $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 748 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 749 | + $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 750 | + |
|
| 751 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 752 | + $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 753 | + |
|
| 754 | + $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 755 | + $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 756 | 756 | # correct en theorie mais produit des debordements arithmetiques |
| 757 | 757 | # $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)(timestamp *'[^']*' *[+-] *timestamp *'[^']*') *[)]/", '\2', $res); |
| 758 | - $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 759 | - $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 758 | + $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 759 | + $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 760 | 760 | |
| 761 | - return str_replace('REGEXP', '~', $res); |
|
| 761 | + return str_replace('REGEXP', '~', $res); |
|
| 762 | 762 | } |
| 763 | 763 | |
| 764 | 764 | function spip_pg_fromfield($arg) { |
| 765 | - while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 766 | - preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 767 | - $res = ''; |
|
| 768 | - $n = 0; |
|
| 769 | - $index = $m[2]; |
|
| 770 | - foreach ($r[1] as $v) { |
|
| 771 | - $n++; |
|
| 772 | - $res .= "\nwhen $index=$v then $n"; |
|
| 773 | - } |
|
| 774 | - $arg = $m[1] . "case $res else 0 end " |
|
| 775 | - . substr($arg, strlen($m[0])); |
|
| 776 | - } |
|
| 777 | - |
|
| 778 | - return $arg; |
|
| 765 | + while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 766 | + preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 767 | + $res = ''; |
|
| 768 | + $n = 0; |
|
| 769 | + $index = $m[2]; |
|
| 770 | + foreach ($r[1] as $v) { |
|
| 771 | + $n++; |
|
| 772 | + $res .= "\nwhen $index=$v then $n"; |
|
| 773 | + } |
|
| 774 | + $arg = $m[1] . "case $res else 0 end " |
|
| 775 | + . substr($arg, strlen($m[0])); |
|
| 776 | + } |
|
| 777 | + |
|
| 778 | + return $arg; |
|
| 779 | 779 | } |
| 780 | 780 | |
| 781 | 781 | function calculer_pg_where($v) { |
| 782 | - if (!is_array($v)) { |
|
| 783 | - return spip_pg_frommysql($v); |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 787 | - if (!($n = count($v))) { |
|
| 788 | - return $op; |
|
| 789 | - } else { |
|
| 790 | - $arg = calculer_pg_where(array_shift($v)); |
|
| 791 | - if ($n == 1) { |
|
| 792 | - return "$op($arg)"; |
|
| 793 | - } else { |
|
| 794 | - $arg2 = calculer_pg_where(array_shift($v)); |
|
| 795 | - if ($n == 2) { |
|
| 796 | - return "($arg $op $arg2)"; |
|
| 797 | - } else { |
|
| 798 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 799 | - } |
|
| 800 | - } |
|
| 801 | - } |
|
| 782 | + if (!is_array($v)) { |
|
| 783 | + return spip_pg_frommysql($v); |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 787 | + if (!($n = count($v))) { |
|
| 788 | + return $op; |
|
| 789 | + } else { |
|
| 790 | + $arg = calculer_pg_where(array_shift($v)); |
|
| 791 | + if ($n == 1) { |
|
| 792 | + return "$op($arg)"; |
|
| 793 | + } else { |
|
| 794 | + $arg2 = calculer_pg_where(array_shift($v)); |
|
| 795 | + if ($n == 2) { |
|
| 796 | + return "($arg $op $arg2)"; |
|
| 797 | + } else { |
|
| 798 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 799 | + } |
|
| 800 | + } |
|
| 801 | + } |
|
| 802 | 802 | } |
| 803 | 803 | |
| 804 | 804 | |
| 805 | 805 | function calculer_pg_expression($expression, $v, $join = 'AND') { |
| 806 | - if (empty($v)) { |
|
| 807 | - return ''; |
|
| 808 | - } |
|
| 806 | + if (empty($v)) { |
|
| 807 | + return ''; |
|
| 808 | + } |
|
| 809 | 809 | |
| 810 | - $exp = "\n$expression "; |
|
| 810 | + $exp = "\n$expression "; |
|
| 811 | 811 | |
| 812 | - if (!is_array($v)) { |
|
| 813 | - $v = [$v]; |
|
| 814 | - } |
|
| 812 | + if (!is_array($v)) { |
|
| 813 | + $v = [$v]; |
|
| 814 | + } |
|
| 815 | 815 | |
| 816 | - if (strtoupper($join) === 'AND') { |
|
| 817 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 818 | - } else { |
|
| 819 | - return $exp . join($join, $v); |
|
| 820 | - } |
|
| 816 | + if (strtoupper($join) === 'AND') { |
|
| 817 | + return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 818 | + } else { |
|
| 819 | + return $exp . join($join, $v); |
|
| 820 | + } |
|
| 821 | 821 | } |
| 822 | 822 | |
| 823 | 823 | function spip_pg_select_as($args) { |
| 824 | - $argsas = ''; |
|
| 825 | - foreach ($args as $k => $v) { |
|
| 826 | - if (substr($k, -1) == '@') { |
|
| 827 | - // c'est une jointure qui se refere au from precedent |
|
| 828 | - // pas de virgule |
|
| 829 | - $argsas .= ' ' . $v; |
|
| 830 | - } else { |
|
| 831 | - $as = ''; |
|
| 832 | - // spip_log("$k : $v", _LOG_DEBUG); |
|
| 833 | - if (!is_numeric($k)) { |
|
| 834 | - if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 835 | - $v = $k; |
|
| 836 | - } elseif ($v != $k) { |
|
| 837 | - $p = strpos($v, ' '); |
|
| 838 | - if ($p) { |
|
| 839 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 840 | - } else { |
|
| 841 | - $as = " AS $k"; |
|
| 842 | - } |
|
| 843 | - } |
|
| 844 | - } |
|
| 845 | - // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 846 | - // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 847 | - $argsas .= ', ' . $v . $as; |
|
| 848 | - } |
|
| 849 | - } |
|
| 850 | - |
|
| 851 | - return substr($argsas, 2); |
|
| 824 | + $argsas = ''; |
|
| 825 | + foreach ($args as $k => $v) { |
|
| 826 | + if (substr($k, -1) == '@') { |
|
| 827 | + // c'est une jointure qui se refere au from precedent |
|
| 828 | + // pas de virgule |
|
| 829 | + $argsas .= ' ' . $v; |
|
| 830 | + } else { |
|
| 831 | + $as = ''; |
|
| 832 | + // spip_log("$k : $v", _LOG_DEBUG); |
|
| 833 | + if (!is_numeric($k)) { |
|
| 834 | + if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 835 | + $v = $k; |
|
| 836 | + } elseif ($v != $k) { |
|
| 837 | + $p = strpos($v, ' '); |
|
| 838 | + if ($p) { |
|
| 839 | + $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 840 | + } else { |
|
| 841 | + $as = " AS $k"; |
|
| 842 | + } |
|
| 843 | + } |
|
| 844 | + } |
|
| 845 | + // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 846 | + // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 847 | + $argsas .= ', ' . $v . $as; |
|
| 848 | + } |
|
| 849 | + } |
|
| 850 | + |
|
| 851 | + return substr($argsas, 2); |
|
| 852 | 852 | } |
| 853 | 853 | |
| 854 | 854 | function spip_pg_fetch($res, $t = '', $serveur = '', $requeter = true) { |
| 855 | 855 | |
| 856 | - if ($res) { |
|
| 857 | - $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 858 | - } |
|
| 856 | + if ($res) { |
|
| 857 | + $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 858 | + } |
|
| 859 | 859 | |
| 860 | - return $res; |
|
| 860 | + return $res; |
|
| 861 | 861 | } |
| 862 | 862 | |
| 863 | 863 | function spip_pg_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 864 | - if ($r) { |
|
| 865 | - return pg_result_seek($r, $row_number); |
|
| 866 | - } |
|
| 864 | + if ($r) { |
|
| 865 | + return pg_result_seek($r, $row_number); |
|
| 866 | + } |
|
| 867 | 867 | } |
| 868 | 868 | |
| 869 | 869 | |
| 870 | 870 | function spip_pg_countsel( |
| 871 | - $from = [], |
|
| 872 | - $where = [], |
|
| 873 | - $groupby = [], |
|
| 874 | - $having = [], |
|
| 875 | - $serveur = '', |
|
| 876 | - $requeter = true |
|
| 871 | + $from = [], |
|
| 872 | + $where = [], |
|
| 873 | + $groupby = [], |
|
| 874 | + $having = [], |
|
| 875 | + $serveur = '', |
|
| 876 | + $requeter = true |
|
| 877 | 877 | ) { |
| 878 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 879 | - $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 880 | - if (!$requeter) { |
|
| 881 | - return $r; |
|
| 882 | - } |
|
| 883 | - if (!is_resource($r)) { |
|
| 884 | - return 0; |
|
| 885 | - } |
|
| 886 | - [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 887 | - |
|
| 888 | - return $c; |
|
| 878 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 879 | + $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 880 | + if (!$requeter) { |
|
| 881 | + return $r; |
|
| 882 | + } |
|
| 883 | + if (!is_resource($r)) { |
|
| 884 | + return 0; |
|
| 885 | + } |
|
| 886 | + [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 887 | + |
|
| 888 | + return $c; |
|
| 889 | 889 | } |
| 890 | 890 | |
| 891 | 891 | function spip_pg_count($res, $serveur = '', $requeter = true) { |
| 892 | - return !$res ? 0 : pg_numrows($res); |
|
| 892 | + return !$res ? 0 : pg_numrows($res); |
|
| 893 | 893 | } |
| 894 | 894 | |
| 895 | 895 | function spip_pg_free($res, $serveur = '', $requeter = true) { |
| 896 | - // rien a faire en postgres |
|
| 896 | + // rien a faire en postgres |
|
| 897 | 897 | } |
| 898 | 898 | |
| 899 | 899 | function spip_pg_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 900 | 900 | |
| 901 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 902 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 901 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 902 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 903 | 903 | |
| 904 | - $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 905 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 904 | + $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 905 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 906 | 906 | |
| 907 | - // renvoyer la requete inerte si demandee |
|
| 908 | - if (!$requeter) { |
|
| 909 | - return $query; |
|
| 910 | - } |
|
| 907 | + // renvoyer la requete inerte si demandee |
|
| 908 | + if (!$requeter) { |
|
| 909 | + return $query; |
|
| 910 | + } |
|
| 911 | 911 | |
| 912 | - $res = spip_pg_trace_query($query, $serveur); |
|
| 913 | - if ($res) { |
|
| 914 | - return pg_affected_rows($res); |
|
| 915 | - } else { |
|
| 916 | - return false; |
|
| 917 | - } |
|
| 912 | + $res = spip_pg_trace_query($query, $serveur); |
|
| 913 | + if ($res) { |
|
| 914 | + return pg_affected_rows($res); |
|
| 915 | + } else { |
|
| 916 | + return false; |
|
| 917 | + } |
|
| 918 | 918 | } |
| 919 | 919 | |
| 920 | 920 | function spip_pg_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 921 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 922 | - $prefixe = $connexion['prefixe']; |
|
| 923 | - $link = $connexion['link']; |
|
| 924 | - |
|
| 925 | - if (!$desc) { |
|
| 926 | - $desc = description_table($table, $serveur); |
|
| 927 | - } |
|
| 928 | - $seq = spip_pg_sequence($table, true); |
|
| 929 | - // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 930 | - if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 931 | - $seq = spip_pg_sequence($table); |
|
| 932 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 933 | - $seq = "currval('$seq')"; |
|
| 934 | - } |
|
| 935 | - |
|
| 936 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 937 | - $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 938 | - $ins = (strlen($champs) < 3) |
|
| 939 | - ? ' DEFAULT VALUES' |
|
| 940 | - : "$champs VALUES $valeurs"; |
|
| 941 | - $q = "INSERT INTO $table $ins $ret"; |
|
| 942 | - if (!$requeter) { |
|
| 943 | - return $q; |
|
| 944 | - } |
|
| 945 | - $connexion['last'] = $q; |
|
| 946 | - $r = spip_pg_query_simple($link, $q); |
|
| 921 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 922 | + $prefixe = $connexion['prefixe']; |
|
| 923 | + $link = $connexion['link']; |
|
| 924 | + |
|
| 925 | + if (!$desc) { |
|
| 926 | + $desc = description_table($table, $serveur); |
|
| 927 | + } |
|
| 928 | + $seq = spip_pg_sequence($table, true); |
|
| 929 | + // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 930 | + if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 931 | + $seq = spip_pg_sequence($table); |
|
| 932 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 933 | + $seq = "currval('$seq')"; |
|
| 934 | + } |
|
| 935 | + |
|
| 936 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 937 | + $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 938 | + $ins = (strlen($champs) < 3) |
|
| 939 | + ? ' DEFAULT VALUES' |
|
| 940 | + : "$champs VALUES $valeurs"; |
|
| 941 | + $q = "INSERT INTO $table $ins $ret"; |
|
| 942 | + if (!$requeter) { |
|
| 943 | + return $q; |
|
| 944 | + } |
|
| 945 | + $connexion['last'] = $q; |
|
| 946 | + $r = spip_pg_query_simple($link, $q); |
|
| 947 | 947 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 948 | - if ($r) { |
|
| 949 | - if (!$ret) { |
|
| 950 | - return 0; |
|
| 951 | - } |
|
| 952 | - if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 953 | - return $r2[0]; |
|
| 954 | - } |
|
| 955 | - } |
|
| 956 | - |
|
| 957 | - return false; |
|
| 948 | + if ($r) { |
|
| 949 | + if (!$ret) { |
|
| 950 | + return 0; |
|
| 951 | + } |
|
| 952 | + if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 953 | + return $r2[0]; |
|
| 954 | + } |
|
| 955 | + } |
|
| 956 | + |
|
| 957 | + return false; |
|
| 958 | 958 | } |
| 959 | 959 | |
| 960 | 960 | function spip_pg_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 961 | 961 | |
| 962 | - if (!$desc) { |
|
| 963 | - $desc = description_table($table, $serveur); |
|
| 964 | - } |
|
| 965 | - if (!$desc) { |
|
| 966 | - die("$table insertion sans description"); |
|
| 967 | - } |
|
| 968 | - $fields = $desc['field']; |
|
| 969 | - |
|
| 970 | - foreach ($couples as $champ => $val) { |
|
| 971 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 972 | - } |
|
| 973 | - |
|
| 974 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 975 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 976 | - |
|
| 977 | - return spip_pg_insert( |
|
| 978 | - $table, |
|
| 979 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 980 | - '(' . join(',', $couples) . ')', |
|
| 981 | - $desc, |
|
| 982 | - $serveur, |
|
| 983 | - $requeter |
|
| 984 | - ); |
|
| 962 | + if (!$desc) { |
|
| 963 | + $desc = description_table($table, $serveur); |
|
| 964 | + } |
|
| 965 | + if (!$desc) { |
|
| 966 | + die("$table insertion sans description"); |
|
| 967 | + } |
|
| 968 | + $fields = $desc['field']; |
|
| 969 | + |
|
| 970 | + foreach ($couples as $champ => $val) { |
|
| 971 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 972 | + } |
|
| 973 | + |
|
| 974 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 975 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 976 | + |
|
| 977 | + return spip_pg_insert( |
|
| 978 | + $table, |
|
| 979 | + '(' . join(',', array_keys($couples)) . ')', |
|
| 980 | + '(' . join(',', $couples) . ')', |
|
| 981 | + $desc, |
|
| 982 | + $serveur, |
|
| 983 | + $requeter |
|
| 984 | + ); |
|
| 985 | 985 | } |
| 986 | 986 | |
| 987 | 987 | |
| 988 | 988 | function spip_pg_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 989 | 989 | |
| 990 | - if (!$desc) { |
|
| 991 | - $desc = description_table($table, $serveur); |
|
| 992 | - } |
|
| 993 | - if (!$desc) { |
|
| 994 | - die("$table insertion sans description"); |
|
| 995 | - } |
|
| 996 | - $fields = $desc['field'] ?? []; |
|
| 997 | - |
|
| 998 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 999 | - // une premiere fois pour ajouter maj dans les cles |
|
| 1000 | - $c = $tab_couples[0] ?? []; |
|
| 1001 | - $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1002 | - |
|
| 1003 | - $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1004 | - $valeurs = []; |
|
| 1005 | - foreach ($tab_couples as $couples) { |
|
| 1006 | - foreach ($couples as $champ => $val) { |
|
| 1007 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1008 | - } |
|
| 1009 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1010 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1011 | - |
|
| 1012 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1013 | - } |
|
| 1014 | - $valeurs = implode(', ', $valeurs); |
|
| 1015 | - |
|
| 1016 | - return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 990 | + if (!$desc) { |
|
| 991 | + $desc = description_table($table, $serveur); |
|
| 992 | + } |
|
| 993 | + if (!$desc) { |
|
| 994 | + die("$table insertion sans description"); |
|
| 995 | + } |
|
| 996 | + $fields = $desc['field'] ?? []; |
|
| 997 | + |
|
| 998 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 999 | + // une premiere fois pour ajouter maj dans les cles |
|
| 1000 | + $c = $tab_couples[0] ?? []; |
|
| 1001 | + $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1002 | + |
|
| 1003 | + $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1004 | + $valeurs = []; |
|
| 1005 | + foreach ($tab_couples as $couples) { |
|
| 1006 | + foreach ($couples as $champ => $val) { |
|
| 1007 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1008 | + } |
|
| 1009 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1010 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1011 | + |
|
| 1012 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1013 | + } |
|
| 1014 | + $valeurs = implode(', ', $valeurs); |
|
| 1015 | + |
|
| 1016 | + return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1017 | 1017 | } |
| 1018 | 1018 | |
| 1019 | 1019 | |
| 1020 | 1020 | function spip_pg_update($table, $couples, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1021 | 1021 | |
| 1022 | - if (!$couples) { |
|
| 1023 | - return; |
|
| 1024 | - } |
|
| 1025 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1026 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1022 | + if (!$couples) { |
|
| 1023 | + return; |
|
| 1024 | + } |
|
| 1025 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1026 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1027 | 1027 | |
| 1028 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1029 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1028 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1029 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1030 | 1030 | |
| 1031 | - $set = []; |
|
| 1032 | - foreach ($couples as $champ => $val) { |
|
| 1033 | - $set[] = $champ . '=' . $val; |
|
| 1034 | - } |
|
| 1031 | + $set = []; |
|
| 1032 | + foreach ($couples as $champ => $val) { |
|
| 1033 | + $set[] = $champ . '=' . $val; |
|
| 1034 | + } |
|
| 1035 | 1035 | |
| 1036 | - $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1037 | - . calculer_pg_expression('SET', $set, ',') |
|
| 1038 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1036 | + $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1037 | + . calculer_pg_expression('SET', $set, ',') |
|
| 1038 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1039 | 1039 | |
| 1040 | - // renvoyer la requete inerte si demandee |
|
| 1041 | - if (!$requeter) { |
|
| 1042 | - return $query; |
|
| 1043 | - } |
|
| 1040 | + // renvoyer la requete inerte si demandee |
|
| 1041 | + if (!$requeter) { |
|
| 1042 | + return $query; |
|
| 1043 | + } |
|
| 1044 | 1044 | |
| 1045 | - return spip_pg_trace_query($query, $serveur); |
|
| 1045 | + return spip_pg_trace_query($query, $serveur); |
|
| 1046 | 1046 | } |
| 1047 | 1047 | |
| 1048 | 1048 | // idem, mais les valeurs sont des constantes a mettre entre apostrophes |
| 1049 | 1049 | // sauf les expressions de date lorsqu'il s'agit de fonctions SQL (NOW etc) |
| 1050 | 1050 | function spip_pg_updateq($table, $couples, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1051 | - if (!$couples) { |
|
| 1052 | - return; |
|
| 1053 | - } |
|
| 1054 | - if (!$desc) { |
|
| 1055 | - $desc = description_table($table, $serveur); |
|
| 1056 | - } |
|
| 1057 | - $fields = $desc['field']; |
|
| 1058 | - foreach ($couples as $k => $val) { |
|
| 1059 | - $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1060 | - } |
|
| 1061 | - |
|
| 1062 | - return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1051 | + if (!$couples) { |
|
| 1052 | + return; |
|
| 1053 | + } |
|
| 1054 | + if (!$desc) { |
|
| 1055 | + $desc = description_table($table, $serveur); |
|
| 1056 | + } |
|
| 1057 | + $fields = $desc['field']; |
|
| 1058 | + foreach ($couples as $k => $val) { |
|
| 1059 | + $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1060 | + } |
|
| 1061 | + |
|
| 1062 | + return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1063 | 1063 | } |
| 1064 | 1064 | |
| 1065 | 1065 | |
| 1066 | 1066 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1067 | - if (!$values) { |
|
| 1068 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1069 | - |
|
| 1070 | - return 0; |
|
| 1071 | - } |
|
| 1072 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1073 | - $prefixe = $connexion['prefixe']; |
|
| 1074 | - $link = $connexion['link']; |
|
| 1075 | - |
|
| 1076 | - if (!$desc) { |
|
| 1077 | - $desc = description_table($table, $serveur); |
|
| 1078 | - } |
|
| 1079 | - if (!$desc) { |
|
| 1080 | - die("$table insertion sans description"); |
|
| 1081 | - } |
|
| 1082 | - $prim = $desc['key']['PRIMARY KEY']; |
|
| 1083 | - $ids = preg_split('/,\s*/', $prim); |
|
| 1084 | - $noprims = $prims = []; |
|
| 1085 | - foreach ($values as $k => $v) { |
|
| 1086 | - $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1087 | - |
|
| 1088 | - if (!in_array($k, $ids)) { |
|
| 1089 | - $noprims[$k] = "$k=$v"; |
|
| 1090 | - } else { |
|
| 1091 | - $prims[$k] = "$k=$v"; |
|
| 1092 | - } |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1096 | - $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1097 | - |
|
| 1098 | - $where = join(' AND ', $prims); |
|
| 1099 | - if (!$where) { |
|
| 1100 | - return spip_pg_insert( |
|
| 1101 | - $table, |
|
| 1102 | - '(' . join(',', array_keys($values)) . ')', |
|
| 1103 | - '(' . join(',', $values) . ')', |
|
| 1104 | - $desc, |
|
| 1105 | - $serveur |
|
| 1106 | - ); |
|
| 1107 | - } |
|
| 1108 | - $couples = join(',', $noprims); |
|
| 1109 | - |
|
| 1110 | - $seq = spip_pg_sequence($table); |
|
| 1111 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 1112 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1113 | - |
|
| 1114 | - $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1115 | - if ($couples) { |
|
| 1116 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1067 | + if (!$values) { |
|
| 1068 | + spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1069 | + |
|
| 1070 | + return 0; |
|
| 1071 | + } |
|
| 1072 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1073 | + $prefixe = $connexion['prefixe']; |
|
| 1074 | + $link = $connexion['link']; |
|
| 1075 | + |
|
| 1076 | + if (!$desc) { |
|
| 1077 | + $desc = description_table($table, $serveur); |
|
| 1078 | + } |
|
| 1079 | + if (!$desc) { |
|
| 1080 | + die("$table insertion sans description"); |
|
| 1081 | + } |
|
| 1082 | + $prim = $desc['key']['PRIMARY KEY']; |
|
| 1083 | + $ids = preg_split('/,\s*/', $prim); |
|
| 1084 | + $noprims = $prims = []; |
|
| 1085 | + foreach ($values as $k => $v) { |
|
| 1086 | + $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1087 | + |
|
| 1088 | + if (!in_array($k, $ids)) { |
|
| 1089 | + $noprims[$k] = "$k=$v"; |
|
| 1090 | + } else { |
|
| 1091 | + $prims[$k] = "$k=$v"; |
|
| 1092 | + } |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1096 | + $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1097 | + |
|
| 1098 | + $where = join(' AND ', $prims); |
|
| 1099 | + if (!$where) { |
|
| 1100 | + return spip_pg_insert( |
|
| 1101 | + $table, |
|
| 1102 | + '(' . join(',', array_keys($values)) . ')', |
|
| 1103 | + '(' . join(',', $values) . ')', |
|
| 1104 | + $desc, |
|
| 1105 | + $serveur |
|
| 1106 | + ); |
|
| 1107 | + } |
|
| 1108 | + $couples = join(',', $noprims); |
|
| 1109 | + |
|
| 1110 | + $seq = spip_pg_sequence($table); |
|
| 1111 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 1112 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1113 | + |
|
| 1114 | + $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1115 | + if ($couples) { |
|
| 1116 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1117 | 1117 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 1118 | - if (!$couples) { |
|
| 1119 | - return false; |
|
| 1120 | - } |
|
| 1121 | - $couples = pg_affected_rows($couples); |
|
| 1122 | - } |
|
| 1123 | - if (!$couples) { |
|
| 1124 | - $ret = !$seq ? '' : |
|
| 1125 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1126 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1127 | - ',', |
|
| 1128 | - $values |
|
| 1129 | - ) . ")$ret"; |
|
| 1130 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1131 | - if (!$couples) { |
|
| 1132 | - return false; |
|
| 1133 | - } elseif ($ret) { |
|
| 1134 | - $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1135 | - if ($r[0]) { |
|
| 1136 | - $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1137 | - // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1138 | - // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1139 | - $r = @pg_query($link, $q); |
|
| 1140 | - } |
|
| 1141 | - } |
|
| 1142 | - } |
|
| 1143 | - |
|
| 1144 | - return $couples; |
|
| 1118 | + if (!$couples) { |
|
| 1119 | + return false; |
|
| 1120 | + } |
|
| 1121 | + $couples = pg_affected_rows($couples); |
|
| 1122 | + } |
|
| 1123 | + if (!$couples) { |
|
| 1124 | + $ret = !$seq ? '' : |
|
| 1125 | + (" RETURNING nextval('$seq') < $prim"); |
|
| 1126 | + $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1127 | + ',', |
|
| 1128 | + $values |
|
| 1129 | + ) . ")$ret"; |
|
| 1130 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1131 | + if (!$couples) { |
|
| 1132 | + return false; |
|
| 1133 | + } elseif ($ret) { |
|
| 1134 | + $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1135 | + if ($r[0]) { |
|
| 1136 | + $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1137 | + // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1138 | + // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1139 | + $r = @pg_query($link, $q); |
|
| 1140 | + } |
|
| 1141 | + } |
|
| 1142 | + } |
|
| 1143 | + |
|
| 1144 | + return $couples; |
|
| 1145 | 1145 | } |
| 1146 | 1146 | |
| 1147 | 1147 | |
| 1148 | 1148 | function spip_pg_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1149 | - $retour = null; |
|
| 1150 | - // boucler pour traiter chaque requete independemment |
|
| 1151 | - foreach ($tab_couples as $couples) { |
|
| 1152 | - $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1153 | - } |
|
| 1154 | - |
|
| 1155 | - // renvoie le dernier id |
|
| 1156 | - return $retour; |
|
| 1149 | + $retour = null; |
|
| 1150 | + // boucler pour traiter chaque requete independemment |
|
| 1151 | + foreach ($tab_couples as $couples) { |
|
| 1152 | + $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1153 | + } |
|
| 1154 | + |
|
| 1155 | + // renvoie le dernier id |
|
| 1156 | + return $retour; |
|
| 1157 | 1157 | } |
| 1158 | 1158 | |
| 1159 | 1159 | |
@@ -1162,149 +1162,149 @@ discard block |
||
| 1162 | 1162 | |
| 1163 | 1163 | function spip_pg_sequence($table, $raw = false) { |
| 1164 | 1164 | |
| 1165 | - include_spip('base/serial'); |
|
| 1166 | - if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1167 | - return false; |
|
| 1168 | - } |
|
| 1169 | - $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1170 | - $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1171 | - if ( |
|
| 1172 | - !preg_match('/^\w+$/', $prim) |
|
| 1173 | - or !str_contains($desc['field'][$prim], 'int') |
|
| 1174 | - ) { |
|
| 1175 | - return ''; |
|
| 1176 | - } else { |
|
| 1177 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1178 | - } |
|
| 1165 | + include_spip('base/serial'); |
|
| 1166 | + if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1167 | + return false; |
|
| 1168 | + } |
|
| 1169 | + $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1170 | + $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1171 | + if ( |
|
| 1172 | + !preg_match('/^\w+$/', $prim) |
|
| 1173 | + or !str_contains($desc['field'][$prim], 'int') |
|
| 1174 | + ) { |
|
| 1175 | + return ''; |
|
| 1176 | + } else { |
|
| 1177 | + return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1178 | + } |
|
| 1179 | 1179 | } |
| 1180 | 1180 | |
| 1181 | 1181 | // Explicite les conversions de Mysql d'une valeur $v de type $t |
| 1182 | 1182 | // Dans le cas d'un champ date, pas d'apostrophe, c'est une syntaxe ad hoc |
| 1183 | 1183 | |
| 1184 | 1184 | function spip_pg_cite($v, $t) { |
| 1185 | - if (is_null($v)) { |
|
| 1186 | - return 'NULL'; |
|
| 1187 | - } // null php se traduit en NULL SQL |
|
| 1188 | - |
|
| 1189 | - if (sql_test_date($t)) { |
|
| 1190 | - if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1191 | - return spip_pg_frommysql($v); |
|
| 1192 | - } else { |
|
| 1193 | - if (strncmp($v, '0000', 4) == 0) { |
|
| 1194 | - $v = '0001' . substr($v, 4); |
|
| 1195 | - } |
|
| 1196 | - if (strpos($v, '-00-00') === 4) { |
|
| 1197 | - $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1198 | - } |
|
| 1199 | - |
|
| 1200 | - return "timestamp '$v'"; |
|
| 1201 | - } |
|
| 1202 | - } elseif (!sql_test_int($t)) { |
|
| 1203 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1204 | - } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1205 | - return $v; |
|
| 1206 | - } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1207 | - return substr($v, 1); |
|
| 1208 | - } else { |
|
| 1209 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1210 | - |
|
| 1211 | - return intval($v); |
|
| 1212 | - } |
|
| 1185 | + if (is_null($v)) { |
|
| 1186 | + return 'NULL'; |
|
| 1187 | + } // null php se traduit en NULL SQL |
|
| 1188 | + |
|
| 1189 | + if (sql_test_date($t)) { |
|
| 1190 | + if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1191 | + return spip_pg_frommysql($v); |
|
| 1192 | + } else { |
|
| 1193 | + if (strncmp($v, '0000', 4) == 0) { |
|
| 1194 | + $v = '0001' . substr($v, 4); |
|
| 1195 | + } |
|
| 1196 | + if (strpos($v, '-00-00') === 4) { |
|
| 1197 | + $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1198 | + } |
|
| 1199 | + |
|
| 1200 | + return "timestamp '$v'"; |
|
| 1201 | + } |
|
| 1202 | + } elseif (!sql_test_int($t)) { |
|
| 1203 | + return ("'" . pg_escape_string($v) . "'"); |
|
| 1204 | + } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1205 | + return $v; |
|
| 1206 | + } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1207 | + return substr($v, 1); |
|
| 1208 | + } else { |
|
| 1209 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1210 | + |
|
| 1211 | + return intval($v); |
|
| 1212 | + } |
|
| 1213 | 1213 | } |
| 1214 | 1214 | |
| 1215 | 1215 | function spip_pg_hex($v) { |
| 1216 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1216 | + return "CAST(x'" . $v . "' as bigint)"; |
|
| 1217 | 1217 | } |
| 1218 | 1218 | |
| 1219 | 1219 | function spip_pg_quote($v, $type = '') { |
| 1220 | - if (!is_array($v)) { |
|
| 1221 | - return spip_pg_cite($v, $type); |
|
| 1222 | - } |
|
| 1223 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1224 | - foreach ($v as $k => $r) { |
|
| 1225 | - $v[$k] = spip_pg_quote($r, $type); |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - return join(',', $v); |
|
| 1220 | + if (!is_array($v)) { |
|
| 1221 | + return spip_pg_cite($v, $type); |
|
| 1222 | + } |
|
| 1223 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1224 | + foreach ($v as $k => $r) { |
|
| 1225 | + $v[$k] = spip_pg_quote($r, $type); |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + return join(',', $v); |
|
| 1229 | 1229 | } |
| 1230 | 1230 | |
| 1231 | 1231 | function spip_pg_date_proche($champ, $interval, $unite) { |
| 1232 | - return '(' |
|
| 1233 | - . $champ |
|
| 1234 | - . (($interval <= 0) ? '>' : '<') |
|
| 1235 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1236 | - . '(' |
|
| 1237 | - . sql_quote(date('Y-m-d H:i:s')) |
|
| 1238 | - . ', INTERVAL ' |
|
| 1239 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1240 | - . ' ' |
|
| 1241 | - . $unite |
|
| 1242 | - . '))'; |
|
| 1232 | + return '(' |
|
| 1233 | + . $champ |
|
| 1234 | + . (($interval <= 0) ? '>' : '<') |
|
| 1235 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1236 | + . '(' |
|
| 1237 | + . sql_quote(date('Y-m-d H:i:s')) |
|
| 1238 | + . ', INTERVAL ' |
|
| 1239 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1240 | + . ' ' |
|
| 1241 | + . $unite |
|
| 1242 | + . '))'; |
|
| 1243 | 1243 | } |
| 1244 | 1244 | |
| 1245 | 1245 | function spip_pg_in($val, $valeurs, $not = '', $serveur = '') { |
| 1246 | 1246 | // |
| 1247 | 1247 | // IN (...) souvent limite a 255 elements, d'ou cette fonction assistante |
| 1248 | 1248 | // |
| 1249 | - // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1250 | - if (!$valeurs) { |
|
| 1251 | - return $not ? '0=0' : '0=1'; |
|
| 1252 | - } |
|
| 1253 | - if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1254 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1255 | - } |
|
| 1256 | - $n = $i = 0; |
|
| 1257 | - $in_sql = ''; |
|
| 1258 | - while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1259 | - if ((++$i) >= 255) { |
|
| 1260 | - $in_sql .= "($val $not IN (" . |
|
| 1261 | - substr($valeurs, 0, $n) . |
|
| 1262 | - "))\n" . |
|
| 1263 | - ($not ? "AND\t" : "OR\t"); |
|
| 1264 | - $valeurs = substr($valeurs, $n + 1); |
|
| 1265 | - $i = $n = 0; |
|
| 1266 | - } |
|
| 1267 | - } |
|
| 1268 | - $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1269 | - |
|
| 1270 | - return "($in_sql)"; |
|
| 1249 | + // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1250 | + if (!$valeurs) { |
|
| 1251 | + return $not ? '0=0' : '0=1'; |
|
| 1252 | + } |
|
| 1253 | + if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1254 | + return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1255 | + } |
|
| 1256 | + $n = $i = 0; |
|
| 1257 | + $in_sql = ''; |
|
| 1258 | + while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1259 | + if ((++$i) >= 255) { |
|
| 1260 | + $in_sql .= "($val $not IN (" . |
|
| 1261 | + substr($valeurs, 0, $n) . |
|
| 1262 | + "))\n" . |
|
| 1263 | + ($not ? "AND\t" : "OR\t"); |
|
| 1264 | + $valeurs = substr($valeurs, $n + 1); |
|
| 1265 | + $i = $n = 0; |
|
| 1266 | + } |
|
| 1267 | + } |
|
| 1268 | + $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1269 | + |
|
| 1270 | + return "($in_sql)"; |
|
| 1271 | 1271 | } |
| 1272 | 1272 | |
| 1273 | 1273 | function spip_pg_error($query = '', $serveur = '', $requeter = true) { |
| 1274 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1275 | - $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1276 | - if ($s) { |
|
| 1277 | - $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1278 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1279 | - } |
|
| 1280 | - |
|
| 1281 | - return $s; |
|
| 1274 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1275 | + $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1276 | + if ($s) { |
|
| 1277 | + $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1278 | + spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1279 | + } |
|
| 1280 | + |
|
| 1281 | + return $s; |
|
| 1282 | 1282 | } |
| 1283 | 1283 | |
| 1284 | 1284 | function spip_pg_errno($serveur = '') { |
| 1285 | - // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1286 | - // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1287 | - return 0; |
|
| 1285 | + // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1286 | + // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1287 | + return 0; |
|
| 1288 | 1288 | } |
| 1289 | 1289 | |
| 1290 | 1290 | function spip_pg_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 1291 | - if ($exist) { |
|
| 1292 | - $exist = ' IF EXISTS'; |
|
| 1293 | - } |
|
| 1294 | - if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1295 | - return true; |
|
| 1296 | - } else { |
|
| 1297 | - return false; |
|
| 1298 | - } |
|
| 1291 | + if ($exist) { |
|
| 1292 | + $exist = ' IF EXISTS'; |
|
| 1293 | + } |
|
| 1294 | + if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1295 | + return true; |
|
| 1296 | + } else { |
|
| 1297 | + return false; |
|
| 1298 | + } |
|
| 1299 | 1299 | } |
| 1300 | 1300 | |
| 1301 | 1301 | // supprime une vue |
| 1302 | 1302 | function spip_pg_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 1303 | - if ($exist) { |
|
| 1304 | - $exist = ' IF EXISTS'; |
|
| 1305 | - } |
|
| 1303 | + if ($exist) { |
|
| 1304 | + $exist = ' IF EXISTS'; |
|
| 1305 | + } |
|
| 1306 | 1306 | |
| 1307 | - return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1307 | + return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1308 | 1308 | } |
| 1309 | 1309 | |
| 1310 | 1310 | /** |
@@ -1321,40 +1321,40 @@ discard block |
||
| 1321 | 1321 | * Ressource à utiliser avec sql_fetch() |
| 1322 | 1322 | **/ |
| 1323 | 1323 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1324 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1325 | - $link = $connexion['link']; |
|
| 1326 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1324 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1325 | + $link = $connexion['link']; |
|
| 1326 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1327 | 1327 | |
| 1328 | - return spip_pg_query_simple($link, $q); |
|
| 1328 | + return spip_pg_query_simple($link, $q); |
|
| 1329 | 1329 | } |
| 1330 | 1330 | |
| 1331 | 1331 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1332 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1333 | - $link = $connexion['link']; |
|
| 1334 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1335 | - |
|
| 1336 | - $res = spip_pg_query_simple($link, $q); |
|
| 1337 | - if (!$res) { |
|
| 1338 | - return false; |
|
| 1339 | - } |
|
| 1340 | - |
|
| 1341 | - // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1342 | - // il faut en tenir compte dans le return |
|
| 1343 | - $fields = []; |
|
| 1344 | - while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1345 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1346 | - } |
|
| 1347 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1348 | - $res = spip_pg_query_simple($link, $q); |
|
| 1349 | - $keys = []; |
|
| 1350 | - while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1351 | - if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1352 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1353 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1354 | - } |
|
| 1355 | - } |
|
| 1356 | - |
|
| 1357 | - return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1332 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1333 | + $link = $connexion['link']; |
|
| 1334 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1335 | + |
|
| 1336 | + $res = spip_pg_query_simple($link, $q); |
|
| 1337 | + if (!$res) { |
|
| 1338 | + return false; |
|
| 1339 | + } |
|
| 1340 | + |
|
| 1341 | + // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1342 | + // il faut en tenir compte dans le return |
|
| 1343 | + $fields = []; |
|
| 1344 | + while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1345 | + $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1346 | + } |
|
| 1347 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1348 | + $res = spip_pg_query_simple($link, $q); |
|
| 1349 | + $keys = []; |
|
| 1350 | + while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1351 | + if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1352 | + $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1353 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1354 | + } |
|
| 1355 | + } |
|
| 1356 | + |
|
| 1357 | + return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1358 | 1358 | } |
| 1359 | 1359 | |
| 1360 | 1360 | // Fonction de creation d'une table SQL nommee $nom |
@@ -1365,116 +1365,116 @@ discard block |
||
| 1365 | 1365 | // Le nom des index est prefixe par celui de la table pour eviter les conflits |
| 1366 | 1366 | function spip_pg_create($nom, $champs, $cles, $autoinc = false, $temporary = false, $serveur = '', $requeter = true) { |
| 1367 | 1367 | |
| 1368 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1369 | - $link = $connexion['link']; |
|
| 1370 | - $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1371 | - |
|
| 1372 | - $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1373 | - $keys = []; |
|
| 1374 | - |
|
| 1375 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1376 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1377 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 1378 | - return; |
|
| 1379 | - } |
|
| 1380 | - |
|
| 1381 | - foreach ($cles as $k => $v) { |
|
| 1382 | - if (strpos($k, 'KEY ') === 0) { |
|
| 1383 | - $n = str_replace('`', '', $k); |
|
| 1384 | - $v = str_replace('`', '"', $v); |
|
| 1385 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1386 | - if ($k != $n) { |
|
| 1387 | - $i = "\"$i\""; |
|
| 1388 | - } |
|
| 1389 | - $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1390 | - } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1391 | - $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1392 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1393 | - } else { |
|
| 1394 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1395 | - } |
|
| 1396 | - if ($k == 'PRIMARY KEY') { |
|
| 1397 | - $prim_name = $v; |
|
| 1398 | - } |
|
| 1399 | - $s = ','; |
|
| 1400 | - } |
|
| 1401 | - $s = ''; |
|
| 1402 | - |
|
| 1403 | - $character_set = ''; |
|
| 1404 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1405 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1406 | - } |
|
| 1407 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1408 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1409 | - } |
|
| 1410 | - |
|
| 1411 | - foreach ($champs as $k => $v) { |
|
| 1412 | - $k = str_replace('`', '"', $k); |
|
| 1413 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1414 | - if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1415 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1416 | - } |
|
| 1417 | - } |
|
| 1418 | - |
|
| 1419 | - $query .= "$s\n\t\t$k " |
|
| 1420 | - . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1421 | - ? ' bigserial' |
|
| 1422 | - : mysql2pg_type($v) |
|
| 1423 | - ); |
|
| 1424 | - $s = ','; |
|
| 1425 | - } |
|
| 1426 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1427 | - |
|
| 1428 | - // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1429 | - |
|
| 1430 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1431 | - ($character_set ? " DEFAULT $character_set" : '') |
|
| 1432 | - . "\n"; |
|
| 1433 | - |
|
| 1434 | - if (!$requeter) { |
|
| 1435 | - return $q; |
|
| 1436 | - } |
|
| 1437 | - $connexion['last'] = $q; |
|
| 1438 | - $r = @pg_query($link, $q); |
|
| 1439 | - |
|
| 1440 | - if (!$r) { |
|
| 1441 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1442 | - } else { |
|
| 1443 | - foreach ($keys as $index) { |
|
| 1444 | - pg_query($link, $index); |
|
| 1445 | - } |
|
| 1446 | - } |
|
| 1447 | - |
|
| 1448 | - return $r; |
|
| 1368 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1369 | + $link = $connexion['link']; |
|
| 1370 | + $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1371 | + |
|
| 1372 | + $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1373 | + $keys = []; |
|
| 1374 | + |
|
| 1375 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1376 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1377 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 1378 | + return; |
|
| 1379 | + } |
|
| 1380 | + |
|
| 1381 | + foreach ($cles as $k => $v) { |
|
| 1382 | + if (strpos($k, 'KEY ') === 0) { |
|
| 1383 | + $n = str_replace('`', '', $k); |
|
| 1384 | + $v = str_replace('`', '"', $v); |
|
| 1385 | + $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1386 | + if ($k != $n) { |
|
| 1387 | + $i = "\"$i\""; |
|
| 1388 | + } |
|
| 1389 | + $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1390 | + } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1391 | + $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1392 | + $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1393 | + } else { |
|
| 1394 | + $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1395 | + } |
|
| 1396 | + if ($k == 'PRIMARY KEY') { |
|
| 1397 | + $prim_name = $v; |
|
| 1398 | + } |
|
| 1399 | + $s = ','; |
|
| 1400 | + } |
|
| 1401 | + $s = ''; |
|
| 1402 | + |
|
| 1403 | + $character_set = ''; |
|
| 1404 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1405 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1406 | + } |
|
| 1407 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1408 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1409 | + } |
|
| 1410 | + |
|
| 1411 | + foreach ($champs as $k => $v) { |
|
| 1412 | + $k = str_replace('`', '"', $k); |
|
| 1413 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1414 | + if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1415 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1416 | + } |
|
| 1417 | + } |
|
| 1418 | + |
|
| 1419 | + $query .= "$s\n\t\t$k " |
|
| 1420 | + . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1421 | + ? ' bigserial' |
|
| 1422 | + : mysql2pg_type($v) |
|
| 1423 | + ); |
|
| 1424 | + $s = ','; |
|
| 1425 | + } |
|
| 1426 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1427 | + |
|
| 1428 | + // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1429 | + |
|
| 1430 | + $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1431 | + ($character_set ? " DEFAULT $character_set" : '') |
|
| 1432 | + . "\n"; |
|
| 1433 | + |
|
| 1434 | + if (!$requeter) { |
|
| 1435 | + return $q; |
|
| 1436 | + } |
|
| 1437 | + $connexion['last'] = $q; |
|
| 1438 | + $r = @pg_query($link, $q); |
|
| 1439 | + |
|
| 1440 | + if (!$r) { |
|
| 1441 | + spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1442 | + } else { |
|
| 1443 | + foreach ($keys as $index) { |
|
| 1444 | + pg_query($link, $index); |
|
| 1445 | + } |
|
| 1446 | + } |
|
| 1447 | + |
|
| 1448 | + return $r; |
|
| 1449 | 1449 | } |
| 1450 | 1450 | |
| 1451 | 1451 | |
| 1452 | 1452 | function spip_pg_create_base($nom, $serveur = '', $requeter = true) { |
| 1453 | - return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1453 | + return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1454 | 1454 | } |
| 1455 | 1455 | |
| 1456 | 1456 | // Fonction de creation d'une vue SQL nommee $nom |
| 1457 | 1457 | function spip_pg_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 1458 | - if (!$query_select) { |
|
| 1459 | - return false; |
|
| 1460 | - } |
|
| 1461 | - // vue deja presente |
|
| 1462 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 1463 | - if ($requeter) { |
|
| 1464 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1465 | - } |
|
| 1458 | + if (!$query_select) { |
|
| 1459 | + return false; |
|
| 1460 | + } |
|
| 1461 | + // vue deja presente |
|
| 1462 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 1463 | + if ($requeter) { |
|
| 1464 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1465 | + } |
|
| 1466 | 1466 | |
| 1467 | - return false; |
|
| 1468 | - } |
|
| 1467 | + return false; |
|
| 1468 | + } |
|
| 1469 | 1469 | |
| 1470 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1470 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1471 | 1471 | |
| 1472 | - return spip_pg_query($query, $serveur, $requeter); |
|
| 1472 | + return spip_pg_query($query, $serveur, $requeter); |
|
| 1473 | 1473 | } |
| 1474 | 1474 | |
| 1475 | 1475 | |
| 1476 | 1476 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1477 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1477 | + spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1478 | 1478 | } |
| 1479 | 1479 | |
| 1480 | 1480 | |
@@ -1487,50 +1487,50 @@ discard block |
||
| 1487 | 1487 | * @return bool|string true / false / requete |
| 1488 | 1488 | **/ |
| 1489 | 1489 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1490 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1490 | + return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1491 | 1491 | } |
| 1492 | 1492 | |
| 1493 | 1493 | // Selectionner la sous-chaine dans $objet |
| 1494 | 1494 | // correspondant a $lang. Cf balise Multi de Spip |
| 1495 | 1495 | |
| 1496 | 1496 | function spip_pg_multi($objet, $lang) { |
| 1497 | - $r = 'regexp_replace(' |
|
| 1498 | - . $objet |
|
| 1499 | - . ",'<multi>.*[[]" |
|
| 1500 | - . $lang |
|
| 1501 | - . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1497 | + $r = 'regexp_replace(' |
|
| 1498 | + . $objet |
|
| 1499 | + . ",'<multi>.*[[]" |
|
| 1500 | + . $lang |
|
| 1501 | + . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1502 | 1502 | |
| 1503 | - return $r; |
|
| 1503 | + return $r; |
|
| 1504 | 1504 | } |
| 1505 | 1505 | |
| 1506 | 1506 | // Palanquee d'idiosyncrasies MySQL dans les creations de table |
| 1507 | 1507 | // A completer par les autres, mais essayer de reduire en amont. |
| 1508 | 1508 | |
| 1509 | 1509 | function mysql2pg_type($v) { |
| 1510 | - $remplace = [ |
|
| 1511 | - '/auto_increment/i' => '', // non reconnu |
|
| 1512 | - '/bigint/i' => 'bigint', |
|
| 1513 | - '/mediumint/i' => 'mediumint', |
|
| 1514 | - '/smallint/i' => 'smallint', |
|
| 1515 | - '/tinyint/i' => 'int', |
|
| 1516 | - '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1517 | - '/longtext/i' => 'text', |
|
| 1518 | - '/mediumtext/i' => 'text', |
|
| 1519 | - '/tinytext/i' => 'text', |
|
| 1520 | - '/longblob/i' => 'text', |
|
| 1521 | - '/0000-00-00/' => '0001-01-01', |
|
| 1522 | - '/datetime/i' => 'timestamp', |
|
| 1523 | - '/unsigned/i' => '', |
|
| 1524 | - '/double/i' => 'double precision', |
|
| 1525 | - '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1526 | - '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1527 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1528 | - ]; |
|
| 1529 | - |
|
| 1530 | - return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1510 | + $remplace = [ |
|
| 1511 | + '/auto_increment/i' => '', // non reconnu |
|
| 1512 | + '/bigint/i' => 'bigint', |
|
| 1513 | + '/mediumint/i' => 'mediumint', |
|
| 1514 | + '/smallint/i' => 'smallint', |
|
| 1515 | + '/tinyint/i' => 'int', |
|
| 1516 | + '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1517 | + '/longtext/i' => 'text', |
|
| 1518 | + '/mediumtext/i' => 'text', |
|
| 1519 | + '/tinytext/i' => 'text', |
|
| 1520 | + '/longblob/i' => 'text', |
|
| 1521 | + '/0000-00-00/' => '0001-01-01', |
|
| 1522 | + '/datetime/i' => 'timestamp', |
|
| 1523 | + '/unsigned/i' => '', |
|
| 1524 | + '/double/i' => 'double precision', |
|
| 1525 | + '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1526 | + '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1527 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1528 | + ]; |
|
| 1529 | + |
|
| 1530 | + return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1531 | 1531 | } |
| 1532 | 1532 | |
| 1533 | 1533 | // Renvoie false si on n'a pas les fonctions pg (pour l'install) |
| 1534 | 1534 | function spip_versions_pg() { |
| 1535 | - return function_exists('pg_connect'); |
|
| 1535 | + return function_exists('pg_connect'); |
|
| 1536 | 1536 | } |