summaryrefslogtreecommitdiff
path: root/lib/minz/dao
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2013-12-15 03:30:24 +0100
committerGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2013-12-15 03:30:24 +0100
commit878e96202e8a22e4857b98e29b0a1fce68eccbc9 (patch)
treef9233c3b48a0cd6e0ac2536ddcc1897201595ad4 /lib/minz/dao
parent4af233e1f736eb2256e5e1696418635165467855 (diff)
Grosse refactorisation pour permettre le chargement automatique des classes
C'est parti de changements pour https://github.com/marienfressinaud/FreshRSS/issues/255 et finalement j'ai continué la refactorisation... Ajout de préfixes FreshRSS_ et Minz_ sur le modèle de SimplePie_. Toutes les classes sont maintenant en chargement automatique (devrait améliorer les performances en évitant de charger plein de classes inutilisées, et faciliter la maintenance). Suppression de set_include_path(). Si souhaité, certaines classes de Minz pourraient être déplacées dans un sous-répertoire, par exemple les exceptions. Tests et relecture nécessaires.
Diffstat (limited to 'lib/minz/dao')
-rwxr-xr-xlib/minz/dao/Model_array.php122
-rwxr-xr-xlib/minz/dao/Model_pdo.php111
-rwxr-xr-xlib/minz/dao/Model_txt.php84
3 files changed, 0 insertions, 317 deletions
diff --git a/lib/minz/dao/Model_array.php b/lib/minz/dao/Model_array.php
deleted file mode 100755
index 0b9ccf071..000000000
--- a/lib/minz/dao/Model_array.php
+++ /dev/null
@@ -1,122 +0,0 @@
-<?php
-/**
- * MINZ - Copyright 2011 Marien Fressinaud
- * Sous licence AGPL3 <http://www.gnu.org/licenses/>
-*/
-
-/**
- * La classe Model_array représente le modèle interragissant avec les fichiers de type texte gérant des tableaux php
- */
-class Model_array extends Model_txt {
- /**
- * $array Le tableau php contenu dans le fichier $nameFile
- */
- protected $array = array ();
-
- /**
- * Ouvre le fichier indiqué, charge le tableau dans $array et le $nameFile
- * @param $nameFile le nom du fichier à ouvrir contenant un tableau
- * Remarque : $array sera obligatoirement un tableau
- */
- public function __construct ($nameFile) {
- parent::__construct ($nameFile);
-
- if (!$this->getLock ('read')) {
- throw new PermissionDeniedException ($this->filename);
- } else {
- $this->array = include ($this->filename);
- $this->releaseLock ();
-
- if (!is_array ($this->array)) {
- $this->array = array ();
- }
-
- $this->array = $this->decodeArray ($this->array);
- }
- }
-
- /**
- * Écrit un tableau dans le fichier $nameFile
- * @param $array le tableau php à enregistrer
- **/
- public function writeFile ($array) {
- if (!$this->getLock ('write')) {
- throw new PermissionDeniedException ($this->namefile);
- } else {
- $this->erase ();
-
- $this->writeLine ('<?php');
- $this->writeLine ('return ', false);
- $this->writeArray ($array);
- $this->writeLine (';');
-
- $this->releaseLock ();
- }
- }
-
- private function writeArray ($array, $profondeur = 0) {
- $tab = '';
- for ($i = 0; $i < $profondeur; $i++) {
- $tab .= "\t";
- }
- $this->writeLine ('array (');
-
- foreach ($array as $key => $value) {
- if (is_int ($key)) {
- $this->writeLine ($tab . "\t" . $key . ' => ', false);
- } else {
- $this->writeLine ($tab . "\t" . '\'' . $key . '\'' . ' => ', false);
- }
-
- if (is_array ($value)) {
- $this->writeArray ($value, $profondeur + 1);
- $this->writeLine (',');
- } else {
- if (is_numeric ($value)) {
- $this->writeLine ($value . ',');
- } else {
- $this->writeLine ('\'' . addslashes ($value) . '\',');
- }
- }
- }
-
- $this->writeLine ($tab . ')', false);
- }
-
- private function decodeArray ($array) {
- $new_array = array ();
-
- foreach ($array as $key => $value) {
- if (is_array ($value)) {
- $new_array[$key] = $this->decodeArray ($value);
- } else {
- $new_array[$key] = stripslashes ($value);
- }
- }
-
- return $new_array;
- }
-
- private function getLock ($type) {
- if ($type == 'write') {
- $lock = LOCK_EX;
- } else {
- $lock = LOCK_SH;
- }
-
- $count = 1;
- while (!flock ($this->file, $lock) && $count <= 50) {
- $count++;
- }
-
- if ($count >= 50) {
- return false;
- } else {
- return true;
- }
- }
-
- private function releaseLock () {
- flock ($this->file, LOCK_UN);
- }
-}
diff --git a/lib/minz/dao/Model_pdo.php b/lib/minz/dao/Model_pdo.php
deleted file mode 100755
index a93291fc8..000000000
--- a/lib/minz/dao/Model_pdo.php
+++ /dev/null
@@ -1,111 +0,0 @@
-<?php
-/**
- * MINZ - Copyright 2011 Marien Fressinaud
- * Sous licence AGPL3 <http://www.gnu.org/licenses/>
-*/
-
-/**
- * La classe Model_sql représente le modèle interragissant avec les bases de données
- * Seul la connexion MySQL est prise en charge pour le moment
- */
-class Model_pdo {
-
- /**
- * Partage la connexion à la base de données entre toutes les instances.
- */
- public static $useSharedBd = true;
- private static $sharedBd = null;
- private static $sharedPrefix;
-
- /**
- * $bd variable représentant la base de données
- */
- protected $bd;
-
- protected $prefix;
-
- /**
- * Créé la connexion à la base de données à l'aide des variables
- * HOST, BASE, USER et PASS définies dans le fichier de configuration
- */
- public function __construct () {
- if (self::$useSharedBd && self::$sharedBd != null) {
- $this->bd = self::$sharedBd;
- $this->prefix = self::$sharedPrefix;
- return;
- }
-
- $db = Configuration::dataBase ();
- $driver_options = null;
-
- try {
- $type = $db['type'];
- if($type == 'mysql') {
- $string = $type
- . ':host=' . $db['host']
- . ';dbname=' . $db['base']
- . ';charset=utf8';
- $driver_options = array(
- PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
- );
- } elseif($type == 'sqlite') {
- $string = $type . ':/' . DATA_PATH . $db['base'] . '.sqlite'; //TODO: DEBUG UTF-8 http://www.siteduzero.com/forum/sujet/sqlite-connexion-utf-8-18797
- }
-
- $this->bd = new FreshPDO (
- $string,
- $db['user'],
- $db['password'],
- $driver_options
- );
- self::$sharedBd = $this->bd;
-
- $userPrefix = Configuration::currentUser ();
- $this->prefix = $db['prefix'] . (empty($userPrefix) ? '' : ($userPrefix . '_'));
- self::$sharedPrefix = $this->prefix;
- } catch (Exception $e) {
- throw new PDOConnectionException (
- $string,
- $db['user'], MinzException::ERROR
- );
- }
- }
-
- public function beginTransaction() {
- $this->bd->beginTransaction();
- }
- public function commit() {
- $this->bd->commit();
- }
- public function rollBack() {
- $this->bd->rollBack();
- }
-
- public function size() {
- $db = Configuration::dataBase ();
- $sql = 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema = ?';
- $stm = $this->bd->prepare ($sql);
- $values = array ($db['base']);
- $stm->execute ($values);
- $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
- return $res[0];
- }
-}
-
-class FreshPDO extends PDO {
- private static function check($statement) {
- if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) {
- invalidateHttpCache();
- }
- }
-
- public function prepare ($statement, $driver_options = array()) {
- FreshPDO::check($statement);
- return parent::prepare($statement, $driver_options);
- }
-
- public function exec ($statement) {
- FreshPDO::check($statement);
- return parent::exec($statement);
- }
-}
diff --git a/lib/minz/dao/Model_txt.php b/lib/minz/dao/Model_txt.php
deleted file mode 100755
index aed653068..000000000
--- a/lib/minz/dao/Model_txt.php
+++ /dev/null
@@ -1,84 +0,0 @@
-<?php
-/**
- * MINZ - Copyright 2011 Marien Fressinaud
- * Sous licence AGPL3 <http://www.gnu.org/licenses/>
-*/
-
-/**
- * La classe Model_txt représente le modèle interragissant avec les fichiers de type texte
- */
-class Model_txt {
- /**
- * $file représente le fichier à ouvrir
- */
- protected $file;
-
- /**
- * $filename est le nom du fichier
- */
- protected $filename;
-
- /**
- * Ouvre un fichier dans $file
- * @param $nameFile nom du fichier à ouvrir
- * @param $mode mode d'ouverture du fichier ('a+' par défaut)
- * @exception FileNotExistException si le fichier n'existe pas
- * > ou ne peux pas être ouvert
- */
- public function __construct ($nameFile, $mode = 'a+') {
- $this->filename = $nameFile;
- if (!file_exists($this->filename)) {
- throw new FileNotExistException (
- $this->filename,
- MinzException::WARNING
- );
- }
-
- $this->file = @fopen ($this->filename, $mode);
-
- if (!$this->file) {
- throw new PermissionDeniedException (
- $this->filename,
- MinzException::WARNING
- );
- }
- }
-
- /**
- * Lit une ligne de $file
- * @return une ligne du fichier
- */
- public function readLine () {
- return fgets ($this->file);
- }
-
- /**
- * Écrit une ligne dans $file
- * @param $line la ligne à écrire
- */
- public function writeLine ($line, $newLine = true) {
- $char = '';
- if ($newLine) {
- $char = "\n";
- }
-
- fwrite ($this->file, $line . $char);
- }
-
- /**
- * Efface le fichier $file
- * @return true en cas de succès, false sinon
- */
- public function erase () {
- return ftruncate ($this->file, 0);
- }
-
- /**
- * Ferme $file
- */
- public function __destruct () {
- if (isset ($this->file)) {
- fclose ($this->file);
- }
- }
-}