X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=inc%2Fpoche%2FDatabase.class.php;h=7be7a394ae8887ba9c6030ba6d0b89c49fcc4494;hb=105ef359b7e7f147f9684f1695f3d9d2b962b5e0;hp=b5dd21203a9a667872303bc71a0b97498f862769;hpb=cf8a5e1eedbed484dbcb1ddc9f7a13fc19b7a27b;p=github%2Fwallabag%2Fwallabag.git diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php index b5dd2120..7be7a394 100755 --- a/inc/poche/Database.class.php +++ b/inc/poche/Database.class.php @@ -24,15 +24,22 @@ class Database { switch (STORAGE) { case 'sqlite': // Check if /db is writeable - if ( !is_writable(STORAGE_SQLITE) || !is_writable(dirname(STORAGE_SQLITE))) { + if ( !is_writable(STORAGE_SQLITE) || !is_writable(dirname(STORAGE_SQLITE))) { die('An error occured: "db" directory must be writeable for your web server user!'); } $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); + if (MYSQL_USE_UTF8MB4) { + $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB . ';charset=utf8mb4'; + $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD, array( + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4', + )); + } else { + $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; @@ -291,7 +298,7 @@ class Database { $sql_limit = "LIMIT ".$limit." OFFSET 0"; } - $sql = "SELECT * FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE 'Untitled - Import%' AND user_id=? ORDER BY id " . $sql_limit; + $sql = "SELECT * FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE '%Import%' AND user_id=? ORDER BY id " . $sql_limit; $query = $this->executeQuery($sql, array($user_id)); $entries = $query->fetchAll(); @@ -300,7 +307,7 @@ class Database { public function retrieveUnfetchedEntriesCount($user_id) { - $sql = "SELECT count(*) FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE 'Untitled - Import%' AND user_id=?"; + $sql = "SELECT count(*) FROM entries WHERE (content = '' OR content IS NULL) AND title LIKE '%Import%' AND user_id=?"; $query = $this->executeQuery($sql, array($user_id)); list($count) = $query->fetch(); @@ -316,6 +323,21 @@ class Database { return $entries; } + public function retrieveAllWithTags($user_id) + { + $entries = $this->retrieveAll($user_id); + $count = count($entries); + for ($i = 0; $i < $count; $i++) { + $tag_entries = $this->retrieveTagsByEntry($entries[$i]['id']); + $tags = []; + foreach ($tag_entries as $tag) { + $tags[] = $tag[1]; + } + $entries[$i]['tags'] = implode(',', $tags); + } + return $entries; + } + public function retrieveOneById($id, $user_id) { $entry = NULL; @@ -404,6 +426,48 @@ class Database { return $count; } + public function getRandomId($user_id, $view) { + $random = (STORAGE == 'mysql') ? 'RAND()' : 'RANDOM()'; + switch ($view) { + case 'archive': + $sql = "SELECT id FROM entries WHERE user_id=? AND is_read=? ORDER BY ". $random . " LIMIT 1"; + $params = array($user_id,1); + break; + case 'fav': + $sql = "SELECT id FROM entries WHERE user_id=? AND is_fav=? ORDER BY ". $random . " LIMIT 1"; + $params = array($user_id,1); + break; + default: + $sql = "SELECT id FROM entries WHERE user_id=? AND is_read=? ORDER BY ". $random . " LIMIT 1"; + $params = array($user_id,0); + break; + } + $query = $this->executeQuery($sql, $params); + $id = $query->fetchAll(); + + return $id; + } + + public function getPreviousArticle($id, $user_id) + { + $sql = "SELECT id FROM entries WHERE id = (SELECT max(id) FROM entries WHERE id < ? AND is_read=0) AND user_id=? AND is_read=0"; + $params = array($id, $user_id); + $query = $this->executeQuery($sql, $params); + $id_entry = $query->fetchAll(); + $id = $id_entry[0][0]; + return $id; + } + + public function getNextArticle($id, $user_id) + { + $sql = "SELECT id FROM entries WHERE id = (SELECT min(id) FROM entries WHERE id > ? AND is_read=0) AND user_id=? AND is_read=0"; + $params = array($id, $user_id); + $query = $this->executeQuery($sql, $params); + $id_entry = $query->fetchAll(); + $id = $id_entry[0][0]; + return $id; + } + public function updateContent($id, $content, $user_id) {