@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | if ($GLOBALS['spip_version_base'] != $GLOBALS['meta']['version_installee']) { |
| 56 | 56 | if (!is_numeric(_request('reinstall'))) { |
| 57 | 57 | include_spip('base/create'); |
| 58 | - spip_log('recree les tables eventuellement disparues', 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 58 | + spip_log('recree les tables eventuellement disparues', 'maj.'._LOG_INFO_IMPORTANTE); |
|
| 59 | 59 | creer_base(); |
| 60 | 60 | } |
| 61 | 61 | |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | exit; |
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | - spip_log('Fin de mise a jour SQL. Debut m-a-j acces et config', 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 71 | + spip_log('Fin de mise a jour SQL. Debut m-a-j acces et config', 'maj.'._LOG_INFO_IMPORTANTE); |
|
| 72 | 72 | |
| 73 | 73 | // supprimer quelques fichiers temporaires qui peuvent se retrouver invalides |
| 74 | 74 | @spip_unlink(_CACHE_RUBRIQUES); |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | @spip_unlink(_CACHE_PLUGINS_OPT); |
| 78 | 78 | @spip_unlink(_CACHE_PLUGINS_FCT); |
| 79 | 79 | @spip_unlink(_CACHE_CHEMIN); |
| 80 | - @spip_unlink(_DIR_TMP . 'plugin_xml_cache.gz'); |
|
| 80 | + @spip_unlink(_DIR_TMP.'plugin_xml_cache.gz'); |
|
| 81 | 81 | |
| 82 | 82 | include_spip('inc/auth'); |
| 83 | 83 | auth_synchroniser_distant(); |
@@ -116,8 +116,8 @@ discard block |
||
| 116 | 116 | $version_installee = $GLOBALS['meta']['version_installee'] ?? null; |
| 117 | 117 | |
| 118 | 118 | spip_log( |
| 119 | - "Version anterieure: $version_installee. Courante: " . $GLOBALS['spip_version_base'], |
|
| 120 | - 'maj.' . _LOG_INFO_IMPORTANTE |
|
| 119 | + "Version anterieure: $version_installee. Courante: ".$GLOBALS['spip_version_base'], |
|
| 120 | + 'maj.'._LOG_INFO_IMPORTANTE |
|
| 121 | 121 | ); |
| 122 | 122 | if (!$version_installee || $GLOBALS['spip_version_base'] < $version_installee) { |
| 123 | 123 | sql_replace( |
@@ -150,9 +150,9 @@ discard block |
||
| 150 | 150 | $res = maj_while($version_installee, $cible, $GLOBALS['maj'], 'version_installee', 'meta', $redirect, $debut_page); |
| 151 | 151 | if ($res) { |
| 152 | 152 | if (!is_array($res)) { |
| 153 | - spip_log("Pb d'acces SQL a la mise a jour", 'maj.' . _LOG_ERREUR); |
|
| 153 | + spip_log("Pb d'acces SQL a la mise a jour", 'maj.'._LOG_ERREUR); |
|
| 154 | 154 | } else { |
| 155 | - echo _T('avis_operation_echec') . ' ' . implode(' ', $res); |
|
| 155 | + echo _T('avis_operation_echec').' '.implode(' ', $res); |
|
| 156 | 156 | echo install_fin_html(); |
| 157 | 157 | } |
| 158 | 158 | } |
@@ -240,9 +240,9 @@ discard block |
||
| 240 | 240 | $res = maj_while($current_version, $version_cible, $maj, $nom_meta_base_version, $table_meta, $redirect); |
| 241 | 241 | if ($res) { |
| 242 | 242 | if (!is_array($res)) { |
| 243 | - spip_log("Pb d'acces SQL a la mise a jour", 'maj.' . _LOG_ERREUR); |
|
| 243 | + spip_log("Pb d'acces SQL a la mise a jour", 'maj.'._LOG_ERREUR); |
|
| 244 | 244 | } else { |
| 245 | - echo '<p>' . _T('avis_operation_echec') . ' ' . implode(' ', $res) . '</p>'; |
|
| 245 | + echo '<p>'._T('avis_operation_echec').' '.implode(' ', $res).'</p>'; |
|
| 246 | 246 | } |
| 247 | 247 | } |
| 248 | 248 | } |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | echo(install_debut_html($titre)); |
| 302 | 302 | // script de rechargement auto sur timeout |
| 303 | 303 | $redirect = generer_url_ecrire('upgrade', "reinstall=$installee&meta=$meta&table=$table", true); |
| 304 | - echo http_script("window.setTimeout('location.href=\"" . $redirect . "\";'," . ($timeout * 1000) . ')'); |
|
| 304 | + echo http_script("window.setTimeout('location.href=\"".$redirect."\";',".($timeout * 1000).')'); |
|
| 305 | 305 | echo "<div style='text-align: left'>\n"; |
| 306 | 306 | if (ob_get_level()) { |
| 307 | 307 | ob_flush(); |
@@ -389,7 +389,7 @@ discard block |
||
| 389 | 389 | return [$v, $etape]; |
| 390 | 390 | } |
| 391 | 391 | $n = time() - $time; |
| 392 | - spip_log("$table $meta: $v en $n secondes", 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 392 | + spip_log("$table $meta: $v en $n secondes", 'maj.'._LOG_INFO_IMPORTANTE); |
|
| 393 | 393 | if ($meta) { |
| 394 | 394 | ecrire_meta($meta, $installee = $v, 'oui', $table); |
| 395 | 395 | } |
@@ -405,7 +405,7 @@ discard block |
||
| 405 | 405 | if ($meta) { |
| 406 | 406 | ecrire_meta($meta, $cible, 'oui', $table); |
| 407 | 407 | } |
| 408 | - spip_log("MAJ terminee. $meta: $installee", 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 408 | + spip_log("MAJ terminee. $meta: $installee", 'maj.'._LOG_INFO_IMPORTANTE); |
|
| 409 | 409 | |
| 410 | 410 | return []; |
| 411 | 411 | } |
@@ -430,7 +430,7 @@ discard block |
||
| 430 | 430 | * @return int |
| 431 | 431 | */ |
| 432 | 432 | function serie_alter($serie, $q = [], $meta = '', $table = 'meta', $redirect = '') { |
| 433 | - $meta2 = $meta . '_maj_' . $serie; |
|
| 433 | + $meta2 = $meta.'_maj_'.$serie; |
|
| 434 | 434 | $etape = 0; |
| 435 | 435 | if (isset($GLOBALS[$table][$meta2])) { |
| 436 | 436 | $etape = (int) $GLOBALS[$table][$meta2]; |
@@ -443,7 +443,7 @@ discard block |
||
| 443 | 443 | && function_exists($f = array_shift($r)) |
| 444 | 444 | ) { |
| 445 | 445 | // note: $r (arguments de la fonction $f) peut avoir des données tabulaires |
| 446 | - spip_log("$msg: $f " . @implode(',', $r), 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 446 | + spip_log("$msg: $f ".@implode(',', $r), 'maj.'._LOG_INFO_IMPORTANTE); |
|
| 447 | 447 | // pour les fonctions atomiques sql_xx |
| 448 | 448 | // on enregistre le meta avant de lancer la fonction, |
| 449 | 449 | // de maniere a eviter de boucler sur timeout |
@@ -461,12 +461,12 @@ discard block |
||
| 461 | 461 | relance_maj($meta, $table, $redirect); |
| 462 | 462 | } |
| 463 | 463 | ecrire_meta($meta2, $i + 1, 'non', $table); |
| 464 | - spip_log("$meta2: ok", 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 464 | + spip_log("$meta2: ok", 'maj.'._LOG_INFO_IMPORTANTE); |
|
| 465 | 465 | } else { |
| 466 | 466 | if (!is_array($r)) { |
| 467 | - spip_log("maj $i format incorrect", 'maj.' . _LOG_ERREUR); |
|
| 467 | + spip_log("maj $i format incorrect", 'maj.'._LOG_ERREUR); |
|
| 468 | 468 | } else { |
| 469 | - spip_log("maj $i fonction $f non definie", 'maj.' . _LOG_ERREUR); |
|
| 469 | + spip_log("maj $i fonction $f non definie", 'maj.'._LOG_ERREUR); |
|
| 470 | 470 | } |
| 471 | 471 | // en cas d'erreur serieuse, on s'arrete |
| 472 | 472 | // mais on permet de passer par dessus en rechargeant la page. |
@@ -16,17 +16,17 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | if (!defined('_UPGRADE_TIME_OUT')) { |
| 23 | - /** |
|
| 24 | - * Durée en secondes pour relancer les scripts de mises à jour, x secondes |
|
| 25 | - * avant que la durée d'exécution du script provoque un timeout |
|
| 26 | - * |
|
| 27 | - * @var int |
|
| 28 | - **/ |
|
| 29 | - define('_UPGRADE_TIME_OUT', 20); |
|
| 23 | + /** |
|
| 24 | + * Durée en secondes pour relancer les scripts de mises à jour, x secondes |
|
| 25 | + * avant que la durée d'exécution du script provoque un timeout |
|
| 26 | + * |
|
| 27 | + * @var int |
|
| 28 | + **/ |
|
| 29 | + define('_UPGRADE_TIME_OUT', 20); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | /** |
@@ -49,40 +49,40 @@ discard block |
||
| 49 | 49 | * @return void |
| 50 | 50 | */ |
| 51 | 51 | function base_upgrade_dist($titre = '', $reprise = '') { |
| 52 | - if (!$titre) { |
|
| 53 | - return; |
|
| 54 | - } // anti-testeur automatique |
|
| 55 | - if ($GLOBALS['spip_version_base'] != $GLOBALS['meta']['version_installee']) { |
|
| 56 | - if (!is_numeric(_request('reinstall'))) { |
|
| 57 | - include_spip('base/create'); |
|
| 58 | - spip_log('recree les tables eventuellement disparues', 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 59 | - creer_base(); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - // quand on rentre par ici, c'est toujours une mise a jour de SPIP |
|
| 63 | - // lancement de l'upgrade SPIP |
|
| 64 | - $res = maj_base(); |
|
| 65 | - |
|
| 66 | - if ($res) { |
|
| 67 | - // on arrete tout ici ! |
|
| 68 | - exit; |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - spip_log('Fin de mise a jour SQL. Debut m-a-j acces et config', 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 72 | - |
|
| 73 | - // supprimer quelques fichiers temporaires qui peuvent se retrouver invalides |
|
| 74 | - @spip_unlink(_CACHE_RUBRIQUES); |
|
| 75 | - @spip_unlink(_CACHE_PIPELINES); |
|
| 76 | - @spip_unlink(_CACHE_PLUGINS_PATH); |
|
| 77 | - @spip_unlink(_CACHE_PLUGINS_OPT); |
|
| 78 | - @spip_unlink(_CACHE_PLUGINS_FCT); |
|
| 79 | - @spip_unlink(_CACHE_CHEMIN); |
|
| 80 | - @spip_unlink(_DIR_TMP . 'plugin_xml_cache.gz'); |
|
| 81 | - |
|
| 82 | - include_spip('inc/auth'); |
|
| 83 | - auth_synchroniser_distant(); |
|
| 84 | - $config = charger_fonction('config', 'inc'); |
|
| 85 | - $config(); |
|
| 52 | + if (!$titre) { |
|
| 53 | + return; |
|
| 54 | + } // anti-testeur automatique |
|
| 55 | + if ($GLOBALS['spip_version_base'] != $GLOBALS['meta']['version_installee']) { |
|
| 56 | + if (!is_numeric(_request('reinstall'))) { |
|
| 57 | + include_spip('base/create'); |
|
| 58 | + spip_log('recree les tables eventuellement disparues', 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 59 | + creer_base(); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + // quand on rentre par ici, c'est toujours une mise a jour de SPIP |
|
| 63 | + // lancement de l'upgrade SPIP |
|
| 64 | + $res = maj_base(); |
|
| 65 | + |
|
| 66 | + if ($res) { |
|
| 67 | + // on arrete tout ici ! |
|
| 68 | + exit; |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + spip_log('Fin de mise a jour SQL. Debut m-a-j acces et config', 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 72 | + |
|
| 73 | + // supprimer quelques fichiers temporaires qui peuvent se retrouver invalides |
|
| 74 | + @spip_unlink(_CACHE_RUBRIQUES); |
|
| 75 | + @spip_unlink(_CACHE_PIPELINES); |
|
| 76 | + @spip_unlink(_CACHE_PLUGINS_PATH); |
|
| 77 | + @spip_unlink(_CACHE_PLUGINS_OPT); |
|
| 78 | + @spip_unlink(_CACHE_PLUGINS_FCT); |
|
| 79 | + @spip_unlink(_CACHE_CHEMIN); |
|
| 80 | + @spip_unlink(_DIR_TMP . 'plugin_xml_cache.gz'); |
|
| 81 | + |
|
| 82 | + include_spip('inc/auth'); |
|
| 83 | + auth_synchroniser_distant(); |
|
| 84 | + $config = charger_fonction('config', 'inc'); |
|
| 85 | + $config(); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -113,47 +113,47 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | function maj_base($version_cible = 0, $redirect = '', $debut_page = true) { |
| 115 | 115 | |
| 116 | - $version_installee = $GLOBALS['meta']['version_installee'] ?? null; |
|
| 117 | - |
|
| 118 | - spip_log( |
|
| 119 | - "Version anterieure: $version_installee. Courante: " . $GLOBALS['spip_version_base'], |
|
| 120 | - 'maj.' . _LOG_INFO_IMPORTANTE |
|
| 121 | - ); |
|
| 122 | - if (!$version_installee || $GLOBALS['spip_version_base'] < $version_installee) { |
|
| 123 | - sql_replace( |
|
| 124 | - 'spip_meta', |
|
| 125 | - [ |
|
| 126 | - 'nom' => 'version_installee', |
|
| 127 | - 'valeur' => $GLOBALS['spip_version_base'], |
|
| 128 | - 'impt' => 'non' |
|
| 129 | - ] |
|
| 130 | - ); |
|
| 131 | - return false; |
|
| 132 | - } |
|
| 133 | - if (!upgrade_test()) { |
|
| 134 | - return true; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - $cible = ($version_cible ?: $GLOBALS['spip_version_base']); |
|
| 138 | - |
|
| 139 | - if ($version_installee < 2021_01_01_00) { |
|
| 140 | - include_spip('maj/legacy/v40'); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - include_spip('maj/2021'); |
|
| 144 | - |
|
| 145 | - ksort($GLOBALS['maj']); |
|
| 146 | - $res = maj_while($version_installee, $cible, $GLOBALS['maj'], 'version_installee', 'meta', $redirect, $debut_page); |
|
| 147 | - if ($res) { |
|
| 148 | - if (!is_array($res)) { |
|
| 149 | - spip_log("Pb d'acces SQL a la mise a jour", 'maj.' . _LOG_ERREUR); |
|
| 150 | - } else { |
|
| 151 | - echo _T('avis_operation_echec') . ' ' . implode(' ', $res); |
|
| 152 | - echo install_fin_html(); |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - return $res; |
|
| 116 | + $version_installee = $GLOBALS['meta']['version_installee'] ?? null; |
|
| 117 | + |
|
| 118 | + spip_log( |
|
| 119 | + "Version anterieure: $version_installee. Courante: " . $GLOBALS['spip_version_base'], |
|
| 120 | + 'maj.' . _LOG_INFO_IMPORTANTE |
|
| 121 | + ); |
|
| 122 | + if (!$version_installee || $GLOBALS['spip_version_base'] < $version_installee) { |
|
| 123 | + sql_replace( |
|
| 124 | + 'spip_meta', |
|
| 125 | + [ |
|
| 126 | + 'nom' => 'version_installee', |
|
| 127 | + 'valeur' => $GLOBALS['spip_version_base'], |
|
| 128 | + 'impt' => 'non' |
|
| 129 | + ] |
|
| 130 | + ); |
|
| 131 | + return false; |
|
| 132 | + } |
|
| 133 | + if (!upgrade_test()) { |
|
| 134 | + return true; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + $cible = ($version_cible ?: $GLOBALS['spip_version_base']); |
|
| 138 | + |
|
| 139 | + if ($version_installee < 2021_01_01_00) { |
|
| 140 | + include_spip('maj/legacy/v40'); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + include_spip('maj/2021'); |
|
| 144 | + |
|
| 145 | + ksort($GLOBALS['maj']); |
|
| 146 | + $res = maj_while($version_installee, $cible, $GLOBALS['maj'], 'version_installee', 'meta', $redirect, $debut_page); |
|
| 147 | + if ($res) { |
|
| 148 | + if (!is_array($res)) { |
|
| 149 | + spip_log("Pb d'acces SQL a la mise a jour", 'maj.' . _LOG_ERREUR); |
|
| 150 | + } else { |
|
| 151 | + echo _T('avis_operation_echec') . ' ' . implode(' ', $res); |
|
| 152 | + echo install_fin_html(); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + return $res; |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | /** |
@@ -195,53 +195,53 @@ discard block |
||
| 195 | 195 | */ |
| 196 | 196 | function maj_plugin($nom_meta_base_version, $version_cible, $maj, $table_meta = 'meta') { |
| 197 | 197 | |
| 198 | - if ($table_meta !== 'meta') { |
|
| 199 | - installer_table_meta($table_meta); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - $current_version = null; |
|
| 203 | - |
|
| 204 | - if ( |
|
| 205 | - (!isset($GLOBALS[$table_meta][$nom_meta_base_version])) |
|
| 206 | - || (!spip_version_compare($current_version = $GLOBALS[$table_meta][$nom_meta_base_version], $version_cible, '=')) |
|
| 207 | - ) { |
|
| 208 | - // $maj['create'] contient les directives propres a la premiere creation de base |
|
| 209 | - // c'est une operation derogatoire qui fait aboutir directement dans la version_cible |
|
| 210 | - if (isset($maj['create'])) { |
|
| 211 | - if (!isset($GLOBALS[$table_meta][$nom_meta_base_version])) { |
|
| 212 | - // installation : on ne fait que l'operation create |
|
| 213 | - $maj = ['init' => $maj['create']]; |
|
| 214 | - // et on lui ajoute un appel a inc/config |
|
| 215 | - // pour creer les metas par defaut |
|
| 216 | - $config = charger_fonction('config', 'inc'); |
|
| 217 | - $maj[$version_cible] = [[$config]]; |
|
| 218 | - } |
|
| 219 | - // dans tous les cas enlever cet index du tableau |
|
| 220 | - unset($maj['create']); |
|
| 221 | - } |
|
| 222 | - // si init, deja dans le bon ordre |
|
| 223 | - if (!isset($maj['init'])) { |
|
| 224 | - include_spip('inc/plugin'); // pour spip_version_compare |
|
| 225 | - uksort($maj, 'spip_version_compare'); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - // la redirection se fait par defaut sur la page d'administration des plugins |
|
| 229 | - // sauf lorsque nous sommes sur l'installation de SPIP |
|
| 230 | - // ou define _REDIRECT_MAJ_PLUGIN |
|
| 231 | - $redirect = (defined('_REDIRECT_MAJ_PLUGIN') ? _REDIRECT_MAJ_PLUGIN : generer_url_ecrire('admin_plugin')); |
|
| 232 | - if (defined('_ECRIRE_INSTALL')) { |
|
| 233 | - $redirect = parametre_url(generer_url_ecrire('install'), 'etape', _request('etape')); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - $res = maj_while($current_version, $version_cible, $maj, $nom_meta_base_version, $table_meta, $redirect); |
|
| 237 | - if ($res) { |
|
| 238 | - if (!is_array($res)) { |
|
| 239 | - spip_log("Pb d'acces SQL a la mise a jour", 'maj.' . _LOG_ERREUR); |
|
| 240 | - } else { |
|
| 241 | - echo '<p>' . _T('avis_operation_echec') . ' ' . implode(' ', $res) . '</p>'; |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - } |
|
| 198 | + if ($table_meta !== 'meta') { |
|
| 199 | + installer_table_meta($table_meta); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + $current_version = null; |
|
| 203 | + |
|
| 204 | + if ( |
|
| 205 | + (!isset($GLOBALS[$table_meta][$nom_meta_base_version])) |
|
| 206 | + || (!spip_version_compare($current_version = $GLOBALS[$table_meta][$nom_meta_base_version], $version_cible, '=')) |
|
| 207 | + ) { |
|
| 208 | + // $maj['create'] contient les directives propres a la premiere creation de base |
|
| 209 | + // c'est une operation derogatoire qui fait aboutir directement dans la version_cible |
|
| 210 | + if (isset($maj['create'])) { |
|
| 211 | + if (!isset($GLOBALS[$table_meta][$nom_meta_base_version])) { |
|
| 212 | + // installation : on ne fait que l'operation create |
|
| 213 | + $maj = ['init' => $maj['create']]; |
|
| 214 | + // et on lui ajoute un appel a inc/config |
|
| 215 | + // pour creer les metas par defaut |
|
| 216 | + $config = charger_fonction('config', 'inc'); |
|
| 217 | + $maj[$version_cible] = [[$config]]; |
|
| 218 | + } |
|
| 219 | + // dans tous les cas enlever cet index du tableau |
|
| 220 | + unset($maj['create']); |
|
| 221 | + } |
|
| 222 | + // si init, deja dans le bon ordre |
|
| 223 | + if (!isset($maj['init'])) { |
|
| 224 | + include_spip('inc/plugin'); // pour spip_version_compare |
|
| 225 | + uksort($maj, 'spip_version_compare'); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + // la redirection se fait par defaut sur la page d'administration des plugins |
|
| 229 | + // sauf lorsque nous sommes sur l'installation de SPIP |
|
| 230 | + // ou define _REDIRECT_MAJ_PLUGIN |
|
| 231 | + $redirect = (defined('_REDIRECT_MAJ_PLUGIN') ? _REDIRECT_MAJ_PLUGIN : generer_url_ecrire('admin_plugin')); |
|
| 232 | + if (defined('_ECRIRE_INSTALL')) { |
|
| 233 | + $redirect = parametre_url(generer_url_ecrire('install'), 'etape', _request('etape')); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + $res = maj_while($current_version, $version_cible, $maj, $nom_meta_base_version, $table_meta, $redirect); |
|
| 237 | + if ($res) { |
|
| 238 | + if (!is_array($res)) { |
|
| 239 | + spip_log("Pb d'acces SQL a la mise a jour", 'maj.' . _LOG_ERREUR); |
|
| 240 | + } else { |
|
| 241 | + echo '<p>' . _T('avis_operation_echec') . ' ' . implode(' ', $res) . '</p>'; |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | /** |
@@ -258,17 +258,17 @@ discard block |
||
| 258 | 258 | * @return void |
| 259 | 259 | */ |
| 260 | 260 | function relance_maj($meta, $table, $redirect = '') { |
| 261 | - include_spip('inc/headers'); |
|
| 262 | - if (!$redirect) { |
|
| 263 | - // recuperer la valeur installee en cours |
|
| 264 | - // on la tronque numeriquement, elle ne sert pas reellement |
|
| 265 | - // sauf pour verifier que ce n'est pas oui ou non |
|
| 266 | - // sinon is_numeric va echouer sur un numero de version 1.2.3 |
|
| 267 | - $installee = (int) $GLOBALS[$table][$meta]; |
|
| 268 | - $redirect = generer_url_ecrire('upgrade', "reinstall=$installee&meta=$meta&table=$table", true); |
|
| 269 | - } |
|
| 270 | - echo redirige_formulaire($redirect); |
|
| 271 | - exit(); |
|
| 261 | + include_spip('inc/headers'); |
|
| 262 | + if (!$redirect) { |
|
| 263 | + // recuperer la valeur installee en cours |
|
| 264 | + // on la tronque numeriquement, elle ne sert pas reellement |
|
| 265 | + // sauf pour verifier que ce n'est pas oui ou non |
|
| 266 | + // sinon is_numeric va echouer sur un numero de version 1.2.3 |
|
| 267 | + $installee = (int) $GLOBALS[$table][$meta]; |
|
| 268 | + $redirect = generer_url_ecrire('upgrade', "reinstall=$installee&meta=$meta&table=$table", true); |
|
| 269 | + } |
|
| 270 | + echo redirige_formulaire($redirect); |
|
| 271 | + exit(); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
@@ -281,29 +281,29 @@ discard block |
||
| 281 | 281 | * @return void |
| 282 | 282 | */ |
| 283 | 283 | function maj_debut_page($installee, $meta, $table) { |
| 284 | - static $done = false; |
|
| 285 | - if ($done) { |
|
| 286 | - return; |
|
| 287 | - } |
|
| 288 | - include_spip('inc/minipres'); |
|
| 289 | - include_spip('inc/filtres'); |
|
| 290 | - if (function_exists('ini_set')) { |
|
| 291 | - @ini_set('zlib.output_compression', '0'); // pour permettre l'affichage au fur et a mesure |
|
| 292 | - } |
|
| 293 | - $timeout = _UPGRADE_TIME_OUT * 2; |
|
| 294 | - $titre = _T('titre_page_upgrade'); |
|
| 295 | - $balise_img = charger_filtre('balise_img'); |
|
| 296 | - $titre .= $balise_img(chemin_image('loader.svg'), '', 'loader'); |
|
| 297 | - echo(install_debut_html($titre)); |
|
| 298 | - // script de rechargement auto sur timeout |
|
| 299 | - $redirect = generer_url_ecrire('upgrade', "reinstall=$installee&meta=$meta&table=$table", true); |
|
| 300 | - echo http_script("window.setTimeout('location.href=\"" . $redirect . "\";'," . ($timeout * 1000) . ')'); |
|
| 301 | - echo "<div style='text-align: left'>\n"; |
|
| 302 | - if (ob_get_level()) { |
|
| 303 | - ob_flush(); |
|
| 304 | - } |
|
| 305 | - flush(); |
|
| 306 | - $done = true; |
|
| 284 | + static $done = false; |
|
| 285 | + if ($done) { |
|
| 286 | + return; |
|
| 287 | + } |
|
| 288 | + include_spip('inc/minipres'); |
|
| 289 | + include_spip('inc/filtres'); |
|
| 290 | + if (function_exists('ini_set')) { |
|
| 291 | + @ini_set('zlib.output_compression', '0'); // pour permettre l'affichage au fur et a mesure |
|
| 292 | + } |
|
| 293 | + $timeout = _UPGRADE_TIME_OUT * 2; |
|
| 294 | + $titre = _T('titre_page_upgrade'); |
|
| 295 | + $balise_img = charger_filtre('balise_img'); |
|
| 296 | + $titre .= $balise_img(chemin_image('loader.svg'), '', 'loader'); |
|
| 297 | + echo(install_debut_html($titre)); |
|
| 298 | + // script de rechargement auto sur timeout |
|
| 299 | + $redirect = generer_url_ecrire('upgrade', "reinstall=$installee&meta=$meta&table=$table", true); |
|
| 300 | + echo http_script("window.setTimeout('location.href=\"" . $redirect . "\";'," . ($timeout * 1000) . ')'); |
|
| 301 | + echo "<div style='text-align: left'>\n"; |
|
| 302 | + if (ob_get_level()) { |
|
| 303 | + ob_flush(); |
|
| 304 | + } |
|
| 305 | + flush(); |
|
| 306 | + $done = true; |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | |
@@ -347,63 +347,63 @@ discard block |
||
| 347 | 347 | * - tableau vide sinon. |
| 348 | 348 | */ |
| 349 | 349 | function maj_while($installee, $cible, $maj, $meta = '', $table = 'meta', $redirect = '', $debut_page = false) { |
| 350 | - # inclusions pour que les procedures d'upgrade disposent des fonctions de base |
|
| 351 | - include_spip('base/create'); |
|
| 352 | - include_spip('base/abstract_sql'); |
|
| 353 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 354 | - include_spip('inc/plugin'); // pour spip_version_compare |
|
| 355 | - $n = 0; |
|
| 356 | - $time = time(); |
|
| 357 | - |
|
| 358 | - if (!defined('_TIME_OUT')) { |
|
| 359 | - /** |
|
| 360 | - * Définir le timeout qui peut-être utilisé dans les fonctions |
|
| 361 | - * de mises à jour qui durent trop longtemps |
|
| 362 | - * |
|
| 363 | - * À utiliser tel que : `if (time() >= _TIME_OUT)` |
|
| 364 | - * |
|
| 365 | - * @var int |
|
| 366 | - */ |
|
| 367 | - define('_TIME_OUT', $time + _UPGRADE_TIME_OUT); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - foreach ($maj as $v => $operations) { |
|
| 371 | - // si une maj pour cette version |
|
| 372 | - if ( |
|
| 373 | - $v == 'init' |
|
| 374 | - || spip_version_compare($v, $installee, '>') && spip_version_compare($v, $cible, '<=') |
|
| 375 | - ) { |
|
| 376 | - if ($debut_page) { |
|
| 377 | - maj_debut_page($v, $meta, $table); |
|
| 378 | - } |
|
| 379 | - echo "MAJ $v"; |
|
| 380 | - $etape = serie_alter($v, $operations, $meta, $table, $redirect); |
|
| 381 | - $trouver_table(''); // vider le cache des descriptions de table |
|
| 382 | - # echec sur une etape en cours ? |
|
| 383 | - # on sort |
|
| 384 | - if ($etape) { |
|
| 385 | - return [$v, $etape]; |
|
| 386 | - } |
|
| 387 | - $n = time() - $time; |
|
| 388 | - spip_log("$table $meta: $v en $n secondes", 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 389 | - if ($meta) { |
|
| 390 | - ecrire_meta($meta, $installee = $v, 'oui', $table); |
|
| 391 | - } |
|
| 392 | - echo (_IS_CLI ? "\n" : '<br />'); |
|
| 393 | - } |
|
| 394 | - if (time() >= _TIME_OUT) { |
|
| 395 | - relance_maj($meta, $table, $redirect); |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - $trouver_table(''); // vider le cache des descriptions de table |
|
| 399 | - // indispensable pour les chgt de versions qui n'ecrivent pas en base |
|
| 400 | - // tant pis pour la redondance eventuelle avec ci-dessus |
|
| 401 | - if ($meta) { |
|
| 402 | - ecrire_meta($meta, $cible, 'oui', $table); |
|
| 403 | - } |
|
| 404 | - spip_log("MAJ terminee. $meta: $installee", 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 405 | - |
|
| 406 | - return []; |
|
| 350 | + # inclusions pour que les procedures d'upgrade disposent des fonctions de base |
|
| 351 | + include_spip('base/create'); |
|
| 352 | + include_spip('base/abstract_sql'); |
|
| 353 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 354 | + include_spip('inc/plugin'); // pour spip_version_compare |
|
| 355 | + $n = 0; |
|
| 356 | + $time = time(); |
|
| 357 | + |
|
| 358 | + if (!defined('_TIME_OUT')) { |
|
| 359 | + /** |
|
| 360 | + * Définir le timeout qui peut-être utilisé dans les fonctions |
|
| 361 | + * de mises à jour qui durent trop longtemps |
|
| 362 | + * |
|
| 363 | + * À utiliser tel que : `if (time() >= _TIME_OUT)` |
|
| 364 | + * |
|
| 365 | + * @var int |
|
| 366 | + */ |
|
| 367 | + define('_TIME_OUT', $time + _UPGRADE_TIME_OUT); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + foreach ($maj as $v => $operations) { |
|
| 371 | + // si une maj pour cette version |
|
| 372 | + if ( |
|
| 373 | + $v == 'init' |
|
| 374 | + || spip_version_compare($v, $installee, '>') && spip_version_compare($v, $cible, '<=') |
|
| 375 | + ) { |
|
| 376 | + if ($debut_page) { |
|
| 377 | + maj_debut_page($v, $meta, $table); |
|
| 378 | + } |
|
| 379 | + echo "MAJ $v"; |
|
| 380 | + $etape = serie_alter($v, $operations, $meta, $table, $redirect); |
|
| 381 | + $trouver_table(''); // vider le cache des descriptions de table |
|
| 382 | + # echec sur une etape en cours ? |
|
| 383 | + # on sort |
|
| 384 | + if ($etape) { |
|
| 385 | + return [$v, $etape]; |
|
| 386 | + } |
|
| 387 | + $n = time() - $time; |
|
| 388 | + spip_log("$table $meta: $v en $n secondes", 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 389 | + if ($meta) { |
|
| 390 | + ecrire_meta($meta, $installee = $v, 'oui', $table); |
|
| 391 | + } |
|
| 392 | + echo (_IS_CLI ? "\n" : '<br />'); |
|
| 393 | + } |
|
| 394 | + if (time() >= _TIME_OUT) { |
|
| 395 | + relance_maj($meta, $table, $redirect); |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + $trouver_table(''); // vider le cache des descriptions de table |
|
| 399 | + // indispensable pour les chgt de versions qui n'ecrivent pas en base |
|
| 400 | + // tant pis pour la redondance eventuelle avec ci-dessus |
|
| 401 | + if ($meta) { |
|
| 402 | + ecrire_meta($meta, $cible, 'oui', $table); |
|
| 403 | + } |
|
| 404 | + spip_log("MAJ terminee. $meta: $installee", 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 405 | + |
|
| 406 | + return []; |
|
| 407 | 407 | } |
| 408 | 408 | |
| 409 | 409 | /** |
@@ -426,53 +426,53 @@ discard block |
||
| 426 | 426 | * @return int |
| 427 | 427 | */ |
| 428 | 428 | function serie_alter($serie, $q = [], $meta = '', $table = 'meta', $redirect = '') { |
| 429 | - $meta2 = $meta . '_maj_' . $serie; |
|
| 430 | - $etape = 0; |
|
| 431 | - if (isset($GLOBALS[$table][$meta2])) { |
|
| 432 | - $etape = (int) $GLOBALS[$table][$meta2]; |
|
| 433 | - } |
|
| 434 | - foreach ($q as $i => $r) { |
|
| 435 | - if ($i >= $etape) { |
|
| 436 | - $msg = "maj $table $meta2 etape $i"; |
|
| 437 | - if ( |
|
| 438 | - is_array($r) |
|
| 439 | - && function_exists($f = array_shift($r)) |
|
| 440 | - ) { |
|
| 441 | - // note: $r (arguments de la fonction $f) peut avoir des données tabulaires |
|
| 442 | - spip_log("$msg: $f " . @implode(',', $r), 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 443 | - // pour les fonctions atomiques sql_xx |
|
| 444 | - // on enregistre le meta avant de lancer la fonction, |
|
| 445 | - // de maniere a eviter de boucler sur timeout |
|
| 446 | - // mais pour les fonctions complexes, |
|
| 447 | - // il faut les rejouer jusqu'a achevement. |
|
| 448 | - // C'est a elle d'assurer qu'elles progressent a chaque rappel |
|
| 449 | - if (str_starts_with($f, 'sql_')) { |
|
| 450 | - ecrire_meta($meta2, $i + 1, 'non', $table); |
|
| 451 | - } |
|
| 452 | - echo (_IS_CLI ? '.' : " <span title='$i'>.</span>"); |
|
| 453 | - $f(...$r); |
|
| 454 | - // si temps imparti depasse, on relance sans ecrire en meta |
|
| 455 | - // car on est peut etre sorti sur timeout si c'est une fonction longue |
|
| 456 | - if (time() >= _TIME_OUT) { |
|
| 457 | - relance_maj($meta, $table, $redirect); |
|
| 458 | - } |
|
| 459 | - ecrire_meta($meta2, $i + 1, 'non', $table); |
|
| 460 | - spip_log("$meta2: ok", 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 461 | - } else { |
|
| 462 | - if (!is_array($r)) { |
|
| 463 | - spip_log("maj $i format incorrect", 'maj.' . _LOG_ERREUR); |
|
| 464 | - } else { |
|
| 465 | - spip_log("maj $i fonction $f non definie", 'maj.' . _LOG_ERREUR); |
|
| 466 | - } |
|
| 467 | - // en cas d'erreur serieuse, on s'arrete |
|
| 468 | - // mais on permet de passer par dessus en rechargeant la page. |
|
| 469 | - return $i + 1; |
|
| 470 | - } |
|
| 471 | - } |
|
| 472 | - } |
|
| 473 | - effacer_meta($meta2, $table); |
|
| 474 | - |
|
| 475 | - return 0; |
|
| 429 | + $meta2 = $meta . '_maj_' . $serie; |
|
| 430 | + $etape = 0; |
|
| 431 | + if (isset($GLOBALS[$table][$meta2])) { |
|
| 432 | + $etape = (int) $GLOBALS[$table][$meta2]; |
|
| 433 | + } |
|
| 434 | + foreach ($q as $i => $r) { |
|
| 435 | + if ($i >= $etape) { |
|
| 436 | + $msg = "maj $table $meta2 etape $i"; |
|
| 437 | + if ( |
|
| 438 | + is_array($r) |
|
| 439 | + && function_exists($f = array_shift($r)) |
|
| 440 | + ) { |
|
| 441 | + // note: $r (arguments de la fonction $f) peut avoir des données tabulaires |
|
| 442 | + spip_log("$msg: $f " . @implode(',', $r), 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 443 | + // pour les fonctions atomiques sql_xx |
|
| 444 | + // on enregistre le meta avant de lancer la fonction, |
|
| 445 | + // de maniere a eviter de boucler sur timeout |
|
| 446 | + // mais pour les fonctions complexes, |
|
| 447 | + // il faut les rejouer jusqu'a achevement. |
|
| 448 | + // C'est a elle d'assurer qu'elles progressent a chaque rappel |
|
| 449 | + if (str_starts_with($f, 'sql_')) { |
|
| 450 | + ecrire_meta($meta2, $i + 1, 'non', $table); |
|
| 451 | + } |
|
| 452 | + echo (_IS_CLI ? '.' : " <span title='$i'>.</span>"); |
|
| 453 | + $f(...$r); |
|
| 454 | + // si temps imparti depasse, on relance sans ecrire en meta |
|
| 455 | + // car on est peut etre sorti sur timeout si c'est une fonction longue |
|
| 456 | + if (time() >= _TIME_OUT) { |
|
| 457 | + relance_maj($meta, $table, $redirect); |
|
| 458 | + } |
|
| 459 | + ecrire_meta($meta2, $i + 1, 'non', $table); |
|
| 460 | + spip_log("$meta2: ok", 'maj.' . _LOG_INFO_IMPORTANTE); |
|
| 461 | + } else { |
|
| 462 | + if (!is_array($r)) { |
|
| 463 | + spip_log("maj $i format incorrect", 'maj.' . _LOG_ERREUR); |
|
| 464 | + } else { |
|
| 465 | + spip_log("maj $i fonction $f non definie", 'maj.' . _LOG_ERREUR); |
|
| 466 | + } |
|
| 467 | + // en cas d'erreur serieuse, on s'arrete |
|
| 468 | + // mais on permet de passer par dessus en rechargeant la page. |
|
| 469 | + return $i + 1; |
|
| 470 | + } |
|
| 471 | + } |
|
| 472 | + } |
|
| 473 | + effacer_meta($meta2, $table); |
|
| 474 | + |
|
| 475 | + return 0; |
|
| 476 | 476 | } |
| 477 | 477 | |
| 478 | 478 | /** |
@@ -481,16 +481,16 @@ discard block |
||
| 481 | 481 | * @return bool True si possible. |
| 482 | 482 | **/ |
| 483 | 483 | function upgrade_test() { |
| 484 | - sql_drop_table('spip_test', true); |
|
| 485 | - sql_create('spip_test', ['a' => 'int']); |
|
| 486 | - sql_alter('TABLE spip_test ADD b INT'); |
|
| 487 | - sql_insertq('spip_test', ['b' => 1], ['field' => ['b' => 'int']]); |
|
| 488 | - $result = sql_select('b', 'spip_test'); |
|
| 489 | - // ne pas garder le resultat de la requete sinon sqlite3 |
|
| 490 | - // ne peut pas supprimer la table spip_test lors du sql_alter qui suit |
|
| 491 | - // car cette table serait alors 'verouillee' |
|
| 492 | - $result = (bool) $result; |
|
| 493 | - sql_alter('TABLE spip_test DROP b'); |
|
| 494 | - |
|
| 495 | - return $result; |
|
| 484 | + sql_drop_table('spip_test', true); |
|
| 485 | + sql_create('spip_test', ['a' => 'int']); |
|
| 486 | + sql_alter('TABLE spip_test ADD b INT'); |
|
| 487 | + sql_insertq('spip_test', ['b' => 1], ['field' => ['b' => 'int']]); |
|
| 488 | + $result = sql_select('b', 'spip_test'); |
|
| 489 | + // ne pas garder le resultat de la requete sinon sqlite3 |
|
| 490 | + // ne peut pas supprimer la table spip_test lors du sql_alter qui suit |
|
| 491 | + // car cette table serait alors 'verouillee' |
|
| 492 | + $result = (bool) $result; |
|
| 493 | + sql_alter('TABLE spip_test DROP b'); |
|
| 494 | + |
|
| 495 | + return $result; |
|
| 496 | 496 | } |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | * @package SPIP\Core\SQL\Tables |
| 16 | 16 | **/ |
| 17 | 17 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 18 | - return; |
|
| 18 | + return; |
|
| 19 | 19 | } |
| 20 | 20 | include_spip('base/objets'); |
| 21 | 21 | |
@@ -67,150 +67,150 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | **/ |
| 69 | 69 | function base_trouver_table_dist($nom, $serveur = '', $table_spip = true, array $options = []) { |
| 70 | - $desc_cache = null; |
|
| 71 | - static $nom_cache_desc_sql = []; |
|
| 72 | - |
|
| 73 | - if ( |
|
| 74 | - !spip_connect($serveur) |
|
| 75 | - || !preg_match('/^[a-zA-Z0-9._-]*/', $nom) |
|
| 76 | - ) { |
|
| 77 | - return null; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $options += [ |
|
| 81 | - // si false, baissera le niveau de log si une table demandée n’existe pas |
|
| 82 | - 'log_missing' => true, |
|
| 83 | - ]; |
|
| 84 | - |
|
| 85 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 86 | - $objets_sql = lister_tables_objets_sql('::md5'); |
|
| 87 | - |
|
| 88 | - // le nom du cache depend du serveur mais aussi du nom de la db et du prefixe |
|
| 89 | - // ce qui permet une auto invalidation en cas de modif manuelle du fichier |
|
| 90 | - // de connexion, et tout risque d'ambiguite |
|
| 91 | - if (!isset($nom_cache_desc_sql[$serveur][$objets_sql])) { |
|
| 92 | - $nom_cache_desc_sql[$serveur][$objets_sql] = |
|
| 93 | - _DIR_CACHE . 'sql_desc_' |
|
| 94 | - . ($serveur ? "{$serveur}_" : '') |
|
| 95 | - . substr(md5($connexion['db'] . ':' . $connexion['prefixe'] . ":$objets_sql"), 0, 8) |
|
| 96 | - . '.txt'; |
|
| 97 | - // nouveau nom de cache = nouvelle version en memoire |
|
| 98 | - unset($connexion['tables']); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // un appel avec $nom vide est une demande explicite de vidange du cache des descriptions |
|
| 102 | - if (!$nom) { |
|
| 103 | - spip_unlink($nom_cache_desc_sql[$serveur][$objets_sql]); |
|
| 104 | - $connexion['tables'] = []; |
|
| 105 | - |
|
| 106 | - return null; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - $nom_sql = $nom; |
|
| 110 | - $nom_sql = preg_match('/\.(.*)$/', $nom, $s) ? $s[1] : $nom; |
|
| 111 | - |
|
| 112 | - $fdesc = $desc = ''; |
|
| 113 | - $connexion = &$GLOBALS['connexions'][$serveur ?: 0]; |
|
| 114 | - |
|
| 115 | - // base sous SPIP: gerer les abreviations explicites des noms de table |
|
| 116 | - if ( |
|
| 117 | - $connexion['spip_connect_version'] |
|
| 118 | - && $table_spip |
|
| 119 | - && isset($GLOBALS['table_des_tables'][$nom]) |
|
| 120 | - ) { |
|
| 121 | - $nom = $GLOBALS['table_des_tables'][$nom]; |
|
| 122 | - $nom_sql = 'spip_' . $nom; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // si c'est la premiere table qu'on cherche |
|
| 126 | - // et si on est pas explicitement en recalcul |
|
| 127 | - // on essaye de recharger le cache des decriptions de ce serveur |
|
| 128 | - // dans le fichier cache |
|
| 129 | - if ( |
|
| 130 | - !isset($connexion['tables'][$nom_sql]) |
|
| 131 | - && defined('_VAR_MODE') |
|
| 132 | - && _VAR_MODE !== 'recalcul' |
|
| 133 | - && (!isset($connexion['tables']) || !$connexion['tables']) |
|
| 134 | - && lire_fichier($nom_cache_desc_sql[$serveur][$objets_sql], $desc_cache) |
|
| 135 | - && ($desc_cache = unserialize($desc_cache)) |
|
| 136 | - ) { |
|
| 137 | - $connexion['tables'] = $desc_cache; |
|
| 138 | - } |
|
| 139 | - if ($table_spip && !isset($connexion['tables'][$nom_sql])) { |
|
| 140 | - if (isset($GLOBALS['tables_principales'][$nom_sql])) { |
|
| 141 | - $fdesc = $GLOBALS['tables_principales'][$nom_sql]; |
|
| 142 | - } |
|
| 143 | - // meme si pas d'abreviation declaree, trouver la table spip_$nom |
|
| 144 | - // si c'est une table principale, |
|
| 145 | - // puisqu'on le fait aussi pour les tables auxiliaires |
|
| 146 | - elseif ($nom_sql == $nom && isset($GLOBALS['tables_principales']['spip_' . $nom])) { |
|
| 147 | - $nom_sql = 'spip_' . $nom; |
|
| 148 | - $fdesc = &$GLOBALS['tables_principales'][$nom_sql]; |
|
| 149 | - } elseif ( |
|
| 150 | - isset($GLOBALS['tables_auxiliaires'][$n = $nom]) |
|
| 151 | - || isset($GLOBALS['tables_auxiliaires'][$n = 'spip_' . $nom]) |
|
| 152 | - ) { |
|
| 153 | - $nom_sql = $n; |
|
| 154 | - $fdesc = &$GLOBALS['tables_auxiliaires'][$n]; |
|
| 155 | - } # table locale a cote de SPIP, comme non SPIP: |
|
| 156 | - } |
|
| 157 | - if (!isset($connexion['tables'][$nom_sql])) { |
|
| 158 | - // La *vraie* base a la priorite |
|
| 159 | - $exists = sql_table_exists($nom_sql, $table_spip, $serveur); |
|
| 160 | - if ( |
|
| 161 | - !$exists |
|
| 162 | - || !($desc = sql_showtable($nom_sql, $table_spip, $serveur)) |
|
| 163 | - || !$desc['field'] |
|
| 164 | - ) { |
|
| 165 | - if (!$fdesc) { |
|
| 166 | - $log_level = $options['log_missing'] ? _LOG_INFO_IMPORTANTE : _LOG_DEBUG; |
|
| 167 | - spip_log("trouver_table: table inconnue '$serveur' '$nom'", 'base' . $log_level); |
|
| 168 | - |
|
| 169 | - return null; |
|
| 170 | - } |
|
| 171 | - // on ne sait pas lire la structure de la table : |
|
| 172 | - // on retombe sur la description donnee dans les fichiers spip |
|
| 173 | - $desc = $fdesc; |
|
| 174 | - $desc['exist'] = false; |
|
| 175 | - } else { |
|
| 176 | - $desc['exist'] = true; |
|
| 177 | - // gerer le cas des cles vides (echec de l'analyse sur une vue par exemple) |
|
| 178 | - // pour recuperer la declaration de lister_tables_objets_sql() si il y en a une |
|
| 179 | - if (! $desc['key']) { |
|
| 180 | - spip_log("trouver_table: table sans cle '$serveur' '$nom'", 'base'); |
|
| 181 | - unset($desc['key']); |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - $desc['table'] = $desc['table_sql'] = $nom_sql; |
|
| 186 | - $desc['connexion'] = $serveur; |
|
| 187 | - |
|
| 188 | - // charger les infos declarees pour cette table |
|
| 189 | - // en lui passant les infos connues |
|
| 190 | - // $desc est prioritaire pour la description de la table |
|
| 191 | - $desc = array_merge(lister_tables_objets_sql($nom_sql, $desc), $desc); |
|
| 192 | - // s'assurer qu'on a toujours un 'key' |
|
| 193 | - if (!isset($desc['key']) && !empty($fdesc['key'])) { |
|
| 194 | - $desc['key'] = $fdesc['key']; |
|
| 195 | - } |
|
| 196 | - if (! isset($desc['key'])) { |
|
| 197 | - $desc['key'] = []; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // si tables_objets_sql est bien fini d'init, on peut cacher |
|
| 201 | - $connexion['tables'][$nom_sql] = $desc; |
|
| 202 | - $res = &$connexion['tables'][$nom_sql]; |
|
| 203 | - // une nouvelle table a ete decrite |
|
| 204 | - // mettons donc a jour le cache des descriptions de ce serveur |
|
| 205 | - if (is_writable(_DIR_CACHE)) { |
|
| 206 | - ecrire_fichier($nom_cache_desc_sql[$serveur][$objets_sql], serialize($connexion['tables']), true); |
|
| 207 | - } |
|
| 208 | - } else { |
|
| 209 | - $res = &$connexion['tables'][$nom_sql]; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - // toujours retourner $nom dans id_table |
|
| 213 | - $res['id_table'] = $nom; |
|
| 214 | - |
|
| 215 | - return $res; |
|
| 70 | + $desc_cache = null; |
|
| 71 | + static $nom_cache_desc_sql = []; |
|
| 72 | + |
|
| 73 | + if ( |
|
| 74 | + !spip_connect($serveur) |
|
| 75 | + || !preg_match('/^[a-zA-Z0-9._-]*/', $nom) |
|
| 76 | + ) { |
|
| 77 | + return null; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $options += [ |
|
| 81 | + // si false, baissera le niveau de log si une table demandée n’existe pas |
|
| 82 | + 'log_missing' => true, |
|
| 83 | + ]; |
|
| 84 | + |
|
| 85 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 86 | + $objets_sql = lister_tables_objets_sql('::md5'); |
|
| 87 | + |
|
| 88 | + // le nom du cache depend du serveur mais aussi du nom de la db et du prefixe |
|
| 89 | + // ce qui permet une auto invalidation en cas de modif manuelle du fichier |
|
| 90 | + // de connexion, et tout risque d'ambiguite |
|
| 91 | + if (!isset($nom_cache_desc_sql[$serveur][$objets_sql])) { |
|
| 92 | + $nom_cache_desc_sql[$serveur][$objets_sql] = |
|
| 93 | + _DIR_CACHE . 'sql_desc_' |
|
| 94 | + . ($serveur ? "{$serveur}_" : '') |
|
| 95 | + . substr(md5($connexion['db'] . ':' . $connexion['prefixe'] . ":$objets_sql"), 0, 8) |
|
| 96 | + . '.txt'; |
|
| 97 | + // nouveau nom de cache = nouvelle version en memoire |
|
| 98 | + unset($connexion['tables']); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // un appel avec $nom vide est une demande explicite de vidange du cache des descriptions |
|
| 102 | + if (!$nom) { |
|
| 103 | + spip_unlink($nom_cache_desc_sql[$serveur][$objets_sql]); |
|
| 104 | + $connexion['tables'] = []; |
|
| 105 | + |
|
| 106 | + return null; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + $nom_sql = $nom; |
|
| 110 | + $nom_sql = preg_match('/\.(.*)$/', $nom, $s) ? $s[1] : $nom; |
|
| 111 | + |
|
| 112 | + $fdesc = $desc = ''; |
|
| 113 | + $connexion = &$GLOBALS['connexions'][$serveur ?: 0]; |
|
| 114 | + |
|
| 115 | + // base sous SPIP: gerer les abreviations explicites des noms de table |
|
| 116 | + if ( |
|
| 117 | + $connexion['spip_connect_version'] |
|
| 118 | + && $table_spip |
|
| 119 | + && isset($GLOBALS['table_des_tables'][$nom]) |
|
| 120 | + ) { |
|
| 121 | + $nom = $GLOBALS['table_des_tables'][$nom]; |
|
| 122 | + $nom_sql = 'spip_' . $nom; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // si c'est la premiere table qu'on cherche |
|
| 126 | + // et si on est pas explicitement en recalcul |
|
| 127 | + // on essaye de recharger le cache des decriptions de ce serveur |
|
| 128 | + // dans le fichier cache |
|
| 129 | + if ( |
|
| 130 | + !isset($connexion['tables'][$nom_sql]) |
|
| 131 | + && defined('_VAR_MODE') |
|
| 132 | + && _VAR_MODE !== 'recalcul' |
|
| 133 | + && (!isset($connexion['tables']) || !$connexion['tables']) |
|
| 134 | + && lire_fichier($nom_cache_desc_sql[$serveur][$objets_sql], $desc_cache) |
|
| 135 | + && ($desc_cache = unserialize($desc_cache)) |
|
| 136 | + ) { |
|
| 137 | + $connexion['tables'] = $desc_cache; |
|
| 138 | + } |
|
| 139 | + if ($table_spip && !isset($connexion['tables'][$nom_sql])) { |
|
| 140 | + if (isset($GLOBALS['tables_principales'][$nom_sql])) { |
|
| 141 | + $fdesc = $GLOBALS['tables_principales'][$nom_sql]; |
|
| 142 | + } |
|
| 143 | + // meme si pas d'abreviation declaree, trouver la table spip_$nom |
|
| 144 | + // si c'est une table principale, |
|
| 145 | + // puisqu'on le fait aussi pour les tables auxiliaires |
|
| 146 | + elseif ($nom_sql == $nom && isset($GLOBALS['tables_principales']['spip_' . $nom])) { |
|
| 147 | + $nom_sql = 'spip_' . $nom; |
|
| 148 | + $fdesc = &$GLOBALS['tables_principales'][$nom_sql]; |
|
| 149 | + } elseif ( |
|
| 150 | + isset($GLOBALS['tables_auxiliaires'][$n = $nom]) |
|
| 151 | + || isset($GLOBALS['tables_auxiliaires'][$n = 'spip_' . $nom]) |
|
| 152 | + ) { |
|
| 153 | + $nom_sql = $n; |
|
| 154 | + $fdesc = &$GLOBALS['tables_auxiliaires'][$n]; |
|
| 155 | + } # table locale a cote de SPIP, comme non SPIP: |
|
| 156 | + } |
|
| 157 | + if (!isset($connexion['tables'][$nom_sql])) { |
|
| 158 | + // La *vraie* base a la priorite |
|
| 159 | + $exists = sql_table_exists($nom_sql, $table_spip, $serveur); |
|
| 160 | + if ( |
|
| 161 | + !$exists |
|
| 162 | + || !($desc = sql_showtable($nom_sql, $table_spip, $serveur)) |
|
| 163 | + || !$desc['field'] |
|
| 164 | + ) { |
|
| 165 | + if (!$fdesc) { |
|
| 166 | + $log_level = $options['log_missing'] ? _LOG_INFO_IMPORTANTE : _LOG_DEBUG; |
|
| 167 | + spip_log("trouver_table: table inconnue '$serveur' '$nom'", 'base' . $log_level); |
|
| 168 | + |
|
| 169 | + return null; |
|
| 170 | + } |
|
| 171 | + // on ne sait pas lire la structure de la table : |
|
| 172 | + // on retombe sur la description donnee dans les fichiers spip |
|
| 173 | + $desc = $fdesc; |
|
| 174 | + $desc['exist'] = false; |
|
| 175 | + } else { |
|
| 176 | + $desc['exist'] = true; |
|
| 177 | + // gerer le cas des cles vides (echec de l'analyse sur une vue par exemple) |
|
| 178 | + // pour recuperer la declaration de lister_tables_objets_sql() si il y en a une |
|
| 179 | + if (! $desc['key']) { |
|
| 180 | + spip_log("trouver_table: table sans cle '$serveur' '$nom'", 'base'); |
|
| 181 | + unset($desc['key']); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + $desc['table'] = $desc['table_sql'] = $nom_sql; |
|
| 186 | + $desc['connexion'] = $serveur; |
|
| 187 | + |
|
| 188 | + // charger les infos declarees pour cette table |
|
| 189 | + // en lui passant les infos connues |
|
| 190 | + // $desc est prioritaire pour la description de la table |
|
| 191 | + $desc = array_merge(lister_tables_objets_sql($nom_sql, $desc), $desc); |
|
| 192 | + // s'assurer qu'on a toujours un 'key' |
|
| 193 | + if (!isset($desc['key']) && !empty($fdesc['key'])) { |
|
| 194 | + $desc['key'] = $fdesc['key']; |
|
| 195 | + } |
|
| 196 | + if (! isset($desc['key'])) { |
|
| 197 | + $desc['key'] = []; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // si tables_objets_sql est bien fini d'init, on peut cacher |
|
| 201 | + $connexion['tables'][$nom_sql] = $desc; |
|
| 202 | + $res = &$connexion['tables'][$nom_sql]; |
|
| 203 | + // une nouvelle table a ete decrite |
|
| 204 | + // mettons donc a jour le cache des descriptions de ce serveur |
|
| 205 | + if (is_writable(_DIR_CACHE)) { |
|
| 206 | + ecrire_fichier($nom_cache_desc_sql[$serveur][$objets_sql], serialize($connexion['tables']), true); |
|
| 207 | + } |
|
| 208 | + } else { |
|
| 209 | + $res = &$connexion['tables'][$nom_sql]; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + // toujours retourner $nom dans id_table |
|
| 213 | + $res['id_table'] = $nom; |
|
| 214 | + |
|
| 215 | + return $res; |
|
| 216 | 216 | } |
@@ -90,9 +90,9 @@ discard block |
||
| 90 | 90 | // de connexion, et tout risque d'ambiguite |
| 91 | 91 | if (!isset($nom_cache_desc_sql[$serveur][$objets_sql])) { |
| 92 | 92 | $nom_cache_desc_sql[$serveur][$objets_sql] = |
| 93 | - _DIR_CACHE . 'sql_desc_' |
|
| 93 | + _DIR_CACHE.'sql_desc_' |
|
| 94 | 94 | . ($serveur ? "{$serveur}_" : '') |
| 95 | - . substr(md5($connexion['db'] . ':' . $connexion['prefixe'] . ":$objets_sql"), 0, 8) |
|
| 95 | + . substr(md5($connexion['db'].':'.$connexion['prefixe'].":$objets_sql"), 0, 8) |
|
| 96 | 96 | . '.txt'; |
| 97 | 97 | // nouveau nom de cache = nouvelle version en memoire |
| 98 | 98 | unset($connexion['tables']); |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | && isset($GLOBALS['table_des_tables'][$nom]) |
| 120 | 120 | ) { |
| 121 | 121 | $nom = $GLOBALS['table_des_tables'][$nom]; |
| 122 | - $nom_sql = 'spip_' . $nom; |
|
| 122 | + $nom_sql = 'spip_'.$nom; |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // si c'est la premiere table qu'on cherche |
@@ -143,12 +143,12 @@ discard block |
||
| 143 | 143 | // meme si pas d'abreviation declaree, trouver la table spip_$nom |
| 144 | 144 | // si c'est une table principale, |
| 145 | 145 | // puisqu'on le fait aussi pour les tables auxiliaires |
| 146 | - elseif ($nom_sql == $nom && isset($GLOBALS['tables_principales']['spip_' . $nom])) { |
|
| 147 | - $nom_sql = 'spip_' . $nom; |
|
| 146 | + elseif ($nom_sql == $nom && isset($GLOBALS['tables_principales']['spip_'.$nom])) { |
|
| 147 | + $nom_sql = 'spip_'.$nom; |
|
| 148 | 148 | $fdesc = &$GLOBALS['tables_principales'][$nom_sql]; |
| 149 | 149 | } elseif ( |
| 150 | 150 | isset($GLOBALS['tables_auxiliaires'][$n = $nom]) |
| 151 | - || isset($GLOBALS['tables_auxiliaires'][$n = 'spip_' . $nom]) |
|
| 151 | + || isset($GLOBALS['tables_auxiliaires'][$n = 'spip_'.$nom]) |
|
| 152 | 152 | ) { |
| 153 | 153 | $nom_sql = $n; |
| 154 | 154 | $fdesc = &$GLOBALS['tables_auxiliaires'][$n]; |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | ) { |
| 165 | 165 | if (!$fdesc) { |
| 166 | 166 | $log_level = $options['log_missing'] ? _LOG_INFO_IMPORTANTE : _LOG_DEBUG; |
| 167 | - spip_log("trouver_table: table inconnue '$serveur' '$nom'", 'base' . $log_level); |
|
| 167 | + spip_log("trouver_table: table inconnue '$serveur' '$nom'", 'base'.$log_level); |
|
| 168 | 168 | |
| 169 | 169 | return null; |
| 170 | 170 | } |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | $desc['exist'] = true; |
| 177 | 177 | // gerer le cas des cles vides (echec de l'analyse sur une vue par exemple) |
| 178 | 178 | // pour recuperer la declaration de lister_tables_objets_sql() si il y en a une |
| 179 | - if (! $desc['key']) { |
|
| 179 | + if (!$desc['key']) { |
|
| 180 | 180 | spip_log("trouver_table: table sans cle '$serveur' '$nom'", 'base'); |
| 181 | 181 | unset($desc['key']); |
| 182 | 182 | } |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | if (!isset($desc['key']) && !empty($fdesc['key'])) { |
| 194 | 194 | $desc['key'] = $fdesc['key']; |
| 195 | 195 | } |
| 196 | - if (! isset($desc['key'])) { |
|
| 196 | + if (!isset($desc['key'])) { |
|
| 197 | 197 | $desc['key'] = []; |
| 198 | 198 | } |
| 199 | 199 | |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | \***************************************************************************/ |
| 11 | 11 | |
| 12 | 12 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 13 | - return; |
|
| 13 | + return; |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | /** |
@@ -22,317 +22,317 @@ discard block |
||
| 22 | 22 | **/ |
| 23 | 23 | class ValidateurXML |
| 24 | 24 | { |
| 25 | - public function validerElement($phraseur, $name, $attrs) { |
|
| 26 | - if (!($p = isset($this->dtc->elements[$name]))) { |
|
| 27 | - if ($p = strpos((string) $name, ':')) { |
|
| 28 | - $name = substr((string) $name, $p + 1); |
|
| 29 | - $p = isset($this->dtc->elements[$name]); |
|
| 30 | - } |
|
| 31 | - if (!$p) { |
|
| 32 | - coordonnees_erreur($this, " <b>$name</b> : " |
|
| 33 | - . _T('zxml_inconnu_balise')); |
|
| 34 | - |
|
| 35 | - return; |
|
| 36 | - } |
|
| 37 | - } |
|
| 38 | - // controler les filles illegitimes, ca suffit |
|
| 39 | - $depth = $this->depth; |
|
| 40 | - $ouvrant = $this->ouvrant; |
|
| 41 | - #spip_log("trouve $name apres " . $ouvrant[$depth]); |
|
| 42 | - if (isset($ouvrant[$depth]) && preg_match('/^\s*(\w+)/', (string) $ouvrant[$depth], $r)) { |
|
| 43 | - $pere = $r[1]; |
|
| 44 | - #spip_log("pere $pere"); |
|
| 45 | - if (isset($this->dtc->elements[$pere])) { |
|
| 46 | - $fils = $this->dtc->elements[$pere]; |
|
| 47 | - #spip_log("rejeton $name fils " . @join(',',$fils)); |
|
| 48 | - if (!($p = @in_array($name, $fils)) && ($p = strpos((string) $name, ':'))) { |
|
| 49 | - $p = substr((string) $name, $p + 1); |
|
| 50 | - $p = @in_array($p, $fils); |
|
| 51 | - } |
|
| 52 | - if (!$p) { |
|
| 53 | - $bons_peres = @implode('</b>, <b>', $this->dtc->peres[$name]); |
|
| 54 | - coordonnees_erreur($this, " <b>$name</b> " |
|
| 55 | - . _T('zxml_non_fils') |
|
| 56 | - . ' <b>' |
|
| 57 | - . $pere |
|
| 58 | - . '</b>' |
|
| 59 | - . ($bons_peres ? '<p style="font-size: 80%"> ' . _T('zxml_mais_de') . ' <b>' . $bons_peres . '</b></p>' : '')); |
|
| 60 | - } elseif ($this->dtc->regles[$pere][0] == '/') { |
|
| 61 | - $frat = substr((string) $depth, 2); |
|
| 62 | - if (!isset($this->fratrie[$frat])) { |
|
| 63 | - $this->fratrie[$frat] = ''; |
|
| 64 | - } |
|
| 65 | - $this->fratrie[$frat] .= "$name "; |
|
| 66 | - } |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - // Init de la suite des balises a memoriser si regle difficile |
|
| 70 | - if ($this->dtc->regles[$name] && $this->dtc->regles[$name][0] == '/') { |
|
| 71 | - $this->fratrie[$depth] = ''; |
|
| 72 | - } |
|
| 73 | - if (isset($this->dtc->attributs[$name])) { |
|
| 74 | - foreach ($this->dtc->attributs[$name] as $n => $v) { |
|
| 75 | - if ($v[1] == '#REQUIRED' && !isset($attrs[$n])) { |
|
| 76 | - coordonnees_erreur($this, " <b>$n</b>" |
|
| 77 | - . ' : ' |
|
| 78 | - . _T('zxml_obligatoire_attribut') |
|
| 79 | - . " <b>$name</b>"); |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - public function validerAttribut($phraseur, $name, $val, $bal) { |
|
| 86 | - // Si la balise est inconnue, eviter d'insister |
|
| 87 | - if (!isset($this->dtc->attributs[$bal])) { |
|
| 88 | - return; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $a = $this->dtc->attributs[$bal]; |
|
| 92 | - if (!isset($a[$name])) { |
|
| 93 | - $bons = implode(', ', array_keys($a)); |
|
| 94 | - if ($bons) { |
|
| 95 | - $bons = " title=' " . |
|
| 96 | - _T('zxml_connus_attributs') . |
|
| 97 | - ' : ' . |
|
| 98 | - $bons . |
|
| 99 | - "'"; |
|
| 100 | - } |
|
| 101 | - $bons .= " style='font-weight: bold'"; |
|
| 102 | - coordonnees_erreur($this, " <b>$name</b> " |
|
| 103 | - . _T('zxml_inconnu_attribut') . ' ' . _T('zxml_de') |
|
| 104 | - . " <a$bons>$bal</a> (" |
|
| 105 | - . _T('zxml_survoler') |
|
| 106 | - . ')'); |
|
| 107 | - } else { |
|
| 108 | - $type = $a[$name][0]; |
|
| 109 | - if (!preg_match('/^\w+$/', (string) $type)) { |
|
| 110 | - $this->valider_motif($phraseur, $name, $val, $bal, $type); |
|
| 111 | - } else { |
|
| 112 | - if (method_exists($this, $f = 'validerAttribut_' . $type)) { |
|
| 113 | - $this->$f($phraseur, $name, $val, $bal); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - # else spip_log("$type type d'attribut inconnu"); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - public function validerAttribut_NMTOKEN($phraseur, $name, $val, $bal) { |
|
| 121 | - $this->valider_motif($phraseur, $name, $val, $bal, _REGEXP_NMTOKEN); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - public function validerAttribut_NMTOKENS($phraseur, $name, $val, $bal) { |
|
| 125 | - $this->valider_motif($phraseur, $name, $val, $bal, _REGEXP_NMTOKENS); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - public function validerAttribut_ID($phraseur, $name, $val, $bal) { |
|
| 129 | - if (isset($this->ids[$val])) { |
|
| 130 | - [$l, $c] = $this->ids[$val]; |
|
| 131 | - coordonnees_erreur($this, " <p><b>$val</b> " |
|
| 132 | - . _T('zxml_valeur_attribut') |
|
| 133 | - . " <b>$name</b> " |
|
| 134 | - . _T('zxml_de') |
|
| 135 | - . " <b>$bal</b> " |
|
| 136 | - . _T('zxml_vu') |
|
| 137 | - . " (L$l,C$c)"); |
|
| 138 | - } else { |
|
| 139 | - $this->valider_motif($phraseur, $name, $val, $bal, _REGEXP_ID); |
|
| 140 | - $this->ids[$val] = [xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur)]; |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - public function validerAttribut_IDREF($phraseur, $name, $val, $bal) { |
|
| 145 | - $this->idrefs[] = [$val, xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur)]; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - public function validerAttribut_IDREFS($phraseur, $name, $val, $bal) { |
|
| 149 | - $this->idrefss[] = [$val, xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur)]; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - public function valider_motif($phraseur, $name, $val, $bal, $motif) { |
|
| 153 | - if (!preg_match($motif, (string) $val)) { |
|
| 154 | - coordonnees_erreur($this, "<b>$val</b> " |
|
| 155 | - . _T('zxml_valeur_attribut') |
|
| 156 | - . " <b>$name</b> " |
|
| 157 | - . _T('zxml_de') |
|
| 158 | - . " <b>$bal</b> " |
|
| 159 | - . _T('zxml_non_conforme') |
|
| 160 | - . '</p><p>' |
|
| 161 | - . '<b>' . $motif . '</b>'); |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - public function valider_idref($nom, $ligne, $col) { |
|
| 166 | - if (!isset($this->ids[$nom])) { |
|
| 167 | - $this->err[] = [" <p><b>$nom</b> " . _T('zxml_inconnu_id'), $ligne, $col]; |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - public function valider_passe2() { |
|
| 172 | - if (!$this->err) { |
|
| 173 | - foreach ($this->idrefs as $idref) { |
|
| 174 | - [$nom, $ligne, $col] = $idref; |
|
| 175 | - $this->valider_idref($nom, $ligne, $col); |
|
| 176 | - } |
|
| 177 | - foreach ($this->idrefss as $idref) { |
|
| 178 | - [$noms, $ligne, $col] = $idref; |
|
| 179 | - foreach (preg_split('/\s+/', (string) $noms) as $nom) { |
|
| 180 | - $this->valider_idref($nom, $ligne, $col); |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - public function debutElement($phraseur, $name, $attrs) { |
|
| 187 | - if ($this->dtc->elements) { |
|
| 188 | - $this->validerElement($phraseur, $name, $attrs); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - if ($f = $this->process['debut']) { |
|
| 192 | - $f($this, $name, $attrs); |
|
| 193 | - } |
|
| 194 | - $depth = $this->depth; |
|
| 195 | - $this->debuts[$depth] = strlen((string) $this->res); |
|
| 196 | - foreach ($attrs as $k => $v) { |
|
| 197 | - $this->validerAttribut($phraseur, $k, $v, $name); |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - public function finElement($phraseur, $name) { |
|
| 202 | - $depth = $this->depth; |
|
| 203 | - $contenu = $this->contenu; |
|
| 204 | - |
|
| 205 | - $n = strlen((string) $this->res); |
|
| 206 | - $c = strlen(trim((string) $contenu[$depth])); |
|
| 207 | - $k = $this->debuts[$depth]; |
|
| 208 | - |
|
| 209 | - $regle = $this->dtc->regles[$name] ?? false; |
|
| 210 | - $vide = ($regle == 'EMPTY'); |
|
| 211 | - // controler que les balises devant etre vides le sont |
|
| 212 | - if ($vide) { |
|
| 213 | - if ($n != $k + $c) { |
|
| 214 | - coordonnees_erreur($this, " <p><b>$name</b> " . _T('zxml_nonvide_balise')); |
|
| 215 | - } |
|
| 216 | - // pour les regles PCDATA ou iteration de disjonction, tout est fait |
|
| 217 | - } elseif ($regle && $regle != '*') { |
|
| 218 | - if ($regle == '+') { |
|
| 219 | - // iteration de disjonction non vide: 1 balise au - |
|
| 220 | - if ($n == $k) { |
|
| 221 | - coordonnees_erreur($this, "<p>\n<b>$name</b> " |
|
| 222 | - . _T('zxml_vide_balise')); |
|
| 223 | - } |
|
| 224 | - } else { |
|
| 225 | - $f = $this->fratrie[substr((string) $depth, 2)] ?? null; |
|
| 226 | - if (is_null($f) || !preg_match($regle, (string) $f)) { |
|
| 227 | - coordonnees_erreur( |
|
| 228 | - $this, |
|
| 229 | - " <p>\n<b>$name</b> " |
|
| 230 | - . _T('zxml_succession_fils_incorrecte') |
|
| 231 | - . ' : <b>' |
|
| 232 | - . $f |
|
| 233 | - . '</b>' |
|
| 234 | - ); |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - if ($f = $this->process['fin']) { |
|
| 239 | - $f($this, $name, $vide); |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - public function textElement($phraseur, $data) { |
|
| 244 | - if (trim((string) $data)) { |
|
| 245 | - $d = $this->depth; |
|
| 246 | - $d = $this->ouvrant[$d]; |
|
| 247 | - preg_match('/^\s*(\S+)/', (string) $d, $m); |
|
| 248 | - if (isset($this->dtc->pcdata[$m[1]]) && $this->dtc->pcdata[$m[1]]) { |
|
| 249 | - coordonnees_erreur($this, ' <p><b>' . $m[1] . '</b> ' |
|
| 250 | - . _T('zxml_nonvide_balise')); // message a affiner |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - if ($f = $this->process['text']) { |
|
| 254 | - $f($this, $data); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - public function piElement($phraseur, $target, $data) { |
|
| 259 | - if ($f = $this->process['pi']) { |
|
| 260 | - $f($this, $target, $data); |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - // Denonciation des entitees XML inconnues |
|
| 265 | - // Pour contourner le bug de conception de SAX qui ne signale pas si elles |
|
| 266 | - // sont dans un attribut, les entites les plus frequentes ont ete |
|
| 267 | - // transcodees au prealable (sauf & < > " que SAX traite correctement). |
|
| 268 | - // On ne les verra donc pas passer a cette etape, contrairement a ce que |
|
| 269 | - // le source de la page laisse legitimement supposer. |
|
| 270 | - |
|
| 271 | - public function defaultElement($phraseur, $data) { |
|
| 272 | - if ( |
|
| 273 | - !preg_match('/^<!--/', (string) $data) |
|
| 274 | - && preg_match_all('/&([^;]*)?/', (string) $data, $r, PREG_SET_ORDER) |
|
| 275 | - ) { |
|
| 276 | - foreach ($r as $m) { |
|
| 277 | - [$t, $e] = $m; |
|
| 278 | - if (!isset($this->dtc->entites[$e])) { |
|
| 279 | - coordonnees_erreur($this, " <b>$e</b> " |
|
| 280 | - . _T('zxml_inconnu_entite') |
|
| 281 | - . ' '); |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - if (isset($this->process['default']) && ($f = $this->process['default'])) { |
|
| 286 | - $f($this, $data); |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - public function phraserTout($phraseur, $data) { |
|
| 291 | - xml_parsestring($this, $data); |
|
| 292 | - |
|
| 293 | - if (!$this->dtc || preg_match(',^' . _MESSAGE_DOCTYPE . ',', (string) $data)) { |
|
| 294 | - $this->err[] = ['DOCTYPE ?', 0, 0]; |
|
| 295 | - } else { |
|
| 296 | - $this->valider_passe2(); |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Constructeur |
|
| 302 | - * |
|
| 303 | - * @param array $process ? |
|
| 304 | - **/ |
|
| 305 | - public function __construct($process = []) { |
|
| 306 | - if (is_array($process)) { |
|
| 307 | - $this->process = $process; |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - public $ids = []; |
|
| 312 | - public $idrefs = []; |
|
| 313 | - public $idrefss = []; |
|
| 314 | - public $debuts = []; |
|
| 315 | - public $fratrie = []; |
|
| 316 | - |
|
| 317 | - public $dtc = null; |
|
| 318 | - public $sax = null; |
|
| 319 | - public $depth = ''; |
|
| 320 | - public $entete = ''; |
|
| 321 | - public $page = ''; |
|
| 322 | - public $res = ''; |
|
| 323 | - public array $err = []; |
|
| 324 | - public array $contenu = []; |
|
| 325 | - public array $versions = []; |
|
| 326 | - |
|
| 327 | - public array $ouvrant = []; |
|
| 328 | - public array $reperes = []; |
|
| 329 | - public array $process = [ |
|
| 330 | - 'debut' => 'xml_debutElement', |
|
| 331 | - 'fin' => 'xml_finElement', |
|
| 332 | - 'text' => 'xml_textElement', |
|
| 333 | - 'pi' => 'xml_piElement', |
|
| 334 | - 'default' => 'xml_defaultElement' |
|
| 335 | - ]; |
|
| 25 | + public function validerElement($phraseur, $name, $attrs) { |
|
| 26 | + if (!($p = isset($this->dtc->elements[$name]))) { |
|
| 27 | + if ($p = strpos((string) $name, ':')) { |
|
| 28 | + $name = substr((string) $name, $p + 1); |
|
| 29 | + $p = isset($this->dtc->elements[$name]); |
|
| 30 | + } |
|
| 31 | + if (!$p) { |
|
| 32 | + coordonnees_erreur($this, " <b>$name</b> : " |
|
| 33 | + . _T('zxml_inconnu_balise')); |
|
| 34 | + |
|
| 35 | + return; |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | + // controler les filles illegitimes, ca suffit |
|
| 39 | + $depth = $this->depth; |
|
| 40 | + $ouvrant = $this->ouvrant; |
|
| 41 | + #spip_log("trouve $name apres " . $ouvrant[$depth]); |
|
| 42 | + if (isset($ouvrant[$depth]) && preg_match('/^\s*(\w+)/', (string) $ouvrant[$depth], $r)) { |
|
| 43 | + $pere = $r[1]; |
|
| 44 | + #spip_log("pere $pere"); |
|
| 45 | + if (isset($this->dtc->elements[$pere])) { |
|
| 46 | + $fils = $this->dtc->elements[$pere]; |
|
| 47 | + #spip_log("rejeton $name fils " . @join(',',$fils)); |
|
| 48 | + if (!($p = @in_array($name, $fils)) && ($p = strpos((string) $name, ':'))) { |
|
| 49 | + $p = substr((string) $name, $p + 1); |
|
| 50 | + $p = @in_array($p, $fils); |
|
| 51 | + } |
|
| 52 | + if (!$p) { |
|
| 53 | + $bons_peres = @implode('</b>, <b>', $this->dtc->peres[$name]); |
|
| 54 | + coordonnees_erreur($this, " <b>$name</b> " |
|
| 55 | + . _T('zxml_non_fils') |
|
| 56 | + . ' <b>' |
|
| 57 | + . $pere |
|
| 58 | + . '</b>' |
|
| 59 | + . ($bons_peres ? '<p style="font-size: 80%"> ' . _T('zxml_mais_de') . ' <b>' . $bons_peres . '</b></p>' : '')); |
|
| 60 | + } elseif ($this->dtc->regles[$pere][0] == '/') { |
|
| 61 | + $frat = substr((string) $depth, 2); |
|
| 62 | + if (!isset($this->fratrie[$frat])) { |
|
| 63 | + $this->fratrie[$frat] = ''; |
|
| 64 | + } |
|
| 65 | + $this->fratrie[$frat] .= "$name "; |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + // Init de la suite des balises a memoriser si regle difficile |
|
| 70 | + if ($this->dtc->regles[$name] && $this->dtc->regles[$name][0] == '/') { |
|
| 71 | + $this->fratrie[$depth] = ''; |
|
| 72 | + } |
|
| 73 | + if (isset($this->dtc->attributs[$name])) { |
|
| 74 | + foreach ($this->dtc->attributs[$name] as $n => $v) { |
|
| 75 | + if ($v[1] == '#REQUIRED' && !isset($attrs[$n])) { |
|
| 76 | + coordonnees_erreur($this, " <b>$n</b>" |
|
| 77 | + . ' : ' |
|
| 78 | + . _T('zxml_obligatoire_attribut') |
|
| 79 | + . " <b>$name</b>"); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + public function validerAttribut($phraseur, $name, $val, $bal) { |
|
| 86 | + // Si la balise est inconnue, eviter d'insister |
|
| 87 | + if (!isset($this->dtc->attributs[$bal])) { |
|
| 88 | + return; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $a = $this->dtc->attributs[$bal]; |
|
| 92 | + if (!isset($a[$name])) { |
|
| 93 | + $bons = implode(', ', array_keys($a)); |
|
| 94 | + if ($bons) { |
|
| 95 | + $bons = " title=' " . |
|
| 96 | + _T('zxml_connus_attributs') . |
|
| 97 | + ' : ' . |
|
| 98 | + $bons . |
|
| 99 | + "'"; |
|
| 100 | + } |
|
| 101 | + $bons .= " style='font-weight: bold'"; |
|
| 102 | + coordonnees_erreur($this, " <b>$name</b> " |
|
| 103 | + . _T('zxml_inconnu_attribut') . ' ' . _T('zxml_de') |
|
| 104 | + . " <a$bons>$bal</a> (" |
|
| 105 | + . _T('zxml_survoler') |
|
| 106 | + . ')'); |
|
| 107 | + } else { |
|
| 108 | + $type = $a[$name][0]; |
|
| 109 | + if (!preg_match('/^\w+$/', (string) $type)) { |
|
| 110 | + $this->valider_motif($phraseur, $name, $val, $bal, $type); |
|
| 111 | + } else { |
|
| 112 | + if (method_exists($this, $f = 'validerAttribut_' . $type)) { |
|
| 113 | + $this->$f($phraseur, $name, $val, $bal); |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + # else spip_log("$type type d'attribut inconnu"); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + public function validerAttribut_NMTOKEN($phraseur, $name, $val, $bal) { |
|
| 121 | + $this->valider_motif($phraseur, $name, $val, $bal, _REGEXP_NMTOKEN); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + public function validerAttribut_NMTOKENS($phraseur, $name, $val, $bal) { |
|
| 125 | + $this->valider_motif($phraseur, $name, $val, $bal, _REGEXP_NMTOKENS); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + public function validerAttribut_ID($phraseur, $name, $val, $bal) { |
|
| 129 | + if (isset($this->ids[$val])) { |
|
| 130 | + [$l, $c] = $this->ids[$val]; |
|
| 131 | + coordonnees_erreur($this, " <p><b>$val</b> " |
|
| 132 | + . _T('zxml_valeur_attribut') |
|
| 133 | + . " <b>$name</b> " |
|
| 134 | + . _T('zxml_de') |
|
| 135 | + . " <b>$bal</b> " |
|
| 136 | + . _T('zxml_vu') |
|
| 137 | + . " (L$l,C$c)"); |
|
| 138 | + } else { |
|
| 139 | + $this->valider_motif($phraseur, $name, $val, $bal, _REGEXP_ID); |
|
| 140 | + $this->ids[$val] = [xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur)]; |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + public function validerAttribut_IDREF($phraseur, $name, $val, $bal) { |
|
| 145 | + $this->idrefs[] = [$val, xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur)]; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + public function validerAttribut_IDREFS($phraseur, $name, $val, $bal) { |
|
| 149 | + $this->idrefss[] = [$val, xml_get_current_line_number($phraseur), xml_get_current_column_number($phraseur)]; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + public function valider_motif($phraseur, $name, $val, $bal, $motif) { |
|
| 153 | + if (!preg_match($motif, (string) $val)) { |
|
| 154 | + coordonnees_erreur($this, "<b>$val</b> " |
|
| 155 | + . _T('zxml_valeur_attribut') |
|
| 156 | + . " <b>$name</b> " |
|
| 157 | + . _T('zxml_de') |
|
| 158 | + . " <b>$bal</b> " |
|
| 159 | + . _T('zxml_non_conforme') |
|
| 160 | + . '</p><p>' |
|
| 161 | + . '<b>' . $motif . '</b>'); |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + public function valider_idref($nom, $ligne, $col) { |
|
| 166 | + if (!isset($this->ids[$nom])) { |
|
| 167 | + $this->err[] = [" <p><b>$nom</b> " . _T('zxml_inconnu_id'), $ligne, $col]; |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + public function valider_passe2() { |
|
| 172 | + if (!$this->err) { |
|
| 173 | + foreach ($this->idrefs as $idref) { |
|
| 174 | + [$nom, $ligne, $col] = $idref; |
|
| 175 | + $this->valider_idref($nom, $ligne, $col); |
|
| 176 | + } |
|
| 177 | + foreach ($this->idrefss as $idref) { |
|
| 178 | + [$noms, $ligne, $col] = $idref; |
|
| 179 | + foreach (preg_split('/\s+/', (string) $noms) as $nom) { |
|
| 180 | + $this->valider_idref($nom, $ligne, $col); |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + public function debutElement($phraseur, $name, $attrs) { |
|
| 187 | + if ($this->dtc->elements) { |
|
| 188 | + $this->validerElement($phraseur, $name, $attrs); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + if ($f = $this->process['debut']) { |
|
| 192 | + $f($this, $name, $attrs); |
|
| 193 | + } |
|
| 194 | + $depth = $this->depth; |
|
| 195 | + $this->debuts[$depth] = strlen((string) $this->res); |
|
| 196 | + foreach ($attrs as $k => $v) { |
|
| 197 | + $this->validerAttribut($phraseur, $k, $v, $name); |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + public function finElement($phraseur, $name) { |
|
| 202 | + $depth = $this->depth; |
|
| 203 | + $contenu = $this->contenu; |
|
| 204 | + |
|
| 205 | + $n = strlen((string) $this->res); |
|
| 206 | + $c = strlen(trim((string) $contenu[$depth])); |
|
| 207 | + $k = $this->debuts[$depth]; |
|
| 208 | + |
|
| 209 | + $regle = $this->dtc->regles[$name] ?? false; |
|
| 210 | + $vide = ($regle == 'EMPTY'); |
|
| 211 | + // controler que les balises devant etre vides le sont |
|
| 212 | + if ($vide) { |
|
| 213 | + if ($n != $k + $c) { |
|
| 214 | + coordonnees_erreur($this, " <p><b>$name</b> " . _T('zxml_nonvide_balise')); |
|
| 215 | + } |
|
| 216 | + // pour les regles PCDATA ou iteration de disjonction, tout est fait |
|
| 217 | + } elseif ($regle && $regle != '*') { |
|
| 218 | + if ($regle == '+') { |
|
| 219 | + // iteration de disjonction non vide: 1 balise au - |
|
| 220 | + if ($n == $k) { |
|
| 221 | + coordonnees_erreur($this, "<p>\n<b>$name</b> " |
|
| 222 | + . _T('zxml_vide_balise')); |
|
| 223 | + } |
|
| 224 | + } else { |
|
| 225 | + $f = $this->fratrie[substr((string) $depth, 2)] ?? null; |
|
| 226 | + if (is_null($f) || !preg_match($regle, (string) $f)) { |
|
| 227 | + coordonnees_erreur( |
|
| 228 | + $this, |
|
| 229 | + " <p>\n<b>$name</b> " |
|
| 230 | + . _T('zxml_succession_fils_incorrecte') |
|
| 231 | + . ' : <b>' |
|
| 232 | + . $f |
|
| 233 | + . '</b>' |
|
| 234 | + ); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + if ($f = $this->process['fin']) { |
|
| 239 | + $f($this, $name, $vide); |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + public function textElement($phraseur, $data) { |
|
| 244 | + if (trim((string) $data)) { |
|
| 245 | + $d = $this->depth; |
|
| 246 | + $d = $this->ouvrant[$d]; |
|
| 247 | + preg_match('/^\s*(\S+)/', (string) $d, $m); |
|
| 248 | + if (isset($this->dtc->pcdata[$m[1]]) && $this->dtc->pcdata[$m[1]]) { |
|
| 249 | + coordonnees_erreur($this, ' <p><b>' . $m[1] . '</b> ' |
|
| 250 | + . _T('zxml_nonvide_balise')); // message a affiner |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + if ($f = $this->process['text']) { |
|
| 254 | + $f($this, $data); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + public function piElement($phraseur, $target, $data) { |
|
| 259 | + if ($f = $this->process['pi']) { |
|
| 260 | + $f($this, $target, $data); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + // Denonciation des entitees XML inconnues |
|
| 265 | + // Pour contourner le bug de conception de SAX qui ne signale pas si elles |
|
| 266 | + // sont dans un attribut, les entites les plus frequentes ont ete |
|
| 267 | + // transcodees au prealable (sauf & < > " que SAX traite correctement). |
|
| 268 | + // On ne les verra donc pas passer a cette etape, contrairement a ce que |
|
| 269 | + // le source de la page laisse legitimement supposer. |
|
| 270 | + |
|
| 271 | + public function defaultElement($phraseur, $data) { |
|
| 272 | + if ( |
|
| 273 | + !preg_match('/^<!--/', (string) $data) |
|
| 274 | + && preg_match_all('/&([^;]*)?/', (string) $data, $r, PREG_SET_ORDER) |
|
| 275 | + ) { |
|
| 276 | + foreach ($r as $m) { |
|
| 277 | + [$t, $e] = $m; |
|
| 278 | + if (!isset($this->dtc->entites[$e])) { |
|
| 279 | + coordonnees_erreur($this, " <b>$e</b> " |
|
| 280 | + . _T('zxml_inconnu_entite') |
|
| 281 | + . ' '); |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + if (isset($this->process['default']) && ($f = $this->process['default'])) { |
|
| 286 | + $f($this, $data); |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + public function phraserTout($phraseur, $data) { |
|
| 291 | + xml_parsestring($this, $data); |
|
| 292 | + |
|
| 293 | + if (!$this->dtc || preg_match(',^' . _MESSAGE_DOCTYPE . ',', (string) $data)) { |
|
| 294 | + $this->err[] = ['DOCTYPE ?', 0, 0]; |
|
| 295 | + } else { |
|
| 296 | + $this->valider_passe2(); |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Constructeur |
|
| 302 | + * |
|
| 303 | + * @param array $process ? |
|
| 304 | + **/ |
|
| 305 | + public function __construct($process = []) { |
|
| 306 | + if (is_array($process)) { |
|
| 307 | + $this->process = $process; |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + public $ids = []; |
|
| 312 | + public $idrefs = []; |
|
| 313 | + public $idrefss = []; |
|
| 314 | + public $debuts = []; |
|
| 315 | + public $fratrie = []; |
|
| 316 | + |
|
| 317 | + public $dtc = null; |
|
| 318 | + public $sax = null; |
|
| 319 | + public $depth = ''; |
|
| 320 | + public $entete = ''; |
|
| 321 | + public $page = ''; |
|
| 322 | + public $res = ''; |
|
| 323 | + public array $err = []; |
|
| 324 | + public array $contenu = []; |
|
| 325 | + public array $versions = []; |
|
| 326 | + |
|
| 327 | + public array $ouvrant = []; |
|
| 328 | + public array $reperes = []; |
|
| 329 | + public array $process = [ |
|
| 330 | + 'debut' => 'xml_debutElement', |
|
| 331 | + 'fin' => 'xml_finElement', |
|
| 332 | + 'text' => 'xml_textElement', |
|
| 333 | + 'pi' => 'xml_piElement', |
|
| 334 | + 'default' => 'xml_defaultElement' |
|
| 335 | + ]; |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | |
@@ -342,8 +342,8 @@ discard block |
||
| 342 | 342 | * |
| 343 | 343 | **/ |
| 344 | 344 | function xml_valider_dist($page, $apply = false, $process = false, $doctype = '', $charset = null) { |
| 345 | - $f = new ValidateurXML($process); |
|
| 346 | - $sax = charger_fonction('sax', 'xml'); |
|
| 345 | + $f = new ValidateurXML($process); |
|
| 346 | + $sax = charger_fonction('sax', 'xml'); |
|
| 347 | 347 | |
| 348 | - return $sax($page, $apply, $f, $doctype, $charset); |
|
| 348 | + return $sax($page, $apply, $f, $doctype, $charset); |
|
| 349 | 349 | } |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | . ' <b>' |
| 57 | 57 | . $pere |
| 58 | 58 | . '</b>' |
| 59 | - . ($bons_peres ? '<p style="font-size: 80%"> ' . _T('zxml_mais_de') . ' <b>' . $bons_peres . '</b></p>' : '')); |
|
| 59 | + . ($bons_peres ? '<p style="font-size: 80%"> '._T('zxml_mais_de').' <b>'.$bons_peres.'</b></p>' : '')); |
|
| 60 | 60 | } elseif ($this->dtc->regles[$pere][0] == '/') { |
| 61 | 61 | $frat = substr((string) $depth, 2); |
| 62 | 62 | if (!isset($this->fratrie[$frat])) { |
@@ -92,15 +92,15 @@ discard block |
||
| 92 | 92 | if (!isset($a[$name])) { |
| 93 | 93 | $bons = implode(', ', array_keys($a)); |
| 94 | 94 | if ($bons) { |
| 95 | - $bons = " title=' " . |
|
| 96 | - _T('zxml_connus_attributs') . |
|
| 97 | - ' : ' . |
|
| 98 | - $bons . |
|
| 95 | + $bons = " title=' ". |
|
| 96 | + _T('zxml_connus_attributs'). |
|
| 97 | + ' : '. |
|
| 98 | + $bons. |
|
| 99 | 99 | "'"; |
| 100 | 100 | } |
| 101 | 101 | $bons .= " style='font-weight: bold'"; |
| 102 | 102 | coordonnees_erreur($this, " <b>$name</b> " |
| 103 | - . _T('zxml_inconnu_attribut') . ' ' . _T('zxml_de') |
|
| 103 | + . _T('zxml_inconnu_attribut').' '._T('zxml_de') |
|
| 104 | 104 | . " <a$bons>$bal</a> (" |
| 105 | 105 | . _T('zxml_survoler') |
| 106 | 106 | . ')'); |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | if (!preg_match('/^\w+$/', (string) $type)) { |
| 110 | 110 | $this->valider_motif($phraseur, $name, $val, $bal, $type); |
| 111 | 111 | } else { |
| 112 | - if (method_exists($this, $f = 'validerAttribut_' . $type)) { |
|
| 112 | + if (method_exists($this, $f = 'validerAttribut_'.$type)) { |
|
| 113 | 113 | $this->$f($phraseur, $name, $val, $bal); |
| 114 | 114 | } |
| 115 | 115 | } |
@@ -158,13 +158,13 @@ discard block |
||
| 158 | 158 | . " <b>$bal</b> " |
| 159 | 159 | . _T('zxml_non_conforme') |
| 160 | 160 | . '</p><p>' |
| 161 | - . '<b>' . $motif . '</b>'); |
|
| 161 | + . '<b>'.$motif.'</b>'); |
|
| 162 | 162 | } |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | public function valider_idref($nom, $ligne, $col) { |
| 166 | 166 | if (!isset($this->ids[$nom])) { |
| 167 | - $this->err[] = [" <p><b>$nom</b> " . _T('zxml_inconnu_id'), $ligne, $col]; |
|
| 167 | + $this->err[] = [" <p><b>$nom</b> "._T('zxml_inconnu_id'), $ligne, $col]; |
|
| 168 | 168 | } |
| 169 | 169 | } |
| 170 | 170 | |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | // controler que les balises devant etre vides le sont |
| 212 | 212 | if ($vide) { |
| 213 | 213 | if ($n != $k + $c) { |
| 214 | - coordonnees_erreur($this, " <p><b>$name</b> " . _T('zxml_nonvide_balise')); |
|
| 214 | + coordonnees_erreur($this, " <p><b>$name</b> "._T('zxml_nonvide_balise')); |
|
| 215 | 215 | } |
| 216 | 216 | // pour les regles PCDATA ou iteration de disjonction, tout est fait |
| 217 | 217 | } elseif ($regle && $regle != '*') { |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | $d = $this->ouvrant[$d]; |
| 247 | 247 | preg_match('/^\s*(\S+)/', (string) $d, $m); |
| 248 | 248 | if (isset($this->dtc->pcdata[$m[1]]) && $this->dtc->pcdata[$m[1]]) { |
| 249 | - coordonnees_erreur($this, ' <p><b>' . $m[1] . '</b> ' |
|
| 249 | + coordonnees_erreur($this, ' <p><b>'.$m[1].'</b> ' |
|
| 250 | 250 | . _T('zxml_nonvide_balise')); // message a affiner |
| 251 | 251 | } |
| 252 | 252 | } |
@@ -290,7 +290,7 @@ discard block |
||
| 290 | 290 | public function phraserTout($phraseur, $data) { |
| 291 | 291 | xml_parsestring($this, $data); |
| 292 | 292 | |
| 293 | - if (!$this->dtc || preg_match(',^' . _MESSAGE_DOCTYPE . ',', (string) $data)) { |
|
| 293 | + if (!$this->dtc || preg_match(',^'._MESSAGE_DOCTYPE.',', (string) $data)) { |
|
| 294 | 294 | $this->err[] = ['DOCTYPE ?', 0, 0]; |
| 295 | 295 | } else { |
| 296 | 296 | $this->valider_passe2(); |
@@ -10,58 +10,58 @@ discard block |
||
| 10 | 10 | \***************************************************************************/ |
| 11 | 11 | |
| 12 | 12 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 13 | - return; |
|
| 13 | + return; |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | include_spip('xml/interfaces'); |
| 17 | 17 | |
| 18 | 18 | function charger_dtd($grammaire, $avail, $rotlvl) { |
| 19 | - $r = null; |
|
| 20 | - static $dtd = []; # cache bien utile pour le validateur en boucle |
|
| 21 | - |
|
| 22 | - if (isset($dtd[$grammaire])) { |
|
| 23 | - return $dtd[$grammaire]; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - if ($avail == 'SYSTEM') { |
|
| 27 | - $grammaire = find_in_path($grammaire); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - $file = _DIR_CACHE_XML . preg_replace('/[^\w.]/', '_', (string) $rotlvl) . '.gz'; |
|
| 31 | - |
|
| 32 | - if (lire_fichier($file, $r)) { |
|
| 33 | - if (!$grammaire) { |
|
| 34 | - return []; |
|
| 35 | - } |
|
| 36 | - if ($avail == 'SYSTEM' && filemtime($file) < filemtime($grammaire)) { |
|
| 37 | - $r = false; |
|
| 38 | - } |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - if ($r) { |
|
| 42 | - $dtc = unserialize($r); |
|
| 43 | - } else { |
|
| 44 | - spip_timer('dtd'); |
|
| 45 | - $dtc = new DTC(); |
|
| 46 | - // L'analyseur retourne un booleen de reussite et modifie $dtc. |
|
| 47 | - // Retourner vide en cas d'echec |
|
| 48 | - if (!analyser_dtd($grammaire, $avail, $dtc)) { |
|
| 49 | - $dtc = []; |
|
| 50 | - } else { |
|
| 51 | - // tri final pour presenter les suggestions de corrections |
|
| 52 | - foreach ($dtc->peres as $k => $v) { |
|
| 53 | - asort($v); |
|
| 54 | - $dtc->peres[$k] = $v; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - spip_log("Analyser DTD $avail $grammaire (" . spip_timer('dtd') . ') ' . (is_countable($dtc->macros) ? count($dtc->macros) : 0) . ' macros, ' . (is_countable($dtc->elements) ? count($dtc->elements) : 0) . ' elements, ' . (is_countable($dtc->attributs) ? count($dtc->attributs) : 0) . " listes d'attributs, " . (is_countable($dtc->entites) ? count($dtc->entites) : 0) . ' entites'); |
|
| 58 | - # $r = $dtc->regles; ksort($r);foreach($r as $l => $v) {$t=array_keys($dtc->attributs[$l]);echo "<b>$l</b> '$v' ", count($t), " attributs: ", join (', ',$t);$t=$dtc->peres[$l];echo "<br />",count($t), " peres: ", @join (', ',$t), "<br />\n";}exit; |
|
| 59 | - ecrire_fichier($file, serialize($dtc), true); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - $dtd[$grammaire] = $dtc; |
|
| 63 | - |
|
| 64 | - return $dtc; |
|
| 19 | + $r = null; |
|
| 20 | + static $dtd = []; # cache bien utile pour le validateur en boucle |
|
| 21 | + |
|
| 22 | + if (isset($dtd[$grammaire])) { |
|
| 23 | + return $dtd[$grammaire]; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + if ($avail == 'SYSTEM') { |
|
| 27 | + $grammaire = find_in_path($grammaire); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + $file = _DIR_CACHE_XML . preg_replace('/[^\w.]/', '_', (string) $rotlvl) . '.gz'; |
|
| 31 | + |
|
| 32 | + if (lire_fichier($file, $r)) { |
|
| 33 | + if (!$grammaire) { |
|
| 34 | + return []; |
|
| 35 | + } |
|
| 36 | + if ($avail == 'SYSTEM' && filemtime($file) < filemtime($grammaire)) { |
|
| 37 | + $r = false; |
|
| 38 | + } |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + if ($r) { |
|
| 42 | + $dtc = unserialize($r); |
|
| 43 | + } else { |
|
| 44 | + spip_timer('dtd'); |
|
| 45 | + $dtc = new DTC(); |
|
| 46 | + // L'analyseur retourne un booleen de reussite et modifie $dtc. |
|
| 47 | + // Retourner vide en cas d'echec |
|
| 48 | + if (!analyser_dtd($grammaire, $avail, $dtc)) { |
|
| 49 | + $dtc = []; |
|
| 50 | + } else { |
|
| 51 | + // tri final pour presenter les suggestions de corrections |
|
| 52 | + foreach ($dtc->peres as $k => $v) { |
|
| 53 | + asort($v); |
|
| 54 | + $dtc->peres[$k] = $v; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + spip_log("Analyser DTD $avail $grammaire (" . spip_timer('dtd') . ') ' . (is_countable($dtc->macros) ? count($dtc->macros) : 0) . ' macros, ' . (is_countable($dtc->elements) ? count($dtc->elements) : 0) . ' elements, ' . (is_countable($dtc->attributs) ? count($dtc->attributs) : 0) . " listes d'attributs, " . (is_countable($dtc->entites) ? count($dtc->entites) : 0) . ' entites'); |
|
| 58 | + # $r = $dtc->regles; ksort($r);foreach($r as $l => $v) {$t=array_keys($dtc->attributs[$l]);echo "<b>$l</b> '$v' ", count($t), " attributs: ", join (', ',$t);$t=$dtc->peres[$l];echo "<br />",count($t), " peres: ", @join (', ',$t), "<br />\n";}exit; |
|
| 59 | + ecrire_fichier($file, serialize($dtc), true); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + $dtd[$grammaire] = $dtc; |
|
| 63 | + |
|
| 64 | + return $dtc; |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | // Compiler une regle de production en une Regexp qu'on appliquera sur la |
@@ -73,137 +73,137 @@ discard block |
||
| 73 | 73 | // et parentheser le tout pour que | + * ? s'applique dessus. |
| 74 | 74 | |
| 75 | 75 | function compilerRegle($val) { |
| 76 | - return str_replace( |
|
| 77 | - '()', |
|
| 78 | - '', |
|
| 79 | - preg_replace( |
|
| 80 | - '/\s*,\s*/', |
|
| 81 | - '', |
|
| 82 | - preg_replace( |
|
| 83 | - '/(\w+)\s*/', |
|
| 84 | - '(?:\1 )', |
|
| 85 | - preg_replace( |
|
| 86 | - '/\s*\)/', |
|
| 87 | - ')', |
|
| 88 | - preg_replace( |
|
| 89 | - '/\s*([(+*|?])\s*/', |
|
| 90 | - '\1', |
|
| 91 | - preg_replace('/\s*#\w+\s*[,|]?\s*/', '', (string) $val) |
|
| 92 | - ) |
|
| 93 | - ) |
|
| 94 | - ) |
|
| 95 | - ) |
|
| 96 | - ); |
|
| 76 | + return str_replace( |
|
| 77 | + '()', |
|
| 78 | + '', |
|
| 79 | + preg_replace( |
|
| 80 | + '/\s*,\s*/', |
|
| 81 | + '', |
|
| 82 | + preg_replace( |
|
| 83 | + '/(\w+)\s*/', |
|
| 84 | + '(?:\1 )', |
|
| 85 | + preg_replace( |
|
| 86 | + '/\s*\)/', |
|
| 87 | + ')', |
|
| 88 | + preg_replace( |
|
| 89 | + '/\s*([(+*|?])\s*/', |
|
| 90 | + '\1', |
|
| 91 | + preg_replace('/\s*#\w+\s*[,|]?\s*/', '', (string) $val) |
|
| 92 | + ) |
|
| 93 | + ) |
|
| 94 | + ) |
|
| 95 | + ) |
|
| 96 | + ); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | |
| 100 | 100 | function analyser_dtd($loc, $avail, &$dtc) { |
| 101 | - // creer le repertoire de cache si ce n'est fait |
|
| 102 | - // (utile aussi pour le resultat de la compil) |
|
| 103 | - $file = sous_repertoire(_DIR_CACHE_XML); |
|
| 104 | - // si DTD locale, ignorer ce repertoire pour le moment |
|
| 105 | - if ($avail == 'SYSTEM') { |
|
| 106 | - $file = $loc; |
|
| 107 | - if (_DIR_RACINE && str_starts_with((string) $file, (string) _DIR_RACINE)) { |
|
| 108 | - $file = substr((string) $file, strlen((string) _DIR_RACINE)); |
|
| 109 | - } |
|
| 110 | - $file = find_in_path($file); |
|
| 111 | - } else { |
|
| 112 | - $file .= preg_replace('/[^\w.]/', '_', (string) $loc); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $dtd = ''; |
|
| 116 | - if (@is_readable($file)) { |
|
| 117 | - lire_fichier($file, $dtd); |
|
| 118 | - } else { |
|
| 119 | - if ($avail == 'PUBLIC') { |
|
| 120 | - include_spip('inc/distant'); |
|
| 121 | - $dtd = recuperer_url($loc); |
|
| 122 | - $dtd = trim($dtd['page'] ?? ''); |
|
| 123 | - if ($dtd) { |
|
| 124 | - ecrire_fichier($file, $dtd, true); |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $dtd = ltrim($dtd); |
|
| 130 | - if (!$dtd) { |
|
| 131 | - spip_log("DTD '$loc' ($file) inaccessible"); |
|
| 132 | - |
|
| 133 | - return false; |
|
| 134 | - } else { |
|
| 135 | - spip_log("analyse de la DTD $loc "); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - while ($dtd) { |
|
| 139 | - if ($dtd[0] != '<') { |
|
| 140 | - $r = analyser_dtd_lexeme($dtd, $dtc, $loc); |
|
| 141 | - } elseif ($dtd[1] != '!') { |
|
| 142 | - $r = analyser_dtd_pi($dtd, $dtc, $loc); |
|
| 143 | - } elseif ($dtd[2] == '[') { |
|
| 144 | - $r = analyser_dtd_data($dtd, $dtc, $loc); |
|
| 145 | - } else { |
|
| 146 | - $r = match ($dtd[3]) { |
|
| 147 | - '%' => analyser_dtd_data($dtd, $dtc, $loc), |
|
| 148 | - 'T' => analyser_dtd_attlist($dtd, $dtc, $loc), |
|
| 149 | - 'L' => analyser_dtd_element($dtd, $dtc, $loc), |
|
| 150 | - 'N' => analyser_dtd_entity($dtd, $dtc, $loc), |
|
| 151 | - 'O' => analyser_dtd_notation($dtd, $dtc, $loc), |
|
| 152 | - '-' => analyser_dtd_comment($dtd, $dtc, $loc), |
|
| 153 | - default => -1, |
|
| 154 | - }; |
|
| 155 | - } |
|
| 156 | - if (!is_string($r)) { |
|
| 157 | - spip_log("erreur $r dans la DTD " . substr($dtd, 0, 80) . '.....'); |
|
| 158 | - |
|
| 159 | - return false; |
|
| 160 | - } |
|
| 161 | - $dtd = $r; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - return true; |
|
| 101 | + // creer le repertoire de cache si ce n'est fait |
|
| 102 | + // (utile aussi pour le resultat de la compil) |
|
| 103 | + $file = sous_repertoire(_DIR_CACHE_XML); |
|
| 104 | + // si DTD locale, ignorer ce repertoire pour le moment |
|
| 105 | + if ($avail == 'SYSTEM') { |
|
| 106 | + $file = $loc; |
|
| 107 | + if (_DIR_RACINE && str_starts_with((string) $file, (string) _DIR_RACINE)) { |
|
| 108 | + $file = substr((string) $file, strlen((string) _DIR_RACINE)); |
|
| 109 | + } |
|
| 110 | + $file = find_in_path($file); |
|
| 111 | + } else { |
|
| 112 | + $file .= preg_replace('/[^\w.]/', '_', (string) $loc); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $dtd = ''; |
|
| 116 | + if (@is_readable($file)) { |
|
| 117 | + lire_fichier($file, $dtd); |
|
| 118 | + } else { |
|
| 119 | + if ($avail == 'PUBLIC') { |
|
| 120 | + include_spip('inc/distant'); |
|
| 121 | + $dtd = recuperer_url($loc); |
|
| 122 | + $dtd = trim($dtd['page'] ?? ''); |
|
| 123 | + if ($dtd) { |
|
| 124 | + ecrire_fichier($file, $dtd, true); |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $dtd = ltrim($dtd); |
|
| 130 | + if (!$dtd) { |
|
| 131 | + spip_log("DTD '$loc' ($file) inaccessible"); |
|
| 132 | + |
|
| 133 | + return false; |
|
| 134 | + } else { |
|
| 135 | + spip_log("analyse de la DTD $loc "); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + while ($dtd) { |
|
| 139 | + if ($dtd[0] != '<') { |
|
| 140 | + $r = analyser_dtd_lexeme($dtd, $dtc, $loc); |
|
| 141 | + } elseif ($dtd[1] != '!') { |
|
| 142 | + $r = analyser_dtd_pi($dtd, $dtc, $loc); |
|
| 143 | + } elseif ($dtd[2] == '[') { |
|
| 144 | + $r = analyser_dtd_data($dtd, $dtc, $loc); |
|
| 145 | + } else { |
|
| 146 | + $r = match ($dtd[3]) { |
|
| 147 | + '%' => analyser_dtd_data($dtd, $dtc, $loc), |
|
| 148 | + 'T' => analyser_dtd_attlist($dtd, $dtc, $loc), |
|
| 149 | + 'L' => analyser_dtd_element($dtd, $dtc, $loc), |
|
| 150 | + 'N' => analyser_dtd_entity($dtd, $dtc, $loc), |
|
| 151 | + 'O' => analyser_dtd_notation($dtd, $dtc, $loc), |
|
| 152 | + '-' => analyser_dtd_comment($dtd, $dtc, $loc), |
|
| 153 | + default => -1, |
|
| 154 | + }; |
|
| 155 | + } |
|
| 156 | + if (!is_string($r)) { |
|
| 157 | + spip_log("erreur $r dans la DTD " . substr($dtd, 0, 80) . '.....'); |
|
| 158 | + |
|
| 159 | + return false; |
|
| 160 | + } |
|
| 161 | + $dtd = $r; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + return true; |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | function analyser_dtd_comment($dtd, &$dtc, $grammaire) { |
| 168 | - // ejecter les commentaires, surtout quand ils contiennent du code. |
|
| 169 | - // Option /s car sur plusieurs lignes parfois |
|
| 168 | + // ejecter les commentaires, surtout quand ils contiennent du code. |
|
| 169 | + // Option /s car sur plusieurs lignes parfois |
|
| 170 | 170 | |
| 171 | - if (!preg_match('/^<!--.*?-->\s*(.*)$/s', (string) $dtd, $m)) { |
|
| 172 | - return -6; |
|
| 173 | - } |
|
| 171 | + if (!preg_match('/^<!--.*?-->\s*(.*)$/s', (string) $dtd, $m)) { |
|
| 172 | + return -6; |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - return $m[1]; |
|
| 175 | + return $m[1]; |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | function analyser_dtd_pi($dtd, &$dtc, $grammaire) { |
| 179 | - if (!preg_match('/^<\?.*?>\s*(.*)$/s', (string) $dtd, $m)) { |
|
| 180 | - return -10; |
|
| 181 | - } |
|
| 179 | + if (!preg_match('/^<\?.*?>\s*(.*)$/s', (string) $dtd, $m)) { |
|
| 180 | + return -10; |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - return $m[1]; |
|
| 183 | + return $m[1]; |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | function analyser_dtd_lexeme($dtd, &$dtc, $grammaire) { |
| 187 | 187 | |
| 188 | - if (!preg_match(_REGEXP_ENTITY_DEF, (string) $dtd, $m)) { |
|
| 189 | - return -9; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - [, $s] = $m; |
|
| 193 | - $n = $dtc->macros[$s]; |
|
| 194 | - |
|
| 195 | - if (is_array($n)) { |
|
| 196 | - // en cas d'inclusion, l'espace de nom est le meme |
|
| 197 | - // mais gaffe aux DTD dont l'URL est relative a l'engloblante |
|
| 198 | - if ( |
|
| 199 | - $n[0] == 'PUBLIC' && !tester_url_absolue($n[1]) |
|
| 200 | - ) { |
|
| 201 | - $n[1] = substr((string) $grammaire, 0, strrpos((string) $grammaire, '/') + 1) . $n[1]; |
|
| 202 | - } |
|
| 203 | - analyser_dtd($n[1], $n[0], $dtc); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - return ltrim(substr((string) $dtd, strlen($m[0]))); |
|
| 188 | + if (!preg_match(_REGEXP_ENTITY_DEF, (string) $dtd, $m)) { |
|
| 189 | + return -9; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + [, $s] = $m; |
|
| 193 | + $n = $dtc->macros[$s]; |
|
| 194 | + |
|
| 195 | + if (is_array($n)) { |
|
| 196 | + // en cas d'inclusion, l'espace de nom est le meme |
|
| 197 | + // mais gaffe aux DTD dont l'URL est relative a l'engloblante |
|
| 198 | + if ( |
|
| 199 | + $n[0] == 'PUBLIC' && !tester_url_absolue($n[1]) |
|
| 200 | + ) { |
|
| 201 | + $n[1] = substr((string) $grammaire, 0, strrpos((string) $grammaire, '/') + 1) . $n[1]; |
|
| 202 | + } |
|
| 203 | + analyser_dtd($n[1], $n[0], $dtc); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + return ltrim(substr((string) $dtd, strlen($m[0]))); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | // il faudrait gerer plus proprement les niveaux d'inclusion: |
@@ -211,78 +211,78 @@ discard block |
||
| 211 | 211 | |
| 212 | 212 | function analyser_dtd_data($dtd, &$dtc, $grammaire) { |
| 213 | 213 | |
| 214 | - if (!preg_match(_REGEXP_INCLUDE_USE, (string) $dtd, $m)) { |
|
| 215 | - return -11; |
|
| 216 | - } |
|
| 217 | - if ( |
|
| 218 | - !preg_match( |
|
| 219 | - '/^((\s*<!(\[\s*%\s*[^;]*;\s*\[([^]<]*<[^>]*>)*[^]<]*\]\]>)|([^]>]*>))*[^]<]*)\]\]>\s*/s', |
|
| 220 | - $m[2], |
|
| 221 | - $r |
|
| 222 | - ) |
|
| 223 | - ) { |
|
| 224 | - return -12; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - return $dtc->macros[$m[1]] == 'INCLUDE' |
|
| 228 | - ? $r[1] . substr($m[2], strlen($r[0])) |
|
| 229 | - : substr($m[2], strlen($r[0])); |
|
| 214 | + if (!preg_match(_REGEXP_INCLUDE_USE, (string) $dtd, $m)) { |
|
| 215 | + return -11; |
|
| 216 | + } |
|
| 217 | + if ( |
|
| 218 | + !preg_match( |
|
| 219 | + '/^((\s*<!(\[\s*%\s*[^;]*;\s*\[([^]<]*<[^>]*>)*[^]<]*\]\]>)|([^]>]*>))*[^]<]*)\]\]>\s*/s', |
|
| 220 | + $m[2], |
|
| 221 | + $r |
|
| 222 | + ) |
|
| 223 | + ) { |
|
| 224 | + return -12; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + return $dtc->macros[$m[1]] == 'INCLUDE' |
|
| 228 | + ? $r[1] . substr($m[2], strlen($r[0])) |
|
| 229 | + : substr($m[2], strlen($r[0])); |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | function analyser_dtd_notation($dtd, &$dtc, $grammaire) { |
| 233 | - if (!preg_match('/^<!NOTATION.*?>\s*(.*)$/s', (string) $dtd, $m)) { |
|
| 234 | - return -8; |
|
| 235 | - } |
|
| 236 | - spip_log('analyser_dtd_notation a ecrire'); |
|
| 233 | + if (!preg_match('/^<!NOTATION.*?>\s*(.*)$/s', (string) $dtd, $m)) { |
|
| 234 | + return -8; |
|
| 235 | + } |
|
| 236 | + spip_log('analyser_dtd_notation a ecrire'); |
|
| 237 | 237 | |
| 238 | - return $m[1]; |
|
| 238 | + return $m[1]; |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | function analyser_dtd_entity($dtd, &$dtc, $grammaire) { |
| 242 | - if (!preg_match(_REGEXP_ENTITY_DECL, (string) $dtd, $m)) { |
|
| 243 | - return -2; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - [$t, $term, $nom, $type, $k1, $k2, $k3, $k4, $k5, $k6, $c, $q, $alt, $dtd] = $m; |
|
| 247 | - |
|
| 248 | - if (isset($dtc->macros[$nom]) && $dtc->macros[$nom]) { |
|
| 249 | - return $dtd; |
|
| 250 | - } |
|
| 251 | - if (isset($dtc->entites[$nom])) { |
|
| 252 | - spip_log("redefinition de l'entite $nom"); |
|
| 253 | - } |
|
| 254 | - if ($k6) { |
|
| 255 | - return $k6 . $dtd; |
|
| 256 | - } // cas du synonyme complet |
|
| 257 | - $val = expanserEntite(($k2 ? $k3 : ($k4 ? $k5 : $k6)), $dtc->macros); |
|
| 258 | - |
|
| 259 | - // cas particulier double evaluation: 'PUBLIC "..." "...."' |
|
| 260 | - if (preg_match('/(PUBLIC|SYSTEM)\s+"([^"]*)"\s*("([^"]*)")?\s*$/s', $val, $r)) { |
|
| 261 | - [$t, $type, $val, $q, $alt] = $r; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - if (!$term) { |
|
| 265 | - $dtc->entites[$nom] = $val; |
|
| 266 | - } elseif (!$type) { |
|
| 267 | - $dtc->macros[$nom] = $val; |
|
| 268 | - } else { |
|
| 269 | - if ($type == 'SYSTEM' && !$alt) { |
|
| 270 | - $alt = $val; |
|
| 271 | - } |
|
| 272 | - if (!$alt) { |
|
| 273 | - $dtc->macros[$nom] = $val; |
|
| 274 | - } else { |
|
| 275 | - if ( |
|
| 276 | - $type == 'PUBLIC' && !str_contains($alt, '/') |
|
| 277 | - ) { |
|
| 278 | - $alt = preg_replace(',/[^/]+$,', '/', (string) $grammaire) |
|
| 279 | - . $alt; |
|
| 280 | - } |
|
| 281 | - $dtc->macros[$nom] = [$type, $alt]; |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - return $dtd; |
|
| 242 | + if (!preg_match(_REGEXP_ENTITY_DECL, (string) $dtd, $m)) { |
|
| 243 | + return -2; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + [$t, $term, $nom, $type, $k1, $k2, $k3, $k4, $k5, $k6, $c, $q, $alt, $dtd] = $m; |
|
| 247 | + |
|
| 248 | + if (isset($dtc->macros[$nom]) && $dtc->macros[$nom]) { |
|
| 249 | + return $dtd; |
|
| 250 | + } |
|
| 251 | + if (isset($dtc->entites[$nom])) { |
|
| 252 | + spip_log("redefinition de l'entite $nom"); |
|
| 253 | + } |
|
| 254 | + if ($k6) { |
|
| 255 | + return $k6 . $dtd; |
|
| 256 | + } // cas du synonyme complet |
|
| 257 | + $val = expanserEntite(($k2 ? $k3 : ($k4 ? $k5 : $k6)), $dtc->macros); |
|
| 258 | + |
|
| 259 | + // cas particulier double evaluation: 'PUBLIC "..." "...."' |
|
| 260 | + if (preg_match('/(PUBLIC|SYSTEM)\s+"([^"]*)"\s*("([^"]*)")?\s*$/s', $val, $r)) { |
|
| 261 | + [$t, $type, $val, $q, $alt] = $r; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + if (!$term) { |
|
| 265 | + $dtc->entites[$nom] = $val; |
|
| 266 | + } elseif (!$type) { |
|
| 267 | + $dtc->macros[$nom] = $val; |
|
| 268 | + } else { |
|
| 269 | + if ($type == 'SYSTEM' && !$alt) { |
|
| 270 | + $alt = $val; |
|
| 271 | + } |
|
| 272 | + if (!$alt) { |
|
| 273 | + $dtc->macros[$nom] = $val; |
|
| 274 | + } else { |
|
| 275 | + if ( |
|
| 276 | + $type == 'PUBLIC' && !str_contains($alt, '/') |
|
| 277 | + ) { |
|
| 278 | + $alt = preg_replace(',/[^/]+$,', '/', (string) $grammaire) |
|
| 279 | + . $alt; |
|
| 280 | + } |
|
| 281 | + $dtc->macros[$nom] = [$type, $alt]; |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + return $dtd; |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | // Dresser le tableau des filles potentielles de l'element |
@@ -295,71 +295,71 @@ discard block |
||
| 295 | 295 | // Fin du controle en finElement |
| 296 | 296 | |
| 297 | 297 | function analyser_dtd_element($dtd, &$dtc, $grammaire) { |
| 298 | - if (!preg_match('/^<!ELEMENT\s+([^>\s]+)([^>]*)>\s*(.*)$/s', (string) $dtd, $m)) { |
|
| 299 | - return -3; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - [, $nom, $contenu, $dtd] = $m; |
|
| 303 | - $nom = expanserEntite($nom, $dtc->macros); |
|
| 304 | - |
|
| 305 | - if (isset($dtc->elements[$nom])) { |
|
| 306 | - spip_log("redefinition de l'element $nom dans la DTD"); |
|
| 307 | - |
|
| 308 | - return -4; |
|
| 309 | - } |
|
| 310 | - $filles = []; |
|
| 311 | - $contenu = expanserEntite($contenu, $dtc->macros); |
|
| 312 | - $val = $contenu ? compilerRegle($contenu) : '(?:EMPTY )'; |
|
| 313 | - if ($val == '(?:EMPTY )') { |
|
| 314 | - $dtc->regles[$nom] = 'EMPTY'; |
|
| 315 | - } elseif ($val == '(?:ANY )') { |
|
| 316 | - $dtc->regles[$nom] = 'ANY'; |
|
| 317 | - } else { |
|
| 318 | - $last = substr((string) $val, -1); |
|
| 319 | - $dtc->regles[$nom] = preg_match('/ \w/', (string) $val) || (!empty($last) && !str_contains('*+?', $last)) |
|
| 320 | - ? "/^$val$/" |
|
| 321 | - : $last; |
|
| 322 | - $filles = array_values(preg_split('/\W+/', (string) $val, -1, PREG_SPLIT_NO_EMPTY)); |
|
| 323 | - |
|
| 324 | - foreach ($filles as $k) { |
|
| 325 | - if (!isset($dtc->peres[$k])) { |
|
| 326 | - $dtc->peres[$k] = []; |
|
| 327 | - } |
|
| 328 | - if (!in_array($nom, $dtc->peres[$k])) { |
|
| 329 | - $dtc->peres[$k][] = $nom; |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - $dtc->pcdata[$nom] = (!str_contains($contenu, '#PCDATA')); |
|
| 334 | - $dtc->elements[$nom] = $filles; |
|
| 335 | - |
|
| 336 | - return $dtd; |
|
| 298 | + if (!preg_match('/^<!ELEMENT\s+([^>\s]+)([^>]*)>\s*(.*)$/s', (string) $dtd, $m)) { |
|
| 299 | + return -3; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + [, $nom, $contenu, $dtd] = $m; |
|
| 303 | + $nom = expanserEntite($nom, $dtc->macros); |
|
| 304 | + |
|
| 305 | + if (isset($dtc->elements[$nom])) { |
|
| 306 | + spip_log("redefinition de l'element $nom dans la DTD"); |
|
| 307 | + |
|
| 308 | + return -4; |
|
| 309 | + } |
|
| 310 | + $filles = []; |
|
| 311 | + $contenu = expanserEntite($contenu, $dtc->macros); |
|
| 312 | + $val = $contenu ? compilerRegle($contenu) : '(?:EMPTY )'; |
|
| 313 | + if ($val == '(?:EMPTY )') { |
|
| 314 | + $dtc->regles[$nom] = 'EMPTY'; |
|
| 315 | + } elseif ($val == '(?:ANY )') { |
|
| 316 | + $dtc->regles[$nom] = 'ANY'; |
|
| 317 | + } else { |
|
| 318 | + $last = substr((string) $val, -1); |
|
| 319 | + $dtc->regles[$nom] = preg_match('/ \w/', (string) $val) || (!empty($last) && !str_contains('*+?', $last)) |
|
| 320 | + ? "/^$val$/" |
|
| 321 | + : $last; |
|
| 322 | + $filles = array_values(preg_split('/\W+/', (string) $val, -1, PREG_SPLIT_NO_EMPTY)); |
|
| 323 | + |
|
| 324 | + foreach ($filles as $k) { |
|
| 325 | + if (!isset($dtc->peres[$k])) { |
|
| 326 | + $dtc->peres[$k] = []; |
|
| 327 | + } |
|
| 328 | + if (!in_array($nom, $dtc->peres[$k])) { |
|
| 329 | + $dtc->peres[$k][] = $nom; |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + $dtc->pcdata[$nom] = (!str_contains($contenu, '#PCDATA')); |
|
| 334 | + $dtc->elements[$nom] = $filles; |
|
| 335 | + |
|
| 336 | + return $dtd; |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | |
| 340 | 340 | function analyser_dtd_attlist($dtd, &$dtc, $grammaire) { |
| 341 | - if (!preg_match('/^<!ATTLIST\s+(\S+)\s+([^>]*)>\s*(.*)/s', (string) $dtd, $m)) { |
|
| 342 | - return -5; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - [, $nom, $val, $dtd] = $m; |
|
| 346 | - $nom = expanserEntite($nom, $dtc->macros); |
|
| 347 | - $val = expanserEntite($val, $dtc->macros); |
|
| 348 | - if (!isset($dtc->attributs[$nom])) { |
|
| 349 | - $dtc->attributs[$nom] = []; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - if (preg_match_all("/\s*(\S+)\s+(([(][^)]*[)])|(\S+))\s+([^\s']*)(\s*'[^']*')?/", $val, $r2, PREG_SET_ORDER)) { |
|
| 353 | - foreach ($r2 as $m2) { |
|
| 354 | - $v = preg_match('/^\w+$/', $m2[2]) ? $m2[2] |
|
| 355 | - : ('/^' . preg_replace('/\s+/', '', $m2[2]) . '$/'); |
|
| 356 | - $m21 = expanserEntite($m2[1], $dtc->macros); |
|
| 357 | - $m25 = expanserEntite($m2[5], $dtc->macros); |
|
| 358 | - $dtc->attributs[$nom][$m21] = [$v, $m25]; |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - return $dtd; |
|
| 341 | + if (!preg_match('/^<!ATTLIST\s+(\S+)\s+([^>]*)>\s*(.*)/s', (string) $dtd, $m)) { |
|
| 342 | + return -5; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + [, $nom, $val, $dtd] = $m; |
|
| 346 | + $nom = expanserEntite($nom, $dtc->macros); |
|
| 347 | + $val = expanserEntite($val, $dtc->macros); |
|
| 348 | + if (!isset($dtc->attributs[$nom])) { |
|
| 349 | + $dtc->attributs[$nom] = []; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + if (preg_match_all("/\s*(\S+)\s+(([(][^)]*[)])|(\S+))\s+([^\s']*)(\s*'[^']*')?/", $val, $r2, PREG_SET_ORDER)) { |
|
| 353 | + foreach ($r2 as $m2) { |
|
| 354 | + $v = preg_match('/^\w+$/', $m2[2]) ? $m2[2] |
|
| 355 | + : ('/^' . preg_replace('/\s+/', '', $m2[2]) . '$/'); |
|
| 356 | + $m21 = expanserEntite($m2[1], $dtc->macros); |
|
| 357 | + $m25 = expanserEntite($m2[5], $dtc->macros); |
|
| 358 | + $dtc->attributs[$nom][$m21] = [$v, $m25]; |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + return $dtd; |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | |
@@ -375,26 +375,26 @@ discard block |
||
| 375 | 375 | * @return string|array |
| 376 | 376 | **/ |
| 377 | 377 | function expanserEntite($val, $macros = []) { |
| 378 | - static $vu = []; |
|
| 379 | - if (!is_string($val)) { |
|
| 380 | - return $vu; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - if (preg_match_all(_REGEXP_ENTITY_USE, $val, $r, PREG_SET_ORDER)) { |
|
| 384 | - foreach ($r as $m) { |
|
| 385 | - $ent = $m[1]; |
|
| 386 | - // il peut valoir "" |
|
| 387 | - if (!isset($macros[$ent])) { |
|
| 388 | - spip_log("Entite $ent inconnu"); |
|
| 389 | - } else { |
|
| 390 | - if (!isset($vu[$ent])) { |
|
| 391 | - $vu[$ent] = 0; |
|
| 392 | - } |
|
| 393 | - ++$vu[$ent]; |
|
| 394 | - $val = str_replace($m[0], $macros[$ent], $val); |
|
| 395 | - } |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - return trim(preg_replace('/\s+/', ' ', $val)); |
|
| 378 | + static $vu = []; |
|
| 379 | + if (!is_string($val)) { |
|
| 380 | + return $vu; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + if (preg_match_all(_REGEXP_ENTITY_USE, $val, $r, PREG_SET_ORDER)) { |
|
| 384 | + foreach ($r as $m) { |
|
| 385 | + $ent = $m[1]; |
|
| 386 | + // il peut valoir "" |
|
| 387 | + if (!isset($macros[$ent])) { |
|
| 388 | + spip_log("Entite $ent inconnu"); |
|
| 389 | + } else { |
|
| 390 | + if (!isset($vu[$ent])) { |
|
| 391 | + $vu[$ent] = 0; |
|
| 392 | + } |
|
| 393 | + ++$vu[$ent]; |
|
| 394 | + $val = str_replace($m[0], $macros[$ent], $val); |
|
| 395 | + } |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + return trim(preg_replace('/\s+/', ' ', $val)); |
|
| 400 | 400 | } |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | $grammaire = find_in_path($grammaire); |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - $file = _DIR_CACHE_XML . preg_replace('/[^\w.]/', '_', (string) $rotlvl) . '.gz'; |
|
| 30 | + $file = _DIR_CACHE_XML.preg_replace('/[^\w.]/', '_', (string) $rotlvl).'.gz'; |
|
| 31 | 31 | |
| 32 | 32 | if (lire_fichier($file, $r)) { |
| 33 | 33 | if (!$grammaire) { |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | $dtc->peres[$k] = $v; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - spip_log("Analyser DTD $avail $grammaire (" . spip_timer('dtd') . ') ' . (is_countable($dtc->macros) ? count($dtc->macros) : 0) . ' macros, ' . (is_countable($dtc->elements) ? count($dtc->elements) : 0) . ' elements, ' . (is_countable($dtc->attributs) ? count($dtc->attributs) : 0) . " listes d'attributs, " . (is_countable($dtc->entites) ? count($dtc->entites) : 0) . ' entites'); |
|
| 57 | + spip_log("Analyser DTD $avail $grammaire (".spip_timer('dtd').') '.(is_countable($dtc->macros) ? count($dtc->macros) : 0).' macros, '.(is_countable($dtc->elements) ? count($dtc->elements) : 0).' elements, '.(is_countable($dtc->attributs) ? count($dtc->attributs) : 0)." listes d'attributs, ".(is_countable($dtc->entites) ? count($dtc->entites) : 0).' entites'); |
|
| 58 | 58 | # $r = $dtc->regles; ksort($r);foreach($r as $l => $v) {$t=array_keys($dtc->attributs[$l]);echo "<b>$l</b> '$v' ", count($t), " attributs: ", join (', ',$t);$t=$dtc->peres[$l];echo "<br />",count($t), " peres: ", @join (', ',$t), "<br />\n";}exit; |
| 59 | 59 | ecrire_fichier($file, serialize($dtc), true); |
| 60 | 60 | } |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | }; |
| 155 | 155 | } |
| 156 | 156 | if (!is_string($r)) { |
| 157 | - spip_log("erreur $r dans la DTD " . substr($dtd, 0, 80) . '.....'); |
|
| 157 | + spip_log("erreur $r dans la DTD ".substr($dtd, 0, 80).'.....'); |
|
| 158 | 158 | |
| 159 | 159 | return false; |
| 160 | 160 | } |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | if ( |
| 199 | 199 | $n[0] == 'PUBLIC' && !tester_url_absolue($n[1]) |
| 200 | 200 | ) { |
| 201 | - $n[1] = substr((string) $grammaire, 0, strrpos((string) $grammaire, '/') + 1) . $n[1]; |
|
| 201 | + $n[1] = substr((string) $grammaire, 0, strrpos((string) $grammaire, '/') + 1).$n[1]; |
|
| 202 | 202 | } |
| 203 | 203 | analyser_dtd($n[1], $n[0], $dtc); |
| 204 | 204 | } |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | return $dtc->macros[$m[1]] == 'INCLUDE' |
| 228 | - ? $r[1] . substr($m[2], strlen($r[0])) |
|
| 228 | + ? $r[1].substr($m[2], strlen($r[0])) |
|
| 229 | 229 | : substr($m[2], strlen($r[0])); |
| 230 | 230 | } |
| 231 | 231 | |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | spip_log("redefinition de l'entite $nom"); |
| 253 | 253 | } |
| 254 | 254 | if ($k6) { |
| 255 | - return $k6 . $dtd; |
|
| 255 | + return $k6.$dtd; |
|
| 256 | 256 | } // cas du synonyme complet |
| 257 | 257 | $val = expanserEntite(($k2 ? $k3 : ($k4 ? $k5 : $k6)), $dtc->macros); |
| 258 | 258 | |
@@ -352,7 +352,7 @@ discard block |
||
| 352 | 352 | if (preg_match_all("/\s*(\S+)\s+(([(][^)]*[)])|(\S+))\s+([^\s']*)(\s*'[^']*')?/", $val, $r2, PREG_SET_ORDER)) { |
| 353 | 353 | foreach ($r2 as $m2) { |
| 354 | 354 | $v = preg_match('/^\w+$/', $m2[2]) ? $m2[2] |
| 355 | - : ('/^' . preg_replace('/\s+/', '', $m2[2]) . '$/'); |
|
| 355 | + : ('/^'.preg_replace('/\s+/', '', $m2[2]).'$/'); |
|
| 356 | 356 | $m21 = expanserEntite($m2[1], $dtc->macros); |
| 357 | 357 | $m25 = expanserEntite($m2[5], $dtc->macros); |
| 358 | 358 | $dtc->attributs[$nom][$m21] = [$v, $m25]; |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | if (!$link) { |
| 60 | 60 | $erreurs[] = pg_last_error(); |
| 61 | 61 | foreach ($erreurs as $e) { |
| 62 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 62 | + spip_log('Echec pg_connect. Erreur : '.$e, 'pg.'._LOG_HS); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | return false; |
@@ -77,8 +77,8 @@ discard block |
||
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | spip_log( |
| 80 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 81 | - 'pg.' . _LOG_DEBUG |
|
| 80 | + "Connexion vers $host, base $db, prefixe $prefixe ".($link ? 'operationnelle' : 'impossible'), |
|
| 81 | + 'pg.'._LOG_DEBUG |
|
| 82 | 82 | ); |
| 83 | 83 | |
| 84 | 84 | return $link ? [ |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | } else { |
| 167 | 167 | $suite = ''; |
| 168 | 168 | } |
| 169 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', (string) $query) . $suite; |
|
| 169 | + $query = preg_replace('/([,\s])spip_/', '\1'.$prefixe.'_', (string) $query).$suite; |
|
| 170 | 170 | |
| 171 | 171 | // renvoyer la requete inerte si demandee |
| 172 | 172 | if (!$requeter) { |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | // ou revoir l'api de sql_alter en creant un |
| 235 | 235 | // sql_alter_table($table,array($actions)); |
| 236 | 236 | if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', (string) $query, $regs)) { |
| 237 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 237 | + spip_log("$query mal comprise", 'pg.'._LOG_ERREUR); |
|
| 238 | 238 | |
| 239 | 239 | return false; |
| 240 | 240 | } |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | $i = 0; |
| 248 | 248 | $ouverte = false; |
| 249 | 249 | while ($do = array_shift($todo)) { |
| 250 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 250 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i].','.$do : $do; |
|
| 251 | 251 | $o = (str_contains($do, '(')); |
| 252 | 252 | $f = (str_contains($do, ')')); |
| 253 | 253 | if ($o && !$f) { |
@@ -260,33 +260,33 @@ discard block |
||
| 260 | 260 | } |
| 261 | 261 | } |
| 262 | 262 | $todo = $todo2; |
| 263 | - $query = $debut . ' ' . array_shift($todo); |
|
| 263 | + $query = $debut.' '.array_shift($todo); |
|
| 264 | 264 | |
| 265 | 265 | if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
| 266 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 266 | + spip_log("$query incompris", 'pg.'._LOG_ERREUR); |
|
| 267 | 267 | } else { |
| 268 | 268 | if ($r[1]) { |
| 269 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 269 | + spip_log("j'ignore IGNORE dans $query", 'pg.'._LOG_AVERTISSEMENT); |
|
| 270 | 270 | } |
| 271 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 271 | + $f = 'spip_pg_alter_'.strtolower($r[3]); |
|
| 272 | 272 | if (function_exists($f)) { |
| 273 | 273 | $f($r[2], $r[4], $serveur, $requeter); |
| 274 | 274 | } else { |
| 275 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 275 | + spip_log("$query non prevu", 'pg.'._LOG_ERREUR); |
|
| 276 | 276 | } |
| 277 | 277 | } |
| 278 | 278 | // Alter a plusieurs args. Faudrait optimiser. |
| 279 | 279 | if ($todo) { |
| 280 | - spip_pg_alter("TABLE $table " . implode(',', $todo)); |
|
| 280 | + spip_pg_alter("TABLE $table ".implode(',', $todo)); |
|
| 281 | 281 | } |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 285 | 285 | if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', (string) $arg, $r)) { |
| 286 | - spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 286 | + spip_log("alter change: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 287 | 287 | } else { |
| 288 | 288 | [, $old, $new, $type, $default, $null, $def2] = $r; |
| 289 | - $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 289 | + $actions = ["ALTER $old TYPE ".mysql2pg_type($type)]; |
|
| 290 | 290 | $actions[] = $null ? "ALTER $old SET NOT NULL" : "ALTER $old DROP NOT NULL"; |
| 291 | 291 | |
| 292 | 292 | if ($d = ($default ?: $def2)) { |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | $actions[] = "ALTER $old DROP DEFAULT"; |
| 296 | 296 | } |
| 297 | 297 | |
| 298 | - spip_pg_query("ALTER TABLE $table " . implode(', ', $actions)); |
|
| 298 | + spip_pg_query("ALTER TABLE $table ".implode(', ', $actions)); |
|
| 299 | 299 | |
| 300 | 300 | if ($old !== $new) { |
| 301 | 301 | spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
@@ -306,7 +306,7 @@ discard block |
||
| 306 | 306 | function spip_pg_alter_add($table, $arg, $serveur = '', $requeter = true) { |
| 307 | 307 | $nom_index = null; |
| 308 | 308 | if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', (string) $arg, $r)) { |
| 309 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 309 | + spip_log("alter add $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 310 | 310 | |
| 311 | 311 | return null; |
| 312 | 312 | } |
@@ -316,14 +316,14 @@ discard block |
||
| 316 | 316 | $m[2] = $n[1]; |
| 317 | 317 | } |
| 318 | 318 | |
| 319 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 319 | + return spip_pg_query("ALTER TABLE $table ADD ".$m[1].' '.mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 320 | 320 | } elseif ($r[1][0] == 'P') { |
| 321 | 321 | // la primary peut etre sur plusieurs champs |
| 322 | 322 | $r[2] = trim(str_replace('`', '', $r[2])); |
| 323 | 323 | $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
| 324 | 324 | |
| 325 | 325 | return spip_pg_query( |
| 326 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 326 | + "ALTER TABLE $table ADD CONSTRAINT $table".'_pkey PRIMARY KEY ('.$m.')', |
|
| 327 | 327 | $serveur, |
| 328 | 328 | $requeter |
| 329 | 329 | ); |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | $colonnes = substr($m[1], 1, -1); |
| 343 | 343 | if (str_contains(',', $colonnes)) { |
| 344 | 344 | spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
| 345 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 345 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.'._LOG_ERREUR); |
|
| 346 | 346 | } else { |
| 347 | 347 | $nom_index = $colonnes; |
| 348 | 348 | } |
@@ -358,23 +358,23 @@ discard block |
||
| 358 | 358 | |
| 359 | 359 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 360 | 360 | if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', (string) $arg, $r)) { |
| 361 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 361 | + spip_log("alter drop: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 362 | 362 | } else { |
| 363 | 363 | if (!$r[1] || $r[1] == 'COLUMN') { |
| 364 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 364 | + return spip_pg_query("ALTER TABLE $table DROP ".$r[2], $serveur); |
|
| 365 | 365 | } elseif ($r[1][0] == 'P') { |
| 366 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 366 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table".'_pkey', $serveur); |
|
| 367 | 367 | } else { |
| 368 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 368 | + return spip_pg_query('DROP INDEX '.$table.'_'.$r[2], $serveur); |
|
| 369 | 369 | } |
| 370 | 370 | } |
| 371 | 371 | } |
| 372 | 372 | |
| 373 | 373 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 374 | 374 | if (!preg_match('/^`?(\w+)`?\s+(.*)$/', (string) $arg, $r)) { |
| 375 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 375 | + spip_log("alter modify: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 376 | 376 | } else { |
| 377 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 377 | + return spip_pg_alter_change($table, $r[1].' '.$arg, $serveur = '', $requeter = true); |
|
| 378 | 378 | } |
| 379 | 379 | } |
| 380 | 380 | |
@@ -390,7 +390,7 @@ discard block |
||
| 390 | 390 | } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', (string) $arg, $r)) { |
| 391 | 391 | $rename = $r[2]; |
| 392 | 392 | } else { |
| 393 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 393 | + spip_log("alter rename: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
@@ -411,8 +411,8 @@ discard block |
||
| 411 | 411 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 412 | 412 | if (!($nom || $table || $champs)) { |
| 413 | 413 | spip_log( |
| 414 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @implode(',', $champs) . '))', |
|
| 415 | - 'pg.' . _LOG_ERREUR |
|
| 414 | + "Champ manquant pour creer un index pg ($nom, $table, (".@implode(',', $champs).'))', |
|
| 415 | + 'pg.'._LOG_ERREUR |
|
| 416 | 416 | ); |
| 417 | 417 | |
| 418 | 418 | return false; |
@@ -423,7 +423,7 @@ discard block |
||
| 423 | 423 | |
| 424 | 424 | // PG ne differentie pas noms des index en fonction des tables |
| 425 | 425 | // il faut donc creer des noms uniques d'index pour une base pg |
| 426 | - $nom = $table . '_' . $nom; |
|
| 426 | + $nom = $table.'_'.$nom; |
|
| 427 | 427 | // enlever d'eventuelles parentheses deja presentes sur champs |
| 428 | 428 | if (!is_array($champs)) { |
| 429 | 429 | if ($champs[0] == '(') { |
@@ -431,7 +431,7 @@ discard block |
||
| 431 | 431 | } |
| 432 | 432 | $champs = [$champs]; |
| 433 | 433 | } |
| 434 | - $query = "CREATE INDEX $nom ON $table (" . implode(',', $champs) . ')'; |
|
| 434 | + $query = "CREATE INDEX $nom ON $table (".implode(',', $champs).')'; |
|
| 435 | 435 | if (!$requeter) { |
| 436 | 436 | return $query; |
| 437 | 437 | } |
@@ -453,7 +453,7 @@ discard block |
||
| 453 | 453 | } else { |
| 454 | 454 | $suite = ''; |
| 455 | 455 | } |
| 456 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', (string) $query) . $suite; |
|
| 456 | + $query = 'EXPLAIN '.preg_replace('/([,\s])spip_/', '\1'.$prefixe.'_', (string) $query).$suite; |
|
| 457 | 457 | |
| 458 | 458 | if (!$requeter) { |
| 459 | 459 | return $query; |
@@ -542,16 +542,16 @@ discard block |
||
| 542 | 542 | $having = implode("\n\tAND ", array_map('calculer_pg_where', $having)); |
| 543 | 543 | } |
| 544 | 544 | $from = spip_pg_from($from, $prefixe); |
| 545 | - $query = 'SELECT ' . $select |
|
| 545 | + $query = 'SELECT '.$select |
|
| 546 | 546 | . ($from ? "\nFROM $from" : '') |
| 547 | - . ($where ? "\nWHERE " . (is_array($where) ? implode( |
|
| 547 | + . ($where ? "\nWHERE ".(is_array($where) ? implode( |
|
| 548 | 548 | "\n\tAND ", |
| 549 | 549 | array_map('calculer_pg_where', $where) |
| 550 | 550 | ) : (calculer_pg_where($where))) : ('')) |
| 551 | 551 | . spip_pg_groupby($groupby, $from, $select) |
| 552 | 552 | . ($having ? "\nHAVING $having" : '') |
| 553 | 553 | . ($orderby ? ("\nORDER BY $orderby") : '') |
| 554 | - . ($limit ? " LIMIT $count" . ($offset ? " OFFSET $offset" : '') : ('')); |
|
| 554 | + . ($limit ? " LIMIT $count".($offset ? " OFFSET $offset" : '') : ('')); |
|
| 555 | 555 | |
| 556 | 556 | // renvoyer la requete inerte si demandee |
| 557 | 557 | if ($requeter === false) { |
@@ -572,7 +572,7 @@ discard block |
||
| 572 | 572 | $from = spip_pg_select_as($from); |
| 573 | 573 | } |
| 574 | 574 | |
| 575 | - return $prefixe ? preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', (string) $from) : $from; |
|
| 575 | + return $prefixe ? preg_replace('/(\b)spip_/', '\1'.$prefixe.'_', (string) $from) : $from; |
|
| 576 | 576 | } |
| 577 | 577 | |
| 578 | 578 | function spip_pg_orderby($order, $select) { |
@@ -580,7 +580,7 @@ discard block |
||
| 580 | 580 | $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', (string) $order)); |
| 581 | 581 | |
| 582 | 582 | foreach ($arg as $v) { |
| 583 | - $res[] = preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', (string) $select, $m) ? $m[1] : $v; |
|
| 583 | + $res[] = preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+'.$v.'/', (string) $select, $m) ? $m[1] : $v; |
|
| 584 | 584 | } |
| 585 | 585 | |
| 586 | 586 | return spip_pg_frommysql(implode(',', $res)); |
@@ -754,7 +754,7 @@ discard block |
||
| 754 | 754 | $n++; |
| 755 | 755 | $res .= "\nwhen $index=$v then $n"; |
| 756 | 756 | } |
| 757 | - $arg = $m[1] . "case $res else 0 end " |
|
| 757 | + $arg = $m[1]."case $res else 0 end " |
|
| 758 | 758 | . substr((string) $arg, strlen($m[0])); |
| 759 | 759 | } |
| 760 | 760 | |
@@ -797,9 +797,9 @@ discard block |
||
| 797 | 797 | } |
| 798 | 798 | |
| 799 | 799 | if (strtoupper((string) $join) === 'AND') { |
| 800 | - return $exp . implode("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 800 | + return $exp.implode("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 801 | 801 | } else { |
| 802 | - return $exp . implode($join, $v); |
|
| 802 | + return $exp.implode($join, $v); |
|
| 803 | 803 | } |
| 804 | 804 | } |
| 805 | 805 | |
@@ -809,7 +809,7 @@ discard block |
||
| 809 | 809 | if (str_ends_with((string) $k, '@')) { |
| 810 | 810 | // c'est une jointure qui se refere au from precedent |
| 811 | 811 | // pas de virgule |
| 812 | - $argsas .= ' ' . $v; |
|
| 812 | + $argsas .= ' '.$v; |
|
| 813 | 813 | } else { |
| 814 | 814 | $as = ''; |
| 815 | 815 | // spip_log("$k : $v", _LOG_DEBUG); |
@@ -819,7 +819,7 @@ discard block |
||
| 819 | 819 | } elseif ($v != $k) { |
| 820 | 820 | $p = strpos((string) $v, ' '); |
| 821 | 821 | if ($p) { |
| 822 | - $v = substr((string) $v, 0, $p) . " AS $k" . substr((string) $v, $p); |
|
| 822 | + $v = substr((string) $v, 0, $p)." AS $k".substr((string) $v, $p); |
|
| 823 | 823 | } else { |
| 824 | 824 | $as = " AS $k"; |
| 825 | 825 | } |
@@ -827,7 +827,7 @@ discard block |
||
| 827 | 827 | } |
| 828 | 828 | // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
| 829 | 829 | // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
| 830 | - $argsas .= ', ' . $v . $as; |
|
| 830 | + $argsas .= ', '.$v.$as; |
|
| 831 | 831 | } |
| 832 | 832 | } |
| 833 | 833 | |
@@ -858,7 +858,7 @@ discard block |
||
| 858 | 858 | $serveur = '', |
| 859 | 859 | $requeter = true |
| 860 | 860 | ) { |
| 861 | - $c = $groupby ? 'DISTINCT ' . (is_string($groupby) ? $groupby : implode(',', $groupby)) : ('*'); |
|
| 861 | + $c = $groupby ? 'DISTINCT '.(is_string($groupby) ? $groupby : implode(',', $groupby)) : ('*'); |
|
| 862 | 862 | $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
| 863 | 863 | if (!$requeter) { |
| 864 | 864 | return $r; |
@@ -959,8 +959,8 @@ discard block |
||
| 959 | 959 | |
| 960 | 960 | return spip_pg_insert( |
| 961 | 961 | $table, |
| 962 | - '(' . implode(',', array_keys($couples)) . ')', |
|
| 963 | - '(' . implode(',', $couples) . ')', |
|
| 962 | + '('.implode(',', array_keys($couples)).')', |
|
| 963 | + '('.implode(',', $couples).')', |
|
| 964 | 964 | $desc, |
| 965 | 965 | $serveur, |
| 966 | 966 | $requeter |
@@ -983,7 +983,7 @@ discard block |
||
| 983 | 983 | $c = $tab_couples[0] ?? []; |
| 984 | 984 | $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
| 985 | 985 | |
| 986 | - $cles = '(' . implode(',', array_keys($les_cles)) . ')'; |
|
| 986 | + $cles = '('.implode(',', array_keys($les_cles)).')'; |
|
| 987 | 987 | $valeurs = []; |
| 988 | 988 | foreach ($tab_couples as $couples) { |
| 989 | 989 | foreach ($couples as $champ => $val) { |
@@ -992,7 +992,7 @@ discard block |
||
| 992 | 992 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
| 993 | 993 | $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
| 994 | 994 | |
| 995 | - $valeurs[] = '(' . implode(',', $couples) . ')'; |
|
| 995 | + $valeurs[] = '('.implode(',', $couples).')'; |
|
| 996 | 996 | } |
| 997 | 997 | $valeurs = implode(', ', $valeurs); |
| 998 | 998 | |
@@ -1013,7 +1013,7 @@ discard block |
||
| 1013 | 1013 | |
| 1014 | 1014 | $set = []; |
| 1015 | 1015 | foreach ($couples as $champ => $val) { |
| 1016 | - $set[] = $champ . '=' . $val; |
|
| 1016 | + $set[] = $champ.'='.$val; |
|
| 1017 | 1017 | } |
| 1018 | 1018 | |
| 1019 | 1019 | $query = calculer_pg_expression('UPDATE', $table, ',') |
@@ -1048,7 +1048,7 @@ discard block |
||
| 1048 | 1048 | |
| 1049 | 1049 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1050 | 1050 | if (!$values) { |
| 1051 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1051 | + spip_log("replace vide $table", 'pg.'._LOG_AVERTISSEMENT); |
|
| 1052 | 1052 | |
| 1053 | 1053 | return 0; |
| 1054 | 1054 | } |
@@ -1082,8 +1082,8 @@ discard block |
||
| 1082 | 1082 | if (!$where) { |
| 1083 | 1083 | return spip_pg_insert( |
| 1084 | 1084 | $table, |
| 1085 | - '(' . implode(',', array_keys($values)) . ')', |
|
| 1086 | - '(' . implode(',', $values) . ')', |
|
| 1085 | + '('.implode(',', array_keys($values)).')', |
|
| 1086 | + '('.implode(',', $values).')', |
|
| 1087 | 1087 | $desc, |
| 1088 | 1088 | $serveur |
| 1089 | 1089 | ); |
@@ -1104,12 +1104,11 @@ discard block |
||
| 1104 | 1104 | $couples = pg_affected_rows($couples); |
| 1105 | 1105 | } |
| 1106 | 1106 | if (!$couples) { |
| 1107 | - $ret = $seq ? " RETURNING nextval('$seq') < $prim" : |
|
| 1108 | - (''); |
|
| 1109 | - $connexion['last'] = $q = "INSERT INTO $table (" . implode(',', array_keys($values)) . ') VALUES (' . implode( |
|
| 1107 | + $ret = $seq ? " RETURNING nextval('$seq') < $prim" : (''); |
|
| 1108 | + $connexion['last'] = $q = "INSERT INTO $table (".implode(',', array_keys($values)).') VALUES ('.implode( |
|
| 1110 | 1109 | ',', |
| 1111 | 1110 | $values |
| 1112 | - ) . ")$ret"; |
|
| 1111 | + ).")$ret"; |
|
| 1113 | 1112 | $couples = spip_pg_query_simple($link, $q); |
| 1114 | 1113 | if (!$couples) { |
| 1115 | 1114 | return false; |
@@ -1156,7 +1155,7 @@ discard block |
||
| 1156 | 1155 | ) { |
| 1157 | 1156 | return ''; |
| 1158 | 1157 | } else { |
| 1159 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1158 | + return $raw ? $prim : $table.'_'.$prim.'_seq'; |
|
| 1160 | 1159 | } |
| 1161 | 1160 | } |
| 1162 | 1161 | |
@@ -1173,29 +1172,29 @@ discard block |
||
| 1173 | 1172 | return spip_pg_frommysql($v); |
| 1174 | 1173 | } else { |
| 1175 | 1174 | if (str_starts_with((string) $v, '0000')) { |
| 1176 | - $v = '0001' . substr((string) $v, 4); |
|
| 1175 | + $v = '0001'.substr((string) $v, 4); |
|
| 1177 | 1176 | } |
| 1178 | 1177 | if (strpos((string) $v, '-00-00') === 4) { |
| 1179 | - $v = substr((string) $v, 0, 4) . '-01-01' . substr((string) $v, 10); |
|
| 1178 | + $v = substr((string) $v, 0, 4).'-01-01'.substr((string) $v, 10); |
|
| 1180 | 1179 | } |
| 1181 | 1180 | |
| 1182 | 1181 | return "timestamp '$v'"; |
| 1183 | 1182 | } |
| 1184 | 1183 | } elseif (!sql_test_int($t)) { |
| 1185 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1184 | + return ("'".pg_escape_string($v)."'"); |
|
| 1186 | 1185 | } elseif (is_numeric($v) || str_starts_with((string) $v, 'CAST(')) { |
| 1187 | 1186 | return $v; |
| 1188 | 1187 | } elseif ($v[0] == '0' && $v[1] !== 'x' && ctype_xdigit(substr((string) $v, 1))) { |
| 1189 | 1188 | return substr((string) $v, 1); |
| 1190 | 1189 | } else { |
| 1191 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1190 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.'._LOG_AVERTISSEMENT); |
|
| 1192 | 1191 | |
| 1193 | 1192 | return (int) $v; |
| 1194 | 1193 | } |
| 1195 | 1194 | } |
| 1196 | 1195 | |
| 1197 | 1196 | function spip_pg_hex($v) { |
| 1198 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1197 | + return "CAST(x'".$v."' as bigint)"; |
|
| 1199 | 1198 | } |
| 1200 | 1199 | |
| 1201 | 1200 | function spip_pg_quote($v, $type = '') { |
@@ -1233,15 +1232,15 @@ discard block |
||
| 1233 | 1232 | return $not ? '0=0' : '0=1'; |
| 1234 | 1233 | } |
| 1235 | 1234 | if (str_contains((string) $valeurs, "CAST(x'")) { |
| 1236 | - return "($val=" . implode("OR $val=", explode(',', (string) $valeurs)) . ')'; |
|
| 1235 | + return "($val=".implode("OR $val=", explode(',', (string) $valeurs)).')'; |
|
| 1237 | 1236 | } |
| 1238 | 1237 | $n = $i = 0; |
| 1239 | 1238 | $in_sql = ''; |
| 1240 | 1239 | while ($n = strpos((string) $valeurs, ',', $n + 1)) { |
| 1241 | 1240 | if ((++$i) >= 255) { |
| 1242 | - $in_sql .= "($val $not IN (" . |
|
| 1243 | - substr((string) $valeurs, 0, $n) . |
|
| 1244 | - "))\n" . |
|
| 1241 | + $in_sql .= "($val $not IN (". |
|
| 1242 | + substr((string) $valeurs, 0, $n). |
|
| 1243 | + "))\n". |
|
| 1245 | 1244 | ($not ? "AND\t" : "OR\t"); |
| 1246 | 1245 | $valeurs = substr((string) $valeurs, $n + 1); |
| 1247 | 1246 | $i = $n = 0; |
@@ -1257,7 +1256,7 @@ discard block |
||
| 1257 | 1256 | $s = $link ? pg_last_error($link) : pg_last_error(); |
| 1258 | 1257 | if ($s) { |
| 1259 | 1258 | $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
| 1260 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1259 | + spip_log("$s - $query", 'pg.'._LOG_ERREUR); |
|
| 1261 | 1260 | } |
| 1262 | 1261 | |
| 1263 | 1262 | return $s; |
@@ -1305,7 +1304,7 @@ discard block |
||
| 1305 | 1304 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1306 | 1305 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 1307 | 1306 | $link = $connexion['link']; |
| 1308 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1307 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE '._q($match); |
|
| 1309 | 1308 | |
| 1310 | 1309 | return spip_pg_query_simple($link, $q); |
| 1311 | 1310 | } |
@@ -1313,7 +1312,7 @@ discard block |
||
| 1313 | 1312 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1314 | 1313 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
| 1315 | 1314 | $link = $connexion['link']; |
| 1316 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1315 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE '._q($nom_table); |
|
| 1317 | 1316 | |
| 1318 | 1317 | $res = spip_pg_query_simple($link, $q); |
| 1319 | 1318 | if (!$res) { |
@@ -1324,15 +1323,15 @@ discard block |
||
| 1324 | 1323 | // il faut en tenir compte dans le return |
| 1325 | 1324 | $fields = []; |
| 1326 | 1325 | while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
| 1327 | - $fields[$field[0]] = $field[2] . ($field[1] ? ' DEFAULT ' . $field[1] : ('')); |
|
| 1326 | + $fields[$field[0]] = $field[2].($field[1] ? ' DEFAULT '.$field[1] : ('')); |
|
| 1328 | 1327 | } |
| 1329 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1328 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE '._q($nom_table); |
|
| 1330 | 1329 | $res = spip_pg_query_simple($link, $q); |
| 1331 | 1330 | $keys = []; |
| 1332 | 1331 | while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
| 1333 | 1332 | if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
| 1334 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1335 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1333 | + $nom = str_replace($nom_table.'_', '', $r[2]); |
|
| 1334 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY '.$nom))] = $r[3]; |
|
| 1336 | 1335 | } |
| 1337 | 1336 | } |
| 1338 | 1337 | |
@@ -1364,16 +1363,16 @@ discard block |
||
| 1364 | 1363 | if (str_starts_with($k, 'KEY ')) { |
| 1365 | 1364 | $n = str_replace('`', '', $k); |
| 1366 | 1365 | $v = str_replace('`', '"', (string) $v); |
| 1367 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1366 | + $i = $nom.preg_replace('/KEY +/', '_', $n); |
|
| 1368 | 1367 | if ($k != $n) { |
| 1369 | 1368 | $i = "\"$i\""; |
| 1370 | 1369 | } |
| 1371 | 1370 | $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
| 1372 | 1371 | } elseif (str_starts_with($k, 'UNIQUE ')) { |
| 1373 | 1372 | $k = preg_replace('/^UNIQUE +/', '', $k); |
| 1374 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1373 | + $prim .= "$s\n\t\tCONSTRAINT ".str_replace('`', '"', $k)." UNIQUE ($v)"; |
|
| 1375 | 1374 | } else { |
| 1376 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1375 | + $prim .= "$s\n\t\t".str_replace('`', '"', $k)." ($v)"; |
|
| 1377 | 1376 | } |
| 1378 | 1377 | if ($k == 'PRIMARY KEY') { |
| 1379 | 1378 | $prim_name = $v; |
@@ -1384,16 +1383,16 @@ discard block |
||
| 1384 | 1383 | |
| 1385 | 1384 | $character_set = ''; |
| 1386 | 1385 | if (@$GLOBALS['meta']['charset_sql_base']) { |
| 1387 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1386 | + $character_set .= ' CHARACTER SET '.$GLOBALS['meta']['charset_sql_base']; |
|
| 1388 | 1387 | } |
| 1389 | 1388 | if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
| 1390 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1389 | + $character_set .= ' COLLATE '.$GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1391 | 1390 | } |
| 1392 | 1391 | |
| 1393 | 1392 | foreach ($champs as $k => $v) { |
| 1394 | 1393 | $k = str_replace('`', '"', $k); |
| 1395 | 1394 | if (preg_match(',([a-z]*\s*(\(\s*\d*\s*\))?(\s*binary)?),i', (string) $v, $defs) && (preg_match(',(char|text),i', $defs[1]) && !preg_match(',binary,i', $defs[1]))) { |
| 1396 | - $v = $defs[1] . $character_set . ' ' . substr((string) $v, strlen($defs[1])); |
|
| 1395 | + $v = $defs[1].$character_set.' '.substr((string) $v, strlen($defs[1])); |
|
| 1397 | 1396 | } |
| 1398 | 1397 | |
| 1399 | 1398 | $query .= "$s\n\t\t$k " |
@@ -1407,7 +1406,7 @@ discard block |
||
| 1407 | 1406 | |
| 1408 | 1407 | // En l'absence de "if not exists" en PG, on neutralise les erreurs |
| 1409 | 1408 | |
| 1410 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1409 | + $q = "CREATE $temporary TABLE $nom ($query".($prim ? ",$prim" : '').')'. |
|
| 1411 | 1410 | ($character_set ? " DEFAULT $character_set" : '') |
| 1412 | 1411 | . "\n"; |
| 1413 | 1412 | |
@@ -1418,7 +1417,7 @@ discard block |
||
| 1418 | 1417 | $r = @pg_query($link, $q); |
| 1419 | 1418 | |
| 1420 | 1419 | if (!$r) { |
| 1421 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1420 | + spip_log("Impossible de creer cette table: $q", 'pg.'._LOG_ERREUR); |
|
| 1422 | 1421 | } else { |
| 1423 | 1422 | foreach ($keys as $index) { |
| 1424 | 1423 | pg_query($link, $index); |
@@ -1441,20 +1440,20 @@ discard block |
||
| 1441 | 1440 | // vue deja presente |
| 1442 | 1441 | if (sql_showtable($nom, false, $serveur)) { |
| 1443 | 1442 | if ($requeter) { |
| 1444 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1443 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.'._LOG_ERREUR); |
|
| 1445 | 1444 | } |
| 1446 | 1445 | |
| 1447 | 1446 | return false; |
| 1448 | 1447 | } |
| 1449 | 1448 | |
| 1450 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1449 | + $query = "CREATE VIEW $nom AS ".$query_select; |
|
| 1451 | 1450 | |
| 1452 | 1451 | return spip_pg_query($query, $serveur, $requeter); |
| 1453 | 1452 | } |
| 1454 | 1453 | |
| 1455 | 1454 | |
| 1456 | 1455 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1457 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1456 | + spip_log('changement de charset sql a ecrire en PG', 'pg.'._LOG_ERREUR); |
|
| 1458 | 1457 | } |
| 1459 | 1458 | |
| 1460 | 1459 | |
@@ -1467,7 +1466,7 @@ discard block |
||
| 1467 | 1466 | * @return bool|string true / false / requete |
| 1468 | 1467 | **/ |
| 1469 | 1468 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1470 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1469 | + return spip_pg_query('VACUUM '.$table, $serveur, $requeter); |
|
| 1471 | 1470 | } |
| 1472 | 1471 | |
| 1473 | 1472 | // Selectionner la sous-chaine dans $objet |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | define('_DEFAULT_DB', 'spip'); |
@@ -30,155 +30,155 @@ discard block |
||
| 30 | 30 | // si ca ne marche toujours pas, echec. |
| 31 | 31 | |
| 32 | 32 | function req_pg_dist($addr, $port, $login, #[\SensitiveParameter] $pass, $db = '', $prefixe = '') { |
| 33 | - static $last_connect = []; |
|
| 34 | - if (!extension_loaded('pgsql')) { |
|
| 35 | - return false; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - // si provient de selectdb |
|
| 39 | - if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 40 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 41 | - ${$a} = $last_connect[$a]; |
|
| 42 | - } |
|
| 43 | - } |
|
| 44 | - [$host, $p] = array_pad(explode(';', (string) $addr), 2, null); |
|
| 45 | - $port = $p > 0 ? " port=$p" : ''; |
|
| 46 | - $erreurs = []; |
|
| 47 | - if ($db) { |
|
| 48 | - @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 49 | - } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 50 | - $erreurs[] = pg_last_error(); |
|
| 51 | - if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 52 | - $db = $login; |
|
| 53 | - } else { |
|
| 54 | - $erreurs[] = pg_last_error(); |
|
| 55 | - $db = _DEFAULT_DB; |
|
| 56 | - $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 57 | - } |
|
| 58 | - } |
|
| 59 | - if (!$link) { |
|
| 60 | - $erreurs[] = pg_last_error(); |
|
| 61 | - foreach ($erreurs as $e) { |
|
| 62 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - return false; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - if ($link) { |
|
| 69 | - $last_connect = [ |
|
| 70 | - 'addr' => $addr, |
|
| 71 | - 'port' => $port, |
|
| 72 | - 'login' => $login, |
|
| 73 | - 'pass' => $pass, |
|
| 74 | - 'db' => $db, |
|
| 75 | - 'prefixe' => $prefixe, |
|
| 76 | - ]; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - spip_log( |
|
| 80 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 81 | - 'pg.' . _LOG_DEBUG |
|
| 82 | - ); |
|
| 83 | - |
|
| 84 | - return $link ? [ |
|
| 85 | - 'db' => $db, |
|
| 86 | - 'prefixe' => $prefixe ?: $db, |
|
| 87 | - 'link' => $link, |
|
| 88 | - ] : false; |
|
| 33 | + static $last_connect = []; |
|
| 34 | + if (!extension_loaded('pgsql')) { |
|
| 35 | + return false; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + // si provient de selectdb |
|
| 39 | + if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 40 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 41 | + ${$a} = $last_connect[$a]; |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | + [$host, $p] = array_pad(explode(';', (string) $addr), 2, null); |
|
| 45 | + $port = $p > 0 ? " port=$p" : ''; |
|
| 46 | + $erreurs = []; |
|
| 47 | + if ($db) { |
|
| 48 | + @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 49 | + } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 50 | + $erreurs[] = pg_last_error(); |
|
| 51 | + if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 52 | + $db = $login; |
|
| 53 | + } else { |
|
| 54 | + $erreurs[] = pg_last_error(); |
|
| 55 | + $db = _DEFAULT_DB; |
|
| 56 | + $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | + if (!$link) { |
|
| 60 | + $erreurs[] = pg_last_error(); |
|
| 61 | + foreach ($erreurs as $e) { |
|
| 62 | + spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + return false; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + if ($link) { |
|
| 69 | + $last_connect = [ |
|
| 70 | + 'addr' => $addr, |
|
| 71 | + 'port' => $port, |
|
| 72 | + 'login' => $login, |
|
| 73 | + 'pass' => $pass, |
|
| 74 | + 'db' => $db, |
|
| 75 | + 'prefixe' => $prefixe, |
|
| 76 | + ]; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + spip_log( |
|
| 80 | + "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 81 | + 'pg.' . _LOG_DEBUG |
|
| 82 | + ); |
|
| 83 | + |
|
| 84 | + return $link ? [ |
|
| 85 | + 'db' => $db, |
|
| 86 | + 'prefixe' => $prefixe ?: $db, |
|
| 87 | + 'link' => $link, |
|
| 88 | + ] : false; |
|
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | $GLOBALS['spip_pg_functions_1'] = [ |
| 92 | - 'alter' => 'spip_pg_alter', |
|
| 93 | - 'count' => 'spip_pg_count', |
|
| 94 | - 'countsel' => 'spip_pg_countsel', |
|
| 95 | - 'create' => 'spip_pg_create', |
|
| 96 | - 'create_base' => 'spip_pg_create_base', |
|
| 97 | - 'create_view' => 'spip_pg_create_view', |
|
| 98 | - 'date_proche' => 'spip_pg_date_proche', |
|
| 99 | - 'delete' => 'spip_pg_delete', |
|
| 100 | - 'drop_table' => 'spip_pg_drop_table', |
|
| 101 | - 'drop_view' => 'spip_pg_drop_view', |
|
| 102 | - 'errno' => 'spip_pg_errno', |
|
| 103 | - 'error' => 'spip_pg_error', |
|
| 104 | - 'explain' => 'spip_pg_explain', |
|
| 105 | - 'fetch' => 'spip_pg_fetch', |
|
| 106 | - 'seek' => 'spip_pg_seek', |
|
| 107 | - 'free' => 'spip_pg_free', |
|
| 108 | - 'hex' => 'spip_pg_hex', |
|
| 109 | - 'in' => 'spip_pg_in', |
|
| 110 | - 'insert' => 'spip_pg_insert', |
|
| 111 | - 'insertq' => 'spip_pg_insertq', |
|
| 112 | - 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 113 | - 'listdbs' => 'spip_pg_listdbs', |
|
| 114 | - 'multi' => 'spip_pg_multi', |
|
| 115 | - 'optimize' => 'spip_pg_optimize', |
|
| 116 | - 'query' => 'spip_pg_query', |
|
| 117 | - 'quote' => 'spip_pg_quote', |
|
| 118 | - 'replace' => 'spip_pg_replace', |
|
| 119 | - 'replace_multi' => 'spip_pg_replace_multi', |
|
| 120 | - 'select' => 'spip_pg_select', |
|
| 121 | - 'selectdb' => 'spip_pg_selectdb', |
|
| 122 | - 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 123 | - 'showbase' => 'spip_pg_showbase', |
|
| 124 | - 'showtable' => 'spip_pg_showtable', |
|
| 125 | - 'update' => 'spip_pg_update', |
|
| 126 | - 'updateq' => 'spip_pg_updateq', |
|
| 92 | + 'alter' => 'spip_pg_alter', |
|
| 93 | + 'count' => 'spip_pg_count', |
|
| 94 | + 'countsel' => 'spip_pg_countsel', |
|
| 95 | + 'create' => 'spip_pg_create', |
|
| 96 | + 'create_base' => 'spip_pg_create_base', |
|
| 97 | + 'create_view' => 'spip_pg_create_view', |
|
| 98 | + 'date_proche' => 'spip_pg_date_proche', |
|
| 99 | + 'delete' => 'spip_pg_delete', |
|
| 100 | + 'drop_table' => 'spip_pg_drop_table', |
|
| 101 | + 'drop_view' => 'spip_pg_drop_view', |
|
| 102 | + 'errno' => 'spip_pg_errno', |
|
| 103 | + 'error' => 'spip_pg_error', |
|
| 104 | + 'explain' => 'spip_pg_explain', |
|
| 105 | + 'fetch' => 'spip_pg_fetch', |
|
| 106 | + 'seek' => 'spip_pg_seek', |
|
| 107 | + 'free' => 'spip_pg_free', |
|
| 108 | + 'hex' => 'spip_pg_hex', |
|
| 109 | + 'in' => 'spip_pg_in', |
|
| 110 | + 'insert' => 'spip_pg_insert', |
|
| 111 | + 'insertq' => 'spip_pg_insertq', |
|
| 112 | + 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 113 | + 'listdbs' => 'spip_pg_listdbs', |
|
| 114 | + 'multi' => 'spip_pg_multi', |
|
| 115 | + 'optimize' => 'spip_pg_optimize', |
|
| 116 | + 'query' => 'spip_pg_query', |
|
| 117 | + 'quote' => 'spip_pg_quote', |
|
| 118 | + 'replace' => 'spip_pg_replace', |
|
| 119 | + 'replace_multi' => 'spip_pg_replace_multi', |
|
| 120 | + 'select' => 'spip_pg_select', |
|
| 121 | + 'selectdb' => 'spip_pg_selectdb', |
|
| 122 | + 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 123 | + 'showbase' => 'spip_pg_showbase', |
|
| 124 | + 'showtable' => 'spip_pg_showtable', |
|
| 125 | + 'update' => 'spip_pg_update', |
|
| 126 | + 'updateq' => 'spip_pg_updateq', |
|
| 127 | 127 | ]; |
| 128 | 128 | |
| 129 | 129 | // Par ou ca passe une fois les traductions faites |
| 130 | 130 | function spip_pg_trace_query($query, $serveur = '') { |
| 131 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 132 | - $prefixe = $connexion['prefixe']; |
|
| 133 | - $link = $connexion['link']; |
|
| 134 | - $db = $connexion['db']; |
|
| 135 | - |
|
| 136 | - if (isset($_GET['var_profile'])) { |
|
| 137 | - include_spip('public/tracer'); |
|
| 138 | - $t = trace_query_start(); |
|
| 139 | - $e = ''; |
|
| 140 | - } else { |
|
| 141 | - $t = 0; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $connexion['last'] = $query; |
|
| 145 | - $r = spip_pg_query_simple($link, $query); |
|
| 146 | - |
|
| 147 | - // Log de l'erreur eventuelle |
|
| 148 | - if ($e = spip_pg_errno($serveur)) { |
|
| 149 | - $e .= spip_pg_error($query, $serveur); |
|
| 150 | - } // et du fautif |
|
| 151 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 131 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 132 | + $prefixe = $connexion['prefixe']; |
|
| 133 | + $link = $connexion['link']; |
|
| 134 | + $db = $connexion['db']; |
|
| 135 | + |
|
| 136 | + if (isset($_GET['var_profile'])) { |
|
| 137 | + include_spip('public/tracer'); |
|
| 138 | + $t = trace_query_start(); |
|
| 139 | + $e = ''; |
|
| 140 | + } else { |
|
| 141 | + $t = 0; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $connexion['last'] = $query; |
|
| 145 | + $r = spip_pg_query_simple($link, $query); |
|
| 146 | + |
|
| 147 | + // Log de l'erreur eventuelle |
|
| 148 | + if ($e = spip_pg_errno($serveur)) { |
|
| 149 | + $e .= spip_pg_error($query, $serveur); |
|
| 150 | + } // et du fautif |
|
| 151 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | // Fonction de requete generale quand on est sur que c'est SQL standard. |
| 155 | 155 | // Elle change juste le noms des tables ($table_prefix) dans le FROM etc |
| 156 | 156 | |
| 157 | 157 | function spip_pg_query($query, $serveur = '', $requeter = true) { |
| 158 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 159 | - $prefixe = $connexion['prefixe']; |
|
| 160 | - $link = $connexion['link']; |
|
| 161 | - $db = $connexion['db']; |
|
| 162 | - |
|
| 163 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', (string) $query, $regs)) { |
|
| 164 | - $suite = strstr((string) $query, (string) $regs[0]); |
|
| 165 | - $query = substr((string) $query, 0, -strlen($suite)); |
|
| 166 | - } else { |
|
| 167 | - $suite = ''; |
|
| 168 | - } |
|
| 169 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', (string) $query) . $suite; |
|
| 170 | - |
|
| 171 | - // renvoyer la requete inerte si demandee |
|
| 172 | - if (!$requeter) { |
|
| 173 | - return $query; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return spip_pg_trace_query($query, $serveur); |
|
| 158 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 159 | + $prefixe = $connexion['prefixe']; |
|
| 160 | + $link = $connexion['link']; |
|
| 161 | + $db = $connexion['db']; |
|
| 162 | + |
|
| 163 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', (string) $query, $regs)) { |
|
| 164 | + $suite = strstr((string) $query, (string) $regs[0]); |
|
| 165 | + $query = substr((string) $query, 0, -strlen($suite)); |
|
| 166 | + } else { |
|
| 167 | + $suite = ''; |
|
| 168 | + } |
|
| 169 | + $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', (string) $query) . $suite; |
|
| 170 | + |
|
| 171 | + // renvoyer la requete inerte si demandee |
|
| 172 | + if (!$requeter) { |
|
| 173 | + return $query; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return spip_pg_trace_query($query, $serveur); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | function spip_pg_query_simple($link, $query) { |
| 180 | - #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 181 | - return pg_query($link, $query); |
|
| 180 | + #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 181 | + return pg_query($link, $query); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | /* |
@@ -190,192 +190,192 @@ discard block |
||
| 190 | 190 | * de requetes showtable intempestives |
| 191 | 191 | */ |
| 192 | 192 | function spip_pg_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 193 | - static $tables = []; |
|
| 194 | - |
|
| 195 | - if (!isset($tables[$table])) { |
|
| 196 | - if (!$desc) { |
|
| 197 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 198 | - $desc = $trouver_table($table, $serveur); |
|
| 199 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 200 | - if (!$desc) { |
|
| 201 | - return $couples; |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 206 | - // cependant, il faudra peut etre etendre |
|
| 207 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 208 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 209 | - $tables[$table] = []; |
|
| 210 | - foreach ($desc['field'] as $k => $v) { |
|
| 211 | - $v = strtolower(ltrim((string) $v)); |
|
| 212 | - // ne pas ajouter de timestamp now() si un default est specifie |
|
| 213 | - if (str_starts_with($v, 'timestamp') && !str_contains($v, 'default')) { |
|
| 214 | - $tables[$table][] = $k; |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - // ajout des champs type 'timestamp' absents |
|
| 220 | - foreach ($tables[$table] as $maj) { |
|
| 221 | - if (!array_key_exists($maj, $couples)) { |
|
| 222 | - $couples[$maj] = 'NOW()'; |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - return $couples; |
|
| 193 | + static $tables = []; |
|
| 194 | + |
|
| 195 | + if (!isset($tables[$table])) { |
|
| 196 | + if (!$desc) { |
|
| 197 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 198 | + $desc = $trouver_table($table, $serveur); |
|
| 199 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 200 | + if (!$desc) { |
|
| 201 | + return $couples; |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 206 | + // cependant, il faudra peut etre etendre |
|
| 207 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 208 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 209 | + $tables[$table] = []; |
|
| 210 | + foreach ($desc['field'] as $k => $v) { |
|
| 211 | + $v = strtolower(ltrim((string) $v)); |
|
| 212 | + // ne pas ajouter de timestamp now() si un default est specifie |
|
| 213 | + if (str_starts_with($v, 'timestamp') && !str_contains($v, 'default')) { |
|
| 214 | + $tables[$table][] = $k; |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + // ajout des champs type 'timestamp' absents |
|
| 220 | + foreach ($tables[$table] as $maj) { |
|
| 221 | + if (!array_key_exists($maj, $couples)) { |
|
| 222 | + $couples[$maj] = 'NOW()'; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + return $couples; |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | |
| 230 | 230 | // Alter en PG ne traite pas les index |
| 231 | 231 | function spip_pg_alter($query, $serveur = '', $requeter = true) { |
| 232 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 233 | - // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 234 | - // ou revoir l'api de sql_alter en creant un |
|
| 235 | - // sql_alter_table($table,array($actions)); |
|
| 236 | - if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', (string) $query, $regs)) { |
|
| 237 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 238 | - |
|
| 239 | - return false; |
|
| 240 | - } |
|
| 241 | - $debut = $regs[1]; |
|
| 242 | - $table = $regs[3]; |
|
| 243 | - $suite = $regs[4]; |
|
| 244 | - $todo = explode(',', $suite); |
|
| 245 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 246 | - $todo2 = []; |
|
| 247 | - $i = 0; |
|
| 248 | - $ouverte = false; |
|
| 249 | - while ($do = array_shift($todo)) { |
|
| 250 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 251 | - $o = (str_contains($do, '(')); |
|
| 252 | - $f = (str_contains($do, ')')); |
|
| 253 | - if ($o && !$f) { |
|
| 254 | - $ouverte = true; |
|
| 255 | - } elseif ($f) { |
|
| 256 | - $ouverte = false; |
|
| 257 | - } |
|
| 258 | - if (!$ouverte) { |
|
| 259 | - $i++; |
|
| 260 | - } |
|
| 261 | - } |
|
| 262 | - $todo = $todo2; |
|
| 263 | - $query = $debut . ' ' . array_shift($todo); |
|
| 264 | - |
|
| 265 | - if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 266 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 267 | - } else { |
|
| 268 | - if ($r[1]) { |
|
| 269 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 270 | - } |
|
| 271 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 272 | - if (function_exists($f)) { |
|
| 273 | - $f($r[2], $r[4], $serveur, $requeter); |
|
| 274 | - } else { |
|
| 275 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - // Alter a plusieurs args. Faudrait optimiser. |
|
| 279 | - if ($todo) { |
|
| 280 | - spip_pg_alter("TABLE $table " . implode(',', $todo)); |
|
| 281 | - } |
|
| 232 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 233 | + // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 234 | + // ou revoir l'api de sql_alter en creant un |
|
| 235 | + // sql_alter_table($table,array($actions)); |
|
| 236 | + if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', (string) $query, $regs)) { |
|
| 237 | + spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 238 | + |
|
| 239 | + return false; |
|
| 240 | + } |
|
| 241 | + $debut = $regs[1]; |
|
| 242 | + $table = $regs[3]; |
|
| 243 | + $suite = $regs[4]; |
|
| 244 | + $todo = explode(',', $suite); |
|
| 245 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 246 | + $todo2 = []; |
|
| 247 | + $i = 0; |
|
| 248 | + $ouverte = false; |
|
| 249 | + while ($do = array_shift($todo)) { |
|
| 250 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 251 | + $o = (str_contains($do, '(')); |
|
| 252 | + $f = (str_contains($do, ')')); |
|
| 253 | + if ($o && !$f) { |
|
| 254 | + $ouverte = true; |
|
| 255 | + } elseif ($f) { |
|
| 256 | + $ouverte = false; |
|
| 257 | + } |
|
| 258 | + if (!$ouverte) { |
|
| 259 | + $i++; |
|
| 260 | + } |
|
| 261 | + } |
|
| 262 | + $todo = $todo2; |
|
| 263 | + $query = $debut . ' ' . array_shift($todo); |
|
| 264 | + |
|
| 265 | + if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 266 | + spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 267 | + } else { |
|
| 268 | + if ($r[1]) { |
|
| 269 | + spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 270 | + } |
|
| 271 | + $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 272 | + if (function_exists($f)) { |
|
| 273 | + $f($r[2], $r[4], $serveur, $requeter); |
|
| 274 | + } else { |
|
| 275 | + spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + // Alter a plusieurs args. Faudrait optimiser. |
|
| 279 | + if ($todo) { |
|
| 280 | + spip_pg_alter("TABLE $table " . implode(',', $todo)); |
|
| 281 | + } |
|
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 285 | - if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', (string) $arg, $r)) { |
|
| 286 | - spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 287 | - } else { |
|
| 288 | - [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 289 | - $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 290 | - $actions[] = $null ? "ALTER $old SET NOT NULL" : "ALTER $old DROP NOT NULL"; |
|
| 291 | - |
|
| 292 | - if ($d = ($default ?: $def2)) { |
|
| 293 | - $actions[] = "ALTER $old SET $d"; |
|
| 294 | - } else { |
|
| 295 | - $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - spip_pg_query("ALTER TABLE $table " . implode(', ', $actions)); |
|
| 299 | - |
|
| 300 | - if ($old !== $new) { |
|
| 301 | - spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 302 | - } |
|
| 303 | - } |
|
| 285 | + if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', (string) $arg, $r)) { |
|
| 286 | + spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 287 | + } else { |
|
| 288 | + [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 289 | + $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 290 | + $actions[] = $null ? "ALTER $old SET NOT NULL" : "ALTER $old DROP NOT NULL"; |
|
| 291 | + |
|
| 292 | + if ($d = ($default ?: $def2)) { |
|
| 293 | + $actions[] = "ALTER $old SET $d"; |
|
| 294 | + } else { |
|
| 295 | + $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + spip_pg_query("ALTER TABLE $table " . implode(', ', $actions)); |
|
| 299 | + |
|
| 300 | + if ($old !== $new) { |
|
| 301 | + spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 302 | + } |
|
| 303 | + } |
|
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | function spip_pg_alter_add($table, $arg, $serveur = '', $requeter = true) { |
| 307 | - $nom_index = null; |
|
| 308 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', (string) $arg, $r)) { |
|
| 309 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 310 | - |
|
| 311 | - return null; |
|
| 312 | - } |
|
| 313 | - if (!$r[1] || $r[1] == 'COLUMN') { |
|
| 314 | - preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 315 | - if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 316 | - $m[2] = $n[1]; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 320 | - } elseif ($r[1][0] == 'P') { |
|
| 321 | - // la primary peut etre sur plusieurs champs |
|
| 322 | - $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 323 | - $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 324 | - |
|
| 325 | - return spip_pg_query( |
|
| 326 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 327 | - $serveur, |
|
| 328 | - $requeter |
|
| 329 | - ); |
|
| 330 | - } else { |
|
| 331 | - preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 332 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 333 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 334 | - // |
|
| 335 | - // nom_index (colonnes) |
|
| 336 | - if ($m[2]) { |
|
| 337 | - $colonnes = substr($m[2], 1, -1); |
|
| 338 | - $nom_index = $m[1]; |
|
| 339 | - } else { |
|
| 340 | - // (colonne) |
|
| 341 | - if ($m[1][0] == '(') { |
|
| 342 | - $colonnes = substr($m[1], 1, -1); |
|
| 343 | - if (str_contains(',', $colonnes)) { |
|
| 344 | - spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 345 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 346 | - } else { |
|
| 347 | - $nom_index = $colonnes; |
|
| 348 | - } |
|
| 349 | - } // nom_index |
|
| 350 | - else { |
|
| 351 | - $nom_index = $colonnes = $m[1]; |
|
| 352 | - } |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 356 | - } |
|
| 307 | + $nom_index = null; |
|
| 308 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', (string) $arg, $r)) { |
|
| 309 | + spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 310 | + |
|
| 311 | + return null; |
|
| 312 | + } |
|
| 313 | + if (!$r[1] || $r[1] == 'COLUMN') { |
|
| 314 | + preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 315 | + if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 316 | + $m[2] = $n[1]; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 320 | + } elseif ($r[1][0] == 'P') { |
|
| 321 | + // la primary peut etre sur plusieurs champs |
|
| 322 | + $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 323 | + $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 324 | + |
|
| 325 | + return spip_pg_query( |
|
| 326 | + "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 327 | + $serveur, |
|
| 328 | + $requeter |
|
| 329 | + ); |
|
| 330 | + } else { |
|
| 331 | + preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 332 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 333 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 334 | + // |
|
| 335 | + // nom_index (colonnes) |
|
| 336 | + if ($m[2]) { |
|
| 337 | + $colonnes = substr($m[2], 1, -1); |
|
| 338 | + $nom_index = $m[1]; |
|
| 339 | + } else { |
|
| 340 | + // (colonne) |
|
| 341 | + if ($m[1][0] == '(') { |
|
| 342 | + $colonnes = substr($m[1], 1, -1); |
|
| 343 | + if (str_contains(',', $colonnes)) { |
|
| 344 | + spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 345 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 346 | + } else { |
|
| 347 | + $nom_index = $colonnes; |
|
| 348 | + } |
|
| 349 | + } // nom_index |
|
| 350 | + else { |
|
| 351 | + $nom_index = $colonnes = $m[1]; |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 356 | + } |
|
| 357 | 357 | } |
| 358 | 358 | |
| 359 | 359 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 360 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', (string) $arg, $r)) { |
|
| 361 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 362 | - } else { |
|
| 363 | - if (!$r[1] || $r[1] == 'COLUMN') { |
|
| 364 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 365 | - } elseif ($r[1][0] == 'P') { |
|
| 366 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 367 | - } else { |
|
| 368 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 369 | - } |
|
| 370 | - } |
|
| 360 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', (string) $arg, $r)) { |
|
| 361 | + spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 362 | + } else { |
|
| 363 | + if (!$r[1] || $r[1] == 'COLUMN') { |
|
| 364 | + return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 365 | + } elseif ($r[1][0] == 'P') { |
|
| 366 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 367 | + } else { |
|
| 368 | + return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | 371 | } |
| 372 | 372 | |
| 373 | 373 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 374 | - if (!preg_match('/^`?(\w+)`?\s+(.*)$/', (string) $arg, $r)) { |
|
| 375 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 376 | - } else { |
|
| 377 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 378 | - } |
|
| 374 | + if (!preg_match('/^`?(\w+)`?\s+(.*)$/', (string) $arg, $r)) { |
|
| 375 | + spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 376 | + } else { |
|
| 377 | + return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 378 | + } |
|
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | // attention (en pg) : |
@@ -383,17 +383,17 @@ discard block |
||
| 383 | 383 | // - alter table A rename X to Y = changer le nom de la colonne X en Y |
| 384 | 384 | // pour l'instant, traiter simplement RENAME TO X |
| 385 | 385 | function spip_pg_alter_rename($table, $arg, $serveur = '', $requeter = true) { |
| 386 | - $rename = ''; |
|
| 387 | - // si TO, mais pas au debut |
|
| 388 | - if (!stripos((string) $arg, 'TO ')) { |
|
| 389 | - $rename = $arg; |
|
| 390 | - } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', (string) $arg, $r)) { |
|
| 391 | - $rename = $r[2]; |
|
| 392 | - } else { |
|
| 393 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 386 | + $rename = ''; |
|
| 387 | + // si TO, mais pas au debut |
|
| 388 | + if (!stripos((string) $arg, 'TO ')) { |
|
| 389 | + $rename = $arg; |
|
| 390 | + } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', (string) $arg, $r)) { |
|
| 391 | + $rename = $r[2]; |
|
| 392 | + } else { |
|
| 393 | + spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 397 | 397 | } |
| 398 | 398 | |
| 399 | 399 | |
@@ -409,58 +409,58 @@ discard block |
||
| 409 | 409 | * @return bool ou requete |
| 410 | 410 | */ |
| 411 | 411 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 412 | - if (!($nom || $table || $champs)) { |
|
| 413 | - spip_log( |
|
| 414 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @implode(',', $champs) . '))', |
|
| 415 | - 'pg.' . _LOG_ERREUR |
|
| 416 | - ); |
|
| 417 | - |
|
| 418 | - return false; |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - $nom = str_replace('`', '', $nom); |
|
| 422 | - $champs = str_replace('`', '', (string) $champs); |
|
| 423 | - |
|
| 424 | - // PG ne differentie pas noms des index en fonction des tables |
|
| 425 | - // il faut donc creer des noms uniques d'index pour une base pg |
|
| 426 | - $nom = $table . '_' . $nom; |
|
| 427 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 428 | - if (!is_array($champs)) { |
|
| 429 | - if ($champs[0] == '(') { |
|
| 430 | - $champs = substr($champs, 1, -1); |
|
| 431 | - } |
|
| 432 | - $champs = [$champs]; |
|
| 433 | - } |
|
| 434 | - $query = "CREATE INDEX $nom ON $table (" . implode(',', $champs) . ')'; |
|
| 435 | - if (!$requeter) { |
|
| 436 | - return $query; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - return spip_pg_query($query, $serveur, $requeter); |
|
| 412 | + if (!($nom || $table || $champs)) { |
|
| 413 | + spip_log( |
|
| 414 | + "Champ manquant pour creer un index pg ($nom, $table, (" . @implode(',', $champs) . '))', |
|
| 415 | + 'pg.' . _LOG_ERREUR |
|
| 416 | + ); |
|
| 417 | + |
|
| 418 | + return false; |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + $nom = str_replace('`', '', $nom); |
|
| 422 | + $champs = str_replace('`', '', (string) $champs); |
|
| 423 | + |
|
| 424 | + // PG ne differentie pas noms des index en fonction des tables |
|
| 425 | + // il faut donc creer des noms uniques d'index pour une base pg |
|
| 426 | + $nom = $table . '_' . $nom; |
|
| 427 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 428 | + if (!is_array($champs)) { |
|
| 429 | + if ($champs[0] == '(') { |
|
| 430 | + $champs = substr($champs, 1, -1); |
|
| 431 | + } |
|
| 432 | + $champs = [$champs]; |
|
| 433 | + } |
|
| 434 | + $query = "CREATE INDEX $nom ON $table (" . implode(',', $champs) . ')'; |
|
| 435 | + if (!$requeter) { |
|
| 436 | + return $query; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + return spip_pg_query($query, $serveur, $requeter); |
|
| 440 | 440 | } |
| 441 | 441 | |
| 442 | 442 | |
| 443 | 443 | function spip_pg_explain($query, $serveur = '', $requeter = true) { |
| 444 | - if (!str_starts_with(ltrim((string) $query), 'SELECT')) { |
|
| 445 | - return []; |
|
| 446 | - } |
|
| 447 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 448 | - $prefixe = $connexion['prefixe']; |
|
| 449 | - $link = $connexion['link']; |
|
| 450 | - if (preg_match('/\s(SET|VALUES|WHERE)\s/i', (string) $query, $regs)) { |
|
| 451 | - $suite = strstr((string) $query, (string) $regs[0]); |
|
| 452 | - $query = substr((string) $query, 0, -strlen($suite)); |
|
| 453 | - } else { |
|
| 454 | - $suite = ''; |
|
| 455 | - } |
|
| 456 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', (string) $query) . $suite; |
|
| 457 | - |
|
| 458 | - if (!$requeter) { |
|
| 459 | - return $query; |
|
| 460 | - } |
|
| 461 | - $r = spip_pg_query_simple($link, $query); |
|
| 462 | - |
|
| 463 | - return spip_pg_fetch($r, null, $serveur); |
|
| 444 | + if (!str_starts_with(ltrim((string) $query), 'SELECT')) { |
|
| 445 | + return []; |
|
| 446 | + } |
|
| 447 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 448 | + $prefixe = $connexion['prefixe']; |
|
| 449 | + $link = $connexion['link']; |
|
| 450 | + if (preg_match('/\s(SET|VALUES|WHERE)\s/i', (string) $query, $regs)) { |
|
| 451 | + $suite = strstr((string) $query, (string) $regs[0]); |
|
| 452 | + $query = substr((string) $query, 0, -strlen($suite)); |
|
| 453 | + } else { |
|
| 454 | + $suite = ''; |
|
| 455 | + } |
|
| 456 | + $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', (string) $query) . $suite; |
|
| 457 | + |
|
| 458 | + if (!$requeter) { |
|
| 459 | + return $query; |
|
| 460 | + } |
|
| 461 | + $r = spip_pg_query_simple($link, $query); |
|
| 462 | + |
|
| 463 | + return spip_pg_fetch($r, null, $serveur); |
|
| 464 | 464 | } |
| 465 | 465 | |
| 466 | 466 | |
@@ -479,88 +479,88 @@ discard block |
||
| 479 | 479 | * - False en cas d'erreur. |
| 480 | 480 | **/ |
| 481 | 481 | function spip_pg_selectdb($db, $serveur = '', $requeter = true) { |
| 482 | - // se connecter a la base indiquee |
|
| 483 | - // avec les identifiants connus |
|
| 484 | - $index = $serveur ? strtolower($serveur) : 0; |
|
| 482 | + // se connecter a la base indiquee |
|
| 483 | + // avec les identifiants connus |
|
| 484 | + $index = $serveur ? strtolower($serveur) : 0; |
|
| 485 | 485 | |
| 486 | - if (($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) && (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link)) { |
|
| 487 | - return $db; |
|
| 488 | - } |
|
| 486 | + if (($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) && (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link)) { |
|
| 487 | + return $db; |
|
| 488 | + } |
|
| 489 | 489 | |
| 490 | - return false; |
|
| 490 | + return false; |
|
| 491 | 491 | } |
| 492 | 492 | |
| 493 | 493 | // Qu'une seule base pour le moment |
| 494 | 494 | |
| 495 | 495 | function spip_pg_listdbs($serveur) { |
| 496 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 497 | - $link = $connexion['link']; |
|
| 498 | - $dbs = []; |
|
| 499 | - $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 500 | - while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 501 | - $dbs[] = reset($row); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - return $dbs; |
|
| 496 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 497 | + $link = $connexion['link']; |
|
| 498 | + $dbs = []; |
|
| 499 | + $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 500 | + while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 501 | + $dbs[] = reset($row); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + return $dbs; |
|
| 505 | 505 | } |
| 506 | 506 | |
| 507 | 507 | function spip_pg_select( |
| 508 | - $select, |
|
| 509 | - $from, |
|
| 510 | - $where = '', |
|
| 511 | - $groupby = [], |
|
| 512 | - $orderby = '', |
|
| 513 | - $limit = '', |
|
| 514 | - $having = '', |
|
| 515 | - $serveur = '', |
|
| 516 | - $requeter = true |
|
| 508 | + $select, |
|
| 509 | + $from, |
|
| 510 | + $where = '', |
|
| 511 | + $groupby = [], |
|
| 512 | + $orderby = '', |
|
| 513 | + $limit = '', |
|
| 514 | + $having = '', |
|
| 515 | + $serveur = '', |
|
| 516 | + $requeter = true |
|
| 517 | 517 | ) { |
| 518 | 518 | |
| 519 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 520 | - $prefixe = $connexion['prefixe']; |
|
| 521 | - $link = $connexion['link']; |
|
| 522 | - $db = $connexion['db']; |
|
| 523 | - |
|
| 524 | - $limit = preg_match('/^\s*((\d+),)?\s*(\d+)\s*$/', (string) $limit, $limatch); |
|
| 525 | - if ($limit) { |
|
| 526 | - $offset = $limatch[2]; |
|
| 527 | - $count = $limatch[3]; |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - $select = spip_pg_frommysql($select); |
|
| 531 | - |
|
| 532 | - // si pas de tri explicitement demande, le GROUP BY ne |
|
| 533 | - // contient que la clef primaire. |
|
| 534 | - // lui ajouter alors le champ de tri par defaut |
|
| 535 | - if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', (string) $orderby[0], $groupbyplus)) { |
|
| 536 | - $groupby[] = $groupbyplus[1]; |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - $orderby = spip_pg_orderby($orderby, $select); |
|
| 540 | - |
|
| 541 | - if ($having && is_array($having)) { |
|
| 542 | - $having = implode("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 543 | - } |
|
| 544 | - $from = spip_pg_from($from, $prefixe); |
|
| 545 | - $query = 'SELECT ' . $select |
|
| 546 | - . ($from ? "\nFROM $from" : '') |
|
| 547 | - . ($where ? "\nWHERE " . (is_array($where) ? implode( |
|
| 548 | - "\n\tAND ", |
|
| 549 | - array_map('calculer_pg_where', $where) |
|
| 550 | - ) : (calculer_pg_where($where))) : ('')) |
|
| 551 | - . spip_pg_groupby($groupby, $from, $select) |
|
| 552 | - . ($having ? "\nHAVING $having" : '') |
|
| 553 | - . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 554 | - . ($limit ? " LIMIT $count" . ($offset ? " OFFSET $offset" : '') : ('')); |
|
| 555 | - |
|
| 556 | - // renvoyer la requete inerte si demandee |
|
| 557 | - if ($requeter === false) { |
|
| 558 | - return $query; |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - $r = spip_pg_trace_query($query, $serveur); |
|
| 562 | - |
|
| 563 | - return $r ?: $query; |
|
| 519 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 520 | + $prefixe = $connexion['prefixe']; |
|
| 521 | + $link = $connexion['link']; |
|
| 522 | + $db = $connexion['db']; |
|
| 523 | + |
|
| 524 | + $limit = preg_match('/^\s*((\d+),)?\s*(\d+)\s*$/', (string) $limit, $limatch); |
|
| 525 | + if ($limit) { |
|
| 526 | + $offset = $limatch[2]; |
|
| 527 | + $count = $limatch[3]; |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + $select = spip_pg_frommysql($select); |
|
| 531 | + |
|
| 532 | + // si pas de tri explicitement demande, le GROUP BY ne |
|
| 533 | + // contient que la clef primaire. |
|
| 534 | + // lui ajouter alors le champ de tri par defaut |
|
| 535 | + if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', (string) $orderby[0], $groupbyplus)) { |
|
| 536 | + $groupby[] = $groupbyplus[1]; |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + $orderby = spip_pg_orderby($orderby, $select); |
|
| 540 | + |
|
| 541 | + if ($having && is_array($having)) { |
|
| 542 | + $having = implode("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 543 | + } |
|
| 544 | + $from = spip_pg_from($from, $prefixe); |
|
| 545 | + $query = 'SELECT ' . $select |
|
| 546 | + . ($from ? "\nFROM $from" : '') |
|
| 547 | + . ($where ? "\nWHERE " . (is_array($where) ? implode( |
|
| 548 | + "\n\tAND ", |
|
| 549 | + array_map('calculer_pg_where', $where) |
|
| 550 | + ) : (calculer_pg_where($where))) : ('')) |
|
| 551 | + . spip_pg_groupby($groupby, $from, $select) |
|
| 552 | + . ($having ? "\nHAVING $having" : '') |
|
| 553 | + . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 554 | + . ($limit ? " LIMIT $count" . ($offset ? " OFFSET $offset" : '') : ('')); |
|
| 555 | + |
|
| 556 | + // renvoyer la requete inerte si demandee |
|
| 557 | + if ($requeter === false) { |
|
| 558 | + return $query; |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + $r = spip_pg_trace_query($query, $serveur); |
|
| 562 | + |
|
| 563 | + return $r ?: $query; |
|
| 564 | 564 | ; |
| 565 | 565 | } |
| 566 | 566 | |
@@ -568,22 +568,22 @@ discard block |
||
| 568 | 568 | // car le reste de la requete utilise les alias (AS) systematiquement |
| 569 | 569 | |
| 570 | 570 | function spip_pg_from($from, $prefixe) { |
| 571 | - if (is_array($from)) { |
|
| 572 | - $from = spip_pg_select_as($from); |
|
| 573 | - } |
|
| 571 | + if (is_array($from)) { |
|
| 572 | + $from = spip_pg_select_as($from); |
|
| 573 | + } |
|
| 574 | 574 | |
| 575 | - return $prefixe ? preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', (string) $from) : $from; |
|
| 575 | + return $prefixe ? preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', (string) $from) : $from; |
|
| 576 | 576 | } |
| 577 | 577 | |
| 578 | 578 | function spip_pg_orderby($order, $select) { |
| 579 | - $res = []; |
|
| 580 | - $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', (string) $order)); |
|
| 579 | + $res = []; |
|
| 580 | + $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', (string) $order)); |
|
| 581 | 581 | |
| 582 | - foreach ($arg as $v) { |
|
| 583 | - $res[] = preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', (string) $select, $m) ? $m[1] : $v; |
|
| 584 | - } |
|
| 582 | + foreach ($arg as $v) { |
|
| 583 | + $res[] = preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', (string) $select, $m) ? $m[1] : $v; |
|
| 584 | + } |
|
| 585 | 585 | |
| 586 | - return spip_pg_frommysql(implode(',', $res)); |
|
| 586 | + return spip_pg_frommysql(implode(',', $res)); |
|
| 587 | 587 | } |
| 588 | 588 | |
| 589 | 589 | // Conversion a l'arrach' des jointures MySQL en jointures PG |
@@ -591,56 +591,56 @@ discard block |
||
| 591 | 591 | // et pour enlever les repetitions (sans incidence de perf, mais ca fait sale) |
| 592 | 592 | |
| 593 | 593 | function spip_pg_groupby($groupby, $from, $select) { |
| 594 | - $join = strpos((string) $from, ','); |
|
| 595 | - // ismplifier avant de decouper |
|
| 596 | - if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 597 | - $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - if ($join || $groupby) { |
|
| 601 | - $join = is_array($select) ? $select : explode(', ', (string) $select); |
|
| 602 | - } |
|
| 603 | - if ($join) { |
|
| 604 | - // enlever les 0 as points, '', ... |
|
| 605 | - foreach ($join as $k => $v) { |
|
| 606 | - $v = str_replace('DISTINCT ', '', (string) $v); |
|
| 607 | - // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 608 | - $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 609 | - $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 610 | - // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 611 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 612 | - // idem sans AS (fetch numerique) |
|
| 613 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 614 | - // des AS simples : on garde le cote droit du AS |
|
| 615 | - $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 616 | - // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 617 | - if (preg_match(',^[\'"],', $v) || is_numeric($v)) { |
|
| 618 | - unset($join[$k]); |
|
| 619 | - } else { |
|
| 620 | - $join[$k] = trim($v); |
|
| 621 | - } |
|
| 622 | - } |
|
| 623 | - $join = array_diff($join, ['']); |
|
| 624 | - $join = implode(',', $join); |
|
| 625 | - } |
|
| 626 | - if (is_array($groupby)) { |
|
| 627 | - $groupby = implode(',', $groupby); |
|
| 628 | - } |
|
| 629 | - if ($join) { |
|
| 630 | - $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 631 | - } |
|
| 632 | - if (!$groupby) { |
|
| 633 | - return ''; |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - $groupby = spip_pg_frommysql($groupby); |
|
| 637 | - // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 638 | - // issue de prepare_recherche |
|
| 639 | - $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', (string) $groupby); |
|
| 640 | - $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 641 | - $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 642 | - |
|
| 643 | - return "\nGROUP BY $groupby"; |
|
| 594 | + $join = strpos((string) $from, ','); |
|
| 595 | + // ismplifier avant de decouper |
|
| 596 | + if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 597 | + $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + if ($join || $groupby) { |
|
| 601 | + $join = is_array($select) ? $select : explode(', ', (string) $select); |
|
| 602 | + } |
|
| 603 | + if ($join) { |
|
| 604 | + // enlever les 0 as points, '', ... |
|
| 605 | + foreach ($join as $k => $v) { |
|
| 606 | + $v = str_replace('DISTINCT ', '', (string) $v); |
|
| 607 | + // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 608 | + $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 609 | + $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 610 | + // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 611 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 612 | + // idem sans AS (fetch numerique) |
|
| 613 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 614 | + // des AS simples : on garde le cote droit du AS |
|
| 615 | + $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 616 | + // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 617 | + if (preg_match(',^[\'"],', $v) || is_numeric($v)) { |
|
| 618 | + unset($join[$k]); |
|
| 619 | + } else { |
|
| 620 | + $join[$k] = trim($v); |
|
| 621 | + } |
|
| 622 | + } |
|
| 623 | + $join = array_diff($join, ['']); |
|
| 624 | + $join = implode(',', $join); |
|
| 625 | + } |
|
| 626 | + if (is_array($groupby)) { |
|
| 627 | + $groupby = implode(',', $groupby); |
|
| 628 | + } |
|
| 629 | + if ($join) { |
|
| 630 | + $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 631 | + } |
|
| 632 | + if (!$groupby) { |
|
| 633 | + return ''; |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + $groupby = spip_pg_frommysql($groupby); |
|
| 637 | + // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 638 | + // issue de prepare_recherche |
|
| 639 | + $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', (string) $groupby); |
|
| 640 | + $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 641 | + $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 642 | + |
|
| 643 | + return "\nGROUP BY $groupby"; |
|
| 644 | 644 | } |
| 645 | 645 | |
| 646 | 646 | // Conversion des operateurs MySQL en PG |
@@ -651,492 +651,492 @@ discard block |
||
| 651 | 651 | // A ameliorer. |
| 652 | 652 | |
| 653 | 653 | function spip_pg_frommysql($arg) { |
| 654 | - if (is_array($arg)) { |
|
| 655 | - $arg = implode(', ', $arg); |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - $res = spip_pg_fromfield($arg); |
|
| 659 | - |
|
| 660 | - $res = preg_replace('/\brand[(][)]/i', 'random()', (string) $res); |
|
| 661 | - |
|
| 662 | - $res = preg_replace( |
|
| 663 | - '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 664 | - 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 665 | - $res |
|
| 666 | - ); |
|
| 667 | - $res = preg_replace( |
|
| 668 | - '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 669 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 670 | - $res |
|
| 671 | - ); |
|
| 672 | - $res = preg_replace( |
|
| 673 | - '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 674 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 675 | - $res |
|
| 676 | - ); |
|
| 677 | - |
|
| 678 | - $res = preg_replace( |
|
| 679 | - '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 680 | - ' EXTRACT(epoch FROM NOW())', |
|
| 681 | - $res |
|
| 682 | - ); |
|
| 683 | - |
|
| 684 | - // la fonction md5(integer) n'est pas connu en pg |
|
| 685 | - // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 686 | - $res = preg_replace( |
|
| 687 | - '/md5\s*[(]([^)]*)[)]/i', |
|
| 688 | - 'MD5(CAST(\1 AS text))', |
|
| 689 | - $res |
|
| 690 | - ); |
|
| 691 | - |
|
| 692 | - $res = preg_replace( |
|
| 693 | - '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 694 | - ' EXTRACT(epoch FROM \1)', |
|
| 695 | - $res |
|
| 696 | - ); |
|
| 697 | - |
|
| 698 | - $res = preg_replace( |
|
| 699 | - '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 700 | - ' EXTRACT(day FROM \1)', |
|
| 701 | - $res |
|
| 702 | - ); |
|
| 703 | - |
|
| 704 | - $res = preg_replace( |
|
| 705 | - '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 706 | - ' EXTRACT(month FROM \1)', |
|
| 707 | - $res |
|
| 708 | - ); |
|
| 709 | - |
|
| 710 | - $res = preg_replace( |
|
| 711 | - '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 712 | - ' EXTRACT(year FROM \1)', |
|
| 713 | - $res |
|
| 714 | - ); |
|
| 715 | - |
|
| 716 | - $res = preg_replace( |
|
| 717 | - '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 718 | - ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 719 | - $res |
|
| 720 | - ); |
|
| 721 | - |
|
| 722 | - $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 723 | - |
|
| 724 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 725 | - |
|
| 726 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 727 | - |
|
| 728 | - $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 729 | - $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 730 | - $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 731 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 732 | - $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 733 | - |
|
| 734 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 735 | - $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 736 | - |
|
| 737 | - $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 738 | - $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 654 | + if (is_array($arg)) { |
|
| 655 | + $arg = implode(', ', $arg); |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + $res = spip_pg_fromfield($arg); |
|
| 659 | + |
|
| 660 | + $res = preg_replace('/\brand[(][)]/i', 'random()', (string) $res); |
|
| 661 | + |
|
| 662 | + $res = preg_replace( |
|
| 663 | + '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 664 | + 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 665 | + $res |
|
| 666 | + ); |
|
| 667 | + $res = preg_replace( |
|
| 668 | + '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 669 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 670 | + $res |
|
| 671 | + ); |
|
| 672 | + $res = preg_replace( |
|
| 673 | + '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 674 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 675 | + $res |
|
| 676 | + ); |
|
| 677 | + |
|
| 678 | + $res = preg_replace( |
|
| 679 | + '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 680 | + ' EXTRACT(epoch FROM NOW())', |
|
| 681 | + $res |
|
| 682 | + ); |
|
| 683 | + |
|
| 684 | + // la fonction md5(integer) n'est pas connu en pg |
|
| 685 | + // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 686 | + $res = preg_replace( |
|
| 687 | + '/md5\s*[(]([^)]*)[)]/i', |
|
| 688 | + 'MD5(CAST(\1 AS text))', |
|
| 689 | + $res |
|
| 690 | + ); |
|
| 691 | + |
|
| 692 | + $res = preg_replace( |
|
| 693 | + '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 694 | + ' EXTRACT(epoch FROM \1)', |
|
| 695 | + $res |
|
| 696 | + ); |
|
| 697 | + |
|
| 698 | + $res = preg_replace( |
|
| 699 | + '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 700 | + ' EXTRACT(day FROM \1)', |
|
| 701 | + $res |
|
| 702 | + ); |
|
| 703 | + |
|
| 704 | + $res = preg_replace( |
|
| 705 | + '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 706 | + ' EXTRACT(month FROM \1)', |
|
| 707 | + $res |
|
| 708 | + ); |
|
| 709 | + |
|
| 710 | + $res = preg_replace( |
|
| 711 | + '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 712 | + ' EXTRACT(year FROM \1)', |
|
| 713 | + $res |
|
| 714 | + ); |
|
| 715 | + |
|
| 716 | + $res = preg_replace( |
|
| 717 | + '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 718 | + ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 719 | + $res |
|
| 720 | + ); |
|
| 721 | + |
|
| 722 | + $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 723 | + |
|
| 724 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 725 | + |
|
| 726 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 727 | + |
|
| 728 | + $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 729 | + $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 730 | + $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 731 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 732 | + $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 733 | + |
|
| 734 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 735 | + $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 736 | + |
|
| 737 | + $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 738 | + $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 739 | 739 | # correct en theorie mais produit des debordements arithmetiques |
| 740 | 740 | # $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)(timestamp *'[^']*' *[+-] *timestamp *'[^']*') *[)]/", '\2', $res); |
| 741 | - $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 742 | - $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 741 | + $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 742 | + $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 743 | 743 | |
| 744 | - return str_replace('REGEXP', '~', $res); |
|
| 744 | + return str_replace('REGEXP', '~', $res); |
|
| 745 | 745 | } |
| 746 | 746 | |
| 747 | 747 | function spip_pg_fromfield($arg) { |
| 748 | - while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', (string) $arg, $m)) { |
|
| 749 | - preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 750 | - $res = ''; |
|
| 751 | - $n = 0; |
|
| 752 | - $index = $m[2]; |
|
| 753 | - foreach ($r[1] as $v) { |
|
| 754 | - $n++; |
|
| 755 | - $res .= "\nwhen $index=$v then $n"; |
|
| 756 | - } |
|
| 757 | - $arg = $m[1] . "case $res else 0 end " |
|
| 758 | - . substr((string) $arg, strlen($m[0])); |
|
| 759 | - } |
|
| 760 | - |
|
| 761 | - return $arg; |
|
| 748 | + while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', (string) $arg, $m)) { |
|
| 749 | + preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 750 | + $res = ''; |
|
| 751 | + $n = 0; |
|
| 752 | + $index = $m[2]; |
|
| 753 | + foreach ($r[1] as $v) { |
|
| 754 | + $n++; |
|
| 755 | + $res .= "\nwhen $index=$v then $n"; |
|
| 756 | + } |
|
| 757 | + $arg = $m[1] . "case $res else 0 end " |
|
| 758 | + . substr((string) $arg, strlen($m[0])); |
|
| 759 | + } |
|
| 760 | + |
|
| 761 | + return $arg; |
|
| 762 | 762 | } |
| 763 | 763 | |
| 764 | 764 | function calculer_pg_where($v) { |
| 765 | - if (!is_array($v)) { |
|
| 766 | - return spip_pg_frommysql($v); |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - $op = str_replace('REGEXP', '~', (string) array_shift($v)); |
|
| 770 | - if (!($n = count($v))) { |
|
| 771 | - return $op; |
|
| 772 | - } else { |
|
| 773 | - $arg = calculer_pg_where(array_shift($v)); |
|
| 774 | - if ($n == 1) { |
|
| 775 | - return "$op($arg)"; |
|
| 776 | - } else { |
|
| 777 | - $arg2 = calculer_pg_where(array_shift($v)); |
|
| 778 | - if ($n == 2) { |
|
| 779 | - return "($arg $op $arg2)"; |
|
| 780 | - } else { |
|
| 781 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 782 | - } |
|
| 783 | - } |
|
| 784 | - } |
|
| 765 | + if (!is_array($v)) { |
|
| 766 | + return spip_pg_frommysql($v); |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + $op = str_replace('REGEXP', '~', (string) array_shift($v)); |
|
| 770 | + if (!($n = count($v))) { |
|
| 771 | + return $op; |
|
| 772 | + } else { |
|
| 773 | + $arg = calculer_pg_where(array_shift($v)); |
|
| 774 | + if ($n == 1) { |
|
| 775 | + return "$op($arg)"; |
|
| 776 | + } else { |
|
| 777 | + $arg2 = calculer_pg_where(array_shift($v)); |
|
| 778 | + if ($n == 2) { |
|
| 779 | + return "($arg $op $arg2)"; |
|
| 780 | + } else { |
|
| 781 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 782 | + } |
|
| 783 | + } |
|
| 784 | + } |
|
| 785 | 785 | } |
| 786 | 786 | |
| 787 | 787 | |
| 788 | 788 | function calculer_pg_expression($expression, $v, $join = 'AND') { |
| 789 | - if (empty($v)) { |
|
| 790 | - return ''; |
|
| 791 | - } |
|
| 789 | + if (empty($v)) { |
|
| 790 | + return ''; |
|
| 791 | + } |
|
| 792 | 792 | |
| 793 | - $exp = "\n$expression "; |
|
| 793 | + $exp = "\n$expression "; |
|
| 794 | 794 | |
| 795 | - if (!is_array($v)) { |
|
| 796 | - $v = [$v]; |
|
| 797 | - } |
|
| 795 | + if (!is_array($v)) { |
|
| 796 | + $v = [$v]; |
|
| 797 | + } |
|
| 798 | 798 | |
| 799 | - if (strtoupper((string) $join) === 'AND') { |
|
| 800 | - return $exp . implode("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 801 | - } else { |
|
| 802 | - return $exp . implode($join, $v); |
|
| 803 | - } |
|
| 799 | + if (strtoupper((string) $join) === 'AND') { |
|
| 800 | + return $exp . implode("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 801 | + } else { |
|
| 802 | + return $exp . implode($join, $v); |
|
| 803 | + } |
|
| 804 | 804 | } |
| 805 | 805 | |
| 806 | 806 | function spip_pg_select_as($args) { |
| 807 | - $argsas = ''; |
|
| 808 | - foreach ($args as $k => $v) { |
|
| 809 | - if (str_ends_with((string) $k, '@')) { |
|
| 810 | - // c'est une jointure qui se refere au from precedent |
|
| 811 | - // pas de virgule |
|
| 812 | - $argsas .= ' ' . $v; |
|
| 813 | - } else { |
|
| 814 | - $as = ''; |
|
| 815 | - // spip_log("$k : $v", _LOG_DEBUG); |
|
| 816 | - if (!is_numeric($k)) { |
|
| 817 | - if (preg_match('/\.(.*)$/', (string) $k, $r)) { |
|
| 818 | - $v = $k; |
|
| 819 | - } elseif ($v != $k) { |
|
| 820 | - $p = strpos((string) $v, ' '); |
|
| 821 | - if ($p) { |
|
| 822 | - $v = substr((string) $v, 0, $p) . " AS $k" . substr((string) $v, $p); |
|
| 823 | - } else { |
|
| 824 | - $as = " AS $k"; |
|
| 825 | - } |
|
| 826 | - } |
|
| 827 | - } |
|
| 828 | - // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 829 | - // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 830 | - $argsas .= ', ' . $v . $as; |
|
| 831 | - } |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - return substr($argsas, 2); |
|
| 807 | + $argsas = ''; |
|
| 808 | + foreach ($args as $k => $v) { |
|
| 809 | + if (str_ends_with((string) $k, '@')) { |
|
| 810 | + // c'est une jointure qui se refere au from precedent |
|
| 811 | + // pas de virgule |
|
| 812 | + $argsas .= ' ' . $v; |
|
| 813 | + } else { |
|
| 814 | + $as = ''; |
|
| 815 | + // spip_log("$k : $v", _LOG_DEBUG); |
|
| 816 | + if (!is_numeric($k)) { |
|
| 817 | + if (preg_match('/\.(.*)$/', (string) $k, $r)) { |
|
| 818 | + $v = $k; |
|
| 819 | + } elseif ($v != $k) { |
|
| 820 | + $p = strpos((string) $v, ' '); |
|
| 821 | + if ($p) { |
|
| 822 | + $v = substr((string) $v, 0, $p) . " AS $k" . substr((string) $v, $p); |
|
| 823 | + } else { |
|
| 824 | + $as = " AS $k"; |
|
| 825 | + } |
|
| 826 | + } |
|
| 827 | + } |
|
| 828 | + // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 829 | + // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 830 | + $argsas .= ', ' . $v . $as; |
|
| 831 | + } |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + return substr($argsas, 2); |
|
| 835 | 835 | } |
| 836 | 836 | |
| 837 | 837 | function spip_pg_fetch($res, $t = '', $serveur = '', $requeter = true) { |
| 838 | 838 | |
| 839 | - if ($res) { |
|
| 840 | - $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 841 | - } |
|
| 839 | + if ($res) { |
|
| 840 | + $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 841 | + } |
|
| 842 | 842 | |
| 843 | - return $res; |
|
| 843 | + return $res; |
|
| 844 | 844 | } |
| 845 | 845 | |
| 846 | 846 | function spip_pg_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 847 | - if ($r) { |
|
| 848 | - return pg_result_seek($r, $row_number); |
|
| 849 | - } |
|
| 847 | + if ($r) { |
|
| 848 | + return pg_result_seek($r, $row_number); |
|
| 849 | + } |
|
| 850 | 850 | } |
| 851 | 851 | |
| 852 | 852 | |
| 853 | 853 | function spip_pg_countsel( |
| 854 | - $from = [], |
|
| 855 | - $where = [], |
|
| 856 | - $groupby = [], |
|
| 857 | - $having = [], |
|
| 858 | - $serveur = '', |
|
| 859 | - $requeter = true |
|
| 854 | + $from = [], |
|
| 855 | + $where = [], |
|
| 856 | + $groupby = [], |
|
| 857 | + $having = [], |
|
| 858 | + $serveur = '', |
|
| 859 | + $requeter = true |
|
| 860 | 860 | ) { |
| 861 | - $c = $groupby ? 'DISTINCT ' . (is_string($groupby) ? $groupby : implode(',', $groupby)) : ('*'); |
|
| 862 | - $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 863 | - if (!$requeter) { |
|
| 864 | - return $r; |
|
| 865 | - } |
|
| 866 | - if (!is_resource($r)) { |
|
| 867 | - return 0; |
|
| 868 | - } |
|
| 869 | - [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 870 | - |
|
| 871 | - return $c; |
|
| 861 | + $c = $groupby ? 'DISTINCT ' . (is_string($groupby) ? $groupby : implode(',', $groupby)) : ('*'); |
|
| 862 | + $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 863 | + if (!$requeter) { |
|
| 864 | + return $r; |
|
| 865 | + } |
|
| 866 | + if (!is_resource($r)) { |
|
| 867 | + return 0; |
|
| 868 | + } |
|
| 869 | + [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 870 | + |
|
| 871 | + return $c; |
|
| 872 | 872 | } |
| 873 | 873 | |
| 874 | 874 | function spip_pg_count($res, $serveur = '', $requeter = true) { |
| 875 | - return $res ? pg_num_rows($res) : 0; |
|
| 875 | + return $res ? pg_num_rows($res) : 0; |
|
| 876 | 876 | } |
| 877 | 877 | |
| 878 | 878 | function spip_pg_free($res, $serveur = '', $requeter = true) { |
| 879 | - // rien a faire en postgres |
|
| 879 | + // rien a faire en postgres |
|
| 880 | 880 | } |
| 881 | 881 | |
| 882 | 882 | function spip_pg_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 883 | 883 | |
| 884 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 885 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 884 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 885 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 886 | 886 | |
| 887 | - $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 888 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 887 | + $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 888 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 889 | 889 | |
| 890 | - // renvoyer la requete inerte si demandee |
|
| 891 | - if (!$requeter) { |
|
| 892 | - return $query; |
|
| 893 | - } |
|
| 890 | + // renvoyer la requete inerte si demandee |
|
| 891 | + if (!$requeter) { |
|
| 892 | + return $query; |
|
| 893 | + } |
|
| 894 | 894 | |
| 895 | - $res = spip_pg_trace_query($query, $serveur); |
|
| 896 | - if ($res) { |
|
| 897 | - return pg_affected_rows($res); |
|
| 898 | - } else { |
|
| 899 | - return false; |
|
| 900 | - } |
|
| 895 | + $res = spip_pg_trace_query($query, $serveur); |
|
| 896 | + if ($res) { |
|
| 897 | + return pg_affected_rows($res); |
|
| 898 | + } else { |
|
| 899 | + return false; |
|
| 900 | + } |
|
| 901 | 901 | } |
| 902 | 902 | |
| 903 | 903 | function spip_pg_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 904 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 905 | - $prefixe = $connexion['prefixe']; |
|
| 906 | - $link = $connexion['link']; |
|
| 907 | - |
|
| 908 | - if (!$desc) { |
|
| 909 | - $desc = description_table($table, $serveur); |
|
| 910 | - } |
|
| 911 | - $seq = spip_pg_sequence($table, true); |
|
| 912 | - // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 913 | - if (!preg_match(",\b$seq\b,", (string) $champs)) { |
|
| 914 | - $seq = spip_pg_sequence($table); |
|
| 915 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 916 | - $seq = "currval('$seq')"; |
|
| 917 | - } |
|
| 918 | - |
|
| 919 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 920 | - $ret = $seq ? " RETURNING $seq" : (''); |
|
| 921 | - $ins = (strlen((string) $champs) < 3) |
|
| 922 | - ? ' DEFAULT VALUES' |
|
| 923 | - : "$champs VALUES $valeurs"; |
|
| 924 | - $q = "INSERT INTO $table $ins $ret"; |
|
| 925 | - if (!$requeter) { |
|
| 926 | - return $q; |
|
| 927 | - } |
|
| 928 | - $connexion['last'] = $q; |
|
| 929 | - $r = spip_pg_query_simple($link, $q); |
|
| 904 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 905 | + $prefixe = $connexion['prefixe']; |
|
| 906 | + $link = $connexion['link']; |
|
| 907 | + |
|
| 908 | + if (!$desc) { |
|
| 909 | + $desc = description_table($table, $serveur); |
|
| 910 | + } |
|
| 911 | + $seq = spip_pg_sequence($table, true); |
|
| 912 | + // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 913 | + if (!preg_match(",\b$seq\b,", (string) $champs)) { |
|
| 914 | + $seq = spip_pg_sequence($table); |
|
| 915 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 916 | + $seq = "currval('$seq')"; |
|
| 917 | + } |
|
| 918 | + |
|
| 919 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 920 | + $ret = $seq ? " RETURNING $seq" : (''); |
|
| 921 | + $ins = (strlen((string) $champs) < 3) |
|
| 922 | + ? ' DEFAULT VALUES' |
|
| 923 | + : "$champs VALUES $valeurs"; |
|
| 924 | + $q = "INSERT INTO $table $ins $ret"; |
|
| 925 | + if (!$requeter) { |
|
| 926 | + return $q; |
|
| 927 | + } |
|
| 928 | + $connexion['last'] = $q; |
|
| 929 | + $r = spip_pg_query_simple($link, $q); |
|
| 930 | 930 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 931 | - if ($r) { |
|
| 932 | - if (!$ret) { |
|
| 933 | - return 0; |
|
| 934 | - } |
|
| 935 | - if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 936 | - return $r2[0]; |
|
| 937 | - } |
|
| 938 | - } |
|
| 939 | - |
|
| 940 | - return false; |
|
| 931 | + if ($r) { |
|
| 932 | + if (!$ret) { |
|
| 933 | + return 0; |
|
| 934 | + } |
|
| 935 | + if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 936 | + return $r2[0]; |
|
| 937 | + } |
|
| 938 | + } |
|
| 939 | + |
|
| 940 | + return false; |
|
| 941 | 941 | } |
| 942 | 942 | |
| 943 | 943 | function spip_pg_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 944 | 944 | |
| 945 | - if (!$desc) { |
|
| 946 | - $desc = description_table($table, $serveur); |
|
| 947 | - } |
|
| 948 | - if (!$desc) { |
|
| 949 | - die("$table insertion sans description"); |
|
| 950 | - } |
|
| 951 | - $fields = $desc['field']; |
|
| 952 | - |
|
| 953 | - foreach ($couples as $champ => $val) { |
|
| 954 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 955 | - } |
|
| 956 | - |
|
| 957 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 958 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 959 | - |
|
| 960 | - return spip_pg_insert( |
|
| 961 | - $table, |
|
| 962 | - '(' . implode(',', array_keys($couples)) . ')', |
|
| 963 | - '(' . implode(',', $couples) . ')', |
|
| 964 | - $desc, |
|
| 965 | - $serveur, |
|
| 966 | - $requeter |
|
| 967 | - ); |
|
| 945 | + if (!$desc) { |
|
| 946 | + $desc = description_table($table, $serveur); |
|
| 947 | + } |
|
| 948 | + if (!$desc) { |
|
| 949 | + die("$table insertion sans description"); |
|
| 950 | + } |
|
| 951 | + $fields = $desc['field']; |
|
| 952 | + |
|
| 953 | + foreach ($couples as $champ => $val) { |
|
| 954 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 955 | + } |
|
| 956 | + |
|
| 957 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 958 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 959 | + |
|
| 960 | + return spip_pg_insert( |
|
| 961 | + $table, |
|
| 962 | + '(' . implode(',', array_keys($couples)) . ')', |
|
| 963 | + '(' . implode(',', $couples) . ')', |
|
| 964 | + $desc, |
|
| 965 | + $serveur, |
|
| 966 | + $requeter |
|
| 967 | + ); |
|
| 968 | 968 | } |
| 969 | 969 | |
| 970 | 970 | |
| 971 | 971 | function spip_pg_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 972 | 972 | |
| 973 | - if (!$desc) { |
|
| 974 | - $desc = description_table($table, $serveur); |
|
| 975 | - } |
|
| 976 | - if (!$desc) { |
|
| 977 | - die("$table insertion sans description"); |
|
| 978 | - } |
|
| 979 | - $fields = $desc['field'] ?? []; |
|
| 980 | - |
|
| 981 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 982 | - // une premiere fois pour ajouter maj dans les cles |
|
| 983 | - $c = $tab_couples[0] ?? []; |
|
| 984 | - $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 985 | - |
|
| 986 | - $cles = '(' . implode(',', array_keys($les_cles)) . ')'; |
|
| 987 | - $valeurs = []; |
|
| 988 | - foreach ($tab_couples as $couples) { |
|
| 989 | - foreach ($couples as $champ => $val) { |
|
| 990 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 991 | - } |
|
| 992 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 993 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 994 | - |
|
| 995 | - $valeurs[] = '(' . implode(',', $couples) . ')'; |
|
| 996 | - } |
|
| 997 | - $valeurs = implode(', ', $valeurs); |
|
| 998 | - |
|
| 999 | - return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 973 | + if (!$desc) { |
|
| 974 | + $desc = description_table($table, $serveur); |
|
| 975 | + } |
|
| 976 | + if (!$desc) { |
|
| 977 | + die("$table insertion sans description"); |
|
| 978 | + } |
|
| 979 | + $fields = $desc['field'] ?? []; |
|
| 980 | + |
|
| 981 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 982 | + // une premiere fois pour ajouter maj dans les cles |
|
| 983 | + $c = $tab_couples[0] ?? []; |
|
| 984 | + $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 985 | + |
|
| 986 | + $cles = '(' . implode(',', array_keys($les_cles)) . ')'; |
|
| 987 | + $valeurs = []; |
|
| 988 | + foreach ($tab_couples as $couples) { |
|
| 989 | + foreach ($couples as $champ => $val) { |
|
| 990 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 991 | + } |
|
| 992 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 993 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 994 | + |
|
| 995 | + $valeurs[] = '(' . implode(',', $couples) . ')'; |
|
| 996 | + } |
|
| 997 | + $valeurs = implode(', ', $valeurs); |
|
| 998 | + |
|
| 999 | + return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1000 | 1000 | } |
| 1001 | 1001 | |
| 1002 | 1002 | |
| 1003 | 1003 | function spip_pg_update($table, $couples, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1004 | 1004 | |
| 1005 | - if (!$couples) { |
|
| 1006 | - return; |
|
| 1007 | - } |
|
| 1008 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 1009 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1005 | + if (!$couples) { |
|
| 1006 | + return; |
|
| 1007 | + } |
|
| 1008 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 1009 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1010 | 1010 | |
| 1011 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1012 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1011 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1012 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1013 | 1013 | |
| 1014 | - $set = []; |
|
| 1015 | - foreach ($couples as $champ => $val) { |
|
| 1016 | - $set[] = $champ . '=' . $val; |
|
| 1017 | - } |
|
| 1014 | + $set = []; |
|
| 1015 | + foreach ($couples as $champ => $val) { |
|
| 1016 | + $set[] = $champ . '=' . $val; |
|
| 1017 | + } |
|
| 1018 | 1018 | |
| 1019 | - $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1020 | - . calculer_pg_expression('SET', $set, ',') |
|
| 1021 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1019 | + $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1020 | + . calculer_pg_expression('SET', $set, ',') |
|
| 1021 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1022 | 1022 | |
| 1023 | - // renvoyer la requete inerte si demandee |
|
| 1024 | - if (!$requeter) { |
|
| 1025 | - return $query; |
|
| 1026 | - } |
|
| 1023 | + // renvoyer la requete inerte si demandee |
|
| 1024 | + if (!$requeter) { |
|
| 1025 | + return $query; |
|
| 1026 | + } |
|
| 1027 | 1027 | |
| 1028 | - return spip_pg_trace_query($query, $serveur); |
|
| 1028 | + return spip_pg_trace_query($query, $serveur); |
|
| 1029 | 1029 | } |
| 1030 | 1030 | |
| 1031 | 1031 | // idem, mais les valeurs sont des constantes a mettre entre apostrophes |
| 1032 | 1032 | // sauf les expressions de date lorsqu'il s'agit de fonctions SQL (NOW etc) |
| 1033 | 1033 | function spip_pg_updateq($table, $couples, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1034 | - if (!$couples) { |
|
| 1035 | - return; |
|
| 1036 | - } |
|
| 1037 | - if (!$desc) { |
|
| 1038 | - $desc = description_table($table, $serveur); |
|
| 1039 | - } |
|
| 1040 | - $fields = $desc['field']; |
|
| 1041 | - foreach ($couples as $k => $val) { |
|
| 1042 | - $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1043 | - } |
|
| 1044 | - |
|
| 1045 | - return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1034 | + if (!$couples) { |
|
| 1035 | + return; |
|
| 1036 | + } |
|
| 1037 | + if (!$desc) { |
|
| 1038 | + $desc = description_table($table, $serveur); |
|
| 1039 | + } |
|
| 1040 | + $fields = $desc['field']; |
|
| 1041 | + foreach ($couples as $k => $val) { |
|
| 1042 | + $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1043 | + } |
|
| 1044 | + |
|
| 1045 | + return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1046 | 1046 | } |
| 1047 | 1047 | |
| 1048 | 1048 | |
| 1049 | 1049 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1050 | - if (!$values) { |
|
| 1051 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1052 | - |
|
| 1053 | - return 0; |
|
| 1054 | - } |
|
| 1055 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 1056 | - $prefixe = $connexion['prefixe']; |
|
| 1057 | - $link = $connexion['link']; |
|
| 1058 | - |
|
| 1059 | - if (!$desc) { |
|
| 1060 | - $desc = description_table($table, $serveur); |
|
| 1061 | - } |
|
| 1062 | - if (!$desc) { |
|
| 1063 | - die("$table insertion sans description"); |
|
| 1064 | - } |
|
| 1065 | - $prim = $desc['key']['PRIMARY KEY']; |
|
| 1066 | - $ids = preg_split('/,\s*/', (string) $prim); |
|
| 1067 | - $noprims = $prims = []; |
|
| 1068 | - foreach ($values as $k => $v) { |
|
| 1069 | - $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1070 | - |
|
| 1071 | - if (!in_array($k, $ids)) { |
|
| 1072 | - $noprims[$k] = "$k=$v"; |
|
| 1073 | - } else { |
|
| 1074 | - $prims[$k] = "$k=$v"; |
|
| 1075 | - } |
|
| 1076 | - } |
|
| 1077 | - |
|
| 1078 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1079 | - $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1080 | - |
|
| 1081 | - $where = implode(' AND ', $prims); |
|
| 1082 | - if (!$where) { |
|
| 1083 | - return spip_pg_insert( |
|
| 1084 | - $table, |
|
| 1085 | - '(' . implode(',', array_keys($values)) . ')', |
|
| 1086 | - '(' . implode(',', $values) . ')', |
|
| 1087 | - $desc, |
|
| 1088 | - $serveur |
|
| 1089 | - ); |
|
| 1090 | - } |
|
| 1091 | - $couples = implode(',', $noprims); |
|
| 1092 | - |
|
| 1093 | - $seq = spip_pg_sequence($table); |
|
| 1094 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 1095 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1096 | - |
|
| 1097 | - $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1098 | - if ($couples) { |
|
| 1099 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1050 | + if (!$values) { |
|
| 1051 | + spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1052 | + |
|
| 1053 | + return 0; |
|
| 1054 | + } |
|
| 1055 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 1056 | + $prefixe = $connexion['prefixe']; |
|
| 1057 | + $link = $connexion['link']; |
|
| 1058 | + |
|
| 1059 | + if (!$desc) { |
|
| 1060 | + $desc = description_table($table, $serveur); |
|
| 1061 | + } |
|
| 1062 | + if (!$desc) { |
|
| 1063 | + die("$table insertion sans description"); |
|
| 1064 | + } |
|
| 1065 | + $prim = $desc['key']['PRIMARY KEY']; |
|
| 1066 | + $ids = preg_split('/,\s*/', (string) $prim); |
|
| 1067 | + $noprims = $prims = []; |
|
| 1068 | + foreach ($values as $k => $v) { |
|
| 1069 | + $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1070 | + |
|
| 1071 | + if (!in_array($k, $ids)) { |
|
| 1072 | + $noprims[$k] = "$k=$v"; |
|
| 1073 | + } else { |
|
| 1074 | + $prims[$k] = "$k=$v"; |
|
| 1075 | + } |
|
| 1076 | + } |
|
| 1077 | + |
|
| 1078 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1079 | + $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1080 | + |
|
| 1081 | + $where = implode(' AND ', $prims); |
|
| 1082 | + if (!$where) { |
|
| 1083 | + return spip_pg_insert( |
|
| 1084 | + $table, |
|
| 1085 | + '(' . implode(',', array_keys($values)) . ')', |
|
| 1086 | + '(' . implode(',', $values) . ')', |
|
| 1087 | + $desc, |
|
| 1088 | + $serveur |
|
| 1089 | + ); |
|
| 1090 | + } |
|
| 1091 | + $couples = implode(',', $noprims); |
|
| 1092 | + |
|
| 1093 | + $seq = spip_pg_sequence($table); |
|
| 1094 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 1095 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1096 | + |
|
| 1097 | + $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1098 | + if ($couples) { |
|
| 1099 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1100 | 1100 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 1101 | - if (!$couples) { |
|
| 1102 | - return false; |
|
| 1103 | - } |
|
| 1104 | - $couples = pg_affected_rows($couples); |
|
| 1105 | - } |
|
| 1106 | - if (!$couples) { |
|
| 1107 | - $ret = $seq ? " RETURNING nextval('$seq') < $prim" : |
|
| 1108 | - (''); |
|
| 1109 | - $connexion['last'] = $q = "INSERT INTO $table (" . implode(',', array_keys($values)) . ') VALUES (' . implode( |
|
| 1110 | - ',', |
|
| 1111 | - $values |
|
| 1112 | - ) . ")$ret"; |
|
| 1113 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1114 | - if (!$couples) { |
|
| 1115 | - return false; |
|
| 1116 | - } elseif ($ret) { |
|
| 1117 | - $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1118 | - if ($r[0]) { |
|
| 1119 | - $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1120 | - // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1121 | - // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1122 | - $r = @pg_query($link, $q); |
|
| 1123 | - } |
|
| 1124 | - } |
|
| 1125 | - } |
|
| 1126 | - |
|
| 1127 | - return $couples; |
|
| 1101 | + if (!$couples) { |
|
| 1102 | + return false; |
|
| 1103 | + } |
|
| 1104 | + $couples = pg_affected_rows($couples); |
|
| 1105 | + } |
|
| 1106 | + if (!$couples) { |
|
| 1107 | + $ret = $seq ? " RETURNING nextval('$seq') < $prim" : |
|
| 1108 | + (''); |
|
| 1109 | + $connexion['last'] = $q = "INSERT INTO $table (" . implode(',', array_keys($values)) . ') VALUES (' . implode( |
|
| 1110 | + ',', |
|
| 1111 | + $values |
|
| 1112 | + ) . ")$ret"; |
|
| 1113 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1114 | + if (!$couples) { |
|
| 1115 | + return false; |
|
| 1116 | + } elseif ($ret) { |
|
| 1117 | + $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1118 | + if ($r[0]) { |
|
| 1119 | + $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1120 | + // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1121 | + // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1122 | + $r = @pg_query($link, $q); |
|
| 1123 | + } |
|
| 1124 | + } |
|
| 1125 | + } |
|
| 1126 | + |
|
| 1127 | + return $couples; |
|
| 1128 | 1128 | } |
| 1129 | 1129 | |
| 1130 | 1130 | |
| 1131 | 1131 | function spip_pg_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1132 | - $retour = null; |
|
| 1133 | - // boucler pour traiter chaque requete independemment |
|
| 1134 | - foreach ($tab_couples as $couples) { |
|
| 1135 | - $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1136 | - } |
|
| 1137 | - |
|
| 1138 | - // renvoie le dernier id |
|
| 1139 | - return $retour; |
|
| 1132 | + $retour = null; |
|
| 1133 | + // boucler pour traiter chaque requete independemment |
|
| 1134 | + foreach ($tab_couples as $couples) { |
|
| 1135 | + $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1136 | + } |
|
| 1137 | + |
|
| 1138 | + // renvoie le dernier id |
|
| 1139 | + return $retour; |
|
| 1140 | 1140 | } |
| 1141 | 1141 | |
| 1142 | 1142 | |
@@ -1145,144 +1145,144 @@ discard block |
||
| 1145 | 1145 | |
| 1146 | 1146 | function spip_pg_sequence($table, $raw = false) { |
| 1147 | 1147 | |
| 1148 | - include_spip('base/serial'); |
|
| 1149 | - if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1150 | - return false; |
|
| 1151 | - } |
|
| 1152 | - $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1153 | - $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1154 | - if ( |
|
| 1155 | - !preg_match('/^\w+$/', (string) $prim) || !str_contains((string) $desc['field'][$prim], 'int') |
|
| 1156 | - ) { |
|
| 1157 | - return ''; |
|
| 1158 | - } else { |
|
| 1159 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1160 | - } |
|
| 1148 | + include_spip('base/serial'); |
|
| 1149 | + if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1150 | + return false; |
|
| 1151 | + } |
|
| 1152 | + $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1153 | + $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1154 | + if ( |
|
| 1155 | + !preg_match('/^\w+$/', (string) $prim) || !str_contains((string) $desc['field'][$prim], 'int') |
|
| 1156 | + ) { |
|
| 1157 | + return ''; |
|
| 1158 | + } else { |
|
| 1159 | + return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1160 | + } |
|
| 1161 | 1161 | } |
| 1162 | 1162 | |
| 1163 | 1163 | // Explicite les conversions de Mysql d'une valeur $v de type $t |
| 1164 | 1164 | // Dans le cas d'un champ date, pas d'apostrophe, c'est une syntaxe ad hoc |
| 1165 | 1165 | |
| 1166 | 1166 | function spip_pg_cite($v, $t) { |
| 1167 | - if (is_null($v)) { |
|
| 1168 | - return 'NULL'; |
|
| 1169 | - } // null php se traduit en NULL SQL |
|
| 1170 | - |
|
| 1171 | - if (sql_test_date($t)) { |
|
| 1172 | - if ($v && !str_contains('0123456789', (string) $v[0])) { |
|
| 1173 | - return spip_pg_frommysql($v); |
|
| 1174 | - } else { |
|
| 1175 | - if (str_starts_with((string) $v, '0000')) { |
|
| 1176 | - $v = '0001' . substr((string) $v, 4); |
|
| 1177 | - } |
|
| 1178 | - if (strpos((string) $v, '-00-00') === 4) { |
|
| 1179 | - $v = substr((string) $v, 0, 4) . '-01-01' . substr((string) $v, 10); |
|
| 1180 | - } |
|
| 1181 | - |
|
| 1182 | - return "timestamp '$v'"; |
|
| 1183 | - } |
|
| 1184 | - } elseif (!sql_test_int($t)) { |
|
| 1185 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1186 | - } elseif (is_numeric($v) || str_starts_with((string) $v, 'CAST(')) { |
|
| 1187 | - return $v; |
|
| 1188 | - } elseif ($v[0] == '0' && $v[1] !== 'x' && ctype_xdigit(substr((string) $v, 1))) { |
|
| 1189 | - return substr((string) $v, 1); |
|
| 1190 | - } else { |
|
| 1191 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1192 | - |
|
| 1193 | - return (int) $v; |
|
| 1194 | - } |
|
| 1167 | + if (is_null($v)) { |
|
| 1168 | + return 'NULL'; |
|
| 1169 | + } // null php se traduit en NULL SQL |
|
| 1170 | + |
|
| 1171 | + if (sql_test_date($t)) { |
|
| 1172 | + if ($v && !str_contains('0123456789', (string) $v[0])) { |
|
| 1173 | + return spip_pg_frommysql($v); |
|
| 1174 | + } else { |
|
| 1175 | + if (str_starts_with((string) $v, '0000')) { |
|
| 1176 | + $v = '0001' . substr((string) $v, 4); |
|
| 1177 | + } |
|
| 1178 | + if (strpos((string) $v, '-00-00') === 4) { |
|
| 1179 | + $v = substr((string) $v, 0, 4) . '-01-01' . substr((string) $v, 10); |
|
| 1180 | + } |
|
| 1181 | + |
|
| 1182 | + return "timestamp '$v'"; |
|
| 1183 | + } |
|
| 1184 | + } elseif (!sql_test_int($t)) { |
|
| 1185 | + return ("'" . pg_escape_string($v) . "'"); |
|
| 1186 | + } elseif (is_numeric($v) || str_starts_with((string) $v, 'CAST(')) { |
|
| 1187 | + return $v; |
|
| 1188 | + } elseif ($v[0] == '0' && $v[1] !== 'x' && ctype_xdigit(substr((string) $v, 1))) { |
|
| 1189 | + return substr((string) $v, 1); |
|
| 1190 | + } else { |
|
| 1191 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1192 | + |
|
| 1193 | + return (int) $v; |
|
| 1194 | + } |
|
| 1195 | 1195 | } |
| 1196 | 1196 | |
| 1197 | 1197 | function spip_pg_hex($v) { |
| 1198 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1198 | + return "CAST(x'" . $v . "' as bigint)"; |
|
| 1199 | 1199 | } |
| 1200 | 1200 | |
| 1201 | 1201 | function spip_pg_quote($v, $type = '') { |
| 1202 | - if (!is_array($v)) { |
|
| 1203 | - return spip_pg_cite($v, $type); |
|
| 1204 | - } |
|
| 1205 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1206 | - foreach ($v as $k => $r) { |
|
| 1207 | - $v[$k] = spip_pg_quote($r, $type); |
|
| 1208 | - } |
|
| 1209 | - |
|
| 1210 | - return implode(',', $v); |
|
| 1202 | + if (!is_array($v)) { |
|
| 1203 | + return spip_pg_cite($v, $type); |
|
| 1204 | + } |
|
| 1205 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1206 | + foreach ($v as $k => $r) { |
|
| 1207 | + $v[$k] = spip_pg_quote($r, $type); |
|
| 1208 | + } |
|
| 1209 | + |
|
| 1210 | + return implode(',', $v); |
|
| 1211 | 1211 | } |
| 1212 | 1212 | |
| 1213 | 1213 | function spip_pg_date_proche($champ, $interval, $unite) { |
| 1214 | - return '(' |
|
| 1215 | - . $champ |
|
| 1216 | - . (($interval <= 0) ? '>' : '<') |
|
| 1217 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1218 | - . '(' |
|
| 1219 | - . sql_quote(date('Y-m-d H:i:s')) |
|
| 1220 | - . ', INTERVAL ' |
|
| 1221 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1222 | - . ' ' |
|
| 1223 | - . $unite |
|
| 1224 | - . '))'; |
|
| 1214 | + return '(' |
|
| 1215 | + . $champ |
|
| 1216 | + . (($interval <= 0) ? '>' : '<') |
|
| 1217 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1218 | + . '(' |
|
| 1219 | + . sql_quote(date('Y-m-d H:i:s')) |
|
| 1220 | + . ', INTERVAL ' |
|
| 1221 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1222 | + . ' ' |
|
| 1223 | + . $unite |
|
| 1224 | + . '))'; |
|
| 1225 | 1225 | } |
| 1226 | 1226 | |
| 1227 | 1227 | function spip_pg_in($val, $valeurs, $not = '', $serveur = '') { |
| 1228 | 1228 | // |
| 1229 | 1229 | // IN (...) souvent limite a 255 elements, d'ou cette fonction assistante |
| 1230 | 1230 | // |
| 1231 | - // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1232 | - if (!$valeurs) { |
|
| 1233 | - return $not ? '0=0' : '0=1'; |
|
| 1234 | - } |
|
| 1235 | - if (str_contains((string) $valeurs, "CAST(x'")) { |
|
| 1236 | - return "($val=" . implode("OR $val=", explode(',', (string) $valeurs)) . ')'; |
|
| 1237 | - } |
|
| 1238 | - $n = $i = 0; |
|
| 1239 | - $in_sql = ''; |
|
| 1240 | - while ($n = strpos((string) $valeurs, ',', $n + 1)) { |
|
| 1241 | - if ((++$i) >= 255) { |
|
| 1242 | - $in_sql .= "($val $not IN (" . |
|
| 1243 | - substr((string) $valeurs, 0, $n) . |
|
| 1244 | - "))\n" . |
|
| 1245 | - ($not ? "AND\t" : "OR\t"); |
|
| 1246 | - $valeurs = substr((string) $valeurs, $n + 1); |
|
| 1247 | - $i = $n = 0; |
|
| 1248 | - } |
|
| 1249 | - } |
|
| 1250 | - $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1251 | - |
|
| 1252 | - return "($in_sql)"; |
|
| 1231 | + // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1232 | + if (!$valeurs) { |
|
| 1233 | + return $not ? '0=0' : '0=1'; |
|
| 1234 | + } |
|
| 1235 | + if (str_contains((string) $valeurs, "CAST(x'")) { |
|
| 1236 | + return "($val=" . implode("OR $val=", explode(',', (string) $valeurs)) . ')'; |
|
| 1237 | + } |
|
| 1238 | + $n = $i = 0; |
|
| 1239 | + $in_sql = ''; |
|
| 1240 | + while ($n = strpos((string) $valeurs, ',', $n + 1)) { |
|
| 1241 | + if ((++$i) >= 255) { |
|
| 1242 | + $in_sql .= "($val $not IN (" . |
|
| 1243 | + substr((string) $valeurs, 0, $n) . |
|
| 1244 | + "))\n" . |
|
| 1245 | + ($not ? "AND\t" : "OR\t"); |
|
| 1246 | + $valeurs = substr((string) $valeurs, $n + 1); |
|
| 1247 | + $i = $n = 0; |
|
| 1248 | + } |
|
| 1249 | + } |
|
| 1250 | + $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1251 | + |
|
| 1252 | + return "($in_sql)"; |
|
| 1253 | 1253 | } |
| 1254 | 1254 | |
| 1255 | 1255 | function spip_pg_error($query = '', $serveur = '', $requeter = true) { |
| 1256 | - $link = $GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]['link']; |
|
| 1257 | - $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1258 | - if ($s) { |
|
| 1259 | - $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1260 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1261 | - } |
|
| 1262 | - |
|
| 1263 | - return $s; |
|
| 1256 | + $link = $GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]['link']; |
|
| 1257 | + $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1258 | + if ($s) { |
|
| 1259 | + $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1260 | + spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1261 | + } |
|
| 1262 | + |
|
| 1263 | + return $s; |
|
| 1264 | 1264 | } |
| 1265 | 1265 | |
| 1266 | 1266 | function spip_pg_errno($serveur = '') { |
| 1267 | - // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1268 | - // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1269 | - return 0; |
|
| 1267 | + // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1268 | + // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1269 | + return 0; |
|
| 1270 | 1270 | } |
| 1271 | 1271 | |
| 1272 | 1272 | function spip_pg_drop_table($table, $exist = false, $serveur = '', $requeter = true) { |
| 1273 | - $exist = (bool) $exist ? ' IF EXISTS' : ''; |
|
| 1274 | - if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1275 | - return true; |
|
| 1276 | - } |
|
| 1273 | + $exist = (bool) $exist ? ' IF EXISTS' : ''; |
|
| 1274 | + if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1275 | + return true; |
|
| 1276 | + } |
|
| 1277 | 1277 | |
| 1278 | - return false; |
|
| 1278 | + return false; |
|
| 1279 | 1279 | } |
| 1280 | 1280 | |
| 1281 | 1281 | // supprime une vue |
| 1282 | 1282 | function spip_pg_drop_view($view, $exist = false, $serveur = '', $requeter = true) { |
| 1283 | - $exist = (bool) $exist ? ' IF EXISTS' : ''; |
|
| 1283 | + $exist = (bool) $exist ? ' IF EXISTS' : ''; |
|
| 1284 | 1284 | |
| 1285 | - return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1285 | + return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1286 | 1286 | } |
| 1287 | 1287 | |
| 1288 | 1288 | /** |
@@ -1299,40 +1299,40 @@ discard block |
||
| 1299 | 1299 | * Ressource à utiliser avec sql_fetch() |
| 1300 | 1300 | **/ |
| 1301 | 1301 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1302 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1303 | - $link = $connexion['link']; |
|
| 1304 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1302 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1303 | + $link = $connexion['link']; |
|
| 1304 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1305 | 1305 | |
| 1306 | - return spip_pg_query_simple($link, $q); |
|
| 1306 | + return spip_pg_query_simple($link, $q); |
|
| 1307 | 1307 | } |
| 1308 | 1308 | |
| 1309 | 1309 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1310 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 1311 | - $link = $connexion['link']; |
|
| 1312 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1313 | - |
|
| 1314 | - $res = spip_pg_query_simple($link, $q); |
|
| 1315 | - if (!$res) { |
|
| 1316 | - return false; |
|
| 1317 | - } |
|
| 1318 | - |
|
| 1319 | - // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1320 | - // il faut en tenir compte dans le return |
|
| 1321 | - $fields = []; |
|
| 1322 | - while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1323 | - $fields[$field[0]] = $field[2] . ($field[1] ? ' DEFAULT ' . $field[1] : ('')); |
|
| 1324 | - } |
|
| 1325 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1326 | - $res = spip_pg_query_simple($link, $q); |
|
| 1327 | - $keys = []; |
|
| 1328 | - while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1329 | - if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1330 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1331 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1332 | - } |
|
| 1333 | - } |
|
| 1334 | - |
|
| 1335 | - return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1310 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 1311 | + $link = $connexion['link']; |
|
| 1312 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1313 | + |
|
| 1314 | + $res = spip_pg_query_simple($link, $q); |
|
| 1315 | + if (!$res) { |
|
| 1316 | + return false; |
|
| 1317 | + } |
|
| 1318 | + |
|
| 1319 | + // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1320 | + // il faut en tenir compte dans le return |
|
| 1321 | + $fields = []; |
|
| 1322 | + while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1323 | + $fields[$field[0]] = $field[2] . ($field[1] ? ' DEFAULT ' . $field[1] : ('')); |
|
| 1324 | + } |
|
| 1325 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1326 | + $res = spip_pg_query_simple($link, $q); |
|
| 1327 | + $keys = []; |
|
| 1328 | + while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1329 | + if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1330 | + $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1331 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1332 | + } |
|
| 1333 | + } |
|
| 1334 | + |
|
| 1335 | + return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1336 | 1336 | } |
| 1337 | 1337 | |
| 1338 | 1338 | // Fonction de creation d'une table SQL nommee $nom |
@@ -1343,114 +1343,114 @@ discard block |
||
| 1343 | 1343 | // Le nom des index est prefixe par celui de la table pour eviter les conflits |
| 1344 | 1344 | function spip_pg_create($nom, $champs, $cles, $autoinc = false, $temporary = false, $serveur = '', $requeter = true) { |
| 1345 | 1345 | |
| 1346 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 1347 | - $link = $connexion['link']; |
|
| 1348 | - $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1349 | - |
|
| 1350 | - $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1351 | - $keys = []; |
|
| 1352 | - |
|
| 1353 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1354 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1355 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 1356 | - return; |
|
| 1357 | - } |
|
| 1358 | - |
|
| 1359 | - foreach ($cles as $k => $v) { |
|
| 1360 | - if (str_starts_with($k, 'KEY ')) { |
|
| 1361 | - $n = str_replace('`', '', $k); |
|
| 1362 | - $v = str_replace('`', '"', (string) $v); |
|
| 1363 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1364 | - if ($k != $n) { |
|
| 1365 | - $i = "\"$i\""; |
|
| 1366 | - } |
|
| 1367 | - $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1368 | - } elseif (str_starts_with($k, 'UNIQUE ')) { |
|
| 1369 | - $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1370 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1371 | - } else { |
|
| 1372 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1373 | - } |
|
| 1374 | - if ($k == 'PRIMARY KEY') { |
|
| 1375 | - $prim_name = $v; |
|
| 1376 | - } |
|
| 1377 | - $s = ','; |
|
| 1378 | - } |
|
| 1379 | - $s = ''; |
|
| 1380 | - |
|
| 1381 | - $character_set = ''; |
|
| 1382 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1383 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1384 | - } |
|
| 1385 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1386 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1387 | - } |
|
| 1388 | - |
|
| 1389 | - foreach ($champs as $k => $v) { |
|
| 1390 | - $k = str_replace('`', '"', $k); |
|
| 1391 | - if (preg_match(',([a-z]*\s*(\(\s*\d*\s*\))?(\s*binary)?),i', (string) $v, $defs) && (preg_match(',(char|text),i', $defs[1]) && !preg_match(',binary,i', $defs[1]))) { |
|
| 1392 | - $v = $defs[1] . $character_set . ' ' . substr((string) $v, strlen($defs[1])); |
|
| 1393 | - } |
|
| 1394 | - |
|
| 1395 | - $query .= "$s\n\t\t$k " |
|
| 1396 | - . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', (string) $v)) |
|
| 1397 | - ? ' bigserial' |
|
| 1398 | - : mysql2pg_type($v) |
|
| 1399 | - ); |
|
| 1400 | - $s = ','; |
|
| 1401 | - } |
|
| 1402 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1403 | - |
|
| 1404 | - // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1405 | - |
|
| 1406 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1407 | - ($character_set ? " DEFAULT $character_set" : '') |
|
| 1408 | - . "\n"; |
|
| 1409 | - |
|
| 1410 | - if (!$requeter) { |
|
| 1411 | - return $q; |
|
| 1412 | - } |
|
| 1413 | - $connexion['last'] = $q; |
|
| 1414 | - $r = @pg_query($link, $q); |
|
| 1415 | - |
|
| 1416 | - if (!$r) { |
|
| 1417 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1418 | - } else { |
|
| 1419 | - foreach ($keys as $index) { |
|
| 1420 | - pg_query($link, $index); |
|
| 1421 | - } |
|
| 1422 | - } |
|
| 1423 | - |
|
| 1424 | - return $r; |
|
| 1346 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 1347 | + $link = $connexion['link']; |
|
| 1348 | + $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1349 | + |
|
| 1350 | + $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1351 | + $keys = []; |
|
| 1352 | + |
|
| 1353 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1354 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1355 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 1356 | + return; |
|
| 1357 | + } |
|
| 1358 | + |
|
| 1359 | + foreach ($cles as $k => $v) { |
|
| 1360 | + if (str_starts_with($k, 'KEY ')) { |
|
| 1361 | + $n = str_replace('`', '', $k); |
|
| 1362 | + $v = str_replace('`', '"', (string) $v); |
|
| 1363 | + $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1364 | + if ($k != $n) { |
|
| 1365 | + $i = "\"$i\""; |
|
| 1366 | + } |
|
| 1367 | + $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1368 | + } elseif (str_starts_with($k, 'UNIQUE ')) { |
|
| 1369 | + $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1370 | + $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1371 | + } else { |
|
| 1372 | + $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1373 | + } |
|
| 1374 | + if ($k == 'PRIMARY KEY') { |
|
| 1375 | + $prim_name = $v; |
|
| 1376 | + } |
|
| 1377 | + $s = ','; |
|
| 1378 | + } |
|
| 1379 | + $s = ''; |
|
| 1380 | + |
|
| 1381 | + $character_set = ''; |
|
| 1382 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1383 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1384 | + } |
|
| 1385 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1386 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1387 | + } |
|
| 1388 | + |
|
| 1389 | + foreach ($champs as $k => $v) { |
|
| 1390 | + $k = str_replace('`', '"', $k); |
|
| 1391 | + if (preg_match(',([a-z]*\s*(\(\s*\d*\s*\))?(\s*binary)?),i', (string) $v, $defs) && (preg_match(',(char|text),i', $defs[1]) && !preg_match(',binary,i', $defs[1]))) { |
|
| 1392 | + $v = $defs[1] . $character_set . ' ' . substr((string) $v, strlen($defs[1])); |
|
| 1393 | + } |
|
| 1394 | + |
|
| 1395 | + $query .= "$s\n\t\t$k " |
|
| 1396 | + . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', (string) $v)) |
|
| 1397 | + ? ' bigserial' |
|
| 1398 | + : mysql2pg_type($v) |
|
| 1399 | + ); |
|
| 1400 | + $s = ','; |
|
| 1401 | + } |
|
| 1402 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1403 | + |
|
| 1404 | + // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1405 | + |
|
| 1406 | + $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1407 | + ($character_set ? " DEFAULT $character_set" : '') |
|
| 1408 | + . "\n"; |
|
| 1409 | + |
|
| 1410 | + if (!$requeter) { |
|
| 1411 | + return $q; |
|
| 1412 | + } |
|
| 1413 | + $connexion['last'] = $q; |
|
| 1414 | + $r = @pg_query($link, $q); |
|
| 1415 | + |
|
| 1416 | + if (!$r) { |
|
| 1417 | + spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1418 | + } else { |
|
| 1419 | + foreach ($keys as $index) { |
|
| 1420 | + pg_query($link, $index); |
|
| 1421 | + } |
|
| 1422 | + } |
|
| 1423 | + |
|
| 1424 | + return $r; |
|
| 1425 | 1425 | } |
| 1426 | 1426 | |
| 1427 | 1427 | |
| 1428 | 1428 | function spip_pg_create_base($nom, $serveur = '', $requeter = true) { |
| 1429 | - return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1429 | + return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1430 | 1430 | } |
| 1431 | 1431 | |
| 1432 | 1432 | // Fonction de creation d'une vue SQL nommee $nom |
| 1433 | 1433 | function spip_pg_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 1434 | - if (!$query_select) { |
|
| 1435 | - return false; |
|
| 1436 | - } |
|
| 1437 | - // vue deja presente |
|
| 1438 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 1439 | - if ($requeter) { |
|
| 1440 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1441 | - } |
|
| 1434 | + if (!$query_select) { |
|
| 1435 | + return false; |
|
| 1436 | + } |
|
| 1437 | + // vue deja presente |
|
| 1438 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 1439 | + if ($requeter) { |
|
| 1440 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1441 | + } |
|
| 1442 | 1442 | |
| 1443 | - return false; |
|
| 1444 | - } |
|
| 1443 | + return false; |
|
| 1444 | + } |
|
| 1445 | 1445 | |
| 1446 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1446 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1447 | 1447 | |
| 1448 | - return spip_pg_query($query, $serveur, $requeter); |
|
| 1448 | + return spip_pg_query($query, $serveur, $requeter); |
|
| 1449 | 1449 | } |
| 1450 | 1450 | |
| 1451 | 1451 | |
| 1452 | 1452 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1453 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1453 | + spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1454 | 1454 | } |
| 1455 | 1455 | |
| 1456 | 1456 | |
@@ -1463,48 +1463,48 @@ discard block |
||
| 1463 | 1463 | * @return bool|string true / false / requete |
| 1464 | 1464 | **/ |
| 1465 | 1465 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1466 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1466 | + return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1467 | 1467 | } |
| 1468 | 1468 | |
| 1469 | 1469 | // Selectionner la sous-chaine dans $objet |
| 1470 | 1470 | // correspondant a $lang. Cf balise Multi de Spip |
| 1471 | 1471 | |
| 1472 | 1472 | function spip_pg_multi($objet, $lang) { |
| 1473 | - return 'regexp_replace(' |
|
| 1474 | - . $objet |
|
| 1475 | - . ",'<multi>.*[[]" |
|
| 1476 | - . $lang |
|
| 1477 | - . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1473 | + return 'regexp_replace(' |
|
| 1474 | + . $objet |
|
| 1475 | + . ",'<multi>.*[[]" |
|
| 1476 | + . $lang |
|
| 1477 | + . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1478 | 1478 | } |
| 1479 | 1479 | |
| 1480 | 1480 | // Palanquee d'idiosyncrasies MySQL dans les creations de table |
| 1481 | 1481 | // A completer par les autres, mais essayer de reduire en amont. |
| 1482 | 1482 | |
| 1483 | 1483 | function mysql2pg_type($v) { |
| 1484 | - $remplace = [ |
|
| 1485 | - '/auto_increment/i' => '', // non reconnu |
|
| 1486 | - '/bigint/i' => 'bigint', |
|
| 1487 | - '/mediumint/i' => 'mediumint', |
|
| 1488 | - '/smallint/i' => 'smallint', |
|
| 1489 | - '/tinyint/i' => 'int', |
|
| 1490 | - '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1491 | - '/longtext/i' => 'text', |
|
| 1492 | - '/mediumtext/i' => 'text', |
|
| 1493 | - '/tinytext/i' => 'text', |
|
| 1494 | - '/longblob/i' => 'text', |
|
| 1495 | - '/0000-00-00/' => '0001-01-01', |
|
| 1496 | - '/datetime/i' => 'timestamp', |
|
| 1497 | - '/unsigned/i' => '', |
|
| 1498 | - '/double/i' => 'double precision', |
|
| 1499 | - '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1500 | - '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1501 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1502 | - ]; |
|
| 1503 | - |
|
| 1504 | - return preg_replace(array_keys($remplace), array_values($remplace), (string) $v); |
|
| 1484 | + $remplace = [ |
|
| 1485 | + '/auto_increment/i' => '', // non reconnu |
|
| 1486 | + '/bigint/i' => 'bigint', |
|
| 1487 | + '/mediumint/i' => 'mediumint', |
|
| 1488 | + '/smallint/i' => 'smallint', |
|
| 1489 | + '/tinyint/i' => 'int', |
|
| 1490 | + '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1491 | + '/longtext/i' => 'text', |
|
| 1492 | + '/mediumtext/i' => 'text', |
|
| 1493 | + '/tinytext/i' => 'text', |
|
| 1494 | + '/longblob/i' => 'text', |
|
| 1495 | + '/0000-00-00/' => '0001-01-01', |
|
| 1496 | + '/datetime/i' => 'timestamp', |
|
| 1497 | + '/unsigned/i' => '', |
|
| 1498 | + '/double/i' => 'double precision', |
|
| 1499 | + '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1500 | + '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1501 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1502 | + ]; |
|
| 1503 | + |
|
| 1504 | + return preg_replace(array_keys($remplace), array_values($remplace), (string) $v); |
|
| 1505 | 1505 | } |
| 1506 | 1506 | |
| 1507 | 1507 | // Renvoie false si on n'a pas les fonctions pg (pour l'install) |
| 1508 | 1508 | function spip_versions_pg() { |
| 1509 | - return function_exists('pg_connect'); |
|
| 1509 | + return function_exists('pg_connect'); |
|
| 1510 | 1510 | } |
@@ -65,35 +65,35 @@ discard block |
||
| 65 | 65 | // determiner le dossier de la base : $addr ou _DIR_DB |
| 66 | 66 | $f = _DIR_DB; |
| 67 | 67 | if ($addr && str_contains($addr, '/')) { |
| 68 | - $f = rtrim($addr, '/') . '/'; |
|
| 68 | + $f = rtrim($addr, '/').'/'; |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | // un nom de base demande et impossible d'obtenir la base, on s'en va : |
| 72 | 72 | // il faut que la base existe ou que le repertoire parent soit writable |
| 73 | - if ($db && !is_file($f .= $db . '.sqlite') && !is_writable(dirname($f))) { |
|
| 74 | - spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 73 | + if ($db && !is_file($f .= $db.'.sqlite') && !is_writable(dirname($f))) { |
|
| 74 | + spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.'._LOG_HS); |
|
| 75 | 75 | |
| 76 | 76 | return false; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | // charger les modules sqlite au besoin |
| 80 | 80 | if (!_sqlite_charger_version($sqlite_version)) { |
| 81 | - spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 81 | + spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.'._LOG_HS); |
|
| 82 | 82 | |
| 83 | 83 | return false; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | // chargement des constantes |
| 87 | 87 | // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
| 88 | - $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 88 | + $define = 'spip_sqlite'.$sqlite_version.'_constantes'; |
|
| 89 | 89 | $define(); |
| 90 | 90 | |
| 91 | 91 | if (!$db) { |
| 92 | 92 | // si pas de db -> |
| 93 | 93 | // base temporaire tant qu'on ne connait pas son vrai nom |
| 94 | 94 | // pour tester la connexion |
| 95 | - $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 96 | - $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 95 | + $db = '_sqlite'.$sqlite_version.'_install'; |
|
| 96 | + $tmp = _DIR_DB.$db.'.sqlite'; |
|
| 97 | 97 | $link = spip_sqlite_open($tmp); |
| 98 | 98 | } else { |
| 99 | 99 | // Ouvrir (eventuellement creer la base) |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | if (!$link) { |
| 104 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f", 'sqlite.' . _LOG_HS); |
|
| 104 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f", 'sqlite.'._LOG_HS); |
|
| 105 | 105 | |
| 106 | 106 | return false; |
| 107 | 107 | } |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | $table = $regs[3]; |
| 199 | 199 | $suite = $regs[4]; |
| 200 | 200 | } else { |
| 201 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 201 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.'._LOG_ERREUR); |
|
| 202 | 202 | |
| 203 | 203 | return false; |
| 204 | 204 | } |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | $i = 0; |
| 216 | 216 | $ouverte = false; |
| 217 | 217 | while ($do = array_shift($todo)) { |
| 218 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 218 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i].','.$do : $do; |
|
| 219 | 219 | $o = (str_contains($do, '(')); |
| 220 | 220 | $f = (str_contains($do, ')')); |
| 221 | 221 | if ($o && !$f) { |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | ) { |
| 242 | 242 | spip_log( |
| 243 | 243 | "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
| 244 | - 'sqlite.' . _LOG_ERREUR |
|
| 244 | + 'sqlite.'._LOG_ERREUR |
|
| 245 | 245 | ); |
| 246 | 246 | |
| 247 | 247 | return false; |
@@ -337,10 +337,10 @@ discard block |
||
| 337 | 337 | |
| 338 | 338 | // pas geres en sqlite2 |
| 339 | 339 | case 'RENAME': |
| 340 | - $do = 'RENAME TO' . substr($do, 6); |
|
| 340 | + $do = 'RENAME TO'.substr($do, 6); |
|
| 341 | 341 | case 'RENAME TO': |
| 342 | 342 | if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
| 343 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 343 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.'._LOG_ERREUR); |
|
| 344 | 344 | |
| 345 | 345 | return false; |
| 346 | 346 | } |
@@ -383,7 +383,7 @@ discard block |
||
| 383 | 383 | $colonnes = substr($colonne_origine, 1, -1); |
| 384 | 384 | if (str_contains(',', $colonnes)) { |
| 385 | 385 | spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
| 386 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 386 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.'._LOG_ERREUR); |
|
| 387 | 387 | break; |
| 388 | 388 | } else { |
| 389 | 389 | $nom_index = $colonnes; |
@@ -398,12 +398,12 @@ discard block |
||
| 398 | 398 | |
| 399 | 399 | // pas geres en sqlite2 |
| 400 | 400 | case 'ADD COLUMN': |
| 401 | - $do = 'ADD' . substr($do, 10); |
|
| 401 | + $do = 'ADD'.substr($do, 10); |
|
| 402 | 402 | case 'ADD': |
| 403 | 403 | default: |
| 404 | 404 | if (!preg_match(',primary\s+key,i', $do)) { |
| 405 | 405 | if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
| 406 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 406 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.'._LOG_ERREUR); |
|
| 407 | 407 | |
| 408 | 408 | return false; |
| 409 | 409 | } |
@@ -423,7 +423,7 @@ discard block |
||
| 423 | 423 | } |
| 424 | 424 | $opts['field'] = [$colonne_ajoutee => $def]; |
| 425 | 425 | if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
| 426 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 426 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.'._LOG_ERREUR); |
|
| 427 | 427 | |
| 428 | 428 | return false; |
| 429 | 429 | } |
@@ -431,10 +431,10 @@ discard block |
||
| 431 | 431 | break; |
| 432 | 432 | } |
| 433 | 433 | // tout est bon, ouf ! |
| 434 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 434 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.'._LOG_INFO); |
|
| 435 | 435 | } |
| 436 | 436 | |
| 437 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 437 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.'._LOG_INFO); |
|
| 438 | 438 | |
| 439 | 439 | return true; |
| 440 | 440 | } |
@@ -502,9 +502,9 @@ discard block |
||
| 502 | 502 | * @return bool true si la base est créee. |
| 503 | 503 | **/ |
| 504 | 504 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) { |
| 505 | - $f = $nom . '.sqlite'; |
|
| 505 | + $f = $nom.'.sqlite'; |
|
| 506 | 506 | if (!str_contains($nom, '/')) { |
| 507 | - $f = _DIR_DB . $f; |
|
| 507 | + $f = _DIR_DB.$f; |
|
| 508 | 508 | } |
| 509 | 509 | |
| 510 | 510 | $ok = new \PDO("sqlite:$f"); |
@@ -544,13 +544,13 @@ discard block |
||
| 544 | 544 | if (sql_showtable($nom, false, $serveur)) { |
| 545 | 545 | spip_log( |
| 546 | 546 | "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
| 547 | - 'sqlite.' . _LOG_ERREUR |
|
| 547 | + 'sqlite.'._LOG_ERREUR |
|
| 548 | 548 | ); |
| 549 | 549 | |
| 550 | 550 | return false; |
| 551 | 551 | } |
| 552 | 552 | |
| 553 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 553 | + $query = "CREATE VIEW $nom AS ".$query_select; |
|
| 554 | 554 | |
| 555 | 555 | return spip_sqlite_query($query, $serveur, $requeter); |
| 556 | 556 | } |
@@ -576,8 +576,8 @@ discard block |
||
| 576 | 576 | function spip_sqlite_create_index($nom, $table, $champs, $unique = '', $serveur = '', $requeter = true) { |
| 577 | 577 | if (!($nom || $table || $champs)) { |
| 578 | 578 | spip_log( |
| 579 | - "Champ manquant pour creer un index sqlite ($nom, $table, (" . implode(',', $champs) . '))', |
|
| 580 | - 'sqlite.' . _LOG_ERREUR |
|
| 579 | + "Champ manquant pour creer un index sqlite ($nom, $table, (".implode(',', $champs).'))', |
|
| 580 | + 'sqlite.'._LOG_ERREUR |
|
| 581 | 581 | ); |
| 582 | 582 | |
| 583 | 583 | return false; |
@@ -585,7 +585,7 @@ discard block |
||
| 585 | 585 | |
| 586 | 586 | // SQLite ne differentie pas noms des index en fonction des tables |
| 587 | 587 | // il faut donc creer des noms uniques d'index pour une base sqlite |
| 588 | - $nom = $table . '_' . $nom; |
|
| 588 | + $nom = $table.'_'.$nom; |
|
| 589 | 589 | // enlever d'eventuelles parentheses deja presentes sur champs |
| 590 | 590 | if (!is_array($champs)) { |
| 591 | 591 | if ($champs[0] == '(') { |
@@ -607,12 +607,12 @@ discard block |
||
| 607 | 607 | } else { |
| 608 | 608 | /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
| 609 | 609 | $a = spip_sqlite_showtable($table, $serveur); |
| 610 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 610 | + if (isset($a['key']['KEY '.$nom])) { |
|
| 611 | 611 | return true; |
| 612 | 612 | } |
| 613 | 613 | } |
| 614 | 614 | |
| 615 | - $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . implode(',', $champs) . ')'; |
|
| 615 | + $query = 'CREATE '.($unique ? 'UNIQUE ' : '')."INDEX$ifnotexists $nom ON $table (".implode(',', $champs).')'; |
|
| 616 | 616 | $res = spip_sqlite_query($query, $serveur, $requeter); |
| 617 | 617 | if (!$requeter) { |
| 618 | 618 | return $res; |
@@ -680,7 +680,7 @@ discard block |
||
| 680 | 680 | $serveur = '', |
| 681 | 681 | $requeter = true |
| 682 | 682 | ) { |
| 683 | - $c = $groupby ? 'DISTINCT ' . (is_string($groupby) ? $groupby : implode(',', $groupby)) : ('*'); |
|
| 683 | + $c = $groupby ? 'DISTINCT '.(is_string($groupby) ? $groupby : implode(',', $groupby)) : ('*'); |
|
| 684 | 684 | $r = spip_sqlite_select( |
| 685 | 685 | "COUNT($c)", |
| 686 | 686 | $from, |
@@ -790,14 +790,14 @@ discard block |
||
| 790 | 790 | */ |
| 791 | 791 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) { |
| 792 | 792 | if (!$nom && !$table) { |
| 793 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 793 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.'._LOG_ERREUR); |
|
| 794 | 794 | |
| 795 | 795 | return false; |
| 796 | 796 | } |
| 797 | 797 | |
| 798 | 798 | // SQLite ne differentie pas noms des index en fonction des tables |
| 799 | 799 | // il faut donc creer des noms uniques d'index pour une base sqlite |
| 800 | - $index = $table . '_' . $nom; |
|
| 800 | + $index = $table.'_'.$nom; |
|
| 801 | 801 | $exist = ' IF EXISTS'; |
| 802 | 802 | |
| 803 | 803 | $query = "DROP INDEX$exist $index"; |
@@ -829,7 +829,7 @@ discard block |
||
| 829 | 829 | if ($s) { |
| 830 | 830 | $trace = debug_backtrace(); |
| 831 | 831 | if ($trace[0]['function'] != 'spip_sqlite_error') { |
| 832 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 832 | + spip_log("$s - $query - ".sql_error_backtrace(), 'sqlite.'._LOG_ERREUR); |
|
| 833 | 833 | } |
| 834 | 834 | } |
| 835 | 835 | |
@@ -876,14 +876,14 @@ discard block |
||
| 876 | 876 | $t = $link->errorInfo(); |
| 877 | 877 | $s = ltrim((string) $t[0], '0'); // 00000 si pas d'erreur |
| 878 | 878 | if ($s) { |
| 879 | - $s .= ' / ' . $t[1]; |
|
| 879 | + $s .= ' / '.$t[1]; |
|
| 880 | 880 | } // ajoute l'erreur du moteur SQLite |
| 881 | 881 | } else { |
| 882 | 882 | $s = ': aucune ressource sqlite (link)'; |
| 883 | 883 | } |
| 884 | 884 | |
| 885 | 885 | if ($s) { |
| 886 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 886 | + spip_log("Erreur sqlite $s", 'sqlite.'._LOG_ERREUR); |
|
| 887 | 887 | } |
| 888 | 888 | |
| 889 | 889 | return $s ?: 0; |
@@ -907,7 +907,7 @@ discard block |
||
| 907 | 907 | } |
| 908 | 908 | |
| 909 | 909 | $query = Sqlite::traduire_requete($query, $serveur); |
| 910 | - $query = 'EXPLAIN ' . $query; |
|
| 910 | + $query = 'EXPLAIN '.$query; |
|
| 911 | 911 | if (!$requeter) { |
| 912 | 912 | return $query; |
| 913 | 913 | } |
@@ -1076,7 +1076,7 @@ discard block |
||
| 1076 | 1076 | **/ |
| 1077 | 1077 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 1078 | 1078 | |
| 1079 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1079 | + $query = "INSERT INTO $table ".($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1080 | 1080 | if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
| 1081 | 1081 | if (!$requeter) { |
| 1082 | 1082 | return $r; |
@@ -1131,8 +1131,8 @@ discard block |
||
| 1131 | 1131 | |
| 1132 | 1132 | $cles = $valeurs = ''; |
| 1133 | 1133 | if (is_countable($couples) ? count($couples) : 0) { |
| 1134 | - $cles = '(' . implode(',', array_keys($couples)) . ')'; |
|
| 1135 | - $valeurs = '(' . implode(',', $couples) . ')'; |
|
| 1134 | + $cles = '('.implode(',', array_keys($couples)).')'; |
|
| 1135 | + $valeurs = '('.implode(',', $couples).')'; |
|
| 1136 | 1136 | } |
| 1137 | 1137 | |
| 1138 | 1138 | return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
@@ -1194,11 +1194,11 @@ discard block |
||
| 1194 | 1194 | |
| 1195 | 1195 | $champs = $valeurs = ''; |
| 1196 | 1196 | if ($couples !== []) { |
| 1197 | - $champs = '(' . implode(',', array_keys($couples)) . ')'; |
|
| 1198 | - $valeurs = '(' . implode(',', $couples) . ')'; |
|
| 1199 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1197 | + $champs = '('.implode(',', array_keys($couples)).')'; |
|
| 1198 | + $valeurs = '('.implode(',', $couples).')'; |
|
| 1199 | + $query = $query_start."$champs VALUES $valeurs"; |
|
| 1200 | 1200 | } else { |
| 1201 | - $query = $query_start . 'DEFAULT VALUES'; |
|
| 1201 | + $query = $query_start.'DEFAULT VALUES'; |
|
| 1202 | 1202 | } |
| 1203 | 1203 | |
| 1204 | 1204 | if ($requeter) { |
@@ -1330,7 +1330,7 @@ discard block |
||
| 1330 | 1330 | * @return string texte de sélection pour la requête |
| 1331 | 1331 | */ |
| 1332 | 1332 | function spip_sqlite_multi($objet, $lang) { |
| 1333 | - return 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1333 | + return 'EXTRAIRE_MULTI('.$objet.", '".$lang."') AS multi"; |
|
| 1334 | 1334 | } |
| 1335 | 1335 | |
| 1336 | 1336 | |
@@ -1399,7 +1399,7 @@ discard block |
||
| 1399 | 1399 | function spip_sqlite_date_proche($champ, $interval, $unite) { |
| 1400 | 1400 | $op = (($interval <= 0) ? '>' : '<'); |
| 1401 | 1401 | |
| 1402 | - return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1402 | + return "($champ $op datetime('".date('Y-m-d H:i:s')."', '$interval $unite'))"; |
|
| 1403 | 1403 | } |
| 1404 | 1404 | |
| 1405 | 1405 | |
@@ -1431,7 +1431,7 @@ discard block |
||
| 1431 | 1431 | && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
| 1432 | 1432 | ) { |
| 1433 | 1433 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
| 1434 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1434 | + spip_log("ALTER $q", 'repair'._LOG_INFO_IMPORTANTE); |
|
| 1435 | 1435 | } |
| 1436 | 1436 | if ( |
| 1437 | 1437 | preg_match(',^(INTEGER),i', (string) $d) |
@@ -1441,7 +1441,7 @@ discard block |
||
| 1441 | 1441 | && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
| 1442 | 1442 | ) { |
| 1443 | 1443 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
| 1444 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1444 | + spip_log("ALTER $q", 'repair'._LOG_INFO_IMPORTANTE); |
|
| 1445 | 1445 | } |
| 1446 | 1446 | if ( |
| 1447 | 1447 | preg_match(',^(datetime),i', (string) $d) |
@@ -1451,7 +1451,7 @@ discard block |
||
| 1451 | 1451 | && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
| 1452 | 1452 | ) { |
| 1453 | 1453 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
| 1454 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1454 | + spip_log("ALTER $q", 'repair'._LOG_INFO_IMPORTANTE); |
|
| 1455 | 1455 | } |
| 1456 | 1456 | } |
| 1457 | 1457 | |
@@ -1502,10 +1502,10 @@ discard block |
||
| 1502 | 1502 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
| 1503 | 1503 | $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
| 1504 | 1504 | |
| 1505 | - return spip_sqlite_query("REPLACE INTO $table (" . implode(',', array_keys($couples)) . ') VALUES (' . implode( |
|
| 1505 | + return spip_sqlite_query("REPLACE INTO $table (".implode(',', array_keys($couples)).') VALUES ('.implode( |
|
| 1506 | 1506 | ',', |
| 1507 | 1507 | $couples |
| 1508 | - ) . ')', $serveur); |
|
| 1508 | + ).')', $serveur); |
|
| 1509 | 1509 | } |
| 1510 | 1510 | |
| 1511 | 1511 | |
@@ -1592,7 +1592,7 @@ discard block |
||
| 1592 | 1592 | . _sqlite_calculer_expression('WHERE', $where) |
| 1593 | 1593 | . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
| 1594 | 1594 | . _sqlite_calculer_expression('HAVING', $having) |
| 1595 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1595 | + . ($orderby ? ("\nORDER BY "._sqlite_calculer_order($orderby)) : '') |
|
| 1596 | 1596 | . ($limit ? "\nLIMIT $limit" : ''); |
| 1597 | 1597 | |
| 1598 | 1598 | // dans un select, on doit renvoyer la requête en cas d'erreur |
@@ -1630,10 +1630,10 @@ discard block |
||
| 1630 | 1630 | // interdire la creation d'une nouvelle base, |
| 1631 | 1631 | // sauf si on est dans l'installation |
| 1632 | 1632 | if ( |
| 1633 | - !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1633 | + !is_file($f = _DIR_DB.$db.'.sqlite') |
|
| 1634 | 1634 | && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
| 1635 | 1635 | ) { |
| 1636 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1636 | + spip_log("Il est interdit de creer la base $db", 'sqlite.'._LOG_HS); |
|
| 1637 | 1637 | |
| 1638 | 1638 | return false; |
| 1639 | 1639 | } |
@@ -1642,12 +1642,12 @@ discard block |
||
| 1642 | 1642 | // avec les identifiants connus |
| 1643 | 1643 | $index = $serveur ?: 0; |
| 1644 | 1644 | |
| 1645 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1645 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@'.$db, $serveur, '', '')) { |
|
| 1646 | 1646 | if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
| 1647 | 1647 | return $db; |
| 1648 | 1648 | } |
| 1649 | 1649 | } else { |
| 1650 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1650 | + spip_log("Impossible de selectionner la base $db", 'sqlite.'._LOG_HS); |
|
| 1651 | 1651 | } |
| 1652 | 1652 | |
| 1653 | 1653 | return false; |
@@ -1698,7 +1698,7 @@ discard block |
||
| 1698 | 1698 | $match = "^$match$"; |
| 1699 | 1699 | |
| 1700 | 1700 | return spip_sqlite_query( |
| 1701 | - "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1701 | + "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP "._q($match), |
|
| 1702 | 1702 | $serveur, |
| 1703 | 1703 | $requeter |
| 1704 | 1704 | ); |
@@ -1722,7 +1722,7 @@ discard block |
||
| 1722 | 1722 | $r = spip_sqlite_query( |
| 1723 | 1723 | 'SELECT name FROM sqlite_master WHERE' |
| 1724 | 1724 | . ' type=\'table\'' |
| 1725 | - . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1725 | + . ' AND name='.spip_sqlite_quote($table, 'string') |
|
| 1726 | 1726 | . ' AND name NOT LIKE \'sqlite_%\'', |
| 1727 | 1727 | $serveur, |
| 1728 | 1728 | $requeter |
@@ -1819,7 +1819,7 @@ discard block |
||
| 1819 | 1819 | // s'il y a une parenthèse fermante dans la clé |
| 1820 | 1820 | // ou dans la définition sans qu'il n'y ait une ouverture avant |
| 1821 | 1821 | if (str_contains($k, ')') || preg_match('/^[^\(]*\)/', (string) $def)) { |
| 1822 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1822 | + $fields[$k_precedent] .= ','.$k.' '.$def; |
|
| 1823 | 1823 | continue; |
| 1824 | 1824 | } |
| 1825 | 1825 | |
@@ -1854,13 +1854,13 @@ discard block |
||
| 1854 | 1854 | . 'ORDER BY substr(type,2,1), name'; |
| 1855 | 1855 | $a = spip_sqlite_query($query, $serveur, $requeter); |
| 1856 | 1856 | while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
| 1857 | - $key = str_replace($nom_table . '_', '', (string) $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1857 | + $key = str_replace($nom_table.'_', '', (string) $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1858 | 1858 | $keytype = 'KEY'; |
| 1859 | 1859 | if (str_contains((string) $r['sql'], 'UNIQUE INDEX')) { |
| 1860 | 1860 | $keytype = 'UNIQUE KEY'; |
| 1861 | 1861 | } |
| 1862 | 1862 | $colonnes = preg_replace(',.*\((.*)\).*,', '$1', (string) $r['sql']); |
| 1863 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1863 | + $keys[$keytype.' '.$key] = $colonnes; |
|
| 1864 | 1864 | } |
| 1865 | 1865 | } |
| 1866 | 1866 | } // c'est une vue, on liste les champs disponibles simplement |
@@ -1907,7 +1907,7 @@ discard block |
||
| 1907 | 1907 | |
| 1908 | 1908 | $set = []; |
| 1909 | 1909 | foreach ($champs as $champ => $val) { |
| 1910 | - $set[] = $champ . "=$val"; |
|
| 1910 | + $set[] = $champ."=$val"; |
|
| 1911 | 1911 | } |
| 1912 | 1912 | if ($set !== []) { |
| 1913 | 1913 | return spip_sqlite_query( |
@@ -1962,7 +1962,7 @@ discard block |
||
| 1962 | 1962 | |
| 1963 | 1963 | $set = []; |
| 1964 | 1964 | foreach ($champs as $champ => $val) { |
| 1965 | - $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, $fields[$champ] ?? ''); |
|
| 1965 | + $set[$champ] = $champ.'='._sqlite_calculer_cite($val, $fields[$champ] ?? ''); |
|
| 1966 | 1966 | } |
| 1967 | 1967 | |
| 1968 | 1968 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
@@ -1970,7 +1970,7 @@ discard block |
||
| 1970 | 1970 | $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
| 1971 | 1971 | foreach ($maj as $champ => $val) { |
| 1972 | 1972 | if (!isset($set[$champ])) { |
| 1973 | - $set[$champ] = $champ . '=' . $val; |
|
| 1973 | + $set[$champ] = $champ.'='.$val; |
|
| 1974 | 1974 | } |
| 1975 | 1975 | } |
| 1976 | 1976 | |
@@ -1999,7 +1999,7 @@ discard block |
||
| 1999 | 1999 | */ |
| 2000 | 2000 | function _sqlite_init() { |
| 2001 | 2001 | if (!defined('_DIR_DB')) { |
| 2002 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2002 | + define('_DIR_DB', _DIR_ETC.'bases/'); |
|
| 2003 | 2003 | } |
| 2004 | 2004 | if (!defined('_SQLITE_CHMOD')) { |
| 2005 | 2005 | define('_SQLITE_CHMOD', _SPIP_CHMOD); |
@@ -2105,9 +2105,9 @@ discard block |
||
| 2105 | 2105 | } |
| 2106 | 2106 | |
| 2107 | 2107 | // echapper les ' en '' |
| 2108 | - spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2108 | + spip_log('Pas de methode ->quote pour echapper', 'sqlite.'._LOG_INFO_IMPORTANTE); |
|
| 2109 | 2109 | |
| 2110 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2110 | + return ("'".str_replace("'", "''", $v)."'"); |
|
| 2111 | 2111 | } |
| 2112 | 2112 | |
| 2113 | 2113 | |
@@ -2130,12 +2130,12 @@ discard block |
||
| 2130 | 2130 | $exp = "\n$expression "; |
| 2131 | 2131 | |
| 2132 | 2132 | if (!is_array($v)) { |
| 2133 | - return $exp . $v; |
|
| 2133 | + return $exp.$v; |
|
| 2134 | 2134 | } else { |
| 2135 | 2135 | if (strtoupper($join) === 'AND') { |
| 2136 | - return $exp . implode("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2136 | + return $exp.implode("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2137 | 2137 | } else { |
| 2138 | - return $exp . implode($join, $v); |
|
| 2138 | + return $exp.implode($join, $v); |
|
| 2139 | 2139 | } |
| 2140 | 2140 | } |
| 2141 | 2141 | } |
@@ -2169,17 +2169,17 @@ discard block |
||
| 2169 | 2169 | if (str_ends_with($k, '@')) { |
| 2170 | 2170 | // c'est une jointure qui se refere au from precedent |
| 2171 | 2171 | // pas de virgule |
| 2172 | - $res .= ' ' . $v; |
|
| 2172 | + $res .= ' '.$v; |
|
| 2173 | 2173 | } else { |
| 2174 | 2174 | if (!is_numeric($k)) { |
| 2175 | 2175 | $p = strpos((string) $v, ' '); |
| 2176 | 2176 | if ($p) { |
| 2177 | - $v = substr((string) $v, 0, $p) . " AS '$k'" . substr((string) $v, $p); |
|
| 2177 | + $v = substr((string) $v, 0, $p)." AS '$k'".substr((string) $v, $p); |
|
| 2178 | 2178 | } else { |
| 2179 | 2179 | $v .= " AS '$k'"; |
| 2180 | 2180 | } |
| 2181 | 2181 | } |
| 2182 | - $res .= ', ' . $v; |
|
| 2182 | + $res .= ', '.$v; |
|
| 2183 | 2183 | } |
| 2184 | 2184 | } |
| 2185 | 2185 | |
@@ -2315,13 +2315,13 @@ discard block |
||
| 2315 | 2315 | |
| 2316 | 2316 | $def_origine = sql_showtable($table_origine, false, $serveur); |
| 2317 | 2317 | if (!$def_origine || !isset($def_origine['field'])) { |
| 2318 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2318 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite'._LOG_ERREUR); |
|
| 2319 | 2319 | |
| 2320 | 2320 | return false; |
| 2321 | 2321 | } |
| 2322 | 2322 | |
| 2323 | 2323 | |
| 2324 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2324 | + $table_tmp = $table_origine.'_tmp'; |
|
| 2325 | 2325 | |
| 2326 | 2326 | // 1) creer une table temporaire avec les modifications |
| 2327 | 2327 | // - DROP : suppression de la colonne |
@@ -2408,7 +2408,7 @@ discard block |
||
| 2408 | 2408 | } else { |
| 2409 | 2409 | // enlever KEY |
| 2410 | 2410 | $k = substr($k, 4); |
| 2411 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2411 | + $queries[] = "CREATE INDEX $table_destination"."_$k ON $table_destination ($v)"; |
|
| 2412 | 2412 | } |
| 2413 | 2413 | } |
| 2414 | 2414 | |
@@ -2419,7 +2419,7 @@ discard block |
||
| 2419 | 2419 | foreach ($queries as $q) { |
| 2420 | 2420 | if (!Sqlite::executer_requete($q, $serveur)) { |
| 2421 | 2421 | spip_log('SQLite : ALTER TABLE table :' |
| 2422 | - . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2422 | + . " Erreur a l'execution de la requete : $q", 'sqlite.'._LOG_ERREUR); |
|
| 2423 | 2423 | Sqlite::annuler_transaction($serveur); |
| 2424 | 2424 | |
| 2425 | 2425 | return false; |
@@ -2509,27 +2509,27 @@ discard block |
||
| 2509 | 2509 | $enum = '(\s*\([^\)]*\))?'; |
| 2510 | 2510 | |
| 2511 | 2511 | $remplace = [ |
| 2512 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2512 | + '/enum'.$enum.'/is' => 'VARCHAR(255)', |
|
| 2513 | 2513 | '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
| 2514 | 2514 | '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
| 2515 | 2515 | '/auto_increment/is' => '', |
| 2516 | 2516 | '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
| 2517 | 2517 | '/(timestamp .* )ON .*$/is' => '\\1', |
| 2518 | 2518 | '/character set \w+/is' => '', |
| 2519 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2519 | + '/((big|small|medium|tiny)?int(eger)?)'.$num.'\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2520 | 2520 | '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
| 2521 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2521 | + '/((char|varchar)'.$num.'\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2522 | 2522 | '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
| 2523 | 2523 | '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
| 2524 | 2524 | ]; |
| 2525 | 2525 | |
| 2526 | 2526 | // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
| 2527 | 2527 | $remplace_autocinc = [ |
| 2528 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2528 | + '/(big|small|medium|tiny)?int(eger)?'.$num.'/is' => 'INTEGER' |
|
| 2529 | 2529 | ]; |
| 2530 | 2530 | // pour les int non autoincrement, il faut un DEFAULT |
| 2531 | 2531 | $remplace_nonautocinc = [ |
| 2532 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2532 | + '/((big|small|medium|tiny)?int(eger)?'.$num.'\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2533 | 2533 | ]; |
| 2534 | 2534 | |
| 2535 | 2535 | if (is_string($query)) { |
@@ -2571,7 +2571,7 @@ discard block |
||
| 2571 | 2571 | return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
| 2572 | 2572 | } |
| 2573 | 2573 | if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
| 2574 | - return $champ . ' COLLATE NOCASE'; |
|
| 2574 | + return $champ.' COLLATE NOCASE'; |
|
| 2575 | 2575 | } |
| 2576 | 2576 | |
| 2577 | 2577 | return $champ; |
@@ -2661,7 +2661,7 @@ discard block |
||
| 2661 | 2661 | } else { |
| 2662 | 2662 | /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
| 2663 | 2663 | $a = spip_sqlite_showtable($nom, $serveur); |
| 2664 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2664 | + if (isset($a['key']['KEY '.$nom])) { |
|
| 2665 | 2665 | return true; |
| 2666 | 2666 | } |
| 2667 | 2667 | } |
@@ -2669,7 +2669,7 @@ discard block |
||
| 2669 | 2669 | |
| 2670 | 2670 | $temporary = $temporary ? ' TEMPORARY' : ''; |
| 2671 | 2671 | |
| 2672 | - return "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2672 | + return "CREATE$temporary TABLE$ifnotexists $nom ($query".($keys ? ",$keys" : '').")\n"; |
|
| 2673 | 2673 | } |
| 2674 | 2674 | |
| 2675 | 2675 | |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | |
| 22 | 22 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 23 | - return; |
|
| 23 | + return; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | // TODO: get/set_caracteres ? |
@@ -43,88 +43,88 @@ discard block |
||
| 43 | 43 | * @return array|bool |
| 44 | 44 | */ |
| 45 | 45 | function req_sqlite_dist($addr, $port, $login, #[\SensitiveParameter] $pass, $db = '', $prefixe = '', $sqlite_version = '') { |
| 46 | - static $last_connect = []; |
|
| 47 | - |
|
| 48 | - // si provient de selectdb |
|
| 49 | - // un code pour etre sur que l'on vient de select_db() |
|
| 50 | - if (str_contains($db, $code = '@selectdb@')) { |
|
| 51 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 52 | - ${$a} = $last_connect[$a]; |
|
| 53 | - } |
|
| 54 | - $db = str_replace($code, '', $db); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /* |
|
| 46 | + static $last_connect = []; |
|
| 47 | + |
|
| 48 | + // si provient de selectdb |
|
| 49 | + // un code pour etre sur que l'on vient de select_db() |
|
| 50 | + if (str_contains($db, $code = '@selectdb@')) { |
|
| 51 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 52 | + ${$a} = $last_connect[$a]; |
|
| 53 | + } |
|
| 54 | + $db = str_replace($code, '', $db); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /* |
|
| 58 | 58 | * En sqlite, seule l'adresse du fichier est importante. |
| 59 | 59 | * Ce sera $db le nom, |
| 60 | 60 | * le path est $addr |
| 61 | 61 | * (_DIR_DB si $addr est vide) |
| 62 | 62 | */ |
| 63 | - _sqlite_init(); |
|
| 64 | - |
|
| 65 | - // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 66 | - $f = _DIR_DB; |
|
| 67 | - if ($addr && str_contains($addr, '/')) { |
|
| 68 | - $f = rtrim($addr, '/') . '/'; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 72 | - // il faut que la base existe ou que le repertoire parent soit writable |
|
| 73 | - if ($db && !is_file($f .= $db . '.sqlite') && !is_writable(dirname($f))) { |
|
| 74 | - spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 75 | - |
|
| 76 | - return false; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - // charger les modules sqlite au besoin |
|
| 80 | - if (!_sqlite_charger_version($sqlite_version)) { |
|
| 81 | - spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 82 | - |
|
| 83 | - return false; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // chargement des constantes |
|
| 87 | - // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 88 | - $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 89 | - $define(); |
|
| 90 | - |
|
| 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 | - $link = spip_sqlite_open($tmp); |
|
| 98 | - } else { |
|
| 99 | - // Ouvrir (eventuellement creer la base) |
|
| 100 | - $link = spip_sqlite_open($f); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - if (!$link) { |
|
| 104 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f", 'sqlite.' . _LOG_HS); |
|
| 105 | - |
|
| 106 | - return false; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - $last_connect = [ |
|
| 110 | - 'addr' => $addr, |
|
| 111 | - 'port' => $port, |
|
| 112 | - 'login' => $login, |
|
| 113 | - 'pass' => $pass, |
|
| 114 | - 'db' => $db, |
|
| 115 | - 'prefixe' => $prefixe, |
|
| 116 | - ]; |
|
| 117 | - |
|
| 118 | - // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 119 | - include_spip('req/sqlite_fonctions'); |
|
| 120 | - _sqlite_init_functions($link); |
|
| 121 | - |
|
| 122 | - return [ |
|
| 123 | - 'db' => $db, |
|
| 124 | - 'prefixe' => $prefixe ?: $db, |
|
| 125 | - 'link' => $link, |
|
| 126 | - 'total_requetes' => 0, |
|
| 127 | - ]; |
|
| 63 | + _sqlite_init(); |
|
| 64 | + |
|
| 65 | + // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 66 | + $f = _DIR_DB; |
|
| 67 | + if ($addr && str_contains($addr, '/')) { |
|
| 68 | + $f = rtrim($addr, '/') . '/'; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 72 | + // il faut que la base existe ou que le repertoire parent soit writable |
|
| 73 | + if ($db && !is_file($f .= $db . '.sqlite') && !is_writable(dirname($f))) { |
|
| 74 | + spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 75 | + |
|
| 76 | + return false; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + // charger les modules sqlite au besoin |
|
| 80 | + if (!_sqlite_charger_version($sqlite_version)) { |
|
| 81 | + spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 82 | + |
|
| 83 | + return false; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // chargement des constantes |
|
| 87 | + // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 88 | + $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 89 | + $define(); |
|
| 90 | + |
|
| 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 | + $link = spip_sqlite_open($tmp); |
|
| 98 | + } else { |
|
| 99 | + // Ouvrir (eventuellement creer la base) |
|
| 100 | + $link = spip_sqlite_open($f); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + if (!$link) { |
|
| 104 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f", 'sqlite.' . _LOG_HS); |
|
| 105 | + |
|
| 106 | + return false; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + $last_connect = [ |
|
| 110 | + 'addr' => $addr, |
|
| 111 | + 'port' => $port, |
|
| 112 | + 'login' => $login, |
|
| 113 | + 'pass' => $pass, |
|
| 114 | + 'db' => $db, |
|
| 115 | + 'prefixe' => $prefixe, |
|
| 116 | + ]; |
|
| 117 | + |
|
| 118 | + // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 119 | + include_spip('req/sqlite_fonctions'); |
|
| 120 | + _sqlite_init_functions($link); |
|
| 121 | + |
|
| 122 | + return [ |
|
| 123 | + 'db' => $db, |
|
| 124 | + 'prefixe' => $prefixe ?: $db, |
|
| 125 | + 'link' => $link, |
|
| 126 | + 'total_requetes' => 0, |
|
| 127 | + ]; |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** |
@@ -134,9 +134,9 @@ discard block |
||
| 134 | 134 | * @return PDO |
| 135 | 135 | */ |
| 136 | 136 | function spip_sqlite_open(string $file): \PDO { |
| 137 | - $PDO = new \PDO("sqlite:$file"); |
|
| 138 | - $PDO->setAttribute(\PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, [&$PDO]]); |
|
| 139 | - return $PDO; |
|
| 137 | + $PDO = new \PDO("sqlite:$file"); |
|
| 138 | + $PDO->setAttribute(\PDO::ATTR_STATEMENT_CLASS, [PDOStatement::class, [&$PDO]]); |
|
| 139 | + return $PDO; |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | |
@@ -155,14 +155,14 @@ discard block |
||
| 155 | 155 | * Resultat de la requete |
| 156 | 156 | */ |
| 157 | 157 | function spip_sqlite_query($query, $serveur = '', $requeter = true) { |
| 158 | - #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 159 | - #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 160 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 161 | - if (!$requeter) { |
|
| 162 | - return $query; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - return Sqlite::executer_requete($query, $serveur); |
|
| 158 | + #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 159 | + #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 160 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 161 | + if (!$requeter) { |
|
| 162 | + return $query; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + return Sqlite::executer_requete($query, $serveur); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | |
@@ -179,11 +179,11 @@ discard block |
||
| 179 | 179 | */ |
| 180 | 180 | function spip_sqlite_alter($query, $serveur = '', $requeter = true) { |
| 181 | 181 | |
| 182 | - $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 183 | - // traduire la requete pour recuperer les bons noms de table |
|
| 184 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 182 | + $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 183 | + // traduire la requete pour recuperer les bons noms de table |
|
| 184 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 185 | 185 | |
| 186 | - /* |
|
| 186 | + /* |
|
| 187 | 187 | * la il faut faire les transformations |
| 188 | 188 | * si ALTER TABLE x (DROP|CHANGE) y |
| 189 | 189 | * |
@@ -192,251 +192,251 @@ discard block |
||
| 192 | 192 | * 3) faire chaque requete independemment |
| 193 | 193 | */ |
| 194 | 194 | |
| 195 | - // 1 |
|
| 196 | - if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 197 | - $debut = $regs[1]; |
|
| 198 | - $table = $regs[3]; |
|
| 199 | - $suite = $regs[4]; |
|
| 200 | - } else { |
|
| 201 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 202 | - |
|
| 203 | - return false; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - // 2 |
|
| 207 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 208 | - // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 209 | - // ou revoir l'api de sql_alter en creant un |
|
| 210 | - // sql_alter_table($table,array($actions)); |
|
| 211 | - $todo = explode(',', $suite); |
|
| 212 | - |
|
| 213 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 214 | - $todo2 = []; |
|
| 215 | - $i = 0; |
|
| 216 | - $ouverte = false; |
|
| 217 | - while ($do = array_shift($todo)) { |
|
| 218 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 219 | - $o = (str_contains($do, '(')); |
|
| 220 | - $f = (str_contains($do, ')')); |
|
| 221 | - if ($o && !$f) { |
|
| 222 | - $ouverte = true; |
|
| 223 | - } elseif ($f) { |
|
| 224 | - $ouverte = false; |
|
| 225 | - } |
|
| 226 | - if (!$ouverte) { |
|
| 227 | - $i++; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // 3 |
|
| 232 | - $resultats = []; |
|
| 233 | - foreach ($todo2 as $do) { |
|
| 234 | - $do = trim($do); |
|
| 235 | - if ( |
|
| 236 | - !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 237 | - . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 238 | - . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 239 | - . '|ADD COLUMN|ADD' |
|
| 240 | - . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 241 | - ) { |
|
| 242 | - spip_log( |
|
| 243 | - "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 244 | - 'sqlite.' . _LOG_ERREUR |
|
| 245 | - ); |
|
| 246 | - |
|
| 247 | - return false; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - $cle = strtoupper($matches[1]); |
|
| 251 | - $colonne_origine = $matches[2]; |
|
| 252 | - $colonne_destination = ''; |
|
| 253 | - |
|
| 254 | - $def = $matches[3]; |
|
| 255 | - |
|
| 256 | - // eluder une eventuelle clause before|after|first inutilisable |
|
| 257 | - $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 258 | - $defo = $defr; // garder la def d'origine pour certains cas |
|
| 259 | - // remplacer les definitions venant de mysql |
|
| 260 | - $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 261 | - |
|
| 262 | - // reinjecter dans le do |
|
| 263 | - $do = str_replace($def, $defr, $do); |
|
| 264 | - $def = $defr; |
|
| 265 | - |
|
| 266 | - switch ($cle) { |
|
| 267 | - // suppression d'un index |
|
| 268 | - case 'DROP KEY': |
|
| 269 | - case 'DROP INDEX': |
|
| 270 | - $nom_index = $colonne_origine; |
|
| 271 | - spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 272 | - break; |
|
| 273 | - |
|
| 274 | - // suppression d'une pk |
|
| 275 | - case 'DROP PRIMARY KEY': |
|
| 276 | - if ( |
|
| 277 | - !_sqlite_modifier_table( |
|
| 278 | - $table, |
|
| 279 | - $colonne_origine, |
|
| 280 | - ['key' => ['PRIMARY KEY' => '']], |
|
| 281 | - $serveur |
|
| 282 | - ) |
|
| 283 | - ) { |
|
| 284 | - return false; |
|
| 285 | - } |
|
| 286 | - break; |
|
| 287 | - // suppression d'une colonne |
|
| 288 | - case 'DROP COLUMN': |
|
| 289 | - case 'DROP': |
|
| 290 | - if ( |
|
| 291 | - !_sqlite_modifier_table( |
|
| 292 | - $table, |
|
| 293 | - [$colonne_origine => ''], |
|
| 294 | - [], |
|
| 295 | - $serveur |
|
| 296 | - ) |
|
| 297 | - ) { |
|
| 298 | - return false; |
|
| 299 | - } |
|
| 300 | - break; |
|
| 301 | - |
|
| 302 | - case 'CHANGE COLUMN': |
|
| 303 | - case 'CHANGE': |
|
| 304 | - // recuperer le nom de la future colonne |
|
| 305 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 306 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 307 | - $def = trim($defo); |
|
| 308 | - $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 309 | - $def = substr($def, strlen($colonne_destination) + 1); |
|
| 310 | - |
|
| 311 | - if ( |
|
| 312 | - !_sqlite_modifier_table( |
|
| 313 | - $table, |
|
| 314 | - [$colonne_origine => $colonne_destination], |
|
| 315 | - ['field' => [$colonne_destination => $def]], |
|
| 316 | - $serveur |
|
| 317 | - ) |
|
| 318 | - ) { |
|
| 319 | - return false; |
|
| 320 | - } |
|
| 321 | - break; |
|
| 322 | - |
|
| 323 | - case 'MODIFY': |
|
| 324 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 325 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 326 | - if ( |
|
| 327 | - !_sqlite_modifier_table( |
|
| 328 | - $table, |
|
| 329 | - $colonne_origine, |
|
| 330 | - ['field' => [$colonne_origine => $defo]], |
|
| 331 | - $serveur |
|
| 332 | - ) |
|
| 333 | - ) { |
|
| 334 | - return false; |
|
| 335 | - } |
|
| 336 | - break; |
|
| 337 | - |
|
| 338 | - // pas geres en sqlite2 |
|
| 339 | - case 'RENAME': |
|
| 340 | - $do = 'RENAME TO' . substr($do, 6); |
|
| 341 | - case 'RENAME TO': |
|
| 342 | - if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 343 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 344 | - |
|
| 345 | - return false; |
|
| 346 | - } |
|
| 347 | - break; |
|
| 348 | - |
|
| 349 | - // ajout d'une pk |
|
| 350 | - case 'ADD PRIMARY KEY': |
|
| 351 | - $pk = trim(substr($do, 16)); |
|
| 352 | - $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 353 | - if ( |
|
| 354 | - !_sqlite_modifier_table( |
|
| 355 | - $table, |
|
| 356 | - $colonne_origine, |
|
| 357 | - ['key' => ['PRIMARY KEY' => $pk]], |
|
| 358 | - $serveur |
|
| 359 | - ) |
|
| 360 | - ) { |
|
| 361 | - return false; |
|
| 362 | - } |
|
| 363 | - break; |
|
| 364 | - // ajout d'un index |
|
| 365 | - case 'ADD UNIQUE KEY': |
|
| 366 | - case 'ADD UNIQUE': |
|
| 367 | - $unique = true; |
|
| 368 | - case 'ADD INDEX': |
|
| 369 | - case 'ADD KEY': |
|
| 370 | - if (!isset($unique)) { |
|
| 371 | - $unique = false; |
|
| 372 | - } |
|
| 373 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 374 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 375 | - // |
|
| 376 | - // nom_index (colonnes) |
|
| 377 | - if ($def) { |
|
| 378 | - $colonnes = substr((string) $def, 1, -1); |
|
| 379 | - $nom_index = $colonne_origine; |
|
| 380 | - } else { |
|
| 381 | - // (colonne) |
|
| 382 | - if ($colonne_origine[0] == '(') { |
|
| 383 | - $colonnes = substr($colonne_origine, 1, -1); |
|
| 384 | - if (str_contains(',', $colonnes)) { |
|
| 385 | - spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 386 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 387 | - break; |
|
| 388 | - } else { |
|
| 389 | - $nom_index = $colonnes; |
|
| 390 | - } |
|
| 391 | - } // nom_index |
|
| 392 | - else { |
|
| 393 | - $nom_index = $colonnes = $colonne_origine; |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 397 | - break; |
|
| 398 | - |
|
| 399 | - // pas geres en sqlite2 |
|
| 400 | - case 'ADD COLUMN': |
|
| 401 | - $do = 'ADD' . substr($do, 10); |
|
| 402 | - case 'ADD': |
|
| 403 | - default: |
|
| 404 | - if (!preg_match(',primary\s+key,i', $do)) { |
|
| 405 | - if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 406 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 407 | - |
|
| 408 | - return false; |
|
| 409 | - } |
|
| 410 | - break; |
|
| 411 | - } |
|
| 412 | - // ou si la colonne est aussi primary key |
|
| 413 | - // cas du add id_truc int primary key |
|
| 414 | - // ajout d'une colonne qui passe en primary key directe |
|
| 415 | - else { |
|
| 416 | - $def = trim(substr($do, 3)); |
|
| 417 | - $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 418 | - $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 419 | - $opts = []; |
|
| 420 | - if (preg_match(',primary\s+key,i', $def)) { |
|
| 421 | - $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 422 | - $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 423 | - } |
|
| 424 | - $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 425 | - if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 426 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 427 | - |
|
| 428 | - return false; |
|
| 429 | - } |
|
| 430 | - } |
|
| 431 | - break; |
|
| 432 | - } |
|
| 433 | - // tout est bon, ouf ! |
|
| 434 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 438 | - |
|
| 439 | - return true; |
|
| 195 | + // 1 |
|
| 196 | + if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 197 | + $debut = $regs[1]; |
|
| 198 | + $table = $regs[3]; |
|
| 199 | + $suite = $regs[4]; |
|
| 200 | + } else { |
|
| 201 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 202 | + |
|
| 203 | + return false; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + // 2 |
|
| 207 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 208 | + // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 209 | + // ou revoir l'api de sql_alter en creant un |
|
| 210 | + // sql_alter_table($table,array($actions)); |
|
| 211 | + $todo = explode(',', $suite); |
|
| 212 | + |
|
| 213 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 214 | + $todo2 = []; |
|
| 215 | + $i = 0; |
|
| 216 | + $ouverte = false; |
|
| 217 | + while ($do = array_shift($todo)) { |
|
| 218 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 219 | + $o = (str_contains($do, '(')); |
|
| 220 | + $f = (str_contains($do, ')')); |
|
| 221 | + if ($o && !$f) { |
|
| 222 | + $ouverte = true; |
|
| 223 | + } elseif ($f) { |
|
| 224 | + $ouverte = false; |
|
| 225 | + } |
|
| 226 | + if (!$ouverte) { |
|
| 227 | + $i++; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // 3 |
|
| 232 | + $resultats = []; |
|
| 233 | + foreach ($todo2 as $do) { |
|
| 234 | + $do = trim($do); |
|
| 235 | + if ( |
|
| 236 | + !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 237 | + . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 238 | + . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 239 | + . '|ADD COLUMN|ADD' |
|
| 240 | + . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 241 | + ) { |
|
| 242 | + spip_log( |
|
| 243 | + "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 244 | + 'sqlite.' . _LOG_ERREUR |
|
| 245 | + ); |
|
| 246 | + |
|
| 247 | + return false; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + $cle = strtoupper($matches[1]); |
|
| 251 | + $colonne_origine = $matches[2]; |
|
| 252 | + $colonne_destination = ''; |
|
| 253 | + |
|
| 254 | + $def = $matches[3]; |
|
| 255 | + |
|
| 256 | + // eluder une eventuelle clause before|after|first inutilisable |
|
| 257 | + $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 258 | + $defo = $defr; // garder la def d'origine pour certains cas |
|
| 259 | + // remplacer les definitions venant de mysql |
|
| 260 | + $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 261 | + |
|
| 262 | + // reinjecter dans le do |
|
| 263 | + $do = str_replace($def, $defr, $do); |
|
| 264 | + $def = $defr; |
|
| 265 | + |
|
| 266 | + switch ($cle) { |
|
| 267 | + // suppression d'un index |
|
| 268 | + case 'DROP KEY': |
|
| 269 | + case 'DROP INDEX': |
|
| 270 | + $nom_index = $colonne_origine; |
|
| 271 | + spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 272 | + break; |
|
| 273 | + |
|
| 274 | + // suppression d'une pk |
|
| 275 | + case 'DROP PRIMARY KEY': |
|
| 276 | + if ( |
|
| 277 | + !_sqlite_modifier_table( |
|
| 278 | + $table, |
|
| 279 | + $colonne_origine, |
|
| 280 | + ['key' => ['PRIMARY KEY' => '']], |
|
| 281 | + $serveur |
|
| 282 | + ) |
|
| 283 | + ) { |
|
| 284 | + return false; |
|
| 285 | + } |
|
| 286 | + break; |
|
| 287 | + // suppression d'une colonne |
|
| 288 | + case 'DROP COLUMN': |
|
| 289 | + case 'DROP': |
|
| 290 | + if ( |
|
| 291 | + !_sqlite_modifier_table( |
|
| 292 | + $table, |
|
| 293 | + [$colonne_origine => ''], |
|
| 294 | + [], |
|
| 295 | + $serveur |
|
| 296 | + ) |
|
| 297 | + ) { |
|
| 298 | + return false; |
|
| 299 | + } |
|
| 300 | + break; |
|
| 301 | + |
|
| 302 | + case 'CHANGE COLUMN': |
|
| 303 | + case 'CHANGE': |
|
| 304 | + // recuperer le nom de la future colonne |
|
| 305 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 306 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 307 | + $def = trim($defo); |
|
| 308 | + $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 309 | + $def = substr($def, strlen($colonne_destination) + 1); |
|
| 310 | + |
|
| 311 | + if ( |
|
| 312 | + !_sqlite_modifier_table( |
|
| 313 | + $table, |
|
| 314 | + [$colonne_origine => $colonne_destination], |
|
| 315 | + ['field' => [$colonne_destination => $def]], |
|
| 316 | + $serveur |
|
| 317 | + ) |
|
| 318 | + ) { |
|
| 319 | + return false; |
|
| 320 | + } |
|
| 321 | + break; |
|
| 322 | + |
|
| 323 | + case 'MODIFY': |
|
| 324 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 325 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 326 | + if ( |
|
| 327 | + !_sqlite_modifier_table( |
|
| 328 | + $table, |
|
| 329 | + $colonne_origine, |
|
| 330 | + ['field' => [$colonne_origine => $defo]], |
|
| 331 | + $serveur |
|
| 332 | + ) |
|
| 333 | + ) { |
|
| 334 | + return false; |
|
| 335 | + } |
|
| 336 | + break; |
|
| 337 | + |
|
| 338 | + // pas geres en sqlite2 |
|
| 339 | + case 'RENAME': |
|
| 340 | + $do = 'RENAME TO' . substr($do, 6); |
|
| 341 | + case 'RENAME TO': |
|
| 342 | + if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 343 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 344 | + |
|
| 345 | + return false; |
|
| 346 | + } |
|
| 347 | + break; |
|
| 348 | + |
|
| 349 | + // ajout d'une pk |
|
| 350 | + case 'ADD PRIMARY KEY': |
|
| 351 | + $pk = trim(substr($do, 16)); |
|
| 352 | + $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 353 | + if ( |
|
| 354 | + !_sqlite_modifier_table( |
|
| 355 | + $table, |
|
| 356 | + $colonne_origine, |
|
| 357 | + ['key' => ['PRIMARY KEY' => $pk]], |
|
| 358 | + $serveur |
|
| 359 | + ) |
|
| 360 | + ) { |
|
| 361 | + return false; |
|
| 362 | + } |
|
| 363 | + break; |
|
| 364 | + // ajout d'un index |
|
| 365 | + case 'ADD UNIQUE KEY': |
|
| 366 | + case 'ADD UNIQUE': |
|
| 367 | + $unique = true; |
|
| 368 | + case 'ADD INDEX': |
|
| 369 | + case 'ADD KEY': |
|
| 370 | + if (!isset($unique)) { |
|
| 371 | + $unique = false; |
|
| 372 | + } |
|
| 373 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 374 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 375 | + // |
|
| 376 | + // nom_index (colonnes) |
|
| 377 | + if ($def) { |
|
| 378 | + $colonnes = substr((string) $def, 1, -1); |
|
| 379 | + $nom_index = $colonne_origine; |
|
| 380 | + } else { |
|
| 381 | + // (colonne) |
|
| 382 | + if ($colonne_origine[0] == '(') { |
|
| 383 | + $colonnes = substr($colonne_origine, 1, -1); |
|
| 384 | + if (str_contains(',', $colonnes)) { |
|
| 385 | + spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 386 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 387 | + break; |
|
| 388 | + } else { |
|
| 389 | + $nom_index = $colonnes; |
|
| 390 | + } |
|
| 391 | + } // nom_index |
|
| 392 | + else { |
|
| 393 | + $nom_index = $colonnes = $colonne_origine; |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 397 | + break; |
|
| 398 | + |
|
| 399 | + // pas geres en sqlite2 |
|
| 400 | + case 'ADD COLUMN': |
|
| 401 | + $do = 'ADD' . substr($do, 10); |
|
| 402 | + case 'ADD': |
|
| 403 | + default: |
|
| 404 | + if (!preg_match(',primary\s+key,i', $do)) { |
|
| 405 | + if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 406 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 407 | + |
|
| 408 | + return false; |
|
| 409 | + } |
|
| 410 | + break; |
|
| 411 | + } |
|
| 412 | + // ou si la colonne est aussi primary key |
|
| 413 | + // cas du add id_truc int primary key |
|
| 414 | + // ajout d'une colonne qui passe en primary key directe |
|
| 415 | + else { |
|
| 416 | + $def = trim(substr($do, 3)); |
|
| 417 | + $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 418 | + $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 419 | + $opts = []; |
|
| 420 | + if (preg_match(',primary\s+key,i', $def)) { |
|
| 421 | + $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 422 | + $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 423 | + } |
|
| 424 | + $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 425 | + if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 426 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 427 | + |
|
| 428 | + return false; |
|
| 429 | + } |
|
| 430 | + } |
|
| 431 | + break; |
|
| 432 | + } |
|
| 433 | + // tout est bon, ouf ! |
|
| 434 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 438 | + |
|
| 439 | + return true; |
|
| 440 | 440 | } |
| 441 | 441 | |
| 442 | 442 | /** |
@@ -458,38 +458,38 @@ discard block |
||
| 458 | 458 | * - true si la requête réussie, false sinon. |
| 459 | 459 | */ |
| 460 | 460 | function spip_sqlite_create( |
| 461 | - $nom, |
|
| 462 | - $champs, |
|
| 463 | - $cles, |
|
| 464 | - $autoinc = false, |
|
| 465 | - $temporary = false, |
|
| 466 | - $serveur = '', |
|
| 467 | - $requeter = true |
|
| 461 | + $nom, |
|
| 462 | + $champs, |
|
| 463 | + $cles, |
|
| 464 | + $autoinc = false, |
|
| 465 | + $temporary = false, |
|
| 466 | + $serveur = '', |
|
| 467 | + $requeter = true |
|
| 468 | 468 | ) { |
| 469 | - $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 470 | - if (!$query) { |
|
| 471 | - return false; |
|
| 472 | - } |
|
| 473 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 474 | - |
|
| 475 | - // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 476 | - // il faut donc les faire creer ensuite |
|
| 477 | - if (!$requeter) { |
|
| 478 | - return $res; |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - $ok = (bool) $res; |
|
| 482 | - if ($ok) { |
|
| 483 | - foreach ($cles as $k => $v) { |
|
| 484 | - if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 485 | - $index = trim(substr($k, strlen($m[1]))); |
|
| 486 | - $unique = (strlen($m[1]) > 3); |
|
| 487 | - $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 488 | - } |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - return (bool) $ok; |
|
| 469 | + $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 470 | + if (!$query) { |
|
| 471 | + return false; |
|
| 472 | + } |
|
| 473 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 474 | + |
|
| 475 | + // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 476 | + // il faut donc les faire creer ensuite |
|
| 477 | + if (!$requeter) { |
|
| 478 | + return $res; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + $ok = (bool) $res; |
|
| 482 | + if ($ok) { |
|
| 483 | + foreach ($cles as $k => $v) { |
|
| 484 | + if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 485 | + $index = trim(substr($k, strlen($m[1]))); |
|
| 486 | + $unique = (strlen($m[1]) > 3); |
|
| 487 | + $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 488 | + } |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + return (bool) $ok; |
|
| 493 | 493 | } |
| 494 | 494 | |
| 495 | 495 | /** |
@@ -502,21 +502,21 @@ discard block |
||
| 502 | 502 | * @return bool true si la base est créee. |
| 503 | 503 | **/ |
| 504 | 504 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) { |
| 505 | - $f = $nom . '.sqlite'; |
|
| 506 | - if (!str_contains($nom, '/')) { |
|
| 507 | - $f = _DIR_DB . $f; |
|
| 508 | - } |
|
| 505 | + $f = $nom . '.sqlite'; |
|
| 506 | + if (!str_contains($nom, '/')) { |
|
| 507 | + $f = _DIR_DB . $f; |
|
| 508 | + } |
|
| 509 | 509 | |
| 510 | - $ok = new \PDO("sqlite:$f"); |
|
| 510 | + $ok = new \PDO("sqlite:$f"); |
|
| 511 | 511 | |
| 512 | - if ($ok) { |
|
| 513 | - unset($ok); |
|
| 512 | + if ($ok) { |
|
| 513 | + unset($ok); |
|
| 514 | 514 | |
| 515 | - return true; |
|
| 516 | - } |
|
| 517 | - unset($ok); |
|
| 515 | + return true; |
|
| 516 | + } |
|
| 517 | + unset($ok); |
|
| 518 | 518 | |
| 519 | - return false; |
|
| 519 | + return false; |
|
| 520 | 520 | } |
| 521 | 521 | |
| 522 | 522 | |
@@ -537,22 +537,22 @@ discard block |
||
| 537 | 537 | * - string texte de la requête si $requeter vaut false |
| 538 | 538 | */ |
| 539 | 539 | function spip_sqlite_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 540 | - if (!$query_select) { |
|
| 541 | - return false; |
|
| 542 | - } |
|
| 543 | - // vue deja presente |
|
| 544 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 545 | - spip_log( |
|
| 546 | - "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 547 | - 'sqlite.' . _LOG_ERREUR |
|
| 548 | - ); |
|
| 549 | - |
|
| 550 | - return false; |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 554 | - |
|
| 555 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 540 | + if (!$query_select) { |
|
| 541 | + return false; |
|
| 542 | + } |
|
| 543 | + // vue deja presente |
|
| 544 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 545 | + spip_log( |
|
| 546 | + "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 547 | + 'sqlite.' . _LOG_ERREUR |
|
| 548 | + ); |
|
| 549 | + |
|
| 550 | + return false; |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 554 | + |
|
| 555 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 556 | 556 | } |
| 557 | 557 | |
| 558 | 558 | /** |
@@ -574,54 +574,54 @@ discard block |
||
| 574 | 574 | * string : requête, false si erreur, true sinon. |
| 575 | 575 | */ |
| 576 | 576 | function spip_sqlite_create_index($nom, $table, $champs, $unique = '', $serveur = '', $requeter = true) { |
| 577 | - if (!($nom || $table || $champs)) { |
|
| 578 | - spip_log( |
|
| 579 | - "Champ manquant pour creer un index sqlite ($nom, $table, (" . implode(',', $champs) . '))', |
|
| 580 | - 'sqlite.' . _LOG_ERREUR |
|
| 581 | - ); |
|
| 582 | - |
|
| 583 | - return false; |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 587 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 588 | - $nom = $table . '_' . $nom; |
|
| 589 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 590 | - if (!is_array($champs)) { |
|
| 591 | - if ($champs[0] == '(') { |
|
| 592 | - $champs = substr($champs, 1, -1); |
|
| 593 | - } |
|
| 594 | - $champs = [$champs]; |
|
| 595 | - // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 596 | - $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - $ifnotexists = ''; |
|
| 600 | - $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 601 | - if (!function_exists('spip_version_compare')) { |
|
| 602 | - include_spip('plugins/installer'); |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - if ($version && spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 606 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 607 | - } else { |
|
| 608 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 609 | - $a = spip_sqlite_showtable($table, $serveur); |
|
| 610 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 611 | - return true; |
|
| 612 | - } |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . implode(',', $champs) . ')'; |
|
| 616 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 617 | - if (!$requeter) { |
|
| 618 | - return $res; |
|
| 619 | - } |
|
| 620 | - if ($res) { |
|
| 621 | - return true; |
|
| 622 | - } else { |
|
| 623 | - return false; |
|
| 624 | - } |
|
| 577 | + if (!($nom || $table || $champs)) { |
|
| 578 | + spip_log( |
|
| 579 | + "Champ manquant pour creer un index sqlite ($nom, $table, (" . implode(',', $champs) . '))', |
|
| 580 | + 'sqlite.' . _LOG_ERREUR |
|
| 581 | + ); |
|
| 582 | + |
|
| 583 | + return false; |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 587 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 588 | + $nom = $table . '_' . $nom; |
|
| 589 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 590 | + if (!is_array($champs)) { |
|
| 591 | + if ($champs[0] == '(') { |
|
| 592 | + $champs = substr($champs, 1, -1); |
|
| 593 | + } |
|
| 594 | + $champs = [$champs]; |
|
| 595 | + // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 596 | + $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + $ifnotexists = ''; |
|
| 600 | + $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 601 | + if (!function_exists('spip_version_compare')) { |
|
| 602 | + include_spip('plugins/installer'); |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + if ($version && spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 606 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 607 | + } else { |
|
| 608 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 609 | + $a = spip_sqlite_showtable($table, $serveur); |
|
| 610 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 611 | + return true; |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . implode(',', $champs) . ')'; |
|
| 616 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 617 | + if (!$requeter) { |
|
| 618 | + return $res; |
|
| 619 | + } |
|
| 620 | + if ($res) { |
|
| 621 | + return true; |
|
| 622 | + } else { |
|
| 623 | + return false; |
|
| 624 | + } |
|
| 625 | 625 | } |
| 626 | 626 | |
| 627 | 627 | /** |
@@ -637,24 +637,24 @@ discard block |
||
| 637 | 637 | * @return int Nombre de lignes |
| 638 | 638 | */ |
| 639 | 639 | function spip_sqlite_count($r, $serveur = '', $requeter = true) { |
| 640 | - if (!$r) { |
|
| 641 | - return 0; |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - // select ou autre (insert, update,...) ? |
|
| 645 | - // (link,requete) a compter |
|
| 646 | - if (strtoupper(substr(ltrim($r->queryString), 0, 6)) === 'SELECT') { |
|
| 647 | - $link = $r->getPDO(); |
|
| 648 | - $query = "SELECT count(*) as zzzzsqlitecount FROM ({$r->queryString})"; |
|
| 649 | - $l = $link->query($query); |
|
| 650 | - $i = 0; |
|
| 651 | - if ($l && ($z = $l->fetch())) { |
|
| 652 | - $i = (int) $z['zzzzsqlitecount']; |
|
| 653 | - } |
|
| 654 | - return $i; |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - return $r->rowCount(); |
|
| 640 | + if (!$r) { |
|
| 641 | + return 0; |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + // select ou autre (insert, update,...) ? |
|
| 645 | + // (link,requete) a compter |
|
| 646 | + if (strtoupper(substr(ltrim($r->queryString), 0, 6)) === 'SELECT') { |
|
| 647 | + $link = $r->getPDO(); |
|
| 648 | + $query = "SELECT count(*) as zzzzsqlitecount FROM ({$r->queryString})"; |
|
| 649 | + $l = $link->query($query); |
|
| 650 | + $i = 0; |
|
| 651 | + if ($l && ($z = $l->fetch())) { |
|
| 652 | + $i = (int) $z['zzzzsqlitecount']; |
|
| 653 | + } |
|
| 654 | + return $i; |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + return $r->rowCount(); |
|
| 658 | 658 | } |
| 659 | 659 | |
| 660 | 660 | |
@@ -673,31 +673,31 @@ discard block |
||
| 673 | 673 | * - false si la requête a échouée |
| 674 | 674 | **/ |
| 675 | 675 | function spip_sqlite_countsel( |
| 676 | - $from = [], |
|
| 677 | - $where = [], |
|
| 678 | - $groupby = '', |
|
| 679 | - $having = [], |
|
| 680 | - $serveur = '', |
|
| 681 | - $requeter = true |
|
| 676 | + $from = [], |
|
| 677 | + $where = [], |
|
| 678 | + $groupby = '', |
|
| 679 | + $having = [], |
|
| 680 | + $serveur = '', |
|
| 681 | + $requeter = true |
|
| 682 | 682 | ) { |
| 683 | - $c = $groupby ? 'DISTINCT ' . (is_string($groupby) ? $groupby : implode(',', $groupby)) : ('*'); |
|
| 684 | - $r = spip_sqlite_select( |
|
| 685 | - "COUNT($c)", |
|
| 686 | - $from, |
|
| 687 | - $where, |
|
| 688 | - '', |
|
| 689 | - '', |
|
| 690 | - '', |
|
| 691 | - $having, |
|
| 692 | - $serveur, |
|
| 693 | - $requeter |
|
| 694 | - ); |
|
| 695 | - if ((is_resource($r) || is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 696 | - [$r] = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 697 | - $r = (int) $r; |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - return $r; |
|
| 683 | + $c = $groupby ? 'DISTINCT ' . (is_string($groupby) ? $groupby : implode(',', $groupby)) : ('*'); |
|
| 684 | + $r = spip_sqlite_select( |
|
| 685 | + "COUNT($c)", |
|
| 686 | + $from, |
|
| 687 | + $where, |
|
| 688 | + '', |
|
| 689 | + '', |
|
| 690 | + '', |
|
| 691 | + $having, |
|
| 692 | + $serveur, |
|
| 693 | + $requeter |
|
| 694 | + ); |
|
| 695 | + if ((is_resource($r) || is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 696 | + [$r] = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 697 | + $r = (int) $r; |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + return $r; |
|
| 701 | 701 | } |
| 702 | 702 | |
| 703 | 703 | |
@@ -714,24 +714,24 @@ discard block |
||
| 714 | 714 | * - false en cas d'erreur. |
| 715 | 715 | **/ |
| 716 | 716 | function spip_sqlite_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 717 | - $res = spip_sqlite_query( |
|
| 718 | - _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 719 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 720 | - $serveur, |
|
| 721 | - $requeter |
|
| 722 | - ); |
|
| 723 | - |
|
| 724 | - // renvoyer la requete inerte si demandee |
|
| 725 | - if (!$requeter) { |
|
| 726 | - return $res; |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - if ($res) { |
|
| 730 | - $link = _sqlite_link($serveur); |
|
| 731 | - return $res->rowCount(); |
|
| 732 | - } else { |
|
| 733 | - return false; |
|
| 734 | - } |
|
| 717 | + $res = spip_sqlite_query( |
|
| 718 | + _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 719 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 720 | + $serveur, |
|
| 721 | + $requeter |
|
| 722 | + ); |
|
| 723 | + |
|
| 724 | + // renvoyer la requete inerte si demandee |
|
| 725 | + if (!$requeter) { |
|
| 726 | + return $res; |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + if ($res) { |
|
| 730 | + $link = _sqlite_link($serveur); |
|
| 731 | + return $res->rowCount(); |
|
| 732 | + } else { |
|
| 733 | + return false; |
|
| 734 | + } |
|
| 735 | 735 | } |
| 736 | 736 | |
| 737 | 737 | |
@@ -747,13 +747,13 @@ discard block |
||
| 747 | 747 | * - true si la requête a réussie, false sinon |
| 748 | 748 | */ |
| 749 | 749 | function spip_sqlite_drop_table($table, $exist = false, $serveur = '', $requeter = true) { |
| 750 | - $exist = (bool) $exist ? ' IF EXISTS' : ''; |
|
| 750 | + $exist = (bool) $exist ? ' IF EXISTS' : ''; |
|
| 751 | 751 | |
| 752 | - if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 753 | - return true; |
|
| 754 | - } |
|
| 752 | + if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 753 | + return true; |
|
| 754 | + } |
|
| 755 | 755 | |
| 756 | - return false; |
|
| 756 | + return false; |
|
| 757 | 757 | } |
| 758 | 758 | |
| 759 | 759 | |
@@ -769,9 +769,9 @@ 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 = false, $serveur = '', $requeter = true) { |
| 772 | - $exist = (bool) $exist ? ' IF EXISTS' : ''; |
|
| 772 | + $exist = (bool) $exist ? ' IF EXISTS' : ''; |
|
| 773 | 773 | |
| 774 | - return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 774 | + return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 775 | 775 | } |
| 776 | 776 | |
| 777 | 777 | /** |
@@ -785,20 +785,20 @@ discard block |
||
| 785 | 785 | * @return bool ou requete |
| 786 | 786 | */ |
| 787 | 787 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) { |
| 788 | - if (!$nom && !$table) { |
|
| 789 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 788 | + if (!$nom && !$table) { |
|
| 789 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 790 | 790 | |
| 791 | - return false; |
|
| 792 | - } |
|
| 791 | + return false; |
|
| 792 | + } |
|
| 793 | 793 | |
| 794 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 795 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 796 | - $index = $table . '_' . $nom; |
|
| 797 | - $exist = ' IF EXISTS'; |
|
| 794 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 795 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 796 | + $index = $table . '_' . $nom; |
|
| 797 | + $exist = ' IF EXISTS'; |
|
| 798 | 798 | |
| 799 | - $query = "DROP INDEX$exist $index"; |
|
| 799 | + $query = "DROP INDEX$exist $index"; |
|
| 800 | 800 | |
| 801 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 801 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 802 | 802 | } |
| 803 | 803 | |
| 804 | 804 | /** |
@@ -814,28 +814,28 @@ discard block |
||
| 814 | 814 | * Erreur eventuelle |
| 815 | 815 | **/ |
| 816 | 816 | function spip_sqlite_error($query = '', $serveur = '') { |
| 817 | - $link = _sqlite_link($serveur); |
|
| 818 | - |
|
| 819 | - if ($link) { |
|
| 820 | - $errs = $link->errorInfo(); |
|
| 821 | - $s = _sqlite_last_error_from_link($link); |
|
| 822 | - } else { |
|
| 823 | - $s = ': aucune ressource sqlite (link)'; |
|
| 824 | - } |
|
| 825 | - if ($s) { |
|
| 826 | - $trace = debug_backtrace(); |
|
| 827 | - if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 828 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 829 | - } |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - return $s; |
|
| 817 | + $link = _sqlite_link($serveur); |
|
| 818 | + |
|
| 819 | + if ($link) { |
|
| 820 | + $errs = $link->errorInfo(); |
|
| 821 | + $s = _sqlite_last_error_from_link($link); |
|
| 822 | + } else { |
|
| 823 | + $s = ': aucune ressource sqlite (link)'; |
|
| 824 | + } |
|
| 825 | + if ($s) { |
|
| 826 | + $trace = debug_backtrace(); |
|
| 827 | + if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 828 | + spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 829 | + } |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + return $s; |
|
| 833 | 833 | } |
| 834 | 834 | |
| 835 | 835 | function _sqlite_last_error_from_link($link) { |
| 836 | - if ($link) { |
|
| 837 | - $errs = $link->errorInfo(); |
|
| 838 | - /* |
|
| 836 | + if ($link) { |
|
| 837 | + $errs = $link->errorInfo(); |
|
| 838 | + /* |
|
| 839 | 839 | $errs[0] |
| 840 | 840 | numero SQLState ('HY000' souvent lors d'une erreur) |
| 841 | 841 | http://www.easysoft.com/developer/interfaces/odbc/sqlstate_status_return_codes.html |
@@ -845,11 +845,11 @@ discard block |
||
| 845 | 845 | $errs[2] |
| 846 | 846 | Le texte du message d'erreur |
| 847 | 847 | */ |
| 848 | - if (ltrim((string) $errs[0], '0')) { // 00000 si pas d'erreur |
|
| 849 | - return "$errs[2]"; |
|
| 850 | - } |
|
| 851 | - } |
|
| 852 | - return ''; |
|
| 848 | + if (ltrim((string) $errs[0], '0')) { // 00000 si pas d'erreur |
|
| 849 | + return "$errs[2]"; |
|
| 850 | + } |
|
| 851 | + } |
|
| 852 | + return ''; |
|
| 853 | 853 | } |
| 854 | 854 | |
| 855 | 855 | /** |
@@ -866,23 +866,23 @@ discard block |
||
| 866 | 866 | * 'HY000/1' : numéro de l'erreur SQLState / numéro d'erreur interne SQLite (en sqlite 3) |
| 867 | 867 | **/ |
| 868 | 868 | function spip_sqlite_errno($serveur = '') { |
| 869 | - $link = _sqlite_link($serveur); |
|
| 870 | - |
|
| 871 | - if ($link) { |
|
| 872 | - $t = $link->errorInfo(); |
|
| 873 | - $s = ltrim((string) $t[0], '0'); // 00000 si pas d'erreur |
|
| 874 | - if ($s) { |
|
| 875 | - $s .= ' / ' . $t[1]; |
|
| 876 | - } // ajoute l'erreur du moteur SQLite |
|
| 877 | - } else { |
|
| 878 | - $s = ': aucune ressource sqlite (link)'; |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - if ($s) { |
|
| 882 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 883 | - } |
|
| 884 | - |
|
| 885 | - return $s ?: 0; |
|
| 869 | + $link = _sqlite_link($serveur); |
|
| 870 | + |
|
| 871 | + if ($link) { |
|
| 872 | + $t = $link->errorInfo(); |
|
| 873 | + $s = ltrim((string) $t[0], '0'); // 00000 si pas d'erreur |
|
| 874 | + if ($s) { |
|
| 875 | + $s .= ' / ' . $t[1]; |
|
| 876 | + } // ajoute l'erreur du moteur SQLite |
|
| 877 | + } else { |
|
| 878 | + $s = ': aucune ressource sqlite (link)'; |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + if ($s) { |
|
| 882 | + spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 883 | + } |
|
| 884 | + |
|
| 885 | + return $s ?: 0; |
|
| 886 | 886 | } |
| 887 | 887 | |
| 888 | 888 | |
@@ -898,19 +898,19 @@ discard block |
||
| 898 | 898 | * - false si on a pas pu avoir d'explication |
| 899 | 899 | */ |
| 900 | 900 | function spip_sqlite_explain($query, $serveur = '', $requeter = true) { |
| 901 | - if (!str_starts_with(ltrim($query), 'SELECT')) { |
|
| 902 | - return []; |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 906 | - $query = 'EXPLAIN ' . $query; |
|
| 907 | - if (!$requeter) { |
|
| 908 | - return $query; |
|
| 909 | - } |
|
| 910 | - // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 911 | - $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 912 | - |
|
| 913 | - return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 901 | + if (!str_starts_with(ltrim($query), 'SELECT')) { |
|
| 902 | + return []; |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 906 | + $query = 'EXPLAIN ' . $query; |
|
| 907 | + if (!$requeter) { |
|
| 908 | + return $query; |
|
| 909 | + } |
|
| 910 | + // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 911 | + $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 912 | + |
|
| 913 | + return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 914 | 914 | } |
| 915 | 915 | |
| 916 | 916 | |
@@ -930,35 +930,35 @@ discard block |
||
| 930 | 930 | */ |
| 931 | 931 | function spip_sqlite_fetch($r, $t = '', $serveur = '', $requeter = true) { |
| 932 | 932 | |
| 933 | - $link = _sqlite_link($serveur); |
|
| 934 | - $t = $t ?: SPIP_SQLITE3_ASSOC; |
|
| 935 | - |
|
| 936 | - if (!$r) { |
|
| 937 | - return false; |
|
| 938 | - } |
|
| 939 | - |
|
| 940 | - $retour = $r->fetch($t); |
|
| 941 | - |
|
| 942 | - if (!$retour) { |
|
| 943 | - if ($r->errorCode() === '00000') { |
|
| 944 | - return null; |
|
| 945 | - } |
|
| 946 | - return false; |
|
| 947 | - } |
|
| 948 | - |
|
| 949 | - // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 950 | - // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 951 | - // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 952 | - if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 953 | - foreach ($retour as $cle => $val) { |
|
| 954 | - if (($pos = strpos((string) $cle, '.')) !== false) { |
|
| 955 | - $retour[substr((string) $cle, $pos + 1)] = &$retour[$cle]; |
|
| 956 | - unset($retour[$cle]); |
|
| 957 | - } |
|
| 958 | - } |
|
| 959 | - } |
|
| 960 | - |
|
| 961 | - return $retour; |
|
| 933 | + $link = _sqlite_link($serveur); |
|
| 934 | + $t = $t ?: SPIP_SQLITE3_ASSOC; |
|
| 935 | + |
|
| 936 | + if (!$r) { |
|
| 937 | + return false; |
|
| 938 | + } |
|
| 939 | + |
|
| 940 | + $retour = $r->fetch($t); |
|
| 941 | + |
|
| 942 | + if (!$retour) { |
|
| 943 | + if ($r->errorCode() === '00000') { |
|
| 944 | + return null; |
|
| 945 | + } |
|
| 946 | + return false; |
|
| 947 | + } |
|
| 948 | + |
|
| 949 | + // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 950 | + // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 951 | + // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 952 | + if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 953 | + foreach ($retour as $cle => $val) { |
|
| 954 | + if (($pos = strpos((string) $cle, '.')) !== false) { |
|
| 955 | + $retour[substr((string) $cle, $pos + 1)] = &$retour[$cle]; |
|
| 956 | + unset($retour[$cle]); |
|
| 957 | + } |
|
| 958 | + } |
|
| 959 | + } |
|
| 960 | + |
|
| 961 | + return $retour; |
|
| 962 | 962 | } |
| 963 | 963 | |
| 964 | 964 | /** |
@@ -971,8 +971,8 @@ discard block |
||
| 971 | 971 | * @return bool True si déplacement réussi, false sinon. |
| 972 | 972 | **/ |
| 973 | 973 | function spip_sqlite_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 974 | - // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 975 | - return false; |
|
| 974 | + // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 975 | + return false; |
|
| 976 | 976 | } |
| 977 | 977 | |
| 978 | 978 | |
@@ -988,10 +988,10 @@ discard block |
||
| 988 | 988 | * @return bool True si réussi |
| 989 | 989 | */ |
| 990 | 990 | function spip_sqlite_free(&$r, $serveur = '', $requeter = true) { |
| 991 | - unset($r); |
|
| 991 | + unset($r); |
|
| 992 | 992 | |
| 993 | - return true; |
|
| 994 | - //return sqlite_free_result($r); |
|
| 993 | + return true; |
|
| 994 | + //return sqlite_free_result($r); |
|
| 995 | 995 | } |
| 996 | 996 | |
| 997 | 997 | |
@@ -1006,8 +1006,8 @@ discard block |
||
| 1006 | 1006 | * @return void |
| 1007 | 1007 | */ |
| 1008 | 1008 | function spip_sqlite_get_charset($charset = [], $serveur = '', $requeter = true) { |
| 1009 | - //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1010 | - //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1009 | + //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1010 | + //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1011 | 1011 | } |
| 1012 | 1012 | |
| 1013 | 1013 | |
@@ -1022,7 +1022,7 @@ discard block |
||
| 1022 | 1022 | * Valeur hexadécimale pour SQLite |
| 1023 | 1023 | **/ |
| 1024 | 1024 | function spip_sqlite_hex($v) { |
| 1025 | - return hexdec($v); |
|
| 1025 | + return hexdec($v); |
|
| 1026 | 1026 | } |
| 1027 | 1027 | |
| 1028 | 1028 | |
@@ -1044,7 +1044,7 @@ discard block |
||
| 1044 | 1044 | * Expression de requête SQL |
| 1045 | 1045 | **/ |
| 1046 | 1046 | function spip_sqlite_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) { |
| 1047 | - return "($val $not IN ($valeurs))"; |
|
| 1047 | + return "($val $not IN ($valeurs))"; |
|
| 1048 | 1048 | } |
| 1049 | 1049 | |
| 1050 | 1050 | |
@@ -1072,20 +1072,20 @@ discard block |
||
| 1072 | 1072 | **/ |
| 1073 | 1073 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 1074 | 1074 | |
| 1075 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1076 | - if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1077 | - if (!$requeter) { |
|
| 1078 | - return $r; |
|
| 1079 | - } |
|
| 1080 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1081 | - } else { |
|
| 1082 | - $nb = false; |
|
| 1083 | - } |
|
| 1075 | + $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1076 | + if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1077 | + if (!$requeter) { |
|
| 1078 | + return $r; |
|
| 1079 | + } |
|
| 1080 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1081 | + } else { |
|
| 1082 | + $nb = false; |
|
| 1083 | + } |
|
| 1084 | 1084 | |
| 1085 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1085 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1086 | 1086 | |
| 1087 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1088 | - return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1087 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1088 | + return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1089 | 1089 | } |
| 1090 | 1090 | |
| 1091 | 1091 | |
@@ -1110,28 +1110,28 @@ discard block |
||
| 1110 | 1110 | * - Tableau de description de la requête et du temps d'exécution, si var_profile activé |
| 1111 | 1111 | **/ |
| 1112 | 1112 | function spip_sqlite_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1113 | - if (!$desc) { |
|
| 1114 | - $desc = description_table($table, $serveur); |
|
| 1115 | - } |
|
| 1116 | - if (!$desc) { |
|
| 1117 | - die("$table insertion sans description"); |
|
| 1118 | - } |
|
| 1119 | - $fields = $desc['field'] ?? []; |
|
| 1120 | - |
|
| 1121 | - foreach ($couples as $champ => $val) { |
|
| 1122 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1123 | - } |
|
| 1124 | - |
|
| 1125 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1126 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1127 | - |
|
| 1128 | - $cles = $valeurs = ''; |
|
| 1129 | - if (is_countable($couples) ? count($couples) : 0) { |
|
| 1130 | - $cles = '(' . implode(',', array_keys($couples)) . ')'; |
|
| 1131 | - $valeurs = '(' . implode(',', $couples) . ')'; |
|
| 1132 | - } |
|
| 1133 | - |
|
| 1134 | - return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1113 | + if (!$desc) { |
|
| 1114 | + $desc = description_table($table, $serveur); |
|
| 1115 | + } |
|
| 1116 | + if (!$desc) { |
|
| 1117 | + die("$table insertion sans description"); |
|
| 1118 | + } |
|
| 1119 | + $fields = $desc['field'] ?? []; |
|
| 1120 | + |
|
| 1121 | + foreach ($couples as $champ => $val) { |
|
| 1122 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1123 | + } |
|
| 1124 | + |
|
| 1125 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1126 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1127 | + |
|
| 1128 | + $cles = $valeurs = ''; |
|
| 1129 | + if (is_countable($couples) ? count($couples) : 0) { |
|
| 1130 | + $cles = '(' . implode(',', array_keys($couples)) . ')'; |
|
| 1131 | + $valeurs = '(' . implode(',', $couples) . ')'; |
|
| 1132 | + } |
|
| 1133 | + |
|
| 1134 | + return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1135 | 1135 | } |
| 1136 | 1136 | |
| 1137 | 1137 | |
@@ -1155,72 +1155,72 @@ discard block |
||
| 1155 | 1155 | * - false en cas d'erreur. |
| 1156 | 1156 | **/ |
| 1157 | 1157 | function spip_sqlite_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1158 | - $retour = null; |
|
| 1159 | - $nb = null; |
|
| 1160 | - if (!$desc) { |
|
| 1161 | - $desc = description_table($table, $serveur); |
|
| 1162 | - } |
|
| 1163 | - if (!$desc) { |
|
| 1164 | - die("$table insertion sans description"); |
|
| 1165 | - } |
|
| 1166 | - if (!isset($desc['field'])) { |
|
| 1167 | - $desc['field'] = []; |
|
| 1168 | - } |
|
| 1169 | - |
|
| 1170 | - // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1171 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1172 | - |
|
| 1173 | - // seul le nom de la table est a traduire ici : |
|
| 1174 | - // le faire une seule fois au debut |
|
| 1175 | - $query_start = "INSERT INTO $table "; |
|
| 1176 | - $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1177 | - |
|
| 1178 | - // ouvrir une transaction |
|
| 1179 | - if ($requeter) { |
|
| 1180 | - Sqlite::demarrer_transaction($serveur); |
|
| 1181 | - } |
|
| 1182 | - |
|
| 1183 | - while ($couples = array_shift($tab_couples)) { |
|
| 1184 | - foreach ($couples as $champ => $val) { |
|
| 1185 | - $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1186 | - } |
|
| 1187 | - |
|
| 1188 | - // inserer les champs timestamp par defaut |
|
| 1189 | - $couples = array_merge($maj, $couples); |
|
| 1190 | - |
|
| 1191 | - $champs = $valeurs = ''; |
|
| 1192 | - if ($couples !== []) { |
|
| 1193 | - $champs = '(' . implode(',', array_keys($couples)) . ')'; |
|
| 1194 | - $valeurs = '(' . implode(',', $couples) . ')'; |
|
| 1195 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1196 | - } else { |
|
| 1197 | - $query = $query_start . 'DEFAULT VALUES'; |
|
| 1198 | - } |
|
| 1199 | - |
|
| 1200 | - if ($requeter) { |
|
| 1201 | - $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1202 | - } |
|
| 1203 | - |
|
| 1204 | - // sur le dernier couple uniquement |
|
| 1205 | - if ($tab_couples === []) { |
|
| 1206 | - $nb = 0; |
|
| 1207 | - if ($requeter) { |
|
| 1208 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1209 | - } else { |
|
| 1210 | - return $query; |
|
| 1211 | - } |
|
| 1212 | - } |
|
| 1213 | - |
|
| 1214 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1215 | - } |
|
| 1216 | - |
|
| 1217 | - if ($requeter) { |
|
| 1218 | - Sqlite::finir_transaction($serveur); |
|
| 1219 | - } |
|
| 1220 | - |
|
| 1221 | - // renvoie le dernier id d'autoincrement ajoute |
|
| 1222 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1223 | - return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1158 | + $retour = null; |
|
| 1159 | + $nb = null; |
|
| 1160 | + if (!$desc) { |
|
| 1161 | + $desc = description_table($table, $serveur); |
|
| 1162 | + } |
|
| 1163 | + if (!$desc) { |
|
| 1164 | + die("$table insertion sans description"); |
|
| 1165 | + } |
|
| 1166 | + if (!isset($desc['field'])) { |
|
| 1167 | + $desc['field'] = []; |
|
| 1168 | + } |
|
| 1169 | + |
|
| 1170 | + // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1171 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1172 | + |
|
| 1173 | + // seul le nom de la table est a traduire ici : |
|
| 1174 | + // le faire une seule fois au debut |
|
| 1175 | + $query_start = "INSERT INTO $table "; |
|
| 1176 | + $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1177 | + |
|
| 1178 | + // ouvrir une transaction |
|
| 1179 | + if ($requeter) { |
|
| 1180 | + Sqlite::demarrer_transaction($serveur); |
|
| 1181 | + } |
|
| 1182 | + |
|
| 1183 | + while ($couples = array_shift($tab_couples)) { |
|
| 1184 | + foreach ($couples as $champ => $val) { |
|
| 1185 | + $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1186 | + } |
|
| 1187 | + |
|
| 1188 | + // inserer les champs timestamp par defaut |
|
| 1189 | + $couples = array_merge($maj, $couples); |
|
| 1190 | + |
|
| 1191 | + $champs = $valeurs = ''; |
|
| 1192 | + if ($couples !== []) { |
|
| 1193 | + $champs = '(' . implode(',', array_keys($couples)) . ')'; |
|
| 1194 | + $valeurs = '(' . implode(',', $couples) . ')'; |
|
| 1195 | + $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1196 | + } else { |
|
| 1197 | + $query = $query_start . 'DEFAULT VALUES'; |
|
| 1198 | + } |
|
| 1199 | + |
|
| 1200 | + if ($requeter) { |
|
| 1201 | + $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1202 | + } |
|
| 1203 | + |
|
| 1204 | + // sur le dernier couple uniquement |
|
| 1205 | + if ($tab_couples === []) { |
|
| 1206 | + $nb = 0; |
|
| 1207 | + if ($requeter) { |
|
| 1208 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1209 | + } else { |
|
| 1210 | + return $query; |
|
| 1211 | + } |
|
| 1212 | + } |
|
| 1213 | + |
|
| 1214 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1215 | + } |
|
| 1216 | + |
|
| 1217 | + if ($requeter) { |
|
| 1218 | + Sqlite::finir_transaction($serveur); |
|
| 1219 | + } |
|
| 1220 | + |
|
| 1221 | + // renvoie le dernier id d'autoincrement ajoute |
|
| 1222 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1223 | + return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1224 | 1224 | } |
| 1225 | 1225 | |
| 1226 | 1226 | |
@@ -1235,7 +1235,7 @@ discard block |
||
| 1235 | 1235 | * Toujours true. |
| 1236 | 1236 | **/ |
| 1237 | 1237 | function spip_sqlite_preferer_transaction($serveur = '', $requeter = true) { |
| 1238 | - return true; |
|
| 1238 | + return true; |
|
| 1239 | 1239 | } |
| 1240 | 1240 | |
| 1241 | 1241 | /** |
@@ -1252,12 +1252,12 @@ discard block |
||
| 1252 | 1252 | * string si texte de la requête demandé, true sinon |
| 1253 | 1253 | **/ |
| 1254 | 1254 | function spip_sqlite_demarrer_transaction($serveur = '', $requeter = true) { |
| 1255 | - if (!$requeter) { |
|
| 1256 | - return 'BEGIN TRANSACTION'; |
|
| 1257 | - } |
|
| 1258 | - Sqlite::demarrer_transaction($serveur); |
|
| 1255 | + if (!$requeter) { |
|
| 1256 | + return 'BEGIN TRANSACTION'; |
|
| 1257 | + } |
|
| 1258 | + Sqlite::demarrer_transaction($serveur); |
|
| 1259 | 1259 | |
| 1260 | - return true; |
|
| 1260 | + return true; |
|
| 1261 | 1261 | } |
| 1262 | 1262 | |
| 1263 | 1263 | /** |
@@ -1271,12 +1271,12 @@ discard block |
||
| 1271 | 1271 | * string si texte de la requête demandé, true sinon |
| 1272 | 1272 | **/ |
| 1273 | 1273 | function spip_sqlite_terminer_transaction($serveur = '', $requeter = true) { |
| 1274 | - if (!$requeter) { |
|
| 1275 | - return 'COMMIT'; |
|
| 1276 | - } |
|
| 1277 | - Sqlite::finir_transaction($serveur); |
|
| 1274 | + if (!$requeter) { |
|
| 1275 | + return 'COMMIT'; |
|
| 1276 | + } |
|
| 1277 | + Sqlite::finir_transaction($serveur); |
|
| 1278 | 1278 | |
| 1279 | - return true; |
|
| 1279 | + return true; |
|
| 1280 | 1280 | } |
| 1281 | 1281 | |
| 1282 | 1282 | |
@@ -1291,27 +1291,27 @@ discard block |
||
| 1291 | 1291 | * Liste des noms de bases |
| 1292 | 1292 | **/ |
| 1293 | 1293 | function spip_sqlite_listdbs($serveur = '', $requeter = true) { |
| 1294 | - _sqlite_init(); |
|
| 1295 | - |
|
| 1296 | - if (!is_dir($d = substr((string) _DIR_DB, 0, -1))) { |
|
| 1297 | - return []; |
|
| 1298 | - } |
|
| 1299 | - |
|
| 1300 | - include_spip('inc/flock'); |
|
| 1301 | - $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1302 | - $bds = []; |
|
| 1303 | - |
|
| 1304 | - foreach ($bases as $b) { |
|
| 1305 | - // pas de bases commencant pas sqlite |
|
| 1306 | - // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1307 | - // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1308 | - if (strpos((string) $b, '_sqlite')) { |
|
| 1309 | - continue; |
|
| 1310 | - } |
|
| 1311 | - $bds[] = preg_replace(";.*/$pattern;iS", '$1', (string) $b); |
|
| 1312 | - } |
|
| 1313 | - |
|
| 1314 | - return $bds; |
|
| 1294 | + _sqlite_init(); |
|
| 1295 | + |
|
| 1296 | + if (!is_dir($d = substr((string) _DIR_DB, 0, -1))) { |
|
| 1297 | + return []; |
|
| 1298 | + } |
|
| 1299 | + |
|
| 1300 | + include_spip('inc/flock'); |
|
| 1301 | + $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1302 | + $bds = []; |
|
| 1303 | + |
|
| 1304 | + foreach ($bases as $b) { |
|
| 1305 | + // pas de bases commencant pas sqlite |
|
| 1306 | + // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1307 | + // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1308 | + if (strpos((string) $b, '_sqlite')) { |
|
| 1309 | + continue; |
|
| 1310 | + } |
|
| 1311 | + $bds[] = preg_replace(";.*/$pattern;iS", '$1', (string) $b); |
|
| 1312 | + } |
|
| 1313 | + |
|
| 1314 | + return $bds; |
|
| 1315 | 1315 | } |
| 1316 | 1316 | |
| 1317 | 1317 | |
@@ -1326,7 +1326,7 @@ discard block |
||
| 1326 | 1326 | * @return string texte de sélection pour la requête |
| 1327 | 1327 | */ |
| 1328 | 1328 | function spip_sqlite_multi($objet, $lang) { |
| 1329 | - return 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1329 | + return 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1330 | 1330 | } |
| 1331 | 1331 | |
| 1332 | 1332 | |
@@ -1343,15 +1343,15 @@ discard block |
||
| 1343 | 1343 | * @return bool|string true / false / requete |
| 1344 | 1344 | **/ |
| 1345 | 1345 | function spip_sqlite_optimize($table, $serveur = '', $requeter = true) { |
| 1346 | - static $do = false; |
|
| 1347 | - if ($requeter && $do) { |
|
| 1348 | - return true; |
|
| 1349 | - } |
|
| 1350 | - if ($requeter) { |
|
| 1351 | - $do = true; |
|
| 1352 | - } |
|
| 1353 | - |
|
| 1354 | - return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1346 | + static $do = false; |
|
| 1347 | + if ($requeter && $do) { |
|
| 1348 | + return true; |
|
| 1349 | + } |
|
| 1350 | + if ($requeter) { |
|
| 1351 | + $do = true; |
|
| 1352 | + } |
|
| 1353 | + |
|
| 1354 | + return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1355 | 1355 | } |
| 1356 | 1356 | |
| 1357 | 1357 | |
@@ -1368,15 +1368,15 @@ discard block |
||
| 1368 | 1368 | * Donnée prête à être utilisée par le gestionnaire SQL |
| 1369 | 1369 | */ |
| 1370 | 1370 | function spip_sqlite_quote($v, $type = '') { |
| 1371 | - if (!is_array($v)) { |
|
| 1372 | - return _sqlite_calculer_cite($v, $type); |
|
| 1373 | - } |
|
| 1374 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1375 | - foreach ($v as $k => $r) { |
|
| 1376 | - $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1377 | - } |
|
| 1378 | - |
|
| 1379 | - return implode(',', $v); |
|
| 1371 | + if (!is_array($v)) { |
|
| 1372 | + return _sqlite_calculer_cite($v, $type); |
|
| 1373 | + } |
|
| 1374 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1375 | + foreach ($v as $k => $r) { |
|
| 1376 | + $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1377 | + } |
|
| 1378 | + |
|
| 1379 | + return implode(',', $v); |
|
| 1380 | 1380 | } |
| 1381 | 1381 | |
| 1382 | 1382 | |
@@ -1393,9 +1393,9 @@ discard block |
||
| 1393 | 1393 | * Expression SQL |
| 1394 | 1394 | **/ |
| 1395 | 1395 | function spip_sqlite_date_proche($champ, $interval, $unite) { |
| 1396 | - $op = (($interval <= 0) ? '>' : '<'); |
|
| 1396 | + $op = (($interval <= 0) ? '>' : '<'); |
|
| 1397 | 1397 | |
| 1398 | - return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1398 | + return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1399 | 1399 | } |
| 1400 | 1400 | |
| 1401 | 1401 | |
@@ -1413,48 +1413,48 @@ discard block |
||
| 1413 | 1413 | * l'état de la table après la réparation |
| 1414 | 1414 | */ |
| 1415 | 1415 | function spip_sqlite_repair($table, $serveur = '', $requeter = true) { |
| 1416 | - if ( |
|
| 1417 | - ($desc = spip_sqlite_showtable($table, $serveur)) |
|
| 1418 | - && isset($desc['field']) |
|
| 1419 | - && is_array($desc['field']) |
|
| 1420 | - ) { |
|
| 1421 | - foreach ($desc['field'] as $c => $d) { |
|
| 1422 | - if ( |
|
| 1423 | - preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', (string) $d) |
|
| 1424 | - && stripos((string) $d, 'NOT NULL') !== false |
|
| 1425 | - && stripos((string) $d, 'DEFAULT') === false |
|
| 1426 | - /* pas touche aux cles primaires */ |
|
| 1427 | - && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1428 | - ) { |
|
| 1429 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1430 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1431 | - } |
|
| 1432 | - if ( |
|
| 1433 | - preg_match(',^(INTEGER),i', (string) $d) |
|
| 1434 | - && stripos((string) $d, 'NOT NULL') !== false |
|
| 1435 | - && stripos((string) $d, 'DEFAULT') === false |
|
| 1436 | - /* pas touche aux cles primaires */ |
|
| 1437 | - && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1438 | - ) { |
|
| 1439 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1440 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1441 | - } |
|
| 1442 | - if ( |
|
| 1443 | - preg_match(',^(datetime),i', (string) $d) |
|
| 1444 | - && stripos((string) $d, 'NOT NULL') !== false |
|
| 1445 | - && stripos((string) $d, 'DEFAULT') === false |
|
| 1446 | - /* pas touche aux cles primaires */ |
|
| 1447 | - && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1448 | - ) { |
|
| 1449 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1450 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1451 | - } |
|
| 1452 | - } |
|
| 1453 | - |
|
| 1454 | - return [' OK ']; |
|
| 1455 | - } |
|
| 1456 | - |
|
| 1457 | - return [' ERROR ']; |
|
| 1416 | + if ( |
|
| 1417 | + ($desc = spip_sqlite_showtable($table, $serveur)) |
|
| 1418 | + && isset($desc['field']) |
|
| 1419 | + && is_array($desc['field']) |
|
| 1420 | + ) { |
|
| 1421 | + foreach ($desc['field'] as $c => $d) { |
|
| 1422 | + if ( |
|
| 1423 | + preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', (string) $d) |
|
| 1424 | + && stripos((string) $d, 'NOT NULL') !== false |
|
| 1425 | + && stripos((string) $d, 'DEFAULT') === false |
|
| 1426 | + /* pas touche aux cles primaires */ |
|
| 1427 | + && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1428 | + ) { |
|
| 1429 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1430 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1431 | + } |
|
| 1432 | + if ( |
|
| 1433 | + preg_match(',^(INTEGER),i', (string) $d) |
|
| 1434 | + && stripos((string) $d, 'NOT NULL') !== false |
|
| 1435 | + && stripos((string) $d, 'DEFAULT') === false |
|
| 1436 | + /* pas touche aux cles primaires */ |
|
| 1437 | + && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1438 | + ) { |
|
| 1439 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1440 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1441 | + } |
|
| 1442 | + if ( |
|
| 1443 | + preg_match(',^(datetime),i', (string) $d) |
|
| 1444 | + && stripos((string) $d, 'NOT NULL') !== false |
|
| 1445 | + && stripos((string) $d, 'DEFAULT') === false |
|
| 1446 | + /* pas touche aux cles primaires */ |
|
| 1447 | + && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1448 | + ) { |
|
| 1449 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1450 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1451 | + } |
|
| 1452 | + } |
|
| 1453 | + |
|
| 1454 | + return [' OK ']; |
|
| 1455 | + } |
|
| 1456 | + |
|
| 1457 | + return [' ERROR ']; |
|
| 1458 | 1458 | } |
| 1459 | 1459 | |
| 1460 | 1460 | |
@@ -1483,25 +1483,25 @@ discard block |
||
| 1483 | 1483 | * - false en cas d'erreur. |
| 1484 | 1484 | **/ |
| 1485 | 1485 | function spip_sqlite_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) { |
| 1486 | - if (!$desc) { |
|
| 1487 | - $desc = description_table($table, $serveur); |
|
| 1488 | - } |
|
| 1489 | - if (!$desc) { |
|
| 1490 | - die("$table insertion sans description"); |
|
| 1491 | - } |
|
| 1492 | - $fields = $desc['field'] ?? []; |
|
| 1493 | - |
|
| 1494 | - foreach ($couples as $champ => $val) { |
|
| 1495 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1496 | - } |
|
| 1497 | - |
|
| 1498 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1499 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1500 | - |
|
| 1501 | - return spip_sqlite_query("REPLACE INTO $table (" . implode(',', array_keys($couples)) . ') VALUES (' . implode( |
|
| 1502 | - ',', |
|
| 1503 | - $couples |
|
| 1504 | - ) . ')', $serveur); |
|
| 1486 | + if (!$desc) { |
|
| 1487 | + $desc = description_table($table, $serveur); |
|
| 1488 | + } |
|
| 1489 | + if (!$desc) { |
|
| 1490 | + die("$table insertion sans description"); |
|
| 1491 | + } |
|
| 1492 | + $fields = $desc['field'] ?? []; |
|
| 1493 | + |
|
| 1494 | + foreach ($couples as $champ => $val) { |
|
| 1495 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1496 | + } |
|
| 1497 | + |
|
| 1498 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1499 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1500 | + |
|
| 1501 | + return spip_sqlite_query("REPLACE INTO $table (" . implode(',', array_keys($couples)) . ') VALUES (' . implode( |
|
| 1502 | + ',', |
|
| 1503 | + $couples |
|
| 1504 | + ) . ')', $serveur); |
|
| 1505 | 1505 | } |
| 1506 | 1506 | |
| 1507 | 1507 | |
@@ -1531,14 +1531,14 @@ discard block |
||
| 1531 | 1531 | **/ |
| 1532 | 1532 | function spip_sqlite_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1533 | 1533 | |
| 1534 | - $retour = null; |
|
| 1535 | - // boucler pour traiter chaque requete independemment |
|
| 1536 | - foreach ($tab_couples as $couples) { |
|
| 1537 | - $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1538 | - } |
|
| 1534 | + $retour = null; |
|
| 1535 | + // boucler pour traiter chaque requete independemment |
|
| 1536 | + foreach ($tab_couples as $couples) { |
|
| 1537 | + $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1538 | + } |
|
| 1539 | 1539 | |
| 1540 | - // renvoie le dernier id |
|
| 1541 | - return $retour; |
|
| 1540 | + // renvoie le dernier id |
|
| 1541 | + return $retour; |
|
| 1542 | 1542 | } |
| 1543 | 1543 | |
| 1544 | 1544 | |
@@ -1565,44 +1565,44 @@ discard block |
||
| 1565 | 1565 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 1566 | 1566 | */ |
| 1567 | 1567 | function spip_sqlite_select( |
| 1568 | - $select, |
|
| 1569 | - $from, |
|
| 1570 | - $where = '', |
|
| 1571 | - $groupby = '', |
|
| 1572 | - $orderby = '', |
|
| 1573 | - $limit = '', |
|
| 1574 | - $having = '', |
|
| 1575 | - $serveur = '', |
|
| 1576 | - $requeter = true |
|
| 1568 | + $select, |
|
| 1569 | + $from, |
|
| 1570 | + $where = '', |
|
| 1571 | + $groupby = '', |
|
| 1572 | + $orderby = '', |
|
| 1573 | + $limit = '', |
|
| 1574 | + $having = '', |
|
| 1575 | + $serveur = '', |
|
| 1576 | + $requeter = true |
|
| 1577 | 1577 | ) { |
| 1578 | 1578 | |
| 1579 | - // version() n'est pas connu de sqlite |
|
| 1580 | - $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1581 | - |
|
| 1582 | - // recomposer from |
|
| 1583 | - $from = (is_array($from) ? _sqlite_calculer_select_as($from) : $from); |
|
| 1584 | - |
|
| 1585 | - $query = |
|
| 1586 | - _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1587 | - . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1588 | - . _sqlite_calculer_expression('WHERE', $where) |
|
| 1589 | - . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1590 | - . _sqlite_calculer_expression('HAVING', $having) |
|
| 1591 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1592 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1593 | - |
|
| 1594 | - // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1595 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1596 | - // texte de la requete demande ? |
|
| 1597 | - if (!$requeter) { |
|
| 1598 | - return $res; |
|
| 1599 | - } |
|
| 1600 | - // erreur survenue ? |
|
| 1601 | - if ($res === false) { |
|
| 1602 | - return Sqlite::traduire_requete($query, $serveur); |
|
| 1603 | - } |
|
| 1604 | - |
|
| 1605 | - return $res; |
|
| 1579 | + // version() n'est pas connu de sqlite |
|
| 1580 | + $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1581 | + |
|
| 1582 | + // recomposer from |
|
| 1583 | + $from = (is_array($from) ? _sqlite_calculer_select_as($from) : $from); |
|
| 1584 | + |
|
| 1585 | + $query = |
|
| 1586 | + _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1587 | + . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1588 | + . _sqlite_calculer_expression('WHERE', $where) |
|
| 1589 | + . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1590 | + . _sqlite_calculer_expression('HAVING', $having) |
|
| 1591 | + . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1592 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1593 | + |
|
| 1594 | + // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1595 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1596 | + // texte de la requete demande ? |
|
| 1597 | + if (!$requeter) { |
|
| 1598 | + return $res; |
|
| 1599 | + } |
|
| 1600 | + // erreur survenue ? |
|
| 1601 | + if ($res === false) { |
|
| 1602 | + return Sqlite::traduire_requete($query, $serveur); |
|
| 1603 | + } |
|
| 1604 | + |
|
| 1605 | + return $res; |
|
| 1606 | 1606 | } |
| 1607 | 1607 | |
| 1608 | 1608 | |
@@ -1621,32 +1621,32 @@ discard block |
||
| 1621 | 1621 | * - False en cas d'erreur. |
| 1622 | 1622 | **/ |
| 1623 | 1623 | function spip_sqlite_selectdb($db, $serveur = '', $requeter = true) { |
| 1624 | - _sqlite_init(); |
|
| 1625 | - |
|
| 1626 | - // interdire la creation d'une nouvelle base, |
|
| 1627 | - // sauf si on est dans l'installation |
|
| 1628 | - if ( |
|
| 1629 | - !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1630 | - && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1631 | - ) { |
|
| 1632 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1633 | - |
|
| 1634 | - return false; |
|
| 1635 | - } |
|
| 1636 | - |
|
| 1637 | - // se connecter a la base indiquee |
|
| 1638 | - // avec les identifiants connus |
|
| 1639 | - $index = $serveur ?: 0; |
|
| 1640 | - |
|
| 1641 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1642 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1643 | - return $db; |
|
| 1644 | - } |
|
| 1645 | - } else { |
|
| 1646 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1647 | - } |
|
| 1648 | - |
|
| 1649 | - return false; |
|
| 1624 | + _sqlite_init(); |
|
| 1625 | + |
|
| 1626 | + // interdire la creation d'une nouvelle base, |
|
| 1627 | + // sauf si on est dans l'installation |
|
| 1628 | + if ( |
|
| 1629 | + !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1630 | + && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1631 | + ) { |
|
| 1632 | + spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1633 | + |
|
| 1634 | + return false; |
|
| 1635 | + } |
|
| 1636 | + |
|
| 1637 | + // se connecter a la base indiquee |
|
| 1638 | + // avec les identifiants connus |
|
| 1639 | + $index = $serveur ?: 0; |
|
| 1640 | + |
|
| 1641 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1642 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1643 | + return $db; |
|
| 1644 | + } |
|
| 1645 | + } else { |
|
| 1646 | + spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1647 | + } |
|
| 1648 | + |
|
| 1649 | + return false; |
|
| 1650 | 1650 | } |
| 1651 | 1651 | |
| 1652 | 1652 | |
@@ -1661,8 +1661,8 @@ discard block |
||
| 1661 | 1661 | * @return void |
| 1662 | 1662 | */ |
| 1663 | 1663 | function spip_sqlite_set_charset($charset, $serveur = '', $requeter = true) { |
| 1664 | - # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1665 | - # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1664 | + # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1665 | + # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1666 | 1666 | } |
| 1667 | 1667 | |
| 1668 | 1668 | |
@@ -1680,24 +1680,24 @@ discard block |
||
| 1680 | 1680 | * Ressource à utiliser avec sql_fetch() |
| 1681 | 1681 | **/ |
| 1682 | 1682 | function spip_sqlite_showbase($match, $serveur = '', $requeter = true) { |
| 1683 | - // type est le type d'entrée : table / index / view |
|
| 1684 | - // on ne retourne que les tables (?) et non les vues... |
|
| 1685 | - # ESCAPE non supporte par les versions sqlite <3 |
|
| 1686 | - # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1687 | - $match = preg_quote($match); |
|
| 1688 | - $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1689 | - $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1690 | - $match = str_replace('_', '.', $match); |
|
| 1691 | - $match = str_replace('%', '.*', $match); |
|
| 1692 | - $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1693 | - $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1694 | - $match = "^$match$"; |
|
| 1695 | - |
|
| 1696 | - return spip_sqlite_query( |
|
| 1697 | - "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1698 | - $serveur, |
|
| 1699 | - $requeter |
|
| 1700 | - ); |
|
| 1683 | + // type est le type d'entrée : table / index / view |
|
| 1684 | + // on ne retourne que les tables (?) et non les vues... |
|
| 1685 | + # ESCAPE non supporte par les versions sqlite <3 |
|
| 1686 | + # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1687 | + $match = preg_quote($match); |
|
| 1688 | + $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1689 | + $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1690 | + $match = str_replace('_', '.', $match); |
|
| 1691 | + $match = str_replace('%', '.*', $match); |
|
| 1692 | + $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1693 | + $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1694 | + $match = "^$match$"; |
|
| 1695 | + |
|
| 1696 | + return spip_sqlite_query( |
|
| 1697 | + "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1698 | + $serveur, |
|
| 1699 | + $requeter |
|
| 1700 | + ); |
|
| 1701 | 1701 | } |
| 1702 | 1702 | |
| 1703 | 1703 | /** |
@@ -1715,19 +1715,19 @@ discard block |
||
| 1715 | 1715 | * - string : requete sql, si $requeter = true |
| 1716 | 1716 | **/ |
| 1717 | 1717 | function spip_sqlite_table_exists(string $table, $serveur = '', $requeter = true) { |
| 1718 | - $r = spip_sqlite_query( |
|
| 1719 | - 'SELECT name FROM sqlite_master WHERE' |
|
| 1720 | - . ' type=\'table\'' |
|
| 1721 | - . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1722 | - . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1723 | - $serveur, |
|
| 1724 | - $requeter |
|
| 1725 | - ); |
|
| 1726 | - if (!$requeter) { |
|
| 1727 | - return $r; |
|
| 1728 | - } |
|
| 1729 | - $res = spip_sqlite_fetch($r, '', $serveur); |
|
| 1730 | - return (bool) $res; |
|
| 1718 | + $r = spip_sqlite_query( |
|
| 1719 | + 'SELECT name FROM sqlite_master WHERE' |
|
| 1720 | + . ' type=\'table\'' |
|
| 1721 | + . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1722 | + . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1723 | + $serveur, |
|
| 1724 | + $requeter |
|
| 1725 | + ); |
|
| 1726 | + if (!$requeter) { |
|
| 1727 | + return $r; |
|
| 1728 | + } |
|
| 1729 | + $res = spip_sqlite_fetch($r, '', $serveur); |
|
| 1730 | + return (bool) $res; |
|
| 1731 | 1731 | } |
| 1732 | 1732 | |
| 1733 | 1733 | define('_SQLITE_RE_SHOW_TABLE', '/^[^(),]*\(((?:[^()]*\((?:[^()]*\([^()]*\))?[^()]*\)[^()]*)*[^()]*)\)[^()]*$/'); |
@@ -1750,129 +1750,129 @@ discard block |
||
| 1750 | 1750 | * - array description de la table sinon |
| 1751 | 1751 | */ |
| 1752 | 1752 | function spip_sqlite_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1753 | - $query = |
|
| 1754 | - 'SELECT sql, type FROM' |
|
| 1755 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1756 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1757 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1758 | - . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1759 | - . ' ORDER BY substr(type,2,1), name'; |
|
| 1760 | - |
|
| 1761 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1762 | - if (!$a) { |
|
| 1763 | - return ''; |
|
| 1764 | - } |
|
| 1765 | - if (!$requeter) { |
|
| 1766 | - return $a; |
|
| 1767 | - } |
|
| 1768 | - if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1769 | - return ''; |
|
| 1770 | - } |
|
| 1771 | - $vue = ($a['type'] == 'view'); // table | vue |
|
| 1772 | - |
|
| 1773 | - // c'est une table |
|
| 1774 | - // il faut parser le create |
|
| 1775 | - if (!$vue) { |
|
| 1776 | - if (!preg_match(_SQLITE_RE_SHOW_TABLE, (string) array_shift($a), $r)) { |
|
| 1777 | - return ''; |
|
| 1778 | - } else { |
|
| 1779 | - $desc = $r[1]; |
|
| 1780 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1781 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1782 | - if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1783 | - $namedkeys = $r[2]; |
|
| 1784 | - $desc = $r[1]; |
|
| 1785 | - } else { |
|
| 1786 | - $namedkeys = ''; |
|
| 1787 | - } |
|
| 1788 | - |
|
| 1789 | - $fields = []; |
|
| 1790 | - $keys = []; |
|
| 1791 | - |
|
| 1792 | - // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1793 | - // par exemple s'il contiennent une virgule. |
|
| 1794 | - // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1795 | - [$desc, $echaps] = query_echappe_textes($desc); |
|
| 1796 | - |
|
| 1797 | - // separer toutes les descriptions de champs, separes par des virgules |
|
| 1798 | - # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1799 | - $k_precedent = null; |
|
| 1800 | - foreach (explode(',', (string) $desc) as $v) { |
|
| 1801 | - preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1802 | - // Les cles de champs peuvent etre entourees |
|
| 1803 | - // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1804 | - // http://www.sqlite.org/lang_keywords.html |
|
| 1805 | - $k = strtolower((string) query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1806 | - if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1807 | - $k = trim($k, $char); |
|
| 1808 | - if ($char == '[') { |
|
| 1809 | - $k = rtrim($k, ']'); |
|
| 1810 | - } |
|
| 1811 | - } |
|
| 1812 | - $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1813 | - |
|
| 1814 | - // rustine pour DECIMAL(10,2) |
|
| 1815 | - // s'il y a une parenthèse fermante dans la clé |
|
| 1816 | - // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1817 | - if (str_contains($k, ')') || preg_match('/^[^\(]*\)/', (string) $def)) { |
|
| 1818 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1819 | - continue; |
|
| 1820 | - } |
|
| 1821 | - |
|
| 1822 | - // la primary key peut etre dans une des descriptions de champs |
|
| 1823 | - // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1824 | - if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1825 | - $keys['PRIMARY KEY'] = $k; |
|
| 1826 | - } |
|
| 1827 | - |
|
| 1828 | - $fields[$k] = $def; |
|
| 1829 | - $k_precedent = $k; |
|
| 1830 | - } |
|
| 1831 | - // key inclues dans la requete |
|
| 1832 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1833 | - if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', (string) $v, $r)) { |
|
| 1834 | - $k = str_replace('`', '', trim($r[1])); |
|
| 1835 | - $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1836 | - if ($k && !isset($keys[$k])) { |
|
| 1837 | - $keys[$k] = $t; |
|
| 1838 | - } else { |
|
| 1839 | - $keys[] = $t; |
|
| 1840 | - } |
|
| 1841 | - } |
|
| 1842 | - } |
|
| 1843 | - // sinon ajouter les key index |
|
| 1844 | - $query = |
|
| 1845 | - 'SELECT name,sql FROM' |
|
| 1846 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1847 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1848 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1849 | - . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1850 | - . 'ORDER BY substr(type,2,1), name'; |
|
| 1851 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1852 | - while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1853 | - $key = str_replace($nom_table . '_', '', (string) $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1854 | - $keytype = 'KEY'; |
|
| 1855 | - if (str_contains((string) $r['sql'], 'UNIQUE INDEX')) { |
|
| 1856 | - $keytype = 'UNIQUE KEY'; |
|
| 1857 | - } |
|
| 1858 | - $colonnes = preg_replace(',.*\((.*)\).*,', '$1', (string) $r['sql']); |
|
| 1859 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1860 | - } |
|
| 1861 | - } |
|
| 1862 | - } // c'est une vue, on liste les champs disponibles simplement |
|
| 1863 | - else { |
|
| 1864 | - if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1865 | - $fields = []; |
|
| 1866 | - foreach ($res as $c => $v) { |
|
| 1867 | - $fields[$c] = ''; |
|
| 1868 | - } |
|
| 1869 | - $keys = []; |
|
| 1870 | - } else { |
|
| 1871 | - return ''; |
|
| 1872 | - } |
|
| 1873 | - } |
|
| 1874 | - |
|
| 1875 | - return ['field' => $fields, 'key' => $keys]; |
|
| 1753 | + $query = |
|
| 1754 | + 'SELECT sql, type FROM' |
|
| 1755 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1756 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1757 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1758 | + . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1759 | + . ' ORDER BY substr(type,2,1), name'; |
|
| 1760 | + |
|
| 1761 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1762 | + if (!$a) { |
|
| 1763 | + return ''; |
|
| 1764 | + } |
|
| 1765 | + if (!$requeter) { |
|
| 1766 | + return $a; |
|
| 1767 | + } |
|
| 1768 | + if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1769 | + return ''; |
|
| 1770 | + } |
|
| 1771 | + $vue = ($a['type'] == 'view'); // table | vue |
|
| 1772 | + |
|
| 1773 | + // c'est une table |
|
| 1774 | + // il faut parser le create |
|
| 1775 | + if (!$vue) { |
|
| 1776 | + if (!preg_match(_SQLITE_RE_SHOW_TABLE, (string) array_shift($a), $r)) { |
|
| 1777 | + return ''; |
|
| 1778 | + } else { |
|
| 1779 | + $desc = $r[1]; |
|
| 1780 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1781 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1782 | + if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1783 | + $namedkeys = $r[2]; |
|
| 1784 | + $desc = $r[1]; |
|
| 1785 | + } else { |
|
| 1786 | + $namedkeys = ''; |
|
| 1787 | + } |
|
| 1788 | + |
|
| 1789 | + $fields = []; |
|
| 1790 | + $keys = []; |
|
| 1791 | + |
|
| 1792 | + // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1793 | + // par exemple s'il contiennent une virgule. |
|
| 1794 | + // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1795 | + [$desc, $echaps] = query_echappe_textes($desc); |
|
| 1796 | + |
|
| 1797 | + // separer toutes les descriptions de champs, separes par des virgules |
|
| 1798 | + # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1799 | + $k_precedent = null; |
|
| 1800 | + foreach (explode(',', (string) $desc) as $v) { |
|
| 1801 | + preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1802 | + // Les cles de champs peuvent etre entourees |
|
| 1803 | + // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1804 | + // http://www.sqlite.org/lang_keywords.html |
|
| 1805 | + $k = strtolower((string) query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1806 | + if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1807 | + $k = trim($k, $char); |
|
| 1808 | + if ($char == '[') { |
|
| 1809 | + $k = rtrim($k, ']'); |
|
| 1810 | + } |
|
| 1811 | + } |
|
| 1812 | + $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1813 | + |
|
| 1814 | + // rustine pour DECIMAL(10,2) |
|
| 1815 | + // s'il y a une parenthèse fermante dans la clé |
|
| 1816 | + // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1817 | + if (str_contains($k, ')') || preg_match('/^[^\(]*\)/', (string) $def)) { |
|
| 1818 | + $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1819 | + continue; |
|
| 1820 | + } |
|
| 1821 | + |
|
| 1822 | + // la primary key peut etre dans une des descriptions de champs |
|
| 1823 | + // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1824 | + if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1825 | + $keys['PRIMARY KEY'] = $k; |
|
| 1826 | + } |
|
| 1827 | + |
|
| 1828 | + $fields[$k] = $def; |
|
| 1829 | + $k_precedent = $k; |
|
| 1830 | + } |
|
| 1831 | + // key inclues dans la requete |
|
| 1832 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1833 | + if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', (string) $v, $r)) { |
|
| 1834 | + $k = str_replace('`', '', trim($r[1])); |
|
| 1835 | + $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1836 | + if ($k && !isset($keys[$k])) { |
|
| 1837 | + $keys[$k] = $t; |
|
| 1838 | + } else { |
|
| 1839 | + $keys[] = $t; |
|
| 1840 | + } |
|
| 1841 | + } |
|
| 1842 | + } |
|
| 1843 | + // sinon ajouter les key index |
|
| 1844 | + $query = |
|
| 1845 | + 'SELECT name,sql FROM' |
|
| 1846 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1847 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1848 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1849 | + . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1850 | + . 'ORDER BY substr(type,2,1), name'; |
|
| 1851 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1852 | + while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1853 | + $key = str_replace($nom_table . '_', '', (string) $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1854 | + $keytype = 'KEY'; |
|
| 1855 | + if (str_contains((string) $r['sql'], 'UNIQUE INDEX')) { |
|
| 1856 | + $keytype = 'UNIQUE KEY'; |
|
| 1857 | + } |
|
| 1858 | + $colonnes = preg_replace(',.*\((.*)\).*,', '$1', (string) $r['sql']); |
|
| 1859 | + $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1860 | + } |
|
| 1861 | + } |
|
| 1862 | + } // c'est une vue, on liste les champs disponibles simplement |
|
| 1863 | + else { |
|
| 1864 | + if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1865 | + $fields = []; |
|
| 1866 | + foreach ($res as $c => $v) { |
|
| 1867 | + $fields[$c] = ''; |
|
| 1868 | + } |
|
| 1869 | + $keys = []; |
|
| 1870 | + } else { |
|
| 1871 | + return ''; |
|
| 1872 | + } |
|
| 1873 | + } |
|
| 1874 | + |
|
| 1875 | + return ['field' => $fields, 'key' => $keys]; |
|
| 1876 | 1876 | } |
| 1877 | 1877 | |
| 1878 | 1878 | |
@@ -1898,24 +1898,24 @@ discard block |
||
| 1898 | 1898 | * - array Tableau décrivant la requête et son temps d'exécution si var_profile est actif |
| 1899 | 1899 | */ |
| 1900 | 1900 | function spip_sqlite_update($table, $champs, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1901 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1902 | - $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1903 | - |
|
| 1904 | - $set = []; |
|
| 1905 | - foreach ($champs as $champ => $val) { |
|
| 1906 | - $set[] = $champ . "=$val"; |
|
| 1907 | - } |
|
| 1908 | - if ($set !== []) { |
|
| 1909 | - return spip_sqlite_query( |
|
| 1910 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1911 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1912 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1913 | - $serveur, |
|
| 1914 | - $requeter |
|
| 1915 | - ); |
|
| 1916 | - } |
|
| 1917 | - |
|
| 1918 | - return false; |
|
| 1901 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1902 | + $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1903 | + |
|
| 1904 | + $set = []; |
|
| 1905 | + foreach ($champs as $champ => $val) { |
|
| 1906 | + $set[] = $champ . "=$val"; |
|
| 1907 | + } |
|
| 1908 | + if ($set !== []) { |
|
| 1909 | + return spip_sqlite_query( |
|
| 1910 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1911 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1912 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1913 | + $serveur, |
|
| 1914 | + $requeter |
|
| 1915 | + ); |
|
| 1916 | + } |
|
| 1917 | + |
|
| 1918 | + return false; |
|
| 1919 | 1919 | } |
| 1920 | 1920 | |
| 1921 | 1921 | |
@@ -1945,38 +1945,38 @@ discard block |
||
| 1945 | 1945 | */ |
| 1946 | 1946 | function spip_sqlite_updateq($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1947 | 1947 | |
| 1948 | - if (!$champs) { |
|
| 1949 | - return; |
|
| 1950 | - } |
|
| 1951 | - if (!$desc) { |
|
| 1952 | - $desc = description_table($table, $serveur); |
|
| 1953 | - } |
|
| 1954 | - if (!$desc) { |
|
| 1955 | - die("$table insertion sans description"); |
|
| 1956 | - } |
|
| 1957 | - $fields = $desc['field']; |
|
| 1958 | - |
|
| 1959 | - $set = []; |
|
| 1960 | - foreach ($champs as $champ => $val) { |
|
| 1961 | - $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, $fields[$champ] ?? ''); |
|
| 1962 | - } |
|
| 1963 | - |
|
| 1964 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1965 | - // attention ils sont deja quotes |
|
| 1966 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1967 | - foreach ($maj as $champ => $val) { |
|
| 1968 | - if (!isset($set[$champ])) { |
|
| 1969 | - $set[$champ] = $champ . '=' . $val; |
|
| 1970 | - } |
|
| 1971 | - } |
|
| 1972 | - |
|
| 1973 | - return spip_sqlite_query( |
|
| 1974 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1975 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1976 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1977 | - $serveur, |
|
| 1978 | - $requeter |
|
| 1979 | - ); |
|
| 1948 | + if (!$champs) { |
|
| 1949 | + return; |
|
| 1950 | + } |
|
| 1951 | + if (!$desc) { |
|
| 1952 | + $desc = description_table($table, $serveur); |
|
| 1953 | + } |
|
| 1954 | + if (!$desc) { |
|
| 1955 | + die("$table insertion sans description"); |
|
| 1956 | + } |
|
| 1957 | + $fields = $desc['field']; |
|
| 1958 | + |
|
| 1959 | + $set = []; |
|
| 1960 | + foreach ($champs as $champ => $val) { |
|
| 1961 | + $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, $fields[$champ] ?? ''); |
|
| 1962 | + } |
|
| 1963 | + |
|
| 1964 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1965 | + // attention ils sont deja quotes |
|
| 1966 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1967 | + foreach ($maj as $champ => $val) { |
|
| 1968 | + if (!isset($set[$champ])) { |
|
| 1969 | + $set[$champ] = $champ . '=' . $val; |
|
| 1970 | + } |
|
| 1971 | + } |
|
| 1972 | + |
|
| 1973 | + return spip_sqlite_query( |
|
| 1974 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1975 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1976 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1977 | + $serveur, |
|
| 1978 | + $requeter |
|
| 1979 | + ); |
|
| 1980 | 1980 | } |
| 1981 | 1981 | |
| 1982 | 1982 | |
@@ -1994,17 +1994,17 @@ discard block |
||
| 1994 | 1994 | * @return void |
| 1995 | 1995 | */ |
| 1996 | 1996 | function _sqlite_init() { |
| 1997 | - if (!defined('_DIR_DB')) { |
|
| 1998 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 1999 | - } |
|
| 2000 | - if (!defined('_SQLITE_CHMOD')) { |
|
| 2001 | - define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2002 | - } |
|
| 2003 | - |
|
| 2004 | - if (!is_dir($d = _DIR_DB)) { |
|
| 2005 | - include_spip('inc/flock'); |
|
| 2006 | - sous_repertoire($d); |
|
| 2007 | - } |
|
| 1997 | + if (!defined('_DIR_DB')) { |
|
| 1998 | + define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 1999 | + } |
|
| 2000 | + if (!defined('_SQLITE_CHMOD')) { |
|
| 2001 | + define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2002 | + } |
|
| 2003 | + |
|
| 2004 | + if (!is_dir($d = _DIR_DB)) { |
|
| 2005 | + include_spip('inc/flock'); |
|
| 2006 | + sous_repertoire($d); |
|
| 2007 | + } |
|
| 2008 | 2008 | } |
| 2009 | 2009 | |
| 2010 | 2010 | |
@@ -2018,20 +2018,20 @@ discard block |
||
| 2018 | 2018 | * @return bool|int |
| 2019 | 2019 | */ |
| 2020 | 2020 | function _sqlite_is_version($version = '', $link = '', $serveur = '', $requeter = true) { |
| 2021 | - if ($link === '') { |
|
| 2022 | - $link = _sqlite_link($serveur); |
|
| 2023 | - } |
|
| 2024 | - if (!$link) { |
|
| 2025 | - return false; |
|
| 2026 | - } |
|
| 2021 | + if ($link === '') { |
|
| 2022 | + $link = _sqlite_link($serveur); |
|
| 2023 | + } |
|
| 2024 | + if (!$link) { |
|
| 2025 | + return false; |
|
| 2026 | + } |
|
| 2027 | 2027 | |
| 2028 | - $v = 3; |
|
| 2028 | + $v = 3; |
|
| 2029 | 2029 | |
| 2030 | - if (!$version) { |
|
| 2031 | - return $v; |
|
| 2032 | - } |
|
| 2030 | + if (!$version) { |
|
| 2031 | + return $v; |
|
| 2032 | + } |
|
| 2033 | 2033 | |
| 2034 | - return ($version == $v); |
|
| 2034 | + return ($version == $v); |
|
| 2035 | 2035 | } |
| 2036 | 2036 | |
| 2037 | 2037 | |
@@ -2042,9 +2042,9 @@ discard block |
||
| 2042 | 2042 | * @return \PDO|null Information de connexion pour SQLite |
| 2043 | 2043 | */ |
| 2044 | 2044 | function _sqlite_link($serveur = ''): ?\PDO { |
| 2045 | - $link = &$GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 2045 | + $link = &$GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 2046 | 2046 | |
| 2047 | - return $link; |
|
| 2047 | + return $link; |
|
| 2048 | 2048 | } |
| 2049 | 2049 | |
| 2050 | 2050 | |
@@ -2059,51 +2059,51 @@ discard block |
||
| 2059 | 2059 | * @return string|number texte ou nombre échappé |
| 2060 | 2060 | */ |
| 2061 | 2061 | function _sqlite_calculer_cite($v, $type) { |
| 2062 | - if ($type) { |
|
| 2063 | - if (is_null($v) && stripos($type, 'NOT NULL') === false) { |
|
| 2064 | - // null php se traduit en NULL SQL |
|
| 2065 | - return 'NULL'; |
|
| 2066 | - } |
|
| 2067 | - |
|
| 2068 | - if (sql_test_date($type) && preg_match('/^\w+\(/', $v)) { |
|
| 2069 | - return $v; |
|
| 2070 | - } |
|
| 2071 | - if (sql_test_int($type)) { |
|
| 2072 | - if (is_numeric($v)) { |
|
| 2073 | - return $v; |
|
| 2074 | - } elseif ($v === null) { |
|
| 2075 | - return 0; |
|
| 2076 | - } elseif (ctype_xdigit(substr($v, 2)) && str_starts_with($v, '0x')) { |
|
| 2077 | - return hexdec(substr($v, 2)); |
|
| 2078 | - } else { |
|
| 2079 | - return (int) $v; |
|
| 2080 | - } |
|
| 2081 | - } |
|
| 2082 | - } else { |
|
| 2083 | - // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2084 | - if (is_bool($v)) { |
|
| 2085 | - return (string) (int) $v; |
|
| 2086 | - } elseif (is_numeric($v)) { |
|
| 2087 | - return (string) $v; |
|
| 2088 | - } |
|
| 2089 | - } |
|
| 2090 | - |
|
| 2091 | - // trouver un link sqlite pour faire l'echappement |
|
| 2092 | - foreach ($GLOBALS['connexions'] as $s) { |
|
| 2093 | - if ( |
|
| 2094 | - ($l = $s['link']) |
|
| 2095 | - && is_object($l) |
|
| 2096 | - && $l instanceof \PDO |
|
| 2097 | - && $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2098 | - ) { |
|
| 2099 | - return $l->quote($v ?? ''); |
|
| 2100 | - } |
|
| 2101 | - } |
|
| 2102 | - |
|
| 2103 | - // echapper les ' en '' |
|
| 2104 | - spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2105 | - |
|
| 2106 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2062 | + if ($type) { |
|
| 2063 | + if (is_null($v) && stripos($type, 'NOT NULL') === false) { |
|
| 2064 | + // null php se traduit en NULL SQL |
|
| 2065 | + return 'NULL'; |
|
| 2066 | + } |
|
| 2067 | + |
|
| 2068 | + if (sql_test_date($type) && preg_match('/^\w+\(/', $v)) { |
|
| 2069 | + return $v; |
|
| 2070 | + } |
|
| 2071 | + if (sql_test_int($type)) { |
|
| 2072 | + if (is_numeric($v)) { |
|
| 2073 | + return $v; |
|
| 2074 | + } elseif ($v === null) { |
|
| 2075 | + return 0; |
|
| 2076 | + } elseif (ctype_xdigit(substr($v, 2)) && str_starts_with($v, '0x')) { |
|
| 2077 | + return hexdec(substr($v, 2)); |
|
| 2078 | + } else { |
|
| 2079 | + return (int) $v; |
|
| 2080 | + } |
|
| 2081 | + } |
|
| 2082 | + } else { |
|
| 2083 | + // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2084 | + if (is_bool($v)) { |
|
| 2085 | + return (string) (int) $v; |
|
| 2086 | + } elseif (is_numeric($v)) { |
|
| 2087 | + return (string) $v; |
|
| 2088 | + } |
|
| 2089 | + } |
|
| 2090 | + |
|
| 2091 | + // trouver un link sqlite pour faire l'echappement |
|
| 2092 | + foreach ($GLOBALS['connexions'] as $s) { |
|
| 2093 | + if ( |
|
| 2094 | + ($l = $s['link']) |
|
| 2095 | + && is_object($l) |
|
| 2096 | + && $l instanceof \PDO |
|
| 2097 | + && $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2098 | + ) { |
|
| 2099 | + return $l->quote($v ?? ''); |
|
| 2100 | + } |
|
| 2101 | + } |
|
| 2102 | + |
|
| 2103 | + // echapper les ' en '' |
|
| 2104 | + spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2105 | + |
|
| 2106 | + return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2107 | 2107 | } |
| 2108 | 2108 | |
| 2109 | 2109 | |
@@ -2119,21 +2119,21 @@ discard block |
||
| 2119 | 2119 | * @return string texte de l'expression, une partie donc, du texte la requête. |
| 2120 | 2120 | */ |
| 2121 | 2121 | function _sqlite_calculer_expression($expression, $v, $join = 'AND') { |
| 2122 | - if (empty($v)) { |
|
| 2123 | - return ''; |
|
| 2124 | - } |
|
| 2125 | - |
|
| 2126 | - $exp = "\n$expression "; |
|
| 2127 | - |
|
| 2128 | - if (!is_array($v)) { |
|
| 2129 | - return $exp . $v; |
|
| 2130 | - } else { |
|
| 2131 | - if (strtoupper($join) === 'AND') { |
|
| 2132 | - return $exp . implode("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2133 | - } else { |
|
| 2134 | - return $exp . implode($join, $v); |
|
| 2135 | - } |
|
| 2136 | - } |
|
| 2122 | + if (empty($v)) { |
|
| 2123 | + return ''; |
|
| 2124 | + } |
|
| 2125 | + |
|
| 2126 | + $exp = "\n$expression "; |
|
| 2127 | + |
|
| 2128 | + if (!is_array($v)) { |
|
| 2129 | + return $exp . $v; |
|
| 2130 | + } else { |
|
| 2131 | + if (strtoupper($join) === 'AND') { |
|
| 2132 | + return $exp . implode("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2133 | + } else { |
|
| 2134 | + return $exp . implode($join, $v); |
|
| 2135 | + } |
|
| 2136 | + } |
|
| 2137 | 2137 | } |
| 2138 | 2138 | |
| 2139 | 2139 | |
@@ -2149,7 +2149,7 @@ discard block |
||
| 2149 | 2149 | * @return string texte du orderby préparé |
| 2150 | 2150 | */ |
| 2151 | 2151 | function _sqlite_calculer_order($orderby) { |
| 2152 | - return (is_array($orderby)) ? implode(', ', $orderby) : $orderby; |
|
| 2152 | + return (is_array($orderby)) ? implode(', ', $orderby) : $orderby; |
|
| 2153 | 2153 | } |
| 2154 | 2154 | |
| 2155 | 2155 | |
@@ -2160,26 +2160,26 @@ discard block |
||
| 2160 | 2160 | * @return string Sélection de colonnes pour une clause SELECT |
| 2161 | 2161 | */ |
| 2162 | 2162 | function _sqlite_calculer_select_as($args) { |
| 2163 | - $res = ''; |
|
| 2164 | - foreach ($args as $k => $v) { |
|
| 2165 | - if (str_ends_with($k, '@')) { |
|
| 2166 | - // c'est une jointure qui se refere au from precedent |
|
| 2167 | - // pas de virgule |
|
| 2168 | - $res .= ' ' . $v; |
|
| 2169 | - } else { |
|
| 2170 | - if (!is_numeric($k)) { |
|
| 2171 | - $p = strpos((string) $v, ' '); |
|
| 2172 | - if ($p) { |
|
| 2173 | - $v = substr((string) $v, 0, $p) . " AS '$k'" . substr((string) $v, $p); |
|
| 2174 | - } else { |
|
| 2175 | - $v .= " AS '$k'"; |
|
| 2176 | - } |
|
| 2177 | - } |
|
| 2178 | - $res .= ', ' . $v; |
|
| 2179 | - } |
|
| 2180 | - } |
|
| 2181 | - |
|
| 2182 | - return substr($res, 2); |
|
| 2163 | + $res = ''; |
|
| 2164 | + foreach ($args as $k => $v) { |
|
| 2165 | + if (str_ends_with($k, '@')) { |
|
| 2166 | + // c'est une jointure qui se refere au from precedent |
|
| 2167 | + // pas de virgule |
|
| 2168 | + $res .= ' ' . $v; |
|
| 2169 | + } else { |
|
| 2170 | + if (!is_numeric($k)) { |
|
| 2171 | + $p = strpos((string) $v, ' '); |
|
| 2172 | + if ($p) { |
|
| 2173 | + $v = substr((string) $v, 0, $p) . " AS '$k'" . substr((string) $v, $p); |
|
| 2174 | + } else { |
|
| 2175 | + $v .= " AS '$k'"; |
|
| 2176 | + } |
|
| 2177 | + } |
|
| 2178 | + $res .= ', ' . $v; |
|
| 2179 | + } |
|
| 2180 | + } |
|
| 2181 | + |
|
| 2182 | + return substr($res, 2); |
|
| 2183 | 2183 | } |
| 2184 | 2184 | |
| 2185 | 2185 | |
@@ -2202,26 +2202,26 @@ discard block |
||
| 2202 | 2202 | * Contrainte pour clause WHERE |
| 2203 | 2203 | */ |
| 2204 | 2204 | function _sqlite_calculer_where($v) { |
| 2205 | - if (!is_array($v)) { |
|
| 2206 | - return $v; |
|
| 2207 | - } |
|
| 2208 | - |
|
| 2209 | - $op = array_shift($v); |
|
| 2210 | - if (!($n = count($v))) { |
|
| 2211 | - return $op; |
|
| 2212 | - } else { |
|
| 2213 | - $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2214 | - if ($n == 1) { |
|
| 2215 | - return "$op($arg)"; |
|
| 2216 | - } else { |
|
| 2217 | - $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2218 | - if ($n == 2) { |
|
| 2219 | - return "($arg $op $arg2)"; |
|
| 2220 | - } else { |
|
| 2221 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 2222 | - } |
|
| 2223 | - } |
|
| 2224 | - } |
|
| 2205 | + if (!is_array($v)) { |
|
| 2206 | + return $v; |
|
| 2207 | + } |
|
| 2208 | + |
|
| 2209 | + $op = array_shift($v); |
|
| 2210 | + if (!($n = count($v))) { |
|
| 2211 | + return $op; |
|
| 2212 | + } else { |
|
| 2213 | + $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2214 | + if ($n == 1) { |
|
| 2215 | + return "$op($arg)"; |
|
| 2216 | + } else { |
|
| 2217 | + $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2218 | + if ($n == 2) { |
|
| 2219 | + return "($arg $op $arg2)"; |
|
| 2220 | + } else { |
|
| 2221 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 2222 | + } |
|
| 2223 | + } |
|
| 2224 | + } |
|
| 2225 | 2225 | } |
| 2226 | 2226 | |
| 2227 | 2227 | |
@@ -2236,17 +2236,17 @@ discard block |
||
| 2236 | 2236 | * @return array|bool |
| 2237 | 2237 | */ |
| 2238 | 2238 | function _sqlite_charger_version($version = '') { |
| 2239 | - $versions = []; |
|
| 2239 | + $versions = []; |
|
| 2240 | 2240 | |
| 2241 | - // version 3 |
|
| 2242 | - if ((!$version || $version == 3) && (extension_loaded('pdo') && extension_loaded('pdo_sqlite'))) { |
|
| 2243 | - $versions[] = 3; |
|
| 2244 | - } |
|
| 2245 | - if ($version) { |
|
| 2246 | - return in_array($version, $versions); |
|
| 2247 | - } |
|
| 2241 | + // version 3 |
|
| 2242 | + if ((!$version || $version == 3) && (extension_loaded('pdo') && extension_loaded('pdo_sqlite'))) { |
|
| 2243 | + $versions[] = 3; |
|
| 2244 | + } |
|
| 2245 | + if ($version) { |
|
| 2246 | + return in_array($version, $versions); |
|
| 2247 | + } |
|
| 2248 | 2248 | |
| 2249 | - return $versions; |
|
| 2249 | + return $versions; |
|
| 2250 | 2250 | } |
| 2251 | 2251 | |
| 2252 | 2252 | |
@@ -2284,147 +2284,147 @@ discard block |
||
| 2284 | 2284 | */ |
| 2285 | 2285 | function _sqlite_modifier_table($table, $colonne, $opt = [], $serveur = '') { |
| 2286 | 2286 | |
| 2287 | - if (is_array($table)) { |
|
| 2288 | - $table_destination = reset($table); |
|
| 2289 | - $table_origine = key($table); |
|
| 2290 | - } else { |
|
| 2291 | - $table_origine = $table_destination = $table; |
|
| 2292 | - } |
|
| 2293 | - // ne prend actuellement qu'un changement |
|
| 2294 | - // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2295 | - if (is_array($colonne)) { |
|
| 2296 | - $colonne_destination = reset($colonne); |
|
| 2297 | - $colonne_origine = key($colonne); |
|
| 2298 | - } else { |
|
| 2299 | - $colonne_origine = $colonne_destination = $colonne; |
|
| 2300 | - } |
|
| 2301 | - if (!isset($opt['field'])) { |
|
| 2302 | - $opt['field'] = []; |
|
| 2303 | - } |
|
| 2304 | - if (!isset($opt['key'])) { |
|
| 2305 | - $opt['key'] = []; |
|
| 2306 | - } |
|
| 2307 | - |
|
| 2308 | - // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2309 | - // on prendra directement le nom de la future table |
|
| 2310 | - $meme_table = ($table_origine == $table_destination); |
|
| 2311 | - |
|
| 2312 | - $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2313 | - if (!$def_origine || !isset($def_origine['field'])) { |
|
| 2314 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2315 | - |
|
| 2316 | - return false; |
|
| 2317 | - } |
|
| 2318 | - |
|
| 2319 | - |
|
| 2320 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2321 | - |
|
| 2322 | - // 1) creer une table temporaire avec les modifications |
|
| 2323 | - // - DROP : suppression de la colonne |
|
| 2324 | - // - CHANGE : modification de la colonne |
|
| 2325 | - // (foreach pour conserver l'ordre des champs) |
|
| 2326 | - |
|
| 2327 | - // field |
|
| 2328 | - $fields = []; |
|
| 2329 | - // pour le INSERT INTO plus loin |
|
| 2330 | - // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2331 | - $fields_correspondances = []; |
|
| 2332 | - foreach ($def_origine['field'] as $c => $d) { |
|
| 2333 | - if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2334 | - // si pas DROP |
|
| 2335 | - if ($colonne_destination) { |
|
| 2336 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2337 | - $fields_correspondances[$colonne_destination] = $c; |
|
| 2338 | - } |
|
| 2339 | - } else { |
|
| 2340 | - $fields[$c] = $d; |
|
| 2341 | - $fields_correspondances[$c] = $c; |
|
| 2342 | - } |
|
| 2343 | - } |
|
| 2344 | - // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2345 | - if (!$colonne_origine && $colonne_destination) { |
|
| 2346 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2347 | - } |
|
| 2348 | - |
|
| 2349 | - // key... |
|
| 2350 | - $keys = []; |
|
| 2351 | - foreach ($def_origine['key'] as $c => $d) { |
|
| 2352 | - $c = str_replace($colonne_origine, $colonne_destination, (string) $c); |
|
| 2353 | - $d = str_replace($colonne_origine, $colonne_destination, (string) $d); |
|
| 2354 | - // seulement si on ne supprime pas la colonne ! |
|
| 2355 | - if ($d) { |
|
| 2356 | - $keys[$c] = $d; |
|
| 2357 | - } |
|
| 2358 | - } |
|
| 2359 | - |
|
| 2360 | - // autres keys, on merge |
|
| 2361 | - $keys = array_merge($keys, $opt['key']); |
|
| 2362 | - $queries = []; |
|
| 2363 | - |
|
| 2364 | - // copier dans destination (si differente de origine), sinon tmp |
|
| 2365 | - $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2366 | - $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2367 | - && $keys['PRIMARY KEY'] |
|
| 2368 | - && stripos((string) $keys['PRIMARY KEY'], ',') === false |
|
| 2369 | - && stripos((string) $fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2370 | - |
|
| 2371 | - if ( |
|
| 2372 | - $q = _sqlite_requete_create( |
|
| 2373 | - $table_copie, |
|
| 2374 | - $fields, |
|
| 2375 | - $keys, |
|
| 2376 | - $autoinc, |
|
| 2377 | - $temporary = false, |
|
| 2378 | - $ifnotexists = true, |
|
| 2379 | - $serveur |
|
| 2380 | - ) |
|
| 2381 | - ) { |
|
| 2382 | - $queries[] = $q; |
|
| 2383 | - } |
|
| 2384 | - |
|
| 2385 | - |
|
| 2386 | - // 2) y copier les champs qui vont bien |
|
| 2387 | - $champs_dest = implode(', ', array_keys($fields_correspondances)); |
|
| 2388 | - $champs_ori = implode(', ', $fields_correspondances); |
|
| 2389 | - $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2390 | - |
|
| 2391 | - // 3) supprimer la table d'origine |
|
| 2392 | - $queries[] = "DROP TABLE $table_origine"; |
|
| 2393 | - |
|
| 2394 | - // 4) renommer la table temporaire |
|
| 2395 | - // avec le nom de la table destination |
|
| 2396 | - // si necessaire |
|
| 2397 | - if ($meme_table) { |
|
| 2398 | - $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2399 | - } |
|
| 2400 | - |
|
| 2401 | - // 5) remettre les index ! |
|
| 2402 | - foreach ($keys as $k => $v) { |
|
| 2403 | - if ($k == 'PRIMARY KEY') { |
|
| 2404 | - } else { |
|
| 2405 | - // enlever KEY |
|
| 2406 | - $k = substr($k, 4); |
|
| 2407 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2408 | - } |
|
| 2409 | - } |
|
| 2410 | - |
|
| 2411 | - |
|
| 2412 | - if ($queries !== []) { |
|
| 2413 | - Sqlite::demarrer_transaction($serveur); |
|
| 2414 | - // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2415 | - foreach ($queries as $q) { |
|
| 2416 | - if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2417 | - spip_log('SQLite : ALTER TABLE table :' |
|
| 2418 | - . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2419 | - Sqlite::annuler_transaction($serveur); |
|
| 2420 | - |
|
| 2421 | - return false; |
|
| 2422 | - } |
|
| 2423 | - } |
|
| 2424 | - Sqlite::finir_transaction($serveur); |
|
| 2425 | - } |
|
| 2426 | - |
|
| 2427 | - return true; |
|
| 2287 | + if (is_array($table)) { |
|
| 2288 | + $table_destination = reset($table); |
|
| 2289 | + $table_origine = key($table); |
|
| 2290 | + } else { |
|
| 2291 | + $table_origine = $table_destination = $table; |
|
| 2292 | + } |
|
| 2293 | + // ne prend actuellement qu'un changement |
|
| 2294 | + // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2295 | + if (is_array($colonne)) { |
|
| 2296 | + $colonne_destination = reset($colonne); |
|
| 2297 | + $colonne_origine = key($colonne); |
|
| 2298 | + } else { |
|
| 2299 | + $colonne_origine = $colonne_destination = $colonne; |
|
| 2300 | + } |
|
| 2301 | + if (!isset($opt['field'])) { |
|
| 2302 | + $opt['field'] = []; |
|
| 2303 | + } |
|
| 2304 | + if (!isset($opt['key'])) { |
|
| 2305 | + $opt['key'] = []; |
|
| 2306 | + } |
|
| 2307 | + |
|
| 2308 | + // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2309 | + // on prendra directement le nom de la future table |
|
| 2310 | + $meme_table = ($table_origine == $table_destination); |
|
| 2311 | + |
|
| 2312 | + $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2313 | + if (!$def_origine || !isset($def_origine['field'])) { |
|
| 2314 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2315 | + |
|
| 2316 | + return false; |
|
| 2317 | + } |
|
| 2318 | + |
|
| 2319 | + |
|
| 2320 | + $table_tmp = $table_origine . '_tmp'; |
|
| 2321 | + |
|
| 2322 | + // 1) creer une table temporaire avec les modifications |
|
| 2323 | + // - DROP : suppression de la colonne |
|
| 2324 | + // - CHANGE : modification de la colonne |
|
| 2325 | + // (foreach pour conserver l'ordre des champs) |
|
| 2326 | + |
|
| 2327 | + // field |
|
| 2328 | + $fields = []; |
|
| 2329 | + // pour le INSERT INTO plus loin |
|
| 2330 | + // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2331 | + $fields_correspondances = []; |
|
| 2332 | + foreach ($def_origine['field'] as $c => $d) { |
|
| 2333 | + if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2334 | + // si pas DROP |
|
| 2335 | + if ($colonne_destination) { |
|
| 2336 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2337 | + $fields_correspondances[$colonne_destination] = $c; |
|
| 2338 | + } |
|
| 2339 | + } else { |
|
| 2340 | + $fields[$c] = $d; |
|
| 2341 | + $fields_correspondances[$c] = $c; |
|
| 2342 | + } |
|
| 2343 | + } |
|
| 2344 | + // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2345 | + if (!$colonne_origine && $colonne_destination) { |
|
| 2346 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2347 | + } |
|
| 2348 | + |
|
| 2349 | + // key... |
|
| 2350 | + $keys = []; |
|
| 2351 | + foreach ($def_origine['key'] as $c => $d) { |
|
| 2352 | + $c = str_replace($colonne_origine, $colonne_destination, (string) $c); |
|
| 2353 | + $d = str_replace($colonne_origine, $colonne_destination, (string) $d); |
|
| 2354 | + // seulement si on ne supprime pas la colonne ! |
|
| 2355 | + if ($d) { |
|
| 2356 | + $keys[$c] = $d; |
|
| 2357 | + } |
|
| 2358 | + } |
|
| 2359 | + |
|
| 2360 | + // autres keys, on merge |
|
| 2361 | + $keys = array_merge($keys, $opt['key']); |
|
| 2362 | + $queries = []; |
|
| 2363 | + |
|
| 2364 | + // copier dans destination (si differente de origine), sinon tmp |
|
| 2365 | + $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2366 | + $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2367 | + && $keys['PRIMARY KEY'] |
|
| 2368 | + && stripos((string) $keys['PRIMARY KEY'], ',') === false |
|
| 2369 | + && stripos((string) $fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2370 | + |
|
| 2371 | + if ( |
|
| 2372 | + $q = _sqlite_requete_create( |
|
| 2373 | + $table_copie, |
|
| 2374 | + $fields, |
|
| 2375 | + $keys, |
|
| 2376 | + $autoinc, |
|
| 2377 | + $temporary = false, |
|
| 2378 | + $ifnotexists = true, |
|
| 2379 | + $serveur |
|
| 2380 | + ) |
|
| 2381 | + ) { |
|
| 2382 | + $queries[] = $q; |
|
| 2383 | + } |
|
| 2384 | + |
|
| 2385 | + |
|
| 2386 | + // 2) y copier les champs qui vont bien |
|
| 2387 | + $champs_dest = implode(', ', array_keys($fields_correspondances)); |
|
| 2388 | + $champs_ori = implode(', ', $fields_correspondances); |
|
| 2389 | + $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2390 | + |
|
| 2391 | + // 3) supprimer la table d'origine |
|
| 2392 | + $queries[] = "DROP TABLE $table_origine"; |
|
| 2393 | + |
|
| 2394 | + // 4) renommer la table temporaire |
|
| 2395 | + // avec le nom de la table destination |
|
| 2396 | + // si necessaire |
|
| 2397 | + if ($meme_table) { |
|
| 2398 | + $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2399 | + } |
|
| 2400 | + |
|
| 2401 | + // 5) remettre les index ! |
|
| 2402 | + foreach ($keys as $k => $v) { |
|
| 2403 | + if ($k == 'PRIMARY KEY') { |
|
| 2404 | + } else { |
|
| 2405 | + // enlever KEY |
|
| 2406 | + $k = substr($k, 4); |
|
| 2407 | + $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2408 | + } |
|
| 2409 | + } |
|
| 2410 | + |
|
| 2411 | + |
|
| 2412 | + if ($queries !== []) { |
|
| 2413 | + Sqlite::demarrer_transaction($serveur); |
|
| 2414 | + // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2415 | + foreach ($queries as $q) { |
|
| 2416 | + if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2417 | + spip_log('SQLite : ALTER TABLE table :' |
|
| 2418 | + . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2419 | + Sqlite::annuler_transaction($serveur); |
|
| 2420 | + |
|
| 2421 | + return false; |
|
| 2422 | + } |
|
| 2423 | + } |
|
| 2424 | + Sqlite::finir_transaction($serveur); |
|
| 2425 | + } |
|
| 2426 | + |
|
| 2427 | + return true; |
|
| 2428 | 2428 | } |
| 2429 | 2429 | |
| 2430 | 2430 | |
@@ -2434,61 +2434,61 @@ discard block |
||
| 2434 | 2434 | * @return array |
| 2435 | 2435 | */ |
| 2436 | 2436 | function _sqlite_ref_fonctions() { |
| 2437 | - $fonctions = [ |
|
| 2438 | - 'alter' => 'spip_sqlite_alter', |
|
| 2439 | - 'count' => 'spip_sqlite_count', |
|
| 2440 | - 'countsel' => 'spip_sqlite_countsel', |
|
| 2441 | - 'create' => 'spip_sqlite_create', |
|
| 2442 | - 'create_base' => 'spip_sqlite_create_base', |
|
| 2443 | - 'create_view' => 'spip_sqlite_create_view', |
|
| 2444 | - 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2445 | - 'delete' => 'spip_sqlite_delete', |
|
| 2446 | - 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2447 | - 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2448 | - 'errno' => 'spip_sqlite_errno', |
|
| 2449 | - 'error' => 'spip_sqlite_error', |
|
| 2450 | - 'explain' => 'spip_sqlite_explain', |
|
| 2451 | - 'fetch' => 'spip_sqlite_fetch', |
|
| 2452 | - 'seek' => 'spip_sqlite_seek', |
|
| 2453 | - 'free' => 'spip_sqlite_free', |
|
| 2454 | - 'hex' => 'spip_sqlite_hex', |
|
| 2455 | - 'in' => 'spip_sqlite_in', |
|
| 2456 | - 'insert' => 'spip_sqlite_insert', |
|
| 2457 | - 'insertq' => 'spip_sqlite_insertq', |
|
| 2458 | - 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2459 | - 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2460 | - 'multi' => 'spip_sqlite_multi', |
|
| 2461 | - 'optimize' => 'spip_sqlite_optimize', |
|
| 2462 | - 'query' => 'spip_sqlite_query', |
|
| 2463 | - 'quote' => 'spip_sqlite_quote', |
|
| 2464 | - 'repair' => 'spip_sqlite_repair', |
|
| 2465 | - 'replace' => 'spip_sqlite_replace', |
|
| 2466 | - 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2467 | - 'select' => 'spip_sqlite_select', |
|
| 2468 | - 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2469 | - 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2470 | - 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2471 | - 'showbase' => 'spip_sqlite_showbase', |
|
| 2472 | - 'showtable' => 'spip_sqlite_showtable', |
|
| 2473 | - 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2474 | - 'update' => 'spip_sqlite_update', |
|
| 2475 | - 'updateq' => 'spip_sqlite_updateq', |
|
| 2476 | - 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2477 | - 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2478 | - 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2479 | - ]; |
|
| 2480 | - |
|
| 2481 | - // association de chaque nom http d'un charset aux couples sqlite |
|
| 2482 | - // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2483 | - $charsets = [ |
|
| 2484 | - 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2485 | - //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2486 | - //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2487 | - ]; |
|
| 2488 | - |
|
| 2489 | - $fonctions['charsets'] = $charsets; |
|
| 2490 | - |
|
| 2491 | - return $fonctions; |
|
| 2437 | + $fonctions = [ |
|
| 2438 | + 'alter' => 'spip_sqlite_alter', |
|
| 2439 | + 'count' => 'spip_sqlite_count', |
|
| 2440 | + 'countsel' => 'spip_sqlite_countsel', |
|
| 2441 | + 'create' => 'spip_sqlite_create', |
|
| 2442 | + 'create_base' => 'spip_sqlite_create_base', |
|
| 2443 | + 'create_view' => 'spip_sqlite_create_view', |
|
| 2444 | + 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2445 | + 'delete' => 'spip_sqlite_delete', |
|
| 2446 | + 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2447 | + 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2448 | + 'errno' => 'spip_sqlite_errno', |
|
| 2449 | + 'error' => 'spip_sqlite_error', |
|
| 2450 | + 'explain' => 'spip_sqlite_explain', |
|
| 2451 | + 'fetch' => 'spip_sqlite_fetch', |
|
| 2452 | + 'seek' => 'spip_sqlite_seek', |
|
| 2453 | + 'free' => 'spip_sqlite_free', |
|
| 2454 | + 'hex' => 'spip_sqlite_hex', |
|
| 2455 | + 'in' => 'spip_sqlite_in', |
|
| 2456 | + 'insert' => 'spip_sqlite_insert', |
|
| 2457 | + 'insertq' => 'spip_sqlite_insertq', |
|
| 2458 | + 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2459 | + 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2460 | + 'multi' => 'spip_sqlite_multi', |
|
| 2461 | + 'optimize' => 'spip_sqlite_optimize', |
|
| 2462 | + 'query' => 'spip_sqlite_query', |
|
| 2463 | + 'quote' => 'spip_sqlite_quote', |
|
| 2464 | + 'repair' => 'spip_sqlite_repair', |
|
| 2465 | + 'replace' => 'spip_sqlite_replace', |
|
| 2466 | + 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2467 | + 'select' => 'spip_sqlite_select', |
|
| 2468 | + 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2469 | + 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2470 | + 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2471 | + 'showbase' => 'spip_sqlite_showbase', |
|
| 2472 | + 'showtable' => 'spip_sqlite_showtable', |
|
| 2473 | + 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2474 | + 'update' => 'spip_sqlite_update', |
|
| 2475 | + 'updateq' => 'spip_sqlite_updateq', |
|
| 2476 | + 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2477 | + 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2478 | + 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2479 | + ]; |
|
| 2480 | + |
|
| 2481 | + // association de chaque nom http d'un charset aux couples sqlite |
|
| 2482 | + // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2483 | + $charsets = [ |
|
| 2484 | + 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2485 | + //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2486 | + //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2487 | + ]; |
|
| 2488 | + |
|
| 2489 | + $fonctions['charsets'] = $charsets; |
|
| 2490 | + |
|
| 2491 | + return $fonctions; |
|
| 2492 | 2492 | } |
| 2493 | 2493 | |
| 2494 | 2494 | |
@@ -2500,56 +2500,56 @@ discard block |
||
| 2500 | 2500 | * @return mixed |
| 2501 | 2501 | */ |
| 2502 | 2502 | function _sqlite_remplacements_definitions_table($query, $autoinc = false) { |
| 2503 | - // quelques remplacements |
|
| 2504 | - $num = '(\s*\([0-9]*\))?'; |
|
| 2505 | - $enum = '(\s*\([^\)]*\))?'; |
|
| 2506 | - |
|
| 2507 | - $remplace = [ |
|
| 2508 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2509 | - '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2510 | - '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2511 | - '/auto_increment/is' => '', |
|
| 2512 | - '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2513 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2514 | - '/character set \w+/is' => '', |
|
| 2515 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2516 | - '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2517 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2518 | - '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2519 | - '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2520 | - ]; |
|
| 2521 | - |
|
| 2522 | - // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2523 | - $remplace_autocinc = [ |
|
| 2524 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2525 | - ]; |
|
| 2526 | - // pour les int non autoincrement, il faut un DEFAULT |
|
| 2527 | - $remplace_nonautocinc = [ |
|
| 2528 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2529 | - ]; |
|
| 2530 | - |
|
| 2531 | - if (is_string($query)) { |
|
| 2532 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2533 | - if ($autoinc || preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2534 | - $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2535 | - } else { |
|
| 2536 | - $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2537 | - $query = _sqlite_collate_ci($query); |
|
| 2538 | - } |
|
| 2539 | - } elseif (is_array($query)) { |
|
| 2540 | - foreach ($query as $k => $q) { |
|
| 2541 | - $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', (string) $q)); |
|
| 2542 | - $query[$k] = preg_replace(array_keys($remplace), $remplace, (string) $query[$k]); |
|
| 2543 | - if ($ai) { |
|
| 2544 | - $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2545 | - } else { |
|
| 2546 | - $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2547 | - $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2548 | - } |
|
| 2549 | - } |
|
| 2550 | - } |
|
| 2551 | - |
|
| 2552 | - return $query; |
|
| 2503 | + // quelques remplacements |
|
| 2504 | + $num = '(\s*\([0-9]*\))?'; |
|
| 2505 | + $enum = '(\s*\([^\)]*\))?'; |
|
| 2506 | + |
|
| 2507 | + $remplace = [ |
|
| 2508 | + '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2509 | + '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2510 | + '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2511 | + '/auto_increment/is' => '', |
|
| 2512 | + '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2513 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2514 | + '/character set \w+/is' => '', |
|
| 2515 | + '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2516 | + '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2517 | + '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2518 | + '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2519 | + '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2520 | + ]; |
|
| 2521 | + |
|
| 2522 | + // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2523 | + $remplace_autocinc = [ |
|
| 2524 | + '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2525 | + ]; |
|
| 2526 | + // pour les int non autoincrement, il faut un DEFAULT |
|
| 2527 | + $remplace_nonautocinc = [ |
|
| 2528 | + '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2529 | + ]; |
|
| 2530 | + |
|
| 2531 | + if (is_string($query)) { |
|
| 2532 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2533 | + if ($autoinc || preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2534 | + $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2535 | + } else { |
|
| 2536 | + $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2537 | + $query = _sqlite_collate_ci($query); |
|
| 2538 | + } |
|
| 2539 | + } elseif (is_array($query)) { |
|
| 2540 | + foreach ($query as $k => $q) { |
|
| 2541 | + $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', (string) $q)); |
|
| 2542 | + $query[$k] = preg_replace(array_keys($remplace), $remplace, (string) $query[$k]); |
|
| 2543 | + if ($ai) { |
|
| 2544 | + $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2545 | + } else { |
|
| 2546 | + $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2547 | + $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2548 | + } |
|
| 2549 | + } |
|
| 2550 | + } |
|
| 2551 | + |
|
| 2552 | + return $query; |
|
| 2553 | 2553 | } |
| 2554 | 2554 | |
| 2555 | 2555 | /** |
@@ -2560,17 +2560,17 @@ discard block |
||
| 2560 | 2560 | * @return string |
| 2561 | 2561 | */ |
| 2562 | 2562 | function _sqlite_collate_ci($champ) { |
| 2563 | - if (stripos($champ, 'COLLATE') !== false) { |
|
| 2564 | - return $champ; |
|
| 2565 | - } |
|
| 2566 | - if (stripos($champ, 'BINARY') !== false) { |
|
| 2567 | - return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2568 | - } |
|
| 2569 | - if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2570 | - return $champ . ' COLLATE NOCASE'; |
|
| 2571 | - } |
|
| 2572 | - |
|
| 2573 | - return $champ; |
|
| 2563 | + if (stripos($champ, 'COLLATE') !== false) { |
|
| 2564 | + return $champ; |
|
| 2565 | + } |
|
| 2566 | + if (stripos($champ, 'BINARY') !== false) { |
|
| 2567 | + return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2568 | + } |
|
| 2569 | + if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2570 | + return $champ . ' COLLATE NOCASE'; |
|
| 2571 | + } |
|
| 2572 | + |
|
| 2573 | + return $champ; |
|
| 2574 | 2574 | } |
| 2575 | 2575 | |
| 2576 | 2576 | |
@@ -2589,83 +2589,83 @@ discard block |
||
| 2589 | 2589 | * @return bool|string |
| 2590 | 2590 | */ |
| 2591 | 2591 | function _sqlite_requete_create( |
| 2592 | - $nom, |
|
| 2593 | - $champs, |
|
| 2594 | - $cles, |
|
| 2595 | - $autoinc = false, |
|
| 2596 | - $temporary = false, |
|
| 2597 | - $_ifnotexists = true, |
|
| 2598 | - $serveur = '', |
|
| 2599 | - $requeter = true |
|
| 2592 | + $nom, |
|
| 2593 | + $champs, |
|
| 2594 | + $cles, |
|
| 2595 | + $autoinc = false, |
|
| 2596 | + $temporary = false, |
|
| 2597 | + $_ifnotexists = true, |
|
| 2598 | + $serveur = '', |
|
| 2599 | + $requeter = true |
|
| 2600 | 2600 | ) { |
| 2601 | - $query = $keys = $s = $p = ''; |
|
| 2602 | - |
|
| 2603 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2604 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2605 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 2606 | - return; |
|
| 2607 | - } |
|
| 2608 | - |
|
| 2609 | - // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2610 | - // il faut passer par des create index |
|
| 2611 | - // Il gere par contre primary key ! |
|
| 2612 | - // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2613 | - // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2614 | - $pk = 'PRIMARY KEY'; |
|
| 2615 | - // le champ de cle primaire |
|
| 2616 | - $champ_pk = empty($cles[$pk]) ? '' : $cles[$pk]; |
|
| 2617 | - |
|
| 2618 | - foreach ($champs as $k => $v) { |
|
| 2619 | - if (false !== stripos((string) $v, $pk)) { |
|
| 2620 | - $champ_pk = $k; |
|
| 2621 | - // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2622 | - $champs[$k] = preg_replace("/$pk/is", '', (string) $champs[$k]); |
|
| 2623 | - break; |
|
| 2624 | - } |
|
| 2625 | - } |
|
| 2626 | - |
|
| 2627 | - if ($champ_pk) { |
|
| 2628 | - $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2629 | - } |
|
| 2630 | - // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2631 | - if ( |
|
| 2632 | - isset($champs[$champ_pk]) |
|
| 2633 | - && stripos((string) $champs[$champ_pk], 'default 0') !== false |
|
| 2634 | - ) { |
|
| 2635 | - $champs[$champ_pk] = trim(str_ireplace('default 0', '', (string) $champs[$champ_pk])); |
|
| 2636 | - } |
|
| 2637 | - |
|
| 2638 | - $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2639 | - foreach ($champs as $k => $v) { |
|
| 2640 | - $query .= "$s\n\t\t$k $v"; |
|
| 2641 | - $s = ','; |
|
| 2642 | - } |
|
| 2643 | - |
|
| 2644 | - $ifnotexists = ''; |
|
| 2645 | - if ($_ifnotexists) { |
|
| 2646 | - $version = spip_sqlite_fetch( |
|
| 2647 | - spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2648 | - '', |
|
| 2649 | - $serveur |
|
| 2650 | - ); |
|
| 2651 | - if (!function_exists('spip_version_compare')) { |
|
| 2652 | - include_spip('plugins/installer'); |
|
| 2653 | - } |
|
| 2654 | - |
|
| 2655 | - if ($version && spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2656 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 2657 | - } else { |
|
| 2658 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2659 | - $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2660 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2661 | - return true; |
|
| 2662 | - } |
|
| 2663 | - } |
|
| 2664 | - } |
|
| 2665 | - |
|
| 2666 | - $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2667 | - |
|
| 2668 | - return "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2601 | + $query = $keys = $s = $p = ''; |
|
| 2602 | + |
|
| 2603 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2604 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2605 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 2606 | + return; |
|
| 2607 | + } |
|
| 2608 | + |
|
| 2609 | + // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2610 | + // il faut passer par des create index |
|
| 2611 | + // Il gere par contre primary key ! |
|
| 2612 | + // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2613 | + // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2614 | + $pk = 'PRIMARY KEY'; |
|
| 2615 | + // le champ de cle primaire |
|
| 2616 | + $champ_pk = empty($cles[$pk]) ? '' : $cles[$pk]; |
|
| 2617 | + |
|
| 2618 | + foreach ($champs as $k => $v) { |
|
| 2619 | + if (false !== stripos((string) $v, $pk)) { |
|
| 2620 | + $champ_pk = $k; |
|
| 2621 | + // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2622 | + $champs[$k] = preg_replace("/$pk/is", '', (string) $champs[$k]); |
|
| 2623 | + break; |
|
| 2624 | + } |
|
| 2625 | + } |
|
| 2626 | + |
|
| 2627 | + if ($champ_pk) { |
|
| 2628 | + $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2629 | + } |
|
| 2630 | + // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2631 | + if ( |
|
| 2632 | + isset($champs[$champ_pk]) |
|
| 2633 | + && stripos((string) $champs[$champ_pk], 'default 0') !== false |
|
| 2634 | + ) { |
|
| 2635 | + $champs[$champ_pk] = trim(str_ireplace('default 0', '', (string) $champs[$champ_pk])); |
|
| 2636 | + } |
|
| 2637 | + |
|
| 2638 | + $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2639 | + foreach ($champs as $k => $v) { |
|
| 2640 | + $query .= "$s\n\t\t$k $v"; |
|
| 2641 | + $s = ','; |
|
| 2642 | + } |
|
| 2643 | + |
|
| 2644 | + $ifnotexists = ''; |
|
| 2645 | + if ($_ifnotexists) { |
|
| 2646 | + $version = spip_sqlite_fetch( |
|
| 2647 | + spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2648 | + '', |
|
| 2649 | + $serveur |
|
| 2650 | + ); |
|
| 2651 | + if (!function_exists('spip_version_compare')) { |
|
| 2652 | + include_spip('plugins/installer'); |
|
| 2653 | + } |
|
| 2654 | + |
|
| 2655 | + if ($version && spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2656 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 2657 | + } else { |
|
| 2658 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2659 | + $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2660 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 2661 | + return true; |
|
| 2662 | + } |
|
| 2663 | + } |
|
| 2664 | + } |
|
| 2665 | + |
|
| 2666 | + $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2667 | + |
|
| 2668 | + return "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2669 | 2669 | } |
| 2670 | 2670 | |
| 2671 | 2671 | |
@@ -2684,40 +2684,40 @@ discard block |
||
| 2684 | 2684 | * @return |
| 2685 | 2685 | */ |
| 2686 | 2686 | function _sqlite_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 2687 | - static $tables = []; |
|
| 2688 | - |
|
| 2689 | - if (!isset($tables[$table])) { |
|
| 2690 | - if (!$desc) { |
|
| 2691 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2692 | - $desc = $trouver_table($table, $serveur); |
|
| 2693 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 2694 | - if (!$desc) { |
|
| 2695 | - return $couples; |
|
| 2696 | - } |
|
| 2697 | - } |
|
| 2698 | - |
|
| 2699 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2700 | - // cependant, il faudra peut etre etendre |
|
| 2701 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 2702 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 2703 | - $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2704 | - |
|
| 2705 | - $now = _sqlite_func_now(true); |
|
| 2706 | - foreach ($desc['field'] as $k => $v) { |
|
| 2707 | - if (str_starts_with(strtolower(ltrim((string) $v)), 'timestamp')) { |
|
| 2708 | - $tables[$table]['desc'][$k] = $v; |
|
| 2709 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2710 | - } |
|
| 2711 | - } |
|
| 2712 | - } else { |
|
| 2713 | - $now = _sqlite_func_now(true); |
|
| 2714 | - foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2715 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2716 | - } |
|
| 2717 | - } |
|
| 2718 | - |
|
| 2719 | - // ajout des champs type 'timestamp' absents |
|
| 2720 | - return array_merge($tables[$table]['valeur'], $couples); |
|
| 2687 | + static $tables = []; |
|
| 2688 | + |
|
| 2689 | + if (!isset($tables[$table])) { |
|
| 2690 | + if (!$desc) { |
|
| 2691 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2692 | + $desc = $trouver_table($table, $serveur); |
|
| 2693 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 2694 | + if (!$desc) { |
|
| 2695 | + return $couples; |
|
| 2696 | + } |
|
| 2697 | + } |
|
| 2698 | + |
|
| 2699 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2700 | + // cependant, il faudra peut etre etendre |
|
| 2701 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 2702 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 2703 | + $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2704 | + |
|
| 2705 | + $now = _sqlite_func_now(true); |
|
| 2706 | + foreach ($desc['field'] as $k => $v) { |
|
| 2707 | + if (str_starts_with(strtolower(ltrim((string) $v)), 'timestamp')) { |
|
| 2708 | + $tables[$table]['desc'][$k] = $v; |
|
| 2709 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2710 | + } |
|
| 2711 | + } |
|
| 2712 | + } else { |
|
| 2713 | + $now = _sqlite_func_now(true); |
|
| 2714 | + foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2715 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2716 | + } |
|
| 2717 | + } |
|
| 2718 | + |
|
| 2719 | + // ajout des champs type 'timestamp' absents |
|
| 2720 | + return array_merge($tables[$table]['valeur'], $couples); |
|
| 2721 | 2721 | } |
| 2722 | 2722 | |
| 2723 | 2723 | |
@@ -2728,5 +2728,5 @@ discard block |
||
| 2728 | 2728 | * @return array|bool |
| 2729 | 2729 | */ |
| 2730 | 2730 | function spip_versions_sqlite() { |
| 2731 | - return _sqlite_charger_version(); |
|
| 2731 | + return _sqlite_charger_version(); |
|
| 2732 | 2732 | } |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | /** Drapeau indiquant que l'on est dans l'espace privé */ |
| 19 | 19 | define('_ESPACE_PRIVE', true); |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - include __DIR__ . '/inc_version.php'; |
|
| 21 | + include __DIR__ . '/inc_version.php'; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | include_spip('inc/cookie'); |
@@ -34,18 +34,18 @@ discard block |
||
| 34 | 34 | // alors il faut blinder les variables d'URL |
| 35 | 35 | // |
| 36 | 36 | if (autoriser_sans_cookie($exec, false)) { |
| 37 | - if (!isset($reinstall)) { |
|
| 38 | - $reinstall = 'non'; |
|
| 39 | - } |
|
| 40 | - $var_auth = true; |
|
| 37 | + if (!isset($reinstall)) { |
|
| 38 | + $reinstall = 'non'; |
|
| 39 | + } |
|
| 40 | + $var_auth = true; |
|
| 41 | 41 | } else { |
| 42 | - // Authentification, redefinissable |
|
| 43 | - $auth = charger_fonction('auth', 'inc'); |
|
| 44 | - $var_auth = $auth(); |
|
| 45 | - if ($var_auth) { |
|
| 46 | - echo auth_echec($var_auth); |
|
| 47 | - exit; |
|
| 48 | - } |
|
| 42 | + // Authentification, redefinissable |
|
| 43 | + $auth = charger_fonction('auth', 'inc'); |
|
| 44 | + $var_auth = $auth(); |
|
| 45 | + if ($var_auth) { |
|
| 46 | + echo auth_echec($var_auth); |
|
| 47 | + exit; |
|
| 48 | + } |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | // initialiser a la langue par defaut |
@@ -56,29 +56,29 @@ discard block |
||
| 56 | 56 | |
| 57 | 57 | |
| 58 | 58 | if ( |
| 59 | - (_request('action') || _request('var_ajax') || _request('formulaire_action')) |
|
| 60 | - && !autoriser_sans_cookie($exec) |
|
| 59 | + (_request('action') || _request('var_ajax') || _request('formulaire_action')) |
|
| 60 | + && !autoriser_sans_cookie($exec) |
|
| 61 | 61 | ) { |
| 62 | - // Charger l'aiguilleur qui va mettre sur la bonne voie les traitements derogatoires |
|
| 63 | - include_spip('public/aiguiller'); |
|
| 64 | - if ( |
|
| 65 | - // cas des appels actions ?action=xxx |
|
| 66 | - traiter_appels_actions() |
|
| 67 | - // cas des hits ajax sur les inclusions ajax |
|
| 68 | - || traiter_appels_inclusions_ajax() |
|
| 69 | - // cas des formulaires charger/verifier/traiter |
|
| 70 | - || traiter_formulaires_dynamiques() |
|
| 71 | - ) { |
|
| 72 | - // le hit est fini ! |
|
| 73 | - exit; |
|
| 74 | - } |
|
| 62 | + // Charger l'aiguilleur qui va mettre sur la bonne voie les traitements derogatoires |
|
| 63 | + include_spip('public/aiguiller'); |
|
| 64 | + if ( |
|
| 65 | + // cas des appels actions ?action=xxx |
|
| 66 | + traiter_appels_actions() |
|
| 67 | + // cas des hits ajax sur les inclusions ajax |
|
| 68 | + || traiter_appels_inclusions_ajax() |
|
| 69 | + // cas des formulaires charger/verifier/traiter |
|
| 70 | + || traiter_formulaires_dynamiques() |
|
| 71 | + ) { |
|
| 72 | + // le hit est fini ! |
|
| 73 | + exit; |
|
| 74 | + } |
|
| 75 | 75 | } |
| 76 | 76 | // securiser les redirect du back-office |
| 77 | 77 | if (_request('redirect')) { |
| 78 | - if (!function_exists('securiser_redirect_action')) { |
|
| 79 | - include_spip('public/aiguiller'); |
|
| 80 | - } |
|
| 81 | - set_request('redirect', securiser_redirect_action(_request('redirect'))); |
|
| 78 | + if (!function_exists('securiser_redirect_action')) { |
|
| 79 | + include_spip('public/aiguiller'); |
|
| 80 | + } |
|
| 81 | + set_request('redirect', securiser_redirect_action(_request('redirect'))); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | |
@@ -88,12 +88,12 @@ discard block |
||
| 88 | 88 | |
| 89 | 89 | // Controle de la version, sauf si on est deja en train de s'en occuper |
| 90 | 90 | if ( |
| 91 | - !$reinstall == 'oui' |
|
| 92 | - && !_AJAX |
|
| 93 | - && isset($GLOBALS['meta']['version_installee']) |
|
| 94 | - && $GLOBALS['spip_version_base'] != str_replace(',', '.', (string) $GLOBALS['meta']['version_installee']) |
|
| 91 | + !$reinstall == 'oui' |
|
| 92 | + && !_AJAX |
|
| 93 | + && isset($GLOBALS['meta']['version_installee']) |
|
| 94 | + && $GLOBALS['spip_version_base'] != str_replace(',', '.', (string) $GLOBALS['meta']['version_installee']) |
|
| 95 | 95 | ) { |
| 96 | - $exec = 'demande_mise_a_jour'; |
|
| 96 | + $exec = 'demande_mise_a_jour'; |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | // Quand une action d'administration est en cours (meta "admin"), |
@@ -103,38 +103,38 @@ discard block |
||
| 103 | 103 | // sinon c'est qu'elle a ete interrompue et il faut la reprendre |
| 104 | 104 | |
| 105 | 105 | elseif (isset($GLOBALS['meta']['admin'])) { |
| 106 | - if (preg_match('/^(.*)_(\d+)_/', (string) $GLOBALS['meta']['admin'], $l)) { |
|
| 107 | - [, $var_f, $n] = $l; |
|
| 108 | - } |
|
| 109 | - if ( |
|
| 110 | - _AJAX |
|
| 111 | - || !isset($_COOKIE['spip_admin']) |
|
| 112 | - && !(isset($GLOBALS['visiteur_session']) |
|
| 113 | - && $GLOBALS['visiteur_session']['statut'] == '0minirezo') |
|
| 114 | - ) { |
|
| 115 | - spip_log('Quand la meta admin vaut ' . |
|
| 116 | - $GLOBALS['meta']['admin'] . |
|
| 117 | - ' seul un admin peut se connecter et sans AJAX.' . |
|
| 118 | - ' En cas de probleme, detruire cette meta.'); |
|
| 119 | - die(_T('info_travaux_texte')); |
|
| 120 | - } |
|
| 121 | - if ($n) { |
|
| 122 | - [, $var_f, $n] = $l; |
|
| 123 | - if (tester_url_ecrire("base_$var_f")) { |
|
| 124 | - $var_f = "base_$var_f"; |
|
| 125 | - } |
|
| 126 | - if ($var_f !== $exec) { |
|
| 127 | - spip_log("Le script $var_f lance par auteur$n se substitue a l'exec $exec"); |
|
| 128 | - $exec = $var_f; |
|
| 129 | - set_request('exec', $exec); |
|
| 130 | - } |
|
| 131 | - } |
|
| 106 | + if (preg_match('/^(.*)_(\d+)_/', (string) $GLOBALS['meta']['admin'], $l)) { |
|
| 107 | + [, $var_f, $n] = $l; |
|
| 108 | + } |
|
| 109 | + if ( |
|
| 110 | + _AJAX |
|
| 111 | + || !isset($_COOKIE['spip_admin']) |
|
| 112 | + && !(isset($GLOBALS['visiteur_session']) |
|
| 113 | + && $GLOBALS['visiteur_session']['statut'] == '0minirezo') |
|
| 114 | + ) { |
|
| 115 | + spip_log('Quand la meta admin vaut ' . |
|
| 116 | + $GLOBALS['meta']['admin'] . |
|
| 117 | + ' seul un admin peut se connecter et sans AJAX.' . |
|
| 118 | + ' En cas de probleme, detruire cette meta.'); |
|
| 119 | + die(_T('info_travaux_texte')); |
|
| 120 | + } |
|
| 121 | + if ($n) { |
|
| 122 | + [, $var_f, $n] = $l; |
|
| 123 | + if (tester_url_ecrire("base_$var_f")) { |
|
| 124 | + $var_f = "base_$var_f"; |
|
| 125 | + } |
|
| 126 | + if ($var_f !== $exec) { |
|
| 127 | + spip_log("Le script $var_f lance par auteur$n se substitue a l'exec $exec"); |
|
| 128 | + $exec = $var_f; |
|
| 129 | + set_request('exec', $exec); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | 132 | } |
| 133 | 133 | // si nom pas plausible, prendre le script par defaut |
| 134 | 134 | // attention aux deux cas 404/403 qui commencent par un 4 ! |
| 135 | 135 | elseif (!preg_match(',^[a-z4_][0-9a-z_-]*$,i', $exec)) { |
| 136 | - $exec = 'accueil'; |
|
| 137 | - set_request('exec', $exec); |
|
| 136 | + $exec = 'accueil'; |
|
| 137 | + set_request('exec', $exec); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | // si la langue est specifiee par cookie et ne correspond pas |
@@ -142,20 +142,20 @@ discard block |
||
| 142 | 142 | // on appelle directement la fonction, car un appel d'action peut conduire a une boucle infinie |
| 143 | 143 | // si le cookie n'est pas pose correctement dans l'action |
| 144 | 144 | if ( |
| 145 | - !$var_auth |
|
| 146 | - && isset($_COOKIE['spip_lang_ecrire']) |
|
| 147 | - && $_COOKIE['spip_lang_ecrire'] != $GLOBALS['visiteur_session']['lang'] |
|
| 145 | + !$var_auth |
|
| 146 | + && isset($_COOKIE['spip_lang_ecrire']) |
|
| 147 | + && $_COOKIE['spip_lang_ecrire'] != $GLOBALS['visiteur_session']['lang'] |
|
| 148 | 148 | ) { |
| 149 | - include_spip('action/converser'); |
|
| 150 | - action_converser_post($GLOBALS['visiteur_session']['lang'], true); |
|
| 149 | + include_spip('action/converser'); |
|
| 150 | + action_converser_post($GLOBALS['visiteur_session']['lang'], true); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | if ($var_f = tester_url_ecrire($exec)) { |
| 154 | - $var_f = charger_fonction($var_f); |
|
| 155 | - $var_f(); // at last |
|
| 154 | + $var_f = charger_fonction($var_f); |
|
| 155 | + $var_f(); // at last |
|
| 156 | 156 | } else { |
| 157 | - // Rien de connu: rerouter vers exec=404 au lieu d'echouer |
|
| 158 | - // ce qui permet de laisser la main a un plugin |
|
| 159 | - $var_f = charger_fonction('404'); |
|
| 160 | - $var_f($exec); |
|
| 157 | + // Rien de connu: rerouter vers exec=404 au lieu d'echouer |
|
| 158 | + // ce qui permet de laisser la main a un plugin |
|
| 159 | + $var_f = charger_fonction('404'); |
|
| 160 | + $var_f($exec); |
|
| 161 | 161 | } |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | /** Drapeau indiquant que l'on est dans l'espace privé */ |
| 19 | 19 | define('_ESPACE_PRIVE', true); |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - include __DIR__ . '/inc_version.php'; |
|
| 21 | + include __DIR__.'/inc_version.php'; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | include_spip('inc/cookie'); |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | // Determiner l'action demandee |
| 28 | 28 | // |
| 29 | 29 | |
| 30 | -$exec = (string)_request('exec'); |
|
| 30 | +$exec = (string) _request('exec'); |
|
| 31 | 31 | $reinstall = (is_null(_request('reinstall'))) ? ($exec == 'install' ? 'oui' : null) : _request('reinstall'); |
| 32 | 32 | // |
| 33 | 33 | // Les scripts d'insallation n'authentifient pas, forcement, |
@@ -112,9 +112,9 @@ discard block |
||
| 112 | 112 | && !(isset($GLOBALS['visiteur_session']) |
| 113 | 113 | && $GLOBALS['visiteur_session']['statut'] == '0minirezo') |
| 114 | 114 | ) { |
| 115 | - spip_log('Quand la meta admin vaut ' . |
|
| 116 | - $GLOBALS['meta']['admin'] . |
|
| 117 | - ' seul un admin peut se connecter et sans AJAX.' . |
|
| 115 | + spip_log('Quand la meta admin vaut '. |
|
| 116 | + $GLOBALS['meta']['admin']. |
|
| 117 | + ' seul un admin peut se connecter et sans AJAX.'. |
|
| 118 | 118 | ' En cas de probleme, detruire cette meta.'); |
| 119 | 119 | die(_T('info_travaux_texte')); |
| 120 | 120 | } |
@@ -16,40 +16,40 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * Exec de la page de destruction des tables de SPIP |
| 24 | 24 | **/ |
| 25 | 25 | function exec_base_delete_all_dist() { |
| 26 | - include_spip('inc/autoriser'); |
|
| 27 | - if (!autoriser('detruire')) { |
|
| 28 | - include_spip('inc/minipres'); |
|
| 29 | - echo minipres(); |
|
| 30 | - } else { |
|
| 31 | - include_spip('base/dump'); |
|
| 32 | - $res = base_lister_toutes_tables('', [], [], true); |
|
| 33 | - if (!$res) { |
|
| 34 | - include_spip('inc/minipres'); |
|
| 35 | - spip_log('Erreur base de donnees'); |
|
| 36 | - echo minipres( |
|
| 37 | - _T('info_travaux_titre'), |
|
| 38 | - _T('titre_probleme_technique') . '<p><tt>' . sql_errno() . ' ' . sql_error() . '</tt></p>' |
|
| 39 | - ); |
|
| 40 | - } else { |
|
| 41 | - $res = base_saisie_tables('delete', $res); |
|
| 42 | - include_spip('inc/headers'); |
|
| 43 | - $res = "\n<ol style='text-align:left'><li>\n" . |
|
| 44 | - implode("</li>\n<li>", $res) . |
|
| 45 | - '</li></ol>'; |
|
| 46 | - $admin = charger_fonction('admin', 'inc'); |
|
| 47 | - $res = $admin('delete_all', _T('titre_page_delete_all'), $res); |
|
| 48 | - if (!$res) { |
|
| 49 | - redirige_url_ecrire('install', ''); |
|
| 50 | - } else { |
|
| 51 | - echo $res; |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - } |
|
| 26 | + include_spip('inc/autoriser'); |
|
| 27 | + if (!autoriser('detruire')) { |
|
| 28 | + include_spip('inc/minipres'); |
|
| 29 | + echo minipres(); |
|
| 30 | + } else { |
|
| 31 | + include_spip('base/dump'); |
|
| 32 | + $res = base_lister_toutes_tables('', [], [], true); |
|
| 33 | + if (!$res) { |
|
| 34 | + include_spip('inc/minipres'); |
|
| 35 | + spip_log('Erreur base de donnees'); |
|
| 36 | + echo minipres( |
|
| 37 | + _T('info_travaux_titre'), |
|
| 38 | + _T('titre_probleme_technique') . '<p><tt>' . sql_errno() . ' ' . sql_error() . '</tt></p>' |
|
| 39 | + ); |
|
| 40 | + } else { |
|
| 41 | + $res = base_saisie_tables('delete', $res); |
|
| 42 | + include_spip('inc/headers'); |
|
| 43 | + $res = "\n<ol style='text-align:left'><li>\n" . |
|
| 44 | + implode("</li>\n<li>", $res) . |
|
| 45 | + '</li></ol>'; |
|
| 46 | + $admin = charger_fonction('admin', 'inc'); |
|
| 47 | + $res = $admin('delete_all', _T('titre_page_delete_all'), $res); |
|
| 48 | + if (!$res) { |
|
| 49 | + redirige_url_ecrire('install', ''); |
|
| 50 | + } else { |
|
| 51 | + echo $res; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | 55 | } |
@@ -35,13 +35,13 @@ |
||
| 35 | 35 | spip_log('Erreur base de donnees'); |
| 36 | 36 | echo minipres( |
| 37 | 37 | _T('info_travaux_titre'), |
| 38 | - _T('titre_probleme_technique') . '<p><tt>' . sql_errno() . ' ' . sql_error() . '</tt></p>' |
|
| 38 | + _T('titre_probleme_technique').'<p><tt>'.sql_errno().' '.sql_error().'</tt></p>' |
|
| 39 | 39 | ); |
| 40 | 40 | } else { |
| 41 | 41 | $res = base_saisie_tables('delete', $res); |
| 42 | 42 | include_spip('inc/headers'); |
| 43 | - $res = "\n<ol style='text-align:left'><li>\n" . |
|
| 44 | - implode("</li>\n<li>", $res) . |
|
| 43 | + $res = "\n<ol style='text-align:left'><li>\n". |
|
| 44 | + implode("</li>\n<li>", $res). |
|
| 45 | 45 | '</li></ol>'; |
| 46 | 46 | $admin = charger_fonction('admin', 'inc'); |
| 47 | 47 | $res = $admin('delete_all', _T('titre_page_delete_all'), $res); |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | **/ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | include_spip('inc/presentation'); |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * @uses exec_puce_statut_args() |
| 31 | 31 | **/ |
| 32 | 32 | function exec_puce_statut_dist(): void { |
| 33 | - exec_puce_statut_args(_request('id'), _request('type')); |
|
| 33 | + exec_puce_statut_args(_request('id'), _request('type')); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /** |
@@ -49,22 +49,22 @@ discard block |
||
| 49 | 49 | * Type d'objet |
| 50 | 50 | **/ |
| 51 | 51 | function exec_puce_statut_args($id, $type): void { |
| 52 | - $id = (int) $id; |
|
| 53 | - if ( |
|
| 54 | - ($table_objet_sql = table_objet_sql($type)) |
|
| 55 | - && ($d = lister_tables_objets_sql($table_objet_sql)) |
|
| 56 | - && isset($d['statut_textes_instituer']) |
|
| 57 | - && $d['statut_textes_instituer'] |
|
| 58 | - ) { |
|
| 59 | - $prim = id_table_objet($type); |
|
| 60 | - $select = isset($d['field']['id_rubrique']) ? 'id_rubrique,statut' : '0 as id_rubrique,statut'; |
|
| 61 | - $r = sql_fetsel($select, $table_objet_sql, "$prim=$id"); |
|
| 62 | - $statut = $r['statut']; |
|
| 63 | - $id_rubrique = $r['id_rubrique']; |
|
| 64 | - } else { |
|
| 65 | - $id_rubrique = $id; |
|
| 66 | - $statut = 'prop'; // arbitraire |
|
| 67 | - } |
|
| 68 | - $puce_statut = charger_fonction('puce_statut', 'inc'); |
|
| 69 | - ajax_retour($puce_statut($id, $statut, $id_rubrique, $type, true)); |
|
| 52 | + $id = (int) $id; |
|
| 53 | + if ( |
|
| 54 | + ($table_objet_sql = table_objet_sql($type)) |
|
| 55 | + && ($d = lister_tables_objets_sql($table_objet_sql)) |
|
| 56 | + && isset($d['statut_textes_instituer']) |
|
| 57 | + && $d['statut_textes_instituer'] |
|
| 58 | + ) { |
|
| 59 | + $prim = id_table_objet($type); |
|
| 60 | + $select = isset($d['field']['id_rubrique']) ? 'id_rubrique,statut' : '0 as id_rubrique,statut'; |
|
| 61 | + $r = sql_fetsel($select, $table_objet_sql, "$prim=$id"); |
|
| 62 | + $statut = $r['statut']; |
|
| 63 | + $id_rubrique = $r['id_rubrique']; |
|
| 64 | + } else { |
|
| 65 | + $id_rubrique = $id; |
|
| 66 | + $statut = 'prop'; // arbitraire |
|
| 67 | + } |
|
| 68 | + $puce_statut = charger_fonction('puce_statut', 'inc'); |
|
| 69 | + ajax_retour($puce_statut($id, $statut, $id_rubrique, $type, true)); |
|
| 70 | 70 | } |