@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - return; |
|
| 21 | + return; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | define('_DEFAULT_DB', 'spip'); |
@@ -32,130 +32,130 @@ discard block |
||
| 32 | 32 | |
| 33 | 33 | // https://code.spip.net/@req_pg_dist |
| 34 | 34 | function req_pg_dist($addr, $port, $login, $pass, $db = '', $prefixe = '') { |
| 35 | - static $last_connect = []; |
|
| 36 | - if (!extension_loaded('pgsql')) { |
|
| 37 | - return false; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - // si provient de selectdb |
|
| 41 | - if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 42 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 43 | - ${$a} = $last_connect[$a]; |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - [$host, $p] = array_pad(explode(';', $addr), 2, null); |
|
| 47 | - if ($p > 0) { |
|
| 48 | - $port = " port=$p"; |
|
| 49 | - } else { |
|
| 50 | - $port = ''; |
|
| 51 | - } |
|
| 52 | - $erreurs = []; |
|
| 53 | - if ($db) { |
|
| 54 | - @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 55 | - } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 56 | - $erreurs[] = pg_last_error(); |
|
| 57 | - if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 58 | - $db = $login; |
|
| 59 | - } else { |
|
| 60 | - $erreurs[] = pg_last_error(); |
|
| 61 | - $db = _DEFAULT_DB; |
|
| 62 | - $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - if (!$link) { |
|
| 66 | - $erreurs[] = pg_last_error(); |
|
| 67 | - foreach ($erreurs as $e) { |
|
| 68 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - return false; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - if ($link) { |
|
| 75 | - $last_connect = [ |
|
| 76 | - 'addr' => $addr, |
|
| 77 | - 'port' => $port, |
|
| 78 | - 'login' => $login, |
|
| 79 | - 'pass' => $pass, |
|
| 80 | - 'db' => $db, |
|
| 81 | - 'prefixe' => $prefixe, |
|
| 82 | - ]; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - spip_log( |
|
| 86 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 87 | - 'pg.' . _LOG_DEBUG |
|
| 88 | - ); |
|
| 89 | - |
|
| 90 | - return !$link ? false : [ |
|
| 91 | - 'db' => $db, |
|
| 92 | - 'prefixe' => $prefixe ?: $db, |
|
| 93 | - 'link' => $link, |
|
| 94 | - ]; |
|
| 35 | + static $last_connect = []; |
|
| 36 | + if (!extension_loaded('pgsql')) { |
|
| 37 | + return false; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + // si provient de selectdb |
|
| 41 | + if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 42 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 43 | + ${$a} = $last_connect[$a]; |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + [$host, $p] = array_pad(explode(';', $addr), 2, null); |
|
| 47 | + if ($p > 0) { |
|
| 48 | + $port = " port=$p"; |
|
| 49 | + } else { |
|
| 50 | + $port = ''; |
|
| 51 | + } |
|
| 52 | + $erreurs = []; |
|
| 53 | + if ($db) { |
|
| 54 | + @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 55 | + } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 56 | + $erreurs[] = pg_last_error(); |
|
| 57 | + if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 58 | + $db = $login; |
|
| 59 | + } else { |
|
| 60 | + $erreurs[] = pg_last_error(); |
|
| 61 | + $db = _DEFAULT_DB; |
|
| 62 | + $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + if (!$link) { |
|
| 66 | + $erreurs[] = pg_last_error(); |
|
| 67 | + foreach ($erreurs as $e) { |
|
| 68 | + spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + return false; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + if ($link) { |
|
| 75 | + $last_connect = [ |
|
| 76 | + 'addr' => $addr, |
|
| 77 | + 'port' => $port, |
|
| 78 | + 'login' => $login, |
|
| 79 | + 'pass' => $pass, |
|
| 80 | + 'db' => $db, |
|
| 81 | + 'prefixe' => $prefixe, |
|
| 82 | + ]; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + spip_log( |
|
| 86 | + "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 87 | + 'pg.' . _LOG_DEBUG |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + return !$link ? false : [ |
|
| 91 | + 'db' => $db, |
|
| 92 | + 'prefixe' => $prefixe ?: $db, |
|
| 93 | + 'link' => $link, |
|
| 94 | + ]; |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | $GLOBALS['spip_pg_functions_1'] = [ |
| 98 | - 'alter' => 'spip_pg_alter', |
|
| 99 | - 'count' => 'spip_pg_count', |
|
| 100 | - 'countsel' => 'spip_pg_countsel', |
|
| 101 | - 'create' => 'spip_pg_create', |
|
| 102 | - 'create_base' => 'spip_pg_create_base', |
|
| 103 | - 'create_view' => 'spip_pg_create_view', |
|
| 104 | - 'date_proche' => 'spip_pg_date_proche', |
|
| 105 | - 'delete' => 'spip_pg_delete', |
|
| 106 | - 'drop_table' => 'spip_pg_drop_table', |
|
| 107 | - 'drop_view' => 'spip_pg_drop_view', |
|
| 108 | - 'errno' => 'spip_pg_errno', |
|
| 109 | - 'error' => 'spip_pg_error', |
|
| 110 | - 'explain' => 'spip_pg_explain', |
|
| 111 | - 'fetch' => 'spip_pg_fetch', |
|
| 112 | - 'seek' => 'spip_pg_seek', |
|
| 113 | - 'free' => 'spip_pg_free', |
|
| 114 | - 'hex' => 'spip_pg_hex', |
|
| 115 | - 'in' => 'spip_pg_in', |
|
| 116 | - 'insert' => 'spip_pg_insert', |
|
| 117 | - 'insertq' => 'spip_pg_insertq', |
|
| 118 | - 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 119 | - 'listdbs' => 'spip_pg_listdbs', |
|
| 120 | - 'multi' => 'spip_pg_multi', |
|
| 121 | - 'optimize' => 'spip_pg_optimize', |
|
| 122 | - 'query' => 'spip_pg_query', |
|
| 123 | - 'quote' => 'spip_pg_quote', |
|
| 124 | - 'replace' => 'spip_pg_replace', |
|
| 125 | - 'replace_multi' => 'spip_pg_replace_multi', |
|
| 126 | - 'select' => 'spip_pg_select', |
|
| 127 | - 'selectdb' => 'spip_pg_selectdb', |
|
| 128 | - 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 129 | - 'showbase' => 'spip_pg_showbase', |
|
| 130 | - 'showtable' => 'spip_pg_showtable', |
|
| 131 | - 'update' => 'spip_pg_update', |
|
| 132 | - 'updateq' => 'spip_pg_updateq', |
|
| 98 | + 'alter' => 'spip_pg_alter', |
|
| 99 | + 'count' => 'spip_pg_count', |
|
| 100 | + 'countsel' => 'spip_pg_countsel', |
|
| 101 | + 'create' => 'spip_pg_create', |
|
| 102 | + 'create_base' => 'spip_pg_create_base', |
|
| 103 | + 'create_view' => 'spip_pg_create_view', |
|
| 104 | + 'date_proche' => 'spip_pg_date_proche', |
|
| 105 | + 'delete' => 'spip_pg_delete', |
|
| 106 | + 'drop_table' => 'spip_pg_drop_table', |
|
| 107 | + 'drop_view' => 'spip_pg_drop_view', |
|
| 108 | + 'errno' => 'spip_pg_errno', |
|
| 109 | + 'error' => 'spip_pg_error', |
|
| 110 | + 'explain' => 'spip_pg_explain', |
|
| 111 | + 'fetch' => 'spip_pg_fetch', |
|
| 112 | + 'seek' => 'spip_pg_seek', |
|
| 113 | + 'free' => 'spip_pg_free', |
|
| 114 | + 'hex' => 'spip_pg_hex', |
|
| 115 | + 'in' => 'spip_pg_in', |
|
| 116 | + 'insert' => 'spip_pg_insert', |
|
| 117 | + 'insertq' => 'spip_pg_insertq', |
|
| 118 | + 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 119 | + 'listdbs' => 'spip_pg_listdbs', |
|
| 120 | + 'multi' => 'spip_pg_multi', |
|
| 121 | + 'optimize' => 'spip_pg_optimize', |
|
| 122 | + 'query' => 'spip_pg_query', |
|
| 123 | + 'quote' => 'spip_pg_quote', |
|
| 124 | + 'replace' => 'spip_pg_replace', |
|
| 125 | + 'replace_multi' => 'spip_pg_replace_multi', |
|
| 126 | + 'select' => 'spip_pg_select', |
|
| 127 | + 'selectdb' => 'spip_pg_selectdb', |
|
| 128 | + 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 129 | + 'showbase' => 'spip_pg_showbase', |
|
| 130 | + 'showtable' => 'spip_pg_showtable', |
|
| 131 | + 'update' => 'spip_pg_update', |
|
| 132 | + 'updateq' => 'spip_pg_updateq', |
|
| 133 | 133 | ]; |
| 134 | 134 | |
| 135 | 135 | // Par ou ca passe une fois les traductions faites |
| 136 | 136 | // https://code.spip.net/@spip_pg_trace_query |
| 137 | 137 | function spip_pg_trace_query($query, $serveur = '') { |
| 138 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 139 | - $prefixe = $connexion['prefixe']; |
|
| 140 | - $link = $connexion['link']; |
|
| 141 | - $db = $connexion['db']; |
|
| 142 | - |
|
| 143 | - if (isset($_GET['var_profile'])) { |
|
| 144 | - include_spip('public/tracer'); |
|
| 145 | - $t = trace_query_start(); |
|
| 146 | - $e = ''; |
|
| 147 | - } else { |
|
| 148 | - $t = 0; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $connexion['last'] = $query; |
|
| 152 | - $r = spip_pg_query_simple($link, $query); |
|
| 153 | - |
|
| 154 | - // Log de l'erreur eventuelle |
|
| 155 | - if ($e = spip_pg_errno($serveur)) { |
|
| 156 | - $e .= spip_pg_error($query, $serveur); |
|
| 157 | - } // et du fautif |
|
| 158 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 138 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 139 | + $prefixe = $connexion['prefixe']; |
|
| 140 | + $link = $connexion['link']; |
|
| 141 | + $db = $connexion['db']; |
|
| 142 | + |
|
| 143 | + if (isset($_GET['var_profile'])) { |
|
| 144 | + include_spip('public/tracer'); |
|
| 145 | + $t = trace_query_start(); |
|
| 146 | + $e = ''; |
|
| 147 | + } else { |
|
| 148 | + $t = 0; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $connexion['last'] = $query; |
|
| 152 | + $r = spip_pg_query_simple($link, $query); |
|
| 153 | + |
|
| 154 | + // Log de l'erreur eventuelle |
|
| 155 | + if ($e = spip_pg_errno($serveur)) { |
|
| 156 | + $e .= spip_pg_error($query, $serveur); |
|
| 157 | + } // et du fautif |
|
| 158 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | // Fonction de requete generale quand on est sur que c'est SQL standard. |
@@ -163,30 +163,30 @@ discard block |
||
| 163 | 163 | |
| 164 | 164 | // https://code.spip.net/@spip_pg_query |
| 165 | 165 | function spip_pg_query($query, $serveur = '', $requeter = true) { |
| 166 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 167 | - $prefixe = $connexion['prefixe']; |
|
| 168 | - $link = $connexion['link']; |
|
| 169 | - $db = $connexion['db']; |
|
| 170 | - |
|
| 171 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 172 | - $suite = strstr($query, (string) $regs[0]); |
|
| 173 | - $query = substr($query, 0, -strlen($suite)); |
|
| 174 | - } else { |
|
| 175 | - $suite = ''; |
|
| 176 | - } |
|
| 177 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 178 | - |
|
| 179 | - // renvoyer la requete inerte si demandee |
|
| 180 | - if (!$requeter) { |
|
| 181 | - return $query; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - return spip_pg_trace_query($query, $serveur); |
|
| 166 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 167 | + $prefixe = $connexion['prefixe']; |
|
| 168 | + $link = $connexion['link']; |
|
| 169 | + $db = $connexion['db']; |
|
| 170 | + |
|
| 171 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 172 | + $suite = strstr($query, (string) $regs[0]); |
|
| 173 | + $query = substr($query, 0, -strlen($suite)); |
|
| 174 | + } else { |
|
| 175 | + $suite = ''; |
|
| 176 | + } |
|
| 177 | + $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 178 | + |
|
| 179 | + // renvoyer la requete inerte si demandee |
|
| 180 | + if (!$requeter) { |
|
| 181 | + return $query; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + return spip_pg_trace_query($query, $serveur); |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | function spip_pg_query_simple($link, $query) { |
| 188 | - #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 189 | - return pg_query($link, $query); |
|
| 188 | + #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 189 | + return pg_query($link, $query); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | /* |
@@ -198,200 +198,200 @@ discard block |
||
| 198 | 198 | * de requetes showtable intempestives |
| 199 | 199 | */ |
| 200 | 200 | function spip_pg_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 201 | - static $tables = []; |
|
| 202 | - |
|
| 203 | - if (!isset($tables[$table])) { |
|
| 204 | - if (!$desc) { |
|
| 205 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 206 | - $desc = $trouver_table($table, $serveur); |
|
| 207 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 208 | - if (!$desc) { |
|
| 209 | - return $couples; |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 214 | - // cependant, il faudra peut etre etendre |
|
| 215 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 216 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 217 | - $tables[$table] = []; |
|
| 218 | - foreach ($desc['field'] as $k => $v) { |
|
| 219 | - $v = strtolower(ltrim($v)); |
|
| 220 | - // ne pas ajouter de timestamp now() si un default est specifie |
|
| 221 | - if (strpos($v, 'timestamp') === 0 and strpos($v, 'default') === false) { |
|
| 222 | - $tables[$table][] = $k; |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // ajout des champs type 'timestamp' absents |
|
| 228 | - foreach ($tables[$table] as $maj) { |
|
| 229 | - if (!array_key_exists($maj, $couples)) { |
|
| 230 | - $couples[$maj] = 'NOW()'; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - return $couples; |
|
| 201 | + static $tables = []; |
|
| 202 | + |
|
| 203 | + if (!isset($tables[$table])) { |
|
| 204 | + if (!$desc) { |
|
| 205 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 206 | + $desc = $trouver_table($table, $serveur); |
|
| 207 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 208 | + if (!$desc) { |
|
| 209 | + return $couples; |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 214 | + // cependant, il faudra peut etre etendre |
|
| 215 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 216 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 217 | + $tables[$table] = []; |
|
| 218 | + foreach ($desc['field'] as $k => $v) { |
|
| 219 | + $v = strtolower(ltrim($v)); |
|
| 220 | + // ne pas ajouter de timestamp now() si un default est specifie |
|
| 221 | + if (strpos($v, 'timestamp') === 0 and strpos($v, 'default') === false) { |
|
| 222 | + $tables[$table][] = $k; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // ajout des champs type 'timestamp' absents |
|
| 228 | + foreach ($tables[$table] as $maj) { |
|
| 229 | + if (!array_key_exists($maj, $couples)) { |
|
| 230 | + $couples[$maj] = 'NOW()'; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + return $couples; |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | |
| 238 | 238 | // Alter en PG ne traite pas les index |
| 239 | 239 | // https://code.spip.net/@spip_pg_alter |
| 240 | 240 | function spip_pg_alter($query, $serveur = '', $requeter = true) { |
| 241 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 242 | - // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 243 | - // ou revoir l'api de sql_alter en creant un |
|
| 244 | - // sql_alter_table($table,array($actions)); |
|
| 245 | - if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 246 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 247 | - |
|
| 248 | - return false; |
|
| 249 | - } |
|
| 250 | - $debut = $regs[1]; |
|
| 251 | - $table = $regs[3]; |
|
| 252 | - $suite = $regs[4]; |
|
| 253 | - $todo = explode(',', $suite); |
|
| 254 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 255 | - $todo2 = []; |
|
| 256 | - $i = 0; |
|
| 257 | - $ouverte = false; |
|
| 258 | - while ($do = array_shift($todo)) { |
|
| 259 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 260 | - $o = (false !== strpos($do, '(')); |
|
| 261 | - $f = (false !== strpos($do, ')')); |
|
| 262 | - if ($o and !$f) { |
|
| 263 | - $ouverte = true; |
|
| 264 | - } elseif ($f) { |
|
| 265 | - $ouverte = false; |
|
| 266 | - } |
|
| 267 | - if (!$ouverte) { |
|
| 268 | - $i++; |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - $todo = $todo2; |
|
| 272 | - $query = $debut . ' ' . array_shift($todo); |
|
| 273 | - |
|
| 274 | - if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 275 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 276 | - } else { |
|
| 277 | - if ($r[1]) { |
|
| 278 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 279 | - } |
|
| 280 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 281 | - if (function_exists($f)) { |
|
| 282 | - $f($r[2], $r[4], $serveur, $requeter); |
|
| 283 | - } else { |
|
| 284 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - // Alter a plusieurs args. Faudrait optimiser. |
|
| 288 | - if ($todo) { |
|
| 289 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 290 | - } |
|
| 241 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 242 | + // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 243 | + // ou revoir l'api de sql_alter en creant un |
|
| 244 | + // sql_alter_table($table,array($actions)); |
|
| 245 | + if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 246 | + spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 247 | + |
|
| 248 | + return false; |
|
| 249 | + } |
|
| 250 | + $debut = $regs[1]; |
|
| 251 | + $table = $regs[3]; |
|
| 252 | + $suite = $regs[4]; |
|
| 253 | + $todo = explode(',', $suite); |
|
| 254 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 255 | + $todo2 = []; |
|
| 256 | + $i = 0; |
|
| 257 | + $ouverte = false; |
|
| 258 | + while ($do = array_shift($todo)) { |
|
| 259 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 260 | + $o = (false !== strpos($do, '(')); |
|
| 261 | + $f = (false !== strpos($do, ')')); |
|
| 262 | + if ($o and !$f) { |
|
| 263 | + $ouverte = true; |
|
| 264 | + } elseif ($f) { |
|
| 265 | + $ouverte = false; |
|
| 266 | + } |
|
| 267 | + if (!$ouverte) { |
|
| 268 | + $i++; |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + $todo = $todo2; |
|
| 272 | + $query = $debut . ' ' . array_shift($todo); |
|
| 273 | + |
|
| 274 | + if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 275 | + spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 276 | + } else { |
|
| 277 | + if ($r[1]) { |
|
| 278 | + spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 279 | + } |
|
| 280 | + $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 281 | + if (function_exists($f)) { |
|
| 282 | + $f($r[2], $r[4], $serveur, $requeter); |
|
| 283 | + } else { |
|
| 284 | + spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + // Alter a plusieurs args. Faudrait optimiser. |
|
| 288 | + if ($todo) { |
|
| 289 | + spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 290 | + } |
|
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | // https://code.spip.net/@spip_pg_alter_change |
| 294 | 294 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 295 | - if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 296 | - spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 297 | - } else { |
|
| 298 | - [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 299 | - $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 300 | - if ($null) { |
|
| 301 | - $actions[] = "ALTER $old SET NOT NULL"; |
|
| 302 | - } else { |
|
| 303 | - $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - if ($d = ($default ?: $def2)) { |
|
| 307 | - $actions[] = "ALTER $old SET $d"; |
|
| 308 | - } else { |
|
| 309 | - $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 313 | - |
|
| 314 | - if ($old != $new) { |
|
| 315 | - spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 316 | - } |
|
| 317 | - } |
|
| 295 | + if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 296 | + spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 297 | + } else { |
|
| 298 | + [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 299 | + $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 300 | + if ($null) { |
|
| 301 | + $actions[] = "ALTER $old SET NOT NULL"; |
|
| 302 | + } else { |
|
| 303 | + $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + if ($d = ($default ?: $def2)) { |
|
| 307 | + $actions[] = "ALTER $old SET $d"; |
|
| 308 | + } else { |
|
| 309 | + $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 313 | + |
|
| 314 | + if ($old != $new) { |
|
| 315 | + spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | // https://code.spip.net/@spip_pg_alter_add |
| 321 | 321 | function spip_pg_alter_add($table, $arg, $serveur = '', $requeter = true) { |
| 322 | - $nom_index = null; |
|
| 323 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 324 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 325 | - |
|
| 326 | - return null; |
|
| 327 | - } |
|
| 328 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 329 | - preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 330 | - if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 331 | - $m[2] = $n[1]; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 335 | - } elseif ($r[1][0] == 'P') { |
|
| 336 | - // la primary peut etre sur plusieurs champs |
|
| 337 | - $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 338 | - $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 339 | - |
|
| 340 | - return spip_pg_query( |
|
| 341 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 342 | - $serveur, |
|
| 343 | - $requeter |
|
| 344 | - ); |
|
| 345 | - } else { |
|
| 346 | - preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 347 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 348 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 349 | - // |
|
| 350 | - // nom_index (colonnes) |
|
| 351 | - if ($m[2]) { |
|
| 352 | - $colonnes = substr($m[2], 1, -1); |
|
| 353 | - $nom_index = $m[1]; |
|
| 354 | - } else { |
|
| 355 | - // (colonne) |
|
| 356 | - if ($m[1][0] == '(') { |
|
| 357 | - $colonnes = substr($m[1], 1, -1); |
|
| 358 | - if (false !== strpos(',', $colonnes)) { |
|
| 359 | - spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 360 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 361 | - } else { |
|
| 362 | - $nom_index = $colonnes; |
|
| 363 | - } |
|
| 364 | - } // nom_index |
|
| 365 | - else { |
|
| 366 | - $nom_index = $colonnes = $m[1]; |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 371 | - } |
|
| 322 | + $nom_index = null; |
|
| 323 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 324 | + spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 325 | + |
|
| 326 | + return null; |
|
| 327 | + } |
|
| 328 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 329 | + preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 330 | + if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 331 | + $m[2] = $n[1]; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 335 | + } elseif ($r[1][0] == 'P') { |
|
| 336 | + // la primary peut etre sur plusieurs champs |
|
| 337 | + $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 338 | + $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 339 | + |
|
| 340 | + return spip_pg_query( |
|
| 341 | + "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 342 | + $serveur, |
|
| 343 | + $requeter |
|
| 344 | + ); |
|
| 345 | + } else { |
|
| 346 | + preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 347 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 348 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 349 | + // |
|
| 350 | + // nom_index (colonnes) |
|
| 351 | + if ($m[2]) { |
|
| 352 | + $colonnes = substr($m[2], 1, -1); |
|
| 353 | + $nom_index = $m[1]; |
|
| 354 | + } else { |
|
| 355 | + // (colonne) |
|
| 356 | + if ($m[1][0] == '(') { |
|
| 357 | + $colonnes = substr($m[1], 1, -1); |
|
| 358 | + if (false !== strpos(',', $colonnes)) { |
|
| 359 | + spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 360 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 361 | + } else { |
|
| 362 | + $nom_index = $colonnes; |
|
| 363 | + } |
|
| 364 | + } // nom_index |
|
| 365 | + else { |
|
| 366 | + $nom_index = $colonnes = $m[1]; |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 371 | + } |
|
| 372 | 372 | } |
| 373 | 373 | |
| 374 | 374 | // https://code.spip.net/@spip_pg_alter_drop |
| 375 | 375 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 376 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 377 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 378 | - } else { |
|
| 379 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 380 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 381 | - } elseif ($r[1][0] == 'P') { |
|
| 382 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 383 | - } else { |
|
| 384 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 385 | - } |
|
| 386 | - } |
|
| 376 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 377 | + spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 378 | + } else { |
|
| 379 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 380 | + return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 381 | + } elseif ($r[1][0] == 'P') { |
|
| 382 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 383 | + } else { |
|
| 384 | + return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 385 | + } |
|
| 386 | + } |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 390 | - if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 391 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 392 | - } else { |
|
| 393 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 394 | - } |
|
| 390 | + if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 391 | + spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 392 | + } else { |
|
| 393 | + return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 394 | + } |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | // attention (en pg) : |
@@ -399,17 +399,17 @@ discard block |
||
| 399 | 399 | // - alter table A rename X to Y = changer le nom de la colonne X en Y |
| 400 | 400 | // pour l'instant, traiter simplement RENAME TO X |
| 401 | 401 | function spip_pg_alter_rename($table, $arg, $serveur = '', $requeter = true) { |
| 402 | - $rename = ''; |
|
| 403 | - // si TO, mais pas au debut |
|
| 404 | - if (!stripos($arg, 'TO ')) { |
|
| 405 | - $rename = $arg; |
|
| 406 | - } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 407 | - $rename = $r[2]; |
|
| 408 | - } else { |
|
| 409 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 402 | + $rename = ''; |
|
| 403 | + // si TO, mais pas au debut |
|
| 404 | + if (!stripos($arg, 'TO ')) { |
|
| 405 | + $rename = $arg; |
|
| 406 | + } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 407 | + $rename = $r[2]; |
|
| 408 | + } else { |
|
| 409 | + spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | |
@@ -425,60 +425,60 @@ discard block |
||
| 425 | 425 | * @return bool ou requete |
| 426 | 426 | */ |
| 427 | 427 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 428 | - if (!($nom or $table or $champs)) { |
|
| 429 | - spip_log( |
|
| 430 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 431 | - 'pg.' . _LOG_ERREUR |
|
| 432 | - ); |
|
| 433 | - |
|
| 434 | - return false; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - $nom = str_replace('`', '', $nom); |
|
| 438 | - $champs = str_replace('`', '', $champs); |
|
| 439 | - |
|
| 440 | - // PG ne differentie pas noms des index en fonction des tables |
|
| 441 | - // il faut donc creer des noms uniques d'index pour une base pg |
|
| 442 | - $nom = $table . '_' . $nom; |
|
| 443 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 444 | - if (!is_array($champs)) { |
|
| 445 | - if ($champs[0] == '(') { |
|
| 446 | - $champs = substr($champs, 1, -1); |
|
| 447 | - } |
|
| 448 | - $champs = [$champs]; |
|
| 449 | - } |
|
| 450 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 451 | - if (!$requeter) { |
|
| 452 | - return $query; |
|
| 453 | - } |
|
| 454 | - $res = spip_pg_query($query, $serveur, $requeter); |
|
| 455 | - |
|
| 456 | - return $res; |
|
| 428 | + if (!($nom or $table or $champs)) { |
|
| 429 | + spip_log( |
|
| 430 | + "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 431 | + 'pg.' . _LOG_ERREUR |
|
| 432 | + ); |
|
| 433 | + |
|
| 434 | + return false; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + $nom = str_replace('`', '', $nom); |
|
| 438 | + $champs = str_replace('`', '', $champs); |
|
| 439 | + |
|
| 440 | + // PG ne differentie pas noms des index en fonction des tables |
|
| 441 | + // il faut donc creer des noms uniques d'index pour une base pg |
|
| 442 | + $nom = $table . '_' . $nom; |
|
| 443 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 444 | + if (!is_array($champs)) { |
|
| 445 | + if ($champs[0] == '(') { |
|
| 446 | + $champs = substr($champs, 1, -1); |
|
| 447 | + } |
|
| 448 | + $champs = [$champs]; |
|
| 449 | + } |
|
| 450 | + $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 451 | + if (!$requeter) { |
|
| 452 | + return $query; |
|
| 453 | + } |
|
| 454 | + $res = spip_pg_query($query, $serveur, $requeter); |
|
| 455 | + |
|
| 456 | + return $res; |
|
| 457 | 457 | } |
| 458 | 458 | |
| 459 | 459 | |
| 460 | 460 | // https://code.spip.net/@spip_pg_explain |
| 461 | 461 | function spip_pg_explain($query, $serveur = '', $requeter = true) { |
| 462 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 463 | - return []; |
|
| 464 | - } |
|
| 465 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 466 | - $prefixe = $connexion['prefixe']; |
|
| 467 | - $link = $connexion['link']; |
|
| 468 | - if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 469 | - $suite = strstr($query, (string) $regs[0]); |
|
| 470 | - $query = substr($query, 0, -strlen($suite)); |
|
| 471 | - } else { |
|
| 472 | - $suite = ''; |
|
| 473 | - } |
|
| 474 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 475 | - |
|
| 476 | - if (!$requeter) { |
|
| 477 | - return $query; |
|
| 478 | - } |
|
| 479 | - $r = spip_pg_query_simple($link, $query); |
|
| 480 | - |
|
| 481 | - return spip_pg_fetch($r, null, $serveur); |
|
| 462 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 463 | + return []; |
|
| 464 | + } |
|
| 465 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 466 | + $prefixe = $connexion['prefixe']; |
|
| 467 | + $link = $connexion['link']; |
|
| 468 | + if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 469 | + $suite = strstr($query, (string) $regs[0]); |
|
| 470 | + $query = substr($query, 0, -strlen($suite)); |
|
| 471 | + } else { |
|
| 472 | + $suite = ''; |
|
| 473 | + } |
|
| 474 | + $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 475 | + |
|
| 476 | + if (!$requeter) { |
|
| 477 | + return $query; |
|
| 478 | + } |
|
| 479 | + $r = spip_pg_query_simple($link, $query); |
|
| 480 | + |
|
| 481 | + return spip_pg_fetch($r, null, $serveur); |
|
| 482 | 482 | } |
| 483 | 483 | |
| 484 | 484 | |
@@ -497,94 +497,94 @@ discard block |
||
| 497 | 497 | * - False en cas d'erreur. |
| 498 | 498 | **/ |
| 499 | 499 | function spip_pg_selectdb($db, $serveur = '', $requeter = true) { |
| 500 | - // se connecter a la base indiquee |
|
| 501 | - // avec les identifiants connus |
|
| 502 | - $index = $serveur ? strtolower($serveur) : 0; |
|
| 503 | - |
|
| 504 | - if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 505 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 506 | - return $db; |
|
| 507 | - } |
|
| 508 | - } else { |
|
| 509 | - return false; |
|
| 510 | - } |
|
| 500 | + // se connecter a la base indiquee |
|
| 501 | + // avec les identifiants connus |
|
| 502 | + $index = $serveur ? strtolower($serveur) : 0; |
|
| 503 | + |
|
| 504 | + if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 505 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 506 | + return $db; |
|
| 507 | + } |
|
| 508 | + } else { |
|
| 509 | + return false; |
|
| 510 | + } |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | // Qu'une seule base pour le moment |
| 514 | 514 | |
| 515 | 515 | // https://code.spip.net/@spip_pg_listdbs |
| 516 | 516 | function spip_pg_listdbs($serveur) { |
| 517 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 518 | - $link = $connexion['link']; |
|
| 519 | - $dbs = []; |
|
| 520 | - $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 521 | - while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 522 | - $dbs[] = reset($row); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - return $dbs; |
|
| 517 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 518 | + $link = $connexion['link']; |
|
| 519 | + $dbs = []; |
|
| 520 | + $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 521 | + while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 522 | + $dbs[] = reset($row); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + return $dbs; |
|
| 526 | 526 | } |
| 527 | 527 | |
| 528 | 528 | // https://code.spip.net/@spip_pg_select |
| 529 | 529 | function spip_pg_select( |
| 530 | - $select, |
|
| 531 | - $from, |
|
| 532 | - $where = '', |
|
| 533 | - $groupby = [], |
|
| 534 | - $orderby = '', |
|
| 535 | - $limit = '', |
|
| 536 | - $having = '', |
|
| 537 | - $serveur = '', |
|
| 538 | - $requeter = true |
|
| 530 | + $select, |
|
| 531 | + $from, |
|
| 532 | + $where = '', |
|
| 533 | + $groupby = [], |
|
| 534 | + $orderby = '', |
|
| 535 | + $limit = '', |
|
| 536 | + $having = '', |
|
| 537 | + $serveur = '', |
|
| 538 | + $requeter = true |
|
| 539 | 539 | ) { |
| 540 | 540 | |
| 541 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 542 | - $prefixe = $connexion['prefixe']; |
|
| 543 | - $link = $connexion['link']; |
|
| 544 | - $db = $connexion['db']; |
|
| 545 | - |
|
| 546 | - $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 547 | - if ($limit) { |
|
| 548 | - $offset = $limatch[2]; |
|
| 549 | - $count = $limatch[3]; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - $select = spip_pg_frommysql($select); |
|
| 553 | - |
|
| 554 | - // si pas de tri explicitement demande, le GROUP BY ne |
|
| 555 | - // contient que la clef primaire. |
|
| 556 | - // lui ajouter alors le champ de tri par defaut |
|
| 557 | - if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 558 | - $groupby[] = $groupbyplus[1]; |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - $orderby = spip_pg_orderby($orderby, $select); |
|
| 562 | - |
|
| 563 | - if ($having) { |
|
| 564 | - if (is_array($having)) { |
|
| 565 | - $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 566 | - } |
|
| 567 | - } |
|
| 568 | - $from = spip_pg_from($from, $prefixe); |
|
| 569 | - $query = 'SELECT ' . $select |
|
| 570 | - . (!$from ? '' : "\nFROM $from") |
|
| 571 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 572 | - "\n\tAND ", |
|
| 573 | - array_map('calculer_pg_where', $where) |
|
| 574 | - ))))) |
|
| 575 | - . spip_pg_groupby($groupby, $from, $select) |
|
| 576 | - . (!$having ? '' : "\nHAVING $having") |
|
| 577 | - . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 578 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 579 | - |
|
| 580 | - // renvoyer la requete inerte si demandee |
|
| 581 | - if ($requeter === false) { |
|
| 582 | - return $query; |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - $r = spip_pg_trace_query($query, $serveur); |
|
| 586 | - |
|
| 587 | - return $r ?: $query; |
|
| 541 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 542 | + $prefixe = $connexion['prefixe']; |
|
| 543 | + $link = $connexion['link']; |
|
| 544 | + $db = $connexion['db']; |
|
| 545 | + |
|
| 546 | + $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 547 | + if ($limit) { |
|
| 548 | + $offset = $limatch[2]; |
|
| 549 | + $count = $limatch[3]; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + $select = spip_pg_frommysql($select); |
|
| 553 | + |
|
| 554 | + // si pas de tri explicitement demande, le GROUP BY ne |
|
| 555 | + // contient que la clef primaire. |
|
| 556 | + // lui ajouter alors le champ de tri par defaut |
|
| 557 | + if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 558 | + $groupby[] = $groupbyplus[1]; |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + $orderby = spip_pg_orderby($orderby, $select); |
|
| 562 | + |
|
| 563 | + if ($having) { |
|
| 564 | + if (is_array($having)) { |
|
| 565 | + $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 566 | + } |
|
| 567 | + } |
|
| 568 | + $from = spip_pg_from($from, $prefixe); |
|
| 569 | + $query = 'SELECT ' . $select |
|
| 570 | + . (!$from ? '' : "\nFROM $from") |
|
| 571 | + . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 572 | + "\n\tAND ", |
|
| 573 | + array_map('calculer_pg_where', $where) |
|
| 574 | + ))))) |
|
| 575 | + . spip_pg_groupby($groupby, $from, $select) |
|
| 576 | + . (!$having ? '' : "\nHAVING $having") |
|
| 577 | + . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 578 | + . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 579 | + |
|
| 580 | + // renvoyer la requete inerte si demandee |
|
| 581 | + if ($requeter === false) { |
|
| 582 | + return $query; |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + $r = spip_pg_trace_query($query, $serveur); |
|
| 586 | + |
|
| 587 | + return $r ?: $query; |
|
| 588 | 588 | ; |
| 589 | 589 | } |
| 590 | 590 | |
@@ -593,27 +593,27 @@ discard block |
||
| 593 | 593 | |
| 594 | 594 | // https://code.spip.net/@spip_pg_from |
| 595 | 595 | function spip_pg_from($from, $prefixe) { |
| 596 | - if (is_array($from)) { |
|
| 597 | - $from = spip_pg_select_as($from); |
|
| 598 | - } |
|
| 596 | + if (is_array($from)) { |
|
| 597 | + $from = spip_pg_select_as($from); |
|
| 598 | + } |
|
| 599 | 599 | |
| 600 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 600 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 601 | 601 | } |
| 602 | 602 | |
| 603 | 603 | // https://code.spip.net/@spip_pg_orderby |
| 604 | 604 | function spip_pg_orderby($order, $select) { |
| 605 | - $res = []; |
|
| 606 | - $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 607 | - |
|
| 608 | - foreach ($arg as $v) { |
|
| 609 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 610 | - $res[] = $m[1]; |
|
| 611 | - } else { |
|
| 612 | - $res[] = $v; |
|
| 613 | - } |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - return spip_pg_frommysql(join(',', $res)); |
|
| 605 | + $res = []; |
|
| 606 | + $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 607 | + |
|
| 608 | + foreach ($arg as $v) { |
|
| 609 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 610 | + $res[] = $m[1]; |
|
| 611 | + } else { |
|
| 612 | + $res[] = $v; |
|
| 613 | + } |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + return spip_pg_frommysql(join(',', $res)); |
|
| 617 | 617 | } |
| 618 | 618 | |
| 619 | 619 | // Conversion a l'arrach' des jointures MySQL en jointures PG |
@@ -622,56 +622,56 @@ discard block |
||
| 622 | 622 | |
| 623 | 623 | // https://code.spip.net/@spip_pg_groupby |
| 624 | 624 | function spip_pg_groupby($groupby, $from, $select) { |
| 625 | - $join = strpos($from, ','); |
|
| 626 | - // ismplifier avant de decouper |
|
| 627 | - if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 628 | - $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - if ($join or $groupby) { |
|
| 632 | - $join = is_array($select) ? $select : explode(', ', $select); |
|
| 633 | - } |
|
| 634 | - if ($join) { |
|
| 635 | - // enlever les 0 as points, '', ... |
|
| 636 | - foreach ($join as $k => $v) { |
|
| 637 | - $v = str_replace('DISTINCT ', '', $v); |
|
| 638 | - // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 639 | - $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 640 | - $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 641 | - // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 642 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 643 | - // idem sans AS (fetch numerique) |
|
| 644 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 645 | - // des AS simples : on garde le cote droit du AS |
|
| 646 | - $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 647 | - // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 648 | - if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 649 | - unset($join[$k]); |
|
| 650 | - } else { |
|
| 651 | - $join[$k] = trim($v); |
|
| 652 | - } |
|
| 653 | - } |
|
| 654 | - $join = array_diff($join, ['']); |
|
| 655 | - $join = implode(',', $join); |
|
| 656 | - } |
|
| 657 | - if (is_array($groupby)) { |
|
| 658 | - $groupby = join(',', $groupby); |
|
| 659 | - } |
|
| 660 | - if ($join) { |
|
| 661 | - $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 662 | - } |
|
| 663 | - if (!$groupby) { |
|
| 664 | - return ''; |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - $groupby = spip_pg_frommysql($groupby); |
|
| 668 | - // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 669 | - // issue de prepare_recherche |
|
| 670 | - $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 671 | - $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 672 | - $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 673 | - |
|
| 674 | - return "\nGROUP BY $groupby"; |
|
| 625 | + $join = strpos($from, ','); |
|
| 626 | + // ismplifier avant de decouper |
|
| 627 | + if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 628 | + $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + if ($join or $groupby) { |
|
| 632 | + $join = is_array($select) ? $select : explode(', ', $select); |
|
| 633 | + } |
|
| 634 | + if ($join) { |
|
| 635 | + // enlever les 0 as points, '', ... |
|
| 636 | + foreach ($join as $k => $v) { |
|
| 637 | + $v = str_replace('DISTINCT ', '', $v); |
|
| 638 | + // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 639 | + $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 640 | + $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 641 | + // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 642 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 643 | + // idem sans AS (fetch numerique) |
|
| 644 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 645 | + // des AS simples : on garde le cote droit du AS |
|
| 646 | + $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 647 | + // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 648 | + if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 649 | + unset($join[$k]); |
|
| 650 | + } else { |
|
| 651 | + $join[$k] = trim($v); |
|
| 652 | + } |
|
| 653 | + } |
|
| 654 | + $join = array_diff($join, ['']); |
|
| 655 | + $join = implode(',', $join); |
|
| 656 | + } |
|
| 657 | + if (is_array($groupby)) { |
|
| 658 | + $groupby = join(',', $groupby); |
|
| 659 | + } |
|
| 660 | + if ($join) { |
|
| 661 | + $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 662 | + } |
|
| 663 | + if (!$groupby) { |
|
| 664 | + return ''; |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + $groupby = spip_pg_frommysql($groupby); |
|
| 668 | + // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 669 | + // issue de prepare_recherche |
|
| 670 | + $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 671 | + $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 672 | + $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 673 | + |
|
| 674 | + return "\nGROUP BY $groupby"; |
|
| 675 | 675 | } |
| 676 | 676 | |
| 677 | 677 | // Conversion des operateurs MySQL en PG |
@@ -683,508 +683,508 @@ discard block |
||
| 683 | 683 | |
| 684 | 684 | // https://code.spip.net/@spip_pg_frommysql |
| 685 | 685 | function spip_pg_frommysql($arg) { |
| 686 | - if (is_array($arg)) { |
|
| 687 | - $arg = join(', ', $arg); |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - $res = spip_pg_fromfield($arg); |
|
| 691 | - |
|
| 692 | - $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 693 | - |
|
| 694 | - $res = preg_replace( |
|
| 695 | - '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 696 | - 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 697 | - $res |
|
| 698 | - ); |
|
| 699 | - $res = preg_replace( |
|
| 700 | - '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 701 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 702 | - $res |
|
| 703 | - ); |
|
| 704 | - $res = preg_replace( |
|
| 705 | - '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 706 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 707 | - $res |
|
| 708 | - ); |
|
| 709 | - |
|
| 710 | - $res = preg_replace( |
|
| 711 | - '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 712 | - ' EXTRACT(epoch FROM NOW())', |
|
| 713 | - $res |
|
| 714 | - ); |
|
| 715 | - |
|
| 716 | - // la fonction md5(integer) n'est pas connu en pg |
|
| 717 | - // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 718 | - $res = preg_replace( |
|
| 719 | - '/md5\s*[(]([^)]*)[)]/i', |
|
| 720 | - 'MD5(CAST(\1 AS text))', |
|
| 721 | - $res |
|
| 722 | - ); |
|
| 723 | - |
|
| 724 | - $res = preg_replace( |
|
| 725 | - '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 726 | - ' EXTRACT(epoch FROM \1)', |
|
| 727 | - $res |
|
| 728 | - ); |
|
| 729 | - |
|
| 730 | - $res = preg_replace( |
|
| 731 | - '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 732 | - ' EXTRACT(day FROM \1)', |
|
| 733 | - $res |
|
| 734 | - ); |
|
| 735 | - |
|
| 736 | - $res = preg_replace( |
|
| 737 | - '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 738 | - ' EXTRACT(month FROM \1)', |
|
| 739 | - $res |
|
| 740 | - ); |
|
| 741 | - |
|
| 742 | - $res = preg_replace( |
|
| 743 | - '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 744 | - ' EXTRACT(year FROM \1)', |
|
| 745 | - $res |
|
| 746 | - ); |
|
| 747 | - |
|
| 748 | - $res = preg_replace( |
|
| 749 | - '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 750 | - ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 751 | - $res |
|
| 752 | - ); |
|
| 753 | - |
|
| 754 | - $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 755 | - |
|
| 756 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 757 | - |
|
| 758 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 759 | - |
|
| 760 | - $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 761 | - $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 762 | - $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 763 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 764 | - $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 765 | - |
|
| 766 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 767 | - $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 768 | - |
|
| 769 | - $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 770 | - $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 686 | + if (is_array($arg)) { |
|
| 687 | + $arg = join(', ', $arg); |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + $res = spip_pg_fromfield($arg); |
|
| 691 | + |
|
| 692 | + $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 693 | + |
|
| 694 | + $res = preg_replace( |
|
| 695 | + '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 696 | + 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 697 | + $res |
|
| 698 | + ); |
|
| 699 | + $res = preg_replace( |
|
| 700 | + '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 701 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 702 | + $res |
|
| 703 | + ); |
|
| 704 | + $res = preg_replace( |
|
| 705 | + '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 706 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 707 | + $res |
|
| 708 | + ); |
|
| 709 | + |
|
| 710 | + $res = preg_replace( |
|
| 711 | + '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 712 | + ' EXTRACT(epoch FROM NOW())', |
|
| 713 | + $res |
|
| 714 | + ); |
|
| 715 | + |
|
| 716 | + // la fonction md5(integer) n'est pas connu en pg |
|
| 717 | + // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 718 | + $res = preg_replace( |
|
| 719 | + '/md5\s*[(]([^)]*)[)]/i', |
|
| 720 | + 'MD5(CAST(\1 AS text))', |
|
| 721 | + $res |
|
| 722 | + ); |
|
| 723 | + |
|
| 724 | + $res = preg_replace( |
|
| 725 | + '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 726 | + ' EXTRACT(epoch FROM \1)', |
|
| 727 | + $res |
|
| 728 | + ); |
|
| 729 | + |
|
| 730 | + $res = preg_replace( |
|
| 731 | + '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 732 | + ' EXTRACT(day FROM \1)', |
|
| 733 | + $res |
|
| 734 | + ); |
|
| 735 | + |
|
| 736 | + $res = preg_replace( |
|
| 737 | + '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 738 | + ' EXTRACT(month FROM \1)', |
|
| 739 | + $res |
|
| 740 | + ); |
|
| 741 | + |
|
| 742 | + $res = preg_replace( |
|
| 743 | + '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 744 | + ' EXTRACT(year FROM \1)', |
|
| 745 | + $res |
|
| 746 | + ); |
|
| 747 | + |
|
| 748 | + $res = preg_replace( |
|
| 749 | + '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 750 | + ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 751 | + $res |
|
| 752 | + ); |
|
| 753 | + |
|
| 754 | + $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 755 | + |
|
| 756 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 757 | + |
|
| 758 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 759 | + |
|
| 760 | + $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 761 | + $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 762 | + $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 763 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 764 | + $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 765 | + |
|
| 766 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 767 | + $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 768 | + |
|
| 769 | + $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 770 | + $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 771 | 771 | # correct en theorie mais produit des debordements arithmetiques |
| 772 | 772 | # $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)(timestamp *'[^']*' *[+-] *timestamp *'[^']*') *[)]/", '\2', $res); |
| 773 | - $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 774 | - $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 773 | + $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 774 | + $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 775 | 775 | |
| 776 | - return str_replace('REGEXP', '~', $res); |
|
| 776 | + return str_replace('REGEXP', '~', $res); |
|
| 777 | 777 | } |
| 778 | 778 | |
| 779 | 779 | // https://code.spip.net/@spip_pg_fromfield |
| 780 | 780 | function spip_pg_fromfield($arg) { |
| 781 | - while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 782 | - preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 783 | - $res = ''; |
|
| 784 | - $n = 0; |
|
| 785 | - $index = $m[2]; |
|
| 786 | - foreach ($r[1] as $v) { |
|
| 787 | - $n++; |
|
| 788 | - $res .= "\nwhen $index=$v then $n"; |
|
| 789 | - } |
|
| 790 | - $arg = $m[1] . "case $res else 0 end " |
|
| 791 | - . substr($arg, strlen($m[0])); |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - return $arg; |
|
| 781 | + while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 782 | + preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 783 | + $res = ''; |
|
| 784 | + $n = 0; |
|
| 785 | + $index = $m[2]; |
|
| 786 | + foreach ($r[1] as $v) { |
|
| 787 | + $n++; |
|
| 788 | + $res .= "\nwhen $index=$v then $n"; |
|
| 789 | + } |
|
| 790 | + $arg = $m[1] . "case $res else 0 end " |
|
| 791 | + . substr($arg, strlen($m[0])); |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + return $arg; |
|
| 795 | 795 | } |
| 796 | 796 | |
| 797 | 797 | // https://code.spip.net/@calculer_pg_where |
| 798 | 798 | function calculer_pg_where($v) { |
| 799 | - if (!is_array($v)) { |
|
| 800 | - return spip_pg_frommysql($v); |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 804 | - if (!($n = count($v))) { |
|
| 805 | - return $op; |
|
| 806 | - } else { |
|
| 807 | - $arg = calculer_pg_where(array_shift($v)); |
|
| 808 | - if ($n == 1) { |
|
| 809 | - return "$op($arg)"; |
|
| 810 | - } else { |
|
| 811 | - $arg2 = calculer_pg_where(array_shift($v)); |
|
| 812 | - if ($n == 2) { |
|
| 813 | - return "($arg $op $arg2)"; |
|
| 814 | - } else { |
|
| 815 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 816 | - } |
|
| 817 | - } |
|
| 818 | - } |
|
| 799 | + if (!is_array($v)) { |
|
| 800 | + return spip_pg_frommysql($v); |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 804 | + if (!($n = count($v))) { |
|
| 805 | + return $op; |
|
| 806 | + } else { |
|
| 807 | + $arg = calculer_pg_where(array_shift($v)); |
|
| 808 | + if ($n == 1) { |
|
| 809 | + return "$op($arg)"; |
|
| 810 | + } else { |
|
| 811 | + $arg2 = calculer_pg_where(array_shift($v)); |
|
| 812 | + if ($n == 2) { |
|
| 813 | + return "($arg $op $arg2)"; |
|
| 814 | + } else { |
|
| 815 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 816 | + } |
|
| 817 | + } |
|
| 818 | + } |
|
| 819 | 819 | } |
| 820 | 820 | |
| 821 | 821 | |
| 822 | 822 | // https://code.spip.net/@calculer_pg_expression |
| 823 | 823 | function calculer_pg_expression($expression, $v, $join = 'AND') { |
| 824 | - if (empty($v)) { |
|
| 825 | - return ''; |
|
| 826 | - } |
|
| 824 | + if (empty($v)) { |
|
| 825 | + return ''; |
|
| 826 | + } |
|
| 827 | 827 | |
| 828 | - $exp = "\n$expression "; |
|
| 828 | + $exp = "\n$expression "; |
|
| 829 | 829 | |
| 830 | - if (!is_array($v)) { |
|
| 831 | - $v = [$v]; |
|
| 832 | - } |
|
| 830 | + if (!is_array($v)) { |
|
| 831 | + $v = [$v]; |
|
| 832 | + } |
|
| 833 | 833 | |
| 834 | - if (strtoupper($join) === 'AND') { |
|
| 835 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 836 | - } else { |
|
| 837 | - return $exp . join($join, $v); |
|
| 838 | - } |
|
| 834 | + if (strtoupper($join) === 'AND') { |
|
| 835 | + return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 836 | + } else { |
|
| 837 | + return $exp . join($join, $v); |
|
| 838 | + } |
|
| 839 | 839 | } |
| 840 | 840 | |
| 841 | 841 | // https://code.spip.net/@spip_pg_select_as |
| 842 | 842 | function spip_pg_select_as($args) { |
| 843 | - $argsas = ''; |
|
| 844 | - foreach ($args as $k => $v) { |
|
| 845 | - if (substr($k, -1) == '@') { |
|
| 846 | - // c'est une jointure qui se refere au from precedent |
|
| 847 | - // pas de virgule |
|
| 848 | - $argsas .= ' ' . $v; |
|
| 849 | - } else { |
|
| 850 | - $as = ''; |
|
| 851 | - // spip_log("$k : $v", _LOG_DEBUG); |
|
| 852 | - if (!is_numeric($k)) { |
|
| 853 | - if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 854 | - $v = $k; |
|
| 855 | - } elseif ($v != $k) { |
|
| 856 | - $p = strpos($v, ' '); |
|
| 857 | - if ($p) { |
|
| 858 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 859 | - } else { |
|
| 860 | - $as = " AS $k"; |
|
| 861 | - } |
|
| 862 | - } |
|
| 863 | - } |
|
| 864 | - // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 865 | - // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 866 | - $argsas .= ', ' . $v . $as; |
|
| 867 | - } |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - return substr($argsas, 2); |
|
| 843 | + $argsas = ''; |
|
| 844 | + foreach ($args as $k => $v) { |
|
| 845 | + if (substr($k, -1) == '@') { |
|
| 846 | + // c'est une jointure qui se refere au from precedent |
|
| 847 | + // pas de virgule |
|
| 848 | + $argsas .= ' ' . $v; |
|
| 849 | + } else { |
|
| 850 | + $as = ''; |
|
| 851 | + // spip_log("$k : $v", _LOG_DEBUG); |
|
| 852 | + if (!is_numeric($k)) { |
|
| 853 | + if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 854 | + $v = $k; |
|
| 855 | + } elseif ($v != $k) { |
|
| 856 | + $p = strpos($v, ' '); |
|
| 857 | + if ($p) { |
|
| 858 | + $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 859 | + } else { |
|
| 860 | + $as = " AS $k"; |
|
| 861 | + } |
|
| 862 | + } |
|
| 863 | + } |
|
| 864 | + // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 865 | + // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 866 | + $argsas .= ', ' . $v . $as; |
|
| 867 | + } |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + return substr($argsas, 2); |
|
| 871 | 871 | } |
| 872 | 872 | |
| 873 | 873 | // https://code.spip.net/@spip_pg_fetch |
| 874 | 874 | function spip_pg_fetch($res, $t = '', $serveur = '', $requeter = true) { |
| 875 | 875 | |
| 876 | - if ($res) { |
|
| 877 | - $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 878 | - } |
|
| 876 | + if ($res) { |
|
| 877 | + $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 878 | + } |
|
| 879 | 879 | |
| 880 | - return $res; |
|
| 880 | + return $res; |
|
| 881 | 881 | } |
| 882 | 882 | |
| 883 | 883 | function spip_pg_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 884 | - if ($r) { |
|
| 885 | - return pg_result_seek($r, $row_number); |
|
| 886 | - } |
|
| 884 | + if ($r) { |
|
| 885 | + return pg_result_seek($r, $row_number); |
|
| 886 | + } |
|
| 887 | 887 | } |
| 888 | 888 | |
| 889 | 889 | |
| 890 | 890 | // https://code.spip.net/@spip_pg_countsel |
| 891 | 891 | function spip_pg_countsel( |
| 892 | - $from = [], |
|
| 893 | - $where = [], |
|
| 894 | - $groupby = [], |
|
| 895 | - $having = [], |
|
| 896 | - $serveur = '', |
|
| 897 | - $requeter = true |
|
| 892 | + $from = [], |
|
| 893 | + $where = [], |
|
| 894 | + $groupby = [], |
|
| 895 | + $having = [], |
|
| 896 | + $serveur = '', |
|
| 897 | + $requeter = true |
|
| 898 | 898 | ) { |
| 899 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 900 | - $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 901 | - if (!$requeter) { |
|
| 902 | - return $r; |
|
| 903 | - } |
|
| 904 | - if (!is_resource($r)) { |
|
| 905 | - return 0; |
|
| 906 | - } |
|
| 907 | - [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 908 | - |
|
| 909 | - return $c; |
|
| 899 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 900 | + $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 901 | + if (!$requeter) { |
|
| 902 | + return $r; |
|
| 903 | + } |
|
| 904 | + if (!is_resource($r)) { |
|
| 905 | + return 0; |
|
| 906 | + } |
|
| 907 | + [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 908 | + |
|
| 909 | + return $c; |
|
| 910 | 910 | } |
| 911 | 911 | |
| 912 | 912 | // https://code.spip.net/@spip_pg_count |
| 913 | 913 | function spip_pg_count($res, $serveur = '', $requeter = true) { |
| 914 | - return !$res ? 0 : pg_numrows($res); |
|
| 914 | + return !$res ? 0 : pg_numrows($res); |
|
| 915 | 915 | } |
| 916 | 916 | |
| 917 | 917 | // https://code.spip.net/@spip_pg_free |
| 918 | 918 | function spip_pg_free($res, $serveur = '', $requeter = true) { |
| 919 | - // rien a faire en postgres |
|
| 919 | + // rien a faire en postgres |
|
| 920 | 920 | } |
| 921 | 921 | |
| 922 | 922 | // https://code.spip.net/@spip_pg_delete |
| 923 | 923 | function spip_pg_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 924 | 924 | |
| 925 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 926 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 925 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 926 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 927 | 927 | |
| 928 | - $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 929 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 928 | + $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 929 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 930 | 930 | |
| 931 | - // renvoyer la requete inerte si demandee |
|
| 932 | - if (!$requeter) { |
|
| 933 | - return $query; |
|
| 934 | - } |
|
| 931 | + // renvoyer la requete inerte si demandee |
|
| 932 | + if (!$requeter) { |
|
| 933 | + return $query; |
|
| 934 | + } |
|
| 935 | 935 | |
| 936 | - $res = spip_pg_trace_query($query, $serveur); |
|
| 937 | - if ($res) { |
|
| 938 | - return pg_affected_rows($res); |
|
| 939 | - } else { |
|
| 940 | - return false; |
|
| 941 | - } |
|
| 936 | + $res = spip_pg_trace_query($query, $serveur); |
|
| 937 | + if ($res) { |
|
| 938 | + return pg_affected_rows($res); |
|
| 939 | + } else { |
|
| 940 | + return false; |
|
| 941 | + } |
|
| 942 | 942 | } |
| 943 | 943 | |
| 944 | 944 | // https://code.spip.net/@spip_pg_insert |
| 945 | 945 | function spip_pg_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 946 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 947 | - $prefixe = $connexion['prefixe']; |
|
| 948 | - $link = $connexion['link']; |
|
| 949 | - |
|
| 950 | - if (!$desc) { |
|
| 951 | - $desc = description_table($table, $serveur); |
|
| 952 | - } |
|
| 953 | - $seq = spip_pg_sequence($table, true); |
|
| 954 | - // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 955 | - if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 956 | - $seq = spip_pg_sequence($table); |
|
| 957 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 958 | - $seq = "currval('$seq')"; |
|
| 959 | - } |
|
| 960 | - |
|
| 961 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 962 | - $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 963 | - $ins = (strlen($champs) < 3) |
|
| 964 | - ? ' DEFAULT VALUES' |
|
| 965 | - : "$champs VALUES $valeurs"; |
|
| 966 | - $q = "INSERT INTO $table $ins $ret"; |
|
| 967 | - if (!$requeter) { |
|
| 968 | - return $q; |
|
| 969 | - } |
|
| 970 | - $connexion['last'] = $q; |
|
| 971 | - $r = spip_pg_query_simple($link, $q); |
|
| 946 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 947 | + $prefixe = $connexion['prefixe']; |
|
| 948 | + $link = $connexion['link']; |
|
| 949 | + |
|
| 950 | + if (!$desc) { |
|
| 951 | + $desc = description_table($table, $serveur); |
|
| 952 | + } |
|
| 953 | + $seq = spip_pg_sequence($table, true); |
|
| 954 | + // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 955 | + if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 956 | + $seq = spip_pg_sequence($table); |
|
| 957 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 958 | + $seq = "currval('$seq')"; |
|
| 959 | + } |
|
| 960 | + |
|
| 961 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 962 | + $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 963 | + $ins = (strlen($champs) < 3) |
|
| 964 | + ? ' DEFAULT VALUES' |
|
| 965 | + : "$champs VALUES $valeurs"; |
|
| 966 | + $q = "INSERT INTO $table $ins $ret"; |
|
| 967 | + if (!$requeter) { |
|
| 968 | + return $q; |
|
| 969 | + } |
|
| 970 | + $connexion['last'] = $q; |
|
| 971 | + $r = spip_pg_query_simple($link, $q); |
|
| 972 | 972 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 973 | - if ($r) { |
|
| 974 | - if (!$ret) { |
|
| 975 | - return 0; |
|
| 976 | - } |
|
| 977 | - if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 978 | - return $r2[0]; |
|
| 979 | - } |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - return false; |
|
| 973 | + if ($r) { |
|
| 974 | + if (!$ret) { |
|
| 975 | + return 0; |
|
| 976 | + } |
|
| 977 | + if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 978 | + return $r2[0]; |
|
| 979 | + } |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + return false; |
|
| 983 | 983 | } |
| 984 | 984 | |
| 985 | 985 | // https://code.spip.net/@spip_pg_insertq |
| 986 | 986 | function spip_pg_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 987 | 987 | |
| 988 | - if (!$desc) { |
|
| 989 | - $desc = description_table($table, $serveur); |
|
| 990 | - } |
|
| 991 | - if (!$desc) { |
|
| 992 | - die("$table insertion sans description"); |
|
| 993 | - } |
|
| 994 | - $fields = $desc['field']; |
|
| 995 | - |
|
| 996 | - foreach ($couples as $champ => $val) { |
|
| 997 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1001 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1002 | - |
|
| 1003 | - return spip_pg_insert( |
|
| 1004 | - $table, |
|
| 1005 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 1006 | - '(' . join(',', $couples) . ')', |
|
| 1007 | - $desc, |
|
| 1008 | - $serveur, |
|
| 1009 | - $requeter |
|
| 1010 | - ); |
|
| 988 | + if (!$desc) { |
|
| 989 | + $desc = description_table($table, $serveur); |
|
| 990 | + } |
|
| 991 | + if (!$desc) { |
|
| 992 | + die("$table insertion sans description"); |
|
| 993 | + } |
|
| 994 | + $fields = $desc['field']; |
|
| 995 | + |
|
| 996 | + foreach ($couples as $champ => $val) { |
|
| 997 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1001 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1002 | + |
|
| 1003 | + return spip_pg_insert( |
|
| 1004 | + $table, |
|
| 1005 | + '(' . join(',', array_keys($couples)) . ')', |
|
| 1006 | + '(' . join(',', $couples) . ')', |
|
| 1007 | + $desc, |
|
| 1008 | + $serveur, |
|
| 1009 | + $requeter |
|
| 1010 | + ); |
|
| 1011 | 1011 | } |
| 1012 | 1012 | |
| 1013 | 1013 | |
| 1014 | 1014 | // https://code.spip.net/@spip_pg_insertq_multi |
| 1015 | 1015 | function spip_pg_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1016 | 1016 | |
| 1017 | - if (!$desc) { |
|
| 1018 | - $desc = description_table($table, $serveur); |
|
| 1019 | - } |
|
| 1020 | - if (!$desc) { |
|
| 1021 | - die("$table insertion sans description"); |
|
| 1022 | - } |
|
| 1023 | - $fields = $desc['field'] ?? []; |
|
| 1024 | - |
|
| 1025 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1026 | - // une premiere fois pour ajouter maj dans les cles |
|
| 1027 | - $c = $tab_couples[0] ?? []; |
|
| 1028 | - $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1029 | - |
|
| 1030 | - $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1031 | - $valeurs = []; |
|
| 1032 | - foreach ($tab_couples as $couples) { |
|
| 1033 | - foreach ($couples as $champ => $val) { |
|
| 1034 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1035 | - } |
|
| 1036 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1037 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1038 | - |
|
| 1039 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1040 | - } |
|
| 1041 | - $valeurs = implode(', ', $valeurs); |
|
| 1042 | - |
|
| 1043 | - return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1017 | + if (!$desc) { |
|
| 1018 | + $desc = description_table($table, $serveur); |
|
| 1019 | + } |
|
| 1020 | + if (!$desc) { |
|
| 1021 | + die("$table insertion sans description"); |
|
| 1022 | + } |
|
| 1023 | + $fields = $desc['field'] ?? []; |
|
| 1024 | + |
|
| 1025 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1026 | + // une premiere fois pour ajouter maj dans les cles |
|
| 1027 | + $c = $tab_couples[0] ?? []; |
|
| 1028 | + $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1029 | + |
|
| 1030 | + $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1031 | + $valeurs = []; |
|
| 1032 | + foreach ($tab_couples as $couples) { |
|
| 1033 | + foreach ($couples as $champ => $val) { |
|
| 1034 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1035 | + } |
|
| 1036 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1037 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1038 | + |
|
| 1039 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1040 | + } |
|
| 1041 | + $valeurs = implode(', ', $valeurs); |
|
| 1042 | + |
|
| 1043 | + return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1044 | 1044 | } |
| 1045 | 1045 | |
| 1046 | 1046 | |
| 1047 | 1047 | // https://code.spip.net/@spip_pg_update |
| 1048 | 1048 | function spip_pg_update($table, $couples, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1049 | 1049 | |
| 1050 | - if (!$couples) { |
|
| 1051 | - return; |
|
| 1052 | - } |
|
| 1053 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1054 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1050 | + if (!$couples) { |
|
| 1051 | + return; |
|
| 1052 | + } |
|
| 1053 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1054 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1055 | 1055 | |
| 1056 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1057 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1056 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1057 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1058 | 1058 | |
| 1059 | - $set = []; |
|
| 1060 | - foreach ($couples as $champ => $val) { |
|
| 1061 | - $set[] = $champ . '=' . $val; |
|
| 1062 | - } |
|
| 1059 | + $set = []; |
|
| 1060 | + foreach ($couples as $champ => $val) { |
|
| 1061 | + $set[] = $champ . '=' . $val; |
|
| 1062 | + } |
|
| 1063 | 1063 | |
| 1064 | - $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1065 | - . calculer_pg_expression('SET', $set, ',') |
|
| 1066 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1064 | + $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1065 | + . calculer_pg_expression('SET', $set, ',') |
|
| 1066 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1067 | 1067 | |
| 1068 | - // renvoyer la requete inerte si demandee |
|
| 1069 | - if (!$requeter) { |
|
| 1070 | - return $query; |
|
| 1071 | - } |
|
| 1068 | + // renvoyer la requete inerte si demandee |
|
| 1069 | + if (!$requeter) { |
|
| 1070 | + return $query; |
|
| 1071 | + } |
|
| 1072 | 1072 | |
| 1073 | - return spip_pg_trace_query($query, $serveur); |
|
| 1073 | + return spip_pg_trace_query($query, $serveur); |
|
| 1074 | 1074 | } |
| 1075 | 1075 | |
| 1076 | 1076 | // idem, mais les valeurs sont des constantes a mettre entre apostrophes |
| 1077 | 1077 | // sauf les expressions de date lorsqu'il s'agit de fonctions SQL (NOW etc) |
| 1078 | 1078 | // https://code.spip.net/@spip_pg_updateq |
| 1079 | 1079 | function spip_pg_updateq($table, $couples, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1080 | - if (!$couples) { |
|
| 1081 | - return; |
|
| 1082 | - } |
|
| 1083 | - if (!$desc) { |
|
| 1084 | - $desc = description_table($table, $serveur); |
|
| 1085 | - } |
|
| 1086 | - $fields = $desc['field']; |
|
| 1087 | - foreach ($couples as $k => $val) { |
|
| 1088 | - $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1080 | + if (!$couples) { |
|
| 1081 | + return; |
|
| 1082 | + } |
|
| 1083 | + if (!$desc) { |
|
| 1084 | + $desc = description_table($table, $serveur); |
|
| 1085 | + } |
|
| 1086 | + $fields = $desc['field']; |
|
| 1087 | + foreach ($couples as $k => $val) { |
|
| 1088 | + $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1092 | 1092 | } |
| 1093 | 1093 | |
| 1094 | 1094 | |
| 1095 | 1095 | // https://code.spip.net/@spip_pg_replace |
| 1096 | 1096 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1097 | - if (!$values) { |
|
| 1098 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1099 | - |
|
| 1100 | - return 0; |
|
| 1101 | - } |
|
| 1102 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1103 | - $prefixe = $connexion['prefixe']; |
|
| 1104 | - $link = $connexion['link']; |
|
| 1105 | - |
|
| 1106 | - if (!$desc) { |
|
| 1107 | - $desc = description_table($table, $serveur); |
|
| 1108 | - } |
|
| 1109 | - if (!$desc) { |
|
| 1110 | - die("$table insertion sans description"); |
|
| 1111 | - } |
|
| 1112 | - $prim = $desc['key']['PRIMARY KEY']; |
|
| 1113 | - $ids = preg_split('/,\s*/', $prim); |
|
| 1114 | - $noprims = $prims = []; |
|
| 1115 | - foreach ($values as $k => $v) { |
|
| 1116 | - $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1117 | - |
|
| 1118 | - if (!in_array($k, $ids)) { |
|
| 1119 | - $noprims[$k] = "$k=$v"; |
|
| 1120 | - } else { |
|
| 1121 | - $prims[$k] = "$k=$v"; |
|
| 1122 | - } |
|
| 1123 | - } |
|
| 1124 | - |
|
| 1125 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1126 | - $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1127 | - |
|
| 1128 | - $where = join(' AND ', $prims); |
|
| 1129 | - if (!$where) { |
|
| 1130 | - return spip_pg_insert( |
|
| 1131 | - $table, |
|
| 1132 | - '(' . join(',', array_keys($values)) . ')', |
|
| 1133 | - '(' . join(',', $values) . ')', |
|
| 1134 | - $desc, |
|
| 1135 | - $serveur |
|
| 1136 | - ); |
|
| 1137 | - } |
|
| 1138 | - $couples = join(',', $noprims); |
|
| 1139 | - |
|
| 1140 | - $seq = spip_pg_sequence($table); |
|
| 1141 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 1142 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1143 | - |
|
| 1144 | - $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1145 | - if ($couples) { |
|
| 1146 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1097 | + if (!$values) { |
|
| 1098 | + spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1099 | + |
|
| 1100 | + return 0; |
|
| 1101 | + } |
|
| 1102 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1103 | + $prefixe = $connexion['prefixe']; |
|
| 1104 | + $link = $connexion['link']; |
|
| 1105 | + |
|
| 1106 | + if (!$desc) { |
|
| 1107 | + $desc = description_table($table, $serveur); |
|
| 1108 | + } |
|
| 1109 | + if (!$desc) { |
|
| 1110 | + die("$table insertion sans description"); |
|
| 1111 | + } |
|
| 1112 | + $prim = $desc['key']['PRIMARY KEY']; |
|
| 1113 | + $ids = preg_split('/,\s*/', $prim); |
|
| 1114 | + $noprims = $prims = []; |
|
| 1115 | + foreach ($values as $k => $v) { |
|
| 1116 | + $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1117 | + |
|
| 1118 | + if (!in_array($k, $ids)) { |
|
| 1119 | + $noprims[$k] = "$k=$v"; |
|
| 1120 | + } else { |
|
| 1121 | + $prims[$k] = "$k=$v"; |
|
| 1122 | + } |
|
| 1123 | + } |
|
| 1124 | + |
|
| 1125 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1126 | + $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1127 | + |
|
| 1128 | + $where = join(' AND ', $prims); |
|
| 1129 | + if (!$where) { |
|
| 1130 | + return spip_pg_insert( |
|
| 1131 | + $table, |
|
| 1132 | + '(' . join(',', array_keys($values)) . ')', |
|
| 1133 | + '(' . join(',', $values) . ')', |
|
| 1134 | + $desc, |
|
| 1135 | + $serveur |
|
| 1136 | + ); |
|
| 1137 | + } |
|
| 1138 | + $couples = join(',', $noprims); |
|
| 1139 | + |
|
| 1140 | + $seq = spip_pg_sequence($table); |
|
| 1141 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 1142 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1143 | + |
|
| 1144 | + $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1145 | + if ($couples) { |
|
| 1146 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1147 | 1147 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 1148 | - if (!$couples) { |
|
| 1149 | - return false; |
|
| 1150 | - } |
|
| 1151 | - $couples = pg_affected_rows($couples); |
|
| 1152 | - } |
|
| 1153 | - if (!$couples) { |
|
| 1154 | - $ret = !$seq ? '' : |
|
| 1155 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1156 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1157 | - ',', |
|
| 1158 | - $values |
|
| 1159 | - ) . ")$ret"; |
|
| 1160 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1161 | - if (!$couples) { |
|
| 1162 | - return false; |
|
| 1163 | - } elseif ($ret) { |
|
| 1164 | - $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1165 | - if ($r[0]) { |
|
| 1166 | - $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1167 | - // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1168 | - // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1169 | - $r = @pg_query($link, $q); |
|
| 1170 | - } |
|
| 1171 | - } |
|
| 1172 | - } |
|
| 1173 | - |
|
| 1174 | - return $couples; |
|
| 1148 | + if (!$couples) { |
|
| 1149 | + return false; |
|
| 1150 | + } |
|
| 1151 | + $couples = pg_affected_rows($couples); |
|
| 1152 | + } |
|
| 1153 | + if (!$couples) { |
|
| 1154 | + $ret = !$seq ? '' : |
|
| 1155 | + (" RETURNING nextval('$seq') < $prim"); |
|
| 1156 | + $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1157 | + ',', |
|
| 1158 | + $values |
|
| 1159 | + ) . ")$ret"; |
|
| 1160 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1161 | + if (!$couples) { |
|
| 1162 | + return false; |
|
| 1163 | + } elseif ($ret) { |
|
| 1164 | + $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1165 | + if ($r[0]) { |
|
| 1166 | + $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1167 | + // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1168 | + // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1169 | + $r = @pg_query($link, $q); |
|
| 1170 | + } |
|
| 1171 | + } |
|
| 1172 | + } |
|
| 1173 | + |
|
| 1174 | + return $couples; |
|
| 1175 | 1175 | } |
| 1176 | 1176 | |
| 1177 | 1177 | |
| 1178 | 1178 | // https://code.spip.net/@spip_pg_replace_multi |
| 1179 | 1179 | function spip_pg_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1180 | - $retour = null; |
|
| 1181 | - // boucler pour traiter chaque requete independemment |
|
| 1182 | - foreach ($tab_couples as $couples) { |
|
| 1183 | - $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - // renvoie le dernier id |
|
| 1187 | - return $retour; |
|
| 1180 | + $retour = null; |
|
| 1181 | + // boucler pour traiter chaque requete independemment |
|
| 1182 | + foreach ($tab_couples as $couples) { |
|
| 1183 | + $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + // renvoie le dernier id |
|
| 1187 | + return $retour; |
|
| 1188 | 1188 | } |
| 1189 | 1189 | |
| 1190 | 1190 | |
@@ -1194,20 +1194,20 @@ discard block |
||
| 1194 | 1194 | // https://code.spip.net/@spip_pg_sequence |
| 1195 | 1195 | function spip_pg_sequence($table, $raw = false) { |
| 1196 | 1196 | |
| 1197 | - include_spip('base/serial'); |
|
| 1198 | - if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1199 | - return false; |
|
| 1200 | - } |
|
| 1201 | - $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1202 | - $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1203 | - if ( |
|
| 1204 | - !preg_match('/^\w+$/', $prim) |
|
| 1205 | - or strpos($desc['field'][$prim], 'int') === false |
|
| 1206 | - ) { |
|
| 1207 | - return ''; |
|
| 1208 | - } else { |
|
| 1209 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1210 | - } |
|
| 1197 | + include_spip('base/serial'); |
|
| 1198 | + if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1199 | + return false; |
|
| 1200 | + } |
|
| 1201 | + $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1202 | + $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1203 | + if ( |
|
| 1204 | + !preg_match('/^\w+$/', $prim) |
|
| 1205 | + or strpos($desc['field'][$prim], 'int') === false |
|
| 1206 | + ) { |
|
| 1207 | + return ''; |
|
| 1208 | + } else { |
|
| 1209 | + return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1210 | + } |
|
| 1211 | 1211 | } |
| 1212 | 1212 | |
| 1213 | 1213 | // Explicite les conversions de Mysql d'une valeur $v de type $t |
@@ -1215,65 +1215,65 @@ discard block |
||
| 1215 | 1215 | |
| 1216 | 1216 | // https://code.spip.net/@spip_pg_cite |
| 1217 | 1217 | function spip_pg_cite($v, $t) { |
| 1218 | - if (is_null($v)) { |
|
| 1219 | - return 'NULL'; |
|
| 1220 | - } // null php se traduit en NULL SQL |
|
| 1221 | - |
|
| 1222 | - if (sql_test_date($t)) { |
|
| 1223 | - if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1224 | - return spip_pg_frommysql($v); |
|
| 1225 | - } else { |
|
| 1226 | - if (strncmp($v, '0000', 4) == 0) { |
|
| 1227 | - $v = '0001' . substr($v, 4); |
|
| 1228 | - } |
|
| 1229 | - if (strpos($v, '-00-00') === 4) { |
|
| 1230 | - $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1231 | - } |
|
| 1232 | - |
|
| 1233 | - return "timestamp '$v'"; |
|
| 1234 | - } |
|
| 1235 | - } elseif (!sql_test_int($t)) { |
|
| 1236 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1237 | - } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1238 | - return $v; |
|
| 1239 | - } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1240 | - return substr($v, 1); |
|
| 1241 | - } else { |
|
| 1242 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1243 | - |
|
| 1244 | - return intval($v); |
|
| 1245 | - } |
|
| 1218 | + if (is_null($v)) { |
|
| 1219 | + return 'NULL'; |
|
| 1220 | + } // null php se traduit en NULL SQL |
|
| 1221 | + |
|
| 1222 | + if (sql_test_date($t)) { |
|
| 1223 | + if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1224 | + return spip_pg_frommysql($v); |
|
| 1225 | + } else { |
|
| 1226 | + if (strncmp($v, '0000', 4) == 0) { |
|
| 1227 | + $v = '0001' . substr($v, 4); |
|
| 1228 | + } |
|
| 1229 | + if (strpos($v, '-00-00') === 4) { |
|
| 1230 | + $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1231 | + } |
|
| 1232 | + |
|
| 1233 | + return "timestamp '$v'"; |
|
| 1234 | + } |
|
| 1235 | + } elseif (!sql_test_int($t)) { |
|
| 1236 | + return ("'" . pg_escape_string($v) . "'"); |
|
| 1237 | + } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1238 | + return $v; |
|
| 1239 | + } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1240 | + return substr($v, 1); |
|
| 1241 | + } else { |
|
| 1242 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1243 | + |
|
| 1244 | + return intval($v); |
|
| 1245 | + } |
|
| 1246 | 1246 | } |
| 1247 | 1247 | |
| 1248 | 1248 | // https://code.spip.net/@spip_pg_hex |
| 1249 | 1249 | function spip_pg_hex($v) { |
| 1250 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1250 | + return "CAST(x'" . $v . "' as bigint)"; |
|
| 1251 | 1251 | } |
| 1252 | 1252 | |
| 1253 | 1253 | function spip_pg_quote($v, $type = '') { |
| 1254 | - if (!is_array($v)) { |
|
| 1255 | - return spip_pg_cite($v, $type); |
|
| 1256 | - } |
|
| 1257 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1258 | - foreach ($v as $k => $r) { |
|
| 1259 | - $v[$k] = spip_pg_quote($r, $type); |
|
| 1260 | - } |
|
| 1261 | - |
|
| 1262 | - return join(',', $v); |
|
| 1254 | + if (!is_array($v)) { |
|
| 1255 | + return spip_pg_cite($v, $type); |
|
| 1256 | + } |
|
| 1257 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1258 | + foreach ($v as $k => $r) { |
|
| 1259 | + $v[$k] = spip_pg_quote($r, $type); |
|
| 1260 | + } |
|
| 1261 | + |
|
| 1262 | + return join(',', $v); |
|
| 1263 | 1263 | } |
| 1264 | 1264 | |
| 1265 | 1265 | function spip_pg_date_proche($champ, $interval, $unite) { |
| 1266 | - return '(' |
|
| 1267 | - . $champ |
|
| 1268 | - . (($interval <= 0) ? '>' : '<') |
|
| 1269 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1270 | - . '(' |
|
| 1271 | - . sql_quote(date('Y-m-d H:i:s')) |
|
| 1272 | - . ', INTERVAL ' |
|
| 1273 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1274 | - . ' ' |
|
| 1275 | - . $unite |
|
| 1276 | - . '))'; |
|
| 1266 | + return '(' |
|
| 1267 | + . $champ |
|
| 1268 | + . (($interval <= 0) ? '>' : '<') |
|
| 1269 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1270 | + . '(' |
|
| 1271 | + . sql_quote(date('Y-m-d H:i:s')) |
|
| 1272 | + . ', INTERVAL ' |
|
| 1273 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1274 | + . ' ' |
|
| 1275 | + . $unite |
|
| 1276 | + . '))'; |
|
| 1277 | 1277 | } |
| 1278 | 1278 | |
| 1279 | 1279 | // https://code.spip.net/@spip_pg_in |
@@ -1281,69 +1281,69 @@ discard block |
||
| 1281 | 1281 | // |
| 1282 | 1282 | // IN (...) souvent limite a 255 elements, d'ou cette fonction assistante |
| 1283 | 1283 | // |
| 1284 | - // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1285 | - if (!$valeurs) { |
|
| 1286 | - return $not ? '0=0' : '0=1'; |
|
| 1287 | - } |
|
| 1288 | - if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1289 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1290 | - } |
|
| 1291 | - $n = $i = 0; |
|
| 1292 | - $in_sql = ''; |
|
| 1293 | - while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1294 | - if ((++$i) >= 255) { |
|
| 1295 | - $in_sql .= "($val $not IN (" . |
|
| 1296 | - substr($valeurs, 0, $n) . |
|
| 1297 | - "))\n" . |
|
| 1298 | - ($not ? "AND\t" : "OR\t"); |
|
| 1299 | - $valeurs = substr($valeurs, $n + 1); |
|
| 1300 | - $i = $n = 0; |
|
| 1301 | - } |
|
| 1302 | - } |
|
| 1303 | - $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1304 | - |
|
| 1305 | - return "($in_sql)"; |
|
| 1284 | + // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1285 | + if (!$valeurs) { |
|
| 1286 | + return $not ? '0=0' : '0=1'; |
|
| 1287 | + } |
|
| 1288 | + if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1289 | + return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1290 | + } |
|
| 1291 | + $n = $i = 0; |
|
| 1292 | + $in_sql = ''; |
|
| 1293 | + while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1294 | + if ((++$i) >= 255) { |
|
| 1295 | + $in_sql .= "($val $not IN (" . |
|
| 1296 | + substr($valeurs, 0, $n) . |
|
| 1297 | + "))\n" . |
|
| 1298 | + ($not ? "AND\t" : "OR\t"); |
|
| 1299 | + $valeurs = substr($valeurs, $n + 1); |
|
| 1300 | + $i = $n = 0; |
|
| 1301 | + } |
|
| 1302 | + } |
|
| 1303 | + $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1304 | + |
|
| 1305 | + return "($in_sql)"; |
|
| 1306 | 1306 | } |
| 1307 | 1307 | |
| 1308 | 1308 | // https://code.spip.net/@spip_pg_error |
| 1309 | 1309 | function spip_pg_error($query = '', $serveur = '', $requeter = true) { |
| 1310 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1311 | - $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1312 | - if ($s) { |
|
| 1313 | - $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1314 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1315 | - } |
|
| 1316 | - |
|
| 1317 | - return $s; |
|
| 1310 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1311 | + $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1312 | + if ($s) { |
|
| 1313 | + $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1314 | + spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1315 | + } |
|
| 1316 | + |
|
| 1317 | + return $s; |
|
| 1318 | 1318 | } |
| 1319 | 1319 | |
| 1320 | 1320 | // https://code.spip.net/@spip_pg_errno |
| 1321 | 1321 | function spip_pg_errno($serveur = '') { |
| 1322 | - // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1323 | - // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1324 | - return 0; |
|
| 1322 | + // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1323 | + // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1324 | + return 0; |
|
| 1325 | 1325 | } |
| 1326 | 1326 | |
| 1327 | 1327 | // https://code.spip.net/@spip_pg_drop_table |
| 1328 | 1328 | function spip_pg_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 1329 | - if ($exist) { |
|
| 1330 | - $exist = ' IF EXISTS'; |
|
| 1331 | - } |
|
| 1332 | - if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1333 | - return true; |
|
| 1334 | - } else { |
|
| 1335 | - return false; |
|
| 1336 | - } |
|
| 1329 | + if ($exist) { |
|
| 1330 | + $exist = ' IF EXISTS'; |
|
| 1331 | + } |
|
| 1332 | + if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1333 | + return true; |
|
| 1334 | + } else { |
|
| 1335 | + return false; |
|
| 1336 | + } |
|
| 1337 | 1337 | } |
| 1338 | 1338 | |
| 1339 | 1339 | // supprime une vue |
| 1340 | 1340 | // https://code.spip.net/@spip_pg_drop_view |
| 1341 | 1341 | function spip_pg_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 1342 | - if ($exist) { |
|
| 1343 | - $exist = ' IF EXISTS'; |
|
| 1344 | - } |
|
| 1342 | + if ($exist) { |
|
| 1343 | + $exist = ' IF EXISTS'; |
|
| 1344 | + } |
|
| 1345 | 1345 | |
| 1346 | - return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1346 | + return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1347 | 1347 | } |
| 1348 | 1348 | |
| 1349 | 1349 | /** |
@@ -1360,41 +1360,41 @@ discard block |
||
| 1360 | 1360 | * Ressource à utiliser avec sql_fetch() |
| 1361 | 1361 | **/ |
| 1362 | 1362 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1363 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1364 | - $link = $connexion['link']; |
|
| 1365 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1363 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1364 | + $link = $connexion['link']; |
|
| 1365 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1366 | 1366 | |
| 1367 | - return spip_pg_query_simple($link, $q); |
|
| 1367 | + return spip_pg_query_simple($link, $q); |
|
| 1368 | 1368 | } |
| 1369 | 1369 | |
| 1370 | 1370 | // https://code.spip.net/@spip_pg_showtable |
| 1371 | 1371 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1372 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1373 | - $link = $connexion['link']; |
|
| 1374 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1375 | - |
|
| 1376 | - $res = spip_pg_query_simple($link, $q); |
|
| 1377 | - if (!$res) { |
|
| 1378 | - return false; |
|
| 1379 | - } |
|
| 1380 | - |
|
| 1381 | - // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1382 | - // il faut en tenir compte dans le return |
|
| 1383 | - $fields = []; |
|
| 1384 | - while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1385 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1386 | - } |
|
| 1387 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1388 | - $res = spip_pg_query_simple($link, $q); |
|
| 1389 | - $keys = []; |
|
| 1390 | - while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1391 | - if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1392 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1393 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1394 | - } |
|
| 1395 | - } |
|
| 1396 | - |
|
| 1397 | - return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1372 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1373 | + $link = $connexion['link']; |
|
| 1374 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1375 | + |
|
| 1376 | + $res = spip_pg_query_simple($link, $q); |
|
| 1377 | + if (!$res) { |
|
| 1378 | + return false; |
|
| 1379 | + } |
|
| 1380 | + |
|
| 1381 | + // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1382 | + // il faut en tenir compte dans le return |
|
| 1383 | + $fields = []; |
|
| 1384 | + while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1385 | + $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1386 | + } |
|
| 1387 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1388 | + $res = spip_pg_query_simple($link, $q); |
|
| 1389 | + $keys = []; |
|
| 1390 | + while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1391 | + if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1392 | + $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1393 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1394 | + } |
|
| 1395 | + } |
|
| 1396 | + |
|
| 1397 | + return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1398 | 1398 | } |
| 1399 | 1399 | |
| 1400 | 1400 | // Fonction de creation d'une table SQL nommee $nom |
@@ -1406,118 +1406,118 @@ discard block |
||
| 1406 | 1406 | // https://code.spip.net/@spip_pg_create |
| 1407 | 1407 | function spip_pg_create($nom, $champs, $cles, $autoinc = false, $temporary = false, $serveur = '', $requeter = true) { |
| 1408 | 1408 | |
| 1409 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1410 | - $link = $connexion['link']; |
|
| 1411 | - $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1412 | - |
|
| 1413 | - $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1414 | - $keys = []; |
|
| 1415 | - |
|
| 1416 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1417 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1418 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 1419 | - return; |
|
| 1420 | - } |
|
| 1421 | - |
|
| 1422 | - foreach ($cles as $k => $v) { |
|
| 1423 | - if (strpos($k, 'KEY ') === 0) { |
|
| 1424 | - $n = str_replace('`', '', $k); |
|
| 1425 | - $v = str_replace('`', '"', $v); |
|
| 1426 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1427 | - if ($k != $n) { |
|
| 1428 | - $i = "\"$i\""; |
|
| 1429 | - } |
|
| 1430 | - $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1431 | - } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1432 | - $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1433 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1434 | - } else { |
|
| 1435 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1436 | - } |
|
| 1437 | - if ($k == 'PRIMARY KEY') { |
|
| 1438 | - $prim_name = $v; |
|
| 1439 | - } |
|
| 1440 | - $s = ','; |
|
| 1441 | - } |
|
| 1442 | - $s = ''; |
|
| 1443 | - |
|
| 1444 | - $character_set = ''; |
|
| 1445 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1446 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1447 | - } |
|
| 1448 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1449 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1450 | - } |
|
| 1451 | - |
|
| 1452 | - foreach ($champs as $k => $v) { |
|
| 1453 | - $k = str_replace('`', '"', $k); |
|
| 1454 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1455 | - if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1456 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1457 | - } |
|
| 1458 | - } |
|
| 1459 | - |
|
| 1460 | - $query .= "$s\n\t\t$k " |
|
| 1461 | - . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1462 | - ? ' bigserial' |
|
| 1463 | - : mysql2pg_type($v) |
|
| 1464 | - ); |
|
| 1465 | - $s = ','; |
|
| 1466 | - } |
|
| 1467 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1468 | - |
|
| 1469 | - // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1470 | - |
|
| 1471 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1472 | - ($character_set ? " DEFAULT $character_set" : '') |
|
| 1473 | - . "\n"; |
|
| 1474 | - |
|
| 1475 | - if (!$requeter) { |
|
| 1476 | - return $q; |
|
| 1477 | - } |
|
| 1478 | - $connexion['last'] = $q; |
|
| 1479 | - $r = @pg_query($link, $q); |
|
| 1480 | - |
|
| 1481 | - if (!$r) { |
|
| 1482 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1483 | - } else { |
|
| 1484 | - foreach ($keys as $index) { |
|
| 1485 | - pg_query($link, $index); |
|
| 1486 | - } |
|
| 1487 | - } |
|
| 1488 | - |
|
| 1489 | - return $r; |
|
| 1409 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1410 | + $link = $connexion['link']; |
|
| 1411 | + $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1412 | + |
|
| 1413 | + $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1414 | + $keys = []; |
|
| 1415 | + |
|
| 1416 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1417 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1418 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 1419 | + return; |
|
| 1420 | + } |
|
| 1421 | + |
|
| 1422 | + foreach ($cles as $k => $v) { |
|
| 1423 | + if (strpos($k, 'KEY ') === 0) { |
|
| 1424 | + $n = str_replace('`', '', $k); |
|
| 1425 | + $v = str_replace('`', '"', $v); |
|
| 1426 | + $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1427 | + if ($k != $n) { |
|
| 1428 | + $i = "\"$i\""; |
|
| 1429 | + } |
|
| 1430 | + $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1431 | + } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1432 | + $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1433 | + $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1434 | + } else { |
|
| 1435 | + $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1436 | + } |
|
| 1437 | + if ($k == 'PRIMARY KEY') { |
|
| 1438 | + $prim_name = $v; |
|
| 1439 | + } |
|
| 1440 | + $s = ','; |
|
| 1441 | + } |
|
| 1442 | + $s = ''; |
|
| 1443 | + |
|
| 1444 | + $character_set = ''; |
|
| 1445 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1446 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1447 | + } |
|
| 1448 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1449 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1450 | + } |
|
| 1451 | + |
|
| 1452 | + foreach ($champs as $k => $v) { |
|
| 1453 | + $k = str_replace('`', '"', $k); |
|
| 1454 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1455 | + if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1456 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1457 | + } |
|
| 1458 | + } |
|
| 1459 | + |
|
| 1460 | + $query .= "$s\n\t\t$k " |
|
| 1461 | + . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1462 | + ? ' bigserial' |
|
| 1463 | + : mysql2pg_type($v) |
|
| 1464 | + ); |
|
| 1465 | + $s = ','; |
|
| 1466 | + } |
|
| 1467 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1468 | + |
|
| 1469 | + // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1470 | + |
|
| 1471 | + $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1472 | + ($character_set ? " DEFAULT $character_set" : '') |
|
| 1473 | + . "\n"; |
|
| 1474 | + |
|
| 1475 | + if (!$requeter) { |
|
| 1476 | + return $q; |
|
| 1477 | + } |
|
| 1478 | + $connexion['last'] = $q; |
|
| 1479 | + $r = @pg_query($link, $q); |
|
| 1480 | + |
|
| 1481 | + if (!$r) { |
|
| 1482 | + spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1483 | + } else { |
|
| 1484 | + foreach ($keys as $index) { |
|
| 1485 | + pg_query($link, $index); |
|
| 1486 | + } |
|
| 1487 | + } |
|
| 1488 | + |
|
| 1489 | + return $r; |
|
| 1490 | 1490 | } |
| 1491 | 1491 | |
| 1492 | 1492 | |
| 1493 | 1493 | function spip_pg_create_base($nom, $serveur = '', $requeter = true) { |
| 1494 | - return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1494 | + return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1495 | 1495 | } |
| 1496 | 1496 | |
| 1497 | 1497 | // Fonction de creation d'une vue SQL nommee $nom |
| 1498 | 1498 | // https://code.spip.net/@spip_pg_create_view |
| 1499 | 1499 | function spip_pg_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 1500 | - if (!$query_select) { |
|
| 1501 | - return false; |
|
| 1502 | - } |
|
| 1503 | - // vue deja presente |
|
| 1504 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 1505 | - if ($requeter) { |
|
| 1506 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1507 | - } |
|
| 1500 | + if (!$query_select) { |
|
| 1501 | + return false; |
|
| 1502 | + } |
|
| 1503 | + // vue deja presente |
|
| 1504 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 1505 | + if ($requeter) { |
|
| 1506 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1507 | + } |
|
| 1508 | 1508 | |
| 1509 | - return false; |
|
| 1510 | - } |
|
| 1509 | + return false; |
|
| 1510 | + } |
|
| 1511 | 1511 | |
| 1512 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1512 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1513 | 1513 | |
| 1514 | - return spip_pg_query($query, $serveur, $requeter); |
|
| 1514 | + return spip_pg_query($query, $serveur, $requeter); |
|
| 1515 | 1515 | } |
| 1516 | 1516 | |
| 1517 | 1517 | |
| 1518 | 1518 | // https://code.spip.net/@spip_pg_set_connect_charset |
| 1519 | 1519 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1520 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1520 | + spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1521 | 1521 | } |
| 1522 | 1522 | |
| 1523 | 1523 | |
@@ -1531,7 +1531,7 @@ discard block |
||
| 1531 | 1531 | **/ |
| 1532 | 1532 | // https://code.spip.net/@spip_sqlite_optimize |
| 1533 | 1533 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1534 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1534 | + return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1535 | 1535 | } |
| 1536 | 1536 | |
| 1537 | 1537 | // Selectionner la sous-chaine dans $objet |
@@ -1539,13 +1539,13 @@ discard block |
||
| 1539 | 1539 | |
| 1540 | 1540 | // https://code.spip.net/@spip_pg_multi |
| 1541 | 1541 | function spip_pg_multi($objet, $lang) { |
| 1542 | - $r = 'regexp_replace(' |
|
| 1543 | - . $objet |
|
| 1544 | - . ",'<multi>.*[[]" |
|
| 1545 | - . $lang |
|
| 1546 | - . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1542 | + $r = 'regexp_replace(' |
|
| 1543 | + . $objet |
|
| 1544 | + . ",'<multi>.*[[]" |
|
| 1545 | + . $lang |
|
| 1546 | + . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1547 | 1547 | |
| 1548 | - return $r; |
|
| 1548 | + return $r; |
|
| 1549 | 1549 | } |
| 1550 | 1550 | |
| 1551 | 1551 | // Palanquee d'idiosyncrasies MySQL dans les creations de table |
@@ -1553,31 +1553,31 @@ discard block |
||
| 1553 | 1553 | |
| 1554 | 1554 | // https://code.spip.net/@mysql2pg_type |
| 1555 | 1555 | function mysql2pg_type($v) { |
| 1556 | - $remplace = [ |
|
| 1557 | - '/auto_increment/i' => '', // non reconnu |
|
| 1558 | - '/bigint/i' => 'bigint', |
|
| 1559 | - '/mediumint/i' => 'mediumint', |
|
| 1560 | - '/smallint/i' => 'smallint', |
|
| 1561 | - '/tinyint/i' => 'int', |
|
| 1562 | - '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1563 | - '/longtext/i' => 'text', |
|
| 1564 | - '/mediumtext/i' => 'text', |
|
| 1565 | - '/tinytext/i' => 'text', |
|
| 1566 | - '/longblob/i' => 'text', |
|
| 1567 | - '/0000-00-00/' => '0001-01-01', |
|
| 1568 | - '/datetime/i' => 'timestamp', |
|
| 1569 | - '/unsigned/i' => '', |
|
| 1570 | - '/double/i' => 'double precision', |
|
| 1571 | - '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1572 | - '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1573 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1574 | - ]; |
|
| 1575 | - |
|
| 1576 | - return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1556 | + $remplace = [ |
|
| 1557 | + '/auto_increment/i' => '', // non reconnu |
|
| 1558 | + '/bigint/i' => 'bigint', |
|
| 1559 | + '/mediumint/i' => 'mediumint', |
|
| 1560 | + '/smallint/i' => 'smallint', |
|
| 1561 | + '/tinyint/i' => 'int', |
|
| 1562 | + '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1563 | + '/longtext/i' => 'text', |
|
| 1564 | + '/mediumtext/i' => 'text', |
|
| 1565 | + '/tinytext/i' => 'text', |
|
| 1566 | + '/longblob/i' => 'text', |
|
| 1567 | + '/0000-00-00/' => '0001-01-01', |
|
| 1568 | + '/datetime/i' => 'timestamp', |
|
| 1569 | + '/unsigned/i' => '', |
|
| 1570 | + '/double/i' => 'double precision', |
|
| 1571 | + '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1572 | + '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1573 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1574 | + ]; |
|
| 1575 | + |
|
| 1576 | + return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1577 | 1577 | } |
| 1578 | 1578 | |
| 1579 | 1579 | // Renvoie false si on n'a pas les fonctions pg (pour l'install) |
| 1580 | 1580 | // https://code.spip.net/@spip_versions_pg |
| 1581 | 1581 | function spip_versions_pg() { |
| 1582 | - return function_exists('pg_connect'); |
|
| 1582 | + return function_exists('pg_connect'); |
|
| 1583 | 1583 | } |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | **/ |
| 8 | 8 | |
| 9 | 9 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 10 | - return; |
|
| 10 | + return; |
|
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | /** |
@@ -28,51 +28,51 @@ discard block |
||
| 28 | 28 | * - afficher : tableau des objets à afficher (mais pas forcément sélectionnables) |
| 29 | 29 | */ |
| 30 | 30 | function selecteur_lister_objets($whitelist = [], $blacklist = []) { |
| 31 | - static $liste_selecteurs, $liste_parents; |
|
| 32 | - |
|
| 33 | - if (!$liste_selecteurs) { |
|
| 34 | - $liste_selecteurs = find_all_in_path('formulaires/selecteur/', 'hierarchie-[\w]+[.]html$'); |
|
| 35 | - } |
|
| 36 | - $objets_selectionner = []; |
|
| 37 | - foreach ($liste_selecteurs as $fichier => $chemin) { |
|
| 38 | - $objets_selectionner[] = preg_replace('/^hierarchie-([\w]+)[.]html$/', '$1', $fichier); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - // S'il y a une whitelist on ne garde que ce qui est dedans |
|
| 42 | - if (!empty($whitelist)) { |
|
| 43 | - $whitelist = array_map('table_objet', $whitelist); |
|
| 44 | - $objets_selectionner = array_intersect($objets_selectionner, $whitelist); |
|
| 45 | - } |
|
| 46 | - // On supprime ce qui est dans la blacklist |
|
| 47 | - $blacklist = array_map('table_objet', $blacklist); |
|
| 48 | - // On enlève toujours la racine |
|
| 49 | - $blacklist[] = 'racine'; |
|
| 50 | - $objets_selectionner = array_diff($objets_selectionner, $blacklist); |
|
| 51 | - |
|
| 52 | - // Ensuite on cherche ce qu'on doit afficher : au moins ceux qu'on peut sélectionner |
|
| 53 | - $objets_afficher = $objets_selectionner; |
|
| 54 | - |
|
| 55 | - // Il faut alors chercher d'éventuels parents obligatoires en plus : |
|
| 56 | - // lister-trucs-bidules.html => on doit afficher des "trucs" pour trouver des "bidules" |
|
| 57 | - if (!$liste_parents) { |
|
| 58 | - $liste_parents = find_all_in_path('formulaires/selecteur/', 'lister-[\w]+-[\w]+[.]html$'); |
|
| 59 | - } |
|
| 60 | - foreach ($liste_parents as $fichier => $chemin) { |
|
| 61 | - preg_match('/^lister-([\w]+)-([\w]+)[.]html$/', $fichier, $captures); |
|
| 62 | - $parent = $captures[1]; |
|
| 63 | - $type = $captures[2]; |
|
| 64 | - // Si le type fait partie de ce qu'on doit afficher alors on ajoute aussi le parent à l'affichage |
|
| 65 | - if (in_array($type, $objets_afficher)) { |
|
| 66 | - $objets_afficher[] = $parent; |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $objets = [ |
|
| 71 | - 'selectionner' => array_unique($objets_selectionner), |
|
| 72 | - 'afficher' => array_unique($objets_afficher), |
|
| 73 | - ]; |
|
| 74 | - |
|
| 75 | - return $objets; |
|
| 31 | + static $liste_selecteurs, $liste_parents; |
|
| 32 | + |
|
| 33 | + if (!$liste_selecteurs) { |
|
| 34 | + $liste_selecteurs = find_all_in_path('formulaires/selecteur/', 'hierarchie-[\w]+[.]html$'); |
|
| 35 | + } |
|
| 36 | + $objets_selectionner = []; |
|
| 37 | + foreach ($liste_selecteurs as $fichier => $chemin) { |
|
| 38 | + $objets_selectionner[] = preg_replace('/^hierarchie-([\w]+)[.]html$/', '$1', $fichier); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + // S'il y a une whitelist on ne garde que ce qui est dedans |
|
| 42 | + if (!empty($whitelist)) { |
|
| 43 | + $whitelist = array_map('table_objet', $whitelist); |
|
| 44 | + $objets_selectionner = array_intersect($objets_selectionner, $whitelist); |
|
| 45 | + } |
|
| 46 | + // On supprime ce qui est dans la blacklist |
|
| 47 | + $blacklist = array_map('table_objet', $blacklist); |
|
| 48 | + // On enlève toujours la racine |
|
| 49 | + $blacklist[] = 'racine'; |
|
| 50 | + $objets_selectionner = array_diff($objets_selectionner, $blacklist); |
|
| 51 | + |
|
| 52 | + // Ensuite on cherche ce qu'on doit afficher : au moins ceux qu'on peut sélectionner |
|
| 53 | + $objets_afficher = $objets_selectionner; |
|
| 54 | + |
|
| 55 | + // Il faut alors chercher d'éventuels parents obligatoires en plus : |
|
| 56 | + // lister-trucs-bidules.html => on doit afficher des "trucs" pour trouver des "bidules" |
|
| 57 | + if (!$liste_parents) { |
|
| 58 | + $liste_parents = find_all_in_path('formulaires/selecteur/', 'lister-[\w]+-[\w]+[.]html$'); |
|
| 59 | + } |
|
| 60 | + foreach ($liste_parents as $fichier => $chemin) { |
|
| 61 | + preg_match('/^lister-([\w]+)-([\w]+)[.]html$/', $fichier, $captures); |
|
| 62 | + $parent = $captures[1]; |
|
| 63 | + $type = $captures[2]; |
|
| 64 | + // Si le type fait partie de ce qu'on doit afficher alors on ajoute aussi le parent à l'affichage |
|
| 65 | + if (in_array($type, $objets_afficher)) { |
|
| 66 | + $objets_afficher[] = $parent; |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $objets = [ |
|
| 71 | + 'selectionner' => array_unique($objets_selectionner), |
|
| 72 | + 'afficher' => array_unique($objets_afficher), |
|
| 73 | + ]; |
|
| 74 | + |
|
| 75 | + return $objets; |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
@@ -99,31 +99,31 @@ discard block |
||
| 99 | 99 | * liste des couples (objets => id_objet) ou liste des identifiants d'un type d'objet. |
| 100 | 100 | **/ |
| 101 | 101 | function picker_selected($selected, $type = '') { |
| 102 | - $select = []; |
|
| 103 | - $type = preg_replace(',\W,', '', $type); |
|
| 104 | - |
|
| 105 | - if ($selected and !is_array($selected)) { |
|
| 106 | - $selected = explode(',', $selected); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - if (is_array($selected)) { |
|
| 110 | - foreach ($selected as $value) { |
|
| 111 | - // Si c'est le bon format déjà |
|
| 112 | - if (preg_match('/^([\w]+)[|]([0-9]+)$/', $value, $captures)) { |
|
| 113 | - $objet = $captures[1]; |
|
| 114 | - $id_objet = intval($captures[2]); |
|
| 115 | - |
|
| 116 | - // Si on cherche un type et que c'est le bon, on renvoit un tableau que d'identifiants |
|
| 117 | - if (is_string($type) and $type == $objet and ($id_objet or in_array($objet, ['racine', 'rubrique']))) { |
|
| 118 | - $select[] = $id_objet; |
|
| 119 | - } elseif (!$type and ($id_objet or in_array($objet, ['racine', 'rubrique']))) { |
|
| 120 | - $select[] = ['objet' => $objet, 'id_objet' => $id_objet]; |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return $select; |
|
| 102 | + $select = []; |
|
| 103 | + $type = preg_replace(',\W,', '', $type); |
|
| 104 | + |
|
| 105 | + if ($selected and !is_array($selected)) { |
|
| 106 | + $selected = explode(',', $selected); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + if (is_array($selected)) { |
|
| 110 | + foreach ($selected as $value) { |
|
| 111 | + // Si c'est le bon format déjà |
|
| 112 | + if (preg_match('/^([\w]+)[|]([0-9]+)$/', $value, $captures)) { |
|
| 113 | + $objet = $captures[1]; |
|
| 114 | + $id_objet = intval($captures[2]); |
|
| 115 | + |
|
| 116 | + // Si on cherche un type et que c'est le bon, on renvoit un tableau que d'identifiants |
|
| 117 | + if (is_string($type) and $type == $objet and ($id_objet or in_array($objet, ['racine', 'rubrique']))) { |
|
| 118 | + $select[] = $id_objet; |
|
| 119 | + } elseif (!$type and ($id_objet or in_array($objet, ['racine', 'rubrique']))) { |
|
| 120 | + $select[] = ['objet' => $objet, 'id_objet' => $id_objet]; |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return $select; |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | /** |
@@ -140,49 +140,49 @@ discard block |
||
| 140 | 140 | * Booléen indiquant si les articles sont sélectionnables |
| 141 | 141 | */ |
| 142 | 142 | function picker_identifie_id_rapide($ref, $rubriques_ou_objets = false, $articles = false) { |
| 143 | - include_spip('inc/json'); |
|
| 144 | - include_spip('inc/lien'); |
|
| 145 | - |
|
| 146 | - // On construit un tableau des objets sélectionnables suivant les paramètres |
|
| 147 | - $objets = []; |
|
| 148 | - if ($rubriques_ou_objets and is_array($rubriques_ou_objets)) { |
|
| 149 | - $objets = $rubriques_ou_objets; |
|
| 150 | - } else { |
|
| 151 | - if ($rubriques_ou_objets) { |
|
| 152 | - $objets[] = 'rubriques'; |
|
| 153 | - } |
|
| 154 | - if ($articles) { |
|
| 155 | - $objets[] = 'articles'; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // si id numerique et un seul objet possible, pas d'ambiguite |
|
| 160 | - if (is_numeric($ref) and count($objets) === 1) { |
|
| 161 | - $ref = reset($objets) . $ref; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - // Si la référence ne correspond à rien, c'est fini |
|
| 165 | - if (!($match = typer_raccourci($ref))) { |
|
| 166 | - return json_export(false); |
|
| 167 | - } |
|
| 168 | - // Sinon on récupère les infos utiles |
|
| 169 | - [$type, , $id, , , , ] = array_pad($match, 7, null); |
|
| 170 | - |
|
| 171 | - // On regarde si le type trouvé fait partie des objets sélectionnables |
|
| 172 | - if (!in_array(table_objet($type), $objets)) { |
|
| 173 | - return json_export(false); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // Maintenant que tout est bon, on cherche les informations sur cet objet |
|
| 177 | - include_spip('inc/filtres'); |
|
| 178 | - if (!$titre = generer_info_entite($id, $type, 'titre')) { |
|
| 179 | - return json_export(false); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - // On simplifie le texte |
|
| 183 | - $titre = attribut_html($titre); |
|
| 184 | - |
|
| 185 | - return json_export(['type' => $type, 'id' => "$type|$id", 'titre' => $titre]); |
|
| 143 | + include_spip('inc/json'); |
|
| 144 | + include_spip('inc/lien'); |
|
| 145 | + |
|
| 146 | + // On construit un tableau des objets sélectionnables suivant les paramètres |
|
| 147 | + $objets = []; |
|
| 148 | + if ($rubriques_ou_objets and is_array($rubriques_ou_objets)) { |
|
| 149 | + $objets = $rubriques_ou_objets; |
|
| 150 | + } else { |
|
| 151 | + if ($rubriques_ou_objets) { |
|
| 152 | + $objets[] = 'rubriques'; |
|
| 153 | + } |
|
| 154 | + if ($articles) { |
|
| 155 | + $objets[] = 'articles'; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // si id numerique et un seul objet possible, pas d'ambiguite |
|
| 160 | + if (is_numeric($ref) and count($objets) === 1) { |
|
| 161 | + $ref = reset($objets) . $ref; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + // Si la référence ne correspond à rien, c'est fini |
|
| 165 | + if (!($match = typer_raccourci($ref))) { |
|
| 166 | + return json_export(false); |
|
| 167 | + } |
|
| 168 | + // Sinon on récupère les infos utiles |
|
| 169 | + [$type, , $id, , , , ] = array_pad($match, 7, null); |
|
| 170 | + |
|
| 171 | + // On regarde si le type trouvé fait partie des objets sélectionnables |
|
| 172 | + if (!in_array(table_objet($type), $objets)) { |
|
| 173 | + return json_export(false); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // Maintenant que tout est bon, on cherche les informations sur cet objet |
|
| 177 | + include_spip('inc/filtres'); |
|
| 178 | + if (!$titre = generer_info_entite($id, $type, 'titre')) { |
|
| 179 | + return json_export(false); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + // On simplifie le texte |
|
| 183 | + $titre = attribut_html($titre); |
|
| 184 | + |
|
| 185 | + return json_export(['type' => $type, 'id' => "$type|$id", 'titre' => $titre]); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | /** |
@@ -205,34 +205,34 @@ discard block |
||
| 205 | 205 | * Comme le filtre `oui` : espace (` `) si rubrique à afficher, chaîne vide sinon. |
| 206 | 206 | */ |
| 207 | 207 | function test_enfants_rubrique($id_rubrique, $types = []) { |
| 208 | - static $has_child = []; |
|
| 209 | - |
|
| 210 | - if (!isset($has_child[$id_rubrique])) { |
|
| 211 | - $types = (is_array($types) ? array_filter($types) : []); |
|
| 212 | - |
|
| 213 | - // recuperer tous les freres et soeurs de la rubrique visee |
|
| 214 | - $id_parent = sql_getfetsel('id_parent', 'spip_rubriques', 'id_rubrique=' . intval($id_rubrique)); |
|
| 215 | - $fratrie = sql_allfetsel('id_rubrique', 'spip_rubriques', 'id_parent=' . intval($id_parent)); |
|
| 216 | - $fratrie = array_column($fratrie, 'id_rubrique'); |
|
| 217 | - $has = sql_allfetsel('DISTINCT id_parent', 'spip_rubriques', sql_in('id_parent', $fratrie)); |
|
| 218 | - $has = array_column($has, 'id_parent'); |
|
| 219 | - $fratrie = array_diff($fratrie, $has); |
|
| 220 | - |
|
| 221 | - while (count($fratrie) and is_array($types) and count($types)) { |
|
| 222 | - $type = array_shift($types); |
|
| 223 | - $h = sql_allfetsel('DISTINCT id_rubrique', table_objet_sql($type), sql_in('id_rubrique', $fratrie)); |
|
| 224 | - $h = array_column($h, 'id_rubrique'); |
|
| 225 | - $has = array_merge($has, $h); |
|
| 226 | - $fratrie = array_diff($fratrie, $h); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - if (count($has)) { |
|
| 230 | - $has_child = $has_child + array_combine($has, array_pad([], count($has), true)); |
|
| 231 | - } |
|
| 232 | - if (count($fratrie)) { |
|
| 233 | - $has_child = $has_child + array_combine($fratrie, array_pad([], count($fratrie), false)); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - return $has_child[$id_rubrique] ? ' ' : ''; |
|
| 208 | + static $has_child = []; |
|
| 209 | + |
|
| 210 | + if (!isset($has_child[$id_rubrique])) { |
|
| 211 | + $types = (is_array($types) ? array_filter($types) : []); |
|
| 212 | + |
|
| 213 | + // recuperer tous les freres et soeurs de la rubrique visee |
|
| 214 | + $id_parent = sql_getfetsel('id_parent', 'spip_rubriques', 'id_rubrique=' . intval($id_rubrique)); |
|
| 215 | + $fratrie = sql_allfetsel('id_rubrique', 'spip_rubriques', 'id_parent=' . intval($id_parent)); |
|
| 216 | + $fratrie = array_column($fratrie, 'id_rubrique'); |
|
| 217 | + $has = sql_allfetsel('DISTINCT id_parent', 'spip_rubriques', sql_in('id_parent', $fratrie)); |
|
| 218 | + $has = array_column($has, 'id_parent'); |
|
| 219 | + $fratrie = array_diff($fratrie, $has); |
|
| 220 | + |
|
| 221 | + while (count($fratrie) and is_array($types) and count($types)) { |
|
| 222 | + $type = array_shift($types); |
|
| 223 | + $h = sql_allfetsel('DISTINCT id_rubrique', table_objet_sql($type), sql_in('id_rubrique', $fratrie)); |
|
| 224 | + $h = array_column($h, 'id_rubrique'); |
|
| 225 | + $has = array_merge($has, $h); |
|
| 226 | + $fratrie = array_diff($fratrie, $h); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + if (count($has)) { |
|
| 230 | + $has_child = $has_child + array_combine($has, array_pad([], count($has), true)); |
|
| 231 | + } |
|
| 232 | + if (count($fratrie)) { |
|
| 233 | + $has_child = $has_child + array_combine($fratrie, array_pad([], count($fratrie), false)); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + return $has_child[$id_rubrique] ? ' ' : ''; |
|
| 238 | 238 | } |
@@ -158,7 +158,7 @@ discard block |
||
| 158 | 158 | |
| 159 | 159 | // si id numerique et un seul objet possible, pas d'ambiguite |
| 160 | 160 | if (is_numeric($ref) and count($objets) === 1) { |
| 161 | - $ref = reset($objets) . $ref; |
|
| 161 | + $ref = reset($objets).$ref; |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | // Si la référence ne correspond à rien, c'est fini |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | return json_export(false); |
| 167 | 167 | } |
| 168 | 168 | // Sinon on récupère les infos utiles |
| 169 | - [$type, , $id, , , , ] = array_pad($match, 7, null); |
|
| 169 | + [$type,, $id,,,,] = array_pad($match, 7, null); |
|
| 170 | 170 | |
| 171 | 171 | // On regarde si le type trouvé fait partie des objets sélectionnables |
| 172 | 172 | if (!in_array(table_objet($type), $objets)) { |
@@ -211,8 +211,8 @@ discard block |
||
| 211 | 211 | $types = (is_array($types) ? array_filter($types) : []); |
| 212 | 212 | |
| 213 | 213 | // recuperer tous les freres et soeurs de la rubrique visee |
| 214 | - $id_parent = sql_getfetsel('id_parent', 'spip_rubriques', 'id_rubrique=' . intval($id_rubrique)); |
|
| 215 | - $fratrie = sql_allfetsel('id_rubrique', 'spip_rubriques', 'id_parent=' . intval($id_parent)); |
|
| 214 | + $id_parent = sql_getfetsel('id_parent', 'spip_rubriques', 'id_rubrique='.intval($id_rubrique)); |
|
| 215 | + $fratrie = sql_allfetsel('id_rubrique', 'spip_rubriques', 'id_parent='.intval($id_parent)); |
|
| 216 | 216 | $fratrie = array_column($fratrie, 'id_rubrique'); |
| 217 | 217 | $has = sql_allfetsel('DISTINCT id_parent', 'spip_rubriques', sql_in('id_parent', $fratrie)); |
| 218 | 218 | $has = array_column($has, 'id_parent'); |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | \***************************************************************************/ |
| 12 | 12 | |
| 13 | 13 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 14 | - return; |
|
| 14 | + return; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | include_spip('base/abstract_sql'); |
@@ -23,16 +23,16 @@ discard block |
||
| 23 | 23 | // |
| 24 | 24 | |
| 25 | 25 | function inc_lien_dist( |
| 26 | - $lien, |
|
| 27 | - $texte = '', |
|
| 28 | - $class = '', |
|
| 29 | - $title = '', |
|
| 30 | - $hlang = '', |
|
| 31 | - $rel = '', |
|
| 32 | - string $connect = '', |
|
| 33 | - $env = [] |
|
| 26 | + $lien, |
|
| 27 | + $texte = '', |
|
| 28 | + $class = '', |
|
| 29 | + $title = '', |
|
| 30 | + $hlang = '', |
|
| 31 | + $rel = '', |
|
| 32 | + string $connect = '', |
|
| 33 | + $env = [] |
|
| 34 | 34 | ) { |
| 35 | - return $lien; |
|
| 35 | + return $lien; |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | // Regexp des raccourcis, aussi utilisee pour la fusion de sauvegarde Spip |
@@ -44,30 +44,30 @@ discard block |
||
| 44 | 44 | // https://code.spip.net/@expanser_liens |
| 45 | 45 | function expanser_liens($t, string $connect = '', $env = []) { |
| 46 | 46 | |
| 47 | - $t = pipeline('pre_liens', $t); |
|
| 47 | + $t = pipeline('pre_liens', $t); |
|
| 48 | 48 | |
| 49 | - // on passe a traiter_modeles la liste des liens reperes pour lui permettre |
|
| 50 | - // de remettre le texte d'origine dans les parametres du modele |
|
| 51 | - $t = traiter_modeles($t, false, false, $connect); |
|
| 49 | + // on passe a traiter_modeles la liste des liens reperes pour lui permettre |
|
| 50 | + // de remettre le texte d'origine dans les parametres du modele |
|
| 51 | + $t = traiter_modeles($t, false, false, $connect); |
|
| 52 | 52 | |
| 53 | - return $t; |
|
| 53 | + return $t; |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | // Meme analyse mais pour eliminer les liens |
| 57 | 57 | // et ne laisser que leur titre, a expliciter si ce n'est fait |
| 58 | 58 | // https://code.spip.net/@nettoyer_raccourcis_typo |
| 59 | 59 | function nettoyer_raccourcis_typo($texte, string $connect = '') { |
| 60 | - return $texte; |
|
| 60 | + return $texte; |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | // Repere dans la partie texte d'un raccourci [texte->...] |
| 64 | 64 | // la langue et la bulle eventuelles |
| 65 | 65 | // https://code.spip.net/@traiter_raccourci_lien_atts |
| 66 | 66 | function traiter_raccourci_lien_atts($texte) { |
| 67 | - $bulle = ''; |
|
| 68 | - $hlang = ''; |
|
| 67 | + $bulle = ''; |
|
| 68 | + $hlang = ''; |
|
| 69 | 69 | |
| 70 | - return [trim($texte), $bulle, $hlang]; |
|
| 70 | + return [trim($texte), $bulle, $hlang]; |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | define('_RACCOURCI_CHAPO', '/^(\W*)(\W*)(\w*\d+([?#].*)?)$/'); |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | * @return string |
| 84 | 84 | */ |
| 85 | 85 | function virtuel_redirige($virtuel, $url = false) { |
| 86 | - return $virtuel; |
|
| 86 | + return $virtuel; |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | // Cherche un lien du type [->raccourci 123] |
@@ -97,123 +97,123 @@ discard block |
||
| 97 | 97 | |
| 98 | 98 | // https://code.spip.net/@calculer_url |
| 99 | 99 | function calculer_url($ref, $texte = '', $pour = 'url', string $connect = '', $echappe_typo = true) { |
| 100 | - $r = traiter_lien_implicite($ref, $texte, $pour, $connect); |
|
| 100 | + $r = traiter_lien_implicite($ref, $texte, $pour, $connect); |
|
| 101 | 101 | |
| 102 | - return $r ?: traiter_lien_explicite($ref, $texte, $pour, $connect, $echappe_typo); |
|
| 102 | + return $r ?: traiter_lien_explicite($ref, $texte, $pour, $connect, $echappe_typo); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | define('_EXTRAIRE_LIEN', ',^\s*(?:' . _PROTOCOLES_STD . '):?/?/?\s*$,iS'); |
| 106 | 106 | |
| 107 | 107 | // https://code.spip.net/@traiter_lien_explicite |
| 108 | 108 | function traiter_lien_explicite($ref, $texte = '', $pour = 'url', string $connect = '', $echappe_typo = true) { |
| 109 | - if (preg_match(_EXTRAIRE_LIEN, $ref)) { |
|
| 110 | - return ($pour != 'tout') ? '' : ['', '', '', '']; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $lien = entites_html(trim($ref)); |
|
| 114 | - |
|
| 115 | - // Liens explicites |
|
| 116 | - if (!$texte) { |
|
| 117 | - $texte = str_replace('"', '', $lien); |
|
| 118 | - // evite l'affichage de trops longues urls. |
|
| 119 | - $lien_court = charger_fonction('lien_court', 'inc'); |
|
| 120 | - $texte = $lien_court($texte); |
|
| 121 | - if ($echappe_typo) { |
|
| 122 | - $texte = '<html>' . quote_amp($texte) . '</html>'; |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - // petites corrections d'URL |
|
| 127 | - if (preg_match('/^www\.[^@]+$/S', $lien)) { |
|
| 128 | - $lien = 'http://' . $lien; |
|
| 129 | - } else { |
|
| 130 | - if (strpos($lien, '@') && email_valide($lien)) { |
|
| 131 | - if (!$texte) { |
|
| 132 | - $texte = $lien; |
|
| 133 | - } |
|
| 134 | - $lien = 'mailto:' . $lien; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - if ($pour == 'url') { |
|
| 139 | - return $lien; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - if ($pour == 'titre') { |
|
| 143 | - return $texte; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return ['url' => $lien, 'titre' => $texte]; |
|
| 109 | + if (preg_match(_EXTRAIRE_LIEN, $ref)) { |
|
| 110 | + return ($pour != 'tout') ? '' : ['', '', '', '']; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $lien = entites_html(trim($ref)); |
|
| 114 | + |
|
| 115 | + // Liens explicites |
|
| 116 | + if (!$texte) { |
|
| 117 | + $texte = str_replace('"', '', $lien); |
|
| 118 | + // evite l'affichage de trops longues urls. |
|
| 119 | + $lien_court = charger_fonction('lien_court', 'inc'); |
|
| 120 | + $texte = $lien_court($texte); |
|
| 121 | + if ($echappe_typo) { |
|
| 122 | + $texte = '<html>' . quote_amp($texte) . '</html>'; |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + // petites corrections d'URL |
|
| 127 | + if (preg_match('/^www\.[^@]+$/S', $lien)) { |
|
| 128 | + $lien = 'http://' . $lien; |
|
| 129 | + } else { |
|
| 130 | + if (strpos($lien, '@') && email_valide($lien)) { |
|
| 131 | + if (!$texte) { |
|
| 132 | + $texte = $lien; |
|
| 133 | + } |
|
| 134 | + $lien = 'mailto:' . $lien; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + if ($pour == 'url') { |
|
| 139 | + return $lien; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + if ($pour == 'titre') { |
|
| 143 | + return $texte; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return ['url' => $lien, 'titre' => $texte]; |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | function liens_implicite_glose_dist($texte, $id, $type, $args, $ancre, string $connect = '') { |
| 150 | - if (function_exists($f = 'glossaire_' . $ancre)) { |
|
| 151 | - $url = $f($texte, $id); |
|
| 152 | - } else { |
|
| 153 | - $url = glossaire_std($texte); |
|
| 154 | - } |
|
| 150 | + if (function_exists($f = 'glossaire_' . $ancre)) { |
|
| 151 | + $url = $f($texte, $id); |
|
| 152 | + } else { |
|
| 153 | + $url = glossaire_std($texte); |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - return $url; |
|
| 156 | + return $url; |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | // https://code.spip.net/@traiter_lien_implicite |
| 160 | 160 | function traiter_lien_implicite($ref, $texte = '', $pour = 'url', string $connect = '') { |
| 161 | - $url = null; |
|
| 162 | - if (!($match = typer_raccourci($ref))) { |
|
| 163 | - return false; |
|
| 164 | - } |
|
| 165 | - [$type, , $id, , $args, , $ancre] = array_pad($match, 7, null); |
|
| 166 | - // attention dans le cas des sites le lien doit pointer non pas sur |
|
| 167 | - // la page locale du site, mais directement sur le site lui-meme |
|
| 168 | - if ($f = charger_fonction("implicite_$type", 'liens', true)) { |
|
| 169 | - $url = $f($texte, $id, $type, $args, $ancre, $connect); |
|
| 170 | - } |
|
| 171 | - if (!$url) { |
|
| 172 | - $url = generer_url_entite($id, $type, $args, $ancre, $connect ?: null); |
|
| 173 | - } |
|
| 174 | - if (!$url) { |
|
| 175 | - return false; |
|
| 176 | - } |
|
| 177 | - if (is_array($url)) { |
|
| 178 | - [$type, $id] = array_pad($url, 2, null); |
|
| 179 | - $url = generer_url_entite($id, $type, $args, $ancre, $connect ?: null); |
|
| 180 | - } |
|
| 181 | - if ($pour === 'url') { |
|
| 182 | - return $url; |
|
| 183 | - } |
|
| 184 | - $r = traiter_raccourci_titre($id, $type, $connect); |
|
| 185 | - if ($r) { |
|
| 186 | - $r['class'] = ($type == 'site') ? 'spip_out' : 'spip_in'; |
|
| 187 | - } |
|
| 188 | - if ($texte = trim($texte)) { |
|
| 189 | - $r['titre'] = $texte; |
|
| 190 | - } |
|
| 191 | - if (!@$r['titre']) { |
|
| 192 | - $r['titre'] = _T($type) . " $id"; |
|
| 193 | - } |
|
| 194 | - if ($pour == 'titre') { |
|
| 195 | - return $r['titre']; |
|
| 196 | - } |
|
| 197 | - $r['url'] = $url; |
|
| 198 | - |
|
| 199 | - // dans le cas d'un lien vers un doc, ajouter le type='mime/type' |
|
| 200 | - if ( |
|
| 201 | - $type == 'document' |
|
| 202 | - and $mime = sql_getfetsel( |
|
| 203 | - 'mime_type', |
|
| 204 | - 'spip_types_documents', |
|
| 205 | - 'extension IN (' . sql_get_select('extension', 'spip_documents', 'id_document=' . sql_quote($id)) . ')', |
|
| 206 | - '', |
|
| 207 | - '', |
|
| 208 | - '', |
|
| 209 | - '', |
|
| 210 | - $connect |
|
| 211 | - ) |
|
| 212 | - ) { |
|
| 213 | - $r['mime'] = $mime; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - return $r; |
|
| 161 | + $url = null; |
|
| 162 | + if (!($match = typer_raccourci($ref))) { |
|
| 163 | + return false; |
|
| 164 | + } |
|
| 165 | + [$type, , $id, , $args, , $ancre] = array_pad($match, 7, null); |
|
| 166 | + // attention dans le cas des sites le lien doit pointer non pas sur |
|
| 167 | + // la page locale du site, mais directement sur le site lui-meme |
|
| 168 | + if ($f = charger_fonction("implicite_$type", 'liens', true)) { |
|
| 169 | + $url = $f($texte, $id, $type, $args, $ancre, $connect); |
|
| 170 | + } |
|
| 171 | + if (!$url) { |
|
| 172 | + $url = generer_url_entite($id, $type, $args, $ancre, $connect ?: null); |
|
| 173 | + } |
|
| 174 | + if (!$url) { |
|
| 175 | + return false; |
|
| 176 | + } |
|
| 177 | + if (is_array($url)) { |
|
| 178 | + [$type, $id] = array_pad($url, 2, null); |
|
| 179 | + $url = generer_url_entite($id, $type, $args, $ancre, $connect ?: null); |
|
| 180 | + } |
|
| 181 | + if ($pour === 'url') { |
|
| 182 | + return $url; |
|
| 183 | + } |
|
| 184 | + $r = traiter_raccourci_titre($id, $type, $connect); |
|
| 185 | + if ($r) { |
|
| 186 | + $r['class'] = ($type == 'site') ? 'spip_out' : 'spip_in'; |
|
| 187 | + } |
|
| 188 | + if ($texte = trim($texte)) { |
|
| 189 | + $r['titre'] = $texte; |
|
| 190 | + } |
|
| 191 | + if (!@$r['titre']) { |
|
| 192 | + $r['titre'] = _T($type) . " $id"; |
|
| 193 | + } |
|
| 194 | + if ($pour == 'titre') { |
|
| 195 | + return $r['titre']; |
|
| 196 | + } |
|
| 197 | + $r['url'] = $url; |
|
| 198 | + |
|
| 199 | + // dans le cas d'un lien vers un doc, ajouter le type='mime/type' |
|
| 200 | + if ( |
|
| 201 | + $type == 'document' |
|
| 202 | + and $mime = sql_getfetsel( |
|
| 203 | + 'mime_type', |
|
| 204 | + 'spip_types_documents', |
|
| 205 | + 'extension IN (' . sql_get_select('extension', 'spip_documents', 'id_document=' . sql_quote($id)) . ')', |
|
| 206 | + '', |
|
| 207 | + '', |
|
| 208 | + '', |
|
| 209 | + '', |
|
| 210 | + $connect |
|
| 211 | + ) |
|
| 212 | + ) { |
|
| 213 | + $r['mime'] = $mime; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + return $r; |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | // analyse des raccourcis issus de [TITRE->RACCOURCInnn] et connexes |
@@ -222,41 +222,41 @@ discard block |
||
| 222 | 222 | |
| 223 | 223 | // https://code.spip.net/@typer_raccourci |
| 224 | 224 | function typer_raccourci($lien) { |
| 225 | - if (!preg_match(_RACCOURCI_URL, $lien, $match)) { |
|
| 226 | - return []; |
|
| 227 | - } |
|
| 228 | - $f = $match[1]; |
|
| 229 | - // valeur par defaut et alias historiques |
|
| 230 | - if (!$f) { |
|
| 231 | - $f = 'article'; |
|
| 232 | - } else { |
|
| 233 | - if ($f == 'art') { |
|
| 234 | - $f = 'article'; |
|
| 235 | - } else { |
|
| 236 | - if ($f == 'br') { |
|
| 237 | - $f = 'breve'; |
|
| 238 | - } else { |
|
| 239 | - if ($f == 'rub') { |
|
| 240 | - $f = 'rubrique'; |
|
| 241 | - } else { |
|
| 242 | - if ($f == 'aut') { |
|
| 243 | - $f = 'auteur'; |
|
| 244 | - } else { |
|
| 245 | - if ($f == 'doc' or $f == 'im' or $f == 'img' or $f == 'image' or $f == 'emb') { |
|
| 246 | - $f = 'document'; |
|
| 247 | - } else { |
|
| 248 | - if (preg_match('/^br..?ve$/S', $f)) { |
|
| 249 | - $f = 'breve'; |
|
| 250 | - } |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - } # accents :( |
|
| 257 | - $match[0] = $f; |
|
| 258 | - |
|
| 259 | - return $match; |
|
| 225 | + if (!preg_match(_RACCOURCI_URL, $lien, $match)) { |
|
| 226 | + return []; |
|
| 227 | + } |
|
| 228 | + $f = $match[1]; |
|
| 229 | + // valeur par defaut et alias historiques |
|
| 230 | + if (!$f) { |
|
| 231 | + $f = 'article'; |
|
| 232 | + } else { |
|
| 233 | + if ($f == 'art') { |
|
| 234 | + $f = 'article'; |
|
| 235 | + } else { |
|
| 236 | + if ($f == 'br') { |
|
| 237 | + $f = 'breve'; |
|
| 238 | + } else { |
|
| 239 | + if ($f == 'rub') { |
|
| 240 | + $f = 'rubrique'; |
|
| 241 | + } else { |
|
| 242 | + if ($f == 'aut') { |
|
| 243 | + $f = 'auteur'; |
|
| 244 | + } else { |
|
| 245 | + if ($f == 'doc' or $f == 'im' or $f == 'img' or $f == 'image' or $f == 'emb') { |
|
| 246 | + $f = 'document'; |
|
| 247 | + } else { |
|
| 248 | + if (preg_match('/^br..?ve$/S', $f)) { |
|
| 249 | + $f = 'breve'; |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + } # accents :( |
|
| 257 | + $match[0] = $f; |
|
| 258 | + |
|
| 259 | + return $match; |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | /** |
@@ -271,25 +271,25 @@ discard block |
||
| 271 | 271 | * } |
| 272 | 272 | **/ |
| 273 | 273 | function traiter_raccourci_titre($id, $type, $connect = null) { |
| 274 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 275 | - $desc = $trouver_table(table_objet($type)); |
|
| 276 | - if (!($desc and $s = $desc['titre'])) { |
|
| 277 | - return []; |
|
| 278 | - } |
|
| 279 | - $_id = $desc['key']['PRIMARY KEY']; |
|
| 280 | - $r = sql_fetsel($s, $desc['table'], "$_id=$id", '', '', '', '', $connect); |
|
| 281 | - if (!$r) { |
|
| 282 | - return []; |
|
| 283 | - } |
|
| 284 | - $r['titre'] = supprimer_numero($r['titre']); |
|
| 285 | - if (!$r['titre'] and !empty($r['surnom'])) { |
|
| 286 | - $r['titre'] = $r['surnom']; |
|
| 287 | - } |
|
| 288 | - if (!isset($r['lang'])) { |
|
| 289 | - $r['lang'] = ''; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - return $r; |
|
| 274 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 275 | + $desc = $trouver_table(table_objet($type)); |
|
| 276 | + if (!($desc and $s = $desc['titre'])) { |
|
| 277 | + return []; |
|
| 278 | + } |
|
| 279 | + $_id = $desc['key']['PRIMARY KEY']; |
|
| 280 | + $r = sql_fetsel($s, $desc['table'], "$_id=$id", '', '', '', '', $connect); |
|
| 281 | + if (!$r) { |
|
| 282 | + return []; |
|
| 283 | + } |
|
| 284 | + $r['titre'] = supprimer_numero($r['titre']); |
|
| 285 | + if (!$r['titre'] and !empty($r['surnom'])) { |
|
| 286 | + $r['titre'] = $r['surnom']; |
|
| 287 | + } |
|
| 288 | + if (!isset($r['lang'])) { |
|
| 289 | + $r['lang'] = ''; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + return $r; |
|
| 293 | 293 | } |
| 294 | 294 | |
| 295 | 295 | // traite les modeles (dans la fonction typo), en remplacant |
@@ -301,124 +301,124 @@ discard block |
||
| 301 | 301 | // https://code.spip.net/@traiter_modeles |
| 302 | 302 | |
| 303 | 303 | define( |
| 304 | - '_RACCOURCI_MODELE', |
|
| 305 | - '(<([a-z_-]{3,})' # <modele |
|
| 306 | - . '\s*([0-9]*)\s*' # id |
|
| 307 | - . '([|](?:<[^<>]*>|[^>])*?)?' # |arguments (y compris des tags <...>) |
|
| 308 | - . '\s*/?' . '>)' # fin du modele > |
|
| 309 | - . '\s*(<\/a>)?' # eventuel </a> |
|
| 304 | + '_RACCOURCI_MODELE', |
|
| 305 | + '(<([a-z_-]{3,})' # <modele |
|
| 306 | + . '\s*([0-9]*)\s*' # id |
|
| 307 | + . '([|](?:<[^<>]*>|[^>])*?)?' # |arguments (y compris des tags <...>) |
|
| 308 | + . '\s*/?' . '>)' # fin du modele > |
|
| 309 | + . '\s*(<\/a>)?' # eventuel </a> |
|
| 310 | 310 | ); |
| 311 | 311 | |
| 312 | 312 | define('_RACCOURCI_MODELE_DEBUT', '@^' . _RACCOURCI_MODELE . '@isS'); |
| 313 | 313 | |
| 314 | 314 | // https://code.spip.net/@traiter_modeles |
| 315 | 315 | function traiter_modeles($texte, $doublons = false, $echap = '', string $connect = '', $liens = null, $env = []) { |
| 316 | - // preserver la compatibilite : true = recherche des documents |
|
| 317 | - if ($doublons === true) { |
|
| 318 | - $doublons = ['documents' => ['doc', 'emb', 'img']]; |
|
| 319 | - } |
|
| 320 | - // detecter les modeles (rapide) |
|
| 321 | - if ( |
|
| 322 | - strpos($texte, '<') !== false and |
|
| 323 | - preg_match_all('/<[a-z_-]{3,}\s*[0-9|]+/iS', $texte, $matches, PREG_SET_ORDER) |
|
| 324 | - ) { |
|
| 325 | - include_spip('public/assembler'); |
|
| 326 | - $wrap_embed_html = charger_fonction('wrap_embed_html', 'inc', true); |
|
| 327 | - foreach ($matches as $match) { |
|
| 328 | - // Recuperer l'appel complet (y compris un eventuel lien) |
|
| 329 | - |
|
| 330 | - $a = strpos($texte, (string) $match[0]); |
|
| 331 | - preg_match( |
|
| 332 | - _RACCOURCI_MODELE_DEBUT, |
|
| 333 | - substr($texte, $a), |
|
| 334 | - $regs |
|
| 335 | - ); |
|
| 336 | - $regs[] = ''; // s'assurer qu'il y a toujours un 5e arg, eventuellement vide |
|
| 337 | - [, $mod, $type, $id, $params, $fin] = $regs; |
|
| 338 | - if ( |
|
| 339 | - $fin and |
|
| 340 | - preg_match( |
|
| 341 | - '/<a\s[^<>]*>\s*$/i', |
|
| 342 | - substr($texte, 0, $a), |
|
| 343 | - $r |
|
| 344 | - ) |
|
| 345 | - ) { |
|
| 346 | - $lien = [ |
|
| 347 | - 'href' => extraire_attribut($r[0], 'href'), |
|
| 348 | - 'class' => extraire_attribut($r[0], 'class'), |
|
| 349 | - 'mime' => extraire_attribut($r[0], 'type'), |
|
| 350 | - 'title' => extraire_attribut($r[0], 'title'), |
|
| 351 | - 'hreflang' => extraire_attribut($r[0], 'hreflang') |
|
| 352 | - ]; |
|
| 353 | - $n = strlen($r[0]); |
|
| 354 | - $a -= $n; |
|
| 355 | - $cherche = $n + strlen($regs[0]); |
|
| 356 | - } else { |
|
| 357 | - $lien = false; |
|
| 358 | - $cherche = strlen($mod); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - // calculer le modele |
|
| 362 | - # hack indexation |
|
| 363 | - if ($doublons) { |
|
| 364 | - $texte .= preg_replace(',[|][^|=]*,s', ' ', $params); |
|
| 365 | - } # version normale |
|
| 366 | - else { |
|
| 367 | - // si un tableau de liens a ete passe, reinjecter le contenu d'origine |
|
| 368 | - // dans les parametres, plutot que les liens echappes |
|
| 369 | - if (!is_null($liens)) { |
|
| 370 | - $params = str_replace($liens[0], $liens[1], $params); |
|
| 371 | - } |
|
| 372 | - $modele = inclure_modele($type, $id, $params, $lien, $connect, $env); |
|
| 373 | - // en cas d'echec, |
|
| 374 | - // si l'objet demande a une url, |
|
| 375 | - // creer un petit encadre vers elle |
|
| 376 | - if ($modele === false) { |
|
| 377 | - if (!$lien) { |
|
| 378 | - $lien = traiter_lien_implicite("$type$id", '', 'tout', $connect); |
|
| 379 | - } |
|
| 380 | - if ($lien) { |
|
| 381 | - $modele = '<a href="' |
|
| 382 | - . $lien['url'] |
|
| 383 | - . '" class="spip_modele' |
|
| 384 | - . '">' |
|
| 385 | - . sinon($lien['titre'], _T('ecrire:info_sans_titre')) |
|
| 386 | - . '</a>'; |
|
| 387 | - } else { |
|
| 388 | - $modele = ''; |
|
| 389 | - if (test_espace_prive()) { |
|
| 390 | - $modele = entites_html(substr($texte, $a, $cherche)); |
|
| 391 | - if (!is_null($liens)) { |
|
| 392 | - $modele = '<pre>' . str_replace($liens[0], $liens[1], $modele) . '</pre>'; |
|
| 393 | - } |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - } |
|
| 397 | - // le remplacer dans le texte |
|
| 398 | - if ($modele !== false) { |
|
| 399 | - $modele = protege_js_modeles($modele); |
|
| 400 | - if ($wrap_embed_html) { |
|
| 401 | - $modele = $wrap_embed_html($mod, $modele); |
|
| 402 | - } |
|
| 403 | - $rempl = code_echappement($modele, $echap); |
|
| 404 | - $texte = substr($texte, 0, $a) |
|
| 405 | - . $rempl |
|
| 406 | - . substr($texte, $a + $cherche); |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - // hack pour tout l'espace prive |
|
| 411 | - if (((!_DIR_RESTREINT) or ($doublons)) and ($id)) { |
|
| 412 | - foreach ($doublons ?: ['documents' => ['doc', 'emb', 'img']] as $quoi => $modeles) { |
|
| 413 | - if (in_array($type, $modeles)) { |
|
| 414 | - $GLOBALS["doublons_{$quoi}_inclus"][] = $id; |
|
| 415 | - } |
|
| 416 | - } |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - return $texte; |
|
| 316 | + // preserver la compatibilite : true = recherche des documents |
|
| 317 | + if ($doublons === true) { |
|
| 318 | + $doublons = ['documents' => ['doc', 'emb', 'img']]; |
|
| 319 | + } |
|
| 320 | + // detecter les modeles (rapide) |
|
| 321 | + if ( |
|
| 322 | + strpos($texte, '<') !== false and |
|
| 323 | + preg_match_all('/<[a-z_-]{3,}\s*[0-9|]+/iS', $texte, $matches, PREG_SET_ORDER) |
|
| 324 | + ) { |
|
| 325 | + include_spip('public/assembler'); |
|
| 326 | + $wrap_embed_html = charger_fonction('wrap_embed_html', 'inc', true); |
|
| 327 | + foreach ($matches as $match) { |
|
| 328 | + // Recuperer l'appel complet (y compris un eventuel lien) |
|
| 329 | + |
|
| 330 | + $a = strpos($texte, (string) $match[0]); |
|
| 331 | + preg_match( |
|
| 332 | + _RACCOURCI_MODELE_DEBUT, |
|
| 333 | + substr($texte, $a), |
|
| 334 | + $regs |
|
| 335 | + ); |
|
| 336 | + $regs[] = ''; // s'assurer qu'il y a toujours un 5e arg, eventuellement vide |
|
| 337 | + [, $mod, $type, $id, $params, $fin] = $regs; |
|
| 338 | + if ( |
|
| 339 | + $fin and |
|
| 340 | + preg_match( |
|
| 341 | + '/<a\s[^<>]*>\s*$/i', |
|
| 342 | + substr($texte, 0, $a), |
|
| 343 | + $r |
|
| 344 | + ) |
|
| 345 | + ) { |
|
| 346 | + $lien = [ |
|
| 347 | + 'href' => extraire_attribut($r[0], 'href'), |
|
| 348 | + 'class' => extraire_attribut($r[0], 'class'), |
|
| 349 | + 'mime' => extraire_attribut($r[0], 'type'), |
|
| 350 | + 'title' => extraire_attribut($r[0], 'title'), |
|
| 351 | + 'hreflang' => extraire_attribut($r[0], 'hreflang') |
|
| 352 | + ]; |
|
| 353 | + $n = strlen($r[0]); |
|
| 354 | + $a -= $n; |
|
| 355 | + $cherche = $n + strlen($regs[0]); |
|
| 356 | + } else { |
|
| 357 | + $lien = false; |
|
| 358 | + $cherche = strlen($mod); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + // calculer le modele |
|
| 362 | + # hack indexation |
|
| 363 | + if ($doublons) { |
|
| 364 | + $texte .= preg_replace(',[|][^|=]*,s', ' ', $params); |
|
| 365 | + } # version normale |
|
| 366 | + else { |
|
| 367 | + // si un tableau de liens a ete passe, reinjecter le contenu d'origine |
|
| 368 | + // dans les parametres, plutot que les liens echappes |
|
| 369 | + if (!is_null($liens)) { |
|
| 370 | + $params = str_replace($liens[0], $liens[1], $params); |
|
| 371 | + } |
|
| 372 | + $modele = inclure_modele($type, $id, $params, $lien, $connect, $env); |
|
| 373 | + // en cas d'echec, |
|
| 374 | + // si l'objet demande a une url, |
|
| 375 | + // creer un petit encadre vers elle |
|
| 376 | + if ($modele === false) { |
|
| 377 | + if (!$lien) { |
|
| 378 | + $lien = traiter_lien_implicite("$type$id", '', 'tout', $connect); |
|
| 379 | + } |
|
| 380 | + if ($lien) { |
|
| 381 | + $modele = '<a href="' |
|
| 382 | + . $lien['url'] |
|
| 383 | + . '" class="spip_modele' |
|
| 384 | + . '">' |
|
| 385 | + . sinon($lien['titre'], _T('ecrire:info_sans_titre')) |
|
| 386 | + . '</a>'; |
|
| 387 | + } else { |
|
| 388 | + $modele = ''; |
|
| 389 | + if (test_espace_prive()) { |
|
| 390 | + $modele = entites_html(substr($texte, $a, $cherche)); |
|
| 391 | + if (!is_null($liens)) { |
|
| 392 | + $modele = '<pre>' . str_replace($liens[0], $liens[1], $modele) . '</pre>'; |
|
| 393 | + } |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + } |
|
| 397 | + // le remplacer dans le texte |
|
| 398 | + if ($modele !== false) { |
|
| 399 | + $modele = protege_js_modeles($modele); |
|
| 400 | + if ($wrap_embed_html) { |
|
| 401 | + $modele = $wrap_embed_html($mod, $modele); |
|
| 402 | + } |
|
| 403 | + $rempl = code_echappement($modele, $echap); |
|
| 404 | + $texte = substr($texte, 0, $a) |
|
| 405 | + . $rempl |
|
| 406 | + . substr($texte, $a + $cherche); |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + // hack pour tout l'espace prive |
|
| 411 | + if (((!_DIR_RESTREINT) or ($doublons)) and ($id)) { |
|
| 412 | + foreach ($doublons ?: ['documents' => ['doc', 'emb', 'img']] as $quoi => $modeles) { |
|
| 413 | + if (in_array($type, $modeles)) { |
|
| 414 | + $GLOBALS["doublons_{$quoi}_inclus"][] = $id; |
|
| 415 | + } |
|
| 416 | + } |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + return $texte; |
|
| 422 | 422 | } |
| 423 | 423 | |
| 424 | 424 | // |
@@ -426,15 +426,15 @@ discard block |
||
| 426 | 426 | // |
| 427 | 427 | // https://code.spip.net/@traiter_raccourci_ancre |
| 428 | 428 | function traiter_raccourci_ancre($letexte) { |
| 429 | - return $letexte; |
|
| 429 | + return $letexte; |
|
| 430 | 430 | } |
| 431 | 431 | |
| 432 | 432 | // https://code.spip.net/@traiter_raccourci_glossaire |
| 433 | 433 | function traiter_raccourci_glossaire($texte) { |
| 434 | - return $texte; |
|
| 434 | + return $texte; |
|
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | // https://code.spip.net/@glossaire_std |
| 438 | 438 | function glossaire_std($terme) { |
| 439 | - return $terme; |
|
| 439 | + return $terme; |
|
| 440 | 440 | } |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | return $r ?: traiter_lien_explicite($ref, $texte, $pour, $connect, $echappe_typo); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | -define('_EXTRAIRE_LIEN', ',^\s*(?:' . _PROTOCOLES_STD . '):?/?/?\s*$,iS'); |
|
| 105 | +define('_EXTRAIRE_LIEN', ',^\s*(?:'._PROTOCOLES_STD.'):?/?/?\s*$,iS'); |
|
| 106 | 106 | |
| 107 | 107 | // https://code.spip.net/@traiter_lien_explicite |
| 108 | 108 | function traiter_lien_explicite($ref, $texte = '', $pour = 'url', string $connect = '', $echappe_typo = true) { |
@@ -119,19 +119,19 @@ discard block |
||
| 119 | 119 | $lien_court = charger_fonction('lien_court', 'inc'); |
| 120 | 120 | $texte = $lien_court($texte); |
| 121 | 121 | if ($echappe_typo) { |
| 122 | - $texte = '<html>' . quote_amp($texte) . '</html>'; |
|
| 122 | + $texte = '<html>'.quote_amp($texte).'</html>'; |
|
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | // petites corrections d'URL |
| 127 | 127 | if (preg_match('/^www\.[^@]+$/S', $lien)) { |
| 128 | - $lien = 'http://' . $lien; |
|
| 128 | + $lien = 'http://'.$lien; |
|
| 129 | 129 | } else { |
| 130 | 130 | if (strpos($lien, '@') && email_valide($lien)) { |
| 131 | 131 | if (!$texte) { |
| 132 | 132 | $texte = $lien; |
| 133 | 133 | } |
| 134 | - $lien = 'mailto:' . $lien; |
|
| 134 | + $lien = 'mailto:'.$lien; |
|
| 135 | 135 | } |
| 136 | 136 | } |
| 137 | 137 | |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | function liens_implicite_glose_dist($texte, $id, $type, $args, $ancre, string $connect = '') { |
| 150 | - if (function_exists($f = 'glossaire_' . $ancre)) { |
|
| 150 | + if (function_exists($f = 'glossaire_'.$ancre)) { |
|
| 151 | 151 | $url = $f($texte, $id); |
| 152 | 152 | } else { |
| 153 | 153 | $url = glossaire_std($texte); |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | if (!($match = typer_raccourci($ref))) { |
| 163 | 163 | return false; |
| 164 | 164 | } |
| 165 | - [$type, , $id, , $args, , $ancre] = array_pad($match, 7, null); |
|
| 165 | + [$type,, $id,, $args,, $ancre] = array_pad($match, 7, null); |
|
| 166 | 166 | // attention dans le cas des sites le lien doit pointer non pas sur |
| 167 | 167 | // la page locale du site, mais directement sur le site lui-meme |
| 168 | 168 | if ($f = charger_fonction("implicite_$type", 'liens', true)) { |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | $r['titre'] = $texte; |
| 190 | 190 | } |
| 191 | 191 | if (!@$r['titre']) { |
| 192 | - $r['titre'] = _T($type) . " $id"; |
|
| 192 | + $r['titre'] = _T($type)." $id"; |
|
| 193 | 193 | } |
| 194 | 194 | if ($pour == 'titre') { |
| 195 | 195 | return $r['titre']; |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | and $mime = sql_getfetsel( |
| 203 | 203 | 'mime_type', |
| 204 | 204 | 'spip_types_documents', |
| 205 | - 'extension IN (' . sql_get_select('extension', 'spip_documents', 'id_document=' . sql_quote($id)) . ')', |
|
| 205 | + 'extension IN ('.sql_get_select('extension', 'spip_documents', 'id_document='.sql_quote($id)).')', |
|
| 206 | 206 | '', |
| 207 | 207 | '', |
| 208 | 208 | '', |
@@ -305,11 +305,11 @@ discard block |
||
| 305 | 305 | '(<([a-z_-]{3,})' # <modele |
| 306 | 306 | . '\s*([0-9]*)\s*' # id |
| 307 | 307 | . '([|](?:<[^<>]*>|[^>])*?)?' # |arguments (y compris des tags <...>) |
| 308 | - . '\s*/?' . '>)' # fin du modele > |
|
| 308 | + . '\s*/?'.'>)' # fin du modele > |
|
| 309 | 309 | . '\s*(<\/a>)?' # eventuel </a> |
| 310 | 310 | ); |
| 311 | 311 | |
| 312 | -define('_RACCOURCI_MODELE_DEBUT', '@^' . _RACCOURCI_MODELE . '@isS'); |
|
| 312 | +define('_RACCOURCI_MODELE_DEBUT', '@^'._RACCOURCI_MODELE.'@isS'); |
|
| 313 | 313 | |
| 314 | 314 | // https://code.spip.net/@traiter_modeles |
| 315 | 315 | function traiter_modeles($texte, $doublons = false, $echap = '', string $connect = '', $liens = null, $env = []) { |
@@ -389,7 +389,7 @@ discard block |
||
| 389 | 389 | if (test_espace_prive()) { |
| 390 | 390 | $modele = entites_html(substr($texte, $a, $cherche)); |
| 391 | 391 | if (!is_null($liens)) { |
| 392 | - $modele = '<pre>' . str_replace($liens[0], $liens[1], $modele) . '</pre>'; |
|
| 392 | + $modele = '<pre>'.str_replace($liens[0], $liens[1], $modele).'</pre>'; |
|
| 393 | 393 | } |
| 394 | 394 | } |
| 395 | 395 | } |