@@ -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,148 +1145,148 @@ 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 = '', $serveur = '', $requeter = true) { |
| 1273 | - if ($exist) { |
|
| 1274 | - $exist = ' IF EXISTS'; |
|
| 1275 | - } |
|
| 1276 | - if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1277 | - return true; |
|
| 1278 | - } else { |
|
| 1279 | - return false; |
|
| 1280 | - } |
|
| 1273 | + if ($exist) { |
|
| 1274 | + $exist = ' IF EXISTS'; |
|
| 1275 | + } |
|
| 1276 | + if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1277 | + return true; |
|
| 1278 | + } else { |
|
| 1279 | + return false; |
|
| 1280 | + } |
|
| 1281 | 1281 | } |
| 1282 | 1282 | |
| 1283 | 1283 | // supprime une vue |
| 1284 | 1284 | function spip_pg_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 1285 | - if ($exist) { |
|
| 1286 | - $exist = ' IF EXISTS'; |
|
| 1287 | - } |
|
| 1285 | + if ($exist) { |
|
| 1286 | + $exist = ' IF EXISTS'; |
|
| 1287 | + } |
|
| 1288 | 1288 | |
| 1289 | - return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1289 | + return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1290 | 1290 | } |
| 1291 | 1291 | |
| 1292 | 1292 | /** |
@@ -1303,40 +1303,40 @@ discard block |
||
| 1303 | 1303 | * Ressource à utiliser avec sql_fetch() |
| 1304 | 1304 | **/ |
| 1305 | 1305 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1306 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1307 | - $link = $connexion['link']; |
|
| 1308 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1306 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1307 | + $link = $connexion['link']; |
|
| 1308 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1309 | 1309 | |
| 1310 | - return spip_pg_query_simple($link, $q); |
|
| 1310 | + return spip_pg_query_simple($link, $q); |
|
| 1311 | 1311 | } |
| 1312 | 1312 | |
| 1313 | 1313 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1314 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 1315 | - $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); |
|
| 1317 | - |
|
| 1318 | - $res = spip_pg_query_simple($link, $q); |
|
| 1319 | - if (!$res) { |
|
| 1320 | - return false; |
|
| 1321 | - } |
|
| 1322 | - |
|
| 1323 | - // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1324 | - // il faut en tenir compte dans le return |
|
| 1325 | - $fields = []; |
|
| 1326 | - while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1327 | - $fields[$field[0]] = $field[2] . ($field[1] ? ' DEFAULT ' . $field[1] : ('')); |
|
| 1328 | - } |
|
| 1329 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1330 | - $res = spip_pg_query_simple($link, $q); |
|
| 1331 | - $keys = []; |
|
| 1332 | - while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1333 | - 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]; |
|
| 1336 | - } |
|
| 1337 | - } |
|
| 1338 | - |
|
| 1339 | - return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1314 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 1315 | + $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); |
|
| 1317 | + |
|
| 1318 | + $res = spip_pg_query_simple($link, $q); |
|
| 1319 | + if (!$res) { |
|
| 1320 | + return false; |
|
| 1321 | + } |
|
| 1322 | + |
|
| 1323 | + // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1324 | + // il faut en tenir compte dans le return |
|
| 1325 | + $fields = []; |
|
| 1326 | + while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1327 | + $fields[$field[0]] = $field[2] . ($field[1] ? ' DEFAULT ' . $field[1] : ('')); |
|
| 1328 | + } |
|
| 1329 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1330 | + $res = spip_pg_query_simple($link, $q); |
|
| 1331 | + $keys = []; |
|
| 1332 | + while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1333 | + 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]; |
|
| 1336 | + } |
|
| 1337 | + } |
|
| 1338 | + |
|
| 1339 | + return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1340 | 1340 | } |
| 1341 | 1341 | |
| 1342 | 1342 | // Fonction de creation d'une table SQL nommee $nom |
@@ -1347,114 +1347,114 @@ discard block |
||
| 1347 | 1347 | // Le nom des index est prefixe par celui de la table pour eviter les conflits |
| 1348 | 1348 | function spip_pg_create($nom, $champs, $cles, $autoinc = false, $temporary = false, $serveur = '', $requeter = true) { |
| 1349 | 1349 | |
| 1350 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 1351 | - $link = $connexion['link']; |
|
| 1352 | - $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1353 | - |
|
| 1354 | - $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1355 | - $keys = []; |
|
| 1356 | - |
|
| 1357 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1358 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1359 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 1360 | - return; |
|
| 1361 | - } |
|
| 1362 | - |
|
| 1363 | - foreach ($cles as $k => $v) { |
|
| 1364 | - if (str_starts_with($k, 'KEY ')) { |
|
| 1365 | - $n = str_replace('`', '', $k); |
|
| 1366 | - $v = str_replace('`', '"', (string) $v); |
|
| 1367 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1368 | - if ($k != $n) { |
|
| 1369 | - $i = "\"$i\""; |
|
| 1370 | - } |
|
| 1371 | - $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1372 | - } elseif (str_starts_with($k, 'UNIQUE ')) { |
|
| 1373 | - $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1374 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1375 | - } else { |
|
| 1376 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1377 | - } |
|
| 1378 | - if ($k == 'PRIMARY KEY') { |
|
| 1379 | - $prim_name = $v; |
|
| 1380 | - } |
|
| 1381 | - $s = ','; |
|
| 1382 | - } |
|
| 1383 | - $s = ''; |
|
| 1384 | - |
|
| 1385 | - $character_set = ''; |
|
| 1386 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1387 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1388 | - } |
|
| 1389 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1390 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1391 | - } |
|
| 1392 | - |
|
| 1393 | - foreach ($champs as $k => $v) { |
|
| 1394 | - $k = str_replace('`', '"', $k); |
|
| 1395 | - 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])); |
|
| 1397 | - } |
|
| 1398 | - |
|
| 1399 | - $query .= "$s\n\t\t$k " |
|
| 1400 | - . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', (string) $v)) |
|
| 1401 | - ? ' bigserial' |
|
| 1402 | - : mysql2pg_type($v) |
|
| 1403 | - ); |
|
| 1404 | - $s = ','; |
|
| 1405 | - } |
|
| 1406 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1407 | - |
|
| 1408 | - // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1409 | - |
|
| 1410 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1411 | - ($character_set ? " DEFAULT $character_set" : '') |
|
| 1412 | - . "\n"; |
|
| 1413 | - |
|
| 1414 | - if (!$requeter) { |
|
| 1415 | - return $q; |
|
| 1416 | - } |
|
| 1417 | - $connexion['last'] = $q; |
|
| 1418 | - $r = @pg_query($link, $q); |
|
| 1419 | - |
|
| 1420 | - if (!$r) { |
|
| 1421 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1422 | - } else { |
|
| 1423 | - foreach ($keys as $index) { |
|
| 1424 | - pg_query($link, $index); |
|
| 1425 | - } |
|
| 1426 | - } |
|
| 1427 | - |
|
| 1428 | - return $r; |
|
| 1350 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower((string) $serveur) : 0]; |
|
| 1351 | + $link = $connexion['link']; |
|
| 1352 | + $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1353 | + |
|
| 1354 | + $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1355 | + $keys = []; |
|
| 1356 | + |
|
| 1357 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1358 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1359 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 1360 | + return; |
|
| 1361 | + } |
|
| 1362 | + |
|
| 1363 | + foreach ($cles as $k => $v) { |
|
| 1364 | + if (str_starts_with($k, 'KEY ')) { |
|
| 1365 | + $n = str_replace('`', '', $k); |
|
| 1366 | + $v = str_replace('`', '"', (string) $v); |
|
| 1367 | + $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1368 | + if ($k != $n) { |
|
| 1369 | + $i = "\"$i\""; |
|
| 1370 | + } |
|
| 1371 | + $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1372 | + } elseif (str_starts_with($k, 'UNIQUE ')) { |
|
| 1373 | + $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1374 | + $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1375 | + } else { |
|
| 1376 | + $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1377 | + } |
|
| 1378 | + if ($k == 'PRIMARY KEY') { |
|
| 1379 | + $prim_name = $v; |
|
| 1380 | + } |
|
| 1381 | + $s = ','; |
|
| 1382 | + } |
|
| 1383 | + $s = ''; |
|
| 1384 | + |
|
| 1385 | + $character_set = ''; |
|
| 1386 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1387 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1388 | + } |
|
| 1389 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1390 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1391 | + } |
|
| 1392 | + |
|
| 1393 | + foreach ($champs as $k => $v) { |
|
| 1394 | + $k = str_replace('`', '"', $k); |
|
| 1395 | + 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])); |
|
| 1397 | + } |
|
| 1398 | + |
|
| 1399 | + $query .= "$s\n\t\t$k " |
|
| 1400 | + . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', (string) $v)) |
|
| 1401 | + ? ' bigserial' |
|
| 1402 | + : mysql2pg_type($v) |
|
| 1403 | + ); |
|
| 1404 | + $s = ','; |
|
| 1405 | + } |
|
| 1406 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1407 | + |
|
| 1408 | + // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1409 | + |
|
| 1410 | + $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1411 | + ($character_set ? " DEFAULT $character_set" : '') |
|
| 1412 | + . "\n"; |
|
| 1413 | + |
|
| 1414 | + if (!$requeter) { |
|
| 1415 | + return $q; |
|
| 1416 | + } |
|
| 1417 | + $connexion['last'] = $q; |
|
| 1418 | + $r = @pg_query($link, $q); |
|
| 1419 | + |
|
| 1420 | + if (!$r) { |
|
| 1421 | + spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1422 | + } else { |
|
| 1423 | + foreach ($keys as $index) { |
|
| 1424 | + pg_query($link, $index); |
|
| 1425 | + } |
|
| 1426 | + } |
|
| 1427 | + |
|
| 1428 | + return $r; |
|
| 1429 | 1429 | } |
| 1430 | 1430 | |
| 1431 | 1431 | |
| 1432 | 1432 | function spip_pg_create_base($nom, $serveur = '', $requeter = true) { |
| 1433 | - return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1433 | + return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1434 | 1434 | } |
| 1435 | 1435 | |
| 1436 | 1436 | // Fonction de creation d'une vue SQL nommee $nom |
| 1437 | 1437 | function spip_pg_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 1438 | - if (!$query_select) { |
|
| 1439 | - return false; |
|
| 1440 | - } |
|
| 1441 | - // vue deja presente |
|
| 1442 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 1443 | - if ($requeter) { |
|
| 1444 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1445 | - } |
|
| 1438 | + if (!$query_select) { |
|
| 1439 | + return false; |
|
| 1440 | + } |
|
| 1441 | + // vue deja presente |
|
| 1442 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 1443 | + if ($requeter) { |
|
| 1444 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1445 | + } |
|
| 1446 | 1446 | |
| 1447 | - return false; |
|
| 1448 | - } |
|
| 1447 | + return false; |
|
| 1448 | + } |
|
| 1449 | 1449 | |
| 1450 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1450 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1451 | 1451 | |
| 1452 | - return spip_pg_query($query, $serveur, $requeter); |
|
| 1452 | + return spip_pg_query($query, $serveur, $requeter); |
|
| 1453 | 1453 | } |
| 1454 | 1454 | |
| 1455 | 1455 | |
| 1456 | 1456 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1457 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1457 | + spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1458 | 1458 | } |
| 1459 | 1459 | |
| 1460 | 1460 | |
@@ -1467,48 +1467,48 @@ discard block |
||
| 1467 | 1467 | * @return bool|string true / false / requete |
| 1468 | 1468 | **/ |
| 1469 | 1469 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1470 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1470 | + return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1471 | 1471 | } |
| 1472 | 1472 | |
| 1473 | 1473 | // Selectionner la sous-chaine dans $objet |
| 1474 | 1474 | // correspondant a $lang. Cf balise Multi de Spip |
| 1475 | 1475 | |
| 1476 | 1476 | function spip_pg_multi($objet, $lang) { |
| 1477 | - return 'regexp_replace(' |
|
| 1478 | - . $objet |
|
| 1479 | - . ",'<multi>.*[[]" |
|
| 1480 | - . $lang |
|
| 1481 | - . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1477 | + return 'regexp_replace(' |
|
| 1478 | + . $objet |
|
| 1479 | + . ",'<multi>.*[[]" |
|
| 1480 | + . $lang |
|
| 1481 | + . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1482 | 1482 | } |
| 1483 | 1483 | |
| 1484 | 1484 | // Palanquee d'idiosyncrasies MySQL dans les creations de table |
| 1485 | 1485 | // A completer par les autres, mais essayer de reduire en amont. |
| 1486 | 1486 | |
| 1487 | 1487 | function mysql2pg_type($v) { |
| 1488 | - $remplace = [ |
|
| 1489 | - '/auto_increment/i' => '', // non reconnu |
|
| 1490 | - '/bigint/i' => 'bigint', |
|
| 1491 | - '/mediumint/i' => 'mediumint', |
|
| 1492 | - '/smallint/i' => 'smallint', |
|
| 1493 | - '/tinyint/i' => 'int', |
|
| 1494 | - '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1495 | - '/longtext/i' => 'text', |
|
| 1496 | - '/mediumtext/i' => 'text', |
|
| 1497 | - '/tinytext/i' => 'text', |
|
| 1498 | - '/longblob/i' => 'text', |
|
| 1499 | - '/0000-00-00/' => '0001-01-01', |
|
| 1500 | - '/datetime/i' => 'timestamp', |
|
| 1501 | - '/unsigned/i' => '', |
|
| 1502 | - '/double/i' => 'double precision', |
|
| 1503 | - '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1504 | - '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1505 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1506 | - ]; |
|
| 1507 | - |
|
| 1508 | - return preg_replace(array_keys($remplace), array_values($remplace), (string) $v); |
|
| 1488 | + $remplace = [ |
|
| 1489 | + '/auto_increment/i' => '', // non reconnu |
|
| 1490 | + '/bigint/i' => 'bigint', |
|
| 1491 | + '/mediumint/i' => 'mediumint', |
|
| 1492 | + '/smallint/i' => 'smallint', |
|
| 1493 | + '/tinyint/i' => 'int', |
|
| 1494 | + '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1495 | + '/longtext/i' => 'text', |
|
| 1496 | + '/mediumtext/i' => 'text', |
|
| 1497 | + '/tinytext/i' => 'text', |
|
| 1498 | + '/longblob/i' => 'text', |
|
| 1499 | + '/0000-00-00/' => '0001-01-01', |
|
| 1500 | + '/datetime/i' => 'timestamp', |
|
| 1501 | + '/unsigned/i' => '', |
|
| 1502 | + '/double/i' => 'double precision', |
|
| 1503 | + '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1504 | + '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1505 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1506 | + ]; |
|
| 1507 | + |
|
| 1508 | + return preg_replace(array_keys($remplace), array_values($remplace), (string) $v); |
|
| 1509 | 1509 | } |
| 1510 | 1510 | |
| 1511 | 1511 | // Renvoie false si on n'a pas les fonctions pg (pour l'install) |
| 1512 | 1512 | function spip_versions_pg() { |
| 1513 | - return function_exists('pg_connect'); |
|
| 1513 | + return function_exists('pg_connect'); |
|
| 1514 | 1514 | } |
@@ -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 |
@@ -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,15 +747,15 @@ 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 = '', $serveur = '', $requeter = true) { |
| 750 | - if ($exist) { |
|
| 751 | - $exist = ' IF EXISTS'; |
|
| 752 | - } |
|
| 753 | - |
|
| 754 | - if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 755 | - return true; |
|
| 756 | - } else { |
|
| 757 | - return false; |
|
| 758 | - } |
|
| 750 | + if ($exist) { |
|
| 751 | + $exist = ' IF EXISTS'; |
|
| 752 | + } |
|
| 753 | + |
|
| 754 | + if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 755 | + return true; |
|
| 756 | + } else { |
|
| 757 | + return false; |
|
| 758 | + } |
|
| 759 | 759 | } |
| 760 | 760 | |
| 761 | 761 | |
@@ -771,11 +771,11 @@ discard block |
||
| 771 | 771 | * - true si la requête a réussie, false sinon |
| 772 | 772 | */ |
| 773 | 773 | function spip_sqlite_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 774 | - if ($exist) { |
|
| 775 | - $exist = ' IF EXISTS'; |
|
| 776 | - } |
|
| 774 | + if ($exist) { |
|
| 775 | + $exist = ' IF EXISTS'; |
|
| 776 | + } |
|
| 777 | 777 | |
| 778 | - return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 778 | + return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 779 | 779 | } |
| 780 | 780 | |
| 781 | 781 | /** |
@@ -789,20 +789,20 @@ discard block |
||
| 789 | 789 | * @return bool ou requete |
| 790 | 790 | */ |
| 791 | 791 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) { |
| 792 | - if (!$nom && !$table) { |
|
| 793 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 792 | + if (!$nom && !$table) { |
|
| 793 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 794 | 794 | |
| 795 | - return false; |
|
| 796 | - } |
|
| 795 | + return false; |
|
| 796 | + } |
|
| 797 | 797 | |
| 798 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 799 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 800 | - $index = $table . '_' . $nom; |
|
| 801 | - $exist = ' IF EXISTS'; |
|
| 798 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 799 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 800 | + $index = $table . '_' . $nom; |
|
| 801 | + $exist = ' IF EXISTS'; |
|
| 802 | 802 | |
| 803 | - $query = "DROP INDEX$exist $index"; |
|
| 803 | + $query = "DROP INDEX$exist $index"; |
|
| 804 | 804 | |
| 805 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 805 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 806 | 806 | } |
| 807 | 807 | |
| 808 | 808 | /** |
@@ -818,28 +818,28 @@ discard block |
||
| 818 | 818 | * Erreur eventuelle |
| 819 | 819 | **/ |
| 820 | 820 | function spip_sqlite_error($query = '', $serveur = '') { |
| 821 | - $link = _sqlite_link($serveur); |
|
| 822 | - |
|
| 823 | - if ($link) { |
|
| 824 | - $errs = $link->errorInfo(); |
|
| 825 | - $s = _sqlite_last_error_from_link($link); |
|
| 826 | - } else { |
|
| 827 | - $s = ': aucune ressource sqlite (link)'; |
|
| 828 | - } |
|
| 829 | - if ($s) { |
|
| 830 | - $trace = debug_backtrace(); |
|
| 831 | - if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 832 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 833 | - } |
|
| 834 | - } |
|
| 835 | - |
|
| 836 | - return $s; |
|
| 821 | + $link = _sqlite_link($serveur); |
|
| 822 | + |
|
| 823 | + if ($link) { |
|
| 824 | + $errs = $link->errorInfo(); |
|
| 825 | + $s = _sqlite_last_error_from_link($link); |
|
| 826 | + } else { |
|
| 827 | + $s = ': aucune ressource sqlite (link)'; |
|
| 828 | + } |
|
| 829 | + if ($s) { |
|
| 830 | + $trace = debug_backtrace(); |
|
| 831 | + if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 832 | + spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 833 | + } |
|
| 834 | + } |
|
| 835 | + |
|
| 836 | + return $s; |
|
| 837 | 837 | } |
| 838 | 838 | |
| 839 | 839 | function _sqlite_last_error_from_link($link) { |
| 840 | - if ($link) { |
|
| 841 | - $errs = $link->errorInfo(); |
|
| 842 | - /* |
|
| 840 | + if ($link) { |
|
| 841 | + $errs = $link->errorInfo(); |
|
| 842 | + /* |
|
| 843 | 843 | $errs[0] |
| 844 | 844 | numero SQLState ('HY000' souvent lors d'une erreur) |
| 845 | 845 | http://www.easysoft.com/developer/interfaces/odbc/sqlstate_status_return_codes.html |
@@ -849,11 +849,11 @@ discard block |
||
| 849 | 849 | $errs[2] |
| 850 | 850 | Le texte du message d'erreur |
| 851 | 851 | */ |
| 852 | - if (ltrim((string) $errs[0], '0')) { // 00000 si pas d'erreur |
|
| 853 | - return "$errs[2]"; |
|
| 854 | - } |
|
| 855 | - } |
|
| 856 | - return ''; |
|
| 852 | + if (ltrim((string) $errs[0], '0')) { // 00000 si pas d'erreur |
|
| 853 | + return "$errs[2]"; |
|
| 854 | + } |
|
| 855 | + } |
|
| 856 | + return ''; |
|
| 857 | 857 | } |
| 858 | 858 | |
| 859 | 859 | /** |
@@ -870,23 +870,23 @@ discard block |
||
| 870 | 870 | * 'HY000/1' : numéro de l'erreur SQLState / numéro d'erreur interne SQLite (en sqlite 3) |
| 871 | 871 | **/ |
| 872 | 872 | function spip_sqlite_errno($serveur = '') { |
| 873 | - $link = _sqlite_link($serveur); |
|
| 874 | - |
|
| 875 | - if ($link) { |
|
| 876 | - $t = $link->errorInfo(); |
|
| 877 | - $s = ltrim((string) $t[0], '0'); // 00000 si pas d'erreur |
|
| 878 | - if ($s) { |
|
| 879 | - $s .= ' / ' . $t[1]; |
|
| 880 | - } // ajoute l'erreur du moteur SQLite |
|
| 881 | - } else { |
|
| 882 | - $s = ': aucune ressource sqlite (link)'; |
|
| 883 | - } |
|
| 884 | - |
|
| 885 | - if ($s) { |
|
| 886 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 887 | - } |
|
| 888 | - |
|
| 889 | - return $s ?: 0; |
|
| 873 | + $link = _sqlite_link($serveur); |
|
| 874 | + |
|
| 875 | + if ($link) { |
|
| 876 | + $t = $link->errorInfo(); |
|
| 877 | + $s = ltrim((string) $t[0], '0'); // 00000 si pas d'erreur |
|
| 878 | + if ($s) { |
|
| 879 | + $s .= ' / ' . $t[1]; |
|
| 880 | + } // ajoute l'erreur du moteur SQLite |
|
| 881 | + } else { |
|
| 882 | + $s = ': aucune ressource sqlite (link)'; |
|
| 883 | + } |
|
| 884 | + |
|
| 885 | + if ($s) { |
|
| 886 | + spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 887 | + } |
|
| 888 | + |
|
| 889 | + return $s ?: 0; |
|
| 890 | 890 | } |
| 891 | 891 | |
| 892 | 892 | |
@@ -902,19 +902,19 @@ discard block |
||
| 902 | 902 | * - false si on a pas pu avoir d'explication |
| 903 | 903 | */ |
| 904 | 904 | function spip_sqlite_explain($query, $serveur = '', $requeter = true) { |
| 905 | - if (!str_starts_with(ltrim($query), 'SELECT')) { |
|
| 906 | - return []; |
|
| 907 | - } |
|
| 908 | - |
|
| 909 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 910 | - $query = 'EXPLAIN ' . $query; |
|
| 911 | - if (!$requeter) { |
|
| 912 | - return $query; |
|
| 913 | - } |
|
| 914 | - // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 915 | - $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 916 | - |
|
| 917 | - return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 905 | + if (!str_starts_with(ltrim($query), 'SELECT')) { |
|
| 906 | + return []; |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 910 | + $query = 'EXPLAIN ' . $query; |
|
| 911 | + if (!$requeter) { |
|
| 912 | + return $query; |
|
| 913 | + } |
|
| 914 | + // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 915 | + $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 916 | + |
|
| 917 | + return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 918 | 918 | } |
| 919 | 919 | |
| 920 | 920 | |
@@ -934,35 +934,35 @@ discard block |
||
| 934 | 934 | */ |
| 935 | 935 | function spip_sqlite_fetch($r, $t = '', $serveur = '', $requeter = true) { |
| 936 | 936 | |
| 937 | - $link = _sqlite_link($serveur); |
|
| 938 | - $t = $t ?: SPIP_SQLITE3_ASSOC; |
|
| 939 | - |
|
| 940 | - if (!$r) { |
|
| 941 | - return false; |
|
| 942 | - } |
|
| 943 | - |
|
| 944 | - $retour = $r->fetch($t); |
|
| 945 | - |
|
| 946 | - if (!$retour) { |
|
| 947 | - if ($r->errorCode() === '00000') { |
|
| 948 | - return null; |
|
| 949 | - } |
|
| 950 | - return false; |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 954 | - // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 955 | - // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 956 | - if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 957 | - foreach ($retour as $cle => $val) { |
|
| 958 | - if (($pos = strpos((string) $cle, '.')) !== false) { |
|
| 959 | - $retour[substr((string) $cle, $pos + 1)] = &$retour[$cle]; |
|
| 960 | - unset($retour[$cle]); |
|
| 961 | - } |
|
| 962 | - } |
|
| 963 | - } |
|
| 964 | - |
|
| 965 | - return $retour; |
|
| 937 | + $link = _sqlite_link($serveur); |
|
| 938 | + $t = $t ?: SPIP_SQLITE3_ASSOC; |
|
| 939 | + |
|
| 940 | + if (!$r) { |
|
| 941 | + return false; |
|
| 942 | + } |
|
| 943 | + |
|
| 944 | + $retour = $r->fetch($t); |
|
| 945 | + |
|
| 946 | + if (!$retour) { |
|
| 947 | + if ($r->errorCode() === '00000') { |
|
| 948 | + return null; |
|
| 949 | + } |
|
| 950 | + return false; |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 954 | + // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 955 | + // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 956 | + if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 957 | + foreach ($retour as $cle => $val) { |
|
| 958 | + if (($pos = strpos((string) $cle, '.')) !== false) { |
|
| 959 | + $retour[substr((string) $cle, $pos + 1)] = &$retour[$cle]; |
|
| 960 | + unset($retour[$cle]); |
|
| 961 | + } |
|
| 962 | + } |
|
| 963 | + } |
|
| 964 | + |
|
| 965 | + return $retour; |
|
| 966 | 966 | } |
| 967 | 967 | |
| 968 | 968 | /** |
@@ -975,8 +975,8 @@ discard block |
||
| 975 | 975 | * @return bool True si déplacement réussi, false sinon. |
| 976 | 976 | **/ |
| 977 | 977 | function spip_sqlite_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 978 | - // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 979 | - return false; |
|
| 978 | + // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 979 | + return false; |
|
| 980 | 980 | } |
| 981 | 981 | |
| 982 | 982 | |
@@ -992,10 +992,10 @@ discard block |
||
| 992 | 992 | * @return bool True si réussi |
| 993 | 993 | */ |
| 994 | 994 | function spip_sqlite_free(&$r, $serveur = '', $requeter = true) { |
| 995 | - unset($r); |
|
| 995 | + unset($r); |
|
| 996 | 996 | |
| 997 | - return true; |
|
| 998 | - //return sqlite_free_result($r); |
|
| 997 | + return true; |
|
| 998 | + //return sqlite_free_result($r); |
|
| 999 | 999 | } |
| 1000 | 1000 | |
| 1001 | 1001 | |
@@ -1010,8 +1010,8 @@ discard block |
||
| 1010 | 1010 | * @return void |
| 1011 | 1011 | */ |
| 1012 | 1012 | function spip_sqlite_get_charset($charset = [], $serveur = '', $requeter = true) { |
| 1013 | - //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1014 | - //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1013 | + //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1014 | + //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1015 | 1015 | } |
| 1016 | 1016 | |
| 1017 | 1017 | |
@@ -1026,7 +1026,7 @@ discard block |
||
| 1026 | 1026 | * Valeur hexadécimale pour SQLite |
| 1027 | 1027 | **/ |
| 1028 | 1028 | function spip_sqlite_hex($v) { |
| 1029 | - return hexdec($v); |
|
| 1029 | + return hexdec($v); |
|
| 1030 | 1030 | } |
| 1031 | 1031 | |
| 1032 | 1032 | |
@@ -1048,7 +1048,7 @@ discard block |
||
| 1048 | 1048 | * Expression de requête SQL |
| 1049 | 1049 | **/ |
| 1050 | 1050 | function spip_sqlite_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) { |
| 1051 | - return "($val $not IN ($valeurs))"; |
|
| 1051 | + return "($val $not IN ($valeurs))"; |
|
| 1052 | 1052 | } |
| 1053 | 1053 | |
| 1054 | 1054 | |
@@ -1076,20 +1076,20 @@ 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'); |
|
| 1080 | - if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1081 | - if (!$requeter) { |
|
| 1082 | - return $r; |
|
| 1083 | - } |
|
| 1084 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1085 | - } else { |
|
| 1086 | - $nb = false; |
|
| 1087 | - } |
|
| 1079 | + $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1080 | + if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1081 | + if (!$requeter) { |
|
| 1082 | + return $r; |
|
| 1083 | + } |
|
| 1084 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1085 | + } else { |
|
| 1086 | + $nb = false; |
|
| 1087 | + } |
|
| 1088 | 1088 | |
| 1089 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1089 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1090 | 1090 | |
| 1091 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1092 | - return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1091 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1092 | + return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1093 | 1093 | } |
| 1094 | 1094 | |
| 1095 | 1095 | |
@@ -1114,28 +1114,28 @@ discard block |
||
| 1114 | 1114 | * - Tableau de description de la requête et du temps d'exécution, si var_profile activé |
| 1115 | 1115 | **/ |
| 1116 | 1116 | function spip_sqlite_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1117 | - if (!$desc) { |
|
| 1118 | - $desc = description_table($table, $serveur); |
|
| 1119 | - } |
|
| 1120 | - if (!$desc) { |
|
| 1121 | - die("$table insertion sans description"); |
|
| 1122 | - } |
|
| 1123 | - $fields = $desc['field'] ?? []; |
|
| 1124 | - |
|
| 1125 | - foreach ($couples as $champ => $val) { |
|
| 1126 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1127 | - } |
|
| 1128 | - |
|
| 1129 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1130 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1131 | - |
|
| 1132 | - $cles = $valeurs = ''; |
|
| 1133 | - if (is_countable($couples) ? count($couples) : 0) { |
|
| 1134 | - $cles = '(' . implode(',', array_keys($couples)) . ')'; |
|
| 1135 | - $valeurs = '(' . implode(',', $couples) . ')'; |
|
| 1136 | - } |
|
| 1137 | - |
|
| 1138 | - return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1117 | + if (!$desc) { |
|
| 1118 | + $desc = description_table($table, $serveur); |
|
| 1119 | + } |
|
| 1120 | + if (!$desc) { |
|
| 1121 | + die("$table insertion sans description"); |
|
| 1122 | + } |
|
| 1123 | + $fields = $desc['field'] ?? []; |
|
| 1124 | + |
|
| 1125 | + foreach ($couples as $champ => $val) { |
|
| 1126 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1127 | + } |
|
| 1128 | + |
|
| 1129 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1130 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1131 | + |
|
| 1132 | + $cles = $valeurs = ''; |
|
| 1133 | + if (is_countable($couples) ? count($couples) : 0) { |
|
| 1134 | + $cles = '(' . implode(',', array_keys($couples)) . ')'; |
|
| 1135 | + $valeurs = '(' . implode(',', $couples) . ')'; |
|
| 1136 | + } |
|
| 1137 | + |
|
| 1138 | + return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1139 | 1139 | } |
| 1140 | 1140 | |
| 1141 | 1141 | |
@@ -1159,72 +1159,72 @@ discard block |
||
| 1159 | 1159 | * - false en cas d'erreur. |
| 1160 | 1160 | **/ |
| 1161 | 1161 | function spip_sqlite_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1162 | - $retour = null; |
|
| 1163 | - $nb = null; |
|
| 1164 | - if (!$desc) { |
|
| 1165 | - $desc = description_table($table, $serveur); |
|
| 1166 | - } |
|
| 1167 | - if (!$desc) { |
|
| 1168 | - die("$table insertion sans description"); |
|
| 1169 | - } |
|
| 1170 | - if (!isset($desc['field'])) { |
|
| 1171 | - $desc['field'] = []; |
|
| 1172 | - } |
|
| 1173 | - |
|
| 1174 | - // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1175 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1176 | - |
|
| 1177 | - // seul le nom de la table est a traduire ici : |
|
| 1178 | - // le faire une seule fois au debut |
|
| 1179 | - $query_start = "INSERT INTO $table "; |
|
| 1180 | - $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1181 | - |
|
| 1182 | - // ouvrir une transaction |
|
| 1183 | - if ($requeter) { |
|
| 1184 | - Sqlite::demarrer_transaction($serveur); |
|
| 1185 | - } |
|
| 1186 | - |
|
| 1187 | - while ($couples = array_shift($tab_couples)) { |
|
| 1188 | - foreach ($couples as $champ => $val) { |
|
| 1189 | - $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1190 | - } |
|
| 1191 | - |
|
| 1192 | - // inserer les champs timestamp par defaut |
|
| 1193 | - $couples = array_merge($maj, $couples); |
|
| 1194 | - |
|
| 1195 | - $champs = $valeurs = ''; |
|
| 1196 | - if ($couples !== []) { |
|
| 1197 | - $champs = '(' . implode(',', array_keys($couples)) . ')'; |
|
| 1198 | - $valeurs = '(' . implode(',', $couples) . ')'; |
|
| 1199 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1200 | - } else { |
|
| 1201 | - $query = $query_start . 'DEFAULT VALUES'; |
|
| 1202 | - } |
|
| 1203 | - |
|
| 1204 | - if ($requeter) { |
|
| 1205 | - $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1206 | - } |
|
| 1207 | - |
|
| 1208 | - // sur le dernier couple uniquement |
|
| 1209 | - if ($tab_couples === []) { |
|
| 1210 | - $nb = 0; |
|
| 1211 | - if ($requeter) { |
|
| 1212 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1213 | - } else { |
|
| 1214 | - return $query; |
|
| 1215 | - } |
|
| 1216 | - } |
|
| 1217 | - |
|
| 1218 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1219 | - } |
|
| 1220 | - |
|
| 1221 | - if ($requeter) { |
|
| 1222 | - Sqlite::finir_transaction($serveur); |
|
| 1223 | - } |
|
| 1224 | - |
|
| 1225 | - // renvoie le dernier id d'autoincrement ajoute |
|
| 1226 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1227 | - return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1162 | + $retour = null; |
|
| 1163 | + $nb = null; |
|
| 1164 | + if (!$desc) { |
|
| 1165 | + $desc = description_table($table, $serveur); |
|
| 1166 | + } |
|
| 1167 | + if (!$desc) { |
|
| 1168 | + die("$table insertion sans description"); |
|
| 1169 | + } |
|
| 1170 | + if (!isset($desc['field'])) { |
|
| 1171 | + $desc['field'] = []; |
|
| 1172 | + } |
|
| 1173 | + |
|
| 1174 | + // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1175 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1176 | + |
|
| 1177 | + // seul le nom de la table est a traduire ici : |
|
| 1178 | + // le faire une seule fois au debut |
|
| 1179 | + $query_start = "INSERT INTO $table "; |
|
| 1180 | + $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1181 | + |
|
| 1182 | + // ouvrir une transaction |
|
| 1183 | + if ($requeter) { |
|
| 1184 | + Sqlite::demarrer_transaction($serveur); |
|
| 1185 | + } |
|
| 1186 | + |
|
| 1187 | + while ($couples = array_shift($tab_couples)) { |
|
| 1188 | + foreach ($couples as $champ => $val) { |
|
| 1189 | + $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1190 | + } |
|
| 1191 | + |
|
| 1192 | + // inserer les champs timestamp par defaut |
|
| 1193 | + $couples = array_merge($maj, $couples); |
|
| 1194 | + |
|
| 1195 | + $champs = $valeurs = ''; |
|
| 1196 | + if ($couples !== []) { |
|
| 1197 | + $champs = '(' . implode(',', array_keys($couples)) . ')'; |
|
| 1198 | + $valeurs = '(' . implode(',', $couples) . ')'; |
|
| 1199 | + $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1200 | + } else { |
|
| 1201 | + $query = $query_start . 'DEFAULT VALUES'; |
|
| 1202 | + } |
|
| 1203 | + |
|
| 1204 | + if ($requeter) { |
|
| 1205 | + $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1206 | + } |
|
| 1207 | + |
|
| 1208 | + // sur le dernier couple uniquement |
|
| 1209 | + if ($tab_couples === []) { |
|
| 1210 | + $nb = 0; |
|
| 1211 | + if ($requeter) { |
|
| 1212 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1213 | + } else { |
|
| 1214 | + return $query; |
|
| 1215 | + } |
|
| 1216 | + } |
|
| 1217 | + |
|
| 1218 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1219 | + } |
|
| 1220 | + |
|
| 1221 | + if ($requeter) { |
|
| 1222 | + Sqlite::finir_transaction($serveur); |
|
| 1223 | + } |
|
| 1224 | + |
|
| 1225 | + // renvoie le dernier id d'autoincrement ajoute |
|
| 1226 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1227 | + return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1228 | 1228 | } |
| 1229 | 1229 | |
| 1230 | 1230 | |
@@ -1239,7 +1239,7 @@ discard block |
||
| 1239 | 1239 | * Toujours true. |
| 1240 | 1240 | **/ |
| 1241 | 1241 | function spip_sqlite_preferer_transaction($serveur = '', $requeter = true) { |
| 1242 | - return true; |
|
| 1242 | + return true; |
|
| 1243 | 1243 | } |
| 1244 | 1244 | |
| 1245 | 1245 | /** |
@@ -1256,12 +1256,12 @@ discard block |
||
| 1256 | 1256 | * string si texte de la requête demandé, true sinon |
| 1257 | 1257 | **/ |
| 1258 | 1258 | function spip_sqlite_demarrer_transaction($serveur = '', $requeter = true) { |
| 1259 | - if (!$requeter) { |
|
| 1260 | - return 'BEGIN TRANSACTION'; |
|
| 1261 | - } |
|
| 1262 | - Sqlite::demarrer_transaction($serveur); |
|
| 1259 | + if (!$requeter) { |
|
| 1260 | + return 'BEGIN TRANSACTION'; |
|
| 1261 | + } |
|
| 1262 | + Sqlite::demarrer_transaction($serveur); |
|
| 1263 | 1263 | |
| 1264 | - return true; |
|
| 1264 | + return true; |
|
| 1265 | 1265 | } |
| 1266 | 1266 | |
| 1267 | 1267 | /** |
@@ -1275,12 +1275,12 @@ discard block |
||
| 1275 | 1275 | * string si texte de la requête demandé, true sinon |
| 1276 | 1276 | **/ |
| 1277 | 1277 | function spip_sqlite_terminer_transaction($serveur = '', $requeter = true) { |
| 1278 | - if (!$requeter) { |
|
| 1279 | - return 'COMMIT'; |
|
| 1280 | - } |
|
| 1281 | - Sqlite::finir_transaction($serveur); |
|
| 1278 | + if (!$requeter) { |
|
| 1279 | + return 'COMMIT'; |
|
| 1280 | + } |
|
| 1281 | + Sqlite::finir_transaction($serveur); |
|
| 1282 | 1282 | |
| 1283 | - return true; |
|
| 1283 | + return true; |
|
| 1284 | 1284 | } |
| 1285 | 1285 | |
| 1286 | 1286 | |
@@ -1295,27 +1295,27 @@ discard block |
||
| 1295 | 1295 | * Liste des noms de bases |
| 1296 | 1296 | **/ |
| 1297 | 1297 | function spip_sqlite_listdbs($serveur = '', $requeter = true) { |
| 1298 | - _sqlite_init(); |
|
| 1299 | - |
|
| 1300 | - if (!is_dir($d = substr((string) _DIR_DB, 0, -1))) { |
|
| 1301 | - return []; |
|
| 1302 | - } |
|
| 1303 | - |
|
| 1304 | - include_spip('inc/flock'); |
|
| 1305 | - $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1306 | - $bds = []; |
|
| 1307 | - |
|
| 1308 | - foreach ($bases as $b) { |
|
| 1309 | - // pas de bases commencant pas sqlite |
|
| 1310 | - // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1311 | - // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1312 | - if (strpos((string) $b, '_sqlite')) { |
|
| 1313 | - continue; |
|
| 1314 | - } |
|
| 1315 | - $bds[] = preg_replace(";.*/$pattern;iS", '$1', (string) $b); |
|
| 1316 | - } |
|
| 1317 | - |
|
| 1318 | - return $bds; |
|
| 1298 | + _sqlite_init(); |
|
| 1299 | + |
|
| 1300 | + if (!is_dir($d = substr((string) _DIR_DB, 0, -1))) { |
|
| 1301 | + return []; |
|
| 1302 | + } |
|
| 1303 | + |
|
| 1304 | + include_spip('inc/flock'); |
|
| 1305 | + $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1306 | + $bds = []; |
|
| 1307 | + |
|
| 1308 | + foreach ($bases as $b) { |
|
| 1309 | + // pas de bases commencant pas sqlite |
|
| 1310 | + // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1311 | + // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1312 | + if (strpos((string) $b, '_sqlite')) { |
|
| 1313 | + continue; |
|
| 1314 | + } |
|
| 1315 | + $bds[] = preg_replace(";.*/$pattern;iS", '$1', (string) $b); |
|
| 1316 | + } |
|
| 1317 | + |
|
| 1318 | + return $bds; |
|
| 1319 | 1319 | } |
| 1320 | 1320 | |
| 1321 | 1321 | |
@@ -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 | |
@@ -1347,15 +1347,15 @@ discard block |
||
| 1347 | 1347 | * @return bool|string true / false / requete |
| 1348 | 1348 | **/ |
| 1349 | 1349 | function spip_sqlite_optimize($table, $serveur = '', $requeter = true) { |
| 1350 | - static $do = false; |
|
| 1351 | - if ($requeter && $do) { |
|
| 1352 | - return true; |
|
| 1353 | - } |
|
| 1354 | - if ($requeter) { |
|
| 1355 | - $do = true; |
|
| 1356 | - } |
|
| 1357 | - |
|
| 1358 | - return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1350 | + static $do = false; |
|
| 1351 | + if ($requeter && $do) { |
|
| 1352 | + return true; |
|
| 1353 | + } |
|
| 1354 | + if ($requeter) { |
|
| 1355 | + $do = true; |
|
| 1356 | + } |
|
| 1357 | + |
|
| 1358 | + return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1359 | 1359 | } |
| 1360 | 1360 | |
| 1361 | 1361 | |
@@ -1372,15 +1372,15 @@ discard block |
||
| 1372 | 1372 | * Donnée prête à être utilisée par le gestionnaire SQL |
| 1373 | 1373 | */ |
| 1374 | 1374 | function spip_sqlite_quote($v, $type = '') { |
| 1375 | - if (!is_array($v)) { |
|
| 1376 | - return _sqlite_calculer_cite($v, $type); |
|
| 1377 | - } |
|
| 1378 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1379 | - foreach ($v as $k => $r) { |
|
| 1380 | - $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1381 | - } |
|
| 1382 | - |
|
| 1383 | - return implode(',', $v); |
|
| 1375 | + if (!is_array($v)) { |
|
| 1376 | + return _sqlite_calculer_cite($v, $type); |
|
| 1377 | + } |
|
| 1378 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1379 | + foreach ($v as $k => $r) { |
|
| 1380 | + $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1381 | + } |
|
| 1382 | + |
|
| 1383 | + return implode(',', $v); |
|
| 1384 | 1384 | } |
| 1385 | 1385 | |
| 1386 | 1386 | |
@@ -1397,9 +1397,9 @@ discard block |
||
| 1397 | 1397 | * Expression SQL |
| 1398 | 1398 | **/ |
| 1399 | 1399 | function spip_sqlite_date_proche($champ, $interval, $unite) { |
| 1400 | - $op = (($interval <= 0) ? '>' : '<'); |
|
| 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 | |
@@ -1417,48 +1417,48 @@ discard block |
||
| 1417 | 1417 | * l'état de la table après la réparation |
| 1418 | 1418 | */ |
| 1419 | 1419 | function spip_sqlite_repair($table, $serveur = '', $requeter = true) { |
| 1420 | - if ( |
|
| 1421 | - ($desc = spip_sqlite_showtable($table, $serveur)) |
|
| 1422 | - && isset($desc['field']) |
|
| 1423 | - && is_array($desc['field']) |
|
| 1424 | - ) { |
|
| 1425 | - foreach ($desc['field'] as $c => $d) { |
|
| 1426 | - if ( |
|
| 1427 | - preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', (string) $d) |
|
| 1428 | - && stripos((string) $d, 'NOT NULL') !== false |
|
| 1429 | - && stripos((string) $d, 'DEFAULT') === false |
|
| 1430 | - /* pas touche aux cles primaires */ |
|
| 1431 | - && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1432 | - ) { |
|
| 1433 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1434 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1435 | - } |
|
| 1436 | - if ( |
|
| 1437 | - preg_match(',^(INTEGER),i', (string) $d) |
|
| 1438 | - && stripos((string) $d, 'NOT NULL') !== false |
|
| 1439 | - && stripos((string) $d, 'DEFAULT') === false |
|
| 1440 | - /* pas touche aux cles primaires */ |
|
| 1441 | - && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1442 | - ) { |
|
| 1443 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1444 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1445 | - } |
|
| 1446 | - if ( |
|
| 1447 | - preg_match(',^(datetime),i', (string) $d) |
|
| 1448 | - && stripos((string) $d, 'NOT NULL') !== false |
|
| 1449 | - && stripos((string) $d, 'DEFAULT') === false |
|
| 1450 | - /* pas touche aux cles primaires */ |
|
| 1451 | - && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1452 | - ) { |
|
| 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); |
|
| 1455 | - } |
|
| 1456 | - } |
|
| 1457 | - |
|
| 1458 | - return [' OK ']; |
|
| 1459 | - } |
|
| 1460 | - |
|
| 1461 | - return [' ERROR ']; |
|
| 1420 | + if ( |
|
| 1421 | + ($desc = spip_sqlite_showtable($table, $serveur)) |
|
| 1422 | + && isset($desc['field']) |
|
| 1423 | + && is_array($desc['field']) |
|
| 1424 | + ) { |
|
| 1425 | + foreach ($desc['field'] as $c => $d) { |
|
| 1426 | + if ( |
|
| 1427 | + preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', (string) $d) |
|
| 1428 | + && stripos((string) $d, 'NOT NULL') !== false |
|
| 1429 | + && stripos((string) $d, 'DEFAULT') === false |
|
| 1430 | + /* pas touche aux cles primaires */ |
|
| 1431 | + && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1432 | + ) { |
|
| 1433 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1434 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1435 | + } |
|
| 1436 | + if ( |
|
| 1437 | + preg_match(',^(INTEGER),i', (string) $d) |
|
| 1438 | + && stripos((string) $d, 'NOT NULL') !== false |
|
| 1439 | + && stripos((string) $d, 'DEFAULT') === false |
|
| 1440 | + /* pas touche aux cles primaires */ |
|
| 1441 | + && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1442 | + ) { |
|
| 1443 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1444 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1445 | + } |
|
| 1446 | + if ( |
|
| 1447 | + preg_match(',^(datetime),i', (string) $d) |
|
| 1448 | + && stripos((string) $d, 'NOT NULL') !== false |
|
| 1449 | + && stripos((string) $d, 'DEFAULT') === false |
|
| 1450 | + /* pas touche aux cles primaires */ |
|
| 1451 | + && (!isset($desc['key']['PRIMARY KEY']) || $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1452 | + ) { |
|
| 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); |
|
| 1455 | + } |
|
| 1456 | + } |
|
| 1457 | + |
|
| 1458 | + return [' OK ']; |
|
| 1459 | + } |
|
| 1460 | + |
|
| 1461 | + return [' ERROR ']; |
|
| 1462 | 1462 | } |
| 1463 | 1463 | |
| 1464 | 1464 | |
@@ -1487,25 +1487,25 @@ discard block |
||
| 1487 | 1487 | * - false en cas d'erreur. |
| 1488 | 1488 | **/ |
| 1489 | 1489 | function spip_sqlite_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) { |
| 1490 | - if (!$desc) { |
|
| 1491 | - $desc = description_table($table, $serveur); |
|
| 1492 | - } |
|
| 1493 | - if (!$desc) { |
|
| 1494 | - die("$table insertion sans description"); |
|
| 1495 | - } |
|
| 1496 | - $fields = $desc['field'] ?? []; |
|
| 1497 | - |
|
| 1498 | - foreach ($couples as $champ => $val) { |
|
| 1499 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1500 | - } |
|
| 1501 | - |
|
| 1502 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1503 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1504 | - |
|
| 1505 | - return spip_sqlite_query("REPLACE INTO $table (" . implode(',', array_keys($couples)) . ') VALUES (' . implode( |
|
| 1506 | - ',', |
|
| 1507 | - $couples |
|
| 1508 | - ) . ')', $serveur); |
|
| 1490 | + if (!$desc) { |
|
| 1491 | + $desc = description_table($table, $serveur); |
|
| 1492 | + } |
|
| 1493 | + if (!$desc) { |
|
| 1494 | + die("$table insertion sans description"); |
|
| 1495 | + } |
|
| 1496 | + $fields = $desc['field'] ?? []; |
|
| 1497 | + |
|
| 1498 | + foreach ($couples as $champ => $val) { |
|
| 1499 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1500 | + } |
|
| 1501 | + |
|
| 1502 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1503 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1504 | + |
|
| 1505 | + return spip_sqlite_query("REPLACE INTO $table (" . implode(',', array_keys($couples)) . ') VALUES (' . implode( |
|
| 1506 | + ',', |
|
| 1507 | + $couples |
|
| 1508 | + ) . ')', $serveur); |
|
| 1509 | 1509 | } |
| 1510 | 1510 | |
| 1511 | 1511 | |
@@ -1535,14 +1535,14 @@ discard block |
||
| 1535 | 1535 | **/ |
| 1536 | 1536 | function spip_sqlite_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1537 | 1537 | |
| 1538 | - $retour = null; |
|
| 1539 | - // boucler pour traiter chaque requete independemment |
|
| 1540 | - foreach ($tab_couples as $couples) { |
|
| 1541 | - $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1542 | - } |
|
| 1538 | + $retour = null; |
|
| 1539 | + // boucler pour traiter chaque requete independemment |
|
| 1540 | + foreach ($tab_couples as $couples) { |
|
| 1541 | + $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1542 | + } |
|
| 1543 | 1543 | |
| 1544 | - // renvoie le dernier id |
|
| 1545 | - return $retour; |
|
| 1544 | + // renvoie le dernier id |
|
| 1545 | + return $retour; |
|
| 1546 | 1546 | } |
| 1547 | 1547 | |
| 1548 | 1548 | |
@@ -1569,44 +1569,44 @@ discard block |
||
| 1569 | 1569 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 1570 | 1570 | */ |
| 1571 | 1571 | function spip_sqlite_select( |
| 1572 | - $select, |
|
| 1573 | - $from, |
|
| 1574 | - $where = '', |
|
| 1575 | - $groupby = '', |
|
| 1576 | - $orderby = '', |
|
| 1577 | - $limit = '', |
|
| 1578 | - $having = '', |
|
| 1579 | - $serveur = '', |
|
| 1580 | - $requeter = true |
|
| 1572 | + $select, |
|
| 1573 | + $from, |
|
| 1574 | + $where = '', |
|
| 1575 | + $groupby = '', |
|
| 1576 | + $orderby = '', |
|
| 1577 | + $limit = '', |
|
| 1578 | + $having = '', |
|
| 1579 | + $serveur = '', |
|
| 1580 | + $requeter = true |
|
| 1581 | 1581 | ) { |
| 1582 | 1582 | |
| 1583 | - // version() n'est pas connu de sqlite |
|
| 1584 | - $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1585 | - |
|
| 1586 | - // recomposer from |
|
| 1587 | - $from = (is_array($from) ? _sqlite_calculer_select_as($from) : $from); |
|
| 1588 | - |
|
| 1589 | - $query = |
|
| 1590 | - _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1591 | - . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1592 | - . _sqlite_calculer_expression('WHERE', $where) |
|
| 1593 | - . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1594 | - . _sqlite_calculer_expression('HAVING', $having) |
|
| 1595 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1596 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1597 | - |
|
| 1598 | - // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1599 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1600 | - // texte de la requete demande ? |
|
| 1601 | - if (!$requeter) { |
|
| 1602 | - return $res; |
|
| 1603 | - } |
|
| 1604 | - // erreur survenue ? |
|
| 1605 | - if ($res === false) { |
|
| 1606 | - return Sqlite::traduire_requete($query, $serveur); |
|
| 1607 | - } |
|
| 1608 | - |
|
| 1609 | - return $res; |
|
| 1583 | + // version() n'est pas connu de sqlite |
|
| 1584 | + $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1585 | + |
|
| 1586 | + // recomposer from |
|
| 1587 | + $from = (is_array($from) ? _sqlite_calculer_select_as($from) : $from); |
|
| 1588 | + |
|
| 1589 | + $query = |
|
| 1590 | + _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1591 | + . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1592 | + . _sqlite_calculer_expression('WHERE', $where) |
|
| 1593 | + . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1594 | + . _sqlite_calculer_expression('HAVING', $having) |
|
| 1595 | + . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1596 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1597 | + |
|
| 1598 | + // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1599 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1600 | + // texte de la requete demande ? |
|
| 1601 | + if (!$requeter) { |
|
| 1602 | + return $res; |
|
| 1603 | + } |
|
| 1604 | + // erreur survenue ? |
|
| 1605 | + if ($res === false) { |
|
| 1606 | + return Sqlite::traduire_requete($query, $serveur); |
|
| 1607 | + } |
|
| 1608 | + |
|
| 1609 | + return $res; |
|
| 1610 | 1610 | } |
| 1611 | 1611 | |
| 1612 | 1612 | |
@@ -1625,32 +1625,32 @@ discard block |
||
| 1625 | 1625 | * - False en cas d'erreur. |
| 1626 | 1626 | **/ |
| 1627 | 1627 | function spip_sqlite_selectdb($db, $serveur = '', $requeter = true) { |
| 1628 | - _sqlite_init(); |
|
| 1629 | - |
|
| 1630 | - // interdire la creation d'une nouvelle base, |
|
| 1631 | - // sauf si on est dans l'installation |
|
| 1632 | - if ( |
|
| 1633 | - !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1634 | - && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1635 | - ) { |
|
| 1636 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1637 | - |
|
| 1638 | - return false; |
|
| 1639 | - } |
|
| 1640 | - |
|
| 1641 | - // se connecter a la base indiquee |
|
| 1642 | - // avec les identifiants connus |
|
| 1643 | - $index = $serveur ?: 0; |
|
| 1644 | - |
|
| 1645 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1646 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1647 | - return $db; |
|
| 1648 | - } |
|
| 1649 | - } else { |
|
| 1650 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1651 | - } |
|
| 1652 | - |
|
| 1653 | - return false; |
|
| 1628 | + _sqlite_init(); |
|
| 1629 | + |
|
| 1630 | + // interdire la creation d'une nouvelle base, |
|
| 1631 | + // sauf si on est dans l'installation |
|
| 1632 | + if ( |
|
| 1633 | + !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1634 | + && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1635 | + ) { |
|
| 1636 | + spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1637 | + |
|
| 1638 | + return false; |
|
| 1639 | + } |
|
| 1640 | + |
|
| 1641 | + // se connecter a la base indiquee |
|
| 1642 | + // avec les identifiants connus |
|
| 1643 | + $index = $serveur ?: 0; |
|
| 1644 | + |
|
| 1645 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1646 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1647 | + return $db; |
|
| 1648 | + } |
|
| 1649 | + } else { |
|
| 1650 | + spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1651 | + } |
|
| 1652 | + |
|
| 1653 | + return false; |
|
| 1654 | 1654 | } |
| 1655 | 1655 | |
| 1656 | 1656 | |
@@ -1665,8 +1665,8 @@ discard block |
||
| 1665 | 1665 | * @return void |
| 1666 | 1666 | */ |
| 1667 | 1667 | function spip_sqlite_set_charset($charset, $serveur = '', $requeter = true) { |
| 1668 | - # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1669 | - # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1668 | + # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1669 | + # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1670 | 1670 | } |
| 1671 | 1671 | |
| 1672 | 1672 | |
@@ -1684,24 +1684,24 @@ discard block |
||
| 1684 | 1684 | * Ressource à utiliser avec sql_fetch() |
| 1685 | 1685 | **/ |
| 1686 | 1686 | function spip_sqlite_showbase($match, $serveur = '', $requeter = true) { |
| 1687 | - // type est le type d'entrée : table / index / view |
|
| 1688 | - // on ne retourne que les tables (?) et non les vues... |
|
| 1689 | - # ESCAPE non supporte par les versions sqlite <3 |
|
| 1690 | - # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1691 | - $match = preg_quote($match); |
|
| 1692 | - $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1693 | - $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1694 | - $match = str_replace('_', '.', $match); |
|
| 1695 | - $match = str_replace('%', '.*', $match); |
|
| 1696 | - $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1697 | - $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1698 | - $match = "^$match$"; |
|
| 1699 | - |
|
| 1700 | - return spip_sqlite_query( |
|
| 1701 | - "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1702 | - $serveur, |
|
| 1703 | - $requeter |
|
| 1704 | - ); |
|
| 1687 | + // type est le type d'entrée : table / index / view |
|
| 1688 | + // on ne retourne que les tables (?) et non les vues... |
|
| 1689 | + # ESCAPE non supporte par les versions sqlite <3 |
|
| 1690 | + # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1691 | + $match = preg_quote($match); |
|
| 1692 | + $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1693 | + $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1694 | + $match = str_replace('_', '.', $match); |
|
| 1695 | + $match = str_replace('%', '.*', $match); |
|
| 1696 | + $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1697 | + $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1698 | + $match = "^$match$"; |
|
| 1699 | + |
|
| 1700 | + return spip_sqlite_query( |
|
| 1701 | + "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1702 | + $serveur, |
|
| 1703 | + $requeter |
|
| 1704 | + ); |
|
| 1705 | 1705 | } |
| 1706 | 1706 | |
| 1707 | 1707 | /** |
@@ -1719,19 +1719,19 @@ discard block |
||
| 1719 | 1719 | * - string : requete sql, si $requeter = true |
| 1720 | 1720 | **/ |
| 1721 | 1721 | function spip_sqlite_table_exists(string $table, $serveur = '', $requeter = true) { |
| 1722 | - $r = spip_sqlite_query( |
|
| 1723 | - 'SELECT name FROM sqlite_master WHERE' |
|
| 1724 | - . ' type=\'table\'' |
|
| 1725 | - . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1726 | - . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1727 | - $serveur, |
|
| 1728 | - $requeter |
|
| 1729 | - ); |
|
| 1730 | - if (!$requeter) { |
|
| 1731 | - return $r; |
|
| 1732 | - } |
|
| 1733 | - $res = spip_sqlite_fetch($r, '', $serveur); |
|
| 1734 | - return (bool) $res; |
|
| 1722 | + $r = spip_sqlite_query( |
|
| 1723 | + 'SELECT name FROM sqlite_master WHERE' |
|
| 1724 | + . ' type=\'table\'' |
|
| 1725 | + . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1726 | + . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1727 | + $serveur, |
|
| 1728 | + $requeter |
|
| 1729 | + ); |
|
| 1730 | + if (!$requeter) { |
|
| 1731 | + return $r; |
|
| 1732 | + } |
|
| 1733 | + $res = spip_sqlite_fetch($r, '', $serveur); |
|
| 1734 | + return (bool) $res; |
|
| 1735 | 1735 | } |
| 1736 | 1736 | |
| 1737 | 1737 | define('_SQLITE_RE_SHOW_TABLE', '/^[^(),]*\(((?:[^()]*\((?:[^()]*\([^()]*\))?[^()]*\)[^()]*)*[^()]*)\)[^()]*$/'); |
@@ -1754,129 +1754,129 @@ discard block |
||
| 1754 | 1754 | * - array description de la table sinon |
| 1755 | 1755 | */ |
| 1756 | 1756 | function spip_sqlite_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1757 | - $query = |
|
| 1758 | - 'SELECT sql, type FROM' |
|
| 1759 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1760 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1761 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1762 | - . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1763 | - . ' ORDER BY substr(type,2,1), name'; |
|
| 1764 | - |
|
| 1765 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1766 | - if (!$a) { |
|
| 1767 | - return ''; |
|
| 1768 | - } |
|
| 1769 | - if (!$requeter) { |
|
| 1770 | - return $a; |
|
| 1771 | - } |
|
| 1772 | - if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1773 | - return ''; |
|
| 1774 | - } |
|
| 1775 | - $vue = ($a['type'] == 'view'); // table | vue |
|
| 1776 | - |
|
| 1777 | - // c'est une table |
|
| 1778 | - // il faut parser le create |
|
| 1779 | - if (!$vue) { |
|
| 1780 | - if (!preg_match(_SQLITE_RE_SHOW_TABLE, (string) array_shift($a), $r)) { |
|
| 1781 | - return ''; |
|
| 1782 | - } else { |
|
| 1783 | - $desc = $r[1]; |
|
| 1784 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1785 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1786 | - if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1787 | - $namedkeys = $r[2]; |
|
| 1788 | - $desc = $r[1]; |
|
| 1789 | - } else { |
|
| 1790 | - $namedkeys = ''; |
|
| 1791 | - } |
|
| 1792 | - |
|
| 1793 | - $fields = []; |
|
| 1794 | - $keys = []; |
|
| 1795 | - |
|
| 1796 | - // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1797 | - // par exemple s'il contiennent une virgule. |
|
| 1798 | - // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1799 | - [$desc, $echaps] = query_echappe_textes($desc); |
|
| 1800 | - |
|
| 1801 | - // separer toutes les descriptions de champs, separes par des virgules |
|
| 1802 | - # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1803 | - $k_precedent = null; |
|
| 1804 | - foreach (explode(',', (string) $desc) as $v) { |
|
| 1805 | - preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1806 | - // Les cles de champs peuvent etre entourees |
|
| 1807 | - // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1808 | - // http://www.sqlite.org/lang_keywords.html |
|
| 1809 | - $k = strtolower((string) query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1810 | - if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1811 | - $k = trim($k, $char); |
|
| 1812 | - if ($char == '[') { |
|
| 1813 | - $k = rtrim($k, ']'); |
|
| 1814 | - } |
|
| 1815 | - } |
|
| 1816 | - $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1817 | - |
|
| 1818 | - // rustine pour DECIMAL(10,2) |
|
| 1819 | - // s'il y a une parenthèse fermante dans la clé |
|
| 1820 | - // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1821 | - if (str_contains($k, ')') || preg_match('/^[^\(]*\)/', (string) $def)) { |
|
| 1822 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1823 | - continue; |
|
| 1824 | - } |
|
| 1825 | - |
|
| 1826 | - // la primary key peut etre dans une des descriptions de champs |
|
| 1827 | - // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1828 | - if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1829 | - $keys['PRIMARY KEY'] = $k; |
|
| 1830 | - } |
|
| 1831 | - |
|
| 1832 | - $fields[$k] = $def; |
|
| 1833 | - $k_precedent = $k; |
|
| 1834 | - } |
|
| 1835 | - // key inclues dans la requete |
|
| 1836 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1837 | - if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', (string) $v, $r)) { |
|
| 1838 | - $k = str_replace('`', '', trim($r[1])); |
|
| 1839 | - $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1840 | - if ($k && !isset($keys[$k])) { |
|
| 1841 | - $keys[$k] = $t; |
|
| 1842 | - } else { |
|
| 1843 | - $keys[] = $t; |
|
| 1844 | - } |
|
| 1845 | - } |
|
| 1846 | - } |
|
| 1847 | - // sinon ajouter les key index |
|
| 1848 | - $query = |
|
| 1849 | - 'SELECT name,sql FROM' |
|
| 1850 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1851 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1852 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1853 | - . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1854 | - . 'ORDER BY substr(type,2,1), name'; |
|
| 1855 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 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 |
|
| 1858 | - $keytype = 'KEY'; |
|
| 1859 | - if (str_contains((string) $r['sql'], 'UNIQUE INDEX')) { |
|
| 1860 | - $keytype = 'UNIQUE KEY'; |
|
| 1861 | - } |
|
| 1862 | - $colonnes = preg_replace(',.*\((.*)\).*,', '$1', (string) $r['sql']); |
|
| 1863 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1864 | - } |
|
| 1865 | - } |
|
| 1866 | - } // c'est une vue, on liste les champs disponibles simplement |
|
| 1867 | - else { |
|
| 1868 | - if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1869 | - $fields = []; |
|
| 1870 | - foreach ($res as $c => $v) { |
|
| 1871 | - $fields[$c] = ''; |
|
| 1872 | - } |
|
| 1873 | - $keys = []; |
|
| 1874 | - } else { |
|
| 1875 | - return ''; |
|
| 1876 | - } |
|
| 1877 | - } |
|
| 1878 | - |
|
| 1879 | - return ['field' => $fields, 'key' => $keys]; |
|
| 1757 | + $query = |
|
| 1758 | + 'SELECT sql, type FROM' |
|
| 1759 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1760 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1761 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1762 | + . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1763 | + . ' ORDER BY substr(type,2,1), name'; |
|
| 1764 | + |
|
| 1765 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1766 | + if (!$a) { |
|
| 1767 | + return ''; |
|
| 1768 | + } |
|
| 1769 | + if (!$requeter) { |
|
| 1770 | + return $a; |
|
| 1771 | + } |
|
| 1772 | + if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1773 | + return ''; |
|
| 1774 | + } |
|
| 1775 | + $vue = ($a['type'] == 'view'); // table | vue |
|
| 1776 | + |
|
| 1777 | + // c'est une table |
|
| 1778 | + // il faut parser le create |
|
| 1779 | + if (!$vue) { |
|
| 1780 | + if (!preg_match(_SQLITE_RE_SHOW_TABLE, (string) array_shift($a), $r)) { |
|
| 1781 | + return ''; |
|
| 1782 | + } else { |
|
| 1783 | + $desc = $r[1]; |
|
| 1784 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1785 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1786 | + if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1787 | + $namedkeys = $r[2]; |
|
| 1788 | + $desc = $r[1]; |
|
| 1789 | + } else { |
|
| 1790 | + $namedkeys = ''; |
|
| 1791 | + } |
|
| 1792 | + |
|
| 1793 | + $fields = []; |
|
| 1794 | + $keys = []; |
|
| 1795 | + |
|
| 1796 | + // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1797 | + // par exemple s'il contiennent une virgule. |
|
| 1798 | + // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1799 | + [$desc, $echaps] = query_echappe_textes($desc); |
|
| 1800 | + |
|
| 1801 | + // separer toutes les descriptions de champs, separes par des virgules |
|
| 1802 | + # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1803 | + $k_precedent = null; |
|
| 1804 | + foreach (explode(',', (string) $desc) as $v) { |
|
| 1805 | + preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1806 | + // Les cles de champs peuvent etre entourees |
|
| 1807 | + // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1808 | + // http://www.sqlite.org/lang_keywords.html |
|
| 1809 | + $k = strtolower((string) query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1810 | + if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1811 | + $k = trim($k, $char); |
|
| 1812 | + if ($char == '[') { |
|
| 1813 | + $k = rtrim($k, ']'); |
|
| 1814 | + } |
|
| 1815 | + } |
|
| 1816 | + $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1817 | + |
|
| 1818 | + // rustine pour DECIMAL(10,2) |
|
| 1819 | + // s'il y a une parenthèse fermante dans la clé |
|
| 1820 | + // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1821 | + if (str_contains($k, ')') || preg_match('/^[^\(]*\)/', (string) $def)) { |
|
| 1822 | + $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1823 | + continue; |
|
| 1824 | + } |
|
| 1825 | + |
|
| 1826 | + // la primary key peut etre dans une des descriptions de champs |
|
| 1827 | + // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1828 | + if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1829 | + $keys['PRIMARY KEY'] = $k; |
|
| 1830 | + } |
|
| 1831 | + |
|
| 1832 | + $fields[$k] = $def; |
|
| 1833 | + $k_precedent = $k; |
|
| 1834 | + } |
|
| 1835 | + // key inclues dans la requete |
|
| 1836 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1837 | + if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', (string) $v, $r)) { |
|
| 1838 | + $k = str_replace('`', '', trim($r[1])); |
|
| 1839 | + $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1840 | + if ($k && !isset($keys[$k])) { |
|
| 1841 | + $keys[$k] = $t; |
|
| 1842 | + } else { |
|
| 1843 | + $keys[] = $t; |
|
| 1844 | + } |
|
| 1845 | + } |
|
| 1846 | + } |
|
| 1847 | + // sinon ajouter les key index |
|
| 1848 | + $query = |
|
| 1849 | + 'SELECT name,sql FROM' |
|
| 1850 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1851 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1852 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1853 | + . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1854 | + . 'ORDER BY substr(type,2,1), name'; |
|
| 1855 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 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 |
|
| 1858 | + $keytype = 'KEY'; |
|
| 1859 | + if (str_contains((string) $r['sql'], 'UNIQUE INDEX')) { |
|
| 1860 | + $keytype = 'UNIQUE KEY'; |
|
| 1861 | + } |
|
| 1862 | + $colonnes = preg_replace(',.*\((.*)\).*,', '$1', (string) $r['sql']); |
|
| 1863 | + $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1864 | + } |
|
| 1865 | + } |
|
| 1866 | + } // c'est une vue, on liste les champs disponibles simplement |
|
| 1867 | + else { |
|
| 1868 | + if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1869 | + $fields = []; |
|
| 1870 | + foreach ($res as $c => $v) { |
|
| 1871 | + $fields[$c] = ''; |
|
| 1872 | + } |
|
| 1873 | + $keys = []; |
|
| 1874 | + } else { |
|
| 1875 | + return ''; |
|
| 1876 | + } |
|
| 1877 | + } |
|
| 1878 | + |
|
| 1879 | + return ['field' => $fields, 'key' => $keys]; |
|
| 1880 | 1880 | } |
| 1881 | 1881 | |
| 1882 | 1882 | |
@@ -1902,24 +1902,24 @@ discard block |
||
| 1902 | 1902 | * - array Tableau décrivant la requête et son temps d'exécution si var_profile est actif |
| 1903 | 1903 | */ |
| 1904 | 1904 | function spip_sqlite_update($table, $champs, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1905 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1906 | - $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1907 | - |
|
| 1908 | - $set = []; |
|
| 1909 | - foreach ($champs as $champ => $val) { |
|
| 1910 | - $set[] = $champ . "=$val"; |
|
| 1911 | - } |
|
| 1912 | - if ($set !== []) { |
|
| 1913 | - return spip_sqlite_query( |
|
| 1914 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1915 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1916 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1917 | - $serveur, |
|
| 1918 | - $requeter |
|
| 1919 | - ); |
|
| 1920 | - } |
|
| 1921 | - |
|
| 1922 | - return false; |
|
| 1905 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1906 | + $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1907 | + |
|
| 1908 | + $set = []; |
|
| 1909 | + foreach ($champs as $champ => $val) { |
|
| 1910 | + $set[] = $champ . "=$val"; |
|
| 1911 | + } |
|
| 1912 | + if ($set !== []) { |
|
| 1913 | + return spip_sqlite_query( |
|
| 1914 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1915 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1916 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1917 | + $serveur, |
|
| 1918 | + $requeter |
|
| 1919 | + ); |
|
| 1920 | + } |
|
| 1921 | + |
|
| 1922 | + return false; |
|
| 1923 | 1923 | } |
| 1924 | 1924 | |
| 1925 | 1925 | |
@@ -1949,38 +1949,38 @@ discard block |
||
| 1949 | 1949 | */ |
| 1950 | 1950 | function spip_sqlite_updateq($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1951 | 1951 | |
| 1952 | - if (!$champs) { |
|
| 1953 | - return; |
|
| 1954 | - } |
|
| 1955 | - if (!$desc) { |
|
| 1956 | - $desc = description_table($table, $serveur); |
|
| 1957 | - } |
|
| 1958 | - if (!$desc) { |
|
| 1959 | - die("$table insertion sans description"); |
|
| 1960 | - } |
|
| 1961 | - $fields = $desc['field']; |
|
| 1962 | - |
|
| 1963 | - $set = []; |
|
| 1964 | - foreach ($champs as $champ => $val) { |
|
| 1965 | - $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, $fields[$champ] ?? ''); |
|
| 1966 | - } |
|
| 1967 | - |
|
| 1968 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1969 | - // attention ils sont deja quotes |
|
| 1970 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1971 | - foreach ($maj as $champ => $val) { |
|
| 1972 | - if (!isset($set[$champ])) { |
|
| 1973 | - $set[$champ] = $champ . '=' . $val; |
|
| 1974 | - } |
|
| 1975 | - } |
|
| 1976 | - |
|
| 1977 | - return spip_sqlite_query( |
|
| 1978 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1979 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1980 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1981 | - $serveur, |
|
| 1982 | - $requeter |
|
| 1983 | - ); |
|
| 1952 | + if (!$champs) { |
|
| 1953 | + return; |
|
| 1954 | + } |
|
| 1955 | + if (!$desc) { |
|
| 1956 | + $desc = description_table($table, $serveur); |
|
| 1957 | + } |
|
| 1958 | + if (!$desc) { |
|
| 1959 | + die("$table insertion sans description"); |
|
| 1960 | + } |
|
| 1961 | + $fields = $desc['field']; |
|
| 1962 | + |
|
| 1963 | + $set = []; |
|
| 1964 | + foreach ($champs as $champ => $val) { |
|
| 1965 | + $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, $fields[$champ] ?? ''); |
|
| 1966 | + } |
|
| 1967 | + |
|
| 1968 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1969 | + // attention ils sont deja quotes |
|
| 1970 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1971 | + foreach ($maj as $champ => $val) { |
|
| 1972 | + if (!isset($set[$champ])) { |
|
| 1973 | + $set[$champ] = $champ . '=' . $val; |
|
| 1974 | + } |
|
| 1975 | + } |
|
| 1976 | + |
|
| 1977 | + return spip_sqlite_query( |
|
| 1978 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1979 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1980 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1981 | + $serveur, |
|
| 1982 | + $requeter |
|
| 1983 | + ); |
|
| 1984 | 1984 | } |
| 1985 | 1985 | |
| 1986 | 1986 | |
@@ -1998,17 +1998,17 @@ discard block |
||
| 1998 | 1998 | * @return void |
| 1999 | 1999 | */ |
| 2000 | 2000 | function _sqlite_init() { |
| 2001 | - if (!defined('_DIR_DB')) { |
|
| 2002 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2003 | - } |
|
| 2004 | - if (!defined('_SQLITE_CHMOD')) { |
|
| 2005 | - define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2006 | - } |
|
| 2007 | - |
|
| 2008 | - if (!is_dir($d = _DIR_DB)) { |
|
| 2009 | - include_spip('inc/flock'); |
|
| 2010 | - sous_repertoire($d); |
|
| 2011 | - } |
|
| 2001 | + if (!defined('_DIR_DB')) { |
|
| 2002 | + define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2003 | + } |
|
| 2004 | + if (!defined('_SQLITE_CHMOD')) { |
|
| 2005 | + define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2006 | + } |
|
| 2007 | + |
|
| 2008 | + if (!is_dir($d = _DIR_DB)) { |
|
| 2009 | + include_spip('inc/flock'); |
|
| 2010 | + sous_repertoire($d); |
|
| 2011 | + } |
|
| 2012 | 2012 | } |
| 2013 | 2013 | |
| 2014 | 2014 | |
@@ -2022,20 +2022,20 @@ discard block |
||
| 2022 | 2022 | * @return bool|int |
| 2023 | 2023 | */ |
| 2024 | 2024 | function _sqlite_is_version($version = '', $link = '', $serveur = '', $requeter = true) { |
| 2025 | - if ($link === '') { |
|
| 2026 | - $link = _sqlite_link($serveur); |
|
| 2027 | - } |
|
| 2028 | - if (!$link) { |
|
| 2029 | - return false; |
|
| 2030 | - } |
|
| 2025 | + if ($link === '') { |
|
| 2026 | + $link = _sqlite_link($serveur); |
|
| 2027 | + } |
|
| 2028 | + if (!$link) { |
|
| 2029 | + return false; |
|
| 2030 | + } |
|
| 2031 | 2031 | |
| 2032 | - $v = 3; |
|
| 2032 | + $v = 3; |
|
| 2033 | 2033 | |
| 2034 | - if (!$version) { |
|
| 2035 | - return $v; |
|
| 2036 | - } |
|
| 2034 | + if (!$version) { |
|
| 2035 | + return $v; |
|
| 2036 | + } |
|
| 2037 | 2037 | |
| 2038 | - return ($version == $v); |
|
| 2038 | + return ($version == $v); |
|
| 2039 | 2039 | } |
| 2040 | 2040 | |
| 2041 | 2041 | |
@@ -2046,9 +2046,9 @@ discard block |
||
| 2046 | 2046 | * @return \PDO|null Information de connexion pour SQLite |
| 2047 | 2047 | */ |
| 2048 | 2048 | function _sqlite_link($serveur = ''): ?\PDO { |
| 2049 | - $link = &$GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 2049 | + $link = &$GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 2050 | 2050 | |
| 2051 | - return $link; |
|
| 2051 | + return $link; |
|
| 2052 | 2052 | } |
| 2053 | 2053 | |
| 2054 | 2054 | |
@@ -2063,51 +2063,51 @@ discard block |
||
| 2063 | 2063 | * @return string|number texte ou nombre échappé |
| 2064 | 2064 | */ |
| 2065 | 2065 | function _sqlite_calculer_cite($v, $type) { |
| 2066 | - if ($type) { |
|
| 2067 | - if (is_null($v) && stripos($type, 'NOT NULL') === false) { |
|
| 2068 | - // null php se traduit en NULL SQL |
|
| 2069 | - return 'NULL'; |
|
| 2070 | - } |
|
| 2071 | - |
|
| 2072 | - if (sql_test_date($type) && preg_match('/^\w+\(/', $v)) { |
|
| 2073 | - return $v; |
|
| 2074 | - } |
|
| 2075 | - if (sql_test_int($type)) { |
|
| 2076 | - if (is_numeric($v)) { |
|
| 2077 | - return $v; |
|
| 2078 | - } elseif ($v === null) { |
|
| 2079 | - return 0; |
|
| 2080 | - } elseif (ctype_xdigit(substr($v, 2)) && str_starts_with($v, '0x')) { |
|
| 2081 | - return hexdec(substr($v, 2)); |
|
| 2082 | - } else { |
|
| 2083 | - return (int) $v; |
|
| 2084 | - } |
|
| 2085 | - } |
|
| 2086 | - } else { |
|
| 2087 | - // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2088 | - if (is_bool($v)) { |
|
| 2089 | - return (string) (int) $v; |
|
| 2090 | - } elseif (is_numeric($v)) { |
|
| 2091 | - return (string) $v; |
|
| 2092 | - } |
|
| 2093 | - } |
|
| 2094 | - |
|
| 2095 | - // trouver un link sqlite pour faire l'echappement |
|
| 2096 | - foreach ($GLOBALS['connexions'] as $s) { |
|
| 2097 | - if ( |
|
| 2098 | - ($l = $s['link']) |
|
| 2099 | - && is_object($l) |
|
| 2100 | - && $l instanceof \PDO |
|
| 2101 | - && $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2102 | - ) { |
|
| 2103 | - return $l->quote($v ?? ''); |
|
| 2104 | - } |
|
| 2105 | - } |
|
| 2106 | - |
|
| 2107 | - // echapper les ' en '' |
|
| 2108 | - spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2109 | - |
|
| 2110 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2066 | + if ($type) { |
|
| 2067 | + if (is_null($v) && stripos($type, 'NOT NULL') === false) { |
|
| 2068 | + // null php se traduit en NULL SQL |
|
| 2069 | + return 'NULL'; |
|
| 2070 | + } |
|
| 2071 | + |
|
| 2072 | + if (sql_test_date($type) && preg_match('/^\w+\(/', $v)) { |
|
| 2073 | + return $v; |
|
| 2074 | + } |
|
| 2075 | + if (sql_test_int($type)) { |
|
| 2076 | + if (is_numeric($v)) { |
|
| 2077 | + return $v; |
|
| 2078 | + } elseif ($v === null) { |
|
| 2079 | + return 0; |
|
| 2080 | + } elseif (ctype_xdigit(substr($v, 2)) && str_starts_with($v, '0x')) { |
|
| 2081 | + return hexdec(substr($v, 2)); |
|
| 2082 | + } else { |
|
| 2083 | + return (int) $v; |
|
| 2084 | + } |
|
| 2085 | + } |
|
| 2086 | + } else { |
|
| 2087 | + // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2088 | + if (is_bool($v)) { |
|
| 2089 | + return (string) (int) $v; |
|
| 2090 | + } elseif (is_numeric($v)) { |
|
| 2091 | + return (string) $v; |
|
| 2092 | + } |
|
| 2093 | + } |
|
| 2094 | + |
|
| 2095 | + // trouver un link sqlite pour faire l'echappement |
|
| 2096 | + foreach ($GLOBALS['connexions'] as $s) { |
|
| 2097 | + if ( |
|
| 2098 | + ($l = $s['link']) |
|
| 2099 | + && is_object($l) |
|
| 2100 | + && $l instanceof \PDO |
|
| 2101 | + && $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2102 | + ) { |
|
| 2103 | + return $l->quote($v ?? ''); |
|
| 2104 | + } |
|
| 2105 | + } |
|
| 2106 | + |
|
| 2107 | + // echapper les ' en '' |
|
| 2108 | + spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2109 | + |
|
| 2110 | + return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2111 | 2111 | } |
| 2112 | 2112 | |
| 2113 | 2113 | |
@@ -2123,21 +2123,21 @@ discard block |
||
| 2123 | 2123 | * @return string texte de l'expression, une partie donc, du texte la requête. |
| 2124 | 2124 | */ |
| 2125 | 2125 | function _sqlite_calculer_expression($expression, $v, $join = 'AND') { |
| 2126 | - if (empty($v)) { |
|
| 2127 | - return ''; |
|
| 2128 | - } |
|
| 2129 | - |
|
| 2130 | - $exp = "\n$expression "; |
|
| 2131 | - |
|
| 2132 | - if (!is_array($v)) { |
|
| 2133 | - return $exp . $v; |
|
| 2134 | - } else { |
|
| 2135 | - if (strtoupper($join) === 'AND') { |
|
| 2136 | - return $exp . implode("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2137 | - } else { |
|
| 2138 | - return $exp . implode($join, $v); |
|
| 2139 | - } |
|
| 2140 | - } |
|
| 2126 | + if (empty($v)) { |
|
| 2127 | + return ''; |
|
| 2128 | + } |
|
| 2129 | + |
|
| 2130 | + $exp = "\n$expression "; |
|
| 2131 | + |
|
| 2132 | + if (!is_array($v)) { |
|
| 2133 | + return $exp . $v; |
|
| 2134 | + } else { |
|
| 2135 | + if (strtoupper($join) === 'AND') { |
|
| 2136 | + return $exp . implode("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2137 | + } else { |
|
| 2138 | + return $exp . implode($join, $v); |
|
| 2139 | + } |
|
| 2140 | + } |
|
| 2141 | 2141 | } |
| 2142 | 2142 | |
| 2143 | 2143 | |
@@ -2153,7 +2153,7 @@ discard block |
||
| 2153 | 2153 | * @return string texte du orderby préparé |
| 2154 | 2154 | */ |
| 2155 | 2155 | function _sqlite_calculer_order($orderby) { |
| 2156 | - return (is_array($orderby)) ? implode(', ', $orderby) : $orderby; |
|
| 2156 | + return (is_array($orderby)) ? implode(', ', $orderby) : $orderby; |
|
| 2157 | 2157 | } |
| 2158 | 2158 | |
| 2159 | 2159 | |
@@ -2164,26 +2164,26 @@ discard block |
||
| 2164 | 2164 | * @return string Sélection de colonnes pour une clause SELECT |
| 2165 | 2165 | */ |
| 2166 | 2166 | function _sqlite_calculer_select_as($args) { |
| 2167 | - $res = ''; |
|
| 2168 | - foreach ($args as $k => $v) { |
|
| 2169 | - if (str_ends_with($k, '@')) { |
|
| 2170 | - // c'est une jointure qui se refere au from precedent |
|
| 2171 | - // pas de virgule |
|
| 2172 | - $res .= ' ' . $v; |
|
| 2173 | - } else { |
|
| 2174 | - if (!is_numeric($k)) { |
|
| 2175 | - $p = strpos((string) $v, ' '); |
|
| 2176 | - if ($p) { |
|
| 2177 | - $v = substr((string) $v, 0, $p) . " AS '$k'" . substr((string) $v, $p); |
|
| 2178 | - } else { |
|
| 2179 | - $v .= " AS '$k'"; |
|
| 2180 | - } |
|
| 2181 | - } |
|
| 2182 | - $res .= ', ' . $v; |
|
| 2183 | - } |
|
| 2184 | - } |
|
| 2185 | - |
|
| 2186 | - return substr($res, 2); |
|
| 2167 | + $res = ''; |
|
| 2168 | + foreach ($args as $k => $v) { |
|
| 2169 | + if (str_ends_with($k, '@')) { |
|
| 2170 | + // c'est une jointure qui se refere au from precedent |
|
| 2171 | + // pas de virgule |
|
| 2172 | + $res .= ' ' . $v; |
|
| 2173 | + } else { |
|
| 2174 | + if (!is_numeric($k)) { |
|
| 2175 | + $p = strpos((string) $v, ' '); |
|
| 2176 | + if ($p) { |
|
| 2177 | + $v = substr((string) $v, 0, $p) . " AS '$k'" . substr((string) $v, $p); |
|
| 2178 | + } else { |
|
| 2179 | + $v .= " AS '$k'"; |
|
| 2180 | + } |
|
| 2181 | + } |
|
| 2182 | + $res .= ', ' . $v; |
|
| 2183 | + } |
|
| 2184 | + } |
|
| 2185 | + |
|
| 2186 | + return substr($res, 2); |
|
| 2187 | 2187 | } |
| 2188 | 2188 | |
| 2189 | 2189 | |
@@ -2206,26 +2206,26 @@ discard block |
||
| 2206 | 2206 | * Contrainte pour clause WHERE |
| 2207 | 2207 | */ |
| 2208 | 2208 | function _sqlite_calculer_where($v) { |
| 2209 | - if (!is_array($v)) { |
|
| 2210 | - return $v; |
|
| 2211 | - } |
|
| 2212 | - |
|
| 2213 | - $op = array_shift($v); |
|
| 2214 | - if (!($n = count($v))) { |
|
| 2215 | - return $op; |
|
| 2216 | - } else { |
|
| 2217 | - $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2218 | - if ($n == 1) { |
|
| 2219 | - return "$op($arg)"; |
|
| 2220 | - } else { |
|
| 2221 | - $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2222 | - if ($n == 2) { |
|
| 2223 | - return "($arg $op $arg2)"; |
|
| 2224 | - } else { |
|
| 2225 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 2226 | - } |
|
| 2227 | - } |
|
| 2228 | - } |
|
| 2209 | + if (!is_array($v)) { |
|
| 2210 | + return $v; |
|
| 2211 | + } |
|
| 2212 | + |
|
| 2213 | + $op = array_shift($v); |
|
| 2214 | + if (!($n = count($v))) { |
|
| 2215 | + return $op; |
|
| 2216 | + } else { |
|
| 2217 | + $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2218 | + if ($n == 1) { |
|
| 2219 | + return "$op($arg)"; |
|
| 2220 | + } else { |
|
| 2221 | + $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2222 | + if ($n == 2) { |
|
| 2223 | + return "($arg $op $arg2)"; |
|
| 2224 | + } else { |
|
| 2225 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 2226 | + } |
|
| 2227 | + } |
|
| 2228 | + } |
|
| 2229 | 2229 | } |
| 2230 | 2230 | |
| 2231 | 2231 | |
@@ -2240,17 +2240,17 @@ discard block |
||
| 2240 | 2240 | * @return array|bool |
| 2241 | 2241 | */ |
| 2242 | 2242 | function _sqlite_charger_version($version = '') { |
| 2243 | - $versions = []; |
|
| 2243 | + $versions = []; |
|
| 2244 | 2244 | |
| 2245 | - // version 3 |
|
| 2246 | - if ((!$version || $version == 3) && (extension_loaded('pdo') && extension_loaded('pdo_sqlite'))) { |
|
| 2247 | - $versions[] = 3; |
|
| 2248 | - } |
|
| 2249 | - if ($version) { |
|
| 2250 | - return in_array($version, $versions); |
|
| 2251 | - } |
|
| 2245 | + // version 3 |
|
| 2246 | + if ((!$version || $version == 3) && (extension_loaded('pdo') && extension_loaded('pdo_sqlite'))) { |
|
| 2247 | + $versions[] = 3; |
|
| 2248 | + } |
|
| 2249 | + if ($version) { |
|
| 2250 | + return in_array($version, $versions); |
|
| 2251 | + } |
|
| 2252 | 2252 | |
| 2253 | - return $versions; |
|
| 2253 | + return $versions; |
|
| 2254 | 2254 | } |
| 2255 | 2255 | |
| 2256 | 2256 | |
@@ -2288,147 +2288,147 @@ discard block |
||
| 2288 | 2288 | */ |
| 2289 | 2289 | function _sqlite_modifier_table($table, $colonne, $opt = [], $serveur = '') { |
| 2290 | 2290 | |
| 2291 | - if (is_array($table)) { |
|
| 2292 | - $table_destination = reset($table); |
|
| 2293 | - $table_origine = key($table); |
|
| 2294 | - } else { |
|
| 2295 | - $table_origine = $table_destination = $table; |
|
| 2296 | - } |
|
| 2297 | - // ne prend actuellement qu'un changement |
|
| 2298 | - // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2299 | - if (is_array($colonne)) { |
|
| 2300 | - $colonne_destination = reset($colonne); |
|
| 2301 | - $colonne_origine = key($colonne); |
|
| 2302 | - } else { |
|
| 2303 | - $colonne_origine = $colonne_destination = $colonne; |
|
| 2304 | - } |
|
| 2305 | - if (!isset($opt['field'])) { |
|
| 2306 | - $opt['field'] = []; |
|
| 2307 | - } |
|
| 2308 | - if (!isset($opt['key'])) { |
|
| 2309 | - $opt['key'] = []; |
|
| 2310 | - } |
|
| 2311 | - |
|
| 2312 | - // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2313 | - // on prendra directement le nom de la future table |
|
| 2314 | - $meme_table = ($table_origine == $table_destination); |
|
| 2315 | - |
|
| 2316 | - $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2317 | - if (!$def_origine || !isset($def_origine['field'])) { |
|
| 2318 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2319 | - |
|
| 2320 | - return false; |
|
| 2321 | - } |
|
| 2322 | - |
|
| 2323 | - |
|
| 2324 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2325 | - |
|
| 2326 | - // 1) creer une table temporaire avec les modifications |
|
| 2327 | - // - DROP : suppression de la colonne |
|
| 2328 | - // - CHANGE : modification de la colonne |
|
| 2329 | - // (foreach pour conserver l'ordre des champs) |
|
| 2330 | - |
|
| 2331 | - // field |
|
| 2332 | - $fields = []; |
|
| 2333 | - // pour le INSERT INTO plus loin |
|
| 2334 | - // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2335 | - $fields_correspondances = []; |
|
| 2336 | - foreach ($def_origine['field'] as $c => $d) { |
|
| 2337 | - if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2338 | - // si pas DROP |
|
| 2339 | - if ($colonne_destination) { |
|
| 2340 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2341 | - $fields_correspondances[$colonne_destination] = $c; |
|
| 2342 | - } |
|
| 2343 | - } else { |
|
| 2344 | - $fields[$c] = $d; |
|
| 2345 | - $fields_correspondances[$c] = $c; |
|
| 2346 | - } |
|
| 2347 | - } |
|
| 2348 | - // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2349 | - if (!$colonne_origine && $colonne_destination) { |
|
| 2350 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2351 | - } |
|
| 2352 | - |
|
| 2353 | - // key... |
|
| 2354 | - $keys = []; |
|
| 2355 | - foreach ($def_origine['key'] as $c => $d) { |
|
| 2356 | - $c = str_replace($colonne_origine, $colonne_destination, (string) $c); |
|
| 2357 | - $d = str_replace($colonne_origine, $colonne_destination, (string) $d); |
|
| 2358 | - // seulement si on ne supprime pas la colonne ! |
|
| 2359 | - if ($d) { |
|
| 2360 | - $keys[$c] = $d; |
|
| 2361 | - } |
|
| 2362 | - } |
|
| 2363 | - |
|
| 2364 | - // autres keys, on merge |
|
| 2365 | - $keys = array_merge($keys, $opt['key']); |
|
| 2366 | - $queries = []; |
|
| 2367 | - |
|
| 2368 | - // copier dans destination (si differente de origine), sinon tmp |
|
| 2369 | - $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2370 | - $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2371 | - && $keys['PRIMARY KEY'] |
|
| 2372 | - && stripos((string) $keys['PRIMARY KEY'], ',') === false |
|
| 2373 | - && stripos((string) $fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2374 | - |
|
| 2375 | - if ( |
|
| 2376 | - $q = _sqlite_requete_create( |
|
| 2377 | - $table_copie, |
|
| 2378 | - $fields, |
|
| 2379 | - $keys, |
|
| 2380 | - $autoinc, |
|
| 2381 | - $temporary = false, |
|
| 2382 | - $ifnotexists = true, |
|
| 2383 | - $serveur |
|
| 2384 | - ) |
|
| 2385 | - ) { |
|
| 2386 | - $queries[] = $q; |
|
| 2387 | - } |
|
| 2388 | - |
|
| 2389 | - |
|
| 2390 | - // 2) y copier les champs qui vont bien |
|
| 2391 | - $champs_dest = implode(', ', array_keys($fields_correspondances)); |
|
| 2392 | - $champs_ori = implode(', ', $fields_correspondances); |
|
| 2393 | - $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2394 | - |
|
| 2395 | - // 3) supprimer la table d'origine |
|
| 2396 | - $queries[] = "DROP TABLE $table_origine"; |
|
| 2397 | - |
|
| 2398 | - // 4) renommer la table temporaire |
|
| 2399 | - // avec le nom de la table destination |
|
| 2400 | - // si necessaire |
|
| 2401 | - if ($meme_table) { |
|
| 2402 | - $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2403 | - } |
|
| 2404 | - |
|
| 2405 | - // 5) remettre les index ! |
|
| 2406 | - foreach ($keys as $k => $v) { |
|
| 2407 | - if ($k == 'PRIMARY KEY') { |
|
| 2408 | - } else { |
|
| 2409 | - // enlever KEY |
|
| 2410 | - $k = substr($k, 4); |
|
| 2411 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2412 | - } |
|
| 2413 | - } |
|
| 2414 | - |
|
| 2415 | - |
|
| 2416 | - if ($queries !== []) { |
|
| 2417 | - Sqlite::demarrer_transaction($serveur); |
|
| 2418 | - // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2419 | - foreach ($queries as $q) { |
|
| 2420 | - if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2421 | - spip_log('SQLite : ALTER TABLE table :' |
|
| 2422 | - . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2423 | - Sqlite::annuler_transaction($serveur); |
|
| 2424 | - |
|
| 2425 | - return false; |
|
| 2426 | - } |
|
| 2427 | - } |
|
| 2428 | - Sqlite::finir_transaction($serveur); |
|
| 2429 | - } |
|
| 2430 | - |
|
| 2431 | - return true; |
|
| 2291 | + if (is_array($table)) { |
|
| 2292 | + $table_destination = reset($table); |
|
| 2293 | + $table_origine = key($table); |
|
| 2294 | + } else { |
|
| 2295 | + $table_origine = $table_destination = $table; |
|
| 2296 | + } |
|
| 2297 | + // ne prend actuellement qu'un changement |
|
| 2298 | + // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2299 | + if (is_array($colonne)) { |
|
| 2300 | + $colonne_destination = reset($colonne); |
|
| 2301 | + $colonne_origine = key($colonne); |
|
| 2302 | + } else { |
|
| 2303 | + $colonne_origine = $colonne_destination = $colonne; |
|
| 2304 | + } |
|
| 2305 | + if (!isset($opt['field'])) { |
|
| 2306 | + $opt['field'] = []; |
|
| 2307 | + } |
|
| 2308 | + if (!isset($opt['key'])) { |
|
| 2309 | + $opt['key'] = []; |
|
| 2310 | + } |
|
| 2311 | + |
|
| 2312 | + // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2313 | + // on prendra directement le nom de la future table |
|
| 2314 | + $meme_table = ($table_origine == $table_destination); |
|
| 2315 | + |
|
| 2316 | + $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2317 | + if (!$def_origine || !isset($def_origine['field'])) { |
|
| 2318 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2319 | + |
|
| 2320 | + return false; |
|
| 2321 | + } |
|
| 2322 | + |
|
| 2323 | + |
|
| 2324 | + $table_tmp = $table_origine . '_tmp'; |
|
| 2325 | + |
|
| 2326 | + // 1) creer une table temporaire avec les modifications |
|
| 2327 | + // - DROP : suppression de la colonne |
|
| 2328 | + // - CHANGE : modification de la colonne |
|
| 2329 | + // (foreach pour conserver l'ordre des champs) |
|
| 2330 | + |
|
| 2331 | + // field |
|
| 2332 | + $fields = []; |
|
| 2333 | + // pour le INSERT INTO plus loin |
|
| 2334 | + // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2335 | + $fields_correspondances = []; |
|
| 2336 | + foreach ($def_origine['field'] as $c => $d) { |
|
| 2337 | + if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2338 | + // si pas DROP |
|
| 2339 | + if ($colonne_destination) { |
|
| 2340 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2341 | + $fields_correspondances[$colonne_destination] = $c; |
|
| 2342 | + } |
|
| 2343 | + } else { |
|
| 2344 | + $fields[$c] = $d; |
|
| 2345 | + $fields_correspondances[$c] = $c; |
|
| 2346 | + } |
|
| 2347 | + } |
|
| 2348 | + // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2349 | + if (!$colonne_origine && $colonne_destination) { |
|
| 2350 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2351 | + } |
|
| 2352 | + |
|
| 2353 | + // key... |
|
| 2354 | + $keys = []; |
|
| 2355 | + foreach ($def_origine['key'] as $c => $d) { |
|
| 2356 | + $c = str_replace($colonne_origine, $colonne_destination, (string) $c); |
|
| 2357 | + $d = str_replace($colonne_origine, $colonne_destination, (string) $d); |
|
| 2358 | + // seulement si on ne supprime pas la colonne ! |
|
| 2359 | + if ($d) { |
|
| 2360 | + $keys[$c] = $d; |
|
| 2361 | + } |
|
| 2362 | + } |
|
| 2363 | + |
|
| 2364 | + // autres keys, on merge |
|
| 2365 | + $keys = array_merge($keys, $opt['key']); |
|
| 2366 | + $queries = []; |
|
| 2367 | + |
|
| 2368 | + // copier dans destination (si differente de origine), sinon tmp |
|
| 2369 | + $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2370 | + $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2371 | + && $keys['PRIMARY KEY'] |
|
| 2372 | + && stripos((string) $keys['PRIMARY KEY'], ',') === false |
|
| 2373 | + && stripos((string) $fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2374 | + |
|
| 2375 | + if ( |
|
| 2376 | + $q = _sqlite_requete_create( |
|
| 2377 | + $table_copie, |
|
| 2378 | + $fields, |
|
| 2379 | + $keys, |
|
| 2380 | + $autoinc, |
|
| 2381 | + $temporary = false, |
|
| 2382 | + $ifnotexists = true, |
|
| 2383 | + $serveur |
|
| 2384 | + ) |
|
| 2385 | + ) { |
|
| 2386 | + $queries[] = $q; |
|
| 2387 | + } |
|
| 2388 | + |
|
| 2389 | + |
|
| 2390 | + // 2) y copier les champs qui vont bien |
|
| 2391 | + $champs_dest = implode(', ', array_keys($fields_correspondances)); |
|
| 2392 | + $champs_ori = implode(', ', $fields_correspondances); |
|
| 2393 | + $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2394 | + |
|
| 2395 | + // 3) supprimer la table d'origine |
|
| 2396 | + $queries[] = "DROP TABLE $table_origine"; |
|
| 2397 | + |
|
| 2398 | + // 4) renommer la table temporaire |
|
| 2399 | + // avec le nom de la table destination |
|
| 2400 | + // si necessaire |
|
| 2401 | + if ($meme_table) { |
|
| 2402 | + $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2403 | + } |
|
| 2404 | + |
|
| 2405 | + // 5) remettre les index ! |
|
| 2406 | + foreach ($keys as $k => $v) { |
|
| 2407 | + if ($k == 'PRIMARY KEY') { |
|
| 2408 | + } else { |
|
| 2409 | + // enlever KEY |
|
| 2410 | + $k = substr($k, 4); |
|
| 2411 | + $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2412 | + } |
|
| 2413 | + } |
|
| 2414 | + |
|
| 2415 | + |
|
| 2416 | + if ($queries !== []) { |
|
| 2417 | + Sqlite::demarrer_transaction($serveur); |
|
| 2418 | + // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2419 | + foreach ($queries as $q) { |
|
| 2420 | + if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2421 | + spip_log('SQLite : ALTER TABLE table :' |
|
| 2422 | + . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2423 | + Sqlite::annuler_transaction($serveur); |
|
| 2424 | + |
|
| 2425 | + return false; |
|
| 2426 | + } |
|
| 2427 | + } |
|
| 2428 | + Sqlite::finir_transaction($serveur); |
|
| 2429 | + } |
|
| 2430 | + |
|
| 2431 | + return true; |
|
| 2432 | 2432 | } |
| 2433 | 2433 | |
| 2434 | 2434 | |
@@ -2438,61 +2438,61 @@ discard block |
||
| 2438 | 2438 | * @return array |
| 2439 | 2439 | */ |
| 2440 | 2440 | function _sqlite_ref_fonctions() { |
| 2441 | - $fonctions = [ |
|
| 2442 | - 'alter' => 'spip_sqlite_alter', |
|
| 2443 | - 'count' => 'spip_sqlite_count', |
|
| 2444 | - 'countsel' => 'spip_sqlite_countsel', |
|
| 2445 | - 'create' => 'spip_sqlite_create', |
|
| 2446 | - 'create_base' => 'spip_sqlite_create_base', |
|
| 2447 | - 'create_view' => 'spip_sqlite_create_view', |
|
| 2448 | - 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2449 | - 'delete' => 'spip_sqlite_delete', |
|
| 2450 | - 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2451 | - 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2452 | - 'errno' => 'spip_sqlite_errno', |
|
| 2453 | - 'error' => 'spip_sqlite_error', |
|
| 2454 | - 'explain' => 'spip_sqlite_explain', |
|
| 2455 | - 'fetch' => 'spip_sqlite_fetch', |
|
| 2456 | - 'seek' => 'spip_sqlite_seek', |
|
| 2457 | - 'free' => 'spip_sqlite_free', |
|
| 2458 | - 'hex' => 'spip_sqlite_hex', |
|
| 2459 | - 'in' => 'spip_sqlite_in', |
|
| 2460 | - 'insert' => 'spip_sqlite_insert', |
|
| 2461 | - 'insertq' => 'spip_sqlite_insertq', |
|
| 2462 | - 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2463 | - 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2464 | - 'multi' => 'spip_sqlite_multi', |
|
| 2465 | - 'optimize' => 'spip_sqlite_optimize', |
|
| 2466 | - 'query' => 'spip_sqlite_query', |
|
| 2467 | - 'quote' => 'spip_sqlite_quote', |
|
| 2468 | - 'repair' => 'spip_sqlite_repair', |
|
| 2469 | - 'replace' => 'spip_sqlite_replace', |
|
| 2470 | - 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2471 | - 'select' => 'spip_sqlite_select', |
|
| 2472 | - 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2473 | - 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2474 | - 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2475 | - 'showbase' => 'spip_sqlite_showbase', |
|
| 2476 | - 'showtable' => 'spip_sqlite_showtable', |
|
| 2477 | - 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2478 | - 'update' => 'spip_sqlite_update', |
|
| 2479 | - 'updateq' => 'spip_sqlite_updateq', |
|
| 2480 | - 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2481 | - 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2482 | - 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2483 | - ]; |
|
| 2484 | - |
|
| 2485 | - // association de chaque nom http d'un charset aux couples sqlite |
|
| 2486 | - // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2487 | - $charsets = [ |
|
| 2488 | - 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2489 | - //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2490 | - //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2491 | - ]; |
|
| 2492 | - |
|
| 2493 | - $fonctions['charsets'] = $charsets; |
|
| 2494 | - |
|
| 2495 | - return $fonctions; |
|
| 2441 | + $fonctions = [ |
|
| 2442 | + 'alter' => 'spip_sqlite_alter', |
|
| 2443 | + 'count' => 'spip_sqlite_count', |
|
| 2444 | + 'countsel' => 'spip_sqlite_countsel', |
|
| 2445 | + 'create' => 'spip_sqlite_create', |
|
| 2446 | + 'create_base' => 'spip_sqlite_create_base', |
|
| 2447 | + 'create_view' => 'spip_sqlite_create_view', |
|
| 2448 | + 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2449 | + 'delete' => 'spip_sqlite_delete', |
|
| 2450 | + 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2451 | + 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2452 | + 'errno' => 'spip_sqlite_errno', |
|
| 2453 | + 'error' => 'spip_sqlite_error', |
|
| 2454 | + 'explain' => 'spip_sqlite_explain', |
|
| 2455 | + 'fetch' => 'spip_sqlite_fetch', |
|
| 2456 | + 'seek' => 'spip_sqlite_seek', |
|
| 2457 | + 'free' => 'spip_sqlite_free', |
|
| 2458 | + 'hex' => 'spip_sqlite_hex', |
|
| 2459 | + 'in' => 'spip_sqlite_in', |
|
| 2460 | + 'insert' => 'spip_sqlite_insert', |
|
| 2461 | + 'insertq' => 'spip_sqlite_insertq', |
|
| 2462 | + 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2463 | + 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2464 | + 'multi' => 'spip_sqlite_multi', |
|
| 2465 | + 'optimize' => 'spip_sqlite_optimize', |
|
| 2466 | + 'query' => 'spip_sqlite_query', |
|
| 2467 | + 'quote' => 'spip_sqlite_quote', |
|
| 2468 | + 'repair' => 'spip_sqlite_repair', |
|
| 2469 | + 'replace' => 'spip_sqlite_replace', |
|
| 2470 | + 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2471 | + 'select' => 'spip_sqlite_select', |
|
| 2472 | + 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2473 | + 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2474 | + 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2475 | + 'showbase' => 'spip_sqlite_showbase', |
|
| 2476 | + 'showtable' => 'spip_sqlite_showtable', |
|
| 2477 | + 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2478 | + 'update' => 'spip_sqlite_update', |
|
| 2479 | + 'updateq' => 'spip_sqlite_updateq', |
|
| 2480 | + 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2481 | + 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2482 | + 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2483 | + ]; |
|
| 2484 | + |
|
| 2485 | + // association de chaque nom http d'un charset aux couples sqlite |
|
| 2486 | + // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2487 | + $charsets = [ |
|
| 2488 | + 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2489 | + //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2490 | + //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2491 | + ]; |
|
| 2492 | + |
|
| 2493 | + $fonctions['charsets'] = $charsets; |
|
| 2494 | + |
|
| 2495 | + return $fonctions; |
|
| 2496 | 2496 | } |
| 2497 | 2497 | |
| 2498 | 2498 | |
@@ -2504,56 +2504,56 @@ discard block |
||
| 2504 | 2504 | * @return mixed |
| 2505 | 2505 | */ |
| 2506 | 2506 | function _sqlite_remplacements_definitions_table($query, $autoinc = false) { |
| 2507 | - // quelques remplacements |
|
| 2508 | - $num = '(\s*\([0-9]*\))?'; |
|
| 2509 | - $enum = '(\s*\([^\)]*\))?'; |
|
| 2510 | - |
|
| 2511 | - $remplace = [ |
|
| 2512 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2513 | - '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2514 | - '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2515 | - '/auto_increment/is' => '', |
|
| 2516 | - '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2517 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2518 | - '/character set \w+/is' => '', |
|
| 2519 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 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 ''", |
|
| 2522 | - '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2523 | - '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2524 | - ]; |
|
| 2525 | - |
|
| 2526 | - // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2527 | - $remplace_autocinc = [ |
|
| 2528 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2529 | - ]; |
|
| 2530 | - // pour les int non autoincrement, il faut un DEFAULT |
|
| 2531 | - $remplace_nonautocinc = [ |
|
| 2532 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2533 | - ]; |
|
| 2534 | - |
|
| 2535 | - if (is_string($query)) { |
|
| 2536 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2537 | - if ($autoinc || preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2538 | - $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2539 | - } else { |
|
| 2540 | - $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2541 | - $query = _sqlite_collate_ci($query); |
|
| 2542 | - } |
|
| 2543 | - } elseif (is_array($query)) { |
|
| 2544 | - foreach ($query as $k => $q) { |
|
| 2545 | - $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', (string) $q)); |
|
| 2546 | - $query[$k] = preg_replace(array_keys($remplace), $remplace, (string) $query[$k]); |
|
| 2547 | - if ($ai) { |
|
| 2548 | - $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2549 | - } else { |
|
| 2550 | - $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2551 | - $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2552 | - } |
|
| 2553 | - } |
|
| 2554 | - } |
|
| 2555 | - |
|
| 2556 | - return $query; |
|
| 2507 | + // quelques remplacements |
|
| 2508 | + $num = '(\s*\([0-9]*\))?'; |
|
| 2509 | + $enum = '(\s*\([^\)]*\))?'; |
|
| 2510 | + |
|
| 2511 | + $remplace = [ |
|
| 2512 | + '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2513 | + '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2514 | + '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2515 | + '/auto_increment/is' => '', |
|
| 2516 | + '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2517 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2518 | + '/character set \w+/is' => '', |
|
| 2519 | + '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 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 ''", |
|
| 2522 | + '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2523 | + '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2524 | + ]; |
|
| 2525 | + |
|
| 2526 | + // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2527 | + $remplace_autocinc = [ |
|
| 2528 | + '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2529 | + ]; |
|
| 2530 | + // pour les int non autoincrement, il faut un DEFAULT |
|
| 2531 | + $remplace_nonautocinc = [ |
|
| 2532 | + '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2533 | + ]; |
|
| 2534 | + |
|
| 2535 | + if (is_string($query)) { |
|
| 2536 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2537 | + if ($autoinc || preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2538 | + $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2539 | + } else { |
|
| 2540 | + $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2541 | + $query = _sqlite_collate_ci($query); |
|
| 2542 | + } |
|
| 2543 | + } elseif (is_array($query)) { |
|
| 2544 | + foreach ($query as $k => $q) { |
|
| 2545 | + $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', (string) $q)); |
|
| 2546 | + $query[$k] = preg_replace(array_keys($remplace), $remplace, (string) $query[$k]); |
|
| 2547 | + if ($ai) { |
|
| 2548 | + $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2549 | + } else { |
|
| 2550 | + $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2551 | + $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2552 | + } |
|
| 2553 | + } |
|
| 2554 | + } |
|
| 2555 | + |
|
| 2556 | + return $query; |
|
| 2557 | 2557 | } |
| 2558 | 2558 | |
| 2559 | 2559 | /** |
@@ -2564,17 +2564,17 @@ discard block |
||
| 2564 | 2564 | * @return string |
| 2565 | 2565 | */ |
| 2566 | 2566 | function _sqlite_collate_ci($champ) { |
| 2567 | - if (stripos($champ, 'COLLATE') !== false) { |
|
| 2568 | - return $champ; |
|
| 2569 | - } |
|
| 2570 | - if (stripos($champ, 'BINARY') !== false) { |
|
| 2571 | - return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2572 | - } |
|
| 2573 | - if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2574 | - return $champ . ' COLLATE NOCASE'; |
|
| 2575 | - } |
|
| 2576 | - |
|
| 2577 | - return $champ; |
|
| 2567 | + if (stripos($champ, 'COLLATE') !== false) { |
|
| 2568 | + return $champ; |
|
| 2569 | + } |
|
| 2570 | + if (stripos($champ, 'BINARY') !== false) { |
|
| 2571 | + return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2572 | + } |
|
| 2573 | + if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2574 | + return $champ . ' COLLATE NOCASE'; |
|
| 2575 | + } |
|
| 2576 | + |
|
| 2577 | + return $champ; |
|
| 2578 | 2578 | } |
| 2579 | 2579 | |
| 2580 | 2580 | |
@@ -2593,83 +2593,83 @@ discard block |
||
| 2593 | 2593 | * @return bool|string |
| 2594 | 2594 | */ |
| 2595 | 2595 | function _sqlite_requete_create( |
| 2596 | - $nom, |
|
| 2597 | - $champs, |
|
| 2598 | - $cles, |
|
| 2599 | - $autoinc = false, |
|
| 2600 | - $temporary = false, |
|
| 2601 | - $_ifnotexists = true, |
|
| 2602 | - $serveur = '', |
|
| 2603 | - $requeter = true |
|
| 2596 | + $nom, |
|
| 2597 | + $champs, |
|
| 2598 | + $cles, |
|
| 2599 | + $autoinc = false, |
|
| 2600 | + $temporary = false, |
|
| 2601 | + $_ifnotexists = true, |
|
| 2602 | + $serveur = '', |
|
| 2603 | + $requeter = true |
|
| 2604 | 2604 | ) { |
| 2605 | - $query = $keys = $s = $p = ''; |
|
| 2606 | - |
|
| 2607 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2608 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2609 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 2610 | - return; |
|
| 2611 | - } |
|
| 2612 | - |
|
| 2613 | - // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2614 | - // il faut passer par des create index |
|
| 2615 | - // Il gere par contre primary key ! |
|
| 2616 | - // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2617 | - // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2618 | - $pk = 'PRIMARY KEY'; |
|
| 2619 | - // le champ de cle primaire |
|
| 2620 | - $champ_pk = empty($cles[$pk]) ? '' : $cles[$pk]; |
|
| 2621 | - |
|
| 2622 | - foreach ($champs as $k => $v) { |
|
| 2623 | - if (false !== stripos((string) $v, $pk)) { |
|
| 2624 | - $champ_pk = $k; |
|
| 2625 | - // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2626 | - $champs[$k] = preg_replace("/$pk/is", '', (string) $champs[$k]); |
|
| 2627 | - break; |
|
| 2628 | - } |
|
| 2629 | - } |
|
| 2630 | - |
|
| 2631 | - if ($champ_pk) { |
|
| 2632 | - $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2633 | - } |
|
| 2634 | - // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2635 | - if ( |
|
| 2636 | - isset($champs[$champ_pk]) |
|
| 2637 | - && stripos((string) $champs[$champ_pk], 'default 0') !== false |
|
| 2638 | - ) { |
|
| 2639 | - $champs[$champ_pk] = trim(str_ireplace('default 0', '', (string) $champs[$champ_pk])); |
|
| 2640 | - } |
|
| 2641 | - |
|
| 2642 | - $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2643 | - foreach ($champs as $k => $v) { |
|
| 2644 | - $query .= "$s\n\t\t$k $v"; |
|
| 2645 | - $s = ','; |
|
| 2646 | - } |
|
| 2647 | - |
|
| 2648 | - $ifnotexists = ''; |
|
| 2649 | - if ($_ifnotexists) { |
|
| 2650 | - $version = spip_sqlite_fetch( |
|
| 2651 | - spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2652 | - '', |
|
| 2653 | - $serveur |
|
| 2654 | - ); |
|
| 2655 | - if (!function_exists('spip_version_compare')) { |
|
| 2656 | - include_spip('plugins/installer'); |
|
| 2657 | - } |
|
| 2658 | - |
|
| 2659 | - if ($version && spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2660 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 2661 | - } else { |
|
| 2662 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2663 | - $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2664 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2665 | - return true; |
|
| 2666 | - } |
|
| 2667 | - } |
|
| 2668 | - } |
|
| 2669 | - |
|
| 2670 | - $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2671 | - |
|
| 2672 | - return "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2605 | + $query = $keys = $s = $p = ''; |
|
| 2606 | + |
|
| 2607 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2608 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2609 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 2610 | + return; |
|
| 2611 | + } |
|
| 2612 | + |
|
| 2613 | + // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2614 | + // il faut passer par des create index |
|
| 2615 | + // Il gere par contre primary key ! |
|
| 2616 | + // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2617 | + // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2618 | + $pk = 'PRIMARY KEY'; |
|
| 2619 | + // le champ de cle primaire |
|
| 2620 | + $champ_pk = empty($cles[$pk]) ? '' : $cles[$pk]; |
|
| 2621 | + |
|
| 2622 | + foreach ($champs as $k => $v) { |
|
| 2623 | + if (false !== stripos((string) $v, $pk)) { |
|
| 2624 | + $champ_pk = $k; |
|
| 2625 | + // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2626 | + $champs[$k] = preg_replace("/$pk/is", '', (string) $champs[$k]); |
|
| 2627 | + break; |
|
| 2628 | + } |
|
| 2629 | + } |
|
| 2630 | + |
|
| 2631 | + if ($champ_pk) { |
|
| 2632 | + $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2633 | + } |
|
| 2634 | + // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2635 | + if ( |
|
| 2636 | + isset($champs[$champ_pk]) |
|
| 2637 | + && stripos((string) $champs[$champ_pk], 'default 0') !== false |
|
| 2638 | + ) { |
|
| 2639 | + $champs[$champ_pk] = trim(str_ireplace('default 0', '', (string) $champs[$champ_pk])); |
|
| 2640 | + } |
|
| 2641 | + |
|
| 2642 | + $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2643 | + foreach ($champs as $k => $v) { |
|
| 2644 | + $query .= "$s\n\t\t$k $v"; |
|
| 2645 | + $s = ','; |
|
| 2646 | + } |
|
| 2647 | + |
|
| 2648 | + $ifnotexists = ''; |
|
| 2649 | + if ($_ifnotexists) { |
|
| 2650 | + $version = spip_sqlite_fetch( |
|
| 2651 | + spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2652 | + '', |
|
| 2653 | + $serveur |
|
| 2654 | + ); |
|
| 2655 | + if (!function_exists('spip_version_compare')) { |
|
| 2656 | + include_spip('plugins/installer'); |
|
| 2657 | + } |
|
| 2658 | + |
|
| 2659 | + if ($version && spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2660 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 2661 | + } else { |
|
| 2662 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2663 | + $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2664 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 2665 | + return true; |
|
| 2666 | + } |
|
| 2667 | + } |
|
| 2668 | + } |
|
| 2669 | + |
|
| 2670 | + $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2671 | + |
|
| 2672 | + return "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2673 | 2673 | } |
| 2674 | 2674 | |
| 2675 | 2675 | |
@@ -2688,40 +2688,40 @@ discard block |
||
| 2688 | 2688 | * @return |
| 2689 | 2689 | */ |
| 2690 | 2690 | function _sqlite_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 2691 | - static $tables = []; |
|
| 2692 | - |
|
| 2693 | - if (!isset($tables[$table])) { |
|
| 2694 | - if (!$desc) { |
|
| 2695 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2696 | - $desc = $trouver_table($table, $serveur); |
|
| 2697 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 2698 | - if (!$desc) { |
|
| 2699 | - return $couples; |
|
| 2700 | - } |
|
| 2701 | - } |
|
| 2702 | - |
|
| 2703 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2704 | - // cependant, il faudra peut etre etendre |
|
| 2705 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 2706 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 2707 | - $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2708 | - |
|
| 2709 | - $now = _sqlite_func_now(true); |
|
| 2710 | - foreach ($desc['field'] as $k => $v) { |
|
| 2711 | - if (str_starts_with(strtolower(ltrim((string) $v)), 'timestamp')) { |
|
| 2712 | - $tables[$table]['desc'][$k] = $v; |
|
| 2713 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2714 | - } |
|
| 2715 | - } |
|
| 2716 | - } else { |
|
| 2717 | - $now = _sqlite_func_now(true); |
|
| 2718 | - foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2719 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2720 | - } |
|
| 2721 | - } |
|
| 2722 | - |
|
| 2723 | - // ajout des champs type 'timestamp' absents |
|
| 2724 | - return array_merge($tables[$table]['valeur'], $couples); |
|
| 2691 | + static $tables = []; |
|
| 2692 | + |
|
| 2693 | + if (!isset($tables[$table])) { |
|
| 2694 | + if (!$desc) { |
|
| 2695 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2696 | + $desc = $trouver_table($table, $serveur); |
|
| 2697 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 2698 | + if (!$desc) { |
|
| 2699 | + return $couples; |
|
| 2700 | + } |
|
| 2701 | + } |
|
| 2702 | + |
|
| 2703 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2704 | + // cependant, il faudra peut etre etendre |
|
| 2705 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 2706 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 2707 | + $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2708 | + |
|
| 2709 | + $now = _sqlite_func_now(true); |
|
| 2710 | + foreach ($desc['field'] as $k => $v) { |
|
| 2711 | + if (str_starts_with(strtolower(ltrim((string) $v)), 'timestamp')) { |
|
| 2712 | + $tables[$table]['desc'][$k] = $v; |
|
| 2713 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2714 | + } |
|
| 2715 | + } |
|
| 2716 | + } else { |
|
| 2717 | + $now = _sqlite_func_now(true); |
|
| 2718 | + foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2719 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2720 | + } |
|
| 2721 | + } |
|
| 2722 | + |
|
| 2723 | + // ajout des champs type 'timestamp' absents |
|
| 2724 | + return array_merge($tables[$table]['valeur'], $couples); |
|
| 2725 | 2725 | } |
| 2726 | 2726 | |
| 2727 | 2727 | |
@@ -2732,5 +2732,5 @@ discard block |
||
| 2732 | 2732 | * @return array|bool |
| 2733 | 2733 | */ |
| 2734 | 2734 | function spip_versions_sqlite() { |
| 2735 | - return _sqlite_charger_version(); |
|
| 2735 | + return _sqlite_charger_version(); |
|
| 2736 | 2736 | } |
@@ -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 | |
@@ -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 | } |
@@ -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/actions'); |
@@ -28,20 +28,20 @@ discard block |
||
| 28 | 28 | * @uses ajax_retour() |
| 29 | 29 | **/ |
| 30 | 30 | function exec_informer_dist() { |
| 31 | - $id = (int) _request('id'); |
|
| 32 | - $col = (int) _request('col'); |
|
| 33 | - $exclus = (int) _request('exclus'); |
|
| 34 | - $do = _request('do'); |
|
| 31 | + $id = (int) _request('id'); |
|
| 32 | + $col = (int) _request('col'); |
|
| 33 | + $exclus = (int) _request('exclus'); |
|
| 34 | + $do = _request('do'); |
|
| 35 | 35 | |
| 36 | - if (preg_match('/^\w*$/', (string) $do)) { |
|
| 37 | - if (!$do) { |
|
| 38 | - $do = 'aff'; |
|
| 39 | - } |
|
| 36 | + if (preg_match('/^\w*$/', (string) $do)) { |
|
| 37 | + if (!$do) { |
|
| 38 | + $do = 'aff'; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - $informer = charger_fonction('informer', 'inc'); |
|
| 42 | - $res = $informer($id, $col, $exclus, _request('rac'), _request('type'), $do); |
|
| 43 | - } else { |
|
| 44 | - $res = ''; |
|
| 45 | - } |
|
| 46 | - ajax_retour($res); |
|
| 41 | + $informer = charger_fonction('informer', 'inc'); |
|
| 42 | + $res = $informer($id, $col, $exclus, _request('rac'), _request('type'), $do); |
|
| 43 | + } else { |
|
| 44 | + $res = ''; |
|
| 45 | + } |
|
| 46 | + ajax_retour($res); |
|
| 47 | 47 | } |
@@ -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/minipres'); |
@@ -41,27 +41,27 @@ discard block |
||
| 41 | 41 | * |
| 42 | 42 | **/ |
| 43 | 43 | function exec_install_dist() { |
| 44 | - $etape = _request('etape'); |
|
| 45 | - $deja = (_FILE_CONNECT && analyse_fichier_connection(_FILE_CONNECT)); |
|
| 44 | + $etape = _request('etape'); |
|
| 45 | + $deja = (_FILE_CONNECT && analyse_fichier_connection(_FILE_CONNECT)); |
|
| 46 | 46 | |
| 47 | - // Si deja installe, on n'a plus le droit qu'a l'etape chmod |
|
| 48 | - // pour chgt post-install ou aux etapes supplementaires |
|
| 49 | - // de declaration de base externes. |
|
| 50 | - // Mais alors il faut authentifier car ecrire/index.php l'a omis |
|
| 47 | + // Si deja installe, on n'a plus le droit qu'a l'etape chmod |
|
| 48 | + // pour chgt post-install ou aux etapes supplementaires |
|
| 49 | + // de declaration de base externes. |
|
| 50 | + // Mais alors il faut authentifier car ecrire/index.php l'a omis |
|
| 51 | 51 | |
| 52 | - if ($deja && in_array($etape, ['chmod', 'sup1', 'sup2'])) { |
|
| 53 | - $auth = charger_fonction('auth', 'inc'); |
|
| 54 | - if (!$auth()) { |
|
| 55 | - verifier_visiteur(); |
|
| 56 | - $deja = (!autoriser('configurer')); |
|
| 57 | - } |
|
| 58 | - } |
|
| 59 | - if ($deja) { |
|
| 60 | - // Rien a faire ici |
|
| 61 | - echo minipres(); |
|
| 62 | - } else { |
|
| 63 | - include_spip('base/create'); |
|
| 64 | - $fonc = charger_fonction("etape_$etape", 'install'); |
|
| 65 | - $fonc(); |
|
| 66 | - } |
|
| 52 | + if ($deja && in_array($etape, ['chmod', 'sup1', 'sup2'])) { |
|
| 53 | + $auth = charger_fonction('auth', 'inc'); |
|
| 54 | + if (!$auth()) { |
|
| 55 | + verifier_visiteur(); |
|
| 56 | + $deja = (!autoriser('configurer')); |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | + if ($deja) { |
|
| 60 | + // Rien a faire ici |
|
| 61 | + echo minipres(); |
|
| 62 | + } else { |
|
| 63 | + include_spip('base/create'); |
|
| 64 | + $fonc = charger_fonction("etape_$etape", 'install'); |
|
| 65 | + $fonc(); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -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/actions'); |
@@ -29,70 +29,70 @@ discard block |
||
| 29 | 29 | **/ |
| 30 | 30 | function exec_upgrade_dist() { |
| 31 | 31 | |
| 32 | - if (!_FILE_CONNECT) { |
|
| 33 | - redirige_url_ecrire('install'); |
|
| 34 | - } |
|
| 32 | + if (!_FILE_CONNECT) { |
|
| 33 | + redirige_url_ecrire('install'); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - // Si reinstallation necessaire, message ad hoc |
|
| 37 | - if (_request('reinstall') == 'oui') { |
|
| 38 | - include_spip('inc/minipres'); |
|
| 39 | - $r = minipres( |
|
| 40 | - _T('titre_page_upgrade'), |
|
| 41 | - '<p><b>' |
|
| 42 | - . _T('texte_nouvelle_version_spip_1') |
|
| 43 | - . '</b><p> ' |
|
| 44 | - . _T( |
|
| 45 | - 'texte_nouvelle_version_spip_2', |
|
| 46 | - ['connect' => '<tt>' . _FILE_CONNECT . '</tt>'] |
|
| 47 | - ) |
|
| 48 | - . generer_form_ecrire( |
|
| 49 | - 'upgrade', |
|
| 50 | - "<input type='hidden' name='reinstall' value='non' />", |
|
| 51 | - '', |
|
| 52 | - _T('bouton_relancer_installation') |
|
| 53 | - ) |
|
| 54 | - ); |
|
| 55 | - echo $r; |
|
| 56 | - } elseif (_request('fin')) { |
|
| 57 | - include_spip('inc/plugin'); |
|
| 58 | - actualise_plugins_actifs(); |
|
| 59 | - include_spip('inc/headers'); |
|
| 60 | - $res = generer_url_ecrire('admin_plugin', 'var_mode=recalcul'); |
|
| 61 | - echo redirige_formulaire($res); |
|
| 62 | - } else { |
|
| 63 | - if (!isset($GLOBALS['meta']['version_installee'])) { |
|
| 64 | - $GLOBALS['meta']['version_installee'] = 0.0; |
|
| 65 | - } else { |
|
| 66 | - $GLOBALS['meta']['version_installee'] = |
|
| 67 | - (double) str_replace(',', '.', (string) $GLOBALS['meta']['version_installee']); |
|
| 68 | - } |
|
| 69 | - # NB: str_replace car, sur club-internet, il semble que version_installe soit |
|
| 70 | - # enregistree au format '1,812' et non '1.812' |
|
| 36 | + // Si reinstallation necessaire, message ad hoc |
|
| 37 | + if (_request('reinstall') == 'oui') { |
|
| 38 | + include_spip('inc/minipres'); |
|
| 39 | + $r = minipres( |
|
| 40 | + _T('titre_page_upgrade'), |
|
| 41 | + '<p><b>' |
|
| 42 | + . _T('texte_nouvelle_version_spip_1') |
|
| 43 | + . '</b><p> ' |
|
| 44 | + . _T( |
|
| 45 | + 'texte_nouvelle_version_spip_2', |
|
| 46 | + ['connect' => '<tt>' . _FILE_CONNECT . '</tt>'] |
|
| 47 | + ) |
|
| 48 | + . generer_form_ecrire( |
|
| 49 | + 'upgrade', |
|
| 50 | + "<input type='hidden' name='reinstall' value='non' />", |
|
| 51 | + '', |
|
| 52 | + _T('bouton_relancer_installation') |
|
| 53 | + ) |
|
| 54 | + ); |
|
| 55 | + echo $r; |
|
| 56 | + } elseif (_request('fin')) { |
|
| 57 | + include_spip('inc/plugin'); |
|
| 58 | + actualise_plugins_actifs(); |
|
| 59 | + include_spip('inc/headers'); |
|
| 60 | + $res = generer_url_ecrire('admin_plugin', 'var_mode=recalcul'); |
|
| 61 | + echo redirige_formulaire($res); |
|
| 62 | + } else { |
|
| 63 | + if (!isset($GLOBALS['meta']['version_installee'])) { |
|
| 64 | + $GLOBALS['meta']['version_installee'] = 0.0; |
|
| 65 | + } else { |
|
| 66 | + $GLOBALS['meta']['version_installee'] = |
|
| 67 | + (double) str_replace(',', '.', (string) $GLOBALS['meta']['version_installee']); |
|
| 68 | + } |
|
| 69 | + # NB: str_replace car, sur club-internet, il semble que version_installe soit |
|
| 70 | + # enregistree au format '1,812' et non '1.812' |
|
| 71 | 71 | |
| 72 | - // Erreur downgrade |
|
| 73 | - // (cas de double installation de fichiers SPIP sur une meme base) |
|
| 74 | - if ($GLOBALS['spip_version_base'] < $GLOBALS['meta']['version_installee']) { |
|
| 75 | - $commentaire = _T('info_mise_a_niveau_base_2'); |
|
| 76 | - } // Commentaire standard upgrade |
|
| 77 | - else { |
|
| 78 | - $commentaire = _T('texte_mise_a_niveau_base_1'); |
|
| 79 | - } |
|
| 72 | + // Erreur downgrade |
|
| 73 | + // (cas de double installation de fichiers SPIP sur une meme base) |
|
| 74 | + if ($GLOBALS['spip_version_base'] < $GLOBALS['meta']['version_installee']) { |
|
| 75 | + $commentaire = _T('info_mise_a_niveau_base_2'); |
|
| 76 | + } // Commentaire standard upgrade |
|
| 77 | + else { |
|
| 78 | + $commentaire = _T('texte_mise_a_niveau_base_1'); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - $commentaire .= '<br />[' . $GLOBALS['meta']['version_installee'] . '/' . $GLOBALS['spip_version_base'] . ']'; |
|
| 81 | + $commentaire .= '<br />[' . $GLOBALS['meta']['version_installee'] . '/' . $GLOBALS['spip_version_base'] . ']'; |
|
| 82 | 82 | |
| 83 | - $_POST['reinstall'] = 'non'; // pour copy_request dans admin |
|
| 84 | - include_spip('inc/headers'); |
|
| 85 | - $admin = charger_fonction('admin', 'inc'); |
|
| 86 | - $res = $admin('upgrade', _T('info_mise_a_niveau_base'), $commentaire); |
|
| 87 | - if ($res) { |
|
| 88 | - echo $res; |
|
| 89 | - } else { |
|
| 90 | - // effacer les alea pour forcer leur relecture |
|
| 91 | - // si jamais ils ont change pendant l'upgrade |
|
| 92 | - unset($GLOBALS['meta']['alea_ephemere']); |
|
| 93 | - unset($GLOBALS['meta']['alea_ephemere_ancien']); |
|
| 94 | - $res = redirige_action_auteur('purger', 'cache', 'upgrade', 'fin=oui', true); |
|
| 95 | - echo redirige_formulaire($res); |
|
| 96 | - } |
|
| 97 | - } |
|
| 83 | + $_POST['reinstall'] = 'non'; // pour copy_request dans admin |
|
| 84 | + include_spip('inc/headers'); |
|
| 85 | + $admin = charger_fonction('admin', 'inc'); |
|
| 86 | + $res = $admin('upgrade', _T('info_mise_a_niveau_base'), $commentaire); |
|
| 87 | + if ($res) { |
|
| 88 | + echo $res; |
|
| 89 | + } else { |
|
| 90 | + // effacer les alea pour forcer leur relecture |
|
| 91 | + // si jamais ils ont change pendant l'upgrade |
|
| 92 | + unset($GLOBALS['meta']['alea_ephemere']); |
|
| 93 | + unset($GLOBALS['meta']['alea_ephemere_ancien']); |
|
| 94 | + $res = redirige_action_auteur('purger', 'cache', 'upgrade', 'fin=oui', true); |
|
| 95 | + echo redirige_formulaire($res); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | . '</b><p> ' |
| 44 | 44 | . _T( |
| 45 | 45 | 'texte_nouvelle_version_spip_2', |
| 46 | - ['connect' => '<tt>' . _FILE_CONNECT . '</tt>'] |
|
| 46 | + ['connect' => '<tt>'._FILE_CONNECT.'</tt>'] |
|
| 47 | 47 | ) |
| 48 | 48 | . generer_form_ecrire( |
| 49 | 49 | 'upgrade', |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | $commentaire = _T('texte_mise_a_niveau_base_1'); |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - $commentaire .= '<br />[' . $GLOBALS['meta']['version_installee'] . '/' . $GLOBALS['spip_version_base'] . ']'; |
|
| 81 | + $commentaire .= '<br />['.$GLOBALS['meta']['version_installee'].'/'.$GLOBALS['spip_version_base'].']'; |
|
| 82 | 82 | |
| 83 | 83 | $_POST['reinstall'] = 'non'; // pour copy_request dans admin |
| 84 | 84 | include_spip('inc/headers'); |
@@ -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 | /** |
@@ -26,23 +26,23 @@ discard block |
||
| 26 | 26 | * @uses ajax_retour() |
| 27 | 27 | **/ |
| 28 | 28 | function exec_plonger_dist() { |
| 29 | - include_spip('inc/actions'); |
|
| 29 | + include_spip('inc/actions'); |
|
| 30 | 30 | |
| 31 | - $rac = preg_replace(',[^\w\,/#&;-]+,', ' ', (string) _request('rac')); |
|
| 32 | - $id = (int) _request('id'); |
|
| 33 | - $exclus = (int) _request('exclus'); |
|
| 34 | - $col = (int) _request('col'); |
|
| 35 | - $do = _request('do'); |
|
| 36 | - if (preg_match('/^\w*$/', (string) $do)) { |
|
| 37 | - if (!$do) { |
|
| 38 | - $do = 'aff'; |
|
| 39 | - } |
|
| 31 | + $rac = preg_replace(',[^\w\,/#&;-]+,', ' ', (string) _request('rac')); |
|
| 32 | + $id = (int) _request('id'); |
|
| 33 | + $exclus = (int) _request('exclus'); |
|
| 34 | + $col = (int) _request('col'); |
|
| 35 | + $do = _request('do'); |
|
| 36 | + if (preg_match('/^\w*$/', (string) $do)) { |
|
| 37 | + if (!$do) { |
|
| 38 | + $do = 'aff'; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - $plonger = charger_fonction('plonger', 'inc'); |
|
| 42 | - $r = $plonger($id, spip_htmlentities($rac), [], $col, $exclus, $do); |
|
| 43 | - } else { |
|
| 44 | - $r = ''; |
|
| 45 | - } |
|
| 41 | + $plonger = charger_fonction('plonger', 'inc'); |
|
| 42 | + $r = $plonger($id, spip_htmlentities($rac), [], $col, $exclus, $do); |
|
| 43 | + } else { |
|
| 44 | + $r = ''; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - ajax_retour($r); |
|
| 47 | + ajax_retour($r); |
|
| 48 | 48 | } |