@@ -6,12 +6,12 @@ |
||
| 6 | 6 | * Pouvoir retrouver le PDO utilisé pour générer un résultat de requête. |
| 7 | 7 | */ |
| 8 | 8 | final class PDOStatement extends \PDOStatement { |
| 9 | - private \PDO $PDO; |
|
| 9 | + private \PDO $PDO; |
|
| 10 | 10 | |
| 11 | - private function __construct(\PDO &$PDO) { |
|
| 12 | - $this->PDO = $PDO; |
|
| 13 | - } |
|
| 14 | - public function getPDO(): \PDO { |
|
| 15 | - return $this->PDO; |
|
| 16 | - } |
|
| 11 | + private function __construct(\PDO &$PDO) { |
|
| 12 | + $this->PDO = $PDO; |
|
| 13 | + } |
|
| 14 | + public function getPDO(): \PDO { |
|
| 15 | + return $this->PDO; |
|
| 16 | + } |
|
| 17 | 17 | } |
@@ -13,112 +13,112 @@ |
||
| 13 | 13 | |
| 14 | 14 | class Requeteur |
| 15 | 15 | { |
| 16 | - /** @var string texte de la requête */ |
|
| 17 | - public $query = ''; // la requete |
|
| 18 | - /** @var string Nom de la connexion */ |
|
| 19 | - public $serveur = ''; |
|
| 20 | - /** @var \PDO|null Identifiant de la connexion SQLite */ |
|
| 21 | - public $link = null; |
|
| 22 | - /** @var string Prefixe des tables SPIP */ |
|
| 23 | - public $prefixe = ''; |
|
| 24 | - /** @var string Nom de la base de donnée */ |
|
| 25 | - public $db = ''; |
|
| 26 | - /** @var bool Doit-on tracer les requetes (var_profile) ? */ |
|
| 27 | - public $tracer = false; // doit-on tracer les requetes (var_profile) |
|
| 28 | - |
|
| 29 | - /** @var string Version de SQLite (2 ou 3) */ |
|
| 30 | - public $sqlite_version = ''; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Constructeur |
|
| 34 | - * |
|
| 35 | - * @param string $serveur |
|
| 36 | - */ |
|
| 37 | - public function __construct($serveur = '') |
|
| 38 | - { |
|
| 39 | - _sqlite_init(); |
|
| 40 | - $this->serveur = strtolower($serveur); |
|
| 41 | - |
|
| 42 | - if (!($this->link = _sqlite_link($this->serveur)) && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL)) { |
|
| 43 | - spip_log('Aucune connexion sqlite (link)', 'sqlite.' . _LOG_ERREUR); |
|
| 44 | - |
|
| 45 | - return; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - $this->sqlite_version = _sqlite_is_version('', $this->link); |
|
| 49 | - |
|
| 50 | - $this->prefixe = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['prefixe']; |
|
| 51 | - $this->db = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['db']; |
|
| 52 | - |
|
| 53 | - // tracage des requetes ? |
|
| 54 | - $this->tracer = (isset($_GET['var_profile']) && $_GET['var_profile']); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Lancer la requête transmise et faire le tracage si demandé |
|
| 59 | - * |
|
| 60 | - * @param string $query |
|
| 61 | - * Requête à exécuter |
|
| 62 | - * @param bool|null $tracer |
|
| 63 | - * true pour tracer la requête |
|
| 64 | - * @return bool|\PDOStatement|array |
|
| 65 | - */ |
|
| 66 | - public function executer_requete($query, $tracer = null) |
|
| 67 | - { |
|
| 68 | - if (is_null($tracer)) { |
|
| 69 | - $tracer = $this->tracer; |
|
| 70 | - } |
|
| 71 | - $err = ''; |
|
| 72 | - $t = 0; |
|
| 73 | - if ($tracer or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 74 | - include_spip('public/tracer'); |
|
| 75 | - $t = trace_query_start(); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - # spip_log("requete: $this->serveur >> $query",'sqlite.'._LOG_DEBUG); // boum ? pourquoi ? |
|
| 79 | - if ($this->link) { |
|
| 80 | - // memoriser la derniere erreur PHP vue |
|
| 81 | - $last_error = (function_exists('error_get_last') ? error_get_last() : ''); |
|
| 82 | - $e = null; |
|
| 83 | - // sauver la derniere requete |
|
| 84 | - $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['last'] = $query; |
|
| 85 | - $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['total_requetes']++; |
|
| 86 | - |
|
| 87 | - try { |
|
| 88 | - $r = $this->link->query($query); |
|
| 89 | - } catch (\PDOException $e) { |
|
| 90 | - spip_log('PDOException: ' . $e->getMessage(), 'sqlite.' . _LOG_DEBUG); |
|
| 91 | - $r = false; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - // loger les warnings/erreurs eventuels de sqlite remontant dans PHP |
|
| 95 | - if ($e and $e instanceof \PDOException) { |
|
| 96 | - $err = strip_tags($e->getMessage()) . ' in ' . $e->getFile() . ' line ' . $e->getLine(); |
|
| 97 | - spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 98 | - } elseif ($err = (function_exists('error_get_last') ? error_get_last() : '') and $err != $last_error) { |
|
| 99 | - $err = strip_tags($err['message']) . ' in ' . $err['file'] . ' line ' . $err['line']; |
|
| 100 | - spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 101 | - } else { |
|
| 102 | - $err = ''; |
|
| 103 | - } |
|
| 104 | - } else { |
|
| 105 | - $r = false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - if (spip_sqlite_errno($this->serveur)) { |
|
| 109 | - $err .= spip_sqlite_error($query, $this->serveur); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return $t ? trace_query_end($query, $t, $r, $err, $this->serveur) : $r; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 117 | - * |
|
| 118 | - * @return string|false |
|
| 119 | - **/ |
|
| 120 | - public function last_insert_id() |
|
| 121 | - { |
|
| 122 | - return $this->link->lastInsertId(); |
|
| 123 | - } |
|
| 16 | + /** @var string texte de la requête */ |
|
| 17 | + public $query = ''; // la requete |
|
| 18 | + /** @var string Nom de la connexion */ |
|
| 19 | + public $serveur = ''; |
|
| 20 | + /** @var \PDO|null Identifiant de la connexion SQLite */ |
|
| 21 | + public $link = null; |
|
| 22 | + /** @var string Prefixe des tables SPIP */ |
|
| 23 | + public $prefixe = ''; |
|
| 24 | + /** @var string Nom de la base de donnée */ |
|
| 25 | + public $db = ''; |
|
| 26 | + /** @var bool Doit-on tracer les requetes (var_profile) ? */ |
|
| 27 | + public $tracer = false; // doit-on tracer les requetes (var_profile) |
|
| 28 | + |
|
| 29 | + /** @var string Version de SQLite (2 ou 3) */ |
|
| 30 | + public $sqlite_version = ''; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Constructeur |
|
| 34 | + * |
|
| 35 | + * @param string $serveur |
|
| 36 | + */ |
|
| 37 | + public function __construct($serveur = '') |
|
| 38 | + { |
|
| 39 | + _sqlite_init(); |
|
| 40 | + $this->serveur = strtolower($serveur); |
|
| 41 | + |
|
| 42 | + if (!($this->link = _sqlite_link($this->serveur)) && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL)) { |
|
| 43 | + spip_log('Aucune connexion sqlite (link)', 'sqlite.' . _LOG_ERREUR); |
|
| 44 | + |
|
| 45 | + return; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + $this->sqlite_version = _sqlite_is_version('', $this->link); |
|
| 49 | + |
|
| 50 | + $this->prefixe = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['prefixe']; |
|
| 51 | + $this->db = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['db']; |
|
| 52 | + |
|
| 53 | + // tracage des requetes ? |
|
| 54 | + $this->tracer = (isset($_GET['var_profile']) && $_GET['var_profile']); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Lancer la requête transmise et faire le tracage si demandé |
|
| 59 | + * |
|
| 60 | + * @param string $query |
|
| 61 | + * Requête à exécuter |
|
| 62 | + * @param bool|null $tracer |
|
| 63 | + * true pour tracer la requête |
|
| 64 | + * @return bool|\PDOStatement|array |
|
| 65 | + */ |
|
| 66 | + public function executer_requete($query, $tracer = null) |
|
| 67 | + { |
|
| 68 | + if (is_null($tracer)) { |
|
| 69 | + $tracer = $this->tracer; |
|
| 70 | + } |
|
| 71 | + $err = ''; |
|
| 72 | + $t = 0; |
|
| 73 | + if ($tracer or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 74 | + include_spip('public/tracer'); |
|
| 75 | + $t = trace_query_start(); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + # spip_log("requete: $this->serveur >> $query",'sqlite.'._LOG_DEBUG); // boum ? pourquoi ? |
|
| 79 | + if ($this->link) { |
|
| 80 | + // memoriser la derniere erreur PHP vue |
|
| 81 | + $last_error = (function_exists('error_get_last') ? error_get_last() : ''); |
|
| 82 | + $e = null; |
|
| 83 | + // sauver la derniere requete |
|
| 84 | + $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['last'] = $query; |
|
| 85 | + $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['total_requetes']++; |
|
| 86 | + |
|
| 87 | + try { |
|
| 88 | + $r = $this->link->query($query); |
|
| 89 | + } catch (\PDOException $e) { |
|
| 90 | + spip_log('PDOException: ' . $e->getMessage(), 'sqlite.' . _LOG_DEBUG); |
|
| 91 | + $r = false; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + // loger les warnings/erreurs eventuels de sqlite remontant dans PHP |
|
| 95 | + if ($e and $e instanceof \PDOException) { |
|
| 96 | + $err = strip_tags($e->getMessage()) . ' in ' . $e->getFile() . ' line ' . $e->getLine(); |
|
| 97 | + spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 98 | + } elseif ($err = (function_exists('error_get_last') ? error_get_last() : '') and $err != $last_error) { |
|
| 99 | + $err = strip_tags($err['message']) . ' in ' . $err['file'] . ' line ' . $err['line']; |
|
| 100 | + spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 101 | + } else { |
|
| 102 | + $err = ''; |
|
| 103 | + } |
|
| 104 | + } else { |
|
| 105 | + $r = false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + if (spip_sqlite_errno($this->serveur)) { |
|
| 109 | + $err .= spip_sqlite_error($query, $this->serveur); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return $t ? trace_query_end($query, $t, $r, $err, $this->serveur) : $r; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 117 | + * |
|
| 118 | + * @return string|false |
|
| 119 | + **/ |
|
| 120 | + public function last_insert_id() |
|
| 121 | + { |
|
| 122 | + return $this->link->lastInsertId(); |
|
| 123 | + } |
|
| 124 | 124 | } |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | $this->serveur = strtolower($serveur); |
| 41 | 41 | |
| 42 | 42 | if (!($this->link = _sqlite_link($this->serveur)) && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL)) { |
| 43 | - spip_log('Aucune connexion sqlite (link)', 'sqlite.' . _LOG_ERREUR); |
|
| 43 | + spip_log('Aucune connexion sqlite (link)', 'sqlite.'._LOG_ERREUR); |
|
| 44 | 44 | |
| 45 | 45 | return; |
| 46 | 46 | } |
@@ -87,17 +87,17 @@ discard block |
||
| 87 | 87 | try { |
| 88 | 88 | $r = $this->link->query($query); |
| 89 | 89 | } catch (\PDOException $e) { |
| 90 | - spip_log('PDOException: ' . $e->getMessage(), 'sqlite.' . _LOG_DEBUG); |
|
| 90 | + spip_log('PDOException: '.$e->getMessage(), 'sqlite.'._LOG_DEBUG); |
|
| 91 | 91 | $r = false; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | // loger les warnings/erreurs eventuels de sqlite remontant dans PHP |
| 95 | 95 | if ($e and $e instanceof \PDOException) { |
| 96 | - $err = strip_tags($e->getMessage()) . ' in ' . $e->getFile() . ' line ' . $e->getLine(); |
|
| 97 | - spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 96 | + $err = strip_tags($e->getMessage()).' in '.$e->getFile().' line '.$e->getLine(); |
|
| 97 | + spip_log("$err - ".$query, 'sqlite.'._LOG_ERREUR); |
|
| 98 | 98 | } elseif ($err = (function_exists('error_get_last') ? error_get_last() : '') and $err != $last_error) { |
| 99 | - $err = strip_tags($err['message']) . ' in ' . $err['file'] . ' line ' . $err['line']; |
|
| 100 | - spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 99 | + $err = strip_tags($err['message']).' in '.$err['file'].' line '.$err['line']; |
|
| 100 | + spip_log("$err - ".$query, 'sqlite.'._LOG_ERREUR); |
|
| 101 | 101 | } else { |
| 102 | 102 | $err = ''; |
| 103 | 103 | } |
@@ -8,119 +8,119 @@ |
||
| 8 | 8 | **/ |
| 9 | 9 | class Sqlite |
| 10 | 10 | { |
| 11 | - /** @var Requeteur[] Liste des instances de requêteurs créés */ |
|
| 12 | - public static $requeteurs = []; |
|
| 13 | - /** @var bool[] Pour chaque connexion, flag pour savoir si une transaction est en cours */ |
|
| 14 | - public static $transaction_en_cours = []; |
|
| 11 | + /** @var Requeteur[] Liste des instances de requêteurs créés */ |
|
| 12 | + public static $requeteurs = []; |
|
| 13 | + /** @var bool[] Pour chaque connexion, flag pour savoir si une transaction est en cours */ |
|
| 14 | + public static $transaction_en_cours = []; |
|
| 15 | 15 | |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Retourne une unique instance du requêteur |
|
| 19 | - * |
|
| 20 | - * Retourne une instance unique du requêteur pour une connexion SQLite |
|
| 21 | - * donnée |
|
| 22 | - * |
|
| 23 | - * @param string $serveur |
|
| 24 | - * Nom du connecteur |
|
| 25 | - * @return SqliteRequeteur |
|
| 26 | - * Instance unique du requêteur |
|
| 27 | - **/ |
|
| 28 | - public static function requeteur($serveur) |
|
| 29 | - { |
|
| 30 | - if (!isset(static::$requeteurs[$serveur])) { |
|
| 31 | - static::$requeteurs[$serveur] = new Requeteur($serveur); |
|
| 32 | - } |
|
| 17 | + /** |
|
| 18 | + * Retourne une unique instance du requêteur |
|
| 19 | + * |
|
| 20 | + * Retourne une instance unique du requêteur pour une connexion SQLite |
|
| 21 | + * donnée |
|
| 22 | + * |
|
| 23 | + * @param string $serveur |
|
| 24 | + * Nom du connecteur |
|
| 25 | + * @return SqliteRequeteur |
|
| 26 | + * Instance unique du requêteur |
|
| 27 | + **/ |
|
| 28 | + public static function requeteur($serveur) |
|
| 29 | + { |
|
| 30 | + if (!isset(static::$requeteurs[$serveur])) { |
|
| 31 | + static::$requeteurs[$serveur] = new Requeteur($serveur); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - return static::$requeteurs[$serveur]; |
|
| 35 | - } |
|
| 34 | + return static::$requeteurs[$serveur]; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Prépare le texte d'une requête avant son exécution |
|
| 39 | - * |
|
| 40 | - * Adapte la requête au format plus ou moins MySQL par un format |
|
| 41 | - * compris de SQLite. |
|
| 42 | - * |
|
| 43 | - * Change les préfixes de tables SPIP par ceux véritables |
|
| 44 | - * |
|
| 45 | - * @param string $query Requête à préparer |
|
| 46 | - * @param string $serveur Nom de la connexion |
|
| 47 | - * @return string Requête préparée |
|
| 48 | - */ |
|
| 49 | - public static function traduire_requete($query, $serveur) |
|
| 50 | - { |
|
| 51 | - $requeteur = static::requeteur($serveur); |
|
| 52 | - $traducteur = new Traducteur($query, $requeteur->prefixe, $requeteur->sqlite_version); |
|
| 37 | + /** |
|
| 38 | + * Prépare le texte d'une requête avant son exécution |
|
| 39 | + * |
|
| 40 | + * Adapte la requête au format plus ou moins MySQL par un format |
|
| 41 | + * compris de SQLite. |
|
| 42 | + * |
|
| 43 | + * Change les préfixes de tables SPIP par ceux véritables |
|
| 44 | + * |
|
| 45 | + * @param string $query Requête à préparer |
|
| 46 | + * @param string $serveur Nom de la connexion |
|
| 47 | + * @return string Requête préparée |
|
| 48 | + */ |
|
| 49 | + public static function traduire_requete($query, $serveur) |
|
| 50 | + { |
|
| 51 | + $requeteur = static::requeteur($serveur); |
|
| 52 | + $traducteur = new Traducteur($query, $requeteur->prefixe, $requeteur->sqlite_version); |
|
| 53 | 53 | |
| 54 | - return $traducteur->traduire_requete(); |
|
| 55 | - } |
|
| 54 | + return $traducteur->traduire_requete(); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Démarre une transaction |
|
| 59 | - * |
|
| 60 | - * @param string $serveur Nom de la connexion |
|
| 61 | - **/ |
|
| 62 | - public static function demarrer_transaction($serveur) |
|
| 63 | - { |
|
| 64 | - Sqlite::executer_requete('BEGIN TRANSACTION', $serveur); |
|
| 65 | - Sqlite::$transaction_en_cours[$serveur] = true; |
|
| 66 | - } |
|
| 57 | + /** |
|
| 58 | + * Démarre une transaction |
|
| 59 | + * |
|
| 60 | + * @param string $serveur Nom de la connexion |
|
| 61 | + **/ |
|
| 62 | + public static function demarrer_transaction($serveur) |
|
| 63 | + { |
|
| 64 | + Sqlite::executer_requete('BEGIN TRANSACTION', $serveur); |
|
| 65 | + Sqlite::$transaction_en_cours[$serveur] = true; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Exécute la requête donnée |
|
| 70 | - * |
|
| 71 | - * @param string $query Requête |
|
| 72 | - * @param string $serveur Nom de la connexion |
|
| 73 | - * @param null|bool $tracer Demander des statistiques (temps) ? |
|
| 74 | - **/ |
|
| 75 | - public static function executer_requete($query, $serveur, $tracer = null) |
|
| 76 | - { |
|
| 77 | - $requeteur = Sqlite::requeteur($serveur); |
|
| 68 | + /** |
|
| 69 | + * Exécute la requête donnée |
|
| 70 | + * |
|
| 71 | + * @param string $query Requête |
|
| 72 | + * @param string $serveur Nom de la connexion |
|
| 73 | + * @param null|bool $tracer Demander des statistiques (temps) ? |
|
| 74 | + **/ |
|
| 75 | + public static function executer_requete($query, $serveur, $tracer = null) |
|
| 76 | + { |
|
| 77 | + $requeteur = Sqlite::requeteur($serveur); |
|
| 78 | 78 | |
| 79 | - return $requeteur->executer_requete($query, $tracer); |
|
| 80 | - } |
|
| 79 | + return $requeteur->executer_requete($query, $tracer); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 84 | - * |
|
| 85 | - * @param string $serveur Nom de la connexion |
|
| 86 | - * return int Identifiant |
|
| 87 | - **/ |
|
| 88 | - public static function last_insert_id($serveur) |
|
| 89 | - { |
|
| 90 | - $requeteur = Sqlite::requeteur($serveur); |
|
| 82 | + /** |
|
| 83 | + * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 84 | + * |
|
| 85 | + * @param string $serveur Nom de la connexion |
|
| 86 | + * return int Identifiant |
|
| 87 | + **/ |
|
| 88 | + public static function last_insert_id($serveur) |
|
| 89 | + { |
|
| 90 | + $requeteur = Sqlite::requeteur($serveur); |
|
| 91 | 91 | |
| 92 | - return $requeteur->last_insert_id($serveur); |
|
| 93 | - } |
|
| 92 | + return $requeteur->last_insert_id($serveur); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * Annule une transaction |
|
| 97 | - * |
|
| 98 | - * @param string $serveur Nom de la connexion |
|
| 99 | - **/ |
|
| 100 | - public static function annuler_transaction($serveur) |
|
| 101 | - { |
|
| 102 | - Sqlite::executer_requete('ROLLBACK', $serveur); |
|
| 103 | - Sqlite::$transaction_en_cours[$serveur] = false; |
|
| 104 | - } |
|
| 95 | + /** |
|
| 96 | + * Annule une transaction |
|
| 97 | + * |
|
| 98 | + * @param string $serveur Nom de la connexion |
|
| 99 | + **/ |
|
| 100 | + public static function annuler_transaction($serveur) |
|
| 101 | + { |
|
| 102 | + Sqlite::executer_requete('ROLLBACK', $serveur); |
|
| 103 | + Sqlite::$transaction_en_cours[$serveur] = false; |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Termine une transaction |
|
| 108 | - * |
|
| 109 | - * @param string $serveur Nom de la connexion |
|
| 110 | - **/ |
|
| 111 | - public static function finir_transaction($serveur) |
|
| 112 | - { |
|
| 113 | - // si pas de transaction en cours, ne rien faire et le dire |
|
| 114 | - if ( |
|
| 115 | - !isset(Sqlite::$transaction_en_cours[$serveur]) |
|
| 116 | - or Sqlite::$transaction_en_cours[$serveur] == false |
|
| 117 | - ) { |
|
| 118 | - return false; |
|
| 119 | - } |
|
| 120 | - // sinon fermer la transaction et retourner true |
|
| 121 | - Sqlite::executer_requete('COMMIT', $serveur); |
|
| 122 | - Sqlite::$transaction_en_cours[$serveur] = false; |
|
| 106 | + /** |
|
| 107 | + * Termine une transaction |
|
| 108 | + * |
|
| 109 | + * @param string $serveur Nom de la connexion |
|
| 110 | + **/ |
|
| 111 | + public static function finir_transaction($serveur) |
|
| 112 | + { |
|
| 113 | + // si pas de transaction en cours, ne rien faire et le dire |
|
| 114 | + if ( |
|
| 115 | + !isset(Sqlite::$transaction_en_cours[$serveur]) |
|
| 116 | + or Sqlite::$transaction_en_cours[$serveur] == false |
|
| 117 | + ) { |
|
| 118 | + return false; |
|
| 119 | + } |
|
| 120 | + // sinon fermer la transaction et retourner true |
|
| 121 | + Sqlite::executer_requete('COMMIT', $serveur); |
|
| 122 | + Sqlite::$transaction_en_cours[$serveur] = false; |
|
| 123 | 123 | |
| 124 | - return true; |
|
| 125 | - } |
|
| 124 | + return true; |
|
| 125 | + } |
|
| 126 | 126 | } |
@@ -9,211 +9,211 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Traducteur |
| 11 | 11 | { |
| 12 | - /** @var string $query texte de la requête */ |
|
| 13 | - public $query = ''; |
|
| 14 | - |
|
| 15 | - /** @var string $prefixe Préfixe des tables */ |
|
| 16 | - public $prefixe = ''; |
|
| 17 | - |
|
| 18 | - /** @var string $sqlite_version Version de sqlite (2 ou 3) */ |
|
| 19 | - public $sqlite_version = ''; |
|
| 20 | - |
|
| 21 | - /** Pour les corrections à effectuer sur les requêtes : array(code=>'texte') trouvé |
|
| 22 | - * |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - public $textes = []; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Constructeur |
|
| 29 | - * |
|
| 30 | - * @param string $query Requête à préparer |
|
| 31 | - * @param string $prefixe Prefixe des tables à utiliser |
|
| 32 | - * @param string $sqlite_version Version SQLite (2 ou 3) |
|
| 33 | - */ |
|
| 34 | - public function __construct($query, $prefixe, $sqlite_version) |
|
| 35 | - { |
|
| 36 | - $this->query = $query; |
|
| 37 | - $this->prefixe = $prefixe; |
|
| 38 | - $this->sqlite_version = $sqlite_version; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Transformer la requete pour SQLite |
|
| 43 | - * |
|
| 44 | - * Enlève les textes, transforme la requête pour quelle soit |
|
| 45 | - * bien interprétée par SQLite, puis remet les textes |
|
| 46 | - * la fonction affecte `$this->query` |
|
| 47 | - */ |
|
| 48 | - public function traduire_requete() |
|
| 49 | - { |
|
| 50 | - // |
|
| 51 | - // 1) Protection des textes en les remplacant par des codes |
|
| 52 | - // |
|
| 53 | - // enlever les 'textes' et initialiser avec |
|
| 54 | - list($this->query, $textes) = query_echappe_textes($this->query); |
|
| 55 | - |
|
| 56 | - // |
|
| 57 | - // 2) Corrections de la requete |
|
| 58 | - // |
|
| 59 | - // Correction Create Database |
|
| 60 | - // Create Database -> requete ignoree |
|
| 61 | - if (strpos($this->query, 'CREATE DATABASE') === 0) { |
|
| 62 | - spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.' . _LOG_AVERTISSEMENT); |
|
| 63 | - $this->query = 'SELECT 1'; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - // Correction Insert Ignore |
|
| 67 | - // INSERT IGNORE -> insert (tout court et pas 'insert or replace') |
|
| 68 | - if (strpos($this->query, 'INSERT IGNORE') === 0) { |
|
| 69 | - spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.' . _LOG_DEBUG); |
|
| 70 | - $this->query = 'INSERT ' . substr($this->query, '13'); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - // Correction des dates avec INTERVAL |
|
| 74 | - // utiliser sql_date_proche() de preference |
|
| 75 | - if (strpos($this->query, 'INTERVAL') !== false) { |
|
| 76 | - $this->query = preg_replace_callback( |
|
| 77 | - '/DATE_(ADD|SUB)(.*)INTERVAL\s+(\d+)\s+([a-zA-Z]+)\)/U', |
|
| 78 | - [&$this, '_remplacerDateParTime'], |
|
| 79 | - $this->query |
|
| 80 | - ); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - if (strpos($this->query, 'LEFT(') !== false) { |
|
| 84 | - $this->query = str_replace('LEFT(', '_LEFT(', $this->query); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - if (strpos($this->query, 'TIMESTAMPDIFF(') !== false) { |
|
| 88 | - $this->query = preg_replace('/TIMESTAMPDIFF\(\s*([^,]*)\s*,/Uims', "TIMESTAMPDIFF('\\1',", $this->query); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - // Correction Using |
|
| 93 | - // USING (non reconnu en sqlite2) |
|
| 94 | - // problematique car la jointure ne se fait pas du coup. |
|
| 95 | - if (($this->sqlite_version == 2) && (strpos($this->query, 'USING') !== false)) { |
|
| 96 | - spip_log( |
|
| 97 | - "'USING (champ)' n'est pas reconnu en SQLite 2. Utilisez 'ON table1.champ = table2.champ'", |
|
| 98 | - 'sqlite.' . _LOG_ERREUR |
|
| 99 | - ); |
|
| 100 | - $this->query = preg_replace('/USING\s*\([^\)]*\)/', '', $this->query); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - // Correction Field |
|
| 104 | - // remplace FIELD(table,i,j,k...) par CASE WHEN table=i THEN n ... ELSE 0 END |
|
| 105 | - if (strpos($this->query, 'FIELD') !== false) { |
|
| 106 | - $this->query = preg_replace_callback( |
|
| 107 | - '/FIELD\s*\(([^\)]*)\)/', |
|
| 108 | - [&$this, '_remplacerFieldParCase'], |
|
| 109 | - $this->query |
|
| 110 | - ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - // Correction des noms de tables FROM |
|
| 114 | - // mettre les bons noms de table dans from, update, insert, replace... |
|
| 115 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/iS', $this->query, $regs)) { |
|
| 116 | - $suite = strstr($this->query, $regs[0]); |
|
| 117 | - $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 118 | - } else { |
|
| 119 | - $suite = ''; |
|
| 120 | - } |
|
| 121 | - $pref = ($this->prefixe) ? $this->prefixe . '_' : ''; |
|
| 122 | - $this->query = preg_replace('/([,\s])spip_/S', '\1' . $pref, $this->query) . $suite; |
|
| 123 | - |
|
| 124 | - // Correction zero AS x |
|
| 125 | - // pg n'aime pas 0+x AS alias, sqlite, dans le meme style, |
|
| 126 | - // n'apprecie pas du tout SELECT 0 as x ... ORDER BY x |
|
| 127 | - // il dit que x ne doit pas être un integer dans le order by ! |
|
| 128 | - // on remplace du coup x par vide() dans ce cas uniquement |
|
| 129 | - // |
|
| 130 | - // apparait dans public/vertebrer.php et dans le plugin menu aussi qui genere aussi ce genre de requete via un {par num #GET{tri_num}} |
|
| 131 | - // mais est-ce encore un soucis pour sqlite en 2021 ? (ie commenter le preg_replace marche très bien en sqlite 3.28) |
|
| 132 | - if ((strpos($this->query, '0 AS') !== false)) { |
|
| 133 | - // on ne remplace que dans ORDER BY ou GROUP BY |
|
| 134 | - if (preg_match('/\s(ORDER|GROUP) BY\s/i', $this->query, $regs)) { |
|
| 135 | - $suite = strstr($this->query, $regs[0]); |
|
| 136 | - $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 137 | - |
|
| 138 | - // on cherche les noms des x dans 0 AS x |
|
| 139 | - // on remplace dans $suite le nom par vide() |
|
| 140 | - preg_match_all('/\b0 AS\s*([^\s,]+)/', $this->query, $matches, PREG_PATTERN_ORDER); |
|
| 141 | - foreach ($matches[1] as $m) { |
|
| 142 | - if (strpos($suite, $m) !== false) { |
|
| 143 | - $suite = preg_replace(",\b$m\b,", 'VIDE()', $suite); |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - $this->query .= $suite; |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - // Correction possible des divisions entieres |
|
| 151 | - // Le standard SQL (lequel? ou?) semble indiquer que |
|
| 152 | - // a/b=c doit donner c entier si a et b sont entiers 4/3=1. |
|
| 153 | - // C'est ce que retournent effectivement SQL Server et SQLite |
|
| 154 | - // Ce n'est pas ce qu'applique MySQL qui retourne un reel : 4/3=1.333... |
|
| 155 | - // |
|
| 156 | - // On peut forcer la conversion en multipliant par 1.0 avant la division |
|
| 157 | - // /!\ SQLite 3.5.9 Debian/Ubuntu est victime d'un bug en plus ! |
|
| 158 | - // cf. https://bugs.launchpad.net/ubuntu/+source/sqlite3/+bug/254228 |
|
| 159 | - // http://www.sqlite.org/cvstrac/tktview?tn=3202 |
|
| 160 | - // (4*1.0/3) n'est pas rendu dans ce cas ! |
|
| 161 | - # $this->query = str_replace('/','* 1.00 / ',$this->query); |
|
| 162 | - |
|
| 163 | - |
|
| 164 | - // Correction critere REGEXP, non reconnu en sqlite2 |
|
| 165 | - if (($this->sqlite_version == 2) && (strpos($this->query, 'REGEXP') !== false)) { |
|
| 166 | - $this->query = preg_replace('/([^\s\(]*)(\s*)REGEXP(\s*)([^\s\)]*)/', 'REGEXP($4, $1)', $this->query); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - // |
|
| 170 | - // 3) Remise en place des textes d'origine |
|
| 171 | - // |
|
| 172 | - // Correction Antiquotes et echappements |
|
| 173 | - // ` => rien |
|
| 174 | - if (strpos($this->query, '`') !== false) { |
|
| 175 | - $this->query = str_replace('`', '', $this->query); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - $this->query = query_reinjecte_textes($this->query, $textes); |
|
| 179 | - |
|
| 180 | - return $this->query; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Callback pour remplacer `DATE_` / `INTERVAL` |
|
| 185 | - * par `DATE ... strtotime` |
|
| 186 | - * |
|
| 187 | - * @param array $matches Captures |
|
| 188 | - * @return string texte de date compris par SQLite |
|
| 189 | - */ |
|
| 190 | - public function _remplacerDateParTime($matches) |
|
| 191 | - { |
|
| 192 | - $op = strtoupper($matches[1] == 'ADD') ? '+' : '-'; |
|
| 193 | - |
|
| 194 | - return "datetime$matches[2] '$op$matches[3] $matches[4]')"; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Callback pour remplacer `FIELD(table,i,j,k...)` |
|
| 199 | - * par `CASE WHEN table=i THEN n ... ELSE 0 END` |
|
| 200 | - * |
|
| 201 | - * @param array $matches Captures |
|
| 202 | - * @return string texte de liste ordonnée compris par SQLite |
|
| 203 | - */ |
|
| 204 | - public function _remplacerFieldParCase($matches) |
|
| 205 | - { |
|
| 206 | - $fields = substr($matches[0], 6, -1); // ne recuperer que l'interieur X de field(X) |
|
| 207 | - $t = explode(',', $fields); |
|
| 208 | - $index = array_shift($t); |
|
| 209 | - |
|
| 210 | - $res = ''; |
|
| 211 | - $n = 0; |
|
| 212 | - foreach ($t as $v) { |
|
| 213 | - $n++; |
|
| 214 | - $res .= "\nWHEN $index=$v THEN $n"; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - return "CASE $res ELSE 0 END "; |
|
| 218 | - } |
|
| 12 | + /** @var string $query texte de la requête */ |
|
| 13 | + public $query = ''; |
|
| 14 | + |
|
| 15 | + /** @var string $prefixe Préfixe des tables */ |
|
| 16 | + public $prefixe = ''; |
|
| 17 | + |
|
| 18 | + /** @var string $sqlite_version Version de sqlite (2 ou 3) */ |
|
| 19 | + public $sqlite_version = ''; |
|
| 20 | + |
|
| 21 | + /** Pour les corrections à effectuer sur les requêtes : array(code=>'texte') trouvé |
|
| 22 | + * |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + public $textes = []; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Constructeur |
|
| 29 | + * |
|
| 30 | + * @param string $query Requête à préparer |
|
| 31 | + * @param string $prefixe Prefixe des tables à utiliser |
|
| 32 | + * @param string $sqlite_version Version SQLite (2 ou 3) |
|
| 33 | + */ |
|
| 34 | + public function __construct($query, $prefixe, $sqlite_version) |
|
| 35 | + { |
|
| 36 | + $this->query = $query; |
|
| 37 | + $this->prefixe = $prefixe; |
|
| 38 | + $this->sqlite_version = $sqlite_version; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Transformer la requete pour SQLite |
|
| 43 | + * |
|
| 44 | + * Enlève les textes, transforme la requête pour quelle soit |
|
| 45 | + * bien interprétée par SQLite, puis remet les textes |
|
| 46 | + * la fonction affecte `$this->query` |
|
| 47 | + */ |
|
| 48 | + public function traduire_requete() |
|
| 49 | + { |
|
| 50 | + // |
|
| 51 | + // 1) Protection des textes en les remplacant par des codes |
|
| 52 | + // |
|
| 53 | + // enlever les 'textes' et initialiser avec |
|
| 54 | + list($this->query, $textes) = query_echappe_textes($this->query); |
|
| 55 | + |
|
| 56 | + // |
|
| 57 | + // 2) Corrections de la requete |
|
| 58 | + // |
|
| 59 | + // Correction Create Database |
|
| 60 | + // Create Database -> requete ignoree |
|
| 61 | + if (strpos($this->query, 'CREATE DATABASE') === 0) { |
|
| 62 | + spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.' . _LOG_AVERTISSEMENT); |
|
| 63 | + $this->query = 'SELECT 1'; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + // Correction Insert Ignore |
|
| 67 | + // INSERT IGNORE -> insert (tout court et pas 'insert or replace') |
|
| 68 | + if (strpos($this->query, 'INSERT IGNORE') === 0) { |
|
| 69 | + spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.' . _LOG_DEBUG); |
|
| 70 | + $this->query = 'INSERT ' . substr($this->query, '13'); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + // Correction des dates avec INTERVAL |
|
| 74 | + // utiliser sql_date_proche() de preference |
|
| 75 | + if (strpos($this->query, 'INTERVAL') !== false) { |
|
| 76 | + $this->query = preg_replace_callback( |
|
| 77 | + '/DATE_(ADD|SUB)(.*)INTERVAL\s+(\d+)\s+([a-zA-Z]+)\)/U', |
|
| 78 | + [&$this, '_remplacerDateParTime'], |
|
| 79 | + $this->query |
|
| 80 | + ); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + if (strpos($this->query, 'LEFT(') !== false) { |
|
| 84 | + $this->query = str_replace('LEFT(', '_LEFT(', $this->query); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + if (strpos($this->query, 'TIMESTAMPDIFF(') !== false) { |
|
| 88 | + $this->query = preg_replace('/TIMESTAMPDIFF\(\s*([^,]*)\s*,/Uims', "TIMESTAMPDIFF('\\1',", $this->query); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + // Correction Using |
|
| 93 | + // USING (non reconnu en sqlite2) |
|
| 94 | + // problematique car la jointure ne se fait pas du coup. |
|
| 95 | + if (($this->sqlite_version == 2) && (strpos($this->query, 'USING') !== false)) { |
|
| 96 | + spip_log( |
|
| 97 | + "'USING (champ)' n'est pas reconnu en SQLite 2. Utilisez 'ON table1.champ = table2.champ'", |
|
| 98 | + 'sqlite.' . _LOG_ERREUR |
|
| 99 | + ); |
|
| 100 | + $this->query = preg_replace('/USING\s*\([^\)]*\)/', '', $this->query); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + // Correction Field |
|
| 104 | + // remplace FIELD(table,i,j,k...) par CASE WHEN table=i THEN n ... ELSE 0 END |
|
| 105 | + if (strpos($this->query, 'FIELD') !== false) { |
|
| 106 | + $this->query = preg_replace_callback( |
|
| 107 | + '/FIELD\s*\(([^\)]*)\)/', |
|
| 108 | + [&$this, '_remplacerFieldParCase'], |
|
| 109 | + $this->query |
|
| 110 | + ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + // Correction des noms de tables FROM |
|
| 114 | + // mettre les bons noms de table dans from, update, insert, replace... |
|
| 115 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/iS', $this->query, $regs)) { |
|
| 116 | + $suite = strstr($this->query, $regs[0]); |
|
| 117 | + $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 118 | + } else { |
|
| 119 | + $suite = ''; |
|
| 120 | + } |
|
| 121 | + $pref = ($this->prefixe) ? $this->prefixe . '_' : ''; |
|
| 122 | + $this->query = preg_replace('/([,\s])spip_/S', '\1' . $pref, $this->query) . $suite; |
|
| 123 | + |
|
| 124 | + // Correction zero AS x |
|
| 125 | + // pg n'aime pas 0+x AS alias, sqlite, dans le meme style, |
|
| 126 | + // n'apprecie pas du tout SELECT 0 as x ... ORDER BY x |
|
| 127 | + // il dit que x ne doit pas être un integer dans le order by ! |
|
| 128 | + // on remplace du coup x par vide() dans ce cas uniquement |
|
| 129 | + // |
|
| 130 | + // apparait dans public/vertebrer.php et dans le plugin menu aussi qui genere aussi ce genre de requete via un {par num #GET{tri_num}} |
|
| 131 | + // mais est-ce encore un soucis pour sqlite en 2021 ? (ie commenter le preg_replace marche très bien en sqlite 3.28) |
|
| 132 | + if ((strpos($this->query, '0 AS') !== false)) { |
|
| 133 | + // on ne remplace que dans ORDER BY ou GROUP BY |
|
| 134 | + if (preg_match('/\s(ORDER|GROUP) BY\s/i', $this->query, $regs)) { |
|
| 135 | + $suite = strstr($this->query, $regs[0]); |
|
| 136 | + $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 137 | + |
|
| 138 | + // on cherche les noms des x dans 0 AS x |
|
| 139 | + // on remplace dans $suite le nom par vide() |
|
| 140 | + preg_match_all('/\b0 AS\s*([^\s,]+)/', $this->query, $matches, PREG_PATTERN_ORDER); |
|
| 141 | + foreach ($matches[1] as $m) { |
|
| 142 | + if (strpos($suite, $m) !== false) { |
|
| 143 | + $suite = preg_replace(",\b$m\b,", 'VIDE()', $suite); |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + $this->query .= $suite; |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + // Correction possible des divisions entieres |
|
| 151 | + // Le standard SQL (lequel? ou?) semble indiquer que |
|
| 152 | + // a/b=c doit donner c entier si a et b sont entiers 4/3=1. |
|
| 153 | + // C'est ce que retournent effectivement SQL Server et SQLite |
|
| 154 | + // Ce n'est pas ce qu'applique MySQL qui retourne un reel : 4/3=1.333... |
|
| 155 | + // |
|
| 156 | + // On peut forcer la conversion en multipliant par 1.0 avant la division |
|
| 157 | + // /!\ SQLite 3.5.9 Debian/Ubuntu est victime d'un bug en plus ! |
|
| 158 | + // cf. https://bugs.launchpad.net/ubuntu/+source/sqlite3/+bug/254228 |
|
| 159 | + // http://www.sqlite.org/cvstrac/tktview?tn=3202 |
|
| 160 | + // (4*1.0/3) n'est pas rendu dans ce cas ! |
|
| 161 | + # $this->query = str_replace('/','* 1.00 / ',$this->query); |
|
| 162 | + |
|
| 163 | + |
|
| 164 | + // Correction critere REGEXP, non reconnu en sqlite2 |
|
| 165 | + if (($this->sqlite_version == 2) && (strpos($this->query, 'REGEXP') !== false)) { |
|
| 166 | + $this->query = preg_replace('/([^\s\(]*)(\s*)REGEXP(\s*)([^\s\)]*)/', 'REGEXP($4, $1)', $this->query); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + // |
|
| 170 | + // 3) Remise en place des textes d'origine |
|
| 171 | + // |
|
| 172 | + // Correction Antiquotes et echappements |
|
| 173 | + // ` => rien |
|
| 174 | + if (strpos($this->query, '`') !== false) { |
|
| 175 | + $this->query = str_replace('`', '', $this->query); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + $this->query = query_reinjecte_textes($this->query, $textes); |
|
| 179 | + |
|
| 180 | + return $this->query; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Callback pour remplacer `DATE_` / `INTERVAL` |
|
| 185 | + * par `DATE ... strtotime` |
|
| 186 | + * |
|
| 187 | + * @param array $matches Captures |
|
| 188 | + * @return string texte de date compris par SQLite |
|
| 189 | + */ |
|
| 190 | + public function _remplacerDateParTime($matches) |
|
| 191 | + { |
|
| 192 | + $op = strtoupper($matches[1] == 'ADD') ? '+' : '-'; |
|
| 193 | + |
|
| 194 | + return "datetime$matches[2] '$op$matches[3] $matches[4]')"; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Callback pour remplacer `FIELD(table,i,j,k...)` |
|
| 199 | + * par `CASE WHEN table=i THEN n ... ELSE 0 END` |
|
| 200 | + * |
|
| 201 | + * @param array $matches Captures |
|
| 202 | + * @return string texte de liste ordonnée compris par SQLite |
|
| 203 | + */ |
|
| 204 | + public function _remplacerFieldParCase($matches) |
|
| 205 | + { |
|
| 206 | + $fields = substr($matches[0], 6, -1); // ne recuperer que l'interieur X de field(X) |
|
| 207 | + $t = explode(',', $fields); |
|
| 208 | + $index = array_shift($t); |
|
| 209 | + |
|
| 210 | + $res = ''; |
|
| 211 | + $n = 0; |
|
| 212 | + foreach ($t as $v) { |
|
| 213 | + $n++; |
|
| 214 | + $res .= "\nWHEN $index=$v THEN $n"; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + return "CASE $res ELSE 0 END "; |
|
| 218 | + } |
|
| 219 | 219 | } |
@@ -59,15 +59,15 @@ discard block |
||
| 59 | 59 | // Correction Create Database |
| 60 | 60 | // Create Database -> requete ignoree |
| 61 | 61 | if (strpos($this->query, 'CREATE DATABASE') === 0) { |
| 62 | - spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.' . _LOG_AVERTISSEMENT); |
|
| 62 | + spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.'._LOG_AVERTISSEMENT); |
|
| 63 | 63 | $this->query = 'SELECT 1'; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | // Correction Insert Ignore |
| 67 | 67 | // INSERT IGNORE -> insert (tout court et pas 'insert or replace') |
| 68 | 68 | if (strpos($this->query, 'INSERT IGNORE') === 0) { |
| 69 | - spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.' . _LOG_DEBUG); |
|
| 70 | - $this->query = 'INSERT ' . substr($this->query, '13'); |
|
| 69 | + spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.'._LOG_DEBUG); |
|
| 70 | + $this->query = 'INSERT '.substr($this->query, '13'); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | // Correction des dates avec INTERVAL |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | if (($this->sqlite_version == 2) && (strpos($this->query, 'USING') !== false)) { |
| 96 | 96 | spip_log( |
| 97 | 97 | "'USING (champ)' n'est pas reconnu en SQLite 2. Utilisez 'ON table1.champ = table2.champ'", |
| 98 | - 'sqlite.' . _LOG_ERREUR |
|
| 98 | + 'sqlite.'._LOG_ERREUR |
|
| 99 | 99 | ); |
| 100 | 100 | $this->query = preg_replace('/USING\s*\([^\)]*\)/', '', $this->query); |
| 101 | 101 | } |
@@ -118,8 +118,8 @@ discard block |
||
| 118 | 118 | } else { |
| 119 | 119 | $suite = ''; |
| 120 | 120 | } |
| 121 | - $pref = ($this->prefixe) ? $this->prefixe . '_' : ''; |
|
| 122 | - $this->query = preg_replace('/([,\s])spip_/S', '\1' . $pref, $this->query) . $suite; |
|
| 121 | + $pref = ($this->prefixe) ? $this->prefixe.'_' : ''; |
|
| 122 | + $this->query = preg_replace('/([,\s])spip_/S', '\1'.$pref, $this->query).$suite; |
|
| 123 | 123 | |
| 124 | 124 | // Correction zero AS x |
| 125 | 125 | // pg n'aime pas 0+x AS alias, sqlite, dans le meme style, |
@@ -67,35 +67,35 @@ discard block |
||
| 67 | 67 | // determiner le dossier de la base : $addr ou _DIR_DB |
| 68 | 68 | $f = _DIR_DB; |
| 69 | 69 | if ($addr and str_contains($addr, '/')) { |
| 70 | - $f = rtrim($addr, '/') . '/'; |
|
| 70 | + $f = rtrim($addr, '/').'/'; |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | // un nom de base demande et impossible d'obtenir la base, on s'en va : |
| 74 | 74 | // il faut que la base existe ou que le repertoire parent soit writable |
| 75 | - if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 76 | - spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 75 | + if ($db and !is_file($f .= $db.'.sqlite') and !is_writable(dirname($f))) { |
|
| 76 | + spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.'._LOG_HS); |
|
| 77 | 77 | |
| 78 | 78 | return false; |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | // charger les modules sqlite au besoin |
| 82 | 82 | if (!_sqlite_charger_version($sqlite_version)) { |
| 83 | - spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 83 | + spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.'._LOG_HS); |
|
| 84 | 84 | |
| 85 | 85 | return false; |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | // chargement des constantes |
| 89 | 89 | // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
| 90 | - $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 90 | + $define = 'spip_sqlite'.$sqlite_version.'_constantes'; |
|
| 91 | 91 | $define(); |
| 92 | 92 | |
| 93 | 93 | if (!$db) { |
| 94 | 94 | // si pas de db -> |
| 95 | 95 | // base temporaire tant qu'on ne connait pas son vrai nom |
| 96 | 96 | // pour tester la connexion |
| 97 | - $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 98 | - $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 97 | + $db = '_sqlite'.$sqlite_version.'_install'; |
|
| 98 | + $tmp = _DIR_DB.$db.'.sqlite'; |
|
| 99 | 99 | $link = spip_sqlite_open($tmp); |
| 100 | 100 | } else { |
| 101 | 101 | // Ouvrir (eventuellement creer la base) |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | if (!$link) { |
| 106 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f", 'sqlite.' . _LOG_HS); |
|
| 106 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f", 'sqlite.'._LOG_HS); |
|
| 107 | 107 | |
| 108 | 108 | return false; |
| 109 | 109 | } |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | */ |
| 139 | 139 | function spip_sqlite_open(string $file): \PDO { |
| 140 | 140 | $PDO = new \PDO("sqlite:$file"); |
| 141 | - $PDO->setAttribute(\PDO::ATTR_STATEMENT_CLASS , [\Spip\Sql\Sqlite\PDOStatement::class, [&$PDO]]); |
|
| 141 | + $PDO->setAttribute(\PDO::ATTR_STATEMENT_CLASS, [\Spip\Sql\Sqlite\PDOStatement::class, [&$PDO]]); |
|
| 142 | 142 | return $PDO; |
| 143 | 143 | } |
| 144 | 144 | |
@@ -203,7 +203,7 @@ discard block |
||
| 203 | 203 | $table = $regs[3]; |
| 204 | 204 | $suite = $regs[4]; |
| 205 | 205 | } else { |
| 206 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 206 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.'._LOG_ERREUR); |
|
| 207 | 207 | |
| 208 | 208 | return false; |
| 209 | 209 | } |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | $i = 0; |
| 221 | 221 | $ouverte = false; |
| 222 | 222 | while ($do = array_shift($todo)) { |
| 223 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 223 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i].','.$do : $do; |
|
| 224 | 224 | $o = (str_contains($do, '(')); |
| 225 | 225 | $f = (str_contains($do, ')')); |
| 226 | 226 | if ($o and !$f) { |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | ) { |
| 247 | 247 | spip_log( |
| 248 | 248 | "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
| 249 | - 'sqlite.' . _LOG_ERREUR |
|
| 249 | + 'sqlite.'._LOG_ERREUR |
|
| 250 | 250 | ); |
| 251 | 251 | |
| 252 | 252 | return false; |
@@ -342,10 +342,10 @@ discard block |
||
| 342 | 342 | |
| 343 | 343 | // pas geres en sqlite2 |
| 344 | 344 | case 'RENAME': |
| 345 | - $do = 'RENAME TO' . substr($do, 6); |
|
| 345 | + $do = 'RENAME TO'.substr($do, 6); |
|
| 346 | 346 | case 'RENAME TO': |
| 347 | 347 | if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
| 348 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 348 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.'._LOG_ERREUR); |
|
| 349 | 349 | |
| 350 | 350 | return false; |
| 351 | 351 | } |
@@ -388,7 +388,7 @@ discard block |
||
| 388 | 388 | $colonnes = substr($colonne_origine, 1, -1); |
| 389 | 389 | if (str_contains(',', $colonnes)) { |
| 390 | 390 | spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
| 391 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 391 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.'._LOG_ERREUR); |
|
| 392 | 392 | break; |
| 393 | 393 | } else { |
| 394 | 394 | $nom_index = $colonnes; |
@@ -403,12 +403,12 @@ discard block |
||
| 403 | 403 | |
| 404 | 404 | // pas geres en sqlite2 |
| 405 | 405 | case 'ADD COLUMN': |
| 406 | - $do = 'ADD' . substr($do, 10); |
|
| 406 | + $do = 'ADD'.substr($do, 10); |
|
| 407 | 407 | case 'ADD': |
| 408 | 408 | default: |
| 409 | 409 | if (!preg_match(',primary\s+key,i', $do)) { |
| 410 | 410 | if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
| 411 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 411 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.'._LOG_ERREUR); |
|
| 412 | 412 | |
| 413 | 413 | return false; |
| 414 | 414 | } |
@@ -428,7 +428,7 @@ discard block |
||
| 428 | 428 | } |
| 429 | 429 | $opts['field'] = [$colonne_ajoutee => $def]; |
| 430 | 430 | if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
| 431 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 431 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.'._LOG_ERREUR); |
|
| 432 | 432 | |
| 433 | 433 | return false; |
| 434 | 434 | } |
@@ -436,10 +436,10 @@ discard block |
||
| 436 | 436 | break; |
| 437 | 437 | } |
| 438 | 438 | // tout est bon, ouf ! |
| 439 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 439 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.'._LOG_INFO); |
|
| 440 | 440 | } |
| 441 | 441 | |
| 442 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 442 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.'._LOG_INFO); |
|
| 443 | 443 | |
| 444 | 444 | return true; |
| 445 | 445 | } |
@@ -508,9 +508,9 @@ discard block |
||
| 508 | 508 | **/ |
| 509 | 509 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) |
| 510 | 510 | { |
| 511 | - $f = $nom . '.sqlite'; |
|
| 511 | + $f = $nom.'.sqlite'; |
|
| 512 | 512 | if (strpos($nom, '/') === false) { |
| 513 | - $f = _DIR_DB . $f; |
|
| 513 | + $f = _DIR_DB.$f; |
|
| 514 | 514 | } |
| 515 | 515 | |
| 516 | 516 | $ok = new \PDO("sqlite:$f"); |
@@ -551,13 +551,13 @@ discard block |
||
| 551 | 551 | if (sql_showtable($nom, false, $serveur)) { |
| 552 | 552 | spip_log( |
| 553 | 553 | "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
| 554 | - 'sqlite.' . _LOG_ERREUR |
|
| 554 | + 'sqlite.'._LOG_ERREUR |
|
| 555 | 555 | ); |
| 556 | 556 | |
| 557 | 557 | return false; |
| 558 | 558 | } |
| 559 | 559 | |
| 560 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 560 | + $query = "CREATE VIEW $nom AS ".$query_select; |
|
| 561 | 561 | |
| 562 | 562 | return spip_sqlite_query($query, $serveur, $requeter); |
| 563 | 563 | } |
@@ -584,8 +584,8 @@ discard block |
||
| 584 | 584 | { |
| 585 | 585 | if (!($nom or $table or $champs)) { |
| 586 | 586 | spip_log( |
| 587 | - "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 588 | - 'sqlite.' . _LOG_ERREUR |
|
| 587 | + "Champ manquant pour creer un index sqlite ($nom, $table, (".join(',', $champs).'))', |
|
| 588 | + 'sqlite.'._LOG_ERREUR |
|
| 589 | 589 | ); |
| 590 | 590 | |
| 591 | 591 | return false; |
@@ -593,7 +593,7 @@ discard block |
||
| 593 | 593 | |
| 594 | 594 | // SQLite ne differentie pas noms des index en fonction des tables |
| 595 | 595 | // il faut donc creer des noms uniques d'index pour une base sqlite |
| 596 | - $nom = $table . '_' . $nom; |
|
| 596 | + $nom = $table.'_'.$nom; |
|
| 597 | 597 | // enlever d'eventuelles parentheses deja presentes sur champs |
| 598 | 598 | if (!is_array($champs)) { |
| 599 | 599 | if ($champs[0] == '(') { |
@@ -615,12 +615,12 @@ discard block |
||
| 615 | 615 | } else { |
| 616 | 616 | /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
| 617 | 617 | $a = spip_sqlite_showtable($table, $serveur); |
| 618 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 618 | + if (isset($a['key']['KEY '.$nom])) { |
|
| 619 | 619 | return true; |
| 620 | 620 | } |
| 621 | 621 | } |
| 622 | 622 | |
| 623 | - $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 623 | + $query = 'CREATE '.($unique ? 'UNIQUE ' : '')."INDEX$ifnotexists $nom ON $table (".join(',', $champs).')'; |
|
| 624 | 624 | $res = spip_sqlite_query($query, $serveur, $requeter); |
| 625 | 625 | if (!$requeter) { |
| 626 | 626 | return $res; |
@@ -689,7 +689,7 @@ discard block |
||
| 689 | 689 | $serveur = '', |
| 690 | 690 | $requeter = true |
| 691 | 691 | ) { |
| 692 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 692 | + $c = !$groupby ? '*' : ('DISTINCT '.(is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 693 | 693 | $r = spip_sqlite_select( |
| 694 | 694 | "COUNT($c)", |
| 695 | 695 | $from, |
@@ -803,14 +803,14 @@ discard block |
||
| 803 | 803 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) |
| 804 | 804 | { |
| 805 | 805 | if (!($nom or $table)) { |
| 806 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 806 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.'._LOG_ERREUR); |
|
| 807 | 807 | |
| 808 | 808 | return false; |
| 809 | 809 | } |
| 810 | 810 | |
| 811 | 811 | // SQLite ne differentie pas noms des index en fonction des tables |
| 812 | 812 | // il faut donc creer des noms uniques d'index pour une base sqlite |
| 813 | - $index = $table . '_' . $nom; |
|
| 813 | + $index = $table.'_'.$nom; |
|
| 814 | 814 | $exist = ' IF EXISTS'; |
| 815 | 815 | |
| 816 | 816 | $query = "DROP INDEX$exist $index"; |
@@ -843,7 +843,7 @@ discard block |
||
| 843 | 843 | if ($s) { |
| 844 | 844 | $trace = debug_backtrace(); |
| 845 | 845 | if ($trace[0]['function'] != 'spip_sqlite_error') { |
| 846 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 846 | + spip_log("$s - $query - ".sql_error_backtrace(), 'sqlite.'._LOG_ERREUR); |
|
| 847 | 847 | } |
| 848 | 848 | } |
| 849 | 849 | |
@@ -892,14 +892,14 @@ discard block |
||
| 892 | 892 | $t = $link->errorInfo(); |
| 893 | 893 | $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
| 894 | 894 | if ($s) { |
| 895 | - $s .= ' / ' . $t[1]; |
|
| 895 | + $s .= ' / '.$t[1]; |
|
| 896 | 896 | } // ajoute l'erreur du moteur SQLite |
| 897 | 897 | } else { |
| 898 | 898 | $s = ': aucune ressource sqlite (link)'; |
| 899 | 899 | } |
| 900 | 900 | |
| 901 | 901 | if ($s) { |
| 902 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 902 | + spip_log("Erreur sqlite $s", 'sqlite.'._LOG_ERREUR); |
|
| 903 | 903 | } |
| 904 | 904 | |
| 905 | 905 | return $s ? $s : 0; |
@@ -924,7 +924,7 @@ discard block |
||
| 924 | 924 | } |
| 925 | 925 | |
| 926 | 926 | $query = Sqlite::traduire_requete($query, $serveur); |
| 927 | - $query = 'EXPLAIN ' . $query; |
|
| 927 | + $query = 'EXPLAIN '.$query; |
|
| 928 | 928 | if (!$requeter) { |
| 929 | 929 | return $query; |
| 930 | 930 | } |
@@ -1100,7 +1100,7 @@ discard block |
||
| 1100 | 1100 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) |
| 1101 | 1101 | { |
| 1102 | 1102 | |
| 1103 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1103 | + $query = "INSERT INTO $table ".($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1104 | 1104 | if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
| 1105 | 1105 | if (!$requeter) { |
| 1106 | 1106 | return $r; |
@@ -1156,8 +1156,8 @@ discard block |
||
| 1156 | 1156 | |
| 1157 | 1157 | $cles = $valeurs = ''; |
| 1158 | 1158 | if (count($couples)) { |
| 1159 | - $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1160 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1159 | + $cles = '('.join(',', array_keys($couples)).')'; |
|
| 1160 | + $valeurs = '('.join(',', $couples).')'; |
|
| 1161 | 1161 | } |
| 1162 | 1162 | |
| 1163 | 1163 | return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
@@ -1218,11 +1218,11 @@ discard block |
||
| 1218 | 1218 | |
| 1219 | 1219 | $champs = $valeurs = ''; |
| 1220 | 1220 | if (count($couples)) { |
| 1221 | - $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1222 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1223 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1221 | + $champs = '('.join(',', array_keys($couples)).')'; |
|
| 1222 | + $valeurs = '('.join(',', $couples).')'; |
|
| 1223 | + $query = $query_start."$champs VALUES $valeurs"; |
|
| 1224 | 1224 | } else { |
| 1225 | - $query = $query_start . 'DEFAULT VALUES'; |
|
| 1225 | + $query = $query_start.'DEFAULT VALUES'; |
|
| 1226 | 1226 | } |
| 1227 | 1227 | |
| 1228 | 1228 | if ($requeter) { |
@@ -1359,7 +1359,7 @@ discard block |
||
| 1359 | 1359 | */ |
| 1360 | 1360 | function spip_sqlite_multi($objet, $lang) |
| 1361 | 1361 | { |
| 1362 | - $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1362 | + $r = 'EXTRAIRE_MULTI('.$objet.", '".$lang."') AS multi"; |
|
| 1363 | 1363 | |
| 1364 | 1364 | return $r; |
| 1365 | 1365 | } |
@@ -1433,7 +1433,7 @@ discard block |
||
| 1433 | 1433 | { |
| 1434 | 1434 | $op = (($interval <= 0) ? '>' : '<'); |
| 1435 | 1435 | |
| 1436 | - return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1436 | + return "($champ $op datetime('".date('Y-m-d H:i:s')."', '$interval $unite'))"; |
|
| 1437 | 1437 | } |
| 1438 | 1438 | |
| 1439 | 1439 | |
@@ -1466,7 +1466,7 @@ discard block |
||
| 1466 | 1466 | and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
| 1467 | 1467 | ) { |
| 1468 | 1468 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
| 1469 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1469 | + spip_log("ALTER $q", 'repair'._LOG_INFO_IMPORTANTE); |
|
| 1470 | 1470 | } |
| 1471 | 1471 | if ( |
| 1472 | 1472 | preg_match(',^(INTEGER),i', $d) |
@@ -1476,7 +1476,7 @@ discard block |
||
| 1476 | 1476 | and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
| 1477 | 1477 | ) { |
| 1478 | 1478 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
| 1479 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1479 | + spip_log("ALTER $q", 'repair'._LOG_INFO_IMPORTANTE); |
|
| 1480 | 1480 | } |
| 1481 | 1481 | if ( |
| 1482 | 1482 | preg_match(',^(datetime),i', $d) |
@@ -1486,7 +1486,7 @@ discard block |
||
| 1486 | 1486 | and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
| 1487 | 1487 | ) { |
| 1488 | 1488 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
| 1489 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1489 | + spip_log("ALTER $q", 'repair'._LOG_INFO_IMPORTANTE); |
|
| 1490 | 1490 | } |
| 1491 | 1491 | } |
| 1492 | 1492 | |
@@ -1538,10 +1538,10 @@ discard block |
||
| 1538 | 1538 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
| 1539 | 1539 | $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
| 1540 | 1540 | |
| 1541 | - return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1541 | + return spip_sqlite_query("REPLACE INTO $table (".join(',', array_keys($couples)).') VALUES ('.join( |
|
| 1542 | 1542 | ',', |
| 1543 | 1543 | $couples |
| 1544 | - ) . ')', $serveur); |
|
| 1544 | + ).')', $serveur); |
|
| 1545 | 1545 | } |
| 1546 | 1546 | |
| 1547 | 1547 | |
@@ -1628,7 +1628,7 @@ discard block |
||
| 1628 | 1628 | . _sqlite_calculer_expression('WHERE', $where) |
| 1629 | 1629 | . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
| 1630 | 1630 | . _sqlite_calculer_expression('HAVING', $having) |
| 1631 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1631 | + . ($orderby ? ("\nORDER BY "._sqlite_calculer_order($orderby)) : '') |
|
| 1632 | 1632 | . ($limit ? "\nLIMIT $limit" : ''); |
| 1633 | 1633 | |
| 1634 | 1634 | // dans un select, on doit renvoyer la requête en cas d'erreur |
@@ -1667,10 +1667,10 @@ discard block |
||
| 1667 | 1667 | // interdire la creation d'une nouvelle base, |
| 1668 | 1668 | // sauf si on est dans l'installation |
| 1669 | 1669 | if ( |
| 1670 | - !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1670 | + !is_file($f = _DIR_DB.$db.'.sqlite') |
|
| 1671 | 1671 | && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
| 1672 | 1672 | ) { |
| 1673 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1673 | + spip_log("Il est interdit de creer la base $db", 'sqlite.'._LOG_HS); |
|
| 1674 | 1674 | |
| 1675 | 1675 | return false; |
| 1676 | 1676 | } |
@@ -1679,12 +1679,12 @@ discard block |
||
| 1679 | 1679 | // avec les identifiants connus |
| 1680 | 1680 | $index = $serveur ? $serveur : 0; |
| 1681 | 1681 | |
| 1682 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1682 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@'.$db, $serveur, '', '')) { |
|
| 1683 | 1683 | if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
| 1684 | 1684 | return $db; |
| 1685 | 1685 | } |
| 1686 | 1686 | } else { |
| 1687 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1687 | + spip_log("Impossible de selectionner la base $db", 'sqlite.'._LOG_HS); |
|
| 1688 | 1688 | } |
| 1689 | 1689 | |
| 1690 | 1690 | return false; |
@@ -1737,7 +1737,7 @@ discard block |
||
| 1737 | 1737 | $match = "^$match$"; |
| 1738 | 1738 | |
| 1739 | 1739 | return spip_sqlite_query( |
| 1740 | - "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1740 | + "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP "._q($match), |
|
| 1741 | 1741 | $serveur, |
| 1742 | 1742 | $requeter |
| 1743 | 1743 | ); |
@@ -1762,7 +1762,7 @@ discard block |
||
| 1762 | 1762 | $r = spip_sqlite_query( |
| 1763 | 1763 | 'SELECT name FROM sqlite_master WHERE' |
| 1764 | 1764 | . ' type=\'table\'' |
| 1765 | - . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1765 | + . ' AND name='.spip_sqlite_quote($table, 'string') |
|
| 1766 | 1766 | . ' AND name NOT LIKE \'sqlite_%\'', |
| 1767 | 1767 | $serveur, |
| 1768 | 1768 | $requeter |
@@ -1860,7 +1860,7 @@ discard block |
||
| 1860 | 1860 | // s'il y a une parenthèse fermante dans la clé |
| 1861 | 1861 | // ou dans la définition sans qu'il n'y ait une ouverture avant |
| 1862 | 1862 | if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
| 1863 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1863 | + $fields[$k_precedent] .= ','.$k.' '.$def; |
|
| 1864 | 1864 | continue; |
| 1865 | 1865 | } |
| 1866 | 1866 | |
@@ -1895,13 +1895,13 @@ discard block |
||
| 1895 | 1895 | . 'ORDER BY substr(type,2,1), name'; |
| 1896 | 1896 | $a = spip_sqlite_query($query, $serveur, $requeter); |
| 1897 | 1897 | while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
| 1898 | - $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1898 | + $key = str_replace($nom_table.'_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1899 | 1899 | $keytype = 'KEY'; |
| 1900 | 1900 | if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
| 1901 | 1901 | $keytype = 'UNIQUE KEY'; |
| 1902 | 1902 | } |
| 1903 | 1903 | $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
| 1904 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1904 | + $keys[$keytype.' '.$key] = $colonnes; |
|
| 1905 | 1905 | } |
| 1906 | 1906 | } |
| 1907 | 1907 | } // c'est une vue, on liste les champs disponibles simplement |
@@ -1949,7 +1949,7 @@ discard block |
||
| 1949 | 1949 | |
| 1950 | 1950 | $set = []; |
| 1951 | 1951 | foreach ($champs as $champ => $val) { |
| 1952 | - $set[] = $champ . "=$val"; |
|
| 1952 | + $set[] = $champ."=$val"; |
|
| 1953 | 1953 | } |
| 1954 | 1954 | if (!empty($set)) { |
| 1955 | 1955 | return spip_sqlite_query( |
@@ -2005,7 +2005,7 @@ discard block |
||
| 2005 | 2005 | |
| 2006 | 2006 | $set = []; |
| 2007 | 2007 | foreach ($champs as $champ => $val) { |
| 2008 | - $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2008 | + $set[$champ] = $champ.'='._sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2009 | 2009 | } |
| 2010 | 2010 | |
| 2011 | 2011 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
@@ -2013,7 +2013,7 @@ discard block |
||
| 2013 | 2013 | $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
| 2014 | 2014 | foreach ($maj as $champ => $val) { |
| 2015 | 2015 | if (!isset($set[$champ])) { |
| 2016 | - $set[$champ] = $champ . '=' . $val; |
|
| 2016 | + $set[$champ] = $champ.'='.$val; |
|
| 2017 | 2017 | } |
| 2018 | 2018 | } |
| 2019 | 2019 | |
@@ -2043,7 +2043,7 @@ discard block |
||
| 2043 | 2043 | function _sqlite_init() |
| 2044 | 2044 | { |
| 2045 | 2045 | if (!defined('_DIR_DB')) { |
| 2046 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2046 | + define('_DIR_DB', _DIR_ETC.'bases/'); |
|
| 2047 | 2047 | } |
| 2048 | 2048 | if (!defined('_SQLITE_CHMOD')) { |
| 2049 | 2049 | define('_SQLITE_CHMOD', _SPIP_CHMOD); |
@@ -2155,9 +2155,9 @@ discard block |
||
| 2155 | 2155 | } |
| 2156 | 2156 | |
| 2157 | 2157 | // echapper les ' en '' |
| 2158 | - spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2158 | + spip_log('Pas de methode ->quote pour echapper', 'sqlite.'._LOG_INFO_IMPORTANTE); |
|
| 2159 | 2159 | |
| 2160 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2160 | + return ("'".str_replace("'", "''", $v)."'"); |
|
| 2161 | 2161 | } |
| 2162 | 2162 | |
| 2163 | 2163 | |
@@ -2181,12 +2181,12 @@ discard block |
||
| 2181 | 2181 | $exp = "\n$expression "; |
| 2182 | 2182 | |
| 2183 | 2183 | if (!is_array($v)) { |
| 2184 | - return $exp . $v; |
|
| 2184 | + return $exp.$v; |
|
| 2185 | 2185 | } else { |
| 2186 | 2186 | if (strtoupper($join) === 'AND') { |
| 2187 | - return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2187 | + return $exp.join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2188 | 2188 | } else { |
| 2189 | - return $exp . join($join, $v); |
|
| 2189 | + return $exp.join($join, $v); |
|
| 2190 | 2190 | } |
| 2191 | 2191 | } |
| 2192 | 2192 | } |
@@ -2222,17 +2222,17 @@ discard block |
||
| 2222 | 2222 | if (substr($k, -1) == '@') { |
| 2223 | 2223 | // c'est une jointure qui se refere au from precedent |
| 2224 | 2224 | // pas de virgule |
| 2225 | - $res .= ' ' . $v; |
|
| 2225 | + $res .= ' '.$v; |
|
| 2226 | 2226 | } else { |
| 2227 | 2227 | if (!is_numeric($k)) { |
| 2228 | 2228 | $p = strpos($v, ' '); |
| 2229 | 2229 | if ($p) { |
| 2230 | - $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2230 | + $v = substr($v, 0, $p)." AS '$k'".substr($v, $p); |
|
| 2231 | 2231 | } else { |
| 2232 | 2232 | $v .= " AS '$k'"; |
| 2233 | 2233 | } |
| 2234 | 2234 | } |
| 2235 | - $res .= ', ' . $v; |
|
| 2235 | + $res .= ', '.$v; |
|
| 2236 | 2236 | } |
| 2237 | 2237 | } |
| 2238 | 2238 | |
@@ -2373,13 +2373,13 @@ discard block |
||
| 2373 | 2373 | |
| 2374 | 2374 | $def_origine = sql_showtable($table_origine, false, $serveur); |
| 2375 | 2375 | if (!$def_origine or !isset($def_origine['field'])) { |
| 2376 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2376 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite'._LOG_ERREUR); |
|
| 2377 | 2377 | |
| 2378 | 2378 | return false; |
| 2379 | 2379 | } |
| 2380 | 2380 | |
| 2381 | 2381 | |
| 2382 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2382 | + $table_tmp = $table_origine.'_tmp'; |
|
| 2383 | 2383 | |
| 2384 | 2384 | // 1) creer une table temporaire avec les modifications |
| 2385 | 2385 | // - DROP : suppression de la colonne |
@@ -2466,7 +2466,7 @@ discard block |
||
| 2466 | 2466 | } else { |
| 2467 | 2467 | // enlever KEY |
| 2468 | 2468 | $k = substr($k, 4); |
| 2469 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2469 | + $queries[] = "CREATE INDEX $table_destination"."_$k ON $table_destination ($v)"; |
|
| 2470 | 2470 | } |
| 2471 | 2471 | } |
| 2472 | 2472 | |
@@ -2477,7 +2477,7 @@ discard block |
||
| 2477 | 2477 | foreach ($queries as $q) { |
| 2478 | 2478 | if (!Sqlite::executer_requete($q, $serveur)) { |
| 2479 | 2479 | spip_log('SQLite : ALTER TABLE table :' |
| 2480 | - . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2480 | + . " Erreur a l'execution de la requete : $q", 'sqlite.'._LOG_ERREUR); |
|
| 2481 | 2481 | Sqlite::annuler_transaction($serveur); |
| 2482 | 2482 | |
| 2483 | 2483 | return false; |
@@ -2569,27 +2569,27 @@ discard block |
||
| 2569 | 2569 | $enum = '(\s*\([^\)]*\))?'; |
| 2570 | 2570 | |
| 2571 | 2571 | $remplace = [ |
| 2572 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2572 | + '/enum'.$enum.'/is' => 'VARCHAR(255)', |
|
| 2573 | 2573 | '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
| 2574 | 2574 | '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
| 2575 | 2575 | '/auto_increment/is' => '', |
| 2576 | 2576 | '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
| 2577 | 2577 | '/(timestamp .* )ON .*$/is' => '\\1', |
| 2578 | 2578 | '/character set \w+/is' => '', |
| 2579 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2579 | + '/((big|small|medium|tiny)?int(eger)?)'.$num.'\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2580 | 2580 | '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
| 2581 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2581 | + '/((char|varchar)'.$num.'\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2582 | 2582 | '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
| 2583 | 2583 | '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
| 2584 | 2584 | ]; |
| 2585 | 2585 | |
| 2586 | 2586 | // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
| 2587 | 2587 | $remplace_autocinc = [ |
| 2588 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2588 | + '/(big|small|medium|tiny)?int(eger)?'.$num.'/is' => 'INTEGER' |
|
| 2589 | 2589 | ]; |
| 2590 | 2590 | // pour les int non autoincrement, il faut un DEFAULT |
| 2591 | 2591 | $remplace_nonautocinc = [ |
| 2592 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2592 | + '/((big|small|medium|tiny)?int(eger)?'.$num.'\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2593 | 2593 | ]; |
| 2594 | 2594 | |
| 2595 | 2595 | if (is_string($query)) { |
@@ -2632,7 +2632,7 @@ discard block |
||
| 2632 | 2632 | return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
| 2633 | 2633 | } |
| 2634 | 2634 | if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
| 2635 | - return $champ . ' COLLATE NOCASE'; |
|
| 2635 | + return $champ.' COLLATE NOCASE'; |
|
| 2636 | 2636 | } |
| 2637 | 2637 | |
| 2638 | 2638 | return $champ; |
@@ -2722,14 +2722,14 @@ discard block |
||
| 2722 | 2722 | } else { |
| 2723 | 2723 | /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
| 2724 | 2724 | $a = spip_sqlite_showtable($nom, $serveur); |
| 2725 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2725 | + if (isset($a['key']['KEY '.$nom])) { |
|
| 2726 | 2726 | return true; |
| 2727 | 2727 | } |
| 2728 | 2728 | } |
| 2729 | 2729 | } |
| 2730 | 2730 | |
| 2731 | 2731 | $temporary = $temporary ? ' TEMPORARY' : ''; |
| 2732 | - $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2732 | + $q = "CREATE$temporary TABLE$ifnotexists $nom ($query".($keys ? ",$keys" : '').")\n"; |
|
| 2733 | 2733 | |
| 2734 | 2734 | return $q; |
| 2735 | 2735 | } |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | |
| 22 | 22 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 23 | - return; |
|
| 23 | + return; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | // TODO: get/set_caracteres ? |
@@ -44,88 +44,88 @@ discard block |
||
| 44 | 44 | */ |
| 45 | 45 | function req_sqlite_dist($addr, $port, $login, #[\SensitiveParameter] $pass, $db = '', $prefixe = '', $sqlite_version = '') |
| 46 | 46 | { |
| 47 | - static $last_connect = []; |
|
| 48 | - |
|
| 49 | - // si provient de selectdb |
|
| 50 | - // un code pour etre sur que l'on vient de select_db() |
|
| 51 | - if (strpos($db, $code = '@selectdb@') !== false) { |
|
| 52 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 53 | - $$a = $last_connect[$a]; |
|
| 54 | - } |
|
| 55 | - $db = str_replace($code, '', $db); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /* |
|
| 47 | + static $last_connect = []; |
|
| 48 | + |
|
| 49 | + // si provient de selectdb |
|
| 50 | + // un code pour etre sur que l'on vient de select_db() |
|
| 51 | + if (strpos($db, $code = '@selectdb@') !== false) { |
|
| 52 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 53 | + $$a = $last_connect[$a]; |
|
| 54 | + } |
|
| 55 | + $db = str_replace($code, '', $db); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /* |
|
| 59 | 59 | * En sqlite, seule l'adresse du fichier est importante. |
| 60 | 60 | * Ce sera $db le nom, |
| 61 | 61 | * le path est $addr |
| 62 | 62 | * (_DIR_DB si $addr est vide) |
| 63 | 63 | */ |
| 64 | - _sqlite_init(); |
|
| 65 | - |
|
| 66 | - // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 67 | - $f = _DIR_DB; |
|
| 68 | - if ($addr and str_contains($addr, '/')) { |
|
| 69 | - $f = rtrim($addr, '/') . '/'; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 73 | - // il faut que la base existe ou que le repertoire parent soit writable |
|
| 74 | - if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 75 | - spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 76 | - |
|
| 77 | - return false; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - // charger les modules sqlite au besoin |
|
| 81 | - if (!_sqlite_charger_version($sqlite_version)) { |
|
| 82 | - spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 83 | - |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // chargement des constantes |
|
| 88 | - // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 89 | - $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 90 | - $define(); |
|
| 91 | - |
|
| 92 | - if (!$db) { |
|
| 93 | - // si pas de db -> |
|
| 94 | - // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 95 | - // pour tester la connexion |
|
| 96 | - $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 97 | - $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 98 | - $link = spip_sqlite_open($tmp); |
|
| 99 | - } else { |
|
| 100 | - // Ouvrir (eventuellement creer la base) |
|
| 101 | - $link = spip_sqlite_open($f); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - if (!$link) { |
|
| 105 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f", 'sqlite.' . _LOG_HS); |
|
| 106 | - |
|
| 107 | - return false; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $last_connect = [ |
|
| 111 | - 'addr' => $addr, |
|
| 112 | - 'port' => $port, |
|
| 113 | - 'login' => $login, |
|
| 114 | - 'pass' => $pass, |
|
| 115 | - 'db' => $db, |
|
| 116 | - 'prefixe' => $prefixe, |
|
| 117 | - ]; |
|
| 118 | - |
|
| 119 | - // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 120 | - include_spip('req/sqlite_fonctions'); |
|
| 121 | - _sqlite_init_functions($link); |
|
| 122 | - |
|
| 123 | - return [ |
|
| 124 | - 'db' => $db, |
|
| 125 | - 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 126 | - 'link' => $link, |
|
| 127 | - 'total_requetes' => 0, |
|
| 128 | - ]; |
|
| 64 | + _sqlite_init(); |
|
| 65 | + |
|
| 66 | + // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 67 | + $f = _DIR_DB; |
|
| 68 | + if ($addr and str_contains($addr, '/')) { |
|
| 69 | + $f = rtrim($addr, '/') . '/'; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 73 | + // il faut que la base existe ou que le repertoire parent soit writable |
|
| 74 | + if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 75 | + spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 76 | + |
|
| 77 | + return false; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + // charger les modules sqlite au besoin |
|
| 81 | + if (!_sqlite_charger_version($sqlite_version)) { |
|
| 82 | + spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 83 | + |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // chargement des constantes |
|
| 88 | + // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 89 | + $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 90 | + $define(); |
|
| 91 | + |
|
| 92 | + if (!$db) { |
|
| 93 | + // si pas de db -> |
|
| 94 | + // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 95 | + // pour tester la connexion |
|
| 96 | + $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 97 | + $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 98 | + $link = spip_sqlite_open($tmp); |
|
| 99 | + } else { |
|
| 100 | + // Ouvrir (eventuellement creer la base) |
|
| 101 | + $link = spip_sqlite_open($f); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + if (!$link) { |
|
| 105 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f", 'sqlite.' . _LOG_HS); |
|
| 106 | + |
|
| 107 | + return false; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $last_connect = [ |
|
| 111 | + 'addr' => $addr, |
|
| 112 | + 'port' => $port, |
|
| 113 | + 'login' => $login, |
|
| 114 | + 'pass' => $pass, |
|
| 115 | + 'db' => $db, |
|
| 116 | + 'prefixe' => $prefixe, |
|
| 117 | + ]; |
|
| 118 | + |
|
| 119 | + // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 120 | + include_spip('req/sqlite_fonctions'); |
|
| 121 | + _sqlite_init_functions($link); |
|
| 122 | + |
|
| 123 | + return [ |
|
| 124 | + 'db' => $db, |
|
| 125 | + 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 126 | + 'link' => $link, |
|
| 127 | + 'total_requetes' => 0, |
|
| 128 | + ]; |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
@@ -136,9 +136,9 @@ discard block |
||
| 136 | 136 | * @return PDO |
| 137 | 137 | */ |
| 138 | 138 | function spip_sqlite_open(string $file): \PDO { |
| 139 | - $PDO = new \PDO("sqlite:$file"); |
|
| 140 | - $PDO->setAttribute(\PDO::ATTR_STATEMENT_CLASS , [\Spip\Sql\Sqlite\PDOStatement::class, [&$PDO]]); |
|
| 141 | - return $PDO; |
|
| 139 | + $PDO = new \PDO("sqlite:$file"); |
|
| 140 | + $PDO->setAttribute(\PDO::ATTR_STATEMENT_CLASS , [\Spip\Sql\Sqlite\PDOStatement::class, [&$PDO]]); |
|
| 141 | + return $PDO; |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | |
@@ -158,14 +158,14 @@ discard block |
||
| 158 | 158 | */ |
| 159 | 159 | function spip_sqlite_query($query, $serveur = '', $requeter = true) |
| 160 | 160 | { |
| 161 | - #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 162 | - #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 163 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 164 | - if (!$requeter) { |
|
| 165 | - return $query; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return Sqlite::executer_requete($query, $serveur); |
|
| 161 | + #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 162 | + #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 163 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 164 | + if (!$requeter) { |
|
| 165 | + return $query; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return Sqlite::executer_requete($query, $serveur); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | |
@@ -183,11 +183,11 @@ discard block |
||
| 183 | 183 | function spip_sqlite_alter($query, $serveur = '', $requeter = true) |
| 184 | 184 | { |
| 185 | 185 | |
| 186 | - $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 187 | - // traduire la requete pour recuperer les bons noms de table |
|
| 188 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 186 | + $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 187 | + // traduire la requete pour recuperer les bons noms de table |
|
| 188 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 189 | 189 | |
| 190 | - /* |
|
| 190 | + /* |
|
| 191 | 191 | * la il faut faire les transformations |
| 192 | 192 | * si ALTER TABLE x (DROP|CHANGE) y |
| 193 | 193 | * |
@@ -196,251 +196,251 @@ discard block |
||
| 196 | 196 | * 3) faire chaque requete independemment |
| 197 | 197 | */ |
| 198 | 198 | |
| 199 | - // 1 |
|
| 200 | - if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 201 | - $debut = $regs[1]; |
|
| 202 | - $table = $regs[3]; |
|
| 203 | - $suite = $regs[4]; |
|
| 204 | - } else { |
|
| 205 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 206 | - |
|
| 207 | - return false; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - // 2 |
|
| 211 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 212 | - // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 213 | - // ou revoir l'api de sql_alter en creant un |
|
| 214 | - // sql_alter_table($table,array($actions)); |
|
| 215 | - $todo = explode(',', $suite); |
|
| 216 | - |
|
| 217 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 218 | - $todo2 = []; |
|
| 219 | - $i = 0; |
|
| 220 | - $ouverte = false; |
|
| 221 | - while ($do = array_shift($todo)) { |
|
| 222 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 223 | - $o = (str_contains($do, '(')); |
|
| 224 | - $f = (str_contains($do, ')')); |
|
| 225 | - if ($o and !$f) { |
|
| 226 | - $ouverte = true; |
|
| 227 | - } elseif ($f) { |
|
| 228 | - $ouverte = false; |
|
| 229 | - } |
|
| 230 | - if (!$ouverte) { |
|
| 231 | - $i++; |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - // 3 |
|
| 236 | - $resultats = []; |
|
| 237 | - foreach ($todo2 as $do) { |
|
| 238 | - $do = trim($do); |
|
| 239 | - if ( |
|
| 240 | - !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 241 | - . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 242 | - . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 243 | - . '|ADD COLUMN|ADD' |
|
| 244 | - . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 245 | - ) { |
|
| 246 | - spip_log( |
|
| 247 | - "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 248 | - 'sqlite.' . _LOG_ERREUR |
|
| 249 | - ); |
|
| 250 | - |
|
| 251 | - return false; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - $cle = strtoupper($matches[1]); |
|
| 255 | - $colonne_origine = $matches[2]; |
|
| 256 | - $colonne_destination = ''; |
|
| 257 | - |
|
| 258 | - $def = $matches[3]; |
|
| 259 | - |
|
| 260 | - // eluder une eventuelle clause before|after|first inutilisable |
|
| 261 | - $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 262 | - $defo = $defr; // garder la def d'origine pour certains cas |
|
| 263 | - // remplacer les definitions venant de mysql |
|
| 264 | - $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 265 | - |
|
| 266 | - // reinjecter dans le do |
|
| 267 | - $do = str_replace($def, $defr, $do); |
|
| 268 | - $def = $defr; |
|
| 269 | - |
|
| 270 | - switch ($cle) { |
|
| 271 | - // suppression d'un index |
|
| 272 | - case 'DROP KEY': |
|
| 273 | - case 'DROP INDEX': |
|
| 274 | - $nom_index = $colonne_origine; |
|
| 275 | - spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 276 | - break; |
|
| 277 | - |
|
| 278 | - // suppression d'une pk |
|
| 279 | - case 'DROP PRIMARY KEY': |
|
| 280 | - if ( |
|
| 281 | - !_sqlite_modifier_table( |
|
| 282 | - $table, |
|
| 283 | - $colonne_origine, |
|
| 284 | - ['key' => ['PRIMARY KEY' => '']], |
|
| 285 | - $serveur |
|
| 286 | - ) |
|
| 287 | - ) { |
|
| 288 | - return false; |
|
| 289 | - } |
|
| 290 | - break; |
|
| 291 | - // suppression d'une colonne |
|
| 292 | - case 'DROP COLUMN': |
|
| 293 | - case 'DROP': |
|
| 294 | - if ( |
|
| 295 | - !_sqlite_modifier_table( |
|
| 296 | - $table, |
|
| 297 | - [$colonne_origine => ''], |
|
| 298 | - [], |
|
| 299 | - $serveur |
|
| 300 | - ) |
|
| 301 | - ) { |
|
| 302 | - return false; |
|
| 303 | - } |
|
| 304 | - break; |
|
| 305 | - |
|
| 306 | - case 'CHANGE COLUMN': |
|
| 307 | - case 'CHANGE': |
|
| 308 | - // recuperer le nom de la future colonne |
|
| 309 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 310 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 311 | - $def = trim($defo); |
|
| 312 | - $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 313 | - $def = substr($def, strlen($colonne_destination) + 1); |
|
| 314 | - |
|
| 315 | - if ( |
|
| 316 | - !_sqlite_modifier_table( |
|
| 317 | - $table, |
|
| 318 | - [$colonne_origine => $colonne_destination], |
|
| 319 | - ['field' => [$colonne_destination => $def]], |
|
| 320 | - $serveur |
|
| 321 | - ) |
|
| 322 | - ) { |
|
| 323 | - return false; |
|
| 324 | - } |
|
| 325 | - break; |
|
| 326 | - |
|
| 327 | - case 'MODIFY': |
|
| 328 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 329 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 330 | - if ( |
|
| 331 | - !_sqlite_modifier_table( |
|
| 332 | - $table, |
|
| 333 | - $colonne_origine, |
|
| 334 | - ['field' => [$colonne_origine => $defo]], |
|
| 335 | - $serveur |
|
| 336 | - ) |
|
| 337 | - ) { |
|
| 338 | - return false; |
|
| 339 | - } |
|
| 340 | - break; |
|
| 341 | - |
|
| 342 | - // pas geres en sqlite2 |
|
| 343 | - case 'RENAME': |
|
| 344 | - $do = 'RENAME TO' . substr($do, 6); |
|
| 345 | - case 'RENAME TO': |
|
| 346 | - if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 347 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 348 | - |
|
| 349 | - return false; |
|
| 350 | - } |
|
| 351 | - break; |
|
| 352 | - |
|
| 353 | - // ajout d'une pk |
|
| 354 | - case 'ADD PRIMARY KEY': |
|
| 355 | - $pk = trim(substr($do, 16)); |
|
| 356 | - $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 357 | - if ( |
|
| 358 | - !_sqlite_modifier_table( |
|
| 359 | - $table, |
|
| 360 | - $colonne_origine, |
|
| 361 | - ['key' => ['PRIMARY KEY' => $pk]], |
|
| 362 | - $serveur |
|
| 363 | - ) |
|
| 364 | - ) { |
|
| 365 | - return false; |
|
| 366 | - } |
|
| 367 | - break; |
|
| 368 | - // ajout d'un index |
|
| 369 | - case 'ADD UNIQUE KEY': |
|
| 370 | - case 'ADD UNIQUE': |
|
| 371 | - $unique = true; |
|
| 372 | - case 'ADD INDEX': |
|
| 373 | - case 'ADD KEY': |
|
| 374 | - if (!isset($unique)) { |
|
| 375 | - $unique = false; |
|
| 376 | - } |
|
| 377 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 378 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 379 | - // |
|
| 380 | - // nom_index (colonnes) |
|
| 381 | - if ($def) { |
|
| 382 | - $colonnes = substr($def, 1, -1); |
|
| 383 | - $nom_index = $colonne_origine; |
|
| 384 | - } else { |
|
| 385 | - // (colonne) |
|
| 386 | - if ($colonne_origine[0] == '(') { |
|
| 387 | - $colonnes = substr($colonne_origine, 1, -1); |
|
| 388 | - if (str_contains(',', $colonnes)) { |
|
| 389 | - spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 390 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 391 | - break; |
|
| 392 | - } else { |
|
| 393 | - $nom_index = $colonnes; |
|
| 394 | - } |
|
| 395 | - } // nom_index |
|
| 396 | - else { |
|
| 397 | - $nom_index = $colonnes = $colonne_origine; |
|
| 398 | - } |
|
| 399 | - } |
|
| 400 | - spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 401 | - break; |
|
| 402 | - |
|
| 403 | - // pas geres en sqlite2 |
|
| 404 | - case 'ADD COLUMN': |
|
| 405 | - $do = 'ADD' . substr($do, 10); |
|
| 406 | - case 'ADD': |
|
| 407 | - default: |
|
| 408 | - if (!preg_match(',primary\s+key,i', $do)) { |
|
| 409 | - if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 410 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 411 | - |
|
| 412 | - return false; |
|
| 413 | - } |
|
| 414 | - break; |
|
| 415 | - } |
|
| 416 | - // ou si la colonne est aussi primary key |
|
| 417 | - // cas du add id_truc int primary key |
|
| 418 | - // ajout d'une colonne qui passe en primary key directe |
|
| 419 | - else { |
|
| 420 | - $def = trim(substr($do, 3)); |
|
| 421 | - $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 422 | - $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 423 | - $opts = []; |
|
| 424 | - if (preg_match(',primary\s+key,i', $def)) { |
|
| 425 | - $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 426 | - $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 427 | - } |
|
| 428 | - $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 429 | - if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 430 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 431 | - |
|
| 432 | - return false; |
|
| 433 | - } |
|
| 434 | - } |
|
| 435 | - break; |
|
| 436 | - } |
|
| 437 | - // tout est bon, ouf ! |
|
| 438 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 442 | - |
|
| 443 | - return true; |
|
| 199 | + // 1 |
|
| 200 | + if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 201 | + $debut = $regs[1]; |
|
| 202 | + $table = $regs[3]; |
|
| 203 | + $suite = $regs[4]; |
|
| 204 | + } else { |
|
| 205 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 206 | + |
|
| 207 | + return false; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + // 2 |
|
| 211 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 212 | + // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 213 | + // ou revoir l'api de sql_alter en creant un |
|
| 214 | + // sql_alter_table($table,array($actions)); |
|
| 215 | + $todo = explode(',', $suite); |
|
| 216 | + |
|
| 217 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 218 | + $todo2 = []; |
|
| 219 | + $i = 0; |
|
| 220 | + $ouverte = false; |
|
| 221 | + while ($do = array_shift($todo)) { |
|
| 222 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 223 | + $o = (str_contains($do, '(')); |
|
| 224 | + $f = (str_contains($do, ')')); |
|
| 225 | + if ($o and !$f) { |
|
| 226 | + $ouverte = true; |
|
| 227 | + } elseif ($f) { |
|
| 228 | + $ouverte = false; |
|
| 229 | + } |
|
| 230 | + if (!$ouverte) { |
|
| 231 | + $i++; |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + // 3 |
|
| 236 | + $resultats = []; |
|
| 237 | + foreach ($todo2 as $do) { |
|
| 238 | + $do = trim($do); |
|
| 239 | + if ( |
|
| 240 | + !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 241 | + . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 242 | + . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 243 | + . '|ADD COLUMN|ADD' |
|
| 244 | + . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 245 | + ) { |
|
| 246 | + spip_log( |
|
| 247 | + "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 248 | + 'sqlite.' . _LOG_ERREUR |
|
| 249 | + ); |
|
| 250 | + |
|
| 251 | + return false; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + $cle = strtoupper($matches[1]); |
|
| 255 | + $colonne_origine = $matches[2]; |
|
| 256 | + $colonne_destination = ''; |
|
| 257 | + |
|
| 258 | + $def = $matches[3]; |
|
| 259 | + |
|
| 260 | + // eluder une eventuelle clause before|after|first inutilisable |
|
| 261 | + $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 262 | + $defo = $defr; // garder la def d'origine pour certains cas |
|
| 263 | + // remplacer les definitions venant de mysql |
|
| 264 | + $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 265 | + |
|
| 266 | + // reinjecter dans le do |
|
| 267 | + $do = str_replace($def, $defr, $do); |
|
| 268 | + $def = $defr; |
|
| 269 | + |
|
| 270 | + switch ($cle) { |
|
| 271 | + // suppression d'un index |
|
| 272 | + case 'DROP KEY': |
|
| 273 | + case 'DROP INDEX': |
|
| 274 | + $nom_index = $colonne_origine; |
|
| 275 | + spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 276 | + break; |
|
| 277 | + |
|
| 278 | + // suppression d'une pk |
|
| 279 | + case 'DROP PRIMARY KEY': |
|
| 280 | + if ( |
|
| 281 | + !_sqlite_modifier_table( |
|
| 282 | + $table, |
|
| 283 | + $colonne_origine, |
|
| 284 | + ['key' => ['PRIMARY KEY' => '']], |
|
| 285 | + $serveur |
|
| 286 | + ) |
|
| 287 | + ) { |
|
| 288 | + return false; |
|
| 289 | + } |
|
| 290 | + break; |
|
| 291 | + // suppression d'une colonne |
|
| 292 | + case 'DROP COLUMN': |
|
| 293 | + case 'DROP': |
|
| 294 | + if ( |
|
| 295 | + !_sqlite_modifier_table( |
|
| 296 | + $table, |
|
| 297 | + [$colonne_origine => ''], |
|
| 298 | + [], |
|
| 299 | + $serveur |
|
| 300 | + ) |
|
| 301 | + ) { |
|
| 302 | + return false; |
|
| 303 | + } |
|
| 304 | + break; |
|
| 305 | + |
|
| 306 | + case 'CHANGE COLUMN': |
|
| 307 | + case 'CHANGE': |
|
| 308 | + // recuperer le nom de la future colonne |
|
| 309 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 310 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 311 | + $def = trim($defo); |
|
| 312 | + $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 313 | + $def = substr($def, strlen($colonne_destination) + 1); |
|
| 314 | + |
|
| 315 | + if ( |
|
| 316 | + !_sqlite_modifier_table( |
|
| 317 | + $table, |
|
| 318 | + [$colonne_origine => $colonne_destination], |
|
| 319 | + ['field' => [$colonne_destination => $def]], |
|
| 320 | + $serveur |
|
| 321 | + ) |
|
| 322 | + ) { |
|
| 323 | + return false; |
|
| 324 | + } |
|
| 325 | + break; |
|
| 326 | + |
|
| 327 | + case 'MODIFY': |
|
| 328 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 329 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 330 | + if ( |
|
| 331 | + !_sqlite_modifier_table( |
|
| 332 | + $table, |
|
| 333 | + $colonne_origine, |
|
| 334 | + ['field' => [$colonne_origine => $defo]], |
|
| 335 | + $serveur |
|
| 336 | + ) |
|
| 337 | + ) { |
|
| 338 | + return false; |
|
| 339 | + } |
|
| 340 | + break; |
|
| 341 | + |
|
| 342 | + // pas geres en sqlite2 |
|
| 343 | + case 'RENAME': |
|
| 344 | + $do = 'RENAME TO' . substr($do, 6); |
|
| 345 | + case 'RENAME TO': |
|
| 346 | + if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 347 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 348 | + |
|
| 349 | + return false; |
|
| 350 | + } |
|
| 351 | + break; |
|
| 352 | + |
|
| 353 | + // ajout d'une pk |
|
| 354 | + case 'ADD PRIMARY KEY': |
|
| 355 | + $pk = trim(substr($do, 16)); |
|
| 356 | + $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 357 | + if ( |
|
| 358 | + !_sqlite_modifier_table( |
|
| 359 | + $table, |
|
| 360 | + $colonne_origine, |
|
| 361 | + ['key' => ['PRIMARY KEY' => $pk]], |
|
| 362 | + $serveur |
|
| 363 | + ) |
|
| 364 | + ) { |
|
| 365 | + return false; |
|
| 366 | + } |
|
| 367 | + break; |
|
| 368 | + // ajout d'un index |
|
| 369 | + case 'ADD UNIQUE KEY': |
|
| 370 | + case 'ADD UNIQUE': |
|
| 371 | + $unique = true; |
|
| 372 | + case 'ADD INDEX': |
|
| 373 | + case 'ADD KEY': |
|
| 374 | + if (!isset($unique)) { |
|
| 375 | + $unique = false; |
|
| 376 | + } |
|
| 377 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 378 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 379 | + // |
|
| 380 | + // nom_index (colonnes) |
|
| 381 | + if ($def) { |
|
| 382 | + $colonnes = substr($def, 1, -1); |
|
| 383 | + $nom_index = $colonne_origine; |
|
| 384 | + } else { |
|
| 385 | + // (colonne) |
|
| 386 | + if ($colonne_origine[0] == '(') { |
|
| 387 | + $colonnes = substr($colonne_origine, 1, -1); |
|
| 388 | + if (str_contains(',', $colonnes)) { |
|
| 389 | + spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 390 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 391 | + break; |
|
| 392 | + } else { |
|
| 393 | + $nom_index = $colonnes; |
|
| 394 | + } |
|
| 395 | + } // nom_index |
|
| 396 | + else { |
|
| 397 | + $nom_index = $colonnes = $colonne_origine; |
|
| 398 | + } |
|
| 399 | + } |
|
| 400 | + spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 401 | + break; |
|
| 402 | + |
|
| 403 | + // pas geres en sqlite2 |
|
| 404 | + case 'ADD COLUMN': |
|
| 405 | + $do = 'ADD' . substr($do, 10); |
|
| 406 | + case 'ADD': |
|
| 407 | + default: |
|
| 408 | + if (!preg_match(',primary\s+key,i', $do)) { |
|
| 409 | + if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 410 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 411 | + |
|
| 412 | + return false; |
|
| 413 | + } |
|
| 414 | + break; |
|
| 415 | + } |
|
| 416 | + // ou si la colonne est aussi primary key |
|
| 417 | + // cas du add id_truc int primary key |
|
| 418 | + // ajout d'une colonne qui passe en primary key directe |
|
| 419 | + else { |
|
| 420 | + $def = trim(substr($do, 3)); |
|
| 421 | + $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 422 | + $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 423 | + $opts = []; |
|
| 424 | + if (preg_match(',primary\s+key,i', $def)) { |
|
| 425 | + $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 426 | + $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 427 | + } |
|
| 428 | + $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 429 | + if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 430 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 431 | + |
|
| 432 | + return false; |
|
| 433 | + } |
|
| 434 | + } |
|
| 435 | + break; |
|
| 436 | + } |
|
| 437 | + // tout est bon, ouf ! |
|
| 438 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 442 | + |
|
| 443 | + return true; |
|
| 444 | 444 | } |
| 445 | 445 | |
| 446 | 446 | /** |
@@ -462,38 +462,38 @@ discard block |
||
| 462 | 462 | * - true si la requête réussie, false sinon. |
| 463 | 463 | */ |
| 464 | 464 | function spip_sqlite_create( |
| 465 | - $nom, |
|
| 466 | - $champs, |
|
| 467 | - $cles, |
|
| 468 | - $autoinc = false, |
|
| 469 | - $temporary = false, |
|
| 470 | - $serveur = '', |
|
| 471 | - $requeter = true |
|
| 465 | + $nom, |
|
| 466 | + $champs, |
|
| 467 | + $cles, |
|
| 468 | + $autoinc = false, |
|
| 469 | + $temporary = false, |
|
| 470 | + $serveur = '', |
|
| 471 | + $requeter = true |
|
| 472 | 472 | ) { |
| 473 | - $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 474 | - if (!$query) { |
|
| 475 | - return false; |
|
| 476 | - } |
|
| 477 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 478 | - |
|
| 479 | - // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 480 | - // il faut donc les faire creer ensuite |
|
| 481 | - if (!$requeter) { |
|
| 482 | - return $res; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - $ok = $res ? true : false; |
|
| 486 | - if ($ok) { |
|
| 487 | - foreach ($cles as $k => $v) { |
|
| 488 | - if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 489 | - $index = trim(substr($k, strlen($m[1]))); |
|
| 490 | - $unique = (strlen($m[1]) > 3); |
|
| 491 | - $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 492 | - } |
|
| 493 | - } |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - return $ok ? true : false; |
|
| 473 | + $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 474 | + if (!$query) { |
|
| 475 | + return false; |
|
| 476 | + } |
|
| 477 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 478 | + |
|
| 479 | + // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 480 | + // il faut donc les faire creer ensuite |
|
| 481 | + if (!$requeter) { |
|
| 482 | + return $res; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + $ok = $res ? true : false; |
|
| 486 | + if ($ok) { |
|
| 487 | + foreach ($cles as $k => $v) { |
|
| 488 | + if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 489 | + $index = trim(substr($k, strlen($m[1]))); |
|
| 490 | + $unique = (strlen($m[1]) > 3); |
|
| 491 | + $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 492 | + } |
|
| 493 | + } |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + return $ok ? true : false; |
|
| 497 | 497 | } |
| 498 | 498 | |
| 499 | 499 | /** |
@@ -507,21 +507,21 @@ discard block |
||
| 507 | 507 | **/ |
| 508 | 508 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) |
| 509 | 509 | { |
| 510 | - $f = $nom . '.sqlite'; |
|
| 511 | - if (strpos($nom, '/') === false) { |
|
| 512 | - $f = _DIR_DB . $f; |
|
| 513 | - } |
|
| 510 | + $f = $nom . '.sqlite'; |
|
| 511 | + if (strpos($nom, '/') === false) { |
|
| 512 | + $f = _DIR_DB . $f; |
|
| 513 | + } |
|
| 514 | 514 | |
| 515 | - $ok = new \PDO("sqlite:$f"); |
|
| 515 | + $ok = new \PDO("sqlite:$f"); |
|
| 516 | 516 | |
| 517 | - if ($ok) { |
|
| 518 | - unset($ok); |
|
| 517 | + if ($ok) { |
|
| 518 | + unset($ok); |
|
| 519 | 519 | |
| 520 | - return true; |
|
| 521 | - } |
|
| 522 | - unset($ok); |
|
| 520 | + return true; |
|
| 521 | + } |
|
| 522 | + unset($ok); |
|
| 523 | 523 | |
| 524 | - return false; |
|
| 524 | + return false; |
|
| 525 | 525 | } |
| 526 | 526 | |
| 527 | 527 | |
@@ -543,22 +543,22 @@ discard block |
||
| 543 | 543 | */ |
| 544 | 544 | function spip_sqlite_create_view($nom, $query_select, $serveur = '', $requeter = true) |
| 545 | 545 | { |
| 546 | - if (!$query_select) { |
|
| 547 | - return false; |
|
| 548 | - } |
|
| 549 | - // vue deja presente |
|
| 550 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 551 | - spip_log( |
|
| 552 | - "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 553 | - 'sqlite.' . _LOG_ERREUR |
|
| 554 | - ); |
|
| 555 | - |
|
| 556 | - return false; |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 560 | - |
|
| 561 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 546 | + if (!$query_select) { |
|
| 547 | + return false; |
|
| 548 | + } |
|
| 549 | + // vue deja presente |
|
| 550 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 551 | + spip_log( |
|
| 552 | + "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 553 | + 'sqlite.' . _LOG_ERREUR |
|
| 554 | + ); |
|
| 555 | + |
|
| 556 | + return false; |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 560 | + |
|
| 561 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 562 | 562 | } |
| 563 | 563 | |
| 564 | 564 | /** |
@@ -581,54 +581,54 @@ discard block |
||
| 581 | 581 | */ |
| 582 | 582 | function spip_sqlite_create_index($nom, $table, $champs, $unique = '', $serveur = '', $requeter = true) |
| 583 | 583 | { |
| 584 | - if (!($nom or $table or $champs)) { |
|
| 585 | - spip_log( |
|
| 586 | - "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 587 | - 'sqlite.' . _LOG_ERREUR |
|
| 588 | - ); |
|
| 589 | - |
|
| 590 | - return false; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 594 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 595 | - $nom = $table . '_' . $nom; |
|
| 596 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 597 | - if (!is_array($champs)) { |
|
| 598 | - if ($champs[0] == '(') { |
|
| 599 | - $champs = substr($champs, 1, -1); |
|
| 600 | - } |
|
| 601 | - $champs = [$champs]; |
|
| 602 | - // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 603 | - $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - $ifnotexists = ''; |
|
| 607 | - $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 608 | - if (!function_exists('spip_version_compare')) { |
|
| 609 | - include_spip('plugins/installer'); |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 613 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 614 | - } else { |
|
| 615 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 616 | - $a = spip_sqlite_showtable($table, $serveur); |
|
| 617 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 618 | - return true; |
|
| 619 | - } |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 623 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 624 | - if (!$requeter) { |
|
| 625 | - return $res; |
|
| 626 | - } |
|
| 627 | - if ($res) { |
|
| 628 | - return true; |
|
| 629 | - } else { |
|
| 630 | - return false; |
|
| 631 | - } |
|
| 584 | + if (!($nom or $table or $champs)) { |
|
| 585 | + spip_log( |
|
| 586 | + "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 587 | + 'sqlite.' . _LOG_ERREUR |
|
| 588 | + ); |
|
| 589 | + |
|
| 590 | + return false; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 594 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 595 | + $nom = $table . '_' . $nom; |
|
| 596 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 597 | + if (!is_array($champs)) { |
|
| 598 | + if ($champs[0] == '(') { |
|
| 599 | + $champs = substr($champs, 1, -1); |
|
| 600 | + } |
|
| 601 | + $champs = [$champs]; |
|
| 602 | + // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 603 | + $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + $ifnotexists = ''; |
|
| 607 | + $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 608 | + if (!function_exists('spip_version_compare')) { |
|
| 609 | + include_spip('plugins/installer'); |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 613 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 614 | + } else { |
|
| 615 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 616 | + $a = spip_sqlite_showtable($table, $serveur); |
|
| 617 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 618 | + return true; |
|
| 619 | + } |
|
| 620 | + } |
|
| 621 | + |
|
| 622 | + $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 623 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 624 | + if (!$requeter) { |
|
| 625 | + return $res; |
|
| 626 | + } |
|
| 627 | + if ($res) { |
|
| 628 | + return true; |
|
| 629 | + } else { |
|
| 630 | + return false; |
|
| 631 | + } |
|
| 632 | 632 | } |
| 633 | 633 | |
| 634 | 634 | /** |
@@ -645,24 +645,24 @@ discard block |
||
| 645 | 645 | */ |
| 646 | 646 | function spip_sqlite_count($r, $serveur = '', $requeter = true) |
| 647 | 647 | { |
| 648 | - if (!$r) { |
|
| 649 | - return 0; |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - // select ou autre (insert, update,...) ? |
|
| 653 | - // (link,requete) a compter |
|
| 654 | - if (strtoupper(substr(ltrim($r->queryString), 0, 6)) === 'SELECT') { |
|
| 655 | - $link = $r->getPDO(); |
|
| 656 | - $query = "SELECT count(*) as zzzzsqlitecount FROM ({$r->queryString})"; |
|
| 657 | - $l = $link->query($query); |
|
| 658 | - $i = 0; |
|
| 659 | - if ($l and $z = $l->fetch()) { |
|
| 660 | - $i = (int) $z['zzzzsqlitecount']; |
|
| 661 | - } |
|
| 662 | - return $i; |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - return $r->rowCount(); |
|
| 648 | + if (!$r) { |
|
| 649 | + return 0; |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + // select ou autre (insert, update,...) ? |
|
| 653 | + // (link,requete) a compter |
|
| 654 | + if (strtoupper(substr(ltrim($r->queryString), 0, 6)) === 'SELECT') { |
|
| 655 | + $link = $r->getPDO(); |
|
| 656 | + $query = "SELECT count(*) as zzzzsqlitecount FROM ({$r->queryString})"; |
|
| 657 | + $l = $link->query($query); |
|
| 658 | + $i = 0; |
|
| 659 | + if ($l and $z = $l->fetch()) { |
|
| 660 | + $i = (int) $z['zzzzsqlitecount']; |
|
| 661 | + } |
|
| 662 | + return $i; |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + return $r->rowCount(); |
|
| 666 | 666 | } |
| 667 | 667 | |
| 668 | 668 | |
@@ -681,31 +681,31 @@ discard block |
||
| 681 | 681 | * - false si la requête a échouée |
| 682 | 682 | **/ |
| 683 | 683 | function spip_sqlite_countsel( |
| 684 | - $from = [], |
|
| 685 | - $where = [], |
|
| 686 | - $groupby = '', |
|
| 687 | - $having = [], |
|
| 688 | - $serveur = '', |
|
| 689 | - $requeter = true |
|
| 684 | + $from = [], |
|
| 685 | + $where = [], |
|
| 686 | + $groupby = '', |
|
| 687 | + $having = [], |
|
| 688 | + $serveur = '', |
|
| 689 | + $requeter = true |
|
| 690 | 690 | ) { |
| 691 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 692 | - $r = spip_sqlite_select( |
|
| 693 | - "COUNT($c)", |
|
| 694 | - $from, |
|
| 695 | - $where, |
|
| 696 | - '', |
|
| 697 | - '', |
|
| 698 | - '', |
|
| 699 | - $having, |
|
| 700 | - $serveur, |
|
| 701 | - $requeter |
|
| 702 | - ); |
|
| 703 | - if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 704 | - [$r] = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 705 | - $r = (int) $r; |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - return $r; |
|
| 691 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 692 | + $r = spip_sqlite_select( |
|
| 693 | + "COUNT($c)", |
|
| 694 | + $from, |
|
| 695 | + $where, |
|
| 696 | + '', |
|
| 697 | + '', |
|
| 698 | + '', |
|
| 699 | + $having, |
|
| 700 | + $serveur, |
|
| 701 | + $requeter |
|
| 702 | + ); |
|
| 703 | + if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 704 | + [$r] = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 705 | + $r = (int) $r; |
|
| 706 | + } |
|
| 707 | + |
|
| 708 | + return $r; |
|
| 709 | 709 | } |
| 710 | 710 | |
| 711 | 711 | |
@@ -723,24 +723,24 @@ discard block |
||
| 723 | 723 | **/ |
| 724 | 724 | function spip_sqlite_delete($table, $where = '', $serveur = '', $requeter = true) |
| 725 | 725 | { |
| 726 | - $res = spip_sqlite_query( |
|
| 727 | - _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 728 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 729 | - $serveur, |
|
| 730 | - $requeter |
|
| 731 | - ); |
|
| 732 | - |
|
| 733 | - // renvoyer la requete inerte si demandee |
|
| 734 | - if (!$requeter) { |
|
| 735 | - return $res; |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - if ($res) { |
|
| 739 | - $link = _sqlite_link($serveur); |
|
| 740 | - return $res->rowCount(); |
|
| 741 | - } else { |
|
| 742 | - return false; |
|
| 743 | - } |
|
| 726 | + $res = spip_sqlite_query( |
|
| 727 | + _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 728 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 729 | + $serveur, |
|
| 730 | + $requeter |
|
| 731 | + ); |
|
| 732 | + |
|
| 733 | + // renvoyer la requete inerte si demandee |
|
| 734 | + if (!$requeter) { |
|
| 735 | + return $res; |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + if ($res) { |
|
| 739 | + $link = _sqlite_link($serveur); |
|
| 740 | + return $res->rowCount(); |
|
| 741 | + } else { |
|
| 742 | + return false; |
|
| 743 | + } |
|
| 744 | 744 | } |
| 745 | 745 | |
| 746 | 746 | |
@@ -757,15 +757,15 @@ discard block |
||
| 757 | 757 | */ |
| 758 | 758 | function spip_sqlite_drop_table($table, $exist = '', $serveur = '', $requeter = true) |
| 759 | 759 | { |
| 760 | - if ($exist) { |
|
| 761 | - $exist = ' IF EXISTS'; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 765 | - return true; |
|
| 766 | - } else { |
|
| 767 | - return false; |
|
| 768 | - } |
|
| 760 | + if ($exist) { |
|
| 761 | + $exist = ' IF EXISTS'; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 765 | + return true; |
|
| 766 | + } else { |
|
| 767 | + return false; |
|
| 768 | + } |
|
| 769 | 769 | } |
| 770 | 770 | |
| 771 | 771 | |
@@ -782,11 +782,11 @@ discard block |
||
| 782 | 782 | */ |
| 783 | 783 | function spip_sqlite_drop_view($view, $exist = '', $serveur = '', $requeter = true) |
| 784 | 784 | { |
| 785 | - if ($exist) { |
|
| 786 | - $exist = ' IF EXISTS'; |
|
| 787 | - } |
|
| 785 | + if ($exist) { |
|
| 786 | + $exist = ' IF EXISTS'; |
|
| 787 | + } |
|
| 788 | 788 | |
| 789 | - return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 789 | + return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 790 | 790 | } |
| 791 | 791 | |
| 792 | 792 | /** |
@@ -801,20 +801,20 @@ discard block |
||
| 801 | 801 | */ |
| 802 | 802 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) |
| 803 | 803 | { |
| 804 | - if (!($nom or $table)) { |
|
| 805 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 804 | + if (!($nom or $table)) { |
|
| 805 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 806 | 806 | |
| 807 | - return false; |
|
| 808 | - } |
|
| 807 | + return false; |
|
| 808 | + } |
|
| 809 | 809 | |
| 810 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 811 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 812 | - $index = $table . '_' . $nom; |
|
| 813 | - $exist = ' IF EXISTS'; |
|
| 810 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 811 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 812 | + $index = $table . '_' . $nom; |
|
| 813 | + $exist = ' IF EXISTS'; |
|
| 814 | 814 | |
| 815 | - $query = "DROP INDEX$exist $index"; |
|
| 815 | + $query = "DROP INDEX$exist $index"; |
|
| 816 | 816 | |
| 817 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 817 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 818 | 818 | } |
| 819 | 819 | |
| 820 | 820 | /** |
@@ -831,29 +831,29 @@ discard block |
||
| 831 | 831 | **/ |
| 832 | 832 | function spip_sqlite_error($query = '', $serveur = '') |
| 833 | 833 | { |
| 834 | - $link = _sqlite_link($serveur); |
|
| 835 | - |
|
| 836 | - if ($link) { |
|
| 837 | - $errs = $link->errorInfo(); |
|
| 838 | - $s = _sqlite_last_error_from_link($link); |
|
| 839 | - } else { |
|
| 840 | - $s = ': aucune ressource sqlite (link)'; |
|
| 841 | - } |
|
| 842 | - if ($s) { |
|
| 843 | - $trace = debug_backtrace(); |
|
| 844 | - if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 845 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 846 | - } |
|
| 847 | - } |
|
| 848 | - |
|
| 849 | - return $s; |
|
| 834 | + $link = _sqlite_link($serveur); |
|
| 835 | + |
|
| 836 | + if ($link) { |
|
| 837 | + $errs = $link->errorInfo(); |
|
| 838 | + $s = _sqlite_last_error_from_link($link); |
|
| 839 | + } else { |
|
| 840 | + $s = ': aucune ressource sqlite (link)'; |
|
| 841 | + } |
|
| 842 | + if ($s) { |
|
| 843 | + $trace = debug_backtrace(); |
|
| 844 | + if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 845 | + spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 846 | + } |
|
| 847 | + } |
|
| 848 | + |
|
| 849 | + return $s; |
|
| 850 | 850 | } |
| 851 | 851 | |
| 852 | 852 | function _sqlite_last_error_from_link($link) |
| 853 | 853 | { |
| 854 | - if ($link) { |
|
| 855 | - $errs = $link->errorInfo(); |
|
| 856 | - /* |
|
| 854 | + if ($link) { |
|
| 855 | + $errs = $link->errorInfo(); |
|
| 856 | + /* |
|
| 857 | 857 | $errs[0] |
| 858 | 858 | numero SQLState ('HY000' souvent lors d'une erreur) |
| 859 | 859 | http://www.easysoft.com/developer/interfaces/odbc/sqlstate_status_return_codes.html |
@@ -863,11 +863,11 @@ discard block |
||
| 863 | 863 | $errs[2] |
| 864 | 864 | Le texte du message d'erreur |
| 865 | 865 | */ |
| 866 | - if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 867 | - return "$errs[2]"; |
|
| 868 | - } |
|
| 869 | - } |
|
| 870 | - return ''; |
|
| 866 | + if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 867 | + return "$errs[2]"; |
|
| 868 | + } |
|
| 869 | + } |
|
| 870 | + return ''; |
|
| 871 | 871 | } |
| 872 | 872 | |
| 873 | 873 | /** |
@@ -885,23 +885,23 @@ discard block |
||
| 885 | 885 | **/ |
| 886 | 886 | function spip_sqlite_errno($serveur = '') |
| 887 | 887 | { |
| 888 | - $link = _sqlite_link($serveur); |
|
| 889 | - |
|
| 890 | - if ($link) { |
|
| 891 | - $t = $link->errorInfo(); |
|
| 892 | - $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 893 | - if ($s) { |
|
| 894 | - $s .= ' / ' . $t[1]; |
|
| 895 | - } // ajoute l'erreur du moteur SQLite |
|
| 896 | - } else { |
|
| 897 | - $s = ': aucune ressource sqlite (link)'; |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - if ($s) { |
|
| 901 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 902 | - } |
|
| 903 | - |
|
| 904 | - return $s ? $s : 0; |
|
| 888 | + $link = _sqlite_link($serveur); |
|
| 889 | + |
|
| 890 | + if ($link) { |
|
| 891 | + $t = $link->errorInfo(); |
|
| 892 | + $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 893 | + if ($s) { |
|
| 894 | + $s .= ' / ' . $t[1]; |
|
| 895 | + } // ajoute l'erreur du moteur SQLite |
|
| 896 | + } else { |
|
| 897 | + $s = ': aucune ressource sqlite (link)'; |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + if ($s) { |
|
| 901 | + spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 902 | + } |
|
| 903 | + |
|
| 904 | + return $s ? $s : 0; |
|
| 905 | 905 | } |
| 906 | 906 | |
| 907 | 907 | |
@@ -918,19 +918,19 @@ discard block |
||
| 918 | 918 | */ |
| 919 | 919 | function spip_sqlite_explain($query, $serveur = '', $requeter = true) |
| 920 | 920 | { |
| 921 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 922 | - return []; |
|
| 923 | - } |
|
| 924 | - |
|
| 925 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 926 | - $query = 'EXPLAIN ' . $query; |
|
| 927 | - if (!$requeter) { |
|
| 928 | - return $query; |
|
| 929 | - } |
|
| 930 | - // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 931 | - $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 932 | - |
|
| 933 | - return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 921 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 922 | + return []; |
|
| 923 | + } |
|
| 924 | + |
|
| 925 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 926 | + $query = 'EXPLAIN ' . $query; |
|
| 927 | + if (!$requeter) { |
|
| 928 | + return $query; |
|
| 929 | + } |
|
| 930 | + // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 931 | + $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 932 | + |
|
| 933 | + return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 934 | 934 | } |
| 935 | 935 | |
| 936 | 936 | |
@@ -951,35 +951,35 @@ discard block |
||
| 951 | 951 | function spip_sqlite_fetch($r, $t = '', $serveur = '', $requeter = true) |
| 952 | 952 | { |
| 953 | 953 | |
| 954 | - $link = _sqlite_link($serveur); |
|
| 955 | - $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 956 | - |
|
| 957 | - if (!$r) { |
|
| 958 | - return false; |
|
| 959 | - } |
|
| 960 | - |
|
| 961 | - $retour = $r->fetch($t); |
|
| 962 | - |
|
| 963 | - if (!$retour) { |
|
| 964 | - if ($r->errorCode() === '00000') { |
|
| 965 | - return null; |
|
| 966 | - } |
|
| 967 | - return false; |
|
| 968 | - } |
|
| 969 | - |
|
| 970 | - // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 971 | - // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 972 | - // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 973 | - if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 974 | - foreach ($retour as $cle => $val) { |
|
| 975 | - if (($pos = strpos($cle, '.')) !== false) { |
|
| 976 | - $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 977 | - unset($retour[$cle]); |
|
| 978 | - } |
|
| 979 | - } |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - return $retour; |
|
| 954 | + $link = _sqlite_link($serveur); |
|
| 955 | + $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 956 | + |
|
| 957 | + if (!$r) { |
|
| 958 | + return false; |
|
| 959 | + } |
|
| 960 | + |
|
| 961 | + $retour = $r->fetch($t); |
|
| 962 | + |
|
| 963 | + if (!$retour) { |
|
| 964 | + if ($r->errorCode() === '00000') { |
|
| 965 | + return null; |
|
| 966 | + } |
|
| 967 | + return false; |
|
| 968 | + } |
|
| 969 | + |
|
| 970 | + // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 971 | + // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 972 | + // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 973 | + if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 974 | + foreach ($retour as $cle => $val) { |
|
| 975 | + if (($pos = strpos($cle, '.')) !== false) { |
|
| 976 | + $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 977 | + unset($retour[$cle]); |
|
| 978 | + } |
|
| 979 | + } |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + return $retour; |
|
| 983 | 983 | } |
| 984 | 984 | |
| 985 | 985 | /** |
@@ -993,8 +993,8 @@ discard block |
||
| 993 | 993 | **/ |
| 994 | 994 | function spip_sqlite_seek($r, $row_number, $serveur = '', $requeter = true) |
| 995 | 995 | { |
| 996 | - // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 997 | - return false; |
|
| 996 | + // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 997 | + return false; |
|
| 998 | 998 | } |
| 999 | 999 | |
| 1000 | 1000 | |
@@ -1011,10 +1011,10 @@ discard block |
||
| 1011 | 1011 | */ |
| 1012 | 1012 | function spip_sqlite_free(&$r, $serveur = '', $requeter = true) |
| 1013 | 1013 | { |
| 1014 | - unset($r); |
|
| 1014 | + unset($r); |
|
| 1015 | 1015 | |
| 1016 | - return true; |
|
| 1017 | - //return sqlite_free_result($r); |
|
| 1016 | + return true; |
|
| 1017 | + //return sqlite_free_result($r); |
|
| 1018 | 1018 | } |
| 1019 | 1019 | |
| 1020 | 1020 | |
@@ -1030,8 +1030,8 @@ discard block |
||
| 1030 | 1030 | */ |
| 1031 | 1031 | function spip_sqlite_get_charset($charset = [], $serveur = '', $requeter = true) |
| 1032 | 1032 | { |
| 1033 | - //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1034 | - //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1033 | + //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1034 | + //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1035 | 1035 | } |
| 1036 | 1036 | |
| 1037 | 1037 | |
@@ -1047,7 +1047,7 @@ discard block |
||
| 1047 | 1047 | **/ |
| 1048 | 1048 | function spip_sqlite_hex($v) |
| 1049 | 1049 | { |
| 1050 | - return hexdec($v); |
|
| 1050 | + return hexdec($v); |
|
| 1051 | 1051 | } |
| 1052 | 1052 | |
| 1053 | 1053 | |
@@ -1070,7 +1070,7 @@ discard block |
||
| 1070 | 1070 | **/ |
| 1071 | 1071 | function spip_sqlite_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) |
| 1072 | 1072 | { |
| 1073 | - return "($val $not IN ($valeurs))"; |
|
| 1073 | + return "($val $not IN ($valeurs))"; |
|
| 1074 | 1074 | } |
| 1075 | 1075 | |
| 1076 | 1076 | |
@@ -1099,20 +1099,20 @@ discard block |
||
| 1099 | 1099 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) |
| 1100 | 1100 | { |
| 1101 | 1101 | |
| 1102 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1103 | - if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1104 | - if (!$requeter) { |
|
| 1105 | - return $r; |
|
| 1106 | - } |
|
| 1107 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1108 | - } else { |
|
| 1109 | - $nb = false; |
|
| 1110 | - } |
|
| 1102 | + $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1103 | + if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1104 | + if (!$requeter) { |
|
| 1105 | + return $r; |
|
| 1106 | + } |
|
| 1107 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1108 | + } else { |
|
| 1109 | + $nb = false; |
|
| 1110 | + } |
|
| 1111 | 1111 | |
| 1112 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1112 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1113 | 1113 | |
| 1114 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1115 | - return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1114 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1115 | + return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1116 | 1116 | } |
| 1117 | 1117 | |
| 1118 | 1118 | |
@@ -1138,28 +1138,28 @@ discard block |
||
| 1138 | 1138 | **/ |
| 1139 | 1139 | function spip_sqlite_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) |
| 1140 | 1140 | { |
| 1141 | - if (!$desc) { |
|
| 1142 | - $desc = description_table($table, $serveur); |
|
| 1143 | - } |
|
| 1144 | - if (!$desc) { |
|
| 1145 | - die("$table insertion sans description"); |
|
| 1146 | - } |
|
| 1147 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1148 | - |
|
| 1149 | - foreach ($couples as $champ => $val) { |
|
| 1150 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1151 | - } |
|
| 1152 | - |
|
| 1153 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1154 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1155 | - |
|
| 1156 | - $cles = $valeurs = ''; |
|
| 1157 | - if (count($couples)) { |
|
| 1158 | - $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1159 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1160 | - } |
|
| 1161 | - |
|
| 1162 | - return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1141 | + if (!$desc) { |
|
| 1142 | + $desc = description_table($table, $serveur); |
|
| 1143 | + } |
|
| 1144 | + if (!$desc) { |
|
| 1145 | + die("$table insertion sans description"); |
|
| 1146 | + } |
|
| 1147 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1148 | + |
|
| 1149 | + foreach ($couples as $champ => $val) { |
|
| 1150 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1151 | + } |
|
| 1152 | + |
|
| 1153 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1154 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1155 | + |
|
| 1156 | + $cles = $valeurs = ''; |
|
| 1157 | + if (count($couples)) { |
|
| 1158 | + $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1159 | + $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1160 | + } |
|
| 1161 | + |
|
| 1162 | + return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1163 | 1163 | } |
| 1164 | 1164 | |
| 1165 | 1165 | |
@@ -1184,70 +1184,70 @@ discard block |
||
| 1184 | 1184 | **/ |
| 1185 | 1185 | function spip_sqlite_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) |
| 1186 | 1186 | { |
| 1187 | - if (!$desc) { |
|
| 1188 | - $desc = description_table($table, $serveur); |
|
| 1189 | - } |
|
| 1190 | - if (!$desc) { |
|
| 1191 | - die("$table insertion sans description"); |
|
| 1192 | - } |
|
| 1193 | - if (!isset($desc['field'])) { |
|
| 1194 | - $desc['field'] = []; |
|
| 1195 | - } |
|
| 1196 | - |
|
| 1197 | - // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1198 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1199 | - |
|
| 1200 | - // seul le nom de la table est a traduire ici : |
|
| 1201 | - // le faire une seule fois au debut |
|
| 1202 | - $query_start = "INSERT INTO $table "; |
|
| 1203 | - $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1204 | - |
|
| 1205 | - // ouvrir une transaction |
|
| 1206 | - if ($requeter) { |
|
| 1207 | - Sqlite::demarrer_transaction($serveur); |
|
| 1208 | - } |
|
| 1209 | - |
|
| 1210 | - while ($couples = array_shift($tab_couples)) { |
|
| 1211 | - foreach ($couples as $champ => $val) { |
|
| 1212 | - $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1213 | - } |
|
| 1214 | - |
|
| 1215 | - // inserer les champs timestamp par defaut |
|
| 1216 | - $couples = array_merge($maj, $couples); |
|
| 1217 | - |
|
| 1218 | - $champs = $valeurs = ''; |
|
| 1219 | - if (count($couples)) { |
|
| 1220 | - $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1221 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1222 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1223 | - } else { |
|
| 1224 | - $query = $query_start . 'DEFAULT VALUES'; |
|
| 1225 | - } |
|
| 1226 | - |
|
| 1227 | - if ($requeter) { |
|
| 1228 | - $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1229 | - } |
|
| 1230 | - |
|
| 1231 | - // sur le dernier couple uniquement |
|
| 1232 | - if (!count($tab_couples)) { |
|
| 1233 | - $nb = 0; |
|
| 1234 | - if ($requeter) { |
|
| 1235 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1236 | - } else { |
|
| 1237 | - return $query; |
|
| 1238 | - } |
|
| 1239 | - } |
|
| 1240 | - |
|
| 1241 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1242 | - } |
|
| 1243 | - |
|
| 1244 | - if ($requeter) { |
|
| 1245 | - Sqlite::finir_transaction($serveur); |
|
| 1246 | - } |
|
| 1247 | - |
|
| 1248 | - // renvoie le dernier id d'autoincrement ajoute |
|
| 1249 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1250 | - return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1187 | + if (!$desc) { |
|
| 1188 | + $desc = description_table($table, $serveur); |
|
| 1189 | + } |
|
| 1190 | + if (!$desc) { |
|
| 1191 | + die("$table insertion sans description"); |
|
| 1192 | + } |
|
| 1193 | + if (!isset($desc['field'])) { |
|
| 1194 | + $desc['field'] = []; |
|
| 1195 | + } |
|
| 1196 | + |
|
| 1197 | + // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1198 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1199 | + |
|
| 1200 | + // seul le nom de la table est a traduire ici : |
|
| 1201 | + // le faire une seule fois au debut |
|
| 1202 | + $query_start = "INSERT INTO $table "; |
|
| 1203 | + $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1204 | + |
|
| 1205 | + // ouvrir une transaction |
|
| 1206 | + if ($requeter) { |
|
| 1207 | + Sqlite::demarrer_transaction($serveur); |
|
| 1208 | + } |
|
| 1209 | + |
|
| 1210 | + while ($couples = array_shift($tab_couples)) { |
|
| 1211 | + foreach ($couples as $champ => $val) { |
|
| 1212 | + $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1213 | + } |
|
| 1214 | + |
|
| 1215 | + // inserer les champs timestamp par defaut |
|
| 1216 | + $couples = array_merge($maj, $couples); |
|
| 1217 | + |
|
| 1218 | + $champs = $valeurs = ''; |
|
| 1219 | + if (count($couples)) { |
|
| 1220 | + $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1221 | + $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1222 | + $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1223 | + } else { |
|
| 1224 | + $query = $query_start . 'DEFAULT VALUES'; |
|
| 1225 | + } |
|
| 1226 | + |
|
| 1227 | + if ($requeter) { |
|
| 1228 | + $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1229 | + } |
|
| 1230 | + |
|
| 1231 | + // sur le dernier couple uniquement |
|
| 1232 | + if (!count($tab_couples)) { |
|
| 1233 | + $nb = 0; |
|
| 1234 | + if ($requeter) { |
|
| 1235 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1236 | + } else { |
|
| 1237 | + return $query; |
|
| 1238 | + } |
|
| 1239 | + } |
|
| 1240 | + |
|
| 1241 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1242 | + } |
|
| 1243 | + |
|
| 1244 | + if ($requeter) { |
|
| 1245 | + Sqlite::finir_transaction($serveur); |
|
| 1246 | + } |
|
| 1247 | + |
|
| 1248 | + // renvoie le dernier id d'autoincrement ajoute |
|
| 1249 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1250 | + return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1251 | 1251 | } |
| 1252 | 1252 | |
| 1253 | 1253 | |
@@ -1263,7 +1263,7 @@ discard block |
||
| 1263 | 1263 | **/ |
| 1264 | 1264 | function spip_sqlite_preferer_transaction($serveur = '', $requeter = true) |
| 1265 | 1265 | { |
| 1266 | - return true; |
|
| 1266 | + return true; |
|
| 1267 | 1267 | } |
| 1268 | 1268 | |
| 1269 | 1269 | /** |
@@ -1281,12 +1281,12 @@ discard block |
||
| 1281 | 1281 | **/ |
| 1282 | 1282 | function spip_sqlite_demarrer_transaction($serveur = '', $requeter = true) |
| 1283 | 1283 | { |
| 1284 | - if (!$requeter) { |
|
| 1285 | - return 'BEGIN TRANSACTION'; |
|
| 1286 | - } |
|
| 1287 | - Sqlite::demarrer_transaction($serveur); |
|
| 1284 | + if (!$requeter) { |
|
| 1285 | + return 'BEGIN TRANSACTION'; |
|
| 1286 | + } |
|
| 1287 | + Sqlite::demarrer_transaction($serveur); |
|
| 1288 | 1288 | |
| 1289 | - return true; |
|
| 1289 | + return true; |
|
| 1290 | 1290 | } |
| 1291 | 1291 | |
| 1292 | 1292 | /** |
@@ -1301,12 +1301,12 @@ discard block |
||
| 1301 | 1301 | **/ |
| 1302 | 1302 | function spip_sqlite_terminer_transaction($serveur = '', $requeter = true) |
| 1303 | 1303 | { |
| 1304 | - if (!$requeter) { |
|
| 1305 | - return 'COMMIT'; |
|
| 1306 | - } |
|
| 1307 | - Sqlite::finir_transaction($serveur); |
|
| 1304 | + if (!$requeter) { |
|
| 1305 | + return 'COMMIT'; |
|
| 1306 | + } |
|
| 1307 | + Sqlite::finir_transaction($serveur); |
|
| 1308 | 1308 | |
| 1309 | - return true; |
|
| 1309 | + return true; |
|
| 1310 | 1310 | } |
| 1311 | 1311 | |
| 1312 | 1312 | |
@@ -1322,27 +1322,27 @@ discard block |
||
| 1322 | 1322 | **/ |
| 1323 | 1323 | function spip_sqlite_listdbs($serveur = '', $requeter = true) |
| 1324 | 1324 | { |
| 1325 | - _sqlite_init(); |
|
| 1326 | - |
|
| 1327 | - if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1328 | - return []; |
|
| 1329 | - } |
|
| 1330 | - |
|
| 1331 | - include_spip('inc/flock'); |
|
| 1332 | - $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1333 | - $bds = []; |
|
| 1334 | - |
|
| 1335 | - foreach ($bases as $b) { |
|
| 1336 | - // pas de bases commencant pas sqlite |
|
| 1337 | - // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1338 | - // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1339 | - if (strpos($b, '_sqlite')) { |
|
| 1340 | - continue; |
|
| 1341 | - } |
|
| 1342 | - $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1343 | - } |
|
| 1344 | - |
|
| 1345 | - return $bds; |
|
| 1325 | + _sqlite_init(); |
|
| 1326 | + |
|
| 1327 | + if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1328 | + return []; |
|
| 1329 | + } |
|
| 1330 | + |
|
| 1331 | + include_spip('inc/flock'); |
|
| 1332 | + $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1333 | + $bds = []; |
|
| 1334 | + |
|
| 1335 | + foreach ($bases as $b) { |
|
| 1336 | + // pas de bases commencant pas sqlite |
|
| 1337 | + // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1338 | + // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1339 | + if (strpos($b, '_sqlite')) { |
|
| 1340 | + continue; |
|
| 1341 | + } |
|
| 1342 | + $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1343 | + } |
|
| 1344 | + |
|
| 1345 | + return $bds; |
|
| 1346 | 1346 | } |
| 1347 | 1347 | |
| 1348 | 1348 | |
@@ -1358,9 +1358,9 @@ discard block |
||
| 1358 | 1358 | */ |
| 1359 | 1359 | function spip_sqlite_multi($objet, $lang) |
| 1360 | 1360 | { |
| 1361 | - $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1361 | + $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1362 | 1362 | |
| 1363 | - return $r; |
|
| 1363 | + return $r; |
|
| 1364 | 1364 | } |
| 1365 | 1365 | |
| 1366 | 1366 | |
@@ -1378,15 +1378,15 @@ discard block |
||
| 1378 | 1378 | **/ |
| 1379 | 1379 | function spip_sqlite_optimize($table, $serveur = '', $requeter = true) |
| 1380 | 1380 | { |
| 1381 | - static $do = false; |
|
| 1382 | - if ($requeter and $do) { |
|
| 1383 | - return true; |
|
| 1384 | - } |
|
| 1385 | - if ($requeter) { |
|
| 1386 | - $do = true; |
|
| 1387 | - } |
|
| 1388 | - |
|
| 1389 | - return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1381 | + static $do = false; |
|
| 1382 | + if ($requeter and $do) { |
|
| 1383 | + return true; |
|
| 1384 | + } |
|
| 1385 | + if ($requeter) { |
|
| 1386 | + $do = true; |
|
| 1387 | + } |
|
| 1388 | + |
|
| 1389 | + return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1390 | 1390 | } |
| 1391 | 1391 | |
| 1392 | 1392 | |
@@ -1404,15 +1404,15 @@ discard block |
||
| 1404 | 1404 | */ |
| 1405 | 1405 | function spip_sqlite_quote($v, $type = '') |
| 1406 | 1406 | { |
| 1407 | - if (!is_array($v)) { |
|
| 1408 | - return _sqlite_calculer_cite($v, $type); |
|
| 1409 | - } |
|
| 1410 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1411 | - foreach ($v as $k => $r) { |
|
| 1412 | - $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1413 | - } |
|
| 1414 | - |
|
| 1415 | - return join(',', $v); |
|
| 1407 | + if (!is_array($v)) { |
|
| 1408 | + return _sqlite_calculer_cite($v, $type); |
|
| 1409 | + } |
|
| 1410 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1411 | + foreach ($v as $k => $r) { |
|
| 1412 | + $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1413 | + } |
|
| 1414 | + |
|
| 1415 | + return join(',', $v); |
|
| 1416 | 1416 | } |
| 1417 | 1417 | |
| 1418 | 1418 | |
@@ -1430,9 +1430,9 @@ discard block |
||
| 1430 | 1430 | **/ |
| 1431 | 1431 | function spip_sqlite_date_proche($champ, $interval, $unite) |
| 1432 | 1432 | { |
| 1433 | - $op = (($interval <= 0) ? '>' : '<'); |
|
| 1433 | + $op = (($interval <= 0) ? '>' : '<'); |
|
| 1434 | 1434 | |
| 1435 | - return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1435 | + return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1436 | 1436 | } |
| 1437 | 1437 | |
| 1438 | 1438 | |
@@ -1451,48 +1451,48 @@ discard block |
||
| 1451 | 1451 | */ |
| 1452 | 1452 | function spip_sqlite_repair($table, $serveur = '', $requeter = true) |
| 1453 | 1453 | { |
| 1454 | - if ( |
|
| 1455 | - $desc = spip_sqlite_showtable($table, $serveur) |
|
| 1456 | - and isset($desc['field']) |
|
| 1457 | - and is_array($desc['field']) |
|
| 1458 | - ) { |
|
| 1459 | - foreach ($desc['field'] as $c => $d) { |
|
| 1460 | - if ( |
|
| 1461 | - preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', $d) |
|
| 1462 | - and stripos($d, 'NOT NULL') !== false |
|
| 1463 | - and stripos($d, 'DEFAULT') === false |
|
| 1464 | - /* pas touche aux cles primaires */ |
|
| 1465 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1466 | - ) { |
|
| 1467 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1468 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1469 | - } |
|
| 1470 | - if ( |
|
| 1471 | - preg_match(',^(INTEGER),i', $d) |
|
| 1472 | - and stripos($d, 'NOT NULL') !== false |
|
| 1473 | - and stripos($d, 'DEFAULT') === false |
|
| 1474 | - /* pas touche aux cles primaires */ |
|
| 1475 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1476 | - ) { |
|
| 1477 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1478 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1479 | - } |
|
| 1480 | - if ( |
|
| 1481 | - preg_match(',^(datetime),i', $d) |
|
| 1482 | - and stripos($d, 'NOT NULL') !== false |
|
| 1483 | - and stripos($d, 'DEFAULT') === false |
|
| 1484 | - /* pas touche aux cles primaires */ |
|
| 1485 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1486 | - ) { |
|
| 1487 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1488 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1489 | - } |
|
| 1490 | - } |
|
| 1491 | - |
|
| 1492 | - return [' OK ']; |
|
| 1493 | - } |
|
| 1494 | - |
|
| 1495 | - return [' ERROR ']; |
|
| 1454 | + if ( |
|
| 1455 | + $desc = spip_sqlite_showtable($table, $serveur) |
|
| 1456 | + and isset($desc['field']) |
|
| 1457 | + and is_array($desc['field']) |
|
| 1458 | + ) { |
|
| 1459 | + foreach ($desc['field'] as $c => $d) { |
|
| 1460 | + if ( |
|
| 1461 | + preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', $d) |
|
| 1462 | + and stripos($d, 'NOT NULL') !== false |
|
| 1463 | + and stripos($d, 'DEFAULT') === false |
|
| 1464 | + /* pas touche aux cles primaires */ |
|
| 1465 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1466 | + ) { |
|
| 1467 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1468 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1469 | + } |
|
| 1470 | + if ( |
|
| 1471 | + preg_match(',^(INTEGER),i', $d) |
|
| 1472 | + and stripos($d, 'NOT NULL') !== false |
|
| 1473 | + and stripos($d, 'DEFAULT') === false |
|
| 1474 | + /* pas touche aux cles primaires */ |
|
| 1475 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1476 | + ) { |
|
| 1477 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1478 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1479 | + } |
|
| 1480 | + if ( |
|
| 1481 | + preg_match(',^(datetime),i', $d) |
|
| 1482 | + and stripos($d, 'NOT NULL') !== false |
|
| 1483 | + and stripos($d, 'DEFAULT') === false |
|
| 1484 | + /* pas touche aux cles primaires */ |
|
| 1485 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1486 | + ) { |
|
| 1487 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1488 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1489 | + } |
|
| 1490 | + } |
|
| 1491 | + |
|
| 1492 | + return [' OK ']; |
|
| 1493 | + } |
|
| 1494 | + |
|
| 1495 | + return [' ERROR ']; |
|
| 1496 | 1496 | } |
| 1497 | 1497 | |
| 1498 | 1498 | |
@@ -1522,25 +1522,25 @@ discard block |
||
| 1522 | 1522 | **/ |
| 1523 | 1523 | function spip_sqlite_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) |
| 1524 | 1524 | { |
| 1525 | - if (!$desc) { |
|
| 1526 | - $desc = description_table($table, $serveur); |
|
| 1527 | - } |
|
| 1528 | - if (!$desc) { |
|
| 1529 | - die("$table insertion sans description"); |
|
| 1530 | - } |
|
| 1531 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1532 | - |
|
| 1533 | - foreach ($couples as $champ => $val) { |
|
| 1534 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1535 | - } |
|
| 1536 | - |
|
| 1537 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1538 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1539 | - |
|
| 1540 | - return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1541 | - ',', |
|
| 1542 | - $couples |
|
| 1543 | - ) . ')', $serveur); |
|
| 1525 | + if (!$desc) { |
|
| 1526 | + $desc = description_table($table, $serveur); |
|
| 1527 | + } |
|
| 1528 | + if (!$desc) { |
|
| 1529 | + die("$table insertion sans description"); |
|
| 1530 | + } |
|
| 1531 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1532 | + |
|
| 1533 | + foreach ($couples as $champ => $val) { |
|
| 1534 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1535 | + } |
|
| 1536 | + |
|
| 1537 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1538 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1539 | + |
|
| 1540 | + return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1541 | + ',', |
|
| 1542 | + $couples |
|
| 1543 | + ) . ')', $serveur); |
|
| 1544 | 1544 | } |
| 1545 | 1545 | |
| 1546 | 1546 | |
@@ -1571,13 +1571,13 @@ discard block |
||
| 1571 | 1571 | function spip_sqlite_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) |
| 1572 | 1572 | { |
| 1573 | 1573 | |
| 1574 | - // boucler pour trainter chaque requete independemment |
|
| 1575 | - foreach ($tab_couples as $couples) { |
|
| 1576 | - $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1577 | - } |
|
| 1574 | + // boucler pour trainter chaque requete independemment |
|
| 1575 | + foreach ($tab_couples as $couples) { |
|
| 1576 | + $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1577 | + } |
|
| 1578 | 1578 | |
| 1579 | - // renvoie le dernier id |
|
| 1580 | - return $retour; |
|
| 1579 | + // renvoie le dernier id |
|
| 1580 | + return $retour; |
|
| 1581 | 1581 | } |
| 1582 | 1582 | |
| 1583 | 1583 | |
@@ -1604,44 +1604,44 @@ discard block |
||
| 1604 | 1604 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 1605 | 1605 | */ |
| 1606 | 1606 | function spip_sqlite_select( |
| 1607 | - $select, |
|
| 1608 | - $from, |
|
| 1609 | - $where = '', |
|
| 1610 | - $groupby = '', |
|
| 1611 | - $orderby = '', |
|
| 1612 | - $limit = '', |
|
| 1613 | - $having = '', |
|
| 1614 | - $serveur = '', |
|
| 1615 | - $requeter = true |
|
| 1607 | + $select, |
|
| 1608 | + $from, |
|
| 1609 | + $where = '', |
|
| 1610 | + $groupby = '', |
|
| 1611 | + $orderby = '', |
|
| 1612 | + $limit = '', |
|
| 1613 | + $having = '', |
|
| 1614 | + $serveur = '', |
|
| 1615 | + $requeter = true |
|
| 1616 | 1616 | ) { |
| 1617 | 1617 | |
| 1618 | - // version() n'est pas connu de sqlite |
|
| 1619 | - $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1620 | - |
|
| 1621 | - // recomposer from |
|
| 1622 | - $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1623 | - |
|
| 1624 | - $query = |
|
| 1625 | - _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1626 | - . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1627 | - . _sqlite_calculer_expression('WHERE', $where) |
|
| 1628 | - . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1629 | - . _sqlite_calculer_expression('HAVING', $having) |
|
| 1630 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1631 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1632 | - |
|
| 1633 | - // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1634 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1635 | - // texte de la requete demande ? |
|
| 1636 | - if (!$requeter) { |
|
| 1637 | - return $res; |
|
| 1638 | - } |
|
| 1639 | - // erreur survenue ? |
|
| 1640 | - if ($res === false) { |
|
| 1641 | - return Sqlite::traduire_requete($query, $serveur); |
|
| 1642 | - } |
|
| 1643 | - |
|
| 1644 | - return $res; |
|
| 1618 | + // version() n'est pas connu de sqlite |
|
| 1619 | + $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1620 | + |
|
| 1621 | + // recomposer from |
|
| 1622 | + $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1623 | + |
|
| 1624 | + $query = |
|
| 1625 | + _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1626 | + . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1627 | + . _sqlite_calculer_expression('WHERE', $where) |
|
| 1628 | + . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1629 | + . _sqlite_calculer_expression('HAVING', $having) |
|
| 1630 | + . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1631 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1632 | + |
|
| 1633 | + // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1634 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1635 | + // texte de la requete demande ? |
|
| 1636 | + if (!$requeter) { |
|
| 1637 | + return $res; |
|
| 1638 | + } |
|
| 1639 | + // erreur survenue ? |
|
| 1640 | + if ($res === false) { |
|
| 1641 | + return Sqlite::traduire_requete($query, $serveur); |
|
| 1642 | + } |
|
| 1643 | + |
|
| 1644 | + return $res; |
|
| 1645 | 1645 | } |
| 1646 | 1646 | |
| 1647 | 1647 | |
@@ -1661,32 +1661,32 @@ discard block |
||
| 1661 | 1661 | **/ |
| 1662 | 1662 | function spip_sqlite_selectdb($db, $serveur = '', $requeter = true) |
| 1663 | 1663 | { |
| 1664 | - _sqlite_init(); |
|
| 1665 | - |
|
| 1666 | - // interdire la creation d'une nouvelle base, |
|
| 1667 | - // sauf si on est dans l'installation |
|
| 1668 | - if ( |
|
| 1669 | - !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1670 | - && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1671 | - ) { |
|
| 1672 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1673 | - |
|
| 1674 | - return false; |
|
| 1675 | - } |
|
| 1676 | - |
|
| 1677 | - // se connecter a la base indiquee |
|
| 1678 | - // avec les identifiants connus |
|
| 1679 | - $index = $serveur ? $serveur : 0; |
|
| 1680 | - |
|
| 1681 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1682 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1683 | - return $db; |
|
| 1684 | - } |
|
| 1685 | - } else { |
|
| 1686 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1687 | - } |
|
| 1688 | - |
|
| 1689 | - return false; |
|
| 1664 | + _sqlite_init(); |
|
| 1665 | + |
|
| 1666 | + // interdire la creation d'une nouvelle base, |
|
| 1667 | + // sauf si on est dans l'installation |
|
| 1668 | + if ( |
|
| 1669 | + !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1670 | + && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1671 | + ) { |
|
| 1672 | + spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1673 | + |
|
| 1674 | + return false; |
|
| 1675 | + } |
|
| 1676 | + |
|
| 1677 | + // se connecter a la base indiquee |
|
| 1678 | + // avec les identifiants connus |
|
| 1679 | + $index = $serveur ? $serveur : 0; |
|
| 1680 | + |
|
| 1681 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1682 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1683 | + return $db; |
|
| 1684 | + } |
|
| 1685 | + } else { |
|
| 1686 | + spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1687 | + } |
|
| 1688 | + |
|
| 1689 | + return false; |
|
| 1690 | 1690 | } |
| 1691 | 1691 | |
| 1692 | 1692 | |
@@ -1702,8 +1702,8 @@ discard block |
||
| 1702 | 1702 | */ |
| 1703 | 1703 | function spip_sqlite_set_charset($charset, $serveur = '', $requeter = true) |
| 1704 | 1704 | { |
| 1705 | - # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1706 | - # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1705 | + # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1706 | + # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1707 | 1707 | } |
| 1708 | 1708 | |
| 1709 | 1709 | |
@@ -1722,24 +1722,24 @@ discard block |
||
| 1722 | 1722 | **/ |
| 1723 | 1723 | function spip_sqlite_showbase($match, $serveur = '', $requeter = true) |
| 1724 | 1724 | { |
| 1725 | - // type est le type d'entrée : table / index / view |
|
| 1726 | - // on ne retourne que les tables (?) et non les vues... |
|
| 1727 | - # ESCAPE non supporte par les versions sqlite <3 |
|
| 1728 | - # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1729 | - $match = preg_quote($match); |
|
| 1730 | - $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1731 | - $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1732 | - $match = str_replace('_', '.', $match); |
|
| 1733 | - $match = str_replace('%', '.*', $match); |
|
| 1734 | - $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1735 | - $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1736 | - $match = "^$match$"; |
|
| 1737 | - |
|
| 1738 | - return spip_sqlite_query( |
|
| 1739 | - "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1740 | - $serveur, |
|
| 1741 | - $requeter |
|
| 1742 | - ); |
|
| 1725 | + // type est le type d'entrée : table / index / view |
|
| 1726 | + // on ne retourne que les tables (?) et non les vues... |
|
| 1727 | + # ESCAPE non supporte par les versions sqlite <3 |
|
| 1728 | + # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1729 | + $match = preg_quote($match); |
|
| 1730 | + $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1731 | + $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1732 | + $match = str_replace('_', '.', $match); |
|
| 1733 | + $match = str_replace('%', '.*', $match); |
|
| 1734 | + $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1735 | + $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1736 | + $match = "^$match$"; |
|
| 1737 | + |
|
| 1738 | + return spip_sqlite_query( |
|
| 1739 | + "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1740 | + $serveur, |
|
| 1741 | + $requeter |
|
| 1742 | + ); |
|
| 1743 | 1743 | } |
| 1744 | 1744 | |
| 1745 | 1745 | /** |
@@ -1758,19 +1758,19 @@ discard block |
||
| 1758 | 1758 | **/ |
| 1759 | 1759 | function spip_sqlite_table_exists(string $table, $serveur = '', $requeter = true) |
| 1760 | 1760 | { |
| 1761 | - $r = spip_sqlite_query( |
|
| 1762 | - 'SELECT name FROM sqlite_master WHERE' |
|
| 1763 | - . ' type=\'table\'' |
|
| 1764 | - . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1765 | - . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1766 | - $serveur, |
|
| 1767 | - $requeter |
|
| 1768 | - ); |
|
| 1769 | - if (!$requeter) { |
|
| 1770 | - return $r; |
|
| 1771 | - } |
|
| 1772 | - $res = spip_sqlite_fetch($r, '', $serveur); |
|
| 1773 | - return (bool) $res; |
|
| 1761 | + $r = spip_sqlite_query( |
|
| 1762 | + 'SELECT name FROM sqlite_master WHERE' |
|
| 1763 | + . ' type=\'table\'' |
|
| 1764 | + . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1765 | + . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1766 | + $serveur, |
|
| 1767 | + $requeter |
|
| 1768 | + ); |
|
| 1769 | + if (!$requeter) { |
|
| 1770 | + return $r; |
|
| 1771 | + } |
|
| 1772 | + $res = spip_sqlite_fetch($r, '', $serveur); |
|
| 1773 | + return (bool) $res; |
|
| 1774 | 1774 | } |
| 1775 | 1775 | |
| 1776 | 1776 | define('_SQLITE_RE_SHOW_TABLE', '/^[^(),]*\(((?:[^()]*\((?:[^()]*\([^()]*\))?[^()]*\)[^()]*)*[^()]*)\)[^()]*$/'); |
@@ -1794,129 +1794,129 @@ discard block |
||
| 1794 | 1794 | */ |
| 1795 | 1795 | function spip_sqlite_showtable($nom_table, $serveur = '', $requeter = true) |
| 1796 | 1796 | { |
| 1797 | - $query = |
|
| 1798 | - 'SELECT sql, type FROM' |
|
| 1799 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1800 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1801 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1802 | - . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1803 | - . ' ORDER BY substr(type,2,1), name'; |
|
| 1804 | - |
|
| 1805 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1806 | - if (!$a) { |
|
| 1807 | - return ''; |
|
| 1808 | - } |
|
| 1809 | - if (!$requeter) { |
|
| 1810 | - return $a; |
|
| 1811 | - } |
|
| 1812 | - if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1813 | - return ''; |
|
| 1814 | - } |
|
| 1815 | - $vue = ($a['type'] == 'view'); // table | vue |
|
| 1816 | - |
|
| 1817 | - // c'est une table |
|
| 1818 | - // il faut parser le create |
|
| 1819 | - if (!$vue) { |
|
| 1820 | - if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1821 | - return ''; |
|
| 1822 | - } else { |
|
| 1823 | - $desc = $r[1]; |
|
| 1824 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1825 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1826 | - if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1827 | - $namedkeys = $r[2]; |
|
| 1828 | - $desc = $r[1]; |
|
| 1829 | - } else { |
|
| 1830 | - $namedkeys = ''; |
|
| 1831 | - } |
|
| 1832 | - |
|
| 1833 | - $fields = []; |
|
| 1834 | - $keys = []; |
|
| 1835 | - |
|
| 1836 | - // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1837 | - // par exemple s'il contiennent une virgule. |
|
| 1838 | - // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1839 | - list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1840 | - |
|
| 1841 | - // separer toutes les descriptions de champs, separes par des virgules |
|
| 1842 | - # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1843 | - $k_precedent = null; |
|
| 1844 | - foreach (explode(',', $desc) as $v) { |
|
| 1845 | - preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1846 | - // Les cles de champs peuvent etre entourees |
|
| 1847 | - // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1848 | - // http://www.sqlite.org/lang_keywords.html |
|
| 1849 | - $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1850 | - if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1851 | - $k = trim($k, $char); |
|
| 1852 | - if ($char == '[') { |
|
| 1853 | - $k = rtrim($k, ']'); |
|
| 1854 | - } |
|
| 1855 | - } |
|
| 1856 | - $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1857 | - |
|
| 1858 | - // rustine pour DECIMAL(10,2) |
|
| 1859 | - // s'il y a une parenthèse fermante dans la clé |
|
| 1860 | - // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1861 | - if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
|
| 1862 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1863 | - continue; |
|
| 1864 | - } |
|
| 1865 | - |
|
| 1866 | - // la primary key peut etre dans une des descriptions de champs |
|
| 1867 | - // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1868 | - if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1869 | - $keys['PRIMARY KEY'] = $k; |
|
| 1870 | - } |
|
| 1871 | - |
|
| 1872 | - $fields[$k] = $def; |
|
| 1873 | - $k_precedent = $k; |
|
| 1874 | - } |
|
| 1875 | - // key inclues dans la requete |
|
| 1876 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1877 | - if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 1878 | - $k = str_replace('`', '', trim($r[1])); |
|
| 1879 | - $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1880 | - if ($k && !isset($keys[$k])) { |
|
| 1881 | - $keys[$k] = $t; |
|
| 1882 | - } else { |
|
| 1883 | - $keys[] = $t; |
|
| 1884 | - } |
|
| 1885 | - } |
|
| 1886 | - } |
|
| 1887 | - // sinon ajouter les key index |
|
| 1888 | - $query = |
|
| 1889 | - 'SELECT name,sql FROM' |
|
| 1890 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1891 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1892 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1893 | - . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1894 | - . 'ORDER BY substr(type,2,1), name'; |
|
| 1895 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1896 | - while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1897 | - $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1898 | - $keytype = 'KEY'; |
|
| 1899 | - if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
|
| 1900 | - $keytype = 'UNIQUE KEY'; |
|
| 1901 | - } |
|
| 1902 | - $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1903 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1904 | - } |
|
| 1905 | - } |
|
| 1906 | - } // c'est une vue, on liste les champs disponibles simplement |
|
| 1907 | - else { |
|
| 1908 | - if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1909 | - $fields = []; |
|
| 1910 | - foreach ($res as $c => $v) { |
|
| 1911 | - $fields[$c] = ''; |
|
| 1912 | - } |
|
| 1913 | - $keys = []; |
|
| 1914 | - } else { |
|
| 1915 | - return ''; |
|
| 1916 | - } |
|
| 1917 | - } |
|
| 1918 | - |
|
| 1919 | - return ['field' => $fields, 'key' => $keys]; |
|
| 1797 | + $query = |
|
| 1798 | + 'SELECT sql, type FROM' |
|
| 1799 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1800 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1801 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1802 | + . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1803 | + . ' ORDER BY substr(type,2,1), name'; |
|
| 1804 | + |
|
| 1805 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1806 | + if (!$a) { |
|
| 1807 | + return ''; |
|
| 1808 | + } |
|
| 1809 | + if (!$requeter) { |
|
| 1810 | + return $a; |
|
| 1811 | + } |
|
| 1812 | + if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1813 | + return ''; |
|
| 1814 | + } |
|
| 1815 | + $vue = ($a['type'] == 'view'); // table | vue |
|
| 1816 | + |
|
| 1817 | + // c'est une table |
|
| 1818 | + // il faut parser le create |
|
| 1819 | + if (!$vue) { |
|
| 1820 | + if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1821 | + return ''; |
|
| 1822 | + } else { |
|
| 1823 | + $desc = $r[1]; |
|
| 1824 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1825 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1826 | + if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1827 | + $namedkeys = $r[2]; |
|
| 1828 | + $desc = $r[1]; |
|
| 1829 | + } else { |
|
| 1830 | + $namedkeys = ''; |
|
| 1831 | + } |
|
| 1832 | + |
|
| 1833 | + $fields = []; |
|
| 1834 | + $keys = []; |
|
| 1835 | + |
|
| 1836 | + // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1837 | + // par exemple s'il contiennent une virgule. |
|
| 1838 | + // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1839 | + list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1840 | + |
|
| 1841 | + // separer toutes les descriptions de champs, separes par des virgules |
|
| 1842 | + # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1843 | + $k_precedent = null; |
|
| 1844 | + foreach (explode(',', $desc) as $v) { |
|
| 1845 | + preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1846 | + // Les cles de champs peuvent etre entourees |
|
| 1847 | + // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1848 | + // http://www.sqlite.org/lang_keywords.html |
|
| 1849 | + $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1850 | + if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1851 | + $k = trim($k, $char); |
|
| 1852 | + if ($char == '[') { |
|
| 1853 | + $k = rtrim($k, ']'); |
|
| 1854 | + } |
|
| 1855 | + } |
|
| 1856 | + $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1857 | + |
|
| 1858 | + // rustine pour DECIMAL(10,2) |
|
| 1859 | + // s'il y a une parenthèse fermante dans la clé |
|
| 1860 | + // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1861 | + if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
|
| 1862 | + $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1863 | + continue; |
|
| 1864 | + } |
|
| 1865 | + |
|
| 1866 | + // la primary key peut etre dans une des descriptions de champs |
|
| 1867 | + // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1868 | + if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1869 | + $keys['PRIMARY KEY'] = $k; |
|
| 1870 | + } |
|
| 1871 | + |
|
| 1872 | + $fields[$k] = $def; |
|
| 1873 | + $k_precedent = $k; |
|
| 1874 | + } |
|
| 1875 | + // key inclues dans la requete |
|
| 1876 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1877 | + if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 1878 | + $k = str_replace('`', '', trim($r[1])); |
|
| 1879 | + $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1880 | + if ($k && !isset($keys[$k])) { |
|
| 1881 | + $keys[$k] = $t; |
|
| 1882 | + } else { |
|
| 1883 | + $keys[] = $t; |
|
| 1884 | + } |
|
| 1885 | + } |
|
| 1886 | + } |
|
| 1887 | + // sinon ajouter les key index |
|
| 1888 | + $query = |
|
| 1889 | + 'SELECT name,sql FROM' |
|
| 1890 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1891 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1892 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1893 | + . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1894 | + . 'ORDER BY substr(type,2,1), name'; |
|
| 1895 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1896 | + while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1897 | + $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1898 | + $keytype = 'KEY'; |
|
| 1899 | + if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
|
| 1900 | + $keytype = 'UNIQUE KEY'; |
|
| 1901 | + } |
|
| 1902 | + $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1903 | + $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1904 | + } |
|
| 1905 | + } |
|
| 1906 | + } // c'est une vue, on liste les champs disponibles simplement |
|
| 1907 | + else { |
|
| 1908 | + if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1909 | + $fields = []; |
|
| 1910 | + foreach ($res as $c => $v) { |
|
| 1911 | + $fields[$c] = ''; |
|
| 1912 | + } |
|
| 1913 | + $keys = []; |
|
| 1914 | + } else { |
|
| 1915 | + return ''; |
|
| 1916 | + } |
|
| 1917 | + } |
|
| 1918 | + |
|
| 1919 | + return ['field' => $fields, 'key' => $keys]; |
|
| 1920 | 1920 | } |
| 1921 | 1921 | |
| 1922 | 1922 | |
@@ -1943,24 +1943,24 @@ discard block |
||
| 1943 | 1943 | */ |
| 1944 | 1944 | function spip_sqlite_update($table, $champs, $where = '', $desc = '', $serveur = '', $requeter = true) |
| 1945 | 1945 | { |
| 1946 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1947 | - $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1948 | - |
|
| 1949 | - $set = []; |
|
| 1950 | - foreach ($champs as $champ => $val) { |
|
| 1951 | - $set[] = $champ . "=$val"; |
|
| 1952 | - } |
|
| 1953 | - if (!empty($set)) { |
|
| 1954 | - return spip_sqlite_query( |
|
| 1955 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1956 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1957 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1958 | - $serveur, |
|
| 1959 | - $requeter |
|
| 1960 | - ); |
|
| 1961 | - } |
|
| 1962 | - |
|
| 1963 | - return false; |
|
| 1946 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1947 | + $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1948 | + |
|
| 1949 | + $set = []; |
|
| 1950 | + foreach ($champs as $champ => $val) { |
|
| 1951 | + $set[] = $champ . "=$val"; |
|
| 1952 | + } |
|
| 1953 | + if (!empty($set)) { |
|
| 1954 | + return spip_sqlite_query( |
|
| 1955 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1956 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1957 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1958 | + $serveur, |
|
| 1959 | + $requeter |
|
| 1960 | + ); |
|
| 1961 | + } |
|
| 1962 | + |
|
| 1963 | + return false; |
|
| 1964 | 1964 | } |
| 1965 | 1965 | |
| 1966 | 1966 | |
@@ -1991,38 +1991,38 @@ discard block |
||
| 1991 | 1991 | function spip_sqlite_updateq($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) |
| 1992 | 1992 | { |
| 1993 | 1993 | |
| 1994 | - if (!$champs) { |
|
| 1995 | - return; |
|
| 1996 | - } |
|
| 1997 | - if (!$desc) { |
|
| 1998 | - $desc = description_table($table, $serveur); |
|
| 1999 | - } |
|
| 2000 | - if (!$desc) { |
|
| 2001 | - die("$table insertion sans description"); |
|
| 2002 | - } |
|
| 2003 | - $fields = $desc['field']; |
|
| 2004 | - |
|
| 2005 | - $set = []; |
|
| 2006 | - foreach ($champs as $champ => $val) { |
|
| 2007 | - $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2008 | - } |
|
| 2009 | - |
|
| 2010 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 2011 | - // attention ils sont deja quotes |
|
| 2012 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 2013 | - foreach ($maj as $champ => $val) { |
|
| 2014 | - if (!isset($set[$champ])) { |
|
| 2015 | - $set[$champ] = $champ . '=' . $val; |
|
| 2016 | - } |
|
| 2017 | - } |
|
| 2018 | - |
|
| 2019 | - return spip_sqlite_query( |
|
| 2020 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 2021 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 2022 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 2023 | - $serveur, |
|
| 2024 | - $requeter |
|
| 2025 | - ); |
|
| 1994 | + if (!$champs) { |
|
| 1995 | + return; |
|
| 1996 | + } |
|
| 1997 | + if (!$desc) { |
|
| 1998 | + $desc = description_table($table, $serveur); |
|
| 1999 | + } |
|
| 2000 | + if (!$desc) { |
|
| 2001 | + die("$table insertion sans description"); |
|
| 2002 | + } |
|
| 2003 | + $fields = $desc['field']; |
|
| 2004 | + |
|
| 2005 | + $set = []; |
|
| 2006 | + foreach ($champs as $champ => $val) { |
|
| 2007 | + $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2008 | + } |
|
| 2009 | + |
|
| 2010 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 2011 | + // attention ils sont deja quotes |
|
| 2012 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 2013 | + foreach ($maj as $champ => $val) { |
|
| 2014 | + if (!isset($set[$champ])) { |
|
| 2015 | + $set[$champ] = $champ . '=' . $val; |
|
| 2016 | + } |
|
| 2017 | + } |
|
| 2018 | + |
|
| 2019 | + return spip_sqlite_query( |
|
| 2020 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 2021 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 2022 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 2023 | + $serveur, |
|
| 2024 | + $requeter |
|
| 2025 | + ); |
|
| 2026 | 2026 | } |
| 2027 | 2027 | |
| 2028 | 2028 | |
@@ -2041,17 +2041,17 @@ discard block |
||
| 2041 | 2041 | */ |
| 2042 | 2042 | function _sqlite_init() |
| 2043 | 2043 | { |
| 2044 | - if (!defined('_DIR_DB')) { |
|
| 2045 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2046 | - } |
|
| 2047 | - if (!defined('_SQLITE_CHMOD')) { |
|
| 2048 | - define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2049 | - } |
|
| 2050 | - |
|
| 2051 | - if (!is_dir($d = _DIR_DB)) { |
|
| 2052 | - include_spip('inc/flock'); |
|
| 2053 | - sous_repertoire($d); |
|
| 2054 | - } |
|
| 2044 | + if (!defined('_DIR_DB')) { |
|
| 2045 | + define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2046 | + } |
|
| 2047 | + if (!defined('_SQLITE_CHMOD')) { |
|
| 2048 | + define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2049 | + } |
|
| 2050 | + |
|
| 2051 | + if (!is_dir($d = _DIR_DB)) { |
|
| 2052 | + include_spip('inc/flock'); |
|
| 2053 | + sous_repertoire($d); |
|
| 2054 | + } |
|
| 2055 | 2055 | } |
| 2056 | 2056 | |
| 2057 | 2057 | |
@@ -2066,20 +2066,20 @@ discard block |
||
| 2066 | 2066 | */ |
| 2067 | 2067 | function _sqlite_is_version($version = '', $link = '', $serveur = '', $requeter = true) |
| 2068 | 2068 | { |
| 2069 | - if ($link === '') { |
|
| 2070 | - $link = _sqlite_link($serveur); |
|
| 2071 | - } |
|
| 2072 | - if (!$link) { |
|
| 2073 | - return false; |
|
| 2074 | - } |
|
| 2069 | + if ($link === '') { |
|
| 2070 | + $link = _sqlite_link($serveur); |
|
| 2071 | + } |
|
| 2072 | + if (!$link) { |
|
| 2073 | + return false; |
|
| 2074 | + } |
|
| 2075 | 2075 | |
| 2076 | - $v = 3; |
|
| 2076 | + $v = 3; |
|
| 2077 | 2077 | |
| 2078 | - if (!$version) { |
|
| 2079 | - return $v; |
|
| 2080 | - } |
|
| 2078 | + if (!$version) { |
|
| 2079 | + return $v; |
|
| 2080 | + } |
|
| 2081 | 2081 | |
| 2082 | - return ($version == $v); |
|
| 2082 | + return ($version == $v); |
|
| 2083 | 2083 | } |
| 2084 | 2084 | |
| 2085 | 2085 | |
@@ -2091,9 +2091,9 @@ discard block |
||
| 2091 | 2091 | */ |
| 2092 | 2092 | function _sqlite_link($serveur = ''): ?\PDO |
| 2093 | 2093 | { |
| 2094 | - $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 2094 | + $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 2095 | 2095 | |
| 2096 | - return $link; |
|
| 2096 | + return $link; |
|
| 2097 | 2097 | } |
| 2098 | 2098 | |
| 2099 | 2099 | |
@@ -2109,54 +2109,54 @@ discard block |
||
| 2109 | 2109 | */ |
| 2110 | 2110 | function _sqlite_calculer_cite($v, $type) |
| 2111 | 2111 | { |
| 2112 | - if ($type) { |
|
| 2113 | - if ( |
|
| 2114 | - is_null($v) |
|
| 2115 | - and stripos($type, 'NOT NULL') === false |
|
| 2116 | - ) { |
|
| 2117 | - // null php se traduit en NULL SQL |
|
| 2118 | - return 'NULL'; |
|
| 2119 | - } |
|
| 2120 | - |
|
| 2121 | - if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 2122 | - return $v; |
|
| 2123 | - } |
|
| 2124 | - if (sql_test_int($type)) { |
|
| 2125 | - if (is_numeric($v)) { |
|
| 2126 | - return $v; |
|
| 2127 | - } elseif ($v === null) { |
|
| 2128 | - return 0; |
|
| 2129 | - } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) === 0) { |
|
| 2130 | - return hexdec(substr($v, 2)); |
|
| 2131 | - } else { |
|
| 2132 | - return intval($v); |
|
| 2133 | - } |
|
| 2134 | - } |
|
| 2135 | - } else { |
|
| 2136 | - // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2137 | - if (is_bool($v)) { |
|
| 2138 | - return strval(intval($v)); |
|
| 2139 | - } elseif (is_numeric($v)) { |
|
| 2140 | - return strval($v); |
|
| 2141 | - } |
|
| 2142 | - } |
|
| 2143 | - |
|
| 2144 | - // trouver un link sqlite pour faire l'echappement |
|
| 2145 | - foreach ($GLOBALS['connexions'] as $s) { |
|
| 2146 | - if ( |
|
| 2147 | - $l = $s['link'] |
|
| 2148 | - and is_object($l) |
|
| 2149 | - and $l instanceof \PDO |
|
| 2150 | - and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2151 | - ) { |
|
| 2152 | - return $l->quote($v ?? ''); |
|
| 2153 | - } |
|
| 2154 | - } |
|
| 2155 | - |
|
| 2156 | - // echapper les ' en '' |
|
| 2157 | - spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2158 | - |
|
| 2159 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2112 | + if ($type) { |
|
| 2113 | + if ( |
|
| 2114 | + is_null($v) |
|
| 2115 | + and stripos($type, 'NOT NULL') === false |
|
| 2116 | + ) { |
|
| 2117 | + // null php se traduit en NULL SQL |
|
| 2118 | + return 'NULL'; |
|
| 2119 | + } |
|
| 2120 | + |
|
| 2121 | + if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 2122 | + return $v; |
|
| 2123 | + } |
|
| 2124 | + if (sql_test_int($type)) { |
|
| 2125 | + if (is_numeric($v)) { |
|
| 2126 | + return $v; |
|
| 2127 | + } elseif ($v === null) { |
|
| 2128 | + return 0; |
|
| 2129 | + } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) === 0) { |
|
| 2130 | + return hexdec(substr($v, 2)); |
|
| 2131 | + } else { |
|
| 2132 | + return intval($v); |
|
| 2133 | + } |
|
| 2134 | + } |
|
| 2135 | + } else { |
|
| 2136 | + // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2137 | + if (is_bool($v)) { |
|
| 2138 | + return strval(intval($v)); |
|
| 2139 | + } elseif (is_numeric($v)) { |
|
| 2140 | + return strval($v); |
|
| 2141 | + } |
|
| 2142 | + } |
|
| 2143 | + |
|
| 2144 | + // trouver un link sqlite pour faire l'echappement |
|
| 2145 | + foreach ($GLOBALS['connexions'] as $s) { |
|
| 2146 | + if ( |
|
| 2147 | + $l = $s['link'] |
|
| 2148 | + and is_object($l) |
|
| 2149 | + and $l instanceof \PDO |
|
| 2150 | + and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2151 | + ) { |
|
| 2152 | + return $l->quote($v ?? ''); |
|
| 2153 | + } |
|
| 2154 | + } |
|
| 2155 | + |
|
| 2156 | + // echapper les ' en '' |
|
| 2157 | + spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2158 | + |
|
| 2159 | + return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2160 | 2160 | } |
| 2161 | 2161 | |
| 2162 | 2162 | |
@@ -2173,21 +2173,21 @@ discard block |
||
| 2173 | 2173 | */ |
| 2174 | 2174 | function _sqlite_calculer_expression($expression, $v, $join = 'AND') |
| 2175 | 2175 | { |
| 2176 | - if (empty($v)) { |
|
| 2177 | - return ''; |
|
| 2178 | - } |
|
| 2179 | - |
|
| 2180 | - $exp = "\n$expression "; |
|
| 2181 | - |
|
| 2182 | - if (!is_array($v)) { |
|
| 2183 | - return $exp . $v; |
|
| 2184 | - } else { |
|
| 2185 | - if (strtoupper($join) === 'AND') { |
|
| 2186 | - return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2187 | - } else { |
|
| 2188 | - return $exp . join($join, $v); |
|
| 2189 | - } |
|
| 2190 | - } |
|
| 2176 | + if (empty($v)) { |
|
| 2177 | + return ''; |
|
| 2178 | + } |
|
| 2179 | + |
|
| 2180 | + $exp = "\n$expression "; |
|
| 2181 | + |
|
| 2182 | + if (!is_array($v)) { |
|
| 2183 | + return $exp . $v; |
|
| 2184 | + } else { |
|
| 2185 | + if (strtoupper($join) === 'AND') { |
|
| 2186 | + return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2187 | + } else { |
|
| 2188 | + return $exp . join($join, $v); |
|
| 2189 | + } |
|
| 2190 | + } |
|
| 2191 | 2191 | } |
| 2192 | 2192 | |
| 2193 | 2193 | |
@@ -2204,7 +2204,7 @@ discard block |
||
| 2204 | 2204 | */ |
| 2205 | 2205 | function _sqlite_calculer_order($orderby) |
| 2206 | 2206 | { |
| 2207 | - return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 2207 | + return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 2208 | 2208 | } |
| 2209 | 2209 | |
| 2210 | 2210 | |
@@ -2216,26 +2216,26 @@ discard block |
||
| 2216 | 2216 | */ |
| 2217 | 2217 | function _sqlite_calculer_select_as($args) |
| 2218 | 2218 | { |
| 2219 | - $res = ''; |
|
| 2220 | - foreach ($args as $k => $v) { |
|
| 2221 | - if (substr($k, -1) == '@') { |
|
| 2222 | - // c'est une jointure qui se refere au from precedent |
|
| 2223 | - // pas de virgule |
|
| 2224 | - $res .= ' ' . $v; |
|
| 2225 | - } else { |
|
| 2226 | - if (!is_numeric($k)) { |
|
| 2227 | - $p = strpos($v, ' '); |
|
| 2228 | - if ($p) { |
|
| 2229 | - $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2230 | - } else { |
|
| 2231 | - $v .= " AS '$k'"; |
|
| 2232 | - } |
|
| 2233 | - } |
|
| 2234 | - $res .= ', ' . $v; |
|
| 2235 | - } |
|
| 2236 | - } |
|
| 2237 | - |
|
| 2238 | - return substr($res, 2); |
|
| 2219 | + $res = ''; |
|
| 2220 | + foreach ($args as $k => $v) { |
|
| 2221 | + if (substr($k, -1) == '@') { |
|
| 2222 | + // c'est une jointure qui se refere au from precedent |
|
| 2223 | + // pas de virgule |
|
| 2224 | + $res .= ' ' . $v; |
|
| 2225 | + } else { |
|
| 2226 | + if (!is_numeric($k)) { |
|
| 2227 | + $p = strpos($v, ' '); |
|
| 2228 | + if ($p) { |
|
| 2229 | + $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2230 | + } else { |
|
| 2231 | + $v .= " AS '$k'"; |
|
| 2232 | + } |
|
| 2233 | + } |
|
| 2234 | + $res .= ', ' . $v; |
|
| 2235 | + } |
|
| 2236 | + } |
|
| 2237 | + |
|
| 2238 | + return substr($res, 2); |
|
| 2239 | 2239 | } |
| 2240 | 2240 | |
| 2241 | 2241 | |
@@ -2259,26 +2259,26 @@ discard block |
||
| 2259 | 2259 | */ |
| 2260 | 2260 | function _sqlite_calculer_where($v) |
| 2261 | 2261 | { |
| 2262 | - if (!is_array($v)) { |
|
| 2263 | - return $v; |
|
| 2264 | - } |
|
| 2265 | - |
|
| 2266 | - $op = array_shift($v); |
|
| 2267 | - if (!($n = count($v))) { |
|
| 2268 | - return $op; |
|
| 2269 | - } else { |
|
| 2270 | - $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2271 | - if ($n == 1) { |
|
| 2272 | - return "$op($arg)"; |
|
| 2273 | - } else { |
|
| 2274 | - $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2275 | - if ($n == 2) { |
|
| 2276 | - return "($arg $op $arg2)"; |
|
| 2277 | - } else { |
|
| 2278 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 2279 | - } |
|
| 2280 | - } |
|
| 2281 | - } |
|
| 2262 | + if (!is_array($v)) { |
|
| 2263 | + return $v; |
|
| 2264 | + } |
|
| 2265 | + |
|
| 2266 | + $op = array_shift($v); |
|
| 2267 | + if (!($n = count($v))) { |
|
| 2268 | + return $op; |
|
| 2269 | + } else { |
|
| 2270 | + $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2271 | + if ($n == 1) { |
|
| 2272 | + return "$op($arg)"; |
|
| 2273 | + } else { |
|
| 2274 | + $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2275 | + if ($n == 2) { |
|
| 2276 | + return "($arg $op $arg2)"; |
|
| 2277 | + } else { |
|
| 2278 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 2279 | + } |
|
| 2280 | + } |
|
| 2281 | + } |
|
| 2282 | 2282 | } |
| 2283 | 2283 | |
| 2284 | 2284 | |
@@ -2294,19 +2294,19 @@ discard block |
||
| 2294 | 2294 | */ |
| 2295 | 2295 | function _sqlite_charger_version($version = '') |
| 2296 | 2296 | { |
| 2297 | - $versions = []; |
|
| 2298 | - |
|
| 2299 | - // version 3 |
|
| 2300 | - if (!$version || $version == 3) { |
|
| 2301 | - if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2302 | - $versions[] = 3; |
|
| 2303 | - } |
|
| 2304 | - } |
|
| 2305 | - if ($version) { |
|
| 2306 | - return in_array($version, $versions); |
|
| 2307 | - } |
|
| 2308 | - |
|
| 2309 | - return $versions; |
|
| 2297 | + $versions = []; |
|
| 2298 | + |
|
| 2299 | + // version 3 |
|
| 2300 | + if (!$version || $version == 3) { |
|
| 2301 | + if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2302 | + $versions[] = 3; |
|
| 2303 | + } |
|
| 2304 | + } |
|
| 2305 | + if ($version) { |
|
| 2306 | + return in_array($version, $versions); |
|
| 2307 | + } |
|
| 2308 | + |
|
| 2309 | + return $versions; |
|
| 2310 | 2310 | } |
| 2311 | 2311 | |
| 2312 | 2312 | |
@@ -2345,147 +2345,147 @@ discard block |
||
| 2345 | 2345 | function _sqlite_modifier_table($table, $colonne, $opt = [], $serveur = '') |
| 2346 | 2346 | { |
| 2347 | 2347 | |
| 2348 | - if (is_array($table)) { |
|
| 2349 | - $table_destination = reset($table); |
|
| 2350 | - $table_origine = key($table); |
|
| 2351 | - } else { |
|
| 2352 | - $table_origine = $table_destination = $table; |
|
| 2353 | - } |
|
| 2354 | - // ne prend actuellement qu'un changement |
|
| 2355 | - // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2356 | - if (is_array($colonne)) { |
|
| 2357 | - $colonne_destination = reset($colonne); |
|
| 2358 | - $colonne_origine = key($colonne); |
|
| 2359 | - } else { |
|
| 2360 | - $colonne_origine = $colonne_destination = $colonne; |
|
| 2361 | - } |
|
| 2362 | - if (!isset($opt['field'])) { |
|
| 2363 | - $opt['field'] = []; |
|
| 2364 | - } |
|
| 2365 | - if (!isset($opt['key'])) { |
|
| 2366 | - $opt['key'] = []; |
|
| 2367 | - } |
|
| 2368 | - |
|
| 2369 | - // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2370 | - // on prendra directement le nom de la future table |
|
| 2371 | - $meme_table = ($table_origine == $table_destination); |
|
| 2372 | - |
|
| 2373 | - $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2374 | - if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2375 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2376 | - |
|
| 2377 | - return false; |
|
| 2378 | - } |
|
| 2379 | - |
|
| 2380 | - |
|
| 2381 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2382 | - |
|
| 2383 | - // 1) creer une table temporaire avec les modifications |
|
| 2384 | - // - DROP : suppression de la colonne |
|
| 2385 | - // - CHANGE : modification de la colonne |
|
| 2386 | - // (foreach pour conserver l'ordre des champs) |
|
| 2387 | - |
|
| 2388 | - // field |
|
| 2389 | - $fields = []; |
|
| 2390 | - // pour le INSERT INTO plus loin |
|
| 2391 | - // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2392 | - $fields_correspondances = []; |
|
| 2393 | - foreach ($def_origine['field'] as $c => $d) { |
|
| 2394 | - if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2395 | - // si pas DROP |
|
| 2396 | - if ($colonne_destination) { |
|
| 2397 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2398 | - $fields_correspondances[$colonne_destination] = $c; |
|
| 2399 | - } |
|
| 2400 | - } else { |
|
| 2401 | - $fields[$c] = $d; |
|
| 2402 | - $fields_correspondances[$c] = $c; |
|
| 2403 | - } |
|
| 2404 | - } |
|
| 2405 | - // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2406 | - if (!$colonne_origine && $colonne_destination) { |
|
| 2407 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2408 | - } |
|
| 2409 | - |
|
| 2410 | - // key... |
|
| 2411 | - $keys = []; |
|
| 2412 | - foreach ($def_origine['key'] as $c => $d) { |
|
| 2413 | - $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2414 | - $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2415 | - // seulement si on ne supprime pas la colonne ! |
|
| 2416 | - if ($d) { |
|
| 2417 | - $keys[$c] = $d; |
|
| 2418 | - } |
|
| 2419 | - } |
|
| 2420 | - |
|
| 2421 | - // autres keys, on merge |
|
| 2422 | - $keys = array_merge($keys, $opt['key']); |
|
| 2423 | - $queries = []; |
|
| 2424 | - |
|
| 2425 | - // copier dans destination (si differente de origine), sinon tmp |
|
| 2426 | - $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2427 | - $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2428 | - and $keys['PRIMARY KEY'] |
|
| 2429 | - and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2430 | - and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2431 | - |
|
| 2432 | - if ( |
|
| 2433 | - $q = _sqlite_requete_create( |
|
| 2434 | - $table_copie, |
|
| 2435 | - $fields, |
|
| 2436 | - $keys, |
|
| 2437 | - $autoinc, |
|
| 2438 | - $temporary = false, |
|
| 2439 | - $ifnotexists = true, |
|
| 2440 | - $serveur |
|
| 2441 | - ) |
|
| 2442 | - ) { |
|
| 2443 | - $queries[] = $q; |
|
| 2444 | - } |
|
| 2445 | - |
|
| 2446 | - |
|
| 2447 | - // 2) y copier les champs qui vont bien |
|
| 2448 | - $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2449 | - $champs_ori = join(', ', $fields_correspondances); |
|
| 2450 | - $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2451 | - |
|
| 2452 | - // 3) supprimer la table d'origine |
|
| 2453 | - $queries[] = "DROP TABLE $table_origine"; |
|
| 2454 | - |
|
| 2455 | - // 4) renommer la table temporaire |
|
| 2456 | - // avec le nom de la table destination |
|
| 2457 | - // si necessaire |
|
| 2458 | - if ($meme_table) { |
|
| 2459 | - $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2460 | - } |
|
| 2461 | - |
|
| 2462 | - // 5) remettre les index ! |
|
| 2463 | - foreach ($keys as $k => $v) { |
|
| 2464 | - if ($k == 'PRIMARY KEY') { |
|
| 2465 | - } else { |
|
| 2466 | - // enlever KEY |
|
| 2467 | - $k = substr($k, 4); |
|
| 2468 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2469 | - } |
|
| 2470 | - } |
|
| 2471 | - |
|
| 2472 | - |
|
| 2473 | - if (count($queries)) { |
|
| 2474 | - Sqlite::demarrer_transaction($serveur); |
|
| 2475 | - // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2476 | - foreach ($queries as $q) { |
|
| 2477 | - if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2478 | - spip_log('SQLite : ALTER TABLE table :' |
|
| 2479 | - . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2480 | - Sqlite::annuler_transaction($serveur); |
|
| 2481 | - |
|
| 2482 | - return false; |
|
| 2483 | - } |
|
| 2484 | - } |
|
| 2485 | - Sqlite::finir_transaction($serveur); |
|
| 2486 | - } |
|
| 2487 | - |
|
| 2488 | - return true; |
|
| 2348 | + if (is_array($table)) { |
|
| 2349 | + $table_destination = reset($table); |
|
| 2350 | + $table_origine = key($table); |
|
| 2351 | + } else { |
|
| 2352 | + $table_origine = $table_destination = $table; |
|
| 2353 | + } |
|
| 2354 | + // ne prend actuellement qu'un changement |
|
| 2355 | + // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2356 | + if (is_array($colonne)) { |
|
| 2357 | + $colonne_destination = reset($colonne); |
|
| 2358 | + $colonne_origine = key($colonne); |
|
| 2359 | + } else { |
|
| 2360 | + $colonne_origine = $colonne_destination = $colonne; |
|
| 2361 | + } |
|
| 2362 | + if (!isset($opt['field'])) { |
|
| 2363 | + $opt['field'] = []; |
|
| 2364 | + } |
|
| 2365 | + if (!isset($opt['key'])) { |
|
| 2366 | + $opt['key'] = []; |
|
| 2367 | + } |
|
| 2368 | + |
|
| 2369 | + // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2370 | + // on prendra directement le nom de la future table |
|
| 2371 | + $meme_table = ($table_origine == $table_destination); |
|
| 2372 | + |
|
| 2373 | + $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2374 | + if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2375 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2376 | + |
|
| 2377 | + return false; |
|
| 2378 | + } |
|
| 2379 | + |
|
| 2380 | + |
|
| 2381 | + $table_tmp = $table_origine . '_tmp'; |
|
| 2382 | + |
|
| 2383 | + // 1) creer une table temporaire avec les modifications |
|
| 2384 | + // - DROP : suppression de la colonne |
|
| 2385 | + // - CHANGE : modification de la colonne |
|
| 2386 | + // (foreach pour conserver l'ordre des champs) |
|
| 2387 | + |
|
| 2388 | + // field |
|
| 2389 | + $fields = []; |
|
| 2390 | + // pour le INSERT INTO plus loin |
|
| 2391 | + // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2392 | + $fields_correspondances = []; |
|
| 2393 | + foreach ($def_origine['field'] as $c => $d) { |
|
| 2394 | + if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2395 | + // si pas DROP |
|
| 2396 | + if ($colonne_destination) { |
|
| 2397 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2398 | + $fields_correspondances[$colonne_destination] = $c; |
|
| 2399 | + } |
|
| 2400 | + } else { |
|
| 2401 | + $fields[$c] = $d; |
|
| 2402 | + $fields_correspondances[$c] = $c; |
|
| 2403 | + } |
|
| 2404 | + } |
|
| 2405 | + // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2406 | + if (!$colonne_origine && $colonne_destination) { |
|
| 2407 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2408 | + } |
|
| 2409 | + |
|
| 2410 | + // key... |
|
| 2411 | + $keys = []; |
|
| 2412 | + foreach ($def_origine['key'] as $c => $d) { |
|
| 2413 | + $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2414 | + $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2415 | + // seulement si on ne supprime pas la colonne ! |
|
| 2416 | + if ($d) { |
|
| 2417 | + $keys[$c] = $d; |
|
| 2418 | + } |
|
| 2419 | + } |
|
| 2420 | + |
|
| 2421 | + // autres keys, on merge |
|
| 2422 | + $keys = array_merge($keys, $opt['key']); |
|
| 2423 | + $queries = []; |
|
| 2424 | + |
|
| 2425 | + // copier dans destination (si differente de origine), sinon tmp |
|
| 2426 | + $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2427 | + $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2428 | + and $keys['PRIMARY KEY'] |
|
| 2429 | + and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2430 | + and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2431 | + |
|
| 2432 | + if ( |
|
| 2433 | + $q = _sqlite_requete_create( |
|
| 2434 | + $table_copie, |
|
| 2435 | + $fields, |
|
| 2436 | + $keys, |
|
| 2437 | + $autoinc, |
|
| 2438 | + $temporary = false, |
|
| 2439 | + $ifnotexists = true, |
|
| 2440 | + $serveur |
|
| 2441 | + ) |
|
| 2442 | + ) { |
|
| 2443 | + $queries[] = $q; |
|
| 2444 | + } |
|
| 2445 | + |
|
| 2446 | + |
|
| 2447 | + // 2) y copier les champs qui vont bien |
|
| 2448 | + $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2449 | + $champs_ori = join(', ', $fields_correspondances); |
|
| 2450 | + $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2451 | + |
|
| 2452 | + // 3) supprimer la table d'origine |
|
| 2453 | + $queries[] = "DROP TABLE $table_origine"; |
|
| 2454 | + |
|
| 2455 | + // 4) renommer la table temporaire |
|
| 2456 | + // avec le nom de la table destination |
|
| 2457 | + // si necessaire |
|
| 2458 | + if ($meme_table) { |
|
| 2459 | + $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2460 | + } |
|
| 2461 | + |
|
| 2462 | + // 5) remettre les index ! |
|
| 2463 | + foreach ($keys as $k => $v) { |
|
| 2464 | + if ($k == 'PRIMARY KEY') { |
|
| 2465 | + } else { |
|
| 2466 | + // enlever KEY |
|
| 2467 | + $k = substr($k, 4); |
|
| 2468 | + $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2469 | + } |
|
| 2470 | + } |
|
| 2471 | + |
|
| 2472 | + |
|
| 2473 | + if (count($queries)) { |
|
| 2474 | + Sqlite::demarrer_transaction($serveur); |
|
| 2475 | + // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2476 | + foreach ($queries as $q) { |
|
| 2477 | + if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2478 | + spip_log('SQLite : ALTER TABLE table :' |
|
| 2479 | + . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2480 | + Sqlite::annuler_transaction($serveur); |
|
| 2481 | + |
|
| 2482 | + return false; |
|
| 2483 | + } |
|
| 2484 | + } |
|
| 2485 | + Sqlite::finir_transaction($serveur); |
|
| 2486 | + } |
|
| 2487 | + |
|
| 2488 | + return true; |
|
| 2489 | 2489 | } |
| 2490 | 2490 | |
| 2491 | 2491 | |
@@ -2496,61 +2496,61 @@ discard block |
||
| 2496 | 2496 | */ |
| 2497 | 2497 | function _sqlite_ref_fonctions() |
| 2498 | 2498 | { |
| 2499 | - $fonctions = [ |
|
| 2500 | - 'alter' => 'spip_sqlite_alter', |
|
| 2501 | - 'count' => 'spip_sqlite_count', |
|
| 2502 | - 'countsel' => 'spip_sqlite_countsel', |
|
| 2503 | - 'create' => 'spip_sqlite_create', |
|
| 2504 | - 'create_base' => 'spip_sqlite_create_base', |
|
| 2505 | - 'create_view' => 'spip_sqlite_create_view', |
|
| 2506 | - 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2507 | - 'delete' => 'spip_sqlite_delete', |
|
| 2508 | - 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2509 | - 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2510 | - 'errno' => 'spip_sqlite_errno', |
|
| 2511 | - 'error' => 'spip_sqlite_error', |
|
| 2512 | - 'explain' => 'spip_sqlite_explain', |
|
| 2513 | - 'fetch' => 'spip_sqlite_fetch', |
|
| 2514 | - 'seek' => 'spip_sqlite_seek', |
|
| 2515 | - 'free' => 'spip_sqlite_free', |
|
| 2516 | - 'hex' => 'spip_sqlite_hex', |
|
| 2517 | - 'in' => 'spip_sqlite_in', |
|
| 2518 | - 'insert' => 'spip_sqlite_insert', |
|
| 2519 | - 'insertq' => 'spip_sqlite_insertq', |
|
| 2520 | - 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2521 | - 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2522 | - 'multi' => 'spip_sqlite_multi', |
|
| 2523 | - 'optimize' => 'spip_sqlite_optimize', |
|
| 2524 | - 'query' => 'spip_sqlite_query', |
|
| 2525 | - 'quote' => 'spip_sqlite_quote', |
|
| 2526 | - 'repair' => 'spip_sqlite_repair', |
|
| 2527 | - 'replace' => 'spip_sqlite_replace', |
|
| 2528 | - 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2529 | - 'select' => 'spip_sqlite_select', |
|
| 2530 | - 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2531 | - 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2532 | - 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2533 | - 'showbase' => 'spip_sqlite_showbase', |
|
| 2534 | - 'showtable' => 'spip_sqlite_showtable', |
|
| 2535 | - 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2536 | - 'update' => 'spip_sqlite_update', |
|
| 2537 | - 'updateq' => 'spip_sqlite_updateq', |
|
| 2538 | - 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2539 | - 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2540 | - 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2541 | - ]; |
|
| 2542 | - |
|
| 2543 | - // association de chaque nom http d'un charset aux couples sqlite |
|
| 2544 | - // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2545 | - $charsets = [ |
|
| 2546 | - 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2547 | - //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2548 | - //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2549 | - ]; |
|
| 2550 | - |
|
| 2551 | - $fonctions['charsets'] = $charsets; |
|
| 2552 | - |
|
| 2553 | - return $fonctions; |
|
| 2499 | + $fonctions = [ |
|
| 2500 | + 'alter' => 'spip_sqlite_alter', |
|
| 2501 | + 'count' => 'spip_sqlite_count', |
|
| 2502 | + 'countsel' => 'spip_sqlite_countsel', |
|
| 2503 | + 'create' => 'spip_sqlite_create', |
|
| 2504 | + 'create_base' => 'spip_sqlite_create_base', |
|
| 2505 | + 'create_view' => 'spip_sqlite_create_view', |
|
| 2506 | + 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2507 | + 'delete' => 'spip_sqlite_delete', |
|
| 2508 | + 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2509 | + 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2510 | + 'errno' => 'spip_sqlite_errno', |
|
| 2511 | + 'error' => 'spip_sqlite_error', |
|
| 2512 | + 'explain' => 'spip_sqlite_explain', |
|
| 2513 | + 'fetch' => 'spip_sqlite_fetch', |
|
| 2514 | + 'seek' => 'spip_sqlite_seek', |
|
| 2515 | + 'free' => 'spip_sqlite_free', |
|
| 2516 | + 'hex' => 'spip_sqlite_hex', |
|
| 2517 | + 'in' => 'spip_sqlite_in', |
|
| 2518 | + 'insert' => 'spip_sqlite_insert', |
|
| 2519 | + 'insertq' => 'spip_sqlite_insertq', |
|
| 2520 | + 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2521 | + 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2522 | + 'multi' => 'spip_sqlite_multi', |
|
| 2523 | + 'optimize' => 'spip_sqlite_optimize', |
|
| 2524 | + 'query' => 'spip_sqlite_query', |
|
| 2525 | + 'quote' => 'spip_sqlite_quote', |
|
| 2526 | + 'repair' => 'spip_sqlite_repair', |
|
| 2527 | + 'replace' => 'spip_sqlite_replace', |
|
| 2528 | + 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2529 | + 'select' => 'spip_sqlite_select', |
|
| 2530 | + 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2531 | + 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2532 | + 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2533 | + 'showbase' => 'spip_sqlite_showbase', |
|
| 2534 | + 'showtable' => 'spip_sqlite_showtable', |
|
| 2535 | + 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2536 | + 'update' => 'spip_sqlite_update', |
|
| 2537 | + 'updateq' => 'spip_sqlite_updateq', |
|
| 2538 | + 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2539 | + 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2540 | + 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2541 | + ]; |
|
| 2542 | + |
|
| 2543 | + // association de chaque nom http d'un charset aux couples sqlite |
|
| 2544 | + // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2545 | + $charsets = [ |
|
| 2546 | + 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2547 | + //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2548 | + //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2549 | + ]; |
|
| 2550 | + |
|
| 2551 | + $fonctions['charsets'] = $charsets; |
|
| 2552 | + |
|
| 2553 | + return $fonctions; |
|
| 2554 | 2554 | } |
| 2555 | 2555 | |
| 2556 | 2556 | |
@@ -2563,56 +2563,56 @@ discard block |
||
| 2563 | 2563 | */ |
| 2564 | 2564 | function _sqlite_remplacements_definitions_table($query, $autoinc = false) |
| 2565 | 2565 | { |
| 2566 | - // quelques remplacements |
|
| 2567 | - $num = '(\s*\([0-9]*\))?'; |
|
| 2568 | - $enum = '(\s*\([^\)]*\))?'; |
|
| 2569 | - |
|
| 2570 | - $remplace = [ |
|
| 2571 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2572 | - '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2573 | - '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2574 | - '/auto_increment/is' => '', |
|
| 2575 | - '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2576 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2577 | - '/character set \w+/is' => '', |
|
| 2578 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2579 | - '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2580 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2581 | - '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2582 | - '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2583 | - ]; |
|
| 2584 | - |
|
| 2585 | - // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2586 | - $remplace_autocinc = [ |
|
| 2587 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2588 | - ]; |
|
| 2589 | - // pour les int non autoincrement, il faut un DEFAULT |
|
| 2590 | - $remplace_nonautocinc = [ |
|
| 2591 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2592 | - ]; |
|
| 2593 | - |
|
| 2594 | - if (is_string($query)) { |
|
| 2595 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2596 | - if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2597 | - $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2598 | - } else { |
|
| 2599 | - $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2600 | - $query = _sqlite_collate_ci($query); |
|
| 2601 | - } |
|
| 2602 | - } elseif (is_array($query)) { |
|
| 2603 | - foreach ($query as $k => $q) { |
|
| 2604 | - $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2605 | - $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2606 | - if ($ai) { |
|
| 2607 | - $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2608 | - } else { |
|
| 2609 | - $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2610 | - $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2611 | - } |
|
| 2612 | - } |
|
| 2613 | - } |
|
| 2614 | - |
|
| 2615 | - return $query; |
|
| 2566 | + // quelques remplacements |
|
| 2567 | + $num = '(\s*\([0-9]*\))?'; |
|
| 2568 | + $enum = '(\s*\([^\)]*\))?'; |
|
| 2569 | + |
|
| 2570 | + $remplace = [ |
|
| 2571 | + '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2572 | + '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2573 | + '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2574 | + '/auto_increment/is' => '', |
|
| 2575 | + '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2576 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2577 | + '/character set \w+/is' => '', |
|
| 2578 | + '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2579 | + '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2580 | + '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2581 | + '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2582 | + '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2583 | + ]; |
|
| 2584 | + |
|
| 2585 | + // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2586 | + $remplace_autocinc = [ |
|
| 2587 | + '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2588 | + ]; |
|
| 2589 | + // pour les int non autoincrement, il faut un DEFAULT |
|
| 2590 | + $remplace_nonautocinc = [ |
|
| 2591 | + '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2592 | + ]; |
|
| 2593 | + |
|
| 2594 | + if (is_string($query)) { |
|
| 2595 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2596 | + if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2597 | + $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2598 | + } else { |
|
| 2599 | + $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2600 | + $query = _sqlite_collate_ci($query); |
|
| 2601 | + } |
|
| 2602 | + } elseif (is_array($query)) { |
|
| 2603 | + foreach ($query as $k => $q) { |
|
| 2604 | + $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2605 | + $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2606 | + if ($ai) { |
|
| 2607 | + $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2608 | + } else { |
|
| 2609 | + $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2610 | + $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2611 | + } |
|
| 2612 | + } |
|
| 2613 | + } |
|
| 2614 | + |
|
| 2615 | + return $query; |
|
| 2616 | 2616 | } |
| 2617 | 2617 | |
| 2618 | 2618 | /** |
@@ -2624,17 +2624,17 @@ discard block |
||
| 2624 | 2624 | */ |
| 2625 | 2625 | function _sqlite_collate_ci($champ) |
| 2626 | 2626 | { |
| 2627 | - if (stripos($champ, 'COLLATE') !== false) { |
|
| 2628 | - return $champ; |
|
| 2629 | - } |
|
| 2630 | - if (stripos($champ, 'BINARY') !== false) { |
|
| 2631 | - return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2632 | - } |
|
| 2633 | - if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2634 | - return $champ . ' COLLATE NOCASE'; |
|
| 2635 | - } |
|
| 2636 | - |
|
| 2637 | - return $champ; |
|
| 2627 | + if (stripos($champ, 'COLLATE') !== false) { |
|
| 2628 | + return $champ; |
|
| 2629 | + } |
|
| 2630 | + if (stripos($champ, 'BINARY') !== false) { |
|
| 2631 | + return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2632 | + } |
|
| 2633 | + if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2634 | + return $champ . ' COLLATE NOCASE'; |
|
| 2635 | + } |
|
| 2636 | + |
|
| 2637 | + return $champ; |
|
| 2638 | 2638 | } |
| 2639 | 2639 | |
| 2640 | 2640 | |
@@ -2653,84 +2653,84 @@ discard block |
||
| 2653 | 2653 | * @return bool|string |
| 2654 | 2654 | */ |
| 2655 | 2655 | function _sqlite_requete_create( |
| 2656 | - $nom, |
|
| 2657 | - $champs, |
|
| 2658 | - $cles, |
|
| 2659 | - $autoinc = false, |
|
| 2660 | - $temporary = false, |
|
| 2661 | - $_ifnotexists = true, |
|
| 2662 | - $serveur = '', |
|
| 2663 | - $requeter = true |
|
| 2656 | + $nom, |
|
| 2657 | + $champs, |
|
| 2658 | + $cles, |
|
| 2659 | + $autoinc = false, |
|
| 2660 | + $temporary = false, |
|
| 2661 | + $_ifnotexists = true, |
|
| 2662 | + $serveur = '', |
|
| 2663 | + $requeter = true |
|
| 2664 | 2664 | ) { |
| 2665 | - $query = $keys = $s = $p = ''; |
|
| 2666 | - |
|
| 2667 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2668 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2669 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 2670 | - return; |
|
| 2671 | - } |
|
| 2672 | - |
|
| 2673 | - // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2674 | - // il faut passer par des create index |
|
| 2675 | - // Il gere par contre primary key ! |
|
| 2676 | - // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2677 | - // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2678 | - $pk = 'PRIMARY KEY'; |
|
| 2679 | - // le champ de cle primaire |
|
| 2680 | - $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2681 | - |
|
| 2682 | - foreach ($champs as $k => $v) { |
|
| 2683 | - if (false !== stripos($v, $pk)) { |
|
| 2684 | - $champ_pk = $k; |
|
| 2685 | - // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2686 | - $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2687 | - break; |
|
| 2688 | - } |
|
| 2689 | - } |
|
| 2690 | - |
|
| 2691 | - if ($champ_pk) { |
|
| 2692 | - $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2693 | - } |
|
| 2694 | - // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2695 | - if ( |
|
| 2696 | - isset($champs[$champ_pk]) |
|
| 2697 | - and stripos($champs[$champ_pk], 'default 0') !== false |
|
| 2698 | - ) { |
|
| 2699 | - $champs[$champ_pk] = trim(str_ireplace('default 0', '', $champs[$champ_pk])); |
|
| 2700 | - } |
|
| 2701 | - |
|
| 2702 | - $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2703 | - foreach ($champs as $k => $v) { |
|
| 2704 | - $query .= "$s\n\t\t$k $v"; |
|
| 2705 | - $s = ','; |
|
| 2706 | - } |
|
| 2707 | - |
|
| 2708 | - $ifnotexists = ''; |
|
| 2709 | - if ($_ifnotexists) { |
|
| 2710 | - $version = spip_sqlite_fetch( |
|
| 2711 | - spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2712 | - '', |
|
| 2713 | - $serveur |
|
| 2714 | - ); |
|
| 2715 | - if (!function_exists('spip_version_compare')) { |
|
| 2716 | - include_spip('plugins/installer'); |
|
| 2717 | - } |
|
| 2718 | - |
|
| 2719 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2720 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 2721 | - } else { |
|
| 2722 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2723 | - $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2724 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2725 | - return true; |
|
| 2726 | - } |
|
| 2727 | - } |
|
| 2728 | - } |
|
| 2729 | - |
|
| 2730 | - $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2731 | - $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2732 | - |
|
| 2733 | - return $q; |
|
| 2665 | + $query = $keys = $s = $p = ''; |
|
| 2666 | + |
|
| 2667 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2668 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2669 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 2670 | + return; |
|
| 2671 | + } |
|
| 2672 | + |
|
| 2673 | + // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2674 | + // il faut passer par des create index |
|
| 2675 | + // Il gere par contre primary key ! |
|
| 2676 | + // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2677 | + // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2678 | + $pk = 'PRIMARY KEY'; |
|
| 2679 | + // le champ de cle primaire |
|
| 2680 | + $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2681 | + |
|
| 2682 | + foreach ($champs as $k => $v) { |
|
| 2683 | + if (false !== stripos($v, $pk)) { |
|
| 2684 | + $champ_pk = $k; |
|
| 2685 | + // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2686 | + $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2687 | + break; |
|
| 2688 | + } |
|
| 2689 | + } |
|
| 2690 | + |
|
| 2691 | + if ($champ_pk) { |
|
| 2692 | + $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2693 | + } |
|
| 2694 | + // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2695 | + if ( |
|
| 2696 | + isset($champs[$champ_pk]) |
|
| 2697 | + and stripos($champs[$champ_pk], 'default 0') !== false |
|
| 2698 | + ) { |
|
| 2699 | + $champs[$champ_pk] = trim(str_ireplace('default 0', '', $champs[$champ_pk])); |
|
| 2700 | + } |
|
| 2701 | + |
|
| 2702 | + $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2703 | + foreach ($champs as $k => $v) { |
|
| 2704 | + $query .= "$s\n\t\t$k $v"; |
|
| 2705 | + $s = ','; |
|
| 2706 | + } |
|
| 2707 | + |
|
| 2708 | + $ifnotexists = ''; |
|
| 2709 | + if ($_ifnotexists) { |
|
| 2710 | + $version = spip_sqlite_fetch( |
|
| 2711 | + spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2712 | + '', |
|
| 2713 | + $serveur |
|
| 2714 | + ); |
|
| 2715 | + if (!function_exists('spip_version_compare')) { |
|
| 2716 | + include_spip('plugins/installer'); |
|
| 2717 | + } |
|
| 2718 | + |
|
| 2719 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2720 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 2721 | + } else { |
|
| 2722 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2723 | + $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2724 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 2725 | + return true; |
|
| 2726 | + } |
|
| 2727 | + } |
|
| 2728 | + } |
|
| 2729 | + |
|
| 2730 | + $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2731 | + $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2732 | + |
|
| 2733 | + return $q; |
|
| 2734 | 2734 | } |
| 2735 | 2735 | |
| 2736 | 2736 | |
@@ -2750,40 +2750,40 @@ discard block |
||
| 2750 | 2750 | */ |
| 2751 | 2751 | function _sqlite_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') |
| 2752 | 2752 | { |
| 2753 | - static $tables = []; |
|
| 2754 | - |
|
| 2755 | - if (!isset($tables[$table])) { |
|
| 2756 | - if (!$desc) { |
|
| 2757 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2758 | - $desc = $trouver_table($table, $serveur); |
|
| 2759 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 2760 | - if (!$desc) { |
|
| 2761 | - return $couples; |
|
| 2762 | - } |
|
| 2763 | - } |
|
| 2764 | - |
|
| 2765 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2766 | - // cependant, il faudra peut etre etendre |
|
| 2767 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 2768 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 2769 | - $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2770 | - |
|
| 2771 | - $now = _sqlite_func_now(true); |
|
| 2772 | - foreach ($desc['field'] as $k => $v) { |
|
| 2773 | - if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2774 | - $tables[$table]['desc'][$k] = $v; |
|
| 2775 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2776 | - } |
|
| 2777 | - } |
|
| 2778 | - } else { |
|
| 2779 | - $now = _sqlite_func_now(true); |
|
| 2780 | - foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2781 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2782 | - } |
|
| 2783 | - } |
|
| 2784 | - |
|
| 2785 | - // ajout des champs type 'timestamp' absents |
|
| 2786 | - return array_merge($tables[$table]['valeur'], $couples); |
|
| 2753 | + static $tables = []; |
|
| 2754 | + |
|
| 2755 | + if (!isset($tables[$table])) { |
|
| 2756 | + if (!$desc) { |
|
| 2757 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2758 | + $desc = $trouver_table($table, $serveur); |
|
| 2759 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 2760 | + if (!$desc) { |
|
| 2761 | + return $couples; |
|
| 2762 | + } |
|
| 2763 | + } |
|
| 2764 | + |
|
| 2765 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2766 | + // cependant, il faudra peut etre etendre |
|
| 2767 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 2768 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 2769 | + $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2770 | + |
|
| 2771 | + $now = _sqlite_func_now(true); |
|
| 2772 | + foreach ($desc['field'] as $k => $v) { |
|
| 2773 | + if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2774 | + $tables[$table]['desc'][$k] = $v; |
|
| 2775 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2776 | + } |
|
| 2777 | + } |
|
| 2778 | + } else { |
|
| 2779 | + $now = _sqlite_func_now(true); |
|
| 2780 | + foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2781 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2782 | + } |
|
| 2783 | + } |
|
| 2784 | + |
|
| 2785 | + // ajout des champs type 'timestamp' absents |
|
| 2786 | + return array_merge($tables[$table]['valeur'], $couples); |
|
| 2787 | 2787 | } |
| 2788 | 2788 | |
| 2789 | 2789 | |
@@ -2795,5 +2795,5 @@ discard block |
||
| 2795 | 2795 | */ |
| 2796 | 2796 | function spip_versions_sqlite() |
| 2797 | 2797 | { |
| 2798 | - return _sqlite_charger_version(); |
|
| 2798 | + return _sqlite_charger_version(); |
|
| 2799 | 2799 | } |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | // Si a ce stade on n'a pas de table, il y a un bug |
| 164 | 164 | if (!is_array($this->tableau)) { |
| 165 | 165 | $this->err = true; |
| 166 | - spip_log('erreur datasource ' . var_export($command, true)); |
|
| 166 | + spip_log('erreur datasource '.var_export($command, true)); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | // {datapath query.results} |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | isset($this->command['sourcemode']) |
| 206 | 206 | and !in_array($this->command['sourcemode'], ['table', 'array', 'tableau']) |
| 207 | 207 | ) { |
| 208 | - charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true); |
|
| 208 | + charger_fonction($this->command['sourcemode'].'_to_array', 'inc', true); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | # le premier argument peut etre un array, une URL etc. |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | # avons-nous un cache dispo ? |
| 215 | 215 | $cle = null; |
| 216 | 216 | if (is_string($src)) { |
| 217 | - $cle = 'datasource_' . md5($this->command['sourcemode'] . ':' . var_export($this->command['source'], true)); |
|
| 217 | + $cle = 'datasource_'.md5($this->command['sourcemode'].':'.var_export($this->command['source'], true)); |
|
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | $cache = $this->cache_get($cle); |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | } |
| 273 | 273 | if ( |
| 274 | 274 | !$this->err |
| 275 | - and $data_to_array = charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true) |
|
| 275 | + and $data_to_array = charger_fonction($this->command['sourcemode'].'_to_array', 'inc', true) |
|
| 276 | 276 | ) { |
| 277 | 277 | $args = $this->command['source']; |
| 278 | 278 | $args[0] = $data; |
@@ -412,13 +412,13 @@ discard block |
||
| 412 | 412 | $tv = '%s'; |
| 413 | 413 | } # {par valeur/xx/yy} ?? |
| 414 | 414 | else { |
| 415 | - $tv = 'table_valeur(%s, ' . var_export($r[1], true) . ')'; |
|
| 415 | + $tv = 'table_valeur(%s, '.var_export($r[1], true).')'; |
|
| 416 | 416 | } |
| 417 | 417 | $sortfunc .= ' |
| 418 | - $a = ' . sprintf($tv, '$aa') . '; |
|
| 419 | - $b = ' . sprintf($tv, '$bb') . '; |
|
| 418 | + $a = ' . sprintf($tv, '$aa').'; |
|
| 419 | + $b = ' . sprintf($tv, '$bb').'; |
|
| 420 | 420 | if ($a <> $b) |
| 421 | - return ($a ' . (!empty($r[2]) ? '>' : '<') . ' $b) ? -1 : 1;'; |
|
| 421 | + return ($a ' . (!empty($r[2]) ? '>' : '<').' $b) ? -1 : 1;'; |
|
| 422 | 422 | } |
| 423 | 423 | } |
| 424 | 424 | } |
@@ -12,500 +12,500 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Data extends AbstractIterateur implements Iterator |
| 14 | 14 | { |
| 15 | - /** Tableau de données */ |
|
| 16 | - protected array $tableau = []; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * Conditions de filtrage |
|
| 20 | - * ie criteres de selection |
|
| 21 | - */ |
|
| 22 | - protected array $filtre = []; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Cle courante |
|
| 26 | - * |
|
| 27 | - * @var scalar |
|
| 28 | - */ |
|
| 29 | - protected $cle = null; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Valeur courante |
|
| 33 | - * |
|
| 34 | - * @var mixed |
|
| 35 | - */ |
|
| 36 | - protected $valeur = null; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Constructeur |
|
| 40 | - * |
|
| 41 | - * @param $command |
|
| 42 | - * @param array $info |
|
| 43 | - */ |
|
| 44 | - public function __construct(array $command, array $info = []) { |
|
| 45 | - include_spip('iterateur/data'); |
|
| 46 | - $this->type = 'DATA'; |
|
| 47 | - $this->command = $command; |
|
| 48 | - $this->info = $info; |
|
| 49 | - $this->select($command); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Revenir au depart |
|
| 54 | - * |
|
| 55 | - * @return void |
|
| 56 | - */ |
|
| 57 | - public function rewind(): void { |
|
| 58 | - reset($this->tableau); |
|
| 59 | - $this->cle = array_key_first($this->tableau); |
|
| 60 | - $this->valeur = current($this->tableau); |
|
| 61 | - next($this->tableau); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Déclarer les critères exceptions |
|
| 66 | - * |
|
| 67 | - * @return array |
|
| 68 | - */ |
|
| 69 | - public function exception_des_criteres() { |
|
| 70 | - return ['tableau']; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Récupérer depuis le cache si possible |
|
| 75 | - * |
|
| 76 | - * @param string $cle |
|
| 77 | - * @return mixed |
|
| 78 | - */ |
|
| 79 | - protected function cache_get($cle) { |
|
| 80 | - if (!$cle) { |
|
| 81 | - return; |
|
| 82 | - } |
|
| 83 | - # utiliser memoization si dispo |
|
| 84 | - if (!function_exists('cache_get')) { |
|
| 85 | - return; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - return cache_get($cle); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Stocker en cache si possible |
|
| 93 | - * |
|
| 94 | - * @param string $cle |
|
| 95 | - * @param int $ttl |
|
| 96 | - * @param null|mixed $valeur |
|
| 97 | - * @return bool |
|
| 98 | - */ |
|
| 99 | - protected function cache_set($cle, $ttl, $valeur = null) { |
|
| 100 | - if (!$cle) { |
|
| 101 | - return; |
|
| 102 | - } |
|
| 103 | - if (is_null($valeur)) { |
|
| 104 | - $valeur = $this->tableau; |
|
| 105 | - } |
|
| 106 | - # utiliser memoization si dispo |
|
| 107 | - if (!function_exists('cache_set')) { |
|
| 108 | - return; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - return cache_set( |
|
| 112 | - $cle, |
|
| 113 | - [ |
|
| 114 | - 'data' => $valeur, |
|
| 115 | - 'time' => time(), |
|
| 116 | - 'ttl' => $ttl |
|
| 117 | - ], |
|
| 118 | - 3600 + $ttl |
|
| 119 | - ); |
|
| 120 | - # conserver le cache 1h de plus que la validite demandee, |
|
| 121 | - # pour le cas ou le serveur distant ne reponde plus |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Aller chercher les données de la boucle DATA |
|
| 126 | - * |
|
| 127 | - * @throws Exception |
|
| 128 | - * @param array $command |
|
| 129 | - * @return void |
|
| 130 | - */ |
|
| 131 | - protected function select($command) { |
|
| 132 | - |
|
| 133 | - // l'iterateur DATA peut etre appele en passant (data:type) |
|
| 134 | - // le type se retrouve dans la commande 'from' |
|
| 135 | - // dans ce cas la le critere {source}, si present, n'a pas besoin du 1er argument |
|
| 136 | - if (isset($this->command['from'][0])) { |
|
| 137 | - if (isset($this->command['source']) and is_array($this->command['source'])) { |
|
| 138 | - array_unshift($this->command['source'], $this->command['sourcemode']); |
|
| 139 | - } |
|
| 140 | - $this->command['sourcemode'] = $this->command['from'][0]; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // cherchons differents moyens de creer le tableau de donnees |
|
| 144 | - // les commandes connues pour l'iterateur DATA |
|
| 145 | - // sont : {tableau #ARRAY} ; {cle=...} ; {valeur=...} |
|
| 146 | - |
|
| 147 | - // {source format, [URL], [arg2]...} |
|
| 148 | - if ( |
|
| 149 | - isset($this->command['source']) |
|
| 150 | - and isset($this->command['sourcemode']) |
|
| 151 | - ) { |
|
| 152 | - $this->select_source(); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - // Critere {liste X1, X2, X3} |
|
| 156 | - if (isset($this->command['liste'])) { |
|
| 157 | - $this->select_liste(); |
|
| 158 | - } |
|
| 159 | - if (isset($this->command['enum'])) { |
|
| 160 | - $this->select_enum(); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // Si a ce stade on n'a pas de table, il y a un bug |
|
| 164 | - if (!is_array($this->tableau)) { |
|
| 165 | - $this->err = true; |
|
| 166 | - spip_log('erreur datasource ' . var_export($command, true)); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - // {datapath query.results} |
|
| 170 | - // extraire le chemin "query.results" du tableau de donnees |
|
| 171 | - if ( |
|
| 172 | - !$this->err |
|
| 173 | - and isset($this->command['datapath']) |
|
| 174 | - and is_array($this->command['datapath']) |
|
| 175 | - ) { |
|
| 176 | - $this->select_datapath(); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // tri {par x} |
|
| 180 | - if ($this->command['orderby']) { |
|
| 181 | - $this->select_orderby(); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - // grouper les resultats {fusion /x/y/z} ; |
|
| 185 | - if ($this->command['groupby']) { |
|
| 186 | - $this->select_groupby(); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $this->rewind(); |
|
| 190 | - #var_dump($this->tableau); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Aller chercher les donnees de la boucle DATA |
|
| 196 | - * depuis une source |
|
| 197 | - * {source format, [URL], [arg2]...} |
|
| 198 | - */ |
|
| 199 | - protected function select_source() { |
|
| 200 | - # un peu crado : avant de charger le cache il faut charger |
|
| 201 | - # les class indispensables, sinon PHP ne saura pas gerer |
|
| 202 | - # l'objet en cache ; cf plugins/icalendar |
|
| 203 | - # perf : pas de fonction table_to_array ! (table est deja un array) |
|
| 204 | - if ( |
|
| 205 | - isset($this->command['sourcemode']) |
|
| 206 | - and !in_array($this->command['sourcemode'], ['table', 'array', 'tableau']) |
|
| 207 | - ) { |
|
| 208 | - charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - # le premier argument peut etre un array, une URL etc. |
|
| 212 | - $src = $this->command['source'][0] ?? null; |
|
| 213 | - |
|
| 214 | - # avons-nous un cache dispo ? |
|
| 215 | - $cle = null; |
|
| 216 | - if (is_string($src)) { |
|
| 217 | - $cle = 'datasource_' . md5($this->command['sourcemode'] . ':' . var_export($this->command['source'], true)); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - $cache = $this->cache_get($cle); |
|
| 221 | - if (isset($this->command['datacache'])) { |
|
| 222 | - $ttl = intval($this->command['datacache']); |
|
| 223 | - } |
|
| 224 | - if ( |
|
| 225 | - $cache |
|
| 226 | - and ($cache['time'] + ($ttl ?? $cache['ttl']) |
|
| 227 | - > time()) |
|
| 228 | - and !(_request('var_mode') === 'recalcul' |
|
| 229 | - and include_spip('inc/autoriser') |
|
| 230 | - and autoriser('recalcul') |
|
| 231 | - ) |
|
| 232 | - ) { |
|
| 233 | - $this->tableau = $cache['data']; |
|
| 234 | - } else { |
|
| 235 | - try { |
|
| 236 | - if ( |
|
| 237 | - isset($this->command['sourcemode']) |
|
| 238 | - and in_array( |
|
| 239 | - $this->command['sourcemode'], |
|
| 240 | - ['table', 'array', 'tableau'] |
|
| 241 | - ) |
|
| 242 | - ) { |
|
| 243 | - if ( |
|
| 244 | - is_array($a = $src) |
|
| 245 | - or (is_string($a) |
|
| 246 | - and $a = str_replace('"', '"', $a) # fragile! |
|
| 247 | - and is_array($a = @unserialize($a))) |
|
| 248 | - ) { |
|
| 249 | - $this->tableau = $a; |
|
| 250 | - } |
|
| 251 | - } else { |
|
| 252 | - $data = $src; |
|
| 253 | - if (is_string($src)) { |
|
| 254 | - if (tester_url_absolue($src)) { |
|
| 255 | - include_spip('inc/distant'); |
|
| 256 | - $data = recuperer_url($src, ['taille_max' => _DATA_SOURCE_MAX_SIZE]); |
|
| 257 | - $data = $data['page'] ?? ''; |
|
| 258 | - if (!$data) { |
|
| 259 | - throw new Exception('404'); |
|
| 260 | - } |
|
| 261 | - if (!isset($ttl)) { |
|
| 262 | - $ttl = 24 * 3600; |
|
| 263 | - } |
|
| 264 | - } elseif (@is_dir($src)) { |
|
| 265 | - $data = $src; |
|
| 266 | - } elseif (@is_readable($src) && @is_file($src)) { |
|
| 267 | - $data = spip_file_get_contents($src); |
|
| 268 | - } |
|
| 269 | - if (!isset($ttl)) { |
|
| 270 | - $ttl = 10; |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - if ( |
|
| 274 | - !$this->err |
|
| 275 | - and $data_to_array = charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true) |
|
| 276 | - ) { |
|
| 277 | - $args = $this->command['source']; |
|
| 278 | - $args[0] = $data; |
|
| 279 | - if (is_array($a = $data_to_array(...$args))) { |
|
| 280 | - $this->tableau = $a; |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - if (!is_array($this->tableau)) { |
|
| 286 | - $this->err = true; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - if (!$this->err and isset($ttl) and $ttl > 0) { |
|
| 290 | - $this->cache_set($cle, $ttl); |
|
| 291 | - } |
|
| 292 | - } catch (Exception $e) { |
|
| 293 | - $e = $e->getMessage(); |
|
| 294 | - $err = sprintf( |
|
| 295 | - "[%s, %s] $e", |
|
| 296 | - $src, |
|
| 297 | - $this->command['sourcemode'] |
|
| 298 | - ); |
|
| 299 | - erreur_squelette([$err, []]); |
|
| 300 | - $this->err = true; |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - # en cas d'erreur, utiliser le cache si encore dispo |
|
| 305 | - if ( |
|
| 306 | - $this->err |
|
| 307 | - and $cache |
|
| 308 | - ) { |
|
| 309 | - $this->tableau = $cache['data']; |
|
| 310 | - $this->err = false; |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Retourne un tableau donne depuis un critère liste |
|
| 317 | - * |
|
| 318 | - * Critère `{liste X1, X2, X3}` |
|
| 319 | - * |
|
| 320 | - * @see critere_DATA_liste_dist() |
|
| 321 | - * |
|
| 322 | - **/ |
|
| 323 | - protected function select_liste() { |
|
| 324 | - # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE |
|
| 325 | - if (!isset($this->command['liste'][1])) { |
|
| 326 | - if (!is_array($this->command['liste'][0])) { |
|
| 327 | - $this->command['liste'] = explode(',', $this->command['liste'][0]); |
|
| 328 | - } else { |
|
| 329 | - $this->command['liste'] = $this->command['liste'][0]; |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - $this->tableau = $this->command['liste']; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Retourne un tableau donne depuis un critere liste |
|
| 337 | - * Critere {enum Xmin, Xmax} |
|
| 338 | - * |
|
| 339 | - **/ |
|
| 340 | - protected function select_enum() { |
|
| 341 | - # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE |
|
| 342 | - if (!isset($this->command['enum'][1])) { |
|
| 343 | - if (!is_array($this->command['enum'][0])) { |
|
| 344 | - $this->command['enum'] = explode(',', $this->command['enum'][0]); |
|
| 345 | - } else { |
|
| 346 | - $this->command['enum'] = $this->command['enum'][0]; |
|
| 347 | - } |
|
| 348 | - } |
|
| 349 | - if ((is_countable($this->command['enum']) ? count($this->command['enum']) : 0) >= 3) { |
|
| 350 | - $enum = range( |
|
| 351 | - array_shift($this->command['enum']), |
|
| 352 | - array_shift($this->command['enum']), |
|
| 353 | - array_shift($this->command['enum']) |
|
| 354 | - ); |
|
| 355 | - } else { |
|
| 356 | - $enum = range(array_shift($this->command['enum']), array_shift($this->command['enum'])); |
|
| 357 | - } |
|
| 358 | - $this->tableau = $enum; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * extraire le chemin "query.results" du tableau de donnees |
|
| 364 | - * {datapath query.results} |
|
| 365 | - * |
|
| 366 | - **/ |
|
| 367 | - protected function select_datapath() { |
|
| 368 | - $base = reset($this->command['datapath']); |
|
| 369 | - if (strlen($base = ltrim(trim($base), '/'))) { |
|
| 370 | - $results = table_valeur($this->tableau, $base); |
|
| 371 | - if (is_array($results)) { |
|
| 372 | - $this->tableau = $results; |
|
| 373 | - } else { |
|
| 374 | - $this->tableau = []; |
|
| 375 | - $this->err = true; |
|
| 376 | - spip_log("datapath '$base' absent"); |
|
| 377 | - } |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Ordonner les resultats |
|
| 383 | - * {par x} |
|
| 384 | - * |
|
| 385 | - **/ |
|
| 386 | - protected function select_orderby() { |
|
| 387 | - $sortfunc = ''; |
|
| 388 | - $aleas = 0; |
|
| 389 | - foreach ($this->command['orderby'] as $tri) { |
|
| 390 | - // virer le / initial pour les criteres de la forme {par /xx} |
|
| 391 | - if (preg_match(',^\.?([/\w:_-]+)( DESC)?$,iS', ltrim($tri, '/'), $r)) { |
|
| 392 | - $r = array_pad($r, 3, null); |
|
| 393 | - |
|
| 394 | - // tri par cle |
|
| 395 | - if ($r[1] == 'cle') { |
|
| 396 | - if (isset($r[2]) and $r[2]) { |
|
| 397 | - krsort($this->tableau); |
|
| 398 | - } else { |
|
| 399 | - ksort($this->tableau); |
|
| 400 | - } |
|
| 401 | - } # {par hasard} |
|
| 402 | - else { |
|
| 403 | - if ($r[1] == 'hasard') { |
|
| 404 | - $k = array_keys($this->tableau); |
|
| 405 | - shuffle($k); |
|
| 406 | - $v = []; |
|
| 407 | - foreach ($k as $cle) { |
|
| 408 | - $v[$cle] = $this->tableau[$cle]; |
|
| 409 | - } |
|
| 410 | - $this->tableau = $v; |
|
| 411 | - } else { |
|
| 412 | - # {par valeur} |
|
| 413 | - if ($r[1] == 'valeur') { |
|
| 414 | - $tv = '%s'; |
|
| 415 | - } # {par valeur/xx/yy} ?? |
|
| 416 | - else { |
|
| 417 | - $tv = 'table_valeur(%s, ' . var_export($r[1], true) . ')'; |
|
| 418 | - } |
|
| 419 | - $sortfunc .= ' |
|
| 15 | + /** Tableau de données */ |
|
| 16 | + protected array $tableau = []; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Conditions de filtrage |
|
| 20 | + * ie criteres de selection |
|
| 21 | + */ |
|
| 22 | + protected array $filtre = []; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Cle courante |
|
| 26 | + * |
|
| 27 | + * @var scalar |
|
| 28 | + */ |
|
| 29 | + protected $cle = null; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Valeur courante |
|
| 33 | + * |
|
| 34 | + * @var mixed |
|
| 35 | + */ |
|
| 36 | + protected $valeur = null; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Constructeur |
|
| 40 | + * |
|
| 41 | + * @param $command |
|
| 42 | + * @param array $info |
|
| 43 | + */ |
|
| 44 | + public function __construct(array $command, array $info = []) { |
|
| 45 | + include_spip('iterateur/data'); |
|
| 46 | + $this->type = 'DATA'; |
|
| 47 | + $this->command = $command; |
|
| 48 | + $this->info = $info; |
|
| 49 | + $this->select($command); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Revenir au depart |
|
| 54 | + * |
|
| 55 | + * @return void |
|
| 56 | + */ |
|
| 57 | + public function rewind(): void { |
|
| 58 | + reset($this->tableau); |
|
| 59 | + $this->cle = array_key_first($this->tableau); |
|
| 60 | + $this->valeur = current($this->tableau); |
|
| 61 | + next($this->tableau); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Déclarer les critères exceptions |
|
| 66 | + * |
|
| 67 | + * @return array |
|
| 68 | + */ |
|
| 69 | + public function exception_des_criteres() { |
|
| 70 | + return ['tableau']; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Récupérer depuis le cache si possible |
|
| 75 | + * |
|
| 76 | + * @param string $cle |
|
| 77 | + * @return mixed |
|
| 78 | + */ |
|
| 79 | + protected function cache_get($cle) { |
|
| 80 | + if (!$cle) { |
|
| 81 | + return; |
|
| 82 | + } |
|
| 83 | + # utiliser memoization si dispo |
|
| 84 | + if (!function_exists('cache_get')) { |
|
| 85 | + return; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + return cache_get($cle); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Stocker en cache si possible |
|
| 93 | + * |
|
| 94 | + * @param string $cle |
|
| 95 | + * @param int $ttl |
|
| 96 | + * @param null|mixed $valeur |
|
| 97 | + * @return bool |
|
| 98 | + */ |
|
| 99 | + protected function cache_set($cle, $ttl, $valeur = null) { |
|
| 100 | + if (!$cle) { |
|
| 101 | + return; |
|
| 102 | + } |
|
| 103 | + if (is_null($valeur)) { |
|
| 104 | + $valeur = $this->tableau; |
|
| 105 | + } |
|
| 106 | + # utiliser memoization si dispo |
|
| 107 | + if (!function_exists('cache_set')) { |
|
| 108 | + return; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + return cache_set( |
|
| 112 | + $cle, |
|
| 113 | + [ |
|
| 114 | + 'data' => $valeur, |
|
| 115 | + 'time' => time(), |
|
| 116 | + 'ttl' => $ttl |
|
| 117 | + ], |
|
| 118 | + 3600 + $ttl |
|
| 119 | + ); |
|
| 120 | + # conserver le cache 1h de plus que la validite demandee, |
|
| 121 | + # pour le cas ou le serveur distant ne reponde plus |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Aller chercher les données de la boucle DATA |
|
| 126 | + * |
|
| 127 | + * @throws Exception |
|
| 128 | + * @param array $command |
|
| 129 | + * @return void |
|
| 130 | + */ |
|
| 131 | + protected function select($command) { |
|
| 132 | + |
|
| 133 | + // l'iterateur DATA peut etre appele en passant (data:type) |
|
| 134 | + // le type se retrouve dans la commande 'from' |
|
| 135 | + // dans ce cas la le critere {source}, si present, n'a pas besoin du 1er argument |
|
| 136 | + if (isset($this->command['from'][0])) { |
|
| 137 | + if (isset($this->command['source']) and is_array($this->command['source'])) { |
|
| 138 | + array_unshift($this->command['source'], $this->command['sourcemode']); |
|
| 139 | + } |
|
| 140 | + $this->command['sourcemode'] = $this->command['from'][0]; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // cherchons differents moyens de creer le tableau de donnees |
|
| 144 | + // les commandes connues pour l'iterateur DATA |
|
| 145 | + // sont : {tableau #ARRAY} ; {cle=...} ; {valeur=...} |
|
| 146 | + |
|
| 147 | + // {source format, [URL], [arg2]...} |
|
| 148 | + if ( |
|
| 149 | + isset($this->command['source']) |
|
| 150 | + and isset($this->command['sourcemode']) |
|
| 151 | + ) { |
|
| 152 | + $this->select_source(); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + // Critere {liste X1, X2, X3} |
|
| 156 | + if (isset($this->command['liste'])) { |
|
| 157 | + $this->select_liste(); |
|
| 158 | + } |
|
| 159 | + if (isset($this->command['enum'])) { |
|
| 160 | + $this->select_enum(); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // Si a ce stade on n'a pas de table, il y a un bug |
|
| 164 | + if (!is_array($this->tableau)) { |
|
| 165 | + $this->err = true; |
|
| 166 | + spip_log('erreur datasource ' . var_export($command, true)); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + // {datapath query.results} |
|
| 170 | + // extraire le chemin "query.results" du tableau de donnees |
|
| 171 | + if ( |
|
| 172 | + !$this->err |
|
| 173 | + and isset($this->command['datapath']) |
|
| 174 | + and is_array($this->command['datapath']) |
|
| 175 | + ) { |
|
| 176 | + $this->select_datapath(); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // tri {par x} |
|
| 180 | + if ($this->command['orderby']) { |
|
| 181 | + $this->select_orderby(); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + // grouper les resultats {fusion /x/y/z} ; |
|
| 185 | + if ($this->command['groupby']) { |
|
| 186 | + $this->select_groupby(); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $this->rewind(); |
|
| 190 | + #var_dump($this->tableau); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Aller chercher les donnees de la boucle DATA |
|
| 196 | + * depuis une source |
|
| 197 | + * {source format, [URL], [arg2]...} |
|
| 198 | + */ |
|
| 199 | + protected function select_source() { |
|
| 200 | + # un peu crado : avant de charger le cache il faut charger |
|
| 201 | + # les class indispensables, sinon PHP ne saura pas gerer |
|
| 202 | + # l'objet en cache ; cf plugins/icalendar |
|
| 203 | + # perf : pas de fonction table_to_array ! (table est deja un array) |
|
| 204 | + if ( |
|
| 205 | + isset($this->command['sourcemode']) |
|
| 206 | + and !in_array($this->command['sourcemode'], ['table', 'array', 'tableau']) |
|
| 207 | + ) { |
|
| 208 | + charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + # le premier argument peut etre un array, une URL etc. |
|
| 212 | + $src = $this->command['source'][0] ?? null; |
|
| 213 | + |
|
| 214 | + # avons-nous un cache dispo ? |
|
| 215 | + $cle = null; |
|
| 216 | + if (is_string($src)) { |
|
| 217 | + $cle = 'datasource_' . md5($this->command['sourcemode'] . ':' . var_export($this->command['source'], true)); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + $cache = $this->cache_get($cle); |
|
| 221 | + if (isset($this->command['datacache'])) { |
|
| 222 | + $ttl = intval($this->command['datacache']); |
|
| 223 | + } |
|
| 224 | + if ( |
|
| 225 | + $cache |
|
| 226 | + and ($cache['time'] + ($ttl ?? $cache['ttl']) |
|
| 227 | + > time()) |
|
| 228 | + and !(_request('var_mode') === 'recalcul' |
|
| 229 | + and include_spip('inc/autoriser') |
|
| 230 | + and autoriser('recalcul') |
|
| 231 | + ) |
|
| 232 | + ) { |
|
| 233 | + $this->tableau = $cache['data']; |
|
| 234 | + } else { |
|
| 235 | + try { |
|
| 236 | + if ( |
|
| 237 | + isset($this->command['sourcemode']) |
|
| 238 | + and in_array( |
|
| 239 | + $this->command['sourcemode'], |
|
| 240 | + ['table', 'array', 'tableau'] |
|
| 241 | + ) |
|
| 242 | + ) { |
|
| 243 | + if ( |
|
| 244 | + is_array($a = $src) |
|
| 245 | + or (is_string($a) |
|
| 246 | + and $a = str_replace('"', '"', $a) # fragile! |
|
| 247 | + and is_array($a = @unserialize($a))) |
|
| 248 | + ) { |
|
| 249 | + $this->tableau = $a; |
|
| 250 | + } |
|
| 251 | + } else { |
|
| 252 | + $data = $src; |
|
| 253 | + if (is_string($src)) { |
|
| 254 | + if (tester_url_absolue($src)) { |
|
| 255 | + include_spip('inc/distant'); |
|
| 256 | + $data = recuperer_url($src, ['taille_max' => _DATA_SOURCE_MAX_SIZE]); |
|
| 257 | + $data = $data['page'] ?? ''; |
|
| 258 | + if (!$data) { |
|
| 259 | + throw new Exception('404'); |
|
| 260 | + } |
|
| 261 | + if (!isset($ttl)) { |
|
| 262 | + $ttl = 24 * 3600; |
|
| 263 | + } |
|
| 264 | + } elseif (@is_dir($src)) { |
|
| 265 | + $data = $src; |
|
| 266 | + } elseif (@is_readable($src) && @is_file($src)) { |
|
| 267 | + $data = spip_file_get_contents($src); |
|
| 268 | + } |
|
| 269 | + if (!isset($ttl)) { |
|
| 270 | + $ttl = 10; |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + if ( |
|
| 274 | + !$this->err |
|
| 275 | + and $data_to_array = charger_fonction($this->command['sourcemode'] . '_to_array', 'inc', true) |
|
| 276 | + ) { |
|
| 277 | + $args = $this->command['source']; |
|
| 278 | + $args[0] = $data; |
|
| 279 | + if (is_array($a = $data_to_array(...$args))) { |
|
| 280 | + $this->tableau = $a; |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + if (!is_array($this->tableau)) { |
|
| 286 | + $this->err = true; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + if (!$this->err and isset($ttl) and $ttl > 0) { |
|
| 290 | + $this->cache_set($cle, $ttl); |
|
| 291 | + } |
|
| 292 | + } catch (Exception $e) { |
|
| 293 | + $e = $e->getMessage(); |
|
| 294 | + $err = sprintf( |
|
| 295 | + "[%s, %s] $e", |
|
| 296 | + $src, |
|
| 297 | + $this->command['sourcemode'] |
|
| 298 | + ); |
|
| 299 | + erreur_squelette([$err, []]); |
|
| 300 | + $this->err = true; |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + # en cas d'erreur, utiliser le cache si encore dispo |
|
| 305 | + if ( |
|
| 306 | + $this->err |
|
| 307 | + and $cache |
|
| 308 | + ) { |
|
| 309 | + $this->tableau = $cache['data']; |
|
| 310 | + $this->err = false; |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Retourne un tableau donne depuis un critère liste |
|
| 317 | + * |
|
| 318 | + * Critère `{liste X1, X2, X3}` |
|
| 319 | + * |
|
| 320 | + * @see critere_DATA_liste_dist() |
|
| 321 | + * |
|
| 322 | + **/ |
|
| 323 | + protected function select_liste() { |
|
| 324 | + # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE |
|
| 325 | + if (!isset($this->command['liste'][1])) { |
|
| 326 | + if (!is_array($this->command['liste'][0])) { |
|
| 327 | + $this->command['liste'] = explode(',', $this->command['liste'][0]); |
|
| 328 | + } else { |
|
| 329 | + $this->command['liste'] = $this->command['liste'][0]; |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + $this->tableau = $this->command['liste']; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Retourne un tableau donne depuis un critere liste |
|
| 337 | + * Critere {enum Xmin, Xmax} |
|
| 338 | + * |
|
| 339 | + **/ |
|
| 340 | + protected function select_enum() { |
|
| 341 | + # s'il n'y a qu'une valeur dans la liste, sans doute une #BALISE |
|
| 342 | + if (!isset($this->command['enum'][1])) { |
|
| 343 | + if (!is_array($this->command['enum'][0])) { |
|
| 344 | + $this->command['enum'] = explode(',', $this->command['enum'][0]); |
|
| 345 | + } else { |
|
| 346 | + $this->command['enum'] = $this->command['enum'][0]; |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | + if ((is_countable($this->command['enum']) ? count($this->command['enum']) : 0) >= 3) { |
|
| 350 | + $enum = range( |
|
| 351 | + array_shift($this->command['enum']), |
|
| 352 | + array_shift($this->command['enum']), |
|
| 353 | + array_shift($this->command['enum']) |
|
| 354 | + ); |
|
| 355 | + } else { |
|
| 356 | + $enum = range(array_shift($this->command['enum']), array_shift($this->command['enum'])); |
|
| 357 | + } |
|
| 358 | + $this->tableau = $enum; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * extraire le chemin "query.results" du tableau de donnees |
|
| 364 | + * {datapath query.results} |
|
| 365 | + * |
|
| 366 | + **/ |
|
| 367 | + protected function select_datapath() { |
|
| 368 | + $base = reset($this->command['datapath']); |
|
| 369 | + if (strlen($base = ltrim(trim($base), '/'))) { |
|
| 370 | + $results = table_valeur($this->tableau, $base); |
|
| 371 | + if (is_array($results)) { |
|
| 372 | + $this->tableau = $results; |
|
| 373 | + } else { |
|
| 374 | + $this->tableau = []; |
|
| 375 | + $this->err = true; |
|
| 376 | + spip_log("datapath '$base' absent"); |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Ordonner les resultats |
|
| 383 | + * {par x} |
|
| 384 | + * |
|
| 385 | + **/ |
|
| 386 | + protected function select_orderby() { |
|
| 387 | + $sortfunc = ''; |
|
| 388 | + $aleas = 0; |
|
| 389 | + foreach ($this->command['orderby'] as $tri) { |
|
| 390 | + // virer le / initial pour les criteres de la forme {par /xx} |
|
| 391 | + if (preg_match(',^\.?([/\w:_-]+)( DESC)?$,iS', ltrim($tri, '/'), $r)) { |
|
| 392 | + $r = array_pad($r, 3, null); |
|
| 393 | + |
|
| 394 | + // tri par cle |
|
| 395 | + if ($r[1] == 'cle') { |
|
| 396 | + if (isset($r[2]) and $r[2]) { |
|
| 397 | + krsort($this->tableau); |
|
| 398 | + } else { |
|
| 399 | + ksort($this->tableau); |
|
| 400 | + } |
|
| 401 | + } # {par hasard} |
|
| 402 | + else { |
|
| 403 | + if ($r[1] == 'hasard') { |
|
| 404 | + $k = array_keys($this->tableau); |
|
| 405 | + shuffle($k); |
|
| 406 | + $v = []; |
|
| 407 | + foreach ($k as $cle) { |
|
| 408 | + $v[$cle] = $this->tableau[$cle]; |
|
| 409 | + } |
|
| 410 | + $this->tableau = $v; |
|
| 411 | + } else { |
|
| 412 | + # {par valeur} |
|
| 413 | + if ($r[1] == 'valeur') { |
|
| 414 | + $tv = '%s'; |
|
| 415 | + } # {par valeur/xx/yy} ?? |
|
| 416 | + else { |
|
| 417 | + $tv = 'table_valeur(%s, ' . var_export($r[1], true) . ')'; |
|
| 418 | + } |
|
| 419 | + $sortfunc .= ' |
|
| 420 | 420 | $a = ' . sprintf($tv, '$aa') . '; |
| 421 | 421 | $b = ' . sprintf($tv, '$bb') . '; |
| 422 | 422 | if ($a <> $b) |
| 423 | 423 | return ($a ' . (!empty($r[2]) ? '>' : '<') . ' $b) ? -1 : 1;'; |
| 424 | - } |
|
| 425 | - } |
|
| 426 | - } |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - if ($sortfunc) { |
|
| 430 | - $sortfunc .= "\n return 0;"; |
|
| 431 | - uasort($this->tableau, fn($aa, $bb) => eval($sortfunc)); |
|
| 432 | - } |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * Grouper les resultats |
|
| 438 | - * {fusion /x/y/z} |
|
| 439 | - * |
|
| 440 | - **/ |
|
| 441 | - protected function select_groupby() { |
|
| 442 | - // virer le / initial pour les criteres de la forme {fusion /xx} |
|
| 443 | - if (strlen($fusion = ltrim($this->command['groupby'][0], '/'))) { |
|
| 444 | - $vu = []; |
|
| 445 | - foreach ($this->tableau as $k => $v) { |
|
| 446 | - $val = table_valeur($v, $fusion); |
|
| 447 | - if (isset($vu[$val])) { |
|
| 448 | - unset($this->tableau[$k]); |
|
| 449 | - } else { |
|
| 450 | - $vu[$val] = true; |
|
| 451 | - } |
|
| 452 | - } |
|
| 453 | - } |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * L'iterateur est-il encore valide ? |
|
| 459 | - * |
|
| 460 | - * @return bool |
|
| 461 | - */ |
|
| 462 | - public function valid(): bool { |
|
| 463 | - return !is_null($this->cle); |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * Retourner la valeur |
|
| 468 | - * |
|
| 469 | - * @return mixed |
|
| 470 | - */ |
|
| 471 | - #[\ReturnTypeWillChange] |
|
| 472 | - public function current() { |
|
| 473 | - return $this->valeur; |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * Retourner la cle |
|
| 478 | - * |
|
| 479 | - * @return mixed |
|
| 480 | - */ |
|
| 481 | - #[\ReturnTypeWillChange] |
|
| 482 | - public function key() { |
|
| 483 | - return $this->cle; |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * Passer a la valeur suivante |
|
| 488 | - * |
|
| 489 | - * @return void |
|
| 490 | - */ |
|
| 491 | - public function next(): void { |
|
| 492 | - if ($this->valid()) { |
|
| 493 | - $this->cle = key($this->tableau); |
|
| 494 | - $this->valeur = current($this->tableau); |
|
| 495 | - next($this->tableau); |
|
| 496 | - } |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - /** |
|
| 500 | - * Compter le nombre total de resultats |
|
| 501 | - * |
|
| 502 | - * @return int |
|
| 503 | - */ |
|
| 504 | - public function count() { |
|
| 505 | - if (is_null($this->total)) { |
|
| 506 | - $this->total = count($this->tableau); |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - return $this->total; |
|
| 510 | - } |
|
| 424 | + } |
|
| 425 | + } |
|
| 426 | + } |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + if ($sortfunc) { |
|
| 430 | + $sortfunc .= "\n return 0;"; |
|
| 431 | + uasort($this->tableau, fn($aa, $bb) => eval($sortfunc)); |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * Grouper les resultats |
|
| 438 | + * {fusion /x/y/z} |
|
| 439 | + * |
|
| 440 | + **/ |
|
| 441 | + protected function select_groupby() { |
|
| 442 | + // virer le / initial pour les criteres de la forme {fusion /xx} |
|
| 443 | + if (strlen($fusion = ltrim($this->command['groupby'][0], '/'))) { |
|
| 444 | + $vu = []; |
|
| 445 | + foreach ($this->tableau as $k => $v) { |
|
| 446 | + $val = table_valeur($v, $fusion); |
|
| 447 | + if (isset($vu[$val])) { |
|
| 448 | + unset($this->tableau[$k]); |
|
| 449 | + } else { |
|
| 450 | + $vu[$val] = true; |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | + } |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * L'iterateur est-il encore valide ? |
|
| 459 | + * |
|
| 460 | + * @return bool |
|
| 461 | + */ |
|
| 462 | + public function valid(): bool { |
|
| 463 | + return !is_null($this->cle); |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * Retourner la valeur |
|
| 468 | + * |
|
| 469 | + * @return mixed |
|
| 470 | + */ |
|
| 471 | + #[\ReturnTypeWillChange] |
|
| 472 | + public function current() { |
|
| 473 | + return $this->valeur; |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * Retourner la cle |
|
| 478 | + * |
|
| 479 | + * @return mixed |
|
| 480 | + */ |
|
| 481 | + #[\ReturnTypeWillChange] |
|
| 482 | + public function key() { |
|
| 483 | + return $this->cle; |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * Passer a la valeur suivante |
|
| 488 | + * |
|
| 489 | + * @return void |
|
| 490 | + */ |
|
| 491 | + public function next(): void { |
|
| 492 | + if ($this->valid()) { |
|
| 493 | + $this->cle = key($this->tableau); |
|
| 494 | + $this->valeur = current($this->tableau); |
|
| 495 | + next($this->tableau); |
|
| 496 | + } |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + /** |
|
| 500 | + * Compter le nombre total de resultats |
|
| 501 | + * |
|
| 502 | + * @return int |
|
| 503 | + */ |
|
| 504 | + public function count() { |
|
| 505 | + if (is_null($this->total)) { |
|
| 506 | + $this->total = count($this->tableau); |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + return $this->total; |
|
| 510 | + } |
|
| 511 | 511 | } |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 18 | 18 | return; |
| 19 | 19 | } |
| 20 | -require_once _ROOT_RESTREINT . 'base/objets.php'; |
|
| 20 | +require_once _ROOT_RESTREINT.'base/objets.php'; |
|
| 21 | 21 | |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | // serveur externe et nom de serveur bien ecrit ? |
| 61 | 61 | if (defined('_DIR_CONNECT') |
| 62 | 62 | and preg_match('/^[\w\.]*$/', $serveur)) { |
| 63 | - $f = _DIR_CONNECT . $serveur . '.php'; |
|
| 63 | + $f = _DIR_CONNECT.$serveur.'.php'; |
|
| 64 | 64 | if (!is_readable($f) and !$install) { |
| 65 | 65 | // chercher une declaration de serveur dans le path |
| 66 | 66 | // qui peut servir à des plugins à declarer des connexions à une base sqlite |
@@ -110,9 +110,9 @@ discard block |
||
| 110 | 110 | // chargement de la version du jeu de fonctions |
| 111 | 111 | // si pas dans le fichier par defaut |
| 112 | 112 | $type = $GLOBALS['db_ok']['type']; |
| 113 | - $jeu = 'spip_' . $type . '_functions_' . $version; |
|
| 113 | + $jeu = 'spip_'.$type.'_functions_'.$version; |
|
| 114 | 114 | if (!isset($GLOBALS[$jeu])) { |
| 115 | - if (!find_in_path($type . '_' . $version . '.php', 'req/', true)) { |
|
| 115 | + if (!find_in_path($type.'_'.$version.'.php', 'req/', true)) { |
|
| 116 | 116 | spip_log("spip_connect: serveur $index version '$version' non defini pour '$type'", _LOG_HS); |
| 117 | 117 | |
| 118 | 118 | // ne plus reessayer |
@@ -174,9 +174,9 @@ discard block |
||
| 174 | 174 | $connexion = spip_connect($serveur); |
| 175 | 175 | $e = sql_errno($serveur); |
| 176 | 176 | $t = ($connexion['type'] ?? 'sql'); |
| 177 | - $m = "Erreur $e de $t: " . sql_error($serveur) . "\nin " . sql_error_backtrace() . "\n" . trim($connexion['last']); |
|
| 178 | - $f = $t . $serveur; |
|
| 179 | - spip_log($m, $f . '.' . _LOG_ERREUR); |
|
| 177 | + $m = "Erreur $e de $t: ".sql_error($serveur)."\nin ".sql_error_backtrace()."\n".trim($connexion['last']); |
|
| 178 | + $f = $t.$serveur; |
|
| 179 | + spip_log($m, $f.'.'._LOG_ERREUR); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | /** |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | // si en cours d'installation ou si db=@test@ on ne pose rien |
| 263 | 263 | // car c'est un test de connexion |
| 264 | 264 | if (!defined('_ECRIRE_INSTALL') and $db !== '@test@') { |
| 265 | - $f = _DIR_TMP . $type . '.' . substr(md5($host . $port . $db), 0, 8) . '.out'; |
|
| 265 | + $f = _DIR_TMP.$type.'.'.substr(md5($host.$port.$db), 0, 8).'.out'; |
|
| 266 | 266 | } elseif ($db == '@test@') { |
| 267 | 267 | $db = ''; |
| 268 | 268 | } |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | // En cas d'indisponibilite du serveur, eviter de le bombarder |
| 302 | 302 | if ($f) { |
| 303 | 303 | @touch($f); |
| 304 | - spip_log("Echec connexion serveur $type : host[$host] port[$port] login[$login] base[$db]", $type . '.' . _LOG_HS); |
|
| 304 | + spip_log("Echec connexion serveur $type : host[$host] port[$port] login[$login] base[$db]", $type.'.'._LOG_HS); |
|
| 305 | 305 | } |
| 306 | 306 | return null; |
| 307 | 307 | } |
@@ -398,11 +398,11 @@ discard block |
||
| 398 | 398 | } elseif (is_array($a)) { |
| 399 | 399 | return join(',', array_map('_q', $a)); |
| 400 | 400 | } elseif (is_scalar($a)) { |
| 401 | - return ("'" . addslashes($a) . "'"); |
|
| 401 | + return ("'".addslashes($a)."'"); |
|
| 402 | 402 | } elseif ($a === null) { |
| 403 | 403 | return "''"; |
| 404 | 404 | } |
| 405 | - throw new \RuntimeException('Can’t use _q with ' . gettype($a)); |
|
| 405 | + throw new \RuntimeException('Can’t use _q with '.gettype($a)); |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | /** |
@@ -453,7 +453,7 @@ discard block |
||
| 453 | 453 | $next = reset($textes); |
| 454 | 454 | if ( |
| 455 | 455 | strpos($next, "'") === 0 |
| 456 | - and strpos($query_echappees, $part . $next, $currentpos) === $nextpos |
|
| 456 | + and strpos($query_echappees, $part.$next, $currentpos) === $nextpos |
|
| 457 | 457 | ) { |
| 458 | 458 | $part .= array_shift($textes); |
| 459 | 459 | } |
@@ -465,7 +465,7 @@ discard block |
||
| 465 | 465 | $parts[$k] = [ |
| 466 | 466 | 'texte' => $part, |
| 467 | 467 | 'position' => $nextpos, |
| 468 | - 'placeholder' => '%' . $k . '$s', |
|
| 468 | + 'placeholder' => '%'.$k.'$s', |
|
| 469 | 469 | ]; |
| 470 | 470 | $currentpos = $nextpos + strlen($part); |
| 471 | 471 | } |
@@ -68,13 +68,11 @@ discard block |
||
| 68 | 68 | $f = find_in_path("$serveur.php", 'connect/'); |
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | - } |
|
| 72 | - else { |
|
| 71 | + } else { |
|
| 73 | 72 | if (defined('_FILE_CONNECT') and _FILE_CONNECT) { |
| 74 | 73 | // init du serveur principal |
| 75 | 74 | $f = _FILE_CONNECT; |
| 76 | - } |
|
| 77 | - elseif ($install and defined('_FILE_CONNECT_TMP')) { |
|
| 75 | + } elseif ($install and defined('_FILE_CONNECT_TMP')) { |
|
| 78 | 76 | // installation en cours |
| 79 | 77 | $f = _FILE_CONNECT_TMP; |
| 80 | 78 | } |
@@ -87,8 +85,7 @@ discard block |
||
| 87 | 85 | if (!isset($GLOBALS['db_ok'])) { |
| 88 | 86 | spip_log("spip_connect: fichier de connexion '$f' OK mais echec connexion au serveur", _LOG_HS); |
| 89 | 87 | } |
| 90 | - } |
|
| 91 | - else { |
|
| 88 | + } else { |
|
| 92 | 89 | spip_log("spip_connect: fichier de connexion '$f' non trouve, pas de connexion serveur", _LOG_HS); |
| 93 | 90 | } |
| 94 | 91 | if (!isset($GLOBALS['db_ok'])) { |
@@ -456,8 +453,7 @@ discard block |
||
| 456 | 453 | and strpos($query_echappees, $part . $next, $currentpos) === $nextpos |
| 457 | 454 | ) { |
| 458 | 455 | $part .= array_shift($textes); |
| 459 | - } |
|
| 460 | - else { |
|
| 456 | + } else { |
|
| 461 | 457 | break; |
| 462 | 458 | } |
| 463 | 459 | } |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | * @package SPIP\Core\SQL |
| 16 | 16 | **/ |
| 17 | 17 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 18 | - return; |
|
| 18 | + return; |
|
| 19 | 19 | } |
| 20 | 20 | require_once _ROOT_RESTREINT . 'base/objets.php'; |
| 21 | 21 | |
@@ -41,128 +41,128 @@ discard block |
||
| 41 | 41 | **/ |
| 42 | 42 | function spip_connect($serveur = '', $version = '') { |
| 43 | 43 | |
| 44 | - $serveur = !is_string($serveur) ? '' : strtolower($serveur); |
|
| 45 | - $index = $serveur ?: 0; |
|
| 46 | - if (!$version) { |
|
| 47 | - $version = $GLOBALS['spip_sql_version']; |
|
| 48 | - } |
|
| 49 | - if (isset($GLOBALS['connexions'][$index][$version])) { |
|
| 50 | - return $GLOBALS['connexions'][$index]; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - include_spip('base/abstract_sql'); |
|
| 54 | - $install = (_request('exec') == 'install'); |
|
| 55 | - |
|
| 56 | - // Premiere connexion ? |
|
| 57 | - if (!($old = isset($GLOBALS['connexions'][$index]))) { |
|
| 58 | - $f = ''; |
|
| 59 | - if ($serveur) { |
|
| 60 | - // serveur externe et nom de serveur bien ecrit ? |
|
| 61 | - if (defined('_DIR_CONNECT') |
|
| 62 | - and preg_match('/^[\w\.]*$/', $serveur)) { |
|
| 63 | - $f = _DIR_CONNECT . $serveur . '.php'; |
|
| 64 | - if (!is_readable($f) and !$install) { |
|
| 65 | - // chercher une declaration de serveur dans le path |
|
| 66 | - // qui peut servir à des plugins à declarer des connexions à une base sqlite |
|
| 67 | - // Ex: sert aux boucles POUR et au plugin-dist dump pour se connecter sur le sqlite du dump |
|
| 68 | - $f = find_in_path("$serveur.php", 'connect/'); |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - else { |
|
| 73 | - if (defined('_FILE_CONNECT') and _FILE_CONNECT) { |
|
| 74 | - // init du serveur principal |
|
| 75 | - $f = _FILE_CONNECT; |
|
| 76 | - } |
|
| 77 | - elseif ($install and defined('_FILE_CONNECT_TMP')) { |
|
| 78 | - // installation en cours |
|
| 79 | - $f = _FILE_CONNECT_TMP; |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - unset($GLOBALS['db_ok']); |
|
| 84 | - unset($GLOBALS['spip_connect_version']); |
|
| 85 | - if ($f and is_readable($f)) { |
|
| 86 | - include($f); |
|
| 87 | - if (!isset($GLOBALS['db_ok'])) { |
|
| 88 | - spip_log("spip_connect: fichier de connexion '$f' OK mais echec connexion au serveur", _LOG_HS); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - else { |
|
| 92 | - spip_log("spip_connect: fichier de connexion '$f' non trouve, pas de connexion serveur", _LOG_HS); |
|
| 93 | - } |
|
| 94 | - if (!isset($GLOBALS['db_ok'])) { |
|
| 95 | - // fera mieux la prochaine fois |
|
| 96 | - if ($install) { |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 99 | - // ne plus reessayer si ce n'est pas l'install |
|
| 100 | - return $GLOBALS['connexions'][$index] = false; |
|
| 101 | - } |
|
| 102 | - $GLOBALS['connexions'][$index] = $GLOBALS['db_ok']; |
|
| 103 | - } |
|
| 104 | - // si la connexion a deja ete tentee mais a echoue, le dire! |
|
| 105 | - if (!$GLOBALS['connexions'][$index]) { |
|
| 106 | - return false; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // la connexion a reussi ou etait deja faite. |
|
| 110 | - // chargement de la version du jeu de fonctions |
|
| 111 | - // si pas dans le fichier par defaut |
|
| 112 | - $type = $GLOBALS['db_ok']['type']; |
|
| 113 | - $jeu = 'spip_' . $type . '_functions_' . $version; |
|
| 114 | - if (!isset($GLOBALS[$jeu])) { |
|
| 115 | - if (!find_in_path($type . '_' . $version . '.php', 'req/', true)) { |
|
| 116 | - spip_log("spip_connect: serveur $index version '$version' non defini pour '$type'", _LOG_HS); |
|
| 117 | - |
|
| 118 | - // ne plus reessayer |
|
| 119 | - return $GLOBALS['connexions'][$index][$version] = []; |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - $GLOBALS['connexions'][$index][$version] = $GLOBALS[$jeu]; |
|
| 123 | - if ($old) { |
|
| 124 | - return $GLOBALS['connexions'][$index]; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $GLOBALS['connexions'][$index]['spip_connect_version'] = $GLOBALS['spip_connect_version'] ?? 0; |
|
| 128 | - |
|
| 129 | - // initialisation de l'alphabet utilise dans les connexions SQL |
|
| 130 | - // si l'installation l'a determine. |
|
| 131 | - // Celui du serveur principal l'impose aux serveurs secondaires |
|
| 132 | - // s'ils le connaissent |
|
| 133 | - |
|
| 134 | - if (!$serveur) { |
|
| 135 | - $charset = spip_connect_main($GLOBALS[$jeu], $GLOBALS['db_ok']['charset']); |
|
| 136 | - if (!$charset) { |
|
| 137 | - unset($GLOBALS['connexions'][$index]); |
|
| 138 | - spip_log('spip_connect: absence de charset', _LOG_AVERTISSEMENT); |
|
| 139 | - |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - } else { |
|
| 143 | - if ($GLOBALS['db_ok']['charset']) { |
|
| 144 | - $charset = $GLOBALS['db_ok']['charset']; |
|
| 145 | - } |
|
| 146 | - // spip_meta n'existe pas toujours dans la base |
|
| 147 | - // C'est le cas d'un dump sqlite par exemple |
|
| 148 | - elseif ( |
|
| 149 | - $GLOBALS['connexions'][$index]['spip_connect_version'] |
|
| 150 | - and sql_showtable('spip_meta', true, $serveur) |
|
| 151 | - and $r = sql_getfetsel('valeur', 'spip_meta', "nom='charset_sql_connexion'", '', '', '', '', $serveur) |
|
| 152 | - ) { |
|
| 153 | - $charset = $r; |
|
| 154 | - } else { |
|
| 155 | - $charset = -1; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - if ($charset != -1) { |
|
| 159 | - $f = $GLOBALS[$jeu]['set_charset']; |
|
| 160 | - if (function_exists($f)) { |
|
| 161 | - $f($charset, $serveur); |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - return $GLOBALS['connexions'][$index]; |
|
| 44 | + $serveur = !is_string($serveur) ? '' : strtolower($serveur); |
|
| 45 | + $index = $serveur ?: 0; |
|
| 46 | + if (!$version) { |
|
| 47 | + $version = $GLOBALS['spip_sql_version']; |
|
| 48 | + } |
|
| 49 | + if (isset($GLOBALS['connexions'][$index][$version])) { |
|
| 50 | + return $GLOBALS['connexions'][$index]; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + include_spip('base/abstract_sql'); |
|
| 54 | + $install = (_request('exec') == 'install'); |
|
| 55 | + |
|
| 56 | + // Premiere connexion ? |
|
| 57 | + if (!($old = isset($GLOBALS['connexions'][$index]))) { |
|
| 58 | + $f = ''; |
|
| 59 | + if ($serveur) { |
|
| 60 | + // serveur externe et nom de serveur bien ecrit ? |
|
| 61 | + if (defined('_DIR_CONNECT') |
|
| 62 | + and preg_match('/^[\w\.]*$/', $serveur)) { |
|
| 63 | + $f = _DIR_CONNECT . $serveur . '.php'; |
|
| 64 | + if (!is_readable($f) and !$install) { |
|
| 65 | + // chercher une declaration de serveur dans le path |
|
| 66 | + // qui peut servir à des plugins à declarer des connexions à une base sqlite |
|
| 67 | + // Ex: sert aux boucles POUR et au plugin-dist dump pour se connecter sur le sqlite du dump |
|
| 68 | + $f = find_in_path("$serveur.php", 'connect/'); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + else { |
|
| 73 | + if (defined('_FILE_CONNECT') and _FILE_CONNECT) { |
|
| 74 | + // init du serveur principal |
|
| 75 | + $f = _FILE_CONNECT; |
|
| 76 | + } |
|
| 77 | + elseif ($install and defined('_FILE_CONNECT_TMP')) { |
|
| 78 | + // installation en cours |
|
| 79 | + $f = _FILE_CONNECT_TMP; |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + unset($GLOBALS['db_ok']); |
|
| 84 | + unset($GLOBALS['spip_connect_version']); |
|
| 85 | + if ($f and is_readable($f)) { |
|
| 86 | + include($f); |
|
| 87 | + if (!isset($GLOBALS['db_ok'])) { |
|
| 88 | + spip_log("spip_connect: fichier de connexion '$f' OK mais echec connexion au serveur", _LOG_HS); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + else { |
|
| 92 | + spip_log("spip_connect: fichier de connexion '$f' non trouve, pas de connexion serveur", _LOG_HS); |
|
| 93 | + } |
|
| 94 | + if (!isset($GLOBALS['db_ok'])) { |
|
| 95 | + // fera mieux la prochaine fois |
|
| 96 | + if ($install) { |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | + // ne plus reessayer si ce n'est pas l'install |
|
| 100 | + return $GLOBALS['connexions'][$index] = false; |
|
| 101 | + } |
|
| 102 | + $GLOBALS['connexions'][$index] = $GLOBALS['db_ok']; |
|
| 103 | + } |
|
| 104 | + // si la connexion a deja ete tentee mais a echoue, le dire! |
|
| 105 | + if (!$GLOBALS['connexions'][$index]) { |
|
| 106 | + return false; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // la connexion a reussi ou etait deja faite. |
|
| 110 | + // chargement de la version du jeu de fonctions |
|
| 111 | + // si pas dans le fichier par defaut |
|
| 112 | + $type = $GLOBALS['db_ok']['type']; |
|
| 113 | + $jeu = 'spip_' . $type . '_functions_' . $version; |
|
| 114 | + if (!isset($GLOBALS[$jeu])) { |
|
| 115 | + if (!find_in_path($type . '_' . $version . '.php', 'req/', true)) { |
|
| 116 | + spip_log("spip_connect: serveur $index version '$version' non defini pour '$type'", _LOG_HS); |
|
| 117 | + |
|
| 118 | + // ne plus reessayer |
|
| 119 | + return $GLOBALS['connexions'][$index][$version] = []; |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + $GLOBALS['connexions'][$index][$version] = $GLOBALS[$jeu]; |
|
| 123 | + if ($old) { |
|
| 124 | + return $GLOBALS['connexions'][$index]; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $GLOBALS['connexions'][$index]['spip_connect_version'] = $GLOBALS['spip_connect_version'] ?? 0; |
|
| 128 | + |
|
| 129 | + // initialisation de l'alphabet utilise dans les connexions SQL |
|
| 130 | + // si l'installation l'a determine. |
|
| 131 | + // Celui du serveur principal l'impose aux serveurs secondaires |
|
| 132 | + // s'ils le connaissent |
|
| 133 | + |
|
| 134 | + if (!$serveur) { |
|
| 135 | + $charset = spip_connect_main($GLOBALS[$jeu], $GLOBALS['db_ok']['charset']); |
|
| 136 | + if (!$charset) { |
|
| 137 | + unset($GLOBALS['connexions'][$index]); |
|
| 138 | + spip_log('spip_connect: absence de charset', _LOG_AVERTISSEMENT); |
|
| 139 | + |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + } else { |
|
| 143 | + if ($GLOBALS['db_ok']['charset']) { |
|
| 144 | + $charset = $GLOBALS['db_ok']['charset']; |
|
| 145 | + } |
|
| 146 | + // spip_meta n'existe pas toujours dans la base |
|
| 147 | + // C'est le cas d'un dump sqlite par exemple |
|
| 148 | + elseif ( |
|
| 149 | + $GLOBALS['connexions'][$index]['spip_connect_version'] |
|
| 150 | + and sql_showtable('spip_meta', true, $serveur) |
|
| 151 | + and $r = sql_getfetsel('valeur', 'spip_meta', "nom='charset_sql_connexion'", '', '', '', '', $serveur) |
|
| 152 | + ) { |
|
| 153 | + $charset = $r; |
|
| 154 | + } else { |
|
| 155 | + $charset = -1; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + if ($charset != -1) { |
|
| 159 | + $f = $GLOBALS[$jeu]['set_charset']; |
|
| 160 | + if (function_exists($f)) { |
|
| 161 | + $f($charset, $serveur); |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + return $GLOBALS['connexions'][$index]; |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
@@ -171,12 +171,12 @@ discard block |
||
| 171 | 171 | * @param string $serveur Nom du connecteur de bdd utilisé |
| 172 | 172 | **/ |
| 173 | 173 | function spip_sql_erreur($serveur = '') { |
| 174 | - $connexion = spip_connect($serveur); |
|
| 175 | - $e = sql_errno($serveur); |
|
| 176 | - $t = ($connexion['type'] ?? 'sql'); |
|
| 177 | - $m = "Erreur $e de $t: " . sql_error($serveur) . "\nin " . sql_error_backtrace() . "\n" . trim($connexion['last']); |
|
| 178 | - $f = $t . $serveur; |
|
| 179 | - spip_log($m, $f . '.' . _LOG_ERREUR); |
|
| 174 | + $connexion = spip_connect($serveur); |
|
| 175 | + $e = sql_errno($serveur); |
|
| 176 | + $t = ($connexion['type'] ?? 'sql'); |
|
| 177 | + $m = "Erreur $e de $t: " . sql_error($serveur) . "\nin " . sql_error_backtrace() . "\n" . trim($connexion['last']); |
|
| 178 | + $f = $t . $serveur; |
|
| 179 | + spip_log($m, $f . '.' . _LOG_ERREUR); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | /** |
@@ -198,23 +198,23 @@ discard block |
||
| 198 | 198 | * - array : description de la connexion, si l'instruction sql est indisponible pour cette connexion |
| 199 | 199 | **/ |
| 200 | 200 | function spip_connect_sql($version, $ins = '', $serveur = '', $continue = false) { |
| 201 | - $desc = spip_connect($serveur, $version); |
|
| 202 | - if ( |
|
| 203 | - $desc |
|
| 204 | - and $f = ($desc[$version][$ins] ?? '') |
|
| 205 | - and function_exists($f) |
|
| 206 | - ) { |
|
| 207 | - return $f; |
|
| 208 | - } |
|
| 209 | - if ($continue) { |
|
| 210 | - return $desc; |
|
| 211 | - } |
|
| 212 | - if ($ins) { |
|
| 213 | - spip_log("Le serveur '$serveur' version $version n'a pas '$ins'", _LOG_ERREUR); |
|
| 214 | - } |
|
| 215 | - include_spip('inc/minipres'); |
|
| 216 | - echo minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), ['status' => 503]); |
|
| 217 | - exit; |
|
| 201 | + $desc = spip_connect($serveur, $version); |
|
| 202 | + if ( |
|
| 203 | + $desc |
|
| 204 | + and $f = ($desc[$version][$ins] ?? '') |
|
| 205 | + and function_exists($f) |
|
| 206 | + ) { |
|
| 207 | + return $f; |
|
| 208 | + } |
|
| 209 | + if ($continue) { |
|
| 210 | + return $desc; |
|
| 211 | + } |
|
| 212 | + if ($ins) { |
|
| 213 | + spip_log("Le serveur '$serveur' version $version n'a pas '$ins'", _LOG_ERREUR); |
|
| 214 | + } |
|
| 215 | + include_spip('inc/minipres'); |
|
| 216 | + echo minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), ['status' => 503]); |
|
| 217 | + exit; |
|
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | /** |
@@ -240,70 +240,70 @@ discard block |
||
| 240 | 240 | * @return array|null Description de la connexion |
| 241 | 241 | */ |
| 242 | 242 | function spip_connect_db( |
| 243 | - $host, |
|
| 244 | - $port, |
|
| 245 | - $login, |
|
| 246 | - #[\SensitiveParameter] $pass, |
|
| 247 | - $db = '', |
|
| 248 | - $type = 'mysql', |
|
| 249 | - $prefixe = '', |
|
| 250 | - $auth = '', |
|
| 251 | - $charset = '' |
|
| 243 | + $host, |
|
| 244 | + $port, |
|
| 245 | + $login, |
|
| 246 | + #[\SensitiveParameter] $pass, |
|
| 247 | + $db = '', |
|
| 248 | + $type = 'mysql', |
|
| 249 | + $prefixe = '', |
|
| 250 | + $auth = '', |
|
| 251 | + $charset = '' |
|
| 252 | 252 | ) { |
| 253 | - // temps avant nouvelle tentative de connexion |
|
| 254 | - // suite a une connection echouee |
|
| 255 | - if (!defined('_CONNECT_RETRY_DELAY')) { |
|
| 256 | - define('_CONNECT_RETRY_DELAY', 30); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - $f = ''; |
|
| 260 | - // un fichier de identifiant par combinaison (type,host,port,db) |
|
| 261 | - // pour ne pas declarer tout indisponible d'un coup |
|
| 262 | - // si en cours d'installation ou si db=@test@ on ne pose rien |
|
| 263 | - // car c'est un test de connexion |
|
| 264 | - if (!defined('_ECRIRE_INSTALL') and $db !== '@test@') { |
|
| 265 | - $f = _DIR_TMP . $type . '.' . substr(md5($host . $port . $db), 0, 8) . '.out'; |
|
| 266 | - } elseif ($db == '@test@') { |
|
| 267 | - $db = ''; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - if ( |
|
| 271 | - $f |
|
| 272 | - and @file_exists($f) |
|
| 273 | - and (time() - @filemtime($f) < _CONNECT_RETRY_DELAY) |
|
| 274 | - ) { |
|
| 275 | - spip_log("Echec : $f recent. Pas de tentative de connexion", _LOG_HS); |
|
| 276 | - |
|
| 277 | - return null; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - if (!$prefixe) { |
|
| 281 | - $prefixe = $GLOBALS['table_prefix'] ?? $db; |
|
| 282 | - } |
|
| 283 | - $h = charger_fonction($type, 'req', true); |
|
| 284 | - if (!$h) { |
|
| 285 | - spip_log("les requetes $type ne sont pas fournies", _LOG_HS); |
|
| 286 | - |
|
| 287 | - return null; |
|
| 288 | - } |
|
| 289 | - if ($g = $h($host, $port, $login, $pass, $db, $prefixe)) { |
|
| 290 | - if (!is_array($auth)) { |
|
| 291 | - // compatibilite version 0.7 initiale |
|
| 292 | - $g['ldap'] = $auth; |
|
| 293 | - $auth = ['ldap' => $auth]; |
|
| 294 | - } |
|
| 295 | - $g['authentification'] = $auth; |
|
| 296 | - $g['type'] = $type; |
|
| 297 | - $g['charset'] = $charset; |
|
| 298 | - |
|
| 299 | - return $GLOBALS['db_ok'] = $g; |
|
| 300 | - } |
|
| 301 | - // En cas d'indisponibilite du serveur, eviter de le bombarder |
|
| 302 | - if ($f) { |
|
| 303 | - @touch($f); |
|
| 304 | - spip_log("Echec connexion serveur $type : host[$host] port[$port] login[$login] base[$db]", $type . '.' . _LOG_HS); |
|
| 305 | - } |
|
| 306 | - return null; |
|
| 253 | + // temps avant nouvelle tentative de connexion |
|
| 254 | + // suite a une connection echouee |
|
| 255 | + if (!defined('_CONNECT_RETRY_DELAY')) { |
|
| 256 | + define('_CONNECT_RETRY_DELAY', 30); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + $f = ''; |
|
| 260 | + // un fichier de identifiant par combinaison (type,host,port,db) |
|
| 261 | + // pour ne pas declarer tout indisponible d'un coup |
|
| 262 | + // si en cours d'installation ou si db=@test@ on ne pose rien |
|
| 263 | + // car c'est un test de connexion |
|
| 264 | + if (!defined('_ECRIRE_INSTALL') and $db !== '@test@') { |
|
| 265 | + $f = _DIR_TMP . $type . '.' . substr(md5($host . $port . $db), 0, 8) . '.out'; |
|
| 266 | + } elseif ($db == '@test@') { |
|
| 267 | + $db = ''; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + if ( |
|
| 271 | + $f |
|
| 272 | + and @file_exists($f) |
|
| 273 | + and (time() - @filemtime($f) < _CONNECT_RETRY_DELAY) |
|
| 274 | + ) { |
|
| 275 | + spip_log("Echec : $f recent. Pas de tentative de connexion", _LOG_HS); |
|
| 276 | + |
|
| 277 | + return null; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + if (!$prefixe) { |
|
| 281 | + $prefixe = $GLOBALS['table_prefix'] ?? $db; |
|
| 282 | + } |
|
| 283 | + $h = charger_fonction($type, 'req', true); |
|
| 284 | + if (!$h) { |
|
| 285 | + spip_log("les requetes $type ne sont pas fournies", _LOG_HS); |
|
| 286 | + |
|
| 287 | + return null; |
|
| 288 | + } |
|
| 289 | + if ($g = $h($host, $port, $login, $pass, $db, $prefixe)) { |
|
| 290 | + if (!is_array($auth)) { |
|
| 291 | + // compatibilite version 0.7 initiale |
|
| 292 | + $g['ldap'] = $auth; |
|
| 293 | + $auth = ['ldap' => $auth]; |
|
| 294 | + } |
|
| 295 | + $g['authentification'] = $auth; |
|
| 296 | + $g['type'] = $type; |
|
| 297 | + $g['charset'] = $charset; |
|
| 298 | + |
|
| 299 | + return $GLOBALS['db_ok'] = $g; |
|
| 300 | + } |
|
| 301 | + // En cas d'indisponibilite du serveur, eviter de le bombarder |
|
| 302 | + if ($f) { |
|
| 303 | + @touch($f); |
|
| 304 | + spip_log("Echec connexion serveur $type : host[$host] port[$port] login[$login] base[$db]", $type . '.' . _LOG_HS); |
|
| 305 | + } |
|
| 306 | + return null; |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | |
@@ -335,32 +335,32 @@ discard block |
||
| 335 | 335 | * - nom du charset sinon |
| 336 | 336 | **/ |
| 337 | 337 | function spip_connect_main($connexion, $charset_sql_connexion = '') { |
| 338 | - if ($GLOBALS['spip_connect_version'] < 0.1 and _DIR_RESTREINT) { |
|
| 339 | - include_spip('inc/headers'); |
|
| 340 | - redirige_url_ecrire('upgrade', 'reinstall=oui'); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - if (!($f = $connexion['select'])) { |
|
| 344 | - return false; |
|
| 345 | - } |
|
| 346 | - // si le charset est fourni, l'utiliser |
|
| 347 | - if ($charset_sql_connexion) { |
|
| 348 | - return $charset_sql_connexion; |
|
| 349 | - } |
|
| 350 | - // sinon on regarde la table spip_meta |
|
| 351 | - // en cas d'erreur select retourne la requette (is_string=true donc) |
|
| 352 | - if ( |
|
| 353 | - !$r = $f('valeur', 'spip_meta', "nom='charset_sql_connexion'") |
|
| 354 | - or is_string($r) |
|
| 355 | - ) { |
|
| 356 | - return false; |
|
| 357 | - } |
|
| 358 | - if (!($f = $connexion['fetch'])) { |
|
| 359 | - return false; |
|
| 360 | - } |
|
| 361 | - $r = $f($r); |
|
| 362 | - |
|
| 363 | - return (isset($r['valeur']) && $r['valeur']) ? $r['valeur'] : -1; |
|
| 338 | + if ($GLOBALS['spip_connect_version'] < 0.1 and _DIR_RESTREINT) { |
|
| 339 | + include_spip('inc/headers'); |
|
| 340 | + redirige_url_ecrire('upgrade', 'reinstall=oui'); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + if (!($f = $connexion['select'])) { |
|
| 344 | + return false; |
|
| 345 | + } |
|
| 346 | + // si le charset est fourni, l'utiliser |
|
| 347 | + if ($charset_sql_connexion) { |
|
| 348 | + return $charset_sql_connexion; |
|
| 349 | + } |
|
| 350 | + // sinon on regarde la table spip_meta |
|
| 351 | + // en cas d'erreur select retourne la requette (is_string=true donc) |
|
| 352 | + if ( |
|
| 353 | + !$r = $f('valeur', 'spip_meta', "nom='charset_sql_connexion'") |
|
| 354 | + or is_string($r) |
|
| 355 | + ) { |
|
| 356 | + return false; |
|
| 357 | + } |
|
| 358 | + if (!($f = $connexion['fetch'])) { |
|
| 359 | + return false; |
|
| 360 | + } |
|
| 361 | + $r = $f($r); |
|
| 362 | + |
|
| 363 | + return (isset($r['valeur']) && $r['valeur']) ? $r['valeur'] : -1; |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | /** |
@@ -376,8 +376,8 @@ discard block |
||
| 376 | 376 | * @return array |
| 377 | 377 | */ |
| 378 | 378 | function spip_connect_ldap($serveur = '') { |
| 379 | - include_spip('auth/ldap'); |
|
| 380 | - return auth_ldap_connect($serveur); |
|
| 379 | + include_spip('auth/ldap'); |
|
| 380 | + return auth_ldap_connect($serveur); |
|
| 381 | 381 | } |
| 382 | 382 | |
| 383 | 383 | /** |
@@ -393,16 +393,16 @@ discard block |
||
| 393 | 393 | * @return string Valeur échappée. |
| 394 | 394 | **/ |
| 395 | 395 | function _q($a): string { |
| 396 | - if (is_numeric($a)) { |
|
| 397 | - return strval($a); |
|
| 398 | - } elseif (is_array($a)) { |
|
| 399 | - return join(',', array_map('_q', $a)); |
|
| 400 | - } elseif (is_scalar($a)) { |
|
| 401 | - return ("'" . addslashes($a) . "'"); |
|
| 402 | - } elseif ($a === null) { |
|
| 403 | - return "''"; |
|
| 404 | - } |
|
| 405 | - throw new \RuntimeException('Can’t use _q with ' . gettype($a)); |
|
| 396 | + if (is_numeric($a)) { |
|
| 397 | + return strval($a); |
|
| 398 | + } elseif (is_array($a)) { |
|
| 399 | + return join(',', array_map('_q', $a)); |
|
| 400 | + } elseif (is_scalar($a)) { |
|
| 401 | + return ("'" . addslashes($a) . "'"); |
|
| 402 | + } elseif ($a === null) { |
|
| 403 | + return "''"; |
|
| 404 | + } |
|
| 405 | + throw new \RuntimeException('Can’t use _q with ' . gettype($a)); |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | /** |
@@ -418,75 +418,75 @@ discard block |
||
| 418 | 418 | * @return array |
| 419 | 419 | */ |
| 420 | 420 | function query_echappe_textes($query, $uniqid = null) { |
| 421 | - static $codeEchappements = null; |
|
| 422 | - if (is_null($codeEchappements) or $uniqid) { |
|
| 423 | - if (is_null($uniqid)) { |
|
| 424 | - $uniqid = uniqid(); |
|
| 425 | - } |
|
| 426 | - $uniqid = substr(md5($uniqid), 0, 4); |
|
| 427 | - $codeEchappements = ['\\\\' => "\x1@#{$uniqid}#@\x1", "\\'" => "\x2@#{$uniqid}#@\x2", '\\"' => "\x3@#{$uniqid}#@\x3", '%' => "\x4@#{$uniqid}#@\x4"]; |
|
| 428 | - } |
|
| 429 | - if ($query === null) { |
|
| 430 | - return $codeEchappements; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - // si la query contient deja des codes d'echappement on va s'emmeler les pinceaux et donc on ne touche a rien |
|
| 434 | - // ce n'est pas un cas legitime |
|
| 435 | - foreach ($codeEchappements as $codeEchappement) { |
|
| 436 | - if (strpos($query, (string) $codeEchappement) !== false) { |
|
| 437 | - return [$query, []]; |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - $query_echappees = str_replace(array_keys($codeEchappements), array_values($codeEchappements), $query); |
|
| 442 | - if (preg_match_all("/('[^']*')|(\"[^\"]*\")/S", $query_echappees, $textes)) { |
|
| 443 | - $textes = reset($textes); |
|
| 444 | - |
|
| 445 | - $parts = []; |
|
| 446 | - $currentpos = 0; |
|
| 447 | - $k = 0; |
|
| 448 | - while (count($textes)) { |
|
| 449 | - $part = array_shift($textes); |
|
| 450 | - $nextpos = strpos($query_echappees, $part, $currentpos); |
|
| 451 | - // si besoin recoller ensemble les doubles '' de sqlite (echappement des ') |
|
| 452 | - while (count($textes) and substr($part, -1) === "'") { |
|
| 453 | - $next = reset($textes); |
|
| 454 | - if ( |
|
| 455 | - strpos($next, "'") === 0 |
|
| 456 | - and strpos($query_echappees, $part . $next, $currentpos) === $nextpos |
|
| 457 | - ) { |
|
| 458 | - $part .= array_shift($textes); |
|
| 459 | - } |
|
| 460 | - else { |
|
| 461 | - break; |
|
| 462 | - } |
|
| 463 | - } |
|
| 464 | - $k++; |
|
| 465 | - $parts[$k] = [ |
|
| 466 | - 'texte' => $part, |
|
| 467 | - 'position' => $nextpos, |
|
| 468 | - 'placeholder' => '%' . $k . '$s', |
|
| 469 | - ]; |
|
| 470 | - $currentpos = $nextpos + strlen($part); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - // et on replace les parts une par une en commencant par la fin |
|
| 474 | - while ($k > 0) { |
|
| 475 | - $query_echappees = substr_replace($query_echappees, $parts[$k]['placeholder'], $parts[$k]['position'], strlen($parts[$k]['texte'])); |
|
| 476 | - $k--; |
|
| 477 | - } |
|
| 478 | - $textes = array_column($parts, 'texte'); |
|
| 479 | - } else { |
|
| 480 | - $textes = []; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - // si il reste des quotes simples ou doubles, c'est qu'on s'est emmelle les pinceaux |
|
| 484 | - // dans le doute on ne touche a rien |
|
| 485 | - if (strpbrk($query_echappees, "'\"") !== false) { |
|
| 486 | - return [$query, []]; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - return [$query_echappees, $textes]; |
|
| 421 | + static $codeEchappements = null; |
|
| 422 | + if (is_null($codeEchappements) or $uniqid) { |
|
| 423 | + if (is_null($uniqid)) { |
|
| 424 | + $uniqid = uniqid(); |
|
| 425 | + } |
|
| 426 | + $uniqid = substr(md5($uniqid), 0, 4); |
|
| 427 | + $codeEchappements = ['\\\\' => "\x1@#{$uniqid}#@\x1", "\\'" => "\x2@#{$uniqid}#@\x2", '\\"' => "\x3@#{$uniqid}#@\x3", '%' => "\x4@#{$uniqid}#@\x4"]; |
|
| 428 | + } |
|
| 429 | + if ($query === null) { |
|
| 430 | + return $codeEchappements; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + // si la query contient deja des codes d'echappement on va s'emmeler les pinceaux et donc on ne touche a rien |
|
| 434 | + // ce n'est pas un cas legitime |
|
| 435 | + foreach ($codeEchappements as $codeEchappement) { |
|
| 436 | + if (strpos($query, (string) $codeEchappement) !== false) { |
|
| 437 | + return [$query, []]; |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + $query_echappees = str_replace(array_keys($codeEchappements), array_values($codeEchappements), $query); |
|
| 442 | + if (preg_match_all("/('[^']*')|(\"[^\"]*\")/S", $query_echappees, $textes)) { |
|
| 443 | + $textes = reset($textes); |
|
| 444 | + |
|
| 445 | + $parts = []; |
|
| 446 | + $currentpos = 0; |
|
| 447 | + $k = 0; |
|
| 448 | + while (count($textes)) { |
|
| 449 | + $part = array_shift($textes); |
|
| 450 | + $nextpos = strpos($query_echappees, $part, $currentpos); |
|
| 451 | + // si besoin recoller ensemble les doubles '' de sqlite (echappement des ') |
|
| 452 | + while (count($textes) and substr($part, -1) === "'") { |
|
| 453 | + $next = reset($textes); |
|
| 454 | + if ( |
|
| 455 | + strpos($next, "'") === 0 |
|
| 456 | + and strpos($query_echappees, $part . $next, $currentpos) === $nextpos |
|
| 457 | + ) { |
|
| 458 | + $part .= array_shift($textes); |
|
| 459 | + } |
|
| 460 | + else { |
|
| 461 | + break; |
|
| 462 | + } |
|
| 463 | + } |
|
| 464 | + $k++; |
|
| 465 | + $parts[$k] = [ |
|
| 466 | + 'texte' => $part, |
|
| 467 | + 'position' => $nextpos, |
|
| 468 | + 'placeholder' => '%' . $k . '$s', |
|
| 469 | + ]; |
|
| 470 | + $currentpos = $nextpos + strlen($part); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + // et on replace les parts une par une en commencant par la fin |
|
| 474 | + while ($k > 0) { |
|
| 475 | + $query_echappees = substr_replace($query_echappees, $parts[$k]['placeholder'], $parts[$k]['position'], strlen($parts[$k]['texte'])); |
|
| 476 | + $k--; |
|
| 477 | + } |
|
| 478 | + $textes = array_column($parts, 'texte'); |
|
| 479 | + } else { |
|
| 480 | + $textes = []; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + // si il reste des quotes simples ou doubles, c'est qu'on s'est emmelle les pinceaux |
|
| 484 | + // dans le doute on ne touche a rien |
|
| 485 | + if (strpbrk($query_echappees, "'\"") !== false) { |
|
| 486 | + return [$query, []]; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + return [$query_echappees, $textes]; |
|
| 490 | 490 | } |
| 491 | 491 | |
| 492 | 492 | /** |
@@ -500,16 +500,16 @@ discard block |
||
| 500 | 500 | * @return string |
| 501 | 501 | */ |
| 502 | 502 | function query_reinjecte_textes($query, $textes) { |
| 503 | - // recuperer les codes echappements |
|
| 504 | - $codeEchappements = query_echappe_textes(null); |
|
| 503 | + // recuperer les codes echappements |
|
| 504 | + $codeEchappements = query_echappe_textes(null); |
|
| 505 | 505 | |
| 506 | - if (!empty($textes)) { |
|
| 507 | - $query = sprintf($query, ...$textes); |
|
| 508 | - } |
|
| 506 | + if (!empty($textes)) { |
|
| 507 | + $query = sprintf($query, ...$textes); |
|
| 508 | + } |
|
| 509 | 509 | |
| 510 | - $query = str_replace(array_values($codeEchappements), array_keys($codeEchappements), $query); |
|
| 510 | + $query = str_replace(array_values($codeEchappements), array_keys($codeEchappements), $query); |
|
| 511 | 511 | |
| 512 | - return $query; |
|
| 512 | + return $query; |
|
| 513 | 513 | } |
| 514 | 514 | |
| 515 | 515 | |
@@ -528,7 +528,7 @@ discard block |
||
| 528 | 528 | **/ |
| 529 | 529 | function spip_query($query, $serveur = '') { |
| 530 | 530 | |
| 531 | - $f = spip_connect_sql($GLOBALS['spip_sql_version'], 'query', $serveur, true); |
|
| 531 | + $f = spip_connect_sql($GLOBALS['spip_sql_version'], 'query', $serveur, true); |
|
| 532 | 532 | |
| 533 | - return function_exists($f) ? $f($query, $serveur) : false; |
|
| 533 | + return function_exists($f) ? $f($query, $serveur) : false; |
|
| 534 | 534 | } |
@@ -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 | /** |
@@ -38,15 +38,15 @@ discard block |
||
| 38 | 38 | define('_PHP_MAX', '8.2.99'); |
| 39 | 39 | |
| 40 | 40 | if (!defined('_DIR_RESTREINT_ABS')) { |
| 41 | - /** le nom du repertoire ecrire/ */ |
|
| 42 | - define('_DIR_RESTREINT_ABS', 'ecrire/'); |
|
| 41 | + /** le nom du repertoire ecrire/ */ |
|
| 42 | + define('_DIR_RESTREINT_ABS', 'ecrire/'); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | /** Chemin relatif pour aller dans ecrire |
| 46 | 46 | * vide si on est dans ecrire, 'ecrire/' sinon */ |
| 47 | 47 | define( |
| 48 | - '_DIR_RESTREINT', |
|
| 49 | - (!is_dir(_DIR_RESTREINT_ABS) ? '' : _DIR_RESTREINT_ABS) |
|
| 48 | + '_DIR_RESTREINT', |
|
| 49 | + (!is_dir(_DIR_RESTREINT_ABS) ? '' : _DIR_RESTREINT_ABS) |
|
| 50 | 50 | ); |
| 51 | 51 | |
| 52 | 52 | /** Chemin relatif pour aller à la racine */ |
@@ -61,8 +61,8 @@ discard block |
||
| 61 | 61 | |
| 62 | 62 | // Icones |
| 63 | 63 | if (!defined('_NOM_IMG_PACK')) { |
| 64 | - /** Nom du dossier images */ |
|
| 65 | - define('_NOM_IMG_PACK', 'images/'); |
|
| 64 | + /** Nom du dossier images */ |
|
| 65 | + define('_NOM_IMG_PACK', 'images/'); |
|
| 66 | 66 | } |
| 67 | 67 | /** le chemin http (relatif) vers les images standard */ |
| 68 | 68 | define('_DIR_IMG_PACK', (_DIR_RACINE . 'prive/' . _NOM_IMG_PACK)); |
@@ -71,8 +71,8 @@ discard block |
||
| 71 | 71 | define('_ROOT_IMG_PACK', dirname(__DIR__) . '/prive/' . _NOM_IMG_PACK); |
| 72 | 72 | |
| 73 | 73 | if (!defined('_JAVASCRIPT')) { |
| 74 | - /** Nom du repertoire des bibliotheques JavaScript */ |
|
| 75 | - define('_JAVASCRIPT', 'javascript/'); |
|
| 74 | + /** Nom du repertoire des bibliotheques JavaScript */ |
|
| 75 | + define('_JAVASCRIPT', 'javascript/'); |
|
| 76 | 76 | } // utilisable avec #CHEMIN et find_in_path |
| 77 | 77 | /** le nom du repertoire des bibliotheques JavaScript du prive */ |
| 78 | 78 | define('_DIR_JAVASCRIPT', (_DIR_RACINE . 'prive/' . _JAVASCRIPT)); |
@@ -82,47 +82,47 @@ discard block |
||
| 82 | 82 | # mais on peut les mettre ailleurs et changer completement les noms |
| 83 | 83 | |
| 84 | 84 | if (!defined('_NOM_TEMPORAIRES_INACCESSIBLES')) { |
| 85 | - /** Nom du repertoire des fichiers Temporaires Inaccessibles par http:// */ |
|
| 86 | - define('_NOM_TEMPORAIRES_INACCESSIBLES', 'tmp/'); |
|
| 85 | + /** Nom du repertoire des fichiers Temporaires Inaccessibles par http:// */ |
|
| 86 | + define('_NOM_TEMPORAIRES_INACCESSIBLES', 'tmp/'); |
|
| 87 | 87 | } |
| 88 | 88 | if (!defined('_NOM_TEMPORAIRES_ACCESSIBLES')) { |
| 89 | - /** Nom du repertoire des fichiers Temporaires Accessibles par http:// */ |
|
| 90 | - define('_NOM_TEMPORAIRES_ACCESSIBLES', 'local/'); |
|
| 89 | + /** Nom du repertoire des fichiers Temporaires Accessibles par http:// */ |
|
| 90 | + define('_NOM_TEMPORAIRES_ACCESSIBLES', 'local/'); |
|
| 91 | 91 | } |
| 92 | 92 | if (!defined('_NOM_PERMANENTS_INACCESSIBLES')) { |
| 93 | - /** Nom du repertoire des fichiers Permanents Inaccessibles par http:// */ |
|
| 94 | - define('_NOM_PERMANENTS_INACCESSIBLES', 'config/'); |
|
| 93 | + /** Nom du repertoire des fichiers Permanents Inaccessibles par http:// */ |
|
| 94 | + define('_NOM_PERMANENTS_INACCESSIBLES', 'config/'); |
|
| 95 | 95 | } |
| 96 | 96 | if (!defined('_NOM_PERMANENTS_ACCESSIBLES')) { |
| 97 | - /** Nom du repertoire des fichiers Permanents Accessibles par http:// */ |
|
| 98 | - define('_NOM_PERMANENTS_ACCESSIBLES', 'IMG/'); |
|
| 97 | + /** Nom du repertoire des fichiers Permanents Accessibles par http:// */ |
|
| 98 | + define('_NOM_PERMANENTS_ACCESSIBLES', 'IMG/'); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | |
| 102 | 102 | /** Le nom du fichier de personnalisation */ |
| 103 | 103 | if (!defined('_NOM_CONFIG')) { |
| 104 | - define('_NOM_CONFIG', 'mes_options'); |
|
| 104 | + define('_NOM_CONFIG', 'mes_options'); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | // Son emplacement absolu si on le trouve |
| 108 | 108 | if ( |
| 109 | - @file_exists($f = _ROOT_RACINE . _NOM_PERMANENTS_INACCESSIBLES . _NOM_CONFIG . '.php') |
|
| 110 | - or (@file_exists($f = _ROOT_RESTREINT . _NOM_CONFIG . '.php')) |
|
| 109 | + @file_exists($f = _ROOT_RACINE . _NOM_PERMANENTS_INACCESSIBLES . _NOM_CONFIG . '.php') |
|
| 110 | + or (@file_exists($f = _ROOT_RESTREINT . _NOM_CONFIG . '.php')) |
|
| 111 | 111 | ) { |
| 112 | - /** Emplacement absolu du fichier d'option */ |
|
| 113 | - define('_FILE_OPTIONS', $f); |
|
| 112 | + /** Emplacement absolu du fichier d'option */ |
|
| 113 | + define('_FILE_OPTIONS', $f); |
|
| 114 | 114 | } else { |
| 115 | - define('_FILE_OPTIONS', ''); |
|
| 115 | + define('_FILE_OPTIONS', ''); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | if (!defined('MODULES_IDIOMES')) { |
| 119 | - /** |
|
| 120 | - * Modules par défaut pour la traduction. |
|
| 121 | - * |
|
| 122 | - * Constante utilisée par le compilateur et le décompilateur |
|
| 123 | - * sa valeur etant traitée par inc_traduire_dist |
|
| 124 | - */ |
|
| 125 | - define('MODULES_IDIOMES', 'public|spip|ecrire'); |
|
| 119 | + /** |
|
| 120 | + * Modules par défaut pour la traduction. |
|
| 121 | + * |
|
| 122 | + * Constante utilisée par le compilateur et le décompilateur |
|
| 123 | + * sa valeur etant traitée par inc_traduire_dist |
|
| 124 | + */ |
|
| 125 | + define('MODULES_IDIOMES', 'public|spip|ecrire'); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | // *** Fin des define *** // |
@@ -130,10 +130,10 @@ discard block |
||
| 130 | 130 | |
| 131 | 131 | // inclure l'ecran de securite |
| 132 | 132 | if ( |
| 133 | - !defined('_ECRAN_SECURITE') |
|
| 134 | - and @file_exists($f = _ROOT_RACINE . _NOM_PERMANENTS_INACCESSIBLES . 'ecran_securite.php') |
|
| 133 | + !defined('_ECRAN_SECURITE') |
|
| 134 | + and @file_exists($f = _ROOT_RACINE . _NOM_PERMANENTS_INACCESSIBLES . 'ecran_securite.php') |
|
| 135 | 135 | ) { |
| 136 | - include $f; |
|
| 136 | + include $f; |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | |
@@ -141,30 +141,30 @@ discard block |
||
| 141 | 141 | * Détecteur de robot d'indexation |
| 142 | 142 | */ |
| 143 | 143 | if (!defined('_IS_BOT')) { |
| 144 | - define( |
|
| 145 | - '_IS_BOT', |
|
| 146 | - isset($_SERVER['HTTP_USER_AGENT']) |
|
| 147 | - and preg_match( |
|
| 148 | - // mots generiques |
|
| 149 | - ',bot|slurp|crawler|spider|webvac|yandex|' |
|
| 150 | - // MSIE 6.0 est un botnet 99,9% du temps, on traite donc ce USER_AGENT comme un bot |
|
| 151 | - . 'MSIE 6\.0|' |
|
| 152 | - // UA plus cibles |
|
| 153 | - . '80legs|accoona|AltaVista|ASPSeek|Baidu|Charlotte|EC2LinkFinder|eStyle|facebook|flipboard|hootsuite|FunWebProducts|Google|Genieo|INA dlweb|InfegyAtlas|Java VM|LiteFinder|Lycos|MetaURI|Moreover|Rambler|Scooter|ScrubbyBloglines|Yahoo|Yeti' |
|
| 154 | - . ',i', |
|
| 155 | - (string)$_SERVER['HTTP_USER_AGENT'] |
|
| 156 | - ) |
|
| 157 | - ); |
|
| 144 | + define( |
|
| 145 | + '_IS_BOT', |
|
| 146 | + isset($_SERVER['HTTP_USER_AGENT']) |
|
| 147 | + and preg_match( |
|
| 148 | + // mots generiques |
|
| 149 | + ',bot|slurp|crawler|spider|webvac|yandex|' |
|
| 150 | + // MSIE 6.0 est un botnet 99,9% du temps, on traite donc ce USER_AGENT comme un bot |
|
| 151 | + . 'MSIE 6\.0|' |
|
| 152 | + // UA plus cibles |
|
| 153 | + . '80legs|accoona|AltaVista|ASPSeek|Baidu|Charlotte|EC2LinkFinder|eStyle|facebook|flipboard|hootsuite|FunWebProducts|Google|Genieo|INA dlweb|InfegyAtlas|Java VM|LiteFinder|Lycos|MetaURI|Moreover|Rambler|Scooter|ScrubbyBloglines|Yahoo|Yeti' |
|
| 154 | + . ',i', |
|
| 155 | + (string)$_SERVER['HTTP_USER_AGENT'] |
|
| 156 | + ) |
|
| 157 | + ); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | if (!defined('_IS_CLI')) { |
| 161 | - define( |
|
| 162 | - '_IS_CLI', |
|
| 163 | - !isset($_SERVER['HTTP_HOST']) |
|
| 164 | - and !strlen($_SERVER['DOCUMENT_ROOT']) |
|
| 165 | - and !empty($_SERVER['argv']) |
|
| 166 | - and empty($_SERVER['REQUEST_METHOD']) |
|
| 167 | - ); |
|
| 161 | + define( |
|
| 162 | + '_IS_CLI', |
|
| 163 | + !isset($_SERVER['HTTP_HOST']) |
|
| 164 | + and !strlen($_SERVER['DOCUMENT_ROOT']) |
|
| 165 | + and !empty($_SERVER['argv']) |
|
| 166 | + and empty($_SERVER['REQUEST_METHOD']) |
|
| 167 | + ); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | // *** Parametrage par defaut de SPIP *** |
@@ -176,61 +176,61 @@ discard block |
||
| 176 | 176 | // Ne pas les rendre indefinies. |
| 177 | 177 | |
| 178 | 178 | global |
| 179 | - $nombre_de_logs, |
|
| 180 | - $taille_des_logs, |
|
| 181 | - $table_prefix, |
|
| 182 | - $cookie_prefix, |
|
| 183 | - $dossier_squelettes, |
|
| 184 | - $filtrer_javascript, |
|
| 185 | - $type_urls, |
|
| 186 | - $debut_date_publication, |
|
| 187 | - $ip, |
|
| 188 | - $mysql_rappel_connexion, |
|
| 189 | - $mysql_rappel_nom_base, |
|
| 190 | - $test_i18n, |
|
| 191 | - $ignore_auth_http, |
|
| 192 | - $ignore_remote_user, |
|
| 193 | - $derniere_modif_invalide, |
|
| 194 | - $home_server, |
|
| 195 | - $help_server, |
|
| 196 | - $url_glossaire_externe, |
|
| 197 | - $tex_server, |
|
| 198 | - $traiter_math, |
|
| 199 | - $xhtml, |
|
| 200 | - $xml_indent, |
|
| 201 | - $source_vignettes, |
|
| 202 | - $formats_logos, |
|
| 203 | - $controler_dates_rss, |
|
| 204 | - $spip_pipeline, |
|
| 205 | - $spip_matrice, |
|
| 206 | - $plugins, |
|
| 207 | - $surcharges, |
|
| 208 | - $exceptions_des_tables, |
|
| 209 | - $tables_principales, |
|
| 210 | - $table_des_tables, |
|
| 211 | - $tables_auxiliaires, |
|
| 212 | - $table_primary, |
|
| 213 | - $table_date, |
|
| 214 | - $table_titre, |
|
| 215 | - $tables_jointures, |
|
| 216 | - $liste_des_statuts, |
|
| 217 | - $liste_des_etats, |
|
| 218 | - $liste_des_authentifications, |
|
| 219 | - $spip_version_branche, |
|
| 220 | - $spip_version_code, |
|
| 221 | - $spip_version_base, |
|
| 222 | - $spip_sql_version, |
|
| 223 | - $spip_version_affichee, |
|
| 224 | - $visiteur_session, |
|
| 225 | - $auteur_session, |
|
| 226 | - $connect_statut, |
|
| 227 | - $connect_toutes_rubriques, |
|
| 228 | - $hash_recherche, |
|
| 229 | - $hash_recherche_strict, |
|
| 230 | - $ldap_present, |
|
| 231 | - $meta, |
|
| 232 | - $connect_id_rubrique, |
|
| 233 | - $puce; |
|
| 179 | + $nombre_de_logs, |
|
| 180 | + $taille_des_logs, |
|
| 181 | + $table_prefix, |
|
| 182 | + $cookie_prefix, |
|
| 183 | + $dossier_squelettes, |
|
| 184 | + $filtrer_javascript, |
|
| 185 | + $type_urls, |
|
| 186 | + $debut_date_publication, |
|
| 187 | + $ip, |
|
| 188 | + $mysql_rappel_connexion, |
|
| 189 | + $mysql_rappel_nom_base, |
|
| 190 | + $test_i18n, |
|
| 191 | + $ignore_auth_http, |
|
| 192 | + $ignore_remote_user, |
|
| 193 | + $derniere_modif_invalide, |
|
| 194 | + $home_server, |
|
| 195 | + $help_server, |
|
| 196 | + $url_glossaire_externe, |
|
| 197 | + $tex_server, |
|
| 198 | + $traiter_math, |
|
| 199 | + $xhtml, |
|
| 200 | + $xml_indent, |
|
| 201 | + $source_vignettes, |
|
| 202 | + $formats_logos, |
|
| 203 | + $controler_dates_rss, |
|
| 204 | + $spip_pipeline, |
|
| 205 | + $spip_matrice, |
|
| 206 | + $plugins, |
|
| 207 | + $surcharges, |
|
| 208 | + $exceptions_des_tables, |
|
| 209 | + $tables_principales, |
|
| 210 | + $table_des_tables, |
|
| 211 | + $tables_auxiliaires, |
|
| 212 | + $table_primary, |
|
| 213 | + $table_date, |
|
| 214 | + $table_titre, |
|
| 215 | + $tables_jointures, |
|
| 216 | + $liste_des_statuts, |
|
| 217 | + $liste_des_etats, |
|
| 218 | + $liste_des_authentifications, |
|
| 219 | + $spip_version_branche, |
|
| 220 | + $spip_version_code, |
|
| 221 | + $spip_version_base, |
|
| 222 | + $spip_sql_version, |
|
| 223 | + $spip_version_affichee, |
|
| 224 | + $visiteur_session, |
|
| 225 | + $auteur_session, |
|
| 226 | + $connect_statut, |
|
| 227 | + $connect_toutes_rubriques, |
|
| 228 | + $hash_recherche, |
|
| 229 | + $hash_recherche_strict, |
|
| 230 | + $ldap_present, |
|
| 231 | + $meta, |
|
| 232 | + $connect_id_rubrique, |
|
| 233 | + $puce; |
|
| 234 | 234 | |
| 235 | 235 | # comment on logge, defaut 4 tmp/spip.log de 100k, 0 ou 0 suppriment le log |
| 236 | 236 | $nombre_de_logs = 4; |
@@ -285,48 +285,48 @@ discard block |
||
| 285 | 285 | // Prendre en compte les entetes HTTP_X_FORWARDED_XX |
| 286 | 286 | // |
| 287 | 287 | if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) and $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { |
| 288 | - if (empty($_SERVER['HTTP_X_FORWARDED_HOST'])) { |
|
| 289 | - $_SERVER['HTTP_X_FORWARDED_HOST'] = $_SERVER['HTTP_HOST']; |
|
| 290 | - } |
|
| 291 | - if (empty($_SERVER['HTTP_X_FORWARDED_PORT'])) { |
|
| 292 | - $_SERVER['HTTP_X_FORWARDED_PORT'] = 443; |
|
| 293 | - } |
|
| 288 | + if (empty($_SERVER['HTTP_X_FORWARDED_HOST'])) { |
|
| 289 | + $_SERVER['HTTP_X_FORWARDED_HOST'] = $_SERVER['HTTP_HOST']; |
|
| 290 | + } |
|
| 291 | + if (empty($_SERVER['HTTP_X_FORWARDED_PORT'])) { |
|
| 292 | + $_SERVER['HTTP_X_FORWARDED_PORT'] = 443; |
|
| 293 | + } |
|
| 294 | 294 | } |
| 295 | 295 | if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { |
| 296 | - if (isset($_SERVER['HTTP_X_FORWARDED_PORT']) and is_numeric($_SERVER['HTTP_X_FORWARDED_PORT'])) { |
|
| 297 | - $_SERVER['SERVER_PORT'] = $_SERVER['HTTP_X_FORWARDED_PORT']; |
|
| 298 | - if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) and $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { |
|
| 299 | - $_SERVER['HTTPS'] = 'on'; |
|
| 300 | - if (isset($_SERVER['REQUEST_SCHEME'])) { |
|
| 301 | - $_SERVER['REQUEST_SCHEME'] = 'https'; |
|
| 302 | - } |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - $host = $_SERVER['HTTP_X_FORWARDED_HOST']; |
|
| 306 | - if (strpos($host, ',') !== false) { |
|
| 307 | - $h = explode(',', $host); |
|
| 308 | - $host = trim(reset($h)); |
|
| 309 | - } |
|
| 310 | - // securite sur le contenu de l'entete |
|
| 311 | - $host = strtr($host, "<>?\"\{\}\$'` \r\n", '____________'); |
|
| 312 | - $_SERVER['HTTP_HOST'] = $host; |
|
| 296 | + if (isset($_SERVER['HTTP_X_FORWARDED_PORT']) and is_numeric($_SERVER['HTTP_X_FORWARDED_PORT'])) { |
|
| 297 | + $_SERVER['SERVER_PORT'] = $_SERVER['HTTP_X_FORWARDED_PORT']; |
|
| 298 | + if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) and $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { |
|
| 299 | + $_SERVER['HTTPS'] = 'on'; |
|
| 300 | + if (isset($_SERVER['REQUEST_SCHEME'])) { |
|
| 301 | + $_SERVER['REQUEST_SCHEME'] = 'https'; |
|
| 302 | + } |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + $host = $_SERVER['HTTP_X_FORWARDED_HOST']; |
|
| 306 | + if (strpos($host, ',') !== false) { |
|
| 307 | + $h = explode(',', $host); |
|
| 308 | + $host = trim(reset($h)); |
|
| 309 | + } |
|
| 310 | + // securite sur le contenu de l'entete |
|
| 311 | + $host = strtr($host, "<>?\"\{\}\$'` \r\n", '____________'); |
|
| 312 | + $_SERVER['HTTP_HOST'] = $host; |
|
| 313 | 313 | } |
| 314 | 314 | // |
| 315 | 315 | // On note le numero IP du client dans la variable $ip |
| 316 | 316 | // |
| 317 | 317 | if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
| 318 | - $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
|
| 319 | - if (strpos($ip, ',') !== false) { |
|
| 320 | - $ip = explode(',', $ip); |
|
| 321 | - $ip = reset($ip); |
|
| 322 | - } |
|
| 323 | - // ecraser $_SERVER['REMOTE_ADDR'] si elle est en localhost |
|
| 324 | - if (isset($_SERVER['REMOTE_ADDR']) and $_SERVER['REMOTE_ADDR'] === '127.0.0.1') { |
|
| 325 | - $_SERVER['REMOTE_ADDR'] = $ip; |
|
| 326 | - } |
|
| 318 | + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
|
| 319 | + if (strpos($ip, ',') !== false) { |
|
| 320 | + $ip = explode(',', $ip); |
|
| 321 | + $ip = reset($ip); |
|
| 322 | + } |
|
| 323 | + // ecraser $_SERVER['REMOTE_ADDR'] si elle est en localhost |
|
| 324 | + if (isset($_SERVER['REMOTE_ADDR']) and $_SERVER['REMOTE_ADDR'] === '127.0.0.1') { |
|
| 325 | + $_SERVER['REMOTE_ADDR'] = $ip; |
|
| 326 | + } |
|
| 327 | 327 | } |
| 328 | 328 | if (isset($_SERVER['REMOTE_ADDR'])) { |
| 329 | - $ip = $_SERVER['REMOTE_ADDR']; |
|
| 329 | + $ip = $_SERVER['REMOTE_ADDR']; |
|
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | // Pour renforcer la privacy, decommentez la ligne ci-dessous (ou recopiez-la |
@@ -411,24 +411,24 @@ discard block |
||
| 411 | 411 | |
| 412 | 412 | // Liste des statuts. |
| 413 | 413 | $liste_des_statuts = [ |
| 414 | - 'info_administrateurs' => '0minirezo', |
|
| 415 | - 'info_redacteurs' => '1comite', |
|
| 416 | - 'info_visiteurs' => '6forum', |
|
| 417 | - 'texte_statut_poubelle' => '5poubelle' |
|
| 414 | + 'info_administrateurs' => '0minirezo', |
|
| 415 | + 'info_redacteurs' => '1comite', |
|
| 416 | + 'info_visiteurs' => '6forum', |
|
| 417 | + 'texte_statut_poubelle' => '5poubelle' |
|
| 418 | 418 | ]; |
| 419 | 419 | |
| 420 | 420 | $liste_des_etats = [ |
| 421 | - 'texte_statut_en_cours_redaction' => 'prepa', |
|
| 422 | - 'texte_statut_propose_evaluation' => 'prop', |
|
| 423 | - 'texte_statut_publie' => 'publie', |
|
| 424 | - 'texte_statut_poubelle' => 'poubelle', |
|
| 425 | - 'texte_statut_refuse' => 'refuse' |
|
| 421 | + 'texte_statut_en_cours_redaction' => 'prepa', |
|
| 422 | + 'texte_statut_propose_evaluation' => 'prop', |
|
| 423 | + 'texte_statut_publie' => 'publie', |
|
| 424 | + 'texte_statut_poubelle' => 'poubelle', |
|
| 425 | + 'texte_statut_refuse' => 'refuse' |
|
| 426 | 426 | ]; |
| 427 | 427 | |
| 428 | 428 | // liste des methodes d'authentifications |
| 429 | 429 | $liste_des_authentifications = [ |
| 430 | - 'spip' => 'spip', |
|
| 431 | - 'ldap' => 'ldap' |
|
| 430 | + 'spip' => 'spip', |
|
| 431 | + 'ldap' => 'ldap' |
|
| 432 | 432 | ]; |
| 433 | 433 | |
| 434 | 434 | // Experimental : pour supprimer systematiquement l'affichage des numeros |
@@ -478,12 +478,12 @@ discard block |
||
| 478 | 478 | // Definition personnelles eventuelles |
| 479 | 479 | |
| 480 | 480 | if (_FILE_OPTIONS) { |
| 481 | - include_once _FILE_OPTIONS; |
|
| 481 | + include_once _FILE_OPTIONS; |
|
| 482 | 482 | } |
| 483 | 483 | |
| 484 | 484 | if (!defined('SPIP_ERREUR_REPORT')) { |
| 485 | - /** Masquer les warning */ |
|
| 486 | - define('SPIP_ERREUR_REPORT', E_ALL ^ E_NOTICE ^ E_DEPRECATED); |
|
| 485 | + /** Masquer les warning */ |
|
| 486 | + define('SPIP_ERREUR_REPORT', E_ALL ^ E_NOTICE ^ E_DEPRECATED); |
|
| 487 | 487 | } |
| 488 | 488 | error_reporting(SPIP_ERREUR_REPORT); |
| 489 | 489 | |
@@ -496,10 +496,10 @@ discard block |
||
| 496 | 496 | // ===> on execute en neutralisant les messages d'erreur |
| 497 | 497 | |
| 498 | 498 | spip_initialisation_core( |
| 499 | - (_DIR_RACINE . _NOM_PERMANENTS_INACCESSIBLES), |
|
| 500 | - (_DIR_RACINE . _NOM_PERMANENTS_ACCESSIBLES), |
|
| 501 | - (_DIR_RACINE . _NOM_TEMPORAIRES_INACCESSIBLES), |
|
| 502 | - (_DIR_RACINE . _NOM_TEMPORAIRES_ACCESSIBLES) |
|
| 499 | + (_DIR_RACINE . _NOM_PERMANENTS_INACCESSIBLES), |
|
| 500 | + (_DIR_RACINE . _NOM_PERMANENTS_ACCESSIBLES), |
|
| 501 | + (_DIR_RACINE . _NOM_TEMPORAIRES_INACCESSIBLES), |
|
| 502 | + (_DIR_RACINE . _NOM_TEMPORAIRES_ACCESSIBLES) |
|
| 503 | 503 | ); |
| 504 | 504 | |
| 505 | 505 | |
@@ -509,71 +509,71 @@ discard block |
||
| 509 | 509 | // donc il faut avoir tout fini ici avant de charger les plugins |
| 510 | 510 | |
| 511 | 511 | if (@is_readable(_CACHE_PLUGINS_OPT) and @is_readable(_CACHE_PLUGINS_PATH)) { |
| 512 | - // chargement optimise precompile |
|
| 513 | - include_once(_CACHE_PLUGINS_OPT); |
|
| 512 | + // chargement optimise precompile |
|
| 513 | + include_once(_CACHE_PLUGINS_OPT); |
|
| 514 | 514 | } else { |
| 515 | - spip_initialisation_suite(); |
|
| 516 | - include_spip('inc/plugin'); |
|
| 517 | - // generer les fichiers php precompiles |
|
| 518 | - // de chargement des plugins et des pipelines |
|
| 519 | - actualise_plugins_actifs(); |
|
| 515 | + spip_initialisation_suite(); |
|
| 516 | + include_spip('inc/plugin'); |
|
| 517 | + // generer les fichiers php precompiles |
|
| 518 | + // de chargement des plugins et des pipelines |
|
| 519 | + actualise_plugins_actifs(); |
|
| 520 | 520 | } |
| 521 | 521 | |
| 522 | 522 | // Initialisations non critiques surchargeables par les plugins |
| 523 | 523 | spip_initialisation_suite(); |
| 524 | 524 | |
| 525 | 525 | if (!defined('_LOG_FILTRE_GRAVITE')) { |
| 526 | - /** niveau maxi d'enregistrement des logs */ |
|
| 527 | - define('_LOG_FILTRE_GRAVITE', _LOG_INFO_IMPORTANTE); |
|
| 526 | + /** niveau maxi d'enregistrement des logs */ |
|
| 527 | + define('_LOG_FILTRE_GRAVITE', _LOG_INFO_IMPORTANTE); |
|
| 528 | 528 | } |
| 529 | 529 | |
| 530 | 530 | if (!defined('_OUTILS_DEVELOPPEURS')) { |
| 531 | - /** Activer des outils pour développeurs ? */ |
|
| 532 | - define('_OUTILS_DEVELOPPEURS', false); |
|
| 531 | + /** Activer des outils pour développeurs ? */ |
|
| 532 | + define('_OUTILS_DEVELOPPEURS', false); |
|
| 533 | 533 | } |
| 534 | 534 | |
| 535 | 535 | // charger systematiquement inc/autoriser dans l'espace restreint |
| 536 | 536 | if (test_espace_prive()) { |
| 537 | - include_spip('inc/autoriser'); |
|
| 537 | + include_spip('inc/autoriser'); |
|
| 538 | 538 | } |
| 539 | 539 | // |
| 540 | 540 | // Installer Spip si pas installe... sauf si justement on est en train |
| 541 | 541 | // |
| 542 | 542 | if ( |
| 543 | - !(_FILE_CONNECT |
|
| 544 | - or autoriser_sans_cookie(_request('exec')) |
|
| 545 | - or _request('action') == 'cookie' |
|
| 546 | - or _request('action') == 'converser' |
|
| 547 | - or _request('action') == 'test_dirs') |
|
| 543 | + !(_FILE_CONNECT |
|
| 544 | + or autoriser_sans_cookie(_request('exec')) |
|
| 545 | + or _request('action') == 'cookie' |
|
| 546 | + or _request('action') == 'converser' |
|
| 547 | + or _request('action') == 'test_dirs') |
|
| 548 | 548 | ) { |
| 549 | - // Si on peut installer, on lance illico |
|
| 550 | - if (test_espace_prive()) { |
|
| 551 | - include_spip('inc/headers'); |
|
| 552 | - redirige_url_ecrire('install'); |
|
| 553 | - } else { |
|
| 554 | - // Si on est dans le site public, dire que qq s'en occupe |
|
| 555 | - include_spip('inc/lang'); |
|
| 556 | - utiliser_langue_visiteur(); |
|
| 557 | - include_spip('inc/minipres'); |
|
| 558 | - echo minipres(_T('info_travaux_titre'), "<p style='text-align: center;'>" . _T('info_travaux_texte') . '</p>', ['status' => 503]); |
|
| 559 | - exit; |
|
| 560 | - } |
|
| 561 | - // autrement c'est une install ad hoc (spikini...), on sait pas faire |
|
| 549 | + // Si on peut installer, on lance illico |
|
| 550 | + if (test_espace_prive()) { |
|
| 551 | + include_spip('inc/headers'); |
|
| 552 | + redirige_url_ecrire('install'); |
|
| 553 | + } else { |
|
| 554 | + // Si on est dans le site public, dire que qq s'en occupe |
|
| 555 | + include_spip('inc/lang'); |
|
| 556 | + utiliser_langue_visiteur(); |
|
| 557 | + include_spip('inc/minipres'); |
|
| 558 | + echo minipres(_T('info_travaux_titre'), "<p style='text-align: center;'>" . _T('info_travaux_texte') . '</p>', ['status' => 503]); |
|
| 559 | + exit; |
|
| 560 | + } |
|
| 561 | + // autrement c'est une install ad hoc (spikini...), on sait pas faire |
|
| 562 | 562 | } |
| 563 | 563 | |
| 564 | 564 | // memoriser un tri sessionne eventuel |
| 565 | 565 | if ( |
| 566 | - isset($_REQUEST['var_memotri']) |
|
| 567 | - and $t = $_REQUEST['var_memotri'] |
|
| 568 | - and (strncmp($t, 'trisession', 10) == 0 or strncmp($t, 'senssession', 11) == 0) |
|
| 566 | + isset($_REQUEST['var_memotri']) |
|
| 567 | + and $t = $_REQUEST['var_memotri'] |
|
| 568 | + and (strncmp($t, 'trisession', 10) == 0 or strncmp($t, 'senssession', 11) == 0) |
|
| 569 | 569 | ) { |
| 570 | - if (!function_exists('session_set')) { |
|
| 571 | - include_spip('inc/session'); |
|
| 572 | - } |
|
| 573 | - $t = preg_replace(',\W,', '_', $t); |
|
| 574 | - if ($v = _request($t)) { |
|
| 575 | - session_set($t, $v); |
|
| 576 | - } |
|
| 570 | + if (!function_exists('session_set')) { |
|
| 571 | + include_spip('inc/session'); |
|
| 572 | + } |
|
| 573 | + $t = preg_replace(',\W,', '_', $t); |
|
| 574 | + if ($v = _request($t)) { |
|
| 575 | + session_set($t, $v); |
|
| 576 | + } |
|
| 577 | 577 | } |
| 578 | 578 | |
| 579 | 579 | /** |
@@ -583,22 +583,22 @@ discard block |
||
| 583 | 583 | * La globale $spip_header_silencieux permet de rendre le header minimal pour raisons de securite |
| 584 | 584 | */ |
| 585 | 585 | if (!defined('_HEADER_COMPOSED_BY')) { |
| 586 | - define('_HEADER_COMPOSED_BY', 'Composed-By: SPIP'); |
|
| 586 | + define('_HEADER_COMPOSED_BY', 'Composed-By: SPIP'); |
|
| 587 | 587 | } |
| 588 | 588 | if (!headers_sent() and _HEADER_COMPOSED_BY) { |
| 589 | - if (!defined('_HEADER_VARY')) { |
|
| 590 | - define('_HEADER_VARY', 'Vary: Cookie, Accept-Encoding'); |
|
| 591 | - } |
|
| 592 | - if (_HEADER_VARY) { |
|
| 593 | - header(_HEADER_VARY); |
|
| 594 | - } |
|
| 595 | - if (!isset($GLOBALS['spip_header_silencieux']) or !$GLOBALS['spip_header_silencieux']) { |
|
| 596 | - include_spip('inc/filtres_mini'); |
|
| 597 | - header(_HEADER_COMPOSED_BY . " $spip_version_affichee @ www.spip.net + " . url_absolue(_DIR_VAR . 'config.txt')); |
|
| 598 | - } else { |
|
| 599 | - // header minimal |
|
| 600 | - header(_HEADER_COMPOSED_BY . ' @ www.spip.net'); |
|
| 601 | - } |
|
| 589 | + if (!defined('_HEADER_VARY')) { |
|
| 590 | + define('_HEADER_VARY', 'Vary: Cookie, Accept-Encoding'); |
|
| 591 | + } |
|
| 592 | + if (_HEADER_VARY) { |
|
| 593 | + header(_HEADER_VARY); |
|
| 594 | + } |
|
| 595 | + if (!isset($GLOBALS['spip_header_silencieux']) or !$GLOBALS['spip_header_silencieux']) { |
|
| 596 | + include_spip('inc/filtres_mini'); |
|
| 597 | + header(_HEADER_COMPOSED_BY . " $spip_version_affichee @ www.spip.net + " . url_absolue(_DIR_VAR . 'config.txt')); |
|
| 598 | + } else { |
|
| 599 | + // header minimal |
|
| 600 | + header(_HEADER_COMPOSED_BY . ' @ www.spip.net'); |
|
| 601 | + } |
|
| 602 | 602 | } |
| 603 | 603 | |
| 604 | 604 | $methode = ($_SERVER['REQUEST_METHOD'] ?? ((php_sapi_name() == 'cli') ? 'cli' : '')); |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | * Ce programme est un logiciel libre distribué sous licence GNU/GPL. * |
| 10 | 10 | \***************************************************************************/ |
| 11 | 11 | |
| 12 | -require_once __DIR__ . '/../vendor/autoload.php'; |
|
| 12 | +require_once __DIR__.'/../vendor/autoload.php'; |
|
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * Initialisation de SPIP |
@@ -53,11 +53,11 @@ discard block |
||
| 53 | 53 | define('_DIR_RACINE', _DIR_RESTREINT ? '' : '../'); |
| 54 | 54 | |
| 55 | 55 | /** chemin absolu vers la racine */ |
| 56 | -define('_ROOT_RACINE', dirname(__DIR__) . '/'); |
|
| 56 | +define('_ROOT_RACINE', dirname(__DIR__).'/'); |
|
| 57 | 57 | /** chemin absolu vers le repertoire de travail */ |
| 58 | -define('_ROOT_CWD', getcwd() . '/'); |
|
| 58 | +define('_ROOT_CWD', getcwd().'/'); |
|
| 59 | 59 | /** chemin absolu vers ecrire */ |
| 60 | -define('_ROOT_RESTREINT', _ROOT_CWD . _DIR_RESTREINT); |
|
| 60 | +define('_ROOT_RESTREINT', _ROOT_CWD._DIR_RESTREINT); |
|
| 61 | 61 | |
| 62 | 62 | // Icones |
| 63 | 63 | if (!defined('_NOM_IMG_PACK')) { |
@@ -65,17 +65,17 @@ discard block |
||
| 65 | 65 | define('_NOM_IMG_PACK', 'images/'); |
| 66 | 66 | } |
| 67 | 67 | /** le chemin http (relatif) vers les images standard */ |
| 68 | -define('_DIR_IMG_PACK', (_DIR_RACINE . 'prive/' . _NOM_IMG_PACK)); |
|
| 68 | +define('_DIR_IMG_PACK', (_DIR_RACINE.'prive/'._NOM_IMG_PACK)); |
|
| 69 | 69 | |
| 70 | 70 | /** le chemin php (absolu) vers les images standard (pour hebergement centralise) */ |
| 71 | -define('_ROOT_IMG_PACK', dirname(__DIR__) . '/prive/' . _NOM_IMG_PACK); |
|
| 71 | +define('_ROOT_IMG_PACK', dirname(__DIR__).'/prive/'._NOM_IMG_PACK); |
|
| 72 | 72 | |
| 73 | 73 | if (!defined('_JAVASCRIPT')) { |
| 74 | 74 | /** Nom du repertoire des bibliotheques JavaScript */ |
| 75 | 75 | define('_JAVASCRIPT', 'javascript/'); |
| 76 | 76 | } // utilisable avec #CHEMIN et find_in_path |
| 77 | 77 | /** le nom du repertoire des bibliotheques JavaScript du prive */ |
| 78 | -define('_DIR_JAVASCRIPT', (_DIR_RACINE . 'prive/' . _JAVASCRIPT)); |
|
| 78 | +define('_DIR_JAVASCRIPT', (_DIR_RACINE.'prive/'._JAVASCRIPT)); |
|
| 79 | 79 | |
| 80 | 80 | # Le nom des 4 repertoires modifiables par les scripts lances par httpd |
| 81 | 81 | # Par defaut ces 4 noms seront suffixes par _DIR_RACINE (cf plus bas) |
@@ -106,8 +106,8 @@ discard block |
||
| 106 | 106 | |
| 107 | 107 | // Son emplacement absolu si on le trouve |
| 108 | 108 | if ( |
| 109 | - @file_exists($f = _ROOT_RACINE . _NOM_PERMANENTS_INACCESSIBLES . _NOM_CONFIG . '.php') |
|
| 110 | - or (@file_exists($f = _ROOT_RESTREINT . _NOM_CONFIG . '.php')) |
|
| 109 | + @file_exists($f = _ROOT_RACINE._NOM_PERMANENTS_INACCESSIBLES._NOM_CONFIG.'.php') |
|
| 110 | + or (@file_exists($f = _ROOT_RESTREINT._NOM_CONFIG.'.php')) |
|
| 111 | 111 | ) { |
| 112 | 112 | /** Emplacement absolu du fichier d'option */ |
| 113 | 113 | define('_FILE_OPTIONS', $f); |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | // inclure l'ecran de securite |
| 132 | 132 | if ( |
| 133 | 133 | !defined('_ECRAN_SECURITE') |
| 134 | - and @file_exists($f = _ROOT_RACINE . _NOM_PERMANENTS_INACCESSIBLES . 'ecran_securite.php') |
|
| 134 | + and @file_exists($f = _ROOT_RACINE._NOM_PERMANENTS_INACCESSIBLES.'ecran_securite.php') |
|
| 135 | 135 | ) { |
| 136 | 136 | include $f; |
| 137 | 137 | } |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | // UA plus cibles |
| 153 | 153 | . '80legs|accoona|AltaVista|ASPSeek|Baidu|Charlotte|EC2LinkFinder|eStyle|facebook|flipboard|hootsuite|FunWebProducts|Google|Genieo|INA dlweb|InfegyAtlas|Java VM|LiteFinder|Lycos|MetaURI|Moreover|Rambler|Scooter|ScrubbyBloglines|Yahoo|Yeti' |
| 154 | 154 | . ',i', |
| 155 | - (string)$_SERVER['HTTP_USER_AGENT'] |
|
| 155 | + (string) $_SERVER['HTTP_USER_AGENT'] |
|
| 156 | 156 | ) |
| 157 | 157 | ); |
| 158 | 158 | } |
@@ -394,7 +394,7 @@ discard block |
||
| 394 | 394 | # la matrice standard (fichiers definissant les fonctions a inclure) |
| 395 | 395 | $spip_matrice = []; |
| 396 | 396 | # les plugins a activer |
| 397 | -$plugins = []; // voir le contenu du repertoire /plugins/ |
|
| 397 | +$plugins = []; // voir le contenu du repertoire /plugins/ |
|
| 398 | 398 | # les surcharges de include_spip() |
| 399 | 399 | $surcharges = []; // format 'inc_truc' => '/plugins/chose/inc_truc2.php' |
| 400 | 400 | |
@@ -472,8 +472,8 @@ discard block |
||
| 472 | 472 | // |
| 473 | 473 | // Charger les fonctions liees aux serveurs Http et Sql. |
| 474 | 474 | // |
| 475 | -require_once _ROOT_RESTREINT . 'inc/utils.php'; |
|
| 476 | -require_once _ROOT_RESTREINT . 'base/connect_sql.php'; |
|
| 475 | +require_once _ROOT_RESTREINT.'inc/utils.php'; |
|
| 476 | +require_once _ROOT_RESTREINT.'base/connect_sql.php'; |
|
| 477 | 477 | |
| 478 | 478 | // Definition personnelles eventuelles |
| 479 | 479 | |
@@ -496,10 +496,10 @@ discard block |
||
| 496 | 496 | // ===> on execute en neutralisant les messages d'erreur |
| 497 | 497 | |
| 498 | 498 | spip_initialisation_core( |
| 499 | - (_DIR_RACINE . _NOM_PERMANENTS_INACCESSIBLES), |
|
| 500 | - (_DIR_RACINE . _NOM_PERMANENTS_ACCESSIBLES), |
|
| 501 | - (_DIR_RACINE . _NOM_TEMPORAIRES_INACCESSIBLES), |
|
| 502 | - (_DIR_RACINE . _NOM_TEMPORAIRES_ACCESSIBLES) |
|
| 499 | + (_DIR_RACINE._NOM_PERMANENTS_INACCESSIBLES), |
|
| 500 | + (_DIR_RACINE._NOM_PERMANENTS_ACCESSIBLES), |
|
| 501 | + (_DIR_RACINE._NOM_TEMPORAIRES_INACCESSIBLES), |
|
| 502 | + (_DIR_RACINE._NOM_TEMPORAIRES_ACCESSIBLES) |
|
| 503 | 503 | ); |
| 504 | 504 | |
| 505 | 505 | |
@@ -555,7 +555,7 @@ discard block |
||
| 555 | 555 | include_spip('inc/lang'); |
| 556 | 556 | utiliser_langue_visiteur(); |
| 557 | 557 | include_spip('inc/minipres'); |
| 558 | - echo minipres(_T('info_travaux_titre'), "<p style='text-align: center;'>" . _T('info_travaux_texte') . '</p>', ['status' => 503]); |
|
| 558 | + echo minipres(_T('info_travaux_titre'), "<p style='text-align: center;'>"._T('info_travaux_texte').'</p>', ['status' => 503]); |
|
| 559 | 559 | exit; |
| 560 | 560 | } |
| 561 | 561 | // autrement c'est une install ad hoc (spikini...), on sait pas faire |
@@ -594,12 +594,12 @@ discard block |
||
| 594 | 594 | } |
| 595 | 595 | if (!isset($GLOBALS['spip_header_silencieux']) or !$GLOBALS['spip_header_silencieux']) { |
| 596 | 596 | include_spip('inc/filtres_mini'); |
| 597 | - header(_HEADER_COMPOSED_BY . " $spip_version_affichee @ www.spip.net + " . url_absolue(_DIR_VAR . 'config.txt')); |
|
| 597 | + header(_HEADER_COMPOSED_BY." $spip_version_affichee @ www.spip.net + ".url_absolue(_DIR_VAR.'config.txt')); |
|
| 598 | 598 | } else { |
| 599 | 599 | // header minimal |
| 600 | - header(_HEADER_COMPOSED_BY . ' @ www.spip.net'); |
|
| 600 | + header(_HEADER_COMPOSED_BY.' @ www.spip.net'); |
|
| 601 | 601 | } |
| 602 | 602 | } |
| 603 | 603 | |
| 604 | 604 | $methode = ($_SERVER['REQUEST_METHOD'] ?? ((php_sapi_name() == 'cli') ? 'cli' : '')); |
| 605 | -spip_log($methode . ' ' . self() . ' - ' . _FILE_CONNECT, _LOG_DEBUG); |
|
| 605 | +spip_log($methode.' '.self().' - '._FILE_CONNECT, _LOG_DEBUG); |
|
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | = spip_connect_db($adresse_db, $port, $login_db, $pass_db, '', $server_db); |
| 44 | 44 | |
| 45 | 45 | $GLOBALS['connexions'][$server_db][$GLOBALS['spip_sql_version']] |
| 46 | - = $GLOBALS['spip_' . $server_db . '_functions_' . $GLOBALS['spip_sql_version']]; |
|
| 46 | + = $GLOBALS['spip_'.$server_db.'_functions_'.$GLOBALS['spip_sql_version']]; |
|
| 47 | 47 | |
| 48 | 48 | $fquery = sql_serveur('query', $server_db); |
| 49 | 49 | if ($choix_db == 'new_spip') { |
@@ -53,13 +53,13 @@ discard block |
||
| 53 | 53 | if (!$ok) { |
| 54 | 54 | $re = "Impossible de creer la base $re"; |
| 55 | 55 | spip_log($re); |
| 56 | - return '<p>' . _T('avis_connexion_erreur_creer_base') . "</p><!--\n$re\n-->"; |
|
| 56 | + return '<p>'._T('avis_connexion_erreur_creer_base')."</p><!--\n$re\n-->"; |
|
| 57 | 57 | } |
| 58 | 58 | } else { |
| 59 | 59 | $re = "Le nom de la base doit correspondre a $re"; |
| 60 | 60 | spip_log($re); |
| 61 | 61 | |
| 62 | - return '<p>' . _T('avis_connexion_erreur_nom_base') . "</p><!--\n$re\n-->"; |
|
| 62 | + return '<p>'._T('avis_connexion_erreur_nom_base')."</p><!--\n$re\n-->"; |
|
| 63 | 63 | } |
| 64 | 64 | } |
| 65 | 65 | |
@@ -70,14 +70,14 @@ discard block |
||
| 70 | 70 | = spip_connect_db($adresse_db, $port, $login_db, $pass_db, $sel_db, $server_db); |
| 71 | 71 | |
| 72 | 72 | $GLOBALS['connexions'][$server_db][$GLOBALS['spip_sql_version']] |
| 73 | - = $GLOBALS['spip_' . $server_db . '_functions_' . $GLOBALS['spip_sql_version']]; |
|
| 73 | + = $GLOBALS['spip_'.$server_db.'_functions_'.$GLOBALS['spip_sql_version']]; |
|
| 74 | 74 | |
| 75 | 75 | // Completer le tableau decrivant la connexion |
| 76 | 76 | |
| 77 | 77 | $GLOBALS['connexions'][$server_db]['prefixe'] = $table_prefix; |
| 78 | 78 | $GLOBALS['connexions'][$server_db]['db'] = $sel_db; |
| 79 | 79 | |
| 80 | - $old = sql_showbase($table_prefix . '_meta', $server_db); |
|
| 80 | + $old = sql_showbase($table_prefix.'_meta', $server_db); |
|
| 81 | 81 | if ($old) { |
| 82 | 82 | $old = sql_fetch($old, $server_db); |
| 83 | 83 | } |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | $charset['charset']; |
| 97 | 97 | $charsetbase = $charset['charset']; |
| 98 | 98 | } else { |
| 99 | - spip_log(_DEFAULT_CHARSET . ' inconnu du serveur SQL'); |
|
| 99 | + spip_log(_DEFAULT_CHARSET.' inconnu du serveur SQL'); |
|
| 100 | 100 | $charsetbase = 'standard'; |
| 101 | 101 | } |
| 102 | 102 | spip_log("Creation des tables. Codage $charsetbase"); |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | if ($r) { |
| 147 | 147 | $r = sql_fetch($r, $server_db); |
| 148 | 148 | } |
| 149 | - $version_installee = !$r ? 0 : (double)$r['valeur']; |
|
| 149 | + $version_installee = !$r ? 0 : (double) $r['valeur']; |
|
| 150 | 150 | if (!$version_installee or ($GLOBALS['spip_version_base'] < $version_installee)) { |
| 151 | 151 | $fupdateq( |
| 152 | 152 | 'spip_meta', |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | '', |
| 156 | 156 | $server_db |
| 157 | 157 | ); |
| 158 | - spip_log('nouvelle version installee: ' . $GLOBALS['spip_version_base']); |
|
| 158 | + spip_log('nouvelle version installee: '.$GLOBALS['spip_version_base']); |
|
| 159 | 159 | } |
| 160 | 160 | // eliminer la derniere operation d'admin mal terminee |
| 161 | 161 | // notamment la mise a jour |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | if ($chmod_db) { |
| 183 | 183 | install_fichier_connexion( |
| 184 | 184 | _FILE_CHMOD_TMP, |
| 185 | - "if (!defined('_SPIP_CHMOD')) define('_SPIP_CHMOD', " . sprintf('0%3o', $chmod_db) . ");\n" |
|
| 185 | + "if (!defined('_SPIP_CHMOD')) define('_SPIP_CHMOD', ".sprintf('0%3o', $chmod_db).");\n" |
|
| 186 | 186 | ); |
| 187 | 187 | } |
| 188 | 188 | |
@@ -245,16 +245,16 @@ discard block |
||
| 245 | 245 | |
| 246 | 246 | |
| 247 | 247 | function install_premier_auteur($email, $login, $nom, #[\SensitiveParameter] $pass, $hidden, $auteur_obligatoire) { |
| 248 | - return info_progression_etape(3, 'etape_', 'install/') . |
|
| 248 | + return info_progression_etape(3, 'etape_', 'install/'). |
|
| 249 | 249 | info_etape( |
| 250 | 250 | _T('info_informations_personnelles'), |
| 251 | - '<b>' . _T('texte_informations_personnelles_1') . '</b>' . |
|
| 252 | - aider('install5', true) . |
|
| 253 | - '<p>' . |
|
| 251 | + '<b>'._T('texte_informations_personnelles_1').'</b>'. |
|
| 252 | + aider('install5', true). |
|
| 253 | + '<p>'. |
|
| 254 | 254 | ($auteur_obligatoire ? |
| 255 | 255 | '' |
| 256 | 256 | : |
| 257 | - _T('texte_informations_personnelles_2') . ' ' . _T('info_laisser_champs_vides') |
|
| 257 | + _T('texte_informations_personnelles_2').' '._T('info_laisser_champs_vides') |
|
| 258 | 258 | ) |
| 259 | 259 | ) |
| 260 | 260 | . generer_form_ecrire('install', ( |
@@ -264,12 +264,12 @@ discard block |
||
| 264 | 264 | _T('info_identification_publique'), |
| 265 | 265 | [ |
| 266 | 266 | 'nom' => [ |
| 267 | - 'label' => '<b>' . _T('entree_signature') . "</b><br />\n" . _T('entree_nom_pseudo_1') . "\n", |
|
| 267 | + 'label' => '<b>'._T('entree_signature')."</b><br />\n"._T('entree_nom_pseudo_1')."\n", |
|
| 268 | 268 | 'valeur' => $nom, |
| 269 | 269 | 'required' => $auteur_obligatoire, |
| 270 | 270 | ], |
| 271 | 271 | 'email' => [ |
| 272 | - 'label' => '<b>' . _T('entree_adresse_email') . "</b>\n", |
|
| 272 | + 'label' => '<b>'._T('entree_adresse_email')."</b>\n", |
|
| 273 | 273 | 'valeur' => $email, |
| 274 | 274 | ] |
| 275 | 275 | ] |
@@ -279,23 +279,23 @@ discard block |
||
| 279 | 279 | _T('entree_identifiants_connexion'), |
| 280 | 280 | [ |
| 281 | 281 | 'login' => [ |
| 282 | - 'label' => '<b>' . _T('entree_login') . "</b><br />\n" . _T( |
|
| 282 | + 'label' => '<b>'._T('entree_login')."</b><br />\n"._T( |
|
| 283 | 283 | 'info_login_trop_court_car_pluriel', |
| 284 | 284 | ['nb' => _LOGIN_TROP_COURT] |
| 285 | - ) . "\n", |
|
| 285 | + )."\n", |
|
| 286 | 286 | 'valeur' => $login, |
| 287 | 287 | 'required' => $auteur_obligatoire, |
| 288 | 288 | ], |
| 289 | 289 | 'pass' => [ |
| 290 | - 'label' => '<b>' . _T('entree_mot_passe') . "</b><br />\n" . _T( |
|
| 290 | + 'label' => '<b>'._T('entree_mot_passe')."</b><br />\n"._T( |
|
| 291 | 291 | 'info_passe_trop_court_car_pluriel', |
| 292 | 292 | ['nb' => _PASS_LONGUEUR_MINI] |
| 293 | - ) . "\n", |
|
| 293 | + )."\n", |
|
| 294 | 294 | 'valeur' => $pass, |
| 295 | 295 | 'required' => $auteur_obligatoire, |
| 296 | 296 | ], |
| 297 | 297 | 'pass_verif' => [ |
| 298 | - 'label' => '<b>' . _T('info_confirmer_passe') . "</b><br />\n", |
|
| 298 | + 'label' => '<b>'._T('info_confirmer_passe')."</b><br />\n", |
|
| 299 | 299 | 'valeur' => $pass, |
| 300 | 300 | 'required' => $auteur_obligatoire, |
| 301 | 301 | ] |
@@ -339,9 +339,9 @@ discard block |
||
| 339 | 339 | |
| 340 | 340 | if ($res) { |
| 341 | 341 | $res = info_progression_etape(2, 'etape_', 'install/', true) |
| 342 | - . "<div class='error'><h3>" . _T('avis_operation_echec') . '</h3>' |
|
| 342 | + . "<div class='error'><h3>"._T('avis_operation_echec').'</h3>' |
|
| 343 | 343 | . $res |
| 344 | - . '<p>' . _T('texte_operation_echec') . '</p>' |
|
| 344 | + . '<p>'._T('texte_operation_echec').'</p>' |
|
| 345 | 345 | . '</div>'; |
| 346 | 346 | } |
| 347 | 347 | } else { |
@@ -365,7 +365,7 @@ discard block |
||
| 365 | 365 | |
| 366 | 366 | $hidden = predef_ou_cache($adresse_db, $login_db, $pass_db, $server_db) |
| 367 | 367 | . (defined('_INSTALL_NAME_DB') ? '' |
| 368 | - : "\n<input type='hidden' name='sel_db' value=\"" . spip_htmlspecialchars($sel_db) . '" />'); |
|
| 368 | + : "\n<input type='hidden' name='sel_db' value=\"".spip_htmlspecialchars($sel_db).'" />'); |
|
| 369 | 369 | |
| 370 | 370 | $auteur_obligatoire = ($ldap_present ? 0 : !sql_countsel('spip_auteurs', '', '', '', $server_db)); |
| 371 | 371 | |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | \***************************************************************************/ |
| 11 | 11 | |
| 12 | 12 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 13 | - return; |
|
| 13 | + return; |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | include_spip('inc/headers'); |
@@ -18,199 +18,199 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | function install_bases($adresse_db, $login_db, $pass_db, $server_db, $choix_db, $sel_db, $chmod_db) { |
| 20 | 20 | |
| 21 | - // Prefix des tables : |
|
| 22 | - // S'il n'est pas defini par mes_options/inc/mutualiser, on va le creer |
|
| 23 | - // a partir de ce qui est envoye a l'installation |
|
| 24 | - if (!defined('_INSTALL_TABLE_PREFIX')) { |
|
| 25 | - $table_prefix = ($GLOBALS['table_prefix'] != 'spip') |
|
| 26 | - ? $GLOBALS['table_prefix'] |
|
| 27 | - : preparer_prefixe_tables(_request('tprefix')); |
|
| 28 | - // S'il est vide on remet spip |
|
| 29 | - if (!$table_prefix) { |
|
| 30 | - $table_prefix = 'spip'; |
|
| 31 | - } |
|
| 32 | - } else { |
|
| 33 | - $table_prefix = _INSTALL_TABLE_PREFIX; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - if (preg_match(',(.*):(.*),', $adresse_db, $r)) { |
|
| 37 | - [, $adresse_db, $port] = $r; |
|
| 38 | - } else { |
|
| 39 | - $port = ''; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - $GLOBALS['connexions'][$server_db] |
|
| 43 | - = spip_connect_db($adresse_db, $port, $login_db, $pass_db, '', $server_db); |
|
| 44 | - |
|
| 45 | - $GLOBALS['connexions'][$server_db][$GLOBALS['spip_sql_version']] |
|
| 46 | - = $GLOBALS['spip_' . $server_db . '_functions_' . $GLOBALS['spip_sql_version']]; |
|
| 47 | - |
|
| 48 | - $fquery = sql_serveur('query', $server_db); |
|
| 49 | - if ($choix_db == 'new_spip') { |
|
| 50 | - $re = ',^[a-z_][a-z_0-9-]*$,i'; |
|
| 51 | - if (preg_match($re, $sel_db)) { |
|
| 52 | - $ok = sql_create_base($sel_db, $server_db); |
|
| 53 | - if (!$ok) { |
|
| 54 | - $re = "Impossible de creer la base $re"; |
|
| 55 | - spip_log($re); |
|
| 56 | - return '<p>' . _T('avis_connexion_erreur_creer_base') . "</p><!--\n$re\n-->"; |
|
| 57 | - } |
|
| 58 | - } else { |
|
| 59 | - $re = "Le nom de la base doit correspondre a $re"; |
|
| 60 | - spip_log($re); |
|
| 61 | - |
|
| 62 | - return '<p>' . _T('avis_connexion_erreur_nom_base') . "</p><!--\n$re\n-->"; |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - // on rejoue la connexion apres avoir teste si il faut lui indiquer |
|
| 67 | - // un sql_mode |
|
| 68 | - install_mode_appel($server_db, false); |
|
| 69 | - $GLOBALS['connexions'][$server_db] |
|
| 70 | - = spip_connect_db($adresse_db, $port, $login_db, $pass_db, $sel_db, $server_db); |
|
| 71 | - |
|
| 72 | - $GLOBALS['connexions'][$server_db][$GLOBALS['spip_sql_version']] |
|
| 73 | - = $GLOBALS['spip_' . $server_db . '_functions_' . $GLOBALS['spip_sql_version']]; |
|
| 74 | - |
|
| 75 | - // Completer le tableau decrivant la connexion |
|
| 76 | - |
|
| 77 | - $GLOBALS['connexions'][$server_db]['prefixe'] = $table_prefix; |
|
| 78 | - $GLOBALS['connexions'][$server_db]['db'] = $sel_db; |
|
| 79 | - |
|
| 80 | - $old = sql_showbase($table_prefix . '_meta', $server_db); |
|
| 81 | - if ($old) { |
|
| 82 | - $old = sql_fetch($old, $server_db); |
|
| 83 | - } |
|
| 84 | - if (!$old) { |
|
| 85 | - // Si possible, demander au serveur d'envoyer les textes |
|
| 86 | - // dans le codage std de SPIP, |
|
| 87 | - $charset = sql_get_charset(_DEFAULT_CHARSET, $server_db); |
|
| 88 | - |
|
| 89 | - if ($charset) { |
|
| 90 | - sql_set_charset($charset['charset'], $server_db); |
|
| 91 | - $GLOBALS['meta']['charset_sql_base'] = |
|
| 92 | - $charset['charset']; |
|
| 93 | - $GLOBALS['meta']['charset_collation_sql_base'] = |
|
| 94 | - $charset['collation']; |
|
| 95 | - $GLOBALS['meta']['charset_sql_connexion'] = |
|
| 96 | - $charset['charset']; |
|
| 97 | - $charsetbase = $charset['charset']; |
|
| 98 | - } else { |
|
| 99 | - spip_log(_DEFAULT_CHARSET . ' inconnu du serveur SQL'); |
|
| 100 | - $charsetbase = 'standard'; |
|
| 101 | - } |
|
| 102 | - spip_log("Creation des tables. Codage $charsetbase"); |
|
| 103 | - creer_base($server_db); // AT LAST |
|
| 104 | - // memoriser avec quel charset on l'a creee |
|
| 105 | - |
|
| 106 | - if ($charset) { |
|
| 107 | - $t = [ |
|
| 108 | - 'nom' => 'charset_sql_base', |
|
| 109 | - 'valeur' => $charset['charset'], |
|
| 110 | - 'impt' => 'non' |
|
| 111 | - ]; |
|
| 112 | - @sql_insertq('spip_meta', $t, [], $server_db); |
|
| 113 | - $t['nom'] = 'charset_collation_sql_base'; |
|
| 114 | - $t['valeur'] = $charset['collation']; |
|
| 115 | - @sql_insertq('spip_meta', $t, [], $server_db); |
|
| 116 | - $t['nom'] = 'charset_sql_connexion'; |
|
| 117 | - $t['valeur'] = $charset['charset']; |
|
| 118 | - @sql_insertq('spip_meta', $t, [], $server_db); |
|
| 119 | - } |
|
| 120 | - $t = [ |
|
| 121 | - 'nom' => 'version_installee', |
|
| 122 | - 'valeur' => $GLOBALS['spip_version_base'], |
|
| 123 | - 'impt' => 'non' |
|
| 124 | - ]; |
|
| 125 | - @sql_insertq('spip_meta', $t, [], $server_db); |
|
| 126 | - $t['nom'] = 'nouvelle_install'; |
|
| 127 | - $t['valeur'] = 1; |
|
| 128 | - @sql_insertq('spip_meta', $t, [], $server_db); |
|
| 129 | - // positionner la langue par defaut du site si un cookie de lang a ete mis |
|
| 130 | - if (isset($_COOKIE['spip_lang_ecrire'])) { |
|
| 131 | - @sql_insertq( |
|
| 132 | - 'spip_meta', |
|
| 133 | - ['nom' => 'langue_site', 'valeur' => $_COOKIE['spip_lang_ecrire']], |
|
| 134 | - [], |
|
| 135 | - $server_db |
|
| 136 | - ); |
|
| 137 | - } |
|
| 138 | - } else { |
|
| 139 | - // pour recreer les tables disparues au besoin |
|
| 140 | - spip_log('Table des Meta deja la. Verification des autres.'); |
|
| 141 | - creer_base($server_db); |
|
| 142 | - $fupdateq = sql_serveur('updateq', $server_db); |
|
| 143 | - |
|
| 144 | - $r = $fquery("SELECT valeur FROM spip_meta WHERE nom='version_installee'", $server_db); |
|
| 145 | - |
|
| 146 | - if ($r) { |
|
| 147 | - $r = sql_fetch($r, $server_db); |
|
| 148 | - } |
|
| 149 | - $version_installee = !$r ? 0 : (double)$r['valeur']; |
|
| 150 | - if (!$version_installee or ($GLOBALS['spip_version_base'] < $version_installee)) { |
|
| 151 | - $fupdateq( |
|
| 152 | - 'spip_meta', |
|
| 153 | - ['valeur' => $GLOBALS['spip_version_base'], 'impt' => 'non'], |
|
| 154 | - "nom='version_installee'", |
|
| 155 | - '', |
|
| 156 | - $server_db |
|
| 157 | - ); |
|
| 158 | - spip_log('nouvelle version installee: ' . $GLOBALS['spip_version_base']); |
|
| 159 | - } |
|
| 160 | - // eliminer la derniere operation d'admin mal terminee |
|
| 161 | - // notamment la mise a jour |
|
| 162 | - @$fquery("DELETE FROM spip_meta WHERE nom='import_all' OR nom='admin'", $server_db); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - // recuperer le charset de la connexion dans les meta |
|
| 166 | - $charset = ''; |
|
| 167 | - $r = $fquery("SELECT valeur FROM spip_meta WHERE nom='charset_sql_connexion'", $server_db); |
|
| 168 | - if ($r) { |
|
| 169 | - $r = sql_fetch($r, $server_db); |
|
| 170 | - } |
|
| 171 | - if ($r) { |
|
| 172 | - $charset = $r['valeur']; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - $ligne_rappel = install_mode_appel($server_db); |
|
| 176 | - |
|
| 177 | - $result_ok = @$fquery('SELECT COUNT(*) FROM spip_meta', $server_db); |
|
| 178 | - if (!$result_ok) { |
|
| 179 | - return "<!--\nvielle = $old rappel= $ligne_rappel\n-->"; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - if ($chmod_db) { |
|
| 183 | - install_fichier_connexion( |
|
| 184 | - _FILE_CHMOD_TMP, |
|
| 185 | - "if (!defined('_SPIP_CHMOD')) define('_SPIP_CHMOD', " . sprintf('0%3o', $chmod_db) . ");\n" |
|
| 186 | - ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - // si ce fichier existe a cette etape c'est qu'il provient |
|
| 190 | - // d'une installation qui ne l'a pas cree correctement. |
|
| 191 | - // Le supprimer pour que _FILE_CONNECT_TMP prime. |
|
| 192 | - |
|
| 193 | - if (_FILE_CONNECT and file_exists(_FILE_CONNECT)) { |
|
| 194 | - spip_unlink(_FILE_CONNECT); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - install_fichier_connexion( |
|
| 198 | - _FILE_CONNECT_TMP, |
|
| 199 | - $ligne_rappel |
|
| 200 | - . install_connexion( |
|
| 201 | - $adresse_db, |
|
| 202 | - $port, |
|
| 203 | - $login_db, |
|
| 204 | - $pass_db, |
|
| 205 | - $sel_db, |
|
| 206 | - $server_db, |
|
| 207 | - $table_prefix, |
|
| 208 | - '', |
|
| 209 | - $charset |
|
| 210 | - ) |
|
| 211 | - ); |
|
| 212 | - |
|
| 213 | - return ''; |
|
| 21 | + // Prefix des tables : |
|
| 22 | + // S'il n'est pas defini par mes_options/inc/mutualiser, on va le creer |
|
| 23 | + // a partir de ce qui est envoye a l'installation |
|
| 24 | + if (!defined('_INSTALL_TABLE_PREFIX')) { |
|
| 25 | + $table_prefix = ($GLOBALS['table_prefix'] != 'spip') |
|
| 26 | + ? $GLOBALS['table_prefix'] |
|
| 27 | + : preparer_prefixe_tables(_request('tprefix')); |
|
| 28 | + // S'il est vide on remet spip |
|
| 29 | + if (!$table_prefix) { |
|
| 30 | + $table_prefix = 'spip'; |
|
| 31 | + } |
|
| 32 | + } else { |
|
| 33 | + $table_prefix = _INSTALL_TABLE_PREFIX; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + if (preg_match(',(.*):(.*),', $adresse_db, $r)) { |
|
| 37 | + [, $adresse_db, $port] = $r; |
|
| 38 | + } else { |
|
| 39 | + $port = ''; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + $GLOBALS['connexions'][$server_db] |
|
| 43 | + = spip_connect_db($adresse_db, $port, $login_db, $pass_db, '', $server_db); |
|
| 44 | + |
|
| 45 | + $GLOBALS['connexions'][$server_db][$GLOBALS['spip_sql_version']] |
|
| 46 | + = $GLOBALS['spip_' . $server_db . '_functions_' . $GLOBALS['spip_sql_version']]; |
|
| 47 | + |
|
| 48 | + $fquery = sql_serveur('query', $server_db); |
|
| 49 | + if ($choix_db == 'new_spip') { |
|
| 50 | + $re = ',^[a-z_][a-z_0-9-]*$,i'; |
|
| 51 | + if (preg_match($re, $sel_db)) { |
|
| 52 | + $ok = sql_create_base($sel_db, $server_db); |
|
| 53 | + if (!$ok) { |
|
| 54 | + $re = "Impossible de creer la base $re"; |
|
| 55 | + spip_log($re); |
|
| 56 | + return '<p>' . _T('avis_connexion_erreur_creer_base') . "</p><!--\n$re\n-->"; |
|
| 57 | + } |
|
| 58 | + } else { |
|
| 59 | + $re = "Le nom de la base doit correspondre a $re"; |
|
| 60 | + spip_log($re); |
|
| 61 | + |
|
| 62 | + return '<p>' . _T('avis_connexion_erreur_nom_base') . "</p><!--\n$re\n-->"; |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + // on rejoue la connexion apres avoir teste si il faut lui indiquer |
|
| 67 | + // un sql_mode |
|
| 68 | + install_mode_appel($server_db, false); |
|
| 69 | + $GLOBALS['connexions'][$server_db] |
|
| 70 | + = spip_connect_db($adresse_db, $port, $login_db, $pass_db, $sel_db, $server_db); |
|
| 71 | + |
|
| 72 | + $GLOBALS['connexions'][$server_db][$GLOBALS['spip_sql_version']] |
|
| 73 | + = $GLOBALS['spip_' . $server_db . '_functions_' . $GLOBALS['spip_sql_version']]; |
|
| 74 | + |
|
| 75 | + // Completer le tableau decrivant la connexion |
|
| 76 | + |
|
| 77 | + $GLOBALS['connexions'][$server_db]['prefixe'] = $table_prefix; |
|
| 78 | + $GLOBALS['connexions'][$server_db]['db'] = $sel_db; |
|
| 79 | + |
|
| 80 | + $old = sql_showbase($table_prefix . '_meta', $server_db); |
|
| 81 | + if ($old) { |
|
| 82 | + $old = sql_fetch($old, $server_db); |
|
| 83 | + } |
|
| 84 | + if (!$old) { |
|
| 85 | + // Si possible, demander au serveur d'envoyer les textes |
|
| 86 | + // dans le codage std de SPIP, |
|
| 87 | + $charset = sql_get_charset(_DEFAULT_CHARSET, $server_db); |
|
| 88 | + |
|
| 89 | + if ($charset) { |
|
| 90 | + sql_set_charset($charset['charset'], $server_db); |
|
| 91 | + $GLOBALS['meta']['charset_sql_base'] = |
|
| 92 | + $charset['charset']; |
|
| 93 | + $GLOBALS['meta']['charset_collation_sql_base'] = |
|
| 94 | + $charset['collation']; |
|
| 95 | + $GLOBALS['meta']['charset_sql_connexion'] = |
|
| 96 | + $charset['charset']; |
|
| 97 | + $charsetbase = $charset['charset']; |
|
| 98 | + } else { |
|
| 99 | + spip_log(_DEFAULT_CHARSET . ' inconnu du serveur SQL'); |
|
| 100 | + $charsetbase = 'standard'; |
|
| 101 | + } |
|
| 102 | + spip_log("Creation des tables. Codage $charsetbase"); |
|
| 103 | + creer_base($server_db); // AT LAST |
|
| 104 | + // memoriser avec quel charset on l'a creee |
|
| 105 | + |
|
| 106 | + if ($charset) { |
|
| 107 | + $t = [ |
|
| 108 | + 'nom' => 'charset_sql_base', |
|
| 109 | + 'valeur' => $charset['charset'], |
|
| 110 | + 'impt' => 'non' |
|
| 111 | + ]; |
|
| 112 | + @sql_insertq('spip_meta', $t, [], $server_db); |
|
| 113 | + $t['nom'] = 'charset_collation_sql_base'; |
|
| 114 | + $t['valeur'] = $charset['collation']; |
|
| 115 | + @sql_insertq('spip_meta', $t, [], $server_db); |
|
| 116 | + $t['nom'] = 'charset_sql_connexion'; |
|
| 117 | + $t['valeur'] = $charset['charset']; |
|
| 118 | + @sql_insertq('spip_meta', $t, [], $server_db); |
|
| 119 | + } |
|
| 120 | + $t = [ |
|
| 121 | + 'nom' => 'version_installee', |
|
| 122 | + 'valeur' => $GLOBALS['spip_version_base'], |
|
| 123 | + 'impt' => 'non' |
|
| 124 | + ]; |
|
| 125 | + @sql_insertq('spip_meta', $t, [], $server_db); |
|
| 126 | + $t['nom'] = 'nouvelle_install'; |
|
| 127 | + $t['valeur'] = 1; |
|
| 128 | + @sql_insertq('spip_meta', $t, [], $server_db); |
|
| 129 | + // positionner la langue par defaut du site si un cookie de lang a ete mis |
|
| 130 | + if (isset($_COOKIE['spip_lang_ecrire'])) { |
|
| 131 | + @sql_insertq( |
|
| 132 | + 'spip_meta', |
|
| 133 | + ['nom' => 'langue_site', 'valeur' => $_COOKIE['spip_lang_ecrire']], |
|
| 134 | + [], |
|
| 135 | + $server_db |
|
| 136 | + ); |
|
| 137 | + } |
|
| 138 | + } else { |
|
| 139 | + // pour recreer les tables disparues au besoin |
|
| 140 | + spip_log('Table des Meta deja la. Verification des autres.'); |
|
| 141 | + creer_base($server_db); |
|
| 142 | + $fupdateq = sql_serveur('updateq', $server_db); |
|
| 143 | + |
|
| 144 | + $r = $fquery("SELECT valeur FROM spip_meta WHERE nom='version_installee'", $server_db); |
|
| 145 | + |
|
| 146 | + if ($r) { |
|
| 147 | + $r = sql_fetch($r, $server_db); |
|
| 148 | + } |
|
| 149 | + $version_installee = !$r ? 0 : (double)$r['valeur']; |
|
| 150 | + if (!$version_installee or ($GLOBALS['spip_version_base'] < $version_installee)) { |
|
| 151 | + $fupdateq( |
|
| 152 | + 'spip_meta', |
|
| 153 | + ['valeur' => $GLOBALS['spip_version_base'], 'impt' => 'non'], |
|
| 154 | + "nom='version_installee'", |
|
| 155 | + '', |
|
| 156 | + $server_db |
|
| 157 | + ); |
|
| 158 | + spip_log('nouvelle version installee: ' . $GLOBALS['spip_version_base']); |
|
| 159 | + } |
|
| 160 | + // eliminer la derniere operation d'admin mal terminee |
|
| 161 | + // notamment la mise a jour |
|
| 162 | + @$fquery("DELETE FROM spip_meta WHERE nom='import_all' OR nom='admin'", $server_db); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + // recuperer le charset de la connexion dans les meta |
|
| 166 | + $charset = ''; |
|
| 167 | + $r = $fquery("SELECT valeur FROM spip_meta WHERE nom='charset_sql_connexion'", $server_db); |
|
| 168 | + if ($r) { |
|
| 169 | + $r = sql_fetch($r, $server_db); |
|
| 170 | + } |
|
| 171 | + if ($r) { |
|
| 172 | + $charset = $r['valeur']; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + $ligne_rappel = install_mode_appel($server_db); |
|
| 176 | + |
|
| 177 | + $result_ok = @$fquery('SELECT COUNT(*) FROM spip_meta', $server_db); |
|
| 178 | + if (!$result_ok) { |
|
| 179 | + return "<!--\nvielle = $old rappel= $ligne_rappel\n-->"; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + if ($chmod_db) { |
|
| 183 | + install_fichier_connexion( |
|
| 184 | + _FILE_CHMOD_TMP, |
|
| 185 | + "if (!defined('_SPIP_CHMOD')) define('_SPIP_CHMOD', " . sprintf('0%3o', $chmod_db) . ");\n" |
|
| 186 | + ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + // si ce fichier existe a cette etape c'est qu'il provient |
|
| 190 | + // d'une installation qui ne l'a pas cree correctement. |
|
| 191 | + // Le supprimer pour que _FILE_CONNECT_TMP prime. |
|
| 192 | + |
|
| 193 | + if (_FILE_CONNECT and file_exists(_FILE_CONNECT)) { |
|
| 194 | + spip_unlink(_FILE_CONNECT); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + install_fichier_connexion( |
|
| 198 | + _FILE_CONNECT_TMP, |
|
| 199 | + $ligne_rappel |
|
| 200 | + . install_connexion( |
|
| 201 | + $adresse_db, |
|
| 202 | + $port, |
|
| 203 | + $login_db, |
|
| 204 | + $pass_db, |
|
| 205 | + $sel_db, |
|
| 206 | + $server_db, |
|
| 207 | + $table_prefix, |
|
| 208 | + '', |
|
| 209 | + $charset |
|
| 210 | + ) |
|
| 211 | + ); |
|
| 212 | + |
|
| 213 | + return ''; |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | /** |
@@ -225,166 +225,166 @@ discard block |
||
| 225 | 225 | * @return string Le préfixe corrigé |
| 226 | 226 | */ |
| 227 | 227 | function preparer_prefixe_tables($prefixe) { |
| 228 | - return trim(preg_replace(',^[0-9]+,', '', preg_replace(',[^a-z0-9],', '', strtolower($prefixe)))); |
|
| 228 | + return trim(preg_replace(',^[0-9]+,', '', preg_replace(',[^a-z0-9],', '', strtolower($prefixe)))); |
|
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | function install_propose_ldap() { |
| 232 | - return generer_form_ecrire('install', ( |
|
| 233 | - fieldset( |
|
| 234 | - _T('info_authentification_externe'), |
|
| 235 | - [ |
|
| 236 | - 'etape' => [ |
|
| 237 | - 'label' => _T('texte_annuaire_ldap_1'), |
|
| 238 | - 'valeur' => 'ldap1', |
|
| 239 | - 'hidden' => true |
|
| 240 | - ] |
|
| 241 | - ], |
|
| 242 | - bouton_suivant(_T('bouton_acces_ldap')) |
|
| 243 | - ))); |
|
| 232 | + return generer_form_ecrire('install', ( |
|
| 233 | + fieldset( |
|
| 234 | + _T('info_authentification_externe'), |
|
| 235 | + [ |
|
| 236 | + 'etape' => [ |
|
| 237 | + 'label' => _T('texte_annuaire_ldap_1'), |
|
| 238 | + 'valeur' => 'ldap1', |
|
| 239 | + 'hidden' => true |
|
| 240 | + ] |
|
| 241 | + ], |
|
| 242 | + bouton_suivant(_T('bouton_acces_ldap')) |
|
| 243 | + ))); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | |
| 247 | 247 | function install_premier_auteur($email, $login, $nom, #[\SensitiveParameter] $pass, $hidden, $auteur_obligatoire) { |
| 248 | - return info_progression_etape(3, 'etape_', 'install/') . |
|
| 249 | - info_etape( |
|
| 250 | - _T('info_informations_personnelles'), |
|
| 251 | - '<b>' . _T('texte_informations_personnelles_1') . '</b>' . |
|
| 252 | - aider('install5', true) . |
|
| 253 | - '<p>' . |
|
| 254 | - ($auteur_obligatoire ? |
|
| 255 | - '' |
|
| 256 | - : |
|
| 257 | - _T('texte_informations_personnelles_2') . ' ' . _T('info_laisser_champs_vides') |
|
| 258 | - ) |
|
| 259 | - ) |
|
| 260 | - . generer_form_ecrire('install', ( |
|
| 261 | - "\n<input type='hidden' name='etape' value='3b' />" |
|
| 262 | - . $hidden |
|
| 263 | - . fieldset( |
|
| 264 | - _T('info_identification_publique'), |
|
| 265 | - [ |
|
| 266 | - 'nom' => [ |
|
| 267 | - 'label' => '<b>' . _T('entree_signature') . "</b><br />\n" . _T('entree_nom_pseudo_1') . "\n", |
|
| 268 | - 'valeur' => $nom, |
|
| 269 | - 'required' => $auteur_obligatoire, |
|
| 270 | - ], |
|
| 271 | - 'email' => [ |
|
| 272 | - 'label' => '<b>' . _T('entree_adresse_email') . "</b>\n", |
|
| 273 | - 'valeur' => $email, |
|
| 274 | - ] |
|
| 275 | - ] |
|
| 276 | - ) |
|
| 277 | - |
|
| 278 | - . fieldset( |
|
| 279 | - _T('entree_identifiants_connexion'), |
|
| 280 | - [ |
|
| 281 | - 'login' => [ |
|
| 282 | - 'label' => '<b>' . _T('entree_login') . "</b><br />\n" . _T( |
|
| 283 | - 'info_login_trop_court_car_pluriel', |
|
| 284 | - ['nb' => _LOGIN_TROP_COURT] |
|
| 285 | - ) . "\n", |
|
| 286 | - 'valeur' => $login, |
|
| 287 | - 'required' => $auteur_obligatoire, |
|
| 288 | - ], |
|
| 289 | - 'pass' => [ |
|
| 290 | - 'label' => '<b>' . _T('entree_mot_passe') . "</b><br />\n" . _T( |
|
| 291 | - 'info_passe_trop_court_car_pluriel', |
|
| 292 | - ['nb' => _PASS_LONGUEUR_MINI] |
|
| 293 | - ) . "\n", |
|
| 294 | - 'valeur' => $pass, |
|
| 295 | - 'required' => $auteur_obligatoire, |
|
| 296 | - ], |
|
| 297 | - 'pass_verif' => [ |
|
| 298 | - 'label' => '<b>' . _T('info_confirmer_passe') . "</b><br />\n", |
|
| 299 | - 'valeur' => $pass, |
|
| 300 | - 'required' => $auteur_obligatoire, |
|
| 301 | - ] |
|
| 302 | - ] |
|
| 303 | - ) |
|
| 304 | - . bouton_suivant())); |
|
| 248 | + return info_progression_etape(3, 'etape_', 'install/') . |
|
| 249 | + info_etape( |
|
| 250 | + _T('info_informations_personnelles'), |
|
| 251 | + '<b>' . _T('texte_informations_personnelles_1') . '</b>' . |
|
| 252 | + aider('install5', true) . |
|
| 253 | + '<p>' . |
|
| 254 | + ($auteur_obligatoire ? |
|
| 255 | + '' |
|
| 256 | + : |
|
| 257 | + _T('texte_informations_personnelles_2') . ' ' . _T('info_laisser_champs_vides') |
|
| 258 | + ) |
|
| 259 | + ) |
|
| 260 | + . generer_form_ecrire('install', ( |
|
| 261 | + "\n<input type='hidden' name='etape' value='3b' />" |
|
| 262 | + . $hidden |
|
| 263 | + . fieldset( |
|
| 264 | + _T('info_identification_publique'), |
|
| 265 | + [ |
|
| 266 | + 'nom' => [ |
|
| 267 | + 'label' => '<b>' . _T('entree_signature') . "</b><br />\n" . _T('entree_nom_pseudo_1') . "\n", |
|
| 268 | + 'valeur' => $nom, |
|
| 269 | + 'required' => $auteur_obligatoire, |
|
| 270 | + ], |
|
| 271 | + 'email' => [ |
|
| 272 | + 'label' => '<b>' . _T('entree_adresse_email') . "</b>\n", |
|
| 273 | + 'valeur' => $email, |
|
| 274 | + ] |
|
| 275 | + ] |
|
| 276 | + ) |
|
| 277 | + |
|
| 278 | + . fieldset( |
|
| 279 | + _T('entree_identifiants_connexion'), |
|
| 280 | + [ |
|
| 281 | + 'login' => [ |
|
| 282 | + 'label' => '<b>' . _T('entree_login') . "</b><br />\n" . _T( |
|
| 283 | + 'info_login_trop_court_car_pluriel', |
|
| 284 | + ['nb' => _LOGIN_TROP_COURT] |
|
| 285 | + ) . "\n", |
|
| 286 | + 'valeur' => $login, |
|
| 287 | + 'required' => $auteur_obligatoire, |
|
| 288 | + ], |
|
| 289 | + 'pass' => [ |
|
| 290 | + 'label' => '<b>' . _T('entree_mot_passe') . "</b><br />\n" . _T( |
|
| 291 | + 'info_passe_trop_court_car_pluriel', |
|
| 292 | + ['nb' => _PASS_LONGUEUR_MINI] |
|
| 293 | + ) . "\n", |
|
| 294 | + 'valeur' => $pass, |
|
| 295 | + 'required' => $auteur_obligatoire, |
|
| 296 | + ], |
|
| 297 | + 'pass_verif' => [ |
|
| 298 | + 'label' => '<b>' . _T('info_confirmer_passe') . "</b><br />\n", |
|
| 299 | + 'valeur' => $pass, |
|
| 300 | + 'required' => $auteur_obligatoire, |
|
| 301 | + ] |
|
| 302 | + ] |
|
| 303 | + ) |
|
| 304 | + . bouton_suivant())); |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | function install_etape_3_dist() { |
| 308 | - $ldap_present = _request('ldap_present'); |
|
| 309 | - |
|
| 310 | - if (!$ldap_present) { |
|
| 311 | - $adresse_db = defined('_INSTALL_HOST_DB') |
|
| 312 | - ? _INSTALL_HOST_DB |
|
| 313 | - : _request('adresse_db'); |
|
| 314 | - |
|
| 315 | - $login_db = defined('_INSTALL_USER_DB') |
|
| 316 | - ? _INSTALL_USER_DB |
|
| 317 | - : _request('login_db'); |
|
| 318 | - |
|
| 319 | - $pass_db = defined('_INSTALL_PASS_DB') |
|
| 320 | - ? _INSTALL_PASS_DB |
|
| 321 | - : _request('pass_db'); |
|
| 322 | - |
|
| 323 | - $server_db = defined('_INSTALL_SERVER_DB') |
|
| 324 | - ? _INSTALL_SERVER_DB |
|
| 325 | - : _request('server_db'); |
|
| 326 | - |
|
| 327 | - $chmod_db = defined('_SPIP_CHMOD') |
|
| 328 | - ? _SPIP_CHMOD |
|
| 329 | - : _request('chmod'); |
|
| 330 | - |
|
| 331 | - $choix_db = defined('_INSTALL_NAME_DB') |
|
| 332 | - ? _INSTALL_NAME_DB |
|
| 333 | - : _request('choix_db'); |
|
| 334 | - |
|
| 335 | - $sel_db = ($choix_db == 'new_spip') |
|
| 336 | - ? _request('table_new') : $choix_db; |
|
| 337 | - |
|
| 338 | - $res = install_bases($adresse_db, $login_db, $pass_db, $server_db, $choix_db, $sel_db, $chmod_db); |
|
| 339 | - |
|
| 340 | - if ($res) { |
|
| 341 | - $res = info_progression_etape(2, 'etape_', 'install/', true) |
|
| 342 | - . "<div class='error'><h3>" . _T('avis_operation_echec') . '</h3>' |
|
| 343 | - . $res |
|
| 344 | - . '<p>' . _T('texte_operation_echec') . '</p>' |
|
| 345 | - . '</div>'; |
|
| 346 | - } |
|
| 347 | - } else { |
|
| 348 | - $res = ''; |
|
| 349 | - [$adresse_db, $login_db, $pass_db, $sel_db, $server_db] = analyse_fichier_connection(_FILE_CONNECT_TMP); |
|
| 350 | - $GLOBALS['connexions'][$server_db] = spip_connect_db($adresse_db, $sel_db, $login_db, $pass_db, $sel_db, $server_db); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - if (!$res) { |
|
| 354 | - if (file_exists(_FILE_CONNECT_TMP)) { |
|
| 355 | - include(_FILE_CONNECT_TMP); |
|
| 356 | - } else { |
|
| 357 | - redirige_url_ecrire('install'); |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - if (file_exists(_FILE_CHMOD_TMP)) { |
|
| 361 | - include(_FILE_CHMOD_TMP); |
|
| 362 | - } else { |
|
| 363 | - redirige_url_ecrire('install'); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - $hidden = predef_ou_cache($adresse_db, $login_db, $pass_db, $server_db) |
|
| 367 | - . (defined('_INSTALL_NAME_DB') ? '' |
|
| 368 | - : "\n<input type='hidden' name='sel_db' value=\"" . spip_htmlspecialchars($sel_db) . '" />'); |
|
| 369 | - |
|
| 370 | - $auteur_obligatoire = ($ldap_present ? 0 : !sql_countsel('spip_auteurs', '', '', '', $server_db)); |
|
| 371 | - |
|
| 372 | - $res = "<div class='success'><b>" |
|
| 373 | - . _T('info_base_installee') |
|
| 374 | - . '</b></div>' |
|
| 375 | - . install_premier_auteur( |
|
| 376 | - _request('email'), |
|
| 377 | - _request('login'), |
|
| 378 | - _request('nom'), |
|
| 379 | - _request('pass'), |
|
| 380 | - $hidden, |
|
| 381 | - $auteur_obligatoire |
|
| 382 | - ) |
|
| 383 | - . (($ldap_present or !function_exists('ldap_connect')) |
|
| 384 | - ? '' : install_propose_ldap()); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - |
|
| 388 | - $minipage = new Spip\Afficher\Minipage\Installation(); |
|
| 389 | - echo $minipage->page($res); |
|
| 308 | + $ldap_present = _request('ldap_present'); |
|
| 309 | + |
|
| 310 | + if (!$ldap_present) { |
|
| 311 | + $adresse_db = defined('_INSTALL_HOST_DB') |
|
| 312 | + ? _INSTALL_HOST_DB |
|
| 313 | + : _request('adresse_db'); |
|
| 314 | + |
|
| 315 | + $login_db = defined('_INSTALL_USER_DB') |
|
| 316 | + ? _INSTALL_USER_DB |
|
| 317 | + : _request('login_db'); |
|
| 318 | + |
|
| 319 | + $pass_db = defined('_INSTALL_PASS_DB') |
|
| 320 | + ? _INSTALL_PASS_DB |
|
| 321 | + : _request('pass_db'); |
|
| 322 | + |
|
| 323 | + $server_db = defined('_INSTALL_SERVER_DB') |
|
| 324 | + ? _INSTALL_SERVER_DB |
|
| 325 | + : _request('server_db'); |
|
| 326 | + |
|
| 327 | + $chmod_db = defined('_SPIP_CHMOD') |
|
| 328 | + ? _SPIP_CHMOD |
|
| 329 | + : _request('chmod'); |
|
| 330 | + |
|
| 331 | + $choix_db = defined('_INSTALL_NAME_DB') |
|
| 332 | + ? _INSTALL_NAME_DB |
|
| 333 | + : _request('choix_db'); |
|
| 334 | + |
|
| 335 | + $sel_db = ($choix_db == 'new_spip') |
|
| 336 | + ? _request('table_new') : $choix_db; |
|
| 337 | + |
|
| 338 | + $res = install_bases($adresse_db, $login_db, $pass_db, $server_db, $choix_db, $sel_db, $chmod_db); |
|
| 339 | + |
|
| 340 | + if ($res) { |
|
| 341 | + $res = info_progression_etape(2, 'etape_', 'install/', true) |
|
| 342 | + . "<div class='error'><h3>" . _T('avis_operation_echec') . '</h3>' |
|
| 343 | + . $res |
|
| 344 | + . '<p>' . _T('texte_operation_echec') . '</p>' |
|
| 345 | + . '</div>'; |
|
| 346 | + } |
|
| 347 | + } else { |
|
| 348 | + $res = ''; |
|
| 349 | + [$adresse_db, $login_db, $pass_db, $sel_db, $server_db] = analyse_fichier_connection(_FILE_CONNECT_TMP); |
|
| 350 | + $GLOBALS['connexions'][$server_db] = spip_connect_db($adresse_db, $sel_db, $login_db, $pass_db, $sel_db, $server_db); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + if (!$res) { |
|
| 354 | + if (file_exists(_FILE_CONNECT_TMP)) { |
|
| 355 | + include(_FILE_CONNECT_TMP); |
|
| 356 | + } else { |
|
| 357 | + redirige_url_ecrire('install'); |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + if (file_exists(_FILE_CHMOD_TMP)) { |
|
| 361 | + include(_FILE_CHMOD_TMP); |
|
| 362 | + } else { |
|
| 363 | + redirige_url_ecrire('install'); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + $hidden = predef_ou_cache($adresse_db, $login_db, $pass_db, $server_db) |
|
| 367 | + . (defined('_INSTALL_NAME_DB') ? '' |
|
| 368 | + : "\n<input type='hidden' name='sel_db' value=\"" . spip_htmlspecialchars($sel_db) . '" />'); |
|
| 369 | + |
|
| 370 | + $auteur_obligatoire = ($ldap_present ? 0 : !sql_countsel('spip_auteurs', '', '', '', $server_db)); |
|
| 371 | + |
|
| 372 | + $res = "<div class='success'><b>" |
|
| 373 | + . _T('info_base_installee') |
|
| 374 | + . '</b></div>' |
|
| 375 | + . install_premier_auteur( |
|
| 376 | + _request('email'), |
|
| 377 | + _request('login'), |
|
| 378 | + _request('nom'), |
|
| 379 | + _request('pass'), |
|
| 380 | + $hidden, |
|
| 381 | + $auteur_obligatoire |
|
| 382 | + ) |
|
| 383 | + . (($ldap_present or !function_exists('ldap_connect')) |
|
| 384 | + ? '' : install_propose_ldap()); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + |
|
| 388 | + $minipage = new Spip\Afficher\Minipage\Installation(); |
|
| 389 | + echo $minipage->page($res); |
|
| 390 | 390 | } |