]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - inc/poche/Database.class.php
new fields in database, reading time / date and domain name are stored
[github/wallabag/wallabag.git] / inc / poche / Database.class.php
index 02e8be8ba042fd71d3ed78baebb5d56441d8b656..a222ed39b76017fc8b758b2d486b429f5e645f92 100755 (executable)
@@ -398,6 +398,21 @@ class Database {
         return $query;
     }
 
+
+    private function _getDomain($url)
+    {
+        return parse_url($url, PHP_URL_HOST);
+    }
+
+    private function _getReadingTime($text) {
+        $word = str_word_count(strip_tags($text));
+        $minutes = floor($word / 200);
+        $seconds = floor($word % 200 / (200 / 60));
+        $time = array('minutes' => $minutes, 'seconds' => $seconds);
+
+        return $minutes;
+    }
+
     /**
      *
      * @param string $url
@@ -407,8 +422,12 @@ class Database {
      * @return integer $id of inserted record
      */
     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);
+        $readingTime = $this->_getReadingTime($content);
+        $domainName  = $this->_getDomain($url);
+        $date        = date('Y-m-d H:i:s');
+
+        $sql_action = 'INSERT INTO entries ( url, title, content, user_id, is_fav, is_read, date, reading_time, domain_name ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
+        $params_action = array($url, $title, $content, $user_id, $isFavorite, $isRead, $date, $readingTime, $domainName);
         if ( !$this->executeQuery($sql_action, $params_action) ) {
           $id = null;
         }
@@ -487,7 +506,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();
 
@@ -512,22 +531,23 @@ class Database {
         return $query;
     }
     
-    public function cleanUnusedTags() {
-        $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id";
-        $query = $this->executeQuery($sql_action,array());
+    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";
-        $query = $this->executeQuery($sql_action,array());
+        $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)) {
-                //delete tag
                 $sql_action = "DELETE FROM tags WHERE id=?";
                 $params_action = array($tag[0]);
-                $query = $this->executeQuery($sql_action, $params_action);
-                return $query;
+                $this->executeQuery($sql_action, $params_action);
+                return true;
             }
         }
+        
     }
 
     public function retrieveTagByValue($value) {