@@ -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 | /** |
@@ -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,59 +63,59 @@ 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 | - $result_repair |
|
| 105 | - and $msg = join( |
|
| 106 | - ' ', |
|
| 107 | - (is_resource($result_repair) or is_object($result_repair)) ? sql_fetch($result_repair) : $result_repair |
|
| 108 | - ) . ' ' |
|
| 109 | - and !str_contains($msg, ' OK ') |
|
| 110 | - ) { |
|
| 111 | - $class = " class='notice'"; |
|
| 112 | - $m .= '<br /><tt>' . spip_htmlentities($msg) . "</tt>\n"; |
|
| 113 | - } else { |
|
| 114 | - $m .= ' ' . _T('texte_table_ok'); |
|
| 115 | - } |
|
| 103 | + if ( |
|
| 104 | + $result_repair |
|
| 105 | + and $msg = join( |
|
| 106 | + ' ', |
|
| 107 | + (is_resource($result_repair) or is_object($result_repair)) ? sql_fetch($result_repair) : $result_repair |
|
| 108 | + ) . ' ' |
|
| 109 | + and !str_contains($msg, ' OK ') |
|
| 110 | + ) { |
|
| 111 | + $class = " class='notice'"; |
|
| 112 | + $m .= '<br /><tt>' . spip_htmlentities($msg) . "</tt>\n"; |
|
| 113 | + } else { |
|
| 114 | + $m .= ' ' . _T('texte_table_ok'); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - $res .= "<div$class>$m</div>"; |
|
| 118 | - } |
|
| 117 | + $res .= "<div$class>$m</div>"; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - return $res; |
|
| 120 | + return $res; |
|
| 121 | 121 | } |
@@ -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>"; |
@@ -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)) { |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - return; |
|
| 21 | + return; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | define('_DEFAULT_DB', 'spip'); |
@@ -31,159 +31,159 @@ discard block |
||
| 31 | 31 | // si ca ne marche toujours pas, echec. |
| 32 | 32 | |
| 33 | 33 | function req_pg_dist($addr, $port, $login, $pass, $db = '', $prefixe = '') { |
| 34 | - static $last_connect = []; |
|
| 35 | - if (!extension_loaded('pgsql')) { |
|
| 36 | - return false; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - // si provient de selectdb |
|
| 40 | - if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 41 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 42 | - ${$a} = $last_connect[$a]; |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - [$host, $p] = array_pad(explode(';', $addr), 2, null); |
|
| 46 | - if ($p > 0) { |
|
| 47 | - $port = " port=$p"; |
|
| 48 | - } else { |
|
| 49 | - $port = ''; |
|
| 50 | - } |
|
| 51 | - $erreurs = []; |
|
| 52 | - if ($db) { |
|
| 53 | - @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 54 | - } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 55 | - $erreurs[] = pg_last_error(); |
|
| 56 | - if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 57 | - $db = $login; |
|
| 58 | - } else { |
|
| 59 | - $erreurs[] = pg_last_error(); |
|
| 60 | - $db = _DEFAULT_DB; |
|
| 61 | - $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - if (!$link) { |
|
| 65 | - $erreurs[] = pg_last_error(); |
|
| 66 | - foreach ($erreurs as $e) { |
|
| 67 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return false; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - if ($link) { |
|
| 74 | - $last_connect = [ |
|
| 75 | - 'addr' => $addr, |
|
| 76 | - 'port' => $port, |
|
| 77 | - 'login' => $login, |
|
| 78 | - 'pass' => $pass, |
|
| 79 | - 'db' => $db, |
|
| 80 | - 'prefixe' => $prefixe, |
|
| 81 | - ]; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - spip_log( |
|
| 85 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 86 | - 'pg.' . _LOG_DEBUG |
|
| 87 | - ); |
|
| 88 | - |
|
| 89 | - return !$link ? false : [ |
|
| 90 | - 'db' => $db, |
|
| 91 | - 'prefixe' => $prefixe ?: $db, |
|
| 92 | - 'link' => $link, |
|
| 93 | - ]; |
|
| 34 | + static $last_connect = []; |
|
| 35 | + if (!extension_loaded('pgsql')) { |
|
| 36 | + return false; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + // si provient de selectdb |
|
| 40 | + if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 41 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 42 | + ${$a} = $last_connect[$a]; |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + [$host, $p] = array_pad(explode(';', $addr), 2, null); |
|
| 46 | + if ($p > 0) { |
|
| 47 | + $port = " port=$p"; |
|
| 48 | + } else { |
|
| 49 | + $port = ''; |
|
| 50 | + } |
|
| 51 | + $erreurs = []; |
|
| 52 | + if ($db) { |
|
| 53 | + @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 54 | + } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 55 | + $erreurs[] = pg_last_error(); |
|
| 56 | + if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 57 | + $db = $login; |
|
| 58 | + } else { |
|
| 59 | + $erreurs[] = pg_last_error(); |
|
| 60 | + $db = _DEFAULT_DB; |
|
| 61 | + $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + if (!$link) { |
|
| 65 | + $erreurs[] = pg_last_error(); |
|
| 66 | + foreach ($erreurs as $e) { |
|
| 67 | + spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + if ($link) { |
|
| 74 | + $last_connect = [ |
|
| 75 | + 'addr' => $addr, |
|
| 76 | + 'port' => $port, |
|
| 77 | + 'login' => $login, |
|
| 78 | + 'pass' => $pass, |
|
| 79 | + 'db' => $db, |
|
| 80 | + 'prefixe' => $prefixe, |
|
| 81 | + ]; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + spip_log( |
|
| 85 | + "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 86 | + 'pg.' . _LOG_DEBUG |
|
| 87 | + ); |
|
| 88 | + |
|
| 89 | + return !$link ? false : [ |
|
| 90 | + 'db' => $db, |
|
| 91 | + 'prefixe' => $prefixe ?: $db, |
|
| 92 | + 'link' => $link, |
|
| 93 | + ]; |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | $GLOBALS['spip_pg_functions_1'] = [ |
| 97 | - 'alter' => 'spip_pg_alter', |
|
| 98 | - 'count' => 'spip_pg_count', |
|
| 99 | - 'countsel' => 'spip_pg_countsel', |
|
| 100 | - 'create' => 'spip_pg_create', |
|
| 101 | - 'create_base' => 'spip_pg_create_base', |
|
| 102 | - 'create_view' => 'spip_pg_create_view', |
|
| 103 | - 'date_proche' => 'spip_pg_date_proche', |
|
| 104 | - 'delete' => 'spip_pg_delete', |
|
| 105 | - 'drop_table' => 'spip_pg_drop_table', |
|
| 106 | - 'drop_view' => 'spip_pg_drop_view', |
|
| 107 | - 'errno' => 'spip_pg_errno', |
|
| 108 | - 'error' => 'spip_pg_error', |
|
| 109 | - 'explain' => 'spip_pg_explain', |
|
| 110 | - 'fetch' => 'spip_pg_fetch', |
|
| 111 | - 'seek' => 'spip_pg_seek', |
|
| 112 | - 'free' => 'spip_pg_free', |
|
| 113 | - 'hex' => 'spip_pg_hex', |
|
| 114 | - 'in' => 'spip_pg_in', |
|
| 115 | - 'insert' => 'spip_pg_insert', |
|
| 116 | - 'insertq' => 'spip_pg_insertq', |
|
| 117 | - 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 118 | - 'listdbs' => 'spip_pg_listdbs', |
|
| 119 | - 'multi' => 'spip_pg_multi', |
|
| 120 | - 'optimize' => 'spip_pg_optimize', |
|
| 121 | - 'query' => 'spip_pg_query', |
|
| 122 | - 'quote' => 'spip_pg_quote', |
|
| 123 | - 'replace' => 'spip_pg_replace', |
|
| 124 | - 'replace_multi' => 'spip_pg_replace_multi', |
|
| 125 | - 'select' => 'spip_pg_select', |
|
| 126 | - 'selectdb' => 'spip_pg_selectdb', |
|
| 127 | - 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 128 | - 'showbase' => 'spip_pg_showbase', |
|
| 129 | - 'showtable' => 'spip_pg_showtable', |
|
| 130 | - 'update' => 'spip_pg_update', |
|
| 131 | - 'updateq' => 'spip_pg_updateq', |
|
| 97 | + 'alter' => 'spip_pg_alter', |
|
| 98 | + 'count' => 'spip_pg_count', |
|
| 99 | + 'countsel' => 'spip_pg_countsel', |
|
| 100 | + 'create' => 'spip_pg_create', |
|
| 101 | + 'create_base' => 'spip_pg_create_base', |
|
| 102 | + 'create_view' => 'spip_pg_create_view', |
|
| 103 | + 'date_proche' => 'spip_pg_date_proche', |
|
| 104 | + 'delete' => 'spip_pg_delete', |
|
| 105 | + 'drop_table' => 'spip_pg_drop_table', |
|
| 106 | + 'drop_view' => 'spip_pg_drop_view', |
|
| 107 | + 'errno' => 'spip_pg_errno', |
|
| 108 | + 'error' => 'spip_pg_error', |
|
| 109 | + 'explain' => 'spip_pg_explain', |
|
| 110 | + 'fetch' => 'spip_pg_fetch', |
|
| 111 | + 'seek' => 'spip_pg_seek', |
|
| 112 | + 'free' => 'spip_pg_free', |
|
| 113 | + 'hex' => 'spip_pg_hex', |
|
| 114 | + 'in' => 'spip_pg_in', |
|
| 115 | + 'insert' => 'spip_pg_insert', |
|
| 116 | + 'insertq' => 'spip_pg_insertq', |
|
| 117 | + 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 118 | + 'listdbs' => 'spip_pg_listdbs', |
|
| 119 | + 'multi' => 'spip_pg_multi', |
|
| 120 | + 'optimize' => 'spip_pg_optimize', |
|
| 121 | + 'query' => 'spip_pg_query', |
|
| 122 | + 'quote' => 'spip_pg_quote', |
|
| 123 | + 'replace' => 'spip_pg_replace', |
|
| 124 | + 'replace_multi' => 'spip_pg_replace_multi', |
|
| 125 | + 'select' => 'spip_pg_select', |
|
| 126 | + 'selectdb' => 'spip_pg_selectdb', |
|
| 127 | + 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 128 | + 'showbase' => 'spip_pg_showbase', |
|
| 129 | + 'showtable' => 'spip_pg_showtable', |
|
| 130 | + 'update' => 'spip_pg_update', |
|
| 131 | + 'updateq' => 'spip_pg_updateq', |
|
| 132 | 132 | ]; |
| 133 | 133 | |
| 134 | 134 | // Par ou ca passe une fois les traductions faites |
| 135 | 135 | function spip_pg_trace_query($query, $serveur = '') { |
| 136 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 137 | - $prefixe = $connexion['prefixe']; |
|
| 138 | - $link = $connexion['link']; |
|
| 139 | - $db = $connexion['db']; |
|
| 140 | - |
|
| 141 | - if (isset($_GET['var_profile'])) { |
|
| 142 | - include_spip('public/tracer'); |
|
| 143 | - $t = trace_query_start(); |
|
| 144 | - $e = ''; |
|
| 145 | - } else { |
|
| 146 | - $t = 0; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $connexion['last'] = $query; |
|
| 150 | - $r = spip_pg_query_simple($link, $query); |
|
| 151 | - |
|
| 152 | - // Log de l'erreur eventuelle |
|
| 153 | - if ($e = spip_pg_errno($serveur)) { |
|
| 154 | - $e .= spip_pg_error($query, $serveur); |
|
| 155 | - } // et du fautif |
|
| 156 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 136 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 137 | + $prefixe = $connexion['prefixe']; |
|
| 138 | + $link = $connexion['link']; |
|
| 139 | + $db = $connexion['db']; |
|
| 140 | + |
|
| 141 | + if (isset($_GET['var_profile'])) { |
|
| 142 | + include_spip('public/tracer'); |
|
| 143 | + $t = trace_query_start(); |
|
| 144 | + $e = ''; |
|
| 145 | + } else { |
|
| 146 | + $t = 0; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $connexion['last'] = $query; |
|
| 150 | + $r = spip_pg_query_simple($link, $query); |
|
| 151 | + |
|
| 152 | + // Log de l'erreur eventuelle |
|
| 153 | + if ($e = spip_pg_errno($serveur)) { |
|
| 154 | + $e .= spip_pg_error($query, $serveur); |
|
| 155 | + } // et du fautif |
|
| 156 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | // Fonction de requete generale quand on est sur que c'est SQL standard. |
| 160 | 160 | // Elle change juste le noms des tables ($table_prefix) dans le FROM etc |
| 161 | 161 | |
| 162 | 162 | function spip_pg_query($query, $serveur = '', $requeter = true) { |
| 163 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 164 | - $prefixe = $connexion['prefixe']; |
|
| 165 | - $link = $connexion['link']; |
|
| 166 | - $db = $connexion['db']; |
|
| 167 | - |
|
| 168 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 169 | - $suite = strstr($query, (string) $regs[0]); |
|
| 170 | - $query = substr($query, 0, -strlen($suite)); |
|
| 171 | - } else { |
|
| 172 | - $suite = ''; |
|
| 173 | - } |
|
| 174 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 175 | - |
|
| 176 | - // renvoyer la requete inerte si demandee |
|
| 177 | - if (!$requeter) { |
|
| 178 | - return $query; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - return spip_pg_trace_query($query, $serveur); |
|
| 163 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 164 | + $prefixe = $connexion['prefixe']; |
|
| 165 | + $link = $connexion['link']; |
|
| 166 | + $db = $connexion['db']; |
|
| 167 | + |
|
| 168 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 169 | + $suite = strstr($query, (string) $regs[0]); |
|
| 170 | + $query = substr($query, 0, -strlen($suite)); |
|
| 171 | + } else { |
|
| 172 | + $suite = ''; |
|
| 173 | + } |
|
| 174 | + $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 175 | + |
|
| 176 | + // renvoyer la requete inerte si demandee |
|
| 177 | + if (!$requeter) { |
|
| 178 | + return $query; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + return spip_pg_trace_query($query, $serveur); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | function spip_pg_query_simple($link, $query) { |
| 185 | - #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 186 | - return pg_query($link, $query); |
|
| 185 | + #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 186 | + return pg_query($link, $query); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | /* |
@@ -195,196 +195,196 @@ discard block |
||
| 195 | 195 | * de requetes showtable intempestives |
| 196 | 196 | */ |
| 197 | 197 | function spip_pg_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 198 | - static $tables = []; |
|
| 199 | - |
|
| 200 | - if (!isset($tables[$table])) { |
|
| 201 | - if (!$desc) { |
|
| 202 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 203 | - $desc = $trouver_table($table, $serveur); |
|
| 204 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 205 | - if (!$desc) { |
|
| 206 | - return $couples; |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 211 | - // cependant, il faudra peut etre etendre |
|
| 212 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 213 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 214 | - $tables[$table] = []; |
|
| 215 | - foreach ($desc['field'] as $k => $v) { |
|
| 216 | - $v = strtolower(ltrim($v)); |
|
| 217 | - // ne pas ajouter de timestamp now() si un default est specifie |
|
| 218 | - if (strpos($v, 'timestamp') === 0 and !str_contains($v, 'default')) { |
|
| 219 | - $tables[$table][] = $k; |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - // ajout des champs type 'timestamp' absents |
|
| 225 | - foreach ($tables[$table] as $maj) { |
|
| 226 | - if (!array_key_exists($maj, $couples)) { |
|
| 227 | - $couples[$maj] = 'NOW()'; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - return $couples; |
|
| 198 | + static $tables = []; |
|
| 199 | + |
|
| 200 | + if (!isset($tables[$table])) { |
|
| 201 | + if (!$desc) { |
|
| 202 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 203 | + $desc = $trouver_table($table, $serveur); |
|
| 204 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 205 | + if (!$desc) { |
|
| 206 | + return $couples; |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 211 | + // cependant, il faudra peut etre etendre |
|
| 212 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 213 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 214 | + $tables[$table] = []; |
|
| 215 | + foreach ($desc['field'] as $k => $v) { |
|
| 216 | + $v = strtolower(ltrim($v)); |
|
| 217 | + // ne pas ajouter de timestamp now() si un default est specifie |
|
| 218 | + if (strpos($v, 'timestamp') === 0 and !str_contains($v, 'default')) { |
|
| 219 | + $tables[$table][] = $k; |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + // ajout des champs type 'timestamp' absents |
|
| 225 | + foreach ($tables[$table] as $maj) { |
|
| 226 | + if (!array_key_exists($maj, $couples)) { |
|
| 227 | + $couples[$maj] = 'NOW()'; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + return $couples; |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | |
| 235 | 235 | // Alter en PG ne traite pas les index |
| 236 | 236 | function spip_pg_alter($query, $serveur = '', $requeter = true) { |
| 237 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 238 | - // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 239 | - // ou revoir l'api de sql_alter en creant un |
|
| 240 | - // sql_alter_table($table,array($actions)); |
|
| 241 | - if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 242 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 243 | - |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - $debut = $regs[1]; |
|
| 247 | - $table = $regs[3]; |
|
| 248 | - $suite = $regs[4]; |
|
| 249 | - $todo = explode(',', $suite); |
|
| 250 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 251 | - $todo2 = []; |
|
| 252 | - $i = 0; |
|
| 253 | - $ouverte = false; |
|
| 254 | - while ($do = array_shift($todo)) { |
|
| 255 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 256 | - $o = (str_contains($do, '(')); |
|
| 257 | - $f = (str_contains($do, ')')); |
|
| 258 | - if ($o and !$f) { |
|
| 259 | - $ouverte = true; |
|
| 260 | - } elseif ($f) { |
|
| 261 | - $ouverte = false; |
|
| 262 | - } |
|
| 263 | - if (!$ouverte) { |
|
| 264 | - $i++; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - $todo = $todo2; |
|
| 268 | - $query = $debut . ' ' . array_shift($todo); |
|
| 269 | - |
|
| 270 | - if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 271 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 272 | - } else { |
|
| 273 | - if ($r[1]) { |
|
| 274 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 275 | - } |
|
| 276 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 277 | - if (function_exists($f)) { |
|
| 278 | - $f($r[2], $r[4], $serveur, $requeter); |
|
| 279 | - } else { |
|
| 280 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - // Alter a plusieurs args. Faudrait optimiser. |
|
| 284 | - if ($todo) { |
|
| 285 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 286 | - } |
|
| 237 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 238 | + // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 239 | + // ou revoir l'api de sql_alter en creant un |
|
| 240 | + // sql_alter_table($table,array($actions)); |
|
| 241 | + if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 242 | + spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 243 | + |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + $debut = $regs[1]; |
|
| 247 | + $table = $regs[3]; |
|
| 248 | + $suite = $regs[4]; |
|
| 249 | + $todo = explode(',', $suite); |
|
| 250 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 251 | + $todo2 = []; |
|
| 252 | + $i = 0; |
|
| 253 | + $ouverte = false; |
|
| 254 | + while ($do = array_shift($todo)) { |
|
| 255 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 256 | + $o = (str_contains($do, '(')); |
|
| 257 | + $f = (str_contains($do, ')')); |
|
| 258 | + if ($o and !$f) { |
|
| 259 | + $ouverte = true; |
|
| 260 | + } elseif ($f) { |
|
| 261 | + $ouverte = false; |
|
| 262 | + } |
|
| 263 | + if (!$ouverte) { |
|
| 264 | + $i++; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + $todo = $todo2; |
|
| 268 | + $query = $debut . ' ' . array_shift($todo); |
|
| 269 | + |
|
| 270 | + if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 271 | + spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 272 | + } else { |
|
| 273 | + if ($r[1]) { |
|
| 274 | + spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 275 | + } |
|
| 276 | + $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 277 | + if (function_exists($f)) { |
|
| 278 | + $f($r[2], $r[4], $serveur, $requeter); |
|
| 279 | + } else { |
|
| 280 | + spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + // Alter a plusieurs args. Faudrait optimiser. |
|
| 284 | + if ($todo) { |
|
| 285 | + spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 286 | + } |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 290 | - if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 291 | - spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 292 | - } else { |
|
| 293 | - [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 294 | - $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 295 | - if ($null) { |
|
| 296 | - $actions[] = "ALTER $old SET NOT NULL"; |
|
| 297 | - } else { |
|
| 298 | - $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - if ($d = ($default ?: $def2)) { |
|
| 302 | - $actions[] = "ALTER $old SET $d"; |
|
| 303 | - } else { |
|
| 304 | - $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 308 | - |
|
| 309 | - if ($old != $new) { |
|
| 310 | - spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 311 | - } |
|
| 312 | - } |
|
| 290 | + if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 291 | + spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 292 | + } else { |
|
| 293 | + [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 294 | + $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 295 | + if ($null) { |
|
| 296 | + $actions[] = "ALTER $old SET NOT NULL"; |
|
| 297 | + } else { |
|
| 298 | + $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + if ($d = ($default ?: $def2)) { |
|
| 302 | + $actions[] = "ALTER $old SET $d"; |
|
| 303 | + } else { |
|
| 304 | + $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 308 | + |
|
| 309 | + if ($old != $new) { |
|
| 310 | + spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | function spip_pg_alter_add($table, $arg, $serveur = '', $requeter = true) { |
| 316 | - $nom_index = null; |
|
| 317 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 318 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 319 | - |
|
| 320 | - return null; |
|
| 321 | - } |
|
| 322 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 323 | - preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 324 | - if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 325 | - $m[2] = $n[1]; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 329 | - } elseif ($r[1][0] == 'P') { |
|
| 330 | - // la primary peut etre sur plusieurs champs |
|
| 331 | - $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 332 | - $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 333 | - |
|
| 334 | - return spip_pg_query( |
|
| 335 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 336 | - $serveur, |
|
| 337 | - $requeter |
|
| 338 | - ); |
|
| 339 | - } else { |
|
| 340 | - preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 341 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 342 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 343 | - // |
|
| 344 | - // nom_index (colonnes) |
|
| 345 | - if ($m[2]) { |
|
| 346 | - $colonnes = substr($m[2], 1, -1); |
|
| 347 | - $nom_index = $m[1]; |
|
| 348 | - } else { |
|
| 349 | - // (colonne) |
|
| 350 | - if ($m[1][0] == '(') { |
|
| 351 | - $colonnes = substr($m[1], 1, -1); |
|
| 352 | - if (str_contains(',', $colonnes)) { |
|
| 353 | - spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 354 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 355 | - } else { |
|
| 356 | - $nom_index = $colonnes; |
|
| 357 | - } |
|
| 358 | - } // nom_index |
|
| 359 | - else { |
|
| 360 | - $nom_index = $colonnes = $m[1]; |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 365 | - } |
|
| 316 | + $nom_index = null; |
|
| 317 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 318 | + spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 319 | + |
|
| 320 | + return null; |
|
| 321 | + } |
|
| 322 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 323 | + preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 324 | + if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 325 | + $m[2] = $n[1]; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 329 | + } elseif ($r[1][0] == 'P') { |
|
| 330 | + // la primary peut etre sur plusieurs champs |
|
| 331 | + $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 332 | + $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 333 | + |
|
| 334 | + return spip_pg_query( |
|
| 335 | + "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 336 | + $serveur, |
|
| 337 | + $requeter |
|
| 338 | + ); |
|
| 339 | + } else { |
|
| 340 | + preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 341 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 342 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 343 | + // |
|
| 344 | + // nom_index (colonnes) |
|
| 345 | + if ($m[2]) { |
|
| 346 | + $colonnes = substr($m[2], 1, -1); |
|
| 347 | + $nom_index = $m[1]; |
|
| 348 | + } else { |
|
| 349 | + // (colonne) |
|
| 350 | + if ($m[1][0] == '(') { |
|
| 351 | + $colonnes = substr($m[1], 1, -1); |
|
| 352 | + if (str_contains(',', $colonnes)) { |
|
| 353 | + spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 354 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 355 | + } else { |
|
| 356 | + $nom_index = $colonnes; |
|
| 357 | + } |
|
| 358 | + } // nom_index |
|
| 359 | + else { |
|
| 360 | + $nom_index = $colonnes = $m[1]; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 365 | + } |
|
| 366 | 366 | } |
| 367 | 367 | |
| 368 | 368 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 369 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 370 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 371 | - } else { |
|
| 372 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 373 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 374 | - } elseif ($r[1][0] == 'P') { |
|
| 375 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 376 | - } else { |
|
| 377 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 378 | - } |
|
| 379 | - } |
|
| 369 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 370 | + spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 371 | + } else { |
|
| 372 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 373 | + return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 374 | + } elseif ($r[1][0] == 'P') { |
|
| 375 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 376 | + } else { |
|
| 377 | + return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | 380 | } |
| 381 | 381 | |
| 382 | 382 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 383 | - if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 384 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 385 | - } else { |
|
| 386 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 387 | - } |
|
| 383 | + if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 384 | + spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 385 | + } else { |
|
| 386 | + return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 387 | + } |
|
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | // attention (en pg) : |
@@ -392,17 +392,17 @@ discard block |
||
| 392 | 392 | // - alter table A rename X to Y = changer le nom de la colonne X en Y |
| 393 | 393 | // pour l'instant, traiter simplement RENAME TO X |
| 394 | 394 | function spip_pg_alter_rename($table, $arg, $serveur = '', $requeter = true) { |
| 395 | - $rename = ''; |
|
| 396 | - // si TO, mais pas au debut |
|
| 397 | - if (!stripos($arg, 'TO ')) { |
|
| 398 | - $rename = $arg; |
|
| 399 | - } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 400 | - $rename = $r[2]; |
|
| 401 | - } else { |
|
| 402 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 395 | + $rename = ''; |
|
| 396 | + // si TO, mais pas au debut |
|
| 397 | + if (!stripos($arg, 'TO ')) { |
|
| 398 | + $rename = $arg; |
|
| 399 | + } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 400 | + $rename = $r[2]; |
|
| 401 | + } else { |
|
| 402 | + spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | |
@@ -418,59 +418,59 @@ discard block |
||
| 418 | 418 | * @return bool ou requete |
| 419 | 419 | */ |
| 420 | 420 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 421 | - if (!($nom or $table or $champs)) { |
|
| 422 | - spip_log( |
|
| 423 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 424 | - 'pg.' . _LOG_ERREUR |
|
| 425 | - ); |
|
| 426 | - |
|
| 427 | - return false; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - $nom = str_replace('`', '', $nom); |
|
| 431 | - $champs = str_replace('`', '', $champs); |
|
| 432 | - |
|
| 433 | - // PG ne differentie pas noms des index en fonction des tables |
|
| 434 | - // il faut donc creer des noms uniques d'index pour une base pg |
|
| 435 | - $nom = $table . '_' . $nom; |
|
| 436 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 437 | - if (!is_array($champs)) { |
|
| 438 | - if ($champs[0] == '(') { |
|
| 439 | - $champs = substr($champs, 1, -1); |
|
| 440 | - } |
|
| 441 | - $champs = [$champs]; |
|
| 442 | - } |
|
| 443 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 444 | - if (!$requeter) { |
|
| 445 | - return $query; |
|
| 446 | - } |
|
| 447 | - $res = spip_pg_query($query, $serveur, $requeter); |
|
| 448 | - |
|
| 449 | - return $res; |
|
| 421 | + if (!($nom or $table or $champs)) { |
|
| 422 | + spip_log( |
|
| 423 | + "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 424 | + 'pg.' . _LOG_ERREUR |
|
| 425 | + ); |
|
| 426 | + |
|
| 427 | + return false; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + $nom = str_replace('`', '', $nom); |
|
| 431 | + $champs = str_replace('`', '', $champs); |
|
| 432 | + |
|
| 433 | + // PG ne differentie pas noms des index en fonction des tables |
|
| 434 | + // il faut donc creer des noms uniques d'index pour une base pg |
|
| 435 | + $nom = $table . '_' . $nom; |
|
| 436 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 437 | + if (!is_array($champs)) { |
|
| 438 | + if ($champs[0] == '(') { |
|
| 439 | + $champs = substr($champs, 1, -1); |
|
| 440 | + } |
|
| 441 | + $champs = [$champs]; |
|
| 442 | + } |
|
| 443 | + $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 444 | + if (!$requeter) { |
|
| 445 | + return $query; |
|
| 446 | + } |
|
| 447 | + $res = spip_pg_query($query, $serveur, $requeter); |
|
| 448 | + |
|
| 449 | + return $res; |
|
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | |
| 453 | 453 | function spip_pg_explain($query, $serveur = '', $requeter = true) { |
| 454 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 455 | - return []; |
|
| 456 | - } |
|
| 457 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 458 | - $prefixe = $connexion['prefixe']; |
|
| 459 | - $link = $connexion['link']; |
|
| 460 | - if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 461 | - $suite = strstr($query, (string) $regs[0]); |
|
| 462 | - $query = substr($query, 0, -strlen($suite)); |
|
| 463 | - } else { |
|
| 464 | - $suite = ''; |
|
| 465 | - } |
|
| 466 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 467 | - |
|
| 468 | - if (!$requeter) { |
|
| 469 | - return $query; |
|
| 470 | - } |
|
| 471 | - $r = spip_pg_query_simple($link, $query); |
|
| 472 | - |
|
| 473 | - return spip_pg_fetch($r, null, $serveur); |
|
| 454 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 455 | + return []; |
|
| 456 | + } |
|
| 457 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 458 | + $prefixe = $connexion['prefixe']; |
|
| 459 | + $link = $connexion['link']; |
|
| 460 | + if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 461 | + $suite = strstr($query, (string) $regs[0]); |
|
| 462 | + $query = substr($query, 0, -strlen($suite)); |
|
| 463 | + } else { |
|
| 464 | + $suite = ''; |
|
| 465 | + } |
|
| 466 | + $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 467 | + |
|
| 468 | + if (!$requeter) { |
|
| 469 | + return $query; |
|
| 470 | + } |
|
| 471 | + $r = spip_pg_query_simple($link, $query); |
|
| 472 | + |
|
| 473 | + return spip_pg_fetch($r, null, $serveur); |
|
| 474 | 474 | } |
| 475 | 475 | |
| 476 | 476 | |
@@ -489,92 +489,92 @@ discard block |
||
| 489 | 489 | * - False en cas d'erreur. |
| 490 | 490 | **/ |
| 491 | 491 | function spip_pg_selectdb($db, $serveur = '', $requeter = true) { |
| 492 | - // se connecter a la base indiquee |
|
| 493 | - // avec les identifiants connus |
|
| 494 | - $index = $serveur ? strtolower($serveur) : 0; |
|
| 495 | - |
|
| 496 | - if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 497 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 498 | - return $db; |
|
| 499 | - } |
|
| 500 | - } else { |
|
| 501 | - return false; |
|
| 502 | - } |
|
| 492 | + // se connecter a la base indiquee |
|
| 493 | + // avec les identifiants connus |
|
| 494 | + $index = $serveur ? strtolower($serveur) : 0; |
|
| 495 | + |
|
| 496 | + if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 497 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 498 | + return $db; |
|
| 499 | + } |
|
| 500 | + } else { |
|
| 501 | + return false; |
|
| 502 | + } |
|
| 503 | 503 | } |
| 504 | 504 | |
| 505 | 505 | // Qu'une seule base pour le moment |
| 506 | 506 | |
| 507 | 507 | function spip_pg_listdbs($serveur) { |
| 508 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 509 | - $link = $connexion['link']; |
|
| 510 | - $dbs = []; |
|
| 511 | - $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 512 | - while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 513 | - $dbs[] = reset($row); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - return $dbs; |
|
| 508 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 509 | + $link = $connexion['link']; |
|
| 510 | + $dbs = []; |
|
| 511 | + $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 512 | + while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 513 | + $dbs[] = reset($row); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + return $dbs; |
|
| 517 | 517 | } |
| 518 | 518 | |
| 519 | 519 | function spip_pg_select( |
| 520 | - $select, |
|
| 521 | - $from, |
|
| 522 | - $where = '', |
|
| 523 | - $groupby = [], |
|
| 524 | - $orderby = '', |
|
| 525 | - $limit = '', |
|
| 526 | - $having = '', |
|
| 527 | - $serveur = '', |
|
| 528 | - $requeter = true |
|
| 520 | + $select, |
|
| 521 | + $from, |
|
| 522 | + $where = '', |
|
| 523 | + $groupby = [], |
|
| 524 | + $orderby = '', |
|
| 525 | + $limit = '', |
|
| 526 | + $having = '', |
|
| 527 | + $serveur = '', |
|
| 528 | + $requeter = true |
|
| 529 | 529 | ) { |
| 530 | 530 | |
| 531 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 532 | - $prefixe = $connexion['prefixe']; |
|
| 533 | - $link = $connexion['link']; |
|
| 534 | - $db = $connexion['db']; |
|
| 535 | - |
|
| 536 | - $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 537 | - if ($limit) { |
|
| 538 | - $offset = $limatch[2]; |
|
| 539 | - $count = $limatch[3]; |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - $select = spip_pg_frommysql($select); |
|
| 543 | - |
|
| 544 | - // si pas de tri explicitement demande, le GROUP BY ne |
|
| 545 | - // contient que la clef primaire. |
|
| 546 | - // lui ajouter alors le champ de tri par defaut |
|
| 547 | - if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 548 | - $groupby[] = $groupbyplus[1]; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - $orderby = spip_pg_orderby($orderby, $select); |
|
| 552 | - |
|
| 553 | - if ($having) { |
|
| 554 | - if (is_array($having)) { |
|
| 555 | - $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 556 | - } |
|
| 557 | - } |
|
| 558 | - $from = spip_pg_from($from, $prefixe); |
|
| 559 | - $query = 'SELECT ' . $select |
|
| 560 | - . (!$from ? '' : "\nFROM $from") |
|
| 561 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 562 | - "\n\tAND ", |
|
| 563 | - array_map('calculer_pg_where', $where) |
|
| 564 | - ))))) |
|
| 565 | - . spip_pg_groupby($groupby, $from, $select) |
|
| 566 | - . (!$having ? '' : "\nHAVING $having") |
|
| 567 | - . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 568 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 569 | - |
|
| 570 | - // renvoyer la requete inerte si demandee |
|
| 571 | - if ($requeter === false) { |
|
| 572 | - return $query; |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - $r = spip_pg_trace_query($query, $serveur); |
|
| 576 | - |
|
| 577 | - return $r ?: $query; |
|
| 531 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 532 | + $prefixe = $connexion['prefixe']; |
|
| 533 | + $link = $connexion['link']; |
|
| 534 | + $db = $connexion['db']; |
|
| 535 | + |
|
| 536 | + $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 537 | + if ($limit) { |
|
| 538 | + $offset = $limatch[2]; |
|
| 539 | + $count = $limatch[3]; |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + $select = spip_pg_frommysql($select); |
|
| 543 | + |
|
| 544 | + // si pas de tri explicitement demande, le GROUP BY ne |
|
| 545 | + // contient que la clef primaire. |
|
| 546 | + // lui ajouter alors le champ de tri par defaut |
|
| 547 | + if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 548 | + $groupby[] = $groupbyplus[1]; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + $orderby = spip_pg_orderby($orderby, $select); |
|
| 552 | + |
|
| 553 | + if ($having) { |
|
| 554 | + if (is_array($having)) { |
|
| 555 | + $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 556 | + } |
|
| 557 | + } |
|
| 558 | + $from = spip_pg_from($from, $prefixe); |
|
| 559 | + $query = 'SELECT ' . $select |
|
| 560 | + . (!$from ? '' : "\nFROM $from") |
|
| 561 | + . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 562 | + "\n\tAND ", |
|
| 563 | + array_map('calculer_pg_where', $where) |
|
| 564 | + ))))) |
|
| 565 | + . spip_pg_groupby($groupby, $from, $select) |
|
| 566 | + . (!$having ? '' : "\nHAVING $having") |
|
| 567 | + . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 568 | + . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 569 | + |
|
| 570 | + // renvoyer la requete inerte si demandee |
|
| 571 | + if ($requeter === false) { |
|
| 572 | + return $query; |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + $r = spip_pg_trace_query($query, $serveur); |
|
| 576 | + |
|
| 577 | + return $r ?: $query; |
|
| 578 | 578 | ; |
| 579 | 579 | } |
| 580 | 580 | |
@@ -582,26 +582,26 @@ discard block |
||
| 582 | 582 | // car le reste de la requete utilise les alias (AS) systematiquement |
| 583 | 583 | |
| 584 | 584 | function spip_pg_from($from, $prefixe) { |
| 585 | - if (is_array($from)) { |
|
| 586 | - $from = spip_pg_select_as($from); |
|
| 587 | - } |
|
| 585 | + if (is_array($from)) { |
|
| 586 | + $from = spip_pg_select_as($from); |
|
| 587 | + } |
|
| 588 | 588 | |
| 589 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 589 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 590 | 590 | } |
| 591 | 591 | |
| 592 | 592 | function spip_pg_orderby($order, $select) { |
| 593 | - $res = []; |
|
| 594 | - $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 595 | - |
|
| 596 | - foreach ($arg as $v) { |
|
| 597 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 598 | - $res[] = $m[1]; |
|
| 599 | - } else { |
|
| 600 | - $res[] = $v; |
|
| 601 | - } |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - return spip_pg_frommysql(join(',', $res)); |
|
| 593 | + $res = []; |
|
| 594 | + $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 595 | + |
|
| 596 | + foreach ($arg as $v) { |
|
| 597 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 598 | + $res[] = $m[1]; |
|
| 599 | + } else { |
|
| 600 | + $res[] = $v; |
|
| 601 | + } |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + return spip_pg_frommysql(join(',', $res)); |
|
| 605 | 605 | } |
| 606 | 606 | |
| 607 | 607 | // Conversion a l'arrach' des jointures MySQL en jointures PG |
@@ -609,56 +609,56 @@ discard block |
||
| 609 | 609 | // et pour enlever les repetitions (sans incidence de perf, mais ca fait sale) |
| 610 | 610 | |
| 611 | 611 | function spip_pg_groupby($groupby, $from, $select) { |
| 612 | - $join = strpos($from, ','); |
|
| 613 | - // ismplifier avant de decouper |
|
| 614 | - if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 615 | - $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - if ($join or $groupby) { |
|
| 619 | - $join = is_array($select) ? $select : explode(', ', $select); |
|
| 620 | - } |
|
| 621 | - if ($join) { |
|
| 622 | - // enlever les 0 as points, '', ... |
|
| 623 | - foreach ($join as $k => $v) { |
|
| 624 | - $v = str_replace('DISTINCT ', '', $v); |
|
| 625 | - // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 626 | - $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 627 | - $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 628 | - // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 629 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 630 | - // idem sans AS (fetch numerique) |
|
| 631 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 632 | - // des AS simples : on garde le cote droit du AS |
|
| 633 | - $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 634 | - // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 635 | - if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 636 | - unset($join[$k]); |
|
| 637 | - } else { |
|
| 638 | - $join[$k] = trim($v); |
|
| 639 | - } |
|
| 640 | - } |
|
| 641 | - $join = array_diff($join, ['']); |
|
| 642 | - $join = implode(',', $join); |
|
| 643 | - } |
|
| 644 | - if (is_array($groupby)) { |
|
| 645 | - $groupby = join(',', $groupby); |
|
| 646 | - } |
|
| 647 | - if ($join) { |
|
| 648 | - $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 649 | - } |
|
| 650 | - if (!$groupby) { |
|
| 651 | - return ''; |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - $groupby = spip_pg_frommysql($groupby); |
|
| 655 | - // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 656 | - // issue de prepare_recherche |
|
| 657 | - $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 658 | - $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 659 | - $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 660 | - |
|
| 661 | - return "\nGROUP BY $groupby"; |
|
| 612 | + $join = strpos($from, ','); |
|
| 613 | + // ismplifier avant de decouper |
|
| 614 | + if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 615 | + $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + if ($join or $groupby) { |
|
| 619 | + $join = is_array($select) ? $select : explode(', ', $select); |
|
| 620 | + } |
|
| 621 | + if ($join) { |
|
| 622 | + // enlever les 0 as points, '', ... |
|
| 623 | + foreach ($join as $k => $v) { |
|
| 624 | + $v = str_replace('DISTINCT ', '', $v); |
|
| 625 | + // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 626 | + $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 627 | + $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 628 | + // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 629 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 630 | + // idem sans AS (fetch numerique) |
|
| 631 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 632 | + // des AS simples : on garde le cote droit du AS |
|
| 633 | + $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 634 | + // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 635 | + if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 636 | + unset($join[$k]); |
|
| 637 | + } else { |
|
| 638 | + $join[$k] = trim($v); |
|
| 639 | + } |
|
| 640 | + } |
|
| 641 | + $join = array_diff($join, ['']); |
|
| 642 | + $join = implode(',', $join); |
|
| 643 | + } |
|
| 644 | + if (is_array($groupby)) { |
|
| 645 | + $groupby = join(',', $groupby); |
|
| 646 | + } |
|
| 647 | + if ($join) { |
|
| 648 | + $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 649 | + } |
|
| 650 | + if (!$groupby) { |
|
| 651 | + return ''; |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + $groupby = spip_pg_frommysql($groupby); |
|
| 655 | + // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 656 | + // issue de prepare_recherche |
|
| 657 | + $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 658 | + $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 659 | + $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 660 | + |
|
| 661 | + return "\nGROUP BY $groupby"; |
|
| 662 | 662 | } |
| 663 | 663 | |
| 664 | 664 | // Conversion des operateurs MySQL en PG |
@@ -669,492 +669,492 @@ discard block |
||
| 669 | 669 | // A ameliorer. |
| 670 | 670 | |
| 671 | 671 | function spip_pg_frommysql($arg) { |
| 672 | - if (is_array($arg)) { |
|
| 673 | - $arg = join(', ', $arg); |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - $res = spip_pg_fromfield($arg); |
|
| 677 | - |
|
| 678 | - $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 679 | - |
|
| 680 | - $res = preg_replace( |
|
| 681 | - '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 682 | - 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 683 | - $res |
|
| 684 | - ); |
|
| 685 | - $res = preg_replace( |
|
| 686 | - '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 687 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 688 | - $res |
|
| 689 | - ); |
|
| 690 | - $res = preg_replace( |
|
| 691 | - '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 692 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 693 | - $res |
|
| 694 | - ); |
|
| 695 | - |
|
| 696 | - $res = preg_replace( |
|
| 697 | - '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 698 | - ' EXTRACT(epoch FROM NOW())', |
|
| 699 | - $res |
|
| 700 | - ); |
|
| 701 | - |
|
| 702 | - // la fonction md5(integer) n'est pas connu en pg |
|
| 703 | - // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 704 | - $res = preg_replace( |
|
| 705 | - '/md5\s*[(]([^)]*)[)]/i', |
|
| 706 | - 'MD5(CAST(\1 AS text))', |
|
| 707 | - $res |
|
| 708 | - ); |
|
| 709 | - |
|
| 710 | - $res = preg_replace( |
|
| 711 | - '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 712 | - ' EXTRACT(epoch FROM \1)', |
|
| 713 | - $res |
|
| 714 | - ); |
|
| 715 | - |
|
| 716 | - $res = preg_replace( |
|
| 717 | - '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 718 | - ' EXTRACT(day FROM \1)', |
|
| 719 | - $res |
|
| 720 | - ); |
|
| 721 | - |
|
| 722 | - $res = preg_replace( |
|
| 723 | - '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 724 | - ' EXTRACT(month FROM \1)', |
|
| 725 | - $res |
|
| 726 | - ); |
|
| 727 | - |
|
| 728 | - $res = preg_replace( |
|
| 729 | - '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 730 | - ' EXTRACT(year FROM \1)', |
|
| 731 | - $res |
|
| 732 | - ); |
|
| 733 | - |
|
| 734 | - $res = preg_replace( |
|
| 735 | - '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 736 | - ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 737 | - $res |
|
| 738 | - ); |
|
| 739 | - |
|
| 740 | - $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 741 | - |
|
| 742 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 743 | - |
|
| 744 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 745 | - |
|
| 746 | - $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 747 | - $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 748 | - $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 749 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 750 | - $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 751 | - |
|
| 752 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 753 | - $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 754 | - |
|
| 755 | - $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 756 | - $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 672 | + if (is_array($arg)) { |
|
| 673 | + $arg = join(', ', $arg); |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + $res = spip_pg_fromfield($arg); |
|
| 677 | + |
|
| 678 | + $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 679 | + |
|
| 680 | + $res = preg_replace( |
|
| 681 | + '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 682 | + 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 683 | + $res |
|
| 684 | + ); |
|
| 685 | + $res = preg_replace( |
|
| 686 | + '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 687 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 688 | + $res |
|
| 689 | + ); |
|
| 690 | + $res = preg_replace( |
|
| 691 | + '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 692 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 693 | + $res |
|
| 694 | + ); |
|
| 695 | + |
|
| 696 | + $res = preg_replace( |
|
| 697 | + '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 698 | + ' EXTRACT(epoch FROM NOW())', |
|
| 699 | + $res |
|
| 700 | + ); |
|
| 701 | + |
|
| 702 | + // la fonction md5(integer) n'est pas connu en pg |
|
| 703 | + // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 704 | + $res = preg_replace( |
|
| 705 | + '/md5\s*[(]([^)]*)[)]/i', |
|
| 706 | + 'MD5(CAST(\1 AS text))', |
|
| 707 | + $res |
|
| 708 | + ); |
|
| 709 | + |
|
| 710 | + $res = preg_replace( |
|
| 711 | + '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 712 | + ' EXTRACT(epoch FROM \1)', |
|
| 713 | + $res |
|
| 714 | + ); |
|
| 715 | + |
|
| 716 | + $res = preg_replace( |
|
| 717 | + '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 718 | + ' EXTRACT(day FROM \1)', |
|
| 719 | + $res |
|
| 720 | + ); |
|
| 721 | + |
|
| 722 | + $res = preg_replace( |
|
| 723 | + '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 724 | + ' EXTRACT(month FROM \1)', |
|
| 725 | + $res |
|
| 726 | + ); |
|
| 727 | + |
|
| 728 | + $res = preg_replace( |
|
| 729 | + '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 730 | + ' EXTRACT(year FROM \1)', |
|
| 731 | + $res |
|
| 732 | + ); |
|
| 733 | + |
|
| 734 | + $res = preg_replace( |
|
| 735 | + '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 736 | + ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 737 | + $res |
|
| 738 | + ); |
|
| 739 | + |
|
| 740 | + $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 741 | + |
|
| 742 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 743 | + |
|
| 744 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 745 | + |
|
| 746 | + $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 747 | + $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 748 | + $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 749 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 750 | + $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 751 | + |
|
| 752 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 753 | + $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 754 | + |
|
| 755 | + $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 756 | + $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 757 | 757 | # correct en theorie mais produit des debordements arithmetiques |
| 758 | 758 | # $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)(timestamp *'[^']*' *[+-] *timestamp *'[^']*') *[)]/", '\2', $res); |
| 759 | - $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 760 | - $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 759 | + $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 760 | + $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 761 | 761 | |
| 762 | - return str_replace('REGEXP', '~', $res); |
|
| 762 | + return str_replace('REGEXP', '~', $res); |
|
| 763 | 763 | } |
| 764 | 764 | |
| 765 | 765 | function spip_pg_fromfield($arg) { |
| 766 | - while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 767 | - preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 768 | - $res = ''; |
|
| 769 | - $n = 0; |
|
| 770 | - $index = $m[2]; |
|
| 771 | - foreach ($r[1] as $v) { |
|
| 772 | - $n++; |
|
| 773 | - $res .= "\nwhen $index=$v then $n"; |
|
| 774 | - } |
|
| 775 | - $arg = $m[1] . "case $res else 0 end " |
|
| 776 | - . substr($arg, strlen($m[0])); |
|
| 777 | - } |
|
| 778 | - |
|
| 779 | - return $arg; |
|
| 766 | + while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 767 | + preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 768 | + $res = ''; |
|
| 769 | + $n = 0; |
|
| 770 | + $index = $m[2]; |
|
| 771 | + foreach ($r[1] as $v) { |
|
| 772 | + $n++; |
|
| 773 | + $res .= "\nwhen $index=$v then $n"; |
|
| 774 | + } |
|
| 775 | + $arg = $m[1] . "case $res else 0 end " |
|
| 776 | + . substr($arg, strlen($m[0])); |
|
| 777 | + } |
|
| 778 | + |
|
| 779 | + return $arg; |
|
| 780 | 780 | } |
| 781 | 781 | |
| 782 | 782 | function calculer_pg_where($v) { |
| 783 | - if (!is_array($v)) { |
|
| 784 | - return spip_pg_frommysql($v); |
|
| 785 | - } |
|
| 786 | - |
|
| 787 | - $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 788 | - if (!($n = count($v))) { |
|
| 789 | - return $op; |
|
| 790 | - } else { |
|
| 791 | - $arg = calculer_pg_where(array_shift($v)); |
|
| 792 | - if ($n == 1) { |
|
| 793 | - return "$op($arg)"; |
|
| 794 | - } else { |
|
| 795 | - $arg2 = calculer_pg_where(array_shift($v)); |
|
| 796 | - if ($n == 2) { |
|
| 797 | - return "($arg $op $arg2)"; |
|
| 798 | - } else { |
|
| 799 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 800 | - } |
|
| 801 | - } |
|
| 802 | - } |
|
| 783 | + if (!is_array($v)) { |
|
| 784 | + return spip_pg_frommysql($v); |
|
| 785 | + } |
|
| 786 | + |
|
| 787 | + $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 788 | + if (!($n = count($v))) { |
|
| 789 | + return $op; |
|
| 790 | + } else { |
|
| 791 | + $arg = calculer_pg_where(array_shift($v)); |
|
| 792 | + if ($n == 1) { |
|
| 793 | + return "$op($arg)"; |
|
| 794 | + } else { |
|
| 795 | + $arg2 = calculer_pg_where(array_shift($v)); |
|
| 796 | + if ($n == 2) { |
|
| 797 | + return "($arg $op $arg2)"; |
|
| 798 | + } else { |
|
| 799 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 800 | + } |
|
| 801 | + } |
|
| 802 | + } |
|
| 803 | 803 | } |
| 804 | 804 | |
| 805 | 805 | |
| 806 | 806 | function calculer_pg_expression($expression, $v, $join = 'AND') { |
| 807 | - if (empty($v)) { |
|
| 808 | - return ''; |
|
| 809 | - } |
|
| 807 | + if (empty($v)) { |
|
| 808 | + return ''; |
|
| 809 | + } |
|
| 810 | 810 | |
| 811 | - $exp = "\n$expression "; |
|
| 811 | + $exp = "\n$expression "; |
|
| 812 | 812 | |
| 813 | - if (!is_array($v)) { |
|
| 814 | - $v = [$v]; |
|
| 815 | - } |
|
| 813 | + if (!is_array($v)) { |
|
| 814 | + $v = [$v]; |
|
| 815 | + } |
|
| 816 | 816 | |
| 817 | - if (strtoupper($join) === 'AND') { |
|
| 818 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 819 | - } else { |
|
| 820 | - return $exp . join($join, $v); |
|
| 821 | - } |
|
| 817 | + if (strtoupper($join) === 'AND') { |
|
| 818 | + return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 819 | + } else { |
|
| 820 | + return $exp . join($join, $v); |
|
| 821 | + } |
|
| 822 | 822 | } |
| 823 | 823 | |
| 824 | 824 | function spip_pg_select_as($args) { |
| 825 | - $argsas = ''; |
|
| 826 | - foreach ($args as $k => $v) { |
|
| 827 | - if (substr($k, -1) == '@') { |
|
| 828 | - // c'est une jointure qui se refere au from precedent |
|
| 829 | - // pas de virgule |
|
| 830 | - $argsas .= ' ' . $v; |
|
| 831 | - } else { |
|
| 832 | - $as = ''; |
|
| 833 | - // spip_log("$k : $v", _LOG_DEBUG); |
|
| 834 | - if (!is_numeric($k)) { |
|
| 835 | - if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 836 | - $v = $k; |
|
| 837 | - } elseif ($v != $k) { |
|
| 838 | - $p = strpos($v, ' '); |
|
| 839 | - if ($p) { |
|
| 840 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 841 | - } else { |
|
| 842 | - $as = " AS $k"; |
|
| 843 | - } |
|
| 844 | - } |
|
| 845 | - } |
|
| 846 | - // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 847 | - // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 848 | - $argsas .= ', ' . $v . $as; |
|
| 849 | - } |
|
| 850 | - } |
|
| 851 | - |
|
| 852 | - return substr($argsas, 2); |
|
| 825 | + $argsas = ''; |
|
| 826 | + foreach ($args as $k => $v) { |
|
| 827 | + if (substr($k, -1) == '@') { |
|
| 828 | + // c'est une jointure qui se refere au from precedent |
|
| 829 | + // pas de virgule |
|
| 830 | + $argsas .= ' ' . $v; |
|
| 831 | + } else { |
|
| 832 | + $as = ''; |
|
| 833 | + // spip_log("$k : $v", _LOG_DEBUG); |
|
| 834 | + if (!is_numeric($k)) { |
|
| 835 | + if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 836 | + $v = $k; |
|
| 837 | + } elseif ($v != $k) { |
|
| 838 | + $p = strpos($v, ' '); |
|
| 839 | + if ($p) { |
|
| 840 | + $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 841 | + } else { |
|
| 842 | + $as = " AS $k"; |
|
| 843 | + } |
|
| 844 | + } |
|
| 845 | + } |
|
| 846 | + // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 847 | + // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 848 | + $argsas .= ', ' . $v . $as; |
|
| 849 | + } |
|
| 850 | + } |
|
| 851 | + |
|
| 852 | + return substr($argsas, 2); |
|
| 853 | 853 | } |
| 854 | 854 | |
| 855 | 855 | function spip_pg_fetch($res, $t = '', $serveur = '', $requeter = true) { |
| 856 | 856 | |
| 857 | - if ($res) { |
|
| 858 | - $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 859 | - } |
|
| 857 | + if ($res) { |
|
| 858 | + $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 859 | + } |
|
| 860 | 860 | |
| 861 | - return $res; |
|
| 861 | + return $res; |
|
| 862 | 862 | } |
| 863 | 863 | |
| 864 | 864 | function spip_pg_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 865 | - if ($r) { |
|
| 866 | - return pg_result_seek($r, $row_number); |
|
| 867 | - } |
|
| 865 | + if ($r) { |
|
| 866 | + return pg_result_seek($r, $row_number); |
|
| 867 | + } |
|
| 868 | 868 | } |
| 869 | 869 | |
| 870 | 870 | |
| 871 | 871 | function spip_pg_countsel( |
| 872 | - $from = [], |
|
| 873 | - $where = [], |
|
| 874 | - $groupby = [], |
|
| 875 | - $having = [], |
|
| 876 | - $serveur = '', |
|
| 877 | - $requeter = true |
|
| 872 | + $from = [], |
|
| 873 | + $where = [], |
|
| 874 | + $groupby = [], |
|
| 875 | + $having = [], |
|
| 876 | + $serveur = '', |
|
| 877 | + $requeter = true |
|
| 878 | 878 | ) { |
| 879 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 880 | - $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 881 | - if (!$requeter) { |
|
| 882 | - return $r; |
|
| 883 | - } |
|
| 884 | - if (!is_resource($r)) { |
|
| 885 | - return 0; |
|
| 886 | - } |
|
| 887 | - [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 888 | - |
|
| 889 | - return $c; |
|
| 879 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 880 | + $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 881 | + if (!$requeter) { |
|
| 882 | + return $r; |
|
| 883 | + } |
|
| 884 | + if (!is_resource($r)) { |
|
| 885 | + return 0; |
|
| 886 | + } |
|
| 887 | + [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 888 | + |
|
| 889 | + return $c; |
|
| 890 | 890 | } |
| 891 | 891 | |
| 892 | 892 | function spip_pg_count($res, $serveur = '', $requeter = true) { |
| 893 | - return !$res ? 0 : pg_numrows($res); |
|
| 893 | + return !$res ? 0 : pg_numrows($res); |
|
| 894 | 894 | } |
| 895 | 895 | |
| 896 | 896 | function spip_pg_free($res, $serveur = '', $requeter = true) { |
| 897 | - // rien a faire en postgres |
|
| 897 | + // rien a faire en postgres |
|
| 898 | 898 | } |
| 899 | 899 | |
| 900 | 900 | function spip_pg_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 901 | 901 | |
| 902 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 903 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 902 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 903 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 904 | 904 | |
| 905 | - $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 906 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 905 | + $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 906 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 907 | 907 | |
| 908 | - // renvoyer la requete inerte si demandee |
|
| 909 | - if (!$requeter) { |
|
| 910 | - return $query; |
|
| 911 | - } |
|
| 908 | + // renvoyer la requete inerte si demandee |
|
| 909 | + if (!$requeter) { |
|
| 910 | + return $query; |
|
| 911 | + } |
|
| 912 | 912 | |
| 913 | - $res = spip_pg_trace_query($query, $serveur); |
|
| 914 | - if ($res) { |
|
| 915 | - return pg_affected_rows($res); |
|
| 916 | - } else { |
|
| 917 | - return false; |
|
| 918 | - } |
|
| 913 | + $res = spip_pg_trace_query($query, $serveur); |
|
| 914 | + if ($res) { |
|
| 915 | + return pg_affected_rows($res); |
|
| 916 | + } else { |
|
| 917 | + return false; |
|
| 918 | + } |
|
| 919 | 919 | } |
| 920 | 920 | |
| 921 | 921 | function spip_pg_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 922 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 923 | - $prefixe = $connexion['prefixe']; |
|
| 924 | - $link = $connexion['link']; |
|
| 925 | - |
|
| 926 | - if (!$desc) { |
|
| 927 | - $desc = description_table($table, $serveur); |
|
| 928 | - } |
|
| 929 | - $seq = spip_pg_sequence($table, true); |
|
| 930 | - // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 931 | - if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 932 | - $seq = spip_pg_sequence($table); |
|
| 933 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 934 | - $seq = "currval('$seq')"; |
|
| 935 | - } |
|
| 936 | - |
|
| 937 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 938 | - $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 939 | - $ins = (strlen($champs) < 3) |
|
| 940 | - ? ' DEFAULT VALUES' |
|
| 941 | - : "$champs VALUES $valeurs"; |
|
| 942 | - $q = "INSERT INTO $table $ins $ret"; |
|
| 943 | - if (!$requeter) { |
|
| 944 | - return $q; |
|
| 945 | - } |
|
| 946 | - $connexion['last'] = $q; |
|
| 947 | - $r = spip_pg_query_simple($link, $q); |
|
| 922 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 923 | + $prefixe = $connexion['prefixe']; |
|
| 924 | + $link = $connexion['link']; |
|
| 925 | + |
|
| 926 | + if (!$desc) { |
|
| 927 | + $desc = description_table($table, $serveur); |
|
| 928 | + } |
|
| 929 | + $seq = spip_pg_sequence($table, true); |
|
| 930 | + // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 931 | + if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 932 | + $seq = spip_pg_sequence($table); |
|
| 933 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 934 | + $seq = "currval('$seq')"; |
|
| 935 | + } |
|
| 936 | + |
|
| 937 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 938 | + $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 939 | + $ins = (strlen($champs) < 3) |
|
| 940 | + ? ' DEFAULT VALUES' |
|
| 941 | + : "$champs VALUES $valeurs"; |
|
| 942 | + $q = "INSERT INTO $table $ins $ret"; |
|
| 943 | + if (!$requeter) { |
|
| 944 | + return $q; |
|
| 945 | + } |
|
| 946 | + $connexion['last'] = $q; |
|
| 947 | + $r = spip_pg_query_simple($link, $q); |
|
| 948 | 948 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 949 | - if ($r) { |
|
| 950 | - if (!$ret) { |
|
| 951 | - return 0; |
|
| 952 | - } |
|
| 953 | - if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 954 | - return $r2[0]; |
|
| 955 | - } |
|
| 956 | - } |
|
| 957 | - |
|
| 958 | - return false; |
|
| 949 | + if ($r) { |
|
| 950 | + if (!$ret) { |
|
| 951 | + return 0; |
|
| 952 | + } |
|
| 953 | + if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 954 | + return $r2[0]; |
|
| 955 | + } |
|
| 956 | + } |
|
| 957 | + |
|
| 958 | + return false; |
|
| 959 | 959 | } |
| 960 | 960 | |
| 961 | 961 | function spip_pg_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 962 | 962 | |
| 963 | - if (!$desc) { |
|
| 964 | - $desc = description_table($table, $serveur); |
|
| 965 | - } |
|
| 966 | - if (!$desc) { |
|
| 967 | - die("$table insertion sans description"); |
|
| 968 | - } |
|
| 969 | - $fields = $desc['field']; |
|
| 970 | - |
|
| 971 | - foreach ($couples as $champ => $val) { |
|
| 972 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 973 | - } |
|
| 974 | - |
|
| 975 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 976 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 977 | - |
|
| 978 | - return spip_pg_insert( |
|
| 979 | - $table, |
|
| 980 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 981 | - '(' . join(',', $couples) . ')', |
|
| 982 | - $desc, |
|
| 983 | - $serveur, |
|
| 984 | - $requeter |
|
| 985 | - ); |
|
| 963 | + if (!$desc) { |
|
| 964 | + $desc = description_table($table, $serveur); |
|
| 965 | + } |
|
| 966 | + if (!$desc) { |
|
| 967 | + die("$table insertion sans description"); |
|
| 968 | + } |
|
| 969 | + $fields = $desc['field']; |
|
| 970 | + |
|
| 971 | + foreach ($couples as $champ => $val) { |
|
| 972 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 973 | + } |
|
| 974 | + |
|
| 975 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 976 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 977 | + |
|
| 978 | + return spip_pg_insert( |
|
| 979 | + $table, |
|
| 980 | + '(' . join(',', array_keys($couples)) . ')', |
|
| 981 | + '(' . join(',', $couples) . ')', |
|
| 982 | + $desc, |
|
| 983 | + $serveur, |
|
| 984 | + $requeter |
|
| 985 | + ); |
|
| 986 | 986 | } |
| 987 | 987 | |
| 988 | 988 | |
| 989 | 989 | function spip_pg_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 990 | 990 | |
| 991 | - if (!$desc) { |
|
| 992 | - $desc = description_table($table, $serveur); |
|
| 993 | - } |
|
| 994 | - if (!$desc) { |
|
| 995 | - die("$table insertion sans description"); |
|
| 996 | - } |
|
| 997 | - $fields = $desc['field'] ?? []; |
|
| 998 | - |
|
| 999 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1000 | - // une premiere fois pour ajouter maj dans les cles |
|
| 1001 | - $c = $tab_couples[0] ?? []; |
|
| 1002 | - $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1003 | - |
|
| 1004 | - $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1005 | - $valeurs = []; |
|
| 1006 | - foreach ($tab_couples as $couples) { |
|
| 1007 | - foreach ($couples as $champ => $val) { |
|
| 1008 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1009 | - } |
|
| 1010 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1011 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1012 | - |
|
| 1013 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1014 | - } |
|
| 1015 | - $valeurs = implode(', ', $valeurs); |
|
| 1016 | - |
|
| 1017 | - return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 991 | + if (!$desc) { |
|
| 992 | + $desc = description_table($table, $serveur); |
|
| 993 | + } |
|
| 994 | + if (!$desc) { |
|
| 995 | + die("$table insertion sans description"); |
|
| 996 | + } |
|
| 997 | + $fields = $desc['field'] ?? []; |
|
| 998 | + |
|
| 999 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1000 | + // une premiere fois pour ajouter maj dans les cles |
|
| 1001 | + $c = $tab_couples[0] ?? []; |
|
| 1002 | + $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1003 | + |
|
| 1004 | + $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1005 | + $valeurs = []; |
|
| 1006 | + foreach ($tab_couples as $couples) { |
|
| 1007 | + foreach ($couples as $champ => $val) { |
|
| 1008 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1009 | + } |
|
| 1010 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1011 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1012 | + |
|
| 1013 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1014 | + } |
|
| 1015 | + $valeurs = implode(', ', $valeurs); |
|
| 1016 | + |
|
| 1017 | + return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1018 | 1018 | } |
| 1019 | 1019 | |
| 1020 | 1020 | |
| 1021 | 1021 | function spip_pg_update($table, $couples, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1022 | 1022 | |
| 1023 | - if (!$couples) { |
|
| 1024 | - return; |
|
| 1025 | - } |
|
| 1026 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1027 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1023 | + if (!$couples) { |
|
| 1024 | + return; |
|
| 1025 | + } |
|
| 1026 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1027 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1028 | 1028 | |
| 1029 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1030 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1029 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1030 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1031 | 1031 | |
| 1032 | - $set = []; |
|
| 1033 | - foreach ($couples as $champ => $val) { |
|
| 1034 | - $set[] = $champ . '=' . $val; |
|
| 1035 | - } |
|
| 1032 | + $set = []; |
|
| 1033 | + foreach ($couples as $champ => $val) { |
|
| 1034 | + $set[] = $champ . '=' . $val; |
|
| 1035 | + } |
|
| 1036 | 1036 | |
| 1037 | - $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1038 | - . calculer_pg_expression('SET', $set, ',') |
|
| 1039 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1037 | + $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1038 | + . calculer_pg_expression('SET', $set, ',') |
|
| 1039 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1040 | 1040 | |
| 1041 | - // renvoyer la requete inerte si demandee |
|
| 1042 | - if (!$requeter) { |
|
| 1043 | - return $query; |
|
| 1044 | - } |
|
| 1041 | + // renvoyer la requete inerte si demandee |
|
| 1042 | + if (!$requeter) { |
|
| 1043 | + return $query; |
|
| 1044 | + } |
|
| 1045 | 1045 | |
| 1046 | - return spip_pg_trace_query($query, $serveur); |
|
| 1046 | + return spip_pg_trace_query($query, $serveur); |
|
| 1047 | 1047 | } |
| 1048 | 1048 | |
| 1049 | 1049 | // idem, mais les valeurs sont des constantes a mettre entre apostrophes |
| 1050 | 1050 | // sauf les expressions de date lorsqu'il s'agit de fonctions SQL (NOW etc) |
| 1051 | 1051 | function spip_pg_updateq($table, $couples, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1052 | - if (!$couples) { |
|
| 1053 | - return; |
|
| 1054 | - } |
|
| 1055 | - if (!$desc) { |
|
| 1056 | - $desc = description_table($table, $serveur); |
|
| 1057 | - } |
|
| 1058 | - $fields = $desc['field']; |
|
| 1059 | - foreach ($couples as $k => $val) { |
|
| 1060 | - $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1052 | + if (!$couples) { |
|
| 1053 | + return; |
|
| 1054 | + } |
|
| 1055 | + if (!$desc) { |
|
| 1056 | + $desc = description_table($table, $serveur); |
|
| 1057 | + } |
|
| 1058 | + $fields = $desc['field']; |
|
| 1059 | + foreach ($couples as $k => $val) { |
|
| 1060 | + $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1064 | 1064 | } |
| 1065 | 1065 | |
| 1066 | 1066 | |
| 1067 | 1067 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1068 | - if (!$values) { |
|
| 1069 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1070 | - |
|
| 1071 | - return 0; |
|
| 1072 | - } |
|
| 1073 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1074 | - $prefixe = $connexion['prefixe']; |
|
| 1075 | - $link = $connexion['link']; |
|
| 1076 | - |
|
| 1077 | - if (!$desc) { |
|
| 1078 | - $desc = description_table($table, $serveur); |
|
| 1079 | - } |
|
| 1080 | - if (!$desc) { |
|
| 1081 | - die("$table insertion sans description"); |
|
| 1082 | - } |
|
| 1083 | - $prim = $desc['key']['PRIMARY KEY']; |
|
| 1084 | - $ids = preg_split('/,\s*/', $prim); |
|
| 1085 | - $noprims = $prims = []; |
|
| 1086 | - foreach ($values as $k => $v) { |
|
| 1087 | - $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1088 | - |
|
| 1089 | - if (!in_array($k, $ids)) { |
|
| 1090 | - $noprims[$k] = "$k=$v"; |
|
| 1091 | - } else { |
|
| 1092 | - $prims[$k] = "$k=$v"; |
|
| 1093 | - } |
|
| 1094 | - } |
|
| 1095 | - |
|
| 1096 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1097 | - $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1098 | - |
|
| 1099 | - $where = join(' AND ', $prims); |
|
| 1100 | - if (!$where) { |
|
| 1101 | - return spip_pg_insert( |
|
| 1102 | - $table, |
|
| 1103 | - '(' . join(',', array_keys($values)) . ')', |
|
| 1104 | - '(' . join(',', $values) . ')', |
|
| 1105 | - $desc, |
|
| 1106 | - $serveur |
|
| 1107 | - ); |
|
| 1108 | - } |
|
| 1109 | - $couples = join(',', $noprims); |
|
| 1110 | - |
|
| 1111 | - $seq = spip_pg_sequence($table); |
|
| 1112 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 1113 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1114 | - |
|
| 1115 | - $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1116 | - if ($couples) { |
|
| 1117 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1068 | + if (!$values) { |
|
| 1069 | + spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1070 | + |
|
| 1071 | + return 0; |
|
| 1072 | + } |
|
| 1073 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1074 | + $prefixe = $connexion['prefixe']; |
|
| 1075 | + $link = $connexion['link']; |
|
| 1076 | + |
|
| 1077 | + if (!$desc) { |
|
| 1078 | + $desc = description_table($table, $serveur); |
|
| 1079 | + } |
|
| 1080 | + if (!$desc) { |
|
| 1081 | + die("$table insertion sans description"); |
|
| 1082 | + } |
|
| 1083 | + $prim = $desc['key']['PRIMARY KEY']; |
|
| 1084 | + $ids = preg_split('/,\s*/', $prim); |
|
| 1085 | + $noprims = $prims = []; |
|
| 1086 | + foreach ($values as $k => $v) { |
|
| 1087 | + $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1088 | + |
|
| 1089 | + if (!in_array($k, $ids)) { |
|
| 1090 | + $noprims[$k] = "$k=$v"; |
|
| 1091 | + } else { |
|
| 1092 | + $prims[$k] = "$k=$v"; |
|
| 1093 | + } |
|
| 1094 | + } |
|
| 1095 | + |
|
| 1096 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1097 | + $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1098 | + |
|
| 1099 | + $where = join(' AND ', $prims); |
|
| 1100 | + if (!$where) { |
|
| 1101 | + return spip_pg_insert( |
|
| 1102 | + $table, |
|
| 1103 | + '(' . join(',', array_keys($values)) . ')', |
|
| 1104 | + '(' . join(',', $values) . ')', |
|
| 1105 | + $desc, |
|
| 1106 | + $serveur |
|
| 1107 | + ); |
|
| 1108 | + } |
|
| 1109 | + $couples = join(',', $noprims); |
|
| 1110 | + |
|
| 1111 | + $seq = spip_pg_sequence($table); |
|
| 1112 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 1113 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1114 | + |
|
| 1115 | + $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1116 | + if ($couples) { |
|
| 1117 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1118 | 1118 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 1119 | - if (!$couples) { |
|
| 1120 | - return false; |
|
| 1121 | - } |
|
| 1122 | - $couples = pg_affected_rows($couples); |
|
| 1123 | - } |
|
| 1124 | - if (!$couples) { |
|
| 1125 | - $ret = !$seq ? '' : |
|
| 1126 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1127 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1128 | - ',', |
|
| 1129 | - $values |
|
| 1130 | - ) . ")$ret"; |
|
| 1131 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1132 | - if (!$couples) { |
|
| 1133 | - return false; |
|
| 1134 | - } elseif ($ret) { |
|
| 1135 | - $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1136 | - if ($r[0]) { |
|
| 1137 | - $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1138 | - // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1139 | - // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1140 | - $r = @pg_query($link, $q); |
|
| 1141 | - } |
|
| 1142 | - } |
|
| 1143 | - } |
|
| 1144 | - |
|
| 1145 | - return $couples; |
|
| 1119 | + if (!$couples) { |
|
| 1120 | + return false; |
|
| 1121 | + } |
|
| 1122 | + $couples = pg_affected_rows($couples); |
|
| 1123 | + } |
|
| 1124 | + if (!$couples) { |
|
| 1125 | + $ret = !$seq ? '' : |
|
| 1126 | + (" RETURNING nextval('$seq') < $prim"); |
|
| 1127 | + $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1128 | + ',', |
|
| 1129 | + $values |
|
| 1130 | + ) . ")$ret"; |
|
| 1131 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1132 | + if (!$couples) { |
|
| 1133 | + return false; |
|
| 1134 | + } elseif ($ret) { |
|
| 1135 | + $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1136 | + if ($r[0]) { |
|
| 1137 | + $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1138 | + // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1139 | + // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1140 | + $r = @pg_query($link, $q); |
|
| 1141 | + } |
|
| 1142 | + } |
|
| 1143 | + } |
|
| 1144 | + |
|
| 1145 | + return $couples; |
|
| 1146 | 1146 | } |
| 1147 | 1147 | |
| 1148 | 1148 | |
| 1149 | 1149 | function spip_pg_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1150 | - $retour = null; |
|
| 1151 | - // boucler pour traiter chaque requete independemment |
|
| 1152 | - foreach ($tab_couples as $couples) { |
|
| 1153 | - $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1154 | - } |
|
| 1155 | - |
|
| 1156 | - // renvoie le dernier id |
|
| 1157 | - return $retour; |
|
| 1150 | + $retour = null; |
|
| 1151 | + // boucler pour traiter chaque requete independemment |
|
| 1152 | + foreach ($tab_couples as $couples) { |
|
| 1153 | + $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1154 | + } |
|
| 1155 | + |
|
| 1156 | + // renvoie le dernier id |
|
| 1157 | + return $retour; |
|
| 1158 | 1158 | } |
| 1159 | 1159 | |
| 1160 | 1160 | |
@@ -1163,149 +1163,149 @@ discard block |
||
| 1163 | 1163 | |
| 1164 | 1164 | function spip_pg_sequence($table, $raw = false) { |
| 1165 | 1165 | |
| 1166 | - include_spip('base/serial'); |
|
| 1167 | - if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1168 | - return false; |
|
| 1169 | - } |
|
| 1170 | - $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1171 | - $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1172 | - if ( |
|
| 1173 | - !preg_match('/^\w+$/', $prim) |
|
| 1174 | - or !str_contains($desc['field'][$prim], 'int') |
|
| 1175 | - ) { |
|
| 1176 | - return ''; |
|
| 1177 | - } else { |
|
| 1178 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1179 | - } |
|
| 1166 | + include_spip('base/serial'); |
|
| 1167 | + if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1168 | + return false; |
|
| 1169 | + } |
|
| 1170 | + $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1171 | + $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1172 | + if ( |
|
| 1173 | + !preg_match('/^\w+$/', $prim) |
|
| 1174 | + or !str_contains($desc['field'][$prim], 'int') |
|
| 1175 | + ) { |
|
| 1176 | + return ''; |
|
| 1177 | + } else { |
|
| 1178 | + return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1179 | + } |
|
| 1180 | 1180 | } |
| 1181 | 1181 | |
| 1182 | 1182 | // Explicite les conversions de Mysql d'une valeur $v de type $t |
| 1183 | 1183 | // Dans le cas d'un champ date, pas d'apostrophe, c'est une syntaxe ad hoc |
| 1184 | 1184 | |
| 1185 | 1185 | function spip_pg_cite($v, $t) { |
| 1186 | - if (is_null($v)) { |
|
| 1187 | - return 'NULL'; |
|
| 1188 | - } // null php se traduit en NULL SQL |
|
| 1189 | - |
|
| 1190 | - if (sql_test_date($t)) { |
|
| 1191 | - if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1192 | - return spip_pg_frommysql($v); |
|
| 1193 | - } else { |
|
| 1194 | - if (strncmp($v, '0000', 4) == 0) { |
|
| 1195 | - $v = '0001' . substr($v, 4); |
|
| 1196 | - } |
|
| 1197 | - if (strpos($v, '-00-00') === 4) { |
|
| 1198 | - $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1199 | - } |
|
| 1200 | - |
|
| 1201 | - return "timestamp '$v'"; |
|
| 1202 | - } |
|
| 1203 | - } elseif (!sql_test_int($t)) { |
|
| 1204 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1205 | - } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1206 | - return $v; |
|
| 1207 | - } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1208 | - return substr($v, 1); |
|
| 1209 | - } else { |
|
| 1210 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1211 | - |
|
| 1212 | - return intval($v); |
|
| 1213 | - } |
|
| 1186 | + if (is_null($v)) { |
|
| 1187 | + return 'NULL'; |
|
| 1188 | + } // null php se traduit en NULL SQL |
|
| 1189 | + |
|
| 1190 | + if (sql_test_date($t)) { |
|
| 1191 | + if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1192 | + return spip_pg_frommysql($v); |
|
| 1193 | + } else { |
|
| 1194 | + if (strncmp($v, '0000', 4) == 0) { |
|
| 1195 | + $v = '0001' . substr($v, 4); |
|
| 1196 | + } |
|
| 1197 | + if (strpos($v, '-00-00') === 4) { |
|
| 1198 | + $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1199 | + } |
|
| 1200 | + |
|
| 1201 | + return "timestamp '$v'"; |
|
| 1202 | + } |
|
| 1203 | + } elseif (!sql_test_int($t)) { |
|
| 1204 | + return ("'" . pg_escape_string($v) . "'"); |
|
| 1205 | + } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1206 | + return $v; |
|
| 1207 | + } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1208 | + return substr($v, 1); |
|
| 1209 | + } else { |
|
| 1210 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1211 | + |
|
| 1212 | + return intval($v); |
|
| 1213 | + } |
|
| 1214 | 1214 | } |
| 1215 | 1215 | |
| 1216 | 1216 | function spip_pg_hex($v) { |
| 1217 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1217 | + return "CAST(x'" . $v . "' as bigint)"; |
|
| 1218 | 1218 | } |
| 1219 | 1219 | |
| 1220 | 1220 | function spip_pg_quote($v, $type = '') { |
| 1221 | - if (!is_array($v)) { |
|
| 1222 | - return spip_pg_cite($v, $type); |
|
| 1223 | - } |
|
| 1224 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1225 | - foreach ($v as $k => $r) { |
|
| 1226 | - $v[$k] = spip_pg_quote($r, $type); |
|
| 1227 | - } |
|
| 1228 | - |
|
| 1229 | - return join(',', $v); |
|
| 1221 | + if (!is_array($v)) { |
|
| 1222 | + return spip_pg_cite($v, $type); |
|
| 1223 | + } |
|
| 1224 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1225 | + foreach ($v as $k => $r) { |
|
| 1226 | + $v[$k] = spip_pg_quote($r, $type); |
|
| 1227 | + } |
|
| 1228 | + |
|
| 1229 | + return join(',', $v); |
|
| 1230 | 1230 | } |
| 1231 | 1231 | |
| 1232 | 1232 | function spip_pg_date_proche($champ, $interval, $unite) { |
| 1233 | - return '(' |
|
| 1234 | - . $champ |
|
| 1235 | - . (($interval <= 0) ? '>' : '<') |
|
| 1236 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1237 | - . '(' |
|
| 1238 | - . sql_quote(date('Y-m-d H:i:s')) |
|
| 1239 | - . ', INTERVAL ' |
|
| 1240 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1241 | - . ' ' |
|
| 1242 | - . $unite |
|
| 1243 | - . '))'; |
|
| 1233 | + return '(' |
|
| 1234 | + . $champ |
|
| 1235 | + . (($interval <= 0) ? '>' : '<') |
|
| 1236 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1237 | + . '(' |
|
| 1238 | + . sql_quote(date('Y-m-d H:i:s')) |
|
| 1239 | + . ', INTERVAL ' |
|
| 1240 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1241 | + . ' ' |
|
| 1242 | + . $unite |
|
| 1243 | + . '))'; |
|
| 1244 | 1244 | } |
| 1245 | 1245 | |
| 1246 | 1246 | function spip_pg_in($val, $valeurs, $not = '', $serveur = '') { |
| 1247 | 1247 | // |
| 1248 | 1248 | // IN (...) souvent limite a 255 elements, d'ou cette fonction assistante |
| 1249 | 1249 | // |
| 1250 | - // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1251 | - if (!$valeurs) { |
|
| 1252 | - return $not ? '0=0' : '0=1'; |
|
| 1253 | - } |
|
| 1254 | - if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1255 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1256 | - } |
|
| 1257 | - $n = $i = 0; |
|
| 1258 | - $in_sql = ''; |
|
| 1259 | - while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1260 | - if ((++$i) >= 255) { |
|
| 1261 | - $in_sql .= "($val $not IN (" . |
|
| 1262 | - substr($valeurs, 0, $n) . |
|
| 1263 | - "))\n" . |
|
| 1264 | - ($not ? "AND\t" : "OR\t"); |
|
| 1265 | - $valeurs = substr($valeurs, $n + 1); |
|
| 1266 | - $i = $n = 0; |
|
| 1267 | - } |
|
| 1268 | - } |
|
| 1269 | - $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1270 | - |
|
| 1271 | - return "($in_sql)"; |
|
| 1250 | + // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1251 | + if (!$valeurs) { |
|
| 1252 | + return $not ? '0=0' : '0=1'; |
|
| 1253 | + } |
|
| 1254 | + if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1255 | + return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1256 | + } |
|
| 1257 | + $n = $i = 0; |
|
| 1258 | + $in_sql = ''; |
|
| 1259 | + while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1260 | + if ((++$i) >= 255) { |
|
| 1261 | + $in_sql .= "($val $not IN (" . |
|
| 1262 | + substr($valeurs, 0, $n) . |
|
| 1263 | + "))\n" . |
|
| 1264 | + ($not ? "AND\t" : "OR\t"); |
|
| 1265 | + $valeurs = substr($valeurs, $n + 1); |
|
| 1266 | + $i = $n = 0; |
|
| 1267 | + } |
|
| 1268 | + } |
|
| 1269 | + $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1270 | + |
|
| 1271 | + return "($in_sql)"; |
|
| 1272 | 1272 | } |
| 1273 | 1273 | |
| 1274 | 1274 | function spip_pg_error($query = '', $serveur = '', $requeter = true) { |
| 1275 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1276 | - $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1277 | - if ($s) { |
|
| 1278 | - $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1279 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1280 | - } |
|
| 1281 | - |
|
| 1282 | - return $s; |
|
| 1275 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1276 | + $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1277 | + if ($s) { |
|
| 1278 | + $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1279 | + spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1280 | + } |
|
| 1281 | + |
|
| 1282 | + return $s; |
|
| 1283 | 1283 | } |
| 1284 | 1284 | |
| 1285 | 1285 | function spip_pg_errno($serveur = '') { |
| 1286 | - // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1287 | - // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1288 | - return 0; |
|
| 1286 | + // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1287 | + // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1288 | + return 0; |
|
| 1289 | 1289 | } |
| 1290 | 1290 | |
| 1291 | 1291 | function spip_pg_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 1292 | - if ($exist) { |
|
| 1293 | - $exist = ' IF EXISTS'; |
|
| 1294 | - } |
|
| 1295 | - if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1296 | - return true; |
|
| 1297 | - } else { |
|
| 1298 | - return false; |
|
| 1299 | - } |
|
| 1292 | + if ($exist) { |
|
| 1293 | + $exist = ' IF EXISTS'; |
|
| 1294 | + } |
|
| 1295 | + if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1296 | + return true; |
|
| 1297 | + } else { |
|
| 1298 | + return false; |
|
| 1299 | + } |
|
| 1300 | 1300 | } |
| 1301 | 1301 | |
| 1302 | 1302 | // supprime une vue |
| 1303 | 1303 | function spip_pg_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 1304 | - if ($exist) { |
|
| 1305 | - $exist = ' IF EXISTS'; |
|
| 1306 | - } |
|
| 1304 | + if ($exist) { |
|
| 1305 | + $exist = ' IF EXISTS'; |
|
| 1306 | + } |
|
| 1307 | 1307 | |
| 1308 | - return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1308 | + return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1309 | 1309 | } |
| 1310 | 1310 | |
| 1311 | 1311 | /** |
@@ -1322,40 +1322,40 @@ discard block |
||
| 1322 | 1322 | * Ressource à utiliser avec sql_fetch() |
| 1323 | 1323 | **/ |
| 1324 | 1324 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1325 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1326 | - $link = $connexion['link']; |
|
| 1327 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1325 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1326 | + $link = $connexion['link']; |
|
| 1327 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1328 | 1328 | |
| 1329 | - return spip_pg_query_simple($link, $q); |
|
| 1329 | + return spip_pg_query_simple($link, $q); |
|
| 1330 | 1330 | } |
| 1331 | 1331 | |
| 1332 | 1332 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1333 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1334 | - $link = $connexion['link']; |
|
| 1335 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1336 | - |
|
| 1337 | - $res = spip_pg_query_simple($link, $q); |
|
| 1338 | - if (!$res) { |
|
| 1339 | - return false; |
|
| 1340 | - } |
|
| 1341 | - |
|
| 1342 | - // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1343 | - // il faut en tenir compte dans le return |
|
| 1344 | - $fields = []; |
|
| 1345 | - while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1346 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1347 | - } |
|
| 1348 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1349 | - $res = spip_pg_query_simple($link, $q); |
|
| 1350 | - $keys = []; |
|
| 1351 | - while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1352 | - if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1353 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1354 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1355 | - } |
|
| 1356 | - } |
|
| 1357 | - |
|
| 1358 | - return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1333 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1334 | + $link = $connexion['link']; |
|
| 1335 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1336 | + |
|
| 1337 | + $res = spip_pg_query_simple($link, $q); |
|
| 1338 | + if (!$res) { |
|
| 1339 | + return false; |
|
| 1340 | + } |
|
| 1341 | + |
|
| 1342 | + // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1343 | + // il faut en tenir compte dans le return |
|
| 1344 | + $fields = []; |
|
| 1345 | + while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1346 | + $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1347 | + } |
|
| 1348 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1349 | + $res = spip_pg_query_simple($link, $q); |
|
| 1350 | + $keys = []; |
|
| 1351 | + while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1352 | + if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1353 | + $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1354 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1355 | + } |
|
| 1356 | + } |
|
| 1357 | + |
|
| 1358 | + return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1359 | 1359 | } |
| 1360 | 1360 | |
| 1361 | 1361 | // Fonction de creation d'une table SQL nommee $nom |
@@ -1366,116 +1366,116 @@ discard block |
||
| 1366 | 1366 | // Le nom des index est prefixe par celui de la table pour eviter les conflits |
| 1367 | 1367 | function spip_pg_create($nom, $champs, $cles, $autoinc = false, $temporary = false, $serveur = '', $requeter = true) { |
| 1368 | 1368 | |
| 1369 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1370 | - $link = $connexion['link']; |
|
| 1371 | - $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1372 | - |
|
| 1373 | - $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1374 | - $keys = []; |
|
| 1375 | - |
|
| 1376 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1377 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1378 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 1379 | - return; |
|
| 1380 | - } |
|
| 1381 | - |
|
| 1382 | - foreach ($cles as $k => $v) { |
|
| 1383 | - if (strpos($k, 'KEY ') === 0) { |
|
| 1384 | - $n = str_replace('`', '', $k); |
|
| 1385 | - $v = str_replace('`', '"', $v); |
|
| 1386 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1387 | - if ($k != $n) { |
|
| 1388 | - $i = "\"$i\""; |
|
| 1389 | - } |
|
| 1390 | - $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1391 | - } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1392 | - $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1393 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1394 | - } else { |
|
| 1395 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1396 | - } |
|
| 1397 | - if ($k == 'PRIMARY KEY') { |
|
| 1398 | - $prim_name = $v; |
|
| 1399 | - } |
|
| 1400 | - $s = ','; |
|
| 1401 | - } |
|
| 1402 | - $s = ''; |
|
| 1403 | - |
|
| 1404 | - $character_set = ''; |
|
| 1405 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1406 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1407 | - } |
|
| 1408 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1409 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1410 | - } |
|
| 1411 | - |
|
| 1412 | - foreach ($champs as $k => $v) { |
|
| 1413 | - $k = str_replace('`', '"', $k); |
|
| 1414 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1415 | - if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1416 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1417 | - } |
|
| 1418 | - } |
|
| 1419 | - |
|
| 1420 | - $query .= "$s\n\t\t$k " |
|
| 1421 | - . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1422 | - ? ' bigserial' |
|
| 1423 | - : mysql2pg_type($v) |
|
| 1424 | - ); |
|
| 1425 | - $s = ','; |
|
| 1426 | - } |
|
| 1427 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1428 | - |
|
| 1429 | - // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1430 | - |
|
| 1431 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1432 | - ($character_set ? " DEFAULT $character_set" : '') |
|
| 1433 | - . "\n"; |
|
| 1434 | - |
|
| 1435 | - if (!$requeter) { |
|
| 1436 | - return $q; |
|
| 1437 | - } |
|
| 1438 | - $connexion['last'] = $q; |
|
| 1439 | - $r = @pg_query($link, $q); |
|
| 1440 | - |
|
| 1441 | - if (!$r) { |
|
| 1442 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1443 | - } else { |
|
| 1444 | - foreach ($keys as $index) { |
|
| 1445 | - pg_query($link, $index); |
|
| 1446 | - } |
|
| 1447 | - } |
|
| 1448 | - |
|
| 1449 | - return $r; |
|
| 1369 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1370 | + $link = $connexion['link']; |
|
| 1371 | + $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1372 | + |
|
| 1373 | + $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1374 | + $keys = []; |
|
| 1375 | + |
|
| 1376 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1377 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1378 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 1379 | + return; |
|
| 1380 | + } |
|
| 1381 | + |
|
| 1382 | + foreach ($cles as $k => $v) { |
|
| 1383 | + if (strpos($k, 'KEY ') === 0) { |
|
| 1384 | + $n = str_replace('`', '', $k); |
|
| 1385 | + $v = str_replace('`', '"', $v); |
|
| 1386 | + $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1387 | + if ($k != $n) { |
|
| 1388 | + $i = "\"$i\""; |
|
| 1389 | + } |
|
| 1390 | + $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1391 | + } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1392 | + $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1393 | + $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1394 | + } else { |
|
| 1395 | + $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1396 | + } |
|
| 1397 | + if ($k == 'PRIMARY KEY') { |
|
| 1398 | + $prim_name = $v; |
|
| 1399 | + } |
|
| 1400 | + $s = ','; |
|
| 1401 | + } |
|
| 1402 | + $s = ''; |
|
| 1403 | + |
|
| 1404 | + $character_set = ''; |
|
| 1405 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1406 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1407 | + } |
|
| 1408 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1409 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1410 | + } |
|
| 1411 | + |
|
| 1412 | + foreach ($champs as $k => $v) { |
|
| 1413 | + $k = str_replace('`', '"', $k); |
|
| 1414 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1415 | + if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1416 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1417 | + } |
|
| 1418 | + } |
|
| 1419 | + |
|
| 1420 | + $query .= "$s\n\t\t$k " |
|
| 1421 | + . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1422 | + ? ' bigserial' |
|
| 1423 | + : mysql2pg_type($v) |
|
| 1424 | + ); |
|
| 1425 | + $s = ','; |
|
| 1426 | + } |
|
| 1427 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1428 | + |
|
| 1429 | + // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1430 | + |
|
| 1431 | + $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1432 | + ($character_set ? " DEFAULT $character_set" : '') |
|
| 1433 | + . "\n"; |
|
| 1434 | + |
|
| 1435 | + if (!$requeter) { |
|
| 1436 | + return $q; |
|
| 1437 | + } |
|
| 1438 | + $connexion['last'] = $q; |
|
| 1439 | + $r = @pg_query($link, $q); |
|
| 1440 | + |
|
| 1441 | + if (!$r) { |
|
| 1442 | + spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1443 | + } else { |
|
| 1444 | + foreach ($keys as $index) { |
|
| 1445 | + pg_query($link, $index); |
|
| 1446 | + } |
|
| 1447 | + } |
|
| 1448 | + |
|
| 1449 | + return $r; |
|
| 1450 | 1450 | } |
| 1451 | 1451 | |
| 1452 | 1452 | |
| 1453 | 1453 | function spip_pg_create_base($nom, $serveur = '', $requeter = true) { |
| 1454 | - return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1454 | + return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1455 | 1455 | } |
| 1456 | 1456 | |
| 1457 | 1457 | // Fonction de creation d'une vue SQL nommee $nom |
| 1458 | 1458 | function spip_pg_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 1459 | - if (!$query_select) { |
|
| 1460 | - return false; |
|
| 1461 | - } |
|
| 1462 | - // vue deja presente |
|
| 1463 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 1464 | - if ($requeter) { |
|
| 1465 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1466 | - } |
|
| 1459 | + if (!$query_select) { |
|
| 1460 | + return false; |
|
| 1461 | + } |
|
| 1462 | + // vue deja presente |
|
| 1463 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 1464 | + if ($requeter) { |
|
| 1465 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1466 | + } |
|
| 1467 | 1467 | |
| 1468 | - return false; |
|
| 1469 | - } |
|
| 1468 | + return false; |
|
| 1469 | + } |
|
| 1470 | 1470 | |
| 1471 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1471 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1472 | 1472 | |
| 1473 | - return spip_pg_query($query, $serveur, $requeter); |
|
| 1473 | + return spip_pg_query($query, $serveur, $requeter); |
|
| 1474 | 1474 | } |
| 1475 | 1475 | |
| 1476 | 1476 | |
| 1477 | 1477 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1478 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1478 | + spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1479 | 1479 | } |
| 1480 | 1480 | |
| 1481 | 1481 | |
@@ -1488,50 +1488,50 @@ discard block |
||
| 1488 | 1488 | * @return bool|string true / false / requete |
| 1489 | 1489 | **/ |
| 1490 | 1490 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1491 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1491 | + return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1492 | 1492 | } |
| 1493 | 1493 | |
| 1494 | 1494 | // Selectionner la sous-chaine dans $objet |
| 1495 | 1495 | // correspondant a $lang. Cf balise Multi de Spip |
| 1496 | 1496 | |
| 1497 | 1497 | function spip_pg_multi($objet, $lang) { |
| 1498 | - $r = 'regexp_replace(' |
|
| 1499 | - . $objet |
|
| 1500 | - . ",'<multi>.*[[]" |
|
| 1501 | - . $lang |
|
| 1502 | - . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1498 | + $r = 'regexp_replace(' |
|
| 1499 | + . $objet |
|
| 1500 | + . ",'<multi>.*[[]" |
|
| 1501 | + . $lang |
|
| 1502 | + . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1503 | 1503 | |
| 1504 | - return $r; |
|
| 1504 | + return $r; |
|
| 1505 | 1505 | } |
| 1506 | 1506 | |
| 1507 | 1507 | // Palanquee d'idiosyncrasies MySQL dans les creations de table |
| 1508 | 1508 | // A completer par les autres, mais essayer de reduire en amont. |
| 1509 | 1509 | |
| 1510 | 1510 | function mysql2pg_type($v) { |
| 1511 | - $remplace = [ |
|
| 1512 | - '/auto_increment/i' => '', // non reconnu |
|
| 1513 | - '/bigint/i' => 'bigint', |
|
| 1514 | - '/mediumint/i' => 'mediumint', |
|
| 1515 | - '/smallint/i' => 'smallint', |
|
| 1516 | - '/tinyint/i' => 'int', |
|
| 1517 | - '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1518 | - '/longtext/i' => 'text', |
|
| 1519 | - '/mediumtext/i' => 'text', |
|
| 1520 | - '/tinytext/i' => 'text', |
|
| 1521 | - '/longblob/i' => 'text', |
|
| 1522 | - '/0000-00-00/' => '0001-01-01', |
|
| 1523 | - '/datetime/i' => 'timestamp', |
|
| 1524 | - '/unsigned/i' => '', |
|
| 1525 | - '/double/i' => 'double precision', |
|
| 1526 | - '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1527 | - '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1528 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1529 | - ]; |
|
| 1530 | - |
|
| 1531 | - return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1511 | + $remplace = [ |
|
| 1512 | + '/auto_increment/i' => '', // non reconnu |
|
| 1513 | + '/bigint/i' => 'bigint', |
|
| 1514 | + '/mediumint/i' => 'mediumint', |
|
| 1515 | + '/smallint/i' => 'smallint', |
|
| 1516 | + '/tinyint/i' => 'int', |
|
| 1517 | + '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1518 | + '/longtext/i' => 'text', |
|
| 1519 | + '/mediumtext/i' => 'text', |
|
| 1520 | + '/tinytext/i' => 'text', |
|
| 1521 | + '/longblob/i' => 'text', |
|
| 1522 | + '/0000-00-00/' => '0001-01-01', |
|
| 1523 | + '/datetime/i' => 'timestamp', |
|
| 1524 | + '/unsigned/i' => '', |
|
| 1525 | + '/double/i' => 'double precision', |
|
| 1526 | + '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1527 | + '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1528 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1529 | + ]; |
|
| 1530 | + |
|
| 1531 | + return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1532 | 1532 | } |
| 1533 | 1533 | |
| 1534 | 1534 | // Renvoie false si on n'a pas les fonctions pg (pour l'install) |
| 1535 | 1535 | function spip_versions_pg() { |
| 1536 | - return function_exists('pg_connect'); |
|
| 1536 | + return function_exists('pg_connect'); |
|
| 1537 | 1537 | } |
@@ -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 |
@@ -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 | // TODO: get/set_caracteres ? |
@@ -44,91 +44,91 @@ discard block |
||
| 44 | 44 | */ |
| 45 | 45 | function req_sqlite_dist($addr, $port, $login, $pass, $db = '', $prefixe = '', $sqlite_version = '') |
| 46 | 46 | { |
| 47 | - static $last_connect = []; |
|
| 48 | - |
|
| 49 | - // si provient de selectdb |
|
| 50 | - // un code pour etre sur que l'on vient de select_db() |
|
| 51 | - if (strpos($db, $code = '@selectdb@') !== false) { |
|
| 52 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 53 | - $$a = $last_connect[$a]; |
|
| 54 | - } |
|
| 55 | - $db = str_replace($code, '', $db); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /* |
|
| 47 | + static $last_connect = []; |
|
| 48 | + |
|
| 49 | + // si provient de selectdb |
|
| 50 | + // un code pour etre sur que l'on vient de select_db() |
|
| 51 | + if (strpos($db, $code = '@selectdb@') !== false) { |
|
| 52 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 53 | + $$a = $last_connect[$a]; |
|
| 54 | + } |
|
| 55 | + $db = str_replace($code, '', $db); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /* |
|
| 59 | 59 | * En sqlite, seule l'adresse du fichier est importante. |
| 60 | 60 | * Ce sera $db le nom, |
| 61 | 61 | * le path est $addr |
| 62 | 62 | * (_DIR_DB si $addr est vide) |
| 63 | 63 | */ |
| 64 | - _sqlite_init(); |
|
| 65 | - |
|
| 66 | - // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 67 | - $f = _DIR_DB; |
|
| 68 | - if ($addr and str_contains($addr, '/')) { |
|
| 69 | - $f = rtrim($addr, '/') . '/'; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 73 | - // il faut que la base existe ou que le repertoire parent soit writable |
|
| 74 | - if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 75 | - spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 76 | - |
|
| 77 | - return false; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - // charger les modules sqlite au besoin |
|
| 81 | - if (!_sqlite_charger_version($sqlite_version)) { |
|
| 82 | - spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 83 | - |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // chargement des constantes |
|
| 88 | - // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 89 | - $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 90 | - $define(); |
|
| 91 | - |
|
| 92 | - $ok = false; |
|
| 93 | - if (!$db) { |
|
| 94 | - // si pas de db -> |
|
| 95 | - // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 96 | - // pour tester la connexion |
|
| 97 | - $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 98 | - $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 99 | - $ok = $link = new \PDO("sqlite:$tmp"); |
|
| 100 | - } else { |
|
| 101 | - // Ouvrir (eventuellement creer la base) |
|
| 102 | - $ok = $link = new \PDO("sqlite:$f"); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - if (!$ok) { |
|
| 106 | - $e = _sqlite_last_error_from_link($link); |
|
| 107 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 108 | - |
|
| 109 | - return false; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - if ($link) { |
|
| 113 | - $last_connect = [ |
|
| 114 | - 'addr' => $addr, |
|
| 115 | - 'port' => $port, |
|
| 116 | - 'login' => $login, |
|
| 117 | - 'pass' => $pass, |
|
| 118 | - 'db' => $db, |
|
| 119 | - 'prefixe' => $prefixe, |
|
| 120 | - ]; |
|
| 121 | - // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 122 | - include_spip('req/sqlite_fonctions'); |
|
| 123 | - _sqlite_init_functions($link); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return [ |
|
| 127 | - 'db' => $db, |
|
| 128 | - 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 129 | - 'link' => $link, |
|
| 130 | - 'total_requetes' => 0, |
|
| 131 | - ]; |
|
| 64 | + _sqlite_init(); |
|
| 65 | + |
|
| 66 | + // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 67 | + $f = _DIR_DB; |
|
| 68 | + if ($addr and str_contains($addr, '/')) { |
|
| 69 | + $f = rtrim($addr, '/') . '/'; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 73 | + // il faut que la base existe ou que le repertoire parent soit writable |
|
| 74 | + if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 75 | + spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 76 | + |
|
| 77 | + return false; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + // charger les modules sqlite au besoin |
|
| 81 | + if (!_sqlite_charger_version($sqlite_version)) { |
|
| 82 | + spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 83 | + |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // chargement des constantes |
|
| 88 | + // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 89 | + $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 90 | + $define(); |
|
| 91 | + |
|
| 92 | + $ok = false; |
|
| 93 | + if (!$db) { |
|
| 94 | + // si pas de db -> |
|
| 95 | + // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 96 | + // pour tester la connexion |
|
| 97 | + $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 98 | + $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 99 | + $ok = $link = new \PDO("sqlite:$tmp"); |
|
| 100 | + } else { |
|
| 101 | + // Ouvrir (eventuellement creer la base) |
|
| 102 | + $ok = $link = new \PDO("sqlite:$f"); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + if (!$ok) { |
|
| 106 | + $e = _sqlite_last_error_from_link($link); |
|
| 107 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 108 | + |
|
| 109 | + return false; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + if ($link) { |
|
| 113 | + $last_connect = [ |
|
| 114 | + 'addr' => $addr, |
|
| 115 | + 'port' => $port, |
|
| 116 | + 'login' => $login, |
|
| 117 | + 'pass' => $pass, |
|
| 118 | + 'db' => $db, |
|
| 119 | + 'prefixe' => $prefixe, |
|
| 120 | + ]; |
|
| 121 | + // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 122 | + include_spip('req/sqlite_fonctions'); |
|
| 123 | + _sqlite_init_functions($link); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return [ |
|
| 127 | + 'db' => $db, |
|
| 128 | + 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 129 | + 'link' => $link, |
|
| 130 | + 'total_requetes' => 0, |
|
| 131 | + ]; |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | |
@@ -148,14 +148,14 @@ discard block |
||
| 148 | 148 | */ |
| 149 | 149 | function spip_sqlite_query($query, $serveur = '', $requeter = true) |
| 150 | 150 | { |
| 151 | - #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 152 | - #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 153 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 154 | - if (!$requeter) { |
|
| 155 | - return $query; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - return Sqlite::executer_requete($query, $serveur); |
|
| 151 | + #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 152 | + #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 153 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 154 | + if (!$requeter) { |
|
| 155 | + return $query; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + return Sqlite::executer_requete($query, $serveur); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | |
@@ -173,11 +173,11 @@ discard block |
||
| 173 | 173 | function spip_sqlite_alter($query, $serveur = '', $requeter = true) |
| 174 | 174 | { |
| 175 | 175 | |
| 176 | - $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 177 | - // traduire la requete pour recuperer les bons noms de table |
|
| 178 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 176 | + $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 177 | + // traduire la requete pour recuperer les bons noms de table |
|
| 178 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 179 | 179 | |
| 180 | - /* |
|
| 180 | + /* |
|
| 181 | 181 | * la il faut faire les transformations |
| 182 | 182 | * si ALTER TABLE x (DROP|CHANGE) y |
| 183 | 183 | * |
@@ -186,251 +186,251 @@ discard block |
||
| 186 | 186 | * 3) faire chaque requete independemment |
| 187 | 187 | */ |
| 188 | 188 | |
| 189 | - // 1 |
|
| 190 | - if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 191 | - $debut = $regs[1]; |
|
| 192 | - $table = $regs[3]; |
|
| 193 | - $suite = $regs[4]; |
|
| 194 | - } else { |
|
| 195 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 196 | - |
|
| 197 | - return false; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // 2 |
|
| 201 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 202 | - // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 203 | - // ou revoir l'api de sql_alter en creant un |
|
| 204 | - // sql_alter_table($table,array($actions)); |
|
| 205 | - $todo = explode(',', $suite); |
|
| 206 | - |
|
| 207 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 208 | - $todo2 = []; |
|
| 209 | - $i = 0; |
|
| 210 | - $ouverte = false; |
|
| 211 | - while ($do = array_shift($todo)) { |
|
| 212 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 213 | - $o = (str_contains($do, '(')); |
|
| 214 | - $f = (str_contains($do, ')')); |
|
| 215 | - if ($o and !$f) { |
|
| 216 | - $ouverte = true; |
|
| 217 | - } elseif ($f) { |
|
| 218 | - $ouverte = false; |
|
| 219 | - } |
|
| 220 | - if (!$ouverte) { |
|
| 221 | - $i++; |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - // 3 |
|
| 226 | - $resultats = []; |
|
| 227 | - foreach ($todo2 as $do) { |
|
| 228 | - $do = trim($do); |
|
| 229 | - if ( |
|
| 230 | - !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 231 | - . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 232 | - . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 233 | - . '|ADD COLUMN|ADD' |
|
| 234 | - . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 235 | - ) { |
|
| 236 | - spip_log( |
|
| 237 | - "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 238 | - 'sqlite.' . _LOG_ERREUR |
|
| 239 | - ); |
|
| 240 | - |
|
| 241 | - return false; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - $cle = strtoupper($matches[1]); |
|
| 245 | - $colonne_origine = $matches[2]; |
|
| 246 | - $colonne_destination = ''; |
|
| 247 | - |
|
| 248 | - $def = $matches[3]; |
|
| 249 | - |
|
| 250 | - // eluder une eventuelle clause before|after|first inutilisable |
|
| 251 | - $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 252 | - $defo = $defr; // garder la def d'origine pour certains cas |
|
| 253 | - // remplacer les definitions venant de mysql |
|
| 254 | - $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 255 | - |
|
| 256 | - // reinjecter dans le do |
|
| 257 | - $do = str_replace($def, $defr, $do); |
|
| 258 | - $def = $defr; |
|
| 259 | - |
|
| 260 | - switch ($cle) { |
|
| 261 | - // suppression d'un index |
|
| 262 | - case 'DROP KEY': |
|
| 263 | - case 'DROP INDEX': |
|
| 264 | - $nom_index = $colonne_origine; |
|
| 265 | - spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 266 | - break; |
|
| 267 | - |
|
| 268 | - // suppression d'une pk |
|
| 269 | - case 'DROP PRIMARY KEY': |
|
| 270 | - if ( |
|
| 271 | - !_sqlite_modifier_table( |
|
| 272 | - $table, |
|
| 273 | - $colonne_origine, |
|
| 274 | - ['key' => ['PRIMARY KEY' => '']], |
|
| 275 | - $serveur |
|
| 276 | - ) |
|
| 277 | - ) { |
|
| 278 | - return false; |
|
| 279 | - } |
|
| 280 | - break; |
|
| 281 | - // suppression d'une colonne |
|
| 282 | - case 'DROP COLUMN': |
|
| 283 | - case 'DROP': |
|
| 284 | - if ( |
|
| 285 | - !_sqlite_modifier_table( |
|
| 286 | - $table, |
|
| 287 | - [$colonne_origine => ''], |
|
| 288 | - [], |
|
| 289 | - $serveur |
|
| 290 | - ) |
|
| 291 | - ) { |
|
| 292 | - return false; |
|
| 293 | - } |
|
| 294 | - break; |
|
| 295 | - |
|
| 296 | - case 'CHANGE COLUMN': |
|
| 297 | - case 'CHANGE': |
|
| 298 | - // recuperer le nom de la future colonne |
|
| 299 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 300 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 301 | - $def = trim($defo); |
|
| 302 | - $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 303 | - $def = substr($def, strlen($colonne_destination) + 1); |
|
| 304 | - |
|
| 305 | - if ( |
|
| 306 | - !_sqlite_modifier_table( |
|
| 307 | - $table, |
|
| 308 | - [$colonne_origine => $colonne_destination], |
|
| 309 | - ['field' => [$colonne_destination => $def]], |
|
| 310 | - $serveur |
|
| 311 | - ) |
|
| 312 | - ) { |
|
| 313 | - return false; |
|
| 314 | - } |
|
| 315 | - break; |
|
| 316 | - |
|
| 317 | - case 'MODIFY': |
|
| 318 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 319 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 320 | - if ( |
|
| 321 | - !_sqlite_modifier_table( |
|
| 322 | - $table, |
|
| 323 | - $colonne_origine, |
|
| 324 | - ['field' => [$colonne_origine => $defo]], |
|
| 325 | - $serveur |
|
| 326 | - ) |
|
| 327 | - ) { |
|
| 328 | - return false; |
|
| 329 | - } |
|
| 330 | - break; |
|
| 331 | - |
|
| 332 | - // pas geres en sqlite2 |
|
| 333 | - case 'RENAME': |
|
| 334 | - $do = 'RENAME TO' . substr($do, 6); |
|
| 335 | - case 'RENAME TO': |
|
| 336 | - if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 337 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 338 | - |
|
| 339 | - return false; |
|
| 340 | - } |
|
| 341 | - break; |
|
| 342 | - |
|
| 343 | - // ajout d'une pk |
|
| 344 | - case 'ADD PRIMARY KEY': |
|
| 345 | - $pk = trim(substr($do, 16)); |
|
| 346 | - $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 347 | - if ( |
|
| 348 | - !_sqlite_modifier_table( |
|
| 349 | - $table, |
|
| 350 | - $colonne_origine, |
|
| 351 | - ['key' => ['PRIMARY KEY' => $pk]], |
|
| 352 | - $serveur |
|
| 353 | - ) |
|
| 354 | - ) { |
|
| 355 | - return false; |
|
| 356 | - } |
|
| 357 | - break; |
|
| 358 | - // ajout d'un index |
|
| 359 | - case 'ADD UNIQUE KEY': |
|
| 360 | - case 'ADD UNIQUE': |
|
| 361 | - $unique = true; |
|
| 362 | - case 'ADD INDEX': |
|
| 363 | - case 'ADD KEY': |
|
| 364 | - if (!isset($unique)) { |
|
| 365 | - $unique = false; |
|
| 366 | - } |
|
| 367 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 368 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 369 | - // |
|
| 370 | - // nom_index (colonnes) |
|
| 371 | - if ($def) { |
|
| 372 | - $colonnes = substr($def, 1, -1); |
|
| 373 | - $nom_index = $colonne_origine; |
|
| 374 | - } else { |
|
| 375 | - // (colonne) |
|
| 376 | - if ($colonne_origine[0] == '(') { |
|
| 377 | - $colonnes = substr($colonne_origine, 1, -1); |
|
| 378 | - if (str_contains(',', $colonnes)) { |
|
| 379 | - spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 380 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 381 | - break; |
|
| 382 | - } else { |
|
| 383 | - $nom_index = $colonnes; |
|
| 384 | - } |
|
| 385 | - } // nom_index |
|
| 386 | - else { |
|
| 387 | - $nom_index = $colonnes = $colonne_origine; |
|
| 388 | - } |
|
| 389 | - } |
|
| 390 | - spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 391 | - break; |
|
| 392 | - |
|
| 393 | - // pas geres en sqlite2 |
|
| 394 | - case 'ADD COLUMN': |
|
| 395 | - $do = 'ADD' . substr($do, 10); |
|
| 396 | - case 'ADD': |
|
| 397 | - default: |
|
| 398 | - if (!preg_match(',primary\s+key,i', $do)) { |
|
| 399 | - if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 400 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 401 | - |
|
| 402 | - return false; |
|
| 403 | - } |
|
| 404 | - break; |
|
| 405 | - } |
|
| 406 | - // ou si la colonne est aussi primary key |
|
| 407 | - // cas du add id_truc int primary key |
|
| 408 | - // ajout d'une colonne qui passe en primary key directe |
|
| 409 | - else { |
|
| 410 | - $def = trim(substr($do, 3)); |
|
| 411 | - $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 412 | - $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 413 | - $opts = []; |
|
| 414 | - if (preg_match(',primary\s+key,i', $def)) { |
|
| 415 | - $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 416 | - $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 417 | - } |
|
| 418 | - $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 419 | - if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 420 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 421 | - |
|
| 422 | - return false; |
|
| 423 | - } |
|
| 424 | - } |
|
| 425 | - break; |
|
| 426 | - } |
|
| 427 | - // tout est bon, ouf ! |
|
| 428 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 432 | - |
|
| 433 | - return true; |
|
| 189 | + // 1 |
|
| 190 | + if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 191 | + $debut = $regs[1]; |
|
| 192 | + $table = $regs[3]; |
|
| 193 | + $suite = $regs[4]; |
|
| 194 | + } else { |
|
| 195 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 196 | + |
|
| 197 | + return false; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // 2 |
|
| 201 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 202 | + // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 203 | + // ou revoir l'api de sql_alter en creant un |
|
| 204 | + // sql_alter_table($table,array($actions)); |
|
| 205 | + $todo = explode(',', $suite); |
|
| 206 | + |
|
| 207 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 208 | + $todo2 = []; |
|
| 209 | + $i = 0; |
|
| 210 | + $ouverte = false; |
|
| 211 | + while ($do = array_shift($todo)) { |
|
| 212 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 213 | + $o = (str_contains($do, '(')); |
|
| 214 | + $f = (str_contains($do, ')')); |
|
| 215 | + if ($o and !$f) { |
|
| 216 | + $ouverte = true; |
|
| 217 | + } elseif ($f) { |
|
| 218 | + $ouverte = false; |
|
| 219 | + } |
|
| 220 | + if (!$ouverte) { |
|
| 221 | + $i++; |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + // 3 |
|
| 226 | + $resultats = []; |
|
| 227 | + foreach ($todo2 as $do) { |
|
| 228 | + $do = trim($do); |
|
| 229 | + if ( |
|
| 230 | + !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 231 | + . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 232 | + . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 233 | + . '|ADD COLUMN|ADD' |
|
| 234 | + . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 235 | + ) { |
|
| 236 | + spip_log( |
|
| 237 | + "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 238 | + 'sqlite.' . _LOG_ERREUR |
|
| 239 | + ); |
|
| 240 | + |
|
| 241 | + return false; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + $cle = strtoupper($matches[1]); |
|
| 245 | + $colonne_origine = $matches[2]; |
|
| 246 | + $colonne_destination = ''; |
|
| 247 | + |
|
| 248 | + $def = $matches[3]; |
|
| 249 | + |
|
| 250 | + // eluder une eventuelle clause before|after|first inutilisable |
|
| 251 | + $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 252 | + $defo = $defr; // garder la def d'origine pour certains cas |
|
| 253 | + // remplacer les definitions venant de mysql |
|
| 254 | + $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 255 | + |
|
| 256 | + // reinjecter dans le do |
|
| 257 | + $do = str_replace($def, $defr, $do); |
|
| 258 | + $def = $defr; |
|
| 259 | + |
|
| 260 | + switch ($cle) { |
|
| 261 | + // suppression d'un index |
|
| 262 | + case 'DROP KEY': |
|
| 263 | + case 'DROP INDEX': |
|
| 264 | + $nom_index = $colonne_origine; |
|
| 265 | + spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 266 | + break; |
|
| 267 | + |
|
| 268 | + // suppression d'une pk |
|
| 269 | + case 'DROP PRIMARY KEY': |
|
| 270 | + if ( |
|
| 271 | + !_sqlite_modifier_table( |
|
| 272 | + $table, |
|
| 273 | + $colonne_origine, |
|
| 274 | + ['key' => ['PRIMARY KEY' => '']], |
|
| 275 | + $serveur |
|
| 276 | + ) |
|
| 277 | + ) { |
|
| 278 | + return false; |
|
| 279 | + } |
|
| 280 | + break; |
|
| 281 | + // suppression d'une colonne |
|
| 282 | + case 'DROP COLUMN': |
|
| 283 | + case 'DROP': |
|
| 284 | + if ( |
|
| 285 | + !_sqlite_modifier_table( |
|
| 286 | + $table, |
|
| 287 | + [$colonne_origine => ''], |
|
| 288 | + [], |
|
| 289 | + $serveur |
|
| 290 | + ) |
|
| 291 | + ) { |
|
| 292 | + return false; |
|
| 293 | + } |
|
| 294 | + break; |
|
| 295 | + |
|
| 296 | + case 'CHANGE COLUMN': |
|
| 297 | + case 'CHANGE': |
|
| 298 | + // recuperer le nom de la future colonne |
|
| 299 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 300 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 301 | + $def = trim($defo); |
|
| 302 | + $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 303 | + $def = substr($def, strlen($colonne_destination) + 1); |
|
| 304 | + |
|
| 305 | + if ( |
|
| 306 | + !_sqlite_modifier_table( |
|
| 307 | + $table, |
|
| 308 | + [$colonne_origine => $colonne_destination], |
|
| 309 | + ['field' => [$colonne_destination => $def]], |
|
| 310 | + $serveur |
|
| 311 | + ) |
|
| 312 | + ) { |
|
| 313 | + return false; |
|
| 314 | + } |
|
| 315 | + break; |
|
| 316 | + |
|
| 317 | + case 'MODIFY': |
|
| 318 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 319 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 320 | + if ( |
|
| 321 | + !_sqlite_modifier_table( |
|
| 322 | + $table, |
|
| 323 | + $colonne_origine, |
|
| 324 | + ['field' => [$colonne_origine => $defo]], |
|
| 325 | + $serveur |
|
| 326 | + ) |
|
| 327 | + ) { |
|
| 328 | + return false; |
|
| 329 | + } |
|
| 330 | + break; |
|
| 331 | + |
|
| 332 | + // pas geres en sqlite2 |
|
| 333 | + case 'RENAME': |
|
| 334 | + $do = 'RENAME TO' . substr($do, 6); |
|
| 335 | + case 'RENAME TO': |
|
| 336 | + if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 337 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 338 | + |
|
| 339 | + return false; |
|
| 340 | + } |
|
| 341 | + break; |
|
| 342 | + |
|
| 343 | + // ajout d'une pk |
|
| 344 | + case 'ADD PRIMARY KEY': |
|
| 345 | + $pk = trim(substr($do, 16)); |
|
| 346 | + $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 347 | + if ( |
|
| 348 | + !_sqlite_modifier_table( |
|
| 349 | + $table, |
|
| 350 | + $colonne_origine, |
|
| 351 | + ['key' => ['PRIMARY KEY' => $pk]], |
|
| 352 | + $serveur |
|
| 353 | + ) |
|
| 354 | + ) { |
|
| 355 | + return false; |
|
| 356 | + } |
|
| 357 | + break; |
|
| 358 | + // ajout d'un index |
|
| 359 | + case 'ADD UNIQUE KEY': |
|
| 360 | + case 'ADD UNIQUE': |
|
| 361 | + $unique = true; |
|
| 362 | + case 'ADD INDEX': |
|
| 363 | + case 'ADD KEY': |
|
| 364 | + if (!isset($unique)) { |
|
| 365 | + $unique = false; |
|
| 366 | + } |
|
| 367 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 368 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 369 | + // |
|
| 370 | + // nom_index (colonnes) |
|
| 371 | + if ($def) { |
|
| 372 | + $colonnes = substr($def, 1, -1); |
|
| 373 | + $nom_index = $colonne_origine; |
|
| 374 | + } else { |
|
| 375 | + // (colonne) |
|
| 376 | + if ($colonne_origine[0] == '(') { |
|
| 377 | + $colonnes = substr($colonne_origine, 1, -1); |
|
| 378 | + if (str_contains(',', $colonnes)) { |
|
| 379 | + spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 380 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 381 | + break; |
|
| 382 | + } else { |
|
| 383 | + $nom_index = $colonnes; |
|
| 384 | + } |
|
| 385 | + } // nom_index |
|
| 386 | + else { |
|
| 387 | + $nom_index = $colonnes = $colonne_origine; |
|
| 388 | + } |
|
| 389 | + } |
|
| 390 | + spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 391 | + break; |
|
| 392 | + |
|
| 393 | + // pas geres en sqlite2 |
|
| 394 | + case 'ADD COLUMN': |
|
| 395 | + $do = 'ADD' . substr($do, 10); |
|
| 396 | + case 'ADD': |
|
| 397 | + default: |
|
| 398 | + if (!preg_match(',primary\s+key,i', $do)) { |
|
| 399 | + if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 400 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 401 | + |
|
| 402 | + return false; |
|
| 403 | + } |
|
| 404 | + break; |
|
| 405 | + } |
|
| 406 | + // ou si la colonne est aussi primary key |
|
| 407 | + // cas du add id_truc int primary key |
|
| 408 | + // ajout d'une colonne qui passe en primary key directe |
|
| 409 | + else { |
|
| 410 | + $def = trim(substr($do, 3)); |
|
| 411 | + $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 412 | + $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 413 | + $opts = []; |
|
| 414 | + if (preg_match(',primary\s+key,i', $def)) { |
|
| 415 | + $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 416 | + $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 417 | + } |
|
| 418 | + $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 419 | + if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 420 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 421 | + |
|
| 422 | + return false; |
|
| 423 | + } |
|
| 424 | + } |
|
| 425 | + break; |
|
| 426 | + } |
|
| 427 | + // tout est bon, ouf ! |
|
| 428 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 432 | + |
|
| 433 | + return true; |
|
| 434 | 434 | } |
| 435 | 435 | |
| 436 | 436 | /** |
@@ -452,38 +452,38 @@ discard block |
||
| 452 | 452 | * - true si la requête réussie, false sinon. |
| 453 | 453 | */ |
| 454 | 454 | function spip_sqlite_create( |
| 455 | - $nom, |
|
| 456 | - $champs, |
|
| 457 | - $cles, |
|
| 458 | - $autoinc = false, |
|
| 459 | - $temporary = false, |
|
| 460 | - $serveur = '', |
|
| 461 | - $requeter = true |
|
| 455 | + $nom, |
|
| 456 | + $champs, |
|
| 457 | + $cles, |
|
| 458 | + $autoinc = false, |
|
| 459 | + $temporary = false, |
|
| 460 | + $serveur = '', |
|
| 461 | + $requeter = true |
|
| 462 | 462 | ) { |
| 463 | - $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 464 | - if (!$query) { |
|
| 465 | - return false; |
|
| 466 | - } |
|
| 467 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 468 | - |
|
| 469 | - // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 470 | - // il faut donc les faire creer ensuite |
|
| 471 | - if (!$requeter) { |
|
| 472 | - return $res; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - $ok = $res ? true : false; |
|
| 476 | - if ($ok) { |
|
| 477 | - foreach ($cles as $k => $v) { |
|
| 478 | - if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 479 | - $index = trim(substr($k, strlen($m[1]))); |
|
| 480 | - $unique = (strlen($m[1]) > 3); |
|
| 481 | - $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - return $ok ? true : false; |
|
| 463 | + $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 464 | + if (!$query) { |
|
| 465 | + return false; |
|
| 466 | + } |
|
| 467 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 468 | + |
|
| 469 | + // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 470 | + // il faut donc les faire creer ensuite |
|
| 471 | + if (!$requeter) { |
|
| 472 | + return $res; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + $ok = $res ? true : false; |
|
| 476 | + if ($ok) { |
|
| 477 | + foreach ($cles as $k => $v) { |
|
| 478 | + if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 479 | + $index = trim(substr($k, strlen($m[1]))); |
|
| 480 | + $unique = (strlen($m[1]) > 3); |
|
| 481 | + $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + return $ok ? true : false; |
|
| 487 | 487 | } |
| 488 | 488 | |
| 489 | 489 | /** |
@@ -497,21 +497,21 @@ discard block |
||
| 497 | 497 | **/ |
| 498 | 498 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) |
| 499 | 499 | { |
| 500 | - $f = $nom . '.sqlite'; |
|
| 501 | - if (strpos($nom, '/') === false) { |
|
| 502 | - $f = _DIR_DB . $f; |
|
| 503 | - } |
|
| 500 | + $f = $nom . '.sqlite'; |
|
| 501 | + if (strpos($nom, '/') === false) { |
|
| 502 | + $f = _DIR_DB . $f; |
|
| 503 | + } |
|
| 504 | 504 | |
| 505 | - $ok = new \PDO("sqlite:$f"); |
|
| 505 | + $ok = new \PDO("sqlite:$f"); |
|
| 506 | 506 | |
| 507 | - if ($ok) { |
|
| 508 | - unset($ok); |
|
| 507 | + if ($ok) { |
|
| 508 | + unset($ok); |
|
| 509 | 509 | |
| 510 | - return true; |
|
| 511 | - } |
|
| 512 | - unset($ok); |
|
| 510 | + return true; |
|
| 511 | + } |
|
| 512 | + unset($ok); |
|
| 513 | 513 | |
| 514 | - return false; |
|
| 514 | + return false; |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | |
@@ -533,22 +533,22 @@ discard block |
||
| 533 | 533 | */ |
| 534 | 534 | function spip_sqlite_create_view($nom, $query_select, $serveur = '', $requeter = true) |
| 535 | 535 | { |
| 536 | - if (!$query_select) { |
|
| 537 | - return false; |
|
| 538 | - } |
|
| 539 | - // vue deja presente |
|
| 540 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 541 | - spip_log( |
|
| 542 | - "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 543 | - 'sqlite.' . _LOG_ERREUR |
|
| 544 | - ); |
|
| 545 | - |
|
| 546 | - return false; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 550 | - |
|
| 551 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 536 | + if (!$query_select) { |
|
| 537 | + return false; |
|
| 538 | + } |
|
| 539 | + // vue deja presente |
|
| 540 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 541 | + spip_log( |
|
| 542 | + "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 543 | + 'sqlite.' . _LOG_ERREUR |
|
| 544 | + ); |
|
| 545 | + |
|
| 546 | + return false; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 550 | + |
|
| 551 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | /** |
@@ -571,54 +571,54 @@ discard block |
||
| 571 | 571 | */ |
| 572 | 572 | function spip_sqlite_create_index($nom, $table, $champs, $unique = '', $serveur = '', $requeter = true) |
| 573 | 573 | { |
| 574 | - if (!($nom or $table or $champs)) { |
|
| 575 | - spip_log( |
|
| 576 | - "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 577 | - 'sqlite.' . _LOG_ERREUR |
|
| 578 | - ); |
|
| 579 | - |
|
| 580 | - return false; |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 584 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 585 | - $nom = $table . '_' . $nom; |
|
| 586 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 587 | - if (!is_array($champs)) { |
|
| 588 | - if ($champs[0] == '(') { |
|
| 589 | - $champs = substr($champs, 1, -1); |
|
| 590 | - } |
|
| 591 | - $champs = [$champs]; |
|
| 592 | - // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 593 | - $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - $ifnotexists = ''; |
|
| 597 | - $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 598 | - if (!function_exists('spip_version_compare')) { |
|
| 599 | - include_spip('plugins/installer'); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 603 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 604 | - } else { |
|
| 605 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 606 | - $a = spip_sqlite_showtable($table, $serveur); |
|
| 607 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 608 | - return true; |
|
| 609 | - } |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 613 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 614 | - if (!$requeter) { |
|
| 615 | - return $res; |
|
| 616 | - } |
|
| 617 | - if ($res) { |
|
| 618 | - return true; |
|
| 619 | - } else { |
|
| 620 | - return false; |
|
| 621 | - } |
|
| 574 | + if (!($nom or $table or $champs)) { |
|
| 575 | + spip_log( |
|
| 576 | + "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 577 | + 'sqlite.' . _LOG_ERREUR |
|
| 578 | + ); |
|
| 579 | + |
|
| 580 | + return false; |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 584 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 585 | + $nom = $table . '_' . $nom; |
|
| 586 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 587 | + if (!is_array($champs)) { |
|
| 588 | + if ($champs[0] == '(') { |
|
| 589 | + $champs = substr($champs, 1, -1); |
|
| 590 | + } |
|
| 591 | + $champs = [$champs]; |
|
| 592 | + // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 593 | + $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + $ifnotexists = ''; |
|
| 597 | + $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 598 | + if (!function_exists('spip_version_compare')) { |
|
| 599 | + include_spip('plugins/installer'); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 603 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 604 | + } else { |
|
| 605 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 606 | + $a = spip_sqlite_showtable($table, $serveur); |
|
| 607 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 608 | + return true; |
|
| 609 | + } |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 613 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 614 | + if (!$requeter) { |
|
| 615 | + return $res; |
|
| 616 | + } |
|
| 617 | + if ($res) { |
|
| 618 | + return true; |
|
| 619 | + } else { |
|
| 620 | + return false; |
|
| 621 | + } |
|
| 622 | 622 | } |
| 623 | 623 | |
| 624 | 624 | /** |
@@ -636,31 +636,31 @@ discard block |
||
| 636 | 636 | */ |
| 637 | 637 | function spip_sqlite_count($r, $serveur = '', $requeter = true) |
| 638 | 638 | { |
| 639 | - if (!$r) { |
|
| 640 | - return 0; |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - // select ou autre (insert, update,...) ? |
|
| 644 | - // (link,requete) a compter |
|
| 645 | - if (is_array($r->spipSqliteRowCount)) { |
|
| 646 | - list($link, $query) = $r->spipSqliteRowCount; |
|
| 647 | - // amelioration possible a tester intensivement : pas de order by pour compter ! |
|
| 648 | - // $query = preg_replace(",ORDER BY .+(LIMIT\s|HAVING\s|GROUP BY\s|$),Uims","\\1",$query); |
|
| 649 | - $query = "SELECT count(*) as zzzzsqlitecount FROM ($query)"; |
|
| 650 | - $l = $link->query($query); |
|
| 651 | - $i = 0; |
|
| 652 | - if ($l and $z = $l->fetch()) { |
|
| 653 | - $i = $z['zzzzsqlitecount']; |
|
| 654 | - } |
|
| 655 | - $r->spipSqliteRowCount = $i; |
|
| 656 | - } |
|
| 657 | - if (isset($r->spipSqliteRowCount)) { |
|
| 658 | - // Ce compte est faux s'il y a des limit dans la requete :( |
|
| 659 | - // il retourne le nombre d'enregistrements sans le limit |
|
| 660 | - return $r->spipSqliteRowCount; |
|
| 661 | - } else { |
|
| 662 | - return $r->rowCount(); |
|
| 663 | - } |
|
| 639 | + if (!$r) { |
|
| 640 | + return 0; |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + // select ou autre (insert, update,...) ? |
|
| 644 | + // (link,requete) a compter |
|
| 645 | + if (is_array($r->spipSqliteRowCount)) { |
|
| 646 | + list($link, $query) = $r->spipSqliteRowCount; |
|
| 647 | + // amelioration possible a tester intensivement : pas de order by pour compter ! |
|
| 648 | + // $query = preg_replace(",ORDER BY .+(LIMIT\s|HAVING\s|GROUP BY\s|$),Uims","\\1",$query); |
|
| 649 | + $query = "SELECT count(*) as zzzzsqlitecount FROM ($query)"; |
|
| 650 | + $l = $link->query($query); |
|
| 651 | + $i = 0; |
|
| 652 | + if ($l and $z = $l->fetch()) { |
|
| 653 | + $i = $z['zzzzsqlitecount']; |
|
| 654 | + } |
|
| 655 | + $r->spipSqliteRowCount = $i; |
|
| 656 | + } |
|
| 657 | + if (isset($r->spipSqliteRowCount)) { |
|
| 658 | + // Ce compte est faux s'il y a des limit dans la requete :( |
|
| 659 | + // il retourne le nombre d'enregistrements sans le limit |
|
| 660 | + return $r->spipSqliteRowCount; |
|
| 661 | + } else { |
|
| 662 | + return $r->rowCount(); |
|
| 663 | + } |
|
| 664 | 664 | } |
| 665 | 665 | |
| 666 | 666 | |
@@ -679,30 +679,30 @@ discard block |
||
| 679 | 679 | * - false si la requête a échouée |
| 680 | 680 | **/ |
| 681 | 681 | function spip_sqlite_countsel( |
| 682 | - $from = [], |
|
| 683 | - $where = [], |
|
| 684 | - $groupby = '', |
|
| 685 | - $having = [], |
|
| 686 | - $serveur = '', |
|
| 687 | - $requeter = true |
|
| 682 | + $from = [], |
|
| 683 | + $where = [], |
|
| 684 | + $groupby = '', |
|
| 685 | + $having = [], |
|
| 686 | + $serveur = '', |
|
| 687 | + $requeter = true |
|
| 688 | 688 | ) { |
| 689 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 690 | - $r = spip_sqlite_select( |
|
| 691 | - "COUNT($c)", |
|
| 692 | - $from, |
|
| 693 | - $where, |
|
| 694 | - '', |
|
| 695 | - '', |
|
| 696 | - '', |
|
| 697 | - $having, |
|
| 698 | - $serveur, |
|
| 699 | - $requeter |
|
| 700 | - ); |
|
| 701 | - if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 702 | - list($r) = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - return $r; |
|
| 689 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 690 | + $r = spip_sqlite_select( |
|
| 691 | + "COUNT($c)", |
|
| 692 | + $from, |
|
| 693 | + $where, |
|
| 694 | + '', |
|
| 695 | + '', |
|
| 696 | + '', |
|
| 697 | + $having, |
|
| 698 | + $serveur, |
|
| 699 | + $requeter |
|
| 700 | + ); |
|
| 701 | + if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 702 | + list($r) = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + return $r; |
|
| 706 | 706 | } |
| 707 | 707 | |
| 708 | 708 | |
@@ -720,24 +720,24 @@ discard block |
||
| 720 | 720 | **/ |
| 721 | 721 | function spip_sqlite_delete($table, $where = '', $serveur = '', $requeter = true) |
| 722 | 722 | { |
| 723 | - $res = spip_sqlite_query( |
|
| 724 | - _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 725 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 726 | - $serveur, |
|
| 727 | - $requeter |
|
| 728 | - ); |
|
| 729 | - |
|
| 730 | - // renvoyer la requete inerte si demandee |
|
| 731 | - if (!$requeter) { |
|
| 732 | - return $res; |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - if ($res) { |
|
| 736 | - $link = _sqlite_link($serveur); |
|
| 737 | - return $res->rowCount(); |
|
| 738 | - } else { |
|
| 739 | - return false; |
|
| 740 | - } |
|
| 723 | + $res = spip_sqlite_query( |
|
| 724 | + _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 725 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 726 | + $serveur, |
|
| 727 | + $requeter |
|
| 728 | + ); |
|
| 729 | + |
|
| 730 | + // renvoyer la requete inerte si demandee |
|
| 731 | + if (!$requeter) { |
|
| 732 | + return $res; |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + if ($res) { |
|
| 736 | + $link = _sqlite_link($serveur); |
|
| 737 | + return $res->rowCount(); |
|
| 738 | + } else { |
|
| 739 | + return false; |
|
| 740 | + } |
|
| 741 | 741 | } |
| 742 | 742 | |
| 743 | 743 | |
@@ -754,15 +754,15 @@ discard block |
||
| 754 | 754 | */ |
| 755 | 755 | function spip_sqlite_drop_table($table, $exist = '', $serveur = '', $requeter = true) |
| 756 | 756 | { |
| 757 | - if ($exist) { |
|
| 758 | - $exist = ' IF EXISTS'; |
|
| 759 | - } |
|
| 760 | - |
|
| 761 | - if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 762 | - return true; |
|
| 763 | - } else { |
|
| 764 | - return false; |
|
| 765 | - } |
|
| 757 | + if ($exist) { |
|
| 758 | + $exist = ' IF EXISTS'; |
|
| 759 | + } |
|
| 760 | + |
|
| 761 | + if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 762 | + return true; |
|
| 763 | + } else { |
|
| 764 | + return false; |
|
| 765 | + } |
|
| 766 | 766 | } |
| 767 | 767 | |
| 768 | 768 | |
@@ -779,11 +779,11 @@ discard block |
||
| 779 | 779 | */ |
| 780 | 780 | function spip_sqlite_drop_view($view, $exist = '', $serveur = '', $requeter = true) |
| 781 | 781 | { |
| 782 | - if ($exist) { |
|
| 783 | - $exist = ' IF EXISTS'; |
|
| 784 | - } |
|
| 782 | + if ($exist) { |
|
| 783 | + $exist = ' IF EXISTS'; |
|
| 784 | + } |
|
| 785 | 785 | |
| 786 | - return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 786 | + return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 787 | 787 | } |
| 788 | 788 | |
| 789 | 789 | /** |
@@ -798,20 +798,20 @@ discard block |
||
| 798 | 798 | */ |
| 799 | 799 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) |
| 800 | 800 | { |
| 801 | - if (!($nom or $table)) { |
|
| 802 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 801 | + if (!($nom or $table)) { |
|
| 802 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 803 | 803 | |
| 804 | - return false; |
|
| 805 | - } |
|
| 804 | + return false; |
|
| 805 | + } |
|
| 806 | 806 | |
| 807 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 808 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 809 | - $index = $table . '_' . $nom; |
|
| 810 | - $exist = ' IF EXISTS'; |
|
| 807 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 808 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 809 | + $index = $table . '_' . $nom; |
|
| 810 | + $exist = ' IF EXISTS'; |
|
| 811 | 811 | |
| 812 | - $query = "DROP INDEX$exist $index"; |
|
| 812 | + $query = "DROP INDEX$exist $index"; |
|
| 813 | 813 | |
| 814 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 814 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 815 | 815 | } |
| 816 | 816 | |
| 817 | 817 | /** |
@@ -828,29 +828,29 @@ discard block |
||
| 828 | 828 | **/ |
| 829 | 829 | function spip_sqlite_error($query = '', $serveur = '') |
| 830 | 830 | { |
| 831 | - $link = _sqlite_link($serveur); |
|
| 832 | - |
|
| 833 | - if ($link) { |
|
| 834 | - $errs = $link->errorInfo(); |
|
| 835 | - $s = _sqlite_last_error_from_link($link); |
|
| 836 | - } else { |
|
| 837 | - $s = ': aucune ressource sqlite (link)'; |
|
| 838 | - } |
|
| 839 | - if ($s) { |
|
| 840 | - $trace = debug_backtrace(); |
|
| 841 | - if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 842 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 843 | - } |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - return $s; |
|
| 831 | + $link = _sqlite_link($serveur); |
|
| 832 | + |
|
| 833 | + if ($link) { |
|
| 834 | + $errs = $link->errorInfo(); |
|
| 835 | + $s = _sqlite_last_error_from_link($link); |
|
| 836 | + } else { |
|
| 837 | + $s = ': aucune ressource sqlite (link)'; |
|
| 838 | + } |
|
| 839 | + if ($s) { |
|
| 840 | + $trace = debug_backtrace(); |
|
| 841 | + if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 842 | + spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 843 | + } |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + return $s; |
|
| 847 | 847 | } |
| 848 | 848 | |
| 849 | 849 | function _sqlite_last_error_from_link($link) |
| 850 | 850 | { |
| 851 | - if ($link) { |
|
| 852 | - $errs = $link->errorInfo(); |
|
| 853 | - /* |
|
| 851 | + if ($link) { |
|
| 852 | + $errs = $link->errorInfo(); |
|
| 853 | + /* |
|
| 854 | 854 | $errs[0] |
| 855 | 855 | numero SQLState ('HY000' souvent lors d'une erreur) |
| 856 | 856 | http://www.easysoft.com/developer/interfaces/odbc/sqlstate_status_return_codes.html |
@@ -860,11 +860,11 @@ discard block |
||
| 860 | 860 | $errs[2] |
| 861 | 861 | Le texte du message d'erreur |
| 862 | 862 | */ |
| 863 | - if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 864 | - return "$errs[2]"; |
|
| 865 | - } |
|
| 866 | - } |
|
| 867 | - return ''; |
|
| 863 | + if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 864 | + return "$errs[2]"; |
|
| 865 | + } |
|
| 866 | + } |
|
| 867 | + return ''; |
|
| 868 | 868 | } |
| 869 | 869 | |
| 870 | 870 | /** |
@@ -882,23 +882,23 @@ discard block |
||
| 882 | 882 | **/ |
| 883 | 883 | function spip_sqlite_errno($serveur = '') |
| 884 | 884 | { |
| 885 | - $link = _sqlite_link($serveur); |
|
| 886 | - |
|
| 887 | - if ($link) { |
|
| 888 | - $t = $link->errorInfo(); |
|
| 889 | - $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 890 | - if ($s) { |
|
| 891 | - $s .= ' / ' . $t[1]; |
|
| 892 | - } // ajoute l'erreur du moteur SQLite |
|
| 893 | - } else { |
|
| 894 | - $s = ': aucune ressource sqlite (link)'; |
|
| 895 | - } |
|
| 896 | - |
|
| 897 | - if ($s) { |
|
| 898 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 899 | - } |
|
| 900 | - |
|
| 901 | - return $s ? $s : 0; |
|
| 885 | + $link = _sqlite_link($serveur); |
|
| 886 | + |
|
| 887 | + if ($link) { |
|
| 888 | + $t = $link->errorInfo(); |
|
| 889 | + $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 890 | + if ($s) { |
|
| 891 | + $s .= ' / ' . $t[1]; |
|
| 892 | + } // ajoute l'erreur du moteur SQLite |
|
| 893 | + } else { |
|
| 894 | + $s = ': aucune ressource sqlite (link)'; |
|
| 895 | + } |
|
| 896 | + |
|
| 897 | + if ($s) { |
|
| 898 | + spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 899 | + } |
|
| 900 | + |
|
| 901 | + return $s ? $s : 0; |
|
| 902 | 902 | } |
| 903 | 903 | |
| 904 | 904 | |
@@ -915,19 +915,19 @@ discard block |
||
| 915 | 915 | */ |
| 916 | 916 | function spip_sqlite_explain($query, $serveur = '', $requeter = true) |
| 917 | 917 | { |
| 918 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 919 | - return []; |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 923 | - $query = 'EXPLAIN ' . $query; |
|
| 924 | - if (!$requeter) { |
|
| 925 | - return $query; |
|
| 926 | - } |
|
| 927 | - // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 928 | - $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 929 | - |
|
| 930 | - return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 918 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 919 | + return []; |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 923 | + $query = 'EXPLAIN ' . $query; |
|
| 924 | + if (!$requeter) { |
|
| 925 | + return $query; |
|
| 926 | + } |
|
| 927 | + // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 928 | + $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 929 | + |
|
| 930 | + return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 931 | 931 | } |
| 932 | 932 | |
| 933 | 933 | |
@@ -948,35 +948,35 @@ discard block |
||
| 948 | 948 | function spip_sqlite_fetch($r, $t = '', $serveur = '', $requeter = true) |
| 949 | 949 | { |
| 950 | 950 | |
| 951 | - $link = _sqlite_link($serveur); |
|
| 952 | - $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 953 | - |
|
| 954 | - if (!$r) { |
|
| 955 | - return false; |
|
| 956 | - } |
|
| 957 | - |
|
| 958 | - $retour = $r->fetch($t); |
|
| 959 | - |
|
| 960 | - if (!$retour) { |
|
| 961 | - if ($r->errorCode() === '00000') { |
|
| 962 | - return null; |
|
| 963 | - } |
|
| 964 | - return false; |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 968 | - // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 969 | - // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 970 | - if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 971 | - foreach ($retour as $cle => $val) { |
|
| 972 | - if (($pos = strpos($cle, '.')) !== false) { |
|
| 973 | - $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 974 | - unset($retour[$cle]); |
|
| 975 | - } |
|
| 976 | - } |
|
| 977 | - } |
|
| 978 | - |
|
| 979 | - return $retour; |
|
| 951 | + $link = _sqlite_link($serveur); |
|
| 952 | + $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 953 | + |
|
| 954 | + if (!$r) { |
|
| 955 | + return false; |
|
| 956 | + } |
|
| 957 | + |
|
| 958 | + $retour = $r->fetch($t); |
|
| 959 | + |
|
| 960 | + if (!$retour) { |
|
| 961 | + if ($r->errorCode() === '00000') { |
|
| 962 | + return null; |
|
| 963 | + } |
|
| 964 | + return false; |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 968 | + // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 969 | + // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 970 | + if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 971 | + foreach ($retour as $cle => $val) { |
|
| 972 | + if (($pos = strpos($cle, '.')) !== false) { |
|
| 973 | + $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 974 | + unset($retour[$cle]); |
|
| 975 | + } |
|
| 976 | + } |
|
| 977 | + } |
|
| 978 | + |
|
| 979 | + return $retour; |
|
| 980 | 980 | } |
| 981 | 981 | |
| 982 | 982 | /** |
@@ -990,8 +990,8 @@ discard block |
||
| 990 | 990 | **/ |
| 991 | 991 | function spip_sqlite_seek($r, $row_number, $serveur = '', $requeter = true) |
| 992 | 992 | { |
| 993 | - // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 994 | - return false; |
|
| 993 | + // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 994 | + return false; |
|
| 995 | 995 | } |
| 996 | 996 | |
| 997 | 997 | |
@@ -1008,10 +1008,10 @@ discard block |
||
| 1008 | 1008 | */ |
| 1009 | 1009 | function spip_sqlite_free(&$r, $serveur = '', $requeter = true) |
| 1010 | 1010 | { |
| 1011 | - unset($r); |
|
| 1011 | + unset($r); |
|
| 1012 | 1012 | |
| 1013 | - return true; |
|
| 1014 | - //return sqlite_free_result($r); |
|
| 1013 | + return true; |
|
| 1014 | + //return sqlite_free_result($r); |
|
| 1015 | 1015 | } |
| 1016 | 1016 | |
| 1017 | 1017 | |
@@ -1027,8 +1027,8 @@ discard block |
||
| 1027 | 1027 | */ |
| 1028 | 1028 | function spip_sqlite_get_charset($charset = [], $serveur = '', $requeter = true) |
| 1029 | 1029 | { |
| 1030 | - //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1031 | - //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1030 | + //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1031 | + //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1032 | 1032 | } |
| 1033 | 1033 | |
| 1034 | 1034 | |
@@ -1044,7 +1044,7 @@ discard block |
||
| 1044 | 1044 | **/ |
| 1045 | 1045 | function spip_sqlite_hex($v) |
| 1046 | 1046 | { |
| 1047 | - return hexdec($v); |
|
| 1047 | + return hexdec($v); |
|
| 1048 | 1048 | } |
| 1049 | 1049 | |
| 1050 | 1050 | |
@@ -1067,7 +1067,7 @@ discard block |
||
| 1067 | 1067 | **/ |
| 1068 | 1068 | function spip_sqlite_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) |
| 1069 | 1069 | { |
| 1070 | - return "($val $not IN ($valeurs))"; |
|
| 1070 | + return "($val $not IN ($valeurs))"; |
|
| 1071 | 1071 | } |
| 1072 | 1072 | |
| 1073 | 1073 | |
@@ -1096,20 +1096,20 @@ discard block |
||
| 1096 | 1096 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) |
| 1097 | 1097 | { |
| 1098 | 1098 | |
| 1099 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1100 | - if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1101 | - if (!$requeter) { |
|
| 1102 | - return $r; |
|
| 1103 | - } |
|
| 1104 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1105 | - } else { |
|
| 1106 | - $nb = false; |
|
| 1107 | - } |
|
| 1099 | + $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1100 | + if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1101 | + if (!$requeter) { |
|
| 1102 | + return $r; |
|
| 1103 | + } |
|
| 1104 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1105 | + } else { |
|
| 1106 | + $nb = false; |
|
| 1107 | + } |
|
| 1108 | 1108 | |
| 1109 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1109 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1110 | 1110 | |
| 1111 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1112 | - return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1111 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1112 | + return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1113 | 1113 | } |
| 1114 | 1114 | |
| 1115 | 1115 | |
@@ -1135,28 +1135,28 @@ discard block |
||
| 1135 | 1135 | **/ |
| 1136 | 1136 | function spip_sqlite_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) |
| 1137 | 1137 | { |
| 1138 | - if (!$desc) { |
|
| 1139 | - $desc = description_table($table, $serveur); |
|
| 1140 | - } |
|
| 1141 | - if (!$desc) { |
|
| 1142 | - die("$table insertion sans description"); |
|
| 1143 | - } |
|
| 1144 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1145 | - |
|
| 1146 | - foreach ($couples as $champ => $val) { |
|
| 1147 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1151 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1152 | - |
|
| 1153 | - $cles = $valeurs = ''; |
|
| 1154 | - if (count($couples)) { |
|
| 1155 | - $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1156 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1157 | - } |
|
| 1158 | - |
|
| 1159 | - return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1138 | + if (!$desc) { |
|
| 1139 | + $desc = description_table($table, $serveur); |
|
| 1140 | + } |
|
| 1141 | + if (!$desc) { |
|
| 1142 | + die("$table insertion sans description"); |
|
| 1143 | + } |
|
| 1144 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1145 | + |
|
| 1146 | + foreach ($couples as $champ => $val) { |
|
| 1147 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1151 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1152 | + |
|
| 1153 | + $cles = $valeurs = ''; |
|
| 1154 | + if (count($couples)) { |
|
| 1155 | + $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1156 | + $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1157 | + } |
|
| 1158 | + |
|
| 1159 | + return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1160 | 1160 | } |
| 1161 | 1161 | |
| 1162 | 1162 | |
@@ -1181,70 +1181,70 @@ discard block |
||
| 1181 | 1181 | **/ |
| 1182 | 1182 | function spip_sqlite_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) |
| 1183 | 1183 | { |
| 1184 | - if (!$desc) { |
|
| 1185 | - $desc = description_table($table, $serveur); |
|
| 1186 | - } |
|
| 1187 | - if (!$desc) { |
|
| 1188 | - die("$table insertion sans description"); |
|
| 1189 | - } |
|
| 1190 | - if (!isset($desc['field'])) { |
|
| 1191 | - $desc['field'] = []; |
|
| 1192 | - } |
|
| 1193 | - |
|
| 1194 | - // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1195 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1196 | - |
|
| 1197 | - // seul le nom de la table est a traduire ici : |
|
| 1198 | - // le faire une seule fois au debut |
|
| 1199 | - $query_start = "INSERT INTO $table "; |
|
| 1200 | - $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1201 | - |
|
| 1202 | - // ouvrir une transaction |
|
| 1203 | - if ($requeter) { |
|
| 1204 | - Sqlite::demarrer_transaction($serveur); |
|
| 1205 | - } |
|
| 1206 | - |
|
| 1207 | - while ($couples = array_shift($tab_couples)) { |
|
| 1208 | - foreach ($couples as $champ => $val) { |
|
| 1209 | - $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1210 | - } |
|
| 1211 | - |
|
| 1212 | - // inserer les champs timestamp par defaut |
|
| 1213 | - $couples = array_merge($maj, $couples); |
|
| 1214 | - |
|
| 1215 | - $champs = $valeurs = ''; |
|
| 1216 | - if (count($couples)) { |
|
| 1217 | - $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1218 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1219 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1220 | - } else { |
|
| 1221 | - $query = $query_start . 'DEFAULT VALUES'; |
|
| 1222 | - } |
|
| 1223 | - |
|
| 1224 | - if ($requeter) { |
|
| 1225 | - $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - // sur le dernier couple uniquement |
|
| 1229 | - if (!count($tab_couples)) { |
|
| 1230 | - $nb = 0; |
|
| 1231 | - if ($requeter) { |
|
| 1232 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1233 | - } else { |
|
| 1234 | - return $query; |
|
| 1235 | - } |
|
| 1236 | - } |
|
| 1237 | - |
|
| 1238 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1239 | - } |
|
| 1240 | - |
|
| 1241 | - if ($requeter) { |
|
| 1242 | - Sqlite::finir_transaction($serveur); |
|
| 1243 | - } |
|
| 1244 | - |
|
| 1245 | - // renvoie le dernier id d'autoincrement ajoute |
|
| 1246 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1247 | - return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1184 | + if (!$desc) { |
|
| 1185 | + $desc = description_table($table, $serveur); |
|
| 1186 | + } |
|
| 1187 | + if (!$desc) { |
|
| 1188 | + die("$table insertion sans description"); |
|
| 1189 | + } |
|
| 1190 | + if (!isset($desc['field'])) { |
|
| 1191 | + $desc['field'] = []; |
|
| 1192 | + } |
|
| 1193 | + |
|
| 1194 | + // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1195 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1196 | + |
|
| 1197 | + // seul le nom de la table est a traduire ici : |
|
| 1198 | + // le faire une seule fois au debut |
|
| 1199 | + $query_start = "INSERT INTO $table "; |
|
| 1200 | + $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1201 | + |
|
| 1202 | + // ouvrir une transaction |
|
| 1203 | + if ($requeter) { |
|
| 1204 | + Sqlite::demarrer_transaction($serveur); |
|
| 1205 | + } |
|
| 1206 | + |
|
| 1207 | + while ($couples = array_shift($tab_couples)) { |
|
| 1208 | + foreach ($couples as $champ => $val) { |
|
| 1209 | + $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1210 | + } |
|
| 1211 | + |
|
| 1212 | + // inserer les champs timestamp par defaut |
|
| 1213 | + $couples = array_merge($maj, $couples); |
|
| 1214 | + |
|
| 1215 | + $champs = $valeurs = ''; |
|
| 1216 | + if (count($couples)) { |
|
| 1217 | + $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1218 | + $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1219 | + $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1220 | + } else { |
|
| 1221 | + $query = $query_start . 'DEFAULT VALUES'; |
|
| 1222 | + } |
|
| 1223 | + |
|
| 1224 | + if ($requeter) { |
|
| 1225 | + $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + // sur le dernier couple uniquement |
|
| 1229 | + if (!count($tab_couples)) { |
|
| 1230 | + $nb = 0; |
|
| 1231 | + if ($requeter) { |
|
| 1232 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1233 | + } else { |
|
| 1234 | + return $query; |
|
| 1235 | + } |
|
| 1236 | + } |
|
| 1237 | + |
|
| 1238 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1239 | + } |
|
| 1240 | + |
|
| 1241 | + if ($requeter) { |
|
| 1242 | + Sqlite::finir_transaction($serveur); |
|
| 1243 | + } |
|
| 1244 | + |
|
| 1245 | + // renvoie le dernier id d'autoincrement ajoute |
|
| 1246 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1247 | + return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1248 | 1248 | } |
| 1249 | 1249 | |
| 1250 | 1250 | |
@@ -1260,7 +1260,7 @@ discard block |
||
| 1260 | 1260 | **/ |
| 1261 | 1261 | function spip_sqlite_preferer_transaction($serveur = '', $requeter = true) |
| 1262 | 1262 | { |
| 1263 | - return true; |
|
| 1263 | + return true; |
|
| 1264 | 1264 | } |
| 1265 | 1265 | |
| 1266 | 1266 | /** |
@@ -1278,12 +1278,12 @@ discard block |
||
| 1278 | 1278 | **/ |
| 1279 | 1279 | function spip_sqlite_demarrer_transaction($serveur = '', $requeter = true) |
| 1280 | 1280 | { |
| 1281 | - if (!$requeter) { |
|
| 1282 | - return 'BEGIN TRANSACTION'; |
|
| 1283 | - } |
|
| 1284 | - Sqlite::demarrer_transaction($serveur); |
|
| 1281 | + if (!$requeter) { |
|
| 1282 | + return 'BEGIN TRANSACTION'; |
|
| 1283 | + } |
|
| 1284 | + Sqlite::demarrer_transaction($serveur); |
|
| 1285 | 1285 | |
| 1286 | - return true; |
|
| 1286 | + return true; |
|
| 1287 | 1287 | } |
| 1288 | 1288 | |
| 1289 | 1289 | /** |
@@ -1298,12 +1298,12 @@ discard block |
||
| 1298 | 1298 | **/ |
| 1299 | 1299 | function spip_sqlite_terminer_transaction($serveur = '', $requeter = true) |
| 1300 | 1300 | { |
| 1301 | - if (!$requeter) { |
|
| 1302 | - return 'COMMIT'; |
|
| 1303 | - } |
|
| 1304 | - Sqlite::finir_transaction($serveur); |
|
| 1301 | + if (!$requeter) { |
|
| 1302 | + return 'COMMIT'; |
|
| 1303 | + } |
|
| 1304 | + Sqlite::finir_transaction($serveur); |
|
| 1305 | 1305 | |
| 1306 | - return true; |
|
| 1306 | + return true; |
|
| 1307 | 1307 | } |
| 1308 | 1308 | |
| 1309 | 1309 | |
@@ -1319,27 +1319,27 @@ discard block |
||
| 1319 | 1319 | **/ |
| 1320 | 1320 | function spip_sqlite_listdbs($serveur = '', $requeter = true) |
| 1321 | 1321 | { |
| 1322 | - _sqlite_init(); |
|
| 1323 | - |
|
| 1324 | - if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1325 | - return []; |
|
| 1326 | - } |
|
| 1327 | - |
|
| 1328 | - include_spip('inc/flock'); |
|
| 1329 | - $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1330 | - $bds = []; |
|
| 1331 | - |
|
| 1332 | - foreach ($bases as $b) { |
|
| 1333 | - // pas de bases commencant pas sqlite |
|
| 1334 | - // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1335 | - // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1336 | - if (strpos($b, '_sqlite')) { |
|
| 1337 | - continue; |
|
| 1338 | - } |
|
| 1339 | - $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1340 | - } |
|
| 1341 | - |
|
| 1342 | - return $bds; |
|
| 1322 | + _sqlite_init(); |
|
| 1323 | + |
|
| 1324 | + if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1325 | + return []; |
|
| 1326 | + } |
|
| 1327 | + |
|
| 1328 | + include_spip('inc/flock'); |
|
| 1329 | + $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1330 | + $bds = []; |
|
| 1331 | + |
|
| 1332 | + foreach ($bases as $b) { |
|
| 1333 | + // pas de bases commencant pas sqlite |
|
| 1334 | + // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1335 | + // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1336 | + if (strpos($b, '_sqlite')) { |
|
| 1337 | + continue; |
|
| 1338 | + } |
|
| 1339 | + $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1340 | + } |
|
| 1341 | + |
|
| 1342 | + return $bds; |
|
| 1343 | 1343 | } |
| 1344 | 1344 | |
| 1345 | 1345 | |
@@ -1355,9 +1355,9 @@ discard block |
||
| 1355 | 1355 | */ |
| 1356 | 1356 | function spip_sqlite_multi($objet, $lang) |
| 1357 | 1357 | { |
| 1358 | - $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1358 | + $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1359 | 1359 | |
| 1360 | - return $r; |
|
| 1360 | + return $r; |
|
| 1361 | 1361 | } |
| 1362 | 1362 | |
| 1363 | 1363 | |
@@ -1375,15 +1375,15 @@ discard block |
||
| 1375 | 1375 | **/ |
| 1376 | 1376 | function spip_sqlite_optimize($table, $serveur = '', $requeter = true) |
| 1377 | 1377 | { |
| 1378 | - static $do = false; |
|
| 1379 | - if ($requeter and $do) { |
|
| 1380 | - return true; |
|
| 1381 | - } |
|
| 1382 | - if ($requeter) { |
|
| 1383 | - $do = true; |
|
| 1384 | - } |
|
| 1385 | - |
|
| 1386 | - return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1378 | + static $do = false; |
|
| 1379 | + if ($requeter and $do) { |
|
| 1380 | + return true; |
|
| 1381 | + } |
|
| 1382 | + if ($requeter) { |
|
| 1383 | + $do = true; |
|
| 1384 | + } |
|
| 1385 | + |
|
| 1386 | + return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1387 | 1387 | } |
| 1388 | 1388 | |
| 1389 | 1389 | |
@@ -1401,15 +1401,15 @@ discard block |
||
| 1401 | 1401 | */ |
| 1402 | 1402 | function spip_sqlite_quote($v, $type = '') |
| 1403 | 1403 | { |
| 1404 | - if (!is_array($v)) { |
|
| 1405 | - return _sqlite_calculer_cite($v, $type); |
|
| 1406 | - } |
|
| 1407 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1408 | - foreach ($v as $k => $r) { |
|
| 1409 | - $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1410 | - } |
|
| 1411 | - |
|
| 1412 | - return join(',', $v); |
|
| 1404 | + if (!is_array($v)) { |
|
| 1405 | + return _sqlite_calculer_cite($v, $type); |
|
| 1406 | + } |
|
| 1407 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1408 | + foreach ($v as $k => $r) { |
|
| 1409 | + $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1410 | + } |
|
| 1411 | + |
|
| 1412 | + return join(',', $v); |
|
| 1413 | 1413 | } |
| 1414 | 1414 | |
| 1415 | 1415 | |
@@ -1427,9 +1427,9 @@ discard block |
||
| 1427 | 1427 | **/ |
| 1428 | 1428 | function spip_sqlite_date_proche($champ, $interval, $unite) |
| 1429 | 1429 | { |
| 1430 | - $op = (($interval <= 0) ? '>' : '<'); |
|
| 1430 | + $op = (($interval <= 0) ? '>' : '<'); |
|
| 1431 | 1431 | |
| 1432 | - return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1432 | + return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1433 | 1433 | } |
| 1434 | 1434 | |
| 1435 | 1435 | |
@@ -1448,48 +1448,48 @@ discard block |
||
| 1448 | 1448 | */ |
| 1449 | 1449 | function spip_sqlite_repair($table, $serveur = '', $requeter = true) |
| 1450 | 1450 | { |
| 1451 | - if ( |
|
| 1452 | - $desc = spip_sqlite_showtable($table, $serveur) |
|
| 1453 | - and isset($desc['field']) |
|
| 1454 | - and is_array($desc['field']) |
|
| 1455 | - ) { |
|
| 1456 | - foreach ($desc['field'] as $c => $d) { |
|
| 1457 | - if ( |
|
| 1458 | - preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', $d) |
|
| 1459 | - and stripos($d, 'NOT NULL') !== false |
|
| 1460 | - and stripos($d, 'DEFAULT') === false |
|
| 1461 | - /* pas touche aux cles primaires */ |
|
| 1462 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1463 | - ) { |
|
| 1464 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1465 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1466 | - } |
|
| 1467 | - if ( |
|
| 1468 | - preg_match(',^(INTEGER),i', $d) |
|
| 1469 | - and stripos($d, 'NOT NULL') !== false |
|
| 1470 | - and stripos($d, 'DEFAULT') === false |
|
| 1471 | - /* pas touche aux cles primaires */ |
|
| 1472 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1473 | - ) { |
|
| 1474 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1475 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1476 | - } |
|
| 1477 | - if ( |
|
| 1478 | - preg_match(',^(datetime),i', $d) |
|
| 1479 | - and stripos($d, 'NOT NULL') !== false |
|
| 1480 | - and stripos($d, 'DEFAULT') === false |
|
| 1481 | - /* pas touche aux cles primaires */ |
|
| 1482 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1483 | - ) { |
|
| 1484 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1485 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1486 | - } |
|
| 1487 | - } |
|
| 1488 | - |
|
| 1489 | - return [' OK ']; |
|
| 1490 | - } |
|
| 1491 | - |
|
| 1492 | - return [' ERROR ']; |
|
| 1451 | + if ( |
|
| 1452 | + $desc = spip_sqlite_showtable($table, $serveur) |
|
| 1453 | + and isset($desc['field']) |
|
| 1454 | + and is_array($desc['field']) |
|
| 1455 | + ) { |
|
| 1456 | + foreach ($desc['field'] as $c => $d) { |
|
| 1457 | + if ( |
|
| 1458 | + preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', $d) |
|
| 1459 | + and stripos($d, 'NOT NULL') !== false |
|
| 1460 | + and stripos($d, 'DEFAULT') === false |
|
| 1461 | + /* pas touche aux cles primaires */ |
|
| 1462 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1463 | + ) { |
|
| 1464 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1465 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1466 | + } |
|
| 1467 | + if ( |
|
| 1468 | + preg_match(',^(INTEGER),i', $d) |
|
| 1469 | + and stripos($d, 'NOT NULL') !== false |
|
| 1470 | + and stripos($d, 'DEFAULT') === false |
|
| 1471 | + /* pas touche aux cles primaires */ |
|
| 1472 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1473 | + ) { |
|
| 1474 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1475 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1476 | + } |
|
| 1477 | + if ( |
|
| 1478 | + preg_match(',^(datetime),i', $d) |
|
| 1479 | + and stripos($d, 'NOT NULL') !== false |
|
| 1480 | + and stripos($d, 'DEFAULT') === false |
|
| 1481 | + /* pas touche aux cles primaires */ |
|
| 1482 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1483 | + ) { |
|
| 1484 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1485 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1486 | + } |
|
| 1487 | + } |
|
| 1488 | + |
|
| 1489 | + return [' OK ']; |
|
| 1490 | + } |
|
| 1491 | + |
|
| 1492 | + return [' ERROR ']; |
|
| 1493 | 1493 | } |
| 1494 | 1494 | |
| 1495 | 1495 | |
@@ -1519,25 +1519,25 @@ discard block |
||
| 1519 | 1519 | **/ |
| 1520 | 1520 | function spip_sqlite_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) |
| 1521 | 1521 | { |
| 1522 | - if (!$desc) { |
|
| 1523 | - $desc = description_table($table, $serveur); |
|
| 1524 | - } |
|
| 1525 | - if (!$desc) { |
|
| 1526 | - die("$table insertion sans description"); |
|
| 1527 | - } |
|
| 1528 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1529 | - |
|
| 1530 | - foreach ($couples as $champ => $val) { |
|
| 1531 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1532 | - } |
|
| 1533 | - |
|
| 1534 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1535 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1536 | - |
|
| 1537 | - return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1538 | - ',', |
|
| 1539 | - $couples |
|
| 1540 | - ) . ')', $serveur); |
|
| 1522 | + if (!$desc) { |
|
| 1523 | + $desc = description_table($table, $serveur); |
|
| 1524 | + } |
|
| 1525 | + if (!$desc) { |
|
| 1526 | + die("$table insertion sans description"); |
|
| 1527 | + } |
|
| 1528 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1529 | + |
|
| 1530 | + foreach ($couples as $champ => $val) { |
|
| 1531 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1532 | + } |
|
| 1533 | + |
|
| 1534 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1535 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1536 | + |
|
| 1537 | + return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1538 | + ',', |
|
| 1539 | + $couples |
|
| 1540 | + ) . ')', $serveur); |
|
| 1541 | 1541 | } |
| 1542 | 1542 | |
| 1543 | 1543 | |
@@ -1568,13 +1568,13 @@ discard block |
||
| 1568 | 1568 | function spip_sqlite_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) |
| 1569 | 1569 | { |
| 1570 | 1570 | |
| 1571 | - // boucler pour trainter chaque requete independemment |
|
| 1572 | - foreach ($tab_couples as $couples) { |
|
| 1573 | - $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1574 | - } |
|
| 1571 | + // boucler pour trainter chaque requete independemment |
|
| 1572 | + foreach ($tab_couples as $couples) { |
|
| 1573 | + $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1574 | + } |
|
| 1575 | 1575 | |
| 1576 | - // renvoie le dernier id |
|
| 1577 | - return $retour; |
|
| 1576 | + // renvoie le dernier id |
|
| 1577 | + return $retour; |
|
| 1578 | 1578 | } |
| 1579 | 1579 | |
| 1580 | 1580 | |
@@ -1601,44 +1601,44 @@ discard block |
||
| 1601 | 1601 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 1602 | 1602 | */ |
| 1603 | 1603 | function spip_sqlite_select( |
| 1604 | - $select, |
|
| 1605 | - $from, |
|
| 1606 | - $where = '', |
|
| 1607 | - $groupby = '', |
|
| 1608 | - $orderby = '', |
|
| 1609 | - $limit = '', |
|
| 1610 | - $having = '', |
|
| 1611 | - $serveur = '', |
|
| 1612 | - $requeter = true |
|
| 1604 | + $select, |
|
| 1605 | + $from, |
|
| 1606 | + $where = '', |
|
| 1607 | + $groupby = '', |
|
| 1608 | + $orderby = '', |
|
| 1609 | + $limit = '', |
|
| 1610 | + $having = '', |
|
| 1611 | + $serveur = '', |
|
| 1612 | + $requeter = true |
|
| 1613 | 1613 | ) { |
| 1614 | 1614 | |
| 1615 | - // version() n'est pas connu de sqlite |
|
| 1616 | - $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1617 | - |
|
| 1618 | - // recomposer from |
|
| 1619 | - $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1620 | - |
|
| 1621 | - $query = |
|
| 1622 | - _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1623 | - . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1624 | - . _sqlite_calculer_expression('WHERE', $where) |
|
| 1625 | - . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1626 | - . _sqlite_calculer_expression('HAVING', $having) |
|
| 1627 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1628 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1629 | - |
|
| 1630 | - // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1631 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1632 | - // texte de la requete demande ? |
|
| 1633 | - if (!$requeter) { |
|
| 1634 | - return $res; |
|
| 1635 | - } |
|
| 1636 | - // erreur survenue ? |
|
| 1637 | - if ($res === false) { |
|
| 1638 | - return Sqlite::traduire_requete($query, $serveur); |
|
| 1639 | - } |
|
| 1640 | - |
|
| 1641 | - return $res; |
|
| 1615 | + // version() n'est pas connu de sqlite |
|
| 1616 | + $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1617 | + |
|
| 1618 | + // recomposer from |
|
| 1619 | + $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1620 | + |
|
| 1621 | + $query = |
|
| 1622 | + _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1623 | + . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1624 | + . _sqlite_calculer_expression('WHERE', $where) |
|
| 1625 | + . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1626 | + . _sqlite_calculer_expression('HAVING', $having) |
|
| 1627 | + . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1628 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1629 | + |
|
| 1630 | + // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1631 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1632 | + // texte de la requete demande ? |
|
| 1633 | + if (!$requeter) { |
|
| 1634 | + return $res; |
|
| 1635 | + } |
|
| 1636 | + // erreur survenue ? |
|
| 1637 | + if ($res === false) { |
|
| 1638 | + return Sqlite::traduire_requete($query, $serveur); |
|
| 1639 | + } |
|
| 1640 | + |
|
| 1641 | + return $res; |
|
| 1642 | 1642 | } |
| 1643 | 1643 | |
| 1644 | 1644 | |
@@ -1658,32 +1658,32 @@ discard block |
||
| 1658 | 1658 | **/ |
| 1659 | 1659 | function spip_sqlite_selectdb($db, $serveur = '', $requeter = true) |
| 1660 | 1660 | { |
| 1661 | - _sqlite_init(); |
|
| 1662 | - |
|
| 1663 | - // interdire la creation d'une nouvelle base, |
|
| 1664 | - // sauf si on est dans l'installation |
|
| 1665 | - if ( |
|
| 1666 | - !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1667 | - && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1668 | - ) { |
|
| 1669 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1670 | - |
|
| 1671 | - return false; |
|
| 1672 | - } |
|
| 1673 | - |
|
| 1674 | - // se connecter a la base indiquee |
|
| 1675 | - // avec les identifiants connus |
|
| 1676 | - $index = $serveur ? $serveur : 0; |
|
| 1677 | - |
|
| 1678 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1679 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1680 | - return $db; |
|
| 1681 | - } |
|
| 1682 | - } else { |
|
| 1683 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1684 | - |
|
| 1685 | - return false; |
|
| 1686 | - } |
|
| 1661 | + _sqlite_init(); |
|
| 1662 | + |
|
| 1663 | + // interdire la creation d'une nouvelle base, |
|
| 1664 | + // sauf si on est dans l'installation |
|
| 1665 | + if ( |
|
| 1666 | + !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1667 | + && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1668 | + ) { |
|
| 1669 | + spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1670 | + |
|
| 1671 | + return false; |
|
| 1672 | + } |
|
| 1673 | + |
|
| 1674 | + // se connecter a la base indiquee |
|
| 1675 | + // avec les identifiants connus |
|
| 1676 | + $index = $serveur ? $serveur : 0; |
|
| 1677 | + |
|
| 1678 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1679 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1680 | + return $db; |
|
| 1681 | + } |
|
| 1682 | + } else { |
|
| 1683 | + spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1684 | + |
|
| 1685 | + return false; |
|
| 1686 | + } |
|
| 1687 | 1687 | } |
| 1688 | 1688 | |
| 1689 | 1689 | |
@@ -1699,8 +1699,8 @@ discard block |
||
| 1699 | 1699 | */ |
| 1700 | 1700 | function spip_sqlite_set_charset($charset, $serveur = '', $requeter = true) |
| 1701 | 1701 | { |
| 1702 | - # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1703 | - # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1702 | + # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1703 | + # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1704 | 1704 | } |
| 1705 | 1705 | |
| 1706 | 1706 | |
@@ -1719,24 +1719,24 @@ discard block |
||
| 1719 | 1719 | **/ |
| 1720 | 1720 | function spip_sqlite_showbase($match, $serveur = '', $requeter = true) |
| 1721 | 1721 | { |
| 1722 | - // type est le type d'entrée : table / index / view |
|
| 1723 | - // on ne retourne que les tables (?) et non les vues... |
|
| 1724 | - # ESCAPE non supporte par les versions sqlite <3 |
|
| 1725 | - # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1726 | - $match = preg_quote($match); |
|
| 1727 | - $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1728 | - $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1729 | - $match = str_replace('_', '.', $match); |
|
| 1730 | - $match = str_replace('%', '.*', $match); |
|
| 1731 | - $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1732 | - $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1733 | - $match = "^$match$"; |
|
| 1734 | - |
|
| 1735 | - return spip_sqlite_query( |
|
| 1736 | - "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1737 | - $serveur, |
|
| 1738 | - $requeter |
|
| 1739 | - ); |
|
| 1722 | + // type est le type d'entrée : table / index / view |
|
| 1723 | + // on ne retourne que les tables (?) et non les vues... |
|
| 1724 | + # ESCAPE non supporte par les versions sqlite <3 |
|
| 1725 | + # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1726 | + $match = preg_quote($match); |
|
| 1727 | + $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1728 | + $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1729 | + $match = str_replace('_', '.', $match); |
|
| 1730 | + $match = str_replace('%', '.*', $match); |
|
| 1731 | + $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1732 | + $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1733 | + $match = "^$match$"; |
|
| 1734 | + |
|
| 1735 | + return spip_sqlite_query( |
|
| 1736 | + "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1737 | + $serveur, |
|
| 1738 | + $requeter |
|
| 1739 | + ); |
|
| 1740 | 1740 | } |
| 1741 | 1741 | |
| 1742 | 1742 | /** |
@@ -1755,19 +1755,19 @@ discard block |
||
| 1755 | 1755 | **/ |
| 1756 | 1756 | function spip_sqlite_table_exists(string $table, $serveur = '', $requeter = true) |
| 1757 | 1757 | { |
| 1758 | - $r = spip_sqlite_query( |
|
| 1759 | - 'SELECT name FROM sqlite_master WHERE' |
|
| 1760 | - . ' type=\'table\'' |
|
| 1761 | - . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1762 | - . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1763 | - $serveur, |
|
| 1764 | - $requeter |
|
| 1765 | - ); |
|
| 1766 | - if (!$requeter) { |
|
| 1767 | - return $r; |
|
| 1768 | - } |
|
| 1769 | - $res = spip_sqlite_fetch($r); |
|
| 1770 | - return (bool) $res; |
|
| 1758 | + $r = spip_sqlite_query( |
|
| 1759 | + 'SELECT name FROM sqlite_master WHERE' |
|
| 1760 | + . ' type=\'table\'' |
|
| 1761 | + . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1762 | + . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1763 | + $serveur, |
|
| 1764 | + $requeter |
|
| 1765 | + ); |
|
| 1766 | + if (!$requeter) { |
|
| 1767 | + return $r; |
|
| 1768 | + } |
|
| 1769 | + $res = spip_sqlite_fetch($r); |
|
| 1770 | + return (bool) $res; |
|
| 1771 | 1771 | } |
| 1772 | 1772 | |
| 1773 | 1773 | define('_SQLITE_RE_SHOW_TABLE', '/^[^(),]*\(((?:[^()]*\((?:[^()]*\([^()]*\))?[^()]*\)[^()]*)*[^()]*)\)[^()]*$/'); |
@@ -1791,129 +1791,129 @@ discard block |
||
| 1791 | 1791 | */ |
| 1792 | 1792 | function spip_sqlite_showtable($nom_table, $serveur = '', $requeter = true) |
| 1793 | 1793 | { |
| 1794 | - $query = |
|
| 1795 | - 'SELECT sql, type FROM' |
|
| 1796 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1797 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1798 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1799 | - . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1800 | - . ' ORDER BY substr(type,2,1), name'; |
|
| 1801 | - |
|
| 1802 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1803 | - if (!$a) { |
|
| 1804 | - return ''; |
|
| 1805 | - } |
|
| 1806 | - if (!$requeter) { |
|
| 1807 | - return $a; |
|
| 1808 | - } |
|
| 1809 | - if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1810 | - return ''; |
|
| 1811 | - } |
|
| 1812 | - $vue = ($a['type'] == 'view'); // table | vue |
|
| 1813 | - |
|
| 1814 | - // c'est une table |
|
| 1815 | - // il faut parser le create |
|
| 1816 | - if (!$vue) { |
|
| 1817 | - if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1818 | - return ''; |
|
| 1819 | - } else { |
|
| 1820 | - $desc = $r[1]; |
|
| 1821 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1822 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1823 | - if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1824 | - $namedkeys = $r[2]; |
|
| 1825 | - $desc = $r[1]; |
|
| 1826 | - } else { |
|
| 1827 | - $namedkeys = ''; |
|
| 1828 | - } |
|
| 1829 | - |
|
| 1830 | - $fields = []; |
|
| 1831 | - $keys = []; |
|
| 1832 | - |
|
| 1833 | - // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1834 | - // par exemple s'il contiennent une virgule. |
|
| 1835 | - // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1836 | - list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1837 | - |
|
| 1838 | - // separer toutes les descriptions de champs, separes par des virgules |
|
| 1839 | - # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1840 | - $k_precedent = null; |
|
| 1841 | - foreach (explode(',', $desc) as $v) { |
|
| 1842 | - preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1843 | - // Les cles de champs peuvent etre entourees |
|
| 1844 | - // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1845 | - // http://www.sqlite.org/lang_keywords.html |
|
| 1846 | - $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1847 | - if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1848 | - $k = trim($k, $char); |
|
| 1849 | - if ($char == '[') { |
|
| 1850 | - $k = rtrim($k, ']'); |
|
| 1851 | - } |
|
| 1852 | - } |
|
| 1853 | - $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1854 | - |
|
| 1855 | - // rustine pour DECIMAL(10,2) |
|
| 1856 | - // s'il y a une parenthèse fermante dans la clé |
|
| 1857 | - // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1858 | - if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
|
| 1859 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1860 | - continue; |
|
| 1861 | - } |
|
| 1862 | - |
|
| 1863 | - // la primary key peut etre dans une des descriptions de champs |
|
| 1864 | - // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1865 | - if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1866 | - $keys['PRIMARY KEY'] = $k; |
|
| 1867 | - } |
|
| 1868 | - |
|
| 1869 | - $fields[$k] = $def; |
|
| 1870 | - $k_precedent = $k; |
|
| 1871 | - } |
|
| 1872 | - // key inclues dans la requete |
|
| 1873 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1874 | - if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 1875 | - $k = str_replace('`', '', trim($r[1])); |
|
| 1876 | - $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1877 | - if ($k && !isset($keys[$k])) { |
|
| 1878 | - $keys[$k] = $t; |
|
| 1879 | - } else { |
|
| 1880 | - $keys[] = $t; |
|
| 1881 | - } |
|
| 1882 | - } |
|
| 1883 | - } |
|
| 1884 | - // sinon ajouter les key index |
|
| 1885 | - $query = |
|
| 1886 | - 'SELECT name,sql FROM' |
|
| 1887 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1888 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1889 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1890 | - . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1891 | - . 'ORDER BY substr(type,2,1), name'; |
|
| 1892 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1893 | - while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1894 | - $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1895 | - $keytype = 'KEY'; |
|
| 1896 | - if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
|
| 1897 | - $keytype = 'UNIQUE KEY'; |
|
| 1898 | - } |
|
| 1899 | - $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1900 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1901 | - } |
|
| 1902 | - } |
|
| 1903 | - } // c'est une vue, on liste les champs disponibles simplement |
|
| 1904 | - else { |
|
| 1905 | - if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1906 | - $fields = []; |
|
| 1907 | - foreach ($res as $c => $v) { |
|
| 1908 | - $fields[$c] = ''; |
|
| 1909 | - } |
|
| 1910 | - $keys = []; |
|
| 1911 | - } else { |
|
| 1912 | - return ''; |
|
| 1913 | - } |
|
| 1914 | - } |
|
| 1915 | - |
|
| 1916 | - return ['field' => $fields, 'key' => $keys]; |
|
| 1794 | + $query = |
|
| 1795 | + 'SELECT sql, type FROM' |
|
| 1796 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1797 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1798 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1799 | + . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1800 | + . ' ORDER BY substr(type,2,1), name'; |
|
| 1801 | + |
|
| 1802 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1803 | + if (!$a) { |
|
| 1804 | + return ''; |
|
| 1805 | + } |
|
| 1806 | + if (!$requeter) { |
|
| 1807 | + return $a; |
|
| 1808 | + } |
|
| 1809 | + if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1810 | + return ''; |
|
| 1811 | + } |
|
| 1812 | + $vue = ($a['type'] == 'view'); // table | vue |
|
| 1813 | + |
|
| 1814 | + // c'est une table |
|
| 1815 | + // il faut parser le create |
|
| 1816 | + if (!$vue) { |
|
| 1817 | + if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1818 | + return ''; |
|
| 1819 | + } else { |
|
| 1820 | + $desc = $r[1]; |
|
| 1821 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1822 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1823 | + if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1824 | + $namedkeys = $r[2]; |
|
| 1825 | + $desc = $r[1]; |
|
| 1826 | + } else { |
|
| 1827 | + $namedkeys = ''; |
|
| 1828 | + } |
|
| 1829 | + |
|
| 1830 | + $fields = []; |
|
| 1831 | + $keys = []; |
|
| 1832 | + |
|
| 1833 | + // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1834 | + // par exemple s'il contiennent une virgule. |
|
| 1835 | + // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1836 | + list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1837 | + |
|
| 1838 | + // separer toutes les descriptions de champs, separes par des virgules |
|
| 1839 | + # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1840 | + $k_precedent = null; |
|
| 1841 | + foreach (explode(',', $desc) as $v) { |
|
| 1842 | + preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1843 | + // Les cles de champs peuvent etre entourees |
|
| 1844 | + // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1845 | + // http://www.sqlite.org/lang_keywords.html |
|
| 1846 | + $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1847 | + if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1848 | + $k = trim($k, $char); |
|
| 1849 | + if ($char == '[') { |
|
| 1850 | + $k = rtrim($k, ']'); |
|
| 1851 | + } |
|
| 1852 | + } |
|
| 1853 | + $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1854 | + |
|
| 1855 | + // rustine pour DECIMAL(10,2) |
|
| 1856 | + // s'il y a une parenthèse fermante dans la clé |
|
| 1857 | + // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1858 | + if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
|
| 1859 | + $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1860 | + continue; |
|
| 1861 | + } |
|
| 1862 | + |
|
| 1863 | + // la primary key peut etre dans une des descriptions de champs |
|
| 1864 | + // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1865 | + if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1866 | + $keys['PRIMARY KEY'] = $k; |
|
| 1867 | + } |
|
| 1868 | + |
|
| 1869 | + $fields[$k] = $def; |
|
| 1870 | + $k_precedent = $k; |
|
| 1871 | + } |
|
| 1872 | + // key inclues dans la requete |
|
| 1873 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1874 | + if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 1875 | + $k = str_replace('`', '', trim($r[1])); |
|
| 1876 | + $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1877 | + if ($k && !isset($keys[$k])) { |
|
| 1878 | + $keys[$k] = $t; |
|
| 1879 | + } else { |
|
| 1880 | + $keys[] = $t; |
|
| 1881 | + } |
|
| 1882 | + } |
|
| 1883 | + } |
|
| 1884 | + // sinon ajouter les key index |
|
| 1885 | + $query = |
|
| 1886 | + 'SELECT name,sql FROM' |
|
| 1887 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1888 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1889 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1890 | + . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1891 | + . 'ORDER BY substr(type,2,1), name'; |
|
| 1892 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1893 | + while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1894 | + $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1895 | + $keytype = 'KEY'; |
|
| 1896 | + if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
|
| 1897 | + $keytype = 'UNIQUE KEY'; |
|
| 1898 | + } |
|
| 1899 | + $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1900 | + $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1901 | + } |
|
| 1902 | + } |
|
| 1903 | + } // c'est une vue, on liste les champs disponibles simplement |
|
| 1904 | + else { |
|
| 1905 | + if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1906 | + $fields = []; |
|
| 1907 | + foreach ($res as $c => $v) { |
|
| 1908 | + $fields[$c] = ''; |
|
| 1909 | + } |
|
| 1910 | + $keys = []; |
|
| 1911 | + } else { |
|
| 1912 | + return ''; |
|
| 1913 | + } |
|
| 1914 | + } |
|
| 1915 | + |
|
| 1916 | + return ['field' => $fields, 'key' => $keys]; |
|
| 1917 | 1917 | } |
| 1918 | 1918 | |
| 1919 | 1919 | |
@@ -1940,22 +1940,22 @@ discard block |
||
| 1940 | 1940 | */ |
| 1941 | 1941 | function spip_sqlite_update($table, $champs, $where = '', $desc = '', $serveur = '', $requeter = true) |
| 1942 | 1942 | { |
| 1943 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1944 | - $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1945 | - |
|
| 1946 | - $set = []; |
|
| 1947 | - foreach ($champs as $champ => $val) { |
|
| 1948 | - $set[] = $champ . "=$val"; |
|
| 1949 | - } |
|
| 1950 | - if (!empty($set)) { |
|
| 1951 | - return spip_sqlite_query( |
|
| 1952 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1953 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1954 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1955 | - $serveur, |
|
| 1956 | - $requeter |
|
| 1957 | - ); |
|
| 1958 | - } |
|
| 1943 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1944 | + $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1945 | + |
|
| 1946 | + $set = []; |
|
| 1947 | + foreach ($champs as $champ => $val) { |
|
| 1948 | + $set[] = $champ . "=$val"; |
|
| 1949 | + } |
|
| 1950 | + if (!empty($set)) { |
|
| 1951 | + return spip_sqlite_query( |
|
| 1952 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1953 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1954 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1955 | + $serveur, |
|
| 1956 | + $requeter |
|
| 1957 | + ); |
|
| 1958 | + } |
|
| 1959 | 1959 | } |
| 1960 | 1960 | |
| 1961 | 1961 | |
@@ -1986,38 +1986,38 @@ discard block |
||
| 1986 | 1986 | function spip_sqlite_updateq($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) |
| 1987 | 1987 | { |
| 1988 | 1988 | |
| 1989 | - if (!$champs) { |
|
| 1990 | - return; |
|
| 1991 | - } |
|
| 1992 | - if (!$desc) { |
|
| 1993 | - $desc = description_table($table, $serveur); |
|
| 1994 | - } |
|
| 1995 | - if (!$desc) { |
|
| 1996 | - die("$table insertion sans description"); |
|
| 1997 | - } |
|
| 1998 | - $fields = $desc['field']; |
|
| 1999 | - |
|
| 2000 | - $set = []; |
|
| 2001 | - foreach ($champs as $champ => $val) { |
|
| 2002 | - $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2003 | - } |
|
| 2004 | - |
|
| 2005 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 2006 | - // attention ils sont deja quotes |
|
| 2007 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 2008 | - foreach ($maj as $champ => $val) { |
|
| 2009 | - if (!isset($set[$champ])) { |
|
| 2010 | - $set[$champ] = $champ . '=' . $val; |
|
| 2011 | - } |
|
| 2012 | - } |
|
| 2013 | - |
|
| 2014 | - return spip_sqlite_query( |
|
| 2015 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 2016 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 2017 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 2018 | - $serveur, |
|
| 2019 | - $requeter |
|
| 2020 | - ); |
|
| 1989 | + if (!$champs) { |
|
| 1990 | + return; |
|
| 1991 | + } |
|
| 1992 | + if (!$desc) { |
|
| 1993 | + $desc = description_table($table, $serveur); |
|
| 1994 | + } |
|
| 1995 | + if (!$desc) { |
|
| 1996 | + die("$table insertion sans description"); |
|
| 1997 | + } |
|
| 1998 | + $fields = $desc['field']; |
|
| 1999 | + |
|
| 2000 | + $set = []; |
|
| 2001 | + foreach ($champs as $champ => $val) { |
|
| 2002 | + $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2003 | + } |
|
| 2004 | + |
|
| 2005 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 2006 | + // attention ils sont deja quotes |
|
| 2007 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 2008 | + foreach ($maj as $champ => $val) { |
|
| 2009 | + if (!isset($set[$champ])) { |
|
| 2010 | + $set[$champ] = $champ . '=' . $val; |
|
| 2011 | + } |
|
| 2012 | + } |
|
| 2013 | + |
|
| 2014 | + return spip_sqlite_query( |
|
| 2015 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 2016 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 2017 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 2018 | + $serveur, |
|
| 2019 | + $requeter |
|
| 2020 | + ); |
|
| 2021 | 2021 | } |
| 2022 | 2022 | |
| 2023 | 2023 | |
@@ -2036,17 +2036,17 @@ discard block |
||
| 2036 | 2036 | */ |
| 2037 | 2037 | function _sqlite_init() |
| 2038 | 2038 | { |
| 2039 | - if (!defined('_DIR_DB')) { |
|
| 2040 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2041 | - } |
|
| 2042 | - if (!defined('_SQLITE_CHMOD')) { |
|
| 2043 | - define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2044 | - } |
|
| 2045 | - |
|
| 2046 | - if (!is_dir($d = _DIR_DB)) { |
|
| 2047 | - include_spip('inc/flock'); |
|
| 2048 | - sous_repertoire($d); |
|
| 2049 | - } |
|
| 2039 | + if (!defined('_DIR_DB')) { |
|
| 2040 | + define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2041 | + } |
|
| 2042 | + if (!defined('_SQLITE_CHMOD')) { |
|
| 2043 | + define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2044 | + } |
|
| 2045 | + |
|
| 2046 | + if (!is_dir($d = _DIR_DB)) { |
|
| 2047 | + include_spip('inc/flock'); |
|
| 2048 | + sous_repertoire($d); |
|
| 2049 | + } |
|
| 2050 | 2050 | } |
| 2051 | 2051 | |
| 2052 | 2052 | |
@@ -2061,20 +2061,20 @@ discard block |
||
| 2061 | 2061 | */ |
| 2062 | 2062 | function _sqlite_is_version($version = '', $link = '', $serveur = '', $requeter = true) |
| 2063 | 2063 | { |
| 2064 | - if ($link === '') { |
|
| 2065 | - $link = _sqlite_link($serveur); |
|
| 2066 | - } |
|
| 2067 | - if (!$link) { |
|
| 2068 | - return false; |
|
| 2069 | - } |
|
| 2064 | + if ($link === '') { |
|
| 2065 | + $link = _sqlite_link($serveur); |
|
| 2066 | + } |
|
| 2067 | + if (!$link) { |
|
| 2068 | + return false; |
|
| 2069 | + } |
|
| 2070 | 2070 | |
| 2071 | - $v = 3; |
|
| 2071 | + $v = 3; |
|
| 2072 | 2072 | |
| 2073 | - if (!$version) { |
|
| 2074 | - return $v; |
|
| 2075 | - } |
|
| 2073 | + if (!$version) { |
|
| 2074 | + return $v; |
|
| 2075 | + } |
|
| 2076 | 2076 | |
| 2077 | - return ($version == $v); |
|
| 2077 | + return ($version == $v); |
|
| 2078 | 2078 | } |
| 2079 | 2079 | |
| 2080 | 2080 | |
@@ -2086,9 +2086,9 @@ discard block |
||
| 2086 | 2086 | */ |
| 2087 | 2087 | function _sqlite_link($serveur = '') |
| 2088 | 2088 | { |
| 2089 | - $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 2089 | + $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 2090 | 2090 | |
| 2091 | - return $link; |
|
| 2091 | + return $link; |
|
| 2092 | 2092 | } |
| 2093 | 2093 | |
| 2094 | 2094 | |
@@ -2104,54 +2104,54 @@ discard block |
||
| 2104 | 2104 | */ |
| 2105 | 2105 | function _sqlite_calculer_cite($v, $type) |
| 2106 | 2106 | { |
| 2107 | - if ($type) { |
|
| 2108 | - if ( |
|
| 2109 | - is_null($v) |
|
| 2110 | - and stripos($type, 'NOT NULL') === false |
|
| 2111 | - ) { |
|
| 2112 | - // null php se traduit en NULL SQL |
|
| 2113 | - return 'NULL'; |
|
| 2114 | - } |
|
| 2115 | - |
|
| 2116 | - if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 2117 | - return $v; |
|
| 2118 | - } |
|
| 2119 | - if (sql_test_int($type)) { |
|
| 2120 | - if (is_numeric($v)) { |
|
| 2121 | - return $v; |
|
| 2122 | - } elseif ($v === null) { |
|
| 2123 | - return 0; |
|
| 2124 | - } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) === 0) { |
|
| 2125 | - return hexdec(substr($v, 2)); |
|
| 2126 | - } else { |
|
| 2127 | - return intval($v); |
|
| 2128 | - } |
|
| 2129 | - } |
|
| 2130 | - } else { |
|
| 2131 | - // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2132 | - if (is_bool($v)) { |
|
| 2133 | - return strval(intval($v)); |
|
| 2134 | - } elseif (is_numeric($v)) { |
|
| 2135 | - return strval($v); |
|
| 2136 | - } |
|
| 2137 | - } |
|
| 2138 | - |
|
| 2139 | - // trouver un link sqlite pour faire l'echappement |
|
| 2140 | - foreach ($GLOBALS['connexions'] as $s) { |
|
| 2141 | - if ( |
|
| 2142 | - $l = $s['link'] |
|
| 2143 | - and is_object($l) |
|
| 2144 | - and $l instanceof \PDO |
|
| 2145 | - and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2146 | - ) { |
|
| 2147 | - return $l->quote($v ?? ''); |
|
| 2148 | - } |
|
| 2149 | - } |
|
| 2150 | - |
|
| 2151 | - // echapper les ' en '' |
|
| 2152 | - spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2153 | - |
|
| 2154 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2107 | + if ($type) { |
|
| 2108 | + if ( |
|
| 2109 | + is_null($v) |
|
| 2110 | + and stripos($type, 'NOT NULL') === false |
|
| 2111 | + ) { |
|
| 2112 | + // null php se traduit en NULL SQL |
|
| 2113 | + return 'NULL'; |
|
| 2114 | + } |
|
| 2115 | + |
|
| 2116 | + if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 2117 | + return $v; |
|
| 2118 | + } |
|
| 2119 | + if (sql_test_int($type)) { |
|
| 2120 | + if (is_numeric($v)) { |
|
| 2121 | + return $v; |
|
| 2122 | + } elseif ($v === null) { |
|
| 2123 | + return 0; |
|
| 2124 | + } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) === 0) { |
|
| 2125 | + return hexdec(substr($v, 2)); |
|
| 2126 | + } else { |
|
| 2127 | + return intval($v); |
|
| 2128 | + } |
|
| 2129 | + } |
|
| 2130 | + } else { |
|
| 2131 | + // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2132 | + if (is_bool($v)) { |
|
| 2133 | + return strval(intval($v)); |
|
| 2134 | + } elseif (is_numeric($v)) { |
|
| 2135 | + return strval($v); |
|
| 2136 | + } |
|
| 2137 | + } |
|
| 2138 | + |
|
| 2139 | + // trouver un link sqlite pour faire l'echappement |
|
| 2140 | + foreach ($GLOBALS['connexions'] as $s) { |
|
| 2141 | + if ( |
|
| 2142 | + $l = $s['link'] |
|
| 2143 | + and is_object($l) |
|
| 2144 | + and $l instanceof \PDO |
|
| 2145 | + and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2146 | + ) { |
|
| 2147 | + return $l->quote($v ?? ''); |
|
| 2148 | + } |
|
| 2149 | + } |
|
| 2150 | + |
|
| 2151 | + // echapper les ' en '' |
|
| 2152 | + spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2153 | + |
|
| 2154 | + return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2155 | 2155 | } |
| 2156 | 2156 | |
| 2157 | 2157 | |
@@ -2168,21 +2168,21 @@ discard block |
||
| 2168 | 2168 | */ |
| 2169 | 2169 | function _sqlite_calculer_expression($expression, $v, $join = 'AND') |
| 2170 | 2170 | { |
| 2171 | - if (empty($v)) { |
|
| 2172 | - return ''; |
|
| 2173 | - } |
|
| 2174 | - |
|
| 2175 | - $exp = "\n$expression "; |
|
| 2176 | - |
|
| 2177 | - if (!is_array($v)) { |
|
| 2178 | - return $exp . $v; |
|
| 2179 | - } else { |
|
| 2180 | - if (strtoupper($join) === 'AND') { |
|
| 2181 | - return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2182 | - } else { |
|
| 2183 | - return $exp . join($join, $v); |
|
| 2184 | - } |
|
| 2185 | - } |
|
| 2171 | + if (empty($v)) { |
|
| 2172 | + return ''; |
|
| 2173 | + } |
|
| 2174 | + |
|
| 2175 | + $exp = "\n$expression "; |
|
| 2176 | + |
|
| 2177 | + if (!is_array($v)) { |
|
| 2178 | + return $exp . $v; |
|
| 2179 | + } else { |
|
| 2180 | + if (strtoupper($join) === 'AND') { |
|
| 2181 | + return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2182 | + } else { |
|
| 2183 | + return $exp . join($join, $v); |
|
| 2184 | + } |
|
| 2185 | + } |
|
| 2186 | 2186 | } |
| 2187 | 2187 | |
| 2188 | 2188 | |
@@ -2199,7 +2199,7 @@ discard block |
||
| 2199 | 2199 | */ |
| 2200 | 2200 | function _sqlite_calculer_order($orderby) |
| 2201 | 2201 | { |
| 2202 | - return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 2202 | + return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 2203 | 2203 | } |
| 2204 | 2204 | |
| 2205 | 2205 | |
@@ -2211,26 +2211,26 @@ discard block |
||
| 2211 | 2211 | */ |
| 2212 | 2212 | function _sqlite_calculer_select_as($args) |
| 2213 | 2213 | { |
| 2214 | - $res = ''; |
|
| 2215 | - foreach ($args as $k => $v) { |
|
| 2216 | - if (substr($k, -1) == '@') { |
|
| 2217 | - // c'est une jointure qui se refere au from precedent |
|
| 2218 | - // pas de virgule |
|
| 2219 | - $res .= ' ' . $v; |
|
| 2220 | - } else { |
|
| 2221 | - if (!is_numeric($k)) { |
|
| 2222 | - $p = strpos($v, ' '); |
|
| 2223 | - if ($p) { |
|
| 2224 | - $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2225 | - } else { |
|
| 2226 | - $v .= " AS '$k'"; |
|
| 2227 | - } |
|
| 2228 | - } |
|
| 2229 | - $res .= ', ' . $v; |
|
| 2230 | - } |
|
| 2231 | - } |
|
| 2232 | - |
|
| 2233 | - return substr($res, 2); |
|
| 2214 | + $res = ''; |
|
| 2215 | + foreach ($args as $k => $v) { |
|
| 2216 | + if (substr($k, -1) == '@') { |
|
| 2217 | + // c'est une jointure qui se refere au from precedent |
|
| 2218 | + // pas de virgule |
|
| 2219 | + $res .= ' ' . $v; |
|
| 2220 | + } else { |
|
| 2221 | + if (!is_numeric($k)) { |
|
| 2222 | + $p = strpos($v, ' '); |
|
| 2223 | + if ($p) { |
|
| 2224 | + $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2225 | + } else { |
|
| 2226 | + $v .= " AS '$k'"; |
|
| 2227 | + } |
|
| 2228 | + } |
|
| 2229 | + $res .= ', ' . $v; |
|
| 2230 | + } |
|
| 2231 | + } |
|
| 2232 | + |
|
| 2233 | + return substr($res, 2); |
|
| 2234 | 2234 | } |
| 2235 | 2235 | |
| 2236 | 2236 | |
@@ -2254,26 +2254,26 @@ discard block |
||
| 2254 | 2254 | */ |
| 2255 | 2255 | function _sqlite_calculer_where($v) |
| 2256 | 2256 | { |
| 2257 | - if (!is_array($v)) { |
|
| 2258 | - return $v; |
|
| 2259 | - } |
|
| 2260 | - |
|
| 2261 | - $op = array_shift($v); |
|
| 2262 | - if (!($n = count($v))) { |
|
| 2263 | - return $op; |
|
| 2264 | - } else { |
|
| 2265 | - $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2266 | - if ($n == 1) { |
|
| 2267 | - return "$op($arg)"; |
|
| 2268 | - } else { |
|
| 2269 | - $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2270 | - if ($n == 2) { |
|
| 2271 | - return "($arg $op $arg2)"; |
|
| 2272 | - } else { |
|
| 2273 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 2274 | - } |
|
| 2275 | - } |
|
| 2276 | - } |
|
| 2257 | + if (!is_array($v)) { |
|
| 2258 | + return $v; |
|
| 2259 | + } |
|
| 2260 | + |
|
| 2261 | + $op = array_shift($v); |
|
| 2262 | + if (!($n = count($v))) { |
|
| 2263 | + return $op; |
|
| 2264 | + } else { |
|
| 2265 | + $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2266 | + if ($n == 1) { |
|
| 2267 | + return "$op($arg)"; |
|
| 2268 | + } else { |
|
| 2269 | + $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2270 | + if ($n == 2) { |
|
| 2271 | + return "($arg $op $arg2)"; |
|
| 2272 | + } else { |
|
| 2273 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 2274 | + } |
|
| 2275 | + } |
|
| 2276 | + } |
|
| 2277 | 2277 | } |
| 2278 | 2278 | |
| 2279 | 2279 | |
@@ -2289,19 +2289,19 @@ discard block |
||
| 2289 | 2289 | */ |
| 2290 | 2290 | function _sqlite_charger_version($version = '') |
| 2291 | 2291 | { |
| 2292 | - $versions = []; |
|
| 2293 | - |
|
| 2294 | - // version 3 |
|
| 2295 | - if (!$version || $version == 3) { |
|
| 2296 | - if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2297 | - $versions[] = 3; |
|
| 2298 | - } |
|
| 2299 | - } |
|
| 2300 | - if ($version) { |
|
| 2301 | - return in_array($version, $versions); |
|
| 2302 | - } |
|
| 2303 | - |
|
| 2304 | - return $versions; |
|
| 2292 | + $versions = []; |
|
| 2293 | + |
|
| 2294 | + // version 3 |
|
| 2295 | + if (!$version || $version == 3) { |
|
| 2296 | + if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2297 | + $versions[] = 3; |
|
| 2298 | + } |
|
| 2299 | + } |
|
| 2300 | + if ($version) { |
|
| 2301 | + return in_array($version, $versions); |
|
| 2302 | + } |
|
| 2303 | + |
|
| 2304 | + return $versions; |
|
| 2305 | 2305 | } |
| 2306 | 2306 | |
| 2307 | 2307 | |
@@ -2340,147 +2340,147 @@ discard block |
||
| 2340 | 2340 | function _sqlite_modifier_table($table, $colonne, $opt = [], $serveur = '') |
| 2341 | 2341 | { |
| 2342 | 2342 | |
| 2343 | - if (is_array($table)) { |
|
| 2344 | - $table_destination = reset($table); |
|
| 2345 | - $table_origine = key($table); |
|
| 2346 | - } else { |
|
| 2347 | - $table_origine = $table_destination = $table; |
|
| 2348 | - } |
|
| 2349 | - // ne prend actuellement qu'un changement |
|
| 2350 | - // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2351 | - if (is_array($colonne)) { |
|
| 2352 | - $colonne_destination = reset($colonne); |
|
| 2353 | - $colonne_origine = key($colonne); |
|
| 2354 | - } else { |
|
| 2355 | - $colonne_origine = $colonne_destination = $colonne; |
|
| 2356 | - } |
|
| 2357 | - if (!isset($opt['field'])) { |
|
| 2358 | - $opt['field'] = []; |
|
| 2359 | - } |
|
| 2360 | - if (!isset($opt['key'])) { |
|
| 2361 | - $opt['key'] = []; |
|
| 2362 | - } |
|
| 2363 | - |
|
| 2364 | - // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2365 | - // on prendra directement le nom de la future table |
|
| 2366 | - $meme_table = ($table_origine == $table_destination); |
|
| 2367 | - |
|
| 2368 | - $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2369 | - if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2370 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2371 | - |
|
| 2372 | - return false; |
|
| 2373 | - } |
|
| 2374 | - |
|
| 2375 | - |
|
| 2376 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2377 | - |
|
| 2378 | - // 1) creer une table temporaire avec les modifications |
|
| 2379 | - // - DROP : suppression de la colonne |
|
| 2380 | - // - CHANGE : modification de la colonne |
|
| 2381 | - // (foreach pour conserver l'ordre des champs) |
|
| 2382 | - |
|
| 2383 | - // field |
|
| 2384 | - $fields = []; |
|
| 2385 | - // pour le INSERT INTO plus loin |
|
| 2386 | - // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2387 | - $fields_correspondances = []; |
|
| 2388 | - foreach ($def_origine['field'] as $c => $d) { |
|
| 2389 | - if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2390 | - // si pas DROP |
|
| 2391 | - if ($colonne_destination) { |
|
| 2392 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2393 | - $fields_correspondances[$colonne_destination] = $c; |
|
| 2394 | - } |
|
| 2395 | - } else { |
|
| 2396 | - $fields[$c] = $d; |
|
| 2397 | - $fields_correspondances[$c] = $c; |
|
| 2398 | - } |
|
| 2399 | - } |
|
| 2400 | - // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2401 | - if (!$colonne_origine && $colonne_destination) { |
|
| 2402 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2403 | - } |
|
| 2404 | - |
|
| 2405 | - // key... |
|
| 2406 | - $keys = []; |
|
| 2407 | - foreach ($def_origine['key'] as $c => $d) { |
|
| 2408 | - $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2409 | - $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2410 | - // seulement si on ne supprime pas la colonne ! |
|
| 2411 | - if ($d) { |
|
| 2412 | - $keys[$c] = $d; |
|
| 2413 | - } |
|
| 2414 | - } |
|
| 2415 | - |
|
| 2416 | - // autres keys, on merge |
|
| 2417 | - $keys = array_merge($keys, $opt['key']); |
|
| 2418 | - $queries = []; |
|
| 2419 | - |
|
| 2420 | - // copier dans destination (si differente de origine), sinon tmp |
|
| 2421 | - $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2422 | - $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2423 | - and $keys['PRIMARY KEY'] |
|
| 2424 | - and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2425 | - and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2426 | - |
|
| 2427 | - if ( |
|
| 2428 | - $q = _sqlite_requete_create( |
|
| 2429 | - $table_copie, |
|
| 2430 | - $fields, |
|
| 2431 | - $keys, |
|
| 2432 | - $autoinc, |
|
| 2433 | - $temporary = false, |
|
| 2434 | - $ifnotexists = true, |
|
| 2435 | - $serveur |
|
| 2436 | - ) |
|
| 2437 | - ) { |
|
| 2438 | - $queries[] = $q; |
|
| 2439 | - } |
|
| 2440 | - |
|
| 2441 | - |
|
| 2442 | - // 2) y copier les champs qui vont bien |
|
| 2443 | - $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2444 | - $champs_ori = join(', ', $fields_correspondances); |
|
| 2445 | - $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2446 | - |
|
| 2447 | - // 3) supprimer la table d'origine |
|
| 2448 | - $queries[] = "DROP TABLE $table_origine"; |
|
| 2449 | - |
|
| 2450 | - // 4) renommer la table temporaire |
|
| 2451 | - // avec le nom de la table destination |
|
| 2452 | - // si necessaire |
|
| 2453 | - if ($meme_table) { |
|
| 2454 | - $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2455 | - } |
|
| 2456 | - |
|
| 2457 | - // 5) remettre les index ! |
|
| 2458 | - foreach ($keys as $k => $v) { |
|
| 2459 | - if ($k == 'PRIMARY KEY') { |
|
| 2460 | - } else { |
|
| 2461 | - // enlever KEY |
|
| 2462 | - $k = substr($k, 4); |
|
| 2463 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2464 | - } |
|
| 2465 | - } |
|
| 2466 | - |
|
| 2467 | - |
|
| 2468 | - if (count($queries)) { |
|
| 2469 | - Sqlite::demarrer_transaction($serveur); |
|
| 2470 | - // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2471 | - foreach ($queries as $q) { |
|
| 2472 | - if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2473 | - spip_log('SQLite : ALTER TABLE table :' |
|
| 2474 | - . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2475 | - Sqlite::annuler_transaction($serveur); |
|
| 2476 | - |
|
| 2477 | - return false; |
|
| 2478 | - } |
|
| 2479 | - } |
|
| 2480 | - Sqlite::finir_transaction($serveur); |
|
| 2481 | - } |
|
| 2482 | - |
|
| 2483 | - return true; |
|
| 2343 | + if (is_array($table)) { |
|
| 2344 | + $table_destination = reset($table); |
|
| 2345 | + $table_origine = key($table); |
|
| 2346 | + } else { |
|
| 2347 | + $table_origine = $table_destination = $table; |
|
| 2348 | + } |
|
| 2349 | + // ne prend actuellement qu'un changement |
|
| 2350 | + // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2351 | + if (is_array($colonne)) { |
|
| 2352 | + $colonne_destination = reset($colonne); |
|
| 2353 | + $colonne_origine = key($colonne); |
|
| 2354 | + } else { |
|
| 2355 | + $colonne_origine = $colonne_destination = $colonne; |
|
| 2356 | + } |
|
| 2357 | + if (!isset($opt['field'])) { |
|
| 2358 | + $opt['field'] = []; |
|
| 2359 | + } |
|
| 2360 | + if (!isset($opt['key'])) { |
|
| 2361 | + $opt['key'] = []; |
|
| 2362 | + } |
|
| 2363 | + |
|
| 2364 | + // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2365 | + // on prendra directement le nom de la future table |
|
| 2366 | + $meme_table = ($table_origine == $table_destination); |
|
| 2367 | + |
|
| 2368 | + $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2369 | + if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2370 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2371 | + |
|
| 2372 | + return false; |
|
| 2373 | + } |
|
| 2374 | + |
|
| 2375 | + |
|
| 2376 | + $table_tmp = $table_origine . '_tmp'; |
|
| 2377 | + |
|
| 2378 | + // 1) creer une table temporaire avec les modifications |
|
| 2379 | + // - DROP : suppression de la colonne |
|
| 2380 | + // - CHANGE : modification de la colonne |
|
| 2381 | + // (foreach pour conserver l'ordre des champs) |
|
| 2382 | + |
|
| 2383 | + // field |
|
| 2384 | + $fields = []; |
|
| 2385 | + // pour le INSERT INTO plus loin |
|
| 2386 | + // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2387 | + $fields_correspondances = []; |
|
| 2388 | + foreach ($def_origine['field'] as $c => $d) { |
|
| 2389 | + if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2390 | + // si pas DROP |
|
| 2391 | + if ($colonne_destination) { |
|
| 2392 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2393 | + $fields_correspondances[$colonne_destination] = $c; |
|
| 2394 | + } |
|
| 2395 | + } else { |
|
| 2396 | + $fields[$c] = $d; |
|
| 2397 | + $fields_correspondances[$c] = $c; |
|
| 2398 | + } |
|
| 2399 | + } |
|
| 2400 | + // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2401 | + if (!$colonne_origine && $colonne_destination) { |
|
| 2402 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2403 | + } |
|
| 2404 | + |
|
| 2405 | + // key... |
|
| 2406 | + $keys = []; |
|
| 2407 | + foreach ($def_origine['key'] as $c => $d) { |
|
| 2408 | + $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2409 | + $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2410 | + // seulement si on ne supprime pas la colonne ! |
|
| 2411 | + if ($d) { |
|
| 2412 | + $keys[$c] = $d; |
|
| 2413 | + } |
|
| 2414 | + } |
|
| 2415 | + |
|
| 2416 | + // autres keys, on merge |
|
| 2417 | + $keys = array_merge($keys, $opt['key']); |
|
| 2418 | + $queries = []; |
|
| 2419 | + |
|
| 2420 | + // copier dans destination (si differente de origine), sinon tmp |
|
| 2421 | + $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2422 | + $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2423 | + and $keys['PRIMARY KEY'] |
|
| 2424 | + and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2425 | + and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2426 | + |
|
| 2427 | + if ( |
|
| 2428 | + $q = _sqlite_requete_create( |
|
| 2429 | + $table_copie, |
|
| 2430 | + $fields, |
|
| 2431 | + $keys, |
|
| 2432 | + $autoinc, |
|
| 2433 | + $temporary = false, |
|
| 2434 | + $ifnotexists = true, |
|
| 2435 | + $serveur |
|
| 2436 | + ) |
|
| 2437 | + ) { |
|
| 2438 | + $queries[] = $q; |
|
| 2439 | + } |
|
| 2440 | + |
|
| 2441 | + |
|
| 2442 | + // 2) y copier les champs qui vont bien |
|
| 2443 | + $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2444 | + $champs_ori = join(', ', $fields_correspondances); |
|
| 2445 | + $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2446 | + |
|
| 2447 | + // 3) supprimer la table d'origine |
|
| 2448 | + $queries[] = "DROP TABLE $table_origine"; |
|
| 2449 | + |
|
| 2450 | + // 4) renommer la table temporaire |
|
| 2451 | + // avec le nom de la table destination |
|
| 2452 | + // si necessaire |
|
| 2453 | + if ($meme_table) { |
|
| 2454 | + $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2455 | + } |
|
| 2456 | + |
|
| 2457 | + // 5) remettre les index ! |
|
| 2458 | + foreach ($keys as $k => $v) { |
|
| 2459 | + if ($k == 'PRIMARY KEY') { |
|
| 2460 | + } else { |
|
| 2461 | + // enlever KEY |
|
| 2462 | + $k = substr($k, 4); |
|
| 2463 | + $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2464 | + } |
|
| 2465 | + } |
|
| 2466 | + |
|
| 2467 | + |
|
| 2468 | + if (count($queries)) { |
|
| 2469 | + Sqlite::demarrer_transaction($serveur); |
|
| 2470 | + // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2471 | + foreach ($queries as $q) { |
|
| 2472 | + if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2473 | + spip_log('SQLite : ALTER TABLE table :' |
|
| 2474 | + . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2475 | + Sqlite::annuler_transaction($serveur); |
|
| 2476 | + |
|
| 2477 | + return false; |
|
| 2478 | + } |
|
| 2479 | + } |
|
| 2480 | + Sqlite::finir_transaction($serveur); |
|
| 2481 | + } |
|
| 2482 | + |
|
| 2483 | + return true; |
|
| 2484 | 2484 | } |
| 2485 | 2485 | |
| 2486 | 2486 | |
@@ -2491,61 +2491,61 @@ discard block |
||
| 2491 | 2491 | */ |
| 2492 | 2492 | function _sqlite_ref_fonctions() |
| 2493 | 2493 | { |
| 2494 | - $fonctions = [ |
|
| 2495 | - 'alter' => 'spip_sqlite_alter', |
|
| 2496 | - 'count' => 'spip_sqlite_count', |
|
| 2497 | - 'countsel' => 'spip_sqlite_countsel', |
|
| 2498 | - 'create' => 'spip_sqlite_create', |
|
| 2499 | - 'create_base' => 'spip_sqlite_create_base', |
|
| 2500 | - 'create_view' => 'spip_sqlite_create_view', |
|
| 2501 | - 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2502 | - 'delete' => 'spip_sqlite_delete', |
|
| 2503 | - 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2504 | - 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2505 | - 'errno' => 'spip_sqlite_errno', |
|
| 2506 | - 'error' => 'spip_sqlite_error', |
|
| 2507 | - 'explain' => 'spip_sqlite_explain', |
|
| 2508 | - 'fetch' => 'spip_sqlite_fetch', |
|
| 2509 | - 'seek' => 'spip_sqlite_seek', |
|
| 2510 | - 'free' => 'spip_sqlite_free', |
|
| 2511 | - 'hex' => 'spip_sqlite_hex', |
|
| 2512 | - 'in' => 'spip_sqlite_in', |
|
| 2513 | - 'insert' => 'spip_sqlite_insert', |
|
| 2514 | - 'insertq' => 'spip_sqlite_insertq', |
|
| 2515 | - 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2516 | - 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2517 | - 'multi' => 'spip_sqlite_multi', |
|
| 2518 | - 'optimize' => 'spip_sqlite_optimize', |
|
| 2519 | - 'query' => 'spip_sqlite_query', |
|
| 2520 | - 'quote' => 'spip_sqlite_quote', |
|
| 2521 | - 'repair' => 'spip_sqlite_repair', |
|
| 2522 | - 'replace' => 'spip_sqlite_replace', |
|
| 2523 | - 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2524 | - 'select' => 'spip_sqlite_select', |
|
| 2525 | - 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2526 | - 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2527 | - 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2528 | - 'showbase' => 'spip_sqlite_showbase', |
|
| 2529 | - 'showtable' => 'spip_sqlite_showtable', |
|
| 2530 | - 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2531 | - 'update' => 'spip_sqlite_update', |
|
| 2532 | - 'updateq' => 'spip_sqlite_updateq', |
|
| 2533 | - 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2534 | - 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2535 | - 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2536 | - ]; |
|
| 2537 | - |
|
| 2538 | - // association de chaque nom http d'un charset aux couples sqlite |
|
| 2539 | - // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2540 | - $charsets = [ |
|
| 2541 | - 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2542 | - //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2543 | - //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2544 | - ]; |
|
| 2545 | - |
|
| 2546 | - $fonctions['charsets'] = $charsets; |
|
| 2547 | - |
|
| 2548 | - return $fonctions; |
|
| 2494 | + $fonctions = [ |
|
| 2495 | + 'alter' => 'spip_sqlite_alter', |
|
| 2496 | + 'count' => 'spip_sqlite_count', |
|
| 2497 | + 'countsel' => 'spip_sqlite_countsel', |
|
| 2498 | + 'create' => 'spip_sqlite_create', |
|
| 2499 | + 'create_base' => 'spip_sqlite_create_base', |
|
| 2500 | + 'create_view' => 'spip_sqlite_create_view', |
|
| 2501 | + 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2502 | + 'delete' => 'spip_sqlite_delete', |
|
| 2503 | + 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2504 | + 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2505 | + 'errno' => 'spip_sqlite_errno', |
|
| 2506 | + 'error' => 'spip_sqlite_error', |
|
| 2507 | + 'explain' => 'spip_sqlite_explain', |
|
| 2508 | + 'fetch' => 'spip_sqlite_fetch', |
|
| 2509 | + 'seek' => 'spip_sqlite_seek', |
|
| 2510 | + 'free' => 'spip_sqlite_free', |
|
| 2511 | + 'hex' => 'spip_sqlite_hex', |
|
| 2512 | + 'in' => 'spip_sqlite_in', |
|
| 2513 | + 'insert' => 'spip_sqlite_insert', |
|
| 2514 | + 'insertq' => 'spip_sqlite_insertq', |
|
| 2515 | + 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2516 | + 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2517 | + 'multi' => 'spip_sqlite_multi', |
|
| 2518 | + 'optimize' => 'spip_sqlite_optimize', |
|
| 2519 | + 'query' => 'spip_sqlite_query', |
|
| 2520 | + 'quote' => 'spip_sqlite_quote', |
|
| 2521 | + 'repair' => 'spip_sqlite_repair', |
|
| 2522 | + 'replace' => 'spip_sqlite_replace', |
|
| 2523 | + 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2524 | + 'select' => 'spip_sqlite_select', |
|
| 2525 | + 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2526 | + 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2527 | + 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2528 | + 'showbase' => 'spip_sqlite_showbase', |
|
| 2529 | + 'showtable' => 'spip_sqlite_showtable', |
|
| 2530 | + 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2531 | + 'update' => 'spip_sqlite_update', |
|
| 2532 | + 'updateq' => 'spip_sqlite_updateq', |
|
| 2533 | + 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2534 | + 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2535 | + 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2536 | + ]; |
|
| 2537 | + |
|
| 2538 | + // association de chaque nom http d'un charset aux couples sqlite |
|
| 2539 | + // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2540 | + $charsets = [ |
|
| 2541 | + 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2542 | + //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2543 | + //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2544 | + ]; |
|
| 2545 | + |
|
| 2546 | + $fonctions['charsets'] = $charsets; |
|
| 2547 | + |
|
| 2548 | + return $fonctions; |
|
| 2549 | 2549 | } |
| 2550 | 2550 | |
| 2551 | 2551 | |
@@ -2558,56 +2558,56 @@ discard block |
||
| 2558 | 2558 | */ |
| 2559 | 2559 | function _sqlite_remplacements_definitions_table($query, $autoinc = false) |
| 2560 | 2560 | { |
| 2561 | - // quelques remplacements |
|
| 2562 | - $num = '(\s*\([0-9]*\))?'; |
|
| 2563 | - $enum = '(\s*\([^\)]*\))?'; |
|
| 2564 | - |
|
| 2565 | - $remplace = [ |
|
| 2566 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2567 | - '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2568 | - '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2569 | - '/auto_increment/is' => '', |
|
| 2570 | - '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2571 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2572 | - '/character set \w+/is' => '', |
|
| 2573 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2574 | - '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2575 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2576 | - '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2577 | - '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2578 | - ]; |
|
| 2579 | - |
|
| 2580 | - // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2581 | - $remplace_autocinc = [ |
|
| 2582 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2583 | - ]; |
|
| 2584 | - // pour les int non autoincrement, il faut un DEFAULT |
|
| 2585 | - $remplace_nonautocinc = [ |
|
| 2586 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2587 | - ]; |
|
| 2588 | - |
|
| 2589 | - if (is_string($query)) { |
|
| 2590 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2591 | - if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2592 | - $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2593 | - } else { |
|
| 2594 | - $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2595 | - $query = _sqlite_collate_ci($query); |
|
| 2596 | - } |
|
| 2597 | - } elseif (is_array($query)) { |
|
| 2598 | - foreach ($query as $k => $q) { |
|
| 2599 | - $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2600 | - $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2601 | - if ($ai) { |
|
| 2602 | - $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2603 | - } else { |
|
| 2604 | - $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2605 | - $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2606 | - } |
|
| 2607 | - } |
|
| 2608 | - } |
|
| 2609 | - |
|
| 2610 | - return $query; |
|
| 2561 | + // quelques remplacements |
|
| 2562 | + $num = '(\s*\([0-9]*\))?'; |
|
| 2563 | + $enum = '(\s*\([^\)]*\))?'; |
|
| 2564 | + |
|
| 2565 | + $remplace = [ |
|
| 2566 | + '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2567 | + '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2568 | + '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2569 | + '/auto_increment/is' => '', |
|
| 2570 | + '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2571 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2572 | + '/character set \w+/is' => '', |
|
| 2573 | + '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2574 | + '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2575 | + '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2576 | + '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2577 | + '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2578 | + ]; |
|
| 2579 | + |
|
| 2580 | + // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2581 | + $remplace_autocinc = [ |
|
| 2582 | + '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2583 | + ]; |
|
| 2584 | + // pour les int non autoincrement, il faut un DEFAULT |
|
| 2585 | + $remplace_nonautocinc = [ |
|
| 2586 | + '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2587 | + ]; |
|
| 2588 | + |
|
| 2589 | + if (is_string($query)) { |
|
| 2590 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2591 | + if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2592 | + $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2593 | + } else { |
|
| 2594 | + $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2595 | + $query = _sqlite_collate_ci($query); |
|
| 2596 | + } |
|
| 2597 | + } elseif (is_array($query)) { |
|
| 2598 | + foreach ($query as $k => $q) { |
|
| 2599 | + $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2600 | + $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2601 | + if ($ai) { |
|
| 2602 | + $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2603 | + } else { |
|
| 2604 | + $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2605 | + $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2606 | + } |
|
| 2607 | + } |
|
| 2608 | + } |
|
| 2609 | + |
|
| 2610 | + return $query; |
|
| 2611 | 2611 | } |
| 2612 | 2612 | |
| 2613 | 2613 | /** |
@@ -2619,17 +2619,17 @@ discard block |
||
| 2619 | 2619 | */ |
| 2620 | 2620 | function _sqlite_collate_ci($champ) |
| 2621 | 2621 | { |
| 2622 | - if (stripos($champ, 'COLLATE') !== false) { |
|
| 2623 | - return $champ; |
|
| 2624 | - } |
|
| 2625 | - if (stripos($champ, 'BINARY') !== false) { |
|
| 2626 | - return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2627 | - } |
|
| 2628 | - if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2629 | - return $champ . ' COLLATE NOCASE'; |
|
| 2630 | - } |
|
| 2631 | - |
|
| 2632 | - return $champ; |
|
| 2622 | + if (stripos($champ, 'COLLATE') !== false) { |
|
| 2623 | + return $champ; |
|
| 2624 | + } |
|
| 2625 | + if (stripos($champ, 'BINARY') !== false) { |
|
| 2626 | + return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2627 | + } |
|
| 2628 | + if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2629 | + return $champ . ' COLLATE NOCASE'; |
|
| 2630 | + } |
|
| 2631 | + |
|
| 2632 | + return $champ; |
|
| 2633 | 2633 | } |
| 2634 | 2634 | |
| 2635 | 2635 | |
@@ -2648,84 +2648,84 @@ discard block |
||
| 2648 | 2648 | * @return bool|string |
| 2649 | 2649 | */ |
| 2650 | 2650 | function _sqlite_requete_create( |
| 2651 | - $nom, |
|
| 2652 | - $champs, |
|
| 2653 | - $cles, |
|
| 2654 | - $autoinc = false, |
|
| 2655 | - $temporary = false, |
|
| 2656 | - $_ifnotexists = true, |
|
| 2657 | - $serveur = '', |
|
| 2658 | - $requeter = true |
|
| 2651 | + $nom, |
|
| 2652 | + $champs, |
|
| 2653 | + $cles, |
|
| 2654 | + $autoinc = false, |
|
| 2655 | + $temporary = false, |
|
| 2656 | + $_ifnotexists = true, |
|
| 2657 | + $serveur = '', |
|
| 2658 | + $requeter = true |
|
| 2659 | 2659 | ) { |
| 2660 | - $query = $keys = $s = $p = ''; |
|
| 2661 | - |
|
| 2662 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2663 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2664 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 2665 | - return; |
|
| 2666 | - } |
|
| 2667 | - |
|
| 2668 | - // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2669 | - // il faut passer par des create index |
|
| 2670 | - // Il gere par contre primary key ! |
|
| 2671 | - // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2672 | - // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2673 | - $pk = 'PRIMARY KEY'; |
|
| 2674 | - // le champ de cle primaire |
|
| 2675 | - $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2676 | - |
|
| 2677 | - foreach ($champs as $k => $v) { |
|
| 2678 | - if (false !== stripos($v, $pk)) { |
|
| 2679 | - $champ_pk = $k; |
|
| 2680 | - // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2681 | - $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2682 | - break; |
|
| 2683 | - } |
|
| 2684 | - } |
|
| 2685 | - |
|
| 2686 | - if ($champ_pk) { |
|
| 2687 | - $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2688 | - } |
|
| 2689 | - // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2690 | - if ( |
|
| 2691 | - isset($champs[$champ_pk]) |
|
| 2692 | - and stripos($champs[$champ_pk], 'default 0') !== false |
|
| 2693 | - ) { |
|
| 2694 | - $champs[$champ_pk] = trim(str_ireplace('default 0', '', $champs[$champ_pk])); |
|
| 2695 | - } |
|
| 2696 | - |
|
| 2697 | - $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2698 | - foreach ($champs as $k => $v) { |
|
| 2699 | - $query .= "$s\n\t\t$k $v"; |
|
| 2700 | - $s = ','; |
|
| 2701 | - } |
|
| 2702 | - |
|
| 2703 | - $ifnotexists = ''; |
|
| 2704 | - if ($_ifnotexists) { |
|
| 2705 | - $version = spip_sqlite_fetch( |
|
| 2706 | - spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2707 | - '', |
|
| 2708 | - $serveur |
|
| 2709 | - ); |
|
| 2710 | - if (!function_exists('spip_version_compare')) { |
|
| 2711 | - include_spip('plugins/installer'); |
|
| 2712 | - } |
|
| 2713 | - |
|
| 2714 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2715 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 2716 | - } else { |
|
| 2717 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2718 | - $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2719 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2720 | - return true; |
|
| 2721 | - } |
|
| 2722 | - } |
|
| 2723 | - } |
|
| 2724 | - |
|
| 2725 | - $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2726 | - $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2727 | - |
|
| 2728 | - return $q; |
|
| 2660 | + $query = $keys = $s = $p = ''; |
|
| 2661 | + |
|
| 2662 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2663 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2664 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 2665 | + return; |
|
| 2666 | + } |
|
| 2667 | + |
|
| 2668 | + // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2669 | + // il faut passer par des create index |
|
| 2670 | + // Il gere par contre primary key ! |
|
| 2671 | + // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2672 | + // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2673 | + $pk = 'PRIMARY KEY'; |
|
| 2674 | + // le champ de cle primaire |
|
| 2675 | + $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2676 | + |
|
| 2677 | + foreach ($champs as $k => $v) { |
|
| 2678 | + if (false !== stripos($v, $pk)) { |
|
| 2679 | + $champ_pk = $k; |
|
| 2680 | + // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2681 | + $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2682 | + break; |
|
| 2683 | + } |
|
| 2684 | + } |
|
| 2685 | + |
|
| 2686 | + if ($champ_pk) { |
|
| 2687 | + $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2688 | + } |
|
| 2689 | + // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2690 | + if ( |
|
| 2691 | + isset($champs[$champ_pk]) |
|
| 2692 | + and stripos($champs[$champ_pk], 'default 0') !== false |
|
| 2693 | + ) { |
|
| 2694 | + $champs[$champ_pk] = trim(str_ireplace('default 0', '', $champs[$champ_pk])); |
|
| 2695 | + } |
|
| 2696 | + |
|
| 2697 | + $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2698 | + foreach ($champs as $k => $v) { |
|
| 2699 | + $query .= "$s\n\t\t$k $v"; |
|
| 2700 | + $s = ','; |
|
| 2701 | + } |
|
| 2702 | + |
|
| 2703 | + $ifnotexists = ''; |
|
| 2704 | + if ($_ifnotexists) { |
|
| 2705 | + $version = spip_sqlite_fetch( |
|
| 2706 | + spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2707 | + '', |
|
| 2708 | + $serveur |
|
| 2709 | + ); |
|
| 2710 | + if (!function_exists('spip_version_compare')) { |
|
| 2711 | + include_spip('plugins/installer'); |
|
| 2712 | + } |
|
| 2713 | + |
|
| 2714 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2715 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 2716 | + } else { |
|
| 2717 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2718 | + $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2719 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 2720 | + return true; |
|
| 2721 | + } |
|
| 2722 | + } |
|
| 2723 | + } |
|
| 2724 | + |
|
| 2725 | + $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2726 | + $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2727 | + |
|
| 2728 | + return $q; |
|
| 2729 | 2729 | } |
| 2730 | 2730 | |
| 2731 | 2731 | |
@@ -2745,40 +2745,40 @@ discard block |
||
| 2745 | 2745 | */ |
| 2746 | 2746 | function _sqlite_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') |
| 2747 | 2747 | { |
| 2748 | - static $tables = []; |
|
| 2749 | - |
|
| 2750 | - if (!isset($tables[$table])) { |
|
| 2751 | - if (!$desc) { |
|
| 2752 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2753 | - $desc = $trouver_table($table, $serveur); |
|
| 2754 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 2755 | - if (!$desc) { |
|
| 2756 | - return $couples; |
|
| 2757 | - } |
|
| 2758 | - } |
|
| 2759 | - |
|
| 2760 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2761 | - // cependant, il faudra peut etre etendre |
|
| 2762 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 2763 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 2764 | - $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2765 | - |
|
| 2766 | - $now = _sqlite_func_now(true); |
|
| 2767 | - foreach ($desc['field'] as $k => $v) { |
|
| 2768 | - if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2769 | - $tables[$table]['desc'][$k] = $v; |
|
| 2770 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2771 | - } |
|
| 2772 | - } |
|
| 2773 | - } else { |
|
| 2774 | - $now = _sqlite_func_now(true); |
|
| 2775 | - foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2776 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2777 | - } |
|
| 2778 | - } |
|
| 2779 | - |
|
| 2780 | - // ajout des champs type 'timestamp' absents |
|
| 2781 | - return array_merge($tables[$table]['valeur'], $couples); |
|
| 2748 | + static $tables = []; |
|
| 2749 | + |
|
| 2750 | + if (!isset($tables[$table])) { |
|
| 2751 | + if (!$desc) { |
|
| 2752 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2753 | + $desc = $trouver_table($table, $serveur); |
|
| 2754 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 2755 | + if (!$desc) { |
|
| 2756 | + return $couples; |
|
| 2757 | + } |
|
| 2758 | + } |
|
| 2759 | + |
|
| 2760 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2761 | + // cependant, il faudra peut etre etendre |
|
| 2762 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 2763 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 2764 | + $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2765 | + |
|
| 2766 | + $now = _sqlite_func_now(true); |
|
| 2767 | + foreach ($desc['field'] as $k => $v) { |
|
| 2768 | + if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2769 | + $tables[$table]['desc'][$k] = $v; |
|
| 2770 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2771 | + } |
|
| 2772 | + } |
|
| 2773 | + } else { |
|
| 2774 | + $now = _sqlite_func_now(true); |
|
| 2775 | + foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2776 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2777 | + } |
|
| 2778 | + } |
|
| 2779 | + |
|
| 2780 | + // ajout des champs type 'timestamp' absents |
|
| 2781 | + return array_merge($tables[$table]['valeur'], $couples); |
|
| 2782 | 2782 | } |
| 2783 | 2783 | |
| 2784 | 2784 | |
@@ -2790,5 +2790,5 @@ discard block |
||
| 2790 | 2790 | */ |
| 2791 | 2791 | function spip_versions_sqlite() |
| 2792 | 2792 | { |
| 2793 | - return _sqlite_charger_version(); |
|
| 2793 | + return _sqlite_charger_version(); |
|
| 2794 | 2794 | } |
@@ -66,27 +66,27 @@ discard block |
||
| 66 | 66 | // determiner le dossier de la base : $addr ou _DIR_DB |
| 67 | 67 | $f = _DIR_DB; |
| 68 | 68 | if ($addr and str_contains($addr, '/')) { |
| 69 | - $f = rtrim($addr, '/') . '/'; |
|
| 69 | + $f = rtrim($addr, '/').'/'; |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | // un nom de base demande et impossible d'obtenir la base, on s'en va : |
| 73 | 73 | // il faut que la base existe ou que le repertoire parent soit writable |
| 74 | - if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 75 | - spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 74 | + if ($db and !is_file($f .= $db.'.sqlite') and !is_writable(dirname($f))) { |
|
| 75 | + spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.'._LOG_HS); |
|
| 76 | 76 | |
| 77 | 77 | return false; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | // charger les modules sqlite au besoin |
| 81 | 81 | if (!_sqlite_charger_version($sqlite_version)) { |
| 82 | - spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 82 | + spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.'._LOG_HS); |
|
| 83 | 83 | |
| 84 | 84 | return false; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | // chargement des constantes |
| 88 | 88 | // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
| 89 | - $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 89 | + $define = 'spip_sqlite'.$sqlite_version.'_constantes'; |
|
| 90 | 90 | $define(); |
| 91 | 91 | |
| 92 | 92 | $ok = false; |
@@ -94,8 +94,8 @@ discard block |
||
| 94 | 94 | // si pas de db -> |
| 95 | 95 | // base temporaire tant qu'on ne connait pas son vrai nom |
| 96 | 96 | // pour tester la connexion |
| 97 | - $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 98 | - $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 97 | + $db = '_sqlite'.$sqlite_version.'_install'; |
|
| 98 | + $tmp = _DIR_DB.$db.'.sqlite'; |
|
| 99 | 99 | $ok = $link = new \PDO("sqlite:$tmp"); |
| 100 | 100 | } else { |
| 101 | 101 | // Ouvrir (eventuellement creer la base) |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | |
| 105 | 105 | if (!$ok) { |
| 106 | 106 | $e = _sqlite_last_error_from_link($link); |
| 107 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 107 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.'._LOG_HS); |
|
| 108 | 108 | |
| 109 | 109 | return false; |
| 110 | 110 | } |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | $table = $regs[3]; |
| 193 | 193 | $suite = $regs[4]; |
| 194 | 194 | } else { |
| 195 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 195 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.'._LOG_ERREUR); |
|
| 196 | 196 | |
| 197 | 197 | return false; |
| 198 | 198 | } |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | $i = 0; |
| 210 | 210 | $ouverte = false; |
| 211 | 211 | while ($do = array_shift($todo)) { |
| 212 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 212 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i].','.$do : $do; |
|
| 213 | 213 | $o = (str_contains($do, '(')); |
| 214 | 214 | $f = (str_contains($do, ')')); |
| 215 | 215 | if ($o and !$f) { |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | ) { |
| 236 | 236 | spip_log( |
| 237 | 237 | "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
| 238 | - 'sqlite.' . _LOG_ERREUR |
|
| 238 | + 'sqlite.'._LOG_ERREUR |
|
| 239 | 239 | ); |
| 240 | 240 | |
| 241 | 241 | return false; |
@@ -331,10 +331,10 @@ discard block |
||
| 331 | 331 | |
| 332 | 332 | // pas geres en sqlite2 |
| 333 | 333 | case 'RENAME': |
| 334 | - $do = 'RENAME TO' . substr($do, 6); |
|
| 334 | + $do = 'RENAME TO'.substr($do, 6); |
|
| 335 | 335 | case 'RENAME TO': |
| 336 | 336 | if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
| 337 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 337 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.'._LOG_ERREUR); |
|
| 338 | 338 | |
| 339 | 339 | return false; |
| 340 | 340 | } |
@@ -377,7 +377,7 @@ discard block |
||
| 377 | 377 | $colonnes = substr($colonne_origine, 1, -1); |
| 378 | 378 | if (str_contains(',', $colonnes)) { |
| 379 | 379 | spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
| 380 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 380 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.'._LOG_ERREUR); |
|
| 381 | 381 | break; |
| 382 | 382 | } else { |
| 383 | 383 | $nom_index = $colonnes; |
@@ -392,12 +392,12 @@ discard block |
||
| 392 | 392 | |
| 393 | 393 | // pas geres en sqlite2 |
| 394 | 394 | case 'ADD COLUMN': |
| 395 | - $do = 'ADD' . substr($do, 10); |
|
| 395 | + $do = 'ADD'.substr($do, 10); |
|
| 396 | 396 | case 'ADD': |
| 397 | 397 | default: |
| 398 | 398 | if (!preg_match(',primary\s+key,i', $do)) { |
| 399 | 399 | if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
| 400 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 400 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.'._LOG_ERREUR); |
|
| 401 | 401 | |
| 402 | 402 | return false; |
| 403 | 403 | } |
@@ -417,7 +417,7 @@ discard block |
||
| 417 | 417 | } |
| 418 | 418 | $opts['field'] = [$colonne_ajoutee => $def]; |
| 419 | 419 | if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
| 420 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 420 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.'._LOG_ERREUR); |
|
| 421 | 421 | |
| 422 | 422 | return false; |
| 423 | 423 | } |
@@ -425,10 +425,10 @@ discard block |
||
| 425 | 425 | break; |
| 426 | 426 | } |
| 427 | 427 | // tout est bon, ouf ! |
| 428 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 428 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.'._LOG_INFO); |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 431 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.'._LOG_INFO); |
|
| 432 | 432 | |
| 433 | 433 | return true; |
| 434 | 434 | } |
@@ -497,9 +497,9 @@ discard block |
||
| 497 | 497 | **/ |
| 498 | 498 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) |
| 499 | 499 | { |
| 500 | - $f = $nom . '.sqlite'; |
|
| 500 | + $f = $nom.'.sqlite'; |
|
| 501 | 501 | if (strpos($nom, '/') === false) { |
| 502 | - $f = _DIR_DB . $f; |
|
| 502 | + $f = _DIR_DB.$f; |
|
| 503 | 503 | } |
| 504 | 504 | |
| 505 | 505 | $ok = new \PDO("sqlite:$f"); |
@@ -540,13 +540,13 @@ discard block |
||
| 540 | 540 | if (sql_showtable($nom, false, $serveur)) { |
| 541 | 541 | spip_log( |
| 542 | 542 | "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
| 543 | - 'sqlite.' . _LOG_ERREUR |
|
| 543 | + 'sqlite.'._LOG_ERREUR |
|
| 544 | 544 | ); |
| 545 | 545 | |
| 546 | 546 | return false; |
| 547 | 547 | } |
| 548 | 548 | |
| 549 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 549 | + $query = "CREATE VIEW $nom AS ".$query_select; |
|
| 550 | 550 | |
| 551 | 551 | return spip_sqlite_query($query, $serveur, $requeter); |
| 552 | 552 | } |
@@ -573,8 +573,8 @@ discard block |
||
| 573 | 573 | { |
| 574 | 574 | if (!($nom or $table or $champs)) { |
| 575 | 575 | spip_log( |
| 576 | - "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 577 | - 'sqlite.' . _LOG_ERREUR |
|
| 576 | + "Champ manquant pour creer un index sqlite ($nom, $table, (".join(',', $champs).'))', |
|
| 577 | + 'sqlite.'._LOG_ERREUR |
|
| 578 | 578 | ); |
| 579 | 579 | |
| 580 | 580 | return false; |
@@ -582,7 +582,7 @@ discard block |
||
| 582 | 582 | |
| 583 | 583 | // SQLite ne differentie pas noms des index en fonction des tables |
| 584 | 584 | // il faut donc creer des noms uniques d'index pour une base sqlite |
| 585 | - $nom = $table . '_' . $nom; |
|
| 585 | + $nom = $table.'_'.$nom; |
|
| 586 | 586 | // enlever d'eventuelles parentheses deja presentes sur champs |
| 587 | 587 | if (!is_array($champs)) { |
| 588 | 588 | if ($champs[0] == '(') { |
@@ -604,12 +604,12 @@ discard block |
||
| 604 | 604 | } else { |
| 605 | 605 | /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
| 606 | 606 | $a = spip_sqlite_showtable($table, $serveur); |
| 607 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 607 | + if (isset($a['key']['KEY '.$nom])) { |
|
| 608 | 608 | return true; |
| 609 | 609 | } |
| 610 | 610 | } |
| 611 | 611 | |
| 612 | - $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 612 | + $query = 'CREATE '.($unique ? 'UNIQUE ' : '')."INDEX$ifnotexists $nom ON $table (".join(',', $champs).')'; |
|
| 613 | 613 | $res = spip_sqlite_query($query, $serveur, $requeter); |
| 614 | 614 | if (!$requeter) { |
| 615 | 615 | return $res; |
@@ -686,7 +686,7 @@ discard block |
||
| 686 | 686 | $serveur = '', |
| 687 | 687 | $requeter = true |
| 688 | 688 | ) { |
| 689 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 689 | + $c = !$groupby ? '*' : ('DISTINCT '.(is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 690 | 690 | $r = spip_sqlite_select( |
| 691 | 691 | "COUNT($c)", |
| 692 | 692 | $from, |
@@ -799,14 +799,14 @@ discard block |
||
| 799 | 799 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) |
| 800 | 800 | { |
| 801 | 801 | if (!($nom or $table)) { |
| 802 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 802 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.'._LOG_ERREUR); |
|
| 803 | 803 | |
| 804 | 804 | return false; |
| 805 | 805 | } |
| 806 | 806 | |
| 807 | 807 | // SQLite ne differentie pas noms des index en fonction des tables |
| 808 | 808 | // il faut donc creer des noms uniques d'index pour une base sqlite |
| 809 | - $index = $table . '_' . $nom; |
|
| 809 | + $index = $table.'_'.$nom; |
|
| 810 | 810 | $exist = ' IF EXISTS'; |
| 811 | 811 | |
| 812 | 812 | $query = "DROP INDEX$exist $index"; |
@@ -839,7 +839,7 @@ discard block |
||
| 839 | 839 | if ($s) { |
| 840 | 840 | $trace = debug_backtrace(); |
| 841 | 841 | if ($trace[0]['function'] != 'spip_sqlite_error') { |
| 842 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 842 | + spip_log("$s - $query - ".sql_error_backtrace(), 'sqlite.'._LOG_ERREUR); |
|
| 843 | 843 | } |
| 844 | 844 | } |
| 845 | 845 | |
@@ -888,14 +888,14 @@ discard block |
||
| 888 | 888 | $t = $link->errorInfo(); |
| 889 | 889 | $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
| 890 | 890 | if ($s) { |
| 891 | - $s .= ' / ' . $t[1]; |
|
| 891 | + $s .= ' / '.$t[1]; |
|
| 892 | 892 | } // ajoute l'erreur du moteur SQLite |
| 893 | 893 | } else { |
| 894 | 894 | $s = ': aucune ressource sqlite (link)'; |
| 895 | 895 | } |
| 896 | 896 | |
| 897 | 897 | if ($s) { |
| 898 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 898 | + spip_log("Erreur sqlite $s", 'sqlite.'._LOG_ERREUR); |
|
| 899 | 899 | } |
| 900 | 900 | |
| 901 | 901 | return $s ? $s : 0; |
@@ -920,7 +920,7 @@ discard block |
||
| 920 | 920 | } |
| 921 | 921 | |
| 922 | 922 | $query = Sqlite::traduire_requete($query, $serveur); |
| 923 | - $query = 'EXPLAIN ' . $query; |
|
| 923 | + $query = 'EXPLAIN '.$query; |
|
| 924 | 924 | if (!$requeter) { |
| 925 | 925 | return $query; |
| 926 | 926 | } |
@@ -1096,7 +1096,7 @@ discard block |
||
| 1096 | 1096 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) |
| 1097 | 1097 | { |
| 1098 | 1098 | |
| 1099 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1099 | + $query = "INSERT INTO $table ".($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1100 | 1100 | if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
| 1101 | 1101 | if (!$requeter) { |
| 1102 | 1102 | return $r; |
@@ -1152,8 +1152,8 @@ discard block |
||
| 1152 | 1152 | |
| 1153 | 1153 | $cles = $valeurs = ''; |
| 1154 | 1154 | if (count($couples)) { |
| 1155 | - $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1156 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1155 | + $cles = '('.join(',', array_keys($couples)).')'; |
|
| 1156 | + $valeurs = '('.join(',', $couples).')'; |
|
| 1157 | 1157 | } |
| 1158 | 1158 | |
| 1159 | 1159 | return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
@@ -1214,11 +1214,11 @@ discard block |
||
| 1214 | 1214 | |
| 1215 | 1215 | $champs = $valeurs = ''; |
| 1216 | 1216 | if (count($couples)) { |
| 1217 | - $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1218 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1219 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1217 | + $champs = '('.join(',', array_keys($couples)).')'; |
|
| 1218 | + $valeurs = '('.join(',', $couples).')'; |
|
| 1219 | + $query = $query_start."$champs VALUES $valeurs"; |
|
| 1220 | 1220 | } else { |
| 1221 | - $query = $query_start . 'DEFAULT VALUES'; |
|
| 1221 | + $query = $query_start.'DEFAULT VALUES'; |
|
| 1222 | 1222 | } |
| 1223 | 1223 | |
| 1224 | 1224 | if ($requeter) { |
@@ -1355,7 +1355,7 @@ discard block |
||
| 1355 | 1355 | */ |
| 1356 | 1356 | function spip_sqlite_multi($objet, $lang) |
| 1357 | 1357 | { |
| 1358 | - $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1358 | + $r = 'EXTRAIRE_MULTI('.$objet.", '".$lang."') AS multi"; |
|
| 1359 | 1359 | |
| 1360 | 1360 | return $r; |
| 1361 | 1361 | } |
@@ -1429,7 +1429,7 @@ discard block |
||
| 1429 | 1429 | { |
| 1430 | 1430 | $op = (($interval <= 0) ? '>' : '<'); |
| 1431 | 1431 | |
| 1432 | - return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1432 | + return "($champ $op datetime('".date('Y-m-d H:i:s')."', '$interval $unite'))"; |
|
| 1433 | 1433 | } |
| 1434 | 1434 | |
| 1435 | 1435 | |
@@ -1462,7 +1462,7 @@ discard block |
||
| 1462 | 1462 | and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
| 1463 | 1463 | ) { |
| 1464 | 1464 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
| 1465 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1465 | + spip_log("ALTER $q", 'repair'._LOG_INFO_IMPORTANTE); |
|
| 1466 | 1466 | } |
| 1467 | 1467 | if ( |
| 1468 | 1468 | preg_match(',^(INTEGER),i', $d) |
@@ -1472,7 +1472,7 @@ discard block |
||
| 1472 | 1472 | and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
| 1473 | 1473 | ) { |
| 1474 | 1474 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
| 1475 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1475 | + spip_log("ALTER $q", 'repair'._LOG_INFO_IMPORTANTE); |
|
| 1476 | 1476 | } |
| 1477 | 1477 | if ( |
| 1478 | 1478 | preg_match(',^(datetime),i', $d) |
@@ -1482,7 +1482,7 @@ discard block |
||
| 1482 | 1482 | and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
| 1483 | 1483 | ) { |
| 1484 | 1484 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
| 1485 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1485 | + spip_log("ALTER $q", 'repair'._LOG_INFO_IMPORTANTE); |
|
| 1486 | 1486 | } |
| 1487 | 1487 | } |
| 1488 | 1488 | |
@@ -1534,10 +1534,10 @@ discard block |
||
| 1534 | 1534 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
| 1535 | 1535 | $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
| 1536 | 1536 | |
| 1537 | - return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1537 | + return spip_sqlite_query("REPLACE INTO $table (".join(',', array_keys($couples)).') VALUES ('.join( |
|
| 1538 | 1538 | ',', |
| 1539 | 1539 | $couples |
| 1540 | - ) . ')', $serveur); |
|
| 1540 | + ).')', $serveur); |
|
| 1541 | 1541 | } |
| 1542 | 1542 | |
| 1543 | 1543 | |
@@ -1624,7 +1624,7 @@ discard block |
||
| 1624 | 1624 | . _sqlite_calculer_expression('WHERE', $where) |
| 1625 | 1625 | . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
| 1626 | 1626 | . _sqlite_calculer_expression('HAVING', $having) |
| 1627 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1627 | + . ($orderby ? ("\nORDER BY "._sqlite_calculer_order($orderby)) : '') |
|
| 1628 | 1628 | . ($limit ? "\nLIMIT $limit" : ''); |
| 1629 | 1629 | |
| 1630 | 1630 | // dans un select, on doit renvoyer la requête en cas d'erreur |
@@ -1663,10 +1663,10 @@ discard block |
||
| 1663 | 1663 | // interdire la creation d'une nouvelle base, |
| 1664 | 1664 | // sauf si on est dans l'installation |
| 1665 | 1665 | if ( |
| 1666 | - !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1666 | + !is_file($f = _DIR_DB.$db.'.sqlite') |
|
| 1667 | 1667 | && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
| 1668 | 1668 | ) { |
| 1669 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1669 | + spip_log("Il est interdit de creer la base $db", 'sqlite.'._LOG_HS); |
|
| 1670 | 1670 | |
| 1671 | 1671 | return false; |
| 1672 | 1672 | } |
@@ -1675,12 +1675,12 @@ discard block |
||
| 1675 | 1675 | // avec les identifiants connus |
| 1676 | 1676 | $index = $serveur ? $serveur : 0; |
| 1677 | 1677 | |
| 1678 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1678 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@'.$db, $serveur, '', '')) { |
|
| 1679 | 1679 | if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
| 1680 | 1680 | return $db; |
| 1681 | 1681 | } |
| 1682 | 1682 | } else { |
| 1683 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1683 | + spip_log("Impossible de selectionner la base $db", 'sqlite.'._LOG_HS); |
|
| 1684 | 1684 | |
| 1685 | 1685 | return false; |
| 1686 | 1686 | } |
@@ -1733,7 +1733,7 @@ discard block |
||
| 1733 | 1733 | $match = "^$match$"; |
| 1734 | 1734 | |
| 1735 | 1735 | return spip_sqlite_query( |
| 1736 | - "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1736 | + "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP "._q($match), |
|
| 1737 | 1737 | $serveur, |
| 1738 | 1738 | $requeter |
| 1739 | 1739 | ); |
@@ -1758,7 +1758,7 @@ discard block |
||
| 1758 | 1758 | $r = spip_sqlite_query( |
| 1759 | 1759 | 'SELECT name FROM sqlite_master WHERE' |
| 1760 | 1760 | . ' type=\'table\'' |
| 1761 | - . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1761 | + . ' AND name='.spip_sqlite_quote($table, 'string') |
|
| 1762 | 1762 | . ' AND name NOT LIKE \'sqlite_%\'', |
| 1763 | 1763 | $serveur, |
| 1764 | 1764 | $requeter |
@@ -1856,7 +1856,7 @@ discard block |
||
| 1856 | 1856 | // s'il y a une parenthèse fermante dans la clé |
| 1857 | 1857 | // ou dans la définition sans qu'il n'y ait une ouverture avant |
| 1858 | 1858 | if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
| 1859 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1859 | + $fields[$k_precedent] .= ','.$k.' '.$def; |
|
| 1860 | 1860 | continue; |
| 1861 | 1861 | } |
| 1862 | 1862 | |
@@ -1891,13 +1891,13 @@ discard block |
||
| 1891 | 1891 | . 'ORDER BY substr(type,2,1), name'; |
| 1892 | 1892 | $a = spip_sqlite_query($query, $serveur, $requeter); |
| 1893 | 1893 | while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
| 1894 | - $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1894 | + $key = str_replace($nom_table.'_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1895 | 1895 | $keytype = 'KEY'; |
| 1896 | 1896 | if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
| 1897 | 1897 | $keytype = 'UNIQUE KEY'; |
| 1898 | 1898 | } |
| 1899 | 1899 | $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
| 1900 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1900 | + $keys[$keytype.' '.$key] = $colonnes; |
|
| 1901 | 1901 | } |
| 1902 | 1902 | } |
| 1903 | 1903 | } // c'est une vue, on liste les champs disponibles simplement |
@@ -1945,7 +1945,7 @@ discard block |
||
| 1945 | 1945 | |
| 1946 | 1946 | $set = []; |
| 1947 | 1947 | foreach ($champs as $champ => $val) { |
| 1948 | - $set[] = $champ . "=$val"; |
|
| 1948 | + $set[] = $champ."=$val"; |
|
| 1949 | 1949 | } |
| 1950 | 1950 | if (!empty($set)) { |
| 1951 | 1951 | return spip_sqlite_query( |
@@ -1999,7 +1999,7 @@ discard block |
||
| 1999 | 1999 | |
| 2000 | 2000 | $set = []; |
| 2001 | 2001 | foreach ($champs as $champ => $val) { |
| 2002 | - $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2002 | + $set[$champ] = $champ.'='._sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2003 | 2003 | } |
| 2004 | 2004 | |
| 2005 | 2005 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
@@ -2007,7 +2007,7 @@ discard block |
||
| 2007 | 2007 | $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
| 2008 | 2008 | foreach ($maj as $champ => $val) { |
| 2009 | 2009 | if (!isset($set[$champ])) { |
| 2010 | - $set[$champ] = $champ . '=' . $val; |
|
| 2010 | + $set[$champ] = $champ.'='.$val; |
|
| 2011 | 2011 | } |
| 2012 | 2012 | } |
| 2013 | 2013 | |
@@ -2037,7 +2037,7 @@ discard block |
||
| 2037 | 2037 | function _sqlite_init() |
| 2038 | 2038 | { |
| 2039 | 2039 | if (!defined('_DIR_DB')) { |
| 2040 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2040 | + define('_DIR_DB', _DIR_ETC.'bases/'); |
|
| 2041 | 2041 | } |
| 2042 | 2042 | if (!defined('_SQLITE_CHMOD')) { |
| 2043 | 2043 | define('_SQLITE_CHMOD', _SPIP_CHMOD); |
@@ -2149,9 +2149,9 @@ discard block |
||
| 2149 | 2149 | } |
| 2150 | 2150 | |
| 2151 | 2151 | // echapper les ' en '' |
| 2152 | - spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2152 | + spip_log('Pas de methode ->quote pour echapper', 'sqlite.'._LOG_INFO_IMPORTANTE); |
|
| 2153 | 2153 | |
| 2154 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2154 | + return ("'".str_replace("'", "''", $v)."'"); |
|
| 2155 | 2155 | } |
| 2156 | 2156 | |
| 2157 | 2157 | |
@@ -2175,12 +2175,12 @@ discard block |
||
| 2175 | 2175 | $exp = "\n$expression "; |
| 2176 | 2176 | |
| 2177 | 2177 | if (!is_array($v)) { |
| 2178 | - return $exp . $v; |
|
| 2178 | + return $exp.$v; |
|
| 2179 | 2179 | } else { |
| 2180 | 2180 | if (strtoupper($join) === 'AND') { |
| 2181 | - return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2181 | + return $exp.join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2182 | 2182 | } else { |
| 2183 | - return $exp . join($join, $v); |
|
| 2183 | + return $exp.join($join, $v); |
|
| 2184 | 2184 | } |
| 2185 | 2185 | } |
| 2186 | 2186 | } |
@@ -2216,17 +2216,17 @@ discard block |
||
| 2216 | 2216 | if (substr($k, -1) == '@') { |
| 2217 | 2217 | // c'est une jointure qui se refere au from precedent |
| 2218 | 2218 | // pas de virgule |
| 2219 | - $res .= ' ' . $v; |
|
| 2219 | + $res .= ' '.$v; |
|
| 2220 | 2220 | } else { |
| 2221 | 2221 | if (!is_numeric($k)) { |
| 2222 | 2222 | $p = strpos($v, ' '); |
| 2223 | 2223 | if ($p) { |
| 2224 | - $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2224 | + $v = substr($v, 0, $p)." AS '$k'".substr($v, $p); |
|
| 2225 | 2225 | } else { |
| 2226 | 2226 | $v .= " AS '$k'"; |
| 2227 | 2227 | } |
| 2228 | 2228 | } |
| 2229 | - $res .= ', ' . $v; |
|
| 2229 | + $res .= ', '.$v; |
|
| 2230 | 2230 | } |
| 2231 | 2231 | } |
| 2232 | 2232 | |
@@ -2367,13 +2367,13 @@ discard block |
||
| 2367 | 2367 | |
| 2368 | 2368 | $def_origine = sql_showtable($table_origine, false, $serveur); |
| 2369 | 2369 | if (!$def_origine or !isset($def_origine['field'])) { |
| 2370 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2370 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite'._LOG_ERREUR); |
|
| 2371 | 2371 | |
| 2372 | 2372 | return false; |
| 2373 | 2373 | } |
| 2374 | 2374 | |
| 2375 | 2375 | |
| 2376 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2376 | + $table_tmp = $table_origine.'_tmp'; |
|
| 2377 | 2377 | |
| 2378 | 2378 | // 1) creer une table temporaire avec les modifications |
| 2379 | 2379 | // - DROP : suppression de la colonne |
@@ -2460,7 +2460,7 @@ discard block |
||
| 2460 | 2460 | } else { |
| 2461 | 2461 | // enlever KEY |
| 2462 | 2462 | $k = substr($k, 4); |
| 2463 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2463 | + $queries[] = "CREATE INDEX $table_destination"."_$k ON $table_destination ($v)"; |
|
| 2464 | 2464 | } |
| 2465 | 2465 | } |
| 2466 | 2466 | |
@@ -2471,7 +2471,7 @@ discard block |
||
| 2471 | 2471 | foreach ($queries as $q) { |
| 2472 | 2472 | if (!Sqlite::executer_requete($q, $serveur)) { |
| 2473 | 2473 | spip_log('SQLite : ALTER TABLE table :' |
| 2474 | - . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2474 | + . " Erreur a l'execution de la requete : $q", 'sqlite.'._LOG_ERREUR); |
|
| 2475 | 2475 | Sqlite::annuler_transaction($serveur); |
| 2476 | 2476 | |
| 2477 | 2477 | return false; |
@@ -2563,27 +2563,27 @@ discard block |
||
| 2563 | 2563 | $enum = '(\s*\([^\)]*\))?'; |
| 2564 | 2564 | |
| 2565 | 2565 | $remplace = [ |
| 2566 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2566 | + '/enum'.$enum.'/is' => 'VARCHAR(255)', |
|
| 2567 | 2567 | '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
| 2568 | 2568 | '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
| 2569 | 2569 | '/auto_increment/is' => '', |
| 2570 | 2570 | '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
| 2571 | 2571 | '/(timestamp .* )ON .*$/is' => '\\1', |
| 2572 | 2572 | '/character set \w+/is' => '', |
| 2573 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2573 | + '/((big|small|medium|tiny)?int(eger)?)'.$num.'\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2574 | 2574 | '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
| 2575 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2575 | + '/((char|varchar)'.$num.'\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2576 | 2576 | '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
| 2577 | 2577 | '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
| 2578 | 2578 | ]; |
| 2579 | 2579 | |
| 2580 | 2580 | // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
| 2581 | 2581 | $remplace_autocinc = [ |
| 2582 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2582 | + '/(big|small|medium|tiny)?int(eger)?'.$num.'/is' => 'INTEGER' |
|
| 2583 | 2583 | ]; |
| 2584 | 2584 | // pour les int non autoincrement, il faut un DEFAULT |
| 2585 | 2585 | $remplace_nonautocinc = [ |
| 2586 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2586 | + '/((big|small|medium|tiny)?int(eger)?'.$num.'\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2587 | 2587 | ]; |
| 2588 | 2588 | |
| 2589 | 2589 | if (is_string($query)) { |
@@ -2626,7 +2626,7 @@ discard block |
||
| 2626 | 2626 | return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
| 2627 | 2627 | } |
| 2628 | 2628 | if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
| 2629 | - return $champ . ' COLLATE NOCASE'; |
|
| 2629 | + return $champ.' COLLATE NOCASE'; |
|
| 2630 | 2630 | } |
| 2631 | 2631 | |
| 2632 | 2632 | return $champ; |
@@ -2716,14 +2716,14 @@ discard block |
||
| 2716 | 2716 | } else { |
| 2717 | 2717 | /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
| 2718 | 2718 | $a = spip_sqlite_showtable($nom, $serveur); |
| 2719 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2719 | + if (isset($a['key']['KEY '.$nom])) { |
|
| 2720 | 2720 | return true; |
| 2721 | 2721 | } |
| 2722 | 2722 | } |
| 2723 | 2723 | } |
| 2724 | 2724 | |
| 2725 | 2725 | $temporary = $temporary ? ' TEMPORARY' : ''; |
| 2726 | - $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2726 | + $q = "CREATE$temporary TABLE$ifnotexists $nom ($query".($keys ? ",$keys" : '').")\n"; |
|
| 2727 | 2727 | |
| 2728 | 2728 | return $q; |
| 2729 | 2729 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | |
| 23 | 23 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 24 | - return; |
|
| 24 | + return; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | |
@@ -44,70 +44,70 @@ discard block |
||
| 44 | 44 | * @return array|string |
| 45 | 45 | */ |
| 46 | 46 | function action_inscrire_auteur_dist($statut, $mail_complet, $nom, $options = []) { |
| 47 | - if (!is_array($options)) { |
|
| 48 | - $options = ['id' => $options]; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - if (function_exists('test_inscription')) { |
|
| 52 | - $f = 'test_inscription'; |
|
| 53 | - } else { |
|
| 54 | - $f = 'test_inscription_dist'; |
|
| 55 | - } |
|
| 56 | - $desc = $f($statut, $mail_complet, $nom, $options); |
|
| 57 | - |
|
| 58 | - // erreur ? |
|
| 59 | - if (!is_array($desc)) { |
|
| 60 | - return _T($desc); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - include_spip('base/abstract_sql'); |
|
| 64 | - $res = sql_select('statut, id_auteur, login, email, nom', 'spip_auteurs', 'email=' . sql_quote($desc['email'])); |
|
| 65 | - // erreur ? |
|
| 66 | - if (!$res) { |
|
| 67 | - return _T('titre_probleme_technique'); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $row = sql_fetch($res); |
|
| 71 | - sql_free($res); |
|
| 72 | - if ($row) { |
|
| 73 | - if (isset($options['force_nouveau']) and $options['force_nouveau'] == true) { |
|
| 74 | - $desc['id_auteur'] = $row['id_auteur']; |
|
| 75 | - $desc = inscription_nouveau($desc); |
|
| 76 | - } else { |
|
| 77 | - $desc = $row; |
|
| 78 | - } |
|
| 79 | - } else // s'il n'existe pas deja, creer les identifiants |
|
| 80 | - { |
|
| 81 | - $desc = inscription_nouveau($desc); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - // erreur ? |
|
| 85 | - if (!is_array($desc)) { |
|
| 86 | - return $desc; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - |
|
| 90 | - // generer le mot de passe (ou le refaire si compte inutilise) |
|
| 91 | - $desc['pass'] = creer_pass_pour_auteur($desc['id_auteur']); |
|
| 92 | - |
|
| 93 | - // attribuer un jeton pour confirmation par clic sur un lien |
|
| 94 | - $desc['jeton'] = auteur_attribuer_jeton($desc['id_auteur']); |
|
| 95 | - |
|
| 96 | - // charger de suite cette fonction, pour ses utilitaires |
|
| 97 | - $envoyer_inscription = charger_fonction('envoyer_inscription', ''); |
|
| 98 | - [$sujet, $msg, $from, $head] = $envoyer_inscription($desc, $nom, $statut, $options); |
|
| 99 | - |
|
| 100 | - $notifications = charger_fonction('notifications', 'inc'); |
|
| 101 | - notifications_envoyer_mails($mail_complet, $msg, $sujet, $from, $head); |
|
| 102 | - |
|
| 103 | - // Notifications |
|
| 104 | - $notifications( |
|
| 105 | - 'inscription', |
|
| 106 | - $desc['id_auteur'], |
|
| 107 | - ['nom' => $desc['nom'], 'email' => $desc['email']] |
|
| 108 | - ); |
|
| 109 | - |
|
| 110 | - return $desc; |
|
| 47 | + if (!is_array($options)) { |
|
| 48 | + $options = ['id' => $options]; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + if (function_exists('test_inscription')) { |
|
| 52 | + $f = 'test_inscription'; |
|
| 53 | + } else { |
|
| 54 | + $f = 'test_inscription_dist'; |
|
| 55 | + } |
|
| 56 | + $desc = $f($statut, $mail_complet, $nom, $options); |
|
| 57 | + |
|
| 58 | + // erreur ? |
|
| 59 | + if (!is_array($desc)) { |
|
| 60 | + return _T($desc); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + include_spip('base/abstract_sql'); |
|
| 64 | + $res = sql_select('statut, id_auteur, login, email, nom', 'spip_auteurs', 'email=' . sql_quote($desc['email'])); |
|
| 65 | + // erreur ? |
|
| 66 | + if (!$res) { |
|
| 67 | + return _T('titre_probleme_technique'); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $row = sql_fetch($res); |
|
| 71 | + sql_free($res); |
|
| 72 | + if ($row) { |
|
| 73 | + if (isset($options['force_nouveau']) and $options['force_nouveau'] == true) { |
|
| 74 | + $desc['id_auteur'] = $row['id_auteur']; |
|
| 75 | + $desc = inscription_nouveau($desc); |
|
| 76 | + } else { |
|
| 77 | + $desc = $row; |
|
| 78 | + } |
|
| 79 | + } else // s'il n'existe pas deja, creer les identifiants |
|
| 80 | + { |
|
| 81 | + $desc = inscription_nouveau($desc); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + // erreur ? |
|
| 85 | + if (!is_array($desc)) { |
|
| 86 | + return $desc; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + |
|
| 90 | + // generer le mot de passe (ou le refaire si compte inutilise) |
|
| 91 | + $desc['pass'] = creer_pass_pour_auteur($desc['id_auteur']); |
|
| 92 | + |
|
| 93 | + // attribuer un jeton pour confirmation par clic sur un lien |
|
| 94 | + $desc['jeton'] = auteur_attribuer_jeton($desc['id_auteur']); |
|
| 95 | + |
|
| 96 | + // charger de suite cette fonction, pour ses utilitaires |
|
| 97 | + $envoyer_inscription = charger_fonction('envoyer_inscription', ''); |
|
| 98 | + [$sujet, $msg, $from, $head] = $envoyer_inscription($desc, $nom, $statut, $options); |
|
| 99 | + |
|
| 100 | + $notifications = charger_fonction('notifications', 'inc'); |
|
| 101 | + notifications_envoyer_mails($mail_complet, $msg, $sujet, $from, $head); |
|
| 102 | + |
|
| 103 | + // Notifications |
|
| 104 | + $notifications( |
|
| 105 | + 'inscription', |
|
| 106 | + $desc['id_auteur'], |
|
| 107 | + ['nom' => $desc['nom'], 'email' => $desc['email']] |
|
| 108 | + ); |
|
| 109 | + |
|
| 110 | + return $desc; |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | |
@@ -130,23 +130,23 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | */ |
| 132 | 132 | function test_inscription_dist($statut, $mail, $nom, $options) { |
| 133 | - include_spip('inc/filtres'); |
|
| 134 | - if (!$r = email_valide($mail)) { |
|
| 135 | - return 'info_email_invalide'; |
|
| 136 | - } |
|
| 137 | - $nom = trim(corriger_caracteres($nom)); |
|
| 138 | - $res = ['email' => $r, 'nom' => $nom, 'prefs' => $statut]; |
|
| 139 | - if (isset($options['login'])) { |
|
| 140 | - $login = trim(corriger_caracteres($options['login'])); |
|
| 141 | - if ((strlen($login) >= _LOGIN_TROP_COURT) and (strlen($nom) <= 64)) { |
|
| 142 | - $res['login'] = $login; |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - if (!isset($res['login']) and ((strlen($nom) < _LOGIN_TROP_COURT) or (strlen($nom) > 64))) { |
|
| 146 | - return 'ecrire:info_login_trop_court'; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - return $res; |
|
| 133 | + include_spip('inc/filtres'); |
|
| 134 | + if (!$r = email_valide($mail)) { |
|
| 135 | + return 'info_email_invalide'; |
|
| 136 | + } |
|
| 137 | + $nom = trim(corriger_caracteres($nom)); |
|
| 138 | + $res = ['email' => $r, 'nom' => $nom, 'prefs' => $statut]; |
|
| 139 | + if (isset($options['login'])) { |
|
| 140 | + $login = trim(corriger_caracteres($options['login'])); |
|
| 141 | + if ((strlen($login) >= _LOGIN_TROP_COURT) and (strlen($nom) <= 64)) { |
|
| 142 | + $res['login'] = $login; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + if (!isset($res['login']) and ((strlen($nom) < _LOGIN_TROP_COURT) or (strlen($nom) > 64))) { |
|
| 146 | + return 'ecrire:info_login_trop_court'; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + return $res; |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | |
@@ -159,33 +159,33 @@ discard block |
||
| 159 | 159 | * @return mixed|string |
| 160 | 160 | */ |
| 161 | 161 | function inscription_nouveau($desc) { |
| 162 | - if (!isset($desc['login']) or !strlen($desc['login'])) { |
|
| 163 | - $desc['login'] = test_login($desc['nom'], $desc['email']); |
|
| 164 | - } |
|
| 162 | + if (!isset($desc['login']) or !strlen($desc['login'])) { |
|
| 163 | + $desc['login'] = test_login($desc['nom'], $desc['email']); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - $desc['statut'] = 'nouveau'; |
|
| 167 | - include_spip('action/editer_auteur'); |
|
| 168 | - if (isset($desc['id_auteur'])) { |
|
| 169 | - $id_auteur = $desc['id_auteur']; |
|
| 170 | - } else { |
|
| 171 | - $id_auteur = auteur_inserer(); |
|
| 172 | - } |
|
| 166 | + $desc['statut'] = 'nouveau'; |
|
| 167 | + include_spip('action/editer_auteur'); |
|
| 168 | + if (isset($desc['id_auteur'])) { |
|
| 169 | + $id_auteur = $desc['id_auteur']; |
|
| 170 | + } else { |
|
| 171 | + $id_auteur = auteur_inserer(); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - if (!$id_auteur) { |
|
| 175 | - return _T('titre_probleme_technique'); |
|
| 176 | - } |
|
| 174 | + if (!$id_auteur) { |
|
| 175 | + return _T('titre_probleme_technique'); |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - $desc['lang'] = $GLOBALS['spip_lang']; |
|
| 178 | + $desc['lang'] = $GLOBALS['spip_lang']; |
|
| 179 | 179 | |
| 180 | - include_spip('inc/autoriser'); |
|
| 181 | - // lever l'autorisation pour pouvoir modifier le statut |
|
| 182 | - autoriser_exception('modifier', 'auteur', $id_auteur); |
|
| 183 | - auteur_modifier($id_auteur, $desc); |
|
| 184 | - autoriser_exception('modifier', 'auteur', $id_auteur, false); |
|
| 180 | + include_spip('inc/autoriser'); |
|
| 181 | + // lever l'autorisation pour pouvoir modifier le statut |
|
| 182 | + autoriser_exception('modifier', 'auteur', $id_auteur); |
|
| 183 | + auteur_modifier($id_auteur, $desc); |
|
| 184 | + autoriser_exception('modifier', 'auteur', $id_auteur, false); |
|
| 185 | 185 | |
| 186 | - $desc['id_auteur'] = $id_auteur; |
|
| 186 | + $desc['id_auteur'] = $id_auteur; |
|
| 187 | 187 | |
| 188 | - return $desc; |
|
| 188 | + return $desc; |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | |
@@ -202,27 +202,27 @@ discard block |
||
| 202 | 202 | * @return string |
| 203 | 203 | */ |
| 204 | 204 | function test_login($nom, $mail) { |
| 205 | - include_spip('inc/charsets'); |
|
| 206 | - $nom = strtolower(translitteration($nom)); |
|
| 207 | - $login_base = preg_replace('/[^\w\d_]/', '_', $nom); |
|
| 208 | - |
|
| 209 | - // il faut eviter que le login soit vraiment trop court |
|
| 210 | - if (strlen($login_base) < 3) { |
|
| 211 | - $mail = strtolower(translitteration(preg_replace('/@.*/', '', $mail))); |
|
| 212 | - $login_base = preg_replace('/[^\w\d]/', '_', $mail); |
|
| 213 | - } |
|
| 214 | - if (strlen($login_base) < 3) { |
|
| 215 | - $login_base = 'user'; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $login = $login_base; |
|
| 219 | - |
|
| 220 | - for ($i = 1;; $i++) { |
|
| 221 | - if (!sql_countsel('spip_auteurs', "login='$login'")) { |
|
| 222 | - return $login; |
|
| 223 | - } |
|
| 224 | - $login = $login_base . $i; |
|
| 225 | - } |
|
| 205 | + include_spip('inc/charsets'); |
|
| 206 | + $nom = strtolower(translitteration($nom)); |
|
| 207 | + $login_base = preg_replace('/[^\w\d_]/', '_', $nom); |
|
| 208 | + |
|
| 209 | + // il faut eviter que le login soit vraiment trop court |
|
| 210 | + if (strlen($login_base) < 3) { |
|
| 211 | + $mail = strtolower(translitteration(preg_replace('/@.*/', '', $mail))); |
|
| 212 | + $login_base = preg_replace('/[^\w\d]/', '_', $mail); |
|
| 213 | + } |
|
| 214 | + if (strlen($login_base) < 3) { |
|
| 215 | + $login_base = 'user'; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $login = $login_base; |
|
| 219 | + |
|
| 220 | + for ($i = 1;; $i++) { |
|
| 221 | + if (!sql_countsel('spip_auteurs', "login='$login'")) { |
|
| 222 | + return $login; |
|
| 223 | + } |
|
| 224 | + $login = $login_base . $i; |
|
| 225 | + } |
|
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | |
@@ -240,26 +240,26 @@ discard block |
||
| 240 | 240 | */ |
| 241 | 241 | function envoyer_inscription_dist($desc, $nom, $mode, $options = []) { |
| 242 | 242 | |
| 243 | - $contexte = array_merge($desc, $options); |
|
| 244 | - $contexte['nom'] = $nom; |
|
| 245 | - $contexte['mode'] = $mode; |
|
| 246 | - $contexte['url_confirm'] = generer_url_action('confirmer_inscription', '', true, true); |
|
| 247 | - $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'email', $desc['email']); |
|
| 248 | - $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'jeton', $desc['jeton']); |
|
| 249 | - // S'il y a l'option redirect, on l'ajoute directement ici |
|
| 250 | - if (isset($options['redirect'])) { |
|
| 251 | - $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'redirect', $options['redirect']); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - $modele_mail = 'modeles/mail_inscription'; |
|
| 255 | - if (isset($options['modele_mail']) and $options['modele_mail']) { |
|
| 256 | - $modele_mail = $options['modele_mail']; |
|
| 257 | - } |
|
| 258 | - $message = recuperer_fond($modele_mail, $contexte); |
|
| 259 | - $from = ($options['from'] ?? ''); |
|
| 260 | - $head = ''; |
|
| 261 | - |
|
| 262 | - return ['', $message, $from, $head]; |
|
| 243 | + $contexte = array_merge($desc, $options); |
|
| 244 | + $contexte['nom'] = $nom; |
|
| 245 | + $contexte['mode'] = $mode; |
|
| 246 | + $contexte['url_confirm'] = generer_url_action('confirmer_inscription', '', true, true); |
|
| 247 | + $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'email', $desc['email']); |
|
| 248 | + $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'jeton', $desc['jeton']); |
|
| 249 | + // S'il y a l'option redirect, on l'ajoute directement ici |
|
| 250 | + if (isset($options['redirect'])) { |
|
| 251 | + $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'redirect', $options['redirect']); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + $modele_mail = 'modeles/mail_inscription'; |
|
| 255 | + if (isset($options['modele_mail']) and $options['modele_mail']) { |
|
| 256 | + $modele_mail = $options['modele_mail']; |
|
| 257 | + } |
|
| 258 | + $message = recuperer_fond($modele_mail, $contexte); |
|
| 259 | + $from = ($options['from'] ?? ''); |
|
| 260 | + $head = ''; |
|
| 261 | + |
|
| 262 | + return ['', $message, $from, $head]; |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | |
@@ -270,12 +270,12 @@ discard block |
||
| 270 | 270 | * @return string |
| 271 | 271 | */ |
| 272 | 272 | function creer_pass_pour_auteur($id_auteur) { |
| 273 | - include_spip('inc/acces'); |
|
| 274 | - $pass = creer_pass_aleatoire(max(_PASS_LONGUEUR_MINI, 16), $id_auteur); |
|
| 275 | - include_spip('action/editer_auteur'); |
|
| 276 | - auteur_instituer($id_auteur, ['pass' => $pass]); |
|
| 273 | + include_spip('inc/acces'); |
|
| 274 | + $pass = creer_pass_aleatoire(max(_PASS_LONGUEUR_MINI, 16), $id_auteur); |
|
| 275 | + include_spip('action/editer_auteur'); |
|
| 276 | + auteur_instituer($id_auteur, ['pass' => $pass]); |
|
| 277 | 277 | |
| 278 | - return $pass; |
|
| 278 | + return $pass; |
|
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | /** |
@@ -288,17 +288,17 @@ discard block |
||
| 288 | 288 | * @return string |
| 289 | 289 | */ |
| 290 | 290 | function tester_statut_inscription($statut_tmp, $id) { |
| 291 | - include_spip('inc/autoriser'); |
|
| 292 | - if ($statut_tmp) { |
|
| 293 | - return autoriser('inscrireauteur', $statut_tmp, $id) ? $statut_tmp : ''; |
|
| 294 | - } elseif ( |
|
| 295 | - autoriser('inscrireauteur', $statut_tmp = '1comite', $id) |
|
| 296 | - or autoriser('inscrireauteur', $statut_tmp = '6forum', $id) |
|
| 297 | - ) { |
|
| 298 | - return $statut_tmp; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - return ''; |
|
| 291 | + include_spip('inc/autoriser'); |
|
| 292 | + if ($statut_tmp) { |
|
| 293 | + return autoriser('inscrireauteur', $statut_tmp, $id) ? $statut_tmp : ''; |
|
| 294 | + } elseif ( |
|
| 295 | + autoriser('inscrireauteur', $statut_tmp = '1comite', $id) |
|
| 296 | + or autoriser('inscrireauteur', $statut_tmp = '6forum', $id) |
|
| 297 | + ) { |
|
| 298 | + return $statut_tmp; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + return ''; |
|
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | |
@@ -312,35 +312,35 @@ discard block |
||
| 312 | 312 | * @return array |
| 313 | 313 | */ |
| 314 | 314 | function confirmer_statut_inscription($auteur) { |
| 315 | - // securite |
|
| 316 | - if ($auteur['statut'] != 'nouveau') { |
|
| 317 | - return $auteur; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - $s = $auteur['prefs']; |
|
| 321 | - // securite, au cas ou prefs aurait ete corrompu (ou deja ecrase par un tableau serialize) |
|
| 322 | - if (!preg_match(',^\w+$,', $s)) { |
|
| 323 | - $s = '6forum'; |
|
| 324 | - } |
|
| 325 | - include_spip('inc/autoriser'); |
|
| 326 | - if (!autoriser('inscrireauteur', $s)) { |
|
| 327 | - return $auteur; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - include_spip('inc/autoriser'); |
|
| 331 | - // accorder l'autorisation de modif du statut auteur |
|
| 332 | - autoriser_exception('modifier', 'auteur', $auteur['id_auteur']); |
|
| 333 | - include_spip('action/editer_auteur'); |
|
| 334 | - // changer le statut |
|
| 335 | - auteur_modifier($auteur['id_auteur'], ['statut' => $s]); |
|
| 336 | - unset($_COOKIE['spip_session']); // forcer la maj de la session |
|
| 337 | - // lever l'autorisation de modif du statut auteur |
|
| 338 | - autoriser_exception('modifier', 'auteur', $auteur['id_auteur'], false); |
|
| 339 | - |
|
| 340 | - // mettre a jour le statut |
|
| 341 | - $auteur['statut'] = $s; |
|
| 342 | - |
|
| 343 | - return $auteur; |
|
| 315 | + // securite |
|
| 316 | + if ($auteur['statut'] != 'nouveau') { |
|
| 317 | + return $auteur; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + $s = $auteur['prefs']; |
|
| 321 | + // securite, au cas ou prefs aurait ete corrompu (ou deja ecrase par un tableau serialize) |
|
| 322 | + if (!preg_match(',^\w+$,', $s)) { |
|
| 323 | + $s = '6forum'; |
|
| 324 | + } |
|
| 325 | + include_spip('inc/autoriser'); |
|
| 326 | + if (!autoriser('inscrireauteur', $s)) { |
|
| 327 | + return $auteur; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + include_spip('inc/autoriser'); |
|
| 331 | + // accorder l'autorisation de modif du statut auteur |
|
| 332 | + autoriser_exception('modifier', 'auteur', $auteur['id_auteur']); |
|
| 333 | + include_spip('action/editer_auteur'); |
|
| 334 | + // changer le statut |
|
| 335 | + auteur_modifier($auteur['id_auteur'], ['statut' => $s]); |
|
| 336 | + unset($_COOKIE['spip_session']); // forcer la maj de la session |
|
| 337 | + // lever l'autorisation de modif du statut auteur |
|
| 338 | + autoriser_exception('modifier', 'auteur', $auteur['id_auteur'], false); |
|
| 339 | + |
|
| 340 | + // mettre a jour le statut |
|
| 341 | + $auteur['statut'] = $s; |
|
| 342 | + |
|
| 343 | + return $auteur; |
|
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | |
@@ -354,20 +354,20 @@ discard block |
||
| 354 | 354 | * @return string |
| 355 | 355 | */ |
| 356 | 356 | function auteur_attribuer_jeton($id_auteur): string { |
| 357 | - include_spip('base/abstract_sql'); |
|
| 358 | - include_spip('inc/acces'); |
|
| 359 | - |
|
| 360 | - // s'assurer de l'unicite du jeton pour le couple (email,cookie) |
|
| 361 | - do { |
|
| 362 | - // Un morceau du jeton est lisible en bdd pour éviter de devoir déchiffrer |
|
| 363 | - // tous les jetons connus pour vérifier le jeton d’un auteur. |
|
| 364 | - $public = substr(creer_uniqid(), 0, 7) . '.'; |
|
| 365 | - $jeton = $public . creer_uniqid(); |
|
| 366 | - $jeton_chiffre_prefixe = $public . Chiffrement::chiffrer($jeton, SpipCles::secret_du_site()); |
|
| 367 | - sql_updateq('spip_auteurs', ['cookie_oubli' => $jeton_chiffre_prefixe], 'id_auteur=' . intval($id_auteur)); |
|
| 368 | - } while (sql_countsel('spip_auteurs', 'cookie_oubli=' . sql_quote($jeton_chiffre_prefixe, '', 'string')) > 1); |
|
| 369 | - |
|
| 370 | - return $jeton; |
|
| 357 | + include_spip('base/abstract_sql'); |
|
| 358 | + include_spip('inc/acces'); |
|
| 359 | + |
|
| 360 | + // s'assurer de l'unicite du jeton pour le couple (email,cookie) |
|
| 361 | + do { |
|
| 362 | + // Un morceau du jeton est lisible en bdd pour éviter de devoir déchiffrer |
|
| 363 | + // tous les jetons connus pour vérifier le jeton d’un auteur. |
|
| 364 | + $public = substr(creer_uniqid(), 0, 7) . '.'; |
|
| 365 | + $jeton = $public . creer_uniqid(); |
|
| 366 | + $jeton_chiffre_prefixe = $public . Chiffrement::chiffrer($jeton, SpipCles::secret_du_site()); |
|
| 367 | + sql_updateq('spip_auteurs', ['cookie_oubli' => $jeton_chiffre_prefixe], 'id_auteur=' . intval($id_auteur)); |
|
| 368 | + } while (sql_countsel('spip_auteurs', 'cookie_oubli=' . sql_quote($jeton_chiffre_prefixe, '', 'string')) > 1); |
|
| 369 | + |
|
| 370 | + return $jeton; |
|
| 371 | 371 | } |
| 372 | 372 | |
| 373 | 373 | /** |
@@ -381,19 +381,19 @@ discard block |
||
| 381 | 381 | * @return string|null |
| 382 | 382 | */ |
| 383 | 383 | function auteur_lire_jeton(int $id_auteur, bool $autoInit = false): ?string { |
| 384 | - include_spip('base/abstract_sql'); |
|
| 385 | - $jeton_chiffre_prefixe = sql_getfetsel('cookie_oubli', 'spip_auteurs', 'id_auteur=' . $id_auteur); |
|
| 386 | - if ($jeton_chiffre_prefixe) { |
|
| 387 | - $jeton_chiffre = substr($jeton_chiffre_prefixe, 8); |
|
| 388 | - $jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
|
| 389 | - if ($jeton) { |
|
| 390 | - return $jeton; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - if ($autoInit) { |
|
| 394 | - return auteur_attribuer_jeton($id_auteur); |
|
| 395 | - } |
|
| 396 | - return null; |
|
| 384 | + include_spip('base/abstract_sql'); |
|
| 385 | + $jeton_chiffre_prefixe = sql_getfetsel('cookie_oubli', 'spip_auteurs', 'id_auteur=' . $id_auteur); |
|
| 386 | + if ($jeton_chiffre_prefixe) { |
|
| 387 | + $jeton_chiffre = substr($jeton_chiffre_prefixe, 8); |
|
| 388 | + $jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
|
| 389 | + if ($jeton) { |
|
| 390 | + return $jeton; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + if ($autoInit) { |
|
| 394 | + return auteur_attribuer_jeton($id_auteur); |
|
| 395 | + } |
|
| 396 | + return null; |
|
| 397 | 397 | } |
| 398 | 398 | |
| 399 | 399 | /** |
@@ -403,24 +403,24 @@ discard block |
||
| 403 | 403 | * @return array|bool |
| 404 | 404 | */ |
| 405 | 405 | function auteur_verifier_jeton($jeton) { |
| 406 | - // refuser un jeton corrompu |
|
| 407 | - if (preg_match(',[^0-9a-f.],i', $jeton)) { |
|
| 408 | - return false; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - include_spip('base/abstract_sql'); |
|
| 412 | - $public = substr($jeton, 0, 8); |
|
| 413 | - |
|
| 414 | - // Les auteurs qui ont un jetons ressemblant |
|
| 415 | - $auteurs = sql_allfetsel('*', 'spip_auteurs', 'cookie_oubli LIKE ' . sql_quote($public . '%')); |
|
| 416 | - foreach ($auteurs as $auteur) { |
|
| 417 | - $jeton_chiffre = substr($auteur['cookie_oubli'], 8); |
|
| 418 | - $_jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
|
| 419 | - if ($_jeton and hash_equals($jeton, $_jeton)) { |
|
| 420 | - return $auteur; |
|
| 421 | - } |
|
| 422 | - } |
|
| 423 | - return false; |
|
| 406 | + // refuser un jeton corrompu |
|
| 407 | + if (preg_match(',[^0-9a-f.],i', $jeton)) { |
|
| 408 | + return false; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + include_spip('base/abstract_sql'); |
|
| 412 | + $public = substr($jeton, 0, 8); |
|
| 413 | + |
|
| 414 | + // Les auteurs qui ont un jetons ressemblant |
|
| 415 | + $auteurs = sql_allfetsel('*', 'spip_auteurs', 'cookie_oubli LIKE ' . sql_quote($public . '%')); |
|
| 416 | + foreach ($auteurs as $auteur) { |
|
| 417 | + $jeton_chiffre = substr($auteur['cookie_oubli'], 8); |
|
| 418 | + $_jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
|
| 419 | + if ($_jeton and hash_equals($jeton, $_jeton)) { |
|
| 420 | + return $auteur; |
|
| 421 | + } |
|
| 422 | + } |
|
| 423 | + return false; |
|
| 424 | 424 | } |
| 425 | 425 | |
| 426 | 426 | /** |
@@ -430,6 +430,6 @@ discard block |
||
| 430 | 430 | * @return bool |
| 431 | 431 | */ |
| 432 | 432 | function auteur_effacer_jeton($id_auteur) { |
| 433 | - include_spip('base/abstract_sql'); |
|
| 434 | - return sql_updateq('spip_auteurs', ['cookie_oubli' => ''], 'id_auteur=' . intval($id_auteur)); |
|
| 433 | + include_spip('base/abstract_sql'); |
|
| 434 | + return sql_updateq('spip_auteurs', ['cookie_oubli' => ''], 'id_auteur=' . intval($id_auteur)); |
|
| 435 | 435 | } |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | include_spip('base/abstract_sql'); |
| 64 | - $res = sql_select('statut, id_auteur, login, email, nom', 'spip_auteurs', 'email=' . sql_quote($desc['email'])); |
|
| 64 | + $res = sql_select('statut, id_auteur, login, email, nom', 'spip_auteurs', 'email='.sql_quote($desc['email'])); |
|
| 65 | 65 | // erreur ? |
| 66 | 66 | if (!$res) { |
| 67 | 67 | return _T('titre_probleme_technique'); |
@@ -217,11 +217,11 @@ discard block |
||
| 217 | 217 | |
| 218 | 218 | $login = $login_base; |
| 219 | 219 | |
| 220 | - for ($i = 1;; $i++) { |
|
| 220 | + for ($i = 1; ; $i++) { |
|
| 221 | 221 | if (!sql_countsel('spip_auteurs', "login='$login'")) { |
| 222 | 222 | return $login; |
| 223 | 223 | } |
| 224 | - $login = $login_base . $i; |
|
| 224 | + $login = $login_base.$i; |
|
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | |
@@ -361,11 +361,11 @@ discard block |
||
| 361 | 361 | do { |
| 362 | 362 | // Un morceau du jeton est lisible en bdd pour éviter de devoir déchiffrer |
| 363 | 363 | // tous les jetons connus pour vérifier le jeton d’un auteur. |
| 364 | - $public = substr(creer_uniqid(), 0, 7) . '.'; |
|
| 365 | - $jeton = $public . creer_uniqid(); |
|
| 366 | - $jeton_chiffre_prefixe = $public . Chiffrement::chiffrer($jeton, SpipCles::secret_du_site()); |
|
| 367 | - sql_updateq('spip_auteurs', ['cookie_oubli' => $jeton_chiffre_prefixe], 'id_auteur=' . intval($id_auteur)); |
|
| 368 | - } while (sql_countsel('spip_auteurs', 'cookie_oubli=' . sql_quote($jeton_chiffre_prefixe, '', 'string')) > 1); |
|
| 364 | + $public = substr(creer_uniqid(), 0, 7).'.'; |
|
| 365 | + $jeton = $public.creer_uniqid(); |
|
| 366 | + $jeton_chiffre_prefixe = $public.Chiffrement::chiffrer($jeton, SpipCles::secret_du_site()); |
|
| 367 | + sql_updateq('spip_auteurs', ['cookie_oubli' => $jeton_chiffre_prefixe], 'id_auteur='.intval($id_auteur)); |
|
| 368 | + } while (sql_countsel('spip_auteurs', 'cookie_oubli='.sql_quote($jeton_chiffre_prefixe, '', 'string')) > 1); |
|
| 369 | 369 | |
| 370 | 370 | return $jeton; |
| 371 | 371 | } |
@@ -382,7 +382,7 @@ discard block |
||
| 382 | 382 | */ |
| 383 | 383 | function auteur_lire_jeton(int $id_auteur, bool $autoInit = false): ?string { |
| 384 | 384 | include_spip('base/abstract_sql'); |
| 385 | - $jeton_chiffre_prefixe = sql_getfetsel('cookie_oubli', 'spip_auteurs', 'id_auteur=' . $id_auteur); |
|
| 385 | + $jeton_chiffre_prefixe = sql_getfetsel('cookie_oubli', 'spip_auteurs', 'id_auteur='.$id_auteur); |
|
| 386 | 386 | if ($jeton_chiffre_prefixe) { |
| 387 | 387 | $jeton_chiffre = substr($jeton_chiffre_prefixe, 8); |
| 388 | 388 | $jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
@@ -412,7 +412,7 @@ discard block |
||
| 412 | 412 | $public = substr($jeton, 0, 8); |
| 413 | 413 | |
| 414 | 414 | // Les auteurs qui ont un jetons ressemblant |
| 415 | - $auteurs = sql_allfetsel('*', 'spip_auteurs', 'cookie_oubli LIKE ' . sql_quote($public . '%')); |
|
| 415 | + $auteurs = sql_allfetsel('*', 'spip_auteurs', 'cookie_oubli LIKE '.sql_quote($public.'%')); |
|
| 416 | 416 | foreach ($auteurs as $auteur) { |
| 417 | 417 | $jeton_chiffre = substr($auteur['cookie_oubli'], 8); |
| 418 | 418 | $_jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
@@ -431,5 +431,5 @@ discard block |
||
| 431 | 431 | */ |
| 432 | 432 | function auteur_effacer_jeton($id_auteur) { |
| 433 | 433 | include_spip('base/abstract_sql'); |
| 434 | - return sql_updateq('spip_auteurs', ['cookie_oubli' => ''], 'id_auteur=' . intval($id_auteur)); |
|
| 434 | + return sql_updateq('spip_auteurs', ['cookie_oubli' => ''], 'id_auteur='.intval($id_auteur)); |
|
| 435 | 435 | } |
@@ -13,122 +13,122 @@ |
||
| 13 | 13 | |
| 14 | 14 | class SqliteRequeteur |
| 15 | 15 | { |
| 16 | - /** @var string texte de la requête */ |
|
| 17 | - public $query = ''; // la requete |
|
| 18 | - /** @var string Nom de la connexion */ |
|
| 19 | - public $serveur = ''; |
|
| 20 | - /** @var PDO Identifiant de la connexion SQLite */ |
|
| 21 | - public $link = ''; |
|
| 22 | - /** @var string Prefixe des tables SPIP */ |
|
| 23 | - public $prefixe = ''; |
|
| 24 | - /** @var string Nom de la base de donnée */ |
|
| 25 | - public $db = ''; |
|
| 26 | - /** @var bool Doit-on tracer les requetes (var_profile) ? */ |
|
| 27 | - public $tracer = false; // doit-on tracer les requetes (var_profile) |
|
| 28 | - |
|
| 29 | - /** @var string Version de SQLite (2 ou 3) */ |
|
| 30 | - public $sqlite_version = ''; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Constructeur |
|
| 34 | - * |
|
| 35 | - * @param string $serveur |
|
| 36 | - * @return bool |
|
| 37 | - */ |
|
| 38 | - public function __construct($serveur = '') |
|
| 39 | - { |
|
| 40 | - _sqlite_init(); |
|
| 41 | - $this->serveur = strtolower($serveur); |
|
| 42 | - |
|
| 43 | - if (!($this->link = _sqlite_link($this->serveur)) && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL)) { |
|
| 44 | - spip_log('Aucune connexion sqlite (link)', 'sqlite.' . _LOG_ERREUR); |
|
| 45 | - |
|
| 46 | - return false; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - $this->sqlite_version = _sqlite_is_version('', $this->link); |
|
| 50 | - |
|
| 51 | - $this->prefixe = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['prefixe']; |
|
| 52 | - $this->db = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['db']; |
|
| 53 | - |
|
| 54 | - // tracage des requetes ? |
|
| 55 | - $this->tracer = (isset($_GET['var_profile']) && $_GET['var_profile']); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Lancer la requête transmise et faire le tracage si demandé |
|
| 60 | - * |
|
| 61 | - * @param string $query |
|
| 62 | - * Requête à exécuter |
|
| 63 | - * @param bool|null $tracer |
|
| 64 | - * true pour tracer la requête |
|
| 65 | - * @return bool|PDOStatement|array |
|
| 66 | - */ |
|
| 67 | - public function executer_requete($query, $tracer = null) |
|
| 68 | - { |
|
| 69 | - if (is_null($tracer)) { |
|
| 70 | - $tracer = $this->tracer; |
|
| 71 | - } |
|
| 72 | - $err = ''; |
|
| 73 | - $t = 0; |
|
| 74 | - if ($tracer or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 75 | - include_spip('public/tracer'); |
|
| 76 | - $t = trace_query_start(); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - # spip_log("requete: $this->serveur >> $query",'sqlite.'._LOG_DEBUG); // boum ? pourquoi ? |
|
| 80 | - if ($this->link) { |
|
| 81 | - // memoriser la derniere erreur PHP vue |
|
| 82 | - $last_error = (function_exists('error_get_last') ? error_get_last() : ''); |
|
| 83 | - $e = null; |
|
| 84 | - // sauver la derniere requete |
|
| 85 | - $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['last'] = $query; |
|
| 86 | - $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['total_requetes']++; |
|
| 87 | - |
|
| 88 | - try { |
|
| 89 | - $r = $this->link->query($query); |
|
| 90 | - } catch (\PDOException $e) { |
|
| 91 | - spip_log('PDOException: ' . $e->getMessage(), 'sqlite.' . _LOG_DEBUG); |
|
| 92 | - $r = false; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - // comptage : oblige de compter le nombre d'entrees retournees |
|
| 96 | - // par une requete SELECT |
|
| 97 | - // aucune autre solution ne donne le nombre attendu :( ! |
|
| 98 | - // particulierement s'il y a des LIMIT dans la requete. |
|
| 99 | - if ($r and strtoupper(substr(ltrim($query), 0, 6)) === 'SELECT') { |
|
| 100 | - // noter le link et la query pour faire le comptage *si* on en a besoin |
|
| 101 | - $r->spipSqliteRowCount = [$this->link, $query]; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - // loger les warnings/erreurs eventuels de sqlite remontant dans PHP |
|
| 105 | - if ($e and $e instanceof \PDOException) { |
|
| 106 | - $err = strip_tags($e->getMessage()) . ' in ' . $e->getFile() . ' line ' . $e->getLine(); |
|
| 107 | - spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 108 | - } elseif ($err = (function_exists('error_get_last') ? error_get_last() : '') and $err != $last_error) { |
|
| 109 | - $err = strip_tags($err['message']) . ' in ' . $err['file'] . ' line ' . $err['line']; |
|
| 110 | - spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 111 | - } else { |
|
| 112 | - $err = ''; |
|
| 113 | - } |
|
| 114 | - } else { |
|
| 115 | - $r = false; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - if (spip_sqlite_errno($this->serveur)) { |
|
| 119 | - $err .= spip_sqlite_error($query, $this->serveur); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $t ? trace_query_end($query, $t, $r, $err, $this->serveur) : $r; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 127 | - * |
|
| 128 | - * @return string|false |
|
| 129 | - **/ |
|
| 130 | - public function last_insert_id() |
|
| 131 | - { |
|
| 132 | - return $this->link->lastInsertId(); |
|
| 133 | - } |
|
| 16 | + /** @var string texte de la requête */ |
|
| 17 | + public $query = ''; // la requete |
|
| 18 | + /** @var string Nom de la connexion */ |
|
| 19 | + public $serveur = ''; |
|
| 20 | + /** @var PDO Identifiant de la connexion SQLite */ |
|
| 21 | + public $link = ''; |
|
| 22 | + /** @var string Prefixe des tables SPIP */ |
|
| 23 | + public $prefixe = ''; |
|
| 24 | + /** @var string Nom de la base de donnée */ |
|
| 25 | + public $db = ''; |
|
| 26 | + /** @var bool Doit-on tracer les requetes (var_profile) ? */ |
|
| 27 | + public $tracer = false; // doit-on tracer les requetes (var_profile) |
|
| 28 | + |
|
| 29 | + /** @var string Version de SQLite (2 ou 3) */ |
|
| 30 | + public $sqlite_version = ''; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Constructeur |
|
| 34 | + * |
|
| 35 | + * @param string $serveur |
|
| 36 | + * @return bool |
|
| 37 | + */ |
|
| 38 | + public function __construct($serveur = '') |
|
| 39 | + { |
|
| 40 | + _sqlite_init(); |
|
| 41 | + $this->serveur = strtolower($serveur); |
|
| 42 | + |
|
| 43 | + if (!($this->link = _sqlite_link($this->serveur)) && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL)) { |
|
| 44 | + spip_log('Aucune connexion sqlite (link)', 'sqlite.' . _LOG_ERREUR); |
|
| 45 | + |
|
| 46 | + return false; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + $this->sqlite_version = _sqlite_is_version('', $this->link); |
|
| 50 | + |
|
| 51 | + $this->prefixe = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['prefixe']; |
|
| 52 | + $this->db = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['db']; |
|
| 53 | + |
|
| 54 | + // tracage des requetes ? |
|
| 55 | + $this->tracer = (isset($_GET['var_profile']) && $_GET['var_profile']); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Lancer la requête transmise et faire le tracage si demandé |
|
| 60 | + * |
|
| 61 | + * @param string $query |
|
| 62 | + * Requête à exécuter |
|
| 63 | + * @param bool|null $tracer |
|
| 64 | + * true pour tracer la requête |
|
| 65 | + * @return bool|PDOStatement|array |
|
| 66 | + */ |
|
| 67 | + public function executer_requete($query, $tracer = null) |
|
| 68 | + { |
|
| 69 | + if (is_null($tracer)) { |
|
| 70 | + $tracer = $this->tracer; |
|
| 71 | + } |
|
| 72 | + $err = ''; |
|
| 73 | + $t = 0; |
|
| 74 | + if ($tracer or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 75 | + include_spip('public/tracer'); |
|
| 76 | + $t = trace_query_start(); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + # spip_log("requete: $this->serveur >> $query",'sqlite.'._LOG_DEBUG); // boum ? pourquoi ? |
|
| 80 | + if ($this->link) { |
|
| 81 | + // memoriser la derniere erreur PHP vue |
|
| 82 | + $last_error = (function_exists('error_get_last') ? error_get_last() : ''); |
|
| 83 | + $e = null; |
|
| 84 | + // sauver la derniere requete |
|
| 85 | + $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['last'] = $query; |
|
| 86 | + $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['total_requetes']++; |
|
| 87 | + |
|
| 88 | + try { |
|
| 89 | + $r = $this->link->query($query); |
|
| 90 | + } catch (\PDOException $e) { |
|
| 91 | + spip_log('PDOException: ' . $e->getMessage(), 'sqlite.' . _LOG_DEBUG); |
|
| 92 | + $r = false; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + // comptage : oblige de compter le nombre d'entrees retournees |
|
| 96 | + // par une requete SELECT |
|
| 97 | + // aucune autre solution ne donne le nombre attendu :( ! |
|
| 98 | + // particulierement s'il y a des LIMIT dans la requete. |
|
| 99 | + if ($r and strtoupper(substr(ltrim($query), 0, 6)) === 'SELECT') { |
|
| 100 | + // noter le link et la query pour faire le comptage *si* on en a besoin |
|
| 101 | + $r->spipSqliteRowCount = [$this->link, $query]; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + // loger les warnings/erreurs eventuels de sqlite remontant dans PHP |
|
| 105 | + if ($e and $e instanceof \PDOException) { |
|
| 106 | + $err = strip_tags($e->getMessage()) . ' in ' . $e->getFile() . ' line ' . $e->getLine(); |
|
| 107 | + spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 108 | + } elseif ($err = (function_exists('error_get_last') ? error_get_last() : '') and $err != $last_error) { |
|
| 109 | + $err = strip_tags($err['message']) . ' in ' . $err['file'] . ' line ' . $err['line']; |
|
| 110 | + spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 111 | + } else { |
|
| 112 | + $err = ''; |
|
| 113 | + } |
|
| 114 | + } else { |
|
| 115 | + $r = false; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + if (spip_sqlite_errno($this->serveur)) { |
|
| 119 | + $err .= spip_sqlite_error($query, $this->serveur); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $t ? trace_query_end($query, $t, $r, $err, $this->serveur) : $r; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 127 | + * |
|
| 128 | + * @return string|false |
|
| 129 | + **/ |
|
| 130 | + public function last_insert_id() |
|
| 131 | + { |
|
| 132 | + return $this->link->lastInsertId(); |
|
| 133 | + } |
|
| 134 | 134 | } |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | $this->serveur = strtolower($serveur); |
| 42 | 42 | |
| 43 | 43 | if (!($this->link = _sqlite_link($this->serveur)) && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL)) { |
| 44 | - spip_log('Aucune connexion sqlite (link)', 'sqlite.' . _LOG_ERREUR); |
|
| 44 | + spip_log('Aucune connexion sqlite (link)', 'sqlite.'._LOG_ERREUR); |
|
| 45 | 45 | |
| 46 | 46 | return false; |
| 47 | 47 | } |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | try { |
| 89 | 89 | $r = $this->link->query($query); |
| 90 | 90 | } catch (\PDOException $e) { |
| 91 | - spip_log('PDOException: ' . $e->getMessage(), 'sqlite.' . _LOG_DEBUG); |
|
| 91 | + spip_log('PDOException: '.$e->getMessage(), 'sqlite.'._LOG_DEBUG); |
|
| 92 | 92 | $r = false; |
| 93 | 93 | } |
| 94 | 94 | |
@@ -103,11 +103,11 @@ discard block |
||
| 103 | 103 | |
| 104 | 104 | // loger les warnings/erreurs eventuels de sqlite remontant dans PHP |
| 105 | 105 | if ($e and $e instanceof \PDOException) { |
| 106 | - $err = strip_tags($e->getMessage()) . ' in ' . $e->getFile() . ' line ' . $e->getLine(); |
|
| 107 | - spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 106 | + $err = strip_tags($e->getMessage()).' in '.$e->getFile().' line '.$e->getLine(); |
|
| 107 | + spip_log("$err - ".$query, 'sqlite.'._LOG_ERREUR); |
|
| 108 | 108 | } elseif ($err = (function_exists('error_get_last') ? error_get_last() : '') and $err != $last_error) { |
| 109 | - $err = strip_tags($err['message']) . ' in ' . $err['file'] . ' line ' . $err['line']; |
|
| 110 | - spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 109 | + $err = strip_tags($err['message']).' in '.$err['file'].' line '.$err['line']; |
|
| 110 | + spip_log("$err - ".$query, 'sqlite.'._LOG_ERREUR); |
|
| 111 | 111 | } else { |
| 112 | 112 | $err = ''; |
| 113 | 113 | } |