@@ -1,22 +1,22 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | function trier_rss($texte) { |
| 8 | - if (preg_match_all(',<item.*</item>\s*?,Uims', $texte, $matches, PREG_SET_ORDER)) { |
|
| 9 | - $placeholder = '<!--REINSERT-->'; |
|
| 10 | - $items = []; |
|
| 11 | - foreach ($matches as $match) { |
|
| 12 | - if (preg_match(',<dc:date>(.*)</dc:date>,Uims', $match[0], $r)) { |
|
| 13 | - $items[strtotime($r[1])] = trim($match[0]); |
|
| 14 | - $texte = str_replace($match[0], unique($placeholder), $texte); |
|
| 15 | - } |
|
| 16 | - } |
|
| 17 | - krsort($items); |
|
| 18 | - $texte = str_replace($placeholder, implode("\n\t", $items) . "\n", $texte); |
|
| 19 | - } |
|
| 8 | + if (preg_match_all(',<item.*</item>\s*?,Uims', $texte, $matches, PREG_SET_ORDER)) { |
|
| 9 | + $placeholder = '<!--REINSERT-->'; |
|
| 10 | + $items = []; |
|
| 11 | + foreach ($matches as $match) { |
|
| 12 | + if (preg_match(',<dc:date>(.*)</dc:date>,Uims', $match[0], $r)) { |
|
| 13 | + $items[strtotime($r[1])] = trim($match[0]); |
|
| 14 | + $texte = str_replace($match[0], unique($placeholder), $texte); |
|
| 15 | + } |
|
| 16 | + } |
|
| 17 | + krsort($items); |
|
| 18 | + $texte = str_replace($placeholder, implode("\n\t", $items) . "\n", $texte); |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - return $texte; |
|
| 21 | + return $texte; |
|
| 22 | 22 | } |
@@ -17,212 +17,212 @@ discard block |
||
| 17 | 17 | **/ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | include_spip('inc/charsets'); |
| 23 | 23 | include_spip('inc/texte'); |
| 24 | 24 | include_spip('inc/plugin'); // pour plugin_est_installe |
| 25 | 25 | |
| 26 | 26 | function plugins_afficher_plugin_dist( |
| 27 | - $url_page, |
|
| 28 | - $plug_file, |
|
| 29 | - $checked, |
|
| 30 | - $actif, |
|
| 31 | - $expose = false, |
|
| 32 | - $class_li = 'item', |
|
| 33 | - $dir_plugins = _DIR_PLUGINS |
|
| 27 | + $url_page, |
|
| 28 | + $plug_file, |
|
| 29 | + $checked, |
|
| 30 | + $actif, |
|
| 31 | + $expose = false, |
|
| 32 | + $class_li = 'item', |
|
| 33 | + $dir_plugins = _DIR_PLUGINS |
|
| 34 | 34 | ) { |
| 35 | 35 | |
| 36 | - static $id_input = 0; |
|
| 37 | - static $versions = []; |
|
| 38 | - |
|
| 39 | - $force_reload = (_request('var_mode') == 'recalcul'); |
|
| 40 | - $get_infos = charger_fonction('get_infos', 'plugins'); |
|
| 41 | - $info = $get_infos($plug_file, $force_reload, $dir_plugins); |
|
| 42 | - $prefix = $info['prefix']; |
|
| 43 | - $cfg = ''; |
|
| 44 | - $checkable = ($dir_plugins !== _DIR_PLUGINS_DIST); |
|
| 45 | - $nom = plugin_nom($info, $dir_plugins, $plug_file); |
|
| 46 | - $erreur = ''; |
|
| 47 | - |
|
| 48 | - if (!plugin_version_compatible($info['compatibilite'], $GLOBALS['spip_version_branche'], 'spip')) { |
|
| 49 | - $info['slogan'] = _T('plugin_info_non_compatible_spip'); |
|
| 50 | - $erreur = http_img_pack( |
|
| 51 | - 'plugin-dis-32.png', |
|
| 52 | - _T('plugin_info_non_compatible_spip'), |
|
| 53 | - " class='picto_err'", |
|
| 54 | - _T('plugin_info_non_compatible_spip') |
|
| 55 | - ); |
|
| 56 | - $class_li .= ' disabled'; |
|
| 57 | - $checkable = false; |
|
| 58 | - } elseif (isset($info['erreur'])) { |
|
| 59 | - $class_li .= ' error'; |
|
| 60 | - $erreur = http_img_pack( |
|
| 61 | - 'plugin-err-32.png', |
|
| 62 | - _T('plugin_info_erreur_xml'), |
|
| 63 | - " class='picto_err'", |
|
| 64 | - _T('plugin_info_erreur_xml') |
|
| 65 | - ) |
|
| 66 | - . "<div class='erreur'>" . join('<br >', $info['erreur']) . '</div>'; |
|
| 67 | - $checkable = false; |
|
| 68 | - } elseif (isset($GLOBALS['erreurs_activation_raw'][$dir_plugins . $plug_file])) { |
|
| 69 | - $class_li .= ' error'; |
|
| 70 | - $erreur = http_img_pack( |
|
| 71 | - 'plugin-err-32.png', |
|
| 72 | - _T('plugin_impossible_activer', ['plugin' => $nom]), |
|
| 73 | - " class='picto_err'", |
|
| 74 | - _T('plugin_impossible_activer', ['plugin' => $nom]) |
|
| 75 | - ) |
|
| 76 | - . "<div class='erreur'>" . implode( |
|
| 77 | - '<br />', |
|
| 78 | - $GLOBALS['erreurs_activation_raw'][$dir_plugins . $plug_file] |
|
| 79 | - ) . '</div>'; |
|
| 80 | - } else { |
|
| 81 | - $cfg = $actif ? plugin_bouton_config($plug_file, $info, $dir_plugins) : ''; |
|
| 82 | - if (defined('_DEV_VERSION_SPIP_COMPAT') and !plugin_version_compatible($info['compatibilite'], $GLOBALS['spip_version_branche'])) { |
|
| 83 | - //$info['slogan'] = _T('plugin_info_non_compatible_spip'); |
|
| 84 | - $erreur = http_img_pack( |
|
| 85 | - 'plugin-dis-32.png', |
|
| 86 | - _T('plugin_info_non_compatible_spip'), |
|
| 87 | - " class='picto_err picto_compat_forcee'", |
|
| 88 | - _L('Version incompatible : compatibilité forcée') |
|
| 89 | - ); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - // numerotons les occurrences d'un meme prefix |
|
| 94 | - $versions[$prefix] = $id = isset($versions[$prefix]) ? intval($versions[$prefix]) + 1 : ''; |
|
| 95 | - |
|
| 96 | - $class_li .= ($actif ? ' actif' : '') . ($expose ? ' on' : ''); |
|
| 97 | - |
|
| 98 | - return "<li id='$prefix$id' class='$class_li'>" |
|
| 99 | - . ((!$checkable and !$checked) |
|
| 100 | - ? '' : plugin_checkbox(++$id_input, $dir_plugins . $plug_file, $checked)) |
|
| 101 | - . plugin_resume($info, $dir_plugins, $plug_file, $url_page) |
|
| 102 | - . $cfg |
|
| 103 | - . $erreur |
|
| 104 | - . (($dir_plugins !== _DIR_PLUGINS_DIST and plugin_est_installe($plug_file)) |
|
| 105 | - ? plugin_desintalle($plug_file, $nom, $dir_plugins) : '') |
|
| 106 | - . "<div class='details'>" // pour l'ajax de exec/info_plugin |
|
| 107 | - . (!$expose ? '' : affiche_bloc_plugin($plug_file, $info, $dir_plugins)) |
|
| 108 | - . '</div>' |
|
| 109 | - . '</li>'; |
|
| 36 | + static $id_input = 0; |
|
| 37 | + static $versions = []; |
|
| 38 | + |
|
| 39 | + $force_reload = (_request('var_mode') == 'recalcul'); |
|
| 40 | + $get_infos = charger_fonction('get_infos', 'plugins'); |
|
| 41 | + $info = $get_infos($plug_file, $force_reload, $dir_plugins); |
|
| 42 | + $prefix = $info['prefix']; |
|
| 43 | + $cfg = ''; |
|
| 44 | + $checkable = ($dir_plugins !== _DIR_PLUGINS_DIST); |
|
| 45 | + $nom = plugin_nom($info, $dir_plugins, $plug_file); |
|
| 46 | + $erreur = ''; |
|
| 47 | + |
|
| 48 | + if (!plugin_version_compatible($info['compatibilite'], $GLOBALS['spip_version_branche'], 'spip')) { |
|
| 49 | + $info['slogan'] = _T('plugin_info_non_compatible_spip'); |
|
| 50 | + $erreur = http_img_pack( |
|
| 51 | + 'plugin-dis-32.png', |
|
| 52 | + _T('plugin_info_non_compatible_spip'), |
|
| 53 | + " class='picto_err'", |
|
| 54 | + _T('plugin_info_non_compatible_spip') |
|
| 55 | + ); |
|
| 56 | + $class_li .= ' disabled'; |
|
| 57 | + $checkable = false; |
|
| 58 | + } elseif (isset($info['erreur'])) { |
|
| 59 | + $class_li .= ' error'; |
|
| 60 | + $erreur = http_img_pack( |
|
| 61 | + 'plugin-err-32.png', |
|
| 62 | + _T('plugin_info_erreur_xml'), |
|
| 63 | + " class='picto_err'", |
|
| 64 | + _T('plugin_info_erreur_xml') |
|
| 65 | + ) |
|
| 66 | + . "<div class='erreur'>" . join('<br >', $info['erreur']) . '</div>'; |
|
| 67 | + $checkable = false; |
|
| 68 | + } elseif (isset($GLOBALS['erreurs_activation_raw'][$dir_plugins . $plug_file])) { |
|
| 69 | + $class_li .= ' error'; |
|
| 70 | + $erreur = http_img_pack( |
|
| 71 | + 'plugin-err-32.png', |
|
| 72 | + _T('plugin_impossible_activer', ['plugin' => $nom]), |
|
| 73 | + " class='picto_err'", |
|
| 74 | + _T('plugin_impossible_activer', ['plugin' => $nom]) |
|
| 75 | + ) |
|
| 76 | + . "<div class='erreur'>" . implode( |
|
| 77 | + '<br />', |
|
| 78 | + $GLOBALS['erreurs_activation_raw'][$dir_plugins . $plug_file] |
|
| 79 | + ) . '</div>'; |
|
| 80 | + } else { |
|
| 81 | + $cfg = $actif ? plugin_bouton_config($plug_file, $info, $dir_plugins) : ''; |
|
| 82 | + if (defined('_DEV_VERSION_SPIP_COMPAT') and !plugin_version_compatible($info['compatibilite'], $GLOBALS['spip_version_branche'])) { |
|
| 83 | + //$info['slogan'] = _T('plugin_info_non_compatible_spip'); |
|
| 84 | + $erreur = http_img_pack( |
|
| 85 | + 'plugin-dis-32.png', |
|
| 86 | + _T('plugin_info_non_compatible_spip'), |
|
| 87 | + " class='picto_err picto_compat_forcee'", |
|
| 88 | + _L('Version incompatible : compatibilité forcée') |
|
| 89 | + ); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + // numerotons les occurrences d'un meme prefix |
|
| 94 | + $versions[$prefix] = $id = isset($versions[$prefix]) ? intval($versions[$prefix]) + 1 : ''; |
|
| 95 | + |
|
| 96 | + $class_li .= ($actif ? ' actif' : '') . ($expose ? ' on' : ''); |
|
| 97 | + |
|
| 98 | + return "<li id='$prefix$id' class='$class_li'>" |
|
| 99 | + . ((!$checkable and !$checked) |
|
| 100 | + ? '' : plugin_checkbox(++$id_input, $dir_plugins . $plug_file, $checked)) |
|
| 101 | + . plugin_resume($info, $dir_plugins, $plug_file, $url_page) |
|
| 102 | + . $cfg |
|
| 103 | + . $erreur |
|
| 104 | + . (($dir_plugins !== _DIR_PLUGINS_DIST and plugin_est_installe($plug_file)) |
|
| 105 | + ? plugin_desintalle($plug_file, $nom, $dir_plugins) : '') |
|
| 106 | + . "<div class='details'>" // pour l'ajax de exec/info_plugin |
|
| 107 | + . (!$expose ? '' : affiche_bloc_plugin($plug_file, $info, $dir_plugins)) |
|
| 108 | + . '</div>' |
|
| 109 | + . '</li>'; |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | function plugin_bouton_config($nom, $infos, $dir) { |
| 113 | - // la verification se base sur le filesystem |
|
| 114 | - // il faut donc n'utiliser que des minuscules, par convention |
|
| 115 | - $prefix = strtolower($infos['prefix']); |
|
| 116 | - // si paquet.xml fournit un squelette, le prendre |
|
| 117 | - if (isset($infos['config']) and $infos['config']) { |
|
| 118 | - return recuperer_fond( |
|
| 119 | - "$dir$nom/" . $infos['config'], |
|
| 120 | - [ |
|
| 121 | - 'script' => 'configurer_' . $prefix, |
|
| 122 | - 'nom' => $nom |
|
| 123 | - ] |
|
| 124 | - ); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - // sinon prendre le squelette std sur le nom std |
|
| 128 | - return recuperer_fond( |
|
| 129 | - 'prive/squelettes/inclure/cfg', |
|
| 130 | - [ |
|
| 131 | - 'script' => 'configurer_' . $prefix, |
|
| 132 | - 'nom' => $nom |
|
| 133 | - ] |
|
| 134 | - ); |
|
| 113 | + // la verification se base sur le filesystem |
|
| 114 | + // il faut donc n'utiliser que des minuscules, par convention |
|
| 115 | + $prefix = strtolower($infos['prefix']); |
|
| 116 | + // si paquet.xml fournit un squelette, le prendre |
|
| 117 | + if (isset($infos['config']) and $infos['config']) { |
|
| 118 | + return recuperer_fond( |
|
| 119 | + "$dir$nom/" . $infos['config'], |
|
| 120 | + [ |
|
| 121 | + 'script' => 'configurer_' . $prefix, |
|
| 122 | + 'nom' => $nom |
|
| 123 | + ] |
|
| 124 | + ); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + // sinon prendre le squelette std sur le nom std |
|
| 128 | + return recuperer_fond( |
|
| 129 | + 'prive/squelettes/inclure/cfg', |
|
| 130 | + [ |
|
| 131 | + 'script' => 'configurer_' . $prefix, |
|
| 132 | + 'nom' => $nom |
|
| 133 | + ] |
|
| 134 | + ); |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | // checkbox pour activer ou desactiver |
| 138 | 138 | // si ce n'est pas une extension |
| 139 | 139 | |
| 140 | 140 | function plugin_checkbox($id_input, $file, $actif) { |
| 141 | - $name = substr(md5($file), 0, 16); |
|
| 142 | - |
|
| 143 | - return "<div class='check'>\n" |
|
| 144 | - . "<input type='checkbox' name='s$name' id='label_$id_input'" |
|
| 145 | - . ($actif ? " checked='checked'" : '') |
|
| 146 | - . " class='checkbox' value='O' />" |
|
| 147 | - . "\n<label for='label_$id_input'>" . _T('activer_plugin') . '</label>' |
|
| 148 | - . '</div>'; |
|
| 141 | + $name = substr(md5($file), 0, 16); |
|
| 142 | + |
|
| 143 | + return "<div class='check'>\n" |
|
| 144 | + . "<input type='checkbox' name='s$name' id='label_$id_input'" |
|
| 145 | + . ($actif ? " checked='checked'" : '') |
|
| 146 | + . " class='checkbox' value='O' />" |
|
| 147 | + . "\n<label for='label_$id_input'>" . _T('activer_plugin') . '</label>' |
|
| 148 | + . '</div>'; |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | function plugin_nom($info, $dir_plugins, $plug_file) { |
| 152 | - $prefix = $info['prefix']; |
|
| 153 | - $dir = "$dir_plugins$plug_file"; |
|
| 154 | - // Si dtd paquet, on traite le nom soit par son item de langue soit par sa valeur immediate a l'index "nom" |
|
| 155 | - if ($info['dtd'] == 'paquet') { |
|
| 156 | - $nom = plugin_typo("{$prefix}_nom", "$dir/lang/paquet-$prefix"); |
|
| 157 | - if (!$nom) { |
|
| 158 | - $nom = typo($info['nom']); |
|
| 159 | - } |
|
| 160 | - } else { |
|
| 161 | - $nom = typo(attribut_html($info['nom'])); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - return trim($nom); |
|
| 152 | + $prefix = $info['prefix']; |
|
| 153 | + $dir = "$dir_plugins$plug_file"; |
|
| 154 | + // Si dtd paquet, on traite le nom soit par son item de langue soit par sa valeur immediate a l'index "nom" |
|
| 155 | + if ($info['dtd'] == 'paquet') { |
|
| 156 | + $nom = plugin_typo("{$prefix}_nom", "$dir/lang/paquet-$prefix"); |
|
| 157 | + if (!$nom) { |
|
| 158 | + $nom = typo($info['nom']); |
|
| 159 | + } |
|
| 160 | + } else { |
|
| 161 | + $nom = typo(attribut_html($info['nom'])); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + return trim($nom); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | // Cartouche Resume |
| 168 | 168 | function plugin_resume($info, $dir_plugins, $plug_file, $url_page) { |
| 169 | - $prefix = $info['prefix']; |
|
| 170 | - $dir = "$dir_plugins$plug_file"; |
|
| 171 | - $slogan = PtoBR(plugin_propre($info['slogan'], "$dir/lang/paquet-$prefix")); |
|
| 172 | - // une seule ligne dans le slogan : couper si besoin |
|
| 173 | - if (($p = strpos($slogan, '<br />')) !== false) { |
|
| 174 | - $slogan = substr($slogan, 0, $p); |
|
| 175 | - } |
|
| 176 | - // couper par securite |
|
| 177 | - $slogan = couper($slogan, 80); |
|
| 178 | - |
|
| 179 | - $nom = plugin_nom($info, $dir_plugins, $plug_file); |
|
| 180 | - |
|
| 181 | - $url = parametre_url($url_page, 'plugin', substr($dir, strlen(_DIR_RACINE))); |
|
| 182 | - |
|
| 183 | - $icon_class = 'icon'; |
|
| 184 | - $img = ''; |
|
| 185 | - if (isset($info['logo']) and $i = trim($info['logo'])) { |
|
| 186 | - $img = http_img_pack("$dir/$i", '', " width='32' height='32'", '', ['variante_svg_si_possible' => true, 'chemin_image' => false]); |
|
| 187 | - if (!extraire_attribut($img, 'src')) { |
|
| 188 | - $img = ''; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - if (!$img) { |
|
| 192 | - $img = http_img_pack('plugin-xx.svg', '', " width='32' height='32'"); |
|
| 193 | - $icon_class .= ' no-logo'; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - $i = "<div class='$icon_class'><a href='$url' rel='info'>$img</a></div>"; |
|
| 197 | - |
|
| 198 | - return "<div class='resume'>" |
|
| 199 | - . "<h3><a href='$url' rel='info'>" |
|
| 200 | - . $nom |
|
| 201 | - . '</a></h3>' |
|
| 202 | - . " <span class='version'>" . $info['version'] . '</span>' |
|
| 203 | - . " <span class='etat'> - " |
|
| 204 | - . plugin_etat_en_clair($info['etat']) |
|
| 205 | - . '</span>' |
|
| 206 | - . "<div class='short'>" . $slogan . '</div>' |
|
| 207 | - . $i |
|
| 208 | - . '</div>'; |
|
| 169 | + $prefix = $info['prefix']; |
|
| 170 | + $dir = "$dir_plugins$plug_file"; |
|
| 171 | + $slogan = PtoBR(plugin_propre($info['slogan'], "$dir/lang/paquet-$prefix")); |
|
| 172 | + // une seule ligne dans le slogan : couper si besoin |
|
| 173 | + if (($p = strpos($slogan, '<br />')) !== false) { |
|
| 174 | + $slogan = substr($slogan, 0, $p); |
|
| 175 | + } |
|
| 176 | + // couper par securite |
|
| 177 | + $slogan = couper($slogan, 80); |
|
| 178 | + |
|
| 179 | + $nom = plugin_nom($info, $dir_plugins, $plug_file); |
|
| 180 | + |
|
| 181 | + $url = parametre_url($url_page, 'plugin', substr($dir, strlen(_DIR_RACINE))); |
|
| 182 | + |
|
| 183 | + $icon_class = 'icon'; |
|
| 184 | + $img = ''; |
|
| 185 | + if (isset($info['logo']) and $i = trim($info['logo'])) { |
|
| 186 | + $img = http_img_pack("$dir/$i", '', " width='32' height='32'", '', ['variante_svg_si_possible' => true, 'chemin_image' => false]); |
|
| 187 | + if (!extraire_attribut($img, 'src')) { |
|
| 188 | + $img = ''; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + if (!$img) { |
|
| 192 | + $img = http_img_pack('plugin-xx.svg', '', " width='32' height='32'"); |
|
| 193 | + $icon_class .= ' no-logo'; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + $i = "<div class='$icon_class'><a href='$url' rel='info'>$img</a></div>"; |
|
| 197 | + |
|
| 198 | + return "<div class='resume'>" |
|
| 199 | + . "<h3><a href='$url' rel='info'>" |
|
| 200 | + . $nom |
|
| 201 | + . '</a></h3>' |
|
| 202 | + . " <span class='version'>" . $info['version'] . '</span>' |
|
| 203 | + . " <span class='etat'> - " |
|
| 204 | + . plugin_etat_en_clair($info['etat']) |
|
| 205 | + . '</span>' |
|
| 206 | + . "<div class='short'>" . $slogan . '</div>' |
|
| 207 | + . $i |
|
| 208 | + . '</div>'; |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | function plugin_desintalle($plug_file, $nom, $dir_plugins = null) { |
| 212 | - if (!$dir_plugins) { |
|
| 213 | - $dir_plugins = _DIR_PLUGINS; |
|
| 214 | - } |
|
| 212 | + if (!$dir_plugins) { |
|
| 213 | + $dir_plugins = _DIR_PLUGINS; |
|
| 214 | + } |
|
| 215 | 215 | |
| 216 | - $action = redirige_action_auteur('desinstaller_plugin', "$dir_plugins::$plug_file", 'admin_plugin'); |
|
| 217 | - $text = _T('bouton_desinstaller'); |
|
| 218 | - $text2 = _T('info_desinstaller_plugin'); |
|
| 219 | - $file = basename($plug_file); |
|
| 216 | + $action = redirige_action_auteur('desinstaller_plugin', "$dir_plugins::$plug_file", 'admin_plugin'); |
|
| 217 | + $text = _T('bouton_desinstaller'); |
|
| 218 | + $text2 = _T('info_desinstaller_plugin'); |
|
| 219 | + $file = basename($plug_file); |
|
| 220 | 220 | |
| 221 | - return "<div class='actions'>[" . |
|
| 222 | - "<a href='$action' |
|
| 221 | + return "<div class='actions'>[" . |
|
| 222 | + "<a href='$action' |
|
| 223 | 223 | onclick='return confirm(\"$text $nom ?\\n$text2\")'>" |
| 224 | - . $text |
|
| 225 | - . '</a>]</div>'; |
|
| 224 | + . $text |
|
| 225 | + . '</a>]</div>'; |
|
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | /** |
@@ -236,145 +236,145 @@ discard block |
||
| 236 | 236 | * Traduction de l'état dans la langue en cours |
| 237 | 237 | **/ |
| 238 | 238 | function plugin_etat_en_clair($etat) { |
| 239 | - if (!in_array($etat, ['stable', 'test', 'experimental'])) { |
|
| 240 | - $etat = 'developpement'; |
|
| 241 | - } |
|
| 239 | + if (!in_array($etat, ['stable', 'test', 'experimental'])) { |
|
| 240 | + $etat = 'developpement'; |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - return _T('plugin_etat_' . $etat); |
|
| 243 | + return _T('plugin_etat_' . $etat); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | function plugin_propre($texte, $module = '', $propre = 'propre') { |
| 247 | - // retirer le retour a la racine du module, car le find_in_path se fait depuis la racine |
|
| 248 | - if (_DIR_RACINE and strncmp($module, _DIR_RACINE, strlen(_DIR_RACINE)) == 0) { |
|
| 249 | - $module = substr($module, strlen(_DIR_RACINE)); |
|
| 250 | - } |
|
| 251 | - if (preg_match('|^\w+_[\w_]+$|', $texte)) { |
|
| 252 | - $texte = _T(($module ? "$module:" : '') . $texte, [], ['force' => false]); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return $propre($texte); |
|
| 247 | + // retirer le retour a la racine du module, car le find_in_path se fait depuis la racine |
|
| 248 | + if (_DIR_RACINE and strncmp($module, _DIR_RACINE, strlen(_DIR_RACINE)) == 0) { |
|
| 249 | + $module = substr($module, strlen(_DIR_RACINE)); |
|
| 250 | + } |
|
| 251 | + if (preg_match('|^\w+_[\w_]+$|', $texte)) { |
|
| 252 | + $texte = _T(($module ? "$module:" : '') . $texte, [], ['force' => false]); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return $propre($texte); |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | function plugin_typo($texte, $module = '') { |
| 259 | - return plugin_propre($texte, $module, 'typo'); |
|
| 259 | + return plugin_propre($texte, $module, 'typo'); |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | |
| 263 | 263 | function affiche_bloc_plugin($plug_file, $info, $dir_plugins = null) { |
| 264 | - $log = null; |
|
| 265 | - if (!$dir_plugins) { |
|
| 266 | - $dir_plugins = _DIR_PLUGINS; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - $prefix = $info['prefix']; |
|
| 270 | - $dir = "$dir_plugins$plug_file/lang/paquet-$prefix"; |
|
| 271 | - |
|
| 272 | - $s = ''; |
|
| 273 | - // TODO: le traiter_multi ici n'est pas beau |
|
| 274 | - // cf. description du plugin/_stable_/ortho/plugin.xml |
|
| 275 | - // concerne les anciens plugin.xml donc on devrait plus en avoir besoin |
|
| 276 | - $description = ''; |
|
| 277 | - if (isset($info['description'])) { |
|
| 278 | - $description = plugin_propre($info['description'], $dir); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - if ( |
|
| 282 | - isset($info['documentation']) |
|
| 283 | - and $lien = $info['documentation'] |
|
| 284 | - ) { |
|
| 285 | - $description .= "<p><em class='site'><a href='$lien' class='spip_out'>" . _T('en_savoir_plus') . '</a></em></p>'; |
|
| 286 | - } |
|
| 287 | - $s .= "<dd class='desc'>" . $description . "</dd>\n"; |
|
| 288 | - |
|
| 289 | - if (isset($info['auteur'])) { |
|
| 290 | - if (is_array($info['auteur'])) { |
|
| 291 | - $a = formater_credits($info['auteur'], ', '); |
|
| 292 | - } // pour compat mais ne doit plus arriver |
|
| 293 | - else { |
|
| 294 | - $a = trim($info['auteur']); |
|
| 295 | - } |
|
| 296 | - if ($a) { |
|
| 297 | - $s .= "<dt class='auteurs'>" . _T('public:par_auteur') . "</dt><dd class='auteurs'>" . PtoBR(propre( |
|
| 298 | - $a, |
|
| 299 | - $dir |
|
| 300 | - )) . "</dd>\n"; |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - if (isset($info['credit'])) { |
|
| 305 | - if ($a = formater_credits($info['credit'], ', ')) { |
|
| 306 | - $s .= "<dt class='credits'>" . _T('plugin_info_credit') . "</dt><dd class='credits'>" . PtoBR(propre( |
|
| 307 | - $a, |
|
| 308 | - $dir |
|
| 309 | - )) . "</dd>\n"; |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - if (isset($info['licence'])) { |
|
| 314 | - if (is_array($info['licence'])) { |
|
| 315 | - $a = formater_credits($info['licence'], ', '); |
|
| 316 | - } // pour compat mais ne doit plus arriver |
|
| 317 | - else { |
|
| 318 | - $a = trim($info['licence']); |
|
| 319 | - } |
|
| 320 | - if ($a) { |
|
| 321 | - $s .= "<dt class='licence'>" . _T('intitule_licence') . "</dt><dd class='licence'>" . PtoBR(propre( |
|
| 322 | - $a, |
|
| 323 | - $dir |
|
| 324 | - )) . "</dd>\n"; |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - $s = "<dl class='description'>$s</dl>"; |
|
| 329 | - |
|
| 330 | - // |
|
| 331 | - // Ajouter les infos techniques |
|
| 332 | - // |
|
| 333 | - $infotech = []; |
|
| 334 | - |
|
| 335 | - $version = '<dt>' . _T('version') . '</dt><dd>' . $info['version']; |
|
| 336 | - // Version VCS |
|
| 337 | - if ($vcs = version_vcs_courante($dir_plugins . $plug_file)) { |
|
| 338 | - $version .= ' ' . $vcs; |
|
| 339 | - } |
|
| 340 | - $version .= '</dd>'; |
|
| 341 | - $infotech[] = $version; |
|
| 342 | - $infotech[] = '<dt>' . _T('repertoire_plugins') . '</dt><dd>' . joli_repertoire("$dir_plugins$plug_file") . '</dd>'; |
|
| 343 | - // source zip le cas echeant |
|
| 344 | - $infotech[] = (lire_fichier($dir_plugins . $plug_file . '/install.log', $log) |
|
| 345 | - and preg_match(',^source:(.*)$,m', $log, $r)) |
|
| 346 | - ? '<dt>' . _T('plugin_source') . '</dt><dd>' . trim($r[1]) . '</dd>' |
|
| 347 | - : ''; |
|
| 348 | - |
|
| 349 | - $infotech[] = !$info['necessite'] ? '' : |
|
| 350 | - ('<dt>' . _T('plugin_info_necessite') . '</dt><dd>' . join( |
|
| 351 | - ' ', |
|
| 352 | - array_map('array_shift', $info['necessite']) |
|
| 353 | - ) . '</dd>'); |
|
| 354 | - |
|
| 355 | - $s .= "<dl class='tech'>" |
|
| 356 | - . join('', $infotech) |
|
| 357 | - . '</dl>'; |
|
| 358 | - |
|
| 359 | - |
|
| 360 | - return $s; |
|
| 264 | + $log = null; |
|
| 265 | + if (!$dir_plugins) { |
|
| 266 | + $dir_plugins = _DIR_PLUGINS; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + $prefix = $info['prefix']; |
|
| 270 | + $dir = "$dir_plugins$plug_file/lang/paquet-$prefix"; |
|
| 271 | + |
|
| 272 | + $s = ''; |
|
| 273 | + // TODO: le traiter_multi ici n'est pas beau |
|
| 274 | + // cf. description du plugin/_stable_/ortho/plugin.xml |
|
| 275 | + // concerne les anciens plugin.xml donc on devrait plus en avoir besoin |
|
| 276 | + $description = ''; |
|
| 277 | + if (isset($info['description'])) { |
|
| 278 | + $description = plugin_propre($info['description'], $dir); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + if ( |
|
| 282 | + isset($info['documentation']) |
|
| 283 | + and $lien = $info['documentation'] |
|
| 284 | + ) { |
|
| 285 | + $description .= "<p><em class='site'><a href='$lien' class='spip_out'>" . _T('en_savoir_plus') . '</a></em></p>'; |
|
| 286 | + } |
|
| 287 | + $s .= "<dd class='desc'>" . $description . "</dd>\n"; |
|
| 288 | + |
|
| 289 | + if (isset($info['auteur'])) { |
|
| 290 | + if (is_array($info['auteur'])) { |
|
| 291 | + $a = formater_credits($info['auteur'], ', '); |
|
| 292 | + } // pour compat mais ne doit plus arriver |
|
| 293 | + else { |
|
| 294 | + $a = trim($info['auteur']); |
|
| 295 | + } |
|
| 296 | + if ($a) { |
|
| 297 | + $s .= "<dt class='auteurs'>" . _T('public:par_auteur') . "</dt><dd class='auteurs'>" . PtoBR(propre( |
|
| 298 | + $a, |
|
| 299 | + $dir |
|
| 300 | + )) . "</dd>\n"; |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + if (isset($info['credit'])) { |
|
| 305 | + if ($a = formater_credits($info['credit'], ', ')) { |
|
| 306 | + $s .= "<dt class='credits'>" . _T('plugin_info_credit') . "</dt><dd class='credits'>" . PtoBR(propre( |
|
| 307 | + $a, |
|
| 308 | + $dir |
|
| 309 | + )) . "</dd>\n"; |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + if (isset($info['licence'])) { |
|
| 314 | + if (is_array($info['licence'])) { |
|
| 315 | + $a = formater_credits($info['licence'], ', '); |
|
| 316 | + } // pour compat mais ne doit plus arriver |
|
| 317 | + else { |
|
| 318 | + $a = trim($info['licence']); |
|
| 319 | + } |
|
| 320 | + if ($a) { |
|
| 321 | + $s .= "<dt class='licence'>" . _T('intitule_licence') . "</dt><dd class='licence'>" . PtoBR(propre( |
|
| 322 | + $a, |
|
| 323 | + $dir |
|
| 324 | + )) . "</dd>\n"; |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + $s = "<dl class='description'>$s</dl>"; |
|
| 329 | + |
|
| 330 | + // |
|
| 331 | + // Ajouter les infos techniques |
|
| 332 | + // |
|
| 333 | + $infotech = []; |
|
| 334 | + |
|
| 335 | + $version = '<dt>' . _T('version') . '</dt><dd>' . $info['version']; |
|
| 336 | + // Version VCS |
|
| 337 | + if ($vcs = version_vcs_courante($dir_plugins . $plug_file)) { |
|
| 338 | + $version .= ' ' . $vcs; |
|
| 339 | + } |
|
| 340 | + $version .= '</dd>'; |
|
| 341 | + $infotech[] = $version; |
|
| 342 | + $infotech[] = '<dt>' . _T('repertoire_plugins') . '</dt><dd>' . joli_repertoire("$dir_plugins$plug_file") . '</dd>'; |
|
| 343 | + // source zip le cas echeant |
|
| 344 | + $infotech[] = (lire_fichier($dir_plugins . $plug_file . '/install.log', $log) |
|
| 345 | + and preg_match(',^source:(.*)$,m', $log, $r)) |
|
| 346 | + ? '<dt>' . _T('plugin_source') . '</dt><dd>' . trim($r[1]) . '</dd>' |
|
| 347 | + : ''; |
|
| 348 | + |
|
| 349 | + $infotech[] = !$info['necessite'] ? '' : |
|
| 350 | + ('<dt>' . _T('plugin_info_necessite') . '</dt><dd>' . join( |
|
| 351 | + ' ', |
|
| 352 | + array_map('array_shift', $info['necessite']) |
|
| 353 | + ) . '</dd>'); |
|
| 354 | + |
|
| 355 | + $s .= "<dl class='tech'>" |
|
| 356 | + . join('', $infotech) |
|
| 357 | + . '</dl>'; |
|
| 358 | + |
|
| 359 | + |
|
| 360 | + return $s; |
|
| 361 | 361 | } |
| 362 | 362 | |
| 363 | 363 | function formater_credits($infos, $sep = ', ') { |
| 364 | - $texte = ''; |
|
| 365 | - |
|
| 366 | - foreach ($infos as $_credit) { |
|
| 367 | - if ($texte) { |
|
| 368 | - $texte .= $sep; |
|
| 369 | - } |
|
| 370 | - // Si le credit en cours n'est pas un array c'est donc un copyright |
|
| 371 | - $texte .= |
|
| 372 | - (!is_array($_credit)) |
|
| 373 | - ? PtoBR(propre($_credit)) |
|
| 374 | - : ($_credit['url'] ? '<a href="' . $_credit['url'] . '">' : '') . |
|
| 375 | - $_credit['nom'] . |
|
| 376 | - ($_credit['url'] ? '</a>' : ''); |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - return $texte; |
|
| 364 | + $texte = ''; |
|
| 365 | + |
|
| 366 | + foreach ($infos as $_credit) { |
|
| 367 | + if ($texte) { |
|
| 368 | + $texte .= $sep; |
|
| 369 | + } |
|
| 370 | + // Si le credit en cours n'est pas un array c'est donc un copyright |
|
| 371 | + $texte .= |
|
| 372 | + (!is_array($_credit)) |
|
| 373 | + ? PtoBR(propre($_credit)) |
|
| 374 | + : ($_credit['url'] ? '<a href="' . $_credit['url'] . '">' : '') . |
|
| 375 | + $_credit['nom'] . |
|
| 376 | + ($_credit['url'] ? '</a>' : ''); |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + return $texte; |
|
| 380 | 380 | } |
@@ -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 | /** |
@@ -45,29 +45,29 @@ discard block |
||
| 45 | 45 | * sinon code HTML de la page après le traitement effectué. |
| 46 | 46 | **/ |
| 47 | 47 | function inc_admin_dist($script, $titre, $comment = '', $anonymous = false) { |
| 48 | - $reprise = true; |
|
| 49 | - if ( |
|
| 50 | - !isset($GLOBALS['meta'][$script]) |
|
| 51 | - or !isset($GLOBALS['meta']['admin']) |
|
| 52 | - ) { |
|
| 53 | - $reprise = false; |
|
| 54 | - $res = debut_admin($script, $titre, $comment); |
|
| 55 | - if ($res) { |
|
| 56 | - return $res; |
|
| 57 | - } |
|
| 58 | - spip_log("meta: $script " . print_r($_POST, true)); |
|
| 59 | - ecrire_meta($script, serialize($_POST)); |
|
| 60 | - } |
|
| 48 | + $reprise = true; |
|
| 49 | + if ( |
|
| 50 | + !isset($GLOBALS['meta'][$script]) |
|
| 51 | + or !isset($GLOBALS['meta']['admin']) |
|
| 52 | + ) { |
|
| 53 | + $reprise = false; |
|
| 54 | + $res = debut_admin($script, $titre, $comment); |
|
| 55 | + if ($res) { |
|
| 56 | + return $res; |
|
| 57 | + } |
|
| 58 | + spip_log("meta: $script " . print_r($_POST, true)); |
|
| 59 | + ecrire_meta($script, serialize($_POST)); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - $res = admin_verifie_session($script, $anonymous); |
|
| 63 | - if ($res) { |
|
| 64 | - return $res; |
|
| 65 | - } |
|
| 66 | - $base = charger_fonction($script, 'base'); |
|
| 67 | - $base($titre, $reprise); |
|
| 68 | - fin_admin($script); |
|
| 62 | + $res = admin_verifie_session($script, $anonymous); |
|
| 63 | + if ($res) { |
|
| 64 | + return $res; |
|
| 65 | + } |
|
| 66 | + $base = charger_fonction($script, 'base'); |
|
| 67 | + $base($titre, $reprise); |
|
| 68 | + fin_admin($script); |
|
| 69 | 69 | |
| 70 | - return ''; |
|
| 70 | + return ''; |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -98,35 +98,35 @@ discard block |
||
| 98 | 98 | * Code HTML si message d'erreur, '' sinon; |
| 99 | 99 | */ |
| 100 | 100 | function admin_verifie_session($script, $anonymous = false) { |
| 101 | - include_spip('base/abstract_sql'); |
|
| 102 | - $pref = sprintf('_%d_', $GLOBALS['visiteur_session']['id_auteur']); |
|
| 103 | - $signal = fichier_admin($script, "$script$pref"); |
|
| 104 | - $valeur = sql_getfetsel('valeur', 'spip_meta', "nom='admin'"); |
|
| 105 | - if ($valeur === null) { |
|
| 106 | - ecrire_meta('admin', $signal, 'non'); |
|
| 107 | - } else { |
|
| 108 | - if (!$anonymous and ($valeur != $signal)) { |
|
| 109 | - if ( |
|
| 110 | - !preg_match('/^(.*)_(\d+)_/', $GLOBALS['meta']['admin'], $l) |
|
| 111 | - or intval($l[2]) != $GLOBALS['visiteur_session']['id_auteur'] |
|
| 112 | - ) { |
|
| 113 | - include_spip('inc/minipres'); |
|
| 114 | - spip_log("refus de lancer $script, priorite a $valeur"); |
|
| 101 | + include_spip('base/abstract_sql'); |
|
| 102 | + $pref = sprintf('_%d_', $GLOBALS['visiteur_session']['id_auteur']); |
|
| 103 | + $signal = fichier_admin($script, "$script$pref"); |
|
| 104 | + $valeur = sql_getfetsel('valeur', 'spip_meta', "nom='admin'"); |
|
| 105 | + if ($valeur === null) { |
|
| 106 | + ecrire_meta('admin', $signal, 'non'); |
|
| 107 | + } else { |
|
| 108 | + if (!$anonymous and ($valeur != $signal)) { |
|
| 109 | + if ( |
|
| 110 | + !preg_match('/^(.*)_(\d+)_/', $GLOBALS['meta']['admin'], $l) |
|
| 111 | + or intval($l[2]) != $GLOBALS['visiteur_session']['id_auteur'] |
|
| 112 | + ) { |
|
| 113 | + include_spip('inc/minipres'); |
|
| 114 | + spip_log("refus de lancer $script, priorite a $valeur"); |
|
| 115 | 115 | |
| 116 | - return minipres(_T('info_travaux_texte'), '', ['status' => 503]); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - $journal = 'spip'; |
|
| 121 | - if (autoriser('configurer')) { |
|
| 122 | - // c'est une action webmestre, soit par ftp soit par statut webmestre |
|
| 123 | - $journal = 'webmestre'; |
|
| 124 | - } |
|
| 125 | - // on pourrait statuer automatiquement les webmestres a l'init d'une action auth par ftp ... ? |
|
| 116 | + return minipres(_T('info_travaux_texte'), '', ['status' => 503]); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + $journal = 'spip'; |
|
| 121 | + if (autoriser('configurer')) { |
|
| 122 | + // c'est une action webmestre, soit par ftp soit par statut webmestre |
|
| 123 | + $journal = 'webmestre'; |
|
| 124 | + } |
|
| 125 | + // on pourrait statuer automatiquement les webmestres a l'init d'une action auth par ftp ... ? |
|
| 126 | 126 | |
| 127 | - spip_log("admin $pref" . ($valeur ? ' (reprise)' : ' (init)'), $journal); |
|
| 127 | + spip_log("admin $pref" . ($valeur ? ' (reprise)' : ' (init)'), $journal); |
|
| 128 | 128 | |
| 129 | - return ''; |
|
| 129 | + return ''; |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
@@ -139,11 +139,11 @@ discard block |
||
| 139 | 139 | * Chemin du répertoire. |
| 140 | 140 | **/ |
| 141 | 141 | function dir_admin() { |
| 142 | - if (autoriser('configurer')) { |
|
| 143 | - return _DIR_TMP; |
|
| 144 | - } else { |
|
| 145 | - return _DIR_TRANSFERT . $GLOBALS['visiteur_session']['login'] . '/'; |
|
| 146 | - } |
|
| 142 | + if (autoriser('configurer')) { |
|
| 143 | + return _DIR_TMP; |
|
| 144 | + } else { |
|
| 145 | + return _DIR_TRANSFERT . $GLOBALS['visiteur_session']['login'] . '/'; |
|
| 146 | + } |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | /** |
@@ -160,8 +160,8 @@ discard block |
||
| 160 | 160 | * Nom du fichier |
| 161 | 161 | **/ |
| 162 | 162 | function fichier_admin($action, $pref = 'admin_') { |
| 163 | - return $pref . |
|
| 164 | - substr(md5($action . (time() & ~2047) . $GLOBALS['visiteur_session']['login']), 0, 10); |
|
| 163 | + return $pref . |
|
| 164 | + substr(md5($action . (time() & ~2047) . $GLOBALS['visiteur_session']['login']), 0, 10); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | /** |
@@ -187,73 +187,73 @@ discard block |
||
| 187 | 187 | * sinon chaîne vide si déjà fait. |
| 188 | 188 | **/ |
| 189 | 189 | function debut_admin($script, $action = '', $corps = '') { |
| 190 | - if ((!$action) || !(autoriser('webmestre') or autoriser('chargerftp'))) { |
|
| 191 | - include_spip('inc/minipres'); |
|
| 190 | + if ((!$action) || !(autoriser('webmestre') or autoriser('chargerftp'))) { |
|
| 191 | + include_spip('inc/minipres'); |
|
| 192 | 192 | |
| 193 | - return minipres(); |
|
| 194 | - } else { |
|
| 195 | - $dir = dir_admin(); |
|
| 196 | - $signal = fichier_admin($script); |
|
| 197 | - if (@file_exists($dir . $signal)) { |
|
| 198 | - spip_log("Action admin: $action"); |
|
| 193 | + return minipres(); |
|
| 194 | + } else { |
|
| 195 | + $dir = dir_admin(); |
|
| 196 | + $signal = fichier_admin($script); |
|
| 197 | + if (@file_exists($dir . $signal)) { |
|
| 198 | + spip_log("Action admin: $action"); |
|
| 199 | 199 | |
| 200 | - return ''; |
|
| 201 | - } |
|
| 202 | - include_spip('inc/minipres'); |
|
| 200 | + return ''; |
|
| 201 | + } |
|
| 202 | + include_spip('inc/minipres'); |
|
| 203 | 203 | |
| 204 | - // Si on est un super-admin, un bouton de validation suffit |
|
| 205 | - // sauf dans les cas destroy |
|
| 206 | - if ( |
|
| 207 | - (autoriser('webmestre') or $script === 'repair') |
|
| 208 | - and $script != 'delete_all' |
|
| 209 | - ) { |
|
| 210 | - if (_request('validation_admin') == $signal) { |
|
| 211 | - spip_log("Action super-admin: $action"); |
|
| 204 | + // Si on est un super-admin, un bouton de validation suffit |
|
| 205 | + // sauf dans les cas destroy |
|
| 206 | + if ( |
|
| 207 | + (autoriser('webmestre') or $script === 'repair') |
|
| 208 | + and $script != 'delete_all' |
|
| 209 | + ) { |
|
| 210 | + if (_request('validation_admin') == $signal) { |
|
| 211 | + spip_log("Action super-admin: $action"); |
|
| 212 | 212 | |
| 213 | - return ''; |
|
| 214 | - } |
|
| 215 | - $corps .= '<input type="hidden" name="validation_admin" value="' . $signal . '" />'; |
|
| 216 | - $suivant = _T('bouton_valider'); |
|
| 217 | - $js = ''; |
|
| 218 | - } else { |
|
| 219 | - // cet appel permet d'assurer un copier-coller du nom du repertoire a creer dans tmp (esj) |
|
| 220 | - // l'insertion du script a cet endroit n'est pas xhtml licite |
|
| 221 | - // mais evite de l'embarquer dans toutes les pages minipres |
|
| 222 | - $corps .= http_script('', 'spip_barre.js'); |
|
| 213 | + return ''; |
|
| 214 | + } |
|
| 215 | + $corps .= '<input type="hidden" name="validation_admin" value="' . $signal . '" />'; |
|
| 216 | + $suivant = _T('bouton_valider'); |
|
| 217 | + $js = ''; |
|
| 218 | + } else { |
|
| 219 | + // cet appel permet d'assurer un copier-coller du nom du repertoire a creer dans tmp (esj) |
|
| 220 | + // l'insertion du script a cet endroit n'est pas xhtml licite |
|
| 221 | + // mais evite de l'embarquer dans toutes les pages minipres |
|
| 222 | + $corps .= http_script('', 'spip_barre.js'); |
|
| 223 | 223 | |
| 224 | - $corps .= '<fieldset><legend>' |
|
| 225 | - . _T('info_authentification_ftp') |
|
| 226 | - . aider('ftp_auth') |
|
| 227 | - . "</legend>\n<label for='fichier'>" |
|
| 228 | - . _T('info_creer_repertoire') |
|
| 229 | - . "</label>\n" |
|
| 230 | - . "<span id='signal' class='formo'>" . $signal . '</span>' |
|
| 231 | - . "<input type='hidden' id='fichier' name='fichier' value='" |
|
| 232 | - . $signal |
|
| 233 | - . "' />" |
|
| 234 | - . _T('info_creer_repertoire_2', ['repertoire' => joli_repertoire($dir)]) |
|
| 235 | - . '</fieldset>'; |
|
| 224 | + $corps .= '<fieldset><legend>' |
|
| 225 | + . _T('info_authentification_ftp') |
|
| 226 | + . aider('ftp_auth') |
|
| 227 | + . "</legend>\n<label for='fichier'>" |
|
| 228 | + . _T('info_creer_repertoire') |
|
| 229 | + . "</label>\n" |
|
| 230 | + . "<span id='signal' class='formo'>" . $signal . '</span>' |
|
| 231 | + . "<input type='hidden' id='fichier' name='fichier' value='" |
|
| 232 | + . $signal |
|
| 233 | + . "' />" |
|
| 234 | + . _T('info_creer_repertoire_2', ['repertoire' => joli_repertoire($dir)]) |
|
| 235 | + . '</fieldset>'; |
|
| 236 | 236 | |
| 237 | - $suivant = _T('bouton_recharger_page'); |
|
| 237 | + $suivant = _T('bouton_recharger_page'); |
|
| 238 | 238 | |
| 239 | - // code volontairement tordu: |
|
| 240 | - // provoquer la copie dans le presse papier du nom du repertoire |
|
| 241 | - // en remettant a vide le champ pour que ca marche aussi en cas |
|
| 242 | - // de JavaScript inactif. |
|
| 243 | - $js = " onload='var range=document.createRange(); var signal = document.getElementById(\"signal\"); var userSelection = window.getSelection(); range.setStart(signal,0); range.setEnd(signal,1); userSelection.addRange(range);'"; |
|
| 244 | - } |
|
| 239 | + // code volontairement tordu: |
|
| 240 | + // provoquer la copie dans le presse papier du nom du repertoire |
|
| 241 | + // en remettant a vide le champ pour que ca marche aussi en cas |
|
| 242 | + // de JavaScript inactif. |
|
| 243 | + $js = " onload='var range=document.createRange(); var signal = document.getElementById(\"signal\"); var userSelection = window.getSelection(); range.setStart(signal,0); range.setEnd(signal,1); userSelection.addRange(range);'"; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - // admin/xxx correspond |
|
| 247 | - // a exec/base_xxx de preference |
|
| 248 | - // et exec/xxx sinon (compat) |
|
| 249 | - if (tester_url_ecrire("base_$script")) { |
|
| 250 | - $script = "base_$script"; |
|
| 251 | - } |
|
| 252 | - $form = copy_request($script, $corps, $suivant); |
|
| 253 | - $info_action = _T('info_action', ['action' => "$action"]); |
|
| 246 | + // admin/xxx correspond |
|
| 247 | + // a exec/base_xxx de preference |
|
| 248 | + // et exec/xxx sinon (compat) |
|
| 249 | + if (tester_url_ecrire("base_$script")) { |
|
| 250 | + $script = "base_$script"; |
|
| 251 | + } |
|
| 252 | + $form = copy_request($script, $corps, $suivant); |
|
| 253 | + $info_action = _T('info_action', ['action' => "$action"]); |
|
| 254 | 254 | |
| 255 | - return minipres($info_action, $form, ['onload' => $js]); |
|
| 256 | - } |
|
| 255 | + return minipres($info_action, $form, ['onload' => $js]); |
|
| 256 | + } |
|
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | /** |
@@ -264,13 +264,13 @@ discard block |
||
| 264 | 264 | * Nom de l'action (en base) qui a été exécutée |
| 265 | 265 | **/ |
| 266 | 266 | function fin_admin($action) { |
| 267 | - $signal = dir_admin() . fichier_admin($action); |
|
| 268 | - spip_unlink($signal); |
|
| 269 | - if ($action != 'delete_all') { |
|
| 270 | - effacer_meta($action); |
|
| 271 | - effacer_meta('admin'); |
|
| 272 | - spip_log("efface les meta admin et $action "); |
|
| 273 | - } |
|
| 267 | + $signal = dir_admin() . fichier_admin($action); |
|
| 268 | + spip_unlink($signal); |
|
| 269 | + if ($action != 'delete_all') { |
|
| 270 | + effacer_meta($action); |
|
| 271 | + effacer_meta('admin'); |
|
| 272 | + spip_log("efface les meta admin et $action "); |
|
| 273 | + } |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | 276 | /** |
@@ -289,14 +289,14 @@ discard block |
||
| 289 | 289 | * Code HTML du formulaire |
| 290 | 290 | **/ |
| 291 | 291 | function copy_request($script, $suite, $submit = '') { |
| 292 | - include_spip('inc/filtres'); |
|
| 293 | - foreach (array_merge($_POST, $_GET) as $n => $c) { |
|
| 294 | - if (!in_array($n, ['fichier', 'exec', 'validation_admin']) and !is_array($c)) { |
|
| 295 | - $suite .= "\n<input type='hidden' name='" . spip_htmlspecialchars($n) . "' value='" . |
|
| 296 | - entites_html($c) . |
|
| 297 | - "' />"; |
|
| 298 | - } |
|
| 299 | - } |
|
| 292 | + include_spip('inc/filtres'); |
|
| 293 | + foreach (array_merge($_POST, $_GET) as $n => $c) { |
|
| 294 | + if (!in_array($n, ['fichier', 'exec', 'validation_admin']) and !is_array($c)) { |
|
| 295 | + $suite .= "\n<input type='hidden' name='" . spip_htmlspecialchars($n) . "' value='" . |
|
| 296 | + entites_html($c) . |
|
| 297 | + "' />"; |
|
| 298 | + } |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | - return generer_form_ecrire($script, $suite, '', $submit); |
|
| 301 | + return generer_form_ecrire($script, $suite, '', $submit); |
|
| 302 | 302 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | * @package SPIP\Core\Langue |
| 17 | 17 | **/ |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | include_spip('inc/cookie'); |
@@ -30,21 +30,21 @@ discard block |
||
| 30 | 30 | * @return void |
| 31 | 31 | */ |
| 32 | 32 | function action_converser_dist() { |
| 33 | - $update_session = false; |
|
| 34 | - if (_request('arg') and spip_connect()) { |
|
| 35 | - $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 36 | - $securiser_action(); |
|
| 37 | - $update_session = true; |
|
| 38 | - } |
|
| 33 | + $update_session = false; |
|
| 34 | + if (_request('arg') and spip_connect()) { |
|
| 35 | + $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 36 | + $securiser_action(); |
|
| 37 | + $update_session = true; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - $lang = action_converser_changer_langue($update_session); |
|
| 41 | - $redirect = rawurldecode(_request('redirect')); |
|
| 40 | + $lang = action_converser_changer_langue($update_session); |
|
| 41 | + $redirect = rawurldecode(_request('redirect')); |
|
| 42 | 42 | |
| 43 | - if (!$redirect) { |
|
| 44 | - $redirect = _DIR_RESTREINT_ABS; |
|
| 45 | - } |
|
| 46 | - $redirect = parametre_url($redirect, 'lang', $lang, '&'); |
|
| 47 | - redirige_par_entete($redirect, true); |
|
| 43 | + if (!$redirect) { |
|
| 44 | + $redirect = _DIR_RESTREINT_ABS; |
|
| 45 | + } |
|
| 46 | + $redirect = parametre_url($redirect, 'lang', $lang, '&'); |
|
| 47 | + redirige_par_entete($redirect, true); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
@@ -56,23 +56,23 @@ discard block |
||
| 56 | 56 | * @return string |
| 57 | 57 | */ |
| 58 | 58 | function action_converser_changer_langue($update_session) { |
| 59 | - if ($lang = _request('var_lang')) { |
|
| 60 | - action_converser_post($lang); |
|
| 61 | - } elseif ($lang = _request('var_lang_ecrire')) { |
|
| 62 | - if ($update_session) { |
|
| 63 | - sql_updateq('spip_auteurs', ['lang' => $lang], 'id_auteur = ' . $GLOBALS['visiteur_session']['id_auteur']); |
|
| 64 | - $GLOBALS['visiteur_session']['lang'] = $lang; |
|
| 65 | - $session = charger_fonction('session', 'inc'); |
|
| 66 | - if ($spip_session = $session($GLOBALS['visiteur_session'])) { |
|
| 67 | - spip_setcookie('spip_session', $spip_session, [ |
|
| 68 | - 'expires' => time() + 3600 * 24 * 14 |
|
| 69 | - ]); |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - action_converser_post($lang, true); |
|
| 73 | - } |
|
| 59 | + if ($lang = _request('var_lang')) { |
|
| 60 | + action_converser_post($lang); |
|
| 61 | + } elseif ($lang = _request('var_lang_ecrire')) { |
|
| 62 | + if ($update_session) { |
|
| 63 | + sql_updateq('spip_auteurs', ['lang' => $lang], 'id_auteur = ' . $GLOBALS['visiteur_session']['id_auteur']); |
|
| 64 | + $GLOBALS['visiteur_session']['lang'] = $lang; |
|
| 65 | + $session = charger_fonction('session', 'inc'); |
|
| 66 | + if ($spip_session = $session($GLOBALS['visiteur_session'])) { |
|
| 67 | + spip_setcookie('spip_session', $spip_session, [ |
|
| 68 | + 'expires' => time() + 3600 * 24 * 14 |
|
| 69 | + ]); |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + action_converser_post($lang, true); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - return $lang; |
|
| 75 | + return $lang; |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
@@ -83,17 +83,17 @@ discard block |
||
| 83 | 83 | * @return void |
| 84 | 84 | */ |
| 85 | 85 | function action_converser_post($lang, $ecrire = false) { |
| 86 | - if ($lang) { |
|
| 87 | - include_spip('inc/lang'); |
|
| 88 | - if (changer_langue($lang)) { |
|
| 89 | - spip_setcookie('spip_lang', $_COOKIE['spip_lang'] = $lang, [ |
|
| 90 | - 'expires' => time() + 365 * 24 * 3600 |
|
| 91 | - ]); |
|
| 92 | - if ($ecrire) { |
|
| 93 | - spip_setcookie('spip_lang_ecrire', $_COOKIE['spip_lang_ecrire'] = $lang, [ |
|
| 94 | - 'expires' => time() + 365 * 24 * 3600 |
|
| 95 | - ]); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - } |
|
| 86 | + if ($lang) { |
|
| 87 | + include_spip('inc/lang'); |
|
| 88 | + if (changer_langue($lang)) { |
|
| 89 | + spip_setcookie('spip_lang', $_COOKIE['spip_lang'] = $lang, [ |
|
| 90 | + 'expires' => time() + 365 * 24 * 3600 |
|
| 91 | + ]); |
|
| 92 | + if ($ecrire) { |
|
| 93 | + spip_setcookie('spip_lang_ecrire', $_COOKIE['spip_lang_ecrire'] = $lang, [ |
|
| 94 | + 'expires' => time() + 365 * 24 * 3600 |
|
| 95 | + ]); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | 99 | } |
@@ -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 | include_spip('inc/cookie'); |
@@ -34,73 +34,73 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | */ |
| 36 | 36 | function action_logout_dist() { |
| 37 | - $logout = _request('logout'); |
|
| 38 | - $url = securiser_redirect_action(_request('url')); |
|
| 39 | - // cas particulier, logout dans l'espace public |
|
| 40 | - if ($logout == 'public' and !$url) { |
|
| 41 | - $url = url_de_base(); |
|
| 42 | - } |
|
| 37 | + $logout = _request('logout'); |
|
| 38 | + $url = securiser_redirect_action(_request('url')); |
|
| 39 | + // cas particulier, logout dans l'espace public |
|
| 40 | + if ($logout == 'public' and !$url) { |
|
| 41 | + $url = url_de_base(); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - // seul le loge peut se deloger (mais id_auteur peut valoir 0 apres une restauration avortee) |
|
| 45 | - if ( |
|
| 46 | - isset($GLOBALS['visiteur_session']['id_auteur']) |
|
| 47 | - and is_numeric($GLOBALS['visiteur_session']['id_auteur']) |
|
| 48 | - // des sessions anonymes avec id_auteur=0 existent, mais elle n'ont pas de statut : double check |
|
| 49 | - and isset($GLOBALS['visiteur_session']['statut']) |
|
| 50 | - ) { |
|
| 51 | - // il faut un jeton pour fermer la session (eviter les CSRF) |
|
| 52 | - if ( |
|
| 53 | - !$jeton = _request('jeton') |
|
| 54 | - or !verifier_jeton_logout($jeton, $GLOBALS['visiteur_session']) |
|
| 55 | - ) { |
|
| 56 | - $jeton = generer_jeton_logout($GLOBALS['visiteur_session']); |
|
| 57 | - $action = generer_url_action('logout', "jeton=$jeton"); |
|
| 58 | - $action = parametre_url($action, 'logout', _request('logout')); |
|
| 59 | - $action = parametre_url($action, 'url', _request('url')); |
|
| 60 | - include_spip('inc/minipres'); |
|
| 61 | - include_spip('inc/filtres'); |
|
| 62 | - $texte = bouton_action(_T('spip:icone_deconnecter'), $action); |
|
| 63 | - $texte = "<div class='boutons'>$texte</div>"; |
|
| 64 | - $texte .= '<script type="text/javascript">document.write("<style>body{visibility:hidden;}</style>");window.document.forms[0].submit();</script>'; |
|
| 65 | - $res = minipres(_T('spip:icone_deconnecter'), $texte, ['all_inline' => true]); |
|
| 66 | - echo $res; |
|
| 44 | + // seul le loge peut se deloger (mais id_auteur peut valoir 0 apres une restauration avortee) |
|
| 45 | + if ( |
|
| 46 | + isset($GLOBALS['visiteur_session']['id_auteur']) |
|
| 47 | + and is_numeric($GLOBALS['visiteur_session']['id_auteur']) |
|
| 48 | + // des sessions anonymes avec id_auteur=0 existent, mais elle n'ont pas de statut : double check |
|
| 49 | + and isset($GLOBALS['visiteur_session']['statut']) |
|
| 50 | + ) { |
|
| 51 | + // il faut un jeton pour fermer la session (eviter les CSRF) |
|
| 52 | + if ( |
|
| 53 | + !$jeton = _request('jeton') |
|
| 54 | + or !verifier_jeton_logout($jeton, $GLOBALS['visiteur_session']) |
|
| 55 | + ) { |
|
| 56 | + $jeton = generer_jeton_logout($GLOBALS['visiteur_session']); |
|
| 57 | + $action = generer_url_action('logout', "jeton=$jeton"); |
|
| 58 | + $action = parametre_url($action, 'logout', _request('logout')); |
|
| 59 | + $action = parametre_url($action, 'url', _request('url')); |
|
| 60 | + include_spip('inc/minipres'); |
|
| 61 | + include_spip('inc/filtres'); |
|
| 62 | + $texte = bouton_action(_T('spip:icone_deconnecter'), $action); |
|
| 63 | + $texte = "<div class='boutons'>$texte</div>"; |
|
| 64 | + $texte .= '<script type="text/javascript">document.write("<style>body{visibility:hidden;}</style>");window.document.forms[0].submit();</script>'; |
|
| 65 | + $res = minipres(_T('spip:icone_deconnecter'), $texte, ['all_inline' => true]); |
|
| 66 | + echo $res; |
|
| 67 | 67 | |
| 68 | - return; |
|
| 69 | - } |
|
| 68 | + return; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - include_spip('inc/auth'); |
|
| 72 | - auth_trace($GLOBALS['visiteur_session'], '0000-00-00 00:00:00'); |
|
| 73 | - // le logout explicite vaut destruction de toutes les sessions |
|
| 74 | - if (isset($_COOKIE['spip_session'])) { |
|
| 75 | - $session = charger_fonction('session', 'inc'); |
|
| 76 | - $session($GLOBALS['visiteur_session']['id_auteur']); |
|
| 77 | - spip_setcookie('spip_session', $_COOKIE['spip_session'], [ |
|
| 78 | - 'expires' => time() - 3600 |
|
| 79 | - ]); |
|
| 80 | - } |
|
| 81 | - // si authentification http, et que la personne est loge, |
|
| 82 | - // pour se deconnecter, il faut proposer un nouveau formulaire de connexion http |
|
| 83 | - if ( |
|
| 84 | - isset($_SERVER['PHP_AUTH_USER']) |
|
| 85 | - and !$GLOBALS['ignore_auth_http'] |
|
| 86 | - and $GLOBALS['auth_can_disconnect'] |
|
| 87 | - ) { |
|
| 88 | - ask_php_auth( |
|
| 89 | - _T('login_deconnexion_ok'), |
|
| 90 | - _T('login_verifiez_navigateur'), |
|
| 91 | - _T('login_retour_public'), |
|
| 92 | - 'redirect=' . _DIR_RESTREINT_ABS, |
|
| 93 | - _T('login_test_navigateur'), |
|
| 94 | - true |
|
| 95 | - ); |
|
| 96 | - } |
|
| 97 | - } |
|
| 71 | + include_spip('inc/auth'); |
|
| 72 | + auth_trace($GLOBALS['visiteur_session'], '0000-00-00 00:00:00'); |
|
| 73 | + // le logout explicite vaut destruction de toutes les sessions |
|
| 74 | + if (isset($_COOKIE['spip_session'])) { |
|
| 75 | + $session = charger_fonction('session', 'inc'); |
|
| 76 | + $session($GLOBALS['visiteur_session']['id_auteur']); |
|
| 77 | + spip_setcookie('spip_session', $_COOKIE['spip_session'], [ |
|
| 78 | + 'expires' => time() - 3600 |
|
| 79 | + ]); |
|
| 80 | + } |
|
| 81 | + // si authentification http, et que la personne est loge, |
|
| 82 | + // pour se deconnecter, il faut proposer un nouveau formulaire de connexion http |
|
| 83 | + if ( |
|
| 84 | + isset($_SERVER['PHP_AUTH_USER']) |
|
| 85 | + and !$GLOBALS['ignore_auth_http'] |
|
| 86 | + and $GLOBALS['auth_can_disconnect'] |
|
| 87 | + ) { |
|
| 88 | + ask_php_auth( |
|
| 89 | + _T('login_deconnexion_ok'), |
|
| 90 | + _T('login_verifiez_navigateur'), |
|
| 91 | + _T('login_retour_public'), |
|
| 92 | + 'redirect=' . _DIR_RESTREINT_ABS, |
|
| 93 | + _T('login_test_navigateur'), |
|
| 94 | + true |
|
| 95 | + ); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - // Rediriger en contrant le cache navigateur (Safari3) |
|
| 100 | - include_spip('inc/headers'); |
|
| 101 | - redirige_par_entete($url |
|
| 102 | - ? parametre_url($url, 'var_hasard', uniqid(random_int(0, mt_getrandmax())), '&') |
|
| 103 | - : generer_url_public('login')); |
|
| 99 | + // Rediriger en contrant le cache navigateur (Safari3) |
|
| 100 | + include_spip('inc/headers'); |
|
| 101 | + redirige_par_entete($url |
|
| 102 | + ? parametre_url($url, 'var_hasard', uniqid(random_int(0, mt_getrandmax())), '&') |
|
| 103 | + : generer_url_public('login')); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | /** |
@@ -111,17 +111,17 @@ discard block |
||
| 111 | 111 | * @return string |
| 112 | 112 | */ |
| 113 | 113 | function generer_jeton_logout($session, $alea = null) { |
| 114 | - if (is_null($alea)) { |
|
| 115 | - include_spip('inc/acces'); |
|
| 116 | - $alea = charger_aleas(); |
|
| 117 | - } |
|
| 114 | + if (is_null($alea)) { |
|
| 115 | + include_spip('inc/acces'); |
|
| 116 | + $alea = charger_aleas(); |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - $jeton = md5($session['date_session'] |
|
| 120 | - . $session['id_auteur'] |
|
| 121 | - . $session['statut'] |
|
| 122 | - . $alea); |
|
| 119 | + $jeton = md5($session['date_session'] |
|
| 120 | + . $session['id_auteur'] |
|
| 121 | + . $session['statut'] |
|
| 122 | + . $alea); |
|
| 123 | 123 | |
| 124 | - return $jeton; |
|
| 124 | + return $jeton; |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | /** |
@@ -135,13 +135,13 @@ discard block |
||
| 135 | 135 | * @return bool |
| 136 | 136 | */ |
| 137 | 137 | function verifier_jeton_logout($jeton, $session) { |
| 138 | - if (generer_jeton_logout($session) === $jeton) { |
|
| 139 | - return true; |
|
| 140 | - } |
|
| 138 | + if (generer_jeton_logout($session) === $jeton) { |
|
| 139 | + return true; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - if (generer_jeton_logout($session, $GLOBALS['meta']['alea_ephemere_ancien']) === $jeton) { |
|
| 143 | - return true; |
|
| 144 | - } |
|
| 142 | + if (generer_jeton_logout($session, $GLOBALS['meta']['alea_ephemere_ancien']) === $jeton) { |
|
| 143 | + return true; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - return false; |
|
| 146 | + return false; |
|
| 147 | 147 | } |
@@ -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 | /** |
@@ -31,43 +31,43 @@ discard block |
||
| 31 | 31 | * @return string |
| 32 | 32 | */ |
| 33 | 33 | function action_instituer_langue_objet_dist($objet, $id, $id_rubrique, $changer_lang, $serveur = '') { |
| 34 | - if ($changer_lang) { |
|
| 35 | - $table_objet_sql = table_objet_sql($objet); |
|
| 36 | - $id_table_objet = id_table_objet($objet); |
|
| 37 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 38 | - $desc = $trouver_table($table_objet_sql, $serveur); |
|
| 34 | + if ($changer_lang) { |
|
| 35 | + $table_objet_sql = table_objet_sql($objet); |
|
| 36 | + $id_table_objet = id_table_objet($objet); |
|
| 37 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 38 | + $desc = $trouver_table($table_objet_sql, $serveur); |
|
| 39 | 39 | |
| 40 | - $set = []; |
|
| 41 | - if (isset($desc['field']['langue_choisie'])) { |
|
| 42 | - $set['langue_choisie'] = 'oui'; |
|
| 43 | - } |
|
| 40 | + $set = []; |
|
| 41 | + if (isset($desc['field']['langue_choisie'])) { |
|
| 42 | + $set['langue_choisie'] = 'oui'; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - if ($changer_lang != 'herit') { |
|
| 46 | - $set['lang'] = $changer_lang; |
|
| 47 | - sql_updateq($table_objet_sql, $set, "$id_table_objet=" . intval($id), [], $serveur); |
|
| 48 | - include_spip('inc/rubriques'); // pour calculer_langues_rubriques et calculer_langues_utilisees |
|
| 49 | - if ($table_objet_sql == 'spip_rubriques') { |
|
| 50 | - calculer_langues_rubriques(); |
|
| 51 | - } |
|
| 52 | - $langues = calculer_langues_utilisees($serveur); |
|
| 53 | - ecrire_meta('langues_utilisees', $langues); |
|
| 54 | - } else { |
|
| 55 | - $langue_parent = sql_getfetsel('lang', 'spip_rubriques', 'id_rubrique=' . intval($id_rubrique)); |
|
| 56 | - if (!$langue_parent) { |
|
| 57 | - $langue_parent = $GLOBALS['meta']['langue_site']; |
|
| 58 | - } |
|
| 59 | - $changer_lang = $langue_parent; |
|
| 60 | - $set['lang'] = $changer_lang; |
|
| 61 | - if (isset($set['langue_choisie'])) { |
|
| 62 | - $set['langue_choisie'] = 'non'; |
|
| 63 | - } |
|
| 64 | - sql_updateq($table_objet_sql, $set, "$id_table_objet=" . intval($id), [], $serveur); |
|
| 65 | - if ($table_objet_sql == 'spip_rubriques') { |
|
| 66 | - include_spip('inc/rubriques'); |
|
| 67 | - calculer_langues_rubriques(); |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - } |
|
| 45 | + if ($changer_lang != 'herit') { |
|
| 46 | + $set['lang'] = $changer_lang; |
|
| 47 | + sql_updateq($table_objet_sql, $set, "$id_table_objet=" . intval($id), [], $serveur); |
|
| 48 | + include_spip('inc/rubriques'); // pour calculer_langues_rubriques et calculer_langues_utilisees |
|
| 49 | + if ($table_objet_sql == 'spip_rubriques') { |
|
| 50 | + calculer_langues_rubriques(); |
|
| 51 | + } |
|
| 52 | + $langues = calculer_langues_utilisees($serveur); |
|
| 53 | + ecrire_meta('langues_utilisees', $langues); |
|
| 54 | + } else { |
|
| 55 | + $langue_parent = sql_getfetsel('lang', 'spip_rubriques', 'id_rubrique=' . intval($id_rubrique)); |
|
| 56 | + if (!$langue_parent) { |
|
| 57 | + $langue_parent = $GLOBALS['meta']['langue_site']; |
|
| 58 | + } |
|
| 59 | + $changer_lang = $langue_parent; |
|
| 60 | + $set['lang'] = $changer_lang; |
|
| 61 | + if (isset($set['langue_choisie'])) { |
|
| 62 | + $set['langue_choisie'] = 'non'; |
|
| 63 | + } |
|
| 64 | + sql_updateq($table_objet_sql, $set, "$id_table_objet=" . intval($id), [], $serveur); |
|
| 65 | + if ($table_objet_sql == 'spip_rubriques') { |
|
| 66 | + include_spip('inc/rubriques'); |
|
| 67 | + calculer_langues_rubriques(); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - return $changer_lang; |
|
| 72 | + return $changer_lang; |
|
| 73 | 73 | } |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | 27 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 28 | - return; |
|
| 28 | + return; |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | /** Version de l'API SQL */ |
@@ -45,39 +45,39 @@ discard block |
||
| 45 | 45 | * contexte de l'erreur |
| 46 | 46 | **/ |
| 47 | 47 | function sql_error_backtrace($compil_info = false) { |
| 48 | - $trace = debug_backtrace(); |
|
| 49 | - $caller = array_shift($trace); |
|
| 50 | - while (count($trace) and (empty($trace[0]['file']) or $trace[0]['file'] === $caller['file'] or $trace[0]['file'] === __FILE__)) { |
|
| 51 | - array_shift($trace); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - if ($compil_info) { |
|
| 55 | - $contexte_compil = [ |
|
| 56 | - $trace[0]['file'],// sourcefile |
|
| 57 | - '', //nom |
|
| 58 | - (isset($trace[1]) ? $trace[1]['function'] . "(){\n" : '') |
|
| 59 | - . $trace[0]['function'] . '();' |
|
| 60 | - . (isset($trace[1]) ? "\n}" : ''), //id_boucle |
|
| 61 | - $trace[0]['line'], // ligne |
|
| 62 | - $GLOBALS['spip_lang'], // lang |
|
| 63 | - ]; |
|
| 64 | - |
|
| 65 | - return $contexte_compil; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $message = count($trace) ? $trace[0]['file'] . ' L' . $trace[0]['line'] : ''; |
|
| 69 | - $f = []; |
|
| 70 | - while (count($trace) and $t = array_shift($trace)) { |
|
| 71 | - if (in_array($t['function'], ['include_once', 'include_spip', 'find_in_path'])) { |
|
| 72 | - break; |
|
| 73 | - } |
|
| 74 | - $f[] = $t['function']; |
|
| 75 | - } |
|
| 76 | - if (count($f)) { |
|
| 77 | - $message .= ' [' . implode('(),', $f) . '()]'; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - return $message; |
|
| 48 | + $trace = debug_backtrace(); |
|
| 49 | + $caller = array_shift($trace); |
|
| 50 | + while (count($trace) and (empty($trace[0]['file']) or $trace[0]['file'] === $caller['file'] or $trace[0]['file'] === __FILE__)) { |
|
| 51 | + array_shift($trace); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + if ($compil_info) { |
|
| 55 | + $contexte_compil = [ |
|
| 56 | + $trace[0]['file'],// sourcefile |
|
| 57 | + '', //nom |
|
| 58 | + (isset($trace[1]) ? $trace[1]['function'] . "(){\n" : '') |
|
| 59 | + . $trace[0]['function'] . '();' |
|
| 60 | + . (isset($trace[1]) ? "\n}" : ''), //id_boucle |
|
| 61 | + $trace[0]['line'], // ligne |
|
| 62 | + $GLOBALS['spip_lang'], // lang |
|
| 63 | + ]; |
|
| 64 | + |
|
| 65 | + return $contexte_compil; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $message = count($trace) ? $trace[0]['file'] . ' L' . $trace[0]['line'] : ''; |
|
| 69 | + $f = []; |
|
| 70 | + while (count($trace) and $t = array_shift($trace)) { |
|
| 71 | + if (in_array($t['function'], ['include_once', 'include_spip', 'find_in_path'])) { |
|
| 72 | + break; |
|
| 73 | + } |
|
| 74 | + $f[] = $t['function']; |
|
| 75 | + } |
|
| 76 | + if (count($f)) { |
|
| 77 | + $message .= ' [' . implode('(),', $f) . '()]'; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + return $message; |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | |
@@ -102,16 +102,16 @@ discard block |
||
| 102 | 102 | * |
| 103 | 103 | **/ |
| 104 | 104 | function sql_serveur($ins_sql = '', $serveur = '', $continue = false) { |
| 105 | - static $sql_serveur = []; |
|
| 106 | - if (!isset($sql_serveur[$serveur][$ins_sql])) { |
|
| 107 | - $f = spip_connect_sql(\SQL_ABSTRACT_VERSION, $ins_sql, $serveur, $continue); |
|
| 108 | - if (!is_string($f) or !$f) { |
|
| 109 | - return $f; |
|
| 110 | - } |
|
| 111 | - $sql_serveur[$serveur][$ins_sql] = $f; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return $sql_serveur[$serveur][$ins_sql]; |
|
| 105 | + static $sql_serveur = []; |
|
| 106 | + if (!isset($sql_serveur[$serveur][$ins_sql])) { |
|
| 107 | + $f = spip_connect_sql(\SQL_ABSTRACT_VERSION, $ins_sql, $serveur, $continue); |
|
| 108 | + if (!is_string($f) or !$f) { |
|
| 109 | + return $f; |
|
| 110 | + } |
|
| 111 | + $sql_serveur[$serveur][$ins_sql] = $f; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return $sql_serveur[$serveur][$ins_sql]; |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | /** |
@@ -133,23 +133,23 @@ discard block |
||
| 133 | 133 | * Retourne le nom du charset si effectivement trouvé, sinon false. |
| 134 | 134 | **/ |
| 135 | 135 | function sql_get_charset($charset, $serveur = '', $option = true) { |
| 136 | - // le nom http du charset differe parfois du nom SQL utf-8 ==> utf8 etc. |
|
| 137 | - $desc = sql_serveur('', $serveur, true); |
|
| 138 | - $desc = $desc[\SQL_ABSTRACT_VERSION]; |
|
| 139 | - $c = $desc['charsets'][$charset]; |
|
| 140 | - if ($c) { |
|
| 141 | - if (function_exists($f = @$desc['get_charset'])) { |
|
| 142 | - if ($f($c, $serveur, $option !== false)) { |
|
| 143 | - return $c; |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - spip_log( |
|
| 148 | - "SPIP ne connait pas les Charsets disponibles sur le serveur $serveur. Le serveur choisira seul.", |
|
| 149 | - _LOG_AVERTISSEMENT |
|
| 150 | - ); |
|
| 151 | - |
|
| 152 | - return false; |
|
| 136 | + // le nom http du charset differe parfois du nom SQL utf-8 ==> utf8 etc. |
|
| 137 | + $desc = sql_serveur('', $serveur, true); |
|
| 138 | + $desc = $desc[\SQL_ABSTRACT_VERSION]; |
|
| 139 | + $c = $desc['charsets'][$charset]; |
|
| 140 | + if ($c) { |
|
| 141 | + if (function_exists($f = @$desc['get_charset'])) { |
|
| 142 | + if ($f($c, $serveur, $option !== false)) { |
|
| 143 | + return $c; |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + spip_log( |
|
| 148 | + "SPIP ne connait pas les Charsets disponibles sur le serveur $serveur. Le serveur choisira seul.", |
|
| 149 | + _LOG_AVERTISSEMENT |
|
| 150 | + ); |
|
| 151 | + |
|
| 152 | + return false; |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | |
@@ -175,12 +175,12 @@ discard block |
||
| 175 | 175 | * Retourne true si elle reussie. |
| 176 | 176 | **/ |
| 177 | 177 | function sql_set_charset($charset, $serveur = '', $option = true) { |
| 178 | - $f = sql_serveur('set_charset', $serveur, $option === 'continue' or $option === false); |
|
| 179 | - if (!is_string($f) or !$f) { |
|
| 180 | - return false; |
|
| 181 | - } |
|
| 178 | + $f = sql_serveur('set_charset', $serveur, $option === 'continue' or $option === false); |
|
| 179 | + if (!is_string($f) or !$f) { |
|
| 180 | + return false; |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - return $f($charset, $serveur, $option !== false); |
|
| 183 | + return $f($charset, $serveur, $option !== false); |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | |
@@ -231,59 +231,59 @@ discard block |
||
| 231 | 231 | * |
| 232 | 232 | **/ |
| 233 | 233 | function sql_select( |
| 234 | - $select = [], |
|
| 235 | - $from = [], |
|
| 236 | - $where = [], |
|
| 237 | - $groupby = [], |
|
| 238 | - $orderby = [], |
|
| 239 | - $limit = '', |
|
| 240 | - $having = [], |
|
| 241 | - $serveur = '', |
|
| 242 | - $option = true |
|
| 234 | + $select = [], |
|
| 235 | + $from = [], |
|
| 236 | + $where = [], |
|
| 237 | + $groupby = [], |
|
| 238 | + $orderby = [], |
|
| 239 | + $limit = '', |
|
| 240 | + $having = [], |
|
| 241 | + $serveur = '', |
|
| 242 | + $option = true |
|
| 243 | 243 | ) { |
| 244 | - $f = sql_serveur('select', $serveur, $option === 'continue' or $option === false); |
|
| 245 | - if (!is_string($f) or !$f) { |
|
| 246 | - return false; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - $debug = (defined('_VAR_MODE') and _VAR_MODE == 'debug'); |
|
| 250 | - if (($option !== false) and !$debug) { |
|
| 251 | - $res = $f( |
|
| 252 | - $select, |
|
| 253 | - $from, |
|
| 254 | - $where, |
|
| 255 | - $groupby, |
|
| 256 | - $orderby, |
|
| 257 | - $limit, |
|
| 258 | - $having, |
|
| 259 | - $serveur, |
|
| 260 | - is_array($option) ? true : $option |
|
| 261 | - ); |
|
| 262 | - } else { |
|
| 263 | - $query = $f($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, false); |
|
| 264 | - if (!$option) { |
|
| 265 | - return $query; |
|
| 266 | - } |
|
| 267 | - // le debug, c'est pour ce qui a ete produit par le compilateur |
|
| 268 | - if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 269 | - [$table, $id, ] = $GLOBALS['debug']['aucasou']; |
|
| 270 | - $nom = $GLOBALS['debug_objets']['courant'] . $id; |
|
| 271 | - $GLOBALS['debug_objets']['requete'][$nom] = $query; |
|
| 272 | - } |
|
| 273 | - $res = $f($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, true); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - // en cas d'erreur |
|
| 277 | - if (!is_string($res)) { |
|
| 278 | - return $res; |
|
| 279 | - } |
|
| 280 | - // denoncer l'erreur SQL dans sa version brute |
|
| 281 | - spip_sql_erreur($serveur); |
|
| 282 | - // idem dans sa version squelette (prefixe des tables non substitue) |
|
| 283 | - $contexte_compil = sql_error_backtrace(true); |
|
| 284 | - erreur_squelette([sql_errno($serveur), sql_error($serveur), $res], $contexte_compil); |
|
| 285 | - |
|
| 286 | - return false; |
|
| 244 | + $f = sql_serveur('select', $serveur, $option === 'continue' or $option === false); |
|
| 245 | + if (!is_string($f) or !$f) { |
|
| 246 | + return false; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + $debug = (defined('_VAR_MODE') and _VAR_MODE == 'debug'); |
|
| 250 | + if (($option !== false) and !$debug) { |
|
| 251 | + $res = $f( |
|
| 252 | + $select, |
|
| 253 | + $from, |
|
| 254 | + $where, |
|
| 255 | + $groupby, |
|
| 256 | + $orderby, |
|
| 257 | + $limit, |
|
| 258 | + $having, |
|
| 259 | + $serveur, |
|
| 260 | + is_array($option) ? true : $option |
|
| 261 | + ); |
|
| 262 | + } else { |
|
| 263 | + $query = $f($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, false); |
|
| 264 | + if (!$option) { |
|
| 265 | + return $query; |
|
| 266 | + } |
|
| 267 | + // le debug, c'est pour ce qui a ete produit par le compilateur |
|
| 268 | + if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 269 | + [$table, $id, ] = $GLOBALS['debug']['aucasou']; |
|
| 270 | + $nom = $GLOBALS['debug_objets']['courant'] . $id; |
|
| 271 | + $GLOBALS['debug_objets']['requete'][$nom] = $query; |
|
| 272 | + } |
|
| 273 | + $res = $f($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, true); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + // en cas d'erreur |
|
| 277 | + if (!is_string($res)) { |
|
| 278 | + return $res; |
|
| 279 | + } |
|
| 280 | + // denoncer l'erreur SQL dans sa version brute |
|
| 281 | + spip_sql_erreur($serveur); |
|
| 282 | + // idem dans sa version squelette (prefixe des tables non substitue) |
|
| 283 | + $contexte_compil = sql_error_backtrace(true); |
|
| 284 | + erreur_squelette([sql_errno($serveur), sql_error($serveur), $res], $contexte_compil); |
|
| 285 | + |
|
| 286 | + return false; |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | |
@@ -320,16 +320,16 @@ discard block |
||
| 320 | 320 | * |
| 321 | 321 | **/ |
| 322 | 322 | function sql_get_select( |
| 323 | - $select = [], |
|
| 324 | - $from = [], |
|
| 325 | - $where = [], |
|
| 326 | - $groupby = [], |
|
| 327 | - $orderby = [], |
|
| 328 | - $limit = '', |
|
| 329 | - $having = [], |
|
| 330 | - $serveur = '' |
|
| 323 | + $select = [], |
|
| 324 | + $from = [], |
|
| 325 | + $where = [], |
|
| 326 | + $groupby = [], |
|
| 327 | + $orderby = [], |
|
| 328 | + $limit = '', |
|
| 329 | + $having = [], |
|
| 330 | + $serveur = '' |
|
| 331 | 331 | ) { |
| 332 | - return sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, false); |
|
| 332 | + return sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, false); |
|
| 333 | 333 | } |
| 334 | 334 | |
| 335 | 335 | |
@@ -373,23 +373,23 @@ discard block |
||
| 373 | 373 | * |
| 374 | 374 | **/ |
| 375 | 375 | function sql_countsel( |
| 376 | - $from = [], |
|
| 377 | - $where = [], |
|
| 378 | - $groupby = [], |
|
| 379 | - $having = [], |
|
| 380 | - $serveur = '', |
|
| 381 | - $option = true |
|
| 376 | + $from = [], |
|
| 377 | + $where = [], |
|
| 378 | + $groupby = [], |
|
| 379 | + $having = [], |
|
| 380 | + $serveur = '', |
|
| 381 | + $option = true |
|
| 382 | 382 | ) { |
| 383 | - $f = sql_serveur('countsel', $serveur, $option === 'continue' or $option === false); |
|
| 384 | - if (!is_string($f) or !$f) { |
|
| 385 | - return false; |
|
| 386 | - } |
|
| 387 | - $r = $f($from, $where, $groupby, $having, $serveur, $option !== false); |
|
| 388 | - if ($r === false) { |
|
| 389 | - spip_sql_erreur($serveur); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - return $r; |
|
| 383 | + $f = sql_serveur('countsel', $serveur, $option === 'continue' or $option === false); |
|
| 384 | + if (!is_string($f) or !$f) { |
|
| 385 | + return false; |
|
| 386 | + } |
|
| 387 | + $r = $f($from, $where, $groupby, $having, $serveur, $option !== false); |
|
| 388 | + if ($r === false) { |
|
| 389 | + spip_sql_erreur($serveur); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + return $r; |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | /** |
@@ -421,16 +421,16 @@ discard block |
||
| 421 | 421 | * Ce retour n'est pas pertinent pour savoir si l'opération est correctement réalisée. |
| 422 | 422 | **/ |
| 423 | 423 | function sql_alter($q, $serveur = '', $option = true) { |
| 424 | - $f = sql_serveur('alter', $serveur, $option === 'continue' or $option === false); |
|
| 425 | - if (!is_string($f) or !$f) { |
|
| 426 | - return false; |
|
| 427 | - } |
|
| 428 | - $r = $f($q, $serveur, $option !== false); |
|
| 429 | - if ($r === false) { |
|
| 430 | - spip_sql_erreur($serveur); |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - return $r; |
|
| 424 | + $f = sql_serveur('alter', $serveur, $option === 'continue' or $option === false); |
|
| 425 | + if (!is_string($f) or !$f) { |
|
| 426 | + return false; |
|
| 427 | + } |
|
| 428 | + $r = $f($q, $serveur, $option !== false); |
|
| 429 | + if ($r === false) { |
|
| 430 | + spip_sql_erreur($serveur); |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + return $r; |
|
| 434 | 434 | } |
| 435 | 435 | |
| 436 | 436 | /** |
@@ -453,12 +453,12 @@ discard block |
||
| 453 | 453 | * presentant une ligne de resultat d'une selection |
| 454 | 454 | */ |
| 455 | 455 | function sql_fetch($res, $serveur = '', $option = true) { |
| 456 | - $f = sql_serveur('fetch', $serveur, $option === 'continue' or $option === false); |
|
| 457 | - if (!is_string($f) or !$f) { |
|
| 458 | - return false; |
|
| 459 | - } |
|
| 456 | + $f = sql_serveur('fetch', $serveur, $option === 'continue' or $option === false); |
|
| 457 | + if (!is_string($f) or !$f) { |
|
| 458 | + return false; |
|
| 459 | + } |
|
| 460 | 460 | |
| 461 | - return $f($res, null, $serveur, $option !== false); |
|
| 461 | + return $f($res, null, $serveur, $option !== false); |
|
| 462 | 462 | } |
| 463 | 463 | |
| 464 | 464 | |
@@ -485,20 +485,20 @@ discard block |
||
| 485 | 485 | * presentant une ligne de resultat d'une selection |
| 486 | 486 | */ |
| 487 | 487 | function sql_fetch_all($res, $serveur = '', $option = true) { |
| 488 | - $rows = []; |
|
| 489 | - if (!$res) { |
|
| 490 | - return $rows; |
|
| 491 | - } |
|
| 492 | - $f = sql_serveur('fetch', $serveur, $option === 'continue' or $option === false); |
|
| 493 | - if (!is_string($f) or !$f) { |
|
| 494 | - return []; |
|
| 495 | - } |
|
| 496 | - while ($r = $f($res, null, $serveur, $option !== false)) { |
|
| 497 | - $rows[] = $r; |
|
| 498 | - } |
|
| 499 | - sql_free($res, $serveur); |
|
| 500 | - |
|
| 501 | - return $rows; |
|
| 488 | + $rows = []; |
|
| 489 | + if (!$res) { |
|
| 490 | + return $rows; |
|
| 491 | + } |
|
| 492 | + $f = sql_serveur('fetch', $serveur, $option === 'continue' or $option === false); |
|
| 493 | + if (!is_string($f) or !$f) { |
|
| 494 | + return []; |
|
| 495 | + } |
|
| 496 | + while ($r = $f($res, null, $serveur, $option !== false)) { |
|
| 497 | + $rows[] = $r; |
|
| 498 | + } |
|
| 499 | + sql_free($res, $serveur); |
|
| 500 | + |
|
| 501 | + return $rows; |
|
| 502 | 502 | } |
| 503 | 503 | |
| 504 | 504 | /** |
@@ -526,16 +526,16 @@ discard block |
||
| 526 | 526 | * Operation effectuée (true), sinon false. |
| 527 | 527 | **/ |
| 528 | 528 | function sql_seek($res, $row_number, $serveur = '', $option = true) { |
| 529 | - $f = sql_serveur('seek', $serveur, $option === 'continue' or $option === false); |
|
| 530 | - if (!is_string($f) or !$f) { |
|
| 531 | - return false; |
|
| 532 | - } |
|
| 533 | - $r = $f($res, $row_number, $serveur, $option !== false); |
|
| 534 | - if ($r === false) { |
|
| 535 | - spip_sql_erreur($serveur); |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - return $r; |
|
| 529 | + $f = sql_serveur('seek', $serveur, $option === 'continue' or $option === false); |
|
| 530 | + if (!is_string($f) or !$f) { |
|
| 531 | + return false; |
|
| 532 | + } |
|
| 533 | + $r = $f($res, $row_number, $serveur, $option !== false); |
|
| 534 | + if ($r === false) { |
|
| 535 | + spip_sql_erreur($serveur); |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + return $r; |
|
| 539 | 539 | } |
| 540 | 540 | |
| 541 | 541 | |
@@ -560,16 +560,16 @@ discard block |
||
| 560 | 560 | * False en cas d'erreur. |
| 561 | 561 | **/ |
| 562 | 562 | function sql_listdbs($serveur = '', $option = true) { |
| 563 | - $f = sql_serveur('listdbs', $serveur, $option === 'continue' or $option === false); |
|
| 564 | - if (!is_string($f) or !$f) { |
|
| 565 | - return false; |
|
| 566 | - } |
|
| 567 | - $r = $f($serveur); |
|
| 568 | - if ($r === false) { |
|
| 569 | - spip_sql_erreur($serveur); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - return $r; |
|
| 563 | + $f = sql_serveur('listdbs', $serveur, $option === 'continue' or $option === false); |
|
| 564 | + if (!is_string($f) or !$f) { |
|
| 565 | + return false; |
|
| 566 | + } |
|
| 567 | + $r = $f($serveur); |
|
| 568 | + if ($r === false) { |
|
| 569 | + spip_sql_erreur($serveur); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + return $r; |
|
| 573 | 573 | } |
| 574 | 574 | |
| 575 | 575 | |
@@ -592,16 +592,16 @@ discard block |
||
| 592 | 592 | * - False en cas d'erreur. |
| 593 | 593 | **/ |
| 594 | 594 | function sql_selectdb($nom, $serveur = '', $option = true) { |
| 595 | - $f = sql_serveur('selectdb', $serveur, $option === 'continue' or $option === false); |
|
| 596 | - if (!is_string($f) or !$f) { |
|
| 597 | - return false; |
|
| 598 | - } |
|
| 599 | - $r = $f($nom, $serveur, $option !== false); |
|
| 600 | - if ($r === false) { |
|
| 601 | - spip_sql_erreur($serveur); |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - return $r; |
|
| 595 | + $f = sql_serveur('selectdb', $serveur, $option === 'continue' or $option === false); |
|
| 596 | + if (!is_string($f) or !$f) { |
|
| 597 | + return false; |
|
| 598 | + } |
|
| 599 | + $r = $f($nom, $serveur, $option !== false); |
|
| 600 | + if ($r === false) { |
|
| 601 | + spip_sql_erreur($serveur); |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + return $r; |
|
| 605 | 605 | } |
| 606 | 606 | |
| 607 | 607 | /** |
@@ -626,16 +626,16 @@ discard block |
||
| 626 | 626 | * - false en cas d'erreur. |
| 627 | 627 | **/ |
| 628 | 628 | function sql_count($res, $serveur = '', $option = true) { |
| 629 | - $f = sql_serveur('count', $serveur, $option === 'continue' or $option === false); |
|
| 630 | - if (!is_string($f) or !$f) { |
|
| 631 | - return false; |
|
| 632 | - } |
|
| 633 | - $r = $f($res, $serveur, $option !== false); |
|
| 634 | - if ($r === false) { |
|
| 635 | - spip_sql_erreur($serveur); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - return $r; |
|
| 629 | + $f = sql_serveur('count', $serveur, $option === 'continue' or $option === false); |
|
| 630 | + if (!is_string($f) or !$f) { |
|
| 631 | + return false; |
|
| 632 | + } |
|
| 633 | + $r = $f($res, $serveur, $option !== false); |
|
| 634 | + if ($r === false) { |
|
| 635 | + spip_sql_erreur($serveur); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + return $r; |
|
| 639 | 639 | } |
| 640 | 640 | |
| 641 | 641 | /** |
@@ -657,12 +657,12 @@ discard block |
||
| 657 | 657 | * True si réussi |
| 658 | 658 | */ |
| 659 | 659 | function sql_free($res, $serveur = '', $option = true) { |
| 660 | - $f = sql_serveur('free', $serveur, $option === 'continue' or $option === false); |
|
| 661 | - if (!is_string($f) or !$f) { |
|
| 662 | - return false; |
|
| 663 | - } |
|
| 660 | + $f = sql_serveur('free', $serveur, $option === 'continue' or $option === false); |
|
| 661 | + if (!is_string($f) or !$f) { |
|
| 662 | + return false; |
|
| 663 | + } |
|
| 664 | 664 | |
| 665 | - return $f($res); |
|
| 665 | + return $f($res); |
|
| 666 | 666 | } |
| 667 | 667 | |
| 668 | 668 | |
@@ -700,17 +700,17 @@ discard block |
||
| 700 | 700 | * - False en cas d'erreur. |
| 701 | 701 | **/ |
| 702 | 702 | function sql_insert($table, $noms, $valeurs, $desc = [], $serveur = '', $option = true) { |
| 703 | - $f = sql_serveur('insert', $serveur, $option === 'continue' or $option === false); |
|
| 704 | - if (!is_string($f) or !$f) { |
|
| 705 | - return false; |
|
| 706 | - } |
|
| 707 | - $r = $f($table, $noms, $valeurs, $desc, $serveur, $option !== false); |
|
| 708 | - if ($r === false or $r === null) { |
|
| 709 | - spip_sql_erreur($serveur); |
|
| 710 | - $r = false; |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - return $r; |
|
| 703 | + $f = sql_serveur('insert', $serveur, $option === 'continue' or $option === false); |
|
| 704 | + if (!is_string($f) or !$f) { |
|
| 705 | + return false; |
|
| 706 | + } |
|
| 707 | + $r = $f($table, $noms, $valeurs, $desc, $serveur, $option !== false); |
|
| 708 | + if ($r === false or $r === null) { |
|
| 709 | + spip_sql_erreur($serveur); |
|
| 710 | + $r = false; |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + return $r; |
|
| 714 | 714 | } |
| 715 | 715 | |
| 716 | 716 | /** |
@@ -751,17 +751,17 @@ discard block |
||
| 751 | 751 | * - False en cas d'erreur. |
| 752 | 752 | **/ |
| 753 | 753 | function sql_insertq($table, $couples = [], $desc = [], $serveur = '', $option = true) { |
| 754 | - $f = sql_serveur('insertq', $serveur, $option === 'continue' or $option === false); |
|
| 755 | - if (!is_string($f) or !$f) { |
|
| 756 | - return false; |
|
| 757 | - } |
|
| 758 | - $r = $f($table, $couples, $desc, $serveur, $option !== false); |
|
| 759 | - if ($r === false or $r === null) { |
|
| 760 | - spip_sql_erreur($serveur); |
|
| 761 | - $r = false; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - return $r; |
|
| 754 | + $f = sql_serveur('insertq', $serveur, $option === 'continue' or $option === false); |
|
| 755 | + if (!is_string($f) or !$f) { |
|
| 756 | + return false; |
|
| 757 | + } |
|
| 758 | + $r = $f($table, $couples, $desc, $serveur, $option !== false); |
|
| 759 | + if ($r === false or $r === null) { |
|
| 760 | + spip_sql_erreur($serveur); |
|
| 761 | + $r = false; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + return $r; |
|
| 765 | 765 | } |
| 766 | 766 | |
| 767 | 767 | /** |
@@ -796,17 +796,17 @@ discard block |
||
| 796 | 796 | * - False en cas d'erreur. |
| 797 | 797 | **/ |
| 798 | 798 | function sql_insertq_multi($table, $couples = [], $desc = [], $serveur = '', $option = true) { |
| 799 | - $f = sql_serveur('insertq_multi', $serveur, $option === 'continue' or $option === false); |
|
| 800 | - if (!is_string($f) or !$f) { |
|
| 801 | - return false; |
|
| 802 | - } |
|
| 803 | - $r = $f($table, $couples, $desc, $serveur, $option !== false); |
|
| 804 | - if ($r === false or $r === null) { |
|
| 805 | - spip_sql_erreur($serveur); |
|
| 806 | - $r = false; |
|
| 807 | - } |
|
| 808 | - |
|
| 809 | - return $r; |
|
| 799 | + $f = sql_serveur('insertq_multi', $serveur, $option === 'continue' or $option === false); |
|
| 800 | + if (!is_string($f) or !$f) { |
|
| 801 | + return false; |
|
| 802 | + } |
|
| 803 | + $r = $f($table, $couples, $desc, $serveur, $option !== false); |
|
| 804 | + if ($r === false or $r === null) { |
|
| 805 | + spip_sql_erreur($serveur); |
|
| 806 | + $r = false; |
|
| 807 | + } |
|
| 808 | + |
|
| 809 | + return $r; |
|
| 810 | 810 | } |
| 811 | 811 | |
| 812 | 812 | /** |
@@ -846,16 +846,16 @@ discard block |
||
| 846 | 846 | * - array Tableau décrivant la requête et son temps d'exécution si var_profile est actif |
| 847 | 847 | */ |
| 848 | 848 | function sql_update($table, $exp, $where = '', $desc = [], $serveur = '', $option = true) { |
| 849 | - $f = sql_serveur('update', $serveur, $option === 'continue' or $option === false); |
|
| 850 | - if (!is_string($f) or !$f) { |
|
| 851 | - return false; |
|
| 852 | - } |
|
| 853 | - $r = $f($table, $exp, $where, $desc, $serveur, $option !== false); |
|
| 854 | - if ($r === false) { |
|
| 855 | - spip_sql_erreur($serveur); |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - return $r; |
|
| 849 | + $f = sql_serveur('update', $serveur, $option === 'continue' or $option === false); |
|
| 850 | + if (!is_string($f) or !$f) { |
|
| 851 | + return false; |
|
| 852 | + } |
|
| 853 | + $r = $f($table, $exp, $where, $desc, $serveur, $option !== false); |
|
| 854 | + if ($r === false) { |
|
| 855 | + spip_sql_erreur($serveur); |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + return $r; |
|
| 859 | 859 | } |
| 860 | 860 | |
| 861 | 861 | |
@@ -901,16 +901,16 @@ discard block |
||
| 901 | 901 | * - False en cas d'erreur. |
| 902 | 902 | **/ |
| 903 | 903 | function sql_updateq($table, $exp, $where = '', $desc = [], $serveur = '', $option = true) { |
| 904 | - $f = sql_serveur('updateq', $serveur, $option === 'continue' or $option === false); |
|
| 905 | - if (!is_string($f) or !$f) { |
|
| 906 | - return false; |
|
| 907 | - } |
|
| 908 | - $r = $f($table, $exp, $where, $desc, $serveur, $option !== false); |
|
| 909 | - if ($r === false) { |
|
| 910 | - spip_sql_erreur($serveur); |
|
| 911 | - } |
|
| 912 | - |
|
| 913 | - return $r; |
|
| 904 | + $f = sql_serveur('updateq', $serveur, $option === 'continue' or $option === false); |
|
| 905 | + if (!is_string($f) or !$f) { |
|
| 906 | + return false; |
|
| 907 | + } |
|
| 908 | + $r = $f($table, $exp, $where, $desc, $serveur, $option !== false); |
|
| 909 | + if ($r === false) { |
|
| 910 | + spip_sql_erreur($serveur); |
|
| 911 | + } |
|
| 912 | + |
|
| 913 | + return $r; |
|
| 914 | 914 | } |
| 915 | 915 | |
| 916 | 916 | /** |
@@ -941,16 +941,16 @@ discard block |
||
| 941 | 941 | * - False en cas d'erreur. |
| 942 | 942 | **/ |
| 943 | 943 | function sql_delete($table, $where = '', $serveur = '', $option = true) { |
| 944 | - $f = sql_serveur('delete', $serveur, $option === 'continue' or $option === false); |
|
| 945 | - if (!is_string($f) or !$f) { |
|
| 946 | - return false; |
|
| 947 | - } |
|
| 948 | - $r = $f($table, $where, $serveur, $option !== false); |
|
| 949 | - if ($r === false) { |
|
| 950 | - spip_sql_erreur($serveur); |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - return $r; |
|
| 944 | + $f = sql_serveur('delete', $serveur, $option === 'continue' or $option === false); |
|
| 945 | + if (!is_string($f) or !$f) { |
|
| 946 | + return false; |
|
| 947 | + } |
|
| 948 | + $r = $f($table, $where, $serveur, $option !== false); |
|
| 949 | + if ($r === false) { |
|
| 950 | + spip_sql_erreur($serveur); |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + return $r; |
|
| 954 | 954 | } |
| 955 | 955 | |
| 956 | 956 | /** |
@@ -986,16 +986,16 @@ discard block |
||
| 986 | 986 | * - False en cas d'erreur. |
| 987 | 987 | **/ |
| 988 | 988 | function sql_replace($table, $couples, $desc = [], $serveur = '', $option = true) { |
| 989 | - $f = sql_serveur('replace', $serveur, $option === 'continue' or $option === false); |
|
| 990 | - if (!is_string($f) or !$f) { |
|
| 991 | - return false; |
|
| 992 | - } |
|
| 993 | - $r = $f($table, $couples, $desc, $serveur, $option !== false); |
|
| 994 | - if ($r === false) { |
|
| 995 | - spip_sql_erreur($serveur); |
|
| 996 | - } |
|
| 997 | - |
|
| 998 | - return $r; |
|
| 989 | + $f = sql_serveur('replace', $serveur, $option === 'continue' or $option === false); |
|
| 990 | + if (!is_string($f) or !$f) { |
|
| 991 | + return false; |
|
| 992 | + } |
|
| 993 | + $r = $f($table, $couples, $desc, $serveur, $option !== false); |
|
| 994 | + if ($r === false) { |
|
| 995 | + spip_sql_erreur($serveur); |
|
| 996 | + } |
|
| 997 | + |
|
| 998 | + return $r; |
|
| 999 | 999 | } |
| 1000 | 1000 | |
| 1001 | 1001 | |
@@ -1033,16 +1033,16 @@ discard block |
||
| 1033 | 1033 | * - False en cas d'erreur. |
| 1034 | 1034 | **/ |
| 1035 | 1035 | function sql_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $option = true) { |
| 1036 | - $f = sql_serveur('replace_multi', $serveur, $option === 'continue' or $option === false); |
|
| 1037 | - if (!is_string($f) or !$f) { |
|
| 1038 | - return false; |
|
| 1039 | - } |
|
| 1040 | - $r = $f($table, $tab_couples, $desc, $serveur, $option !== false); |
|
| 1041 | - if ($r === false) { |
|
| 1042 | - spip_sql_erreur($serveur); |
|
| 1043 | - } |
|
| 1044 | - |
|
| 1045 | - return $r; |
|
| 1036 | + $f = sql_serveur('replace_multi', $serveur, $option === 'continue' or $option === false); |
|
| 1037 | + if (!is_string($f) or !$f) { |
|
| 1038 | + return false; |
|
| 1039 | + } |
|
| 1040 | + $r = $f($table, $tab_couples, $desc, $serveur, $option !== false); |
|
| 1041 | + if ($r === false) { |
|
| 1042 | + spip_sql_erreur($serveur); |
|
| 1043 | + } |
|
| 1044 | + |
|
| 1045 | + return $r; |
|
| 1046 | 1046 | } |
| 1047 | 1047 | |
| 1048 | 1048 | /** |
@@ -1070,16 +1070,16 @@ discard block |
||
| 1070 | 1070 | * - False en cas d'erreur. |
| 1071 | 1071 | **/ |
| 1072 | 1072 | function sql_drop_table($table, $exist = '', $serveur = '', $option = true) { |
| 1073 | - $f = sql_serveur('drop_table', $serveur, $option === 'continue' or $option === false); |
|
| 1074 | - if (!is_string($f) or !$f) { |
|
| 1075 | - return false; |
|
| 1076 | - } |
|
| 1077 | - $r = $f($table, $exist, $serveur, $option !== false); |
|
| 1078 | - if ($r === false) { |
|
| 1079 | - spip_sql_erreur($serveur); |
|
| 1080 | - } |
|
| 1081 | - |
|
| 1082 | - return $r; |
|
| 1073 | + $f = sql_serveur('drop_table', $serveur, $option === 'continue' or $option === false); |
|
| 1074 | + if (!is_string($f) or !$f) { |
|
| 1075 | + return false; |
|
| 1076 | + } |
|
| 1077 | + $r = $f($table, $exist, $serveur, $option !== false); |
|
| 1078 | + if ($r === false) { |
|
| 1079 | + spip_sql_erreur($serveur); |
|
| 1080 | + } |
|
| 1081 | + |
|
| 1082 | + return $r; |
|
| 1083 | 1083 | } |
| 1084 | 1084 | |
| 1085 | 1085 | /** |
@@ -1103,16 +1103,16 @@ discard block |
||
| 1103 | 1103 | * - true si la requête a réussie, false sinon |
| 1104 | 1104 | */ |
| 1105 | 1105 | function sql_drop_view($table, $exist = '', $serveur = '', $option = true) { |
| 1106 | - $f = sql_serveur('drop_view', $serveur, $option === 'continue' or $option === false); |
|
| 1107 | - if (!is_string($f) or !$f) { |
|
| 1108 | - return false; |
|
| 1109 | - } |
|
| 1110 | - $r = $f($table, $exist, $serveur, $option !== false); |
|
| 1111 | - if ($r === false) { |
|
| 1112 | - spip_sql_erreur($serveur); |
|
| 1113 | - } |
|
| 1114 | - |
|
| 1115 | - return $r; |
|
| 1106 | + $f = sql_serveur('drop_view', $serveur, $option === 'continue' or $option === false); |
|
| 1107 | + if (!is_string($f) or !$f) { |
|
| 1108 | + return false; |
|
| 1109 | + } |
|
| 1110 | + $r = $f($table, $exist, $serveur, $option !== false); |
|
| 1111 | + if ($r === false) { |
|
| 1112 | + spip_sql_erreur($serveur); |
|
| 1113 | + } |
|
| 1114 | + |
|
| 1115 | + return $r; |
|
| 1116 | 1116 | } |
| 1117 | 1117 | |
| 1118 | 1118 | /** |
@@ -1136,18 +1136,18 @@ discard block |
||
| 1136 | 1136 | * Ressource à utiliser avec sql_fetch() |
| 1137 | 1137 | **/ |
| 1138 | 1138 | function sql_showbase($spip = null, $serveur = '', $option = true) { |
| 1139 | - $f = sql_serveur('showbase', $serveur, $option === 'continue' or $option === false); |
|
| 1140 | - if (!is_string($f) or !$f) { |
|
| 1141 | - return false; |
|
| 1142 | - } |
|
| 1143 | - |
|
| 1144 | - // la globale n'est remplie qu'apres l'appel de sql_serveur. |
|
| 1145 | - if ($spip == null) { |
|
| 1146 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1147 | - $spip = $connexion['prefixe'] . '\_%'; |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - return $f($spip, $serveur, $option !== false); |
|
| 1139 | + $f = sql_serveur('showbase', $serveur, $option === 'continue' or $option === false); |
|
| 1140 | + if (!is_string($f) or !$f) { |
|
| 1141 | + return false; |
|
| 1142 | + } |
|
| 1143 | + |
|
| 1144 | + // la globale n'est remplie qu'apres l'appel de sql_serveur. |
|
| 1145 | + if ($spip == null) { |
|
| 1146 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1147 | + $spip = $connexion['prefixe'] . '\_%'; |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + return $f($spip, $serveur, $option !== false); |
|
| 1151 | 1151 | } |
| 1152 | 1152 | |
| 1153 | 1153 | /** |
@@ -1170,15 +1170,15 @@ discard block |
||
| 1170 | 1170 | * Liste des tables SQL |
| 1171 | 1171 | **/ |
| 1172 | 1172 | function sql_alltable($spip = null, $serveur = '', $option = true) { |
| 1173 | - $q = sql_showbase($spip, $serveur, $option); |
|
| 1174 | - $r = []; |
|
| 1175 | - if ($q) { |
|
| 1176 | - while ($t = sql_fetch($q, $serveur)) { |
|
| 1177 | - $r[] = array_shift($t); |
|
| 1178 | - } |
|
| 1179 | - } |
|
| 1180 | - |
|
| 1181 | - return $r; |
|
| 1173 | + $q = sql_showbase($spip, $serveur, $option); |
|
| 1174 | + $r = []; |
|
| 1175 | + if ($q) { |
|
| 1176 | + while ($t = sql_fetch($q, $serveur)) { |
|
| 1177 | + $r[] = array_shift($t); |
|
| 1178 | + } |
|
| 1179 | + } |
|
| 1180 | + |
|
| 1181 | + return $r; |
|
| 1182 | 1182 | } |
| 1183 | 1183 | |
| 1184 | 1184 | /** |
@@ -1211,31 +1211,31 @@ discard block |
||
| 1211 | 1211 | * - 'join' => array() // jointures, si déclarées. |
| 1212 | 1212 | **/ |
| 1213 | 1213 | function sql_showtable($table, $table_spip = false, $serveur = '', $option = true) { |
| 1214 | - $f = sql_serveur('showtable', $serveur, $option === 'continue' or $option === false); |
|
| 1215 | - if (!is_string($f) or !$f) { |
|
| 1216 | - return false; |
|
| 1217 | - } |
|
| 1218 | - |
|
| 1219 | - // la globale n'est remplie qu'apres l'appel de sql_serveur. |
|
| 1220 | - if ($table_spip) { |
|
| 1221 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1222 | - $prefixe = $connexion['prefixe']; |
|
| 1223 | - $vraie_table = prefixer_table_spip($table, $prefixe); |
|
| 1224 | - } else { |
|
| 1225 | - $vraie_table = $table; |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - $f = $f($vraie_table, $serveur, $option !== false); |
|
| 1229 | - if (!$f) { |
|
| 1230 | - return []; |
|
| 1231 | - } |
|
| 1232 | - if (isset($GLOBALS['tables_principales'][$table]['join'])) { |
|
| 1233 | - $f['join'] = $GLOBALS['tables_principales'][$table]['join']; |
|
| 1234 | - } elseif (isset($GLOBALS['tables_auxiliaires'][$table]['join'])) { |
|
| 1235 | - $f['join'] = $GLOBALS['tables_auxiliaires'][$table]['join']; |
|
| 1236 | - } |
|
| 1237 | - |
|
| 1238 | - return $f; |
|
| 1214 | + $f = sql_serveur('showtable', $serveur, $option === 'continue' or $option === false); |
|
| 1215 | + if (!is_string($f) or !$f) { |
|
| 1216 | + return false; |
|
| 1217 | + } |
|
| 1218 | + |
|
| 1219 | + // la globale n'est remplie qu'apres l'appel de sql_serveur. |
|
| 1220 | + if ($table_spip) { |
|
| 1221 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1222 | + $prefixe = $connexion['prefixe']; |
|
| 1223 | + $vraie_table = prefixer_table_spip($table, $prefixe); |
|
| 1224 | + } else { |
|
| 1225 | + $vraie_table = $table; |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + $f = $f($vraie_table, $serveur, $option !== false); |
|
| 1229 | + if (!$f) { |
|
| 1230 | + return []; |
|
| 1231 | + } |
|
| 1232 | + if (isset($GLOBALS['tables_principales'][$table]['join'])) { |
|
| 1233 | + $f['join'] = $GLOBALS['tables_principales'][$table]['join']; |
|
| 1234 | + } elseif (isset($GLOBALS['tables_auxiliaires'][$table]['join'])) { |
|
| 1235 | + $f['join'] = $GLOBALS['tables_auxiliaires'][$table]['join']; |
|
| 1236 | + } |
|
| 1237 | + |
|
| 1238 | + return $f; |
|
| 1239 | 1239 | } |
| 1240 | 1240 | |
| 1241 | 1241 | |
@@ -1263,21 +1263,21 @@ discard block |
||
| 1263 | 1263 | * - False en cas d'erreur. |
| 1264 | 1264 | **/ |
| 1265 | 1265 | function sql_table_exists(string $table, bool $table_spip = true, $serveur = '', $option = true) { |
| 1266 | - $f = sql_serveur('table_exists', $serveur, $option === 'continue' or $option === false); |
|
| 1267 | - if (!is_string($f) or !$f) { |
|
| 1268 | - return false; |
|
| 1269 | - } |
|
| 1270 | - |
|
| 1271 | - // la globale n'est remplie qu'apres l'appel de sql_serveur. |
|
| 1272 | - if ($table_spip) { |
|
| 1273 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1274 | - $prefixe = $connexion['prefixe']; |
|
| 1275 | - $vraie_table = prefixer_table_spip($table, $prefixe); |
|
| 1276 | - } else { |
|
| 1277 | - $vraie_table = $table; |
|
| 1278 | - } |
|
| 1279 | - |
|
| 1280 | - return $f($vraie_table, $serveur, $option !== false); |
|
| 1266 | + $f = sql_serveur('table_exists', $serveur, $option === 'continue' or $option === false); |
|
| 1267 | + if (!is_string($f) or !$f) { |
|
| 1268 | + return false; |
|
| 1269 | + } |
|
| 1270 | + |
|
| 1271 | + // la globale n'est remplie qu'apres l'appel de sql_serveur. |
|
| 1272 | + if ($table_spip) { |
|
| 1273 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1274 | + $prefixe = $connexion['prefixe']; |
|
| 1275 | + $vraie_table = prefixer_table_spip($table, $prefixe); |
|
| 1276 | + } else { |
|
| 1277 | + $vraie_table = $table; |
|
| 1278 | + } |
|
| 1279 | + |
|
| 1280 | + return $f($vraie_table, $serveur, $option !== false); |
|
| 1281 | 1281 | } |
| 1282 | 1282 | |
| 1283 | 1283 | |
@@ -1323,24 +1323,24 @@ discard block |
||
| 1323 | 1323 | * true si succès, false en cas d'echec |
| 1324 | 1324 | **/ |
| 1325 | 1325 | function sql_create( |
| 1326 | - $nom, |
|
| 1327 | - $champs, |
|
| 1328 | - $cles = [], |
|
| 1329 | - $autoinc = false, |
|
| 1330 | - $temporary = false, |
|
| 1331 | - $serveur = '', |
|
| 1332 | - $option = true |
|
| 1326 | + $nom, |
|
| 1327 | + $champs, |
|
| 1328 | + $cles = [], |
|
| 1329 | + $autoinc = false, |
|
| 1330 | + $temporary = false, |
|
| 1331 | + $serveur = '', |
|
| 1332 | + $option = true |
|
| 1333 | 1333 | ) { |
| 1334 | - $f = sql_serveur('create', $serveur, $option === 'continue' or $option === false); |
|
| 1335 | - if (!is_string($f) or !$f) { |
|
| 1336 | - return false; |
|
| 1337 | - } |
|
| 1338 | - $r = $f($nom, $champs, $cles, $autoinc, $temporary, $serveur, $option !== false); |
|
| 1339 | - if ($r === false) { |
|
| 1340 | - spip_sql_erreur($serveur); |
|
| 1341 | - } |
|
| 1342 | - |
|
| 1343 | - return $r; |
|
| 1334 | + $f = sql_serveur('create', $serveur, $option === 'continue' or $option === false); |
|
| 1335 | + if (!is_string($f) or !$f) { |
|
| 1336 | + return false; |
|
| 1337 | + } |
|
| 1338 | + $r = $f($nom, $champs, $cles, $autoinc, $temporary, $serveur, $option !== false); |
|
| 1339 | + if ($r === false) { |
|
| 1340 | + spip_sql_erreur($serveur); |
|
| 1341 | + } |
|
| 1342 | + |
|
| 1343 | + return $r; |
|
| 1344 | 1344 | } |
| 1345 | 1345 | |
| 1346 | 1346 | /** |
@@ -1358,16 +1358,16 @@ discard block |
||
| 1358 | 1358 | * @return bool true si la base est créee. |
| 1359 | 1359 | **/ |
| 1360 | 1360 | function sql_create_base($nom, $serveur = '', $option = true) { |
| 1361 | - $f = sql_serveur('create_base', $serveur, $option === 'continue' or $option === false); |
|
| 1362 | - if (!is_string($f) or !$f) { |
|
| 1363 | - return false; |
|
| 1364 | - } |
|
| 1365 | - $r = $f($nom, $serveur, $option !== false); |
|
| 1366 | - if ($r === false) { |
|
| 1367 | - spip_sql_erreur($serveur); |
|
| 1368 | - } |
|
| 1369 | - |
|
| 1370 | - return $r; |
|
| 1361 | + $f = sql_serveur('create_base', $serveur, $option === 'continue' or $option === false); |
|
| 1362 | + if (!is_string($f) or !$f) { |
|
| 1363 | + return false; |
|
| 1364 | + } |
|
| 1365 | + $r = $f($nom, $serveur, $option !== false); |
|
| 1366 | + if ($r === false) { |
|
| 1367 | + spip_sql_erreur($serveur); |
|
| 1368 | + } |
|
| 1369 | + |
|
| 1370 | + return $r; |
|
| 1371 | 1371 | } |
| 1372 | 1372 | |
| 1373 | 1373 | |
@@ -1397,16 +1397,16 @@ discard block |
||
| 1397 | 1397 | * - false en cas d'échec. |
| 1398 | 1398 | **/ |
| 1399 | 1399 | function sql_create_view($nom, $select_query, $serveur = '', $option = true) { |
| 1400 | - $f = sql_serveur('create_view', $serveur, $option === 'continue' or $option === false); |
|
| 1401 | - if (!is_string($f) or !$f) { |
|
| 1402 | - return false; |
|
| 1403 | - } |
|
| 1404 | - $r = $f($nom, $select_query, $serveur, $option !== false); |
|
| 1405 | - if ($r === false) { |
|
| 1406 | - spip_sql_erreur($serveur); |
|
| 1407 | - } |
|
| 1408 | - |
|
| 1409 | - return $r; |
|
| 1400 | + $f = sql_serveur('create_view', $serveur, $option === 'continue' or $option === false); |
|
| 1401 | + if (!is_string($f) or !$f) { |
|
| 1402 | + return false; |
|
| 1403 | + } |
|
| 1404 | + $r = $f($nom, $select_query, $serveur, $option !== false); |
|
| 1405 | + if ($r === false) { |
|
| 1406 | + spip_sql_erreur($serveur); |
|
| 1407 | + } |
|
| 1408 | + |
|
| 1409 | + return $r; |
|
| 1410 | 1410 | } |
| 1411 | 1411 | |
| 1412 | 1412 | /** |
@@ -1435,12 +1435,12 @@ discard block |
||
| 1435 | 1435 | * Texte de sélection pour la requête |
| 1436 | 1436 | */ |
| 1437 | 1437 | function sql_multi($sel, $lang, $serveur = '', $option = true) { |
| 1438 | - $f = sql_serveur('multi', $serveur, $option === 'continue' or $option === false); |
|
| 1439 | - if (!is_string($f) or !$f) { |
|
| 1440 | - return false; |
|
| 1441 | - } |
|
| 1438 | + $f = sql_serveur('multi', $serveur, $option === 'continue' or $option === false); |
|
| 1439 | + if (!is_string($f) or !$f) { |
|
| 1440 | + return false; |
|
| 1441 | + } |
|
| 1442 | 1442 | |
| 1443 | - return $f($sel, $lang); |
|
| 1443 | + return $f($sel, $lang); |
|
| 1444 | 1444 | } |
| 1445 | 1445 | |
| 1446 | 1446 | |
@@ -1455,12 +1455,12 @@ discard block |
||
| 1455 | 1455 | * False si le serveur est indisponible |
| 1456 | 1456 | */ |
| 1457 | 1457 | function sql_error($serveur = '') { |
| 1458 | - $f = sql_serveur('error', $serveur, true); |
|
| 1459 | - if (!is_string($f) or !$f) { |
|
| 1460 | - return false; |
|
| 1461 | - } |
|
| 1458 | + $f = sql_serveur('error', $serveur, true); |
|
| 1459 | + if (!is_string($f) or !$f) { |
|
| 1460 | + return false; |
|
| 1461 | + } |
|
| 1462 | 1462 | |
| 1463 | - return $f('query inconnue', $serveur); |
|
| 1463 | + return $f('query inconnue', $serveur); |
|
| 1464 | 1464 | } |
| 1465 | 1465 | |
| 1466 | 1466 | /** |
@@ -1474,12 +1474,12 @@ discard block |
||
| 1474 | 1474 | * False si le serveur est indisponible |
| 1475 | 1475 | */ |
| 1476 | 1476 | function sql_errno($serveur = '') { |
| 1477 | - $f = sql_serveur('errno', $serveur, true); |
|
| 1478 | - if (!is_string($f) or !$f) { |
|
| 1479 | - return false; |
|
| 1480 | - } |
|
| 1477 | + $f = sql_serveur('errno', $serveur, true); |
|
| 1478 | + if (!is_string($f) or !$f) { |
|
| 1479 | + return false; |
|
| 1480 | + } |
|
| 1481 | 1481 | |
| 1482 | - return $f($serveur); |
|
| 1482 | + return $f($serveur); |
|
| 1483 | 1483 | } |
| 1484 | 1484 | |
| 1485 | 1485 | /** |
@@ -1497,16 +1497,16 @@ discard block |
||
| 1497 | 1497 | * @return array|false Tableau de l'explication |
| 1498 | 1498 | */ |
| 1499 | 1499 | function sql_explain($q, $serveur = '', $option = true) { |
| 1500 | - $f = sql_serveur('explain', $serveur, true); |
|
| 1501 | - if (!is_string($f) or !$f) { |
|
| 1502 | - return false; |
|
| 1503 | - } |
|
| 1504 | - $r = $f($q, $serveur, $option !== false); |
|
| 1505 | - if ($r === false) { |
|
| 1506 | - spip_sql_erreur($serveur); |
|
| 1507 | - } |
|
| 1508 | - |
|
| 1509 | - return $r; |
|
| 1500 | + $f = sql_serveur('explain', $serveur, true); |
|
| 1501 | + if (!is_string($f) or !$f) { |
|
| 1502 | + return false; |
|
| 1503 | + } |
|
| 1504 | + $r = $f($q, $serveur, $option !== false); |
|
| 1505 | + if ($r === false) { |
|
| 1506 | + spip_sql_erreur($serveur); |
|
| 1507 | + } |
|
| 1508 | + |
|
| 1509 | + return $r; |
|
| 1510 | 1510 | } |
| 1511 | 1511 | |
| 1512 | 1512 | /** |
@@ -1524,16 +1524,16 @@ discard block |
||
| 1524 | 1524 | * @return bool Toujours true |
| 1525 | 1525 | */ |
| 1526 | 1526 | function sql_optimize($table, $serveur = '', $option = true) { |
| 1527 | - $f = sql_serveur('optimize', $serveur, $option === 'continue' or $option === false); |
|
| 1528 | - if (!is_string($f) or !$f) { |
|
| 1529 | - return false; |
|
| 1530 | - } |
|
| 1531 | - $r = $f($table, $serveur, $option !== false); |
|
| 1532 | - if ($r === false) { |
|
| 1533 | - spip_sql_erreur($serveur); |
|
| 1534 | - } |
|
| 1535 | - |
|
| 1536 | - return $r; |
|
| 1527 | + $f = sql_serveur('optimize', $serveur, $option === 'continue' or $option === false); |
|
| 1528 | + if (!is_string($f) or !$f) { |
|
| 1529 | + return false; |
|
| 1530 | + } |
|
| 1531 | + $r = $f($table, $serveur, $option !== false); |
|
| 1532 | + if ($r === false) { |
|
| 1533 | + spip_sql_erreur($serveur); |
|
| 1534 | + } |
|
| 1535 | + |
|
| 1536 | + return $r; |
|
| 1537 | 1537 | } |
| 1538 | 1538 | |
| 1539 | 1539 | /** |
@@ -1553,16 +1553,16 @@ discard block |
||
| 1553 | 1553 | * - true si la requête a réussie, false sinon |
| 1554 | 1554 | */ |
| 1555 | 1555 | function sql_repair($table, $serveur = '', $option = true) { |
| 1556 | - $f = sql_serveur('repair', $serveur, $option === 'continue' or $option === false); |
|
| 1557 | - if (!is_string($f) or !$f) { |
|
| 1558 | - return false; |
|
| 1559 | - } |
|
| 1560 | - $r = $f($table, $serveur, $option !== false); |
|
| 1561 | - if ($r === false) { |
|
| 1562 | - spip_sql_erreur($serveur); |
|
| 1563 | - } |
|
| 1564 | - |
|
| 1565 | - return $r; |
|
| 1556 | + $f = sql_serveur('repair', $serveur, $option === 'continue' or $option === false); |
|
| 1557 | + if (!is_string($f) or !$f) { |
|
| 1558 | + return false; |
|
| 1559 | + } |
|
| 1560 | + $r = $f($table, $serveur, $option !== false); |
|
| 1561 | + if ($r === false) { |
|
| 1562 | + spip_sql_erreur($serveur); |
|
| 1563 | + } |
|
| 1564 | + |
|
| 1565 | + return $r; |
|
| 1566 | 1566 | } |
| 1567 | 1567 | |
| 1568 | 1568 | |
@@ -1587,16 +1587,16 @@ discard block |
||
| 1587 | 1587 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 1588 | 1588 | */ |
| 1589 | 1589 | function sql_query($ins, $serveur = '', $option = true) { |
| 1590 | - $f = sql_serveur('query', $serveur, $option === 'continue' or $option === false); |
|
| 1591 | - if (!is_string($f) or !$f) { |
|
| 1592 | - return false; |
|
| 1593 | - } |
|
| 1594 | - $r = $f($ins, $serveur, $option !== false); |
|
| 1595 | - if ($r === false) { |
|
| 1596 | - spip_sql_erreur($serveur); |
|
| 1597 | - } |
|
| 1598 | - |
|
| 1599 | - return $r; |
|
| 1590 | + $f = sql_serveur('query', $serveur, $option === 'continue' or $option === false); |
|
| 1591 | + if (!is_string($f) or !$f) { |
|
| 1592 | + return false; |
|
| 1593 | + } |
|
| 1594 | + $r = $f($ins, $serveur, $option !== false); |
|
| 1595 | + if ($r === false) { |
|
| 1596 | + spip_sql_erreur($serveur); |
|
| 1597 | + } |
|
| 1598 | + |
|
| 1599 | + return $r; |
|
| 1600 | 1600 | } |
| 1601 | 1601 | |
| 1602 | 1602 | /** |
@@ -1644,27 +1644,27 @@ discard block |
||
| 1644 | 1644 | * |
| 1645 | 1645 | **/ |
| 1646 | 1646 | function sql_fetsel( |
| 1647 | - $select = [], |
|
| 1648 | - $from = [], |
|
| 1649 | - $where = [], |
|
| 1650 | - $groupby = [], |
|
| 1651 | - $orderby = [], |
|
| 1652 | - $limit = '', |
|
| 1653 | - $having = [], |
|
| 1654 | - $serveur = '', |
|
| 1655 | - $option = true |
|
| 1647 | + $select = [], |
|
| 1648 | + $from = [], |
|
| 1649 | + $where = [], |
|
| 1650 | + $groupby = [], |
|
| 1651 | + $orderby = [], |
|
| 1652 | + $limit = '', |
|
| 1653 | + $having = [], |
|
| 1654 | + $serveur = '', |
|
| 1655 | + $option = true |
|
| 1656 | 1656 | ) { |
| 1657 | - $q = sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, $option); |
|
| 1658 | - if ($option === false) { |
|
| 1659 | - return $q; |
|
| 1660 | - } |
|
| 1661 | - if (!$q) { |
|
| 1662 | - return []; |
|
| 1663 | - } |
|
| 1664 | - $r = sql_fetch($q, $serveur, $option); |
|
| 1665 | - sql_free($q, $serveur, $option); |
|
| 1666 | - |
|
| 1667 | - return $r; |
|
| 1657 | + $q = sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, $option); |
|
| 1658 | + if ($option === false) { |
|
| 1659 | + return $q; |
|
| 1660 | + } |
|
| 1661 | + if (!$q) { |
|
| 1662 | + return []; |
|
| 1663 | + } |
|
| 1664 | + $r = sql_fetch($q, $serveur, $option); |
|
| 1665 | + sql_free($q, $serveur, $option); |
|
| 1666 | + |
|
| 1667 | + return $r; |
|
| 1668 | 1668 | } |
| 1669 | 1669 | |
| 1670 | 1670 | |
@@ -1721,22 +1721,22 @@ discard block |
||
| 1721 | 1721 | * |
| 1722 | 1722 | **/ |
| 1723 | 1723 | function sql_allfetsel( |
| 1724 | - $select = [], |
|
| 1725 | - $from = [], |
|
| 1726 | - $where = [], |
|
| 1727 | - $groupby = [], |
|
| 1728 | - $orderby = [], |
|
| 1729 | - $limit = '', |
|
| 1730 | - $having = [], |
|
| 1731 | - $serveur = '', |
|
| 1732 | - $option = true |
|
| 1724 | + $select = [], |
|
| 1725 | + $from = [], |
|
| 1726 | + $where = [], |
|
| 1727 | + $groupby = [], |
|
| 1728 | + $orderby = [], |
|
| 1729 | + $limit = '', |
|
| 1730 | + $having = [], |
|
| 1731 | + $serveur = '', |
|
| 1732 | + $option = true |
|
| 1733 | 1733 | ) { |
| 1734 | - $q = sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, $option); |
|
| 1735 | - if ($option === false) { |
|
| 1736 | - return $q; |
|
| 1737 | - } |
|
| 1734 | + $q = sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, $option); |
|
| 1735 | + if ($option === false) { |
|
| 1736 | + return $q; |
|
| 1737 | + } |
|
| 1738 | 1738 | |
| 1739 | - return sql_fetch_all($q, $serveur, $option); |
|
| 1739 | + return sql_fetch_all($q, $serveur, $option); |
|
| 1740 | 1740 | } |
| 1741 | 1741 | |
| 1742 | 1742 | |
@@ -1783,33 +1783,33 @@ discard block |
||
| 1783 | 1783 | * |
| 1784 | 1784 | **/ |
| 1785 | 1785 | function sql_getfetsel( |
| 1786 | - $select, |
|
| 1787 | - $from = [], |
|
| 1788 | - $where = [], |
|
| 1789 | - $groupby = [], |
|
| 1790 | - $orderby = [], |
|
| 1791 | - $limit = '', |
|
| 1792 | - $having = [], |
|
| 1793 | - $serveur = '', |
|
| 1794 | - $option = true |
|
| 1786 | + $select, |
|
| 1787 | + $from = [], |
|
| 1788 | + $where = [], |
|
| 1789 | + $groupby = [], |
|
| 1790 | + $orderby = [], |
|
| 1791 | + $limit = '', |
|
| 1792 | + $having = [], |
|
| 1793 | + $serveur = '', |
|
| 1794 | + $option = true |
|
| 1795 | 1795 | ) { |
| 1796 | - if (preg_match('/\s+as\s+(\w+)$/i', $select, $c)) { |
|
| 1797 | - $id = $c[1]; |
|
| 1798 | - } elseif (!preg_match('/\W/', $select)) { |
|
| 1799 | - $id = $select; |
|
| 1800 | - } else { |
|
| 1801 | - $id = 'n'; |
|
| 1802 | - $select .= ' AS n'; |
|
| 1803 | - } |
|
| 1804 | - $r = sql_fetsel($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, $option); |
|
| 1805 | - if ($option === false) { |
|
| 1806 | - return $r; |
|
| 1807 | - } |
|
| 1808 | - if (!$r) { |
|
| 1809 | - return null; |
|
| 1810 | - } |
|
| 1811 | - |
|
| 1812 | - return $r[$id]; |
|
| 1796 | + if (preg_match('/\s+as\s+(\w+)$/i', $select, $c)) { |
|
| 1797 | + $id = $c[1]; |
|
| 1798 | + } elseif (!preg_match('/\W/', $select)) { |
|
| 1799 | + $id = $select; |
|
| 1800 | + } else { |
|
| 1801 | + $id = 'n'; |
|
| 1802 | + $select .= ' AS n'; |
|
| 1803 | + } |
|
| 1804 | + $r = sql_fetsel($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, $option); |
|
| 1805 | + if ($option === false) { |
|
| 1806 | + return $r; |
|
| 1807 | + } |
|
| 1808 | + if (!$r) { |
|
| 1809 | + return null; |
|
| 1810 | + } |
|
| 1811 | + |
|
| 1812 | + return $r[$id]; |
|
| 1813 | 1813 | } |
| 1814 | 1814 | |
| 1815 | 1815 | /** |
@@ -1827,9 +1827,9 @@ discard block |
||
| 1827 | 1827 | * Numero de version du serveur SQL |
| 1828 | 1828 | **/ |
| 1829 | 1829 | function sql_version($serveur = '', $option = true) { |
| 1830 | - $row = sql_fetsel('version() AS n', '', '', '', '', '', '', $serveur); |
|
| 1830 | + $row = sql_fetsel('version() AS n', '', '', '', '', '', '', $serveur); |
|
| 1831 | 1831 | |
| 1832 | - return ($row['n']); |
|
| 1832 | + return ($row['n']); |
|
| 1833 | 1833 | } |
| 1834 | 1834 | |
| 1835 | 1835 | /** |
@@ -1865,16 +1865,16 @@ discard block |
||
| 1865 | 1865 | * Le serveur SQL prefere t'il des transactions pour les insertions multiples ? |
| 1866 | 1866 | **/ |
| 1867 | 1867 | function sql_preferer_transaction($serveur = '', $option = true) { |
| 1868 | - $f = sql_serveur('preferer_transaction', $serveur, true); |
|
| 1869 | - if (!is_string($f) or !$f) { |
|
| 1870 | - return false; |
|
| 1871 | - } |
|
| 1872 | - $r = $f($serveur, $option !== false); |
|
| 1873 | - if ($r === false) { |
|
| 1874 | - spip_sql_erreur($serveur); |
|
| 1875 | - } |
|
| 1876 | - |
|
| 1877 | - return $r; |
|
| 1868 | + $f = sql_serveur('preferer_transaction', $serveur, true); |
|
| 1869 | + if (!is_string($f) or !$f) { |
|
| 1870 | + return false; |
|
| 1871 | + } |
|
| 1872 | + $r = $f($serveur, $option !== false); |
|
| 1873 | + if ($r === false) { |
|
| 1874 | + spip_sql_erreur($serveur); |
|
| 1875 | + } |
|
| 1876 | + |
|
| 1877 | + return $r; |
|
| 1878 | 1878 | } |
| 1879 | 1879 | |
| 1880 | 1880 | ; |
@@ -1899,16 +1899,16 @@ discard block |
||
| 1899 | 1899 | * false en cas d'erreur |
| 1900 | 1900 | **/ |
| 1901 | 1901 | function sql_demarrer_transaction($serveur = '', $option = true) { |
| 1902 | - $f = sql_serveur('demarrer_transaction', $serveur, true); |
|
| 1903 | - if (!is_string($f) or !$f) { |
|
| 1904 | - return false; |
|
| 1905 | - } |
|
| 1906 | - $r = $f($serveur, $option !== false); |
|
| 1907 | - if ($r === false) { |
|
| 1908 | - spip_sql_erreur($serveur); |
|
| 1909 | - } |
|
| 1910 | - |
|
| 1911 | - return $r; |
|
| 1902 | + $f = sql_serveur('demarrer_transaction', $serveur, true); |
|
| 1903 | + if (!is_string($f) or !$f) { |
|
| 1904 | + return false; |
|
| 1905 | + } |
|
| 1906 | + $r = $f($serveur, $option !== false); |
|
| 1907 | + if ($r === false) { |
|
| 1908 | + spip_sql_erreur($serveur); |
|
| 1909 | + } |
|
| 1910 | + |
|
| 1911 | + return $r; |
|
| 1912 | 1912 | } |
| 1913 | 1913 | |
| 1914 | 1914 | ; |
@@ -1933,16 +1933,16 @@ discard block |
||
| 1933 | 1933 | * false en cas d'erreur |
| 1934 | 1934 | **/ |
| 1935 | 1935 | function sql_terminer_transaction($serveur = '', $option = true) { |
| 1936 | - $f = sql_serveur('terminer_transaction', $serveur, true); |
|
| 1937 | - if (!is_string($f) or !$f) { |
|
| 1938 | - return false; |
|
| 1939 | - } |
|
| 1940 | - $r = $f($serveur, $option !== false); |
|
| 1941 | - if ($r === false) { |
|
| 1942 | - spip_sql_erreur($serveur); |
|
| 1943 | - } |
|
| 1944 | - |
|
| 1945 | - return $r; |
|
| 1936 | + $f = sql_serveur('terminer_transaction', $serveur, true); |
|
| 1937 | + if (!is_string($f) or !$f) { |
|
| 1938 | + return false; |
|
| 1939 | + } |
|
| 1940 | + $r = $f($serveur, $option !== false); |
|
| 1941 | + if ($r === false) { |
|
| 1942 | + spip_sql_erreur($serveur); |
|
| 1943 | + } |
|
| 1944 | + |
|
| 1945 | + return $r; |
|
| 1946 | 1946 | } |
| 1947 | 1947 | |
| 1948 | 1948 | ; |
@@ -1969,12 +1969,12 @@ discard block |
||
| 1969 | 1969 | * Valeur hexadécimale attendue par le serveur SQL |
| 1970 | 1970 | **/ |
| 1971 | 1971 | function sql_hex($val, $serveur = '', $option = true) { |
| 1972 | - $f = sql_serveur('hex', $serveur, $option === 'continue' or $option === false); |
|
| 1973 | - if (!is_string($f) or !$f) { |
|
| 1974 | - return false; |
|
| 1975 | - } |
|
| 1972 | + $f = sql_serveur('hex', $serveur, $option === 'continue' or $option === false); |
|
| 1973 | + if (!is_string($f) or !$f) { |
|
| 1974 | + return false; |
|
| 1975 | + } |
|
| 1976 | 1976 | |
| 1977 | - return $f($val); |
|
| 1977 | + return $f($val); |
|
| 1978 | 1978 | } |
| 1979 | 1979 | |
| 1980 | 1980 | /** |
@@ -2000,12 +2000,12 @@ discard block |
||
| 2000 | 2000 | * La chaine echappee |
| 2001 | 2001 | **/ |
| 2002 | 2002 | function sql_quote($val, $serveur = '', $type = '') { |
| 2003 | - $f = sql_serveur('quote', $serveur, true); |
|
| 2004 | - if (!is_string($f) or !$f) { |
|
| 2005 | - $f = '_q'; |
|
| 2006 | - } |
|
| 2003 | + $f = sql_serveur('quote', $serveur, true); |
|
| 2004 | + if (!is_string($f) or !$f) { |
|
| 2005 | + $f = '_q'; |
|
| 2006 | + } |
|
| 2007 | 2007 | |
| 2008 | - return $f($val, $type); |
|
| 2008 | + return $f($val, $type); |
|
| 2009 | 2009 | } |
| 2010 | 2010 | |
| 2011 | 2011 | /** |
@@ -2030,12 +2030,12 @@ discard block |
||
| 2030 | 2030 | * - false si le serveur SQL est indisponible |
| 2031 | 2031 | **/ |
| 2032 | 2032 | function sql_date_proche($champ, $interval, $unite, $serveur = '', $option = true) { |
| 2033 | - $f = sql_serveur('date_proche', $serveur, true); |
|
| 2034 | - if (!is_string($f) or !$f) { |
|
| 2035 | - return false; |
|
| 2036 | - } |
|
| 2033 | + $f = sql_serveur('date_proche', $serveur, true); |
|
| 2034 | + if (!is_string($f) or !$f) { |
|
| 2035 | + return false; |
|
| 2036 | + } |
|
| 2037 | 2037 | |
| 2038 | - return $f($champ, $interval, $unite); |
|
| 2038 | + return $f($champ, $interval, $unite); |
|
| 2039 | 2039 | } |
| 2040 | 2040 | |
| 2041 | 2041 | /** |
@@ -2071,26 +2071,26 @@ discard block |
||
| 2071 | 2071 | * Expression de requête SQL |
| 2072 | 2072 | **/ |
| 2073 | 2073 | function sql_in_quote($champ, $valeurs, $not = '', $serveur = '', $type = '', $option = true) { |
| 2074 | - $quote = sql_serveur('quote', $serveur, true); |
|
| 2075 | - if (!is_string($quote) or !$quote) { |
|
| 2076 | - return false; |
|
| 2077 | - } |
|
| 2078 | - |
|
| 2079 | - // sql_quote produit une chaine dans tous les cas |
|
| 2080 | - $valeurs = array_filter($valeurs, fn($v) => !is_array($v)); |
|
| 2081 | - $valeurs = array_unique($valeurs); |
|
| 2082 | - $valeurs = $quote($valeurs, $type); |
|
| 2083 | - |
|
| 2084 | - if (!strlen(trim($valeurs))) { |
|
| 2085 | - return ($not ? '0=0' : '0=1'); |
|
| 2086 | - } |
|
| 2087 | - |
|
| 2088 | - $f = sql_serveur('in', $serveur, $option === 'continue' or $option === false); |
|
| 2089 | - if (!is_string($f) or !$f) { |
|
| 2090 | - return false; |
|
| 2091 | - } |
|
| 2092 | - |
|
| 2093 | - return $f($champ, $valeurs, $not ? 'NOT' : '', $serveur, $option !== false); |
|
| 2074 | + $quote = sql_serveur('quote', $serveur, true); |
|
| 2075 | + if (!is_string($quote) or !$quote) { |
|
| 2076 | + return false; |
|
| 2077 | + } |
|
| 2078 | + |
|
| 2079 | + // sql_quote produit une chaine dans tous les cas |
|
| 2080 | + $valeurs = array_filter($valeurs, fn($v) => !is_array($v)); |
|
| 2081 | + $valeurs = array_unique($valeurs); |
|
| 2082 | + $valeurs = $quote($valeurs, $type); |
|
| 2083 | + |
|
| 2084 | + if (!strlen(trim($valeurs))) { |
|
| 2085 | + return ($not ? '0=0' : '0=1'); |
|
| 2086 | + } |
|
| 2087 | + |
|
| 2088 | + $f = sql_serveur('in', $serveur, $option === 'continue' or $option === false); |
|
| 2089 | + if (!is_string($f) or !$f) { |
|
| 2090 | + return false; |
|
| 2091 | + } |
|
| 2092 | + |
|
| 2093 | + return $f($champ, $valeurs, $not ? 'NOT' : '', $serveur, $option !== false); |
|
| 2094 | 2094 | } |
| 2095 | 2095 | |
| 2096 | 2096 | /** |
@@ -2107,19 +2107,19 @@ discard block |
||
| 2107 | 2107 | * @param bool $option |
| 2108 | 2108 | */ |
| 2109 | 2109 | function sql_in($champ, $valeurs, $not = '', $serveur = '', $option = true) { |
| 2110 | - $type = ''; |
|
| 2111 | - if (!is_array($valeurs)) { |
|
| 2112 | - $valeurs = strval($valeurs); |
|
| 2113 | - if (isset($valeurs[0]) and $valeurs[0] === ',') { |
|
| 2114 | - $valeurs = substr($valeurs, 1); |
|
| 2115 | - } |
|
| 2116 | - // on explode en tableau pour pouvoir securiser le contenu |
|
| 2117 | - $valeurs = explode(',', $valeurs); |
|
| 2118 | - // et on force un cast de type int donc |
|
| 2119 | - $type = 'int'; |
|
| 2120 | - } |
|
| 2121 | - |
|
| 2122 | - return sql_in_quote($champ, $valeurs, $not, $serveur, $type, $option); |
|
| 2110 | + $type = ''; |
|
| 2111 | + if (!is_array($valeurs)) { |
|
| 2112 | + $valeurs = strval($valeurs); |
|
| 2113 | + if (isset($valeurs[0]) and $valeurs[0] === ',') { |
|
| 2114 | + $valeurs = substr($valeurs, 1); |
|
| 2115 | + } |
|
| 2116 | + // on explode en tableau pour pouvoir securiser le contenu |
|
| 2117 | + $valeurs = explode(',', $valeurs); |
|
| 2118 | + // et on force un cast de type int donc |
|
| 2119 | + $type = 'int'; |
|
| 2120 | + } |
|
| 2121 | + |
|
| 2122 | + return sql_in_quote($champ, $valeurs, $not, $serveur, $type, $option); |
|
| 2123 | 2123 | } |
| 2124 | 2124 | |
| 2125 | 2125 | |
@@ -2160,24 +2160,24 @@ discard block |
||
| 2160 | 2160 | * Expression de requête SQL |
| 2161 | 2161 | **/ |
| 2162 | 2162 | function sql_in_select( |
| 2163 | - $in, |
|
| 2164 | - $select, |
|
| 2165 | - $from = [], |
|
| 2166 | - $where = [], |
|
| 2167 | - $groupby = [], |
|
| 2168 | - $orderby = [], |
|
| 2169 | - $limit = '', |
|
| 2170 | - $having = [], |
|
| 2171 | - $serveur = '' |
|
| 2163 | + $in, |
|
| 2164 | + $select, |
|
| 2165 | + $from = [], |
|
| 2166 | + $where = [], |
|
| 2167 | + $groupby = [], |
|
| 2168 | + $orderby = [], |
|
| 2169 | + $limit = '', |
|
| 2170 | + $having = [], |
|
| 2171 | + $serveur = '' |
|
| 2172 | 2172 | ) { |
| 2173 | - $liste = []; |
|
| 2174 | - $res = sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur); |
|
| 2175 | - while ($r = sql_fetch($res)) { |
|
| 2176 | - $liste[] = array_shift($r); |
|
| 2177 | - } |
|
| 2178 | - sql_free($res); |
|
| 2179 | - |
|
| 2180 | - return sql_in($in, $liste); |
|
| 2173 | + $liste = []; |
|
| 2174 | + $res = sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur); |
|
| 2175 | + while ($r = sql_fetch($res)) { |
|
| 2176 | + $liste[] = array_shift($r); |
|
| 2177 | + } |
|
| 2178 | + sql_free($res); |
|
| 2179 | + |
|
| 2180 | + return sql_in($in, $liste); |
|
| 2181 | 2181 | } |
| 2182 | 2182 | |
| 2183 | 2183 | /** |
@@ -2209,29 +2209,29 @@ discard block |
||
| 2209 | 2209 | * Position apres le saut. |
| 2210 | 2210 | */ |
| 2211 | 2211 | function sql_skip($res, $pos, $saut, $count, $serveur = '', $option = true) { |
| 2212 | - // pas de saut en arriere qu'on ne sait pas faire sans sql_seek |
|
| 2213 | - if (($saut = intval($saut)) <= 0) { |
|
| 2214 | - return $pos; |
|
| 2215 | - } |
|
| 2216 | - |
|
| 2217 | - $seek = $pos + $saut; |
|
| 2218 | - // si le saut fait depasser le maxi, on libere la resource |
|
| 2219 | - // et on sort |
|
| 2220 | - if ($seek >= $count) { |
|
| 2221 | - sql_free($res, $serveur, $option); |
|
| 2222 | - |
|
| 2223 | - return $count; |
|
| 2224 | - } |
|
| 2225 | - |
|
| 2226 | - if (sql_seek($res, $seek)) { |
|
| 2227 | - $pos = $seek; |
|
| 2228 | - } else { |
|
| 2229 | - while ($pos < $seek and sql_fetch($res, $serveur, $option)) { |
|
| 2230 | - $pos++; |
|
| 2231 | - } |
|
| 2232 | - } |
|
| 2233 | - |
|
| 2234 | - return $pos; |
|
| 2212 | + // pas de saut en arriere qu'on ne sait pas faire sans sql_seek |
|
| 2213 | + if (($saut = intval($saut)) <= 0) { |
|
| 2214 | + return $pos; |
|
| 2215 | + } |
|
| 2216 | + |
|
| 2217 | + $seek = $pos + $saut; |
|
| 2218 | + // si le saut fait depasser le maxi, on libere la resource |
|
| 2219 | + // et on sort |
|
| 2220 | + if ($seek >= $count) { |
|
| 2221 | + sql_free($res, $serveur, $option); |
|
| 2222 | + |
|
| 2223 | + return $count; |
|
| 2224 | + } |
|
| 2225 | + |
|
| 2226 | + if (sql_seek($res, $seek)) { |
|
| 2227 | + $pos = $seek; |
|
| 2228 | + } else { |
|
| 2229 | + while ($pos < $seek and sql_fetch($res, $serveur, $option)) { |
|
| 2230 | + $pos++; |
|
| 2231 | + } |
|
| 2232 | + } |
|
| 2233 | + |
|
| 2234 | + return $pos; |
|
| 2235 | 2235 | } |
| 2236 | 2236 | |
| 2237 | 2237 | |
@@ -2251,7 +2251,7 @@ discard block |
||
| 2251 | 2251 | * True si le champ est de type entier |
| 2252 | 2252 | */ |
| 2253 | 2253 | function sql_test_int($type, $serveur = '', $option = true) { |
| 2254 | - return preg_match('/^(TINYINT|SMALLINT|MEDIUMINT|INT|INTEGER|BIGINT)/i', trim($type)); |
|
| 2254 | + return preg_match('/^(TINYINT|SMALLINT|MEDIUMINT|INT|INTEGER|BIGINT)/i', trim($type)); |
|
| 2255 | 2255 | } |
| 2256 | 2256 | |
| 2257 | 2257 | /** |
@@ -2270,7 +2270,7 @@ discard block |
||
| 2270 | 2270 | * True si le champ est de type entier |
| 2271 | 2271 | */ |
| 2272 | 2272 | function sql_test_date($type, $serveur = '', $option = true) { |
| 2273 | - return preg_match('/^(DATE|DATETIME|TIMESTAMP|TIME)/i', trim($type)); |
|
| 2273 | + return preg_match('/^(DATE|DATETIME|TIMESTAMP|TIME)/i', trim($type)); |
|
| 2274 | 2274 | } |
| 2275 | 2275 | |
| 2276 | 2276 | /** |
@@ -2292,19 +2292,19 @@ discard block |
||
| 2292 | 2292 | * La date formatee |
| 2293 | 2293 | */ |
| 2294 | 2294 | function sql_format_date($annee = 0, $mois = 0, $jour = 0, $h = 0, $m = 0, $s = 0, $serveur = '') { |
| 2295 | - $annee = sprintf('%04s', $annee); |
|
| 2296 | - $mois = sprintf('%02s', $mois); |
|
| 2297 | - |
|
| 2298 | - if ($annee == '0000') { |
|
| 2299 | - $mois = 0; |
|
| 2300 | - } |
|
| 2301 | - if ($mois == '00') { |
|
| 2302 | - $jour = 0; |
|
| 2303 | - } |
|
| 2304 | - |
|
| 2305 | - return sprintf('%04u', $annee) . '-' . sprintf('%02u', $mois) . '-' |
|
| 2306 | - . sprintf('%02u', $jour) . ' ' . sprintf('%02u', $h) . ':' |
|
| 2307 | - . sprintf('%02u', $m) . ':' . sprintf('%02u', $s); |
|
| 2295 | + $annee = sprintf('%04s', $annee); |
|
| 2296 | + $mois = sprintf('%02s', $mois); |
|
| 2297 | + |
|
| 2298 | + if ($annee == '0000') { |
|
| 2299 | + $mois = 0; |
|
| 2300 | + } |
|
| 2301 | + if ($mois == '00') { |
|
| 2302 | + $jour = 0; |
|
| 2303 | + } |
|
| 2304 | + |
|
| 2305 | + return sprintf('%04u', $annee) . '-' . sprintf('%02u', $mois) . '-' |
|
| 2306 | + . sprintf('%02u', $jour) . ' ' . sprintf('%02u', $h) . ':' |
|
| 2307 | + . sprintf('%02u', $m) . ':' . sprintf('%02u', $s); |
|
| 2308 | 2308 | } |
| 2309 | 2309 | |
| 2310 | 2310 | |
@@ -2327,32 +2327,32 @@ discard block |
||
| 2327 | 2327 | **/ |
| 2328 | 2328 | function description_table($nom, $serveur = '') { |
| 2329 | 2329 | |
| 2330 | - static $trouver_table; |
|
| 2330 | + static $trouver_table; |
|
| 2331 | 2331 | |
| 2332 | - /* toujours utiliser trouver_table |
|
| 2332 | + /* toujours utiliser trouver_table |
|
| 2333 | 2333 | qui renverra la description theorique |
| 2334 | 2334 | car sinon on va se comporter differement selon que la table est declaree |
| 2335 | 2335 | ou non |
| 2336 | 2336 | */ |
| 2337 | - if (!$trouver_table) { |
|
| 2338 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2339 | - } |
|
| 2340 | - if ($desc = $trouver_table($nom, $serveur)) { |
|
| 2341 | - return $desc; |
|
| 2342 | - } |
|
| 2343 | - |
|
| 2344 | - // sauf a l'installation : |
|
| 2345 | - include_spip('base/serial'); |
|
| 2346 | - if (isset($GLOBALS['tables_principales'][$nom])) { |
|
| 2347 | - return $GLOBALS['tables_principales'][$nom]; |
|
| 2348 | - } |
|
| 2349 | - |
|
| 2350 | - include_spip('base/auxiliaires'); |
|
| 2351 | - if (isset($GLOBALS['tables_auxiliaires'][$nom])) { |
|
| 2352 | - return $GLOBALS['tables_auxiliaires'][$nom]; |
|
| 2353 | - } |
|
| 2354 | - |
|
| 2355 | - return false; |
|
| 2337 | + if (!$trouver_table) { |
|
| 2338 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2339 | + } |
|
| 2340 | + if ($desc = $trouver_table($nom, $serveur)) { |
|
| 2341 | + return $desc; |
|
| 2342 | + } |
|
| 2343 | + |
|
| 2344 | + // sauf a l'installation : |
|
| 2345 | + include_spip('base/serial'); |
|
| 2346 | + if (isset($GLOBALS['tables_principales'][$nom])) { |
|
| 2347 | + return $GLOBALS['tables_principales'][$nom]; |
|
| 2348 | + } |
|
| 2349 | + |
|
| 2350 | + include_spip('base/auxiliaires'); |
|
| 2351 | + if (isset($GLOBALS['tables_auxiliaires'][$nom])) { |
|
| 2352 | + return $GLOBALS['tables_auxiliaires'][$nom]; |
|
| 2353 | + } |
|
| 2354 | + |
|
| 2355 | + return false; |
|
| 2356 | 2356 | } |
| 2357 | 2357 | |
| 2358 | 2358 | /** |
@@ -2365,8 +2365,8 @@ discard block |
||
| 2365 | 2365 | * @return string Table sql éventuellement renommée |
| 2366 | 2366 | */ |
| 2367 | 2367 | function prefixer_table_spip($table, $prefixe) { |
| 2368 | - if ($prefixe) { |
|
| 2369 | - $table = preg_replace('/^spip_/', $prefixe . '_', $table); |
|
| 2370 | - } |
|
| 2371 | - return $table; |
|
| 2368 | + if ($prefixe) { |
|
| 2369 | + $table = preg_replace('/^spip_/', $prefixe . '_', $table); |
|
| 2370 | + } |
|
| 2371 | + return $table; |
|
| 2372 | 2372 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | |
| 24 | 24 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 25 | - return; |
|
| 25 | + return; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
@@ -43,90 +43,90 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | function _sqlite_init_functions(&$sqlite) { |
| 45 | 45 | |
| 46 | - if (!$sqlite) { |
|
| 47 | - return false; |
|
| 48 | - } |
|
| 46 | + if (!$sqlite) { |
|
| 47 | + return false; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | 50 | |
| 51 | - $fonctions = [ |
|
| 52 | - // A |
|
| 53 | - 'ACOS' => ['acos', 1], |
|
| 54 | - 'ASIN' => ['asin', 1], |
|
| 55 | - 'ATAN' => ['atan', 1], // mysql accepte 2 params comme atan2… hum ? |
|
| 56 | - 'ATAN2' => ['atan2', 2], |
|
| 51 | + $fonctions = [ |
|
| 52 | + // A |
|
| 53 | + 'ACOS' => ['acos', 1], |
|
| 54 | + 'ASIN' => ['asin', 1], |
|
| 55 | + 'ATAN' => ['atan', 1], // mysql accepte 2 params comme atan2… hum ? |
|
| 56 | + 'ATAN2' => ['atan2', 2], |
|
| 57 | 57 | |
| 58 | - // C |
|
| 59 | - 'CEIL' => ['_sqlite_func_ceil', 1], |
|
| 60 | - 'CONCAT' => ['_sqlite_func_concat', -1], |
|
| 61 | - 'COS' => ['cos', 1], |
|
| 58 | + // C |
|
| 59 | + 'CEIL' => ['_sqlite_func_ceil', 1], |
|
| 60 | + 'CONCAT' => ['_sqlite_func_concat', -1], |
|
| 61 | + 'COS' => ['cos', 1], |
|
| 62 | 62 | |
| 63 | - // D |
|
| 64 | - 'DATE_FORMAT' => ['_sqlite_func_date_format', 2], // équivalent a strftime avec args inversés |
|
| 65 | - 'DAYOFMONTH' => ['_sqlite_func_dayofmonth', 1], |
|
| 66 | - 'DEGREES' => ['rad2deg', 1], |
|
| 63 | + // D |
|
| 64 | + 'DATE_FORMAT' => ['_sqlite_func_date_format', 2], // équivalent a strftime avec args inversés |
|
| 65 | + 'DAYOFMONTH' => ['_sqlite_func_dayofmonth', 1], |
|
| 66 | + 'DEGREES' => ['rad2deg', 1], |
|
| 67 | 67 | |
| 68 | - // E |
|
| 69 | - 'EXTRAIRE_MULTI' => ['_sqlite_func_extraire_multi', 2], // specifique a SPIP/sql_multi() |
|
| 70 | - 'EXP' => ['exp', 1], |
|
| 68 | + // E |
|
| 69 | + 'EXTRAIRE_MULTI' => ['_sqlite_func_extraire_multi', 2], // specifique a SPIP/sql_multi() |
|
| 70 | + 'EXP' => ['exp', 1], |
|
| 71 | 71 | |
| 72 | - // F |
|
| 73 | - 'FIND_IN_SET' => ['_sqlite_func_find_in_set', 2], |
|
| 74 | - 'FLOOR' => ['_sqlite_func_floor', 1], |
|
| 72 | + // F |
|
| 73 | + 'FIND_IN_SET' => ['_sqlite_func_find_in_set', 2], |
|
| 74 | + 'FLOOR' => ['_sqlite_func_floor', 1], |
|
| 75 | 75 | |
| 76 | - // G |
|
| 77 | - 'GREATEST' => ['_sqlite_func_greatest', -1], |
|
| 76 | + // G |
|
| 77 | + 'GREATEST' => ['_sqlite_func_greatest', -1], |
|
| 78 | 78 | |
| 79 | - // I |
|
| 80 | - 'IF' => ['_sqlite_func_if', 3], |
|
| 81 | - 'INSERT' => ['_sqlite_func_insert', 4], |
|
| 82 | - 'INSTR' => ['_sqlite_func_instr', 2], |
|
| 79 | + // I |
|
| 80 | + 'IF' => ['_sqlite_func_if', 3], |
|
| 81 | + 'INSERT' => ['_sqlite_func_insert', 4], |
|
| 82 | + 'INSTR' => ['_sqlite_func_instr', 2], |
|
| 83 | 83 | |
| 84 | - // L |
|
| 85 | - 'LEAST' => ['_sqlite_func_least', -1], |
|
| 86 | - '_LEFT' => ['_sqlite_func_left', 2], |
|
| 84 | + // L |
|
| 85 | + 'LEAST' => ['_sqlite_func_least', -1], |
|
| 86 | + '_LEFT' => ['_sqlite_func_left', 2], |
|
| 87 | 87 | |
| 88 | - // N |
|
| 89 | - 'NOW' => ['_sqlite_func_now', 0], |
|
| 88 | + // N |
|
| 89 | + 'NOW' => ['_sqlite_func_now', 0], |
|
| 90 | 90 | |
| 91 | - // M |
|
| 92 | - 'MD5' => ['md5', 1], |
|
| 93 | - 'MONTH' => ['_sqlite_func_month', 1], |
|
| 91 | + // M |
|
| 92 | + 'MD5' => ['md5', 1], |
|
| 93 | + 'MONTH' => ['_sqlite_func_month', 1], |
|
| 94 | 94 | |
| 95 | - // P |
|
| 96 | - 'PREG_REPLACE' => ['_sqlite_func_preg_replace', 3], |
|
| 95 | + // P |
|
| 96 | + 'PREG_REPLACE' => ['_sqlite_func_preg_replace', 3], |
|
| 97 | 97 | |
| 98 | - // R |
|
| 99 | - 'RADIANS' => ['deg2rad', 1], |
|
| 100 | - 'RAND' => ['_sqlite_func_rand', 0], // sinon random() v2.4 |
|
| 101 | - 'REGEXP' => ['_sqlite_func_regexp_match', 2], // critere REGEXP supporte a partir de v3.3.2 |
|
| 102 | - 'RIGHT' => ['_sqlite_func_right', 2], |
|
| 98 | + // R |
|
| 99 | + 'RADIANS' => ['deg2rad', 1], |
|
| 100 | + 'RAND' => ['_sqlite_func_rand', 0], // sinon random() v2.4 |
|
| 101 | + 'REGEXP' => ['_sqlite_func_regexp_match', 2], // critere REGEXP supporte a partir de v3.3.2 |
|
| 102 | + 'RIGHT' => ['_sqlite_func_right', 2], |
|
| 103 | 103 | |
| 104 | - // S |
|
| 105 | - 'SETTYPE' => ['settype', 2], // CAST present en v3.2.3 |
|
| 106 | - 'SIN' => ['sin', 1], |
|
| 107 | - 'SQRT' => ['sqrt', 1], |
|
| 108 | - 'SUBSTRING' => ['_sqlite_func_substring' /*, 3*/], // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0 |
|
| 104 | + // S |
|
| 105 | + 'SETTYPE' => ['settype', 2], // CAST present en v3.2.3 |
|
| 106 | + 'SIN' => ['sin', 1], |
|
| 107 | + 'SQRT' => ['sqrt', 1], |
|
| 108 | + 'SUBSTRING' => ['_sqlite_func_substring' /*, 3*/], // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0 |
|
| 109 | 109 | |
| 110 | - // T |
|
| 111 | - 'TAN' => ['tan', 1], |
|
| 112 | - 'TIMESTAMPDIFF' => ['_sqlite_timestampdiff' /*, 3*/], |
|
| 113 | - 'TO_DAYS' => ['_sqlite_func_to_days', 1], |
|
| 110 | + // T |
|
| 111 | + 'TAN' => ['tan', 1], |
|
| 112 | + 'TIMESTAMPDIFF' => ['_sqlite_timestampdiff' /*, 3*/], |
|
| 113 | + 'TO_DAYS' => ['_sqlite_func_to_days', 1], |
|
| 114 | 114 | |
| 115 | - // U |
|
| 116 | - 'UNIX_TIMESTAMP' => ['_sqlite_func_unix_timestamp', 1], |
|
| 115 | + // U |
|
| 116 | + 'UNIX_TIMESTAMP' => ['_sqlite_func_unix_timestamp', 1], |
|
| 117 | 117 | |
| 118 | - // V |
|
| 119 | - 'VIDE' => ['_sqlite_func_vide', 0], // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide() |
|
| 118 | + // V |
|
| 119 | + 'VIDE' => ['_sqlite_func_vide', 0], // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide() |
|
| 120 | 120 | |
| 121 | - // Y |
|
| 122 | - 'YEAR' => ['_sqlite_func_year', 1] |
|
| 123 | - ]; |
|
| 121 | + // Y |
|
| 122 | + 'YEAR' => ['_sqlite_func_year', 1] |
|
| 123 | + ]; |
|
| 124 | 124 | |
| 125 | - foreach ($fonctions as $f => $r) { |
|
| 126 | - _sqlite_add_function($sqlite, $f, $r); |
|
| 127 | - } |
|
| 125 | + foreach ($fonctions as $f => $r) { |
|
| 126 | + _sqlite_add_function($sqlite, $f, $r); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - #spip_log('functions sqlite chargees ','sqlite.'._LOG_DEBUG); |
|
| 129 | + #spip_log('functions sqlite chargees ','sqlite.'._LOG_DEBUG); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | |
@@ -147,9 +147,9 @@ discard block |
||
| 147 | 147 | * |
| 148 | 148 | **/ |
| 149 | 149 | function _sqlite_add_function(&$sqlite, &$f, &$r) { |
| 150 | - isset($r[1]) |
|
| 151 | - ? $sqlite->sqliteCreateFunction($f, $r[0], $r[1]) |
|
| 152 | - : $sqlite->sqliteCreateFunction($f, $r[0]); |
|
| 150 | + isset($r[1]) |
|
| 151 | + ? $sqlite->sqliteCreateFunction($f, $r[0], $r[1]) |
|
| 152 | + : $sqlite->sqliteCreateFunction($f, $r[0]); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | /** |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | * @return int |
| 160 | 160 | */ |
| 161 | 161 | function _sqlite_func_ceil($a) { |
| 162 | - return ceil($a); |
|
| 162 | + return ceil($a); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | * @return string |
| 170 | 170 | */ |
| 171 | 171 | function _sqlite_func_concat(...$args) { |
| 172 | - return join('', $args); |
|
| 172 | + return join('', $args); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | * @return string |
| 183 | 183 | */ |
| 184 | 184 | function _sqlite_func_dayofmonth($d) { |
| 185 | - return _sqlite_func_date('d', $d); |
|
| 185 | + return _sqlite_func_date('d', $d); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | |
@@ -194,15 +194,15 @@ discard block |
||
| 194 | 194 | * @return int |
| 195 | 195 | */ |
| 196 | 196 | function _sqlite_func_find_in_set($num, $set) { |
| 197 | - $rank = 0; |
|
| 198 | - foreach (explode(',', $set) as $v) { |
|
| 199 | - if ($v == $num) { |
|
| 200 | - return (++$rank); |
|
| 201 | - } |
|
| 202 | - $rank++; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - return 0; |
|
| 197 | + $rank = 0; |
|
| 198 | + foreach (explode(',', $set) as $v) { |
|
| 199 | + if ($v == $num) { |
|
| 200 | + return (++$rank); |
|
| 201 | + } |
|
| 202 | + $rank++; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + return 0; |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | /** |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | * @return int |
| 213 | 213 | */ |
| 214 | 214 | function _sqlite_func_floor($a) { |
| 215 | - return floor($a); |
|
| 215 | + return floor($a); |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | * @return mixed |
| 226 | 226 | */ |
| 227 | 227 | function _sqlite_func_if($bool, $oui, $non) { |
| 228 | - return ($bool) ? $oui : $non; |
|
| 228 | + return ($bool) ? $oui : $non; |
|
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | |
@@ -242,10 +242,10 @@ discard block |
||
| 242 | 242 | * @return string |
| 243 | 243 | */ |
| 244 | 244 | function _sqlite_func_insert($s, $index, $longueur, $chaine) { |
| 245 | - return |
|
| 246 | - substr($s, 0, $index) |
|
| 247 | - . $chaine |
|
| 248 | - . substr(substr($s, $index), $longueur); |
|
| 245 | + return |
|
| 246 | + substr($s, 0, $index) |
|
| 247 | + . $chaine |
|
| 248 | + . substr(substr($s, $index), $longueur); |
|
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | * @return int |
| 258 | 258 | */ |
| 259 | 259 | function _sqlite_func_instr($s, $search) { |
| 260 | - return strpos($s, $search); |
|
| 260 | + return strpos($s, $search); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | |
@@ -268,7 +268,7 @@ discard block |
||
| 268 | 268 | * @return int |
| 269 | 269 | */ |
| 270 | 270 | function _sqlite_func_least(...$args) { |
| 271 | - return min($args); |
|
| 271 | + return min($args); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | * @return int |
| 280 | 280 | */ |
| 281 | 281 | function _sqlite_func_greatest(...$args) { |
| 282 | - return max($args); |
|
| 282 | + return max($args); |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | |
@@ -291,7 +291,7 @@ discard block |
||
| 291 | 291 | * @return string |
| 292 | 292 | */ |
| 293 | 293 | function _sqlite_func_left($s, $lenght) { |
| 294 | - return substr($s, $lenght); |
|
| 294 | + return substr($s, $lenght); |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | /** |
@@ -301,13 +301,13 @@ discard block |
||
| 301 | 301 | * @return string |
| 302 | 302 | */ |
| 303 | 303 | function _sqlite_func_now($force_refresh = false) { |
| 304 | - static $now = null; |
|
| 305 | - if (is_null($now) or $force_refresh) { |
|
| 306 | - $now = date('Y-m-d H:i:s'); |
|
| 307 | - } |
|
| 304 | + static $now = null; |
|
| 305 | + if (is_null($now) or $force_refresh) { |
|
| 306 | + $now = date('Y-m-d H:i:s'); |
|
| 307 | + } |
|
| 308 | 308 | |
| 309 | - #spip_log("Passage avec NOW : $now | ".time(),'sqlite.'._LOG_DEBUG); |
|
| 310 | - return $now; |
|
| 309 | + #spip_log("Passage avec NOW : $now | ".time(),'sqlite.'._LOG_DEBUG); |
|
| 310 | + return $now; |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | * @return string |
| 321 | 321 | */ |
| 322 | 322 | function _sqlite_func_month($d) { |
| 323 | - return _sqlite_func_date('m', $d); |
|
| 323 | + return _sqlite_func_date('m', $d); |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | |
@@ -333,10 +333,10 @@ discard block |
||
| 333 | 333 | * @return string |
| 334 | 334 | */ |
| 335 | 335 | function _sqlite_func_preg_replace($quoi, $cherche, $remplace) { |
| 336 | - $return = preg_replace('%' . $cherche . '%', $remplace, $quoi); |
|
| 336 | + $return = preg_replace('%' . $cherche . '%', $remplace, $quoi); |
|
| 337 | 337 | |
| 338 | - #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 339 | - return $return; |
|
| 338 | + #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 339 | + return $return; |
|
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | /** |
@@ -349,26 +349,26 @@ discard block |
||
| 349 | 349 | * @return string, l'extrait trouve. |
| 350 | 350 | **/ |
| 351 | 351 | function _sqlite_func_extraire_multi($quoi, $lang) { |
| 352 | - if (!defined('_EXTRAIRE_MULTI')) { |
|
| 353 | - include_spip('inc/filtres'); |
|
| 354 | - } |
|
| 355 | - if (!function_exists('approcher_langue')) { |
|
| 356 | - include_spip('inc/lang'); |
|
| 357 | - } |
|
| 358 | - if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) { |
|
| 359 | - foreach ($regs as $reg) { |
|
| 360 | - // chercher la version de la langue courante |
|
| 361 | - $trads = extraire_trads($reg[1]); |
|
| 362 | - if ($l = approcher_langue($trads, $lang)) { |
|
| 363 | - $trad = $trads[$l]; |
|
| 364 | - } else { |
|
| 365 | - $trad = reset($trads); |
|
| 366 | - } |
|
| 367 | - $quoi = str_replace($reg[0], $trad, $quoi); |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return $quoi; |
|
| 352 | + if (!defined('_EXTRAIRE_MULTI')) { |
|
| 353 | + include_spip('inc/filtres'); |
|
| 354 | + } |
|
| 355 | + if (!function_exists('approcher_langue')) { |
|
| 356 | + include_spip('inc/lang'); |
|
| 357 | + } |
|
| 358 | + if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) { |
|
| 359 | + foreach ($regs as $reg) { |
|
| 360 | + // chercher la version de la langue courante |
|
| 361 | + $trads = extraire_trads($reg[1]); |
|
| 362 | + if ($l = approcher_langue($trads, $lang)) { |
|
| 363 | + $trad = $trads[$l]; |
|
| 364 | + } else { |
|
| 365 | + $trad = reset($trads); |
|
| 366 | + } |
|
| 367 | + $quoi = str_replace($reg[0], $trad, $quoi); |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return $quoi; |
|
| 372 | 372 | } |
| 373 | 373 | |
| 374 | 374 | |
@@ -378,7 +378,7 @@ discard block |
||
| 378 | 378 | * @return float |
| 379 | 379 | */ |
| 380 | 380 | function _sqlite_func_rand() { |
| 381 | - return random_int(0, mt_getrandmax()); |
|
| 381 | + return random_int(0, mt_getrandmax()); |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | |
@@ -390,7 +390,7 @@ discard block |
||
| 390 | 390 | * @return string |
| 391 | 391 | */ |
| 392 | 392 | function _sqlite_func_right($s, $length) { |
| 393 | - return substr($s, 0 - $length); |
|
| 393 | + return substr($s, 0 - $length); |
|
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | |
@@ -402,17 +402,17 @@ discard block |
||
| 402 | 402 | * @return bool |
| 403 | 403 | */ |
| 404 | 404 | function _sqlite_func_regexp_match($cherche, $quoi) { |
| 405 | - // optimiser un cas tres courant avec les requetes en base |
|
| 406 | - if (!$quoi and !strlen($quoi)) { |
|
| 407 | - return false; |
|
| 408 | - } |
|
| 409 | - // il faut enlever un niveau d'echappement pour être homogène à mysql |
|
| 410 | - $cherche = str_replace('\\\\', '\\', $cherche); |
|
| 411 | - $u = $GLOBALS['meta']['pcre_u'] ?? 'u'; |
|
| 412 | - $return = preg_match('%' . $cherche . '%imsS' . $u, $quoi); |
|
| 413 | - |
|
| 414 | - #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 415 | - return $return; |
|
| 405 | + // optimiser un cas tres courant avec les requetes en base |
|
| 406 | + if (!$quoi and !strlen($quoi)) { |
|
| 407 | + return false; |
|
| 408 | + } |
|
| 409 | + // il faut enlever un niveau d'echappement pour être homogène à mysql |
|
| 410 | + $cherche = str_replace('\\\\', '\\', $cherche); |
|
| 411 | + $u = $GLOBALS['meta']['pcre_u'] ?? 'u'; |
|
| 412 | + $return = preg_match('%' . $cherche . '%imsS' . $u, $quoi); |
|
| 413 | + |
|
| 414 | + #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 415 | + return $return; |
|
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | |
@@ -427,8 +427,8 @@ discard block |
||
| 427 | 427 | * @return string |
| 428 | 428 | */ |
| 429 | 429 | function _sqlite_func_date_format($date, $conv) { |
| 430 | - $conv = _sqlite_func_strftime_format_converter($conv); |
|
| 431 | - return strftime($conv, is_int($date) ? $date : strtotime($date)); |
|
| 430 | + $conv = _sqlite_func_strftime_format_converter($conv); |
|
| 431 | + return strftime($conv, is_int($date) ? $date : strtotime($date)); |
|
| 432 | 432 | } |
| 433 | 433 | |
| 434 | 434 | /** |
@@ -444,28 +444,28 @@ discard block |
||
| 444 | 444 | * @return void |
| 445 | 445 | */ |
| 446 | 446 | function _sqlite_func_strftime_format_converter(string $conv): string { |
| 447 | - // ok : %a %b %d %e %H %I %l %j %k %m %p %r %S %T %w %y %Y |
|
| 448 | - // on ne sait pas en gérer certains... |
|
| 449 | - static $mysql_to_strftime_not_ok = ['%c', '%D', '%f', '%U', '%V', '%W', '%X']; |
|
| 450 | - static $mysql_to_strftime = [ |
|
| 451 | - '%h' => '%I', |
|
| 452 | - '%i' => '%M', |
|
| 453 | - '%M' => '%B', |
|
| 454 | - '%s' => '%S', |
|
| 455 | - '%u' => '%U', |
|
| 456 | - '%v' => '%V', |
|
| 457 | - '%x' => '%G', |
|
| 458 | - ]; |
|
| 459 | - static $to_strftime = []; |
|
| 460 | - if (!isset($to_strftime[$conv])) { |
|
| 461 | - $count = 0; |
|
| 462 | - str_replace($mysql_to_strftime_not_ok, '', $conv, $count); |
|
| 463 | - if ($count > 0) { |
|
| 464 | - spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_ERREUR); |
|
| 465 | - } |
|
| 466 | - $to_strftime[$conv] = str_replace(array_keys($mysql_to_strftime), $mysql_to_strftime, $conv); |
|
| 467 | - } |
|
| 468 | - return $to_strftime[$conv]; |
|
| 447 | + // ok : %a %b %d %e %H %I %l %j %k %m %p %r %S %T %w %y %Y |
|
| 448 | + // on ne sait pas en gérer certains... |
|
| 449 | + static $mysql_to_strftime_not_ok = ['%c', '%D', '%f', '%U', '%V', '%W', '%X']; |
|
| 450 | + static $mysql_to_strftime = [ |
|
| 451 | + '%h' => '%I', |
|
| 452 | + '%i' => '%M', |
|
| 453 | + '%M' => '%B', |
|
| 454 | + '%s' => '%S', |
|
| 455 | + '%u' => '%U', |
|
| 456 | + '%v' => '%V', |
|
| 457 | + '%x' => '%G', |
|
| 458 | + ]; |
|
| 459 | + static $to_strftime = []; |
|
| 460 | + if (!isset($to_strftime[$conv])) { |
|
| 461 | + $count = 0; |
|
| 462 | + str_replace($mysql_to_strftime_not_ok, '', $conv, $count); |
|
| 463 | + if ($count > 0) { |
|
| 464 | + spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_ERREUR); |
|
| 465 | + } |
|
| 466 | + $to_strftime[$conv] = str_replace(array_keys($mysql_to_strftime), $mysql_to_strftime, $conv); |
|
| 467 | + } |
|
| 468 | + return $to_strftime[$conv]; |
|
| 469 | 469 | } |
| 470 | 470 | |
| 471 | 471 | /** |
@@ -479,11 +479,11 @@ discard block |
||
| 479 | 479 | * @return int |
| 480 | 480 | */ |
| 481 | 481 | function _sqlite_func_to_days($d) { |
| 482 | - static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01 |
|
| 483 | - $result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600)); |
|
| 482 | + static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01 |
|
| 483 | + $result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600)); |
|
| 484 | 484 | |
| 485 | - #spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG); |
|
| 486 | - return $result; |
|
| 485 | + #spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG); |
|
| 486 | + return $result; |
|
| 487 | 487 | } |
| 488 | 488 | |
| 489 | 489 | /** |
@@ -495,13 +495,13 @@ discard block |
||
| 495 | 495 | * @return string |
| 496 | 496 | */ |
| 497 | 497 | function _sqlite_func_substring($string, $start, $len = null) { |
| 498 | - // SQL compte a partir de 1, php a partir de 0 |
|
| 499 | - $start = ($start > 0) ? $start - 1 : $start; |
|
| 500 | - if (is_null($len)) { |
|
| 501 | - return substr($string, $start); |
|
| 502 | - } else { |
|
| 503 | - return substr($string, $start, $len); |
|
| 504 | - } |
|
| 498 | + // SQL compte a partir de 1, php a partir de 0 |
|
| 499 | + $start = ($start > 0) ? $start - 1 : $start; |
|
| 500 | + if (is_null($len)) { |
|
| 501 | + return substr($string, $start); |
|
| 502 | + } else { |
|
| 503 | + return substr($string, $start, $len); |
|
| 504 | + } |
|
| 505 | 505 | } |
| 506 | 506 | |
| 507 | 507 | /** |
@@ -517,33 +517,33 @@ discard block |
||
| 517 | 517 | * @return int |
| 518 | 518 | */ |
| 519 | 519 | function _sqlite_timestampdiff($unit, $date1, $date2) { |
| 520 | - $d1 = date_create($date1); |
|
| 521 | - $d2 = date_create($date2); |
|
| 522 | - $diff = date_diff($d1, $d2); |
|
| 523 | - $inv = $diff->invert ? -1 : 1; |
|
| 524 | - switch ($unit) { |
|
| 525 | - case 'YEAR': |
|
| 526 | - return $inv * $diff->y; |
|
| 527 | - case 'QUARTER': |
|
| 528 | - return $inv * (4 * $diff->y + intval(floor($diff->m / 3))); |
|
| 529 | - case 'MONTH': |
|
| 530 | - return $inv * (12 * $diff->y + $diff->m); |
|
| 531 | - case 'WEEK': |
|
| 532 | - return $inv * intval(floor($diff->days / 7)); |
|
| 533 | - case 'DAY': |
|
| 534 | - #var_dump($inv*$diff->days); |
|
| 535 | - return $inv * $diff->days; |
|
| 536 | - case 'HOUR': |
|
| 537 | - return $inv * (24 * $diff->days + $diff->h); |
|
| 538 | - case 'MINUTE': |
|
| 539 | - return $inv * ((24 * $diff->days + $diff->h) * 60 + $diff->i); |
|
| 540 | - case 'SECOND': |
|
| 541 | - return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s); |
|
| 542 | - case 'MICROSECOND': |
|
| 543 | - return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s) * 1_000_000; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - return 0; |
|
| 520 | + $d1 = date_create($date1); |
|
| 521 | + $d2 = date_create($date2); |
|
| 522 | + $diff = date_diff($d1, $d2); |
|
| 523 | + $inv = $diff->invert ? -1 : 1; |
|
| 524 | + switch ($unit) { |
|
| 525 | + case 'YEAR': |
|
| 526 | + return $inv * $diff->y; |
|
| 527 | + case 'QUARTER': |
|
| 528 | + return $inv * (4 * $diff->y + intval(floor($diff->m / 3))); |
|
| 529 | + case 'MONTH': |
|
| 530 | + return $inv * (12 * $diff->y + $diff->m); |
|
| 531 | + case 'WEEK': |
|
| 532 | + return $inv * intval(floor($diff->days / 7)); |
|
| 533 | + case 'DAY': |
|
| 534 | + #var_dump($inv*$diff->days); |
|
| 535 | + return $inv * $diff->days; |
|
| 536 | + case 'HOUR': |
|
| 537 | + return $inv * (24 * $diff->days + $diff->h); |
|
| 538 | + case 'MINUTE': |
|
| 539 | + return $inv * ((24 * $diff->days + $diff->h) * 60 + $diff->i); |
|
| 540 | + case 'SECOND': |
|
| 541 | + return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s); |
|
| 542 | + case 'MICROSECOND': |
|
| 543 | + return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s) * 1_000_000; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + return 0; |
|
| 547 | 547 | } |
| 548 | 548 | |
| 549 | 549 | /** |
@@ -553,24 +553,24 @@ discard block |
||
| 553 | 553 | * @return int |
| 554 | 554 | */ |
| 555 | 555 | function _sqlite_func_unix_timestamp($d) { |
| 556 | - static $mem = []; |
|
| 557 | - static $n = 0; |
|
| 558 | - if (isset($mem[$d])) { |
|
| 559 | - return $mem[$d]; |
|
| 560 | - } |
|
| 561 | - if ($n++ > 100) { |
|
| 562 | - $mem = []; |
|
| 563 | - $n = 0; |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - //2005-12-02 20:53:53 |
|
| 567 | - #spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG); |
|
| 568 | - if (!$d) { |
|
| 569 | - return $mem[$d] = time(); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - // une pile plus grosse n'accelere pas le calcul |
|
| 573 | - return $mem[$d] = strtotime($d); |
|
| 556 | + static $mem = []; |
|
| 557 | + static $n = 0; |
|
| 558 | + if (isset($mem[$d])) { |
|
| 559 | + return $mem[$d]; |
|
| 560 | + } |
|
| 561 | + if ($n++ > 100) { |
|
| 562 | + $mem = []; |
|
| 563 | + $n = 0; |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + //2005-12-02 20:53:53 |
|
| 567 | + #spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG); |
|
| 568 | + if (!$d) { |
|
| 569 | + return $mem[$d] = time(); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + // une pile plus grosse n'accelere pas le calcul |
|
| 573 | + return $mem[$d] = strtotime($d); |
|
| 574 | 574 | } |
| 575 | 575 | |
| 576 | 576 | |
@@ -583,7 +583,7 @@ discard block |
||
| 583 | 583 | * @return string |
| 584 | 584 | */ |
| 585 | 585 | function _sqlite_func_year($d) { |
| 586 | - return _sqlite_func_date('Y', $d); |
|
| 586 | + return _sqlite_func_date('Y', $d); |
|
| 587 | 587 | } |
| 588 | 588 | |
| 589 | 589 | /** |
@@ -596,20 +596,20 @@ discard block |
||
| 596 | 596 | * @return string |
| 597 | 597 | */ |
| 598 | 598 | function _sqlite_func_date($quoi, $d) { |
| 599 | - static $mem = []; |
|
| 600 | - static $n = 0; |
|
| 601 | - if (isset($mem[$d])) { |
|
| 602 | - return $mem[$d][$quoi]; |
|
| 603 | - } |
|
| 604 | - if ($n++ > 100) { |
|
| 605 | - $mem = []; |
|
| 606 | - $n = 0; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - $dec = date('Y-m-d', _sqlite_func_unix_timestamp($d)); |
|
| 610 | - $mem[$d] = ['Y' => substr($dec, 0, 4), 'm' => substr($dec, 5, 2), 'd' => substr($dec, 8, 2)]; |
|
| 611 | - |
|
| 612 | - return $mem[$d][$quoi]; |
|
| 599 | + static $mem = []; |
|
| 600 | + static $n = 0; |
|
| 601 | + if (isset($mem[$d])) { |
|
| 602 | + return $mem[$d][$quoi]; |
|
| 603 | + } |
|
| 604 | + if ($n++ > 100) { |
|
| 605 | + $mem = []; |
|
| 606 | + $n = 0; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + $dec = date('Y-m-d', _sqlite_func_unix_timestamp($d)); |
|
| 610 | + $mem[$d] = ['Y' => substr($dec, 0, 4), 'm' => substr($dec, 5, 2), 'd' => substr($dec, 8, 2)]; |
|
| 611 | + |
|
| 612 | + return $mem[$d][$quoi]; |
|
| 613 | 613 | } |
| 614 | 614 | |
| 615 | 615 | /** |
@@ -618,5 +618,5 @@ discard block |
||
| 618 | 618 | * @return void |
| 619 | 619 | */ |
| 620 | 620 | function _sqlite_func_vide() { |
| 621 | - return; |
|
| 621 | + return; |
|
| 622 | 622 | } |
@@ -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 | // TODO: get/set_caracteres ? |
@@ -42,91 +42,91 @@ discard block |
||
| 42 | 42 | * @return array|bool |
| 43 | 43 | */ |
| 44 | 44 | function req_sqlite_dist($addr, $port, $login, $pass, $db = '', $prefixe = '', $sqlite_version = '') { |
| 45 | - static $last_connect = []; |
|
| 46 | - |
|
| 47 | - // si provient de selectdb |
|
| 48 | - // un code pour etre sur que l'on vient de select_db() |
|
| 49 | - if (strpos($db, $code = '@selectdb@') !== false) { |
|
| 50 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 51 | - $$a = $last_connect[$a]; |
|
| 52 | - } |
|
| 53 | - $db = str_replace($code, '', $db); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /* |
|
| 45 | + static $last_connect = []; |
|
| 46 | + |
|
| 47 | + // si provient de selectdb |
|
| 48 | + // un code pour etre sur que l'on vient de select_db() |
|
| 49 | + if (strpos($db, $code = '@selectdb@') !== false) { |
|
| 50 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 51 | + $$a = $last_connect[$a]; |
|
| 52 | + } |
|
| 53 | + $db = str_replace($code, '', $db); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /* |
|
| 57 | 57 | * En sqlite, seule l'adresse du fichier est importante. |
| 58 | 58 | * Ce sera $db le nom, |
| 59 | 59 | * le path est $addr |
| 60 | 60 | * (_DIR_DB si $addr est vide) |
| 61 | 61 | */ |
| 62 | - _sqlite_init(); |
|
| 63 | - |
|
| 64 | - // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 65 | - $f = _DIR_DB; |
|
| 66 | - if ($addr and strpos($addr, '/') !== false) { |
|
| 67 | - $f = rtrim($addr, '/') . '/'; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 71 | - // il faut que la base existe ou que le repertoire parent soit writable |
|
| 72 | - if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 73 | - spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 74 | - |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // charger les modules sqlite au besoin |
|
| 79 | - if (!_sqlite_charger_version($sqlite_version)) { |
|
| 80 | - spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 81 | - |
|
| 82 | - return false; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - // chargement des constantes |
|
| 86 | - // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 87 | - $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 88 | - $define(); |
|
| 89 | - |
|
| 90 | - $ok = false; |
|
| 91 | - if (!$db) { |
|
| 92 | - // si pas de db -> |
|
| 93 | - // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 94 | - // pour tester la connexion |
|
| 95 | - $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 96 | - $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 97 | - $ok = $link = new \PDO("sqlite:$tmp"); |
|
| 98 | - } else { |
|
| 99 | - // Ouvrir (eventuellement creer la base) |
|
| 100 | - $ok = $link = new \PDO("sqlite:$f"); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - if (!$ok) { |
|
| 104 | - $e = _sqlite_last_error_from_link($link); |
|
| 105 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 106 | - |
|
| 107 | - return false; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if ($link) { |
|
| 111 | - $last_connect = [ |
|
| 112 | - 'addr' => $addr, |
|
| 113 | - 'port' => $port, |
|
| 114 | - 'login' => $login, |
|
| 115 | - 'pass' => $pass, |
|
| 116 | - 'db' => $db, |
|
| 117 | - 'prefixe' => $prefixe, |
|
| 118 | - ]; |
|
| 119 | - // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 120 | - include_spip('req/sqlite_fonctions'); |
|
| 121 | - _sqlite_init_functions($link); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return [ |
|
| 125 | - 'db' => $db, |
|
| 126 | - 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 127 | - 'link' => $link, |
|
| 128 | - 'total_requetes' => 0, |
|
| 129 | - ]; |
|
| 62 | + _sqlite_init(); |
|
| 63 | + |
|
| 64 | + // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 65 | + $f = _DIR_DB; |
|
| 66 | + if ($addr and strpos($addr, '/') !== false) { |
|
| 67 | + $f = rtrim($addr, '/') . '/'; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 71 | + // il faut que la base existe ou que le repertoire parent soit writable |
|
| 72 | + if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 73 | + spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 74 | + |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // charger les modules sqlite au besoin |
|
| 79 | + if (!_sqlite_charger_version($sqlite_version)) { |
|
| 80 | + spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 81 | + |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + // chargement des constantes |
|
| 86 | + // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 87 | + $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 88 | + $define(); |
|
| 89 | + |
|
| 90 | + $ok = false; |
|
| 91 | + if (!$db) { |
|
| 92 | + // si pas de db -> |
|
| 93 | + // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 94 | + // pour tester la connexion |
|
| 95 | + $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 96 | + $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 97 | + $ok = $link = new \PDO("sqlite:$tmp"); |
|
| 98 | + } else { |
|
| 99 | + // Ouvrir (eventuellement creer la base) |
|
| 100 | + $ok = $link = new \PDO("sqlite:$f"); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + if (!$ok) { |
|
| 104 | + $e = _sqlite_last_error_from_link($link); |
|
| 105 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 106 | + |
|
| 107 | + return false; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if ($link) { |
|
| 111 | + $last_connect = [ |
|
| 112 | + 'addr' => $addr, |
|
| 113 | + 'port' => $port, |
|
| 114 | + 'login' => $login, |
|
| 115 | + 'pass' => $pass, |
|
| 116 | + 'db' => $db, |
|
| 117 | + 'prefixe' => $prefixe, |
|
| 118 | + ]; |
|
| 119 | + // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 120 | + include_spip('req/sqlite_fonctions'); |
|
| 121 | + _sqlite_init_functions($link); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return [ |
|
| 125 | + 'db' => $db, |
|
| 126 | + 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 127 | + 'link' => $link, |
|
| 128 | + 'total_requetes' => 0, |
|
| 129 | + ]; |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | |
@@ -145,14 +145,14 @@ discard block |
||
| 145 | 145 | * Resultat de la requete |
| 146 | 146 | */ |
| 147 | 147 | function spip_sqlite_query($query, $serveur = '', $requeter = true) { |
| 148 | - #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 149 | - #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 150 | - $query = spip_sqlite::traduire_requete($query, $serveur); |
|
| 151 | - if (!$requeter) { |
|
| 152 | - return $query; |
|
| 153 | - } |
|
| 148 | + #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 149 | + #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 150 | + $query = spip_sqlite::traduire_requete($query, $serveur); |
|
| 151 | + if (!$requeter) { |
|
| 152 | + return $query; |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - return spip_sqlite::executer_requete($query, $serveur); |
|
| 155 | + return spip_sqlite::executer_requete($query, $serveur); |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | |
@@ -169,11 +169,11 @@ discard block |
||
| 169 | 169 | */ |
| 170 | 170 | function spip_sqlite_alter($query, $serveur = '', $requeter = true) { |
| 171 | 171 | |
| 172 | - $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 173 | - // traduire la requete pour recuperer les bons noms de table |
|
| 174 | - $query = spip_sqlite::traduire_requete($query, $serveur); |
|
| 172 | + $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 173 | + // traduire la requete pour recuperer les bons noms de table |
|
| 174 | + $query = spip_sqlite::traduire_requete($query, $serveur); |
|
| 175 | 175 | |
| 176 | - /* |
|
| 176 | + /* |
|
| 177 | 177 | * la il faut faire les transformations |
| 178 | 178 | * si ALTER TABLE x (DROP|CHANGE) y |
| 179 | 179 | * |
@@ -182,251 +182,251 @@ discard block |
||
| 182 | 182 | * 3) faire chaque requete independemment |
| 183 | 183 | */ |
| 184 | 184 | |
| 185 | - // 1 |
|
| 186 | - if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 187 | - $debut = $regs[1]; |
|
| 188 | - $table = $regs[3]; |
|
| 189 | - $suite = $regs[4]; |
|
| 190 | - } else { |
|
| 191 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 192 | - |
|
| 193 | - return false; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - // 2 |
|
| 197 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 198 | - // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 199 | - // ou revoir l'api de sql_alter en creant un |
|
| 200 | - // sql_alter_table($table,array($actions)); |
|
| 201 | - $todo = explode(',', $suite); |
|
| 202 | - |
|
| 203 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 204 | - $todo2 = []; |
|
| 205 | - $i = 0; |
|
| 206 | - $ouverte = false; |
|
| 207 | - while ($do = array_shift($todo)) { |
|
| 208 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 209 | - $o = (false !== strpos($do, '(')); |
|
| 210 | - $f = (false !== strpos($do, ')')); |
|
| 211 | - if ($o and !$f) { |
|
| 212 | - $ouverte = true; |
|
| 213 | - } elseif ($f) { |
|
| 214 | - $ouverte = false; |
|
| 215 | - } |
|
| 216 | - if (!$ouverte) { |
|
| 217 | - $i++; |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - // 3 |
|
| 222 | - $resultats = []; |
|
| 223 | - foreach ($todo2 as $do) { |
|
| 224 | - $do = trim($do); |
|
| 225 | - if ( |
|
| 226 | - !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 227 | - . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 228 | - . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 229 | - . '|ADD COLUMN|ADD' |
|
| 230 | - . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 231 | - ) { |
|
| 232 | - spip_log( |
|
| 233 | - "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 234 | - 'sqlite.' . _LOG_ERREUR |
|
| 235 | - ); |
|
| 236 | - |
|
| 237 | - return false; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - $cle = strtoupper($matches[1]); |
|
| 241 | - $colonne_origine = $matches[2]; |
|
| 242 | - $colonne_destination = ''; |
|
| 243 | - |
|
| 244 | - $def = $matches[3]; |
|
| 245 | - |
|
| 246 | - // eluder une eventuelle clause before|after|first inutilisable |
|
| 247 | - $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 248 | - $defo = $defr; // garder la def d'origine pour certains cas |
|
| 249 | - // remplacer les definitions venant de mysql |
|
| 250 | - $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 251 | - |
|
| 252 | - // reinjecter dans le do |
|
| 253 | - $do = str_replace($def, $defr, $do); |
|
| 254 | - $def = $defr; |
|
| 255 | - |
|
| 256 | - switch ($cle) { |
|
| 257 | - // suppression d'un index |
|
| 258 | - case 'DROP KEY': |
|
| 259 | - case 'DROP INDEX': |
|
| 260 | - $nom_index = $colonne_origine; |
|
| 261 | - spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 262 | - break; |
|
| 263 | - |
|
| 264 | - // suppression d'une pk |
|
| 265 | - case 'DROP PRIMARY KEY': |
|
| 266 | - if ( |
|
| 267 | - !_sqlite_modifier_table( |
|
| 268 | - $table, |
|
| 269 | - $colonne_origine, |
|
| 270 | - ['key' => ['PRIMARY KEY' => '']], |
|
| 271 | - $serveur |
|
| 272 | - ) |
|
| 273 | - ) { |
|
| 274 | - return false; |
|
| 275 | - } |
|
| 276 | - break; |
|
| 277 | - // suppression d'une colonne |
|
| 278 | - case 'DROP COLUMN': |
|
| 279 | - case 'DROP': |
|
| 280 | - if ( |
|
| 281 | - !_sqlite_modifier_table( |
|
| 282 | - $table, |
|
| 283 | - [$colonne_origine => ''], |
|
| 284 | - [], |
|
| 285 | - $serveur |
|
| 286 | - ) |
|
| 287 | - ) { |
|
| 288 | - return false; |
|
| 289 | - } |
|
| 290 | - break; |
|
| 291 | - |
|
| 292 | - case 'CHANGE COLUMN': |
|
| 293 | - case 'CHANGE': |
|
| 294 | - // recuperer le nom de la future colonne |
|
| 295 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 296 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 297 | - $def = trim($defo); |
|
| 298 | - $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 299 | - $def = substr($def, strlen($colonne_destination) + 1); |
|
| 300 | - |
|
| 301 | - if ( |
|
| 302 | - !_sqlite_modifier_table( |
|
| 303 | - $table, |
|
| 304 | - [$colonne_origine => $colonne_destination], |
|
| 305 | - ['field' => [$colonne_destination => $def]], |
|
| 306 | - $serveur |
|
| 307 | - ) |
|
| 308 | - ) { |
|
| 309 | - return false; |
|
| 310 | - } |
|
| 311 | - break; |
|
| 312 | - |
|
| 313 | - case 'MODIFY': |
|
| 314 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 315 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 316 | - if ( |
|
| 317 | - !_sqlite_modifier_table( |
|
| 318 | - $table, |
|
| 319 | - $colonne_origine, |
|
| 320 | - ['field' => [$colonne_origine => $defo]], |
|
| 321 | - $serveur |
|
| 322 | - ) |
|
| 323 | - ) { |
|
| 324 | - return false; |
|
| 325 | - } |
|
| 326 | - break; |
|
| 327 | - |
|
| 328 | - // pas geres en sqlite2 |
|
| 329 | - case 'RENAME': |
|
| 330 | - $do = 'RENAME TO' . substr($do, 6); |
|
| 331 | - case 'RENAME TO': |
|
| 332 | - if (!spip_sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 333 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 334 | - |
|
| 335 | - return false; |
|
| 336 | - } |
|
| 337 | - break; |
|
| 338 | - |
|
| 339 | - // ajout d'une pk |
|
| 340 | - case 'ADD PRIMARY KEY': |
|
| 341 | - $pk = trim(substr($do, 16)); |
|
| 342 | - $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 343 | - if ( |
|
| 344 | - !_sqlite_modifier_table( |
|
| 345 | - $table, |
|
| 346 | - $colonne_origine, |
|
| 347 | - ['key' => ['PRIMARY KEY' => $pk]], |
|
| 348 | - $serveur |
|
| 349 | - ) |
|
| 350 | - ) { |
|
| 351 | - return false; |
|
| 352 | - } |
|
| 353 | - break; |
|
| 354 | - // ajout d'un index |
|
| 355 | - case 'ADD UNIQUE KEY': |
|
| 356 | - case 'ADD UNIQUE': |
|
| 357 | - $unique = true; |
|
| 358 | - case 'ADD INDEX': |
|
| 359 | - case 'ADD KEY': |
|
| 360 | - if (!isset($unique)) { |
|
| 361 | - $unique = false; |
|
| 362 | - } |
|
| 363 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 364 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 365 | - // |
|
| 366 | - // nom_index (colonnes) |
|
| 367 | - if ($def) { |
|
| 368 | - $colonnes = substr($def, 1, -1); |
|
| 369 | - $nom_index = $colonne_origine; |
|
| 370 | - } else { |
|
| 371 | - // (colonne) |
|
| 372 | - if ($colonne_origine[0] == '(') { |
|
| 373 | - $colonnes = substr($colonne_origine, 1, -1); |
|
| 374 | - if (false !== strpos(',', $colonnes)) { |
|
| 375 | - spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 376 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 377 | - break; |
|
| 378 | - } else { |
|
| 379 | - $nom_index = $colonnes; |
|
| 380 | - } |
|
| 381 | - } // nom_index |
|
| 382 | - else { |
|
| 383 | - $nom_index = $colonnes = $colonne_origine; |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 387 | - break; |
|
| 388 | - |
|
| 389 | - // pas geres en sqlite2 |
|
| 390 | - case 'ADD COLUMN': |
|
| 391 | - $do = 'ADD' . substr($do, 10); |
|
| 392 | - case 'ADD': |
|
| 393 | - default: |
|
| 394 | - if (!preg_match(',primary\s+key,i', $do)) { |
|
| 395 | - if (!spip_sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 396 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 397 | - |
|
| 398 | - return false; |
|
| 399 | - } |
|
| 400 | - break; |
|
| 401 | - } |
|
| 402 | - // ou si la colonne est aussi primary key |
|
| 403 | - // cas du add id_truc int primary key |
|
| 404 | - // ajout d'une colonne qui passe en primary key directe |
|
| 405 | - else { |
|
| 406 | - $def = trim(substr($do, 3)); |
|
| 407 | - $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 408 | - $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 409 | - $opts = []; |
|
| 410 | - if (preg_match(',primary\s+key,i', $def)) { |
|
| 411 | - $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 412 | - $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 413 | - } |
|
| 414 | - $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 415 | - if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 416 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 417 | - |
|
| 418 | - return false; |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - break; |
|
| 422 | - } |
|
| 423 | - // tout est bon, ouf ! |
|
| 424 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 428 | - |
|
| 429 | - return true; |
|
| 185 | + // 1 |
|
| 186 | + if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 187 | + $debut = $regs[1]; |
|
| 188 | + $table = $regs[3]; |
|
| 189 | + $suite = $regs[4]; |
|
| 190 | + } else { |
|
| 191 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 192 | + |
|
| 193 | + return false; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + // 2 |
|
| 197 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 198 | + // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 199 | + // ou revoir l'api de sql_alter en creant un |
|
| 200 | + // sql_alter_table($table,array($actions)); |
|
| 201 | + $todo = explode(',', $suite); |
|
| 202 | + |
|
| 203 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 204 | + $todo2 = []; |
|
| 205 | + $i = 0; |
|
| 206 | + $ouverte = false; |
|
| 207 | + while ($do = array_shift($todo)) { |
|
| 208 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 209 | + $o = (false !== strpos($do, '(')); |
|
| 210 | + $f = (false !== strpos($do, ')')); |
|
| 211 | + if ($o and !$f) { |
|
| 212 | + $ouverte = true; |
|
| 213 | + } elseif ($f) { |
|
| 214 | + $ouverte = false; |
|
| 215 | + } |
|
| 216 | + if (!$ouverte) { |
|
| 217 | + $i++; |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + // 3 |
|
| 222 | + $resultats = []; |
|
| 223 | + foreach ($todo2 as $do) { |
|
| 224 | + $do = trim($do); |
|
| 225 | + if ( |
|
| 226 | + !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 227 | + . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 228 | + . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 229 | + . '|ADD COLUMN|ADD' |
|
| 230 | + . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 231 | + ) { |
|
| 232 | + spip_log( |
|
| 233 | + "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 234 | + 'sqlite.' . _LOG_ERREUR |
|
| 235 | + ); |
|
| 236 | + |
|
| 237 | + return false; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + $cle = strtoupper($matches[1]); |
|
| 241 | + $colonne_origine = $matches[2]; |
|
| 242 | + $colonne_destination = ''; |
|
| 243 | + |
|
| 244 | + $def = $matches[3]; |
|
| 245 | + |
|
| 246 | + // eluder une eventuelle clause before|after|first inutilisable |
|
| 247 | + $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 248 | + $defo = $defr; // garder la def d'origine pour certains cas |
|
| 249 | + // remplacer les definitions venant de mysql |
|
| 250 | + $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 251 | + |
|
| 252 | + // reinjecter dans le do |
|
| 253 | + $do = str_replace($def, $defr, $do); |
|
| 254 | + $def = $defr; |
|
| 255 | + |
|
| 256 | + switch ($cle) { |
|
| 257 | + // suppression d'un index |
|
| 258 | + case 'DROP KEY': |
|
| 259 | + case 'DROP INDEX': |
|
| 260 | + $nom_index = $colonne_origine; |
|
| 261 | + spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 262 | + break; |
|
| 263 | + |
|
| 264 | + // suppression d'une pk |
|
| 265 | + case 'DROP PRIMARY KEY': |
|
| 266 | + if ( |
|
| 267 | + !_sqlite_modifier_table( |
|
| 268 | + $table, |
|
| 269 | + $colonne_origine, |
|
| 270 | + ['key' => ['PRIMARY KEY' => '']], |
|
| 271 | + $serveur |
|
| 272 | + ) |
|
| 273 | + ) { |
|
| 274 | + return false; |
|
| 275 | + } |
|
| 276 | + break; |
|
| 277 | + // suppression d'une colonne |
|
| 278 | + case 'DROP COLUMN': |
|
| 279 | + case 'DROP': |
|
| 280 | + if ( |
|
| 281 | + !_sqlite_modifier_table( |
|
| 282 | + $table, |
|
| 283 | + [$colonne_origine => ''], |
|
| 284 | + [], |
|
| 285 | + $serveur |
|
| 286 | + ) |
|
| 287 | + ) { |
|
| 288 | + return false; |
|
| 289 | + } |
|
| 290 | + break; |
|
| 291 | + |
|
| 292 | + case 'CHANGE COLUMN': |
|
| 293 | + case 'CHANGE': |
|
| 294 | + // recuperer le nom de la future colonne |
|
| 295 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 296 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 297 | + $def = trim($defo); |
|
| 298 | + $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 299 | + $def = substr($def, strlen($colonne_destination) + 1); |
|
| 300 | + |
|
| 301 | + if ( |
|
| 302 | + !_sqlite_modifier_table( |
|
| 303 | + $table, |
|
| 304 | + [$colonne_origine => $colonne_destination], |
|
| 305 | + ['field' => [$colonne_destination => $def]], |
|
| 306 | + $serveur |
|
| 307 | + ) |
|
| 308 | + ) { |
|
| 309 | + return false; |
|
| 310 | + } |
|
| 311 | + break; |
|
| 312 | + |
|
| 313 | + case 'MODIFY': |
|
| 314 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 315 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 316 | + if ( |
|
| 317 | + !_sqlite_modifier_table( |
|
| 318 | + $table, |
|
| 319 | + $colonne_origine, |
|
| 320 | + ['field' => [$colonne_origine => $defo]], |
|
| 321 | + $serveur |
|
| 322 | + ) |
|
| 323 | + ) { |
|
| 324 | + return false; |
|
| 325 | + } |
|
| 326 | + break; |
|
| 327 | + |
|
| 328 | + // pas geres en sqlite2 |
|
| 329 | + case 'RENAME': |
|
| 330 | + $do = 'RENAME TO' . substr($do, 6); |
|
| 331 | + case 'RENAME TO': |
|
| 332 | + if (!spip_sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 333 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 334 | + |
|
| 335 | + return false; |
|
| 336 | + } |
|
| 337 | + break; |
|
| 338 | + |
|
| 339 | + // ajout d'une pk |
|
| 340 | + case 'ADD PRIMARY KEY': |
|
| 341 | + $pk = trim(substr($do, 16)); |
|
| 342 | + $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 343 | + if ( |
|
| 344 | + !_sqlite_modifier_table( |
|
| 345 | + $table, |
|
| 346 | + $colonne_origine, |
|
| 347 | + ['key' => ['PRIMARY KEY' => $pk]], |
|
| 348 | + $serveur |
|
| 349 | + ) |
|
| 350 | + ) { |
|
| 351 | + return false; |
|
| 352 | + } |
|
| 353 | + break; |
|
| 354 | + // ajout d'un index |
|
| 355 | + case 'ADD UNIQUE KEY': |
|
| 356 | + case 'ADD UNIQUE': |
|
| 357 | + $unique = true; |
|
| 358 | + case 'ADD INDEX': |
|
| 359 | + case 'ADD KEY': |
|
| 360 | + if (!isset($unique)) { |
|
| 361 | + $unique = false; |
|
| 362 | + } |
|
| 363 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 364 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 365 | + // |
|
| 366 | + // nom_index (colonnes) |
|
| 367 | + if ($def) { |
|
| 368 | + $colonnes = substr($def, 1, -1); |
|
| 369 | + $nom_index = $colonne_origine; |
|
| 370 | + } else { |
|
| 371 | + // (colonne) |
|
| 372 | + if ($colonne_origine[0] == '(') { |
|
| 373 | + $colonnes = substr($colonne_origine, 1, -1); |
|
| 374 | + if (false !== strpos(',', $colonnes)) { |
|
| 375 | + spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 376 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 377 | + break; |
|
| 378 | + } else { |
|
| 379 | + $nom_index = $colonnes; |
|
| 380 | + } |
|
| 381 | + } // nom_index |
|
| 382 | + else { |
|
| 383 | + $nom_index = $colonnes = $colonne_origine; |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 387 | + break; |
|
| 388 | + |
|
| 389 | + // pas geres en sqlite2 |
|
| 390 | + case 'ADD COLUMN': |
|
| 391 | + $do = 'ADD' . substr($do, 10); |
|
| 392 | + case 'ADD': |
|
| 393 | + default: |
|
| 394 | + if (!preg_match(',primary\s+key,i', $do)) { |
|
| 395 | + if (!spip_sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 396 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 397 | + |
|
| 398 | + return false; |
|
| 399 | + } |
|
| 400 | + break; |
|
| 401 | + } |
|
| 402 | + // ou si la colonne est aussi primary key |
|
| 403 | + // cas du add id_truc int primary key |
|
| 404 | + // ajout d'une colonne qui passe en primary key directe |
|
| 405 | + else { |
|
| 406 | + $def = trim(substr($do, 3)); |
|
| 407 | + $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 408 | + $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 409 | + $opts = []; |
|
| 410 | + if (preg_match(',primary\s+key,i', $def)) { |
|
| 411 | + $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 412 | + $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 413 | + } |
|
| 414 | + $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 415 | + if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 416 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 417 | + |
|
| 418 | + return false; |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + break; |
|
| 422 | + } |
|
| 423 | + // tout est bon, ouf ! |
|
| 424 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 428 | + |
|
| 429 | + return true; |
|
| 430 | 430 | } |
| 431 | 431 | |
| 432 | 432 | |
@@ -449,38 +449,38 @@ discard block |
||
| 449 | 449 | * - true si la requête réussie, false sinon. |
| 450 | 450 | */ |
| 451 | 451 | function spip_sqlite_create( |
| 452 | - $nom, |
|
| 453 | - $champs, |
|
| 454 | - $cles, |
|
| 455 | - $autoinc = false, |
|
| 456 | - $temporary = false, |
|
| 457 | - $serveur = '', |
|
| 458 | - $requeter = true |
|
| 452 | + $nom, |
|
| 453 | + $champs, |
|
| 454 | + $cles, |
|
| 455 | + $autoinc = false, |
|
| 456 | + $temporary = false, |
|
| 457 | + $serveur = '', |
|
| 458 | + $requeter = true |
|
| 459 | 459 | ) { |
| 460 | - $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 461 | - if (!$query) { |
|
| 462 | - return false; |
|
| 463 | - } |
|
| 464 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 465 | - |
|
| 466 | - // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 467 | - // il faut donc les faire creer ensuite |
|
| 468 | - if (!$requeter) { |
|
| 469 | - return $res; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - $ok = $res ? true : false; |
|
| 473 | - if ($ok) { |
|
| 474 | - foreach ($cles as $k => $v) { |
|
| 475 | - if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 476 | - $index = trim(substr($k, strlen($m[1]))); |
|
| 477 | - $unique = (strlen($m[1]) > 3); |
|
| 478 | - $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - return $ok ? true : false; |
|
| 460 | + $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 461 | + if (!$query) { |
|
| 462 | + return false; |
|
| 463 | + } |
|
| 464 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 465 | + |
|
| 466 | + // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 467 | + // il faut donc les faire creer ensuite |
|
| 468 | + if (!$requeter) { |
|
| 469 | + return $res; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + $ok = $res ? true : false; |
|
| 473 | + if ($ok) { |
|
| 474 | + foreach ($cles as $k => $v) { |
|
| 475 | + if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 476 | + $index = trim(substr($k, strlen($m[1]))); |
|
| 477 | + $unique = (strlen($m[1]) > 3); |
|
| 478 | + $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + return $ok ? true : false; |
|
| 484 | 484 | } |
| 485 | 485 | |
| 486 | 486 | /** |
@@ -493,21 +493,21 @@ discard block |
||
| 493 | 493 | * @return bool true si la base est créee. |
| 494 | 494 | **/ |
| 495 | 495 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) { |
| 496 | - $f = $nom . '.sqlite'; |
|
| 497 | - if (strpos($nom, '/') === false) { |
|
| 498 | - $f = _DIR_DB . $f; |
|
| 499 | - } |
|
| 496 | + $f = $nom . '.sqlite'; |
|
| 497 | + if (strpos($nom, '/') === false) { |
|
| 498 | + $f = _DIR_DB . $f; |
|
| 499 | + } |
|
| 500 | 500 | |
| 501 | - $ok = new \PDO("sqlite:$f"); |
|
| 501 | + $ok = new \PDO("sqlite:$f"); |
|
| 502 | 502 | |
| 503 | - if ($ok) { |
|
| 504 | - unset($ok); |
|
| 503 | + if ($ok) { |
|
| 504 | + unset($ok); |
|
| 505 | 505 | |
| 506 | - return true; |
|
| 507 | - } |
|
| 508 | - unset($ok); |
|
| 506 | + return true; |
|
| 507 | + } |
|
| 508 | + unset($ok); |
|
| 509 | 509 | |
| 510 | - return false; |
|
| 510 | + return false; |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | |
@@ -528,22 +528,22 @@ discard block |
||
| 528 | 528 | * - string texte de la requête si $requeter vaut false |
| 529 | 529 | */ |
| 530 | 530 | function spip_sqlite_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 531 | - if (!$query_select) { |
|
| 532 | - return false; |
|
| 533 | - } |
|
| 534 | - // vue deja presente |
|
| 535 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 536 | - spip_log( |
|
| 537 | - "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 538 | - 'sqlite.' . _LOG_ERREUR |
|
| 539 | - ); |
|
| 531 | + if (!$query_select) { |
|
| 532 | + return false; |
|
| 533 | + } |
|
| 534 | + // vue deja presente |
|
| 535 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 536 | + spip_log( |
|
| 537 | + "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 538 | + 'sqlite.' . _LOG_ERREUR |
|
| 539 | + ); |
|
| 540 | 540 | |
| 541 | - return false; |
|
| 542 | - } |
|
| 541 | + return false; |
|
| 542 | + } |
|
| 543 | 543 | |
| 544 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 544 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 545 | 545 | |
| 546 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 546 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 547 | 547 | } |
| 548 | 548 | |
| 549 | 549 | /** |
@@ -565,54 +565,54 @@ discard block |
||
| 565 | 565 | * string : requête, false si erreur, true sinon. |
| 566 | 566 | */ |
| 567 | 567 | function spip_sqlite_create_index($nom, $table, $champs, $unique = '', $serveur = '', $requeter = true) { |
| 568 | - if (!($nom or $table or $champs)) { |
|
| 569 | - spip_log( |
|
| 570 | - "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 571 | - 'sqlite.' . _LOG_ERREUR |
|
| 572 | - ); |
|
| 573 | - |
|
| 574 | - return false; |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 578 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 579 | - $nom = $table . '_' . $nom; |
|
| 580 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 581 | - if (!is_array($champs)) { |
|
| 582 | - if ($champs[0] == '(') { |
|
| 583 | - $champs = substr($champs, 1, -1); |
|
| 584 | - } |
|
| 585 | - $champs = [$champs]; |
|
| 586 | - // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 587 | - $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - $ifnotexists = ''; |
|
| 591 | - $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 592 | - if (!function_exists('spip_version_compare')) { |
|
| 593 | - include_spip('plugins/installer'); |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 597 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 598 | - } else { |
|
| 599 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 600 | - $a = spip_sqlite_showtable($table, $serveur); |
|
| 601 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 602 | - return true; |
|
| 603 | - } |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 607 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 608 | - if (!$requeter) { |
|
| 609 | - return $res; |
|
| 610 | - } |
|
| 611 | - if ($res) { |
|
| 612 | - return true; |
|
| 613 | - } else { |
|
| 614 | - return false; |
|
| 615 | - } |
|
| 568 | + if (!($nom or $table or $champs)) { |
|
| 569 | + spip_log( |
|
| 570 | + "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 571 | + 'sqlite.' . _LOG_ERREUR |
|
| 572 | + ); |
|
| 573 | + |
|
| 574 | + return false; |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 578 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 579 | + $nom = $table . '_' . $nom; |
|
| 580 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 581 | + if (!is_array($champs)) { |
|
| 582 | + if ($champs[0] == '(') { |
|
| 583 | + $champs = substr($champs, 1, -1); |
|
| 584 | + } |
|
| 585 | + $champs = [$champs]; |
|
| 586 | + // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 587 | + $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + $ifnotexists = ''; |
|
| 591 | + $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 592 | + if (!function_exists('spip_version_compare')) { |
|
| 593 | + include_spip('plugins/installer'); |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 597 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 598 | + } else { |
|
| 599 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 600 | + $a = spip_sqlite_showtable($table, $serveur); |
|
| 601 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 602 | + return true; |
|
| 603 | + } |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 607 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 608 | + if (!$requeter) { |
|
| 609 | + return $res; |
|
| 610 | + } |
|
| 611 | + if ($res) { |
|
| 612 | + return true; |
|
| 613 | + } else { |
|
| 614 | + return false; |
|
| 615 | + } |
|
| 616 | 616 | } |
| 617 | 617 | |
| 618 | 618 | /** |
@@ -629,31 +629,31 @@ discard block |
||
| 629 | 629 | * @return int Nombre de lignes |
| 630 | 630 | */ |
| 631 | 631 | function spip_sqlite_count($r, $serveur = '', $requeter = true) { |
| 632 | - if (!$r) { |
|
| 633 | - return 0; |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - // select ou autre (insert, update,...) ? |
|
| 637 | - // (link,requete) a compter |
|
| 638 | - if (is_array($r->spipSqliteRowCount)) { |
|
| 639 | - list($link, $query) = $r->spipSqliteRowCount; |
|
| 640 | - // amelioration possible a tester intensivement : pas de order by pour compter ! |
|
| 641 | - // $query = preg_replace(",ORDER BY .+(LIMIT\s|HAVING\s|GROUP BY\s|$),Uims","\\1",$query); |
|
| 642 | - $query = "SELECT count(*) as zzzzsqlitecount FROM ($query)"; |
|
| 643 | - $l = $link->query($query); |
|
| 644 | - $i = 0; |
|
| 645 | - if ($l and $z = $l->fetch()) { |
|
| 646 | - $i = $z['zzzzsqlitecount']; |
|
| 647 | - } |
|
| 648 | - $r->spipSqliteRowCount = $i; |
|
| 649 | - } |
|
| 650 | - if (isset($r->spipSqliteRowCount)) { |
|
| 651 | - // Ce compte est faux s'il y a des limit dans la requete :( |
|
| 652 | - // il retourne le nombre d'enregistrements sans le limit |
|
| 653 | - return $r->spipSqliteRowCount; |
|
| 654 | - } else { |
|
| 655 | - return $r->rowCount(); |
|
| 656 | - } |
|
| 632 | + if (!$r) { |
|
| 633 | + return 0; |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + // select ou autre (insert, update,...) ? |
|
| 637 | + // (link,requete) a compter |
|
| 638 | + if (is_array($r->spipSqliteRowCount)) { |
|
| 639 | + list($link, $query) = $r->spipSqliteRowCount; |
|
| 640 | + // amelioration possible a tester intensivement : pas de order by pour compter ! |
|
| 641 | + // $query = preg_replace(",ORDER BY .+(LIMIT\s|HAVING\s|GROUP BY\s|$),Uims","\\1",$query); |
|
| 642 | + $query = "SELECT count(*) as zzzzsqlitecount FROM ($query)"; |
|
| 643 | + $l = $link->query($query); |
|
| 644 | + $i = 0; |
|
| 645 | + if ($l and $z = $l->fetch()) { |
|
| 646 | + $i = $z['zzzzsqlitecount']; |
|
| 647 | + } |
|
| 648 | + $r->spipSqliteRowCount = $i; |
|
| 649 | + } |
|
| 650 | + if (isset($r->spipSqliteRowCount)) { |
|
| 651 | + // Ce compte est faux s'il y a des limit dans la requete :( |
|
| 652 | + // il retourne le nombre d'enregistrements sans le limit |
|
| 653 | + return $r->spipSqliteRowCount; |
|
| 654 | + } else { |
|
| 655 | + return $r->rowCount(); |
|
| 656 | + } |
|
| 657 | 657 | } |
| 658 | 658 | |
| 659 | 659 | |
@@ -672,30 +672,30 @@ discard block |
||
| 672 | 672 | * - false si la requête a échouée |
| 673 | 673 | **/ |
| 674 | 674 | function spip_sqlite_countsel( |
| 675 | - $from = [], |
|
| 676 | - $where = [], |
|
| 677 | - $groupby = '', |
|
| 678 | - $having = [], |
|
| 679 | - $serveur = '', |
|
| 680 | - $requeter = true |
|
| 675 | + $from = [], |
|
| 676 | + $where = [], |
|
| 677 | + $groupby = '', |
|
| 678 | + $having = [], |
|
| 679 | + $serveur = '', |
|
| 680 | + $requeter = true |
|
| 681 | 681 | ) { |
| 682 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 683 | - $r = spip_sqlite_select( |
|
| 684 | - "COUNT($c)", |
|
| 685 | - $from, |
|
| 686 | - $where, |
|
| 687 | - '', |
|
| 688 | - '', |
|
| 689 | - '', |
|
| 690 | - $having, |
|
| 691 | - $serveur, |
|
| 692 | - $requeter |
|
| 693 | - ); |
|
| 694 | - if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 695 | - list($r) = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - return $r; |
|
| 682 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 683 | + $r = spip_sqlite_select( |
|
| 684 | + "COUNT($c)", |
|
| 685 | + $from, |
|
| 686 | + $where, |
|
| 687 | + '', |
|
| 688 | + '', |
|
| 689 | + '', |
|
| 690 | + $having, |
|
| 691 | + $serveur, |
|
| 692 | + $requeter |
|
| 693 | + ); |
|
| 694 | + if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 695 | + list($r) = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + return $r; |
|
| 699 | 699 | } |
| 700 | 700 | |
| 701 | 701 | |
@@ -712,24 +712,24 @@ discard block |
||
| 712 | 712 | * - False en cas d'erreur. |
| 713 | 713 | **/ |
| 714 | 714 | function spip_sqlite_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 715 | - $res = spip_sqlite_query( |
|
| 716 | - _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 717 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 718 | - $serveur, |
|
| 719 | - $requeter |
|
| 720 | - ); |
|
| 715 | + $res = spip_sqlite_query( |
|
| 716 | + _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 717 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 718 | + $serveur, |
|
| 719 | + $requeter |
|
| 720 | + ); |
|
| 721 | 721 | |
| 722 | - // renvoyer la requete inerte si demandee |
|
| 723 | - if (!$requeter) { |
|
| 724 | - return $res; |
|
| 725 | - } |
|
| 722 | + // renvoyer la requete inerte si demandee |
|
| 723 | + if (!$requeter) { |
|
| 724 | + return $res; |
|
| 725 | + } |
|
| 726 | 726 | |
| 727 | - if ($res) { |
|
| 728 | - $link = _sqlite_link($serveur); |
|
| 729 | - return $res->rowCount(); |
|
| 730 | - } else { |
|
| 731 | - return false; |
|
| 732 | - } |
|
| 727 | + if ($res) { |
|
| 728 | + $link = _sqlite_link($serveur); |
|
| 729 | + return $res->rowCount(); |
|
| 730 | + } else { |
|
| 731 | + return false; |
|
| 732 | + } |
|
| 733 | 733 | } |
| 734 | 734 | |
| 735 | 735 | |
@@ -745,15 +745,15 @@ discard block |
||
| 745 | 745 | * - true si la requête a réussie, false sinon |
| 746 | 746 | */ |
| 747 | 747 | function spip_sqlite_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 748 | - if ($exist) { |
|
| 749 | - $exist = ' IF EXISTS'; |
|
| 750 | - } |
|
| 748 | + if ($exist) { |
|
| 749 | + $exist = ' IF EXISTS'; |
|
| 750 | + } |
|
| 751 | 751 | |
| 752 | - if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 753 | - return true; |
|
| 754 | - } else { |
|
| 755 | - return false; |
|
| 756 | - } |
|
| 752 | + if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 753 | + return true; |
|
| 754 | + } else { |
|
| 755 | + return false; |
|
| 756 | + } |
|
| 757 | 757 | } |
| 758 | 758 | |
| 759 | 759 | |
@@ -769,11 +769,11 @@ discard block |
||
| 769 | 769 | * - true si la requête a réussie, false sinon |
| 770 | 770 | */ |
| 771 | 771 | function spip_sqlite_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 772 | - if ($exist) { |
|
| 773 | - $exist = ' IF EXISTS'; |
|
| 774 | - } |
|
| 772 | + if ($exist) { |
|
| 773 | + $exist = ' IF EXISTS'; |
|
| 774 | + } |
|
| 775 | 775 | |
| 776 | - return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 776 | + return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 777 | 777 | } |
| 778 | 778 | |
| 779 | 779 | /** |
@@ -787,20 +787,20 @@ discard block |
||
| 787 | 787 | * @return bool ou requete |
| 788 | 788 | */ |
| 789 | 789 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) { |
| 790 | - if (!($nom or $table)) { |
|
| 791 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 790 | + if (!($nom or $table)) { |
|
| 791 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 792 | 792 | |
| 793 | - return false; |
|
| 794 | - } |
|
| 793 | + return false; |
|
| 794 | + } |
|
| 795 | 795 | |
| 796 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 797 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 798 | - $index = $table . '_' . $nom; |
|
| 799 | - $exist = ' IF EXISTS'; |
|
| 796 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 797 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 798 | + $index = $table . '_' . $nom; |
|
| 799 | + $exist = ' IF EXISTS'; |
|
| 800 | 800 | |
| 801 | - $query = "DROP INDEX$exist $index"; |
|
| 801 | + $query = "DROP INDEX$exist $index"; |
|
| 802 | 802 | |
| 803 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 803 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 804 | 804 | } |
| 805 | 805 | |
| 806 | 806 | /** |
@@ -816,28 +816,28 @@ discard block |
||
| 816 | 816 | * Erreur eventuelle |
| 817 | 817 | **/ |
| 818 | 818 | function spip_sqlite_error($query = '', $serveur = '') { |
| 819 | - $link = _sqlite_link($serveur); |
|
| 819 | + $link = _sqlite_link($serveur); |
|
| 820 | 820 | |
| 821 | - if ($link) { |
|
| 822 | - $errs = $link->errorInfo(); |
|
| 823 | - $s = _sqlite_last_error_from_link($link); |
|
| 824 | - } else { |
|
| 825 | - $s = ': aucune ressource sqlite (link)'; |
|
| 826 | - } |
|
| 827 | - if ($s) { |
|
| 828 | - $trace = debug_backtrace(); |
|
| 829 | - if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 830 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 831 | - } |
|
| 832 | - } |
|
| 821 | + if ($link) { |
|
| 822 | + $errs = $link->errorInfo(); |
|
| 823 | + $s = _sqlite_last_error_from_link($link); |
|
| 824 | + } else { |
|
| 825 | + $s = ': aucune ressource sqlite (link)'; |
|
| 826 | + } |
|
| 827 | + if ($s) { |
|
| 828 | + $trace = debug_backtrace(); |
|
| 829 | + if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 830 | + spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 831 | + } |
|
| 832 | + } |
|
| 833 | 833 | |
| 834 | - return $s; |
|
| 834 | + return $s; |
|
| 835 | 835 | } |
| 836 | 836 | |
| 837 | 837 | function _sqlite_last_error_from_link($link) { |
| 838 | - if ($link) { |
|
| 839 | - $errs = $link->errorInfo(); |
|
| 840 | - /* |
|
| 838 | + if ($link) { |
|
| 839 | + $errs = $link->errorInfo(); |
|
| 840 | + /* |
|
| 841 | 841 | $errs[0] |
| 842 | 842 | numero SQLState ('HY000' souvent lors d'une erreur) |
| 843 | 843 | http://www.easysoft.com/developer/interfaces/odbc/sqlstate_status_return_codes.html |
@@ -847,11 +847,11 @@ discard block |
||
| 847 | 847 | $errs[2] |
| 848 | 848 | Le texte du message d'erreur |
| 849 | 849 | */ |
| 850 | - if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 851 | - return "$errs[2]"; |
|
| 852 | - } |
|
| 853 | - } |
|
| 854 | - return ''; |
|
| 850 | + if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 851 | + return "$errs[2]"; |
|
| 852 | + } |
|
| 853 | + } |
|
| 854 | + return ''; |
|
| 855 | 855 | } |
| 856 | 856 | |
| 857 | 857 | /** |
@@ -868,23 +868,23 @@ discard block |
||
| 868 | 868 | * 'HY000/1' : numéro de l'erreur SQLState / numéro d'erreur interne SQLite (en sqlite 3) |
| 869 | 869 | **/ |
| 870 | 870 | function spip_sqlite_errno($serveur = '') { |
| 871 | - $link = _sqlite_link($serveur); |
|
| 871 | + $link = _sqlite_link($serveur); |
|
| 872 | 872 | |
| 873 | - if ($link) { |
|
| 874 | - $t = $link->errorInfo(); |
|
| 875 | - $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 876 | - if ($s) { |
|
| 877 | - $s .= ' / ' . $t[1]; |
|
| 878 | - } // ajoute l'erreur du moteur SQLite |
|
| 879 | - } else { |
|
| 880 | - $s = ': aucune ressource sqlite (link)'; |
|
| 881 | - } |
|
| 873 | + if ($link) { |
|
| 874 | + $t = $link->errorInfo(); |
|
| 875 | + $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 876 | + if ($s) { |
|
| 877 | + $s .= ' / ' . $t[1]; |
|
| 878 | + } // ajoute l'erreur du moteur SQLite |
|
| 879 | + } else { |
|
| 880 | + $s = ': aucune ressource sqlite (link)'; |
|
| 881 | + } |
|
| 882 | 882 | |
| 883 | - if ($s) { |
|
| 884 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 885 | - } |
|
| 883 | + if ($s) { |
|
| 884 | + spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 885 | + } |
|
| 886 | 886 | |
| 887 | - return $s ? $s : 0; |
|
| 887 | + return $s ? $s : 0; |
|
| 888 | 888 | } |
| 889 | 889 | |
| 890 | 890 | |
@@ -900,19 +900,19 @@ discard block |
||
| 900 | 900 | * - false si on a pas pu avoir d'explication |
| 901 | 901 | */ |
| 902 | 902 | function spip_sqlite_explain($query, $serveur = '', $requeter = true) { |
| 903 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 904 | - return []; |
|
| 905 | - } |
|
| 903 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 904 | + return []; |
|
| 905 | + } |
|
| 906 | 906 | |
| 907 | - $query = spip_sqlite::traduire_requete($query, $serveur); |
|
| 908 | - $query = 'EXPLAIN ' . $query; |
|
| 909 | - if (!$requeter) { |
|
| 910 | - return $query; |
|
| 911 | - } |
|
| 912 | - // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 913 | - $r = spip_sqlite::executer_requete($query, $serveur, false); |
|
| 907 | + $query = spip_sqlite::traduire_requete($query, $serveur); |
|
| 908 | + $query = 'EXPLAIN ' . $query; |
|
| 909 | + if (!$requeter) { |
|
| 910 | + return $query; |
|
| 911 | + } |
|
| 912 | + // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 913 | + $r = spip_sqlite::executer_requete($query, $serveur, false); |
|
| 914 | 914 | |
| 915 | - return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 915 | + return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 916 | 916 | } |
| 917 | 917 | |
| 918 | 918 | |
@@ -929,30 +929,30 @@ discard block |
||
| 929 | 929 | */ |
| 930 | 930 | function spip_sqlite_fetch($r, $t = '', $serveur = '', $requeter = true) { |
| 931 | 931 | |
| 932 | - $link = _sqlite_link($serveur); |
|
| 933 | - $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 932 | + $link = _sqlite_link($serveur); |
|
| 933 | + $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 934 | 934 | |
| 935 | - $retour = false; |
|
| 936 | - if ($r) { |
|
| 937 | - $retour = $r->fetch($t); |
|
| 938 | - } |
|
| 935 | + $retour = false; |
|
| 936 | + if ($r) { |
|
| 937 | + $retour = $r->fetch($t); |
|
| 938 | + } |
|
| 939 | 939 | |
| 940 | - // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 941 | - // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 942 | - // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 943 | - if ( |
|
| 944 | - $retour |
|
| 945 | - and strpos(implode('', array_keys($retour)), '.') !== false |
|
| 946 | - ) { |
|
| 947 | - foreach ($retour as $cle => $val) { |
|
| 948 | - if (($pos = strpos($cle, '.')) !== false) { |
|
| 949 | - $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 950 | - unset($retour[$cle]); |
|
| 951 | - } |
|
| 952 | - } |
|
| 953 | - } |
|
| 940 | + // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 941 | + // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 942 | + // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 943 | + if ( |
|
| 944 | + $retour |
|
| 945 | + and strpos(implode('', array_keys($retour)), '.') !== false |
|
| 946 | + ) { |
|
| 947 | + foreach ($retour as $cle => $val) { |
|
| 948 | + if (($pos = strpos($cle, '.')) !== false) { |
|
| 949 | + $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 950 | + unset($retour[$cle]); |
|
| 951 | + } |
|
| 952 | + } |
|
| 953 | + } |
|
| 954 | 954 | |
| 955 | - return $retour; |
|
| 955 | + return $retour; |
|
| 956 | 956 | } |
| 957 | 957 | |
| 958 | 958 | /** |
@@ -965,8 +965,8 @@ discard block |
||
| 965 | 965 | * @return bool True si déplacement réussi, false sinon. |
| 966 | 966 | **/ |
| 967 | 967 | function spip_sqlite_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 968 | - // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 969 | - return false; |
|
| 968 | + // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 969 | + return false; |
|
| 970 | 970 | } |
| 971 | 971 | |
| 972 | 972 | |
@@ -982,10 +982,10 @@ discard block |
||
| 982 | 982 | * @return bool True si réussi |
| 983 | 983 | */ |
| 984 | 984 | function spip_sqlite_free(&$r, $serveur = '', $requeter = true) { |
| 985 | - unset($r); |
|
| 985 | + unset($r); |
|
| 986 | 986 | |
| 987 | - return true; |
|
| 988 | - //return sqlite_free_result($r); |
|
| 987 | + return true; |
|
| 988 | + //return sqlite_free_result($r); |
|
| 989 | 989 | } |
| 990 | 990 | |
| 991 | 991 | |
@@ -1000,8 +1000,8 @@ discard block |
||
| 1000 | 1000 | * @return void |
| 1001 | 1001 | */ |
| 1002 | 1002 | function spip_sqlite_get_charset($charset = [], $serveur = '', $requeter = true) { |
| 1003 | - //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1004 | - //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1003 | + //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1004 | + //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1005 | 1005 | } |
| 1006 | 1006 | |
| 1007 | 1007 | |
@@ -1016,7 +1016,7 @@ discard block |
||
| 1016 | 1016 | * Valeur hexadécimale pour SQLite |
| 1017 | 1017 | **/ |
| 1018 | 1018 | function spip_sqlite_hex($v) { |
| 1019 | - return hexdec($v); |
|
| 1019 | + return hexdec($v); |
|
| 1020 | 1020 | } |
| 1021 | 1021 | |
| 1022 | 1022 | |
@@ -1038,7 +1038,7 @@ discard block |
||
| 1038 | 1038 | * Expression de requête SQL |
| 1039 | 1039 | **/ |
| 1040 | 1040 | function spip_sqlite_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) { |
| 1041 | - return "($val $not IN ($valeurs))"; |
|
| 1041 | + return "($val $not IN ($valeurs))"; |
|
| 1042 | 1042 | } |
| 1043 | 1043 | |
| 1044 | 1044 | |
@@ -1066,20 +1066,20 @@ discard block |
||
| 1066 | 1066 | **/ |
| 1067 | 1067 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 1068 | 1068 | |
| 1069 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1070 | - if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1071 | - if (!$requeter) { |
|
| 1072 | - return $r; |
|
| 1073 | - } |
|
| 1074 | - $nb = spip_sqlite::last_insert_id($serveur); |
|
| 1075 | - } else { |
|
| 1076 | - $nb = false; |
|
| 1077 | - } |
|
| 1069 | + $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1070 | + if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1071 | + if (!$requeter) { |
|
| 1072 | + return $r; |
|
| 1073 | + } |
|
| 1074 | + $nb = spip_sqlite::last_insert_id($serveur); |
|
| 1075 | + } else { |
|
| 1076 | + $nb = false; |
|
| 1077 | + } |
|
| 1078 | 1078 | |
| 1079 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1079 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1080 | 1080 | |
| 1081 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1082 | - return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1081 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1082 | + return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1083 | 1083 | } |
| 1084 | 1084 | |
| 1085 | 1085 | |
@@ -1104,28 +1104,28 @@ discard block |
||
| 1104 | 1104 | * - Tableau de description de la requête et du temps d'exécution, si var_profile activé |
| 1105 | 1105 | **/ |
| 1106 | 1106 | function spip_sqlite_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1107 | - if (!$desc) { |
|
| 1108 | - $desc = description_table($table, $serveur); |
|
| 1109 | - } |
|
| 1110 | - if (!$desc) { |
|
| 1111 | - die("$table insertion sans description"); |
|
| 1112 | - } |
|
| 1113 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1107 | + if (!$desc) { |
|
| 1108 | + $desc = description_table($table, $serveur); |
|
| 1109 | + } |
|
| 1110 | + if (!$desc) { |
|
| 1111 | + die("$table insertion sans description"); |
|
| 1112 | + } |
|
| 1113 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1114 | 1114 | |
| 1115 | - foreach ($couples as $champ => $val) { |
|
| 1116 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1117 | - } |
|
| 1115 | + foreach ($couples as $champ => $val) { |
|
| 1116 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1117 | + } |
|
| 1118 | 1118 | |
| 1119 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1120 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1119 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1120 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1121 | 1121 | |
| 1122 | - $cles = $valeurs = ''; |
|
| 1123 | - if (count($couples)) { |
|
| 1124 | - $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1125 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1126 | - } |
|
| 1122 | + $cles = $valeurs = ''; |
|
| 1123 | + if (count($couples)) { |
|
| 1124 | + $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1125 | + $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1126 | + } |
|
| 1127 | 1127 | |
| 1128 | - return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1128 | + return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1129 | 1129 | } |
| 1130 | 1130 | |
| 1131 | 1131 | |
@@ -1149,70 +1149,70 @@ discard block |
||
| 1149 | 1149 | * - False en cas d'erreur. |
| 1150 | 1150 | **/ |
| 1151 | 1151 | function spip_sqlite_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1152 | - if (!$desc) { |
|
| 1153 | - $desc = description_table($table, $serveur); |
|
| 1154 | - } |
|
| 1155 | - if (!$desc) { |
|
| 1156 | - die("$table insertion sans description"); |
|
| 1157 | - } |
|
| 1158 | - if (!isset($desc['field'])) { |
|
| 1159 | - $desc['field'] = []; |
|
| 1160 | - } |
|
| 1161 | - |
|
| 1162 | - // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1163 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1164 | - |
|
| 1165 | - // seul le nom de la table est a traduire ici : |
|
| 1166 | - // le faire une seule fois au debut |
|
| 1167 | - $query_start = "INSERT INTO $table "; |
|
| 1168 | - $query_start = spip_sqlite::traduire_requete($query_start, $serveur); |
|
| 1169 | - |
|
| 1170 | - // ouvrir une transaction |
|
| 1171 | - if ($requeter) { |
|
| 1172 | - spip_sqlite::demarrer_transaction($serveur); |
|
| 1173 | - } |
|
| 1174 | - |
|
| 1175 | - while ($couples = array_shift($tab_couples)) { |
|
| 1176 | - foreach ($couples as $champ => $val) { |
|
| 1177 | - $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1178 | - } |
|
| 1179 | - |
|
| 1180 | - // inserer les champs timestamp par defaut |
|
| 1181 | - $couples = array_merge($maj, $couples); |
|
| 1182 | - |
|
| 1183 | - $champs = $valeurs = ''; |
|
| 1184 | - if (count($couples)) { |
|
| 1185 | - $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1186 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1187 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1188 | - } else { |
|
| 1189 | - $query = $query_start . 'DEFAULT VALUES'; |
|
| 1190 | - } |
|
| 1191 | - |
|
| 1192 | - if ($requeter) { |
|
| 1193 | - $retour = spip_sqlite::executer_requete($query, $serveur); |
|
| 1194 | - } |
|
| 1195 | - |
|
| 1196 | - // sur le dernier couple uniquement |
|
| 1197 | - if (!count($tab_couples)) { |
|
| 1198 | - $nb = 0; |
|
| 1199 | - if ($requeter) { |
|
| 1200 | - $nb = spip_sqlite::last_insert_id($serveur); |
|
| 1201 | - } else { |
|
| 1202 | - return $query; |
|
| 1203 | - } |
|
| 1204 | - } |
|
| 1205 | - |
|
| 1206 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1207 | - } |
|
| 1208 | - |
|
| 1209 | - if ($requeter) { |
|
| 1210 | - spip_sqlite::finir_transaction($serveur); |
|
| 1211 | - } |
|
| 1212 | - |
|
| 1213 | - // renvoie le dernier id d'autoincrement ajoute |
|
| 1214 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1215 | - return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1152 | + if (!$desc) { |
|
| 1153 | + $desc = description_table($table, $serveur); |
|
| 1154 | + } |
|
| 1155 | + if (!$desc) { |
|
| 1156 | + die("$table insertion sans description"); |
|
| 1157 | + } |
|
| 1158 | + if (!isset($desc['field'])) { |
|
| 1159 | + $desc['field'] = []; |
|
| 1160 | + } |
|
| 1161 | + |
|
| 1162 | + // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1163 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1164 | + |
|
| 1165 | + // seul le nom de la table est a traduire ici : |
|
| 1166 | + // le faire une seule fois au debut |
|
| 1167 | + $query_start = "INSERT INTO $table "; |
|
| 1168 | + $query_start = spip_sqlite::traduire_requete($query_start, $serveur); |
|
| 1169 | + |
|
| 1170 | + // ouvrir une transaction |
|
| 1171 | + if ($requeter) { |
|
| 1172 | + spip_sqlite::demarrer_transaction($serveur); |
|
| 1173 | + } |
|
| 1174 | + |
|
| 1175 | + while ($couples = array_shift($tab_couples)) { |
|
| 1176 | + foreach ($couples as $champ => $val) { |
|
| 1177 | + $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1178 | + } |
|
| 1179 | + |
|
| 1180 | + // inserer les champs timestamp par defaut |
|
| 1181 | + $couples = array_merge($maj, $couples); |
|
| 1182 | + |
|
| 1183 | + $champs = $valeurs = ''; |
|
| 1184 | + if (count($couples)) { |
|
| 1185 | + $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1186 | + $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1187 | + $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1188 | + } else { |
|
| 1189 | + $query = $query_start . 'DEFAULT VALUES'; |
|
| 1190 | + } |
|
| 1191 | + |
|
| 1192 | + if ($requeter) { |
|
| 1193 | + $retour = spip_sqlite::executer_requete($query, $serveur); |
|
| 1194 | + } |
|
| 1195 | + |
|
| 1196 | + // sur le dernier couple uniquement |
|
| 1197 | + if (!count($tab_couples)) { |
|
| 1198 | + $nb = 0; |
|
| 1199 | + if ($requeter) { |
|
| 1200 | + $nb = spip_sqlite::last_insert_id($serveur); |
|
| 1201 | + } else { |
|
| 1202 | + return $query; |
|
| 1203 | + } |
|
| 1204 | + } |
|
| 1205 | + |
|
| 1206 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1207 | + } |
|
| 1208 | + |
|
| 1209 | + if ($requeter) { |
|
| 1210 | + spip_sqlite::finir_transaction($serveur); |
|
| 1211 | + } |
|
| 1212 | + |
|
| 1213 | + // renvoie le dernier id d'autoincrement ajoute |
|
| 1214 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1215 | + return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1216 | 1216 | } |
| 1217 | 1217 | |
| 1218 | 1218 | |
@@ -1227,7 +1227,7 @@ discard block |
||
| 1227 | 1227 | * Toujours true. |
| 1228 | 1228 | **/ |
| 1229 | 1229 | function spip_sqlite_preferer_transaction($serveur = '', $requeter = true) { |
| 1230 | - return true; |
|
| 1230 | + return true; |
|
| 1231 | 1231 | } |
| 1232 | 1232 | |
| 1233 | 1233 | /** |
@@ -1244,12 +1244,12 @@ discard block |
||
| 1244 | 1244 | * string si texte de la requête demandé, true sinon |
| 1245 | 1245 | **/ |
| 1246 | 1246 | function spip_sqlite_demarrer_transaction($serveur = '', $requeter = true) { |
| 1247 | - if (!$requeter) { |
|
| 1248 | - return 'BEGIN TRANSACTION'; |
|
| 1249 | - } |
|
| 1250 | - spip_sqlite::demarrer_transaction($serveur); |
|
| 1247 | + if (!$requeter) { |
|
| 1248 | + return 'BEGIN TRANSACTION'; |
|
| 1249 | + } |
|
| 1250 | + spip_sqlite::demarrer_transaction($serveur); |
|
| 1251 | 1251 | |
| 1252 | - return true; |
|
| 1252 | + return true; |
|
| 1253 | 1253 | } |
| 1254 | 1254 | |
| 1255 | 1255 | /** |
@@ -1263,12 +1263,12 @@ discard block |
||
| 1263 | 1263 | * string si texte de la requête demandé, true sinon |
| 1264 | 1264 | **/ |
| 1265 | 1265 | function spip_sqlite_terminer_transaction($serveur = '', $requeter = true) { |
| 1266 | - if (!$requeter) { |
|
| 1267 | - return 'COMMIT'; |
|
| 1268 | - } |
|
| 1269 | - spip_sqlite::finir_transaction($serveur); |
|
| 1266 | + if (!$requeter) { |
|
| 1267 | + return 'COMMIT'; |
|
| 1268 | + } |
|
| 1269 | + spip_sqlite::finir_transaction($serveur); |
|
| 1270 | 1270 | |
| 1271 | - return true; |
|
| 1271 | + return true; |
|
| 1272 | 1272 | } |
| 1273 | 1273 | |
| 1274 | 1274 | |
@@ -1283,27 +1283,27 @@ discard block |
||
| 1283 | 1283 | * Liste des noms de bases |
| 1284 | 1284 | **/ |
| 1285 | 1285 | function spip_sqlite_listdbs($serveur = '', $requeter = true) { |
| 1286 | - _sqlite_init(); |
|
| 1286 | + _sqlite_init(); |
|
| 1287 | 1287 | |
| 1288 | - if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1289 | - return []; |
|
| 1290 | - } |
|
| 1288 | + if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1289 | + return []; |
|
| 1290 | + } |
|
| 1291 | 1291 | |
| 1292 | - include_spip('inc/flock'); |
|
| 1293 | - $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1294 | - $bds = []; |
|
| 1292 | + include_spip('inc/flock'); |
|
| 1293 | + $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1294 | + $bds = []; |
|
| 1295 | 1295 | |
| 1296 | - foreach ($bases as $b) { |
|
| 1297 | - // pas de bases commencant pas sqlite |
|
| 1298 | - // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1299 | - // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1300 | - if (strpos($b, '_sqlite')) { |
|
| 1301 | - continue; |
|
| 1302 | - } |
|
| 1303 | - $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1304 | - } |
|
| 1296 | + foreach ($bases as $b) { |
|
| 1297 | + // pas de bases commencant pas sqlite |
|
| 1298 | + // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1299 | + // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1300 | + if (strpos($b, '_sqlite')) { |
|
| 1301 | + continue; |
|
| 1302 | + } |
|
| 1303 | + $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1304 | + } |
|
| 1305 | 1305 | |
| 1306 | - return $bds; |
|
| 1306 | + return $bds; |
|
| 1307 | 1307 | } |
| 1308 | 1308 | |
| 1309 | 1309 | |
@@ -1318,9 +1318,9 @@ discard block |
||
| 1318 | 1318 | * @return string Texte de sélection pour la requête |
| 1319 | 1319 | */ |
| 1320 | 1320 | function spip_sqlite_multi($objet, $lang) { |
| 1321 | - $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1321 | + $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1322 | 1322 | |
| 1323 | - return $r; |
|
| 1323 | + return $r; |
|
| 1324 | 1324 | } |
| 1325 | 1325 | |
| 1326 | 1326 | |
@@ -1337,15 +1337,15 @@ discard block |
||
| 1337 | 1337 | * @return bool|string true / false / requete |
| 1338 | 1338 | **/ |
| 1339 | 1339 | function spip_sqlite_optimize($table, $serveur = '', $requeter = true) { |
| 1340 | - static $do = false; |
|
| 1341 | - if ($requeter and $do) { |
|
| 1342 | - return true; |
|
| 1343 | - } |
|
| 1344 | - if ($requeter) { |
|
| 1345 | - $do = true; |
|
| 1346 | - } |
|
| 1340 | + static $do = false; |
|
| 1341 | + if ($requeter and $do) { |
|
| 1342 | + return true; |
|
| 1343 | + } |
|
| 1344 | + if ($requeter) { |
|
| 1345 | + $do = true; |
|
| 1346 | + } |
|
| 1347 | 1347 | |
| 1348 | - return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1348 | + return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1349 | 1349 | } |
| 1350 | 1350 | |
| 1351 | 1351 | |
@@ -1362,15 +1362,15 @@ discard block |
||
| 1362 | 1362 | * Donnée prête à être utilisée par le gestionnaire SQL |
| 1363 | 1363 | */ |
| 1364 | 1364 | function spip_sqlite_quote($v, $type = '') { |
| 1365 | - if (!is_array($v)) { |
|
| 1366 | - return _sqlite_calculer_cite($v, $type); |
|
| 1367 | - } |
|
| 1368 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1369 | - foreach ($v as $k => $r) { |
|
| 1370 | - $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1371 | - } |
|
| 1365 | + if (!is_array($v)) { |
|
| 1366 | + return _sqlite_calculer_cite($v, $type); |
|
| 1367 | + } |
|
| 1368 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1369 | + foreach ($v as $k => $r) { |
|
| 1370 | + $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1371 | + } |
|
| 1372 | 1372 | |
| 1373 | - return join(',', $v); |
|
| 1373 | + return join(',', $v); |
|
| 1374 | 1374 | } |
| 1375 | 1375 | |
| 1376 | 1376 | |
@@ -1387,9 +1387,9 @@ discard block |
||
| 1387 | 1387 | * Expression SQL |
| 1388 | 1388 | **/ |
| 1389 | 1389 | function spip_sqlite_date_proche($champ, $interval, $unite) { |
| 1390 | - $op = (($interval <= 0) ? '>' : '<'); |
|
| 1390 | + $op = (($interval <= 0) ? '>' : '<'); |
|
| 1391 | 1391 | |
| 1392 | - return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1392 | + return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1393 | 1393 | } |
| 1394 | 1394 | |
| 1395 | 1395 | |
@@ -1407,48 +1407,48 @@ discard block |
||
| 1407 | 1407 | * l'état de la table après la réparation |
| 1408 | 1408 | */ |
| 1409 | 1409 | function spip_sqlite_repair($table, $serveur = '', $requeter = true) { |
| 1410 | - if ( |
|
| 1411 | - $desc = spip_sqlite_showtable($table, $serveur) |
|
| 1412 | - and isset($desc['field']) |
|
| 1413 | - and is_array($desc['field']) |
|
| 1414 | - ) { |
|
| 1415 | - foreach ($desc['field'] as $c => $d) { |
|
| 1416 | - if ( |
|
| 1417 | - preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', $d) |
|
| 1418 | - and stripos($d, 'NOT NULL') !== false |
|
| 1419 | - and stripos($d, 'DEFAULT') === false |
|
| 1420 | - /* pas touche aux cles primaires */ |
|
| 1421 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1422 | - ) { |
|
| 1423 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1424 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1425 | - } |
|
| 1426 | - if ( |
|
| 1427 | - preg_match(',^(INTEGER),i', $d) |
|
| 1428 | - and stripos($d, 'NOT NULL') !== false |
|
| 1429 | - and stripos($d, 'DEFAULT') === false |
|
| 1430 | - /* pas touche aux cles primaires */ |
|
| 1431 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1432 | - ) { |
|
| 1433 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1434 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1435 | - } |
|
| 1436 | - if ( |
|
| 1437 | - preg_match(',^(datetime),i', $d) |
|
| 1438 | - and stripos($d, 'NOT NULL') !== false |
|
| 1439 | - and stripos($d, 'DEFAULT') === false |
|
| 1440 | - /* pas touche aux cles primaires */ |
|
| 1441 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1442 | - ) { |
|
| 1443 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1444 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1445 | - } |
|
| 1446 | - } |
|
| 1447 | - |
|
| 1448 | - return [' OK ']; |
|
| 1449 | - } |
|
| 1450 | - |
|
| 1451 | - return [' ERROR ']; |
|
| 1410 | + if ( |
|
| 1411 | + $desc = spip_sqlite_showtable($table, $serveur) |
|
| 1412 | + and isset($desc['field']) |
|
| 1413 | + and is_array($desc['field']) |
|
| 1414 | + ) { |
|
| 1415 | + foreach ($desc['field'] as $c => $d) { |
|
| 1416 | + if ( |
|
| 1417 | + preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', $d) |
|
| 1418 | + and stripos($d, 'NOT NULL') !== false |
|
| 1419 | + and stripos($d, 'DEFAULT') === false |
|
| 1420 | + /* pas touche aux cles primaires */ |
|
| 1421 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1422 | + ) { |
|
| 1423 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1424 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1425 | + } |
|
| 1426 | + if ( |
|
| 1427 | + preg_match(',^(INTEGER),i', $d) |
|
| 1428 | + and stripos($d, 'NOT NULL') !== false |
|
| 1429 | + and stripos($d, 'DEFAULT') === false |
|
| 1430 | + /* pas touche aux cles primaires */ |
|
| 1431 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1432 | + ) { |
|
| 1433 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1434 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1435 | + } |
|
| 1436 | + if ( |
|
| 1437 | + preg_match(',^(datetime),i', $d) |
|
| 1438 | + and stripos($d, 'NOT NULL') !== false |
|
| 1439 | + and stripos($d, 'DEFAULT') === false |
|
| 1440 | + /* pas touche aux cles primaires */ |
|
| 1441 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1442 | + ) { |
|
| 1443 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1444 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1445 | + } |
|
| 1446 | + } |
|
| 1447 | + |
|
| 1448 | + return [' OK ']; |
|
| 1449 | + } |
|
| 1450 | + |
|
| 1451 | + return [' ERROR ']; |
|
| 1452 | 1452 | } |
| 1453 | 1453 | |
| 1454 | 1454 | |
@@ -1477,25 +1477,25 @@ discard block |
||
| 1477 | 1477 | * - False en cas d'erreur. |
| 1478 | 1478 | **/ |
| 1479 | 1479 | function spip_sqlite_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) { |
| 1480 | - if (!$desc) { |
|
| 1481 | - $desc = description_table($table, $serveur); |
|
| 1482 | - } |
|
| 1483 | - if (!$desc) { |
|
| 1484 | - die("$table insertion sans description"); |
|
| 1485 | - } |
|
| 1486 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1480 | + if (!$desc) { |
|
| 1481 | + $desc = description_table($table, $serveur); |
|
| 1482 | + } |
|
| 1483 | + if (!$desc) { |
|
| 1484 | + die("$table insertion sans description"); |
|
| 1485 | + } |
|
| 1486 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1487 | 1487 | |
| 1488 | - foreach ($couples as $champ => $val) { |
|
| 1489 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1490 | - } |
|
| 1488 | + foreach ($couples as $champ => $val) { |
|
| 1489 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1490 | + } |
|
| 1491 | 1491 | |
| 1492 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1493 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1492 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1493 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1494 | 1494 | |
| 1495 | - return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1496 | - ',', |
|
| 1497 | - $couples |
|
| 1498 | - ) . ')', $serveur); |
|
| 1495 | + return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1496 | + ',', |
|
| 1497 | + $couples |
|
| 1498 | + ) . ')', $serveur); |
|
| 1499 | 1499 | } |
| 1500 | 1500 | |
| 1501 | 1501 | |
@@ -1525,13 +1525,13 @@ discard block |
||
| 1525 | 1525 | **/ |
| 1526 | 1526 | function spip_sqlite_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1527 | 1527 | |
| 1528 | - // boucler pour trainter chaque requete independemment |
|
| 1529 | - foreach ($tab_couples as $couples) { |
|
| 1530 | - $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1531 | - } |
|
| 1528 | + // boucler pour trainter chaque requete independemment |
|
| 1529 | + foreach ($tab_couples as $couples) { |
|
| 1530 | + $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1531 | + } |
|
| 1532 | 1532 | |
| 1533 | - // renvoie le dernier id |
|
| 1534 | - return $retour; |
|
| 1533 | + // renvoie le dernier id |
|
| 1534 | + return $retour; |
|
| 1535 | 1535 | } |
| 1536 | 1536 | |
| 1537 | 1537 | |
@@ -1558,44 +1558,44 @@ discard block |
||
| 1558 | 1558 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 1559 | 1559 | */ |
| 1560 | 1560 | function spip_sqlite_select( |
| 1561 | - $select, |
|
| 1562 | - $from, |
|
| 1563 | - $where = '', |
|
| 1564 | - $groupby = '', |
|
| 1565 | - $orderby = '', |
|
| 1566 | - $limit = '', |
|
| 1567 | - $having = '', |
|
| 1568 | - $serveur = '', |
|
| 1569 | - $requeter = true |
|
| 1561 | + $select, |
|
| 1562 | + $from, |
|
| 1563 | + $where = '', |
|
| 1564 | + $groupby = '', |
|
| 1565 | + $orderby = '', |
|
| 1566 | + $limit = '', |
|
| 1567 | + $having = '', |
|
| 1568 | + $serveur = '', |
|
| 1569 | + $requeter = true |
|
| 1570 | 1570 | ) { |
| 1571 | 1571 | |
| 1572 | - // version() n'est pas connu de sqlite |
|
| 1573 | - $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1572 | + // version() n'est pas connu de sqlite |
|
| 1573 | + $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1574 | 1574 | |
| 1575 | - // recomposer from |
|
| 1576 | - $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1575 | + // recomposer from |
|
| 1576 | + $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1577 | 1577 | |
| 1578 | - $query = |
|
| 1579 | - _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1580 | - . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1581 | - . _sqlite_calculer_expression('WHERE', $where) |
|
| 1582 | - . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1583 | - . _sqlite_calculer_expression('HAVING', $having) |
|
| 1584 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1585 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1578 | + $query = |
|
| 1579 | + _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1580 | + . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1581 | + . _sqlite_calculer_expression('WHERE', $where) |
|
| 1582 | + . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1583 | + . _sqlite_calculer_expression('HAVING', $having) |
|
| 1584 | + . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1585 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1586 | 1586 | |
| 1587 | - // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1588 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1589 | - // texte de la requete demande ? |
|
| 1590 | - if (!$requeter) { |
|
| 1591 | - return $res; |
|
| 1592 | - } |
|
| 1593 | - // erreur survenue ? |
|
| 1594 | - if ($res === false) { |
|
| 1595 | - return spip_sqlite::traduire_requete($query, $serveur); |
|
| 1596 | - } |
|
| 1587 | + // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1588 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1589 | + // texte de la requete demande ? |
|
| 1590 | + if (!$requeter) { |
|
| 1591 | + return $res; |
|
| 1592 | + } |
|
| 1593 | + // erreur survenue ? |
|
| 1594 | + if ($res === false) { |
|
| 1595 | + return spip_sqlite::traduire_requete($query, $serveur); |
|
| 1596 | + } |
|
| 1597 | 1597 | |
| 1598 | - return $res; |
|
| 1598 | + return $res; |
|
| 1599 | 1599 | } |
| 1600 | 1600 | |
| 1601 | 1601 | |
@@ -1614,32 +1614,32 @@ discard block |
||
| 1614 | 1614 | * - False en cas d'erreur. |
| 1615 | 1615 | **/ |
| 1616 | 1616 | function spip_sqlite_selectdb($db, $serveur = '', $requeter = true) { |
| 1617 | - _sqlite_init(); |
|
| 1617 | + _sqlite_init(); |
|
| 1618 | 1618 | |
| 1619 | - // interdire la creation d'une nouvelle base, |
|
| 1620 | - // sauf si on est dans l'installation |
|
| 1621 | - if ( |
|
| 1622 | - !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1623 | - && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1624 | - ) { |
|
| 1625 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1619 | + // interdire la creation d'une nouvelle base, |
|
| 1620 | + // sauf si on est dans l'installation |
|
| 1621 | + if ( |
|
| 1622 | + !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1623 | + && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1624 | + ) { |
|
| 1625 | + spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1626 | 1626 | |
| 1627 | - return false; |
|
| 1628 | - } |
|
| 1627 | + return false; |
|
| 1628 | + } |
|
| 1629 | 1629 | |
| 1630 | - // se connecter a la base indiquee |
|
| 1631 | - // avec les identifiants connus |
|
| 1632 | - $index = $serveur ? $serveur : 0; |
|
| 1630 | + // se connecter a la base indiquee |
|
| 1631 | + // avec les identifiants connus |
|
| 1632 | + $index = $serveur ? $serveur : 0; |
|
| 1633 | 1633 | |
| 1634 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1635 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1636 | - return $db; |
|
| 1637 | - } |
|
| 1638 | - } else { |
|
| 1639 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1634 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1635 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1636 | + return $db; |
|
| 1637 | + } |
|
| 1638 | + } else { |
|
| 1639 | + spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1640 | 1640 | |
| 1641 | - return false; |
|
| 1642 | - } |
|
| 1641 | + return false; |
|
| 1642 | + } |
|
| 1643 | 1643 | } |
| 1644 | 1644 | |
| 1645 | 1645 | |
@@ -1654,8 +1654,8 @@ discard block |
||
| 1654 | 1654 | * @return void |
| 1655 | 1655 | */ |
| 1656 | 1656 | function spip_sqlite_set_charset($charset, $serveur = '', $requeter = true) { |
| 1657 | - # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1658 | - # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1657 | + # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1658 | + # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1659 | 1659 | } |
| 1660 | 1660 | |
| 1661 | 1661 | |
@@ -1673,24 +1673,24 @@ discard block |
||
| 1673 | 1673 | * Ressource à utiliser avec sql_fetch() |
| 1674 | 1674 | **/ |
| 1675 | 1675 | function spip_sqlite_showbase($match, $serveur = '', $requeter = true) { |
| 1676 | - // type est le type d'entrée : table / index / view |
|
| 1677 | - // on ne retourne que les tables (?) et non les vues... |
|
| 1678 | - # ESCAPE non supporte par les versions sqlite <3 |
|
| 1679 | - # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1680 | - $match = preg_quote($match); |
|
| 1681 | - $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1682 | - $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1683 | - $match = str_replace('_', '.', $match); |
|
| 1684 | - $match = str_replace('%', '.*', $match); |
|
| 1685 | - $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1686 | - $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1687 | - $match = "^$match$"; |
|
| 1688 | - |
|
| 1689 | - return spip_sqlite_query( |
|
| 1690 | - "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1691 | - $serveur, |
|
| 1692 | - $requeter |
|
| 1693 | - ); |
|
| 1676 | + // type est le type d'entrée : table / index / view |
|
| 1677 | + // on ne retourne que les tables (?) et non les vues... |
|
| 1678 | + # ESCAPE non supporte par les versions sqlite <3 |
|
| 1679 | + # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1680 | + $match = preg_quote($match); |
|
| 1681 | + $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1682 | + $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1683 | + $match = str_replace('_', '.', $match); |
|
| 1684 | + $match = str_replace('%', '.*', $match); |
|
| 1685 | + $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1686 | + $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1687 | + $match = "^$match$"; |
|
| 1688 | + |
|
| 1689 | + return spip_sqlite_query( |
|
| 1690 | + "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1691 | + $serveur, |
|
| 1692 | + $requeter |
|
| 1693 | + ); |
|
| 1694 | 1694 | } |
| 1695 | 1695 | |
| 1696 | 1696 | /** |
@@ -1708,19 +1708,19 @@ discard block |
||
| 1708 | 1708 | * - string : requete sql, si $requeter = true |
| 1709 | 1709 | **/ |
| 1710 | 1710 | function spip_sqlite_table_exists(string $table, $serveur = '', $requeter = true) { |
| 1711 | - $r = spip_sqlite_query( |
|
| 1712 | - 'SELECT name FROM sqlite_master WHERE' |
|
| 1713 | - . ' type=\'table\'' |
|
| 1714 | - . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1715 | - . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1716 | - $serveur, |
|
| 1717 | - $requeter |
|
| 1718 | - ); |
|
| 1719 | - if (!$requeter) { |
|
| 1720 | - return $r; |
|
| 1721 | - } |
|
| 1722 | - $res = spip_sqlite_fetch($r); |
|
| 1723 | - return (bool) $res; |
|
| 1711 | + $r = spip_sqlite_query( |
|
| 1712 | + 'SELECT name FROM sqlite_master WHERE' |
|
| 1713 | + . ' type=\'table\'' |
|
| 1714 | + . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1715 | + . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1716 | + $serveur, |
|
| 1717 | + $requeter |
|
| 1718 | + ); |
|
| 1719 | + if (!$requeter) { |
|
| 1720 | + return $r; |
|
| 1721 | + } |
|
| 1722 | + $res = spip_sqlite_fetch($r); |
|
| 1723 | + return (bool) $res; |
|
| 1724 | 1724 | } |
| 1725 | 1725 | |
| 1726 | 1726 | define('_SQLITE_RE_SHOW_TABLE', '/^[^(),]*\(((?:[^()]*\((?:[^()]*\([^()]*\))?[^()]*\)[^()]*)*[^()]*)\)[^()]*$/'); |
@@ -1743,129 +1743,129 @@ discard block |
||
| 1743 | 1743 | * - array description de la table sinon |
| 1744 | 1744 | */ |
| 1745 | 1745 | function spip_sqlite_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1746 | - $query = |
|
| 1747 | - 'SELECT sql, type FROM' |
|
| 1748 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1749 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1750 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1751 | - . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1752 | - . ' ORDER BY substr(type,2,1), name'; |
|
| 1753 | - |
|
| 1754 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1755 | - if (!$a) { |
|
| 1756 | - return ''; |
|
| 1757 | - } |
|
| 1758 | - if (!$requeter) { |
|
| 1759 | - return $a; |
|
| 1760 | - } |
|
| 1761 | - if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1762 | - return ''; |
|
| 1763 | - } |
|
| 1764 | - $vue = ($a['type'] == 'view'); // table | vue |
|
| 1765 | - |
|
| 1766 | - // c'est une table |
|
| 1767 | - // il faut parser le create |
|
| 1768 | - if (!$vue) { |
|
| 1769 | - if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1770 | - return ''; |
|
| 1771 | - } else { |
|
| 1772 | - $desc = $r[1]; |
|
| 1773 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1774 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1775 | - if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1776 | - $namedkeys = $r[2]; |
|
| 1777 | - $desc = $r[1]; |
|
| 1778 | - } else { |
|
| 1779 | - $namedkeys = ''; |
|
| 1780 | - } |
|
| 1781 | - |
|
| 1782 | - $fields = []; |
|
| 1783 | - $keys = []; |
|
| 1784 | - |
|
| 1785 | - // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1786 | - // par exemple s'il contiennent une virgule. |
|
| 1787 | - // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1788 | - list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1789 | - |
|
| 1790 | - // separer toutes les descriptions de champs, separes par des virgules |
|
| 1791 | - # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1792 | - $k_precedent = null; |
|
| 1793 | - foreach (explode(',', $desc) as $v) { |
|
| 1794 | - preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1795 | - // Les cles de champs peuvent etre entourees |
|
| 1796 | - // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1797 | - // http://www.sqlite.org/lang_keywords.html |
|
| 1798 | - $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1799 | - if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1800 | - $k = trim($k, $char); |
|
| 1801 | - if ($char == '[') { |
|
| 1802 | - $k = rtrim($k, ']'); |
|
| 1803 | - } |
|
| 1804 | - } |
|
| 1805 | - $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1806 | - |
|
| 1807 | - // rustine pour DECIMAL(10,2) |
|
| 1808 | - // s'il y a une parenthèse fermante dans la clé |
|
| 1809 | - // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1810 | - if (false !== strpos($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
|
| 1811 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1812 | - continue; |
|
| 1813 | - } |
|
| 1814 | - |
|
| 1815 | - // la primary key peut etre dans une des descriptions de champs |
|
| 1816 | - // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1817 | - if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1818 | - $keys['PRIMARY KEY'] = $k; |
|
| 1819 | - } |
|
| 1820 | - |
|
| 1821 | - $fields[$k] = $def; |
|
| 1822 | - $k_precedent = $k; |
|
| 1823 | - } |
|
| 1824 | - // key inclues dans la requete |
|
| 1825 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1826 | - if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 1827 | - $k = str_replace('`', '', trim($r[1])); |
|
| 1828 | - $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1829 | - if ($k && !isset($keys[$k])) { |
|
| 1830 | - $keys[$k] = $t; |
|
| 1831 | - } else { |
|
| 1832 | - $keys[] = $t; |
|
| 1833 | - } |
|
| 1834 | - } |
|
| 1835 | - } |
|
| 1836 | - // sinon ajouter les key index |
|
| 1837 | - $query = |
|
| 1838 | - 'SELECT name,sql FROM' |
|
| 1839 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1840 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1841 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1842 | - . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1843 | - . 'ORDER BY substr(type,2,1), name'; |
|
| 1844 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1845 | - while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1846 | - $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1847 | - $keytype = 'KEY'; |
|
| 1848 | - if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
|
| 1849 | - $keytype = 'UNIQUE KEY'; |
|
| 1850 | - } |
|
| 1851 | - $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1852 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1853 | - } |
|
| 1854 | - } |
|
| 1855 | - } // c'est une vue, on liste les champs disponibles simplement |
|
| 1856 | - else { |
|
| 1857 | - if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1858 | - $fields = []; |
|
| 1859 | - foreach ($res as $c => $v) { |
|
| 1860 | - $fields[$c] = ''; |
|
| 1861 | - } |
|
| 1862 | - $keys = []; |
|
| 1863 | - } else { |
|
| 1864 | - return ''; |
|
| 1865 | - } |
|
| 1866 | - } |
|
| 1867 | - |
|
| 1868 | - return ['field' => $fields, 'key' => $keys]; |
|
| 1746 | + $query = |
|
| 1747 | + 'SELECT sql, type FROM' |
|
| 1748 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1749 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1750 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1751 | + . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1752 | + . ' ORDER BY substr(type,2,1), name'; |
|
| 1753 | + |
|
| 1754 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1755 | + if (!$a) { |
|
| 1756 | + return ''; |
|
| 1757 | + } |
|
| 1758 | + if (!$requeter) { |
|
| 1759 | + return $a; |
|
| 1760 | + } |
|
| 1761 | + if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1762 | + return ''; |
|
| 1763 | + } |
|
| 1764 | + $vue = ($a['type'] == 'view'); // table | vue |
|
| 1765 | + |
|
| 1766 | + // c'est une table |
|
| 1767 | + // il faut parser le create |
|
| 1768 | + if (!$vue) { |
|
| 1769 | + if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1770 | + return ''; |
|
| 1771 | + } else { |
|
| 1772 | + $desc = $r[1]; |
|
| 1773 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1774 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1775 | + if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1776 | + $namedkeys = $r[2]; |
|
| 1777 | + $desc = $r[1]; |
|
| 1778 | + } else { |
|
| 1779 | + $namedkeys = ''; |
|
| 1780 | + } |
|
| 1781 | + |
|
| 1782 | + $fields = []; |
|
| 1783 | + $keys = []; |
|
| 1784 | + |
|
| 1785 | + // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1786 | + // par exemple s'il contiennent une virgule. |
|
| 1787 | + // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1788 | + list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1789 | + |
|
| 1790 | + // separer toutes les descriptions de champs, separes par des virgules |
|
| 1791 | + # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1792 | + $k_precedent = null; |
|
| 1793 | + foreach (explode(',', $desc) as $v) { |
|
| 1794 | + preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1795 | + // Les cles de champs peuvent etre entourees |
|
| 1796 | + // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1797 | + // http://www.sqlite.org/lang_keywords.html |
|
| 1798 | + $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1799 | + if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1800 | + $k = trim($k, $char); |
|
| 1801 | + if ($char == '[') { |
|
| 1802 | + $k = rtrim($k, ']'); |
|
| 1803 | + } |
|
| 1804 | + } |
|
| 1805 | + $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1806 | + |
|
| 1807 | + // rustine pour DECIMAL(10,2) |
|
| 1808 | + // s'il y a une parenthèse fermante dans la clé |
|
| 1809 | + // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1810 | + if (false !== strpos($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
|
| 1811 | + $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1812 | + continue; |
|
| 1813 | + } |
|
| 1814 | + |
|
| 1815 | + // la primary key peut etre dans une des descriptions de champs |
|
| 1816 | + // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1817 | + if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1818 | + $keys['PRIMARY KEY'] = $k; |
|
| 1819 | + } |
|
| 1820 | + |
|
| 1821 | + $fields[$k] = $def; |
|
| 1822 | + $k_precedent = $k; |
|
| 1823 | + } |
|
| 1824 | + // key inclues dans la requete |
|
| 1825 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1826 | + if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 1827 | + $k = str_replace('`', '', trim($r[1])); |
|
| 1828 | + $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1829 | + if ($k && !isset($keys[$k])) { |
|
| 1830 | + $keys[$k] = $t; |
|
| 1831 | + } else { |
|
| 1832 | + $keys[] = $t; |
|
| 1833 | + } |
|
| 1834 | + } |
|
| 1835 | + } |
|
| 1836 | + // sinon ajouter les key index |
|
| 1837 | + $query = |
|
| 1838 | + 'SELECT name,sql FROM' |
|
| 1839 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1840 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1841 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1842 | + . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1843 | + . 'ORDER BY substr(type,2,1), name'; |
|
| 1844 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1845 | + while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1846 | + $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1847 | + $keytype = 'KEY'; |
|
| 1848 | + if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
|
| 1849 | + $keytype = 'UNIQUE KEY'; |
|
| 1850 | + } |
|
| 1851 | + $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1852 | + $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1853 | + } |
|
| 1854 | + } |
|
| 1855 | + } // c'est une vue, on liste les champs disponibles simplement |
|
| 1856 | + else { |
|
| 1857 | + if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1858 | + $fields = []; |
|
| 1859 | + foreach ($res as $c => $v) { |
|
| 1860 | + $fields[$c] = ''; |
|
| 1861 | + } |
|
| 1862 | + $keys = []; |
|
| 1863 | + } else { |
|
| 1864 | + return ''; |
|
| 1865 | + } |
|
| 1866 | + } |
|
| 1867 | + |
|
| 1868 | + return ['field' => $fields, 'key' => $keys]; |
|
| 1869 | 1869 | } |
| 1870 | 1870 | |
| 1871 | 1871 | |
@@ -1891,22 +1891,22 @@ discard block |
||
| 1891 | 1891 | * - array Tableau décrivant la requête et son temps d'exécution si var_profile est actif |
| 1892 | 1892 | */ |
| 1893 | 1893 | function spip_sqlite_update($table, $champs, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1894 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1895 | - $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1894 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1895 | + $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1896 | 1896 | |
| 1897 | - $set = []; |
|
| 1898 | - foreach ($champs as $champ => $val) { |
|
| 1899 | - $set[] = $champ . "=$val"; |
|
| 1900 | - } |
|
| 1901 | - if (!empty($set)) { |
|
| 1902 | - return spip_sqlite_query( |
|
| 1903 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1904 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1905 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1906 | - $serveur, |
|
| 1907 | - $requeter |
|
| 1908 | - ); |
|
| 1909 | - } |
|
| 1897 | + $set = []; |
|
| 1898 | + foreach ($champs as $champ => $val) { |
|
| 1899 | + $set[] = $champ . "=$val"; |
|
| 1900 | + } |
|
| 1901 | + if (!empty($set)) { |
|
| 1902 | + return spip_sqlite_query( |
|
| 1903 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1904 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1905 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1906 | + $serveur, |
|
| 1907 | + $requeter |
|
| 1908 | + ); |
|
| 1909 | + } |
|
| 1910 | 1910 | } |
| 1911 | 1911 | |
| 1912 | 1912 | |
@@ -1936,38 +1936,38 @@ discard block |
||
| 1936 | 1936 | */ |
| 1937 | 1937 | function spip_sqlite_updateq($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1938 | 1938 | |
| 1939 | - if (!$champs) { |
|
| 1940 | - return; |
|
| 1941 | - } |
|
| 1942 | - if (!$desc) { |
|
| 1943 | - $desc = description_table($table, $serveur); |
|
| 1944 | - } |
|
| 1945 | - if (!$desc) { |
|
| 1946 | - die("$table insertion sans description"); |
|
| 1947 | - } |
|
| 1948 | - $fields = $desc['field']; |
|
| 1949 | - |
|
| 1950 | - $set = []; |
|
| 1951 | - foreach ($champs as $champ => $val) { |
|
| 1952 | - $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 1953 | - } |
|
| 1954 | - |
|
| 1955 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1956 | - // attention ils sont deja quotes |
|
| 1957 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1958 | - foreach ($maj as $champ => $val) { |
|
| 1959 | - if (!isset($set[$champ])) { |
|
| 1960 | - $set[$champ] = $champ . '=' . $val; |
|
| 1961 | - } |
|
| 1962 | - } |
|
| 1963 | - |
|
| 1964 | - return spip_sqlite_query( |
|
| 1965 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1966 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1967 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1968 | - $serveur, |
|
| 1969 | - $requeter |
|
| 1970 | - ); |
|
| 1939 | + if (!$champs) { |
|
| 1940 | + return; |
|
| 1941 | + } |
|
| 1942 | + if (!$desc) { |
|
| 1943 | + $desc = description_table($table, $serveur); |
|
| 1944 | + } |
|
| 1945 | + if (!$desc) { |
|
| 1946 | + die("$table insertion sans description"); |
|
| 1947 | + } |
|
| 1948 | + $fields = $desc['field']; |
|
| 1949 | + |
|
| 1950 | + $set = []; |
|
| 1951 | + foreach ($champs as $champ => $val) { |
|
| 1952 | + $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 1953 | + } |
|
| 1954 | + |
|
| 1955 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1956 | + // attention ils sont deja quotes |
|
| 1957 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1958 | + foreach ($maj as $champ => $val) { |
|
| 1959 | + if (!isset($set[$champ])) { |
|
| 1960 | + $set[$champ] = $champ . '=' . $val; |
|
| 1961 | + } |
|
| 1962 | + } |
|
| 1963 | + |
|
| 1964 | + return spip_sqlite_query( |
|
| 1965 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1966 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1967 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1968 | + $serveur, |
|
| 1969 | + $requeter |
|
| 1970 | + ); |
|
| 1971 | 1971 | } |
| 1972 | 1972 | |
| 1973 | 1973 | |
@@ -1985,17 +1985,17 @@ discard block |
||
| 1985 | 1985 | * @return void |
| 1986 | 1986 | */ |
| 1987 | 1987 | function _sqlite_init() { |
| 1988 | - if (!defined('_DIR_DB')) { |
|
| 1989 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 1990 | - } |
|
| 1991 | - if (!defined('_SQLITE_CHMOD')) { |
|
| 1992 | - define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 1993 | - } |
|
| 1988 | + if (!defined('_DIR_DB')) { |
|
| 1989 | + define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 1990 | + } |
|
| 1991 | + if (!defined('_SQLITE_CHMOD')) { |
|
| 1992 | + define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 1993 | + } |
|
| 1994 | 1994 | |
| 1995 | - if (!is_dir($d = _DIR_DB)) { |
|
| 1996 | - include_spip('inc/flock'); |
|
| 1997 | - sous_repertoire($d); |
|
| 1998 | - } |
|
| 1995 | + if (!is_dir($d = _DIR_DB)) { |
|
| 1996 | + include_spip('inc/flock'); |
|
| 1997 | + sous_repertoire($d); |
|
| 1998 | + } |
|
| 1999 | 1999 | } |
| 2000 | 2000 | |
| 2001 | 2001 | |
@@ -2009,20 +2009,20 @@ discard block |
||
| 2009 | 2009 | * @return bool|int |
| 2010 | 2010 | */ |
| 2011 | 2011 | function _sqlite_is_version($version = '', $link = '', $serveur = '', $requeter = true) { |
| 2012 | - if ($link === '') { |
|
| 2013 | - $link = _sqlite_link($serveur); |
|
| 2014 | - } |
|
| 2015 | - if (!$link) { |
|
| 2016 | - return false; |
|
| 2017 | - } |
|
| 2012 | + if ($link === '') { |
|
| 2013 | + $link = _sqlite_link($serveur); |
|
| 2014 | + } |
|
| 2015 | + if (!$link) { |
|
| 2016 | + return false; |
|
| 2017 | + } |
|
| 2018 | 2018 | |
| 2019 | - $v = 3; |
|
| 2019 | + $v = 3; |
|
| 2020 | 2020 | |
| 2021 | - if (!$version) { |
|
| 2022 | - return $v; |
|
| 2023 | - } |
|
| 2021 | + if (!$version) { |
|
| 2022 | + return $v; |
|
| 2023 | + } |
|
| 2024 | 2024 | |
| 2025 | - return ($version == $v); |
|
| 2025 | + return ($version == $v); |
|
| 2026 | 2026 | } |
| 2027 | 2027 | |
| 2028 | 2028 | |
@@ -2033,9 +2033,9 @@ discard block |
||
| 2033 | 2033 | * @return PDO Information de connexion pour SQLite |
| 2034 | 2034 | */ |
| 2035 | 2035 | function _sqlite_link($serveur = '') { |
| 2036 | - $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 2036 | + $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 2037 | 2037 | |
| 2038 | - return $link; |
|
| 2038 | + return $link; |
|
| 2039 | 2039 | } |
| 2040 | 2040 | |
| 2041 | 2041 | |
@@ -2050,52 +2050,52 @@ discard block |
||
| 2050 | 2050 | * @return string|number Texte ou nombre échappé |
| 2051 | 2051 | */ |
| 2052 | 2052 | function _sqlite_calculer_cite($v, $type) { |
| 2053 | - if ($type) { |
|
| 2054 | - if ( |
|
| 2055 | - is_null($v) |
|
| 2056 | - and stripos($type, 'NOT NULL') === false |
|
| 2057 | - ) { |
|
| 2058 | - return 'NULL'; |
|
| 2059 | - } // null php se traduit en NULL SQL |
|
| 2060 | - |
|
| 2061 | - if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 2062 | - return $v; |
|
| 2063 | - } |
|
| 2064 | - if (sql_test_int($type)) { |
|
| 2065 | - if (is_numeric($v)) { |
|
| 2066 | - return $v; |
|
| 2067 | - } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) == 0) { |
|
| 2068 | - return hexdec(substr($v, 2)); |
|
| 2069 | - } else { |
|
| 2070 | - return intval($v); |
|
| 2071 | - } |
|
| 2072 | - } |
|
| 2073 | - } else { |
|
| 2074 | - // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2075 | - if (is_bool($v)) { |
|
| 2076 | - return strval(intval($v)); |
|
| 2077 | - } |
|
| 2078 | - elseif (is_numeric($v)) { |
|
| 2079 | - return strval($v); |
|
| 2080 | - } |
|
| 2081 | - } |
|
| 2082 | - |
|
| 2083 | - // trouver un link sqlite pour faire l'echappement |
|
| 2084 | - foreach ($GLOBALS['connexions'] as $s) { |
|
| 2085 | - if ( |
|
| 2086 | - $l = $s['link'] |
|
| 2087 | - and is_object($l) |
|
| 2088 | - and $l instanceof \PDO |
|
| 2089 | - and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2090 | - ) { |
|
| 2091 | - return $l->quote($v); |
|
| 2092 | - } |
|
| 2093 | - } |
|
| 2094 | - |
|
| 2095 | - // echapper les ' en '' |
|
| 2096 | - spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2097 | - |
|
| 2098 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2053 | + if ($type) { |
|
| 2054 | + if ( |
|
| 2055 | + is_null($v) |
|
| 2056 | + and stripos($type, 'NOT NULL') === false |
|
| 2057 | + ) { |
|
| 2058 | + return 'NULL'; |
|
| 2059 | + } // null php se traduit en NULL SQL |
|
| 2060 | + |
|
| 2061 | + if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 2062 | + return $v; |
|
| 2063 | + } |
|
| 2064 | + if (sql_test_int($type)) { |
|
| 2065 | + if (is_numeric($v)) { |
|
| 2066 | + return $v; |
|
| 2067 | + } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) == 0) { |
|
| 2068 | + return hexdec(substr($v, 2)); |
|
| 2069 | + } else { |
|
| 2070 | + return intval($v); |
|
| 2071 | + } |
|
| 2072 | + } |
|
| 2073 | + } else { |
|
| 2074 | + // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2075 | + if (is_bool($v)) { |
|
| 2076 | + return strval(intval($v)); |
|
| 2077 | + } |
|
| 2078 | + elseif (is_numeric($v)) { |
|
| 2079 | + return strval($v); |
|
| 2080 | + } |
|
| 2081 | + } |
|
| 2082 | + |
|
| 2083 | + // trouver un link sqlite pour faire l'echappement |
|
| 2084 | + foreach ($GLOBALS['connexions'] as $s) { |
|
| 2085 | + if ( |
|
| 2086 | + $l = $s['link'] |
|
| 2087 | + and is_object($l) |
|
| 2088 | + and $l instanceof \PDO |
|
| 2089 | + and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2090 | + ) { |
|
| 2091 | + return $l->quote($v); |
|
| 2092 | + } |
|
| 2093 | + } |
|
| 2094 | + |
|
| 2095 | + // echapper les ' en '' |
|
| 2096 | + spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2097 | + |
|
| 2098 | + return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2099 | 2099 | } |
| 2100 | 2100 | |
| 2101 | 2101 | |
@@ -2111,21 +2111,21 @@ discard block |
||
| 2111 | 2111 | * @return string Texte de l'expression, une partie donc, du texte la requête. |
| 2112 | 2112 | */ |
| 2113 | 2113 | function _sqlite_calculer_expression($expression, $v, $join = 'AND') { |
| 2114 | - if (empty($v)) { |
|
| 2115 | - return ''; |
|
| 2116 | - } |
|
| 2114 | + if (empty($v)) { |
|
| 2115 | + return ''; |
|
| 2116 | + } |
|
| 2117 | 2117 | |
| 2118 | - $exp = "\n$expression "; |
|
| 2118 | + $exp = "\n$expression "; |
|
| 2119 | 2119 | |
| 2120 | - if (!is_array($v)) { |
|
| 2121 | - return $exp . $v; |
|
| 2122 | - } else { |
|
| 2123 | - if (strtoupper($join) === 'AND') { |
|
| 2124 | - return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2125 | - } else { |
|
| 2126 | - return $exp . join($join, $v); |
|
| 2127 | - } |
|
| 2128 | - } |
|
| 2120 | + if (!is_array($v)) { |
|
| 2121 | + return $exp . $v; |
|
| 2122 | + } else { |
|
| 2123 | + if (strtoupper($join) === 'AND') { |
|
| 2124 | + return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2125 | + } else { |
|
| 2126 | + return $exp . join($join, $v); |
|
| 2127 | + } |
|
| 2128 | + } |
|
| 2129 | 2129 | } |
| 2130 | 2130 | |
| 2131 | 2131 | |
@@ -2141,7 +2141,7 @@ discard block |
||
| 2141 | 2141 | * @return string Texte du orderby préparé |
| 2142 | 2142 | */ |
| 2143 | 2143 | function _sqlite_calculer_order($orderby) { |
| 2144 | - return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 2144 | + return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 2145 | 2145 | } |
| 2146 | 2146 | |
| 2147 | 2147 | |
@@ -2152,26 +2152,26 @@ discard block |
||
| 2152 | 2152 | * @return string Sélection de colonnes pour une clause SELECT |
| 2153 | 2153 | */ |
| 2154 | 2154 | function _sqlite_calculer_select_as($args) { |
| 2155 | - $res = ''; |
|
| 2156 | - foreach ($args as $k => $v) { |
|
| 2157 | - if (substr($k, -1) == '@') { |
|
| 2158 | - // c'est une jointure qui se refere au from precedent |
|
| 2159 | - // pas de virgule |
|
| 2160 | - $res .= ' ' . $v; |
|
| 2161 | - } else { |
|
| 2162 | - if (!is_numeric($k)) { |
|
| 2163 | - $p = strpos($v, ' '); |
|
| 2164 | - if ($p) { |
|
| 2165 | - $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2166 | - } else { |
|
| 2167 | - $v .= " AS '$k'"; |
|
| 2168 | - } |
|
| 2169 | - } |
|
| 2170 | - $res .= ', ' . $v; |
|
| 2171 | - } |
|
| 2172 | - } |
|
| 2173 | - |
|
| 2174 | - return substr($res, 2); |
|
| 2155 | + $res = ''; |
|
| 2156 | + foreach ($args as $k => $v) { |
|
| 2157 | + if (substr($k, -1) == '@') { |
|
| 2158 | + // c'est une jointure qui se refere au from precedent |
|
| 2159 | + // pas de virgule |
|
| 2160 | + $res .= ' ' . $v; |
|
| 2161 | + } else { |
|
| 2162 | + if (!is_numeric($k)) { |
|
| 2163 | + $p = strpos($v, ' '); |
|
| 2164 | + if ($p) { |
|
| 2165 | + $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2166 | + } else { |
|
| 2167 | + $v .= " AS '$k'"; |
|
| 2168 | + } |
|
| 2169 | + } |
|
| 2170 | + $res .= ', ' . $v; |
|
| 2171 | + } |
|
| 2172 | + } |
|
| 2173 | + |
|
| 2174 | + return substr($res, 2); |
|
| 2175 | 2175 | } |
| 2176 | 2176 | |
| 2177 | 2177 | |
@@ -2194,26 +2194,26 @@ discard block |
||
| 2194 | 2194 | * Contrainte pour clause WHERE |
| 2195 | 2195 | */ |
| 2196 | 2196 | function _sqlite_calculer_where($v) { |
| 2197 | - if (!is_array($v)) { |
|
| 2198 | - return $v; |
|
| 2199 | - } |
|
| 2200 | - |
|
| 2201 | - $op = array_shift($v); |
|
| 2202 | - if (!($n = count($v))) { |
|
| 2203 | - return $op; |
|
| 2204 | - } else { |
|
| 2205 | - $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2206 | - if ($n == 1) { |
|
| 2207 | - return "$op($arg)"; |
|
| 2208 | - } else { |
|
| 2209 | - $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2210 | - if ($n == 2) { |
|
| 2211 | - return "($arg $op $arg2)"; |
|
| 2212 | - } else { |
|
| 2213 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 2214 | - } |
|
| 2215 | - } |
|
| 2216 | - } |
|
| 2197 | + if (!is_array($v)) { |
|
| 2198 | + return $v; |
|
| 2199 | + } |
|
| 2200 | + |
|
| 2201 | + $op = array_shift($v); |
|
| 2202 | + if (!($n = count($v))) { |
|
| 2203 | + return $op; |
|
| 2204 | + } else { |
|
| 2205 | + $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2206 | + if ($n == 1) { |
|
| 2207 | + return "$op($arg)"; |
|
| 2208 | + } else { |
|
| 2209 | + $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2210 | + if ($n == 2) { |
|
| 2211 | + return "($arg $op $arg2)"; |
|
| 2212 | + } else { |
|
| 2213 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 2214 | + } |
|
| 2215 | + } |
|
| 2216 | + } |
|
| 2217 | 2217 | } |
| 2218 | 2218 | |
| 2219 | 2219 | |
@@ -2228,19 +2228,19 @@ discard block |
||
| 2228 | 2228 | * @return array|bool |
| 2229 | 2229 | */ |
| 2230 | 2230 | function _sqlite_charger_version($version = '') { |
| 2231 | - $versions = []; |
|
| 2231 | + $versions = []; |
|
| 2232 | 2232 | |
| 2233 | - // version 3 |
|
| 2234 | - if (!$version || $version == 3) { |
|
| 2235 | - if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2236 | - $versions[] = 3; |
|
| 2237 | - } |
|
| 2238 | - } |
|
| 2239 | - if ($version) { |
|
| 2240 | - return in_array($version, $versions); |
|
| 2241 | - } |
|
| 2233 | + // version 3 |
|
| 2234 | + if (!$version || $version == 3) { |
|
| 2235 | + if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2236 | + $versions[] = 3; |
|
| 2237 | + } |
|
| 2238 | + } |
|
| 2239 | + if ($version) { |
|
| 2240 | + return in_array($version, $versions); |
|
| 2241 | + } |
|
| 2242 | 2242 | |
| 2243 | - return $versions; |
|
| 2243 | + return $versions; |
|
| 2244 | 2244 | } |
| 2245 | 2245 | |
| 2246 | 2246 | |
@@ -2278,147 +2278,147 @@ discard block |
||
| 2278 | 2278 | */ |
| 2279 | 2279 | function _sqlite_modifier_table($table, $colonne, $opt = [], $serveur = '') { |
| 2280 | 2280 | |
| 2281 | - if (is_array($table)) { |
|
| 2282 | - $table_destination = reset($table); |
|
| 2283 | - $table_origine = key($table); |
|
| 2284 | - } else { |
|
| 2285 | - $table_origine = $table_destination = $table; |
|
| 2286 | - } |
|
| 2287 | - // ne prend actuellement qu'un changement |
|
| 2288 | - // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2289 | - if (is_array($colonne)) { |
|
| 2290 | - $colonne_destination = reset($colonne); |
|
| 2291 | - $colonne_origine = key($colonne); |
|
| 2292 | - } else { |
|
| 2293 | - $colonne_origine = $colonne_destination = $colonne; |
|
| 2294 | - } |
|
| 2295 | - if (!isset($opt['field'])) { |
|
| 2296 | - $opt['field'] = []; |
|
| 2297 | - } |
|
| 2298 | - if (!isset($opt['key'])) { |
|
| 2299 | - $opt['key'] = []; |
|
| 2300 | - } |
|
| 2301 | - |
|
| 2302 | - // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2303 | - // on prendra directement le nom de la future table |
|
| 2304 | - $meme_table = ($table_origine == $table_destination); |
|
| 2305 | - |
|
| 2306 | - $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2307 | - if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2308 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2309 | - |
|
| 2310 | - return false; |
|
| 2311 | - } |
|
| 2312 | - |
|
| 2313 | - |
|
| 2314 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2315 | - |
|
| 2316 | - // 1) creer une table temporaire avec les modifications |
|
| 2317 | - // - DROP : suppression de la colonne |
|
| 2318 | - // - CHANGE : modification de la colonne |
|
| 2319 | - // (foreach pour conserver l'ordre des champs) |
|
| 2320 | - |
|
| 2321 | - // field |
|
| 2322 | - $fields = []; |
|
| 2323 | - // pour le INSERT INTO plus loin |
|
| 2324 | - // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2325 | - $fields_correspondances = []; |
|
| 2326 | - foreach ($def_origine['field'] as $c => $d) { |
|
| 2327 | - if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2328 | - // si pas DROP |
|
| 2329 | - if ($colonne_destination) { |
|
| 2330 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2331 | - $fields_correspondances[$colonne_destination] = $c; |
|
| 2332 | - } |
|
| 2333 | - } else { |
|
| 2334 | - $fields[$c] = $d; |
|
| 2335 | - $fields_correspondances[$c] = $c; |
|
| 2336 | - } |
|
| 2337 | - } |
|
| 2338 | - // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2339 | - if (!$colonne_origine && $colonne_destination) { |
|
| 2340 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2341 | - } |
|
| 2342 | - |
|
| 2343 | - // key... |
|
| 2344 | - $keys = []; |
|
| 2345 | - foreach ($def_origine['key'] as $c => $d) { |
|
| 2346 | - $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2347 | - $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2348 | - // seulement si on ne supprime pas la colonne ! |
|
| 2349 | - if ($d) { |
|
| 2350 | - $keys[$c] = $d; |
|
| 2351 | - } |
|
| 2352 | - } |
|
| 2353 | - |
|
| 2354 | - // autres keys, on merge |
|
| 2355 | - $keys = array_merge($keys, $opt['key']); |
|
| 2356 | - $queries = []; |
|
| 2357 | - |
|
| 2358 | - // copier dans destination (si differente de origine), sinon tmp |
|
| 2359 | - $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2360 | - $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2361 | - and $keys['PRIMARY KEY'] |
|
| 2362 | - and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2363 | - and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2364 | - |
|
| 2365 | - if ( |
|
| 2366 | - $q = _sqlite_requete_create( |
|
| 2367 | - $table_copie, |
|
| 2368 | - $fields, |
|
| 2369 | - $keys, |
|
| 2370 | - $autoinc, |
|
| 2371 | - $temporary = false, |
|
| 2372 | - $ifnotexists = true, |
|
| 2373 | - $serveur |
|
| 2374 | - ) |
|
| 2375 | - ) { |
|
| 2376 | - $queries[] = $q; |
|
| 2377 | - } |
|
| 2378 | - |
|
| 2379 | - |
|
| 2380 | - // 2) y copier les champs qui vont bien |
|
| 2381 | - $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2382 | - $champs_ori = join(', ', $fields_correspondances); |
|
| 2383 | - $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2384 | - |
|
| 2385 | - // 3) supprimer la table d'origine |
|
| 2386 | - $queries[] = "DROP TABLE $table_origine"; |
|
| 2387 | - |
|
| 2388 | - // 4) renommer la table temporaire |
|
| 2389 | - // avec le nom de la table destination |
|
| 2390 | - // si necessaire |
|
| 2391 | - if ($meme_table) { |
|
| 2392 | - $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2393 | - } |
|
| 2394 | - |
|
| 2395 | - // 5) remettre les index ! |
|
| 2396 | - foreach ($keys as $k => $v) { |
|
| 2397 | - if ($k == 'PRIMARY KEY') { |
|
| 2398 | - } else { |
|
| 2399 | - // enlever KEY |
|
| 2400 | - $k = substr($k, 4); |
|
| 2401 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2402 | - } |
|
| 2403 | - } |
|
| 2404 | - |
|
| 2405 | - |
|
| 2406 | - if (count($queries)) { |
|
| 2407 | - spip_sqlite::demarrer_transaction($serveur); |
|
| 2408 | - // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2409 | - foreach ($queries as $q) { |
|
| 2410 | - if (!spip_sqlite::executer_requete($q, $serveur)) { |
|
| 2411 | - spip_log('SQLite : ALTER TABLE table :' |
|
| 2412 | - . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2413 | - spip_sqlite::annuler_transaction($serveur); |
|
| 2414 | - |
|
| 2415 | - return false; |
|
| 2416 | - } |
|
| 2417 | - } |
|
| 2418 | - spip_sqlite::finir_transaction($serveur); |
|
| 2419 | - } |
|
| 2420 | - |
|
| 2421 | - return true; |
|
| 2281 | + if (is_array($table)) { |
|
| 2282 | + $table_destination = reset($table); |
|
| 2283 | + $table_origine = key($table); |
|
| 2284 | + } else { |
|
| 2285 | + $table_origine = $table_destination = $table; |
|
| 2286 | + } |
|
| 2287 | + // ne prend actuellement qu'un changement |
|
| 2288 | + // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2289 | + if (is_array($colonne)) { |
|
| 2290 | + $colonne_destination = reset($colonne); |
|
| 2291 | + $colonne_origine = key($colonne); |
|
| 2292 | + } else { |
|
| 2293 | + $colonne_origine = $colonne_destination = $colonne; |
|
| 2294 | + } |
|
| 2295 | + if (!isset($opt['field'])) { |
|
| 2296 | + $opt['field'] = []; |
|
| 2297 | + } |
|
| 2298 | + if (!isset($opt['key'])) { |
|
| 2299 | + $opt['key'] = []; |
|
| 2300 | + } |
|
| 2301 | + |
|
| 2302 | + // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2303 | + // on prendra directement le nom de la future table |
|
| 2304 | + $meme_table = ($table_origine == $table_destination); |
|
| 2305 | + |
|
| 2306 | + $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2307 | + if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2308 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2309 | + |
|
| 2310 | + return false; |
|
| 2311 | + } |
|
| 2312 | + |
|
| 2313 | + |
|
| 2314 | + $table_tmp = $table_origine . '_tmp'; |
|
| 2315 | + |
|
| 2316 | + // 1) creer une table temporaire avec les modifications |
|
| 2317 | + // - DROP : suppression de la colonne |
|
| 2318 | + // - CHANGE : modification de la colonne |
|
| 2319 | + // (foreach pour conserver l'ordre des champs) |
|
| 2320 | + |
|
| 2321 | + // field |
|
| 2322 | + $fields = []; |
|
| 2323 | + // pour le INSERT INTO plus loin |
|
| 2324 | + // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2325 | + $fields_correspondances = []; |
|
| 2326 | + foreach ($def_origine['field'] as $c => $d) { |
|
| 2327 | + if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2328 | + // si pas DROP |
|
| 2329 | + if ($colonne_destination) { |
|
| 2330 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2331 | + $fields_correspondances[$colonne_destination] = $c; |
|
| 2332 | + } |
|
| 2333 | + } else { |
|
| 2334 | + $fields[$c] = $d; |
|
| 2335 | + $fields_correspondances[$c] = $c; |
|
| 2336 | + } |
|
| 2337 | + } |
|
| 2338 | + // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2339 | + if (!$colonne_origine && $colonne_destination) { |
|
| 2340 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2341 | + } |
|
| 2342 | + |
|
| 2343 | + // key... |
|
| 2344 | + $keys = []; |
|
| 2345 | + foreach ($def_origine['key'] as $c => $d) { |
|
| 2346 | + $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2347 | + $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2348 | + // seulement si on ne supprime pas la colonne ! |
|
| 2349 | + if ($d) { |
|
| 2350 | + $keys[$c] = $d; |
|
| 2351 | + } |
|
| 2352 | + } |
|
| 2353 | + |
|
| 2354 | + // autres keys, on merge |
|
| 2355 | + $keys = array_merge($keys, $opt['key']); |
|
| 2356 | + $queries = []; |
|
| 2357 | + |
|
| 2358 | + // copier dans destination (si differente de origine), sinon tmp |
|
| 2359 | + $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2360 | + $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2361 | + and $keys['PRIMARY KEY'] |
|
| 2362 | + and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2363 | + and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2364 | + |
|
| 2365 | + if ( |
|
| 2366 | + $q = _sqlite_requete_create( |
|
| 2367 | + $table_copie, |
|
| 2368 | + $fields, |
|
| 2369 | + $keys, |
|
| 2370 | + $autoinc, |
|
| 2371 | + $temporary = false, |
|
| 2372 | + $ifnotexists = true, |
|
| 2373 | + $serveur |
|
| 2374 | + ) |
|
| 2375 | + ) { |
|
| 2376 | + $queries[] = $q; |
|
| 2377 | + } |
|
| 2378 | + |
|
| 2379 | + |
|
| 2380 | + // 2) y copier les champs qui vont bien |
|
| 2381 | + $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2382 | + $champs_ori = join(', ', $fields_correspondances); |
|
| 2383 | + $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2384 | + |
|
| 2385 | + // 3) supprimer la table d'origine |
|
| 2386 | + $queries[] = "DROP TABLE $table_origine"; |
|
| 2387 | + |
|
| 2388 | + // 4) renommer la table temporaire |
|
| 2389 | + // avec le nom de la table destination |
|
| 2390 | + // si necessaire |
|
| 2391 | + if ($meme_table) { |
|
| 2392 | + $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2393 | + } |
|
| 2394 | + |
|
| 2395 | + // 5) remettre les index ! |
|
| 2396 | + foreach ($keys as $k => $v) { |
|
| 2397 | + if ($k == 'PRIMARY KEY') { |
|
| 2398 | + } else { |
|
| 2399 | + // enlever KEY |
|
| 2400 | + $k = substr($k, 4); |
|
| 2401 | + $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2402 | + } |
|
| 2403 | + } |
|
| 2404 | + |
|
| 2405 | + |
|
| 2406 | + if (count($queries)) { |
|
| 2407 | + spip_sqlite::demarrer_transaction($serveur); |
|
| 2408 | + // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2409 | + foreach ($queries as $q) { |
|
| 2410 | + if (!spip_sqlite::executer_requete($q, $serveur)) { |
|
| 2411 | + spip_log('SQLite : ALTER TABLE table :' |
|
| 2412 | + . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2413 | + spip_sqlite::annuler_transaction($serveur); |
|
| 2414 | + |
|
| 2415 | + return false; |
|
| 2416 | + } |
|
| 2417 | + } |
|
| 2418 | + spip_sqlite::finir_transaction($serveur); |
|
| 2419 | + } |
|
| 2420 | + |
|
| 2421 | + return true; |
|
| 2422 | 2422 | } |
| 2423 | 2423 | |
| 2424 | 2424 | |
@@ -2428,61 +2428,61 @@ discard block |
||
| 2428 | 2428 | * @return array |
| 2429 | 2429 | */ |
| 2430 | 2430 | function _sqlite_ref_fonctions() { |
| 2431 | - $fonctions = [ |
|
| 2432 | - 'alter' => 'spip_sqlite_alter', |
|
| 2433 | - 'count' => 'spip_sqlite_count', |
|
| 2434 | - 'countsel' => 'spip_sqlite_countsel', |
|
| 2435 | - 'create' => 'spip_sqlite_create', |
|
| 2436 | - 'create_base' => 'spip_sqlite_create_base', |
|
| 2437 | - 'create_view' => 'spip_sqlite_create_view', |
|
| 2438 | - 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2439 | - 'delete' => 'spip_sqlite_delete', |
|
| 2440 | - 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2441 | - 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2442 | - 'errno' => 'spip_sqlite_errno', |
|
| 2443 | - 'error' => 'spip_sqlite_error', |
|
| 2444 | - 'explain' => 'spip_sqlite_explain', |
|
| 2445 | - 'fetch' => 'spip_sqlite_fetch', |
|
| 2446 | - 'seek' => 'spip_sqlite_seek', |
|
| 2447 | - 'free' => 'spip_sqlite_free', |
|
| 2448 | - 'hex' => 'spip_sqlite_hex', |
|
| 2449 | - 'in' => 'spip_sqlite_in', |
|
| 2450 | - 'insert' => 'spip_sqlite_insert', |
|
| 2451 | - 'insertq' => 'spip_sqlite_insertq', |
|
| 2452 | - 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2453 | - 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2454 | - 'multi' => 'spip_sqlite_multi', |
|
| 2455 | - 'optimize' => 'spip_sqlite_optimize', |
|
| 2456 | - 'query' => 'spip_sqlite_query', |
|
| 2457 | - 'quote' => 'spip_sqlite_quote', |
|
| 2458 | - 'repair' => 'spip_sqlite_repair', |
|
| 2459 | - 'replace' => 'spip_sqlite_replace', |
|
| 2460 | - 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2461 | - 'select' => 'spip_sqlite_select', |
|
| 2462 | - 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2463 | - 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2464 | - 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2465 | - 'showbase' => 'spip_sqlite_showbase', |
|
| 2466 | - 'showtable' => 'spip_sqlite_showtable', |
|
| 2467 | - 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2468 | - 'update' => 'spip_sqlite_update', |
|
| 2469 | - 'updateq' => 'spip_sqlite_updateq', |
|
| 2470 | - 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2471 | - 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2472 | - 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2473 | - ]; |
|
| 2474 | - |
|
| 2475 | - // association de chaque nom http d'un charset aux couples sqlite |
|
| 2476 | - // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2477 | - $charsets = [ |
|
| 2478 | - 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2479 | - //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2480 | - //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2481 | - ]; |
|
| 2482 | - |
|
| 2483 | - $fonctions['charsets'] = $charsets; |
|
| 2484 | - |
|
| 2485 | - return $fonctions; |
|
| 2431 | + $fonctions = [ |
|
| 2432 | + 'alter' => 'spip_sqlite_alter', |
|
| 2433 | + 'count' => 'spip_sqlite_count', |
|
| 2434 | + 'countsel' => 'spip_sqlite_countsel', |
|
| 2435 | + 'create' => 'spip_sqlite_create', |
|
| 2436 | + 'create_base' => 'spip_sqlite_create_base', |
|
| 2437 | + 'create_view' => 'spip_sqlite_create_view', |
|
| 2438 | + 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2439 | + 'delete' => 'spip_sqlite_delete', |
|
| 2440 | + 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2441 | + 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2442 | + 'errno' => 'spip_sqlite_errno', |
|
| 2443 | + 'error' => 'spip_sqlite_error', |
|
| 2444 | + 'explain' => 'spip_sqlite_explain', |
|
| 2445 | + 'fetch' => 'spip_sqlite_fetch', |
|
| 2446 | + 'seek' => 'spip_sqlite_seek', |
|
| 2447 | + 'free' => 'spip_sqlite_free', |
|
| 2448 | + 'hex' => 'spip_sqlite_hex', |
|
| 2449 | + 'in' => 'spip_sqlite_in', |
|
| 2450 | + 'insert' => 'spip_sqlite_insert', |
|
| 2451 | + 'insertq' => 'spip_sqlite_insertq', |
|
| 2452 | + 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2453 | + 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2454 | + 'multi' => 'spip_sqlite_multi', |
|
| 2455 | + 'optimize' => 'spip_sqlite_optimize', |
|
| 2456 | + 'query' => 'spip_sqlite_query', |
|
| 2457 | + 'quote' => 'spip_sqlite_quote', |
|
| 2458 | + 'repair' => 'spip_sqlite_repair', |
|
| 2459 | + 'replace' => 'spip_sqlite_replace', |
|
| 2460 | + 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2461 | + 'select' => 'spip_sqlite_select', |
|
| 2462 | + 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2463 | + 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2464 | + 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2465 | + 'showbase' => 'spip_sqlite_showbase', |
|
| 2466 | + 'showtable' => 'spip_sqlite_showtable', |
|
| 2467 | + 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2468 | + 'update' => 'spip_sqlite_update', |
|
| 2469 | + 'updateq' => 'spip_sqlite_updateq', |
|
| 2470 | + 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2471 | + 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2472 | + 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2473 | + ]; |
|
| 2474 | + |
|
| 2475 | + // association de chaque nom http d'un charset aux couples sqlite |
|
| 2476 | + // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2477 | + $charsets = [ |
|
| 2478 | + 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2479 | + //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2480 | + //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2481 | + ]; |
|
| 2482 | + |
|
| 2483 | + $fonctions['charsets'] = $charsets; |
|
| 2484 | + |
|
| 2485 | + return $fonctions; |
|
| 2486 | 2486 | } |
| 2487 | 2487 | |
| 2488 | 2488 | |
@@ -2494,56 +2494,56 @@ discard block |
||
| 2494 | 2494 | * @return mixed |
| 2495 | 2495 | */ |
| 2496 | 2496 | function _sqlite_remplacements_definitions_table($query, $autoinc = false) { |
| 2497 | - // quelques remplacements |
|
| 2498 | - $num = '(\s*\([0-9]*\))?'; |
|
| 2499 | - $enum = '(\s*\([^\)]*\))?'; |
|
| 2500 | - |
|
| 2501 | - $remplace = [ |
|
| 2502 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2503 | - '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2504 | - '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2505 | - '/auto_increment/is' => '', |
|
| 2506 | - '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2507 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2508 | - '/character set \w+/is' => '', |
|
| 2509 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2510 | - '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2511 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2512 | - '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2513 | - '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2514 | - ]; |
|
| 2515 | - |
|
| 2516 | - // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2517 | - $remplace_autocinc = [ |
|
| 2518 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2519 | - ]; |
|
| 2520 | - // pour les int non autoincrement, il faut un DEFAULT |
|
| 2521 | - $remplace_nonautocinc = [ |
|
| 2522 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2523 | - ]; |
|
| 2524 | - |
|
| 2525 | - if (is_string($query)) { |
|
| 2526 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2527 | - if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2528 | - $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2529 | - } else { |
|
| 2530 | - $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2531 | - $query = _sqlite_collate_ci($query); |
|
| 2532 | - } |
|
| 2533 | - } elseif (is_array($query)) { |
|
| 2534 | - foreach ($query as $k => $q) { |
|
| 2535 | - $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2536 | - $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2537 | - if ($ai) { |
|
| 2538 | - $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2539 | - } else { |
|
| 2540 | - $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2541 | - $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2542 | - } |
|
| 2543 | - } |
|
| 2544 | - } |
|
| 2545 | - |
|
| 2546 | - return $query; |
|
| 2497 | + // quelques remplacements |
|
| 2498 | + $num = '(\s*\([0-9]*\))?'; |
|
| 2499 | + $enum = '(\s*\([^\)]*\))?'; |
|
| 2500 | + |
|
| 2501 | + $remplace = [ |
|
| 2502 | + '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2503 | + '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2504 | + '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2505 | + '/auto_increment/is' => '', |
|
| 2506 | + '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2507 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2508 | + '/character set \w+/is' => '', |
|
| 2509 | + '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2510 | + '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2511 | + '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2512 | + '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2513 | + '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2514 | + ]; |
|
| 2515 | + |
|
| 2516 | + // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2517 | + $remplace_autocinc = [ |
|
| 2518 | + '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2519 | + ]; |
|
| 2520 | + // pour les int non autoincrement, il faut un DEFAULT |
|
| 2521 | + $remplace_nonautocinc = [ |
|
| 2522 | + '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2523 | + ]; |
|
| 2524 | + |
|
| 2525 | + if (is_string($query)) { |
|
| 2526 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2527 | + if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2528 | + $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2529 | + } else { |
|
| 2530 | + $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2531 | + $query = _sqlite_collate_ci($query); |
|
| 2532 | + } |
|
| 2533 | + } elseif (is_array($query)) { |
|
| 2534 | + foreach ($query as $k => $q) { |
|
| 2535 | + $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2536 | + $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2537 | + if ($ai) { |
|
| 2538 | + $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2539 | + } else { |
|
| 2540 | + $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2541 | + $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2542 | + } |
|
| 2543 | + } |
|
| 2544 | + } |
|
| 2545 | + |
|
| 2546 | + return $query; |
|
| 2547 | 2547 | } |
| 2548 | 2548 | |
| 2549 | 2549 | /** |
@@ -2554,17 +2554,17 @@ discard block |
||
| 2554 | 2554 | * @return string |
| 2555 | 2555 | */ |
| 2556 | 2556 | function _sqlite_collate_ci($champ) { |
| 2557 | - if (stripos($champ, 'COLLATE') !== false) { |
|
| 2558 | - return $champ; |
|
| 2559 | - } |
|
| 2560 | - if (stripos($champ, 'BINARY') !== false) { |
|
| 2561 | - return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2562 | - } |
|
| 2563 | - if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2564 | - return $champ . ' COLLATE NOCASE'; |
|
| 2565 | - } |
|
| 2557 | + if (stripos($champ, 'COLLATE') !== false) { |
|
| 2558 | + return $champ; |
|
| 2559 | + } |
|
| 2560 | + if (stripos($champ, 'BINARY') !== false) { |
|
| 2561 | + return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2562 | + } |
|
| 2563 | + if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2564 | + return $champ . ' COLLATE NOCASE'; |
|
| 2565 | + } |
|
| 2566 | 2566 | |
| 2567 | - return $champ; |
|
| 2567 | + return $champ; |
|
| 2568 | 2568 | } |
| 2569 | 2569 | |
| 2570 | 2570 | |
@@ -2583,84 +2583,84 @@ discard block |
||
| 2583 | 2583 | * @return bool|string |
| 2584 | 2584 | */ |
| 2585 | 2585 | function _sqlite_requete_create( |
| 2586 | - $nom, |
|
| 2587 | - $champs, |
|
| 2588 | - $cles, |
|
| 2589 | - $autoinc = false, |
|
| 2590 | - $temporary = false, |
|
| 2591 | - $_ifnotexists = true, |
|
| 2592 | - $serveur = '', |
|
| 2593 | - $requeter = true |
|
| 2586 | + $nom, |
|
| 2587 | + $champs, |
|
| 2588 | + $cles, |
|
| 2589 | + $autoinc = false, |
|
| 2590 | + $temporary = false, |
|
| 2591 | + $_ifnotexists = true, |
|
| 2592 | + $serveur = '', |
|
| 2593 | + $requeter = true |
|
| 2594 | 2594 | ) { |
| 2595 | - $query = $keys = $s = $p = ''; |
|
| 2596 | - |
|
| 2597 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2598 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2599 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 2600 | - return; |
|
| 2601 | - } |
|
| 2602 | - |
|
| 2603 | - // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2604 | - // il faut passer par des create index |
|
| 2605 | - // Il gere par contre primary key ! |
|
| 2606 | - // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2607 | - // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2608 | - $pk = 'PRIMARY KEY'; |
|
| 2609 | - // le champ de cle primaire |
|
| 2610 | - $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2611 | - |
|
| 2612 | - foreach ($champs as $k => $v) { |
|
| 2613 | - if (false !== stripos($v, $pk)) { |
|
| 2614 | - $champ_pk = $k; |
|
| 2615 | - // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2616 | - $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2617 | - break; |
|
| 2618 | - } |
|
| 2619 | - } |
|
| 2620 | - |
|
| 2621 | - if ($champ_pk) { |
|
| 2622 | - $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2623 | - } |
|
| 2624 | - // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2625 | - if ( |
|
| 2626 | - isset($champs[$champ_pk]) |
|
| 2627 | - and stripos($champs[$champ_pk], 'default 0') !== false |
|
| 2628 | - ) { |
|
| 2629 | - $champs[$champ_pk] = trim(str_ireplace('default 0', '', $champs[$champ_pk])); |
|
| 2630 | - } |
|
| 2631 | - |
|
| 2632 | - $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2633 | - foreach ($champs as $k => $v) { |
|
| 2634 | - $query .= "$s\n\t\t$k $v"; |
|
| 2635 | - $s = ','; |
|
| 2636 | - } |
|
| 2637 | - |
|
| 2638 | - $ifnotexists = ''; |
|
| 2639 | - if ($_ifnotexists) { |
|
| 2640 | - $version = spip_sqlite_fetch( |
|
| 2641 | - spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2642 | - '', |
|
| 2643 | - $serveur |
|
| 2644 | - ); |
|
| 2645 | - if (!function_exists('spip_version_compare')) { |
|
| 2646 | - include_spip('plugins/installer'); |
|
| 2647 | - } |
|
| 2648 | - |
|
| 2649 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2650 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 2651 | - } else { |
|
| 2652 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2653 | - $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2654 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2655 | - return true; |
|
| 2656 | - } |
|
| 2657 | - } |
|
| 2658 | - } |
|
| 2659 | - |
|
| 2660 | - $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2661 | - $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2662 | - |
|
| 2663 | - return $q; |
|
| 2595 | + $query = $keys = $s = $p = ''; |
|
| 2596 | + |
|
| 2597 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2598 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2599 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 2600 | + return; |
|
| 2601 | + } |
|
| 2602 | + |
|
| 2603 | + // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2604 | + // il faut passer par des create index |
|
| 2605 | + // Il gere par contre primary key ! |
|
| 2606 | + // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2607 | + // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2608 | + $pk = 'PRIMARY KEY'; |
|
| 2609 | + // le champ de cle primaire |
|
| 2610 | + $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2611 | + |
|
| 2612 | + foreach ($champs as $k => $v) { |
|
| 2613 | + if (false !== stripos($v, $pk)) { |
|
| 2614 | + $champ_pk = $k; |
|
| 2615 | + // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2616 | + $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2617 | + break; |
|
| 2618 | + } |
|
| 2619 | + } |
|
| 2620 | + |
|
| 2621 | + if ($champ_pk) { |
|
| 2622 | + $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2623 | + } |
|
| 2624 | + // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2625 | + if ( |
|
| 2626 | + isset($champs[$champ_pk]) |
|
| 2627 | + and stripos($champs[$champ_pk], 'default 0') !== false |
|
| 2628 | + ) { |
|
| 2629 | + $champs[$champ_pk] = trim(str_ireplace('default 0', '', $champs[$champ_pk])); |
|
| 2630 | + } |
|
| 2631 | + |
|
| 2632 | + $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2633 | + foreach ($champs as $k => $v) { |
|
| 2634 | + $query .= "$s\n\t\t$k $v"; |
|
| 2635 | + $s = ','; |
|
| 2636 | + } |
|
| 2637 | + |
|
| 2638 | + $ifnotexists = ''; |
|
| 2639 | + if ($_ifnotexists) { |
|
| 2640 | + $version = spip_sqlite_fetch( |
|
| 2641 | + spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2642 | + '', |
|
| 2643 | + $serveur |
|
| 2644 | + ); |
|
| 2645 | + if (!function_exists('spip_version_compare')) { |
|
| 2646 | + include_spip('plugins/installer'); |
|
| 2647 | + } |
|
| 2648 | + |
|
| 2649 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2650 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 2651 | + } else { |
|
| 2652 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2653 | + $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2654 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 2655 | + return true; |
|
| 2656 | + } |
|
| 2657 | + } |
|
| 2658 | + } |
|
| 2659 | + |
|
| 2660 | + $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2661 | + $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2662 | + |
|
| 2663 | + return $q; |
|
| 2664 | 2664 | } |
| 2665 | 2665 | |
| 2666 | 2666 | |
@@ -2679,41 +2679,41 @@ discard block |
||
| 2679 | 2679 | * @return |
| 2680 | 2680 | */ |
| 2681 | 2681 | function _sqlite_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 2682 | - static $tables = []; |
|
| 2683 | - |
|
| 2684 | - if (!isset($tables[$table])) { |
|
| 2685 | - if (!$desc) { |
|
| 2686 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2687 | - $desc = $trouver_table($table, $serveur); |
|
| 2688 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 2689 | - if (!$desc) { |
|
| 2690 | - return $couples; |
|
| 2691 | - } |
|
| 2692 | - } |
|
| 2693 | - |
|
| 2694 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2695 | - // cependant, il faudra peut etre etendre |
|
| 2696 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 2697 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 2698 | - $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2699 | - |
|
| 2700 | - $now = _sqlite_func_now(true); |
|
| 2701 | - foreach ($desc['field'] as $k => $v) { |
|
| 2702 | - if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2703 | - $tables[$table]['desc'][$k] = $v; |
|
| 2704 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2705 | - } |
|
| 2706 | - } |
|
| 2707 | - } |
|
| 2708 | - else { |
|
| 2709 | - $now = _sqlite_func_now(true); |
|
| 2710 | - foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2711 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2712 | - } |
|
| 2713 | - } |
|
| 2714 | - |
|
| 2715 | - // ajout des champs type 'timestamp' absents |
|
| 2716 | - return array_merge($tables[$table]['valeur'], $couples); |
|
| 2682 | + static $tables = []; |
|
| 2683 | + |
|
| 2684 | + if (!isset($tables[$table])) { |
|
| 2685 | + if (!$desc) { |
|
| 2686 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2687 | + $desc = $trouver_table($table, $serveur); |
|
| 2688 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 2689 | + if (!$desc) { |
|
| 2690 | + return $couples; |
|
| 2691 | + } |
|
| 2692 | + } |
|
| 2693 | + |
|
| 2694 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2695 | + // cependant, il faudra peut etre etendre |
|
| 2696 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 2697 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 2698 | + $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2699 | + |
|
| 2700 | + $now = _sqlite_func_now(true); |
|
| 2701 | + foreach ($desc['field'] as $k => $v) { |
|
| 2702 | + if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2703 | + $tables[$table]['desc'][$k] = $v; |
|
| 2704 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2705 | + } |
|
| 2706 | + } |
|
| 2707 | + } |
|
| 2708 | + else { |
|
| 2709 | + $now = _sqlite_func_now(true); |
|
| 2710 | + foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2711 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2712 | + } |
|
| 2713 | + } |
|
| 2714 | + |
|
| 2715 | + // ajout des champs type 'timestamp' absents |
|
| 2716 | + return array_merge($tables[$table]['valeur'], $couples); |
|
| 2717 | 2717 | } |
| 2718 | 2718 | |
| 2719 | 2719 | |
@@ -2724,7 +2724,7 @@ discard block |
||
| 2724 | 2724 | * @return array|bool |
| 2725 | 2725 | */ |
| 2726 | 2726 | function spip_versions_sqlite() { |
| 2727 | - return _sqlite_charger_version(); |
|
| 2727 | + return _sqlite_charger_version(); |
|
| 2728 | 2728 | } |
| 2729 | 2729 | |
| 2730 | 2730 | /** |
@@ -2732,114 +2732,114 @@ discard block |
||
| 2732 | 2732 | * encadrées de transactions. |
| 2733 | 2733 | **/ |
| 2734 | 2734 | class spip_sqlite { |
| 2735 | - /** @var sqlite_requeteur[] Liste des instances de requêteurs créés */ |
|
| 2736 | - public static $requeteurs = []; |
|
| 2737 | - /** @var bool[] Pour chaque connexion, flag pour savoir si une transaction est en cours */ |
|
| 2738 | - public static $transaction_en_cours = []; |
|
| 2739 | - |
|
| 2740 | - |
|
| 2741 | - /** |
|
| 2742 | - * Retourne une unique instance du requêteur |
|
| 2743 | - * |
|
| 2744 | - * Retourne une instance unique du requêteur pour une connexion SQLite |
|
| 2745 | - * donnée |
|
| 2746 | - * |
|
| 2747 | - * @param string $serveur |
|
| 2748 | - * Nom du connecteur |
|
| 2749 | - * @return sqlite_requeteur |
|
| 2750 | - * Instance unique du requêteur |
|
| 2751 | - **/ |
|
| 2752 | - public static function requeteur($serveur) { |
|
| 2753 | - if (!isset(spip_sqlite::$requeteurs[$serveur])) { |
|
| 2754 | - spip_sqlite::$requeteurs[$serveur] = new sqlite_requeteur($serveur); |
|
| 2755 | - } |
|
| 2756 | - |
|
| 2757 | - return spip_sqlite::$requeteurs[$serveur]; |
|
| 2758 | - } |
|
| 2759 | - |
|
| 2760 | - /** |
|
| 2761 | - * Prépare le texte d'une requête avant son exécution |
|
| 2762 | - * |
|
| 2763 | - * Adapte la requête au format plus ou moins MySQL par un format |
|
| 2764 | - * compris de SQLite. |
|
| 2765 | - * |
|
| 2766 | - * Change les préfixes de tables SPIP par ceux véritables |
|
| 2767 | - * |
|
| 2768 | - * @param string $query Requête à préparer |
|
| 2769 | - * @param string $serveur Nom de la connexion |
|
| 2770 | - * @return string Requête préparée |
|
| 2771 | - */ |
|
| 2772 | - public static function traduire_requete($query, $serveur) { |
|
| 2773 | - $requeteur = spip_sqlite::requeteur($serveur); |
|
| 2774 | - $traducteur = new sqlite_traducteur($query, $requeteur->prefixe, $requeteur->sqlite_version); |
|
| 2775 | - |
|
| 2776 | - return $traducteur->traduire_requete(); |
|
| 2777 | - } |
|
| 2778 | - |
|
| 2779 | - /** |
|
| 2780 | - * Démarre une transaction |
|
| 2781 | - * |
|
| 2782 | - * @param string $serveur Nom de la connexion |
|
| 2783 | - **/ |
|
| 2784 | - public static function demarrer_transaction($serveur) { |
|
| 2785 | - spip_sqlite::executer_requete('BEGIN TRANSACTION', $serveur); |
|
| 2786 | - spip_sqlite::$transaction_en_cours[$serveur] = true; |
|
| 2787 | - } |
|
| 2788 | - |
|
| 2789 | - /** |
|
| 2790 | - * Exécute la requête donnée |
|
| 2791 | - * |
|
| 2792 | - * @param string $query Requête |
|
| 2793 | - * @param string $serveur Nom de la connexion |
|
| 2794 | - * @param null|bool $tracer Demander des statistiques (temps) ? |
|
| 2795 | - **/ |
|
| 2796 | - public static function executer_requete($query, $serveur, $tracer = null) { |
|
| 2797 | - $requeteur = spip_sqlite::requeteur($serveur); |
|
| 2798 | - |
|
| 2799 | - return $requeteur->executer_requete($query, $tracer); |
|
| 2800 | - } |
|
| 2801 | - |
|
| 2802 | - /** |
|
| 2803 | - * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 2804 | - * |
|
| 2805 | - * @param string $serveur Nom de la connexion |
|
| 2806 | - * return int Identifiant |
|
| 2807 | - **/ |
|
| 2808 | - public static function last_insert_id($serveur) { |
|
| 2809 | - $requeteur = spip_sqlite::requeteur($serveur); |
|
| 2810 | - |
|
| 2811 | - return $requeteur->last_insert_id($serveur); |
|
| 2812 | - } |
|
| 2813 | - |
|
| 2814 | - /** |
|
| 2815 | - * Annule une transaction |
|
| 2816 | - * |
|
| 2817 | - * @param string $serveur Nom de la connexion |
|
| 2818 | - **/ |
|
| 2819 | - public static function annuler_transaction($serveur) { |
|
| 2820 | - spip_sqlite::executer_requete('ROLLBACK', $serveur); |
|
| 2821 | - spip_sqlite::$transaction_en_cours[$serveur] = false; |
|
| 2822 | - } |
|
| 2823 | - |
|
| 2824 | - /** |
|
| 2825 | - * Termine une transaction |
|
| 2826 | - * |
|
| 2827 | - * @param string $serveur Nom de la connexion |
|
| 2828 | - **/ |
|
| 2829 | - public static function finir_transaction($serveur) { |
|
| 2830 | - // si pas de transaction en cours, ne rien faire et le dire |
|
| 2831 | - if ( |
|
| 2832 | - !isset(spip_sqlite::$transaction_en_cours[$serveur]) |
|
| 2833 | - or spip_sqlite::$transaction_en_cours[$serveur] == false |
|
| 2834 | - ) { |
|
| 2835 | - return false; |
|
| 2836 | - } |
|
| 2837 | - // sinon fermer la transaction et retourner true |
|
| 2838 | - spip_sqlite::executer_requete('COMMIT', $serveur); |
|
| 2839 | - spip_sqlite::$transaction_en_cours[$serveur] = false; |
|
| 2840 | - |
|
| 2841 | - return true; |
|
| 2842 | - } |
|
| 2735 | + /** @var sqlite_requeteur[] Liste des instances de requêteurs créés */ |
|
| 2736 | + public static $requeteurs = []; |
|
| 2737 | + /** @var bool[] Pour chaque connexion, flag pour savoir si une transaction est en cours */ |
|
| 2738 | + public static $transaction_en_cours = []; |
|
| 2739 | + |
|
| 2740 | + |
|
| 2741 | + /** |
|
| 2742 | + * Retourne une unique instance du requêteur |
|
| 2743 | + * |
|
| 2744 | + * Retourne une instance unique du requêteur pour une connexion SQLite |
|
| 2745 | + * donnée |
|
| 2746 | + * |
|
| 2747 | + * @param string $serveur |
|
| 2748 | + * Nom du connecteur |
|
| 2749 | + * @return sqlite_requeteur |
|
| 2750 | + * Instance unique du requêteur |
|
| 2751 | + **/ |
|
| 2752 | + public static function requeteur($serveur) { |
|
| 2753 | + if (!isset(spip_sqlite::$requeteurs[$serveur])) { |
|
| 2754 | + spip_sqlite::$requeteurs[$serveur] = new sqlite_requeteur($serveur); |
|
| 2755 | + } |
|
| 2756 | + |
|
| 2757 | + return spip_sqlite::$requeteurs[$serveur]; |
|
| 2758 | + } |
|
| 2759 | + |
|
| 2760 | + /** |
|
| 2761 | + * Prépare le texte d'une requête avant son exécution |
|
| 2762 | + * |
|
| 2763 | + * Adapte la requête au format plus ou moins MySQL par un format |
|
| 2764 | + * compris de SQLite. |
|
| 2765 | + * |
|
| 2766 | + * Change les préfixes de tables SPIP par ceux véritables |
|
| 2767 | + * |
|
| 2768 | + * @param string $query Requête à préparer |
|
| 2769 | + * @param string $serveur Nom de la connexion |
|
| 2770 | + * @return string Requête préparée |
|
| 2771 | + */ |
|
| 2772 | + public static function traduire_requete($query, $serveur) { |
|
| 2773 | + $requeteur = spip_sqlite::requeteur($serveur); |
|
| 2774 | + $traducteur = new sqlite_traducteur($query, $requeteur->prefixe, $requeteur->sqlite_version); |
|
| 2775 | + |
|
| 2776 | + return $traducteur->traduire_requete(); |
|
| 2777 | + } |
|
| 2778 | + |
|
| 2779 | + /** |
|
| 2780 | + * Démarre une transaction |
|
| 2781 | + * |
|
| 2782 | + * @param string $serveur Nom de la connexion |
|
| 2783 | + **/ |
|
| 2784 | + public static function demarrer_transaction($serveur) { |
|
| 2785 | + spip_sqlite::executer_requete('BEGIN TRANSACTION', $serveur); |
|
| 2786 | + spip_sqlite::$transaction_en_cours[$serveur] = true; |
|
| 2787 | + } |
|
| 2788 | + |
|
| 2789 | + /** |
|
| 2790 | + * Exécute la requête donnée |
|
| 2791 | + * |
|
| 2792 | + * @param string $query Requête |
|
| 2793 | + * @param string $serveur Nom de la connexion |
|
| 2794 | + * @param null|bool $tracer Demander des statistiques (temps) ? |
|
| 2795 | + **/ |
|
| 2796 | + public static function executer_requete($query, $serveur, $tracer = null) { |
|
| 2797 | + $requeteur = spip_sqlite::requeteur($serveur); |
|
| 2798 | + |
|
| 2799 | + return $requeteur->executer_requete($query, $tracer); |
|
| 2800 | + } |
|
| 2801 | + |
|
| 2802 | + /** |
|
| 2803 | + * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 2804 | + * |
|
| 2805 | + * @param string $serveur Nom de la connexion |
|
| 2806 | + * return int Identifiant |
|
| 2807 | + **/ |
|
| 2808 | + public static function last_insert_id($serveur) { |
|
| 2809 | + $requeteur = spip_sqlite::requeteur($serveur); |
|
| 2810 | + |
|
| 2811 | + return $requeteur->last_insert_id($serveur); |
|
| 2812 | + } |
|
| 2813 | + |
|
| 2814 | + /** |
|
| 2815 | + * Annule une transaction |
|
| 2816 | + * |
|
| 2817 | + * @param string $serveur Nom de la connexion |
|
| 2818 | + **/ |
|
| 2819 | + public static function annuler_transaction($serveur) { |
|
| 2820 | + spip_sqlite::executer_requete('ROLLBACK', $serveur); |
|
| 2821 | + spip_sqlite::$transaction_en_cours[$serveur] = false; |
|
| 2822 | + } |
|
| 2823 | + |
|
| 2824 | + /** |
|
| 2825 | + * Termine une transaction |
|
| 2826 | + * |
|
| 2827 | + * @param string $serveur Nom de la connexion |
|
| 2828 | + **/ |
|
| 2829 | + public static function finir_transaction($serveur) { |
|
| 2830 | + // si pas de transaction en cours, ne rien faire et le dire |
|
| 2831 | + if ( |
|
| 2832 | + !isset(spip_sqlite::$transaction_en_cours[$serveur]) |
|
| 2833 | + or spip_sqlite::$transaction_en_cours[$serveur] == false |
|
| 2834 | + ) { |
|
| 2835 | + return false; |
|
| 2836 | + } |
|
| 2837 | + // sinon fermer la transaction et retourner true |
|
| 2838 | + spip_sqlite::executer_requete('COMMIT', $serveur); |
|
| 2839 | + spip_sqlite::$transaction_en_cours[$serveur] = false; |
|
| 2840 | + |
|
| 2841 | + return true; |
|
| 2842 | + } |
|
| 2843 | 2843 | } |
| 2844 | 2844 | |
| 2845 | 2845 | /* |
@@ -2852,127 +2852,127 @@ discard block |
||
| 2852 | 2852 | */ |
| 2853 | 2853 | |
| 2854 | 2854 | class sqlite_requeteur { |
| 2855 | - /** @var string Texte de la requête */ |
|
| 2856 | - public $query = ''; // la requete |
|
| 2857 | - /** @var string Nom de la connexion */ |
|
| 2858 | - public $serveur = ''; |
|
| 2859 | - /** @var PDO Identifiant de la connexion SQLite */ |
|
| 2860 | - public $link = ''; |
|
| 2861 | - /** @var string Prefixe des tables SPIP */ |
|
| 2862 | - public $prefixe = ''; |
|
| 2863 | - /** @var string Nom de la base de donnée */ |
|
| 2864 | - public $db = ''; |
|
| 2865 | - /** @var bool Doit-on tracer les requetes (var_profile) ? */ |
|
| 2866 | - public $tracer = false; // doit-on tracer les requetes (var_profile) |
|
| 2867 | - |
|
| 2868 | - /** @var string Version de SQLite (2 ou 3) */ |
|
| 2869 | - public $sqlite_version = ''; |
|
| 2870 | - |
|
| 2871 | - /** |
|
| 2872 | - * Constructeur |
|
| 2873 | - * |
|
| 2874 | - * @param string $serveur |
|
| 2875 | - * @return bool |
|
| 2876 | - */ |
|
| 2877 | - public function __construct($serveur = '') { |
|
| 2878 | - _sqlite_init(); |
|
| 2879 | - $this->serveur = strtolower($serveur); |
|
| 2880 | - |
|
| 2881 | - if (!($this->link = _sqlite_link($this->serveur)) && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL)) { |
|
| 2882 | - spip_log('Aucune connexion sqlite (link)', 'sqlite.' . _LOG_ERREUR); |
|
| 2883 | - |
|
| 2884 | - return false; |
|
| 2885 | - } |
|
| 2886 | - |
|
| 2887 | - $this->sqlite_version = _sqlite_is_version('', $this->link); |
|
| 2888 | - |
|
| 2889 | - $this->prefixe = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['prefixe']; |
|
| 2890 | - $this->db = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['db']; |
|
| 2891 | - |
|
| 2892 | - // tracage des requetes ? |
|
| 2893 | - $this->tracer = (isset($_GET['var_profile']) && $_GET['var_profile']); |
|
| 2894 | - } |
|
| 2895 | - |
|
| 2896 | - /** |
|
| 2897 | - * Lancer la requête transmise et faire le tracage si demandé |
|
| 2898 | - * |
|
| 2899 | - * @param string $query |
|
| 2900 | - * Requête à exécuter |
|
| 2901 | - * @param bool|null $tracer |
|
| 2902 | - * true pour tracer la requête |
|
| 2903 | - * @return bool|PDOStatement|array |
|
| 2904 | - */ |
|
| 2905 | - public function executer_requete($query, $tracer = null) { |
|
| 2906 | - if (is_null($tracer)) { |
|
| 2907 | - $tracer = $this->tracer; |
|
| 2908 | - } |
|
| 2909 | - $err = ''; |
|
| 2910 | - $t = 0; |
|
| 2911 | - if ($tracer or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 2912 | - include_spip('public/tracer'); |
|
| 2913 | - $t = trace_query_start(); |
|
| 2914 | - } |
|
| 2915 | - |
|
| 2916 | - # spip_log("requete: $this->serveur >> $query",'sqlite.'._LOG_DEBUG); // boum ? pourquoi ? |
|
| 2917 | - if ($this->link) { |
|
| 2918 | - // memoriser la derniere erreur PHP vue |
|
| 2919 | - $last_error = (function_exists('error_get_last') ? error_get_last() : ''); |
|
| 2920 | - $e = null; |
|
| 2921 | - // sauver la derniere requete |
|
| 2922 | - $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['last'] = $query; |
|
| 2923 | - $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['total_requetes']++; |
|
| 2924 | - |
|
| 2925 | - try { |
|
| 2926 | - $r = $this->link->query($query); |
|
| 2927 | - } catch (\PDOException $e) { |
|
| 2928 | - spip_log('PDOException: ' . $e->getMessage(), 'sqlite.' . _LOG_DEBUG); |
|
| 2929 | - $r = false; |
|
| 2930 | - } |
|
| 2931 | - // sauvegarde de la requete (elle y est deja dans $r->queryString) |
|
| 2932 | - # $r->spipQueryString = $query; |
|
| 2933 | - |
|
| 2934 | - // comptage : oblige de compter le nombre d'entrees retournees |
|
| 2935 | - // par une requete SELECT |
|
| 2936 | - // aucune autre solution ne donne le nombre attendu :( ! |
|
| 2937 | - // particulierement s'il y a des LIMIT dans la requete. |
|
| 2938 | - if (strtoupper(substr(ltrim($query), 0, 6)) == 'SELECT') { |
|
| 2939 | - if ($r) { |
|
| 2940 | - // noter le link et la query pour faire le comptage *si* on en a besoin |
|
| 2941 | - $r->spipSqliteRowCount = [$this->link, $query]; |
|
| 2942 | - } elseif ($r instanceof PDOStatement) { |
|
| 2943 | - $r->spipSqliteRowCount = 0; |
|
| 2944 | - } |
|
| 2945 | - } |
|
| 2946 | - |
|
| 2947 | - // loger les warnings/erreurs eventuels de sqlite remontant dans PHP |
|
| 2948 | - if ($e and $e instanceof \PDOException) { |
|
| 2949 | - $err = strip_tags($e->getMessage()) . ' in ' . $e->getFile() . ' line ' . $e->getLine(); |
|
| 2950 | - spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 2951 | - } elseif ($err = (function_exists('error_get_last') ? error_get_last() : '') and $err != $last_error) { |
|
| 2952 | - $err = strip_tags($err['message']) . ' in ' . $err['file'] . ' line ' . $err['line']; |
|
| 2953 | - spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 2954 | - } else { |
|
| 2955 | - $err = ''; |
|
| 2956 | - } |
|
| 2957 | - } else { |
|
| 2958 | - $r = false; |
|
| 2959 | - } |
|
| 2960 | - |
|
| 2961 | - if (spip_sqlite_errno($this->serveur)) { |
|
| 2962 | - $err .= spip_sqlite_error($query, $this->serveur); |
|
| 2963 | - } |
|
| 2964 | - |
|
| 2965 | - return $t ? trace_query_end($query, $t, $r, $err, $this->serveur) : $r; |
|
| 2966 | - } |
|
| 2967 | - |
|
| 2968 | - /** |
|
| 2969 | - * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 2970 | - * |
|
| 2971 | - * @return string|false |
|
| 2972 | - **/ |
|
| 2973 | - public function last_insert_id() { |
|
| 2974 | - return $this->link->lastInsertId(); |
|
| 2975 | - } |
|
| 2855 | + /** @var string Texte de la requête */ |
|
| 2856 | + public $query = ''; // la requete |
|
| 2857 | + /** @var string Nom de la connexion */ |
|
| 2858 | + public $serveur = ''; |
|
| 2859 | + /** @var PDO Identifiant de la connexion SQLite */ |
|
| 2860 | + public $link = ''; |
|
| 2861 | + /** @var string Prefixe des tables SPIP */ |
|
| 2862 | + public $prefixe = ''; |
|
| 2863 | + /** @var string Nom de la base de donnée */ |
|
| 2864 | + public $db = ''; |
|
| 2865 | + /** @var bool Doit-on tracer les requetes (var_profile) ? */ |
|
| 2866 | + public $tracer = false; // doit-on tracer les requetes (var_profile) |
|
| 2867 | + |
|
| 2868 | + /** @var string Version de SQLite (2 ou 3) */ |
|
| 2869 | + public $sqlite_version = ''; |
|
| 2870 | + |
|
| 2871 | + /** |
|
| 2872 | + * Constructeur |
|
| 2873 | + * |
|
| 2874 | + * @param string $serveur |
|
| 2875 | + * @return bool |
|
| 2876 | + */ |
|
| 2877 | + public function __construct($serveur = '') { |
|
| 2878 | + _sqlite_init(); |
|
| 2879 | + $this->serveur = strtolower($serveur); |
|
| 2880 | + |
|
| 2881 | + if (!($this->link = _sqlite_link($this->serveur)) && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL)) { |
|
| 2882 | + spip_log('Aucune connexion sqlite (link)', 'sqlite.' . _LOG_ERREUR); |
|
| 2883 | + |
|
| 2884 | + return false; |
|
| 2885 | + } |
|
| 2886 | + |
|
| 2887 | + $this->sqlite_version = _sqlite_is_version('', $this->link); |
|
| 2888 | + |
|
| 2889 | + $this->prefixe = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['prefixe']; |
|
| 2890 | + $this->db = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['db']; |
|
| 2891 | + |
|
| 2892 | + // tracage des requetes ? |
|
| 2893 | + $this->tracer = (isset($_GET['var_profile']) && $_GET['var_profile']); |
|
| 2894 | + } |
|
| 2895 | + |
|
| 2896 | + /** |
|
| 2897 | + * Lancer la requête transmise et faire le tracage si demandé |
|
| 2898 | + * |
|
| 2899 | + * @param string $query |
|
| 2900 | + * Requête à exécuter |
|
| 2901 | + * @param bool|null $tracer |
|
| 2902 | + * true pour tracer la requête |
|
| 2903 | + * @return bool|PDOStatement|array |
|
| 2904 | + */ |
|
| 2905 | + public function executer_requete($query, $tracer = null) { |
|
| 2906 | + if (is_null($tracer)) { |
|
| 2907 | + $tracer = $this->tracer; |
|
| 2908 | + } |
|
| 2909 | + $err = ''; |
|
| 2910 | + $t = 0; |
|
| 2911 | + if ($tracer or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 2912 | + include_spip('public/tracer'); |
|
| 2913 | + $t = trace_query_start(); |
|
| 2914 | + } |
|
| 2915 | + |
|
| 2916 | + # spip_log("requete: $this->serveur >> $query",'sqlite.'._LOG_DEBUG); // boum ? pourquoi ? |
|
| 2917 | + if ($this->link) { |
|
| 2918 | + // memoriser la derniere erreur PHP vue |
|
| 2919 | + $last_error = (function_exists('error_get_last') ? error_get_last() : ''); |
|
| 2920 | + $e = null; |
|
| 2921 | + // sauver la derniere requete |
|
| 2922 | + $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['last'] = $query; |
|
| 2923 | + $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['total_requetes']++; |
|
| 2924 | + |
|
| 2925 | + try { |
|
| 2926 | + $r = $this->link->query($query); |
|
| 2927 | + } catch (\PDOException $e) { |
|
| 2928 | + spip_log('PDOException: ' . $e->getMessage(), 'sqlite.' . _LOG_DEBUG); |
|
| 2929 | + $r = false; |
|
| 2930 | + } |
|
| 2931 | + // sauvegarde de la requete (elle y est deja dans $r->queryString) |
|
| 2932 | + # $r->spipQueryString = $query; |
|
| 2933 | + |
|
| 2934 | + // comptage : oblige de compter le nombre d'entrees retournees |
|
| 2935 | + // par une requete SELECT |
|
| 2936 | + // aucune autre solution ne donne le nombre attendu :( ! |
|
| 2937 | + // particulierement s'il y a des LIMIT dans la requete. |
|
| 2938 | + if (strtoupper(substr(ltrim($query), 0, 6)) == 'SELECT') { |
|
| 2939 | + if ($r) { |
|
| 2940 | + // noter le link et la query pour faire le comptage *si* on en a besoin |
|
| 2941 | + $r->spipSqliteRowCount = [$this->link, $query]; |
|
| 2942 | + } elseif ($r instanceof PDOStatement) { |
|
| 2943 | + $r->spipSqliteRowCount = 0; |
|
| 2944 | + } |
|
| 2945 | + } |
|
| 2946 | + |
|
| 2947 | + // loger les warnings/erreurs eventuels de sqlite remontant dans PHP |
|
| 2948 | + if ($e and $e instanceof \PDOException) { |
|
| 2949 | + $err = strip_tags($e->getMessage()) . ' in ' . $e->getFile() . ' line ' . $e->getLine(); |
|
| 2950 | + spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 2951 | + } elseif ($err = (function_exists('error_get_last') ? error_get_last() : '') and $err != $last_error) { |
|
| 2952 | + $err = strip_tags($err['message']) . ' in ' . $err['file'] . ' line ' . $err['line']; |
|
| 2953 | + spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 2954 | + } else { |
|
| 2955 | + $err = ''; |
|
| 2956 | + } |
|
| 2957 | + } else { |
|
| 2958 | + $r = false; |
|
| 2959 | + } |
|
| 2960 | + |
|
| 2961 | + if (spip_sqlite_errno($this->serveur)) { |
|
| 2962 | + $err .= spip_sqlite_error($query, $this->serveur); |
|
| 2963 | + } |
|
| 2964 | + |
|
| 2965 | + return $t ? trace_query_end($query, $t, $r, $err, $this->serveur) : $r; |
|
| 2966 | + } |
|
| 2967 | + |
|
| 2968 | + /** |
|
| 2969 | + * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 2970 | + * |
|
| 2971 | + * @return string|false |
|
| 2972 | + **/ |
|
| 2973 | + public function last_insert_id() { |
|
| 2974 | + return $this->link->lastInsertId(); |
|
| 2975 | + } |
|
| 2976 | 2976 | } |
| 2977 | 2977 | |
| 2978 | 2978 | |
@@ -2982,206 +2982,206 @@ discard block |
||
| 2982 | 2982 | * (fonction pour proteger les textes) |
| 2983 | 2983 | */ |
| 2984 | 2984 | class sqlite_traducteur { |
| 2985 | - /** @var string $query Texte de la requête */ |
|
| 2986 | - public $query = ''; |
|
| 2987 | - /** @var string $prefixe Préfixe des tables */ |
|
| 2988 | - public $prefixe = ''; |
|
| 2989 | - /** @var string $sqlite_version Version de sqlite (2 ou 3) */ |
|
| 2990 | - public $sqlite_version = ''; |
|
| 2991 | - |
|
| 2992 | - /** Pour les corrections à effectuer sur les requêtes : array(code=>'texte') trouvé |
|
| 2993 | - * |
|
| 2994 | - * @var array |
|
| 2995 | - */ |
|
| 2996 | - public $textes = []; |
|
| 2997 | - |
|
| 2998 | - /** |
|
| 2999 | - * Constructeur |
|
| 3000 | - * |
|
| 3001 | - * @param string $query Requête à préparer |
|
| 3002 | - * @param string $prefixe Prefixe des tables à utiliser |
|
| 3003 | - * @param string $sqlite_version Version SQLite (2 ou 3) |
|
| 3004 | - */ |
|
| 3005 | - public function __construct($query, $prefixe, $sqlite_version) { |
|
| 3006 | - $this->query = $query; |
|
| 3007 | - $this->prefixe = $prefixe; |
|
| 3008 | - $this->sqlite_version = $sqlite_version; |
|
| 3009 | - } |
|
| 3010 | - |
|
| 3011 | - /** |
|
| 3012 | - * Transformer la requete pour SQLite |
|
| 3013 | - * |
|
| 3014 | - * Enlève les textes, transforme la requête pour quelle soit |
|
| 3015 | - * bien interprétée par SQLite, puis remet les textes |
|
| 3016 | - * la fonction affecte `$this->query` |
|
| 3017 | - */ |
|
| 3018 | - public function traduire_requete() { |
|
| 3019 | - // |
|
| 3020 | - // 1) Protection des textes en les remplacant par des codes |
|
| 3021 | - // |
|
| 3022 | - // enlever les 'textes' et initialiser avec |
|
| 3023 | - list($this->query, $textes) = query_echappe_textes($this->query); |
|
| 3024 | - |
|
| 3025 | - // |
|
| 3026 | - // 2) Corrections de la requete |
|
| 3027 | - // |
|
| 3028 | - // Correction Create Database |
|
| 3029 | - // Create Database -> requete ignoree |
|
| 3030 | - if (strpos($this->query, 'CREATE DATABASE') === 0) { |
|
| 3031 | - spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.' . _LOG_AVERTISSEMENT); |
|
| 3032 | - $this->query = 'SELECT 1'; |
|
| 3033 | - } |
|
| 3034 | - |
|
| 3035 | - // Correction Insert Ignore |
|
| 3036 | - // INSERT IGNORE -> insert (tout court et pas 'insert or replace') |
|
| 3037 | - if (strpos($this->query, 'INSERT IGNORE') === 0) { |
|
| 3038 | - spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.' . _LOG_DEBUG); |
|
| 3039 | - $this->query = 'INSERT ' . substr($this->query, '13'); |
|
| 3040 | - } |
|
| 3041 | - |
|
| 3042 | - // Correction des dates avec INTERVAL |
|
| 3043 | - // utiliser sql_date_proche() de preference |
|
| 3044 | - if (strpos($this->query, 'INTERVAL') !== false) { |
|
| 3045 | - $this->query = preg_replace_callback( |
|
| 3046 | - '/DATE_(ADD|SUB)(.*)INTERVAL\s+(\d+)\s+([a-zA-Z]+)\)/U', |
|
| 3047 | - [&$this, '_remplacerDateParTime'], |
|
| 3048 | - $this->query |
|
| 3049 | - ); |
|
| 3050 | - } |
|
| 3051 | - |
|
| 3052 | - if (strpos($this->query, 'LEFT(') !== false) { |
|
| 3053 | - $this->query = str_replace('LEFT(', '_LEFT(', $this->query); |
|
| 3054 | - } |
|
| 3055 | - |
|
| 3056 | - if (strpos($this->query, 'TIMESTAMPDIFF(') !== false) { |
|
| 3057 | - $this->query = preg_replace('/TIMESTAMPDIFF\(\s*([^,]*)\s*,/Uims', "TIMESTAMPDIFF('\\1',", $this->query); |
|
| 3058 | - } |
|
| 3059 | - |
|
| 3060 | - |
|
| 3061 | - // Correction Using |
|
| 3062 | - // USING (non reconnu en sqlite2) |
|
| 3063 | - // problematique car la jointure ne se fait pas du coup. |
|
| 3064 | - if (($this->sqlite_version == 2) && (strpos($this->query, 'USING') !== false)) { |
|
| 3065 | - spip_log( |
|
| 3066 | - "'USING (champ)' n'est pas reconnu en SQLite 2. Utilisez 'ON table1.champ = table2.champ'", |
|
| 3067 | - 'sqlite.' . _LOG_ERREUR |
|
| 3068 | - ); |
|
| 3069 | - $this->query = preg_replace('/USING\s*\([^\)]*\)/', '', $this->query); |
|
| 3070 | - } |
|
| 3071 | - |
|
| 3072 | - // Correction Field |
|
| 3073 | - // remplace FIELD(table,i,j,k...) par CASE WHEN table=i THEN n ... ELSE 0 END |
|
| 3074 | - if (strpos($this->query, 'FIELD') !== false) { |
|
| 3075 | - $this->query = preg_replace_callback( |
|
| 3076 | - '/FIELD\s*\(([^\)]*)\)/', |
|
| 3077 | - [&$this, '_remplacerFieldParCase'], |
|
| 3078 | - $this->query |
|
| 3079 | - ); |
|
| 3080 | - } |
|
| 3081 | - |
|
| 3082 | - // Correction des noms de tables FROM |
|
| 3083 | - // mettre les bons noms de table dans from, update, insert, replace... |
|
| 3084 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/iS', $this->query, $regs)) { |
|
| 3085 | - $suite = strstr($this->query, $regs[0]); |
|
| 3086 | - $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 3087 | - } else { |
|
| 3088 | - $suite = ''; |
|
| 3089 | - } |
|
| 3090 | - $pref = ($this->prefixe) ? $this->prefixe . '_' : ''; |
|
| 3091 | - $this->query = preg_replace('/([,\s])spip_/S', '\1' . $pref, $this->query) . $suite; |
|
| 3092 | - |
|
| 3093 | - // Correction zero AS x |
|
| 3094 | - // pg n'aime pas 0+x AS alias, sqlite, dans le meme style, |
|
| 3095 | - // n'apprecie pas du tout SELECT 0 as x ... ORDER BY x |
|
| 3096 | - // il dit que x ne doit pas être un integer dans le order by ! |
|
| 3097 | - // on remplace du coup x par vide() dans ce cas uniquement |
|
| 3098 | - // |
|
| 3099 | - // apparait dans public/vertebrer.php et dans le plugin menu aussi qui genere aussi ce genre de requete via un {par num #GET{tri_num}} |
|
| 3100 | - // mais est-ce encore un soucis pour sqlite en 2021 ? (ie commenter le preg_replace marche très bien en sqlite 3.28) |
|
| 3101 | - if ((strpos($this->query, '0 AS') !== false)) { |
|
| 3102 | - // on ne remplace que dans ORDER BY ou GROUP BY |
|
| 3103 | - if (preg_match('/\s(ORDER|GROUP) BY\s/i', $this->query, $regs)) { |
|
| 3104 | - $suite = strstr($this->query, $regs[0]); |
|
| 3105 | - $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 3106 | - |
|
| 3107 | - // on cherche les noms des x dans 0 AS x |
|
| 3108 | - // on remplace dans $suite le nom par vide() |
|
| 3109 | - preg_match_all('/\b0 AS\s*([^\s,]+)/', $this->query, $matches, PREG_PATTERN_ORDER); |
|
| 3110 | - foreach ($matches[1] as $m) { |
|
| 3111 | - if (strpos($suite, $m) !== false) { |
|
| 3112 | - $suite = preg_replace(",\b$m\b,", 'VIDE()', $suite); |
|
| 3113 | - } |
|
| 3114 | - } |
|
| 3115 | - $this->query .= $suite; |
|
| 3116 | - } |
|
| 3117 | - } |
|
| 3118 | - |
|
| 3119 | - // Correction possible des divisions entieres |
|
| 3120 | - // Le standard SQL (lequel? ou?) semble indiquer que |
|
| 3121 | - // a/b=c doit donner c entier si a et b sont entiers 4/3=1. |
|
| 3122 | - // C'est ce que retournent effectivement SQL Server et SQLite |
|
| 3123 | - // Ce n'est pas ce qu'applique MySQL qui retourne un reel : 4/3=1.333... |
|
| 3124 | - // |
|
| 3125 | - // On peut forcer la conversion en multipliant par 1.0 avant la division |
|
| 3126 | - // /!\ SQLite 3.5.9 Debian/Ubuntu est victime d'un bug en plus ! |
|
| 3127 | - // cf. https://bugs.launchpad.net/ubuntu/+source/sqlite3/+bug/254228 |
|
| 3128 | - // http://www.sqlite.org/cvstrac/tktview?tn=3202 |
|
| 3129 | - // (4*1.0/3) n'est pas rendu dans ce cas ! |
|
| 3130 | - # $this->query = str_replace('/','* 1.00 / ',$this->query); |
|
| 3131 | - |
|
| 3132 | - |
|
| 3133 | - // Correction critere REGEXP, non reconnu en sqlite2 |
|
| 3134 | - if (($this->sqlite_version == 2) && (strpos($this->query, 'REGEXP') !== false)) { |
|
| 3135 | - $this->query = preg_replace('/([^\s\(]*)(\s*)REGEXP(\s*)([^\s\)]*)/', 'REGEXP($4, $1)', $this->query); |
|
| 3136 | - } |
|
| 3137 | - |
|
| 3138 | - // |
|
| 3139 | - // 3) Remise en place des textes d'origine |
|
| 3140 | - // |
|
| 3141 | - // Correction Antiquotes et echappements |
|
| 3142 | - // ` => rien |
|
| 3143 | - if (strpos($this->query, '`') !== false) { |
|
| 3144 | - $this->query = str_replace('`', '', $this->query); |
|
| 3145 | - } |
|
| 3146 | - |
|
| 3147 | - $this->query = query_reinjecte_textes($this->query, $textes); |
|
| 3148 | - |
|
| 3149 | - return $this->query; |
|
| 3150 | - } |
|
| 3151 | - |
|
| 3152 | - |
|
| 3153 | - /** |
|
| 3154 | - * Callback pour remplacer `DATE_` / `INTERVAL` |
|
| 3155 | - * par `DATE ... strtotime` |
|
| 3156 | - * |
|
| 3157 | - * @param array $matches Captures |
|
| 3158 | - * @return string Texte de date compris par SQLite |
|
| 3159 | - */ |
|
| 3160 | - public function _remplacerDateParTime($matches) { |
|
| 3161 | - $op = strtoupper($matches[1] == 'ADD') ? '+' : '-'; |
|
| 3162 | - |
|
| 3163 | - return "datetime$matches[2] '$op$matches[3] $matches[4]')"; |
|
| 3164 | - } |
|
| 3165 | - |
|
| 3166 | - /** |
|
| 3167 | - * Callback pour remplacer `FIELD(table,i,j,k...)` |
|
| 3168 | - * par `CASE WHEN table=i THEN n ... ELSE 0 END` |
|
| 3169 | - * |
|
| 3170 | - * @param array $matches Captures |
|
| 3171 | - * @return string Texte de liste ordonnée compris par SQLite |
|
| 3172 | - */ |
|
| 3173 | - public function _remplacerFieldParCase($matches) { |
|
| 3174 | - $fields = substr($matches[0], 6, -1); // ne recuperer que l'interieur X de field(X) |
|
| 3175 | - $t = explode(',', $fields); |
|
| 3176 | - $index = array_shift($t); |
|
| 3177 | - |
|
| 3178 | - $res = ''; |
|
| 3179 | - $n = 0; |
|
| 3180 | - foreach ($t as $v) { |
|
| 3181 | - $n++; |
|
| 3182 | - $res .= "\nWHEN $index=$v THEN $n"; |
|
| 3183 | - } |
|
| 3184 | - |
|
| 3185 | - return "CASE $res ELSE 0 END "; |
|
| 3186 | - } |
|
| 2985 | + /** @var string $query Texte de la requête */ |
|
| 2986 | + public $query = ''; |
|
| 2987 | + /** @var string $prefixe Préfixe des tables */ |
|
| 2988 | + public $prefixe = ''; |
|
| 2989 | + /** @var string $sqlite_version Version de sqlite (2 ou 3) */ |
|
| 2990 | + public $sqlite_version = ''; |
|
| 2991 | + |
|
| 2992 | + /** Pour les corrections à effectuer sur les requêtes : array(code=>'texte') trouvé |
|
| 2993 | + * |
|
| 2994 | + * @var array |
|
| 2995 | + */ |
|
| 2996 | + public $textes = []; |
|
| 2997 | + |
|
| 2998 | + /** |
|
| 2999 | + * Constructeur |
|
| 3000 | + * |
|
| 3001 | + * @param string $query Requête à préparer |
|
| 3002 | + * @param string $prefixe Prefixe des tables à utiliser |
|
| 3003 | + * @param string $sqlite_version Version SQLite (2 ou 3) |
|
| 3004 | + */ |
|
| 3005 | + public function __construct($query, $prefixe, $sqlite_version) { |
|
| 3006 | + $this->query = $query; |
|
| 3007 | + $this->prefixe = $prefixe; |
|
| 3008 | + $this->sqlite_version = $sqlite_version; |
|
| 3009 | + } |
|
| 3010 | + |
|
| 3011 | + /** |
|
| 3012 | + * Transformer la requete pour SQLite |
|
| 3013 | + * |
|
| 3014 | + * Enlève les textes, transforme la requête pour quelle soit |
|
| 3015 | + * bien interprétée par SQLite, puis remet les textes |
|
| 3016 | + * la fonction affecte `$this->query` |
|
| 3017 | + */ |
|
| 3018 | + public function traduire_requete() { |
|
| 3019 | + // |
|
| 3020 | + // 1) Protection des textes en les remplacant par des codes |
|
| 3021 | + // |
|
| 3022 | + // enlever les 'textes' et initialiser avec |
|
| 3023 | + list($this->query, $textes) = query_echappe_textes($this->query); |
|
| 3024 | + |
|
| 3025 | + // |
|
| 3026 | + // 2) Corrections de la requete |
|
| 3027 | + // |
|
| 3028 | + // Correction Create Database |
|
| 3029 | + // Create Database -> requete ignoree |
|
| 3030 | + if (strpos($this->query, 'CREATE DATABASE') === 0) { |
|
| 3031 | + spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.' . _LOG_AVERTISSEMENT); |
|
| 3032 | + $this->query = 'SELECT 1'; |
|
| 3033 | + } |
|
| 3034 | + |
|
| 3035 | + // Correction Insert Ignore |
|
| 3036 | + // INSERT IGNORE -> insert (tout court et pas 'insert or replace') |
|
| 3037 | + if (strpos($this->query, 'INSERT IGNORE') === 0) { |
|
| 3038 | + spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.' . _LOG_DEBUG); |
|
| 3039 | + $this->query = 'INSERT ' . substr($this->query, '13'); |
|
| 3040 | + } |
|
| 3041 | + |
|
| 3042 | + // Correction des dates avec INTERVAL |
|
| 3043 | + // utiliser sql_date_proche() de preference |
|
| 3044 | + if (strpos($this->query, 'INTERVAL') !== false) { |
|
| 3045 | + $this->query = preg_replace_callback( |
|
| 3046 | + '/DATE_(ADD|SUB)(.*)INTERVAL\s+(\d+)\s+([a-zA-Z]+)\)/U', |
|
| 3047 | + [&$this, '_remplacerDateParTime'], |
|
| 3048 | + $this->query |
|
| 3049 | + ); |
|
| 3050 | + } |
|
| 3051 | + |
|
| 3052 | + if (strpos($this->query, 'LEFT(') !== false) { |
|
| 3053 | + $this->query = str_replace('LEFT(', '_LEFT(', $this->query); |
|
| 3054 | + } |
|
| 3055 | + |
|
| 3056 | + if (strpos($this->query, 'TIMESTAMPDIFF(') !== false) { |
|
| 3057 | + $this->query = preg_replace('/TIMESTAMPDIFF\(\s*([^,]*)\s*,/Uims', "TIMESTAMPDIFF('\\1',", $this->query); |
|
| 3058 | + } |
|
| 3059 | + |
|
| 3060 | + |
|
| 3061 | + // Correction Using |
|
| 3062 | + // USING (non reconnu en sqlite2) |
|
| 3063 | + // problematique car la jointure ne se fait pas du coup. |
|
| 3064 | + if (($this->sqlite_version == 2) && (strpos($this->query, 'USING') !== false)) { |
|
| 3065 | + spip_log( |
|
| 3066 | + "'USING (champ)' n'est pas reconnu en SQLite 2. Utilisez 'ON table1.champ = table2.champ'", |
|
| 3067 | + 'sqlite.' . _LOG_ERREUR |
|
| 3068 | + ); |
|
| 3069 | + $this->query = preg_replace('/USING\s*\([^\)]*\)/', '', $this->query); |
|
| 3070 | + } |
|
| 3071 | + |
|
| 3072 | + // Correction Field |
|
| 3073 | + // remplace FIELD(table,i,j,k...) par CASE WHEN table=i THEN n ... ELSE 0 END |
|
| 3074 | + if (strpos($this->query, 'FIELD') !== false) { |
|
| 3075 | + $this->query = preg_replace_callback( |
|
| 3076 | + '/FIELD\s*\(([^\)]*)\)/', |
|
| 3077 | + [&$this, '_remplacerFieldParCase'], |
|
| 3078 | + $this->query |
|
| 3079 | + ); |
|
| 3080 | + } |
|
| 3081 | + |
|
| 3082 | + // Correction des noms de tables FROM |
|
| 3083 | + // mettre les bons noms de table dans from, update, insert, replace... |
|
| 3084 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/iS', $this->query, $regs)) { |
|
| 3085 | + $suite = strstr($this->query, $regs[0]); |
|
| 3086 | + $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 3087 | + } else { |
|
| 3088 | + $suite = ''; |
|
| 3089 | + } |
|
| 3090 | + $pref = ($this->prefixe) ? $this->prefixe . '_' : ''; |
|
| 3091 | + $this->query = preg_replace('/([,\s])spip_/S', '\1' . $pref, $this->query) . $suite; |
|
| 3092 | + |
|
| 3093 | + // Correction zero AS x |
|
| 3094 | + // pg n'aime pas 0+x AS alias, sqlite, dans le meme style, |
|
| 3095 | + // n'apprecie pas du tout SELECT 0 as x ... ORDER BY x |
|
| 3096 | + // il dit que x ne doit pas être un integer dans le order by ! |
|
| 3097 | + // on remplace du coup x par vide() dans ce cas uniquement |
|
| 3098 | + // |
|
| 3099 | + // apparait dans public/vertebrer.php et dans le plugin menu aussi qui genere aussi ce genre de requete via un {par num #GET{tri_num}} |
|
| 3100 | + // mais est-ce encore un soucis pour sqlite en 2021 ? (ie commenter le preg_replace marche très bien en sqlite 3.28) |
|
| 3101 | + if ((strpos($this->query, '0 AS') !== false)) { |
|
| 3102 | + // on ne remplace que dans ORDER BY ou GROUP BY |
|
| 3103 | + if (preg_match('/\s(ORDER|GROUP) BY\s/i', $this->query, $regs)) { |
|
| 3104 | + $suite = strstr($this->query, $regs[0]); |
|
| 3105 | + $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 3106 | + |
|
| 3107 | + // on cherche les noms des x dans 0 AS x |
|
| 3108 | + // on remplace dans $suite le nom par vide() |
|
| 3109 | + preg_match_all('/\b0 AS\s*([^\s,]+)/', $this->query, $matches, PREG_PATTERN_ORDER); |
|
| 3110 | + foreach ($matches[1] as $m) { |
|
| 3111 | + if (strpos($suite, $m) !== false) { |
|
| 3112 | + $suite = preg_replace(",\b$m\b,", 'VIDE()', $suite); |
|
| 3113 | + } |
|
| 3114 | + } |
|
| 3115 | + $this->query .= $suite; |
|
| 3116 | + } |
|
| 3117 | + } |
|
| 3118 | + |
|
| 3119 | + // Correction possible des divisions entieres |
|
| 3120 | + // Le standard SQL (lequel? ou?) semble indiquer que |
|
| 3121 | + // a/b=c doit donner c entier si a et b sont entiers 4/3=1. |
|
| 3122 | + // C'est ce que retournent effectivement SQL Server et SQLite |
|
| 3123 | + // Ce n'est pas ce qu'applique MySQL qui retourne un reel : 4/3=1.333... |
|
| 3124 | + // |
|
| 3125 | + // On peut forcer la conversion en multipliant par 1.0 avant la division |
|
| 3126 | + // /!\ SQLite 3.5.9 Debian/Ubuntu est victime d'un bug en plus ! |
|
| 3127 | + // cf. https://bugs.launchpad.net/ubuntu/+source/sqlite3/+bug/254228 |
|
| 3128 | + // http://www.sqlite.org/cvstrac/tktview?tn=3202 |
|
| 3129 | + // (4*1.0/3) n'est pas rendu dans ce cas ! |
|
| 3130 | + # $this->query = str_replace('/','* 1.00 / ',$this->query); |
|
| 3131 | + |
|
| 3132 | + |
|
| 3133 | + // Correction critere REGEXP, non reconnu en sqlite2 |
|
| 3134 | + if (($this->sqlite_version == 2) && (strpos($this->query, 'REGEXP') !== false)) { |
|
| 3135 | + $this->query = preg_replace('/([^\s\(]*)(\s*)REGEXP(\s*)([^\s\)]*)/', 'REGEXP($4, $1)', $this->query); |
|
| 3136 | + } |
|
| 3137 | + |
|
| 3138 | + // |
|
| 3139 | + // 3) Remise en place des textes d'origine |
|
| 3140 | + // |
|
| 3141 | + // Correction Antiquotes et echappements |
|
| 3142 | + // ` => rien |
|
| 3143 | + if (strpos($this->query, '`') !== false) { |
|
| 3144 | + $this->query = str_replace('`', '', $this->query); |
|
| 3145 | + } |
|
| 3146 | + |
|
| 3147 | + $this->query = query_reinjecte_textes($this->query, $textes); |
|
| 3148 | + |
|
| 3149 | + return $this->query; |
|
| 3150 | + } |
|
| 3151 | + |
|
| 3152 | + |
|
| 3153 | + /** |
|
| 3154 | + * Callback pour remplacer `DATE_` / `INTERVAL` |
|
| 3155 | + * par `DATE ... strtotime` |
|
| 3156 | + * |
|
| 3157 | + * @param array $matches Captures |
|
| 3158 | + * @return string Texte de date compris par SQLite |
|
| 3159 | + */ |
|
| 3160 | + public function _remplacerDateParTime($matches) { |
|
| 3161 | + $op = strtoupper($matches[1] == 'ADD') ? '+' : '-'; |
|
| 3162 | + |
|
| 3163 | + return "datetime$matches[2] '$op$matches[3] $matches[4]')"; |
|
| 3164 | + } |
|
| 3165 | + |
|
| 3166 | + /** |
|
| 3167 | + * Callback pour remplacer `FIELD(table,i,j,k...)` |
|
| 3168 | + * par `CASE WHEN table=i THEN n ... ELSE 0 END` |
|
| 3169 | + * |
|
| 3170 | + * @param array $matches Captures |
|
| 3171 | + * @return string Texte de liste ordonnée compris par SQLite |
|
| 3172 | + */ |
|
| 3173 | + public function _remplacerFieldParCase($matches) { |
|
| 3174 | + $fields = substr($matches[0], 6, -1); // ne recuperer que l'interieur X de field(X) |
|
| 3175 | + $t = explode(',', $fields); |
|
| 3176 | + $index = array_shift($t); |
|
| 3177 | + |
|
| 3178 | + $res = ''; |
|
| 3179 | + $n = 0; |
|
| 3180 | + foreach ($t as $v) { |
|
| 3181 | + $n++; |
|
| 3182 | + $res .= "\nWHEN $index=$v THEN $n"; |
|
| 3183 | + } |
|
| 3184 | + |
|
| 3185 | + return "CASE $res ELSE 0 END "; |
|
| 3186 | + } |
|
| 3187 | 3187 | } |