]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - inc/poche/Database.class.php
WHAT. A. BIG. REFACTOR. + new license (we moved to MIT one)
[github/wallabag/wallabag.git] / inc / poche / Database.class.php
index ba2d1d9438565b194914d216a120dd0fae7c2617..9c1c0286b4fd2feafebea8e04b94c83c2bc2be3f 100755 (executable)
@@ -5,7 +5,7 @@
  * @category   wallabag
  * @author     Nicolas LÅ“uillet <nicolas@loeuillet.org>
  * @copyright  2013
- * @license    http://www.wtfpl.net/ see COPYING file
+ * @license    http://opensource.org/licenses/MIT see COPYING file
  */
 
 class Database {
@@ -33,9 +33,12 @@ class Database {
                 $db_path = 'pgsql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB;
                 $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD);
                 break;
+            default:
+                die(STORAGE . ' is not a recognised database system !');
         }
 
         $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+        $this->_checkTags();
         Tools::logm('storage type ' . STORAGE);
     }
 
@@ -43,21 +46,7 @@ class Database {
         return $this->handle;
     }
 
-    public function isInstalled() {
-        $sql = "SELECT username FROM users";
-        $query = $this->executeQuery($sql, array());
-        if ($query == false) {
-            die(STORAGE . ' database looks empty. You have to create it (you can find database structure in install folder).');
-        }
-        $hasAdmin = count($query->fetchAll());
-
-        if ($hasAdmin == 0)
-            return false;
-
-        return true;
-    }
-
-    public function checkTags() {
+    private function _checkTags() {
 
         if (STORAGE == 'sqlite') {
             $sql = '
@@ -407,6 +396,7 @@ class Database {
     public function add($url, $title, $content, $user_id, $isFavorite=0, $isRead=0) {
         $sql_action = 'INSERT INTO entries ( url, title, content, user_id, is_fav, is_read ) VALUES (?, ?, ?, ?, ?, ?)';
         $params_action = array($url, $title, $content, $user_id, $isFavorite, $isRead);
+
         if ( !$this->executeQuery($sql_action, $params_action) ) {
           $id = null;
         }
@@ -485,7 +475,7 @@ class Database {
         $sql =
             "SELECT entries.* FROM entries
             LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
-            WHERE tags_entries.tag_id = ? AND entries.user_id=?";
+            WHERE tags_entries.tag_id = ? AND entries.user_id=? ORDER by entries.id DESC";
         $query = $this->executeQuery($sql, array($tag_id, $user_id));
         $entries = $query->fetchAll();
 
@@ -509,6 +499,25 @@ class Database {
         $query          = $this->executeQuery($sql_action, $params_action);
         return $query;
     }
+    
+    public function cleanUnusedTag($tag_id) {
+        $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
+        $query = $this->executeQuery($sql_action,array($tag_id));
+        $tagstokeep = $query->fetchAll();
+        $sql_action = "SELECT tags.* FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
+        $query = $this->executeQuery($sql_action,array($tag_id));
+        $alltags = $query->fetchAll();
+        
+        foreach ($alltags as $tag) {
+            if ($tag && !in_array($tag,$tagstokeep)) {
+                $sql_action = "DELETE FROM tags WHERE id=?";
+                $params_action = array($tag[0]);
+                $this->executeQuery($sql_action, $params_action);
+                return true;
+            }
+        }
+        
+    }
 
     public function retrieveTagByValue($value) {
         $tag  = NULL;