]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - inc/poche/Database.class.php
Revert "Add SHAARLI support to view template"
[github/wallabag/wallabag.git] / inc / poche / Database.class.php
index a226b31e999cfb5d10f3bf4cdca1aac6f5cb7294..84916b83efe3cebd2fc6d9f42d03ed327068b71e 100644 (file)
@@ -9,25 +9,27 @@
  */
 
 class Database {
-
-    #postgresql
-    public static $db_path = 'pgsql:host=localhost;dbname=poche';
-    public static $user = 'postgres';
-    public static $password = 'postgres';
-    #sqlite
-    // public static $db_path = 'sqlite:./db/poche.sqlite';
-    // public static $user = '';
-    // public static $password = '';
-    #mysql
-    // public static $db_path = 'mysql:host=localhost;dbname=poche';
-    // public static $user = 'root';
-    // public static $password = 'root';
-
     var $handle;
 
-    function __construct() {
-        $this->handle = new PDO(self::$db_path, self::$user, self::$password);
+    function __construct()
+    {
+        switch (STORAGE) {
+            case 'sqlite':
+                $db_path = 'sqlite:' . STORAGE_SQLITE;
+                $this->handle = new PDO($db_path);
+                break;
+            case 'mysql':
+                $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB;
+                $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); 
+                break;
+            case 'postgres':
+                $db_path = 'pgsql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB;
+                $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); 
+                break;
+        }
+
         $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+        Tools::logm('storage type ' . STORAGE);
     }
 
     private function getHandle() {
@@ -35,19 +37,34 @@ class Database {
     }
 
     public function isInstalled() {
-        $sql = "SELECT username FROM users WHERE id=?";
-        $query = $this->executeQuery($sql, array('1'));
-        $hasAdmin = $query->fetchAll();
+        $sql = "SELECT username FROM users";
+        $query = $this->executeQuery($sql, array());
+        $hasAdmin = count($query->fetchAll());
 
-        if (count($hasAdmin) == 0) 
+        if ($hasAdmin == 0) 
             return FALSE;
 
         return TRUE;
     }
 
     public function install($login, $password) {
-        $sql = 'INSERT INTO users ( username, password ) VALUES (?, ?)';
-        $params = array($login, $password);
+        $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)';
+        $params = array($login, $password, $login, ' ');
+        $query = $this->executeQuery($sql, $params);
+
+        $sequence = '';
+        if (STORAGE == 'postgres') {
+            $sequence = 'users_id_seq';
+        }
+
+        $id_user = intval($this->getLastId($sequence));
+
+        $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
+        $params = array($id_user, 'pager', '10');
+        $query = $this->executeQuery($sql, $params);
+
+        $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
+        $params = array($id_user, 'language', 'en_EN.UTF8');
         $query = $this->executeQuery($sql, $params);
 
         return TRUE;
@@ -167,6 +184,13 @@ class Database {
         return $entries;
     }
 
+    public function updateContent($id, $content, $user_id) {
+        $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?';
+        $params_action = array($content, $id, $user_id);
+        $query = $this->executeQuery($sql_action, $params_action);
+        return $query;
+    }
+
     public function add($url, $title, $content, $user_id) {
         $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)';
         $params_action = array($url, $title, $content, $user_id);
@@ -193,7 +217,7 @@ class Database {
         $query          = $this->executeQuery($sql_action, $params_action);
     }
 
-    public function getLastId() {
-        return $this->getHandle()->lastInsertId();
+    public function getLastId($column = '') {
+        return $this->getHandle()->lastInsertId($column);
     }
 }