@@ -1,7 +1,7 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | include_spip('inc/bandeau'); |
@@ -19,7 +19,7 @@ |
||
| 19 | 19 | **/ |
| 20 | 20 | |
| 21 | 21 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 22 | - return; |
|
| 22 | + return; |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | load_charset('iso-8859-1'); |
@@ -21,7 +21,7 @@ |
||
| 21 | 21 | **/ |
| 22 | 22 | |
| 23 | 23 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 24 | - return; |
|
| 24 | + return; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | include_spip('base/objets'); |
@@ -616,10 +616,12 @@ |
||
| 616 | 616 | function spip_pg_groupby($groupby, $from, $select) { |
| 617 | 617 | $join = strpos($from, ","); |
| 618 | 618 | // ismplifier avant de decouper |
| 619 | - if (is_string($select)) // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 619 | + if (is_string($select)) { |
|
| 620 | + // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 620 | 621 | { |
| 621 | 622 | $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
| 622 | 623 | } |
| 624 | + } |
|
| 623 | 625 | |
| 624 | 626 | if ($join or $groupby) { |
| 625 | 627 | $join = is_array($select) ? $select : explode(", ", $select); |
@@ -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,128 +32,128 @@ 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 = array(); |
|
| 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 (array('addr', 'port', 'login', 'pass', 'prefixe') as $a) { |
|
| 43 | - $$a = $last_connect[$a]; |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - @list($host, $p) = explode(';', $addr); |
|
| 47 | - if ($p > 0) { |
|
| 48 | - $port = " port=$p"; |
|
| 49 | - } else { |
|
| 50 | - $port = ''; |
|
| 51 | - } |
|
| 52 | - $erreurs = array(); |
|
| 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 = array( |
|
| 76 | - 'addr' => $addr, |
|
| 77 | - 'port' => $port, |
|
| 78 | - 'login' => $login, |
|
| 79 | - 'pass' => $pass, |
|
| 80 | - 'db' => $db, |
|
| 81 | - 'prefixe' => $prefixe, |
|
| 82 | - ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - spip_log("Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 86 | - 'pg.' . _LOG_DEBUG); |
|
| 87 | - |
|
| 88 | - return !$link ? false : array( |
|
| 89 | - 'db' => $db, |
|
| 90 | - 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 91 | - 'link' => $link, |
|
| 92 | - ); |
|
| 35 | + static $last_connect = array(); |
|
| 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 (array('addr', 'port', 'login', 'pass', 'prefixe') as $a) { |
|
| 43 | + $$a = $last_connect[$a]; |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + @list($host, $p) = explode(';', $addr); |
|
| 47 | + if ($p > 0) { |
|
| 48 | + $port = " port=$p"; |
|
| 49 | + } else { |
|
| 50 | + $port = ''; |
|
| 51 | + } |
|
| 52 | + $erreurs = array(); |
|
| 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 = array( |
|
| 76 | + 'addr' => $addr, |
|
| 77 | + 'port' => $port, |
|
| 78 | + 'login' => $login, |
|
| 79 | + 'pass' => $pass, |
|
| 80 | + 'db' => $db, |
|
| 81 | + 'prefixe' => $prefixe, |
|
| 82 | + ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + spip_log("Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 86 | + 'pg.' . _LOG_DEBUG); |
|
| 87 | + |
|
| 88 | + return !$link ? false : array( |
|
| 89 | + 'db' => $db, |
|
| 90 | + 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 91 | + 'link' => $link, |
|
| 92 | + ); |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | $GLOBALS['spip_pg_functions_1'] = array( |
| 96 | - 'alter' => 'spip_pg_alter', |
|
| 97 | - 'count' => 'spip_pg_count', |
|
| 98 | - 'countsel' => 'spip_pg_countsel', |
|
| 99 | - 'create' => 'spip_pg_create', |
|
| 100 | - 'create_base' => 'spip_pg_create_base', |
|
| 101 | - 'create_view' => 'spip_pg_create_view', |
|
| 102 | - 'date_proche' => 'spip_pg_date_proche', |
|
| 103 | - 'delete' => 'spip_pg_delete', |
|
| 104 | - 'drop_table' => 'spip_pg_drop_table', |
|
| 105 | - 'drop_view' => 'spip_pg_drop_view', |
|
| 106 | - 'errno' => 'spip_pg_errno', |
|
| 107 | - 'error' => 'spip_pg_error', |
|
| 108 | - 'explain' => 'spip_pg_explain', |
|
| 109 | - 'fetch' => 'spip_pg_fetch', |
|
| 110 | - 'seek' => 'spip_pg_seek', |
|
| 111 | - 'free' => 'spip_pg_free', |
|
| 112 | - 'hex' => 'spip_pg_hex', |
|
| 113 | - 'in' => 'spip_pg_in', |
|
| 114 | - 'insert' => 'spip_pg_insert', |
|
| 115 | - 'insertq' => 'spip_pg_insertq', |
|
| 116 | - 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 117 | - 'listdbs' => 'spip_pg_listdbs', |
|
| 118 | - 'multi' => 'spip_pg_multi', |
|
| 119 | - 'optimize' => 'spip_pg_optimize', |
|
| 120 | - 'query' => 'spip_pg_query', |
|
| 121 | - 'quote' => 'spip_pg_quote', |
|
| 122 | - 'replace' => 'spip_pg_replace', |
|
| 123 | - 'replace_multi' => 'spip_pg_replace_multi', |
|
| 124 | - 'select' => 'spip_pg_select', |
|
| 125 | - 'selectdb' => 'spip_pg_selectdb', |
|
| 126 | - 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 127 | - 'showbase' => 'spip_pg_showbase', |
|
| 128 | - 'showtable' => 'spip_pg_showtable', |
|
| 129 | - 'update' => 'spip_pg_update', |
|
| 130 | - 'updateq' => 'spip_pg_updateq', |
|
| 96 | + 'alter' => 'spip_pg_alter', |
|
| 97 | + 'count' => 'spip_pg_count', |
|
| 98 | + 'countsel' => 'spip_pg_countsel', |
|
| 99 | + 'create' => 'spip_pg_create', |
|
| 100 | + 'create_base' => 'spip_pg_create_base', |
|
| 101 | + 'create_view' => 'spip_pg_create_view', |
|
| 102 | + 'date_proche' => 'spip_pg_date_proche', |
|
| 103 | + 'delete' => 'spip_pg_delete', |
|
| 104 | + 'drop_table' => 'spip_pg_drop_table', |
|
| 105 | + 'drop_view' => 'spip_pg_drop_view', |
|
| 106 | + 'errno' => 'spip_pg_errno', |
|
| 107 | + 'error' => 'spip_pg_error', |
|
| 108 | + 'explain' => 'spip_pg_explain', |
|
| 109 | + 'fetch' => 'spip_pg_fetch', |
|
| 110 | + 'seek' => 'spip_pg_seek', |
|
| 111 | + 'free' => 'spip_pg_free', |
|
| 112 | + 'hex' => 'spip_pg_hex', |
|
| 113 | + 'in' => 'spip_pg_in', |
|
| 114 | + 'insert' => 'spip_pg_insert', |
|
| 115 | + 'insertq' => 'spip_pg_insertq', |
|
| 116 | + 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 117 | + 'listdbs' => 'spip_pg_listdbs', |
|
| 118 | + 'multi' => 'spip_pg_multi', |
|
| 119 | + 'optimize' => 'spip_pg_optimize', |
|
| 120 | + 'query' => 'spip_pg_query', |
|
| 121 | + 'quote' => 'spip_pg_quote', |
|
| 122 | + 'replace' => 'spip_pg_replace', |
|
| 123 | + 'replace_multi' => 'spip_pg_replace_multi', |
|
| 124 | + 'select' => 'spip_pg_select', |
|
| 125 | + 'selectdb' => 'spip_pg_selectdb', |
|
| 126 | + 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 127 | + 'showbase' => 'spip_pg_showbase', |
|
| 128 | + 'showtable' => 'spip_pg_showtable', |
|
| 129 | + 'update' => 'spip_pg_update', |
|
| 130 | + 'updateq' => 'spip_pg_updateq', |
|
| 131 | 131 | ); |
| 132 | 132 | |
| 133 | 133 | // Par ou ca passe une fois les traductions faites |
| 134 | 134 | // https://code.spip.net/@spip_pg_trace_query |
| 135 | 135 | function spip_pg_trace_query($query, $serveur = '') { |
| 136 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 137 | - $prefixe = $connexion['prefixe']; |
|
| 138 | - $link = $connexion['link']; |
|
| 139 | - $db = $connexion['db']; |
|
| 140 | - |
|
| 141 | - if (isset($_GET['var_profile'])) { |
|
| 142 | - include_spip('public/tracer'); |
|
| 143 | - $t = trace_query_start(); |
|
| 144 | - $e = ''; |
|
| 145 | - } else { |
|
| 146 | - $t = 0; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $connexion['last'] = $query; |
|
| 150 | - $r = spip_pg_query_simple($link, $query); |
|
| 151 | - |
|
| 152 | - // Log de l'erreur eventuelle |
|
| 153 | - if ($e = spip_pg_errno($serveur)) { |
|
| 154 | - $e .= spip_pg_error($query, $serveur); |
|
| 155 | - } // et du fautif |
|
| 156 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 136 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 137 | + $prefixe = $connexion['prefixe']; |
|
| 138 | + $link = $connexion['link']; |
|
| 139 | + $db = $connexion['db']; |
|
| 140 | + |
|
| 141 | + if (isset($_GET['var_profile'])) { |
|
| 142 | + include_spip('public/tracer'); |
|
| 143 | + $t = trace_query_start(); |
|
| 144 | + $e = ''; |
|
| 145 | + } else { |
|
| 146 | + $t = 0; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $connexion['last'] = $query; |
|
| 150 | + $r = spip_pg_query_simple($link, $query); |
|
| 151 | + |
|
| 152 | + // Log de l'erreur eventuelle |
|
| 153 | + if ($e = spip_pg_errno($serveur)) { |
|
| 154 | + $e .= spip_pg_error($query, $serveur); |
|
| 155 | + } // et du fautif |
|
| 156 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | // Fonction de requete generale quand on est sur que c'est SQL standard. |
@@ -161,30 +161,30 @@ discard block |
||
| 161 | 161 | |
| 162 | 162 | // https://code.spip.net/@spip_pg_query |
| 163 | 163 | function spip_pg_query($query, $serveur = '', $requeter = true) { |
| 164 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 165 | - $prefixe = $connexion['prefixe']; |
|
| 166 | - $link = $connexion['link']; |
|
| 167 | - $db = $connexion['db']; |
|
| 168 | - |
|
| 169 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 170 | - $suite = strstr($query, $regs[0]); |
|
| 171 | - $query = substr($query, 0, -strlen($suite)); |
|
| 172 | - } else { |
|
| 173 | - $suite = ''; |
|
| 174 | - } |
|
| 175 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 176 | - |
|
| 177 | - // renvoyer la requete inerte si demandee |
|
| 178 | - if (!$requeter) { |
|
| 179 | - return $query; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return spip_pg_trace_query($query, $serveur); |
|
| 164 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 165 | + $prefixe = $connexion['prefixe']; |
|
| 166 | + $link = $connexion['link']; |
|
| 167 | + $db = $connexion['db']; |
|
| 168 | + |
|
| 169 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 170 | + $suite = strstr($query, $regs[0]); |
|
| 171 | + $query = substr($query, 0, -strlen($suite)); |
|
| 172 | + } else { |
|
| 173 | + $suite = ''; |
|
| 174 | + } |
|
| 175 | + $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 176 | + |
|
| 177 | + // renvoyer la requete inerte si demandee |
|
| 178 | + if (!$requeter) { |
|
| 179 | + return $query; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return spip_pg_trace_query($query, $serveur); |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | function spip_pg_query_simple($link, $query) { |
| 186 | - #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 187 | - return pg_query($link, $query); |
|
| 186 | + #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 187 | + return pg_query($link, $query); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | /* |
@@ -196,198 +196,198 @@ discard block |
||
| 196 | 196 | * de requetes showtable intempestives |
| 197 | 197 | */ |
| 198 | 198 | function spip_pg_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 199 | - static $tables = array(); |
|
| 200 | - |
|
| 201 | - if (!isset($tables[$table])) { |
|
| 202 | - |
|
| 203 | - if (!$desc) { |
|
| 204 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 205 | - $desc = $trouver_table($table, $serveur); |
|
| 206 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 207 | - if (!$desc) { |
|
| 208 | - return $couples; |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 213 | - // cependant, il faudra peut etre etendre |
|
| 214 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 215 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 216 | - $tables[$table] = array(); |
|
| 217 | - foreach ($desc['field'] as $k => $v) { |
|
| 218 | - $v = strtolower(ltrim($v)); |
|
| 219 | - // ne pas ajouter de timestamp now() si un default est specifie |
|
| 220 | - if (strpos($v, 'timestamp') === 0 and strpos($v, 'default') === false) { |
|
| 221 | - $tables[$table][] = $k; |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // ajout des champs type 'timestamp' absents |
|
| 227 | - foreach ($tables[$table] as $maj) { |
|
| 228 | - if (!array_key_exists($maj, $couples)) { |
|
| 229 | - $couples[$maj] = "NOW()"; |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - return $couples; |
|
| 199 | + static $tables = array(); |
|
| 200 | + |
|
| 201 | + if (!isset($tables[$table])) { |
|
| 202 | + |
|
| 203 | + if (!$desc) { |
|
| 204 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 205 | + $desc = $trouver_table($table, $serveur); |
|
| 206 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 207 | + if (!$desc) { |
|
| 208 | + return $couples; |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 213 | + // cependant, il faudra peut etre etendre |
|
| 214 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 215 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 216 | + $tables[$table] = array(); |
|
| 217 | + foreach ($desc['field'] as $k => $v) { |
|
| 218 | + $v = strtolower(ltrim($v)); |
|
| 219 | + // ne pas ajouter de timestamp now() si un default est specifie |
|
| 220 | + if (strpos($v, 'timestamp') === 0 and strpos($v, 'default') === false) { |
|
| 221 | + $tables[$table][] = $k; |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // ajout des champs type 'timestamp' absents |
|
| 227 | + foreach ($tables[$table] as $maj) { |
|
| 228 | + if (!array_key_exists($maj, $couples)) { |
|
| 229 | + $couples[$maj] = "NOW()"; |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + return $couples; |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | |
| 237 | 237 | // Alter en PG ne traite pas les index |
| 238 | 238 | // https://code.spip.net/@spip_pg_alter |
| 239 | 239 | function spip_pg_alter($query, $serveur = '', $requeter = true) { |
| 240 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 241 | - // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 242 | - // ou revoir l'api de sql_alter en creant un |
|
| 243 | - // sql_alter_table($table,array($actions)); |
|
| 244 | - if (!preg_match("/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is", $query, $regs)) { |
|
| 245 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 246 | - |
|
| 247 | - return false; |
|
| 248 | - } |
|
| 249 | - $debut = $regs[1]; |
|
| 250 | - $table = $regs[3]; |
|
| 251 | - $suite = $regs[4]; |
|
| 252 | - $todo = explode(',', $suite); |
|
| 253 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 254 | - $todo2 = array(); |
|
| 255 | - $i = 0; |
|
| 256 | - $ouverte = false; |
|
| 257 | - while ($do = array_shift($todo)) { |
|
| 258 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . "," . $do : $do; |
|
| 259 | - $o = (false !== strpos($do, "(")); |
|
| 260 | - $f = (false !== strpos($do, ")")); |
|
| 261 | - if ($o and !$f) { |
|
| 262 | - $ouverte = true; |
|
| 263 | - } elseif ($f) { |
|
| 264 | - $ouverte = false; |
|
| 265 | - } |
|
| 266 | - if (!$ouverte) { |
|
| 267 | - $i++; |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - $todo = $todo2; |
|
| 271 | - $query = $debut . ' ' . array_shift($todo); |
|
| 272 | - |
|
| 273 | - if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 274 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 275 | - } else { |
|
| 276 | - if ($r[1]) { |
|
| 277 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 278 | - } |
|
| 279 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 280 | - if (function_exists($f)) { |
|
| 281 | - $f($r[2], $r[4], $serveur, $requeter); |
|
| 282 | - } else { |
|
| 283 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - // Alter a plusieurs args. Faudrait optimiser. |
|
| 287 | - if ($todo) { |
|
| 288 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 289 | - } |
|
| 240 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 241 | + // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 242 | + // ou revoir l'api de sql_alter en creant un |
|
| 243 | + // sql_alter_table($table,array($actions)); |
|
| 244 | + if (!preg_match("/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is", $query, $regs)) { |
|
| 245 | + spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 246 | + |
|
| 247 | + return false; |
|
| 248 | + } |
|
| 249 | + $debut = $regs[1]; |
|
| 250 | + $table = $regs[3]; |
|
| 251 | + $suite = $regs[4]; |
|
| 252 | + $todo = explode(',', $suite); |
|
| 253 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 254 | + $todo2 = array(); |
|
| 255 | + $i = 0; |
|
| 256 | + $ouverte = false; |
|
| 257 | + while ($do = array_shift($todo)) { |
|
| 258 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . "," . $do : $do; |
|
| 259 | + $o = (false !== strpos($do, "(")); |
|
| 260 | + $f = (false !== strpos($do, ")")); |
|
| 261 | + if ($o and !$f) { |
|
| 262 | + $ouverte = true; |
|
| 263 | + } elseif ($f) { |
|
| 264 | + $ouverte = false; |
|
| 265 | + } |
|
| 266 | + if (!$ouverte) { |
|
| 267 | + $i++; |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + $todo = $todo2; |
|
| 271 | + $query = $debut . ' ' . array_shift($todo); |
|
| 272 | + |
|
| 273 | + if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 274 | + spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 275 | + } else { |
|
| 276 | + if ($r[1]) { |
|
| 277 | + spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 278 | + } |
|
| 279 | + $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 280 | + if (function_exists($f)) { |
|
| 281 | + $f($r[2], $r[4], $serveur, $requeter); |
|
| 282 | + } else { |
|
| 283 | + spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + // Alter a plusieurs args. Faudrait optimiser. |
|
| 287 | + if ($todo) { |
|
| 288 | + spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 289 | + } |
|
| 290 | 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 | - list(, $old, $new, $type, $default, $null, $def2) = $r; |
|
| 299 | - $actions = array("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 ? $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 | + list(, $old, $new, $type, $default, $null, $def2) = $r; |
|
| 299 | + $actions = array("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 ? $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 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 323 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 324 | - |
|
| 325 | - return null; |
|
| 326 | - } |
|
| 327 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 328 | - preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 329 | - if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 330 | - $m[2] = $n[1]; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 334 | - } elseif ($r[1][0] == 'P') { |
|
| 335 | - // la primary peut etre sur plusieurs champs |
|
| 336 | - $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 337 | - $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 338 | - |
|
| 339 | - return spip_pg_query("ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', $serveur, |
|
| 340 | - $requeter); |
|
| 341 | - } else { |
|
| 342 | - preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 343 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 344 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 345 | - // |
|
| 346 | - // nom_index (colonnes) |
|
| 347 | - if ($m[2]) { |
|
| 348 | - $colonnes = substr($m[2], 1, -1); |
|
| 349 | - $nom_index = $m[1]; |
|
| 350 | - } else { |
|
| 351 | - // (colonne) |
|
| 352 | - if ($m[1][0] == "(") { |
|
| 353 | - $colonnes = substr($m[1], 1, -1); |
|
| 354 | - if (false !== strpos(",", $colonnes)) { |
|
| 355 | - spip_log("PG : Erreur, impossible de creer un index sur plusieurs colonnes" |
|
| 356 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 357 | - } else { |
|
| 358 | - $nom_index = $colonnes; |
|
| 359 | - } |
|
| 360 | - } // nom_index |
|
| 361 | - else { |
|
| 362 | - $nom_index = $colonnes = $m[1]; |
|
| 363 | - } |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 367 | - } |
|
| 322 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 323 | + spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 324 | + |
|
| 325 | + return null; |
|
| 326 | + } |
|
| 327 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 328 | + preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 329 | + if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 330 | + $m[2] = $n[1]; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 334 | + } elseif ($r[1][0] == 'P') { |
|
| 335 | + // la primary peut etre sur plusieurs champs |
|
| 336 | + $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 337 | + $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 338 | + |
|
| 339 | + return spip_pg_query("ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', $serveur, |
|
| 340 | + $requeter); |
|
| 341 | + } else { |
|
| 342 | + preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 343 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 344 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 345 | + // |
|
| 346 | + // nom_index (colonnes) |
|
| 347 | + if ($m[2]) { |
|
| 348 | + $colonnes = substr($m[2], 1, -1); |
|
| 349 | + $nom_index = $m[1]; |
|
| 350 | + } else { |
|
| 351 | + // (colonne) |
|
| 352 | + if ($m[1][0] == "(") { |
|
| 353 | + $colonnes = substr($m[1], 1, -1); |
|
| 354 | + if (false !== strpos(",", $colonnes)) { |
|
| 355 | + spip_log("PG : Erreur, impossible de creer un index sur plusieurs colonnes" |
|
| 356 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 357 | + } else { |
|
| 358 | + $nom_index = $colonnes; |
|
| 359 | + } |
|
| 360 | + } // nom_index |
|
| 361 | + else { |
|
| 362 | + $nom_index = $colonnes = $m[1]; |
|
| 363 | + } |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 367 | + } |
|
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | // https://code.spip.net/@spip_pg_alter_drop |
| 371 | 371 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 372 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 373 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 374 | - } else { |
|
| 375 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 376 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 377 | - } elseif ($r[1][0] == 'P') { |
|
| 378 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 379 | - } else { |
|
| 380 | - return spip_pg_query("DROP INDEX " . $table . '_' . $r[2], $serveur); |
|
| 381 | - } |
|
| 382 | - } |
|
| 372 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 373 | + spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 374 | + } else { |
|
| 375 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 376 | + return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 377 | + } elseif ($r[1][0] == 'P') { |
|
| 378 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 379 | + } else { |
|
| 380 | + return spip_pg_query("DROP INDEX " . $table . '_' . $r[2], $serveur); |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 386 | - if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 387 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 388 | - } else { |
|
| 389 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 390 | - } |
|
| 386 | + if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 387 | + spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 388 | + } else { |
|
| 389 | + return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 390 | + } |
|
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | // attention (en pg) : |
@@ -395,17 +395,17 @@ discard block |
||
| 395 | 395 | // - alter table A rename X to Y = changer le nom de la colonne X en Y |
| 396 | 396 | // pour l'instant, traiter simplement RENAME TO X |
| 397 | 397 | function spip_pg_alter_rename($table, $arg, $serveur = '', $requeter = true) { |
| 398 | - $rename = ""; |
|
| 399 | - // si TO, mais pas au debut |
|
| 400 | - if (!stripos($arg, 'TO ')) { |
|
| 401 | - $rename = $arg; |
|
| 402 | - } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 403 | - $rename = $r[2]; |
|
| 404 | - } else { |
|
| 405 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 398 | + $rename = ""; |
|
| 399 | + // si TO, mais pas au debut |
|
| 400 | + if (!stripos($arg, 'TO ')) { |
|
| 401 | + $rename = $arg; |
|
| 402 | + } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 403 | + $rename = $r[2]; |
|
| 404 | + } else { |
|
| 405 | + spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 409 | 409 | } |
| 410 | 410 | |
| 411 | 411 | |
@@ -421,58 +421,58 @@ discard block |
||
| 421 | 421 | * @return bool ou requete |
| 422 | 422 | */ |
| 423 | 423 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 424 | - if (!($nom or $table or $champs)) { |
|
| 425 | - spip_log("Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . "))", |
|
| 426 | - 'pg.' . _LOG_ERREUR); |
|
| 427 | - |
|
| 428 | - return false; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - $nom = str_replace("`", "", $nom); |
|
| 432 | - $champs = str_replace("`", "", $champs); |
|
| 433 | - |
|
| 434 | - // PG ne differentie pas noms des index en fonction des tables |
|
| 435 | - // il faut donc creer des noms uniques d'index pour une base pg |
|
| 436 | - $nom = $table . '_' . $nom; |
|
| 437 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 438 | - if (!is_array($champs)) { |
|
| 439 | - if ($champs[0] == "(") { |
|
| 440 | - $champs = substr($champs, 1, -1); |
|
| 441 | - } |
|
| 442 | - $champs = array($champs); |
|
| 443 | - } |
|
| 444 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ")"; |
|
| 445 | - if (!$requeter) { |
|
| 446 | - return $query; |
|
| 447 | - } |
|
| 448 | - $res = spip_pg_query($query, $serveur, $requeter); |
|
| 449 | - |
|
| 450 | - return $res; |
|
| 424 | + if (!($nom or $table or $champs)) { |
|
| 425 | + spip_log("Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . "))", |
|
| 426 | + 'pg.' . _LOG_ERREUR); |
|
| 427 | + |
|
| 428 | + return false; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + $nom = str_replace("`", "", $nom); |
|
| 432 | + $champs = str_replace("`", "", $champs); |
|
| 433 | + |
|
| 434 | + // PG ne differentie pas noms des index en fonction des tables |
|
| 435 | + // il faut donc creer des noms uniques d'index pour une base pg |
|
| 436 | + $nom = $table . '_' . $nom; |
|
| 437 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 438 | + if (!is_array($champs)) { |
|
| 439 | + if ($champs[0] == "(") { |
|
| 440 | + $champs = substr($champs, 1, -1); |
|
| 441 | + } |
|
| 442 | + $champs = array($champs); |
|
| 443 | + } |
|
| 444 | + $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ")"; |
|
| 445 | + if (!$requeter) { |
|
| 446 | + return $query; |
|
| 447 | + } |
|
| 448 | + $res = spip_pg_query($query, $serveur, $requeter); |
|
| 449 | + |
|
| 450 | + return $res; |
|
| 451 | 451 | } |
| 452 | 452 | |
| 453 | 453 | |
| 454 | 454 | // https://code.spip.net/@spip_pg_explain |
| 455 | 455 | function spip_pg_explain($query, $serveur = '', $requeter = true) { |
| 456 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 457 | - return array(); |
|
| 458 | - } |
|
| 459 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 460 | - $prefixe = $connexion['prefixe']; |
|
| 461 | - $link = $connexion['link']; |
|
| 462 | - if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 463 | - $suite = strstr($query, $regs[0]); |
|
| 464 | - $query = substr($query, 0, -strlen($suite)); |
|
| 465 | - } else { |
|
| 466 | - $suite = ''; |
|
| 467 | - } |
|
| 468 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 469 | - |
|
| 470 | - if (!$requeter) { |
|
| 471 | - return $query; |
|
| 472 | - } |
|
| 473 | - $r = spip_pg_query_simple($link, $query); |
|
| 474 | - |
|
| 475 | - return spip_pg_fetch($r, null, $serveur); |
|
| 456 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 457 | + return array(); |
|
| 458 | + } |
|
| 459 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 460 | + $prefixe = $connexion['prefixe']; |
|
| 461 | + $link = $connexion['link']; |
|
| 462 | + if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 463 | + $suite = strstr($query, $regs[0]); |
|
| 464 | + $query = substr($query, 0, -strlen($suite)); |
|
| 465 | + } else { |
|
| 466 | + $suite = ''; |
|
| 467 | + } |
|
| 468 | + $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 469 | + |
|
| 470 | + if (!$requeter) { |
|
| 471 | + return $query; |
|
| 472 | + } |
|
| 473 | + $r = spip_pg_query_simple($link, $query); |
|
| 474 | + |
|
| 475 | + return spip_pg_fetch($r, null, $serveur); |
|
| 476 | 476 | } |
| 477 | 477 | |
| 478 | 478 | |
@@ -491,92 +491,92 @@ discard block |
||
| 491 | 491 | * - False en cas d'erreur. |
| 492 | 492 | **/ |
| 493 | 493 | function spip_pg_selectdb($db, $serveur = '', $requeter = true) { |
| 494 | - // se connecter a la base indiquee |
|
| 495 | - // avec les identifiants connus |
|
| 496 | - $index = $serveur ? strtolower($serveur) : 0; |
|
| 497 | - |
|
| 498 | - if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 499 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 500 | - return $db; |
|
| 501 | - } |
|
| 502 | - } else { |
|
| 503 | - return false; |
|
| 504 | - } |
|
| 494 | + // se connecter a la base indiquee |
|
| 495 | + // avec les identifiants connus |
|
| 496 | + $index = $serveur ? strtolower($serveur) : 0; |
|
| 497 | + |
|
| 498 | + if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 499 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 500 | + return $db; |
|
| 501 | + } |
|
| 502 | + } else { |
|
| 503 | + return false; |
|
| 504 | + } |
|
| 505 | 505 | } |
| 506 | 506 | |
| 507 | 507 | // Qu'une seule base pour le moment |
| 508 | 508 | |
| 509 | 509 | // https://code.spip.net/@spip_pg_listdbs |
| 510 | 510 | function spip_pg_listdbs($serveur) { |
| 511 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 512 | - $link = $connexion['link']; |
|
| 513 | - $dbs = array(); |
|
| 514 | - $res = spip_pg_query_simple($link, "select * From pg_database"); |
|
| 515 | - while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 516 | - $dbs[] = reset($row); |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - return $dbs; |
|
| 511 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 512 | + $link = $connexion['link']; |
|
| 513 | + $dbs = array(); |
|
| 514 | + $res = spip_pg_query_simple($link, "select * From pg_database"); |
|
| 515 | + while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 516 | + $dbs[] = reset($row); |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + return $dbs; |
|
| 520 | 520 | } |
| 521 | 521 | |
| 522 | 522 | // https://code.spip.net/@spip_pg_select |
| 523 | 523 | function spip_pg_select( |
| 524 | - $select, |
|
| 525 | - $from, |
|
| 526 | - $where = '', |
|
| 527 | - $groupby = array(), |
|
| 528 | - $orderby = '', |
|
| 529 | - $limit = '', |
|
| 530 | - $having = '', |
|
| 531 | - $serveur = '', |
|
| 532 | - $requeter = true |
|
| 524 | + $select, |
|
| 525 | + $from, |
|
| 526 | + $where = '', |
|
| 527 | + $groupby = array(), |
|
| 528 | + $orderby = '', |
|
| 529 | + $limit = '', |
|
| 530 | + $having = '', |
|
| 531 | + $serveur = '', |
|
| 532 | + $requeter = true |
|
| 533 | 533 | ) { |
| 534 | 534 | |
| 535 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 536 | - $prefixe = $connexion['prefixe']; |
|
| 537 | - $link = $connexion['link']; |
|
| 538 | - $db = $connexion['db']; |
|
| 539 | - |
|
| 540 | - $limit = preg_match("/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/", $limit, $limatch); |
|
| 541 | - if ($limit) { |
|
| 542 | - $offset = $limatch[2]; |
|
| 543 | - $count = $limatch[3]; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - $select = spip_pg_frommysql($select); |
|
| 547 | - |
|
| 548 | - // si pas de tri explicitement demande, le GROUP BY ne |
|
| 549 | - // contient que la clef primaire. |
|
| 550 | - // lui ajouter alors le champ de tri par defaut |
|
| 551 | - if (preg_match("/FIELD\(([a-z]+\.[a-z]+),/i", $orderby[0], $groupbyplus)) { |
|
| 552 | - $groupby[] = $groupbyplus[1]; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - $orderby = spip_pg_orderby($orderby, $select); |
|
| 556 | - |
|
| 557 | - if ($having) { |
|
| 558 | - if (is_array($having)) { |
|
| 559 | - $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 560 | - } |
|
| 561 | - } |
|
| 562 | - $from = spip_pg_from($from, $prefixe); |
|
| 563 | - $query = "SELECT " . $select |
|
| 564 | - . (!$from ? '' : "\nFROM $from") |
|
| 565 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join("\n\tAND ", |
|
| 566 | - array_map('calculer_pg_where', $where)))))) |
|
| 567 | - . spip_pg_groupby($groupby, $from, $select) |
|
| 568 | - . (!$having ? '' : "\nHAVING $having") |
|
| 569 | - . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 570 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 571 | - |
|
| 572 | - // renvoyer la requete inerte si demandee |
|
| 573 | - if ($requeter === false) { |
|
| 574 | - return $query; |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - $r = spip_pg_trace_query($query, $serveur); |
|
| 578 | - |
|
| 579 | - return $r ? $r : $query;; |
|
| 535 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 536 | + $prefixe = $connexion['prefixe']; |
|
| 537 | + $link = $connexion['link']; |
|
| 538 | + $db = $connexion['db']; |
|
| 539 | + |
|
| 540 | + $limit = preg_match("/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/", $limit, $limatch); |
|
| 541 | + if ($limit) { |
|
| 542 | + $offset = $limatch[2]; |
|
| 543 | + $count = $limatch[3]; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + $select = spip_pg_frommysql($select); |
|
| 547 | + |
|
| 548 | + // si pas de tri explicitement demande, le GROUP BY ne |
|
| 549 | + // contient que la clef primaire. |
|
| 550 | + // lui ajouter alors le champ de tri par defaut |
|
| 551 | + if (preg_match("/FIELD\(([a-z]+\.[a-z]+),/i", $orderby[0], $groupbyplus)) { |
|
| 552 | + $groupby[] = $groupbyplus[1]; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + $orderby = spip_pg_orderby($orderby, $select); |
|
| 556 | + |
|
| 557 | + if ($having) { |
|
| 558 | + if (is_array($having)) { |
|
| 559 | + $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 560 | + } |
|
| 561 | + } |
|
| 562 | + $from = spip_pg_from($from, $prefixe); |
|
| 563 | + $query = "SELECT " . $select |
|
| 564 | + . (!$from ? '' : "\nFROM $from") |
|
| 565 | + . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join("\n\tAND ", |
|
| 566 | + array_map('calculer_pg_where', $where)))))) |
|
| 567 | + . spip_pg_groupby($groupby, $from, $select) |
|
| 568 | + . (!$having ? '' : "\nHAVING $having") |
|
| 569 | + . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 570 | + . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 571 | + |
|
| 572 | + // renvoyer la requete inerte si demandee |
|
| 573 | + if ($requeter === false) { |
|
| 574 | + return $query; |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + $r = spip_pg_trace_query($query, $serveur); |
|
| 578 | + |
|
| 579 | + return $r ? $r : $query;; |
|
| 580 | 580 | } |
| 581 | 581 | |
| 582 | 582 | // Le traitement des prefixes de table dans un Select se limite au FROM |
@@ -584,28 +584,28 @@ discard block |
||
| 584 | 584 | |
| 585 | 585 | // https://code.spip.net/@spip_pg_from |
| 586 | 586 | function spip_pg_from($from, $prefixe) { |
| 587 | - if (is_array($from)) { |
|
| 588 | - $from = spip_pg_select_as($from); |
|
| 589 | - } |
|
| 587 | + if (is_array($from)) { |
|
| 588 | + $from = spip_pg_select_as($from); |
|
| 589 | + } |
|
| 590 | 590 | |
| 591 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 591 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 592 | 592 | } |
| 593 | 593 | |
| 594 | 594 | // https://code.spip.net/@spip_pg_orderby |
| 595 | 595 | function spip_pg_orderby($order, $select) { |
| 596 | - $res = array(); |
|
| 597 | - $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 596 | + $res = array(); |
|
| 597 | + $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 598 | 598 | |
| 599 | - foreach ($arg as $v) { |
|
| 600 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 599 | + foreach ($arg as $v) { |
|
| 600 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 601 | 601 | |
| 602 | - $res[] = $m[1]; |
|
| 603 | - } else { |
|
| 604 | - $res[] = $v; |
|
| 605 | - } |
|
| 606 | - } |
|
| 602 | + $res[] = $m[1]; |
|
| 603 | + } else { |
|
| 604 | + $res[] = $v; |
|
| 605 | + } |
|
| 606 | + } |
|
| 607 | 607 | |
| 608 | - return spip_pg_frommysql(join(',', $res)); |
|
| 608 | + return spip_pg_frommysql(join(',', $res)); |
|
| 609 | 609 | } |
| 610 | 610 | |
| 611 | 611 | // Conversion a l'arrach' des jointures MySQL en jointures PG |
@@ -614,57 +614,57 @@ discard block |
||
| 614 | 614 | |
| 615 | 615 | // https://code.spip.net/@spip_pg_groupby |
| 616 | 616 | function spip_pg_groupby($groupby, $from, $select) { |
| 617 | - $join = strpos($from, ","); |
|
| 618 | - // ismplifier avant de decouper |
|
| 619 | - if (is_string($select)) // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 620 | - { |
|
| 621 | - $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - if ($join or $groupby) { |
|
| 625 | - $join = is_array($select) ? $select : explode(", ", $select); |
|
| 626 | - } |
|
| 627 | - if ($join) { |
|
| 628 | - // enlever les 0 as points, '', ... |
|
| 629 | - foreach ($join as $k => $v) { |
|
| 630 | - $v = str_replace('DISTINCT ', '', $v); |
|
| 631 | - // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 632 | - $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 633 | - $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 634 | - // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 635 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 636 | - // idem sans AS (fetch numerique) |
|
| 637 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 638 | - // des AS simples : on garde le cote droit du AS |
|
| 639 | - $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 640 | - // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 641 | - if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 642 | - unset($join[$k]); |
|
| 643 | - } else { |
|
| 644 | - $join[$k] = trim($v); |
|
| 645 | - } |
|
| 646 | - } |
|
| 647 | - $join = array_diff($join, array('')); |
|
| 648 | - $join = implode(',', $join); |
|
| 649 | - } |
|
| 650 | - if (is_array($groupby)) { |
|
| 651 | - $groupby = join(',', $groupby); |
|
| 652 | - } |
|
| 653 | - if ($join) { |
|
| 654 | - $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 655 | - } |
|
| 656 | - if (!$groupby) { |
|
| 657 | - return ''; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - $groupby = spip_pg_frommysql($groupby); |
|
| 661 | - // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 662 | - // issue de prepare_recherche |
|
| 663 | - $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 664 | - $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 665 | - $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 666 | - |
|
| 667 | - return "\nGROUP BY $groupby"; |
|
| 617 | + $join = strpos($from, ","); |
|
| 618 | + // ismplifier avant de decouper |
|
| 619 | + if (is_string($select)) // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 620 | + { |
|
| 621 | + $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + if ($join or $groupby) { |
|
| 625 | + $join = is_array($select) ? $select : explode(", ", $select); |
|
| 626 | + } |
|
| 627 | + if ($join) { |
|
| 628 | + // enlever les 0 as points, '', ... |
|
| 629 | + foreach ($join as $k => $v) { |
|
| 630 | + $v = str_replace('DISTINCT ', '', $v); |
|
| 631 | + // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 632 | + $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 633 | + $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 634 | + // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 635 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 636 | + // idem sans AS (fetch numerique) |
|
| 637 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 638 | + // des AS simples : on garde le cote droit du AS |
|
| 639 | + $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 640 | + // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 641 | + if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 642 | + unset($join[$k]); |
|
| 643 | + } else { |
|
| 644 | + $join[$k] = trim($v); |
|
| 645 | + } |
|
| 646 | + } |
|
| 647 | + $join = array_diff($join, array('')); |
|
| 648 | + $join = implode(',', $join); |
|
| 649 | + } |
|
| 650 | + if (is_array($groupby)) { |
|
| 651 | + $groupby = join(',', $groupby); |
|
| 652 | + } |
|
| 653 | + if ($join) { |
|
| 654 | + $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 655 | + } |
|
| 656 | + if (!$groupby) { |
|
| 657 | + return ''; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + $groupby = spip_pg_frommysql($groupby); |
|
| 661 | + // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 662 | + // issue de prepare_recherche |
|
| 663 | + $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 664 | + $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 665 | + $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 666 | + |
|
| 667 | + return "\nGROUP BY $groupby"; |
|
| 668 | 668 | } |
| 669 | 669 | |
| 670 | 670 | // Conversion des operateurs MySQL en PG |
@@ -676,472 +676,472 @@ discard block |
||
| 676 | 676 | |
| 677 | 677 | // https://code.spip.net/@spip_pg_frommysql |
| 678 | 678 | function spip_pg_frommysql($arg) { |
| 679 | - if (is_array($arg)) { |
|
| 680 | - $arg = join(", ", $arg); |
|
| 681 | - } |
|
| 679 | + if (is_array($arg)) { |
|
| 680 | + $arg = join(", ", $arg); |
|
| 681 | + } |
|
| 682 | 682 | |
| 683 | - $res = spip_pg_fromfield($arg); |
|
| 683 | + $res = spip_pg_fromfield($arg); |
|
| 684 | 684 | |
| 685 | - $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 685 | + $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 686 | 686 | |
| 687 | - $res = preg_replace('/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 688 | - 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 689 | - $res); |
|
| 690 | - $res = preg_replace('/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 691 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 692 | - $res); |
|
| 693 | - $res = preg_replace('/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 694 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 695 | - $res); |
|
| 687 | + $res = preg_replace('/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 688 | + 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 689 | + $res); |
|
| 690 | + $res = preg_replace('/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 691 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 692 | + $res); |
|
| 693 | + $res = preg_replace('/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 694 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 695 | + $res); |
|
| 696 | 696 | |
| 697 | - $res = preg_replace('/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 698 | - ' EXTRACT(epoch FROM NOW())', $res); |
|
| 697 | + $res = preg_replace('/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 698 | + ' EXTRACT(epoch FROM NOW())', $res); |
|
| 699 | 699 | |
| 700 | - // la fonction md5(integer) n'est pas connu en pg |
|
| 701 | - // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 702 | - $res = preg_replace('/md5\s*[(]([^)]*)[)]/i', |
|
| 703 | - 'MD5(CAST(\1 AS text))', $res); |
|
| 700 | + // la fonction md5(integer) n'est pas connu en pg |
|
| 701 | + // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 702 | + $res = preg_replace('/md5\s*[(]([^)]*)[)]/i', |
|
| 703 | + 'MD5(CAST(\1 AS text))', $res); |
|
| 704 | 704 | |
| 705 | - $res = preg_replace('/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 706 | - ' EXTRACT(epoch FROM \1)', $res); |
|
| 705 | + $res = preg_replace('/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 706 | + ' EXTRACT(epoch FROM \1)', $res); |
|
| 707 | 707 | |
| 708 | - $res = preg_replace('/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 709 | - ' EXTRACT(day FROM \1)', |
|
| 710 | - $res); |
|
| 708 | + $res = preg_replace('/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 709 | + ' EXTRACT(day FROM \1)', |
|
| 710 | + $res); |
|
| 711 | 711 | |
| 712 | - $res = preg_replace('/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 713 | - ' EXTRACT(month FROM \1)', |
|
| 714 | - $res); |
|
| 712 | + $res = preg_replace('/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 713 | + ' EXTRACT(month FROM \1)', |
|
| 714 | + $res); |
|
| 715 | 715 | |
| 716 | - $res = preg_replace('/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 717 | - ' EXTRACT(year FROM \1)', |
|
| 718 | - $res); |
|
| 716 | + $res = preg_replace('/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 717 | + ' EXTRACT(year FROM \1)', |
|
| 718 | + $res); |
|
| 719 | 719 | |
| 720 | - $res = preg_replace('/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 721 | - ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 722 | - $res); |
|
| 720 | + $res = preg_replace('/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 721 | + ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 722 | + $res); |
|
| 723 | 723 | |
| 724 | - $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)\"([^\"]*)\"/", '\1\'\2\'', $res); |
|
| 724 | + $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)\"([^\"]*)\"/", '\1\'\2\'', $res); |
|
| 725 | 725 | |
| 726 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 726 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 727 | 727 | |
| 728 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 728 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 729 | 729 | |
| 730 | - $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 731 | - $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 732 | - $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 733 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 734 | - $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 730 | + $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 731 | + $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 732 | + $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 733 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 734 | + $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 735 | 735 | |
| 736 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 737 | - $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 736 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 737 | + $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 738 | 738 | |
| 739 | - $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 740 | - $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 739 | + $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 740 | + $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 741 | 741 | # correct en theorie mais produit des debordements arithmetiques |
| 742 | 742 | # $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)(timestamp *'[^']*' *[+-] *timestamp *'[^']*') *[)]/", '\2', $res); |
| 743 | - $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 744 | - $res = preg_replace("/\sLIKE\s+/", ' ILIKE ', $res); |
|
| 743 | + $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 744 | + $res = preg_replace("/\sLIKE\s+/", ' ILIKE ', $res); |
|
| 745 | 745 | |
| 746 | - return str_replace('REGEXP', '~', $res); |
|
| 746 | + return str_replace('REGEXP', '~', $res); |
|
| 747 | 747 | } |
| 748 | 748 | |
| 749 | 749 | // https://code.spip.net/@spip_pg_fromfield |
| 750 | 750 | function spip_pg_fromfield($arg) { |
| 751 | - while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 752 | - |
|
| 753 | - preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 754 | - $res = ''; |
|
| 755 | - $n = 0; |
|
| 756 | - $index = $m[2]; |
|
| 757 | - foreach ($r[1] as $v) { |
|
| 758 | - $n++; |
|
| 759 | - $res .= "\nwhen $index=$v then $n"; |
|
| 760 | - } |
|
| 761 | - $arg = $m[1] . "case $res else 0 end " |
|
| 762 | - . substr($arg, strlen($m[0])); |
|
| 763 | - } |
|
| 764 | - |
|
| 765 | - return $arg; |
|
| 751 | + while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 752 | + |
|
| 753 | + preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 754 | + $res = ''; |
|
| 755 | + $n = 0; |
|
| 756 | + $index = $m[2]; |
|
| 757 | + foreach ($r[1] as $v) { |
|
| 758 | + $n++; |
|
| 759 | + $res .= "\nwhen $index=$v then $n"; |
|
| 760 | + } |
|
| 761 | + $arg = $m[1] . "case $res else 0 end " |
|
| 762 | + . substr($arg, strlen($m[0])); |
|
| 763 | + } |
|
| 764 | + |
|
| 765 | + return $arg; |
|
| 766 | 766 | } |
| 767 | 767 | |
| 768 | 768 | // https://code.spip.net/@calculer_pg_where |
| 769 | 769 | function calculer_pg_where($v) { |
| 770 | - if (!is_array($v)) { |
|
| 771 | - return spip_pg_frommysql($v); |
|
| 772 | - } |
|
| 773 | - |
|
| 774 | - $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 775 | - if (!($n = count($v))) { |
|
| 776 | - return $op; |
|
| 777 | - } else { |
|
| 778 | - $arg = calculer_pg_where(array_shift($v)); |
|
| 779 | - if ($n == 1) { |
|
| 780 | - return "$op($arg)"; |
|
| 781 | - } else { |
|
| 782 | - $arg2 = calculer_pg_where(array_shift($v)); |
|
| 783 | - if ($n == 2) { |
|
| 784 | - return "($arg $op $arg2)"; |
|
| 785 | - } else { |
|
| 786 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 787 | - } |
|
| 788 | - } |
|
| 789 | - } |
|
| 770 | + if (!is_array($v)) { |
|
| 771 | + return spip_pg_frommysql($v); |
|
| 772 | + } |
|
| 773 | + |
|
| 774 | + $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 775 | + if (!($n = count($v))) { |
|
| 776 | + return $op; |
|
| 777 | + } else { |
|
| 778 | + $arg = calculer_pg_where(array_shift($v)); |
|
| 779 | + if ($n == 1) { |
|
| 780 | + return "$op($arg)"; |
|
| 781 | + } else { |
|
| 782 | + $arg2 = calculer_pg_where(array_shift($v)); |
|
| 783 | + if ($n == 2) { |
|
| 784 | + return "($arg $op $arg2)"; |
|
| 785 | + } else { |
|
| 786 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 787 | + } |
|
| 788 | + } |
|
| 789 | + } |
|
| 790 | 790 | } |
| 791 | 791 | |
| 792 | 792 | |
| 793 | 793 | // https://code.spip.net/@calculer_pg_expression |
| 794 | 794 | function calculer_pg_expression($expression, $v, $join = 'AND') { |
| 795 | - if (empty($v)) { |
|
| 796 | - return ''; |
|
| 797 | - } |
|
| 795 | + if (empty($v)) { |
|
| 796 | + return ''; |
|
| 797 | + } |
|
| 798 | 798 | |
| 799 | - $exp = "\n$expression "; |
|
| 799 | + $exp = "\n$expression "; |
|
| 800 | 800 | |
| 801 | - if (!is_array($v)) { |
|
| 802 | - $v = array($v); |
|
| 803 | - } |
|
| 801 | + if (!is_array($v)) { |
|
| 802 | + $v = array($v); |
|
| 803 | + } |
|
| 804 | 804 | |
| 805 | - if (strtoupper($join) === 'AND') { |
|
| 806 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 807 | - } else { |
|
| 808 | - return $exp . join($join, $v); |
|
| 809 | - } |
|
| 805 | + if (strtoupper($join) === 'AND') { |
|
| 806 | + return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 807 | + } else { |
|
| 808 | + return $exp . join($join, $v); |
|
| 809 | + } |
|
| 810 | 810 | } |
| 811 | 811 | |
| 812 | 812 | // https://code.spip.net/@spip_pg_select_as |
| 813 | 813 | function spip_pg_select_as($args) { |
| 814 | - $argsas = ""; |
|
| 815 | - foreach ($args as $k => $v) { |
|
| 816 | - if (substr($k, -1) == '@') { |
|
| 817 | - // c'est une jointure qui se refere au from precedent |
|
| 818 | - // pas de virgule |
|
| 819 | - $argsas .= ' ' . $v; |
|
| 820 | - } else { |
|
| 821 | - $as = ''; |
|
| 822 | - // spip_log("$k : $v", _LOG_DEBUG); |
|
| 823 | - if (!is_numeric($k)) { |
|
| 824 | - if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 825 | - $v = $k; |
|
| 826 | - } elseif ($v != $k) { |
|
| 827 | - $p = strpos($v, " "); |
|
| 828 | - if ($p) { |
|
| 829 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 830 | - } else { |
|
| 831 | - $as = " AS $k"; |
|
| 832 | - } |
|
| 833 | - } |
|
| 834 | - } |
|
| 835 | - // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 836 | - // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 837 | - $argsas .= ', ' . $v . $as; |
|
| 838 | - } |
|
| 839 | - } |
|
| 840 | - |
|
| 841 | - return substr($argsas, 2); |
|
| 814 | + $argsas = ""; |
|
| 815 | + foreach ($args as $k => $v) { |
|
| 816 | + if (substr($k, -1) == '@') { |
|
| 817 | + // c'est une jointure qui se refere au from precedent |
|
| 818 | + // pas de virgule |
|
| 819 | + $argsas .= ' ' . $v; |
|
| 820 | + } else { |
|
| 821 | + $as = ''; |
|
| 822 | + // spip_log("$k : $v", _LOG_DEBUG); |
|
| 823 | + if (!is_numeric($k)) { |
|
| 824 | + if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 825 | + $v = $k; |
|
| 826 | + } elseif ($v != $k) { |
|
| 827 | + $p = strpos($v, " "); |
|
| 828 | + if ($p) { |
|
| 829 | + $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 830 | + } else { |
|
| 831 | + $as = " AS $k"; |
|
| 832 | + } |
|
| 833 | + } |
|
| 834 | + } |
|
| 835 | + // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 836 | + // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 837 | + $argsas .= ', ' . $v . $as; |
|
| 838 | + } |
|
| 839 | + } |
|
| 840 | + |
|
| 841 | + return substr($argsas, 2); |
|
| 842 | 842 | } |
| 843 | 843 | |
| 844 | 844 | // https://code.spip.net/@spip_pg_fetch |
| 845 | 845 | function spip_pg_fetch($res, $t = '', $serveur = '', $requeter = true) { |
| 846 | 846 | |
| 847 | - if ($res) { |
|
| 848 | - $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 849 | - } |
|
| 847 | + if ($res) { |
|
| 848 | + $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 849 | + } |
|
| 850 | 850 | |
| 851 | - return $res; |
|
| 851 | + return $res; |
|
| 852 | 852 | } |
| 853 | 853 | |
| 854 | 854 | function spip_pg_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 855 | - if ($r) { |
|
| 856 | - return pg_result_seek($r, $row_number); |
|
| 857 | - } |
|
| 855 | + if ($r) { |
|
| 856 | + return pg_result_seek($r, $row_number); |
|
| 857 | + } |
|
| 858 | 858 | } |
| 859 | 859 | |
| 860 | 860 | |
| 861 | 861 | // https://code.spip.net/@spip_pg_countsel |
| 862 | 862 | function spip_pg_countsel( |
| 863 | - $from = array(), |
|
| 864 | - $where = array(), |
|
| 865 | - $groupby = array(), |
|
| 866 | - $having = array(), |
|
| 867 | - $serveur = '', |
|
| 868 | - $requeter = true |
|
| 863 | + $from = array(), |
|
| 864 | + $where = array(), |
|
| 865 | + $groupby = array(), |
|
| 866 | + $having = array(), |
|
| 867 | + $serveur = '', |
|
| 868 | + $requeter = true |
|
| 869 | 869 | ) { |
| 870 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 871 | - $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 872 | - if (!$requeter) { |
|
| 873 | - return $r; |
|
| 874 | - } |
|
| 875 | - if (!is_resource($r)) { |
|
| 876 | - return 0; |
|
| 877 | - } |
|
| 878 | - list($c) = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 879 | - |
|
| 880 | - return $c; |
|
| 870 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 871 | + $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 872 | + if (!$requeter) { |
|
| 873 | + return $r; |
|
| 874 | + } |
|
| 875 | + if (!is_resource($r)) { |
|
| 876 | + return 0; |
|
| 877 | + } |
|
| 878 | + list($c) = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 879 | + |
|
| 880 | + return $c; |
|
| 881 | 881 | } |
| 882 | 882 | |
| 883 | 883 | // https://code.spip.net/@spip_pg_count |
| 884 | 884 | function spip_pg_count($res, $serveur = '', $requeter = true) { |
| 885 | - return !$res ? 0 : pg_numrows($res); |
|
| 885 | + return !$res ? 0 : pg_numrows($res); |
|
| 886 | 886 | } |
| 887 | 887 | |
| 888 | 888 | // https://code.spip.net/@spip_pg_free |
| 889 | 889 | function spip_pg_free($res, $serveur = '', $requeter = true) { |
| 890 | - // rien a faire en postgres |
|
| 890 | + // rien a faire en postgres |
|
| 891 | 891 | } |
| 892 | 892 | |
| 893 | 893 | // https://code.spip.net/@spip_pg_delete |
| 894 | 894 | function spip_pg_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 895 | 895 | |
| 896 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 897 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 896 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 897 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 898 | 898 | |
| 899 | - $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 900 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 899 | + $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 900 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 901 | 901 | |
| 902 | - // renvoyer la requete inerte si demandee |
|
| 903 | - if (!$requeter) { |
|
| 904 | - return $query; |
|
| 905 | - } |
|
| 902 | + // renvoyer la requete inerte si demandee |
|
| 903 | + if (!$requeter) { |
|
| 904 | + return $query; |
|
| 905 | + } |
|
| 906 | 906 | |
| 907 | - $res = spip_pg_trace_query($query, $serveur); |
|
| 908 | - if ($res) { |
|
| 909 | - return pg_affected_rows($res); |
|
| 910 | - } else { |
|
| 911 | - return false; |
|
| 912 | - } |
|
| 907 | + $res = spip_pg_trace_query($query, $serveur); |
|
| 908 | + if ($res) { |
|
| 909 | + return pg_affected_rows($res); |
|
| 910 | + } else { |
|
| 911 | + return false; |
|
| 912 | + } |
|
| 913 | 913 | } |
| 914 | 914 | |
| 915 | 915 | // https://code.spip.net/@spip_pg_insert |
| 916 | 916 | function spip_pg_insert($table, $champs, $valeurs, $desc = array(), $serveur = '', $requeter = true) { |
| 917 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 918 | - $prefixe = $connexion['prefixe']; |
|
| 919 | - $link = $connexion['link']; |
|
| 920 | - |
|
| 921 | - if (!$desc) { |
|
| 922 | - $desc = description_table($table, $serveur); |
|
| 923 | - } |
|
| 924 | - $seq = spip_pg_sequence($table, true); |
|
| 925 | - // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 926 | - if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 927 | - $seq = spip_pg_sequence($table); |
|
| 928 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 929 | - $seq = "currval('$seq')"; |
|
| 930 | - } |
|
| 931 | - |
|
| 932 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 933 | - $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 934 | - $ins = (strlen($champs) < 3) |
|
| 935 | - ? " DEFAULT VALUES" |
|
| 936 | - : "$champs VALUES $valeurs"; |
|
| 937 | - $q = "INSERT INTO $table $ins $ret"; |
|
| 938 | - if (!$requeter) { |
|
| 939 | - return $q; |
|
| 940 | - } |
|
| 941 | - $connexion['last'] = $q; |
|
| 942 | - $r = spip_pg_query_simple($link, $q); |
|
| 917 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 918 | + $prefixe = $connexion['prefixe']; |
|
| 919 | + $link = $connexion['link']; |
|
| 920 | + |
|
| 921 | + if (!$desc) { |
|
| 922 | + $desc = description_table($table, $serveur); |
|
| 923 | + } |
|
| 924 | + $seq = spip_pg_sequence($table, true); |
|
| 925 | + // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 926 | + if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 927 | + $seq = spip_pg_sequence($table); |
|
| 928 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 929 | + $seq = "currval('$seq')"; |
|
| 930 | + } |
|
| 931 | + |
|
| 932 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 933 | + $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 934 | + $ins = (strlen($champs) < 3) |
|
| 935 | + ? " DEFAULT VALUES" |
|
| 936 | + : "$champs VALUES $valeurs"; |
|
| 937 | + $q = "INSERT INTO $table $ins $ret"; |
|
| 938 | + if (!$requeter) { |
|
| 939 | + return $q; |
|
| 940 | + } |
|
| 941 | + $connexion['last'] = $q; |
|
| 942 | + $r = spip_pg_query_simple($link, $q); |
|
| 943 | 943 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 944 | - if ($r) { |
|
| 945 | - if (!$ret) { |
|
| 946 | - return 0; |
|
| 947 | - } |
|
| 948 | - if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 949 | - return $r2[0]; |
|
| 950 | - } |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - return false; |
|
| 944 | + if ($r) { |
|
| 945 | + if (!$ret) { |
|
| 946 | + return 0; |
|
| 947 | + } |
|
| 948 | + if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 949 | + return $r2[0]; |
|
| 950 | + } |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + return false; |
|
| 954 | 954 | } |
| 955 | 955 | |
| 956 | 956 | // https://code.spip.net/@spip_pg_insertq |
| 957 | 957 | function spip_pg_insertq($table, $couples = array(), $desc = array(), $serveur = '', $requeter = true) { |
| 958 | 958 | |
| 959 | - if (!$desc) { |
|
| 960 | - $desc = description_table($table, $serveur); |
|
| 961 | - } |
|
| 962 | - if (!$desc) { |
|
| 963 | - die("$table insertion sans description"); |
|
| 964 | - } |
|
| 965 | - $fields = $desc['field']; |
|
| 959 | + if (!$desc) { |
|
| 960 | + $desc = description_table($table, $serveur); |
|
| 961 | + } |
|
| 962 | + if (!$desc) { |
|
| 963 | + die("$table insertion sans description"); |
|
| 964 | + } |
|
| 965 | + $fields = $desc['field']; |
|
| 966 | 966 | |
| 967 | - foreach ($couples as $champ => $val) { |
|
| 968 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 969 | - } |
|
| 967 | + foreach ($couples as $champ => $val) { |
|
| 968 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 969 | + } |
|
| 970 | 970 | |
| 971 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 972 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 971 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 972 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 973 | 973 | |
| 974 | - return spip_pg_insert($table, "(" . join(',', array_keys($couples)) . ")", "(" . join(',', $couples) . ")", $desc, |
|
| 975 | - $serveur, $requeter); |
|
| 974 | + return spip_pg_insert($table, "(" . join(',', array_keys($couples)) . ")", "(" . join(',', $couples) . ")", $desc, |
|
| 975 | + $serveur, $requeter); |
|
| 976 | 976 | } |
| 977 | 977 | |
| 978 | 978 | |
| 979 | 979 | // https://code.spip.net/@spip_pg_insertq_multi |
| 980 | 980 | function spip_pg_insertq_multi($table, $tab_couples = array(), $desc = array(), $serveur = '', $requeter = true) { |
| 981 | 981 | |
| 982 | - if (!$desc) { |
|
| 983 | - $desc = description_table($table, $serveur); |
|
| 984 | - } |
|
| 985 | - if (!$desc) { |
|
| 986 | - die("$table insertion sans description"); |
|
| 987 | - } |
|
| 988 | - $fields = isset($desc['field']) ? $desc['field'] : array(); |
|
| 989 | - |
|
| 990 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 991 | - // une premiere fois pour ajouter maj dans les cles |
|
| 992 | - $c = isset($tab_couples[0]) ? $tab_couples[0] : array(); |
|
| 993 | - $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 994 | - |
|
| 995 | - $cles = "(" . join(',', array_keys($les_cles)) . ')'; |
|
| 996 | - $valeurs = array(); |
|
| 997 | - foreach ($tab_couples as $couples) { |
|
| 998 | - foreach ($couples as $champ => $val) { |
|
| 999 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1000 | - } |
|
| 1001 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1002 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1003 | - |
|
| 1004 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1005 | - } |
|
| 1006 | - $valeurs = implode(', ', $valeurs); |
|
| 1007 | - |
|
| 1008 | - return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 982 | + if (!$desc) { |
|
| 983 | + $desc = description_table($table, $serveur); |
|
| 984 | + } |
|
| 985 | + if (!$desc) { |
|
| 986 | + die("$table insertion sans description"); |
|
| 987 | + } |
|
| 988 | + $fields = isset($desc['field']) ? $desc['field'] : array(); |
|
| 989 | + |
|
| 990 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 991 | + // une premiere fois pour ajouter maj dans les cles |
|
| 992 | + $c = isset($tab_couples[0]) ? $tab_couples[0] : array(); |
|
| 993 | + $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 994 | + |
|
| 995 | + $cles = "(" . join(',', array_keys($les_cles)) . ')'; |
|
| 996 | + $valeurs = array(); |
|
| 997 | + foreach ($tab_couples as $couples) { |
|
| 998 | + foreach ($couples as $champ => $val) { |
|
| 999 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1000 | + } |
|
| 1001 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1002 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1003 | + |
|
| 1004 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1005 | + } |
|
| 1006 | + $valeurs = implode(', ', $valeurs); |
|
| 1007 | + |
|
| 1008 | + return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1009 | 1009 | } |
| 1010 | 1010 | |
| 1011 | 1011 | |
| 1012 | 1012 | // https://code.spip.net/@spip_pg_update |
| 1013 | 1013 | function spip_pg_update($table, $couples, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1014 | 1014 | |
| 1015 | - if (!$couples) { |
|
| 1016 | - return; |
|
| 1017 | - } |
|
| 1018 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1019 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1015 | + if (!$couples) { |
|
| 1016 | + return; |
|
| 1017 | + } |
|
| 1018 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1019 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1020 | 1020 | |
| 1021 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1022 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1021 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1022 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1023 | 1023 | |
| 1024 | - $set = array(); |
|
| 1025 | - foreach ($couples as $champ => $val) { |
|
| 1026 | - $set[] = $champ . '=' . $val; |
|
| 1027 | - } |
|
| 1024 | + $set = array(); |
|
| 1025 | + foreach ($couples as $champ => $val) { |
|
| 1026 | + $set[] = $champ . '=' . $val; |
|
| 1027 | + } |
|
| 1028 | 1028 | |
| 1029 | - $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1030 | - . calculer_pg_expression('SET', $set, ',') |
|
| 1031 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1029 | + $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1030 | + . calculer_pg_expression('SET', $set, ',') |
|
| 1031 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1032 | 1032 | |
| 1033 | - // renvoyer la requete inerte si demandee |
|
| 1034 | - if (!$requeter) { |
|
| 1035 | - return $query; |
|
| 1036 | - } |
|
| 1033 | + // renvoyer la requete inerte si demandee |
|
| 1034 | + if (!$requeter) { |
|
| 1035 | + return $query; |
|
| 1036 | + } |
|
| 1037 | 1037 | |
| 1038 | - return spip_pg_trace_query($query, $serveur); |
|
| 1038 | + return spip_pg_trace_query($query, $serveur); |
|
| 1039 | 1039 | } |
| 1040 | 1040 | |
| 1041 | 1041 | // idem, mais les valeurs sont des constantes a mettre entre apostrophes |
| 1042 | 1042 | // sauf les expressions de date lorsqu'il s'agit de fonctions SQL (NOW etc) |
| 1043 | 1043 | // https://code.spip.net/@spip_pg_updateq |
| 1044 | 1044 | function spip_pg_updateq($table, $couples, $where = '', $desc = array(), $serveur = '', $requeter = true) { |
| 1045 | - if (!$couples) { |
|
| 1046 | - return; |
|
| 1047 | - } |
|
| 1048 | - if (!$desc) { |
|
| 1049 | - $desc = description_table($table, $serveur); |
|
| 1050 | - } |
|
| 1051 | - $fields = $desc['field']; |
|
| 1052 | - foreach ($couples as $k => $val) { |
|
| 1053 | - $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1054 | - } |
|
| 1055 | - |
|
| 1056 | - return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1045 | + if (!$couples) { |
|
| 1046 | + return; |
|
| 1047 | + } |
|
| 1048 | + if (!$desc) { |
|
| 1049 | + $desc = description_table($table, $serveur); |
|
| 1050 | + } |
|
| 1051 | + $fields = $desc['field']; |
|
| 1052 | + foreach ($couples as $k => $val) { |
|
| 1053 | + $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1054 | + } |
|
| 1055 | + |
|
| 1056 | + return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1057 | 1057 | } |
| 1058 | 1058 | |
| 1059 | 1059 | |
| 1060 | 1060 | // https://code.spip.net/@spip_pg_replace |
| 1061 | 1061 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1062 | - if (!$values) { |
|
| 1063 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1064 | - |
|
| 1065 | - return 0; |
|
| 1066 | - } |
|
| 1067 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1068 | - $prefixe = $connexion['prefixe']; |
|
| 1069 | - $link = $connexion['link']; |
|
| 1070 | - |
|
| 1071 | - if (!$desc) { |
|
| 1072 | - $desc = description_table($table, $serveur); |
|
| 1073 | - } |
|
| 1074 | - if (!$desc) { |
|
| 1075 | - die("$table insertion sans description"); |
|
| 1076 | - } |
|
| 1077 | - $prim = $desc['key']['PRIMARY KEY']; |
|
| 1078 | - $ids = preg_split('/,\s*/', $prim); |
|
| 1079 | - $noprims = $prims = array(); |
|
| 1080 | - foreach ($values as $k => $v) { |
|
| 1081 | - $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1082 | - |
|
| 1083 | - if (!in_array($k, $ids)) { |
|
| 1084 | - $noprims[$k] = "$k=$v"; |
|
| 1085 | - } else { |
|
| 1086 | - $prims[$k] = "$k=$v"; |
|
| 1087 | - } |
|
| 1088 | - } |
|
| 1089 | - |
|
| 1090 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1091 | - $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1092 | - |
|
| 1093 | - $where = join(' AND ', $prims); |
|
| 1094 | - if (!$where) { |
|
| 1095 | - return spip_pg_insert($table, "(" . join(',', array_keys($values)) . ")", "(" . join(',', $values) . ")", $desc, |
|
| 1096 | - $serveur); |
|
| 1097 | - } |
|
| 1098 | - $couples = join(',', $noprims); |
|
| 1099 | - |
|
| 1100 | - $seq = spip_pg_sequence($table); |
|
| 1101 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 1102 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1103 | - |
|
| 1104 | - $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1105 | - if ($couples) { |
|
| 1106 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1062 | + if (!$values) { |
|
| 1063 | + spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1064 | + |
|
| 1065 | + return 0; |
|
| 1066 | + } |
|
| 1067 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1068 | + $prefixe = $connexion['prefixe']; |
|
| 1069 | + $link = $connexion['link']; |
|
| 1070 | + |
|
| 1071 | + if (!$desc) { |
|
| 1072 | + $desc = description_table($table, $serveur); |
|
| 1073 | + } |
|
| 1074 | + if (!$desc) { |
|
| 1075 | + die("$table insertion sans description"); |
|
| 1076 | + } |
|
| 1077 | + $prim = $desc['key']['PRIMARY KEY']; |
|
| 1078 | + $ids = preg_split('/,\s*/', $prim); |
|
| 1079 | + $noprims = $prims = array(); |
|
| 1080 | + foreach ($values as $k => $v) { |
|
| 1081 | + $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1082 | + |
|
| 1083 | + if (!in_array($k, $ids)) { |
|
| 1084 | + $noprims[$k] = "$k=$v"; |
|
| 1085 | + } else { |
|
| 1086 | + $prims[$k] = "$k=$v"; |
|
| 1087 | + } |
|
| 1088 | + } |
|
| 1089 | + |
|
| 1090 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1091 | + $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1092 | + |
|
| 1093 | + $where = join(' AND ', $prims); |
|
| 1094 | + if (!$where) { |
|
| 1095 | + return spip_pg_insert($table, "(" . join(',', array_keys($values)) . ")", "(" . join(',', $values) . ")", $desc, |
|
| 1096 | + $serveur); |
|
| 1097 | + } |
|
| 1098 | + $couples = join(',', $noprims); |
|
| 1099 | + |
|
| 1100 | + $seq = spip_pg_sequence($table); |
|
| 1101 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 1102 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1103 | + |
|
| 1104 | + $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1105 | + if ($couples) { |
|
| 1106 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1107 | 1107 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 1108 | - if (!$couples) { |
|
| 1109 | - return false; |
|
| 1110 | - } |
|
| 1111 | - $couples = pg_affected_rows($couples); |
|
| 1112 | - } |
|
| 1113 | - if (!$couples) { |
|
| 1114 | - $ret = !$seq ? '' : |
|
| 1115 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1116 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join(',', |
|
| 1117 | - $values) . ")$ret"; |
|
| 1118 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1119 | - if (!$couples) { |
|
| 1120 | - return false; |
|
| 1121 | - } elseif ($ret) { |
|
| 1122 | - $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1123 | - if ($r[0]) { |
|
| 1124 | - $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1125 | - // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1126 | - // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1127 | - $r = @pg_query($link, $q); |
|
| 1128 | - } |
|
| 1129 | - } |
|
| 1130 | - } |
|
| 1131 | - |
|
| 1132 | - return $couples; |
|
| 1108 | + if (!$couples) { |
|
| 1109 | + return false; |
|
| 1110 | + } |
|
| 1111 | + $couples = pg_affected_rows($couples); |
|
| 1112 | + } |
|
| 1113 | + if (!$couples) { |
|
| 1114 | + $ret = !$seq ? '' : |
|
| 1115 | + (" RETURNING nextval('$seq') < $prim"); |
|
| 1116 | + $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join(',', |
|
| 1117 | + $values) . ")$ret"; |
|
| 1118 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1119 | + if (!$couples) { |
|
| 1120 | + return false; |
|
| 1121 | + } elseif ($ret) { |
|
| 1122 | + $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1123 | + if ($r[0]) { |
|
| 1124 | + $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1125 | + // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1126 | + // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1127 | + $r = @pg_query($link, $q); |
|
| 1128 | + } |
|
| 1129 | + } |
|
| 1130 | + } |
|
| 1131 | + |
|
| 1132 | + return $couples; |
|
| 1133 | 1133 | } |
| 1134 | 1134 | |
| 1135 | 1135 | |
| 1136 | 1136 | // https://code.spip.net/@spip_pg_replace_multi |
| 1137 | 1137 | function spip_pg_replace_multi($table, $tab_couples, $desc = array(), $serveur = '', $requeter = true) { |
| 1138 | - // boucler pour traiter chaque requete independemment |
|
| 1139 | - foreach ($tab_couples as $couples) { |
|
| 1140 | - $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1141 | - } |
|
| 1138 | + // boucler pour traiter chaque requete independemment |
|
| 1139 | + foreach ($tab_couples as $couples) { |
|
| 1140 | + $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1141 | + } |
|
| 1142 | 1142 | |
| 1143 | - // renvoie le dernier id |
|
| 1144 | - return $retour; |
|
| 1143 | + // renvoie le dernier id |
|
| 1144 | + return $retour; |
|
| 1145 | 1145 | } |
| 1146 | 1146 | |
| 1147 | 1147 | |
@@ -1151,19 +1151,19 @@ discard block |
||
| 1151 | 1151 | // https://code.spip.net/@spip_pg_sequence |
| 1152 | 1152 | function spip_pg_sequence($table, $raw = false) { |
| 1153 | 1153 | |
| 1154 | - include_spip('base/serial'); |
|
| 1155 | - if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1156 | - return false; |
|
| 1157 | - } |
|
| 1158 | - $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1159 | - $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1160 | - if (!preg_match('/^\w+$/', $prim) |
|
| 1161 | - or strpos($desc['field'][$prim], 'int') === false |
|
| 1162 | - ) { |
|
| 1163 | - return ''; |
|
| 1164 | - } else { |
|
| 1165 | - return $raw ? $prim : $table . '_' . $prim . "_seq"; |
|
| 1166 | - } |
|
| 1154 | + include_spip('base/serial'); |
|
| 1155 | + if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1156 | + return false; |
|
| 1157 | + } |
|
| 1158 | + $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1159 | + $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1160 | + if (!preg_match('/^\w+$/', $prim) |
|
| 1161 | + or strpos($desc['field'][$prim], 'int') === false |
|
| 1162 | + ) { |
|
| 1163 | + return ''; |
|
| 1164 | + } else { |
|
| 1165 | + return $raw ? $prim : $table . '_' . $prim . "_seq"; |
|
| 1166 | + } |
|
| 1167 | 1167 | } |
| 1168 | 1168 | |
| 1169 | 1169 | // Explicite les conversions de Mysql d'une valeur $v de type $t |
@@ -1171,65 +1171,65 @@ discard block |
||
| 1171 | 1171 | |
| 1172 | 1172 | // https://code.spip.net/@spip_pg_cite |
| 1173 | 1173 | function spip_pg_cite($v, $t) { |
| 1174 | - if (is_null($v)) { |
|
| 1175 | - return 'NULL'; |
|
| 1176 | - } // null php se traduit en NULL SQL |
|
| 1177 | - |
|
| 1178 | - if (sql_test_date($t)) { |
|
| 1179 | - if ($v and (strpos("0123456789", $v[0]) === false)) { |
|
| 1180 | - return spip_pg_frommysql($v); |
|
| 1181 | - } else { |
|
| 1182 | - if (strncmp($v, '0000', 4) == 0) { |
|
| 1183 | - $v = "0001" . substr($v, 4); |
|
| 1184 | - } |
|
| 1185 | - if (strpos($v, "-00-00") === 4) { |
|
| 1186 | - $v = substr($v, 0, 4) . "-01-01" . substr($v, 10); |
|
| 1187 | - } |
|
| 1188 | - |
|
| 1189 | - return "timestamp '$v'"; |
|
| 1190 | - } |
|
| 1191 | - } elseif (!sql_test_int($t)) { |
|
| 1192 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1193 | - } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1194 | - return $v; |
|
| 1195 | - } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1196 | - return substr($v, 1); |
|
| 1197 | - } else { |
|
| 1198 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1199 | - |
|
| 1200 | - return intval($v); |
|
| 1201 | - } |
|
| 1174 | + if (is_null($v)) { |
|
| 1175 | + return 'NULL'; |
|
| 1176 | + } // null php se traduit en NULL SQL |
|
| 1177 | + |
|
| 1178 | + if (sql_test_date($t)) { |
|
| 1179 | + if ($v and (strpos("0123456789", $v[0]) === false)) { |
|
| 1180 | + return spip_pg_frommysql($v); |
|
| 1181 | + } else { |
|
| 1182 | + if (strncmp($v, '0000', 4) == 0) { |
|
| 1183 | + $v = "0001" . substr($v, 4); |
|
| 1184 | + } |
|
| 1185 | + if (strpos($v, "-00-00") === 4) { |
|
| 1186 | + $v = substr($v, 0, 4) . "-01-01" . substr($v, 10); |
|
| 1187 | + } |
|
| 1188 | + |
|
| 1189 | + return "timestamp '$v'"; |
|
| 1190 | + } |
|
| 1191 | + } elseif (!sql_test_int($t)) { |
|
| 1192 | + return ("'" . pg_escape_string($v) . "'"); |
|
| 1193 | + } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1194 | + return $v; |
|
| 1195 | + } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1196 | + return substr($v, 1); |
|
| 1197 | + } else { |
|
| 1198 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1199 | + |
|
| 1200 | + return intval($v); |
|
| 1201 | + } |
|
| 1202 | 1202 | } |
| 1203 | 1203 | |
| 1204 | 1204 | // https://code.spip.net/@spip_pg_hex |
| 1205 | 1205 | function spip_pg_hex($v) { |
| 1206 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1206 | + return "CAST(x'" . $v . "' as bigint)"; |
|
| 1207 | 1207 | } |
| 1208 | 1208 | |
| 1209 | 1209 | function spip_pg_quote($v, $type = '') { |
| 1210 | - if (!is_array($v)) { |
|
| 1211 | - return spip_pg_cite($v, $type); |
|
| 1212 | - } |
|
| 1213 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1214 | - foreach ($v as $k => $r) { |
|
| 1215 | - $v[$k] = spip_pg_quote($r, $type); |
|
| 1216 | - } |
|
| 1217 | - |
|
| 1218 | - return join(",", $v); |
|
| 1210 | + if (!is_array($v)) { |
|
| 1211 | + return spip_pg_cite($v, $type); |
|
| 1212 | + } |
|
| 1213 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1214 | + foreach ($v as $k => $r) { |
|
| 1215 | + $v[$k] = spip_pg_quote($r, $type); |
|
| 1216 | + } |
|
| 1217 | + |
|
| 1218 | + return join(",", $v); |
|
| 1219 | 1219 | } |
| 1220 | 1220 | |
| 1221 | 1221 | function spip_pg_date_proche($champ, $interval, $unite) { |
| 1222 | - return '(' |
|
| 1223 | - . $champ |
|
| 1224 | - . (($interval <= 0) ? '>' : '<') |
|
| 1225 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1226 | - . '(' |
|
| 1227 | - . sql_quote(date('Y-m-d H:i:s')) |
|
| 1228 | - . ', INTERVAL ' |
|
| 1229 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1230 | - . ' ' |
|
| 1231 | - . $unite |
|
| 1232 | - . '))'; |
|
| 1222 | + return '(' |
|
| 1223 | + . $champ |
|
| 1224 | + . (($interval <= 0) ? '>' : '<') |
|
| 1225 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1226 | + . '(' |
|
| 1227 | + . sql_quote(date('Y-m-d H:i:s')) |
|
| 1228 | + . ', INTERVAL ' |
|
| 1229 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1230 | + . ' ' |
|
| 1231 | + . $unite |
|
| 1232 | + . '))'; |
|
| 1233 | 1233 | } |
| 1234 | 1234 | |
| 1235 | 1235 | // https://code.spip.net/@spip_pg_in |
@@ -1237,69 +1237,69 @@ discard block |
||
| 1237 | 1237 | // |
| 1238 | 1238 | // IN (...) souvent limite a 255 elements, d'ou cette fonction assistante |
| 1239 | 1239 | // |
| 1240 | - // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1241 | - if (!$valeurs) { |
|
| 1242 | - return $not ? '0=0' : '0=1'; |
|
| 1243 | - } |
|
| 1244 | - if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1245 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1246 | - } |
|
| 1247 | - $n = $i = 0; |
|
| 1248 | - $in_sql = ""; |
|
| 1249 | - while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1250 | - if ((++$i) >= 255) { |
|
| 1251 | - $in_sql .= "($val $not IN (" . |
|
| 1252 | - substr($valeurs, 0, $n) . |
|
| 1253 | - "))\n" . |
|
| 1254 | - ($not ? "AND\t" : "OR\t"); |
|
| 1255 | - $valeurs = substr($valeurs, $n + 1); |
|
| 1256 | - $i = $n = 0; |
|
| 1257 | - } |
|
| 1258 | - } |
|
| 1259 | - $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1260 | - |
|
| 1261 | - return "($in_sql)"; |
|
| 1240 | + // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1241 | + if (!$valeurs) { |
|
| 1242 | + return $not ? '0=0' : '0=1'; |
|
| 1243 | + } |
|
| 1244 | + if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1245 | + return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1246 | + } |
|
| 1247 | + $n = $i = 0; |
|
| 1248 | + $in_sql = ""; |
|
| 1249 | + while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1250 | + if ((++$i) >= 255) { |
|
| 1251 | + $in_sql .= "($val $not IN (" . |
|
| 1252 | + substr($valeurs, 0, $n) . |
|
| 1253 | + "))\n" . |
|
| 1254 | + ($not ? "AND\t" : "OR\t"); |
|
| 1255 | + $valeurs = substr($valeurs, $n + 1); |
|
| 1256 | + $i = $n = 0; |
|
| 1257 | + } |
|
| 1258 | + } |
|
| 1259 | + $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1260 | + |
|
| 1261 | + return "($in_sql)"; |
|
| 1262 | 1262 | } |
| 1263 | 1263 | |
| 1264 | 1264 | // https://code.spip.net/@spip_pg_error |
| 1265 | 1265 | function spip_pg_error($query = '', $serveur = '', $requeter = true) { |
| 1266 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1267 | - $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1268 | - if ($s) { |
|
| 1269 | - $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1270 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1271 | - } |
|
| 1272 | - |
|
| 1273 | - return $s; |
|
| 1266 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1267 | + $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1268 | + if ($s) { |
|
| 1269 | + $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1270 | + spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1271 | + } |
|
| 1272 | + |
|
| 1273 | + return $s; |
|
| 1274 | 1274 | } |
| 1275 | 1275 | |
| 1276 | 1276 | // https://code.spip.net/@spip_pg_errno |
| 1277 | 1277 | function spip_pg_errno($serveur = '') { |
| 1278 | - // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1279 | - // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1280 | - return 0; |
|
| 1278 | + // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1279 | + // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1280 | + return 0; |
|
| 1281 | 1281 | } |
| 1282 | 1282 | |
| 1283 | 1283 | // https://code.spip.net/@spip_pg_drop_table |
| 1284 | 1284 | function spip_pg_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 1285 | - if ($exist) { |
|
| 1286 | - $exist = " IF EXISTS"; |
|
| 1287 | - } |
|
| 1288 | - if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1289 | - return true; |
|
| 1290 | - } else { |
|
| 1291 | - return false; |
|
| 1292 | - } |
|
| 1285 | + if ($exist) { |
|
| 1286 | + $exist = " IF EXISTS"; |
|
| 1287 | + } |
|
| 1288 | + if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1289 | + return true; |
|
| 1290 | + } else { |
|
| 1291 | + return false; |
|
| 1292 | + } |
|
| 1293 | 1293 | } |
| 1294 | 1294 | |
| 1295 | 1295 | // supprime une vue |
| 1296 | 1296 | // https://code.spip.net/@spip_pg_drop_view |
| 1297 | 1297 | function spip_pg_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 1298 | - if ($exist) { |
|
| 1299 | - $exist = " IF EXISTS"; |
|
| 1300 | - } |
|
| 1298 | + if ($exist) { |
|
| 1299 | + $exist = " IF EXISTS"; |
|
| 1300 | + } |
|
| 1301 | 1301 | |
| 1302 | - return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1302 | + return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1303 | 1303 | } |
| 1304 | 1304 | |
| 1305 | 1305 | /** |
@@ -1316,41 +1316,41 @@ discard block |
||
| 1316 | 1316 | * Ressource à utiliser avec sql_fetch() |
| 1317 | 1317 | **/ |
| 1318 | 1318 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1319 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1320 | - $link = $connexion['link']; |
|
| 1321 | - $connexion['last'] = $q = "SELECT tablename FROM pg_tables WHERE tablename ILIKE " . _q($match); |
|
| 1319 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1320 | + $link = $connexion['link']; |
|
| 1321 | + $connexion['last'] = $q = "SELECT tablename FROM pg_tables WHERE tablename ILIKE " . _q($match); |
|
| 1322 | 1322 | |
| 1323 | - return spip_pg_query_simple($link, $q); |
|
| 1323 | + return spip_pg_query_simple($link, $q); |
|
| 1324 | 1324 | } |
| 1325 | 1325 | |
| 1326 | 1326 | // https://code.spip.net/@spip_pg_showtable |
| 1327 | 1327 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1328 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1329 | - $link = $connexion['link']; |
|
| 1330 | - $connexion['last'] = $q = "SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE " . _q($nom_table); |
|
| 1331 | - |
|
| 1332 | - $res = spip_pg_query_simple($link, $q); |
|
| 1333 | - if (!$res) { |
|
| 1334 | - return false; |
|
| 1335 | - } |
|
| 1336 | - |
|
| 1337 | - // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1338 | - // il faut en tenir compte dans le return |
|
| 1339 | - $fields = array(); |
|
| 1340 | - while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1341 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (" DEFAULT " . $field[1])); |
|
| 1342 | - } |
|
| 1343 | - $connexion['last'] = $q = "SELECT indexdef FROM pg_indexes WHERE tablename ILIKE " . _q($nom_table); |
|
| 1344 | - $res = spip_pg_query_simple($link, $q); |
|
| 1345 | - $keys = array(); |
|
| 1346 | - while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1347 | - if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1348 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1349 | - $keys[($r[1] ? "PRIMARY KEY" : ("KEY " . $nom))] = $r[3]; |
|
| 1350 | - } |
|
| 1351 | - } |
|
| 1352 | - |
|
| 1353 | - return count($fields) ? array('field' => $fields, 'key' => $keys) : false; |
|
| 1328 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1329 | + $link = $connexion['link']; |
|
| 1330 | + $connexion['last'] = $q = "SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE " . _q($nom_table); |
|
| 1331 | + |
|
| 1332 | + $res = spip_pg_query_simple($link, $q); |
|
| 1333 | + if (!$res) { |
|
| 1334 | + return false; |
|
| 1335 | + } |
|
| 1336 | + |
|
| 1337 | + // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1338 | + // il faut en tenir compte dans le return |
|
| 1339 | + $fields = array(); |
|
| 1340 | + while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1341 | + $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (" DEFAULT " . $field[1])); |
|
| 1342 | + } |
|
| 1343 | + $connexion['last'] = $q = "SELECT indexdef FROM pg_indexes WHERE tablename ILIKE " . _q($nom_table); |
|
| 1344 | + $res = spip_pg_query_simple($link, $q); |
|
| 1345 | + $keys = array(); |
|
| 1346 | + while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1347 | + if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1348 | + $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1349 | + $keys[($r[1] ? "PRIMARY KEY" : ("KEY " . $nom))] = $r[3]; |
|
| 1350 | + } |
|
| 1351 | + } |
|
| 1352 | + |
|
| 1353 | + return count($fields) ? array('field' => $fields, 'key' => $keys) : false; |
|
| 1354 | 1354 | } |
| 1355 | 1355 | |
| 1356 | 1356 | // Fonction de creation d'une table SQL nommee $nom |
@@ -1362,118 +1362,118 @@ discard block |
||
| 1362 | 1362 | // https://code.spip.net/@spip_pg_create |
| 1363 | 1363 | function spip_pg_create($nom, $champs, $cles, $autoinc = false, $temporary = false, $serveur = '', $requeter = true) { |
| 1364 | 1364 | |
| 1365 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1366 | - $link = $connexion['link']; |
|
| 1367 | - $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1368 | - |
|
| 1369 | - $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1370 | - $keys = array(); |
|
| 1371 | - |
|
| 1372 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1373 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1374 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 1375 | - return; |
|
| 1376 | - } |
|
| 1377 | - |
|
| 1378 | - foreach ($cles as $k => $v) { |
|
| 1379 | - if (strpos($k, "KEY ") === 0) { |
|
| 1380 | - $n = str_replace('`', '', $k); |
|
| 1381 | - $v = str_replace('`', '"', $v); |
|
| 1382 | - $i = $nom . preg_replace("/KEY +/", '_', $n); |
|
| 1383 | - if ($k != $n) { |
|
| 1384 | - $i = "\"$i\""; |
|
| 1385 | - } |
|
| 1386 | - $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1387 | - } elseif (strpos($k, "UNIQUE ") === 0) { |
|
| 1388 | - $k = preg_replace("/^UNIQUE +/", '', $k); |
|
| 1389 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1390 | - } else { |
|
| 1391 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1392 | - } |
|
| 1393 | - if ($k == "PRIMARY KEY") { |
|
| 1394 | - $prim_name = $v; |
|
| 1395 | - } |
|
| 1396 | - $s = ","; |
|
| 1397 | - } |
|
| 1398 | - $s = ''; |
|
| 1399 | - |
|
| 1400 | - $character_set = ""; |
|
| 1401 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1402 | - $character_set .= " CHARACTER SET " . $GLOBALS['meta']['charset_sql_base']; |
|
| 1403 | - } |
|
| 1404 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1405 | - $character_set .= " COLLATE " . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1406 | - } |
|
| 1407 | - |
|
| 1408 | - foreach ($champs as $k => $v) { |
|
| 1409 | - $k = str_replace('`', '"', $k); |
|
| 1410 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1411 | - if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1412 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1413 | - } |
|
| 1414 | - } |
|
| 1415 | - |
|
| 1416 | - $query .= "$s\n\t\t$k " |
|
| 1417 | - . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1418 | - ? " bigserial" |
|
| 1419 | - : mysql2pg_type($v) |
|
| 1420 | - ); |
|
| 1421 | - $s = ","; |
|
| 1422 | - } |
|
| 1423 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1424 | - |
|
| 1425 | - // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1426 | - |
|
| 1427 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ")" . |
|
| 1428 | - ($character_set ? " DEFAULT $character_set" : "") |
|
| 1429 | - . "\n"; |
|
| 1430 | - |
|
| 1431 | - if (!$requeter) { |
|
| 1432 | - return $q; |
|
| 1433 | - } |
|
| 1434 | - $connexion['last'] = $q; |
|
| 1435 | - $r = @pg_query($link, $q); |
|
| 1436 | - |
|
| 1437 | - if (!$r) { |
|
| 1438 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1439 | - } else { |
|
| 1440 | - foreach ($keys as $index) { |
|
| 1441 | - pg_query($link, $index); |
|
| 1442 | - } |
|
| 1443 | - } |
|
| 1444 | - |
|
| 1445 | - return $r; |
|
| 1365 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1366 | + $link = $connexion['link']; |
|
| 1367 | + $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1368 | + |
|
| 1369 | + $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1370 | + $keys = array(); |
|
| 1371 | + |
|
| 1372 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1373 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1374 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 1375 | + return; |
|
| 1376 | + } |
|
| 1377 | + |
|
| 1378 | + foreach ($cles as $k => $v) { |
|
| 1379 | + if (strpos($k, "KEY ") === 0) { |
|
| 1380 | + $n = str_replace('`', '', $k); |
|
| 1381 | + $v = str_replace('`', '"', $v); |
|
| 1382 | + $i = $nom . preg_replace("/KEY +/", '_', $n); |
|
| 1383 | + if ($k != $n) { |
|
| 1384 | + $i = "\"$i\""; |
|
| 1385 | + } |
|
| 1386 | + $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1387 | + } elseif (strpos($k, "UNIQUE ") === 0) { |
|
| 1388 | + $k = preg_replace("/^UNIQUE +/", '', $k); |
|
| 1389 | + $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1390 | + } else { |
|
| 1391 | + $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1392 | + } |
|
| 1393 | + if ($k == "PRIMARY KEY") { |
|
| 1394 | + $prim_name = $v; |
|
| 1395 | + } |
|
| 1396 | + $s = ","; |
|
| 1397 | + } |
|
| 1398 | + $s = ''; |
|
| 1399 | + |
|
| 1400 | + $character_set = ""; |
|
| 1401 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1402 | + $character_set .= " CHARACTER SET " . $GLOBALS['meta']['charset_sql_base']; |
|
| 1403 | + } |
|
| 1404 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1405 | + $character_set .= " COLLATE " . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1406 | + } |
|
| 1407 | + |
|
| 1408 | + foreach ($champs as $k => $v) { |
|
| 1409 | + $k = str_replace('`', '"', $k); |
|
| 1410 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1411 | + if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1412 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1413 | + } |
|
| 1414 | + } |
|
| 1415 | + |
|
| 1416 | + $query .= "$s\n\t\t$k " |
|
| 1417 | + . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1418 | + ? " bigserial" |
|
| 1419 | + : mysql2pg_type($v) |
|
| 1420 | + ); |
|
| 1421 | + $s = ","; |
|
| 1422 | + } |
|
| 1423 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1424 | + |
|
| 1425 | + // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1426 | + |
|
| 1427 | + $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ")" . |
|
| 1428 | + ($character_set ? " DEFAULT $character_set" : "") |
|
| 1429 | + . "\n"; |
|
| 1430 | + |
|
| 1431 | + if (!$requeter) { |
|
| 1432 | + return $q; |
|
| 1433 | + } |
|
| 1434 | + $connexion['last'] = $q; |
|
| 1435 | + $r = @pg_query($link, $q); |
|
| 1436 | + |
|
| 1437 | + if (!$r) { |
|
| 1438 | + spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1439 | + } else { |
|
| 1440 | + foreach ($keys as $index) { |
|
| 1441 | + pg_query($link, $index); |
|
| 1442 | + } |
|
| 1443 | + } |
|
| 1444 | + |
|
| 1445 | + return $r; |
|
| 1446 | 1446 | } |
| 1447 | 1447 | |
| 1448 | 1448 | |
| 1449 | 1449 | function spip_pg_create_base($nom, $serveur = '', $requeter = true) { |
| 1450 | - return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1450 | + return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1451 | 1451 | } |
| 1452 | 1452 | |
| 1453 | 1453 | // Fonction de creation d'une vue SQL nommee $nom |
| 1454 | 1454 | // https://code.spip.net/@spip_pg_create_view |
| 1455 | 1455 | function spip_pg_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 1456 | - if (!$query_select) { |
|
| 1457 | - return false; |
|
| 1458 | - } |
|
| 1459 | - // vue deja presente |
|
| 1460 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 1461 | - if ($requeter) { |
|
| 1462 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1463 | - } |
|
| 1456 | + if (!$query_select) { |
|
| 1457 | + return false; |
|
| 1458 | + } |
|
| 1459 | + // vue deja presente |
|
| 1460 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 1461 | + if ($requeter) { |
|
| 1462 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1463 | + } |
|
| 1464 | 1464 | |
| 1465 | - return false; |
|
| 1466 | - } |
|
| 1465 | + return false; |
|
| 1466 | + } |
|
| 1467 | 1467 | |
| 1468 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1468 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1469 | 1469 | |
| 1470 | - return spip_pg_query($query, $serveur, $requeter); |
|
| 1470 | + return spip_pg_query($query, $serveur, $requeter); |
|
| 1471 | 1471 | } |
| 1472 | 1472 | |
| 1473 | 1473 | |
| 1474 | 1474 | // https://code.spip.net/@spip_pg_set_connect_charset |
| 1475 | 1475 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1476 | - spip_log("changement de charset sql a ecrire en PG", 'pg.' . _LOG_ERREUR); |
|
| 1476 | + spip_log("changement de charset sql a ecrire en PG", 'pg.' . _LOG_ERREUR); |
|
| 1477 | 1477 | } |
| 1478 | 1478 | |
| 1479 | 1479 | |
@@ -1487,7 +1487,7 @@ discard block |
||
| 1487 | 1487 | **/ |
| 1488 | 1488 | // https://code.spip.net/@spip_sqlite_optimize |
| 1489 | 1489 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1490 | - return spip_pg_query("VACUUM " . $table, $serveur, $requeter); |
|
| 1490 | + return spip_pg_query("VACUUM " . $table, $serveur, $requeter); |
|
| 1491 | 1491 | } |
| 1492 | 1492 | |
| 1493 | 1493 | // Selectionner la sous-chaine dans $objet |
@@ -1495,13 +1495,13 @@ discard block |
||
| 1495 | 1495 | |
| 1496 | 1496 | // https://code.spip.net/@spip_pg_multi |
| 1497 | 1497 | function spip_pg_multi($objet, $lang) { |
| 1498 | - $r = "regexp_replace(" |
|
| 1499 | - . $objet |
|
| 1500 | - . ",'<multi>.*[[]" |
|
| 1501 | - . $lang |
|
| 1502 | - . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1498 | + $r = "regexp_replace(" |
|
| 1499 | + . $objet |
|
| 1500 | + . ",'<multi>.*[[]" |
|
| 1501 | + . $lang |
|
| 1502 | + . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1503 | 1503 | |
| 1504 | - return $r; |
|
| 1504 | + return $r; |
|
| 1505 | 1505 | } |
| 1506 | 1506 | |
| 1507 | 1507 | // Palanquee d'idiosyncrasies MySQL dans les creations de table |
@@ -1509,31 +1509,31 @@ discard block |
||
| 1509 | 1509 | |
| 1510 | 1510 | // https://code.spip.net/@mysql2pg_type |
| 1511 | 1511 | function mysql2pg_type($v) { |
| 1512 | - $remplace = array( |
|
| 1513 | - '/auto_increment/i' => '', // non reconnu |
|
| 1514 | - '/bigint/i' => 'bigint', |
|
| 1515 | - '/mediumint/i' => 'mediumint', |
|
| 1516 | - '/smallint/i' => 'smallint', |
|
| 1517 | - "/tinyint/i" => 'int', |
|
| 1518 | - '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1519 | - "/longtext/i" => 'text', |
|
| 1520 | - "/mediumtext/i" => 'text', |
|
| 1521 | - "/tinytext/i" => 'text', |
|
| 1522 | - "/longblob/i" => 'text', |
|
| 1523 | - "/0000-00-00/" => '0001-01-01', |
|
| 1524 | - "/datetime/i" => 'timestamp', |
|
| 1525 | - "/unsigned/i" => '', |
|
| 1526 | - "/double/i" => 'double precision', |
|
| 1527 | - '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1528 | - "/ENUM *[(][^)]*[)]/i" => "varchar(255)", |
|
| 1529 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1530 | - ); |
|
| 1531 | - |
|
| 1532 | - return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1512 | + $remplace = array( |
|
| 1513 | + '/auto_increment/i' => '', // non reconnu |
|
| 1514 | + '/bigint/i' => 'bigint', |
|
| 1515 | + '/mediumint/i' => 'mediumint', |
|
| 1516 | + '/smallint/i' => 'smallint', |
|
| 1517 | + "/tinyint/i" => 'int', |
|
| 1518 | + '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1519 | + "/longtext/i" => 'text', |
|
| 1520 | + "/mediumtext/i" => 'text', |
|
| 1521 | + "/tinytext/i" => 'text', |
|
| 1522 | + "/longblob/i" => 'text', |
|
| 1523 | + "/0000-00-00/" => '0001-01-01', |
|
| 1524 | + "/datetime/i" => 'timestamp', |
|
| 1525 | + "/unsigned/i" => '', |
|
| 1526 | + "/double/i" => 'double precision', |
|
| 1527 | + '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1528 | + "/ENUM *[(][^)]*[)]/i" => "varchar(255)", |
|
| 1529 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1530 | + ); |
|
| 1531 | + |
|
| 1532 | + return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1533 | 1533 | } |
| 1534 | 1534 | |
| 1535 | 1535 | // Renvoie false si on n'a pas les fonctions pg (pour l'install) |
| 1536 | 1536 | // https://code.spip.net/@spip_versions_pg |
| 1537 | 1537 | function spip_versions_pg() { |
| 1538 | - return function_exists('pg_connect'); |
|
| 1538 | + return function_exists('pg_connect'); |
|
| 1539 | 1539 | } |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | if (!$link) { |
| 66 | 66 | $erreurs[] = pg_last_error(); |
| 67 | 67 | foreach ($erreurs as $e) { |
| 68 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 68 | + spip_log('Echec pg_connect. Erreur : '.$e, 'pg.'._LOG_HS); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | return false; |
@@ -82,8 +82,8 @@ discard block |
||
| 82 | 82 | ); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - spip_log("Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 86 | - 'pg.' . _LOG_DEBUG); |
|
| 85 | + spip_log("Connexion vers $host, base $db, prefixe $prefixe ".($link ? 'operationnelle' : 'impossible'), |
|
| 86 | + 'pg.'._LOG_DEBUG); |
|
| 87 | 87 | |
| 88 | 88 | return !$link ? false : array( |
| 89 | 89 | 'db' => $db, |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | } else { |
| 173 | 173 | $suite = ''; |
| 174 | 174 | } |
| 175 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 175 | + $query = preg_replace('/([,\s])spip_/', '\1'.$prefixe.'_', $query).$suite; |
|
| 176 | 176 | |
| 177 | 177 | // renvoyer la requete inerte si demandee |
| 178 | 178 | if (!$requeter) { |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | // ou revoir l'api de sql_alter en creant un |
| 243 | 243 | // sql_alter_table($table,array($actions)); |
| 244 | 244 | if (!preg_match("/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is", $query, $regs)) { |
| 245 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 245 | + spip_log("$query mal comprise", 'pg.'._LOG_ERREUR); |
|
| 246 | 246 | |
| 247 | 247 | return false; |
| 248 | 248 | } |
@@ -255,7 +255,7 @@ discard block |
||
| 255 | 255 | $i = 0; |
| 256 | 256 | $ouverte = false; |
| 257 | 257 | while ($do = array_shift($todo)) { |
| 258 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . "," . $do : $do; |
|
| 258 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i].",".$do : $do; |
|
| 259 | 259 | $o = (false !== strpos($do, "(")); |
| 260 | 260 | $f = (false !== strpos($do, ")")); |
| 261 | 261 | if ($o and !$f) { |
@@ -268,24 +268,24 @@ discard block |
||
| 268 | 268 | } |
| 269 | 269 | } |
| 270 | 270 | $todo = $todo2; |
| 271 | - $query = $debut . ' ' . array_shift($todo); |
|
| 271 | + $query = $debut.' '.array_shift($todo); |
|
| 272 | 272 | |
| 273 | 273 | if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
| 274 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 274 | + spip_log("$query incompris", 'pg.'._LOG_ERREUR); |
|
| 275 | 275 | } else { |
| 276 | 276 | if ($r[1]) { |
| 277 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 277 | + spip_log("j'ignore IGNORE dans $query", 'pg.'._LOG_AVERTISSEMENT); |
|
| 278 | 278 | } |
| 279 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 279 | + $f = 'spip_pg_alter_'.strtolower($r[3]); |
|
| 280 | 280 | if (function_exists($f)) { |
| 281 | 281 | $f($r[2], $r[4], $serveur, $requeter); |
| 282 | 282 | } else { |
| 283 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 283 | + spip_log("$query non prevu", 'pg.'._LOG_ERREUR); |
|
| 284 | 284 | } |
| 285 | 285 | } |
| 286 | 286 | // Alter a plusieurs args. Faudrait optimiser. |
| 287 | 287 | if ($todo) { |
| 288 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 288 | + spip_pg_alter("TABLE $table ".join(',', $todo)); |
|
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | } |
@@ -293,10 +293,10 @@ discard block |
||
| 293 | 293 | // https://code.spip.net/@spip_pg_alter_change |
| 294 | 294 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 295 | 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); |
|
| 296 | + spip_log("alter change: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 297 | 297 | } else { |
| 298 | 298 | list(, $old, $new, $type, $default, $null, $def2) = $r; |
| 299 | - $actions = array("ALTER $old TYPE " . mysql2pg_type($type)); |
|
| 299 | + $actions = array("ALTER $old TYPE ".mysql2pg_type($type)); |
|
| 300 | 300 | if ($null) { |
| 301 | 301 | $actions[] = "ALTER $old SET NOT NULL"; |
| 302 | 302 | } else { |
@@ -309,7 +309,7 @@ discard block |
||
| 309 | 309 | $actions[] = "ALTER $old DROP DEFAULT"; |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | - spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 312 | + spip_pg_query("ALTER TABLE $table ".join(', ', $actions)); |
|
| 313 | 313 | |
| 314 | 314 | if ($old != $new) { |
| 315 | 315 | spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | // https://code.spip.net/@spip_pg_alter_add |
| 321 | 321 | function spip_pg_alter_add($table, $arg, $serveur = '', $requeter = true) { |
| 322 | 322 | if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
| 323 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 323 | + spip_log("alter add $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 324 | 324 | |
| 325 | 325 | return null; |
| 326 | 326 | } |
@@ -330,13 +330,13 @@ discard block |
||
| 330 | 330 | $m[2] = $n[1]; |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 333 | + return spip_pg_query("ALTER TABLE $table ADD ".$m[1].' '.mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 334 | 334 | } elseif ($r[1][0] == 'P') { |
| 335 | 335 | // la primary peut etre sur plusieurs champs |
| 336 | 336 | $r[2] = trim(str_replace('`', '', $r[2])); |
| 337 | 337 | $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
| 338 | 338 | |
| 339 | - return spip_pg_query("ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', $serveur, |
|
| 339 | + return spip_pg_query("ALTER TABLE $table ADD CONSTRAINT $table".'_pkey PRIMARY KEY ('.$m.')', $serveur, |
|
| 340 | 340 | $requeter); |
| 341 | 341 | } else { |
| 342 | 342 | preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | $colonnes = substr($m[1], 1, -1); |
| 354 | 354 | if (false !== strpos(",", $colonnes)) { |
| 355 | 355 | spip_log("PG : Erreur, impossible de creer un index sur plusieurs colonnes" |
| 356 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 356 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.'._LOG_ERREUR); |
|
| 357 | 357 | } else { |
| 358 | 358 | $nom_index = $colonnes; |
| 359 | 359 | } |
@@ -370,23 +370,23 @@ discard block |
||
| 370 | 370 | // https://code.spip.net/@spip_pg_alter_drop |
| 371 | 371 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 372 | 372 | if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
| 373 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 373 | + spip_log("alter drop: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 374 | 374 | } else { |
| 375 | 375 | if (!$r[1] or $r[1] == 'COLUMN') { |
| 376 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 376 | + return spip_pg_query("ALTER TABLE $table DROP ".$r[2], $serveur); |
|
| 377 | 377 | } elseif ($r[1][0] == 'P') { |
| 378 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 378 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table".'_pkey', $serveur); |
|
| 379 | 379 | } else { |
| 380 | - return spip_pg_query("DROP INDEX " . $table . '_' . $r[2], $serveur); |
|
| 380 | + return spip_pg_query("DROP INDEX ".$table.'_'.$r[2], $serveur); |
|
| 381 | 381 | } |
| 382 | 382 | } |
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 386 | 386 | if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
| 387 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 387 | + spip_log("alter modify: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 388 | 388 | } else { |
| 389 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 389 | + return spip_pg_alter_change($table, $r[1].' '.$arg, $serveur = '', $requeter = true); |
|
| 390 | 390 | } |
| 391 | 391 | } |
| 392 | 392 | |
@@ -402,7 +402,7 @@ discard block |
||
| 402 | 402 | } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
| 403 | 403 | $rename = $r[2]; |
| 404 | 404 | } else { |
| 405 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 405 | + spip_log("alter rename: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
@@ -422,8 +422,8 @@ discard block |
||
| 422 | 422 | */ |
| 423 | 423 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 424 | 424 | if (!($nom or $table or $champs)) { |
| 425 | - spip_log("Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . "))", |
|
| 426 | - 'pg.' . _LOG_ERREUR); |
|
| 425 | + spip_log("Champ manquant pour creer un index pg ($nom, $table, (".@join(',', $champs)."))", |
|
| 426 | + 'pg.'._LOG_ERREUR); |
|
| 427 | 427 | |
| 428 | 428 | return false; |
| 429 | 429 | } |
@@ -433,7 +433,7 @@ discard block |
||
| 433 | 433 | |
| 434 | 434 | // PG ne differentie pas noms des index en fonction des tables |
| 435 | 435 | // il faut donc creer des noms uniques d'index pour une base pg |
| 436 | - $nom = $table . '_' . $nom; |
|
| 436 | + $nom = $table.'_'.$nom; |
|
| 437 | 437 | // enlever d'eventuelles parentheses deja presentes sur champs |
| 438 | 438 | if (!is_array($champs)) { |
| 439 | 439 | if ($champs[0] == "(") { |
@@ -441,7 +441,7 @@ discard block |
||
| 441 | 441 | } |
| 442 | 442 | $champs = array($champs); |
| 443 | 443 | } |
| 444 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ")"; |
|
| 444 | + $query = "CREATE INDEX $nom ON $table (".join(',', $champs).")"; |
|
| 445 | 445 | if (!$requeter) { |
| 446 | 446 | return $query; |
| 447 | 447 | } |
@@ -465,7 +465,7 @@ discard block |
||
| 465 | 465 | } else { |
| 466 | 466 | $suite = ''; |
| 467 | 467 | } |
| 468 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 468 | + $query = 'EXPLAIN '.preg_replace('/([,\s])spip_/', '\1'.$prefixe.'_', $query).$suite; |
|
| 469 | 469 | |
| 470 | 470 | if (!$requeter) { |
| 471 | 471 | return $query; |
@@ -560,14 +560,14 @@ discard block |
||
| 560 | 560 | } |
| 561 | 561 | } |
| 562 | 562 | $from = spip_pg_from($from, $prefixe); |
| 563 | - $query = "SELECT " . $select |
|
| 563 | + $query = "SELECT ".$select |
|
| 564 | 564 | . (!$from ? '' : "\nFROM $from") |
| 565 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join("\n\tAND ", |
|
| 565 | + . (!$where ? '' : ("\nWHERE ".(!is_array($where) ? calculer_pg_where($where) : (join("\n\tAND ", |
|
| 566 | 566 | array_map('calculer_pg_where', $where)))))) |
| 567 | 567 | . spip_pg_groupby($groupby, $from, $select) |
| 568 | 568 | . (!$having ? '' : "\nHAVING $having") |
| 569 | 569 | . ($orderby ? ("\nORDER BY $orderby") : '') |
| 570 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 570 | + . (!$limit ? '' : (" LIMIT $count".(!$offset ? '' : " OFFSET $offset"))); |
|
| 571 | 571 | |
| 572 | 572 | // renvoyer la requete inerte si demandee |
| 573 | 573 | if ($requeter === false) { |
@@ -576,7 +576,7 @@ discard block |
||
| 576 | 576 | |
| 577 | 577 | $r = spip_pg_trace_query($query, $serveur); |
| 578 | 578 | |
| 579 | - return $r ? $r : $query;; |
|
| 579 | + return $r ? $r : $query; ; |
|
| 580 | 580 | } |
| 581 | 581 | |
| 582 | 582 | // Le traitement des prefixes de table dans un Select se limite au FROM |
@@ -588,7 +588,7 @@ discard block |
||
| 588 | 588 | $from = spip_pg_select_as($from); |
| 589 | 589 | } |
| 590 | 590 | |
| 591 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 591 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1'.$prefixe.'_', $from); |
|
| 592 | 592 | } |
| 593 | 593 | |
| 594 | 594 | // https://code.spip.net/@spip_pg_orderby |
@@ -597,7 +597,7 @@ discard block |
||
| 597 | 597 | $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
| 598 | 598 | |
| 599 | 599 | foreach ($arg as $v) { |
| 600 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 600 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+'.$v.'/', $select, $m)) { |
|
| 601 | 601 | |
| 602 | 602 | $res[] = $m[1]; |
| 603 | 603 | } else { |
@@ -758,7 +758,7 @@ discard block |
||
| 758 | 758 | $n++; |
| 759 | 759 | $res .= "\nwhen $index=$v then $n"; |
| 760 | 760 | } |
| 761 | - $arg = $m[1] . "case $res else 0 end " |
|
| 761 | + $arg = $m[1]."case $res else 0 end " |
|
| 762 | 762 | . substr($arg, strlen($m[0])); |
| 763 | 763 | } |
| 764 | 764 | |
@@ -803,9 +803,9 @@ discard block |
||
| 803 | 803 | } |
| 804 | 804 | |
| 805 | 805 | if (strtoupper($join) === 'AND') { |
| 806 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 806 | + return $exp.join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 807 | 807 | } else { |
| 808 | - return $exp . join($join, $v); |
|
| 808 | + return $exp.join($join, $v); |
|
| 809 | 809 | } |
| 810 | 810 | } |
| 811 | 811 | |
@@ -816,7 +816,7 @@ discard block |
||
| 816 | 816 | if (substr($k, -1) == '@') { |
| 817 | 817 | // c'est une jointure qui se refere au from precedent |
| 818 | 818 | // pas de virgule |
| 819 | - $argsas .= ' ' . $v; |
|
| 819 | + $argsas .= ' '.$v; |
|
| 820 | 820 | } else { |
| 821 | 821 | $as = ''; |
| 822 | 822 | // spip_log("$k : $v", _LOG_DEBUG); |
@@ -826,7 +826,7 @@ discard block |
||
| 826 | 826 | } elseif ($v != $k) { |
| 827 | 827 | $p = strpos($v, " "); |
| 828 | 828 | if ($p) { |
| 829 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 829 | + $v = substr($v, 0, $p)." AS $k".substr($v, $p); |
|
| 830 | 830 | } else { |
| 831 | 831 | $as = " AS $k"; |
| 832 | 832 | } |
@@ -834,7 +834,7 @@ discard block |
||
| 834 | 834 | } |
| 835 | 835 | // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
| 836 | 836 | // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
| 837 | - $argsas .= ', ' . $v . $as; |
|
| 837 | + $argsas .= ', '.$v.$as; |
|
| 838 | 838 | } |
| 839 | 839 | } |
| 840 | 840 | |
@@ -867,7 +867,7 @@ discard block |
||
| 867 | 867 | $serveur = '', |
| 868 | 868 | $requeter = true |
| 869 | 869 | ) { |
| 870 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 870 | + $c = !$groupby ? '*' : ('DISTINCT '.(is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 871 | 871 | $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
| 872 | 872 | if (!$requeter) { |
| 873 | 873 | return $r; |
@@ -971,7 +971,7 @@ discard block |
||
| 971 | 971 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
| 972 | 972 | $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
| 973 | 973 | |
| 974 | - return spip_pg_insert($table, "(" . join(',', array_keys($couples)) . ")", "(" . join(',', $couples) . ")", $desc, |
|
| 974 | + return spip_pg_insert($table, "(".join(',', array_keys($couples)).")", "(".join(',', $couples).")", $desc, |
|
| 975 | 975 | $serveur, $requeter); |
| 976 | 976 | } |
| 977 | 977 | |
@@ -992,7 +992,7 @@ discard block |
||
| 992 | 992 | $c = isset($tab_couples[0]) ? $tab_couples[0] : array(); |
| 993 | 993 | $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
| 994 | 994 | |
| 995 | - $cles = "(" . join(',', array_keys($les_cles)) . ')'; |
|
| 995 | + $cles = "(".join(',', array_keys($les_cles)).')'; |
|
| 996 | 996 | $valeurs = array(); |
| 997 | 997 | foreach ($tab_couples as $couples) { |
| 998 | 998 | foreach ($couples as $champ => $val) { |
@@ -1001,7 +1001,7 @@ discard block |
||
| 1001 | 1001 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
| 1002 | 1002 | $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
| 1003 | 1003 | |
| 1004 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1004 | + $valeurs[] = '('.join(',', $couples).')'; |
|
| 1005 | 1005 | } |
| 1006 | 1006 | $valeurs = implode(', ', $valeurs); |
| 1007 | 1007 | |
@@ -1023,7 +1023,7 @@ discard block |
||
| 1023 | 1023 | |
| 1024 | 1024 | $set = array(); |
| 1025 | 1025 | foreach ($couples as $champ => $val) { |
| 1026 | - $set[] = $champ . '=' . $val; |
|
| 1026 | + $set[] = $champ.'='.$val; |
|
| 1027 | 1027 | } |
| 1028 | 1028 | |
| 1029 | 1029 | $query = calculer_pg_expression('UPDATE', $table, ',') |
@@ -1060,7 +1060,7 @@ discard block |
||
| 1060 | 1060 | // https://code.spip.net/@spip_pg_replace |
| 1061 | 1061 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1062 | 1062 | if (!$values) { |
| 1063 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1063 | + spip_log("replace vide $table", 'pg.'._LOG_AVERTISSEMENT); |
|
| 1064 | 1064 | |
| 1065 | 1065 | return 0; |
| 1066 | 1066 | } |
@@ -1092,7 +1092,7 @@ discard block |
||
| 1092 | 1092 | |
| 1093 | 1093 | $where = join(' AND ', $prims); |
| 1094 | 1094 | if (!$where) { |
| 1095 | - return spip_pg_insert($table, "(" . join(',', array_keys($values)) . ")", "(" . join(',', $values) . ")", $desc, |
|
| 1095 | + return spip_pg_insert($table, "(".join(',', array_keys($values)).")", "(".join(',', $values).")", $desc, |
|
| 1096 | 1096 | $serveur); |
| 1097 | 1097 | } |
| 1098 | 1098 | $couples = join(',', $noprims); |
@@ -1111,10 +1111,9 @@ discard block |
||
| 1111 | 1111 | $couples = pg_affected_rows($couples); |
| 1112 | 1112 | } |
| 1113 | 1113 | if (!$couples) { |
| 1114 | - $ret = !$seq ? '' : |
|
| 1115 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1116 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join(',', |
|
| 1117 | - $values) . ")$ret"; |
|
| 1114 | + $ret = !$seq ? '' : (" RETURNING nextval('$seq') < $prim"); |
|
| 1115 | + $connexion['last'] = $q = "INSERT INTO $table (".join(',', array_keys($values)).') VALUES ('.join(',', |
|
| 1116 | + $values).")$ret"; |
|
| 1118 | 1117 | $couples = spip_pg_query_simple($link, $q); |
| 1119 | 1118 | if (!$couples) { |
| 1120 | 1119 | return false; |
@@ -1162,7 +1161,7 @@ discard block |
||
| 1162 | 1161 | ) { |
| 1163 | 1162 | return ''; |
| 1164 | 1163 | } else { |
| 1165 | - return $raw ? $prim : $table . '_' . $prim . "_seq"; |
|
| 1164 | + return $raw ? $prim : $table.'_'.$prim."_seq"; |
|
| 1166 | 1165 | } |
| 1167 | 1166 | } |
| 1168 | 1167 | |
@@ -1180,22 +1179,22 @@ discard block |
||
| 1180 | 1179 | return spip_pg_frommysql($v); |
| 1181 | 1180 | } else { |
| 1182 | 1181 | if (strncmp($v, '0000', 4) == 0) { |
| 1183 | - $v = "0001" . substr($v, 4); |
|
| 1182 | + $v = "0001".substr($v, 4); |
|
| 1184 | 1183 | } |
| 1185 | 1184 | if (strpos($v, "-00-00") === 4) { |
| 1186 | - $v = substr($v, 0, 4) . "-01-01" . substr($v, 10); |
|
| 1185 | + $v = substr($v, 0, 4)."-01-01".substr($v, 10); |
|
| 1187 | 1186 | } |
| 1188 | 1187 | |
| 1189 | 1188 | return "timestamp '$v'"; |
| 1190 | 1189 | } |
| 1191 | 1190 | } elseif (!sql_test_int($t)) { |
| 1192 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1191 | + return ("'".pg_escape_string($v)."'"); |
|
| 1193 | 1192 | } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
| 1194 | 1193 | return $v; |
| 1195 | 1194 | } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
| 1196 | 1195 | return substr($v, 1); |
| 1197 | 1196 | } else { |
| 1198 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1197 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.'._LOG_AVERTISSEMENT); |
|
| 1199 | 1198 | |
| 1200 | 1199 | return intval($v); |
| 1201 | 1200 | } |
@@ -1203,7 +1202,7 @@ discard block |
||
| 1203 | 1202 | |
| 1204 | 1203 | // https://code.spip.net/@spip_pg_hex |
| 1205 | 1204 | function spip_pg_hex($v) { |
| 1206 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1205 | + return "CAST(x'".$v."' as bigint)"; |
|
| 1207 | 1206 | } |
| 1208 | 1207 | |
| 1209 | 1208 | function spip_pg_quote($v, $type = '') { |
@@ -1242,15 +1241,15 @@ discard block |
||
| 1242 | 1241 | return $not ? '0=0' : '0=1'; |
| 1243 | 1242 | } |
| 1244 | 1243 | if (strpos($valeurs, "CAST(x'") !== false) { |
| 1245 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1244 | + return "($val=".join("OR $val=", explode(',', $valeurs)).')'; |
|
| 1246 | 1245 | } |
| 1247 | 1246 | $n = $i = 0; |
| 1248 | 1247 | $in_sql = ""; |
| 1249 | 1248 | while ($n = strpos($valeurs, ',', $n + 1)) { |
| 1250 | 1249 | if ((++$i) >= 255) { |
| 1251 | - $in_sql .= "($val $not IN (" . |
|
| 1252 | - substr($valeurs, 0, $n) . |
|
| 1253 | - "))\n" . |
|
| 1250 | + $in_sql .= "($val $not IN (". |
|
| 1251 | + substr($valeurs, 0, $n). |
|
| 1252 | + "))\n". |
|
| 1254 | 1253 | ($not ? "AND\t" : "OR\t"); |
| 1255 | 1254 | $valeurs = substr($valeurs, $n + 1); |
| 1256 | 1255 | $i = $n = 0; |
@@ -1267,7 +1266,7 @@ discard block |
||
| 1267 | 1266 | $s = $link ? pg_last_error($link) : pg_last_error(); |
| 1268 | 1267 | if ($s) { |
| 1269 | 1268 | $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
| 1270 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1269 | + spip_log("$s - $query", 'pg.'._LOG_ERREUR); |
|
| 1271 | 1270 | } |
| 1272 | 1271 | |
| 1273 | 1272 | return $s; |
@@ -1318,7 +1317,7 @@ discard block |
||
| 1318 | 1317 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1319 | 1318 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 1320 | 1319 | $link = $connexion['link']; |
| 1321 | - $connexion['last'] = $q = "SELECT tablename FROM pg_tables WHERE tablename ILIKE " . _q($match); |
|
| 1320 | + $connexion['last'] = $q = "SELECT tablename FROM pg_tables WHERE tablename ILIKE "._q($match); |
|
| 1322 | 1321 | |
| 1323 | 1322 | return spip_pg_query_simple($link, $q); |
| 1324 | 1323 | } |
@@ -1327,7 +1326,7 @@ discard block |
||
| 1327 | 1326 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1328 | 1327 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 1329 | 1328 | $link = $connexion['link']; |
| 1330 | - $connexion['last'] = $q = "SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE " . _q($nom_table); |
|
| 1329 | + $connexion['last'] = $q = "SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE "._q($nom_table); |
|
| 1331 | 1330 | |
| 1332 | 1331 | $res = spip_pg_query_simple($link, $q); |
| 1333 | 1332 | if (!$res) { |
@@ -1338,15 +1337,15 @@ discard block |
||
| 1338 | 1337 | // il faut en tenir compte dans le return |
| 1339 | 1338 | $fields = array(); |
| 1340 | 1339 | while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
| 1341 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (" DEFAULT " . $field[1])); |
|
| 1340 | + $fields[$field[0]] = $field[2].(!$field[1] ? '' : (" DEFAULT ".$field[1])); |
|
| 1342 | 1341 | } |
| 1343 | - $connexion['last'] = $q = "SELECT indexdef FROM pg_indexes WHERE tablename ILIKE " . _q($nom_table); |
|
| 1342 | + $connexion['last'] = $q = "SELECT indexdef FROM pg_indexes WHERE tablename ILIKE "._q($nom_table); |
|
| 1344 | 1343 | $res = spip_pg_query_simple($link, $q); |
| 1345 | 1344 | $keys = array(); |
| 1346 | 1345 | while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
| 1347 | 1346 | if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
| 1348 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1349 | - $keys[($r[1] ? "PRIMARY KEY" : ("KEY " . $nom))] = $r[3]; |
|
| 1347 | + $nom = str_replace($nom_table.'_', '', $r[2]); |
|
| 1348 | + $keys[($r[1] ? "PRIMARY KEY" : ("KEY ".$nom))] = $r[3]; |
|
| 1350 | 1349 | } |
| 1351 | 1350 | } |
| 1352 | 1351 | |
@@ -1379,16 +1378,16 @@ discard block |
||
| 1379 | 1378 | if (strpos($k, "KEY ") === 0) { |
| 1380 | 1379 | $n = str_replace('`', '', $k); |
| 1381 | 1380 | $v = str_replace('`', '"', $v); |
| 1382 | - $i = $nom . preg_replace("/KEY +/", '_', $n); |
|
| 1381 | + $i = $nom.preg_replace("/KEY +/", '_', $n); |
|
| 1383 | 1382 | if ($k != $n) { |
| 1384 | 1383 | $i = "\"$i\""; |
| 1385 | 1384 | } |
| 1386 | 1385 | $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
| 1387 | 1386 | } elseif (strpos($k, "UNIQUE ") === 0) { |
| 1388 | 1387 | $k = preg_replace("/^UNIQUE +/", '', $k); |
| 1389 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1388 | + $prim .= "$s\n\t\tCONSTRAINT ".str_replace('`', '"', $k)." UNIQUE ($v)"; |
|
| 1390 | 1389 | } else { |
| 1391 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1390 | + $prim .= "$s\n\t\t".str_replace('`', '"', $k)." ($v)"; |
|
| 1392 | 1391 | } |
| 1393 | 1392 | if ($k == "PRIMARY KEY") { |
| 1394 | 1393 | $prim_name = $v; |
@@ -1399,17 +1398,17 @@ discard block |
||
| 1399 | 1398 | |
| 1400 | 1399 | $character_set = ""; |
| 1401 | 1400 | if (@$GLOBALS['meta']['charset_sql_base']) { |
| 1402 | - $character_set .= " CHARACTER SET " . $GLOBALS['meta']['charset_sql_base']; |
|
| 1401 | + $character_set .= " CHARACTER SET ".$GLOBALS['meta']['charset_sql_base']; |
|
| 1403 | 1402 | } |
| 1404 | 1403 | if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
| 1405 | - $character_set .= " COLLATE " . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1404 | + $character_set .= " COLLATE ".$GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1406 | 1405 | } |
| 1407 | 1406 | |
| 1408 | 1407 | foreach ($champs as $k => $v) { |
| 1409 | 1408 | $k = str_replace('`', '"', $k); |
| 1410 | 1409 | if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
| 1411 | 1410 | if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
| 1412 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1411 | + $v = $defs[1].$character_set.' '.substr($v, strlen($defs[1])); |
|
| 1413 | 1412 | } |
| 1414 | 1413 | } |
| 1415 | 1414 | |
@@ -1424,7 +1423,7 @@ discard block |
||
| 1424 | 1423 | |
| 1425 | 1424 | // En l'absence de "if not exists" en PG, on neutralise les erreurs |
| 1426 | 1425 | |
| 1427 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ")" . |
|
| 1426 | + $q = "CREATE $temporary TABLE $nom ($query".($prim ? ",$prim" : '').")". |
|
| 1428 | 1427 | ($character_set ? " DEFAULT $character_set" : "") |
| 1429 | 1428 | . "\n"; |
| 1430 | 1429 | |
@@ -1435,7 +1434,7 @@ discard block |
||
| 1435 | 1434 | $r = @pg_query($link, $q); |
| 1436 | 1435 | |
| 1437 | 1436 | if (!$r) { |
| 1438 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1437 | + spip_log("Impossible de creer cette table: $q", 'pg.'._LOG_ERREUR); |
|
| 1439 | 1438 | } else { |
| 1440 | 1439 | foreach ($keys as $index) { |
| 1441 | 1440 | pg_query($link, $index); |
@@ -1459,13 +1458,13 @@ discard block |
||
| 1459 | 1458 | // vue deja presente |
| 1460 | 1459 | if (sql_showtable($nom, false, $serveur)) { |
| 1461 | 1460 | if ($requeter) { |
| 1462 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1461 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.'._LOG_ERREUR); |
|
| 1463 | 1462 | } |
| 1464 | 1463 | |
| 1465 | 1464 | return false; |
| 1466 | 1465 | } |
| 1467 | 1466 | |
| 1468 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1467 | + $query = "CREATE VIEW $nom AS ".$query_select; |
|
| 1469 | 1468 | |
| 1470 | 1469 | return spip_pg_query($query, $serveur, $requeter); |
| 1471 | 1470 | } |
@@ -1473,7 +1472,7 @@ discard block |
||
| 1473 | 1472 | |
| 1474 | 1473 | // https://code.spip.net/@spip_pg_set_connect_charset |
| 1475 | 1474 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1476 | - spip_log("changement de charset sql a ecrire en PG", 'pg.' . _LOG_ERREUR); |
|
| 1475 | + spip_log("changement de charset sql a ecrire en PG", 'pg.'._LOG_ERREUR); |
|
| 1477 | 1476 | } |
| 1478 | 1477 | |
| 1479 | 1478 | |
@@ -1487,7 +1486,7 @@ discard block |
||
| 1487 | 1486 | **/ |
| 1488 | 1487 | // https://code.spip.net/@spip_sqlite_optimize |
| 1489 | 1488 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1490 | - return spip_pg_query("VACUUM " . $table, $serveur, $requeter); |
|
| 1489 | + return spip_pg_query("VACUUM ".$table, $serveur, $requeter); |
|
| 1491 | 1490 | } |
| 1492 | 1491 | |
| 1493 | 1492 | // Selectionner la sous-chaine dans $objet |
@@ -11,35 +11,35 @@ |
||
| 11 | 11 | \***************************************************************************/ |
| 12 | 12 | |
| 13 | 13 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 14 | - return; |
|
| 14 | + return; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | function formulaires_configurer_transcodeur_charger_dist() { |
| 18 | - $valeurs = array( |
|
| 19 | - 'charset' => $GLOBALS['meta']['charset'], |
|
| 20 | - ); |
|
| 18 | + $valeurs = array( |
|
| 19 | + 'charset' => $GLOBALS['meta']['charset'], |
|
| 20 | + ); |
|
| 21 | 21 | |
| 22 | - return $valeurs; |
|
| 22 | + return $valeurs; |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | function formulaires_configurer_transcodeur_verifier_dist() { |
| 26 | - include_spip('inc/charsets'); |
|
| 26 | + include_spip('inc/charsets'); |
|
| 27 | 27 | |
| 28 | - $erreurs = array(); |
|
| 29 | - if (!$charset = _request('charset')) { |
|
| 30 | - $erreurs['charset'] = _T('info_obligatoire'); |
|
| 31 | - } elseif ($charset != 'utf-8' and !load_charset($charset)) { |
|
| 32 | - $erreurs['charset'] = _T('utf8_convert_erreur_orig', array('charset' => entites_html($charset))); |
|
| 33 | - } |
|
| 28 | + $erreurs = array(); |
|
| 29 | + if (!$charset = _request('charset')) { |
|
| 30 | + $erreurs['charset'] = _T('info_obligatoire'); |
|
| 31 | + } elseif ($charset != 'utf-8' and !load_charset($charset)) { |
|
| 32 | + $erreurs['charset'] = _T('utf8_convert_erreur_orig', array('charset' => entites_html($charset))); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - return $erreurs; |
|
| 35 | + return $erreurs; |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | |
| 39 | 39 | function formulaires_configurer_transcodeur_traiter_dist() { |
| 40 | - $res = array('editable' => true); |
|
| 41 | - ecrire_meta('charset', _request('charset')); |
|
| 42 | - $res['message_ok'] = _T('config_info_enregistree'); |
|
| 40 | + $res = array('editable' => true); |
|
| 41 | + ecrire_meta('charset', _request('charset')); |
|
| 42 | + $res['message_ok'] = _T('config_info_enregistree'); |
|
| 43 | 43 | |
| 44 | - return $res; |
|
| 44 | + return $res; |
|
| 45 | 45 | } |
@@ -12,7 +12,7 @@ |
||
| 12 | 12 | |
| 13 | 13 | # ou est l'espace prive ? |
| 14 | 14 | if (!defined('_DIR_RESTREINT_ABS')) { |
| 15 | - define('_DIR_RESTREINT_ABS', 'ecrire/'); |
|
| 15 | + define('_DIR_RESTREINT_ABS', 'ecrire/'); |
|
| 16 | 16 | } |
| 17 | 17 | include_once _DIR_RESTREINT_ABS.'inc_version.php'; |
| 18 | 18 | |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | * Code HTML du formulaire |
| 79 | 79 | */ |
| 80 | 80 | function redirige_action_auteur($action, $arg, $ret, $gra = '', $mode = false, $atts = '', $public = false) { |
| 81 | - $r = ($public ? _DIR_RESTREINT_ABS : _DIR_RESTREINT) . generer_url_ecrire($ret, $gra, true, true); |
|
| 81 | + $r = ($public ? _DIR_RESTREINT_ABS : _DIR_RESTREINT).generer_url_ecrire($ret, $gra, true, true); |
|
| 82 | 82 | |
| 83 | 83 | return generer_action_auteur($action, $arg, $r, $mode, $atts, $public); |
| 84 | 84 | } |
@@ -105,9 +105,9 @@ discard block |
||
| 105 | 105 | * URL, code HTML du formulaire ou tableau (action, arg, hash) |
| 106 | 106 | */ |
| 107 | 107 | function redirige_action_post($action, $arg, $ret, $gra, $corps, $att = '') { |
| 108 | - $r = _DIR_RESTREINT . generer_url_ecrire($ret, $gra, false, true); |
|
| 108 | + $r = _DIR_RESTREINT.generer_url_ecrire($ret, $gra, false, true); |
|
| 109 | 109 | |
| 110 | - return generer_action_auteur($action, $arg, $r, $corps, $att . " method='post'"); |
|
| 110 | + return generer_action_auteur($action, $arg, $r, $corps, $att." method='post'"); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | |
@@ -138,8 +138,8 @@ discard block |
||
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | $c = $GLOBALS['meta']['charset']; |
| 141 | - header('Content-Type: ' . $content_type . '; charset=' . $c); |
|
| 142 | - $debut = (($xml and strlen(trim($corps))) ? '<' . "?xml version='1.0' encoding='" . $c . "'?" . ">\n" : ''); |
|
| 141 | + header('Content-Type: '.$content_type.'; charset='.$c); |
|
| 142 | + $debut = (($xml and strlen(trim($corps))) ? '<'."?xml version='1.0' encoding='".$c."'?".">\n" : ''); |
|
| 143 | 143 | $fin = ''; |
| 144 | 144 | |
| 145 | 145 | echo $debut, $corps, $fin, $e; |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | **/ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -43,9 +43,9 @@ discard block |
||
| 43 | 43 | * URL, code HTML du formulaire ou tableau (action, arg, hash) |
| 44 | 44 | */ |
| 45 | 45 | function generer_action_auteur($action, $arg = '', $redirect = '', $mode = false, $att = '', $public = false) { |
| 46 | - $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 46 | + $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 47 | 47 | |
| 48 | - return $securiser_action($action, $arg, $redirect, $mode, $att, $public); |
|
| 48 | + return $securiser_action($action, $arg, $redirect, $mode, $att, $public); |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /** |
@@ -78,9 +78,9 @@ discard block |
||
| 78 | 78 | * Code HTML du formulaire |
| 79 | 79 | */ |
| 80 | 80 | function redirige_action_auteur($action, $arg, $ret, $gra = '', $mode = false, $atts = '', $public = false) { |
| 81 | - $r = ($public ? _DIR_RESTREINT_ABS : _DIR_RESTREINT) . generer_url_ecrire($ret, $gra, true, true); |
|
| 81 | + $r = ($public ? _DIR_RESTREINT_ABS : _DIR_RESTREINT) . generer_url_ecrire($ret, $gra, true, true); |
|
| 82 | 82 | |
| 83 | - return generer_action_auteur($action, $arg, $r, $mode, $atts, $public); |
|
| 83 | + return generer_action_auteur($action, $arg, $r, $mode, $atts, $public); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
@@ -105,9 +105,9 @@ discard block |
||
| 105 | 105 | * URL, code HTML du formulaire ou tableau (action, arg, hash) |
| 106 | 106 | */ |
| 107 | 107 | function redirige_action_post($action, $arg, $ret, $gra, $corps, $att = '') { |
| 108 | - $r = _DIR_RESTREINT . generer_url_ecrire($ret, $gra, false, true); |
|
| 108 | + $r = _DIR_RESTREINT . generer_url_ecrire($ret, $gra, false, true); |
|
| 109 | 109 | |
| 110 | - return generer_action_auteur($action, $arg, $r, $corps, $att . " method='post'"); |
|
| 110 | + return generer_action_auteur($action, $arg, $r, $corps, $att . " method='post'"); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | |
@@ -122,25 +122,25 @@ discard block |
||
| 122 | 122 | * passer "text/html" comme $content_type |
| 123 | 123 | */ |
| 124 | 124 | function ajax_retour($corps, $content_type = null) { |
| 125 | - $xml = false; |
|
| 126 | - if (is_null($content_type) or $content_type === true) { |
|
| 127 | - $xml = true; |
|
| 128 | - $content_type = 'text/html'; |
|
| 129 | - } elseif (!$content_type or !is_string($content_type) or strpos($content_type, '/') === false) { |
|
| 130 | - $content_type = 'text/html'; |
|
| 131 | - } |
|
| 125 | + $xml = false; |
|
| 126 | + if (is_null($content_type) or $content_type === true) { |
|
| 127 | + $xml = true; |
|
| 128 | + $content_type = 'text/html'; |
|
| 129 | + } elseif (!$content_type or !is_string($content_type) or strpos($content_type, '/') === false) { |
|
| 130 | + $content_type = 'text/html'; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - $e = ''; |
|
| 134 | - if (isset($_COOKIE['spip_admin']) |
|
| 135 | - and ((_request('var_mode') == 'debug') or !empty($GLOBALS['tableau_des_temps'])) |
|
| 136 | - ) { |
|
| 137 | - $e = erreur_squelette(); |
|
| 138 | - } |
|
| 133 | + $e = ''; |
|
| 134 | + if (isset($_COOKIE['spip_admin']) |
|
| 135 | + and ((_request('var_mode') == 'debug') or !empty($GLOBALS['tableau_des_temps'])) |
|
| 136 | + ) { |
|
| 137 | + $e = erreur_squelette(); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - $c = $GLOBALS['meta']['charset']; |
|
| 141 | - header('Content-Type: ' . $content_type . '; charset=' . $c); |
|
| 142 | - $debut = (($xml and strlen(trim($corps))) ? '<' . "?xml version='1.0' encoding='" . $c . "'?" . ">\n" : ''); |
|
| 143 | - $fin = ''; |
|
| 140 | + $c = $GLOBALS['meta']['charset']; |
|
| 141 | + header('Content-Type: ' . $content_type . '; charset=' . $c); |
|
| 142 | + $debut = (($xml and strlen(trim($corps))) ? '<' . "?xml version='1.0' encoding='" . $c . "'?" . ">\n" : ''); |
|
| 143 | + $fin = ''; |
|
| 144 | 144 | |
| 145 | - echo $debut, $corps, $fin, $e; |
|
| 145 | + echo $debut, $corps, $fin, $e; |
|
| 146 | 146 | } |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | $t = 0; |
| 83 | 83 | $n = 0; |
| 84 | 84 | $time = isset($GLOBALS['meta']['cache_mark']) ? $GLOBALS['meta']['cache_mark'] : 0; |
| 85 | - for ($i=0; $i < 256; $i++) { |
|
| 85 | + for ($i = 0; $i < 256; $i++) { |
|
| 86 | 86 | $dir = _DIR_CACHE.sprintf('%02s', dechex($i)); |
| 87 | 87 | if (@is_dir($dir) and is_readable($dir) and $d = opendir($dir)) { |
| 88 | 88 | while (($f = readdir($d)) !== false) { |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | $n++; |
| 91 | 91 | if ($a['mtime'] >= $time) { |
| 92 | 92 | if ($a['blocks'] > 0) { |
| 93 | - $t += 512*$a['blocks']; |
|
| 93 | + $t += 512 * $a['blocks']; |
|
| 94 | 94 | } else { |
| 95 | 95 | $t += $a['size']; |
| 96 | 96 | } |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | } |
| 100 | 100 | } |
| 101 | 101 | if ($n > 500) { |
| 102 | - return intval(256*$t/(1+$i)); |
|
| 102 | + return intval(256 * $t / (1 + $i)); |
|
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | 105 | return $t; |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | |
| 141 | 141 | // stocker la date_modif_$objet (ne sert a rien pour le moment) |
| 142 | 142 | if (isset($objet)) { |
| 143 | - ecrire_meta('derniere_modif_' . $objet, time()); |
|
| 143 | + ecrire_meta('derniere_modif_'.$objet, time()); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // si $derniere_modif_invalide est un array('article', 'rubrique') |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | $cache |
| 237 | 237 | )) { |
| 238 | 238 | // supprimer le fichier (de facon propre) |
| 239 | - supprimer_fichier(_DIR_CACHE . $cache); |
|
| 239 | + supprimer_fichier(_DIR_CACHE.$cache); |
|
| 240 | 240 | } else { |
| 241 | 241 | spip_log("Nom de fichier cache incorrect : $cache"); |
| 242 | 242 | } |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | **/ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | include_spip('base/serial'); |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | * en droit de l'éliminer |
| 28 | 28 | */ |
| 29 | 29 | if (!defined('_AGE_CACHE_ATIME')) { |
| 30 | - define('_AGE_CACHE_ATIME', 3600); |
|
| 30 | + define('_AGE_CACHE_ATIME', 3600); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | |
@@ -40,31 +40,31 @@ discard block |
||
| 40 | 40 | * @return int Taille approximative en octets |
| 41 | 41 | **/ |
| 42 | 42 | function taille_du_cache() { |
| 43 | - # check dirs until we reach > 500 files |
|
| 44 | - $t = 0; |
|
| 45 | - $n = 0; |
|
| 46 | - $time = isset($GLOBALS['meta']['cache_mark']) ? $GLOBALS['meta']['cache_mark'] : 0; |
|
| 47 | - for ($i=0; $i < 256; $i++) { |
|
| 48 | - $dir = _DIR_CACHE.sprintf('%02s', dechex($i)); |
|
| 49 | - if (@is_dir($dir) and is_readable($dir) and $d = opendir($dir)) { |
|
| 50 | - while (($f = readdir($d)) !== false) { |
|
| 51 | - if (preg_match(',^[[0-9a-f]+\.cache$,S', $f) and $a = stat("$dir/$f")) { |
|
| 52 | - $n++; |
|
| 53 | - if ($a['mtime'] >= $time) { |
|
| 54 | - if ($a['blocks'] > 0) { |
|
| 55 | - $t += 512*$a['blocks']; |
|
| 56 | - } else { |
|
| 57 | - $t += $a['size']; |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - if ($n > 500) { |
|
| 64 | - return intval(256*$t/(1+$i)); |
|
| 65 | - } |
|
| 66 | - } |
|
| 67 | - return $t; |
|
| 43 | + # check dirs until we reach > 500 files |
|
| 44 | + $t = 0; |
|
| 45 | + $n = 0; |
|
| 46 | + $time = isset($GLOBALS['meta']['cache_mark']) ? $GLOBALS['meta']['cache_mark'] : 0; |
|
| 47 | + for ($i=0; $i < 256; $i++) { |
|
| 48 | + $dir = _DIR_CACHE.sprintf('%02s', dechex($i)); |
|
| 49 | + if (@is_dir($dir) and is_readable($dir) and $d = opendir($dir)) { |
|
| 50 | + while (($f = readdir($d)) !== false) { |
|
| 51 | + if (preg_match(',^[[0-9a-f]+\.cache$,S', $f) and $a = stat("$dir/$f")) { |
|
| 52 | + $n++; |
|
| 53 | + if ($a['mtime'] >= $time) { |
|
| 54 | + if ($a['blocks'] > 0) { |
|
| 55 | + $t += 512*$a['blocks']; |
|
| 56 | + } else { |
|
| 57 | + $t += $a['size']; |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + if ($n > 500) { |
|
| 64 | + return intval(256*$t/(1+$i)); |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + return $t; |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | |
@@ -91,30 +91,30 @@ discard block |
||
| 91 | 91 | * Inutilisé |
| 92 | 92 | **/ |
| 93 | 93 | function inc_suivre_invalideur_dist($cond, $modif = true) { |
| 94 | - if (!$modif) { |
|
| 95 | - return; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - // determiner l'objet modifie : forum, article, etc |
|
| 99 | - if (preg_match(',["\']([a-z_]+)[/"\'],', $cond, $r)) { |
|
| 100 | - $objet = objet_type($r[1]); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - // stocker la date_modif_$objet (ne sert a rien pour le moment) |
|
| 104 | - if (isset($objet)) { |
|
| 105 | - ecrire_meta('derniere_modif_' . $objet, time()); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // si $derniere_modif_invalide est un array('article', 'rubrique') |
|
| 109 | - // n'affecter la meta que si un de ces objets est modifie |
|
| 110 | - if (is_array($GLOBALS['derniere_modif_invalide'])) { |
|
| 111 | - if (in_array($objet, $GLOBALS['derniere_modif_invalide'])) { |
|
| 112 | - ecrire_meta('derniere_modif', time()); |
|
| 113 | - } |
|
| 114 | - } // sinon, cas standard, toujours affecter la meta |
|
| 115 | - else { |
|
| 116 | - ecrire_meta('derniere_modif', time()); |
|
| 117 | - } |
|
| 94 | + if (!$modif) { |
|
| 95 | + return; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + // determiner l'objet modifie : forum, article, etc |
|
| 99 | + if (preg_match(',["\']([a-z_]+)[/"\'],', $cond, $r)) { |
|
| 100 | + $objet = objet_type($r[1]); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + // stocker la date_modif_$objet (ne sert a rien pour le moment) |
|
| 104 | + if (isset($objet)) { |
|
| 105 | + ecrire_meta('derniere_modif_' . $objet, time()); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // si $derniere_modif_invalide est un array('article', 'rubrique') |
|
| 109 | + // n'affecter la meta que si un de ces objets est modifie |
|
| 110 | + if (is_array($GLOBALS['derniere_modif_invalide'])) { |
|
| 111 | + if (in_array($objet, $GLOBALS['derniere_modif_invalide'])) { |
|
| 112 | + ecrire_meta('derniere_modif', time()); |
|
| 113 | + } |
|
| 114 | + } // sinon, cas standard, toujours affecter la meta |
|
| 115 | + else { |
|
| 116 | + ecrire_meta('derniere_modif', time()); |
|
| 117 | + } |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | |
@@ -139,49 +139,49 @@ discard block |
||
| 139 | 139 | * Nombre de fichiers supprimés |
| 140 | 140 | **/ |
| 141 | 141 | function purger_repertoire($dir, $options = array()) { |
| 142 | - if (!is_dir($dir) or !is_readable($dir)) { |
|
| 143 | - return; |
|
| 144 | - } |
|
| 145 | - $handle = opendir($dir); |
|
| 146 | - if (!$handle) { |
|
| 147 | - return; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $total = 0; |
|
| 151 | - |
|
| 152 | - while (($fichier = @readdir($handle)) !== false) { |
|
| 153 | - // Eviter ".", "..", ".htaccess", ".svn" etc & CACHEDIR.TAG |
|
| 154 | - if ($fichier[0] == '.' or $fichier == 'CACHEDIR.TAG') { |
|
| 155 | - continue; |
|
| 156 | - } |
|
| 157 | - $chemin = "$dir/$fichier"; |
|
| 158 | - if (is_file($chemin)) { |
|
| 159 | - if ((!isset($options['atime']) or (@fileatime($chemin) < $options['atime'])) |
|
| 160 | - and (!isset($options['mtime']) or (@filemtime($chemin) < $options['mtime'])) |
|
| 161 | - ) { |
|
| 162 | - supprimer_fichier($chemin); |
|
| 163 | - $total++; |
|
| 164 | - } |
|
| 165 | - } else { |
|
| 166 | - if (is_dir($chemin)) { |
|
| 167 | - $opts = $options; |
|
| 168 | - if (isset($options['limit'])) { |
|
| 169 | - $opts['limit'] = $options['limit'] - $total; |
|
| 170 | - } |
|
| 171 | - $total += purger_repertoire($chemin, $opts); |
|
| 172 | - if (isset($options['subdir']) && $options['subdir']) { |
|
| 173 | - spip_unlink($chemin); |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if (isset($options['limit']) and $total >= $options['limit']) { |
|
| 179 | - break; |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - closedir($handle); |
|
| 183 | - |
|
| 184 | - return $total; |
|
| 142 | + if (!is_dir($dir) or !is_readable($dir)) { |
|
| 143 | + return; |
|
| 144 | + } |
|
| 145 | + $handle = opendir($dir); |
|
| 146 | + if (!$handle) { |
|
| 147 | + return; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $total = 0; |
|
| 151 | + |
|
| 152 | + while (($fichier = @readdir($handle)) !== false) { |
|
| 153 | + // Eviter ".", "..", ".htaccess", ".svn" etc & CACHEDIR.TAG |
|
| 154 | + if ($fichier[0] == '.' or $fichier == 'CACHEDIR.TAG') { |
|
| 155 | + continue; |
|
| 156 | + } |
|
| 157 | + $chemin = "$dir/$fichier"; |
|
| 158 | + if (is_file($chemin)) { |
|
| 159 | + if ((!isset($options['atime']) or (@fileatime($chemin) < $options['atime'])) |
|
| 160 | + and (!isset($options['mtime']) or (@filemtime($chemin) < $options['mtime'])) |
|
| 161 | + ) { |
|
| 162 | + supprimer_fichier($chemin); |
|
| 163 | + $total++; |
|
| 164 | + } |
|
| 165 | + } else { |
|
| 166 | + if (is_dir($chemin)) { |
|
| 167 | + $opts = $options; |
|
| 168 | + if (isset($options['limit'])) { |
|
| 169 | + $opts['limit'] = $options['limit'] - $total; |
|
| 170 | + } |
|
| 171 | + $total += purger_repertoire($chemin, $opts); |
|
| 172 | + if (isset($options['subdir']) && $options['subdir']) { |
|
| 173 | + spip_unlink($chemin); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if (isset($options['limit']) and $total >= $options['limit']) { |
|
| 179 | + break; |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + closedir($handle); |
|
| 183 | + |
|
| 184 | + return $total; |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | |
@@ -193,15 +193,15 @@ discard block |
||
| 193 | 193 | // https://code.spip.net/@retire_cache |
| 194 | 194 | function retire_cache($cache) { |
| 195 | 195 | |
| 196 | - if (preg_match( |
|
| 197 | - ',^([0-9a-f]/)?([0-9]+/)?[0-9a-f]+\.cache(\.gz)?$,i', |
|
| 198 | - $cache |
|
| 199 | - )) { |
|
| 200 | - // supprimer le fichier (de facon propre) |
|
| 201 | - supprimer_fichier(_DIR_CACHE . $cache); |
|
| 202 | - } else { |
|
| 203 | - spip_log("Nom de fichier cache incorrect : $cache"); |
|
| 204 | - } |
|
| 196 | + if (preg_match( |
|
| 197 | + ',^([0-9a-f]/)?([0-9]+/)?[0-9a-f]+\.cache(\.gz)?$,i', |
|
| 198 | + $cache |
|
| 199 | + )) { |
|
| 200 | + // supprimer le fichier (de facon propre) |
|
| 201 | + supprimer_fichier(_DIR_CACHE . $cache); |
|
| 202 | + } else { |
|
| 203 | + spip_log("Nom de fichier cache incorrect : $cache"); |
|
| 204 | + } |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | // Supprimer les caches marques "x" |
@@ -209,9 +209,9 @@ discard block |
||
| 209 | 209 | // la meta est toujours false ; mais evitons un bug si elle est appellee |
| 210 | 210 | // https://code.spip.net/@retire_caches |
| 211 | 211 | function inc_retire_caches_dist($chemin = '') { |
| 212 | - if (isset($GLOBALS['meta']['invalider_caches'])) { |
|
| 213 | - effacer_meta('invalider_caches'); |
|
| 214 | - } # concurrence |
|
| 212 | + if (isset($GLOBALS['meta']['invalider_caches'])) { |
|
| 213 | + effacer_meta('invalider_caches'); |
|
| 214 | + } # concurrence |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | ####################################################################### |
@@ -222,9 +222,9 @@ discard block |
||
| 222 | 222 | ## |
| 223 | 223 | |
| 224 | 224 | function retire_caches($chemin = '') { |
| 225 | - if ($retire_caches = charger_fonction('retire_caches', 'inc', true)) { |
|
| 226 | - return $retire_caches($chemin); |
|
| 227 | - } |
|
| 225 | + if ($retire_caches = charger_fonction('retire_caches', 'inc', true)) { |
|
| 226 | + return $retire_caches($chemin); |
|
| 227 | + } |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | |
@@ -233,10 +233,10 @@ discard block |
||
| 233 | 233 | |
| 234 | 234 | // https://code.spip.net/@calcul_invalideurs |
| 235 | 235 | function calcul_invalideurs($corps, $primary, &$boucles, $id_boucle) { |
| 236 | - if ($calcul_invalideurs = charger_fonction('calcul_invalideurs', 'inc', true)) { |
|
| 237 | - return $calcul_invalideurs($corps, $primary, $boucles, $id_boucle); |
|
| 238 | - } |
|
| 239 | - return $corps; |
|
| 236 | + if ($calcul_invalideurs = charger_fonction('calcul_invalideurs', 'inc', true)) { |
|
| 237 | + return $calcul_invalideurs($corps, $primary, $boucles, $id_boucle); |
|
| 238 | + } |
|
| 239 | + return $corps; |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | |
@@ -246,27 +246,27 @@ discard block |
||
| 246 | 246 | // |
| 247 | 247 | // https://code.spip.net/@supprime_invalideurs |
| 248 | 248 | function supprime_invalideurs() { |
| 249 | - if ($supprime_invalideurs = charger_fonction('supprime_invalideurs', 'inc', true)) { |
|
| 250 | - return $supprime_invalideurs(); |
|
| 251 | - } |
|
| 249 | + if ($supprime_invalideurs = charger_fonction('supprime_invalideurs', 'inc', true)) { |
|
| 250 | + return $supprime_invalideurs(); |
|
| 251 | + } |
|
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | |
| 255 | 255 | // Calcul des pages : noter dans la base les liens d'invalidation |
| 256 | 256 | // https://code.spip.net/@maj_invalideurs |
| 257 | 257 | function maj_invalideurs($fichier, &$page) { |
| 258 | - if ($maj_invalideurs = charger_fonction('maj_invalideurs', 'inc', true)) { |
|
| 259 | - return $maj_invalideurs($fichier, $page); |
|
| 260 | - } |
|
| 258 | + if ($maj_invalideurs = charger_fonction('maj_invalideurs', 'inc', true)) { |
|
| 259 | + return $maj_invalideurs($fichier, $page); |
|
| 260 | + } |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | |
| 264 | 264 | // les invalideurs sont de la forme "objet/id_objet" |
| 265 | 265 | // https://code.spip.net/@insere_invalideur |
| 266 | 266 | function insere_invalideur($inval, $fichier) { |
| 267 | - if ($insere_invalideur = charger_fonction('insere_invalideur', 'inc', true)) { |
|
| 268 | - return $insere_invalideur($inval, $fichier); |
|
| 269 | - } |
|
| 267 | + if ($insere_invalideur = charger_fonction('insere_invalideur', 'inc', true)) { |
|
| 268 | + return $insere_invalideur($inval, $fichier); |
|
| 269 | + } |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | 272 | // |
@@ -274,16 +274,16 @@ discard block |
||
| 274 | 274 | // |
| 275 | 275 | // https://code.spip.net/@applique_invalideur |
| 276 | 276 | function applique_invalideur($depart) { |
| 277 | - if ($applique_invalideur = charger_fonction('applique_invalideur', 'inc', true)) { |
|
| 278 | - return $applique_invalideur($depart); |
|
| 279 | - } |
|
| 277 | + if ($applique_invalideur = charger_fonction('applique_invalideur', 'inc', true)) { |
|
| 278 | + return $applique_invalideur($depart); |
|
| 279 | + } |
|
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | // |
| 283 | 283 | // Invalider les caches liés à telle condition |
| 284 | 284 | // |
| 285 | 285 | function suivre_invalideur($cond, $modif = true) { |
| 286 | - if ($suivre_invalideur = charger_fonction('suivre_invalideur', 'inc', true)) { |
|
| 287 | - return $suivre_invalideur($cond, $modif); |
|
| 288 | - } |
|
| 286 | + if ($suivre_invalideur = charger_fonction('suivre_invalideur', 'inc', true)) { |
|
| 287 | + return $suivre_invalideur($cond, $modif); |
|
| 288 | + } |
|
| 289 | 289 | } |
| 290 | 290 | \ No newline at end of file |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | **/ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | |
@@ -45,130 +45,130 @@ discard block |
||
| 45 | 45 | **/ |
| 46 | 46 | function formulaires_dater_charger_dist($objet, $id_objet, $retour = '', $options = array()) { |
| 47 | 47 | |
| 48 | - $objet = objet_type($objet); |
|
| 49 | - if (!$objet or !intval($id_objet)) { |
|
| 50 | - return false; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - if (!is_array($options)) { |
|
| 54 | - $options = unserialize($options); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $_id_objet = id_table_objet($objet); |
|
| 58 | - $table = table_objet($objet); |
|
| 59 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 60 | - $desc = $trouver_table($table); |
|
| 61 | - |
|
| 62 | - if (!$desc) { |
|
| 63 | - return false; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - $champ_date = $desc['date'] ? $desc['date'] : 'date'; |
|
| 67 | - if (isset($options['champ_date']) and $options['champ_date']) { |
|
| 68 | - $champ_date = $options['champ_date']; |
|
| 69 | - } |
|
| 70 | - if (!isset($desc['field'][$champ_date])) { |
|
| 71 | - return false; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - $valeurs = array( |
|
| 75 | - 'objet' => $objet, |
|
| 76 | - 'id_objet' => $id_objet, |
|
| 77 | - 'id' => $id_objet, |
|
| 78 | - ); |
|
| 79 | - |
|
| 80 | - |
|
| 81 | - $select = "$champ_date as date"; |
|
| 82 | - $champ_date_redac = 'date_redac'; |
|
| 83 | - if (isset($options['champ_date_redac']) and $options['champ_date_redac']) { |
|
| 84 | - $champ_date_redac = $options['champ_date_redac']; |
|
| 85 | - } |
|
| 86 | - if (isset($desc['field'][$champ_date_redac])) { |
|
| 87 | - $select .= ",$champ_date_redac as date_redac"; |
|
| 88 | - } |
|
| 89 | - if (isset($desc['field']['statut'])) { |
|
| 90 | - $select .= ",statut"; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - |
|
| 94 | - $row = sql_fetsel($select, $desc['table'], "$_id_objet=" . intval($id_objet)); |
|
| 95 | - $statut = isset($row['statut']) ? $row['statut'] : 'publie'; // pas de statut => publie |
|
| 96 | - |
|
| 97 | - $valeurs['editable'] = autoriser('dater', $objet, $id_objet, null, array('statut' => $statut)); |
|
| 98 | - |
|
| 99 | - $possedeDateRedac = false; |
|
| 100 | - |
|
| 101 | - if (isset($row['date_redac']) and |
|
| 102 | - $regs = recup_date($row['date_redac'], false) |
|
| 103 | - ) { |
|
| 104 | - $annee_redac = $regs[0]; |
|
| 105 | - $mois_redac = $regs[1]; |
|
| 106 | - $jour_redac = $regs[2]; |
|
| 107 | - $heure_redac = $regs[3]; |
|
| 108 | - $minute_redac = $regs[4]; |
|
| 109 | - $possedeDateRedac = true; |
|
| 110 | - // attention : les vrai dates de l'annee 1 sont stockee avec +9000 => 9001 |
|
| 111 | - // mais reviennent ici en annee 1 par recup_date |
|
| 112 | - // on verifie donc que le intval($row['date_redac']) qui ressort l'annee |
|
| 113 | - // est bien lui aussi <=1 : dans ce cas c'est une date sql 'nulle' ou presque, selon |
|
| 114 | - // le gestionnnaire sql utilise (0001-01-01 pour PG par exemple) |
|
| 115 | - if (intval($row['date_redac']) <= 1 and ($annee_redac <= 1) and ($mois_redac <= 1) and ($jour_redac <= 1)) { |
|
| 116 | - $possedeDateRedac = false; |
|
| 117 | - } |
|
| 118 | - } else { |
|
| 119 | - $annee_redac = $mois_redac = $jour_redac = $heure_redac = $minute_redac = 0; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - if ($regs = recup_date($row['date'], false)) { |
|
| 123 | - $annee = $regs[0]; |
|
| 124 | - $mois = $regs[1]; |
|
| 125 | - $jour = $regs[2]; |
|
| 126 | - $heure = $regs[3]; |
|
| 127 | - $minute = $regs[4]; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - // attention, si la variable s'appelle date ou date_redac, le compilo va |
|
| 131 | - // la normaliser, ce qu'on ne veut pas ici. |
|
| 132 | - $valeurs['afficher_date_redac'] = ($possedeDateRedac ? $row['date_redac'] : ''); |
|
| 133 | - $valeurs['date_redac_jour'] = dater_formater_saisie_jour($jour_redac, $mois_redac, $annee_redac); |
|
| 134 | - $valeurs['date_redac_heure'] = "$heure_redac:$minute_redac"; |
|
| 135 | - |
|
| 136 | - $valeurs['afficher_date'] = $row['date']; |
|
| 137 | - $valeurs['date_jour'] = dater_formater_saisie_jour($jour, $mois, $annee); |
|
| 138 | - $valeurs['date_heure'] = "$heure:$minute"; |
|
| 139 | - |
|
| 140 | - $valeurs['sans_redac'] = !$possedeDateRedac; |
|
| 141 | - |
|
| 142 | - if (isset($options['date_redac'])) { |
|
| 143 | - $valeurs['_editer_date_anterieure'] = $options['date_redac']; |
|
| 144 | - } else { |
|
| 145 | - $valeurs['_editer_date_anterieure'] = ($objet == 'article' and ($GLOBALS['meta']['articles_redac'] != 'non' or $possedeDateRedac)); |
|
| 146 | - } |
|
| 147 | - $valeurs['_label_date'] = (($statut == 'publie') ? |
|
| 148 | - _T('texte_date_publication_objet') : _T('texte_date_creation_objet')); |
|
| 149 | - if (isset($options['label_date']) and $options['label_date']) { |
|
| 150 | - $valeurs['_label_date'] = $options['label_date']; |
|
| 151 | - } |
|
| 152 | - if (isset($options['label_date_redac']) and $options['label_date_redac']) { |
|
| 153 | - $valeurs['_label_date_redac'] = $options['label_date_redac']; |
|
| 154 | - } |
|
| 155 | - if (isset($options['texte_sans_date_redac']) and $options['texte_sans_date_redac']) { |
|
| 156 | - $valeurs['_texte_sans_date_redac'] = $options['texte_sans_date_redac']; |
|
| 157 | - } |
|
| 158 | - if (isset($options['class']) and $options['class']) { |
|
| 159 | - $valeurs['_class'] = $options['class']; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - $valeurs['_saisie_en_cours'] = (_request('_saisie_en_cours') !== null or _request('date_jour') !== null); |
|
| 163 | - |
|
| 164 | - // cas ou l'on ne peut pas dater mais on peut modifier la date de redac anterieure |
|
| 165 | - // https://core.spip.net/issues/3494 |
|
| 166 | - $valeurs['_editer_date'] = $valeurs['editable']; |
|
| 167 | - if ($valeurs['_editer_date_anterieure'] and !$valeurs['editable']) { |
|
| 168 | - $valeurs['editable'] = autoriser('modifier', $objet, $id_objet); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return $valeurs; |
|
| 48 | + $objet = objet_type($objet); |
|
| 49 | + if (!$objet or !intval($id_objet)) { |
|
| 50 | + return false; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + if (!is_array($options)) { |
|
| 54 | + $options = unserialize($options); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $_id_objet = id_table_objet($objet); |
|
| 58 | + $table = table_objet($objet); |
|
| 59 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 60 | + $desc = $trouver_table($table); |
|
| 61 | + |
|
| 62 | + if (!$desc) { |
|
| 63 | + return false; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + $champ_date = $desc['date'] ? $desc['date'] : 'date'; |
|
| 67 | + if (isset($options['champ_date']) and $options['champ_date']) { |
|
| 68 | + $champ_date = $options['champ_date']; |
|
| 69 | + } |
|
| 70 | + if (!isset($desc['field'][$champ_date])) { |
|
| 71 | + return false; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + $valeurs = array( |
|
| 75 | + 'objet' => $objet, |
|
| 76 | + 'id_objet' => $id_objet, |
|
| 77 | + 'id' => $id_objet, |
|
| 78 | + ); |
|
| 79 | + |
|
| 80 | + |
|
| 81 | + $select = "$champ_date as date"; |
|
| 82 | + $champ_date_redac = 'date_redac'; |
|
| 83 | + if (isset($options['champ_date_redac']) and $options['champ_date_redac']) { |
|
| 84 | + $champ_date_redac = $options['champ_date_redac']; |
|
| 85 | + } |
|
| 86 | + if (isset($desc['field'][$champ_date_redac])) { |
|
| 87 | + $select .= ",$champ_date_redac as date_redac"; |
|
| 88 | + } |
|
| 89 | + if (isset($desc['field']['statut'])) { |
|
| 90 | + $select .= ",statut"; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + $row = sql_fetsel($select, $desc['table'], "$_id_objet=" . intval($id_objet)); |
|
| 95 | + $statut = isset($row['statut']) ? $row['statut'] : 'publie'; // pas de statut => publie |
|
| 96 | + |
|
| 97 | + $valeurs['editable'] = autoriser('dater', $objet, $id_objet, null, array('statut' => $statut)); |
|
| 98 | + |
|
| 99 | + $possedeDateRedac = false; |
|
| 100 | + |
|
| 101 | + if (isset($row['date_redac']) and |
|
| 102 | + $regs = recup_date($row['date_redac'], false) |
|
| 103 | + ) { |
|
| 104 | + $annee_redac = $regs[0]; |
|
| 105 | + $mois_redac = $regs[1]; |
|
| 106 | + $jour_redac = $regs[2]; |
|
| 107 | + $heure_redac = $regs[3]; |
|
| 108 | + $minute_redac = $regs[4]; |
|
| 109 | + $possedeDateRedac = true; |
|
| 110 | + // attention : les vrai dates de l'annee 1 sont stockee avec +9000 => 9001 |
|
| 111 | + // mais reviennent ici en annee 1 par recup_date |
|
| 112 | + // on verifie donc que le intval($row['date_redac']) qui ressort l'annee |
|
| 113 | + // est bien lui aussi <=1 : dans ce cas c'est une date sql 'nulle' ou presque, selon |
|
| 114 | + // le gestionnnaire sql utilise (0001-01-01 pour PG par exemple) |
|
| 115 | + if (intval($row['date_redac']) <= 1 and ($annee_redac <= 1) and ($mois_redac <= 1) and ($jour_redac <= 1)) { |
|
| 116 | + $possedeDateRedac = false; |
|
| 117 | + } |
|
| 118 | + } else { |
|
| 119 | + $annee_redac = $mois_redac = $jour_redac = $heure_redac = $minute_redac = 0; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + if ($regs = recup_date($row['date'], false)) { |
|
| 123 | + $annee = $regs[0]; |
|
| 124 | + $mois = $regs[1]; |
|
| 125 | + $jour = $regs[2]; |
|
| 126 | + $heure = $regs[3]; |
|
| 127 | + $minute = $regs[4]; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + // attention, si la variable s'appelle date ou date_redac, le compilo va |
|
| 131 | + // la normaliser, ce qu'on ne veut pas ici. |
|
| 132 | + $valeurs['afficher_date_redac'] = ($possedeDateRedac ? $row['date_redac'] : ''); |
|
| 133 | + $valeurs['date_redac_jour'] = dater_formater_saisie_jour($jour_redac, $mois_redac, $annee_redac); |
|
| 134 | + $valeurs['date_redac_heure'] = "$heure_redac:$minute_redac"; |
|
| 135 | + |
|
| 136 | + $valeurs['afficher_date'] = $row['date']; |
|
| 137 | + $valeurs['date_jour'] = dater_formater_saisie_jour($jour, $mois, $annee); |
|
| 138 | + $valeurs['date_heure'] = "$heure:$minute"; |
|
| 139 | + |
|
| 140 | + $valeurs['sans_redac'] = !$possedeDateRedac; |
|
| 141 | + |
|
| 142 | + if (isset($options['date_redac'])) { |
|
| 143 | + $valeurs['_editer_date_anterieure'] = $options['date_redac']; |
|
| 144 | + } else { |
|
| 145 | + $valeurs['_editer_date_anterieure'] = ($objet == 'article' and ($GLOBALS['meta']['articles_redac'] != 'non' or $possedeDateRedac)); |
|
| 146 | + } |
|
| 147 | + $valeurs['_label_date'] = (($statut == 'publie') ? |
|
| 148 | + _T('texte_date_publication_objet') : _T('texte_date_creation_objet')); |
|
| 149 | + if (isset($options['label_date']) and $options['label_date']) { |
|
| 150 | + $valeurs['_label_date'] = $options['label_date']; |
|
| 151 | + } |
|
| 152 | + if (isset($options['label_date_redac']) and $options['label_date_redac']) { |
|
| 153 | + $valeurs['_label_date_redac'] = $options['label_date_redac']; |
|
| 154 | + } |
|
| 155 | + if (isset($options['texte_sans_date_redac']) and $options['texte_sans_date_redac']) { |
|
| 156 | + $valeurs['_texte_sans_date_redac'] = $options['texte_sans_date_redac']; |
|
| 157 | + } |
|
| 158 | + if (isset($options['class']) and $options['class']) { |
|
| 159 | + $valeurs['_class'] = $options['class']; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + $valeurs['_saisie_en_cours'] = (_request('_saisie_en_cours') !== null or _request('date_jour') !== null); |
|
| 163 | + |
|
| 164 | + // cas ou l'on ne peut pas dater mais on peut modifier la date de redac anterieure |
|
| 165 | + // https://core.spip.net/issues/3494 |
|
| 166 | + $valeurs['_editer_date'] = $valeurs['editable']; |
|
| 167 | + if ($valeurs['_editer_date_anterieure'] and !$valeurs['editable']) { |
|
| 168 | + $valeurs['editable'] = autoriser('modifier', $objet, $id_objet); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return $valeurs; |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -186,20 +186,20 @@ discard block |
||
| 186 | 186 | * Date formatée tel que `02/10/2012` |
| 187 | 187 | **/ |
| 188 | 188 | function dater_formater_saisie_jour($jour, $mois, $annee, $sep = '/') { |
| 189 | - $annee = str_pad($annee, 4, '0', STR_PAD_LEFT); |
|
| 190 | - if (intval($jour)) { |
|
| 191 | - $jour = str_pad($jour, 2, '0', STR_PAD_LEFT); |
|
| 192 | - $mois = str_pad($mois, 2, '0', STR_PAD_LEFT); |
|
| 189 | + $annee = str_pad($annee, 4, '0', STR_PAD_LEFT); |
|
| 190 | + if (intval($jour)) { |
|
| 191 | + $jour = str_pad($jour, 2, '0', STR_PAD_LEFT); |
|
| 192 | + $mois = str_pad($mois, 2, '0', STR_PAD_LEFT); |
|
| 193 | 193 | |
| 194 | - return "$jour$sep$mois$sep$annee"; |
|
| 195 | - } |
|
| 196 | - if (intval($mois)) { |
|
| 197 | - $mois = str_pad($mois, 2, '0', STR_PAD_LEFT); |
|
| 194 | + return "$jour$sep$mois$sep$annee"; |
|
| 195 | + } |
|
| 196 | + if (intval($mois)) { |
|
| 197 | + $mois = str_pad($mois, 2, '0', STR_PAD_LEFT); |
|
| 198 | 198 | |
| 199 | - return "$mois$sep$annee"; |
|
| 200 | - } |
|
| 199 | + return "$mois$sep$annee"; |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - return $annee; |
|
| 202 | + return $annee; |
|
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | /** |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | * Hash du formulaire |
| 219 | 219 | **/ |
| 220 | 220 | function formulaires_dater_identifier_dist($objet, $id_objet, $retour = '', $options = array()) { |
| 221 | - return serialize(array($objet, $id_objet)); |
|
| 221 | + return serialize(array($objet, $id_objet)); |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
@@ -236,30 +236,30 @@ discard block |
||
| 236 | 236 | * Tableau des erreurs |
| 237 | 237 | */ |
| 238 | 238 | function formulaires_dater_verifier_dist($objet, $id_objet, $retour = '', $options = array()) { |
| 239 | - $erreurs = array(); |
|
| 240 | - |
|
| 241 | - // ouvrir le formulaire en edition ? |
|
| 242 | - if (_request('_saisie_en_cours')) { |
|
| 243 | - $erreurs['message_erreur'] = ''; |
|
| 244 | - |
|
| 245 | - return $erreurs; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - if (_request('changer')) { |
|
| 249 | - foreach (array('date', 'date_redac') as $k) { |
|
| 250 | - if ($v = _request($k . '_jour') and !dater_recuperer_date_saisie($v, $k)) { |
|
| 251 | - $erreurs[$k] = _T('format_date_incorrecte'); |
|
| 252 | - } elseif ($v = _request($k . '_heure') and !dater_recuperer_heure_saisie($v)) { |
|
| 253 | - $erreurs[$k] = _T('format_heure_incorrecte'); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - if (!_request('date_jour')) { |
|
| 258 | - $erreurs['date'] = _T('info_obligatoire'); |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - return $erreurs; |
|
| 239 | + $erreurs = array(); |
|
| 240 | + |
|
| 241 | + // ouvrir le formulaire en edition ? |
|
| 242 | + if (_request('_saisie_en_cours')) { |
|
| 243 | + $erreurs['message_erreur'] = ''; |
|
| 244 | + |
|
| 245 | + return $erreurs; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + if (_request('changer')) { |
|
| 249 | + foreach (array('date', 'date_redac') as $k) { |
|
| 250 | + if ($v = _request($k . '_jour') and !dater_recuperer_date_saisie($v, $k)) { |
|
| 251 | + $erreurs[$k] = _T('format_date_incorrecte'); |
|
| 252 | + } elseif ($v = _request($k . '_heure') and !dater_recuperer_heure_saisie($v)) { |
|
| 253 | + $erreurs[$k] = _T('format_heure_incorrecte'); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + if (!_request('date_jour')) { |
|
| 258 | + $erreurs['date'] = _T('info_obligatoire'); |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + return $erreurs; |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | /** |
@@ -277,79 +277,79 @@ discard block |
||
| 277 | 277 | * Retours des traitements |
| 278 | 278 | */ |
| 279 | 279 | function formulaires_dater_traiter_dist($objet, $id_objet, $retour = '', $options = array()) { |
| 280 | - $res = array('editable' => ' '); |
|
| 281 | - |
|
| 282 | - if (_request('changer')) { |
|
| 283 | - $table = table_objet($objet); |
|
| 284 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 285 | - $desc = $trouver_table($table); |
|
| 286 | - |
|
| 287 | - if (!$desc) { |
|
| 288 | - return array('message_erreur' => _L('erreur')); |
|
| 289 | - } #impossible en principe |
|
| 290 | - |
|
| 291 | - $champ_date = $desc['date'] ? $desc['date'] : 'date'; |
|
| 292 | - if (isset($options['champ_date']) and $options['champ_date']) { |
|
| 293 | - $champ_date = $options['champ_date']; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - $set = array(); |
|
| 297 | - |
|
| 298 | - $charger = charger_fonction('charger', 'formulaires/dater/'); |
|
| 299 | - $v = $charger($objet, $id_objet, $retour, $options); |
|
| 300 | - |
|
| 301 | - if ($v['_editer_date']) { |
|
| 302 | - if (!$d = dater_recuperer_date_saisie(_request('date_jour'))) { |
|
| 303 | - $d = array(date('Y'), date('m'), date('d')); |
|
| 304 | - } |
|
| 305 | - if (!$h = dater_recuperer_heure_saisie(_request('date_heure'))) { |
|
| 306 | - $h = array(0, 0); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - $set[$champ_date] = sql_format_date($d[0], $d[1], $d[2], $h[0], $h[1]); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - $champ_date_redac = 'date_redac'; |
|
| 313 | - if (isset($options['champ_date_redac']) and $options['champ_date_redac']) { |
|
| 314 | - $champ_date_redac = $options['champ_date_redac']; |
|
| 315 | - } |
|
| 316 | - if (isset($desc['field'][$champ_date_redac]) and $v['_editer_date_anterieure']) { |
|
| 317 | - if (!_request('date_redac_jour') or _request('sans_redac')) { |
|
| 318 | - $set[$champ_date_redac] = sql_format_date(0, 0, 0, 0, 0, 0); |
|
| 319 | - } else { |
|
| 320 | - if (!$d = dater_recuperer_date_saisie(_request('date_redac_jour'), 'date_redac')) { |
|
| 321 | - $d = array(date('Y'), date('m'), date('d')); |
|
| 322 | - } |
|
| 323 | - if (!$h = dater_recuperer_heure_saisie(_request('date_redac_heure'))) { |
|
| 324 | - $h = array(0, 0); |
|
| 325 | - } |
|
| 326 | - $set[$champ_date_redac] = sql_format_date($d[0], $d[1], $d[2], $h[0], $h[1]); |
|
| 327 | - } |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - if (count($set)) { |
|
| 331 | - $publie_avant = objet_test_si_publie($objet, $id_objet); |
|
| 332 | - include_spip('action/editer_objet'); |
|
| 333 | - objet_modifier($objet, $id_objet, $set); |
|
| 334 | - $publie_apres = objet_test_si_publie($objet, $id_objet); |
|
| 335 | - if ($publie_avant !== $publie_apres) { |
|
| 336 | - // on refuse ajax pour forcer le rechargement de la page ici |
|
| 337 | - // on refera traiter une 2eme fois, mais c'est sans consequence |
|
| 338 | - refuser_traiter_formulaire_ajax(); |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - if ($retour) { |
|
| 344 | - $res['redirect'] = $retour; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - set_request('date_jour'); |
|
| 348 | - set_request('date_redac_jour'); |
|
| 349 | - set_request('date_heure'); |
|
| 350 | - set_request('date_redac_heure'); |
|
| 351 | - |
|
| 352 | - return $res; |
|
| 280 | + $res = array('editable' => ' '); |
|
| 281 | + |
|
| 282 | + if (_request('changer')) { |
|
| 283 | + $table = table_objet($objet); |
|
| 284 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 285 | + $desc = $trouver_table($table); |
|
| 286 | + |
|
| 287 | + if (!$desc) { |
|
| 288 | + return array('message_erreur' => _L('erreur')); |
|
| 289 | + } #impossible en principe |
|
| 290 | + |
|
| 291 | + $champ_date = $desc['date'] ? $desc['date'] : 'date'; |
|
| 292 | + if (isset($options['champ_date']) and $options['champ_date']) { |
|
| 293 | + $champ_date = $options['champ_date']; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + $set = array(); |
|
| 297 | + |
|
| 298 | + $charger = charger_fonction('charger', 'formulaires/dater/'); |
|
| 299 | + $v = $charger($objet, $id_objet, $retour, $options); |
|
| 300 | + |
|
| 301 | + if ($v['_editer_date']) { |
|
| 302 | + if (!$d = dater_recuperer_date_saisie(_request('date_jour'))) { |
|
| 303 | + $d = array(date('Y'), date('m'), date('d')); |
|
| 304 | + } |
|
| 305 | + if (!$h = dater_recuperer_heure_saisie(_request('date_heure'))) { |
|
| 306 | + $h = array(0, 0); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + $set[$champ_date] = sql_format_date($d[0], $d[1], $d[2], $h[0], $h[1]); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + $champ_date_redac = 'date_redac'; |
|
| 313 | + if (isset($options['champ_date_redac']) and $options['champ_date_redac']) { |
|
| 314 | + $champ_date_redac = $options['champ_date_redac']; |
|
| 315 | + } |
|
| 316 | + if (isset($desc['field'][$champ_date_redac]) and $v['_editer_date_anterieure']) { |
|
| 317 | + if (!_request('date_redac_jour') or _request('sans_redac')) { |
|
| 318 | + $set[$champ_date_redac] = sql_format_date(0, 0, 0, 0, 0, 0); |
|
| 319 | + } else { |
|
| 320 | + if (!$d = dater_recuperer_date_saisie(_request('date_redac_jour'), 'date_redac')) { |
|
| 321 | + $d = array(date('Y'), date('m'), date('d')); |
|
| 322 | + } |
|
| 323 | + if (!$h = dater_recuperer_heure_saisie(_request('date_redac_heure'))) { |
|
| 324 | + $h = array(0, 0); |
|
| 325 | + } |
|
| 326 | + $set[$champ_date_redac] = sql_format_date($d[0], $d[1], $d[2], $h[0], $h[1]); |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + if (count($set)) { |
|
| 331 | + $publie_avant = objet_test_si_publie($objet, $id_objet); |
|
| 332 | + include_spip('action/editer_objet'); |
|
| 333 | + objet_modifier($objet, $id_objet, $set); |
|
| 334 | + $publie_apres = objet_test_si_publie($objet, $id_objet); |
|
| 335 | + if ($publie_avant !== $publie_apres) { |
|
| 336 | + // on refuse ajax pour forcer le rechargement de la page ici |
|
| 337 | + // on refera traiter une 2eme fois, mais c'est sans consequence |
|
| 338 | + refuser_traiter_formulaire_ajax(); |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + if ($retour) { |
|
| 344 | + $res['redirect'] = $retour; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + set_request('date_jour'); |
|
| 348 | + set_request('date_redac_jour'); |
|
| 349 | + set_request('date_heure'); |
|
| 350 | + set_request('date_redac_heure'); |
|
| 351 | + |
|
| 352 | + return $res; |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | /** |
@@ -360,24 +360,24 @@ discard block |
||
| 360 | 360 | * @return array|string Chaîne vide si date invalide, tableau (année, mois, jour) sinon. |
| 361 | 361 | */ |
| 362 | 362 | function dater_recuperer_date_saisie($post, $quoi = 'date') { |
| 363 | - if (!preg_match('#^(?:(?:([0-9]{1,2})[/-])?([0-9]{1,2})[/-])?([0-9]{4}|[0-9]{1,2})#', $post, $regs)) { |
|
| 364 | - return ''; |
|
| 365 | - } |
|
| 366 | - if ($quoi == 'date_redac') { |
|
| 367 | - if ($regs[3] <> '' and $regs[3] < 1001) { |
|
| 368 | - $regs[3] += 9000; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return array($regs[3], $regs[2], $regs[1]); |
|
| 372 | - } else { |
|
| 373 | - if ( |
|
| 374 | - checkdate(intval($regs[2]), intval($regs[1]), intval($regs[3])) |
|
| 375 | - and $t = mktime(0, 0, 0, $regs[2], $regs[1], $regs[3]) |
|
| 376 | - ) { |
|
| 377 | - return array(date('Y', $t), date('m', $t), date('d', $t)); |
|
| 378 | - } |
|
| 379 | - return ''; |
|
| 380 | - } |
|
| 363 | + if (!preg_match('#^(?:(?:([0-9]{1,2})[/-])?([0-9]{1,2})[/-])?([0-9]{4}|[0-9]{1,2})#', $post, $regs)) { |
|
| 364 | + return ''; |
|
| 365 | + } |
|
| 366 | + if ($quoi == 'date_redac') { |
|
| 367 | + if ($regs[3] <> '' and $regs[3] < 1001) { |
|
| 368 | + $regs[3] += 9000; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return array($regs[3], $regs[2], $regs[1]); |
|
| 372 | + } else { |
|
| 373 | + if ( |
|
| 374 | + checkdate(intval($regs[2]), intval($regs[1]), intval($regs[3])) |
|
| 375 | + and $t = mktime(0, 0, 0, $regs[2], $regs[1], $regs[3]) |
|
| 376 | + ) { |
|
| 377 | + return array(date('Y', $t), date('m', $t), date('d', $t)); |
|
| 378 | + } |
|
| 379 | + return ''; |
|
| 380 | + } |
|
| 381 | 381 | } |
| 382 | 382 | |
| 383 | 383 | /** |
@@ -387,12 +387,12 @@ discard block |
||
| 387 | 387 | * @return array |
| 388 | 388 | */ |
| 389 | 389 | function dater_recuperer_heure_saisie($post) { |
| 390 | - if (!preg_match('#([0-9]{1,2})(?:[h:](?:([0-9]{1,2}))?)?#', $post, $regs)) { |
|
| 391 | - return ''; |
|
| 392 | - } |
|
| 393 | - if ($regs[1] > 23 or $regs[2] > 59) { |
|
| 394 | - return ''; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - return array($regs[1], $regs[2]); |
|
| 390 | + if (!preg_match('#([0-9]{1,2})(?:[h:](?:([0-9]{1,2}))?)?#', $post, $regs)) { |
|
| 391 | + return ''; |
|
| 392 | + } |
|
| 393 | + if ($regs[1] > 23 or $regs[2] > 59) { |
|
| 394 | + return ''; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + return array($regs[1], $regs[2]); |
|
| 398 | 398 | } |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | |
| 94 | - $row = sql_fetsel($select, $desc['table'], "$_id_objet=" . intval($id_objet)); |
|
| 94 | + $row = sql_fetsel($select, $desc['table'], "$_id_objet=".intval($id_objet)); |
|
| 95 | 95 | $statut = isset($row['statut']) ? $row['statut'] : 'publie'; // pas de statut => publie |
| 96 | 96 | |
| 97 | 97 | $valeurs['editable'] = autoriser('dater', $objet, $id_objet, null, array('statut' => $statut)); |
@@ -247,9 +247,9 @@ discard block |
||
| 247 | 247 | |
| 248 | 248 | if (_request('changer')) { |
| 249 | 249 | foreach (array('date', 'date_redac') as $k) { |
| 250 | - if ($v = _request($k . '_jour') and !dater_recuperer_date_saisie($v, $k)) { |
|
| 250 | + if ($v = _request($k.'_jour') and !dater_recuperer_date_saisie($v, $k)) { |
|
| 251 | 251 | $erreurs[$k] = _T('format_date_incorrecte'); |
| 252 | - } elseif ($v = _request($k . '_heure') and !dater_recuperer_heure_saisie($v)) { |
|
| 252 | + } elseif ($v = _request($k.'_heure') and !dater_recuperer_heure_saisie($v)) { |
|
| 253 | 253 | $erreurs[$k] = _T('format_heure_incorrecte'); |
| 254 | 254 | } |
| 255 | 255 | } |