@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | * @package SPIP\Core\Rubriques |
| 17 | 17 | **/ |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | include_spip('inc/actions'); |
@@ -28,21 +28,21 @@ discard block |
||
| 28 | 28 | * @uses ajax_retour() |
| 29 | 29 | **/ |
| 30 | 30 | function exec_selectionner_dist() { |
| 31 | - $id = intval(_request('id')); |
|
| 32 | - $exclus = intval(_request('exclus')); |
|
| 33 | - $type = _request('type'); |
|
| 34 | - $rac = _request('racine'); |
|
| 35 | - $do = _request('do'); |
|
| 36 | - if (preg_match('/^\w*$/', $do)) { |
|
| 37 | - if (!$do) { |
|
| 38 | - $do = 'aff'; |
|
| 39 | - } |
|
| 31 | + $id = intval(_request('id')); |
|
| 32 | + $exclus = intval(_request('exclus')); |
|
| 33 | + $type = _request('type'); |
|
| 34 | + $rac = _request('racine'); |
|
| 35 | + $do = _request('do'); |
|
| 36 | + if (preg_match('/^\w*$/', $do)) { |
|
| 37 | + if (!$do) { |
|
| 38 | + $do = 'aff'; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - $selectionner = charger_fonction('selectionner', 'inc'); |
|
| 41 | + $selectionner = charger_fonction('selectionner', 'inc'); |
|
| 42 | 42 | |
| 43 | - $r = $selectionner($id, 'choix_parent', $exclus, $rac, $type != 'breve', $do); |
|
| 44 | - } else { |
|
| 45 | - $r = ''; |
|
| 46 | - } |
|
| 47 | - ajax_retour($r); |
|
| 43 | + $r = $selectionner($id, 'choix_parent', $exclus, $rac, $type != 'breve', $do); |
|
| 44 | + } else { |
|
| 45 | + $r = ''; |
|
| 46 | + } |
|
| 47 | + ajax_retour($r); |
|
| 48 | 48 | } |
@@ -17,40 +17,40 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | 24 | * Exec de la page de destruction des tables de SPIP |
| 25 | 25 | **/ |
| 26 | 26 | function exec_base_delete_all_dist() { |
| 27 | - include_spip('inc/autoriser'); |
|
| 28 | - if (!autoriser('detruire')) { |
|
| 29 | - include_spip('inc/minipres'); |
|
| 30 | - echo minipres(); |
|
| 31 | - } else { |
|
| 32 | - include_spip('base/dump'); |
|
| 33 | - $res = base_lister_toutes_tables('', [], [], true); |
|
| 34 | - if (!$res) { |
|
| 35 | - include_spip('inc/minipres'); |
|
| 36 | - spip_log('Erreur base de donnees'); |
|
| 37 | - echo minipres( |
|
| 38 | - _T('info_travaux_titre'), |
|
| 39 | - _T('titre_probleme_technique') . '<p><tt>' . sql_errno() . ' ' . sql_error() . '</tt></p>' |
|
| 40 | - ); |
|
| 41 | - } else { |
|
| 42 | - $res = base_saisie_tables('delete', $res); |
|
| 43 | - include_spip('inc/headers'); |
|
| 44 | - $res = "\n<ol style='text-align:left'><li>\n" . |
|
| 45 | - join("</li>\n<li>", $res) . |
|
| 46 | - '</li></ol>'; |
|
| 47 | - $admin = charger_fonction('admin', 'inc'); |
|
| 48 | - $res = $admin('delete_all', _T('titre_page_delete_all'), $res); |
|
| 49 | - if (!$res) { |
|
| 50 | - redirige_url_ecrire('install', ''); |
|
| 51 | - } else { |
|
| 52 | - echo $res; |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - } |
|
| 27 | + include_spip('inc/autoriser'); |
|
| 28 | + if (!autoriser('detruire')) { |
|
| 29 | + include_spip('inc/minipres'); |
|
| 30 | + echo minipres(); |
|
| 31 | + } else { |
|
| 32 | + include_spip('base/dump'); |
|
| 33 | + $res = base_lister_toutes_tables('', [], [], true); |
|
| 34 | + if (!$res) { |
|
| 35 | + include_spip('inc/minipres'); |
|
| 36 | + spip_log('Erreur base de donnees'); |
|
| 37 | + echo minipres( |
|
| 38 | + _T('info_travaux_titre'), |
|
| 39 | + _T('titre_probleme_technique') . '<p><tt>' . sql_errno() . ' ' . sql_error() . '</tt></p>' |
|
| 40 | + ); |
|
| 41 | + } else { |
|
| 42 | + $res = base_saisie_tables('delete', $res); |
|
| 43 | + include_spip('inc/headers'); |
|
| 44 | + $res = "\n<ol style='text-align:left'><li>\n" . |
|
| 45 | + join("</li>\n<li>", $res) . |
|
| 46 | + '</li></ol>'; |
|
| 47 | + $admin = charger_fonction('admin', 'inc'); |
|
| 48 | + $res = $admin('delete_all', _T('titre_page_delete_all'), $res); |
|
| 49 | + if (!$res) { |
|
| 50 | + redirige_url_ecrire('install', ''); |
|
| 51 | + } else { |
|
| 52 | + echo $res; |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | 56 | } |
@@ -36,13 +36,13 @@ |
||
| 36 | 36 | spip_log('Erreur base de donnees'); |
| 37 | 37 | echo minipres( |
| 38 | 38 | _T('info_travaux_titre'), |
| 39 | - _T('titre_probleme_technique') . '<p><tt>' . sql_errno() . ' ' . sql_error() . '</tt></p>' |
|
| 39 | + _T('titre_probleme_technique').'<p><tt>'.sql_errno().' '.sql_error().'</tt></p>' |
|
| 40 | 40 | ); |
| 41 | 41 | } else { |
| 42 | 42 | $res = base_saisie_tables('delete', $res); |
| 43 | 43 | include_spip('inc/headers'); |
| 44 | - $res = "\n<ol style='text-align:left'><li>\n" . |
|
| 45 | - join("</li>\n<li>", $res) . |
|
| 44 | + $res = "\n<ol style='text-align:left'><li>\n". |
|
| 45 | + join("</li>\n<li>", $res). |
|
| 46 | 46 | '</li></ol>'; |
| 47 | 47 | $admin = charger_fonction('admin', 'inc'); |
| 48 | 48 | $res = $admin('delete_all', _T('titre_page_delete_all'), $res); |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | |
| 23 | 23 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 24 | - return; |
|
| 24 | + return; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | |
@@ -29,16 +29,16 @@ discard block |
||
| 29 | 29 | $GLOBALS['delais'] = 0;// pas de cache ! |
| 30 | 30 | // Securite |
| 31 | 31 | if (strstr($fond, '/')) { |
| 32 | - if ( |
|
| 33 | - !include_spip('inc/autoriser') |
|
| 34 | - or !autoriser('webmestre') |
|
| 35 | - ) { |
|
| 36 | - include_spip('inc/minipres'); |
|
| 37 | - echo minipres(); |
|
| 38 | - exit; |
|
| 39 | - } |
|
| 32 | + if ( |
|
| 33 | + !include_spip('inc/autoriser') |
|
| 34 | + or !autoriser('webmestre') |
|
| 35 | + ) { |
|
| 36 | + include_spip('inc/minipres'); |
|
| 37 | + echo minipres(); |
|
| 38 | + exit; |
|
| 39 | + } |
|
| 40 | 40 | } else { |
| 41 | - $fond = "prive/squelettes/$fond"; |
|
| 41 | + $fond = "prive/squelettes/$fond"; |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | // quelques inclusions et ini prealables |
@@ -50,8 +50,8 @@ discard block |
||
| 50 | 50 | * @todo supprimer cette fonction vide ? |
| 51 | 51 | **/ |
| 52 | 52 | function shutdown_error() { |
| 53 | - // si on arrive ici avec un tampon non ferme : erreur fatale |
|
| 54 | - /* if (ob_get_level()){ |
|
| 53 | + // si on arrive ici avec un tampon non ferme : erreur fatale |
|
| 54 | + /* if (ob_get_level()){ |
|
| 55 | 55 | // envoyer tous les tampons |
| 56 | 56 | while (ob_get_level()) |
| 57 | 57 | ob_end_flush(); |
@@ -26,7 +26,7 @@ |
||
| 26 | 26 | |
| 27 | 27 | |
| 28 | 28 | $fond = _request('exec'); |
| 29 | -$GLOBALS['delais'] = 0;// pas de cache ! |
|
| 29 | +$GLOBALS['delais'] = 0; // pas de cache ! |
|
| 30 | 30 | // Securite |
| 31 | 31 | if (strstr($fond, '/')) { |
| 32 | 32 | if ( |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - return; |
|
| 21 | + return; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | define('_DEFAULT_DB', 'spip'); |
@@ -32,130 +32,130 @@ discard block |
||
| 32 | 32 | |
| 33 | 33 | // https://code.spip.net/@req_pg_dist |
| 34 | 34 | function req_pg_dist($addr, $port, $login, $pass, $db = '', $prefixe = '') { |
| 35 | - static $last_connect = []; |
|
| 36 | - if (!extension_loaded('pgsql')) { |
|
| 37 | - return false; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - // si provient de selectdb |
|
| 41 | - if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 42 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 43 | - $$a = $last_connect[$a]; |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - @list($host, $p) = explode(';', $addr); |
|
| 47 | - if ($p > 0) { |
|
| 48 | - $port = " port=$p"; |
|
| 49 | - } else { |
|
| 50 | - $port = ''; |
|
| 51 | - } |
|
| 52 | - $erreurs = []; |
|
| 53 | - if ($db) { |
|
| 54 | - @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 55 | - } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 56 | - $erreurs[] = pg_last_error(); |
|
| 57 | - if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 58 | - $db = $login; |
|
| 59 | - } else { |
|
| 60 | - $erreurs[] = pg_last_error(); |
|
| 61 | - $db = _DEFAULT_DB; |
|
| 62 | - $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - if (!$link) { |
|
| 66 | - $erreurs[] = pg_last_error(); |
|
| 67 | - foreach ($erreurs as $e) { |
|
| 68 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - return false; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - if ($link) { |
|
| 75 | - $last_connect = [ |
|
| 76 | - 'addr' => $addr, |
|
| 77 | - 'port' => $port, |
|
| 78 | - 'login' => $login, |
|
| 79 | - 'pass' => $pass, |
|
| 80 | - 'db' => $db, |
|
| 81 | - 'prefixe' => $prefixe, |
|
| 82 | - ]; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - spip_log( |
|
| 86 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 87 | - 'pg.' . _LOG_DEBUG |
|
| 88 | - ); |
|
| 89 | - |
|
| 90 | - return !$link ? false : [ |
|
| 91 | - 'db' => $db, |
|
| 92 | - 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 93 | - 'link' => $link, |
|
| 94 | - ]; |
|
| 35 | + static $last_connect = []; |
|
| 36 | + if (!extension_loaded('pgsql')) { |
|
| 37 | + return false; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + // si provient de selectdb |
|
| 41 | + if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 42 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 43 | + $$a = $last_connect[$a]; |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + @list($host, $p) = explode(';', $addr); |
|
| 47 | + if ($p > 0) { |
|
| 48 | + $port = " port=$p"; |
|
| 49 | + } else { |
|
| 50 | + $port = ''; |
|
| 51 | + } |
|
| 52 | + $erreurs = []; |
|
| 53 | + if ($db) { |
|
| 54 | + @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 55 | + } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 56 | + $erreurs[] = pg_last_error(); |
|
| 57 | + if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 58 | + $db = $login; |
|
| 59 | + } else { |
|
| 60 | + $erreurs[] = pg_last_error(); |
|
| 61 | + $db = _DEFAULT_DB; |
|
| 62 | + $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + if (!$link) { |
|
| 66 | + $erreurs[] = pg_last_error(); |
|
| 67 | + foreach ($erreurs as $e) { |
|
| 68 | + spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + return false; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + if ($link) { |
|
| 75 | + $last_connect = [ |
|
| 76 | + 'addr' => $addr, |
|
| 77 | + 'port' => $port, |
|
| 78 | + 'login' => $login, |
|
| 79 | + 'pass' => $pass, |
|
| 80 | + 'db' => $db, |
|
| 81 | + 'prefixe' => $prefixe, |
|
| 82 | + ]; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + spip_log( |
|
| 86 | + "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 87 | + 'pg.' . _LOG_DEBUG |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + return !$link ? false : [ |
|
| 91 | + 'db' => $db, |
|
| 92 | + 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 93 | + 'link' => $link, |
|
| 94 | + ]; |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | $GLOBALS['spip_pg_functions_1'] = [ |
| 98 | - 'alter' => 'spip_pg_alter', |
|
| 99 | - 'count' => 'spip_pg_count', |
|
| 100 | - 'countsel' => 'spip_pg_countsel', |
|
| 101 | - 'create' => 'spip_pg_create', |
|
| 102 | - 'create_base' => 'spip_pg_create_base', |
|
| 103 | - 'create_view' => 'spip_pg_create_view', |
|
| 104 | - 'date_proche' => 'spip_pg_date_proche', |
|
| 105 | - 'delete' => 'spip_pg_delete', |
|
| 106 | - 'drop_table' => 'spip_pg_drop_table', |
|
| 107 | - 'drop_view' => 'spip_pg_drop_view', |
|
| 108 | - 'errno' => 'spip_pg_errno', |
|
| 109 | - 'error' => 'spip_pg_error', |
|
| 110 | - 'explain' => 'spip_pg_explain', |
|
| 111 | - 'fetch' => 'spip_pg_fetch', |
|
| 112 | - 'seek' => 'spip_pg_seek', |
|
| 113 | - 'free' => 'spip_pg_free', |
|
| 114 | - 'hex' => 'spip_pg_hex', |
|
| 115 | - 'in' => 'spip_pg_in', |
|
| 116 | - 'insert' => 'spip_pg_insert', |
|
| 117 | - 'insertq' => 'spip_pg_insertq', |
|
| 118 | - 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 119 | - 'listdbs' => 'spip_pg_listdbs', |
|
| 120 | - 'multi' => 'spip_pg_multi', |
|
| 121 | - 'optimize' => 'spip_pg_optimize', |
|
| 122 | - 'query' => 'spip_pg_query', |
|
| 123 | - 'quote' => 'spip_pg_quote', |
|
| 124 | - 'replace' => 'spip_pg_replace', |
|
| 125 | - 'replace_multi' => 'spip_pg_replace_multi', |
|
| 126 | - 'select' => 'spip_pg_select', |
|
| 127 | - 'selectdb' => 'spip_pg_selectdb', |
|
| 128 | - 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 129 | - 'showbase' => 'spip_pg_showbase', |
|
| 130 | - 'showtable' => 'spip_pg_showtable', |
|
| 131 | - 'update' => 'spip_pg_update', |
|
| 132 | - 'updateq' => 'spip_pg_updateq', |
|
| 98 | + 'alter' => 'spip_pg_alter', |
|
| 99 | + 'count' => 'spip_pg_count', |
|
| 100 | + 'countsel' => 'spip_pg_countsel', |
|
| 101 | + 'create' => 'spip_pg_create', |
|
| 102 | + 'create_base' => 'spip_pg_create_base', |
|
| 103 | + 'create_view' => 'spip_pg_create_view', |
|
| 104 | + 'date_proche' => 'spip_pg_date_proche', |
|
| 105 | + 'delete' => 'spip_pg_delete', |
|
| 106 | + 'drop_table' => 'spip_pg_drop_table', |
|
| 107 | + 'drop_view' => 'spip_pg_drop_view', |
|
| 108 | + 'errno' => 'spip_pg_errno', |
|
| 109 | + 'error' => 'spip_pg_error', |
|
| 110 | + 'explain' => 'spip_pg_explain', |
|
| 111 | + 'fetch' => 'spip_pg_fetch', |
|
| 112 | + 'seek' => 'spip_pg_seek', |
|
| 113 | + 'free' => 'spip_pg_free', |
|
| 114 | + 'hex' => 'spip_pg_hex', |
|
| 115 | + 'in' => 'spip_pg_in', |
|
| 116 | + 'insert' => 'spip_pg_insert', |
|
| 117 | + 'insertq' => 'spip_pg_insertq', |
|
| 118 | + 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 119 | + 'listdbs' => 'spip_pg_listdbs', |
|
| 120 | + 'multi' => 'spip_pg_multi', |
|
| 121 | + 'optimize' => 'spip_pg_optimize', |
|
| 122 | + 'query' => 'spip_pg_query', |
|
| 123 | + 'quote' => 'spip_pg_quote', |
|
| 124 | + 'replace' => 'spip_pg_replace', |
|
| 125 | + 'replace_multi' => 'spip_pg_replace_multi', |
|
| 126 | + 'select' => 'spip_pg_select', |
|
| 127 | + 'selectdb' => 'spip_pg_selectdb', |
|
| 128 | + 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 129 | + 'showbase' => 'spip_pg_showbase', |
|
| 130 | + 'showtable' => 'spip_pg_showtable', |
|
| 131 | + 'update' => 'spip_pg_update', |
|
| 132 | + 'updateq' => 'spip_pg_updateq', |
|
| 133 | 133 | ]; |
| 134 | 134 | |
| 135 | 135 | // Par ou ca passe une fois les traductions faites |
| 136 | 136 | // https://code.spip.net/@spip_pg_trace_query |
| 137 | 137 | function spip_pg_trace_query($query, $serveur = '') { |
| 138 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 139 | - $prefixe = $connexion['prefixe']; |
|
| 140 | - $link = $connexion['link']; |
|
| 141 | - $db = $connexion['db']; |
|
| 142 | - |
|
| 143 | - if (isset($_GET['var_profile'])) { |
|
| 144 | - include_spip('public/tracer'); |
|
| 145 | - $t = trace_query_start(); |
|
| 146 | - $e = ''; |
|
| 147 | - } else { |
|
| 148 | - $t = 0; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $connexion['last'] = $query; |
|
| 152 | - $r = spip_pg_query_simple($link, $query); |
|
| 153 | - |
|
| 154 | - // Log de l'erreur eventuelle |
|
| 155 | - if ($e = spip_pg_errno($serveur)) { |
|
| 156 | - $e .= spip_pg_error($query, $serveur); |
|
| 157 | - } // et du fautif |
|
| 158 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 138 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 139 | + $prefixe = $connexion['prefixe']; |
|
| 140 | + $link = $connexion['link']; |
|
| 141 | + $db = $connexion['db']; |
|
| 142 | + |
|
| 143 | + if (isset($_GET['var_profile'])) { |
|
| 144 | + include_spip('public/tracer'); |
|
| 145 | + $t = trace_query_start(); |
|
| 146 | + $e = ''; |
|
| 147 | + } else { |
|
| 148 | + $t = 0; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $connexion['last'] = $query; |
|
| 152 | + $r = spip_pg_query_simple($link, $query); |
|
| 153 | + |
|
| 154 | + // Log de l'erreur eventuelle |
|
| 155 | + if ($e = spip_pg_errno($serveur)) { |
|
| 156 | + $e .= spip_pg_error($query, $serveur); |
|
| 157 | + } // et du fautif |
|
| 158 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | // Fonction de requete generale quand on est sur que c'est SQL standard. |
@@ -163,30 +163,30 @@ discard block |
||
| 163 | 163 | |
| 164 | 164 | // https://code.spip.net/@spip_pg_query |
| 165 | 165 | function spip_pg_query($query, $serveur = '', $requeter = true) { |
| 166 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 167 | - $prefixe = $connexion['prefixe']; |
|
| 168 | - $link = $connexion['link']; |
|
| 169 | - $db = $connexion['db']; |
|
| 170 | - |
|
| 171 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 172 | - $suite = strstr($query, $regs[0]); |
|
| 173 | - $query = substr($query, 0, -strlen($suite)); |
|
| 174 | - } else { |
|
| 175 | - $suite = ''; |
|
| 176 | - } |
|
| 177 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 178 | - |
|
| 179 | - // renvoyer la requete inerte si demandee |
|
| 180 | - if (!$requeter) { |
|
| 181 | - return $query; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - return spip_pg_trace_query($query, $serveur); |
|
| 166 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 167 | + $prefixe = $connexion['prefixe']; |
|
| 168 | + $link = $connexion['link']; |
|
| 169 | + $db = $connexion['db']; |
|
| 170 | + |
|
| 171 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 172 | + $suite = strstr($query, $regs[0]); |
|
| 173 | + $query = substr($query, 0, -strlen($suite)); |
|
| 174 | + } else { |
|
| 175 | + $suite = ''; |
|
| 176 | + } |
|
| 177 | + $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 178 | + |
|
| 179 | + // renvoyer la requete inerte si demandee |
|
| 180 | + if (!$requeter) { |
|
| 181 | + return $query; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + return spip_pg_trace_query($query, $serveur); |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | function spip_pg_query_simple($link, $query) { |
| 188 | - #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 189 | - return pg_query($link, $query); |
|
| 188 | + #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 189 | + return pg_query($link, $query); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | /* |
@@ -198,199 +198,199 @@ discard block |
||
| 198 | 198 | * de requetes showtable intempestives |
| 199 | 199 | */ |
| 200 | 200 | function spip_pg_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 201 | - static $tables = []; |
|
| 202 | - |
|
| 203 | - if (!isset($tables[$table])) { |
|
| 204 | - if (!$desc) { |
|
| 205 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 206 | - $desc = $trouver_table($table, $serveur); |
|
| 207 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 208 | - if (!$desc) { |
|
| 209 | - return $couples; |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 214 | - // cependant, il faudra peut etre etendre |
|
| 215 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 216 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 217 | - $tables[$table] = []; |
|
| 218 | - foreach ($desc['field'] as $k => $v) { |
|
| 219 | - $v = strtolower(ltrim($v)); |
|
| 220 | - // ne pas ajouter de timestamp now() si un default est specifie |
|
| 221 | - if (strpos($v, 'timestamp') === 0 and strpos($v, 'default') === false) { |
|
| 222 | - $tables[$table][] = $k; |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // ajout des champs type 'timestamp' absents |
|
| 228 | - foreach ($tables[$table] as $maj) { |
|
| 229 | - if (!array_key_exists($maj, $couples)) { |
|
| 230 | - $couples[$maj] = 'NOW()'; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - return $couples; |
|
| 201 | + static $tables = []; |
|
| 202 | + |
|
| 203 | + if (!isset($tables[$table])) { |
|
| 204 | + if (!$desc) { |
|
| 205 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 206 | + $desc = $trouver_table($table, $serveur); |
|
| 207 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 208 | + if (!$desc) { |
|
| 209 | + return $couples; |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 214 | + // cependant, il faudra peut etre etendre |
|
| 215 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 216 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 217 | + $tables[$table] = []; |
|
| 218 | + foreach ($desc['field'] as $k => $v) { |
|
| 219 | + $v = strtolower(ltrim($v)); |
|
| 220 | + // ne pas ajouter de timestamp now() si un default est specifie |
|
| 221 | + if (strpos($v, 'timestamp') === 0 and strpos($v, 'default') === false) { |
|
| 222 | + $tables[$table][] = $k; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // ajout des champs type 'timestamp' absents |
|
| 228 | + foreach ($tables[$table] as $maj) { |
|
| 229 | + if (!array_key_exists($maj, $couples)) { |
|
| 230 | + $couples[$maj] = 'NOW()'; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + return $couples; |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | |
| 238 | 238 | // Alter en PG ne traite pas les index |
| 239 | 239 | // https://code.spip.net/@spip_pg_alter |
| 240 | 240 | function spip_pg_alter($query, $serveur = '', $requeter = true) { |
| 241 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 242 | - // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 243 | - // ou revoir l'api de sql_alter en creant un |
|
| 244 | - // sql_alter_table($table,array($actions)); |
|
| 245 | - if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 246 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 247 | - |
|
| 248 | - return false; |
|
| 249 | - } |
|
| 250 | - $debut = $regs[1]; |
|
| 251 | - $table = $regs[3]; |
|
| 252 | - $suite = $regs[4]; |
|
| 253 | - $todo = explode(',', $suite); |
|
| 254 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 255 | - $todo2 = []; |
|
| 256 | - $i = 0; |
|
| 257 | - $ouverte = false; |
|
| 258 | - while ($do = array_shift($todo)) { |
|
| 259 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 260 | - $o = (false !== strpos($do, '(')); |
|
| 261 | - $f = (false !== strpos($do, ')')); |
|
| 262 | - if ($o and !$f) { |
|
| 263 | - $ouverte = true; |
|
| 264 | - } elseif ($f) { |
|
| 265 | - $ouverte = false; |
|
| 266 | - } |
|
| 267 | - if (!$ouverte) { |
|
| 268 | - $i++; |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - $todo = $todo2; |
|
| 272 | - $query = $debut . ' ' . array_shift($todo); |
|
| 273 | - |
|
| 274 | - if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 275 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 276 | - } else { |
|
| 277 | - if ($r[1]) { |
|
| 278 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 279 | - } |
|
| 280 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 281 | - if (function_exists($f)) { |
|
| 282 | - $f($r[2], $r[4], $serveur, $requeter); |
|
| 283 | - } else { |
|
| 284 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - // Alter a plusieurs args. Faudrait optimiser. |
|
| 288 | - if ($todo) { |
|
| 289 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 290 | - } |
|
| 241 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 242 | + // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 243 | + // ou revoir l'api de sql_alter en creant un |
|
| 244 | + // sql_alter_table($table,array($actions)); |
|
| 245 | + if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 246 | + spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 247 | + |
|
| 248 | + return false; |
|
| 249 | + } |
|
| 250 | + $debut = $regs[1]; |
|
| 251 | + $table = $regs[3]; |
|
| 252 | + $suite = $regs[4]; |
|
| 253 | + $todo = explode(',', $suite); |
|
| 254 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 255 | + $todo2 = []; |
|
| 256 | + $i = 0; |
|
| 257 | + $ouverte = false; |
|
| 258 | + while ($do = array_shift($todo)) { |
|
| 259 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 260 | + $o = (false !== strpos($do, '(')); |
|
| 261 | + $f = (false !== strpos($do, ')')); |
|
| 262 | + if ($o and !$f) { |
|
| 263 | + $ouverte = true; |
|
| 264 | + } elseif ($f) { |
|
| 265 | + $ouverte = false; |
|
| 266 | + } |
|
| 267 | + if (!$ouverte) { |
|
| 268 | + $i++; |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + $todo = $todo2; |
|
| 272 | + $query = $debut . ' ' . array_shift($todo); |
|
| 273 | + |
|
| 274 | + if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 275 | + spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 276 | + } else { |
|
| 277 | + if ($r[1]) { |
|
| 278 | + spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 279 | + } |
|
| 280 | + $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 281 | + if (function_exists($f)) { |
|
| 282 | + $f($r[2], $r[4], $serveur, $requeter); |
|
| 283 | + } else { |
|
| 284 | + spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + // Alter a plusieurs args. Faudrait optimiser. |
|
| 288 | + if ($todo) { |
|
| 289 | + spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 290 | + } |
|
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | // https://code.spip.net/@spip_pg_alter_change |
| 294 | 294 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 295 | - if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 296 | - spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 297 | - } else { |
|
| 298 | - list(, $old, $new, $type, $default, $null, $def2) = $r; |
|
| 299 | - $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 300 | - if ($null) { |
|
| 301 | - $actions[] = "ALTER $old SET NOT NULL"; |
|
| 302 | - } else { |
|
| 303 | - $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - if ($d = ($default ? $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 = ["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( |
|
| 340 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 341 | - $serveur, |
|
| 342 | - $requeter |
|
| 343 | - ); |
|
| 344 | - } else { |
|
| 345 | - preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 346 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 347 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 348 | - // |
|
| 349 | - // nom_index (colonnes) |
|
| 350 | - if ($m[2]) { |
|
| 351 | - $colonnes = substr($m[2], 1, -1); |
|
| 352 | - $nom_index = $m[1]; |
|
| 353 | - } else { |
|
| 354 | - // (colonne) |
|
| 355 | - if ($m[1][0] == '(') { |
|
| 356 | - $colonnes = substr($m[1], 1, -1); |
|
| 357 | - if (false !== strpos(',', $colonnes)) { |
|
| 358 | - spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 359 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 360 | - } else { |
|
| 361 | - $nom_index = $colonnes; |
|
| 362 | - } |
|
| 363 | - } // nom_index |
|
| 364 | - else { |
|
| 365 | - $nom_index = $colonnes = $m[1]; |
|
| 366 | - } |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 370 | - } |
|
| 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( |
|
| 340 | + "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 341 | + $serveur, |
|
| 342 | + $requeter |
|
| 343 | + ); |
|
| 344 | + } else { |
|
| 345 | + preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 346 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 347 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 348 | + // |
|
| 349 | + // nom_index (colonnes) |
|
| 350 | + if ($m[2]) { |
|
| 351 | + $colonnes = substr($m[2], 1, -1); |
|
| 352 | + $nom_index = $m[1]; |
|
| 353 | + } else { |
|
| 354 | + // (colonne) |
|
| 355 | + if ($m[1][0] == '(') { |
|
| 356 | + $colonnes = substr($m[1], 1, -1); |
|
| 357 | + if (false !== strpos(',', $colonnes)) { |
|
| 358 | + spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 359 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 360 | + } else { |
|
| 361 | + $nom_index = $colonnes; |
|
| 362 | + } |
|
| 363 | + } // nom_index |
|
| 364 | + else { |
|
| 365 | + $nom_index = $colonnes = $m[1]; |
|
| 366 | + } |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 370 | + } |
|
| 371 | 371 | } |
| 372 | 372 | |
| 373 | 373 | // https://code.spip.net/@spip_pg_alter_drop |
| 374 | 374 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 375 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 376 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 377 | - } else { |
|
| 378 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 379 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 380 | - } elseif ($r[1][0] == 'P') { |
|
| 381 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 382 | - } else { |
|
| 383 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 384 | - } |
|
| 385 | - } |
|
| 375 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 376 | + spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 377 | + } else { |
|
| 378 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 379 | + return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 380 | + } elseif ($r[1][0] == 'P') { |
|
| 381 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 382 | + } else { |
|
| 383 | + return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | 386 | } |
| 387 | 387 | |
| 388 | 388 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 389 | - if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 390 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 391 | - } else { |
|
| 392 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 393 | - } |
|
| 389 | + if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 390 | + spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 391 | + } else { |
|
| 392 | + return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 393 | + } |
|
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | // attention (en pg) : |
@@ -398,17 +398,17 @@ discard block |
||
| 398 | 398 | // - alter table A rename X to Y = changer le nom de la colonne X en Y |
| 399 | 399 | // pour l'instant, traiter simplement RENAME TO X |
| 400 | 400 | function spip_pg_alter_rename($table, $arg, $serveur = '', $requeter = true) { |
| 401 | - $rename = ''; |
|
| 402 | - // si TO, mais pas au debut |
|
| 403 | - if (!stripos($arg, 'TO ')) { |
|
| 404 | - $rename = $arg; |
|
| 405 | - } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 406 | - $rename = $r[2]; |
|
| 407 | - } else { |
|
| 408 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 401 | + $rename = ''; |
|
| 402 | + // si TO, mais pas au debut |
|
| 403 | + if (!stripos($arg, 'TO ')) { |
|
| 404 | + $rename = $arg; |
|
| 405 | + } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 406 | + $rename = $r[2]; |
|
| 407 | + } else { |
|
| 408 | + spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | |
@@ -424,60 +424,60 @@ discard block |
||
| 424 | 424 | * @return bool ou requete |
| 425 | 425 | */ |
| 426 | 426 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 427 | - if (!($nom or $table or $champs)) { |
|
| 428 | - spip_log( |
|
| 429 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 430 | - 'pg.' . _LOG_ERREUR |
|
| 431 | - ); |
|
| 432 | - |
|
| 433 | - return false; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - $nom = str_replace('`', '', $nom); |
|
| 437 | - $champs = str_replace('`', '', $champs); |
|
| 438 | - |
|
| 439 | - // PG ne differentie pas noms des index en fonction des tables |
|
| 440 | - // il faut donc creer des noms uniques d'index pour une base pg |
|
| 441 | - $nom = $table . '_' . $nom; |
|
| 442 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 443 | - if (!is_array($champs)) { |
|
| 444 | - if ($champs[0] == '(') { |
|
| 445 | - $champs = substr($champs, 1, -1); |
|
| 446 | - } |
|
| 447 | - $champs = [$champs]; |
|
| 448 | - } |
|
| 449 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 450 | - if (!$requeter) { |
|
| 451 | - return $query; |
|
| 452 | - } |
|
| 453 | - $res = spip_pg_query($query, $serveur, $requeter); |
|
| 454 | - |
|
| 455 | - return $res; |
|
| 427 | + if (!($nom or $table or $champs)) { |
|
| 428 | + spip_log( |
|
| 429 | + "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 430 | + 'pg.' . _LOG_ERREUR |
|
| 431 | + ); |
|
| 432 | + |
|
| 433 | + return false; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + $nom = str_replace('`', '', $nom); |
|
| 437 | + $champs = str_replace('`', '', $champs); |
|
| 438 | + |
|
| 439 | + // PG ne differentie pas noms des index en fonction des tables |
|
| 440 | + // il faut donc creer des noms uniques d'index pour une base pg |
|
| 441 | + $nom = $table . '_' . $nom; |
|
| 442 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 443 | + if (!is_array($champs)) { |
|
| 444 | + if ($champs[0] == '(') { |
|
| 445 | + $champs = substr($champs, 1, -1); |
|
| 446 | + } |
|
| 447 | + $champs = [$champs]; |
|
| 448 | + } |
|
| 449 | + $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 450 | + if (!$requeter) { |
|
| 451 | + return $query; |
|
| 452 | + } |
|
| 453 | + $res = spip_pg_query($query, $serveur, $requeter); |
|
| 454 | + |
|
| 455 | + return $res; |
|
| 456 | 456 | } |
| 457 | 457 | |
| 458 | 458 | |
| 459 | 459 | // https://code.spip.net/@spip_pg_explain |
| 460 | 460 | function spip_pg_explain($query, $serveur = '', $requeter = true) { |
| 461 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 462 | - return []; |
|
| 463 | - } |
|
| 464 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 465 | - $prefixe = $connexion['prefixe']; |
|
| 466 | - $link = $connexion['link']; |
|
| 467 | - if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 468 | - $suite = strstr($query, $regs[0]); |
|
| 469 | - $query = substr($query, 0, -strlen($suite)); |
|
| 470 | - } else { |
|
| 471 | - $suite = ''; |
|
| 472 | - } |
|
| 473 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 474 | - |
|
| 475 | - if (!$requeter) { |
|
| 476 | - return $query; |
|
| 477 | - } |
|
| 478 | - $r = spip_pg_query_simple($link, $query); |
|
| 479 | - |
|
| 480 | - return spip_pg_fetch($r, null, $serveur); |
|
| 461 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 462 | + return []; |
|
| 463 | + } |
|
| 464 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 465 | + $prefixe = $connexion['prefixe']; |
|
| 466 | + $link = $connexion['link']; |
|
| 467 | + if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 468 | + $suite = strstr($query, $regs[0]); |
|
| 469 | + $query = substr($query, 0, -strlen($suite)); |
|
| 470 | + } else { |
|
| 471 | + $suite = ''; |
|
| 472 | + } |
|
| 473 | + $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 474 | + |
|
| 475 | + if (!$requeter) { |
|
| 476 | + return $query; |
|
| 477 | + } |
|
| 478 | + $r = spip_pg_query_simple($link, $query); |
|
| 479 | + |
|
| 480 | + return spip_pg_fetch($r, null, $serveur); |
|
| 481 | 481 | } |
| 482 | 482 | |
| 483 | 483 | |
@@ -496,94 +496,94 @@ discard block |
||
| 496 | 496 | * - False en cas d'erreur. |
| 497 | 497 | **/ |
| 498 | 498 | function spip_pg_selectdb($db, $serveur = '', $requeter = true) { |
| 499 | - // se connecter a la base indiquee |
|
| 500 | - // avec les identifiants connus |
|
| 501 | - $index = $serveur ? strtolower($serveur) : 0; |
|
| 502 | - |
|
| 503 | - if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 504 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 505 | - return $db; |
|
| 506 | - } |
|
| 507 | - } else { |
|
| 508 | - return false; |
|
| 509 | - } |
|
| 499 | + // se connecter a la base indiquee |
|
| 500 | + // avec les identifiants connus |
|
| 501 | + $index = $serveur ? strtolower($serveur) : 0; |
|
| 502 | + |
|
| 503 | + if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 504 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 505 | + return $db; |
|
| 506 | + } |
|
| 507 | + } else { |
|
| 508 | + return false; |
|
| 509 | + } |
|
| 510 | 510 | } |
| 511 | 511 | |
| 512 | 512 | // Qu'une seule base pour le moment |
| 513 | 513 | |
| 514 | 514 | // https://code.spip.net/@spip_pg_listdbs |
| 515 | 515 | function spip_pg_listdbs($serveur) { |
| 516 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 517 | - $link = $connexion['link']; |
|
| 518 | - $dbs = []; |
|
| 519 | - $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 520 | - while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 521 | - $dbs[] = reset($row); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - return $dbs; |
|
| 516 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 517 | + $link = $connexion['link']; |
|
| 518 | + $dbs = []; |
|
| 519 | + $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 520 | + while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 521 | + $dbs[] = reset($row); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + return $dbs; |
|
| 525 | 525 | } |
| 526 | 526 | |
| 527 | 527 | // https://code.spip.net/@spip_pg_select |
| 528 | 528 | function spip_pg_select( |
| 529 | - $select, |
|
| 530 | - $from, |
|
| 531 | - $where = '', |
|
| 532 | - $groupby = [], |
|
| 533 | - $orderby = '', |
|
| 534 | - $limit = '', |
|
| 535 | - $having = '', |
|
| 536 | - $serveur = '', |
|
| 537 | - $requeter = true |
|
| 529 | + $select, |
|
| 530 | + $from, |
|
| 531 | + $where = '', |
|
| 532 | + $groupby = [], |
|
| 533 | + $orderby = '', |
|
| 534 | + $limit = '', |
|
| 535 | + $having = '', |
|
| 536 | + $serveur = '', |
|
| 537 | + $requeter = true |
|
| 538 | 538 | ) { |
| 539 | 539 | |
| 540 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 541 | - $prefixe = $connexion['prefixe']; |
|
| 542 | - $link = $connexion['link']; |
|
| 543 | - $db = $connexion['db']; |
|
| 544 | - |
|
| 545 | - $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 546 | - if ($limit) { |
|
| 547 | - $offset = $limatch[2]; |
|
| 548 | - $count = $limatch[3]; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - $select = spip_pg_frommysql($select); |
|
| 552 | - |
|
| 553 | - // si pas de tri explicitement demande, le GROUP BY ne |
|
| 554 | - // contient que la clef primaire. |
|
| 555 | - // lui ajouter alors le champ de tri par defaut |
|
| 556 | - if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 557 | - $groupby[] = $groupbyplus[1]; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - $orderby = spip_pg_orderby($orderby, $select); |
|
| 561 | - |
|
| 562 | - if ($having) { |
|
| 563 | - if (is_array($having)) { |
|
| 564 | - $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 565 | - } |
|
| 566 | - } |
|
| 567 | - $from = spip_pg_from($from, $prefixe); |
|
| 568 | - $query = 'SELECT ' . $select |
|
| 569 | - . (!$from ? '' : "\nFROM $from") |
|
| 570 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 571 | - "\n\tAND ", |
|
| 572 | - array_map('calculer_pg_where', $where) |
|
| 573 | - ))))) |
|
| 574 | - . spip_pg_groupby($groupby, $from, $select) |
|
| 575 | - . (!$having ? '' : "\nHAVING $having") |
|
| 576 | - . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 577 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 578 | - |
|
| 579 | - // renvoyer la requete inerte si demandee |
|
| 580 | - if ($requeter === false) { |
|
| 581 | - return $query; |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - $r = spip_pg_trace_query($query, $serveur); |
|
| 585 | - |
|
| 586 | - return $r ? $r : $query; |
|
| 540 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 541 | + $prefixe = $connexion['prefixe']; |
|
| 542 | + $link = $connexion['link']; |
|
| 543 | + $db = $connexion['db']; |
|
| 544 | + |
|
| 545 | + $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 546 | + if ($limit) { |
|
| 547 | + $offset = $limatch[2]; |
|
| 548 | + $count = $limatch[3]; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + $select = spip_pg_frommysql($select); |
|
| 552 | + |
|
| 553 | + // si pas de tri explicitement demande, le GROUP BY ne |
|
| 554 | + // contient que la clef primaire. |
|
| 555 | + // lui ajouter alors le champ de tri par defaut |
|
| 556 | + if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 557 | + $groupby[] = $groupbyplus[1]; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + $orderby = spip_pg_orderby($orderby, $select); |
|
| 561 | + |
|
| 562 | + if ($having) { |
|
| 563 | + if (is_array($having)) { |
|
| 564 | + $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 565 | + } |
|
| 566 | + } |
|
| 567 | + $from = spip_pg_from($from, $prefixe); |
|
| 568 | + $query = 'SELECT ' . $select |
|
| 569 | + . (!$from ? '' : "\nFROM $from") |
|
| 570 | + . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 571 | + "\n\tAND ", |
|
| 572 | + array_map('calculer_pg_where', $where) |
|
| 573 | + ))))) |
|
| 574 | + . spip_pg_groupby($groupby, $from, $select) |
|
| 575 | + . (!$having ? '' : "\nHAVING $having") |
|
| 576 | + . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 577 | + . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 578 | + |
|
| 579 | + // renvoyer la requete inerte si demandee |
|
| 580 | + if ($requeter === false) { |
|
| 581 | + return $query; |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + $r = spip_pg_trace_query($query, $serveur); |
|
| 585 | + |
|
| 586 | + return $r ? $r : $query; |
|
| 587 | 587 | ; |
| 588 | 588 | } |
| 589 | 589 | |
@@ -592,27 +592,27 @@ discard block |
||
| 592 | 592 | |
| 593 | 593 | // https://code.spip.net/@spip_pg_from |
| 594 | 594 | function spip_pg_from($from, $prefixe) { |
| 595 | - if (is_array($from)) { |
|
| 596 | - $from = spip_pg_select_as($from); |
|
| 597 | - } |
|
| 595 | + if (is_array($from)) { |
|
| 596 | + $from = spip_pg_select_as($from); |
|
| 597 | + } |
|
| 598 | 598 | |
| 599 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 599 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 600 | 600 | } |
| 601 | 601 | |
| 602 | 602 | // https://code.spip.net/@spip_pg_orderby |
| 603 | 603 | function spip_pg_orderby($order, $select) { |
| 604 | - $res = []; |
|
| 605 | - $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 606 | - |
|
| 607 | - foreach ($arg as $v) { |
|
| 608 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 609 | - $res[] = $m[1]; |
|
| 610 | - } else { |
|
| 611 | - $res[] = $v; |
|
| 612 | - } |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - return spip_pg_frommysql(join(',', $res)); |
|
| 604 | + $res = []; |
|
| 605 | + $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 606 | + |
|
| 607 | + foreach ($arg as $v) { |
|
| 608 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 609 | + $res[] = $m[1]; |
|
| 610 | + } else { |
|
| 611 | + $res[] = $v; |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + return spip_pg_frommysql(join(',', $res)); |
|
| 616 | 616 | } |
| 617 | 617 | |
| 618 | 618 | // Conversion a l'arrach' des jointures MySQL en jointures PG |
@@ -621,56 +621,56 @@ discard block |
||
| 621 | 621 | |
| 622 | 622 | // https://code.spip.net/@spip_pg_groupby |
| 623 | 623 | function spip_pg_groupby($groupby, $from, $select) { |
| 624 | - $join = strpos($from, ','); |
|
| 625 | - // ismplifier avant de decouper |
|
| 626 | - if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 627 | - $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - if ($join or $groupby) { |
|
| 631 | - $join = is_array($select) ? $select : explode(', ', $select); |
|
| 632 | - } |
|
| 633 | - if ($join) { |
|
| 634 | - // enlever les 0 as points, '', ... |
|
| 635 | - foreach ($join as $k => $v) { |
|
| 636 | - $v = str_replace('DISTINCT ', '', $v); |
|
| 637 | - // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 638 | - $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 639 | - $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 640 | - // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 641 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 642 | - // idem sans AS (fetch numerique) |
|
| 643 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 644 | - // des AS simples : on garde le cote droit du AS |
|
| 645 | - $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 646 | - // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 647 | - if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 648 | - unset($join[$k]); |
|
| 649 | - } else { |
|
| 650 | - $join[$k] = trim($v); |
|
| 651 | - } |
|
| 652 | - } |
|
| 653 | - $join = array_diff($join, ['']); |
|
| 654 | - $join = implode(',', $join); |
|
| 655 | - } |
|
| 656 | - if (is_array($groupby)) { |
|
| 657 | - $groupby = join(',', $groupby); |
|
| 658 | - } |
|
| 659 | - if ($join) { |
|
| 660 | - $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 661 | - } |
|
| 662 | - if (!$groupby) { |
|
| 663 | - return ''; |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - $groupby = spip_pg_frommysql($groupby); |
|
| 667 | - // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 668 | - // issue de prepare_recherche |
|
| 669 | - $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 670 | - $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 671 | - $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 672 | - |
|
| 673 | - return "\nGROUP BY $groupby"; |
|
| 624 | + $join = strpos($from, ','); |
|
| 625 | + // ismplifier avant de decouper |
|
| 626 | + if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 627 | + $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + if ($join or $groupby) { |
|
| 631 | + $join = is_array($select) ? $select : explode(', ', $select); |
|
| 632 | + } |
|
| 633 | + if ($join) { |
|
| 634 | + // enlever les 0 as points, '', ... |
|
| 635 | + foreach ($join as $k => $v) { |
|
| 636 | + $v = str_replace('DISTINCT ', '', $v); |
|
| 637 | + // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 638 | + $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 639 | + $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 640 | + // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 641 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 642 | + // idem sans AS (fetch numerique) |
|
| 643 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 644 | + // des AS simples : on garde le cote droit du AS |
|
| 645 | + $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 646 | + // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 647 | + if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 648 | + unset($join[$k]); |
|
| 649 | + } else { |
|
| 650 | + $join[$k] = trim($v); |
|
| 651 | + } |
|
| 652 | + } |
|
| 653 | + $join = array_diff($join, ['']); |
|
| 654 | + $join = implode(',', $join); |
|
| 655 | + } |
|
| 656 | + if (is_array($groupby)) { |
|
| 657 | + $groupby = join(',', $groupby); |
|
| 658 | + } |
|
| 659 | + if ($join) { |
|
| 660 | + $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 661 | + } |
|
| 662 | + if (!$groupby) { |
|
| 663 | + return ''; |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + $groupby = spip_pg_frommysql($groupby); |
|
| 667 | + // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 668 | + // issue de prepare_recherche |
|
| 669 | + $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 670 | + $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 671 | + $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 672 | + |
|
| 673 | + return "\nGROUP BY $groupby"; |
|
| 674 | 674 | } |
| 675 | 675 | |
| 676 | 676 | // Conversion des operateurs MySQL en PG |
@@ -682,507 +682,507 @@ discard block |
||
| 682 | 682 | |
| 683 | 683 | // https://code.spip.net/@spip_pg_frommysql |
| 684 | 684 | function spip_pg_frommysql($arg) { |
| 685 | - if (is_array($arg)) { |
|
| 686 | - $arg = join(', ', $arg); |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - $res = spip_pg_fromfield($arg); |
|
| 690 | - |
|
| 691 | - $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 692 | - |
|
| 693 | - $res = preg_replace( |
|
| 694 | - '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 695 | - 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 696 | - $res |
|
| 697 | - ); |
|
| 698 | - $res = preg_replace( |
|
| 699 | - '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 700 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 701 | - $res |
|
| 702 | - ); |
|
| 703 | - $res = preg_replace( |
|
| 704 | - '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 705 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 706 | - $res |
|
| 707 | - ); |
|
| 708 | - |
|
| 709 | - $res = preg_replace( |
|
| 710 | - '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 711 | - ' EXTRACT(epoch FROM NOW())', |
|
| 712 | - $res |
|
| 713 | - ); |
|
| 714 | - |
|
| 715 | - // la fonction md5(integer) n'est pas connu en pg |
|
| 716 | - // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 717 | - $res = preg_replace( |
|
| 718 | - '/md5\s*[(]([^)]*)[)]/i', |
|
| 719 | - 'MD5(CAST(\1 AS text))', |
|
| 720 | - $res |
|
| 721 | - ); |
|
| 722 | - |
|
| 723 | - $res = preg_replace( |
|
| 724 | - '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 725 | - ' EXTRACT(epoch FROM \1)', |
|
| 726 | - $res |
|
| 727 | - ); |
|
| 728 | - |
|
| 729 | - $res = preg_replace( |
|
| 730 | - '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 731 | - ' EXTRACT(day FROM \1)', |
|
| 732 | - $res |
|
| 733 | - ); |
|
| 734 | - |
|
| 735 | - $res = preg_replace( |
|
| 736 | - '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 737 | - ' EXTRACT(month FROM \1)', |
|
| 738 | - $res |
|
| 739 | - ); |
|
| 740 | - |
|
| 741 | - $res = preg_replace( |
|
| 742 | - '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 743 | - ' EXTRACT(year FROM \1)', |
|
| 744 | - $res |
|
| 745 | - ); |
|
| 746 | - |
|
| 747 | - $res = preg_replace( |
|
| 748 | - '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 749 | - ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 750 | - $res |
|
| 751 | - ); |
|
| 752 | - |
|
| 753 | - $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 754 | - |
|
| 755 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 756 | - |
|
| 757 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 758 | - |
|
| 759 | - $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 760 | - $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 761 | - $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 762 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 763 | - $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 764 | - |
|
| 765 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 766 | - $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 767 | - |
|
| 768 | - $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 769 | - $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 685 | + if (is_array($arg)) { |
|
| 686 | + $arg = join(', ', $arg); |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + $res = spip_pg_fromfield($arg); |
|
| 690 | + |
|
| 691 | + $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 692 | + |
|
| 693 | + $res = preg_replace( |
|
| 694 | + '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 695 | + 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 696 | + $res |
|
| 697 | + ); |
|
| 698 | + $res = preg_replace( |
|
| 699 | + '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 700 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 701 | + $res |
|
| 702 | + ); |
|
| 703 | + $res = preg_replace( |
|
| 704 | + '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 705 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 706 | + $res |
|
| 707 | + ); |
|
| 708 | + |
|
| 709 | + $res = preg_replace( |
|
| 710 | + '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 711 | + ' EXTRACT(epoch FROM NOW())', |
|
| 712 | + $res |
|
| 713 | + ); |
|
| 714 | + |
|
| 715 | + // la fonction md5(integer) n'est pas connu en pg |
|
| 716 | + // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 717 | + $res = preg_replace( |
|
| 718 | + '/md5\s*[(]([^)]*)[)]/i', |
|
| 719 | + 'MD5(CAST(\1 AS text))', |
|
| 720 | + $res |
|
| 721 | + ); |
|
| 722 | + |
|
| 723 | + $res = preg_replace( |
|
| 724 | + '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 725 | + ' EXTRACT(epoch FROM \1)', |
|
| 726 | + $res |
|
| 727 | + ); |
|
| 728 | + |
|
| 729 | + $res = preg_replace( |
|
| 730 | + '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 731 | + ' EXTRACT(day FROM \1)', |
|
| 732 | + $res |
|
| 733 | + ); |
|
| 734 | + |
|
| 735 | + $res = preg_replace( |
|
| 736 | + '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 737 | + ' EXTRACT(month FROM \1)', |
|
| 738 | + $res |
|
| 739 | + ); |
|
| 740 | + |
|
| 741 | + $res = preg_replace( |
|
| 742 | + '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 743 | + ' EXTRACT(year FROM \1)', |
|
| 744 | + $res |
|
| 745 | + ); |
|
| 746 | + |
|
| 747 | + $res = preg_replace( |
|
| 748 | + '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 749 | + ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 750 | + $res |
|
| 751 | + ); |
|
| 752 | + |
|
| 753 | + $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 754 | + |
|
| 755 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 756 | + |
|
| 757 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 758 | + |
|
| 759 | + $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 760 | + $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 761 | + $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 762 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 763 | + $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 764 | + |
|
| 765 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 766 | + $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 767 | + |
|
| 768 | + $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 769 | + $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 770 | 770 | # correct en theorie mais produit des debordements arithmetiques |
| 771 | 771 | # $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)(timestamp *'[^']*' *[+-] *timestamp *'[^']*') *[)]/", '\2', $res); |
| 772 | - $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 773 | - $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 772 | + $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 773 | + $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 774 | 774 | |
| 775 | - return str_replace('REGEXP', '~', $res); |
|
| 775 | + return str_replace('REGEXP', '~', $res); |
|
| 776 | 776 | } |
| 777 | 777 | |
| 778 | 778 | // https://code.spip.net/@spip_pg_fromfield |
| 779 | 779 | function spip_pg_fromfield($arg) { |
| 780 | - while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 781 | - preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 782 | - $res = ''; |
|
| 783 | - $n = 0; |
|
| 784 | - $index = $m[2]; |
|
| 785 | - foreach ($r[1] as $v) { |
|
| 786 | - $n++; |
|
| 787 | - $res .= "\nwhen $index=$v then $n"; |
|
| 788 | - } |
|
| 789 | - $arg = $m[1] . "case $res else 0 end " |
|
| 790 | - . substr($arg, strlen($m[0])); |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - return $arg; |
|
| 780 | + while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 781 | + preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 782 | + $res = ''; |
|
| 783 | + $n = 0; |
|
| 784 | + $index = $m[2]; |
|
| 785 | + foreach ($r[1] as $v) { |
|
| 786 | + $n++; |
|
| 787 | + $res .= "\nwhen $index=$v then $n"; |
|
| 788 | + } |
|
| 789 | + $arg = $m[1] . "case $res else 0 end " |
|
| 790 | + . substr($arg, strlen($m[0])); |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + return $arg; |
|
| 794 | 794 | } |
| 795 | 795 | |
| 796 | 796 | // https://code.spip.net/@calculer_pg_where |
| 797 | 797 | function calculer_pg_where($v) { |
| 798 | - if (!is_array($v)) { |
|
| 799 | - return spip_pg_frommysql($v); |
|
| 800 | - } |
|
| 801 | - |
|
| 802 | - $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 803 | - if (!($n = count($v))) { |
|
| 804 | - return $op; |
|
| 805 | - } else { |
|
| 806 | - $arg = calculer_pg_where(array_shift($v)); |
|
| 807 | - if ($n == 1) { |
|
| 808 | - return "$op($arg)"; |
|
| 809 | - } else { |
|
| 810 | - $arg2 = calculer_pg_where(array_shift($v)); |
|
| 811 | - if ($n == 2) { |
|
| 812 | - return "($arg $op $arg2)"; |
|
| 813 | - } else { |
|
| 814 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 815 | - } |
|
| 816 | - } |
|
| 817 | - } |
|
| 798 | + if (!is_array($v)) { |
|
| 799 | + return spip_pg_frommysql($v); |
|
| 800 | + } |
|
| 801 | + |
|
| 802 | + $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 803 | + if (!($n = count($v))) { |
|
| 804 | + return $op; |
|
| 805 | + } else { |
|
| 806 | + $arg = calculer_pg_where(array_shift($v)); |
|
| 807 | + if ($n == 1) { |
|
| 808 | + return "$op($arg)"; |
|
| 809 | + } else { |
|
| 810 | + $arg2 = calculer_pg_where(array_shift($v)); |
|
| 811 | + if ($n == 2) { |
|
| 812 | + return "($arg $op $arg2)"; |
|
| 813 | + } else { |
|
| 814 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 815 | + } |
|
| 816 | + } |
|
| 817 | + } |
|
| 818 | 818 | } |
| 819 | 819 | |
| 820 | 820 | |
| 821 | 821 | // https://code.spip.net/@calculer_pg_expression |
| 822 | 822 | function calculer_pg_expression($expression, $v, $join = 'AND') { |
| 823 | - if (empty($v)) { |
|
| 824 | - return ''; |
|
| 825 | - } |
|
| 823 | + if (empty($v)) { |
|
| 824 | + return ''; |
|
| 825 | + } |
|
| 826 | 826 | |
| 827 | - $exp = "\n$expression "; |
|
| 827 | + $exp = "\n$expression "; |
|
| 828 | 828 | |
| 829 | - if (!is_array($v)) { |
|
| 830 | - $v = [$v]; |
|
| 831 | - } |
|
| 829 | + if (!is_array($v)) { |
|
| 830 | + $v = [$v]; |
|
| 831 | + } |
|
| 832 | 832 | |
| 833 | - if (strtoupper($join) === 'AND') { |
|
| 834 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 835 | - } else { |
|
| 836 | - return $exp . join($join, $v); |
|
| 837 | - } |
|
| 833 | + if (strtoupper($join) === 'AND') { |
|
| 834 | + return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 835 | + } else { |
|
| 836 | + return $exp . join($join, $v); |
|
| 837 | + } |
|
| 838 | 838 | } |
| 839 | 839 | |
| 840 | 840 | // https://code.spip.net/@spip_pg_select_as |
| 841 | 841 | function spip_pg_select_as($args) { |
| 842 | - $argsas = ''; |
|
| 843 | - foreach ($args as $k => $v) { |
|
| 844 | - if (substr($k, -1) == '@') { |
|
| 845 | - // c'est une jointure qui se refere au from precedent |
|
| 846 | - // pas de virgule |
|
| 847 | - $argsas .= ' ' . $v; |
|
| 848 | - } else { |
|
| 849 | - $as = ''; |
|
| 850 | - // spip_log("$k : $v", _LOG_DEBUG); |
|
| 851 | - if (!is_numeric($k)) { |
|
| 852 | - if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 853 | - $v = $k; |
|
| 854 | - } elseif ($v != $k) { |
|
| 855 | - $p = strpos($v, ' '); |
|
| 856 | - if ($p) { |
|
| 857 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 858 | - } else { |
|
| 859 | - $as = " AS $k"; |
|
| 860 | - } |
|
| 861 | - } |
|
| 862 | - } |
|
| 863 | - // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 864 | - // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 865 | - $argsas .= ', ' . $v . $as; |
|
| 866 | - } |
|
| 867 | - } |
|
| 868 | - |
|
| 869 | - return substr($argsas, 2); |
|
| 842 | + $argsas = ''; |
|
| 843 | + foreach ($args as $k => $v) { |
|
| 844 | + if (substr($k, -1) == '@') { |
|
| 845 | + // c'est une jointure qui se refere au from precedent |
|
| 846 | + // pas de virgule |
|
| 847 | + $argsas .= ' ' . $v; |
|
| 848 | + } else { |
|
| 849 | + $as = ''; |
|
| 850 | + // spip_log("$k : $v", _LOG_DEBUG); |
|
| 851 | + if (!is_numeric($k)) { |
|
| 852 | + if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 853 | + $v = $k; |
|
| 854 | + } elseif ($v != $k) { |
|
| 855 | + $p = strpos($v, ' '); |
|
| 856 | + if ($p) { |
|
| 857 | + $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 858 | + } else { |
|
| 859 | + $as = " AS $k"; |
|
| 860 | + } |
|
| 861 | + } |
|
| 862 | + } |
|
| 863 | + // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 864 | + // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 865 | + $argsas .= ', ' . $v . $as; |
|
| 866 | + } |
|
| 867 | + } |
|
| 868 | + |
|
| 869 | + return substr($argsas, 2); |
|
| 870 | 870 | } |
| 871 | 871 | |
| 872 | 872 | // https://code.spip.net/@spip_pg_fetch |
| 873 | 873 | function spip_pg_fetch($res, $t = '', $serveur = '', $requeter = true) { |
| 874 | 874 | |
| 875 | - if ($res) { |
|
| 876 | - $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 877 | - } |
|
| 875 | + if ($res) { |
|
| 876 | + $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 877 | + } |
|
| 878 | 878 | |
| 879 | - return $res; |
|
| 879 | + return $res; |
|
| 880 | 880 | } |
| 881 | 881 | |
| 882 | 882 | function spip_pg_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 883 | - if ($r) { |
|
| 884 | - return pg_result_seek($r, $row_number); |
|
| 885 | - } |
|
| 883 | + if ($r) { |
|
| 884 | + return pg_result_seek($r, $row_number); |
|
| 885 | + } |
|
| 886 | 886 | } |
| 887 | 887 | |
| 888 | 888 | |
| 889 | 889 | // https://code.spip.net/@spip_pg_countsel |
| 890 | 890 | function spip_pg_countsel( |
| 891 | - $from = [], |
|
| 892 | - $where = [], |
|
| 893 | - $groupby = [], |
|
| 894 | - $having = [], |
|
| 895 | - $serveur = '', |
|
| 896 | - $requeter = true |
|
| 891 | + $from = [], |
|
| 892 | + $where = [], |
|
| 893 | + $groupby = [], |
|
| 894 | + $having = [], |
|
| 895 | + $serveur = '', |
|
| 896 | + $requeter = true |
|
| 897 | 897 | ) { |
| 898 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 899 | - $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 900 | - if (!$requeter) { |
|
| 901 | - return $r; |
|
| 902 | - } |
|
| 903 | - if (!is_resource($r)) { |
|
| 904 | - return 0; |
|
| 905 | - } |
|
| 906 | - list($c) = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 907 | - |
|
| 908 | - return $c; |
|
| 898 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 899 | + $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 900 | + if (!$requeter) { |
|
| 901 | + return $r; |
|
| 902 | + } |
|
| 903 | + if (!is_resource($r)) { |
|
| 904 | + return 0; |
|
| 905 | + } |
|
| 906 | + list($c) = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 907 | + |
|
| 908 | + return $c; |
|
| 909 | 909 | } |
| 910 | 910 | |
| 911 | 911 | // https://code.spip.net/@spip_pg_count |
| 912 | 912 | function spip_pg_count($res, $serveur = '', $requeter = true) { |
| 913 | - return !$res ? 0 : pg_numrows($res); |
|
| 913 | + return !$res ? 0 : pg_numrows($res); |
|
| 914 | 914 | } |
| 915 | 915 | |
| 916 | 916 | // https://code.spip.net/@spip_pg_free |
| 917 | 917 | function spip_pg_free($res, $serveur = '', $requeter = true) { |
| 918 | - // rien a faire en postgres |
|
| 918 | + // rien a faire en postgres |
|
| 919 | 919 | } |
| 920 | 920 | |
| 921 | 921 | // https://code.spip.net/@spip_pg_delete |
| 922 | 922 | function spip_pg_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 923 | 923 | |
| 924 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 925 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 924 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 925 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 926 | 926 | |
| 927 | - $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 928 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 927 | + $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 928 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 929 | 929 | |
| 930 | - // renvoyer la requete inerte si demandee |
|
| 931 | - if (!$requeter) { |
|
| 932 | - return $query; |
|
| 933 | - } |
|
| 930 | + // renvoyer la requete inerte si demandee |
|
| 931 | + if (!$requeter) { |
|
| 932 | + return $query; |
|
| 933 | + } |
|
| 934 | 934 | |
| 935 | - $res = spip_pg_trace_query($query, $serveur); |
|
| 936 | - if ($res) { |
|
| 937 | - return pg_affected_rows($res); |
|
| 938 | - } else { |
|
| 939 | - return false; |
|
| 940 | - } |
|
| 935 | + $res = spip_pg_trace_query($query, $serveur); |
|
| 936 | + if ($res) { |
|
| 937 | + return pg_affected_rows($res); |
|
| 938 | + } else { |
|
| 939 | + return false; |
|
| 940 | + } |
|
| 941 | 941 | } |
| 942 | 942 | |
| 943 | 943 | // https://code.spip.net/@spip_pg_insert |
| 944 | 944 | function spip_pg_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 945 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 946 | - $prefixe = $connexion['prefixe']; |
|
| 947 | - $link = $connexion['link']; |
|
| 948 | - |
|
| 949 | - if (!$desc) { |
|
| 950 | - $desc = description_table($table, $serveur); |
|
| 951 | - } |
|
| 952 | - $seq = spip_pg_sequence($table, true); |
|
| 953 | - // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 954 | - if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 955 | - $seq = spip_pg_sequence($table); |
|
| 956 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 957 | - $seq = "currval('$seq')"; |
|
| 958 | - } |
|
| 959 | - |
|
| 960 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 961 | - $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 962 | - $ins = (strlen($champs) < 3) |
|
| 963 | - ? ' DEFAULT VALUES' |
|
| 964 | - : "$champs VALUES $valeurs"; |
|
| 965 | - $q = "INSERT INTO $table $ins $ret"; |
|
| 966 | - if (!$requeter) { |
|
| 967 | - return $q; |
|
| 968 | - } |
|
| 969 | - $connexion['last'] = $q; |
|
| 970 | - $r = spip_pg_query_simple($link, $q); |
|
| 945 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 946 | + $prefixe = $connexion['prefixe']; |
|
| 947 | + $link = $connexion['link']; |
|
| 948 | + |
|
| 949 | + if (!$desc) { |
|
| 950 | + $desc = description_table($table, $serveur); |
|
| 951 | + } |
|
| 952 | + $seq = spip_pg_sequence($table, true); |
|
| 953 | + // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 954 | + if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 955 | + $seq = spip_pg_sequence($table); |
|
| 956 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 957 | + $seq = "currval('$seq')"; |
|
| 958 | + } |
|
| 959 | + |
|
| 960 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 961 | + $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 962 | + $ins = (strlen($champs) < 3) |
|
| 963 | + ? ' DEFAULT VALUES' |
|
| 964 | + : "$champs VALUES $valeurs"; |
|
| 965 | + $q = "INSERT INTO $table $ins $ret"; |
|
| 966 | + if (!$requeter) { |
|
| 967 | + return $q; |
|
| 968 | + } |
|
| 969 | + $connexion['last'] = $q; |
|
| 970 | + $r = spip_pg_query_simple($link, $q); |
|
| 971 | 971 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 972 | - if ($r) { |
|
| 973 | - if (!$ret) { |
|
| 974 | - return 0; |
|
| 975 | - } |
|
| 976 | - if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 977 | - return $r2[0]; |
|
| 978 | - } |
|
| 979 | - } |
|
| 980 | - |
|
| 981 | - return false; |
|
| 972 | + if ($r) { |
|
| 973 | + if (!$ret) { |
|
| 974 | + return 0; |
|
| 975 | + } |
|
| 976 | + if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 977 | + return $r2[0]; |
|
| 978 | + } |
|
| 979 | + } |
|
| 980 | + |
|
| 981 | + return false; |
|
| 982 | 982 | } |
| 983 | 983 | |
| 984 | 984 | // https://code.spip.net/@spip_pg_insertq |
| 985 | 985 | function spip_pg_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 986 | 986 | |
| 987 | - if (!$desc) { |
|
| 988 | - $desc = description_table($table, $serveur); |
|
| 989 | - } |
|
| 990 | - if (!$desc) { |
|
| 991 | - die("$table insertion sans description"); |
|
| 992 | - } |
|
| 993 | - $fields = $desc['field']; |
|
| 994 | - |
|
| 995 | - foreach ($couples as $champ => $val) { |
|
| 996 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 997 | - } |
|
| 998 | - |
|
| 999 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1000 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1001 | - |
|
| 1002 | - return spip_pg_insert( |
|
| 1003 | - $table, |
|
| 1004 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 1005 | - '(' . join(',', $couples) . ')', |
|
| 1006 | - $desc, |
|
| 1007 | - $serveur, |
|
| 1008 | - $requeter |
|
| 1009 | - ); |
|
| 987 | + if (!$desc) { |
|
| 988 | + $desc = description_table($table, $serveur); |
|
| 989 | + } |
|
| 990 | + if (!$desc) { |
|
| 991 | + die("$table insertion sans description"); |
|
| 992 | + } |
|
| 993 | + $fields = $desc['field']; |
|
| 994 | + |
|
| 995 | + foreach ($couples as $champ => $val) { |
|
| 996 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 997 | + } |
|
| 998 | + |
|
| 999 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1000 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1001 | + |
|
| 1002 | + return spip_pg_insert( |
|
| 1003 | + $table, |
|
| 1004 | + '(' . join(',', array_keys($couples)) . ')', |
|
| 1005 | + '(' . join(',', $couples) . ')', |
|
| 1006 | + $desc, |
|
| 1007 | + $serveur, |
|
| 1008 | + $requeter |
|
| 1009 | + ); |
|
| 1010 | 1010 | } |
| 1011 | 1011 | |
| 1012 | 1012 | |
| 1013 | 1013 | // https://code.spip.net/@spip_pg_insertq_multi |
| 1014 | 1014 | function spip_pg_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1015 | 1015 | |
| 1016 | - if (!$desc) { |
|
| 1017 | - $desc = description_table($table, $serveur); |
|
| 1018 | - } |
|
| 1019 | - if (!$desc) { |
|
| 1020 | - die("$table insertion sans description"); |
|
| 1021 | - } |
|
| 1022 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1023 | - |
|
| 1024 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1025 | - // une premiere fois pour ajouter maj dans les cles |
|
| 1026 | - $c = isset($tab_couples[0]) ? $tab_couples[0] : []; |
|
| 1027 | - $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1028 | - |
|
| 1029 | - $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1030 | - $valeurs = []; |
|
| 1031 | - foreach ($tab_couples as $couples) { |
|
| 1032 | - foreach ($couples as $champ => $val) { |
|
| 1033 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1034 | - } |
|
| 1035 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1036 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1037 | - |
|
| 1038 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1039 | - } |
|
| 1040 | - $valeurs = implode(', ', $valeurs); |
|
| 1041 | - |
|
| 1042 | - return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1016 | + if (!$desc) { |
|
| 1017 | + $desc = description_table($table, $serveur); |
|
| 1018 | + } |
|
| 1019 | + if (!$desc) { |
|
| 1020 | + die("$table insertion sans description"); |
|
| 1021 | + } |
|
| 1022 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1023 | + |
|
| 1024 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1025 | + // une premiere fois pour ajouter maj dans les cles |
|
| 1026 | + $c = isset($tab_couples[0]) ? $tab_couples[0] : []; |
|
| 1027 | + $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1028 | + |
|
| 1029 | + $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1030 | + $valeurs = []; |
|
| 1031 | + foreach ($tab_couples as $couples) { |
|
| 1032 | + foreach ($couples as $champ => $val) { |
|
| 1033 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1034 | + } |
|
| 1035 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1036 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1037 | + |
|
| 1038 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1039 | + } |
|
| 1040 | + $valeurs = implode(', ', $valeurs); |
|
| 1041 | + |
|
| 1042 | + return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1043 | 1043 | } |
| 1044 | 1044 | |
| 1045 | 1045 | |
| 1046 | 1046 | // https://code.spip.net/@spip_pg_update |
| 1047 | 1047 | function spip_pg_update($table, $couples, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1048 | 1048 | |
| 1049 | - if (!$couples) { |
|
| 1050 | - return; |
|
| 1051 | - } |
|
| 1052 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1053 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1049 | + if (!$couples) { |
|
| 1050 | + return; |
|
| 1051 | + } |
|
| 1052 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1053 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1054 | 1054 | |
| 1055 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1056 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1055 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1056 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1057 | 1057 | |
| 1058 | - $set = []; |
|
| 1059 | - foreach ($couples as $champ => $val) { |
|
| 1060 | - $set[] = $champ . '=' . $val; |
|
| 1061 | - } |
|
| 1058 | + $set = []; |
|
| 1059 | + foreach ($couples as $champ => $val) { |
|
| 1060 | + $set[] = $champ . '=' . $val; |
|
| 1061 | + } |
|
| 1062 | 1062 | |
| 1063 | - $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1064 | - . calculer_pg_expression('SET', $set, ',') |
|
| 1065 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1063 | + $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1064 | + . calculer_pg_expression('SET', $set, ',') |
|
| 1065 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1066 | 1066 | |
| 1067 | - // renvoyer la requete inerte si demandee |
|
| 1068 | - if (!$requeter) { |
|
| 1069 | - return $query; |
|
| 1070 | - } |
|
| 1067 | + // renvoyer la requete inerte si demandee |
|
| 1068 | + if (!$requeter) { |
|
| 1069 | + return $query; |
|
| 1070 | + } |
|
| 1071 | 1071 | |
| 1072 | - return spip_pg_trace_query($query, $serveur); |
|
| 1072 | + return spip_pg_trace_query($query, $serveur); |
|
| 1073 | 1073 | } |
| 1074 | 1074 | |
| 1075 | 1075 | // idem, mais les valeurs sont des constantes a mettre entre apostrophes |
| 1076 | 1076 | // sauf les expressions de date lorsqu'il s'agit de fonctions SQL (NOW etc) |
| 1077 | 1077 | // https://code.spip.net/@spip_pg_updateq |
| 1078 | 1078 | function spip_pg_updateq($table, $couples, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1079 | - if (!$couples) { |
|
| 1080 | - return; |
|
| 1081 | - } |
|
| 1082 | - if (!$desc) { |
|
| 1083 | - $desc = description_table($table, $serveur); |
|
| 1084 | - } |
|
| 1085 | - $fields = $desc['field']; |
|
| 1086 | - foreach ($couples as $k => $val) { |
|
| 1087 | - $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1088 | - } |
|
| 1089 | - |
|
| 1090 | - return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1079 | + if (!$couples) { |
|
| 1080 | + return; |
|
| 1081 | + } |
|
| 1082 | + if (!$desc) { |
|
| 1083 | + $desc = description_table($table, $serveur); |
|
| 1084 | + } |
|
| 1085 | + $fields = $desc['field']; |
|
| 1086 | + foreach ($couples as $k => $val) { |
|
| 1087 | + $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1088 | + } |
|
| 1089 | + |
|
| 1090 | + return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1091 | 1091 | } |
| 1092 | 1092 | |
| 1093 | 1093 | |
| 1094 | 1094 | // https://code.spip.net/@spip_pg_replace |
| 1095 | 1095 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1096 | - if (!$values) { |
|
| 1097 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1098 | - |
|
| 1099 | - return 0; |
|
| 1100 | - } |
|
| 1101 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1102 | - $prefixe = $connexion['prefixe']; |
|
| 1103 | - $link = $connexion['link']; |
|
| 1104 | - |
|
| 1105 | - if (!$desc) { |
|
| 1106 | - $desc = description_table($table, $serveur); |
|
| 1107 | - } |
|
| 1108 | - if (!$desc) { |
|
| 1109 | - die("$table insertion sans description"); |
|
| 1110 | - } |
|
| 1111 | - $prim = $desc['key']['PRIMARY KEY']; |
|
| 1112 | - $ids = preg_split('/,\s*/', $prim); |
|
| 1113 | - $noprims = $prims = []; |
|
| 1114 | - foreach ($values as $k => $v) { |
|
| 1115 | - $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1116 | - |
|
| 1117 | - if (!in_array($k, $ids)) { |
|
| 1118 | - $noprims[$k] = "$k=$v"; |
|
| 1119 | - } else { |
|
| 1120 | - $prims[$k] = "$k=$v"; |
|
| 1121 | - } |
|
| 1122 | - } |
|
| 1123 | - |
|
| 1124 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1125 | - $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1126 | - |
|
| 1127 | - $where = join(' AND ', $prims); |
|
| 1128 | - if (!$where) { |
|
| 1129 | - return spip_pg_insert( |
|
| 1130 | - $table, |
|
| 1131 | - '(' . join(',', array_keys($values)) . ')', |
|
| 1132 | - '(' . join(',', $values) . ')', |
|
| 1133 | - $desc, |
|
| 1134 | - $serveur |
|
| 1135 | - ); |
|
| 1136 | - } |
|
| 1137 | - $couples = join(',', $noprims); |
|
| 1138 | - |
|
| 1139 | - $seq = spip_pg_sequence($table); |
|
| 1140 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 1141 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1142 | - |
|
| 1143 | - $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1144 | - if ($couples) { |
|
| 1145 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1096 | + if (!$values) { |
|
| 1097 | + spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1098 | + |
|
| 1099 | + return 0; |
|
| 1100 | + } |
|
| 1101 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1102 | + $prefixe = $connexion['prefixe']; |
|
| 1103 | + $link = $connexion['link']; |
|
| 1104 | + |
|
| 1105 | + if (!$desc) { |
|
| 1106 | + $desc = description_table($table, $serveur); |
|
| 1107 | + } |
|
| 1108 | + if (!$desc) { |
|
| 1109 | + die("$table insertion sans description"); |
|
| 1110 | + } |
|
| 1111 | + $prim = $desc['key']['PRIMARY KEY']; |
|
| 1112 | + $ids = preg_split('/,\s*/', $prim); |
|
| 1113 | + $noprims = $prims = []; |
|
| 1114 | + foreach ($values as $k => $v) { |
|
| 1115 | + $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1116 | + |
|
| 1117 | + if (!in_array($k, $ids)) { |
|
| 1118 | + $noprims[$k] = "$k=$v"; |
|
| 1119 | + } else { |
|
| 1120 | + $prims[$k] = "$k=$v"; |
|
| 1121 | + } |
|
| 1122 | + } |
|
| 1123 | + |
|
| 1124 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1125 | + $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1126 | + |
|
| 1127 | + $where = join(' AND ', $prims); |
|
| 1128 | + if (!$where) { |
|
| 1129 | + return spip_pg_insert( |
|
| 1130 | + $table, |
|
| 1131 | + '(' . join(',', array_keys($values)) . ')', |
|
| 1132 | + '(' . join(',', $values) . ')', |
|
| 1133 | + $desc, |
|
| 1134 | + $serveur |
|
| 1135 | + ); |
|
| 1136 | + } |
|
| 1137 | + $couples = join(',', $noprims); |
|
| 1138 | + |
|
| 1139 | + $seq = spip_pg_sequence($table); |
|
| 1140 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 1141 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1142 | + |
|
| 1143 | + $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1144 | + if ($couples) { |
|
| 1145 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1146 | 1146 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 1147 | - if (!$couples) { |
|
| 1148 | - return false; |
|
| 1149 | - } |
|
| 1150 | - $couples = pg_affected_rows($couples); |
|
| 1151 | - } |
|
| 1152 | - if (!$couples) { |
|
| 1153 | - $ret = !$seq ? '' : |
|
| 1154 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1155 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1156 | - ',', |
|
| 1157 | - $values |
|
| 1158 | - ) . ")$ret"; |
|
| 1159 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1160 | - if (!$couples) { |
|
| 1161 | - return false; |
|
| 1162 | - } elseif ($ret) { |
|
| 1163 | - $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1164 | - if ($r[0]) { |
|
| 1165 | - $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1166 | - // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1167 | - // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1168 | - $r = @pg_query($link, $q); |
|
| 1169 | - } |
|
| 1170 | - } |
|
| 1171 | - } |
|
| 1172 | - |
|
| 1173 | - return $couples; |
|
| 1147 | + if (!$couples) { |
|
| 1148 | + return false; |
|
| 1149 | + } |
|
| 1150 | + $couples = pg_affected_rows($couples); |
|
| 1151 | + } |
|
| 1152 | + if (!$couples) { |
|
| 1153 | + $ret = !$seq ? '' : |
|
| 1154 | + (" RETURNING nextval('$seq') < $prim"); |
|
| 1155 | + $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1156 | + ',', |
|
| 1157 | + $values |
|
| 1158 | + ) . ")$ret"; |
|
| 1159 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1160 | + if (!$couples) { |
|
| 1161 | + return false; |
|
| 1162 | + } elseif ($ret) { |
|
| 1163 | + $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1164 | + if ($r[0]) { |
|
| 1165 | + $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1166 | + // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1167 | + // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1168 | + $r = @pg_query($link, $q); |
|
| 1169 | + } |
|
| 1170 | + } |
|
| 1171 | + } |
|
| 1172 | + |
|
| 1173 | + return $couples; |
|
| 1174 | 1174 | } |
| 1175 | 1175 | |
| 1176 | 1176 | |
| 1177 | 1177 | // https://code.spip.net/@spip_pg_replace_multi |
| 1178 | 1178 | function spip_pg_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1179 | - // boucler pour traiter chaque requete independemment |
|
| 1180 | - foreach ($tab_couples as $couples) { |
|
| 1181 | - $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1182 | - } |
|
| 1179 | + // boucler pour traiter chaque requete independemment |
|
| 1180 | + foreach ($tab_couples as $couples) { |
|
| 1181 | + $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1182 | + } |
|
| 1183 | 1183 | |
| 1184 | - // renvoie le dernier id |
|
| 1185 | - return $retour; |
|
| 1184 | + // renvoie le dernier id |
|
| 1185 | + return $retour; |
|
| 1186 | 1186 | } |
| 1187 | 1187 | |
| 1188 | 1188 | |
@@ -1192,20 +1192,20 @@ discard block |
||
| 1192 | 1192 | // https://code.spip.net/@spip_pg_sequence |
| 1193 | 1193 | function spip_pg_sequence($table, $raw = false) { |
| 1194 | 1194 | |
| 1195 | - include_spip('base/serial'); |
|
| 1196 | - if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1197 | - return false; |
|
| 1198 | - } |
|
| 1199 | - $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1200 | - $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1201 | - if ( |
|
| 1202 | - !preg_match('/^\w+$/', $prim) |
|
| 1203 | - or strpos($desc['field'][$prim], 'int') === false |
|
| 1204 | - ) { |
|
| 1205 | - return ''; |
|
| 1206 | - } else { |
|
| 1207 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1208 | - } |
|
| 1195 | + include_spip('base/serial'); |
|
| 1196 | + if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1197 | + return false; |
|
| 1198 | + } |
|
| 1199 | + $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1200 | + $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1201 | + if ( |
|
| 1202 | + !preg_match('/^\w+$/', $prim) |
|
| 1203 | + or strpos($desc['field'][$prim], 'int') === false |
|
| 1204 | + ) { |
|
| 1205 | + return ''; |
|
| 1206 | + } else { |
|
| 1207 | + return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1208 | + } |
|
| 1209 | 1209 | } |
| 1210 | 1210 | |
| 1211 | 1211 | // Explicite les conversions de Mysql d'une valeur $v de type $t |
@@ -1213,65 +1213,65 @@ discard block |
||
| 1213 | 1213 | |
| 1214 | 1214 | // https://code.spip.net/@spip_pg_cite |
| 1215 | 1215 | function spip_pg_cite($v, $t) { |
| 1216 | - if (is_null($v)) { |
|
| 1217 | - return 'NULL'; |
|
| 1218 | - } // null php se traduit en NULL SQL |
|
| 1219 | - |
|
| 1220 | - if (sql_test_date($t)) { |
|
| 1221 | - if ($v and (strpos('0123456789', $v[0]) === false)) { |
|
| 1222 | - return spip_pg_frommysql($v); |
|
| 1223 | - } else { |
|
| 1224 | - if (strncmp($v, '0000', 4) == 0) { |
|
| 1225 | - $v = '0001' . substr($v, 4); |
|
| 1226 | - } |
|
| 1227 | - if (strpos($v, '-00-00') === 4) { |
|
| 1228 | - $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1229 | - } |
|
| 1230 | - |
|
| 1231 | - return "timestamp '$v'"; |
|
| 1232 | - } |
|
| 1233 | - } elseif (!sql_test_int($t)) { |
|
| 1234 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1235 | - } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1236 | - return $v; |
|
| 1237 | - } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1238 | - return substr($v, 1); |
|
| 1239 | - } else { |
|
| 1240 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1241 | - |
|
| 1242 | - return intval($v); |
|
| 1243 | - } |
|
| 1216 | + if (is_null($v)) { |
|
| 1217 | + return 'NULL'; |
|
| 1218 | + } // null php se traduit en NULL SQL |
|
| 1219 | + |
|
| 1220 | + if (sql_test_date($t)) { |
|
| 1221 | + if ($v and (strpos('0123456789', $v[0]) === false)) { |
|
| 1222 | + return spip_pg_frommysql($v); |
|
| 1223 | + } else { |
|
| 1224 | + if (strncmp($v, '0000', 4) == 0) { |
|
| 1225 | + $v = '0001' . substr($v, 4); |
|
| 1226 | + } |
|
| 1227 | + if (strpos($v, '-00-00') === 4) { |
|
| 1228 | + $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1229 | + } |
|
| 1230 | + |
|
| 1231 | + return "timestamp '$v'"; |
|
| 1232 | + } |
|
| 1233 | + } elseif (!sql_test_int($t)) { |
|
| 1234 | + return ("'" . pg_escape_string($v) . "'"); |
|
| 1235 | + } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1236 | + return $v; |
|
| 1237 | + } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1238 | + return substr($v, 1); |
|
| 1239 | + } else { |
|
| 1240 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1241 | + |
|
| 1242 | + return intval($v); |
|
| 1243 | + } |
|
| 1244 | 1244 | } |
| 1245 | 1245 | |
| 1246 | 1246 | // https://code.spip.net/@spip_pg_hex |
| 1247 | 1247 | function spip_pg_hex($v) { |
| 1248 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1248 | + return "CAST(x'" . $v . "' as bigint)"; |
|
| 1249 | 1249 | } |
| 1250 | 1250 | |
| 1251 | 1251 | function spip_pg_quote($v, $type = '') { |
| 1252 | - if (!is_array($v)) { |
|
| 1253 | - return spip_pg_cite($v, $type); |
|
| 1254 | - } |
|
| 1255 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1256 | - foreach ($v as $k => $r) { |
|
| 1257 | - $v[$k] = spip_pg_quote($r, $type); |
|
| 1258 | - } |
|
| 1259 | - |
|
| 1260 | - return join(',', $v); |
|
| 1252 | + if (!is_array($v)) { |
|
| 1253 | + return spip_pg_cite($v, $type); |
|
| 1254 | + } |
|
| 1255 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1256 | + foreach ($v as $k => $r) { |
|
| 1257 | + $v[$k] = spip_pg_quote($r, $type); |
|
| 1258 | + } |
|
| 1259 | + |
|
| 1260 | + return join(',', $v); |
|
| 1261 | 1261 | } |
| 1262 | 1262 | |
| 1263 | 1263 | function spip_pg_date_proche($champ, $interval, $unite) { |
| 1264 | - return '(' |
|
| 1265 | - . $champ |
|
| 1266 | - . (($interval <= 0) ? '>' : '<') |
|
| 1267 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1268 | - . '(' |
|
| 1269 | - . sql_quote(date('Y-m-d H:i:s')) |
|
| 1270 | - . ', INTERVAL ' |
|
| 1271 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1272 | - . ' ' |
|
| 1273 | - . $unite |
|
| 1274 | - . '))'; |
|
| 1264 | + return '(' |
|
| 1265 | + . $champ |
|
| 1266 | + . (($interval <= 0) ? '>' : '<') |
|
| 1267 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1268 | + . '(' |
|
| 1269 | + . sql_quote(date('Y-m-d H:i:s')) |
|
| 1270 | + . ', INTERVAL ' |
|
| 1271 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1272 | + . ' ' |
|
| 1273 | + . $unite |
|
| 1274 | + . '))'; |
|
| 1275 | 1275 | } |
| 1276 | 1276 | |
| 1277 | 1277 | // https://code.spip.net/@spip_pg_in |
@@ -1279,69 +1279,69 @@ discard block |
||
| 1279 | 1279 | // |
| 1280 | 1280 | // IN (...) souvent limite a 255 elements, d'ou cette fonction assistante |
| 1281 | 1281 | // |
| 1282 | - // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1283 | - if (!$valeurs) { |
|
| 1284 | - return $not ? '0=0' : '0=1'; |
|
| 1285 | - } |
|
| 1286 | - if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1287 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1288 | - } |
|
| 1289 | - $n = $i = 0; |
|
| 1290 | - $in_sql = ''; |
|
| 1291 | - while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1292 | - if ((++$i) >= 255) { |
|
| 1293 | - $in_sql .= "($val $not IN (" . |
|
| 1294 | - substr($valeurs, 0, $n) . |
|
| 1295 | - "))\n" . |
|
| 1296 | - ($not ? "AND\t" : "OR\t"); |
|
| 1297 | - $valeurs = substr($valeurs, $n + 1); |
|
| 1298 | - $i = $n = 0; |
|
| 1299 | - } |
|
| 1300 | - } |
|
| 1301 | - $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1302 | - |
|
| 1303 | - return "($in_sql)"; |
|
| 1282 | + // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1283 | + if (!$valeurs) { |
|
| 1284 | + return $not ? '0=0' : '0=1'; |
|
| 1285 | + } |
|
| 1286 | + if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1287 | + return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1288 | + } |
|
| 1289 | + $n = $i = 0; |
|
| 1290 | + $in_sql = ''; |
|
| 1291 | + while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1292 | + if ((++$i) >= 255) { |
|
| 1293 | + $in_sql .= "($val $not IN (" . |
|
| 1294 | + substr($valeurs, 0, $n) . |
|
| 1295 | + "))\n" . |
|
| 1296 | + ($not ? "AND\t" : "OR\t"); |
|
| 1297 | + $valeurs = substr($valeurs, $n + 1); |
|
| 1298 | + $i = $n = 0; |
|
| 1299 | + } |
|
| 1300 | + } |
|
| 1301 | + $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1302 | + |
|
| 1303 | + return "($in_sql)"; |
|
| 1304 | 1304 | } |
| 1305 | 1305 | |
| 1306 | 1306 | // https://code.spip.net/@spip_pg_error |
| 1307 | 1307 | function spip_pg_error($query = '', $serveur = '', $requeter = true) { |
| 1308 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1309 | - $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1310 | - if ($s) { |
|
| 1311 | - $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1312 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1313 | - } |
|
| 1314 | - |
|
| 1315 | - return $s; |
|
| 1308 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1309 | + $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1310 | + if ($s) { |
|
| 1311 | + $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1312 | + spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1313 | + } |
|
| 1314 | + |
|
| 1315 | + return $s; |
|
| 1316 | 1316 | } |
| 1317 | 1317 | |
| 1318 | 1318 | // https://code.spip.net/@spip_pg_errno |
| 1319 | 1319 | function spip_pg_errno($serveur = '') { |
| 1320 | - // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1321 | - // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1322 | - return 0; |
|
| 1320 | + // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1321 | + // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1322 | + return 0; |
|
| 1323 | 1323 | } |
| 1324 | 1324 | |
| 1325 | 1325 | // https://code.spip.net/@spip_pg_drop_table |
| 1326 | 1326 | function spip_pg_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 1327 | - if ($exist) { |
|
| 1328 | - $exist = ' IF EXISTS'; |
|
| 1329 | - } |
|
| 1330 | - if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1331 | - return true; |
|
| 1332 | - } else { |
|
| 1333 | - return false; |
|
| 1334 | - } |
|
| 1327 | + if ($exist) { |
|
| 1328 | + $exist = ' IF EXISTS'; |
|
| 1329 | + } |
|
| 1330 | + if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1331 | + return true; |
|
| 1332 | + } else { |
|
| 1333 | + return false; |
|
| 1334 | + } |
|
| 1335 | 1335 | } |
| 1336 | 1336 | |
| 1337 | 1337 | // supprime une vue |
| 1338 | 1338 | // https://code.spip.net/@spip_pg_drop_view |
| 1339 | 1339 | function spip_pg_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 1340 | - if ($exist) { |
|
| 1341 | - $exist = ' IF EXISTS'; |
|
| 1342 | - } |
|
| 1340 | + if ($exist) { |
|
| 1341 | + $exist = ' IF EXISTS'; |
|
| 1342 | + } |
|
| 1343 | 1343 | |
| 1344 | - return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1344 | + return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1345 | 1345 | } |
| 1346 | 1346 | |
| 1347 | 1347 | /** |
@@ -1358,41 +1358,41 @@ discard block |
||
| 1358 | 1358 | * Ressource à utiliser avec sql_fetch() |
| 1359 | 1359 | **/ |
| 1360 | 1360 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1361 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1362 | - $link = $connexion['link']; |
|
| 1363 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1361 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1362 | + $link = $connexion['link']; |
|
| 1363 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1364 | 1364 | |
| 1365 | - return spip_pg_query_simple($link, $q); |
|
| 1365 | + return spip_pg_query_simple($link, $q); |
|
| 1366 | 1366 | } |
| 1367 | 1367 | |
| 1368 | 1368 | // https://code.spip.net/@spip_pg_showtable |
| 1369 | 1369 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1370 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1371 | - $link = $connexion['link']; |
|
| 1372 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1373 | - |
|
| 1374 | - $res = spip_pg_query_simple($link, $q); |
|
| 1375 | - if (!$res) { |
|
| 1376 | - return false; |
|
| 1377 | - } |
|
| 1378 | - |
|
| 1379 | - // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1380 | - // il faut en tenir compte dans le return |
|
| 1381 | - $fields = []; |
|
| 1382 | - while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1383 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1384 | - } |
|
| 1385 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1386 | - $res = spip_pg_query_simple($link, $q); |
|
| 1387 | - $keys = []; |
|
| 1388 | - while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1389 | - if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1390 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1391 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1392 | - } |
|
| 1393 | - } |
|
| 1394 | - |
|
| 1395 | - return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1370 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1371 | + $link = $connexion['link']; |
|
| 1372 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1373 | + |
|
| 1374 | + $res = spip_pg_query_simple($link, $q); |
|
| 1375 | + if (!$res) { |
|
| 1376 | + return false; |
|
| 1377 | + } |
|
| 1378 | + |
|
| 1379 | + // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1380 | + // il faut en tenir compte dans le return |
|
| 1381 | + $fields = []; |
|
| 1382 | + while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1383 | + $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1384 | + } |
|
| 1385 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1386 | + $res = spip_pg_query_simple($link, $q); |
|
| 1387 | + $keys = []; |
|
| 1388 | + while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1389 | + if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1390 | + $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1391 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1392 | + } |
|
| 1393 | + } |
|
| 1394 | + |
|
| 1395 | + return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1396 | 1396 | } |
| 1397 | 1397 | |
| 1398 | 1398 | // Fonction de creation d'une table SQL nommee $nom |
@@ -1404,118 +1404,118 @@ discard block |
||
| 1404 | 1404 | // https://code.spip.net/@spip_pg_create |
| 1405 | 1405 | function spip_pg_create($nom, $champs, $cles, $autoinc = false, $temporary = false, $serveur = '', $requeter = true) { |
| 1406 | 1406 | |
| 1407 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1408 | - $link = $connexion['link']; |
|
| 1409 | - $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1410 | - |
|
| 1411 | - $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1412 | - $keys = []; |
|
| 1413 | - |
|
| 1414 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1415 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1416 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 1417 | - return; |
|
| 1418 | - } |
|
| 1419 | - |
|
| 1420 | - foreach ($cles as $k => $v) { |
|
| 1421 | - if (strpos($k, 'KEY ') === 0) { |
|
| 1422 | - $n = str_replace('`', '', $k); |
|
| 1423 | - $v = str_replace('`', '"', $v); |
|
| 1424 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1425 | - if ($k != $n) { |
|
| 1426 | - $i = "\"$i\""; |
|
| 1427 | - } |
|
| 1428 | - $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1429 | - } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1430 | - $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1431 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1432 | - } else { |
|
| 1433 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1434 | - } |
|
| 1435 | - if ($k == 'PRIMARY KEY') { |
|
| 1436 | - $prim_name = $v; |
|
| 1437 | - } |
|
| 1438 | - $s = ','; |
|
| 1439 | - } |
|
| 1440 | - $s = ''; |
|
| 1441 | - |
|
| 1442 | - $character_set = ''; |
|
| 1443 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1444 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1445 | - } |
|
| 1446 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1447 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1448 | - } |
|
| 1449 | - |
|
| 1450 | - foreach ($champs as $k => $v) { |
|
| 1451 | - $k = str_replace('`', '"', $k); |
|
| 1452 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1453 | - if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1454 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1455 | - } |
|
| 1456 | - } |
|
| 1457 | - |
|
| 1458 | - $query .= "$s\n\t\t$k " |
|
| 1459 | - . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1460 | - ? ' bigserial' |
|
| 1461 | - : mysql2pg_type($v) |
|
| 1462 | - ); |
|
| 1463 | - $s = ','; |
|
| 1464 | - } |
|
| 1465 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1466 | - |
|
| 1467 | - // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1468 | - |
|
| 1469 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1470 | - ($character_set ? " DEFAULT $character_set" : '') |
|
| 1471 | - . "\n"; |
|
| 1472 | - |
|
| 1473 | - if (!$requeter) { |
|
| 1474 | - return $q; |
|
| 1475 | - } |
|
| 1476 | - $connexion['last'] = $q; |
|
| 1477 | - $r = @pg_query($link, $q); |
|
| 1478 | - |
|
| 1479 | - if (!$r) { |
|
| 1480 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1481 | - } else { |
|
| 1482 | - foreach ($keys as $index) { |
|
| 1483 | - pg_query($link, $index); |
|
| 1484 | - } |
|
| 1485 | - } |
|
| 1486 | - |
|
| 1487 | - return $r; |
|
| 1407 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1408 | + $link = $connexion['link']; |
|
| 1409 | + $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1410 | + |
|
| 1411 | + $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1412 | + $keys = []; |
|
| 1413 | + |
|
| 1414 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1415 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1416 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 1417 | + return; |
|
| 1418 | + } |
|
| 1419 | + |
|
| 1420 | + foreach ($cles as $k => $v) { |
|
| 1421 | + if (strpos($k, 'KEY ') === 0) { |
|
| 1422 | + $n = str_replace('`', '', $k); |
|
| 1423 | + $v = str_replace('`', '"', $v); |
|
| 1424 | + $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1425 | + if ($k != $n) { |
|
| 1426 | + $i = "\"$i\""; |
|
| 1427 | + } |
|
| 1428 | + $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1429 | + } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1430 | + $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1431 | + $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1432 | + } else { |
|
| 1433 | + $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1434 | + } |
|
| 1435 | + if ($k == 'PRIMARY KEY') { |
|
| 1436 | + $prim_name = $v; |
|
| 1437 | + } |
|
| 1438 | + $s = ','; |
|
| 1439 | + } |
|
| 1440 | + $s = ''; |
|
| 1441 | + |
|
| 1442 | + $character_set = ''; |
|
| 1443 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1444 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1445 | + } |
|
| 1446 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1447 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1448 | + } |
|
| 1449 | + |
|
| 1450 | + foreach ($champs as $k => $v) { |
|
| 1451 | + $k = str_replace('`', '"', $k); |
|
| 1452 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1453 | + if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1454 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1455 | + } |
|
| 1456 | + } |
|
| 1457 | + |
|
| 1458 | + $query .= "$s\n\t\t$k " |
|
| 1459 | + . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1460 | + ? ' bigserial' |
|
| 1461 | + : mysql2pg_type($v) |
|
| 1462 | + ); |
|
| 1463 | + $s = ','; |
|
| 1464 | + } |
|
| 1465 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1466 | + |
|
| 1467 | + // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1468 | + |
|
| 1469 | + $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1470 | + ($character_set ? " DEFAULT $character_set" : '') |
|
| 1471 | + . "\n"; |
|
| 1472 | + |
|
| 1473 | + if (!$requeter) { |
|
| 1474 | + return $q; |
|
| 1475 | + } |
|
| 1476 | + $connexion['last'] = $q; |
|
| 1477 | + $r = @pg_query($link, $q); |
|
| 1478 | + |
|
| 1479 | + if (!$r) { |
|
| 1480 | + spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1481 | + } else { |
|
| 1482 | + foreach ($keys as $index) { |
|
| 1483 | + pg_query($link, $index); |
|
| 1484 | + } |
|
| 1485 | + } |
|
| 1486 | + |
|
| 1487 | + return $r; |
|
| 1488 | 1488 | } |
| 1489 | 1489 | |
| 1490 | 1490 | |
| 1491 | 1491 | function spip_pg_create_base($nom, $serveur = '', $requeter = true) { |
| 1492 | - return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1492 | + return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1493 | 1493 | } |
| 1494 | 1494 | |
| 1495 | 1495 | // Fonction de creation d'une vue SQL nommee $nom |
| 1496 | 1496 | // https://code.spip.net/@spip_pg_create_view |
| 1497 | 1497 | function spip_pg_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 1498 | - if (!$query_select) { |
|
| 1499 | - return false; |
|
| 1500 | - } |
|
| 1501 | - // vue deja presente |
|
| 1502 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 1503 | - if ($requeter) { |
|
| 1504 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1505 | - } |
|
| 1498 | + if (!$query_select) { |
|
| 1499 | + return false; |
|
| 1500 | + } |
|
| 1501 | + // vue deja presente |
|
| 1502 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 1503 | + if ($requeter) { |
|
| 1504 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1505 | + } |
|
| 1506 | 1506 | |
| 1507 | - return false; |
|
| 1508 | - } |
|
| 1507 | + return false; |
|
| 1508 | + } |
|
| 1509 | 1509 | |
| 1510 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1510 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1511 | 1511 | |
| 1512 | - return spip_pg_query($query, $serveur, $requeter); |
|
| 1512 | + return spip_pg_query($query, $serveur, $requeter); |
|
| 1513 | 1513 | } |
| 1514 | 1514 | |
| 1515 | 1515 | |
| 1516 | 1516 | // https://code.spip.net/@spip_pg_set_connect_charset |
| 1517 | 1517 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1518 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1518 | + spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1519 | 1519 | } |
| 1520 | 1520 | |
| 1521 | 1521 | |
@@ -1529,7 +1529,7 @@ discard block |
||
| 1529 | 1529 | **/ |
| 1530 | 1530 | // https://code.spip.net/@spip_sqlite_optimize |
| 1531 | 1531 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1532 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1532 | + return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1533 | 1533 | } |
| 1534 | 1534 | |
| 1535 | 1535 | // Selectionner la sous-chaine dans $objet |
@@ -1537,13 +1537,13 @@ discard block |
||
| 1537 | 1537 | |
| 1538 | 1538 | // https://code.spip.net/@spip_pg_multi |
| 1539 | 1539 | function spip_pg_multi($objet, $lang) { |
| 1540 | - $r = 'regexp_replace(' |
|
| 1541 | - . $objet |
|
| 1542 | - . ",'<multi>.*[[]" |
|
| 1543 | - . $lang |
|
| 1544 | - . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1540 | + $r = 'regexp_replace(' |
|
| 1541 | + . $objet |
|
| 1542 | + . ",'<multi>.*[[]" |
|
| 1543 | + . $lang |
|
| 1544 | + . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1545 | 1545 | |
| 1546 | - return $r; |
|
| 1546 | + return $r; |
|
| 1547 | 1547 | } |
| 1548 | 1548 | |
| 1549 | 1549 | // Palanquee d'idiosyncrasies MySQL dans les creations de table |
@@ -1551,31 +1551,31 @@ discard block |
||
| 1551 | 1551 | |
| 1552 | 1552 | // https://code.spip.net/@mysql2pg_type |
| 1553 | 1553 | function mysql2pg_type($v) { |
| 1554 | - $remplace = [ |
|
| 1555 | - '/auto_increment/i' => '', // non reconnu |
|
| 1556 | - '/bigint/i' => 'bigint', |
|
| 1557 | - '/mediumint/i' => 'mediumint', |
|
| 1558 | - '/smallint/i' => 'smallint', |
|
| 1559 | - '/tinyint/i' => 'int', |
|
| 1560 | - '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1561 | - '/longtext/i' => 'text', |
|
| 1562 | - '/mediumtext/i' => 'text', |
|
| 1563 | - '/tinytext/i' => 'text', |
|
| 1564 | - '/longblob/i' => 'text', |
|
| 1565 | - '/0000-00-00/' => '0001-01-01', |
|
| 1566 | - '/datetime/i' => 'timestamp', |
|
| 1567 | - '/unsigned/i' => '', |
|
| 1568 | - '/double/i' => 'double precision', |
|
| 1569 | - '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1570 | - '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1571 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1572 | - ]; |
|
| 1573 | - |
|
| 1574 | - return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1554 | + $remplace = [ |
|
| 1555 | + '/auto_increment/i' => '', // non reconnu |
|
| 1556 | + '/bigint/i' => 'bigint', |
|
| 1557 | + '/mediumint/i' => 'mediumint', |
|
| 1558 | + '/smallint/i' => 'smallint', |
|
| 1559 | + '/tinyint/i' => 'int', |
|
| 1560 | + '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1561 | + '/longtext/i' => 'text', |
|
| 1562 | + '/mediumtext/i' => 'text', |
|
| 1563 | + '/tinytext/i' => 'text', |
|
| 1564 | + '/longblob/i' => 'text', |
|
| 1565 | + '/0000-00-00/' => '0001-01-01', |
|
| 1566 | + '/datetime/i' => 'timestamp', |
|
| 1567 | + '/unsigned/i' => '', |
|
| 1568 | + '/double/i' => 'double precision', |
|
| 1569 | + '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1570 | + '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1571 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1572 | + ]; |
|
| 1573 | + |
|
| 1574 | + return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1575 | 1575 | } |
| 1576 | 1576 | |
| 1577 | 1577 | // Renvoie false si on n'a pas les fonctions pg (pour l'install) |
| 1578 | 1578 | // https://code.spip.net/@spip_versions_pg |
| 1579 | 1579 | function spip_versions_pg() { |
| 1580 | - return function_exists('pg_connect'); |
|
| 1580 | + return function_exists('pg_connect'); |
|
| 1581 | 1581 | } |
@@ -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; |
@@ -83,8 +83,8 @@ discard block |
||
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | spip_log( |
| 86 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 87 | - 'pg.' . _LOG_DEBUG |
|
| 86 | + "Connexion vers $host, base $db, prefixe $prefixe ".($link ? 'operationnelle' : 'impossible'), |
|
| 87 | + 'pg.'._LOG_DEBUG |
|
| 88 | 88 | ); |
| 89 | 89 | |
| 90 | 90 | return !$link ? false : [ |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | } else { |
| 175 | 175 | $suite = ''; |
| 176 | 176 | } |
| 177 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 177 | + $query = preg_replace('/([,\s])spip_/', '\1'.$prefixe.'_', $query).$suite; |
|
| 178 | 178 | |
| 179 | 179 | // renvoyer la requete inerte si demandee |
| 180 | 180 | if (!$requeter) { |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | // ou revoir l'api de sql_alter en creant un |
| 244 | 244 | // sql_alter_table($table,array($actions)); |
| 245 | 245 | if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
| 246 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 246 | + spip_log("$query mal comprise", 'pg.'._LOG_ERREUR); |
|
| 247 | 247 | |
| 248 | 248 | return false; |
| 249 | 249 | } |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | $i = 0; |
| 257 | 257 | $ouverte = false; |
| 258 | 258 | while ($do = array_shift($todo)) { |
| 259 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 259 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i].','.$do : $do; |
|
| 260 | 260 | $o = (false !== strpos($do, '(')); |
| 261 | 261 | $f = (false !== strpos($do, ')')); |
| 262 | 262 | if ($o and !$f) { |
@@ -269,34 +269,34 @@ discard block |
||
| 269 | 269 | } |
| 270 | 270 | } |
| 271 | 271 | $todo = $todo2; |
| 272 | - $query = $debut . ' ' . array_shift($todo); |
|
| 272 | + $query = $debut.' '.array_shift($todo); |
|
| 273 | 273 | |
| 274 | 274 | if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
| 275 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 275 | + spip_log("$query incompris", 'pg.'._LOG_ERREUR); |
|
| 276 | 276 | } else { |
| 277 | 277 | if ($r[1]) { |
| 278 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 278 | + spip_log("j'ignore IGNORE dans $query", 'pg.'._LOG_AVERTISSEMENT); |
|
| 279 | 279 | } |
| 280 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 280 | + $f = 'spip_pg_alter_'.strtolower($r[3]); |
|
| 281 | 281 | if (function_exists($f)) { |
| 282 | 282 | $f($r[2], $r[4], $serveur, $requeter); |
| 283 | 283 | } else { |
| 284 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 284 | + spip_log("$query non prevu", 'pg.'._LOG_ERREUR); |
|
| 285 | 285 | } |
| 286 | 286 | } |
| 287 | 287 | // Alter a plusieurs args. Faudrait optimiser. |
| 288 | 288 | if ($todo) { |
| 289 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 289 | + spip_pg_alter("TABLE $table ".join(',', $todo)); |
|
| 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 | 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 = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 299 | + $actions = ["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,14 +330,14 @@ 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 | 339 | return spip_pg_query( |
| 340 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 340 | + "ALTER TABLE $table ADD CONSTRAINT $table".'_pkey PRIMARY KEY ('.$m.')', |
|
| 341 | 341 | $serveur, |
| 342 | 342 | $requeter |
| 343 | 343 | ); |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | $colonnes = substr($m[1], 1, -1); |
| 357 | 357 | if (false !== strpos(',', $colonnes)) { |
| 358 | 358 | spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
| 359 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 359 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.'._LOG_ERREUR); |
|
| 360 | 360 | } else { |
| 361 | 361 | $nom_index = $colonnes; |
| 362 | 362 | } |
@@ -373,23 +373,23 @@ discard block |
||
| 373 | 373 | // https://code.spip.net/@spip_pg_alter_drop |
| 374 | 374 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 375 | 375 | if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
| 376 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 376 | + spip_log("alter drop: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 377 | 377 | } else { |
| 378 | 378 | if (!$r[1] or $r[1] == 'COLUMN') { |
| 379 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 379 | + return spip_pg_query("ALTER TABLE $table DROP ".$r[2], $serveur); |
|
| 380 | 380 | } elseif ($r[1][0] == 'P') { |
| 381 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 381 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table".'_pkey', $serveur); |
|
| 382 | 382 | } else { |
| 383 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 383 | + return spip_pg_query('DROP INDEX '.$table.'_'.$r[2], $serveur); |
|
| 384 | 384 | } |
| 385 | 385 | } |
| 386 | 386 | } |
| 387 | 387 | |
| 388 | 388 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 389 | 389 | if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
| 390 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 390 | + spip_log("alter modify: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 391 | 391 | } else { |
| 392 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 392 | + return spip_pg_alter_change($table, $r[1].' '.$arg, $serveur = '', $requeter = true); |
|
| 393 | 393 | } |
| 394 | 394 | } |
| 395 | 395 | |
@@ -405,7 +405,7 @@ discard block |
||
| 405 | 405 | } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
| 406 | 406 | $rename = $r[2]; |
| 407 | 407 | } else { |
| 408 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 408 | + spip_log("alter rename: $arg incompris", 'pg.'._LOG_ERREUR); |
|
| 409 | 409 | } |
| 410 | 410 | |
| 411 | 411 | return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
@@ -426,8 +426,8 @@ discard block |
||
| 426 | 426 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 427 | 427 | if (!($nom or $table or $champs)) { |
| 428 | 428 | spip_log( |
| 429 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 430 | - 'pg.' . _LOG_ERREUR |
|
| 429 | + "Champ manquant pour creer un index pg ($nom, $table, (".@join(',', $champs).'))', |
|
| 430 | + 'pg.'._LOG_ERREUR |
|
| 431 | 431 | ); |
| 432 | 432 | |
| 433 | 433 | return false; |
@@ -438,7 +438,7 @@ discard block |
||
| 438 | 438 | |
| 439 | 439 | // PG ne differentie pas noms des index en fonction des tables |
| 440 | 440 | // il faut donc creer des noms uniques d'index pour une base pg |
| 441 | - $nom = $table . '_' . $nom; |
|
| 441 | + $nom = $table.'_'.$nom; |
|
| 442 | 442 | // enlever d'eventuelles parentheses deja presentes sur champs |
| 443 | 443 | if (!is_array($champs)) { |
| 444 | 444 | if ($champs[0] == '(') { |
@@ -446,7 +446,7 @@ discard block |
||
| 446 | 446 | } |
| 447 | 447 | $champs = [$champs]; |
| 448 | 448 | } |
| 449 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 449 | + $query = "CREATE INDEX $nom ON $table (".join(',', $champs).')'; |
|
| 450 | 450 | if (!$requeter) { |
| 451 | 451 | return $query; |
| 452 | 452 | } |
@@ -470,7 +470,7 @@ discard block |
||
| 470 | 470 | } else { |
| 471 | 471 | $suite = ''; |
| 472 | 472 | } |
| 473 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 473 | + $query = 'EXPLAIN '.preg_replace('/([,\s])spip_/', '\1'.$prefixe.'_', $query).$suite; |
|
| 474 | 474 | |
| 475 | 475 | if (!$requeter) { |
| 476 | 476 | return $query; |
@@ -565,16 +565,16 @@ discard block |
||
| 565 | 565 | } |
| 566 | 566 | } |
| 567 | 567 | $from = spip_pg_from($from, $prefixe); |
| 568 | - $query = 'SELECT ' . $select |
|
| 568 | + $query = 'SELECT '.$select |
|
| 569 | 569 | . (!$from ? '' : "\nFROM $from") |
| 570 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 570 | + . (!$where ? '' : ("\nWHERE ".(!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 571 | 571 | "\n\tAND ", |
| 572 | 572 | array_map('calculer_pg_where', $where) |
| 573 | 573 | ))))) |
| 574 | 574 | . spip_pg_groupby($groupby, $from, $select) |
| 575 | 575 | . (!$having ? '' : "\nHAVING $having") |
| 576 | 576 | . ($orderby ? ("\nORDER BY $orderby") : '') |
| 577 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 577 | + . (!$limit ? '' : (" LIMIT $count".(!$offset ? '' : " OFFSET $offset"))); |
|
| 578 | 578 | |
| 579 | 579 | // renvoyer la requete inerte si demandee |
| 580 | 580 | if ($requeter === false) { |
@@ -596,7 +596,7 @@ discard block |
||
| 596 | 596 | $from = spip_pg_select_as($from); |
| 597 | 597 | } |
| 598 | 598 | |
| 599 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 599 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1'.$prefixe.'_', $from); |
|
| 600 | 600 | } |
| 601 | 601 | |
| 602 | 602 | // https://code.spip.net/@spip_pg_orderby |
@@ -605,7 +605,7 @@ discard block |
||
| 605 | 605 | $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
| 606 | 606 | |
| 607 | 607 | foreach ($arg as $v) { |
| 608 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 608 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+'.$v.'/', $select, $m)) { |
|
| 609 | 609 | $res[] = $m[1]; |
| 610 | 610 | } else { |
| 611 | 611 | $res[] = $v; |
@@ -786,7 +786,7 @@ discard block |
||
| 786 | 786 | $n++; |
| 787 | 787 | $res .= "\nwhen $index=$v then $n"; |
| 788 | 788 | } |
| 789 | - $arg = $m[1] . "case $res else 0 end " |
|
| 789 | + $arg = $m[1]."case $res else 0 end " |
|
| 790 | 790 | . substr($arg, strlen($m[0])); |
| 791 | 791 | } |
| 792 | 792 | |
@@ -831,9 +831,9 @@ discard block |
||
| 831 | 831 | } |
| 832 | 832 | |
| 833 | 833 | if (strtoupper($join) === 'AND') { |
| 834 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 834 | + return $exp.join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 835 | 835 | } else { |
| 836 | - return $exp . join($join, $v); |
|
| 836 | + return $exp.join($join, $v); |
|
| 837 | 837 | } |
| 838 | 838 | } |
| 839 | 839 | |
@@ -844,7 +844,7 @@ discard block |
||
| 844 | 844 | if (substr($k, -1) == '@') { |
| 845 | 845 | // c'est une jointure qui se refere au from precedent |
| 846 | 846 | // pas de virgule |
| 847 | - $argsas .= ' ' . $v; |
|
| 847 | + $argsas .= ' '.$v; |
|
| 848 | 848 | } else { |
| 849 | 849 | $as = ''; |
| 850 | 850 | // spip_log("$k : $v", _LOG_DEBUG); |
@@ -854,7 +854,7 @@ discard block |
||
| 854 | 854 | } elseif ($v != $k) { |
| 855 | 855 | $p = strpos($v, ' '); |
| 856 | 856 | if ($p) { |
| 857 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 857 | + $v = substr($v, 0, $p)." AS $k".substr($v, $p); |
|
| 858 | 858 | } else { |
| 859 | 859 | $as = " AS $k"; |
| 860 | 860 | } |
@@ -862,7 +862,7 @@ discard block |
||
| 862 | 862 | } |
| 863 | 863 | // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
| 864 | 864 | // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
| 865 | - $argsas .= ', ' . $v . $as; |
|
| 865 | + $argsas .= ', '.$v.$as; |
|
| 866 | 866 | } |
| 867 | 867 | } |
| 868 | 868 | |
@@ -895,7 +895,7 @@ discard block |
||
| 895 | 895 | $serveur = '', |
| 896 | 896 | $requeter = true |
| 897 | 897 | ) { |
| 898 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 898 | + $c = !$groupby ? '*' : ('DISTINCT '.(is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 899 | 899 | $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
| 900 | 900 | if (!$requeter) { |
| 901 | 901 | return $r; |
@@ -1001,8 +1001,8 @@ discard block |
||
| 1001 | 1001 | |
| 1002 | 1002 | return spip_pg_insert( |
| 1003 | 1003 | $table, |
| 1004 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 1005 | - '(' . join(',', $couples) . ')', |
|
| 1004 | + '('.join(',', array_keys($couples)).')', |
|
| 1005 | + '('.join(',', $couples).')', |
|
| 1006 | 1006 | $desc, |
| 1007 | 1007 | $serveur, |
| 1008 | 1008 | $requeter |
@@ -1026,7 +1026,7 @@ discard block |
||
| 1026 | 1026 | $c = isset($tab_couples[0]) ? $tab_couples[0] : []; |
| 1027 | 1027 | $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
| 1028 | 1028 | |
| 1029 | - $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1029 | + $cles = '('.join(',', array_keys($les_cles)).')'; |
|
| 1030 | 1030 | $valeurs = []; |
| 1031 | 1031 | foreach ($tab_couples as $couples) { |
| 1032 | 1032 | foreach ($couples as $champ => $val) { |
@@ -1035,7 +1035,7 @@ discard block |
||
| 1035 | 1035 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
| 1036 | 1036 | $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
| 1037 | 1037 | |
| 1038 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1038 | + $valeurs[] = '('.join(',', $couples).')'; |
|
| 1039 | 1039 | } |
| 1040 | 1040 | $valeurs = implode(', ', $valeurs); |
| 1041 | 1041 | |
@@ -1057,7 +1057,7 @@ discard block |
||
| 1057 | 1057 | |
| 1058 | 1058 | $set = []; |
| 1059 | 1059 | foreach ($couples as $champ => $val) { |
| 1060 | - $set[] = $champ . '=' . $val; |
|
| 1060 | + $set[] = $champ.'='.$val; |
|
| 1061 | 1061 | } |
| 1062 | 1062 | |
| 1063 | 1063 | $query = calculer_pg_expression('UPDATE', $table, ',') |
@@ -1094,7 +1094,7 @@ discard block |
||
| 1094 | 1094 | // https://code.spip.net/@spip_pg_replace |
| 1095 | 1095 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1096 | 1096 | if (!$values) { |
| 1097 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1097 | + spip_log("replace vide $table", 'pg.'._LOG_AVERTISSEMENT); |
|
| 1098 | 1098 | |
| 1099 | 1099 | return 0; |
| 1100 | 1100 | } |
@@ -1128,8 +1128,8 @@ discard block |
||
| 1128 | 1128 | if (!$where) { |
| 1129 | 1129 | return spip_pg_insert( |
| 1130 | 1130 | $table, |
| 1131 | - '(' . join(',', array_keys($values)) . ')', |
|
| 1132 | - '(' . join(',', $values) . ')', |
|
| 1131 | + '('.join(',', array_keys($values)).')', |
|
| 1132 | + '('.join(',', $values).')', |
|
| 1133 | 1133 | $desc, |
| 1134 | 1134 | $serveur |
| 1135 | 1135 | ); |
@@ -1150,12 +1150,11 @@ discard block |
||
| 1150 | 1150 | $couples = pg_affected_rows($couples); |
| 1151 | 1151 | } |
| 1152 | 1152 | if (!$couples) { |
| 1153 | - $ret = !$seq ? '' : |
|
| 1154 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1155 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1153 | + $ret = !$seq ? '' : (" RETURNING nextval('$seq') < $prim"); |
|
| 1154 | + $connexion['last'] = $q = "INSERT INTO $table (".join(',', array_keys($values)).') VALUES ('.join( |
|
| 1156 | 1155 | ',', |
| 1157 | 1156 | $values |
| 1158 | - ) . ")$ret"; |
|
| 1157 | + ).")$ret"; |
|
| 1159 | 1158 | $couples = spip_pg_query_simple($link, $q); |
| 1160 | 1159 | if (!$couples) { |
| 1161 | 1160 | return false; |
@@ -1204,7 +1203,7 @@ discard block |
||
| 1204 | 1203 | ) { |
| 1205 | 1204 | return ''; |
| 1206 | 1205 | } else { |
| 1207 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1206 | + return $raw ? $prim : $table.'_'.$prim.'_seq'; |
|
| 1208 | 1207 | } |
| 1209 | 1208 | } |
| 1210 | 1209 | |
@@ -1222,22 +1221,22 @@ discard block |
||
| 1222 | 1221 | return spip_pg_frommysql($v); |
| 1223 | 1222 | } else { |
| 1224 | 1223 | if (strncmp($v, '0000', 4) == 0) { |
| 1225 | - $v = '0001' . substr($v, 4); |
|
| 1224 | + $v = '0001'.substr($v, 4); |
|
| 1226 | 1225 | } |
| 1227 | 1226 | if (strpos($v, '-00-00') === 4) { |
| 1228 | - $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1227 | + $v = substr($v, 0, 4).'-01-01'.substr($v, 10); |
|
| 1229 | 1228 | } |
| 1230 | 1229 | |
| 1231 | 1230 | return "timestamp '$v'"; |
| 1232 | 1231 | } |
| 1233 | 1232 | } elseif (!sql_test_int($t)) { |
| 1234 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1233 | + return ("'".pg_escape_string($v)."'"); |
|
| 1235 | 1234 | } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
| 1236 | 1235 | return $v; |
| 1237 | 1236 | } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
| 1238 | 1237 | return substr($v, 1); |
| 1239 | 1238 | } else { |
| 1240 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1239 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.'._LOG_AVERTISSEMENT); |
|
| 1241 | 1240 | |
| 1242 | 1241 | return intval($v); |
| 1243 | 1242 | } |
@@ -1245,7 +1244,7 @@ discard block |
||
| 1245 | 1244 | |
| 1246 | 1245 | // https://code.spip.net/@spip_pg_hex |
| 1247 | 1246 | function spip_pg_hex($v) { |
| 1248 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1247 | + return "CAST(x'".$v."' as bigint)"; |
|
| 1249 | 1248 | } |
| 1250 | 1249 | |
| 1251 | 1250 | function spip_pg_quote($v, $type = '') { |
@@ -1284,15 +1283,15 @@ discard block |
||
| 1284 | 1283 | return $not ? '0=0' : '0=1'; |
| 1285 | 1284 | } |
| 1286 | 1285 | if (strpos($valeurs, "CAST(x'") !== false) { |
| 1287 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1286 | + return "($val=".join("OR $val=", explode(',', $valeurs)).')'; |
|
| 1288 | 1287 | } |
| 1289 | 1288 | $n = $i = 0; |
| 1290 | 1289 | $in_sql = ''; |
| 1291 | 1290 | while ($n = strpos($valeurs, ',', $n + 1)) { |
| 1292 | 1291 | if ((++$i) >= 255) { |
| 1293 | - $in_sql .= "($val $not IN (" . |
|
| 1294 | - substr($valeurs, 0, $n) . |
|
| 1295 | - "))\n" . |
|
| 1292 | + $in_sql .= "($val $not IN (". |
|
| 1293 | + substr($valeurs, 0, $n). |
|
| 1294 | + "))\n". |
|
| 1296 | 1295 | ($not ? "AND\t" : "OR\t"); |
| 1297 | 1296 | $valeurs = substr($valeurs, $n + 1); |
| 1298 | 1297 | $i = $n = 0; |
@@ -1309,7 +1308,7 @@ discard block |
||
| 1309 | 1308 | $s = $link ? pg_last_error($link) : pg_last_error(); |
| 1310 | 1309 | if ($s) { |
| 1311 | 1310 | $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
| 1312 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1311 | + spip_log("$s - $query", 'pg.'._LOG_ERREUR); |
|
| 1313 | 1312 | } |
| 1314 | 1313 | |
| 1315 | 1314 | return $s; |
@@ -1360,7 +1359,7 @@ discard block |
||
| 1360 | 1359 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1361 | 1360 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 1362 | 1361 | $link = $connexion['link']; |
| 1363 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1362 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE '._q($match); |
|
| 1364 | 1363 | |
| 1365 | 1364 | return spip_pg_query_simple($link, $q); |
| 1366 | 1365 | } |
@@ -1369,7 +1368,7 @@ discard block |
||
| 1369 | 1368 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1370 | 1369 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 1371 | 1370 | $link = $connexion['link']; |
| 1372 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1371 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE '._q($nom_table); |
|
| 1373 | 1372 | |
| 1374 | 1373 | $res = spip_pg_query_simple($link, $q); |
| 1375 | 1374 | if (!$res) { |
@@ -1380,15 +1379,15 @@ discard block |
||
| 1380 | 1379 | // il faut en tenir compte dans le return |
| 1381 | 1380 | $fields = []; |
| 1382 | 1381 | while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
| 1383 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1382 | + $fields[$field[0]] = $field[2].(!$field[1] ? '' : (' DEFAULT '.$field[1])); |
|
| 1384 | 1383 | } |
| 1385 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1384 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE '._q($nom_table); |
|
| 1386 | 1385 | $res = spip_pg_query_simple($link, $q); |
| 1387 | 1386 | $keys = []; |
| 1388 | 1387 | while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
| 1389 | 1388 | if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
| 1390 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1391 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1389 | + $nom = str_replace($nom_table.'_', '', $r[2]); |
|
| 1390 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY '.$nom))] = $r[3]; |
|
| 1392 | 1391 | } |
| 1393 | 1392 | } |
| 1394 | 1393 | |
@@ -1421,16 +1420,16 @@ discard block |
||
| 1421 | 1420 | if (strpos($k, 'KEY ') === 0) { |
| 1422 | 1421 | $n = str_replace('`', '', $k); |
| 1423 | 1422 | $v = str_replace('`', '"', $v); |
| 1424 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1423 | + $i = $nom.preg_replace('/KEY +/', '_', $n); |
|
| 1425 | 1424 | if ($k != $n) { |
| 1426 | 1425 | $i = "\"$i\""; |
| 1427 | 1426 | } |
| 1428 | 1427 | $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
| 1429 | 1428 | } elseif (strpos($k, 'UNIQUE ') === 0) { |
| 1430 | 1429 | $k = preg_replace('/^UNIQUE +/', '', $k); |
| 1431 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1430 | + $prim .= "$s\n\t\tCONSTRAINT ".str_replace('`', '"', $k)." UNIQUE ($v)"; |
|
| 1432 | 1431 | } else { |
| 1433 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1432 | + $prim .= "$s\n\t\t".str_replace('`', '"', $k)." ($v)"; |
|
| 1434 | 1433 | } |
| 1435 | 1434 | if ($k == 'PRIMARY KEY') { |
| 1436 | 1435 | $prim_name = $v; |
@@ -1441,17 +1440,17 @@ discard block |
||
| 1441 | 1440 | |
| 1442 | 1441 | $character_set = ''; |
| 1443 | 1442 | if (@$GLOBALS['meta']['charset_sql_base']) { |
| 1444 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1443 | + $character_set .= ' CHARACTER SET '.$GLOBALS['meta']['charset_sql_base']; |
|
| 1445 | 1444 | } |
| 1446 | 1445 | if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
| 1447 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1446 | + $character_set .= ' COLLATE '.$GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1448 | 1447 | } |
| 1449 | 1448 | |
| 1450 | 1449 | foreach ($champs as $k => $v) { |
| 1451 | 1450 | $k = str_replace('`', '"', $k); |
| 1452 | 1451 | if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
| 1453 | 1452 | if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
| 1454 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1453 | + $v = $defs[1].$character_set.' '.substr($v, strlen($defs[1])); |
|
| 1455 | 1454 | } |
| 1456 | 1455 | } |
| 1457 | 1456 | |
@@ -1466,7 +1465,7 @@ discard block |
||
| 1466 | 1465 | |
| 1467 | 1466 | // En l'absence de "if not exists" en PG, on neutralise les erreurs |
| 1468 | 1467 | |
| 1469 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1468 | + $q = "CREATE $temporary TABLE $nom ($query".($prim ? ",$prim" : '').')'. |
|
| 1470 | 1469 | ($character_set ? " DEFAULT $character_set" : '') |
| 1471 | 1470 | . "\n"; |
| 1472 | 1471 | |
@@ -1477,7 +1476,7 @@ discard block |
||
| 1477 | 1476 | $r = @pg_query($link, $q); |
| 1478 | 1477 | |
| 1479 | 1478 | if (!$r) { |
| 1480 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1479 | + spip_log("Impossible de creer cette table: $q", 'pg.'._LOG_ERREUR); |
|
| 1481 | 1480 | } else { |
| 1482 | 1481 | foreach ($keys as $index) { |
| 1483 | 1482 | pg_query($link, $index); |
@@ -1501,13 +1500,13 @@ discard block |
||
| 1501 | 1500 | // vue deja presente |
| 1502 | 1501 | if (sql_showtable($nom, false, $serveur)) { |
| 1503 | 1502 | if ($requeter) { |
| 1504 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1503 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.'._LOG_ERREUR); |
|
| 1505 | 1504 | } |
| 1506 | 1505 | |
| 1507 | 1506 | return false; |
| 1508 | 1507 | } |
| 1509 | 1508 | |
| 1510 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1509 | + $query = "CREATE VIEW $nom AS ".$query_select; |
|
| 1511 | 1510 | |
| 1512 | 1511 | return spip_pg_query($query, $serveur, $requeter); |
| 1513 | 1512 | } |
@@ -1515,7 +1514,7 @@ discard block |
||
| 1515 | 1514 | |
| 1516 | 1515 | // https://code.spip.net/@spip_pg_set_connect_charset |
| 1517 | 1516 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1518 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1517 | + spip_log('changement de charset sql a ecrire en PG', 'pg.'._LOG_ERREUR); |
|
| 1519 | 1518 | } |
| 1520 | 1519 | |
| 1521 | 1520 | |
@@ -1529,7 +1528,7 @@ discard block |
||
| 1529 | 1528 | **/ |
| 1530 | 1529 | // https://code.spip.net/@spip_sqlite_optimize |
| 1531 | 1530 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1532 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1531 | + return spip_pg_query('VACUUM '.$table, $serveur, $requeter); |
|
| 1533 | 1532 | } |
| 1534 | 1533 | |
| 1535 | 1534 | // Selectionner la sous-chaine dans $objet |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -32,28 +32,28 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | function action_desinstaller_plugin_dist() { |
| 34 | 34 | |
| 35 | - $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 36 | - $arg = $securiser_action(); |
|
| 37 | - list($dir_plugins, $plugin) = explode('::', $arg); |
|
| 38 | - $dir_type = '_DIR_PLUGINS'; |
|
| 39 | - if (defined('_DIR_PLUGINS_SUPPL') and $dir_plugins == _DIR_PLUGINS_SUPPL) { |
|
| 40 | - $dir_type = '_DIR_PLUGINS_SUPPL'; |
|
| 41 | - } |
|
| 42 | - $installer_plugins = charger_fonction('installer', 'plugins'); |
|
| 43 | - $infos = $installer_plugins($plugin, 'uninstall', $dir_type); |
|
| 44 | - if ($infos and !$infos['install_test'][0]) { |
|
| 45 | - include_spip('inc/plugin'); |
|
| 46 | - ecrire_plugin_actifs([$plugin], false, 'enleve'); |
|
| 47 | - $erreur = ''; |
|
| 48 | - } else { |
|
| 49 | - $erreur = 'erreur_plugin_desinstalation_echouee'; |
|
| 50 | - } |
|
| 51 | - if ($redirect = _request('redirect')) { |
|
| 52 | - include_spip('inc/headers'); |
|
| 53 | - if ($erreur) { |
|
| 54 | - $redirect = parametre_url($redirect, 'erreur', $erreur); |
|
| 55 | - } |
|
| 56 | - $redirect = str_replace('&', '&', $redirect); |
|
| 57 | - redirige_par_entete($redirect); |
|
| 58 | - } |
|
| 35 | + $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 36 | + $arg = $securiser_action(); |
|
| 37 | + list($dir_plugins, $plugin) = explode('::', $arg); |
|
| 38 | + $dir_type = '_DIR_PLUGINS'; |
|
| 39 | + if (defined('_DIR_PLUGINS_SUPPL') and $dir_plugins == _DIR_PLUGINS_SUPPL) { |
|
| 40 | + $dir_type = '_DIR_PLUGINS_SUPPL'; |
|
| 41 | + } |
|
| 42 | + $installer_plugins = charger_fonction('installer', 'plugins'); |
|
| 43 | + $infos = $installer_plugins($plugin, 'uninstall', $dir_type); |
|
| 44 | + if ($infos and !$infos['install_test'][0]) { |
|
| 45 | + include_spip('inc/plugin'); |
|
| 46 | + ecrire_plugin_actifs([$plugin], false, 'enleve'); |
|
| 47 | + $erreur = ''; |
|
| 48 | + } else { |
|
| 49 | + $erreur = 'erreur_plugin_desinstalation_echouee'; |
|
| 50 | + } |
|
| 51 | + if ($redirect = _request('redirect')) { |
|
| 52 | + include_spip('inc/headers'); |
|
| 53 | + if ($erreur) { |
|
| 54 | + $redirect = parametre_url($redirect, 'erreur', $erreur); |
|
| 55 | + } |
|
| 56 | + $redirect = str_replace('&', '&', $redirect); |
|
| 57 | + redirige_par_entete($redirect); |
|
| 58 | + } |
|
| 59 | 59 | } |
@@ -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 | /** |
@@ -37,41 +37,41 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | function action_editer_auteur_dist($arg = null) { |
| 39 | 39 | |
| 40 | - if (is_null($arg)) { |
|
| 41 | - $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 42 | - $arg = $securiser_action(); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - // si id_auteur n'est pas un nombre, c'est une creation |
|
| 47 | - if (!$id_auteur = intval($arg)) { |
|
| 48 | - if (($id_auteur = auteur_inserer()) > 0) { |
|
| 49 | - # cf. GROS HACK |
|
| 50 | - # recuperer l'eventuel logo charge avant la creation |
|
| 51 | - # ils ont un id = 0-id_auteur de la session |
|
| 52 | - $id_hack = 0 - $GLOBALS['visiteur_session']['id_auteur']; |
|
| 53 | - $chercher_logo = charger_fonction('chercher_logo', 'inc'); |
|
| 54 | - foreach (['on', 'off'] as $type) { |
|
| 55 | - if ($logo = $chercher_logo($id_hack, 'id_auteur', $type)) { |
|
| 56 | - if ($logo = reset($logo)) { |
|
| 57 | - rename($logo, str_replace($id_hack, $id_auteur, $logo)); |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - // Enregistre l'envoi dans la BD |
|
| 65 | - $err = ''; |
|
| 66 | - if ($id_auteur > 0) { |
|
| 67 | - $err = auteur_modifier($id_auteur); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - if ($err) { |
|
| 71 | - spip_log("echec editeur auteur: $err", _LOG_ERREUR); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - return [$id_auteur, $err]; |
|
| 40 | + if (is_null($arg)) { |
|
| 41 | + $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 42 | + $arg = $securiser_action(); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + // si id_auteur n'est pas un nombre, c'est une creation |
|
| 47 | + if (!$id_auteur = intval($arg)) { |
|
| 48 | + if (($id_auteur = auteur_inserer()) > 0) { |
|
| 49 | + # cf. GROS HACK |
|
| 50 | + # recuperer l'eventuel logo charge avant la creation |
|
| 51 | + # ils ont un id = 0-id_auteur de la session |
|
| 52 | + $id_hack = 0 - $GLOBALS['visiteur_session']['id_auteur']; |
|
| 53 | + $chercher_logo = charger_fonction('chercher_logo', 'inc'); |
|
| 54 | + foreach (['on', 'off'] as $type) { |
|
| 55 | + if ($logo = $chercher_logo($id_hack, 'id_auteur', $type)) { |
|
| 56 | + if ($logo = reset($logo)) { |
|
| 57 | + rename($logo, str_replace($id_hack, $id_auteur, $logo)); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + // Enregistre l'envoi dans la BD |
|
| 65 | + $err = ''; |
|
| 66 | + if ($id_auteur > 0) { |
|
| 67 | + $err = auteur_modifier($id_auteur); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + if ($err) { |
|
| 71 | + spip_log("echec editeur auteur: $err", _LOG_ERREUR); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + return [$id_auteur, $err]; |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | /** |
@@ -88,44 +88,44 @@ discard block |
||
| 88 | 88 | */ |
| 89 | 89 | function auteur_inserer($source = null, $set = null) { |
| 90 | 90 | |
| 91 | - // Ce qu'on va demander comme modifications |
|
| 92 | - $champs = []; |
|
| 93 | - $champs['source'] = $source ? $source : 'spip'; |
|
| 94 | - |
|
| 95 | - $champs['login'] = ''; |
|
| 96 | - $champs['statut'] = '5poubelle'; // inutilisable tant qu'il n'a pas ete renseigne et institue |
|
| 97 | - $champs['webmestre'] = 'non'; |
|
| 98 | - if (empty($champs['imessage'])) { |
|
| 99 | - $champs['imessage'] = 'oui'; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - if ($set) { |
|
| 103 | - $champs = array_merge($champs, $set); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - // Envoyer aux plugins |
|
| 107 | - $champs = pipeline( |
|
| 108 | - 'pre_insertion', |
|
| 109 | - [ |
|
| 110 | - 'args' => [ |
|
| 111 | - 'table' => 'spip_auteurs', |
|
| 112 | - ], |
|
| 113 | - 'data' => $champs |
|
| 114 | - ] |
|
| 115 | - ); |
|
| 116 | - $id_auteur = sql_insertq('spip_auteurs', $champs); |
|
| 117 | - pipeline( |
|
| 118 | - 'post_insertion', |
|
| 119 | - [ |
|
| 120 | - 'args' => [ |
|
| 121 | - 'table' => 'spip_auteurs', |
|
| 122 | - 'id_objet' => $id_auteur |
|
| 123 | - ], |
|
| 124 | - 'data' => $champs |
|
| 125 | - ] |
|
| 126 | - ); |
|
| 127 | - |
|
| 128 | - return $id_auteur; |
|
| 91 | + // Ce qu'on va demander comme modifications |
|
| 92 | + $champs = []; |
|
| 93 | + $champs['source'] = $source ? $source : 'spip'; |
|
| 94 | + |
|
| 95 | + $champs['login'] = ''; |
|
| 96 | + $champs['statut'] = '5poubelle'; // inutilisable tant qu'il n'a pas ete renseigne et institue |
|
| 97 | + $champs['webmestre'] = 'non'; |
|
| 98 | + if (empty($champs['imessage'])) { |
|
| 99 | + $champs['imessage'] = 'oui'; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + if ($set) { |
|
| 103 | + $champs = array_merge($champs, $set); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + // Envoyer aux plugins |
|
| 107 | + $champs = pipeline( |
|
| 108 | + 'pre_insertion', |
|
| 109 | + [ |
|
| 110 | + 'args' => [ |
|
| 111 | + 'table' => 'spip_auteurs', |
|
| 112 | + ], |
|
| 113 | + 'data' => $champs |
|
| 114 | + ] |
|
| 115 | + ); |
|
| 116 | + $id_auteur = sql_insertq('spip_auteurs', $champs); |
|
| 117 | + pipeline( |
|
| 118 | + 'post_insertion', |
|
| 119 | + [ |
|
| 120 | + 'args' => [ |
|
| 121 | + 'table' => 'spip_auteurs', |
|
| 122 | + 'id_objet' => $id_auteur |
|
| 123 | + ], |
|
| 124 | + 'data' => $champs |
|
| 125 | + ] |
|
| 126 | + ); |
|
| 127 | + |
|
| 128 | + return $id_auteur; |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | |
@@ -150,70 +150,70 @@ discard block |
||
| 150 | 150 | */ |
| 151 | 151 | function auteur_modifier($id_auteur, $set = null, $force_update = false) { |
| 152 | 152 | |
| 153 | - include_spip('inc/modifier'); |
|
| 154 | - include_spip('inc/filtres'); |
|
| 155 | - $c = collecter_requests( |
|
| 156 | - // white list |
|
| 157 | - objet_info('auteur', 'champs_editables'), |
|
| 158 | - // black list |
|
| 159 | - $force_update ? [] : ['webmestre', 'pass', 'login'], |
|
| 160 | - // donnees eventuellement fournies |
|
| 161 | - $set |
|
| 162 | - ); |
|
| 163 | - |
|
| 164 | - if ( |
|
| 165 | - $err = objet_modifier_champs( |
|
| 166 | - 'auteur', |
|
| 167 | - $id_auteur, |
|
| 168 | - [ |
|
| 169 | - 'data' => $set, |
|
| 170 | - 'nonvide' => ['nom' => _T('ecrire:item_nouvel_auteur')] |
|
| 171 | - ], |
|
| 172 | - $c |
|
| 173 | - ) |
|
| 174 | - ) { |
|
| 175 | - return $err; |
|
| 176 | - } |
|
| 177 | - $session = $c; |
|
| 178 | - |
|
| 179 | - $err = ''; |
|
| 180 | - if (!$force_update) { |
|
| 181 | - // Modification de statut, changement de rubrique ? |
|
| 182 | - $c = collecter_requests( |
|
| 183 | - // white list |
|
| 184 | - [ |
|
| 185 | - 'statut', |
|
| 186 | - 'new_login', |
|
| 187 | - 'new_pass', |
|
| 188 | - 'login', |
|
| 189 | - 'pass', |
|
| 190 | - 'webmestre', |
|
| 191 | - 'restreintes', |
|
| 192 | - 'id_parent' |
|
| 193 | - ], |
|
| 194 | - // black list |
|
| 195 | - [], |
|
| 196 | - // donnees eventuellement fournies |
|
| 197 | - $set |
|
| 198 | - ); |
|
| 199 | - if (isset($c['new_login']) and !isset($c['login'])) { |
|
| 200 | - $c['login'] = $c['new_login']; |
|
| 201 | - } |
|
| 202 | - if (isset($c['new_pass']) and !isset($c['pass'])) { |
|
| 203 | - $c['pass'] = $c['new_pass']; |
|
| 204 | - } |
|
| 205 | - $err = auteur_instituer($id_auteur, $c); |
|
| 206 | - $session = array_merge($session, $c); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - // .. mettre a jour les sessions de cet auteur |
|
| 210 | - include_spip('inc/session'); |
|
| 211 | - $session['id_auteur'] = $id_auteur; |
|
| 212 | - unset($session['new_login']); |
|
| 213 | - unset($session['new_pass']); |
|
| 214 | - actualiser_sessions($session); |
|
| 215 | - |
|
| 216 | - return $err; |
|
| 153 | + include_spip('inc/modifier'); |
|
| 154 | + include_spip('inc/filtres'); |
|
| 155 | + $c = collecter_requests( |
|
| 156 | + // white list |
|
| 157 | + objet_info('auteur', 'champs_editables'), |
|
| 158 | + // black list |
|
| 159 | + $force_update ? [] : ['webmestre', 'pass', 'login'], |
|
| 160 | + // donnees eventuellement fournies |
|
| 161 | + $set |
|
| 162 | + ); |
|
| 163 | + |
|
| 164 | + if ( |
|
| 165 | + $err = objet_modifier_champs( |
|
| 166 | + 'auteur', |
|
| 167 | + $id_auteur, |
|
| 168 | + [ |
|
| 169 | + 'data' => $set, |
|
| 170 | + 'nonvide' => ['nom' => _T('ecrire:item_nouvel_auteur')] |
|
| 171 | + ], |
|
| 172 | + $c |
|
| 173 | + ) |
|
| 174 | + ) { |
|
| 175 | + return $err; |
|
| 176 | + } |
|
| 177 | + $session = $c; |
|
| 178 | + |
|
| 179 | + $err = ''; |
|
| 180 | + if (!$force_update) { |
|
| 181 | + // Modification de statut, changement de rubrique ? |
|
| 182 | + $c = collecter_requests( |
|
| 183 | + // white list |
|
| 184 | + [ |
|
| 185 | + 'statut', |
|
| 186 | + 'new_login', |
|
| 187 | + 'new_pass', |
|
| 188 | + 'login', |
|
| 189 | + 'pass', |
|
| 190 | + 'webmestre', |
|
| 191 | + 'restreintes', |
|
| 192 | + 'id_parent' |
|
| 193 | + ], |
|
| 194 | + // black list |
|
| 195 | + [], |
|
| 196 | + // donnees eventuellement fournies |
|
| 197 | + $set |
|
| 198 | + ); |
|
| 199 | + if (isset($c['new_login']) and !isset($c['login'])) { |
|
| 200 | + $c['login'] = $c['new_login']; |
|
| 201 | + } |
|
| 202 | + if (isset($c['new_pass']) and !isset($c['pass'])) { |
|
| 203 | + $c['pass'] = $c['new_pass']; |
|
| 204 | + } |
|
| 205 | + $err = auteur_instituer($id_auteur, $c); |
|
| 206 | + $session = array_merge($session, $c); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + // .. mettre a jour les sessions de cet auteur |
|
| 210 | + include_spip('inc/session'); |
|
| 211 | + $session['id_auteur'] = $id_auteur; |
|
| 212 | + unset($session['new_login']); |
|
| 213 | + unset($session['new_pass']); |
|
| 214 | + actualiser_sessions($session); |
|
| 215 | + |
|
| 216 | + return $err; |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | /** |
@@ -234,9 +234,9 @@ discard block |
||
| 234 | 234 | * @return string |
| 235 | 235 | */ |
| 236 | 236 | function auteur_associer($id_auteur, $objets, $qualif = null) { |
| 237 | - include_spip('action/editer_liens'); |
|
| 237 | + include_spip('action/editer_liens'); |
|
| 238 | 238 | |
| 239 | - return objet_associer(['auteur' => $id_auteur], $objets, $qualif); |
|
| 239 | + return objet_associer(['auteur' => $id_auteur], $objets, $qualif); |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | /** |
@@ -255,9 +255,9 @@ discard block |
||
| 255 | 255 | * @return string |
| 256 | 256 | */ |
| 257 | 257 | function auteur_dissocier($id_auteur, $objets) { |
| 258 | - include_spip('action/editer_liens'); |
|
| 258 | + include_spip('action/editer_liens'); |
|
| 259 | 259 | |
| 260 | - return objet_dissocier(['auteur' => $id_auteur], $objets); |
|
| 260 | + return objet_dissocier(['auteur' => $id_auteur], $objets); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | /** |
@@ -278,9 +278,9 @@ discard block |
||
| 278 | 278 | * @return bool|int |
| 279 | 279 | */ |
| 280 | 280 | function auteur_qualifier($id_auteur, $objets, $qualif) { |
| 281 | - include_spip('action/editer_liens'); |
|
| 281 | + include_spip('action/editer_liens'); |
|
| 282 | 282 | |
| 283 | - return objet_qualifier_liens(['auteur' => $id_auteur], $objets, $qualif); |
|
| 283 | + return objet_qualifier_liens(['auteur' => $id_auteur], $objets, $qualif); |
|
| 284 | 284 | } |
| 285 | 285 | |
| 286 | 286 | |
@@ -299,139 +299,139 @@ discard block |
||
| 299 | 299 | * @return bool|string |
| 300 | 300 | */ |
| 301 | 301 | function auteur_instituer($id_auteur, $c, $force_webmestre = false) { |
| 302 | - if (!$id_auteur = intval($id_auteur)) { |
|
| 303 | - return false; |
|
| 304 | - } |
|
| 305 | - $erreurs = []; // contiendra les differentes erreurs a traduire par _T() |
|
| 306 | - $champs = []; |
|
| 307 | - |
|
| 308 | - // les memoriser pour les faire passer dans le pipeline pre_edition |
|
| 309 | - if (isset($c['login']) and strlen($c['login'])) { |
|
| 310 | - $champs['login'] = $c['login']; |
|
| 311 | - } |
|
| 312 | - if (isset($c['pass']) and strlen($c['pass'])) { |
|
| 313 | - $champs['pass'] = $c['pass']; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - $statut = $statut_ancien = sql_getfetsel('statut', 'spip_auteurs', 'id_auteur=' . intval($id_auteur)); |
|
| 317 | - |
|
| 318 | - if ( |
|
| 319 | - isset($c['statut']) |
|
| 320 | - and (autoriser('modifier', 'auteur', $id_auteur, null, ['statut' => $c['statut']])) |
|
| 321 | - ) { |
|
| 322 | - $statut = $champs['statut'] = $c['statut']; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - // Restreindre avant de declarer l'auteur |
|
| 326 | - // (section critique sur les droits) |
|
| 327 | - if (isset($c['id_parent']) and $c['id_parent']) { |
|
| 328 | - if (is_array($c['restreintes'])) { |
|
| 329 | - $c['restreintes'][] = $c['id_parent']; |
|
| 330 | - } else { |
|
| 331 | - $c['restreintes'] = [$c['id_parent']]; |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - if ( |
|
| 336 | - isset($c['webmestre']) |
|
| 337 | - and ($force_webmestre or autoriser('modifier', 'auteur', $id_auteur, null, ['webmestre' => '?'])) |
|
| 338 | - ) { |
|
| 339 | - $champs['webmestre'] = $c['webmestre'] == 'oui' ? 'oui' : 'non'; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - // si statut change et n'est pas 0minirezo, on force webmestre a non |
|
| 343 | - if (isset($c['statut']) and $c['statut'] !== '0minirezo') { |
|
| 344 | - $champs['webmestre'] = $c['webmestre'] = 'non'; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - // Envoyer aux plugins |
|
| 348 | - $champs = pipeline( |
|
| 349 | - 'pre_edition', |
|
| 350 | - [ |
|
| 351 | - 'args' => [ |
|
| 352 | - 'table' => 'spip_auteurs', |
|
| 353 | - 'id_objet' => $id_auteur, |
|
| 354 | - 'action' => 'instituer', |
|
| 355 | - 'statut_ancien' => $statut_ancien, |
|
| 356 | - ], |
|
| 357 | - 'data' => $champs |
|
| 358 | - ] |
|
| 359 | - ); |
|
| 360 | - |
|
| 361 | - if ( |
|
| 362 | - isset($c['restreintes']) and is_array($c['restreintes']) |
|
| 363 | - and autoriser('modifier', 'auteur', $id_auteur, null, ['restreint' => $c['restreintes']]) |
|
| 364 | - ) { |
|
| 365 | - $rubriques = array_map('intval', $c['restreintes']); |
|
| 366 | - $rubriques = array_unique($rubriques); |
|
| 367 | - $rubriques = array_diff($rubriques, [0]); |
|
| 368 | - auteur_dissocier($id_auteur, ['rubrique' => '*']); |
|
| 369 | - auteur_associer($id_auteur, ['rubrique' => $rubriques]); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - $flag_ecrire_acces = false; |
|
| 373 | - // commencer par traiter les cas particuliers des logins et pass |
|
| 374 | - // avant les autres ecritures en base |
|
| 375 | - if (isset($champs['login']) or isset($champs['pass'])) { |
|
| 376 | - $auth_methode = sql_getfetsel('source', 'spip_auteurs', 'id_auteur=' . intval($id_auteur)); |
|
| 377 | - include_spip('inc/auth'); |
|
| 378 | - if (isset($champs['login']) and strlen($champs['login'])) { |
|
| 379 | - if (!auth_modifier_login($auth_methode, $champs['login'], $id_auteur)) { |
|
| 380 | - $erreurs[] = 'ecrire:impossible_modifier_login_auteur'; |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - if (isset($champs['pass']) and strlen($champs['pass'])) { |
|
| 384 | - $champs['login'] = sql_getfetsel('login', 'spip_auteurs', 'id_auteur=' . intval($id_auteur)); |
|
| 385 | - if (!auth_modifier_pass($auth_methode, $champs['login'], $champs['pass'], $id_auteur)) { |
|
| 386 | - $erreurs[] = 'ecrire:impossible_modifier_pass_auteur'; |
|
| 387 | - } |
|
| 388 | - } |
|
| 389 | - unset($champs['login']); |
|
| 390 | - unset($champs['pass']); |
|
| 391 | - $flag_ecrire_acces = true; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - if (!count($champs)) { |
|
| 395 | - return implode(' ', array_map('_T', $erreurs)); |
|
| 396 | - } |
|
| 397 | - sql_updateq('spip_auteurs', $champs, 'id_auteur=' . $id_auteur); |
|
| 398 | - |
|
| 399 | - // .. mettre a jour les fichiers .htpasswd et .htpasswd-admin |
|
| 400 | - if ( |
|
| 401 | - $flag_ecrire_acces |
|
| 402 | - or isset($champs['statut']) |
|
| 403 | - ) { |
|
| 404 | - include_spip('inc/acces'); |
|
| 405 | - ecrire_acces(); |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - // Invalider les caches |
|
| 409 | - include_spip('inc/invalideur'); |
|
| 410 | - suivre_invalideur("id='auteur/$id_auteur'"); |
|
| 411 | - |
|
| 412 | - // Pipeline |
|
| 413 | - pipeline( |
|
| 414 | - 'post_edition', |
|
| 415 | - [ |
|
| 416 | - 'args' => [ |
|
| 417 | - 'table' => 'spip_auteurs', |
|
| 418 | - 'id_objet' => $id_auteur, |
|
| 419 | - 'action' => 'instituer', |
|
| 420 | - 'statut_ancien' => $statut_ancien, |
|
| 421 | - ], |
|
| 422 | - 'data' => $champs |
|
| 423 | - ] |
|
| 424 | - ); |
|
| 425 | - |
|
| 426 | - |
|
| 427 | - // Notifications |
|
| 428 | - if ($notifications = charger_fonction('notifications', 'inc')) { |
|
| 429 | - $notifications( |
|
| 430 | - 'instituerauteur', |
|
| 431 | - $id_auteur, |
|
| 432 | - ['statut' => $statut, 'statut_ancien' => $statut_ancien] |
|
| 433 | - ); |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - return implode(' ', array_map('_T', $erreurs)); |
|
| 302 | + if (!$id_auteur = intval($id_auteur)) { |
|
| 303 | + return false; |
|
| 304 | + } |
|
| 305 | + $erreurs = []; // contiendra les differentes erreurs a traduire par _T() |
|
| 306 | + $champs = []; |
|
| 307 | + |
|
| 308 | + // les memoriser pour les faire passer dans le pipeline pre_edition |
|
| 309 | + if (isset($c['login']) and strlen($c['login'])) { |
|
| 310 | + $champs['login'] = $c['login']; |
|
| 311 | + } |
|
| 312 | + if (isset($c['pass']) and strlen($c['pass'])) { |
|
| 313 | + $champs['pass'] = $c['pass']; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + $statut = $statut_ancien = sql_getfetsel('statut', 'spip_auteurs', 'id_auteur=' . intval($id_auteur)); |
|
| 317 | + |
|
| 318 | + if ( |
|
| 319 | + isset($c['statut']) |
|
| 320 | + and (autoriser('modifier', 'auteur', $id_auteur, null, ['statut' => $c['statut']])) |
|
| 321 | + ) { |
|
| 322 | + $statut = $champs['statut'] = $c['statut']; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + // Restreindre avant de declarer l'auteur |
|
| 326 | + // (section critique sur les droits) |
|
| 327 | + if (isset($c['id_parent']) and $c['id_parent']) { |
|
| 328 | + if (is_array($c['restreintes'])) { |
|
| 329 | + $c['restreintes'][] = $c['id_parent']; |
|
| 330 | + } else { |
|
| 331 | + $c['restreintes'] = [$c['id_parent']]; |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + if ( |
|
| 336 | + isset($c['webmestre']) |
|
| 337 | + and ($force_webmestre or autoriser('modifier', 'auteur', $id_auteur, null, ['webmestre' => '?'])) |
|
| 338 | + ) { |
|
| 339 | + $champs['webmestre'] = $c['webmestre'] == 'oui' ? 'oui' : 'non'; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + // si statut change et n'est pas 0minirezo, on force webmestre a non |
|
| 343 | + if (isset($c['statut']) and $c['statut'] !== '0minirezo') { |
|
| 344 | + $champs['webmestre'] = $c['webmestre'] = 'non'; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + // Envoyer aux plugins |
|
| 348 | + $champs = pipeline( |
|
| 349 | + 'pre_edition', |
|
| 350 | + [ |
|
| 351 | + 'args' => [ |
|
| 352 | + 'table' => 'spip_auteurs', |
|
| 353 | + 'id_objet' => $id_auteur, |
|
| 354 | + 'action' => 'instituer', |
|
| 355 | + 'statut_ancien' => $statut_ancien, |
|
| 356 | + ], |
|
| 357 | + 'data' => $champs |
|
| 358 | + ] |
|
| 359 | + ); |
|
| 360 | + |
|
| 361 | + if ( |
|
| 362 | + isset($c['restreintes']) and is_array($c['restreintes']) |
|
| 363 | + and autoriser('modifier', 'auteur', $id_auteur, null, ['restreint' => $c['restreintes']]) |
|
| 364 | + ) { |
|
| 365 | + $rubriques = array_map('intval', $c['restreintes']); |
|
| 366 | + $rubriques = array_unique($rubriques); |
|
| 367 | + $rubriques = array_diff($rubriques, [0]); |
|
| 368 | + auteur_dissocier($id_auteur, ['rubrique' => '*']); |
|
| 369 | + auteur_associer($id_auteur, ['rubrique' => $rubriques]); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + $flag_ecrire_acces = false; |
|
| 373 | + // commencer par traiter les cas particuliers des logins et pass |
|
| 374 | + // avant les autres ecritures en base |
|
| 375 | + if (isset($champs['login']) or isset($champs['pass'])) { |
|
| 376 | + $auth_methode = sql_getfetsel('source', 'spip_auteurs', 'id_auteur=' . intval($id_auteur)); |
|
| 377 | + include_spip('inc/auth'); |
|
| 378 | + if (isset($champs['login']) and strlen($champs['login'])) { |
|
| 379 | + if (!auth_modifier_login($auth_methode, $champs['login'], $id_auteur)) { |
|
| 380 | + $erreurs[] = 'ecrire:impossible_modifier_login_auteur'; |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + if (isset($champs['pass']) and strlen($champs['pass'])) { |
|
| 384 | + $champs['login'] = sql_getfetsel('login', 'spip_auteurs', 'id_auteur=' . intval($id_auteur)); |
|
| 385 | + if (!auth_modifier_pass($auth_methode, $champs['login'], $champs['pass'], $id_auteur)) { |
|
| 386 | + $erreurs[] = 'ecrire:impossible_modifier_pass_auteur'; |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | + unset($champs['login']); |
|
| 390 | + unset($champs['pass']); |
|
| 391 | + $flag_ecrire_acces = true; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + if (!count($champs)) { |
|
| 395 | + return implode(' ', array_map('_T', $erreurs)); |
|
| 396 | + } |
|
| 397 | + sql_updateq('spip_auteurs', $champs, 'id_auteur=' . $id_auteur); |
|
| 398 | + |
|
| 399 | + // .. mettre a jour les fichiers .htpasswd et .htpasswd-admin |
|
| 400 | + if ( |
|
| 401 | + $flag_ecrire_acces |
|
| 402 | + or isset($champs['statut']) |
|
| 403 | + ) { |
|
| 404 | + include_spip('inc/acces'); |
|
| 405 | + ecrire_acces(); |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + // Invalider les caches |
|
| 409 | + include_spip('inc/invalideur'); |
|
| 410 | + suivre_invalideur("id='auteur/$id_auteur'"); |
|
| 411 | + |
|
| 412 | + // Pipeline |
|
| 413 | + pipeline( |
|
| 414 | + 'post_edition', |
|
| 415 | + [ |
|
| 416 | + 'args' => [ |
|
| 417 | + 'table' => 'spip_auteurs', |
|
| 418 | + 'id_objet' => $id_auteur, |
|
| 419 | + 'action' => 'instituer', |
|
| 420 | + 'statut_ancien' => $statut_ancien, |
|
| 421 | + ], |
|
| 422 | + 'data' => $champs |
|
| 423 | + ] |
|
| 424 | + ); |
|
| 425 | + |
|
| 426 | + |
|
| 427 | + // Notifications |
|
| 428 | + if ($notifications = charger_fonction('notifications', 'inc')) { |
|
| 429 | + $notifications( |
|
| 430 | + 'instituerauteur', |
|
| 431 | + $id_auteur, |
|
| 432 | + ['statut' => $statut, 'statut_ancien' => $statut_ancien] |
|
| 433 | + ); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + return implode(' ', array_map('_T', $erreurs)); |
|
| 437 | 437 | } |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | $champs['source'] = $source ? $source : 'spip'; |
| 94 | 94 | |
| 95 | 95 | $champs['login'] = ''; |
| 96 | - $champs['statut'] = '5poubelle'; // inutilisable tant qu'il n'a pas ete renseigne et institue |
|
| 96 | + $champs['statut'] = '5poubelle'; // inutilisable tant qu'il n'a pas ete renseigne et institue |
|
| 97 | 97 | $champs['webmestre'] = 'non'; |
| 98 | 98 | if (empty($champs['imessage'])) { |
| 99 | 99 | $champs['imessage'] = 'oui'; |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | $champs['pass'] = $c['pass']; |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | - $statut = $statut_ancien = sql_getfetsel('statut', 'spip_auteurs', 'id_auteur=' . intval($id_auteur)); |
|
| 316 | + $statut = $statut_ancien = sql_getfetsel('statut', 'spip_auteurs', 'id_auteur='.intval($id_auteur)); |
|
| 317 | 317 | |
| 318 | 318 | if ( |
| 319 | 319 | isset($c['statut']) |
@@ -373,7 +373,7 @@ discard block |
||
| 373 | 373 | // commencer par traiter les cas particuliers des logins et pass |
| 374 | 374 | // avant les autres ecritures en base |
| 375 | 375 | if (isset($champs['login']) or isset($champs['pass'])) { |
| 376 | - $auth_methode = sql_getfetsel('source', 'spip_auteurs', 'id_auteur=' . intval($id_auteur)); |
|
| 376 | + $auth_methode = sql_getfetsel('source', 'spip_auteurs', 'id_auteur='.intval($id_auteur)); |
|
| 377 | 377 | include_spip('inc/auth'); |
| 378 | 378 | if (isset($champs['login']) and strlen($champs['login'])) { |
| 379 | 379 | if (!auth_modifier_login($auth_methode, $champs['login'], $id_auteur)) { |
@@ -381,7 +381,7 @@ discard block |
||
| 381 | 381 | } |
| 382 | 382 | } |
| 383 | 383 | if (isset($champs['pass']) and strlen($champs['pass'])) { |
| 384 | - $champs['login'] = sql_getfetsel('login', 'spip_auteurs', 'id_auteur=' . intval($id_auteur)); |
|
| 384 | + $champs['login'] = sql_getfetsel('login', 'spip_auteurs', 'id_auteur='.intval($id_auteur)); |
|
| 385 | 385 | if (!auth_modifier_pass($auth_methode, $champs['login'], $champs['pass'], $id_auteur)) { |
| 386 | 386 | $erreurs[] = 'ecrire:impossible_modifier_pass_auteur'; |
| 387 | 387 | } |
@@ -394,7 +394,7 @@ discard block |
||
| 394 | 394 | if (!count($champs)) { |
| 395 | 395 | return implode(' ', array_map('_T', $erreurs)); |
| 396 | 396 | } |
| 397 | - sql_updateq('spip_auteurs', $champs, 'id_auteur=' . $id_auteur); |
|
| 397 | + sql_updateq('spip_auteurs', $champs, 'id_auteur='.$id_auteur); |
|
| 398 | 398 | |
| 399 | 399 | // .. mettre a jour les fichiers .htpasswd et .htpasswd-admin |
| 400 | 400 | if ( |
@@ -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 | /** |
@@ -28,49 +28,49 @@ discard block |
||
| 28 | 28 | * @return void |
| 29 | 29 | */ |
| 30 | 30 | function enregistre_modif_plugin() { |
| 31 | - include_spip('inc/plugin'); |
|
| 32 | - // recuperer les plugins dans l'ordre des $_POST |
|
| 33 | - $test = []; |
|
| 34 | - foreach (liste_plugin_files() as $file) { |
|
| 35 | - $test['s' . substr(md5(_DIR_PLUGINS . $file), 0, 16)] = $file; |
|
| 36 | - } |
|
| 37 | - if (defined('_DIR_PLUGINS_SUPPL')) { |
|
| 38 | - foreach (liste_plugin_files(_DIR_PLUGINS_SUPPL) as $file) { |
|
| 39 | - $test['s' . substr(md5(_DIR_PLUGINS_SUPPL . $file), 0, 16)] = $file; |
|
| 40 | - } |
|
| 41 | - } |
|
| 31 | + include_spip('inc/plugin'); |
|
| 32 | + // recuperer les plugins dans l'ordre des $_POST |
|
| 33 | + $test = []; |
|
| 34 | + foreach (liste_plugin_files() as $file) { |
|
| 35 | + $test['s' . substr(md5(_DIR_PLUGINS . $file), 0, 16)] = $file; |
|
| 36 | + } |
|
| 37 | + if (defined('_DIR_PLUGINS_SUPPL')) { |
|
| 38 | + foreach (liste_plugin_files(_DIR_PLUGINS_SUPPL) as $file) { |
|
| 39 | + $test['s' . substr(md5(_DIR_PLUGINS_SUPPL . $file), 0, 16)] = $file; |
|
| 40 | + } |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - $plugin = []; |
|
| 43 | + $plugin = []; |
|
| 44 | 44 | |
| 45 | - foreach ($_POST as $choix => $val) { |
|
| 46 | - if (isset($test[$choix]) && $val == 'O') { |
|
| 47 | - $plugin[] = $test[$choix]; |
|
| 48 | - } |
|
| 49 | - } |
|
| 45 | + foreach ($_POST as $choix => $val) { |
|
| 46 | + if (isset($test[$choix]) && $val == 'O') { |
|
| 47 | + $plugin[] = $test[$choix]; |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - spip_log("Changement des plugins actifs par l'auteur " . $GLOBALS['visiteur_session']['id_auteur'] . ': ' . join( |
|
| 52 | - ',', |
|
| 53 | - $plugin |
|
| 54 | - )); |
|
| 55 | - ecrire_plugin_actifs($plugin); |
|
| 51 | + spip_log("Changement des plugins actifs par l'auteur " . $GLOBALS['visiteur_session']['id_auteur'] . ': ' . join( |
|
| 52 | + ',', |
|
| 53 | + $plugin |
|
| 54 | + )); |
|
| 55 | + ecrire_plugin_actifs($plugin); |
|
| 56 | 56 | |
| 57 | - // Chaque fois que l'on valide des plugins, on memorise la liste de ces plugins comme etant "interessants", avec un score initial, qui sera decremente a chaque tour : ainsi un plugin active pourra reter visible a l'ecran, jusqu'a ce qu'il tombe dans l'oubli. |
|
| 58 | - $plugins_interessants = @unserialize($GLOBALS['meta']['plugins_interessants']); |
|
| 59 | - if (!is_array($plugins_interessants)) { |
|
| 60 | - $plugins_interessants = []; |
|
| 61 | - } |
|
| 57 | + // Chaque fois que l'on valide des plugins, on memorise la liste de ces plugins comme etant "interessants", avec un score initial, qui sera decremente a chaque tour : ainsi un plugin active pourra reter visible a l'ecran, jusqu'a ce qu'il tombe dans l'oubli. |
|
| 58 | + $plugins_interessants = @unserialize($GLOBALS['meta']['plugins_interessants']); |
|
| 59 | + if (!is_array($plugins_interessants)) { |
|
| 60 | + $plugins_interessants = []; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - $plugins_interessants2 = []; |
|
| 63 | + $plugins_interessants2 = []; |
|
| 64 | 64 | |
| 65 | - foreach ($plugins_interessants as $plug => $score) { |
|
| 66 | - if ($score > 1) { |
|
| 67 | - $plugins_interessants2[$plug] = $score - 1; |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - foreach ($plugin as $plug) { |
|
| 71 | - $plugins_interessants2[$plug] = 10; |
|
| 72 | - } // score initial |
|
| 73 | - ecrire_meta('plugins_interessants', serialize($plugins_interessants2)); |
|
| 65 | + foreach ($plugins_interessants as $plug => $score) { |
|
| 66 | + if ($score > 1) { |
|
| 67 | + $plugins_interessants2[$plug] = $score - 1; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + foreach ($plugin as $plug) { |
|
| 71 | + $plugins_interessants2[$plug] = 10; |
|
| 72 | + } // score initial |
|
| 73 | + ecrire_meta('plugins_interessants', serialize($plugins_interessants2)); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | /** |
@@ -82,13 +82,13 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | function action_activer_plugins_dist() { |
| 84 | 84 | |
| 85 | - $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 86 | - $securiser_action(); |
|
| 85 | + $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 86 | + $securiser_action(); |
|
| 87 | 87 | |
| 88 | - if (!autoriser('configurer', '_plugins')) { |
|
| 89 | - die('erreur'); |
|
| 90 | - } |
|
| 91 | - // forcer la maj des meta pour les cas de modif de numero de version base via phpmyadmin |
|
| 92 | - lire_metas(); |
|
| 93 | - enregistre_modif_plugin(); |
|
| 88 | + if (!autoriser('configurer', '_plugins')) { |
|
| 89 | + die('erreur'); |
|
| 90 | + } |
|
| 91 | + // forcer la maj des meta pour les cas de modif de numero de version base via phpmyadmin |
|
| 92 | + lire_metas(); |
|
| 93 | + enregistre_modif_plugin(); |
|
| 94 | 94 | } |
@@ -32,11 +32,11 @@ discard block |
||
| 32 | 32 | // recuperer les plugins dans l'ordre des $_POST |
| 33 | 33 | $test = []; |
| 34 | 34 | foreach (liste_plugin_files() as $file) { |
| 35 | - $test['s' . substr(md5(_DIR_PLUGINS . $file), 0, 16)] = $file; |
|
| 35 | + $test['s'.substr(md5(_DIR_PLUGINS.$file), 0, 16)] = $file; |
|
| 36 | 36 | } |
| 37 | 37 | if (defined('_DIR_PLUGINS_SUPPL')) { |
| 38 | 38 | foreach (liste_plugin_files(_DIR_PLUGINS_SUPPL) as $file) { |
| 39 | - $test['s' . substr(md5(_DIR_PLUGINS_SUPPL . $file), 0, 16)] = $file; |
|
| 39 | + $test['s'.substr(md5(_DIR_PLUGINS_SUPPL.$file), 0, 16)] = $file; |
|
| 40 | 40 | } |
| 41 | 41 | } |
| 42 | 42 | |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - spip_log("Changement des plugins actifs par l'auteur " . $GLOBALS['visiteur_session']['id_auteur'] . ': ' . join( |
|
| 51 | + spip_log("Changement des plugins actifs par l'auteur ".$GLOBALS['visiteur_session']['id_auteur'].': '.join( |
|
| 52 | 52 | ',', |
| 53 | 53 | $plugin |
| 54 | 54 | )); |
@@ -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 | /** |
@@ -33,36 +33,36 @@ discard block |
||
| 33 | 33 | */ |
| 34 | 34 | function action_editer_objet_dist($id = null, $objet = null, $set = null) { |
| 35 | 35 | |
| 36 | - // appel direct depuis une url avec arg = "objet/id" |
|
| 37 | - if (is_null($id) or is_null($objet)) { |
|
| 38 | - $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 39 | - $arg = $securiser_action(); |
|
| 40 | - list($objet, $id) = array_pad(explode('/', $arg, 2), 2, null); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - // appel incorrect ou depuis une url erronnée interdit |
|
| 44 | - if (is_null($id) or is_null($objet)) { |
|
| 45 | - include_spip('inc/minipres'); |
|
| 46 | - echo minipres(_T('info_acces_interdit')); |
|
| 47 | - die(); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - // si id n'est pas un nombre, c'est une creation |
|
| 51 | - // mais on verifie qu'on a toutes les donnees qu'il faut. |
|
| 52 | - if (!$id = intval($id)) { |
|
| 53 | - // on ne sait pas si un parent existe mais on essaye |
|
| 54 | - $id_parent = _request('id_parent'); |
|
| 55 | - $id = objet_inserer($objet, $id_parent); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - if (!($id = intval($id)) > 0) { |
|
| 59 | - return [$id, _L('echec enregistrement en base')]; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - // Enregistre l'envoi dans la BD |
|
| 63 | - $err = objet_modifier($objet, $id, $set); |
|
| 64 | - |
|
| 65 | - return [$id, $err]; |
|
| 36 | + // appel direct depuis une url avec arg = "objet/id" |
|
| 37 | + if (is_null($id) or is_null($objet)) { |
|
| 38 | + $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 39 | + $arg = $securiser_action(); |
|
| 40 | + list($objet, $id) = array_pad(explode('/', $arg, 2), 2, null); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + // appel incorrect ou depuis une url erronnée interdit |
|
| 44 | + if (is_null($id) or is_null($objet)) { |
|
| 45 | + include_spip('inc/minipres'); |
|
| 46 | + echo minipres(_T('info_acces_interdit')); |
|
| 47 | + die(); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + // si id n'est pas un nombre, c'est une creation |
|
| 51 | + // mais on verifie qu'on a toutes les donnees qu'il faut. |
|
| 52 | + if (!$id = intval($id)) { |
|
| 53 | + // on ne sait pas si un parent existe mais on essaye |
|
| 54 | + $id_parent = _request('id_parent'); |
|
| 55 | + $id = objet_inserer($objet, $id_parent); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + if (!($id = intval($id)) > 0) { |
|
| 59 | + return [$id, _L('echec enregistrement en base')]; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + // Enregistre l'envoi dans la BD |
|
| 63 | + $err = objet_modifier($objet, $id, $set); |
|
| 64 | + |
|
| 65 | + return [$id, $err]; |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
@@ -75,85 +75,85 @@ discard block |
||
| 75 | 75 | * @return mixed|string |
| 76 | 76 | */ |
| 77 | 77 | function objet_modifier($objet, $id, $set = null) { |
| 78 | - if (($t = objet_type($objet)) !== $objet) { |
|
| 79 | - spip_log("objet_modifier: appel avec type $objet invalide au lieu de $t", 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 80 | - $objet = $t; |
|
| 81 | - } |
|
| 82 | - if ( |
|
| 83 | - include_spip('action/editer_' . $objet) |
|
| 84 | - and function_exists($modifier = $objet . '_modifier') |
|
| 85 | - ) { |
|
| 86 | - return $modifier($id, $set); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $table_sql = table_objet_sql($objet); |
|
| 90 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 91 | - $desc = $trouver_table($table_sql); |
|
| 92 | - if (!$desc or !isset($desc['field'])) { |
|
| 93 | - spip_log("Objet $objet inconnu dans objet_modifier", 'editer' . _LOG_ERREUR); |
|
| 94 | - |
|
| 95 | - return _L("Erreur objet $objet inconnu"); |
|
| 96 | - } |
|
| 97 | - include_spip('inc/modifier'); |
|
| 98 | - |
|
| 99 | - $champ_date = ''; |
|
| 100 | - if (isset($desc['date']) and $desc['date']) { |
|
| 101 | - $champ_date = $desc['date']; |
|
| 102 | - } elseif (isset($desc['field']['date'])) { |
|
| 103 | - $champ_date = 'date'; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $white = array_keys($desc['field']); |
|
| 107 | - // on ne traite pas la cle primaire par defaut, notamment car |
|
| 108 | - // sur une creation, id_x vaut 'oui', et serait enregistre en id_x=0 dans la base |
|
| 109 | - $white = array_diff($white, [$desc['key']['PRIMARY KEY']]); |
|
| 110 | - |
|
| 111 | - if (isset($desc['champs_editables']) and is_array($desc['champs_editables'])) { |
|
| 112 | - $white = $desc['champs_editables']; |
|
| 113 | - } |
|
| 114 | - $c = collecter_requests( |
|
| 115 | - // white list |
|
| 116 | - $white, |
|
| 117 | - // black list |
|
| 118 | - [$champ_date, 'statut', 'id_parent', 'id_secteur'], |
|
| 119 | - // donnees eventuellement fournies |
|
| 120 | - $set |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - // Si l'objet est publie, invalider les caches et demander sa reindexation |
|
| 124 | - if (objet_test_si_publie($objet, $id)) { |
|
| 125 | - $invalideur = "id='$objet/$id'"; |
|
| 126 | - $indexation = true; |
|
| 127 | - } else { |
|
| 128 | - $invalideur = ''; |
|
| 129 | - $indexation = false; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if ( |
|
| 133 | - $err = objet_modifier_champs( |
|
| 134 | - $objet, |
|
| 135 | - $id, |
|
| 136 | - [ |
|
| 137 | - 'data' => $set, |
|
| 138 | - 'nonvide' => '', |
|
| 139 | - 'invalideur' => $invalideur, |
|
| 140 | - 'indexation' => $indexation, |
|
| 141 | - // champ a mettre a date('Y-m-d H:i:s') s'il y a modif |
|
| 142 | - 'date_modif' => (isset($desc['field']['date_modif']) ? 'date_modif' : '') |
|
| 143 | - ], |
|
| 144 | - $c |
|
| 145 | - ) |
|
| 146 | - ) { |
|
| 147 | - return $err; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - // Modification de statut, changement de rubrique ? |
|
| 151 | - // FIXME: Ici lorsqu'un $set est passé, la fonction collecter_requests() retourne tout |
|
| 152 | - // le tableau $set hors black liste, mais du coup on a possiblement des champs en trop. |
|
| 153 | - $c = collecter_requests([$champ_date, 'statut', 'id_parent'], [], $set); |
|
| 154 | - $err = objet_instituer($objet, $id, $c); |
|
| 155 | - |
|
| 156 | - return $err; |
|
| 78 | + if (($t = objet_type($objet)) !== $objet) { |
|
| 79 | + spip_log("objet_modifier: appel avec type $objet invalide au lieu de $t", 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 80 | + $objet = $t; |
|
| 81 | + } |
|
| 82 | + if ( |
|
| 83 | + include_spip('action/editer_' . $objet) |
|
| 84 | + and function_exists($modifier = $objet . '_modifier') |
|
| 85 | + ) { |
|
| 86 | + return $modifier($id, $set); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $table_sql = table_objet_sql($objet); |
|
| 90 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 91 | + $desc = $trouver_table($table_sql); |
|
| 92 | + if (!$desc or !isset($desc['field'])) { |
|
| 93 | + spip_log("Objet $objet inconnu dans objet_modifier", 'editer' . _LOG_ERREUR); |
|
| 94 | + |
|
| 95 | + return _L("Erreur objet $objet inconnu"); |
|
| 96 | + } |
|
| 97 | + include_spip('inc/modifier'); |
|
| 98 | + |
|
| 99 | + $champ_date = ''; |
|
| 100 | + if (isset($desc['date']) and $desc['date']) { |
|
| 101 | + $champ_date = $desc['date']; |
|
| 102 | + } elseif (isset($desc['field']['date'])) { |
|
| 103 | + $champ_date = 'date'; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $white = array_keys($desc['field']); |
|
| 107 | + // on ne traite pas la cle primaire par defaut, notamment car |
|
| 108 | + // sur une creation, id_x vaut 'oui', et serait enregistre en id_x=0 dans la base |
|
| 109 | + $white = array_diff($white, [$desc['key']['PRIMARY KEY']]); |
|
| 110 | + |
|
| 111 | + if (isset($desc['champs_editables']) and is_array($desc['champs_editables'])) { |
|
| 112 | + $white = $desc['champs_editables']; |
|
| 113 | + } |
|
| 114 | + $c = collecter_requests( |
|
| 115 | + // white list |
|
| 116 | + $white, |
|
| 117 | + // black list |
|
| 118 | + [$champ_date, 'statut', 'id_parent', 'id_secteur'], |
|
| 119 | + // donnees eventuellement fournies |
|
| 120 | + $set |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + // Si l'objet est publie, invalider les caches et demander sa reindexation |
|
| 124 | + if (objet_test_si_publie($objet, $id)) { |
|
| 125 | + $invalideur = "id='$objet/$id'"; |
|
| 126 | + $indexation = true; |
|
| 127 | + } else { |
|
| 128 | + $invalideur = ''; |
|
| 129 | + $indexation = false; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if ( |
|
| 133 | + $err = objet_modifier_champs( |
|
| 134 | + $objet, |
|
| 135 | + $id, |
|
| 136 | + [ |
|
| 137 | + 'data' => $set, |
|
| 138 | + 'nonvide' => '', |
|
| 139 | + 'invalideur' => $invalideur, |
|
| 140 | + 'indexation' => $indexation, |
|
| 141 | + // champ a mettre a date('Y-m-d H:i:s') s'il y a modif |
|
| 142 | + 'date_modif' => (isset($desc['field']['date_modif']) ? 'date_modif' : '') |
|
| 143 | + ], |
|
| 144 | + $c |
|
| 145 | + ) |
|
| 146 | + ) { |
|
| 147 | + return $err; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + // Modification de statut, changement de rubrique ? |
|
| 151 | + // FIXME: Ici lorsqu'un $set est passé, la fonction collecter_requests() retourne tout |
|
| 152 | + // le tableau $set hors black liste, mais du coup on a possiblement des champs en trop. |
|
| 153 | + $c = collecter_requests([$champ_date, 'statut', 'id_parent'], [], $set); |
|
| 154 | + $err = objet_instituer($objet, $id, $c); |
|
| 155 | + |
|
| 156 | + return $err; |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | /** |
@@ -168,134 +168,134 @@ discard block |
||
| 168 | 168 | * @return bool|int |
| 169 | 169 | */ |
| 170 | 170 | function objet_inserer($objet, $id_parent = null, $set = null) { |
| 171 | - if (($t = objet_type($objet)) !== $objet) { |
|
| 172 | - spip_log("objet_inserer: appel avec type $objet invalide au lieu de $t", 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 173 | - $objet = $t; |
|
| 174 | - } |
|
| 175 | - if ( |
|
| 176 | - include_spip('action/editer_' . $objet) |
|
| 177 | - and function_exists($inserer = $objet . '_inserer') |
|
| 178 | - ) { |
|
| 179 | - return $inserer($id_parent, $set); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - $table_sql = table_objet_sql($objet); |
|
| 183 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 184 | - $desc = $trouver_table($table_sql); |
|
| 185 | - if (!$desc or !isset($desc['field'])) { |
|
| 186 | - return 0; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $lang_rub = ''; |
|
| 190 | - $champs = []; |
|
| 191 | - if (isset($desc['field']['id_rubrique'])) { |
|
| 192 | - // Si id_rubrique vaut 0 ou n'est pas definie, creer l'objet |
|
| 193 | - // dans la premiere rubrique racine |
|
| 194 | - if (!$id_rubrique = intval($id_parent)) { |
|
| 195 | - $row = sql_fetsel('id_rubrique, id_secteur, lang', 'spip_rubriques', 'id_parent=0', '', '0+titre,titre', '1'); |
|
| 196 | - $id_rubrique = $row['id_rubrique']; |
|
| 197 | - } else { |
|
| 198 | - $row = sql_fetsel('lang, id_secteur', 'spip_rubriques', 'id_rubrique=' . intval($id_rubrique)); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $champs['id_rubrique'] = $id_rubrique; |
|
| 202 | - if (isset($desc['field']['id_secteur'])) { |
|
| 203 | - $champs['id_secteur'] = $row['id_secteur']; |
|
| 204 | - } |
|
| 205 | - $lang_rub = $row['lang']; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - // La langue a la creation : si les liens de traduction sont autorises |
|
| 209 | - // dans les rubriques, on essaie avec la langue de l'auteur, |
|
| 210 | - // ou a defaut celle de la rubrique |
|
| 211 | - // Sinon c'est la langue de la rubrique qui est choisie + heritee |
|
| 212 | - if ( |
|
| 213 | - isset($desc['field']['lang']) and !empty($GLOBALS['meta']['multi_objets']) and in_array( |
|
| 214 | - $table_sql, |
|
| 215 | - explode(',', $GLOBALS['meta']['multi_objets']) |
|
| 216 | - ) |
|
| 217 | - ) { |
|
| 218 | - lang_select($GLOBALS['visiteur_session']['lang']); |
|
| 219 | - if ( |
|
| 220 | - in_array( |
|
| 221 | - $GLOBALS['spip_lang'], |
|
| 222 | - explode(',', $GLOBALS['meta']['langues_multilingue']) |
|
| 223 | - ) |
|
| 224 | - ) { |
|
| 225 | - $champs['lang'] = $GLOBALS['spip_lang']; |
|
| 226 | - if (isset($desc['field']['langue_choisie'])) { |
|
| 227 | - $champs['langue_choisie'] = 'oui'; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - } elseif (isset($desc['field']['lang']) and isset($desc['field']['langue_choisie'])) { |
|
| 231 | - $champs['lang'] = ($lang_rub ? $lang_rub : $GLOBALS['meta']['langue_site']); |
|
| 232 | - $champs['langue_choisie'] = 'non'; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - if (isset($desc['field']['statut'])) { |
|
| 236 | - if (isset($desc['statut_textes_instituer'])) { |
|
| 237 | - $cles_statut = array_keys($desc['statut_textes_instituer']); |
|
| 238 | - $champs['statut'] = reset($cles_statut); |
|
| 239 | - } else { |
|
| 240 | - $champs['statut'] = 'prepa'; |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - if ((isset($desc['date']) and $d = $desc['date']) or isset($desc['field'][$d = 'date'])) { |
|
| 246 | - $champs[$d] = date('Y-m-d H:i:s'); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if ($set) { |
|
| 250 | - $champs = array_merge($champs, $set); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - // Envoyer aux plugins |
|
| 254 | - $champs = pipeline( |
|
| 255 | - 'pre_insertion', |
|
| 256 | - [ |
|
| 257 | - 'args' => [ |
|
| 258 | - 'table' => $table_sql, |
|
| 259 | - 'id_parent' => $id_parent, |
|
| 260 | - ], |
|
| 261 | - 'data' => $champs |
|
| 262 | - ] |
|
| 263 | - ); |
|
| 264 | - |
|
| 265 | - $id = sql_insertq($table_sql, $champs); |
|
| 266 | - |
|
| 267 | - if ($id) { |
|
| 268 | - // controler si le serveur n'a pas renvoye une erreur |
|
| 269 | - // et associer l'auteur sinon |
|
| 270 | - // si la table n'a pas deja un champ id_auteur |
|
| 271 | - // et si le form n'a pas poste un id_auteur (meme vide, ce qui sert a annuler cette auto association) |
|
| 272 | - if ( |
|
| 273 | - $id > 0 |
|
| 274 | - and !isset($desc['field']['id_auteur']) |
|
| 275 | - ) { |
|
| 276 | - $id_auteur = ((is_null(_request('id_auteur')) and isset($GLOBALS['visiteur_session']['id_auteur'])) ? |
|
| 277 | - $GLOBALS['visiteur_session']['id_auteur'] |
|
| 278 | - : _request('id_auteur')); |
|
| 279 | - if ($id_auteur) { |
|
| 280 | - include_spip('action/editer_auteur'); |
|
| 281 | - auteur_associer($id_auteur, [$objet => $id]); |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - pipeline( |
|
| 286 | - 'post_insertion', |
|
| 287 | - [ |
|
| 288 | - 'args' => [ |
|
| 289 | - 'table' => $table_sql, |
|
| 290 | - 'id_parent' => $id_parent, |
|
| 291 | - 'id_objet' => $id, |
|
| 292 | - ], |
|
| 293 | - 'data' => $champs |
|
| 294 | - ] |
|
| 295 | - ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - return $id; |
|
| 171 | + if (($t = objet_type($objet)) !== $objet) { |
|
| 172 | + spip_log("objet_inserer: appel avec type $objet invalide au lieu de $t", 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 173 | + $objet = $t; |
|
| 174 | + } |
|
| 175 | + if ( |
|
| 176 | + include_spip('action/editer_' . $objet) |
|
| 177 | + and function_exists($inserer = $objet . '_inserer') |
|
| 178 | + ) { |
|
| 179 | + return $inserer($id_parent, $set); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + $table_sql = table_objet_sql($objet); |
|
| 183 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 184 | + $desc = $trouver_table($table_sql); |
|
| 185 | + if (!$desc or !isset($desc['field'])) { |
|
| 186 | + return 0; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $lang_rub = ''; |
|
| 190 | + $champs = []; |
|
| 191 | + if (isset($desc['field']['id_rubrique'])) { |
|
| 192 | + // Si id_rubrique vaut 0 ou n'est pas definie, creer l'objet |
|
| 193 | + // dans la premiere rubrique racine |
|
| 194 | + if (!$id_rubrique = intval($id_parent)) { |
|
| 195 | + $row = sql_fetsel('id_rubrique, id_secteur, lang', 'spip_rubriques', 'id_parent=0', '', '0+titre,titre', '1'); |
|
| 196 | + $id_rubrique = $row['id_rubrique']; |
|
| 197 | + } else { |
|
| 198 | + $row = sql_fetsel('lang, id_secteur', 'spip_rubriques', 'id_rubrique=' . intval($id_rubrique)); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $champs['id_rubrique'] = $id_rubrique; |
|
| 202 | + if (isset($desc['field']['id_secteur'])) { |
|
| 203 | + $champs['id_secteur'] = $row['id_secteur']; |
|
| 204 | + } |
|
| 205 | + $lang_rub = $row['lang']; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + // La langue a la creation : si les liens de traduction sont autorises |
|
| 209 | + // dans les rubriques, on essaie avec la langue de l'auteur, |
|
| 210 | + // ou a defaut celle de la rubrique |
|
| 211 | + // Sinon c'est la langue de la rubrique qui est choisie + heritee |
|
| 212 | + if ( |
|
| 213 | + isset($desc['field']['lang']) and !empty($GLOBALS['meta']['multi_objets']) and in_array( |
|
| 214 | + $table_sql, |
|
| 215 | + explode(',', $GLOBALS['meta']['multi_objets']) |
|
| 216 | + ) |
|
| 217 | + ) { |
|
| 218 | + lang_select($GLOBALS['visiteur_session']['lang']); |
|
| 219 | + if ( |
|
| 220 | + in_array( |
|
| 221 | + $GLOBALS['spip_lang'], |
|
| 222 | + explode(',', $GLOBALS['meta']['langues_multilingue']) |
|
| 223 | + ) |
|
| 224 | + ) { |
|
| 225 | + $champs['lang'] = $GLOBALS['spip_lang']; |
|
| 226 | + if (isset($desc['field']['langue_choisie'])) { |
|
| 227 | + $champs['langue_choisie'] = 'oui'; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + } elseif (isset($desc['field']['lang']) and isset($desc['field']['langue_choisie'])) { |
|
| 231 | + $champs['lang'] = ($lang_rub ? $lang_rub : $GLOBALS['meta']['langue_site']); |
|
| 232 | + $champs['langue_choisie'] = 'non'; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + if (isset($desc['field']['statut'])) { |
|
| 236 | + if (isset($desc['statut_textes_instituer'])) { |
|
| 237 | + $cles_statut = array_keys($desc['statut_textes_instituer']); |
|
| 238 | + $champs['statut'] = reset($cles_statut); |
|
| 239 | + } else { |
|
| 240 | + $champs['statut'] = 'prepa'; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + if ((isset($desc['date']) and $d = $desc['date']) or isset($desc['field'][$d = 'date'])) { |
|
| 246 | + $champs[$d] = date('Y-m-d H:i:s'); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if ($set) { |
|
| 250 | + $champs = array_merge($champs, $set); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + // Envoyer aux plugins |
|
| 254 | + $champs = pipeline( |
|
| 255 | + 'pre_insertion', |
|
| 256 | + [ |
|
| 257 | + 'args' => [ |
|
| 258 | + 'table' => $table_sql, |
|
| 259 | + 'id_parent' => $id_parent, |
|
| 260 | + ], |
|
| 261 | + 'data' => $champs |
|
| 262 | + ] |
|
| 263 | + ); |
|
| 264 | + |
|
| 265 | + $id = sql_insertq($table_sql, $champs); |
|
| 266 | + |
|
| 267 | + if ($id) { |
|
| 268 | + // controler si le serveur n'a pas renvoye une erreur |
|
| 269 | + // et associer l'auteur sinon |
|
| 270 | + // si la table n'a pas deja un champ id_auteur |
|
| 271 | + // et si le form n'a pas poste un id_auteur (meme vide, ce qui sert a annuler cette auto association) |
|
| 272 | + if ( |
|
| 273 | + $id > 0 |
|
| 274 | + and !isset($desc['field']['id_auteur']) |
|
| 275 | + ) { |
|
| 276 | + $id_auteur = ((is_null(_request('id_auteur')) and isset($GLOBALS['visiteur_session']['id_auteur'])) ? |
|
| 277 | + $GLOBALS['visiteur_session']['id_auteur'] |
|
| 278 | + : _request('id_auteur')); |
|
| 279 | + if ($id_auteur) { |
|
| 280 | + include_spip('action/editer_auteur'); |
|
| 281 | + auteur_associer($id_auteur, [$objet => $id]); |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + pipeline( |
|
| 286 | + 'post_insertion', |
|
| 287 | + [ |
|
| 288 | + 'args' => [ |
|
| 289 | + 'table' => $table_sql, |
|
| 290 | + 'id_parent' => $id_parent, |
|
| 291 | + 'id_objet' => $id, |
|
| 292 | + ], |
|
| 293 | + 'data' => $champs |
|
| 294 | + ] |
|
| 295 | + ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + return $id; |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | |
@@ -312,138 +312,138 @@ discard block |
||
| 312 | 312 | * @return string |
| 313 | 313 | */ |
| 314 | 314 | function objet_instituer($objet, $id, $c, $calcul_rub = true) { |
| 315 | - if (($t = objet_type($objet)) !== $objet) { |
|
| 316 | - spip_log("objet_instituer: appel avec type $objet invalide au lieu de $t", 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 317 | - $objet = $t; |
|
| 318 | - } |
|
| 319 | - if ( |
|
| 320 | - include_spip('action/editer_' . $objet) |
|
| 321 | - and function_exists($instituer = $objet . '_instituer') |
|
| 322 | - ) { |
|
| 323 | - return $instituer($id, $c, $calcul_rub); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - $table_sql = table_objet_sql($objet); |
|
| 327 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 328 | - $desc = $trouver_table($table_sql); |
|
| 329 | - if (!$desc or !isset($desc['field'])) { |
|
| 330 | - return _L("Impossible d'instituer $objet : non connu en base"); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - include_spip('inc/autoriser'); |
|
| 334 | - include_spip('inc/rubriques'); |
|
| 335 | - include_spip('inc/modifier'); |
|
| 336 | - |
|
| 337 | - $sel = []; |
|
| 338 | - $sel[] = (isset($desc['field']['statut']) ? 'statut' : "'' as statut"); |
|
| 339 | - |
|
| 340 | - $champ_date = ''; |
|
| 341 | - if (isset($desc['date']) and $desc['date']) { |
|
| 342 | - $champ_date = $desc['date']; |
|
| 343 | - } elseif (isset($desc['field']['date'])) { |
|
| 344 | - $champ_date = 'date'; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - $sel[] = ($champ_date ? "$champ_date as date" : "'' as date"); |
|
| 348 | - $sel[] = (isset($desc['field']['id_rubrique']) ? 'id_rubrique' : '0 as id_rubrique'); |
|
| 349 | - |
|
| 350 | - $row = sql_fetsel($sel, $table_sql, id_table_objet($objet) . '=' . intval($id)); |
|
| 351 | - |
|
| 352 | - $id_rubrique = $row['id_rubrique']; |
|
| 353 | - $statut_ancien = $statut = $row['statut']; |
|
| 354 | - $date_ancienne = $date = $row['date']; |
|
| 355 | - $champs = []; |
|
| 356 | - |
|
| 357 | - $d = ($date and isset($c[$champ_date])) ? $c[$champ_date] : null; |
|
| 358 | - $s = (isset($desc['field']['statut']) and isset($c['statut'])) ? $c['statut'] : $statut; |
|
| 359 | - |
|
| 360 | - // cf autorisations dans inc/instituer_objet |
|
| 361 | - if ($s != $statut or ($d and $d != $date)) { |
|
| 362 | - if ( |
|
| 363 | - $id_rubrique ? |
|
| 364 | - autoriser('publierdans', 'rubrique', $id_rubrique) |
|
| 365 | - : |
|
| 366 | - autoriser('instituer', $objet, $id, null, ['statut' => $s]) |
|
| 367 | - ) { |
|
| 368 | - $statut = $champs['statut'] = $s; |
|
| 369 | - } else { |
|
| 370 | - if ($s != 'publie' and autoriser('modifier', $objet, $id)) { |
|
| 371 | - $statut = $champs['statut'] = $s; |
|
| 372 | - } else { |
|
| 373 | - spip_log("editer_objet $objet #$id refus " . json_encode($c), 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - // En cas de publication, fixer la date a "maintenant" |
|
| 378 | - // sauf si $c commande autre chose |
|
| 379 | - // ou si l'objet est deja date dans le futur |
|
| 380 | - // En cas de proposition d'un objet (mais pas depublication), idem |
|
| 381 | - if ($champ_date) { |
|
| 382 | - if ( |
|
| 383 | - $champs['statut'] == 'publie' |
|
| 384 | - or ($champs['statut'] == 'prop' and !in_array($statut_ancien, ['publie', 'prop'])) |
|
| 385 | - or $d |
|
| 386 | - ) { |
|
| 387 | - if ($d or strtotime($d = $date) > time()) { |
|
| 388 | - $champs[$champ_date] = $date = $d; |
|
| 389 | - } else { |
|
| 390 | - $champs[$champ_date] = $date = date('Y-m-d H:i:s'); |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - } |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - // Verifier que la rubrique demandee existe et est differente |
|
| 397 | - // de la rubrique actuelle |
|
| 398 | - if ( |
|
| 399 | - $id_rubrique |
|
| 400 | - and isset($c['id_parent']) |
|
| 401 | - and $id_parent = $c['id_parent'] |
|
| 402 | - and $id_parent != $id_rubrique |
|
| 403 | - and (sql_fetsel('1', 'spip_rubriques', 'id_rubrique=' . intval($id_parent))) |
|
| 404 | - ) { |
|
| 405 | - $champs['id_rubrique'] = $id_parent; |
|
| 406 | - |
|
| 407 | - // si l'objet etait publie |
|
| 408 | - // et que le demandeur n'est pas admin de la rubrique |
|
| 409 | - // repasser l'objet en statut 'propose'. |
|
| 410 | - if ( |
|
| 411 | - $statut == 'publie' |
|
| 412 | - and !autoriser('publierdans', 'rubrique', $id_rubrique) |
|
| 413 | - ) { |
|
| 414 | - $champs['statut'] = 'prop'; |
|
| 415 | - } |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - |
|
| 419 | - // Envoyer aux plugins |
|
| 420 | - $champs = pipeline( |
|
| 421 | - 'pre_edition', |
|
| 422 | - [ |
|
| 423 | - 'args' => [ |
|
| 424 | - 'table' => $table_sql, |
|
| 425 | - 'id_objet' => $id, |
|
| 426 | - 'action' => 'instituer', |
|
| 427 | - 'statut_ancien' => $statut_ancien, |
|
| 428 | - 'date_ancienne' => $date_ancienne, |
|
| 429 | - 'id_parent_ancien' => $id_rubrique, |
|
| 430 | - ], |
|
| 431 | - 'data' => $champs |
|
| 432 | - ] |
|
| 433 | - ); |
|
| 434 | - |
|
| 435 | - if (!count($champs)) { |
|
| 436 | - return ''; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - // Envoyer les modifs. |
|
| 440 | - objet_editer_heritage($objet, $id, $id_rubrique, $statut_ancien, $champs, $calcul_rub); |
|
| 441 | - |
|
| 442 | - // Invalider les caches |
|
| 443 | - include_spip('inc/invalideur'); |
|
| 444 | - suivre_invalideur("id='$objet/$id'"); |
|
| 445 | - |
|
| 446 | - /* |
|
| 315 | + if (($t = objet_type($objet)) !== $objet) { |
|
| 316 | + spip_log("objet_instituer: appel avec type $objet invalide au lieu de $t", 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 317 | + $objet = $t; |
|
| 318 | + } |
|
| 319 | + if ( |
|
| 320 | + include_spip('action/editer_' . $objet) |
|
| 321 | + and function_exists($instituer = $objet . '_instituer') |
|
| 322 | + ) { |
|
| 323 | + return $instituer($id, $c, $calcul_rub); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + $table_sql = table_objet_sql($objet); |
|
| 327 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 328 | + $desc = $trouver_table($table_sql); |
|
| 329 | + if (!$desc or !isset($desc['field'])) { |
|
| 330 | + return _L("Impossible d'instituer $objet : non connu en base"); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + include_spip('inc/autoriser'); |
|
| 334 | + include_spip('inc/rubriques'); |
|
| 335 | + include_spip('inc/modifier'); |
|
| 336 | + |
|
| 337 | + $sel = []; |
|
| 338 | + $sel[] = (isset($desc['field']['statut']) ? 'statut' : "'' as statut"); |
|
| 339 | + |
|
| 340 | + $champ_date = ''; |
|
| 341 | + if (isset($desc['date']) and $desc['date']) { |
|
| 342 | + $champ_date = $desc['date']; |
|
| 343 | + } elseif (isset($desc['field']['date'])) { |
|
| 344 | + $champ_date = 'date'; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + $sel[] = ($champ_date ? "$champ_date as date" : "'' as date"); |
|
| 348 | + $sel[] = (isset($desc['field']['id_rubrique']) ? 'id_rubrique' : '0 as id_rubrique'); |
|
| 349 | + |
|
| 350 | + $row = sql_fetsel($sel, $table_sql, id_table_objet($objet) . '=' . intval($id)); |
|
| 351 | + |
|
| 352 | + $id_rubrique = $row['id_rubrique']; |
|
| 353 | + $statut_ancien = $statut = $row['statut']; |
|
| 354 | + $date_ancienne = $date = $row['date']; |
|
| 355 | + $champs = []; |
|
| 356 | + |
|
| 357 | + $d = ($date and isset($c[$champ_date])) ? $c[$champ_date] : null; |
|
| 358 | + $s = (isset($desc['field']['statut']) and isset($c['statut'])) ? $c['statut'] : $statut; |
|
| 359 | + |
|
| 360 | + // cf autorisations dans inc/instituer_objet |
|
| 361 | + if ($s != $statut or ($d and $d != $date)) { |
|
| 362 | + if ( |
|
| 363 | + $id_rubrique ? |
|
| 364 | + autoriser('publierdans', 'rubrique', $id_rubrique) |
|
| 365 | + : |
|
| 366 | + autoriser('instituer', $objet, $id, null, ['statut' => $s]) |
|
| 367 | + ) { |
|
| 368 | + $statut = $champs['statut'] = $s; |
|
| 369 | + } else { |
|
| 370 | + if ($s != 'publie' and autoriser('modifier', $objet, $id)) { |
|
| 371 | + $statut = $champs['statut'] = $s; |
|
| 372 | + } else { |
|
| 373 | + spip_log("editer_objet $objet #$id refus " . json_encode($c), 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + // En cas de publication, fixer la date a "maintenant" |
|
| 378 | + // sauf si $c commande autre chose |
|
| 379 | + // ou si l'objet est deja date dans le futur |
|
| 380 | + // En cas de proposition d'un objet (mais pas depublication), idem |
|
| 381 | + if ($champ_date) { |
|
| 382 | + if ( |
|
| 383 | + $champs['statut'] == 'publie' |
|
| 384 | + or ($champs['statut'] == 'prop' and !in_array($statut_ancien, ['publie', 'prop'])) |
|
| 385 | + or $d |
|
| 386 | + ) { |
|
| 387 | + if ($d or strtotime($d = $date) > time()) { |
|
| 388 | + $champs[$champ_date] = $date = $d; |
|
| 389 | + } else { |
|
| 390 | + $champs[$champ_date] = $date = date('Y-m-d H:i:s'); |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + } |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + // Verifier que la rubrique demandee existe et est differente |
|
| 397 | + // de la rubrique actuelle |
|
| 398 | + if ( |
|
| 399 | + $id_rubrique |
|
| 400 | + and isset($c['id_parent']) |
|
| 401 | + and $id_parent = $c['id_parent'] |
|
| 402 | + and $id_parent != $id_rubrique |
|
| 403 | + and (sql_fetsel('1', 'spip_rubriques', 'id_rubrique=' . intval($id_parent))) |
|
| 404 | + ) { |
|
| 405 | + $champs['id_rubrique'] = $id_parent; |
|
| 406 | + |
|
| 407 | + // si l'objet etait publie |
|
| 408 | + // et que le demandeur n'est pas admin de la rubrique |
|
| 409 | + // repasser l'objet en statut 'propose'. |
|
| 410 | + if ( |
|
| 411 | + $statut == 'publie' |
|
| 412 | + and !autoriser('publierdans', 'rubrique', $id_rubrique) |
|
| 413 | + ) { |
|
| 414 | + $champs['statut'] = 'prop'; |
|
| 415 | + } |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + |
|
| 419 | + // Envoyer aux plugins |
|
| 420 | + $champs = pipeline( |
|
| 421 | + 'pre_edition', |
|
| 422 | + [ |
|
| 423 | + 'args' => [ |
|
| 424 | + 'table' => $table_sql, |
|
| 425 | + 'id_objet' => $id, |
|
| 426 | + 'action' => 'instituer', |
|
| 427 | + 'statut_ancien' => $statut_ancien, |
|
| 428 | + 'date_ancienne' => $date_ancienne, |
|
| 429 | + 'id_parent_ancien' => $id_rubrique, |
|
| 430 | + ], |
|
| 431 | + 'data' => $champs |
|
| 432 | + ] |
|
| 433 | + ); |
|
| 434 | + |
|
| 435 | + if (!count($champs)) { |
|
| 436 | + return ''; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + // Envoyer les modifs. |
|
| 440 | + objet_editer_heritage($objet, $id, $id_rubrique, $statut_ancien, $champs, $calcul_rub); |
|
| 441 | + |
|
| 442 | + // Invalider les caches |
|
| 443 | + include_spip('inc/invalideur'); |
|
| 444 | + suivre_invalideur("id='$objet/$id'"); |
|
| 445 | + |
|
| 446 | + /* |
|
| 447 | 447 | if ($date) { |
| 448 | 448 | $t = strtotime($date); |
| 449 | 449 | $p = @$GLOBALS['meta']['date_prochain_postdate']; |
@@ -452,32 +452,32 @@ discard block |
||
| 452 | 452 | } |
| 453 | 453 | }*/ |
| 454 | 454 | |
| 455 | - // Pipeline |
|
| 456 | - pipeline( |
|
| 457 | - 'post_edition', |
|
| 458 | - [ |
|
| 459 | - 'args' => [ |
|
| 460 | - 'table' => $table_sql, |
|
| 461 | - 'id_objet' => $id, |
|
| 462 | - 'action' => 'instituer', |
|
| 463 | - 'statut_ancien' => $statut_ancien, |
|
| 464 | - 'date_ancienne' => $date_ancienne, |
|
| 465 | - 'id_parent_ancien' => $id_rubrique, |
|
| 466 | - ], |
|
| 467 | - 'data' => $champs |
|
| 468 | - ] |
|
| 469 | - ); |
|
| 470 | - |
|
| 471 | - // Notifications |
|
| 472 | - if ($notifications = charger_fonction('notifications', 'inc')) { |
|
| 473 | - $notifications( |
|
| 474 | - "instituer$objet", |
|
| 475 | - $id, |
|
| 476 | - ['statut' => $statut, 'statut_ancien' => $statut_ancien, 'date' => $date, 'date_ancienne' => $date_ancienne] |
|
| 477 | - ); |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - return ''; // pas d'erreur |
|
| 455 | + // Pipeline |
|
| 456 | + pipeline( |
|
| 457 | + 'post_edition', |
|
| 458 | + [ |
|
| 459 | + 'args' => [ |
|
| 460 | + 'table' => $table_sql, |
|
| 461 | + 'id_objet' => $id, |
|
| 462 | + 'action' => 'instituer', |
|
| 463 | + 'statut_ancien' => $statut_ancien, |
|
| 464 | + 'date_ancienne' => $date_ancienne, |
|
| 465 | + 'id_parent_ancien' => $id_rubrique, |
|
| 466 | + ], |
|
| 467 | + 'data' => $champs |
|
| 468 | + ] |
|
| 469 | + ); |
|
| 470 | + |
|
| 471 | + // Notifications |
|
| 472 | + if ($notifications = charger_fonction('notifications', 'inc')) { |
|
| 473 | + $notifications( |
|
| 474 | + "instituer$objet", |
|
| 475 | + $id, |
|
| 476 | + ['statut' => $statut, 'statut_ancien' => $statut_ancien, 'date' => $date, 'date_ancienne' => $date_ancienne] |
|
| 477 | + ); |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + return ''; // pas d'erreur |
|
| 481 | 481 | } |
| 482 | 482 | |
| 483 | 483 | /** |
@@ -492,51 +492,51 @@ discard block |
||
| 492 | 492 | * @return void |
| 493 | 493 | */ |
| 494 | 494 | function objet_editer_heritage($objet, $id, $id_rubrique, $statut, $champs, $cond = true) { |
| 495 | - $table_sql = table_objet_sql($objet); |
|
| 496 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 497 | - $desc = $trouver_table($table_sql); |
|
| 498 | - |
|
| 499 | - // Si on deplace l'objet |
|
| 500 | - // changer aussi son secteur et sa langue (si heritee) |
|
| 501 | - if (isset($champs['id_rubrique'])) { |
|
| 502 | - $row_rub = sql_fetsel('id_secteur, lang', 'spip_rubriques', 'id_rubrique=' . sql_quote($champs['id_rubrique'])); |
|
| 503 | - $langue = $row_rub['lang']; |
|
| 504 | - |
|
| 505 | - if (isset($desc['field']['id_secteur'])) { |
|
| 506 | - $champs['id_secteur'] = $row_rub['id_secteur']; |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - if (isset($desc['field']['lang']) and isset($desc['field']['langue_choisie'])) { |
|
| 510 | - if ( |
|
| 511 | - sql_fetsel( |
|
| 512 | - '1', |
|
| 513 | - $table_sql, |
|
| 514 | - id_table_objet($objet) . '=' . intval($id) . " AND langue_choisie<>'oui' AND lang<>" . sql_quote($langue) |
|
| 515 | - ) |
|
| 516 | - ) { |
|
| 517 | - $champs['lang'] = $langue; |
|
| 518 | - } |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - if (!$champs) { |
|
| 523 | - return; |
|
| 524 | - } |
|
| 525 | - sql_updateq($table_sql, $champs, id_table_objet($objet) . '=' . intval($id)); |
|
| 526 | - |
|
| 527 | - // Changer le statut des rubriques concernees |
|
| 528 | - if ($cond) { |
|
| 529 | - include_spip('inc/rubriques'); |
|
| 530 | - //$postdate = ($GLOBALS['meta']["post_dates"] == "non" AND isset($champs['date']) AND (strtotime($champs['date']) < time()))?$champs['date']:false; |
|
| 531 | - $postdate = false; |
|
| 532 | - // On rajoute les infos de l'objet |
|
| 533 | - $infos = [ |
|
| 534 | - 'objet' => $objet, |
|
| 535 | - 'id_objet' => $id, |
|
| 536 | - 'statut_ancien' => $statut, |
|
| 537 | - ]; |
|
| 538 | - calculer_rubriques_if($id_rubrique, $champs, $infos, $postdate); |
|
| 539 | - } |
|
| 495 | + $table_sql = table_objet_sql($objet); |
|
| 496 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 497 | + $desc = $trouver_table($table_sql); |
|
| 498 | + |
|
| 499 | + // Si on deplace l'objet |
|
| 500 | + // changer aussi son secteur et sa langue (si heritee) |
|
| 501 | + if (isset($champs['id_rubrique'])) { |
|
| 502 | + $row_rub = sql_fetsel('id_secteur, lang', 'spip_rubriques', 'id_rubrique=' . sql_quote($champs['id_rubrique'])); |
|
| 503 | + $langue = $row_rub['lang']; |
|
| 504 | + |
|
| 505 | + if (isset($desc['field']['id_secteur'])) { |
|
| 506 | + $champs['id_secteur'] = $row_rub['id_secteur']; |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + if (isset($desc['field']['lang']) and isset($desc['field']['langue_choisie'])) { |
|
| 510 | + if ( |
|
| 511 | + sql_fetsel( |
|
| 512 | + '1', |
|
| 513 | + $table_sql, |
|
| 514 | + id_table_objet($objet) . '=' . intval($id) . " AND langue_choisie<>'oui' AND lang<>" . sql_quote($langue) |
|
| 515 | + ) |
|
| 516 | + ) { |
|
| 517 | + $champs['lang'] = $langue; |
|
| 518 | + } |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + if (!$champs) { |
|
| 523 | + return; |
|
| 524 | + } |
|
| 525 | + sql_updateq($table_sql, $champs, id_table_objet($objet) . '=' . intval($id)); |
|
| 526 | + |
|
| 527 | + // Changer le statut des rubriques concernees |
|
| 528 | + if ($cond) { |
|
| 529 | + include_spip('inc/rubriques'); |
|
| 530 | + //$postdate = ($GLOBALS['meta']["post_dates"] == "non" AND isset($champs['date']) AND (strtotime($champs['date']) < time()))?$champs['date']:false; |
|
| 531 | + $postdate = false; |
|
| 532 | + // On rajoute les infos de l'objet |
|
| 533 | + $infos = [ |
|
| 534 | + 'objet' => $objet, |
|
| 535 | + 'id_objet' => $id, |
|
| 536 | + 'statut_ancien' => $statut, |
|
| 537 | + ]; |
|
| 538 | + calculer_rubriques_if($id_rubrique, $champs, $infos, $postdate); |
|
| 539 | + } |
|
| 540 | 540 | } |
| 541 | 541 | |
| 542 | 542 | |
@@ -565,75 +565,75 @@ discard block |
||
| 565 | 565 | * string|int : valeur du champ demande pour l'objet demande |
| 566 | 566 | */ |
| 567 | 567 | function objet_lire($objet, $valeur_id, $options = []) { |
| 568 | - if (($t = objet_type($objet)) !== $objet) { |
|
| 569 | - spip_log("objet_lire: appel avec type $objet invalide au lieu de $t", 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 570 | - $objet = $t; |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - // tableau du cache des descriptions et des id d'objet (au sens id_xxx). |
|
| 574 | - // Les tableaux sont toujours indexés par le trio [objet][cle][valeur_cle] |
|
| 575 | - static $descriptions = []; |
|
| 576 | - |
|
| 577 | - // On détermine le nom du champ id de la table. |
|
| 578 | - include_spip('base/objets'); |
|
| 579 | - $primary = id_table_objet($objet); |
|
| 580 | - |
|
| 581 | - // On détermine l'id à utiliser. |
|
| 582 | - $champ_id = (!empty($options['champ_id']) ? $options['champ_id'] : $primary); |
|
| 583 | - |
|
| 584 | - // Si l'objet n'a pas encore été stocké, il faut récupérer sa description complète. |
|
| 585 | - if ( |
|
| 586 | - !isset($descriptions[$objet][$champ_id][$valeur_id]) |
|
| 587 | - or (isset($options['force']) and $options['force']) |
|
| 588 | - ) { |
|
| 589 | - // Il est possible pour un type d'objet de fournir une fonction de lecture de tous les champs d'un objet. |
|
| 590 | - if ( |
|
| 591 | - include_spip('action/editer_' . $objet) |
|
| 592 | - and function_exists($lire = "${objet}_lire_champs") |
|
| 593 | - ) { |
|
| 594 | - $valeurs = $lire($objet, $valeur_id, $champ_id); |
|
| 595 | - } else { |
|
| 596 | - // On récupère la table SQL à partir du type d'objet. |
|
| 597 | - $table = table_objet_sql($objet); |
|
| 598 | - |
|
| 599 | - // La condition est appliquée sur le champ désigné par l'utilisateur. |
|
| 600 | - $where = [ |
|
| 601 | - "${champ_id}=" . sql_quote($valeur_id) |
|
| 602 | - ]; |
|
| 603 | - |
|
| 604 | - // Acquisition de tous les champs de l'objet : si l'accès SQL retourne une erreur on renvoie un tableau vide. |
|
| 605 | - $valeurs = sql_fetsel('*', $table, $where); |
|
| 606 | - } |
|
| 607 | - |
|
| 608 | - if (!$valeurs) { |
|
| 609 | - $valeurs = false; |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - $descriptions[$objet][$champ_id][$valeur_id] = $valeurs; |
|
| 613 | - |
|
| 614 | - if ($champ_id !== $primary and isset($valeurs[$primary])) { |
|
| 615 | - $descriptions[$objet][$primary][$valeurs[$primary]] = $valeurs; |
|
| 616 | - $descriptions[$objet][$champ_id][$valeur_id] = &$descriptions[$objet][$primary][$valeurs[$primary]]; |
|
| 617 | - } |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - $retour = $descriptions[$objet][$champ_id][$valeur_id]; |
|
| 621 | - |
|
| 622 | - // On ne retourne maintenant que les champs demandés. |
|
| 623 | - // - on détermine les informations à renvoyer. |
|
| 624 | - if ($retour and !empty($options['champs'])) { |
|
| 625 | - $champs = $options['champs']; |
|
| 626 | - // Extraction des seules informations demandées. |
|
| 627 | - // -- si on demande une information unique on renvoie la valeur simple, sinon on renvoie un tableau. |
|
| 628 | - // -- si une information n'est pas un champ valide elle n'est pas renvoyée sans renvoyer d'erreur. |
|
| 629 | - if (is_array($champs)) { |
|
| 630 | - // Tableau des informations valides |
|
| 631 | - $retour = array_intersect_key($retour, array_flip($champs)); |
|
| 632 | - } else { |
|
| 633 | - // Valeur unique demandée. |
|
| 634 | - $retour = (isset($retour[$champs]) ? $retour[$champs] : false); |
|
| 635 | - } |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - return $retour; |
|
| 568 | + if (($t = objet_type($objet)) !== $objet) { |
|
| 569 | + spip_log("objet_lire: appel avec type $objet invalide au lieu de $t", 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 570 | + $objet = $t; |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + // tableau du cache des descriptions et des id d'objet (au sens id_xxx). |
|
| 574 | + // Les tableaux sont toujours indexés par le trio [objet][cle][valeur_cle] |
|
| 575 | + static $descriptions = []; |
|
| 576 | + |
|
| 577 | + // On détermine le nom du champ id de la table. |
|
| 578 | + include_spip('base/objets'); |
|
| 579 | + $primary = id_table_objet($objet); |
|
| 580 | + |
|
| 581 | + // On détermine l'id à utiliser. |
|
| 582 | + $champ_id = (!empty($options['champ_id']) ? $options['champ_id'] : $primary); |
|
| 583 | + |
|
| 584 | + // Si l'objet n'a pas encore été stocké, il faut récupérer sa description complète. |
|
| 585 | + if ( |
|
| 586 | + !isset($descriptions[$objet][$champ_id][$valeur_id]) |
|
| 587 | + or (isset($options['force']) and $options['force']) |
|
| 588 | + ) { |
|
| 589 | + // Il est possible pour un type d'objet de fournir une fonction de lecture de tous les champs d'un objet. |
|
| 590 | + if ( |
|
| 591 | + include_spip('action/editer_' . $objet) |
|
| 592 | + and function_exists($lire = "${objet}_lire_champs") |
|
| 593 | + ) { |
|
| 594 | + $valeurs = $lire($objet, $valeur_id, $champ_id); |
|
| 595 | + } else { |
|
| 596 | + // On récupère la table SQL à partir du type d'objet. |
|
| 597 | + $table = table_objet_sql($objet); |
|
| 598 | + |
|
| 599 | + // La condition est appliquée sur le champ désigné par l'utilisateur. |
|
| 600 | + $where = [ |
|
| 601 | + "${champ_id}=" . sql_quote($valeur_id) |
|
| 602 | + ]; |
|
| 603 | + |
|
| 604 | + // Acquisition de tous les champs de l'objet : si l'accès SQL retourne une erreur on renvoie un tableau vide. |
|
| 605 | + $valeurs = sql_fetsel('*', $table, $where); |
|
| 606 | + } |
|
| 607 | + |
|
| 608 | + if (!$valeurs) { |
|
| 609 | + $valeurs = false; |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + $descriptions[$objet][$champ_id][$valeur_id] = $valeurs; |
|
| 613 | + |
|
| 614 | + if ($champ_id !== $primary and isset($valeurs[$primary])) { |
|
| 615 | + $descriptions[$objet][$primary][$valeurs[$primary]] = $valeurs; |
|
| 616 | + $descriptions[$objet][$champ_id][$valeur_id] = &$descriptions[$objet][$primary][$valeurs[$primary]]; |
|
| 617 | + } |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + $retour = $descriptions[$objet][$champ_id][$valeur_id]; |
|
| 621 | + |
|
| 622 | + // On ne retourne maintenant que les champs demandés. |
|
| 623 | + // - on détermine les informations à renvoyer. |
|
| 624 | + if ($retour and !empty($options['champs'])) { |
|
| 625 | + $champs = $options['champs']; |
|
| 626 | + // Extraction des seules informations demandées. |
|
| 627 | + // -- si on demande une information unique on renvoie la valeur simple, sinon on renvoie un tableau. |
|
| 628 | + // -- si une information n'est pas un champ valide elle n'est pas renvoyée sans renvoyer d'erreur. |
|
| 629 | + if (is_array($champs)) { |
|
| 630 | + // Tableau des informations valides |
|
| 631 | + $retour = array_intersect_key($retour, array_flip($champs)); |
|
| 632 | + } else { |
|
| 633 | + // Valeur unique demandée. |
|
| 634 | + $retour = (isset($retour[$champs]) ? $retour[$champs] : false); |
|
| 635 | + } |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + return $retour; |
|
| 639 | 639 | } |
@@ -76,12 +76,12 @@ discard block |
||
| 76 | 76 | */ |
| 77 | 77 | function objet_modifier($objet, $id, $set = null) { |
| 78 | 78 | if (($t = objet_type($objet)) !== $objet) { |
| 79 | - spip_log("objet_modifier: appel avec type $objet invalide au lieu de $t", 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 79 | + spip_log("objet_modifier: appel avec type $objet invalide au lieu de $t", 'editer'._LOG_INFO_IMPORTANTE); |
|
| 80 | 80 | $objet = $t; |
| 81 | 81 | } |
| 82 | 82 | if ( |
| 83 | - include_spip('action/editer_' . $objet) |
|
| 84 | - and function_exists($modifier = $objet . '_modifier') |
|
| 83 | + include_spip('action/editer_'.$objet) |
|
| 84 | + and function_exists($modifier = $objet.'_modifier') |
|
| 85 | 85 | ) { |
| 86 | 86 | return $modifier($id, $set); |
| 87 | 87 | } |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | $trouver_table = charger_fonction('trouver_table', 'base'); |
| 91 | 91 | $desc = $trouver_table($table_sql); |
| 92 | 92 | if (!$desc or !isset($desc['field'])) { |
| 93 | - spip_log("Objet $objet inconnu dans objet_modifier", 'editer' . _LOG_ERREUR); |
|
| 93 | + spip_log("Objet $objet inconnu dans objet_modifier", 'editer'._LOG_ERREUR); |
|
| 94 | 94 | |
| 95 | 95 | return _L("Erreur objet $objet inconnu"); |
| 96 | 96 | } |
@@ -169,12 +169,12 @@ discard block |
||
| 169 | 169 | */ |
| 170 | 170 | function objet_inserer($objet, $id_parent = null, $set = null) { |
| 171 | 171 | if (($t = objet_type($objet)) !== $objet) { |
| 172 | - spip_log("objet_inserer: appel avec type $objet invalide au lieu de $t", 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 172 | + spip_log("objet_inserer: appel avec type $objet invalide au lieu de $t", 'editer'._LOG_INFO_IMPORTANTE); |
|
| 173 | 173 | $objet = $t; |
| 174 | 174 | } |
| 175 | 175 | if ( |
| 176 | - include_spip('action/editer_' . $objet) |
|
| 177 | - and function_exists($inserer = $objet . '_inserer') |
|
| 176 | + include_spip('action/editer_'.$objet) |
|
| 177 | + and function_exists($inserer = $objet.'_inserer') |
|
| 178 | 178 | ) { |
| 179 | 179 | return $inserer($id_parent, $set); |
| 180 | 180 | } |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | $row = sql_fetsel('id_rubrique, id_secteur, lang', 'spip_rubriques', 'id_parent=0', '', '0+titre,titre', '1'); |
| 196 | 196 | $id_rubrique = $row['id_rubrique']; |
| 197 | 197 | } else { |
| 198 | - $row = sql_fetsel('lang, id_secteur', 'spip_rubriques', 'id_rubrique=' . intval($id_rubrique)); |
|
| 198 | + $row = sql_fetsel('lang, id_secteur', 'spip_rubriques', 'id_rubrique='.intval($id_rubrique)); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | $champs['id_rubrique'] = $id_rubrique; |
@@ -313,12 +313,12 @@ discard block |
||
| 313 | 313 | */ |
| 314 | 314 | function objet_instituer($objet, $id, $c, $calcul_rub = true) { |
| 315 | 315 | if (($t = objet_type($objet)) !== $objet) { |
| 316 | - spip_log("objet_instituer: appel avec type $objet invalide au lieu de $t", 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 316 | + spip_log("objet_instituer: appel avec type $objet invalide au lieu de $t", 'editer'._LOG_INFO_IMPORTANTE); |
|
| 317 | 317 | $objet = $t; |
| 318 | 318 | } |
| 319 | 319 | if ( |
| 320 | - include_spip('action/editer_' . $objet) |
|
| 321 | - and function_exists($instituer = $objet . '_instituer') |
|
| 320 | + include_spip('action/editer_'.$objet) |
|
| 321 | + and function_exists($instituer = $objet.'_instituer') |
|
| 322 | 322 | ) { |
| 323 | 323 | return $instituer($id, $c, $calcul_rub); |
| 324 | 324 | } |
@@ -347,7 +347,7 @@ discard block |
||
| 347 | 347 | $sel[] = ($champ_date ? "$champ_date as date" : "'' as date"); |
| 348 | 348 | $sel[] = (isset($desc['field']['id_rubrique']) ? 'id_rubrique' : '0 as id_rubrique'); |
| 349 | 349 | |
| 350 | - $row = sql_fetsel($sel, $table_sql, id_table_objet($objet) . '=' . intval($id)); |
|
| 350 | + $row = sql_fetsel($sel, $table_sql, id_table_objet($objet).'='.intval($id)); |
|
| 351 | 351 | |
| 352 | 352 | $id_rubrique = $row['id_rubrique']; |
| 353 | 353 | $statut_ancien = $statut = $row['statut']; |
@@ -370,7 +370,7 @@ discard block |
||
| 370 | 370 | if ($s != 'publie' and autoriser('modifier', $objet, $id)) { |
| 371 | 371 | $statut = $champs['statut'] = $s; |
| 372 | 372 | } else { |
| 373 | - spip_log("editer_objet $objet #$id refus " . json_encode($c), 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 373 | + spip_log("editer_objet $objet #$id refus ".json_encode($c), 'editer'._LOG_INFO_IMPORTANTE); |
|
| 374 | 374 | } |
| 375 | 375 | } |
| 376 | 376 | |
@@ -400,7 +400,7 @@ discard block |
||
| 400 | 400 | and isset($c['id_parent']) |
| 401 | 401 | and $id_parent = $c['id_parent'] |
| 402 | 402 | and $id_parent != $id_rubrique |
| 403 | - and (sql_fetsel('1', 'spip_rubriques', 'id_rubrique=' . intval($id_parent))) |
|
| 403 | + and (sql_fetsel('1', 'spip_rubriques', 'id_rubrique='.intval($id_parent))) |
|
| 404 | 404 | ) { |
| 405 | 405 | $champs['id_rubrique'] = $id_parent; |
| 406 | 406 | |
@@ -499,7 +499,7 @@ discard block |
||
| 499 | 499 | // Si on deplace l'objet |
| 500 | 500 | // changer aussi son secteur et sa langue (si heritee) |
| 501 | 501 | if (isset($champs['id_rubrique'])) { |
| 502 | - $row_rub = sql_fetsel('id_secteur, lang', 'spip_rubriques', 'id_rubrique=' . sql_quote($champs['id_rubrique'])); |
|
| 502 | + $row_rub = sql_fetsel('id_secteur, lang', 'spip_rubriques', 'id_rubrique='.sql_quote($champs['id_rubrique'])); |
|
| 503 | 503 | $langue = $row_rub['lang']; |
| 504 | 504 | |
| 505 | 505 | if (isset($desc['field']['id_secteur'])) { |
@@ -511,7 +511,7 @@ discard block |
||
| 511 | 511 | sql_fetsel( |
| 512 | 512 | '1', |
| 513 | 513 | $table_sql, |
| 514 | - id_table_objet($objet) . '=' . intval($id) . " AND langue_choisie<>'oui' AND lang<>" . sql_quote($langue) |
|
| 514 | + id_table_objet($objet).'='.intval($id)." AND langue_choisie<>'oui' AND lang<>".sql_quote($langue) |
|
| 515 | 515 | ) |
| 516 | 516 | ) { |
| 517 | 517 | $champs['lang'] = $langue; |
@@ -522,7 +522,7 @@ discard block |
||
| 522 | 522 | if (!$champs) { |
| 523 | 523 | return; |
| 524 | 524 | } |
| 525 | - sql_updateq($table_sql, $champs, id_table_objet($objet) . '=' . intval($id)); |
|
| 525 | + sql_updateq($table_sql, $champs, id_table_objet($objet).'='.intval($id)); |
|
| 526 | 526 | |
| 527 | 527 | // Changer le statut des rubriques concernees |
| 528 | 528 | if ($cond) { |
@@ -566,7 +566,7 @@ discard block |
||
| 566 | 566 | */ |
| 567 | 567 | function objet_lire($objet, $valeur_id, $options = []) { |
| 568 | 568 | if (($t = objet_type($objet)) !== $objet) { |
| 569 | - spip_log("objet_lire: appel avec type $objet invalide au lieu de $t", 'editer' . _LOG_INFO_IMPORTANTE); |
|
| 569 | + spip_log("objet_lire: appel avec type $objet invalide au lieu de $t", 'editer'._LOG_INFO_IMPORTANTE); |
|
| 570 | 570 | $objet = $t; |
| 571 | 571 | } |
| 572 | 572 | |
@@ -588,7 +588,7 @@ discard block |
||
| 588 | 588 | ) { |
| 589 | 589 | // Il est possible pour un type d'objet de fournir une fonction de lecture de tous les champs d'un objet. |
| 590 | 590 | if ( |
| 591 | - include_spip('action/editer_' . $objet) |
|
| 591 | + include_spip('action/editer_'.$objet) |
|
| 592 | 592 | and function_exists($lire = "${objet}_lire_champs") |
| 593 | 593 | ) { |
| 594 | 594 | $valeurs = $lire($objet, $valeur_id, $champ_id); |
@@ -598,7 +598,7 @@ discard block |
||
| 598 | 598 | |
| 599 | 599 | // La condition est appliquée sur le champ désigné par l'utilisateur. |
| 600 | 600 | $where = [ |
| 601 | - "${champ_id}=" . sql_quote($valeur_id) |
|
| 601 | + "${champ_id}=".sql_quote($valeur_id) |
|
| 602 | 602 | ]; |
| 603 | 603 | |
| 604 | 604 | // Acquisition de tous les champs de l'objet : si l'accès SQL retourne une erreur on renvoie un tableau vide. |
@@ -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 | |
@@ -30,36 +30,36 @@ discard block |
||
| 30 | 30 | * `on` ou `off` |
| 31 | 31 | */ |
| 32 | 32 | function logo_supprimer($objet, $id_objet, $etat) { |
| 33 | - $chercher_logo = charger_fonction('chercher_logo', 'inc'); |
|
| 34 | - $objet = objet_type($objet); |
|
| 35 | - $primary = id_table_objet($objet); |
|
| 36 | - include_spip('inc/chercher_logo'); |
|
| 37 | - |
|
| 38 | - // existe-t-il deja un logo ? |
|
| 39 | - $logo = $chercher_logo($id_objet, $primary, $etat); |
|
| 40 | - if ($logo) { |
|
| 41 | - # TODO : deprecated, a supprimer -> anciens logos IMG/artonxx.png pas en base |
|
| 42 | - if (count($logo) < 6) { |
|
| 43 | - spip_log("Supprimer ancien logo $logo", 'logo'); |
|
| 44 | - spip_unlink($logo[0]); |
|
| 45 | - } |
|
| 46 | - elseif ( |
|
| 47 | - $doc = $logo[5] |
|
| 48 | - and isset($doc['id_document']) |
|
| 49 | - and $id_document = $doc['id_document'] |
|
| 50 | - ) { |
|
| 51 | - include_spip('action/editer_liens'); |
|
| 52 | - // supprimer le lien dans la base |
|
| 53 | - objet_dissocier(['document' => $id_document], [$objet => $id_objet], ['role' => '*']); |
|
| 54 | - |
|
| 55 | - // verifier si il reste des liens avec d'autres objets et sinon supprimer |
|
| 56 | - $liens = objet_trouver_liens(['document' => $id_document], '*'); |
|
| 57 | - if (!count($liens)) { |
|
| 58 | - $supprimer_document = charger_fonction('supprimer_document', 'action'); |
|
| 59 | - $supprimer_document($doc['id_document']); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - } |
|
| 33 | + $chercher_logo = charger_fonction('chercher_logo', 'inc'); |
|
| 34 | + $objet = objet_type($objet); |
|
| 35 | + $primary = id_table_objet($objet); |
|
| 36 | + include_spip('inc/chercher_logo'); |
|
| 37 | + |
|
| 38 | + // existe-t-il deja un logo ? |
|
| 39 | + $logo = $chercher_logo($id_objet, $primary, $etat); |
|
| 40 | + if ($logo) { |
|
| 41 | + # TODO : deprecated, a supprimer -> anciens logos IMG/artonxx.png pas en base |
|
| 42 | + if (count($logo) < 6) { |
|
| 43 | + spip_log("Supprimer ancien logo $logo", 'logo'); |
|
| 44 | + spip_unlink($logo[0]); |
|
| 45 | + } |
|
| 46 | + elseif ( |
|
| 47 | + $doc = $logo[5] |
|
| 48 | + and isset($doc['id_document']) |
|
| 49 | + and $id_document = $doc['id_document'] |
|
| 50 | + ) { |
|
| 51 | + include_spip('action/editer_liens'); |
|
| 52 | + // supprimer le lien dans la base |
|
| 53 | + objet_dissocier(['document' => $id_document], [$objet => $id_objet], ['role' => '*']); |
|
| 54 | + |
|
| 55 | + // verifier si il reste des liens avec d'autres objets et sinon supprimer |
|
| 56 | + $liens = objet_trouver_liens(['document' => $id_document], '*'); |
|
| 57 | + if (!count($liens)) { |
|
| 58 | + $supprimer_document = charger_fonction('supprimer_document', 'action'); |
|
| 59 | + $supprimer_document($doc['id_document']); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -76,133 +76,133 @@ discard block |
||
| 76 | 76 | * Erreur, sinon '' |
| 77 | 77 | */ |
| 78 | 78 | function logo_modifier($objet, $id_objet, $etat, $source) { |
| 79 | - $chercher_logo = charger_fonction('chercher_logo', 'inc'); |
|
| 80 | - $objet = objet_type($objet); |
|
| 81 | - $primary = id_table_objet($objet); |
|
| 82 | - include_spip('inc/chercher_logo'); |
|
| 83 | - |
|
| 84 | - $mode = preg_replace(',\W,', '', $etat); |
|
| 85 | - if (!$mode) { |
|
| 86 | - spip_log("logo_modifier : etat $etat invalide", 'logo'); |
|
| 87 | - $erreur = 'etat invalide'; |
|
| 88 | - |
|
| 89 | - return $erreur; |
|
| 90 | - } |
|
| 91 | - // chercher dans la base |
|
| 92 | - $mode_document = 'logo' . $mode; |
|
| 93 | - |
|
| 94 | - include_spip('inc/documents'); |
|
| 95 | - $erreur = ''; |
|
| 96 | - |
|
| 97 | - if (!$source) { |
|
| 98 | - spip_log('spip_image_ajouter : source inconnue', 'logo'); |
|
| 99 | - $erreur = 'source inconnue'; |
|
| 100 | - |
|
| 101 | - return $erreur; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - // fichier dans upload/ |
|
| 105 | - if (is_string($source)) { |
|
| 106 | - $tmp_name = false; |
|
| 107 | - if (file_exists($source)) { |
|
| 108 | - $tmp_name = $source; |
|
| 109 | - } elseif (file_exists($f = determine_upload() . $source)) { |
|
| 110 | - $tmp_name = $f; |
|
| 111 | - } |
|
| 112 | - if (!$tmp_name) { |
|
| 113 | - spip_log('spip_image_ajouter : source inconnue', 'logo'); |
|
| 114 | - $erreur = 'source inconnue'; |
|
| 115 | - |
|
| 116 | - return $erreur; |
|
| 117 | - } |
|
| 118 | - $source = [ |
|
| 119 | - 'tmp_name' => $tmp_name, |
|
| 120 | - 'name' => basename($tmp_name), |
|
| 121 | - ]; |
|
| 122 | - } elseif ($erreur = check_upload_error($source['error'], '', true)) { |
|
| 123 | - return $erreur; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - // supprimer le logo eventueel existant |
|
| 127 | - // TODO : si un logo existe, le modifier plutot que supprimer + reinserer (mais il faut gerer le cas ou il est utilise par plusieurs objets, donc pas si simple) |
|
| 128 | - // mais de toute facon l'interface actuelle oblige a supprimer + reinserer |
|
| 129 | - logo_supprimer($objet, $id_objet, $etat); |
|
| 130 | - |
|
| 131 | - |
|
| 132 | - include_spip('inc/autoriser'); |
|
| 133 | - $source['mode'] = $mode_document; |
|
| 134 | - $ajouter_documents = charger_fonction('ajouter_documents', 'action'); |
|
| 135 | - autoriser_exception('associerdocuments', $objet, $id_objet); |
|
| 136 | - $ajoutes = $ajouter_documents('new', [$source], $objet, $id_objet, $mode_document); |
|
| 137 | - autoriser_exception('associerdocuments', $objet, $id_objet, false); |
|
| 138 | - |
|
| 139 | - $id_document = reset($ajoutes); |
|
| 140 | - |
|
| 141 | - if (!is_numeric($id_document)) { |
|
| 142 | - $erreur = ($id_document ? $id_document : 'Erreur inconnue'); |
|
| 143 | - spip_log("Erreur ajout logo : $erreur pour source=" . json_encode($source), 'logo'); |
|
| 144 | - return $erreur; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - return ''; // tout est bon, pas d'erreur |
|
| 79 | + $chercher_logo = charger_fonction('chercher_logo', 'inc'); |
|
| 80 | + $objet = objet_type($objet); |
|
| 81 | + $primary = id_table_objet($objet); |
|
| 82 | + include_spip('inc/chercher_logo'); |
|
| 83 | + |
|
| 84 | + $mode = preg_replace(',\W,', '', $etat); |
|
| 85 | + if (!$mode) { |
|
| 86 | + spip_log("logo_modifier : etat $etat invalide", 'logo'); |
|
| 87 | + $erreur = 'etat invalide'; |
|
| 88 | + |
|
| 89 | + return $erreur; |
|
| 90 | + } |
|
| 91 | + // chercher dans la base |
|
| 92 | + $mode_document = 'logo' . $mode; |
|
| 93 | + |
|
| 94 | + include_spip('inc/documents'); |
|
| 95 | + $erreur = ''; |
|
| 96 | + |
|
| 97 | + if (!$source) { |
|
| 98 | + spip_log('spip_image_ajouter : source inconnue', 'logo'); |
|
| 99 | + $erreur = 'source inconnue'; |
|
| 100 | + |
|
| 101 | + return $erreur; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + // fichier dans upload/ |
|
| 105 | + if (is_string($source)) { |
|
| 106 | + $tmp_name = false; |
|
| 107 | + if (file_exists($source)) { |
|
| 108 | + $tmp_name = $source; |
|
| 109 | + } elseif (file_exists($f = determine_upload() . $source)) { |
|
| 110 | + $tmp_name = $f; |
|
| 111 | + } |
|
| 112 | + if (!$tmp_name) { |
|
| 113 | + spip_log('spip_image_ajouter : source inconnue', 'logo'); |
|
| 114 | + $erreur = 'source inconnue'; |
|
| 115 | + |
|
| 116 | + return $erreur; |
|
| 117 | + } |
|
| 118 | + $source = [ |
|
| 119 | + 'tmp_name' => $tmp_name, |
|
| 120 | + 'name' => basename($tmp_name), |
|
| 121 | + ]; |
|
| 122 | + } elseif ($erreur = check_upload_error($source['error'], '', true)) { |
|
| 123 | + return $erreur; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + // supprimer le logo eventueel existant |
|
| 127 | + // TODO : si un logo existe, le modifier plutot que supprimer + reinserer (mais il faut gerer le cas ou il est utilise par plusieurs objets, donc pas si simple) |
|
| 128 | + // mais de toute facon l'interface actuelle oblige a supprimer + reinserer |
|
| 129 | + logo_supprimer($objet, $id_objet, $etat); |
|
| 130 | + |
|
| 131 | + |
|
| 132 | + include_spip('inc/autoriser'); |
|
| 133 | + $source['mode'] = $mode_document; |
|
| 134 | + $ajouter_documents = charger_fonction('ajouter_documents', 'action'); |
|
| 135 | + autoriser_exception('associerdocuments', $objet, $id_objet); |
|
| 136 | + $ajoutes = $ajouter_documents('new', [$source], $objet, $id_objet, $mode_document); |
|
| 137 | + autoriser_exception('associerdocuments', $objet, $id_objet, false); |
|
| 138 | + |
|
| 139 | + $id_document = reset($ajoutes); |
|
| 140 | + |
|
| 141 | + if (!is_numeric($id_document)) { |
|
| 142 | + $erreur = ($id_document ? $id_document : 'Erreur inconnue'); |
|
| 143 | + spip_log("Erreur ajout logo : $erreur pour source=" . json_encode($source), 'logo'); |
|
| 144 | + return $erreur; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + return ''; // tout est bon, pas d'erreur |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | function logo_migrer_en_base($objet, $time_limit) { |
| 151 | 151 | |
| 152 | - $dir_logos_erreurs = sous_repertoire(_DIR_IMG, 'logo_erreurs'); |
|
| 153 | - $dir_logos = sous_repertoire(_DIR_IMG, 'logo'); |
|
| 154 | - $formats_logos = ['jpg', 'png', 'svg', 'gif']; |
|
| 155 | - if (isset($GLOBALS['formats_logos'])) { |
|
| 156 | - $formats_logos = $GLOBALS['formats_logos']; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - |
|
| 160 | - $chercher_logo = charger_fonction('chercher_logo', 'inc'); |
|
| 161 | - include_spip('inc/chercher_logo'); |
|
| 162 | - $_id_objet = id_table_objet($objet); |
|
| 163 | - $type = type_du_logo($_id_objet); |
|
| 164 | - |
|
| 165 | - foreach (['on', 'off'] as $mode) { |
|
| 166 | - $nom_base = $type . $mode; |
|
| 167 | - $dir = (defined('_DIR_LOGOS') ? _DIR_LOGOS : _DIR_IMG); |
|
| 168 | - |
|
| 169 | - $deja = []; |
|
| 170 | - $files = glob($dir . $nom_base . '*'); |
|
| 171 | - |
|
| 172 | - foreach ($files as $file) { |
|
| 173 | - $logo = substr($file, strlen($dir . $nom_base)); |
|
| 174 | - $logo = explode('.', $logo); |
|
| 175 | - if ( |
|
| 176 | - is_numeric($logo[0]) |
|
| 177 | - and ($id_objet = intval($logo[0]) or in_array($objet, ['site', 'rubrique'])) |
|
| 178 | - ) { |
|
| 179 | - if (!isset($deja[$id_objet])) { |
|
| 180 | - $logo = $chercher_logo($id_objet, $_id_objet, $mode); |
|
| 181 | - // if no logo in base |
|
| 182 | - if (!$logo or count($logo) < 6) { |
|
| 183 | - foreach ($formats_logos as $format) { |
|
| 184 | - if (@file_exists($d = ($dir . ($nom = $nom_base . intval($id_objet) . '.' . $format)))) { |
|
| 185 | - // logo_modifier commence par supprimer le logo existant, donc on le deplace pour pas le perdre |
|
| 186 | - @rename($d, $dir_logos . $nom); |
|
| 187 | - // et on le declare comme nouveau logo |
|
| 188 | - logo_modifier($objet, $id_objet, $mode, $dir_logos . $nom); |
|
| 189 | - break; |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - $deja[$id_objet] = true; |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - // si le fichier est encore la on le move : rien a faire ici |
|
| 197 | - if (file_exists($file)) { |
|
| 198 | - @rename($file, $dir_logos_erreurs . basename($file)); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - if ($time_limit and time() > $time_limit) { |
|
| 202 | - effacer_meta('drapeau_edition'); |
|
| 203 | - return; |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - effacer_meta('drapeau_edition'); |
|
| 152 | + $dir_logos_erreurs = sous_repertoire(_DIR_IMG, 'logo_erreurs'); |
|
| 153 | + $dir_logos = sous_repertoire(_DIR_IMG, 'logo'); |
|
| 154 | + $formats_logos = ['jpg', 'png', 'svg', 'gif']; |
|
| 155 | + if (isset($GLOBALS['formats_logos'])) { |
|
| 156 | + $formats_logos = $GLOBALS['formats_logos']; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + |
|
| 160 | + $chercher_logo = charger_fonction('chercher_logo', 'inc'); |
|
| 161 | + include_spip('inc/chercher_logo'); |
|
| 162 | + $_id_objet = id_table_objet($objet); |
|
| 163 | + $type = type_du_logo($_id_objet); |
|
| 164 | + |
|
| 165 | + foreach (['on', 'off'] as $mode) { |
|
| 166 | + $nom_base = $type . $mode; |
|
| 167 | + $dir = (defined('_DIR_LOGOS') ? _DIR_LOGOS : _DIR_IMG); |
|
| 168 | + |
|
| 169 | + $deja = []; |
|
| 170 | + $files = glob($dir . $nom_base . '*'); |
|
| 171 | + |
|
| 172 | + foreach ($files as $file) { |
|
| 173 | + $logo = substr($file, strlen($dir . $nom_base)); |
|
| 174 | + $logo = explode('.', $logo); |
|
| 175 | + if ( |
|
| 176 | + is_numeric($logo[0]) |
|
| 177 | + and ($id_objet = intval($logo[0]) or in_array($objet, ['site', 'rubrique'])) |
|
| 178 | + ) { |
|
| 179 | + if (!isset($deja[$id_objet])) { |
|
| 180 | + $logo = $chercher_logo($id_objet, $_id_objet, $mode); |
|
| 181 | + // if no logo in base |
|
| 182 | + if (!$logo or count($logo) < 6) { |
|
| 183 | + foreach ($formats_logos as $format) { |
|
| 184 | + if (@file_exists($d = ($dir . ($nom = $nom_base . intval($id_objet) . '.' . $format)))) { |
|
| 185 | + // logo_modifier commence par supprimer le logo existant, donc on le deplace pour pas le perdre |
|
| 186 | + @rename($d, $dir_logos . $nom); |
|
| 187 | + // et on le declare comme nouveau logo |
|
| 188 | + logo_modifier($objet, $id_objet, $mode, $dir_logos . $nom); |
|
| 189 | + break; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + $deja[$id_objet] = true; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + // si le fichier est encore la on le move : rien a faire ici |
|
| 197 | + if (file_exists($file)) { |
|
| 198 | + @rename($file, $dir_logos_erreurs . basename($file)); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + if ($time_limit and time() > $time_limit) { |
|
| 202 | + effacer_meta('drapeau_edition'); |
|
| 203 | + return; |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + effacer_meta('drapeau_edition'); |
|
| 208 | 208 | } |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | return $erreur; |
| 90 | 90 | } |
| 91 | 91 | // chercher dans la base |
| 92 | - $mode_document = 'logo' . $mode; |
|
| 92 | + $mode_document = 'logo'.$mode; |
|
| 93 | 93 | |
| 94 | 94 | include_spip('inc/documents'); |
| 95 | 95 | $erreur = ''; |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | $tmp_name = false; |
| 107 | 107 | if (file_exists($source)) { |
| 108 | 108 | $tmp_name = $source; |
| 109 | - } elseif (file_exists($f = determine_upload() . $source)) { |
|
| 109 | + } elseif (file_exists($f = determine_upload().$source)) { |
|
| 110 | 110 | $tmp_name = $f; |
| 111 | 111 | } |
| 112 | 112 | if (!$tmp_name) { |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | |
| 141 | 141 | if (!is_numeric($id_document)) { |
| 142 | 142 | $erreur = ($id_document ? $id_document : 'Erreur inconnue'); |
| 143 | - spip_log("Erreur ajout logo : $erreur pour source=" . json_encode($source), 'logo'); |
|
| 143 | + spip_log("Erreur ajout logo : $erreur pour source=".json_encode($source), 'logo'); |
|
| 144 | 144 | return $erreur; |
| 145 | 145 | } |
| 146 | 146 | |
@@ -163,14 +163,14 @@ discard block |
||
| 163 | 163 | $type = type_du_logo($_id_objet); |
| 164 | 164 | |
| 165 | 165 | foreach (['on', 'off'] as $mode) { |
| 166 | - $nom_base = $type . $mode; |
|
| 166 | + $nom_base = $type.$mode; |
|
| 167 | 167 | $dir = (defined('_DIR_LOGOS') ? _DIR_LOGOS : _DIR_IMG); |
| 168 | 168 | |
| 169 | 169 | $deja = []; |
| 170 | - $files = glob($dir . $nom_base . '*'); |
|
| 170 | + $files = glob($dir.$nom_base.'*'); |
|
| 171 | 171 | |
| 172 | 172 | foreach ($files as $file) { |
| 173 | - $logo = substr($file, strlen($dir . $nom_base)); |
|
| 173 | + $logo = substr($file, strlen($dir.$nom_base)); |
|
| 174 | 174 | $logo = explode('.', $logo); |
| 175 | 175 | if ( |
| 176 | 176 | is_numeric($logo[0]) |
@@ -181,11 +181,11 @@ discard block |
||
| 181 | 181 | // if no logo in base |
| 182 | 182 | if (!$logo or count($logo) < 6) { |
| 183 | 183 | foreach ($formats_logos as $format) { |
| 184 | - if (@file_exists($d = ($dir . ($nom = $nom_base . intval($id_objet) . '.' . $format)))) { |
|
| 184 | + if (@file_exists($d = ($dir.($nom = $nom_base.intval($id_objet).'.'.$format)))) { |
|
| 185 | 185 | // logo_modifier commence par supprimer le logo existant, donc on le deplace pour pas le perdre |
| 186 | - @rename($d, $dir_logos . $nom); |
|
| 186 | + @rename($d, $dir_logos.$nom); |
|
| 187 | 187 | // et on le declare comme nouveau logo |
| 188 | - logo_modifier($objet, $id_objet, $mode, $dir_logos . $nom); |
|
| 188 | + logo_modifier($objet, $id_objet, $mode, $dir_logos.$nom); |
|
| 189 | 189 | break; |
| 190 | 190 | } |
| 191 | 191 | } |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | } |
| 196 | 196 | // si le fichier est encore la on le move : rien a faire ici |
| 197 | 197 | if (file_exists($file)) { |
| 198 | - @rename($file, $dir_logos_erreurs . basename($file)); |
|
| 198 | + @rename($file, $dir_logos_erreurs.basename($file)); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | if ($time_limit and time() > $time_limit) { |
@@ -42,8 +42,7 @@ |
||
| 42 | 42 | if (count($logo) < 6) { |
| 43 | 43 | spip_log("Supprimer ancien logo $logo", 'logo'); |
| 44 | 44 | spip_unlink($logo[0]); |
| 45 | - } |
|
| 46 | - elseif ( |
|
| 45 | + } elseif ( |
|
| 47 | 46 | $doc = $logo[5] |
| 48 | 47 | and isset($doc['id_document']) |
| 49 | 48 | and $id_document = $doc['id_document'] |