diff --git a/src/library/database.php b/src/library/database.php index 86c263c..a6e986b 100644 --- a/src/library/database.php +++ b/src/library/database.php @@ -494,6 +494,15 @@ class Database { return $query->fetch(); } + public function getUsers() { + + $this->_debug->query->select->total++; + + $query = $this->_db->query('SELECT * FROM `user`'); + + return $query->fetchAll(); + } + public function getUsersTotal() : int { $this->_debug->query->select->total++; @@ -639,7 +648,18 @@ class Database { return $query->fetch()->result; } - public function getMagnetsTotalByUserId(int $userId) : int { + public function findMagnetsByUserId(int $userId) { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT * FROM `magnet` WHERE `userId` = ?'); + + $query->execute([$userId]); + + return $query->fetchAll(); + } + + public function findMagnetsTotalByUserId(int $userId) : int { $this->_debug->query->select->total++; @@ -1237,42 +1257,44 @@ class Database { return $query->rowCount(); } - public function getMagnetCommentsTotal(mixed $magnetId = null) : int { + public function getMagnetCommentsTotal() : int { $this->_debug->query->select->total++; - if ($magnetId) - { - $query = $this->_db->prepare('SELECT COUNT(*) AS `result` FROM `magnetComment` WHERE `magnetId` = ?'); - - $query->execute([$magnetId]); - } - else - { - $query = $this->_db->prepare('SELECT COUNT(*) AS `result` FROM `magnetComment`'); - - $query->execute(); - } + $query = $this->_db->query('SELECT COUNT(*) AS `result` FROM `magnetComment`'); return $query->fetch()->result; } - public function getMagnetCommentsTotalByUserId(int $userId, mixed $magnetId = null) : int { + public function findMagnetCommentsTotalByMagnetId(int $magnetId) : int { $this->_debug->query->select->total++; - if ($magnetId) - { - $query = $this->_db->prepare('SELECT COUNT(*) AS `result` FROM `magnetComment` WHERE `userId` = ? AND `magnetId` = ?'); + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `userId`) AS `result` FROM `magnetComment` WHERE `magnetId` = ?'); - $query->execute([$userId, $magnetId]); - } - else - { - $query = $this->_db->prepare('SELECT COUNT(*) AS `result` FROM `magnetComment` WHERE `userId` = ?'); + $query->execute([$magnetId]); - $query->execute([$userId]); - } + return $query->fetch()->result; + } + + public function findMagnetCommentsTotalByUserId(int $userId) : int { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `magnetId`) AS `result` FROM `magnetComment` WHERE `userId` = ?'); + + $query->execute([$userId]); + + return $query->fetch()->result; + } + + public function findMagnetCommentsTotal(int $userId, int $magnetId) : int { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT COUNT(*) AS `result` FROM `magnetComment` WHERE `userId` = ? AND `magnetId` = ?'); + + $query->execute([$userId, $magnetId]); return $query->fetch()->result; } @@ -1310,17 +1332,6 @@ class Database { return $query->fetchAll(); } - public function findMagnetCommentsTotalByUserId(int $magnetId, int $userId) : int { - - $this->_debug->query->select->total++; - - $query = $this->_db->prepare('SELECT COUNT(*) AS `result` FROM `magnetComment` WHERE `magnetId` = ? AND `userId` = ?'); - - $query->execute([$magnetId, $userId]); - - return $query->fetch()->result; - } - public function getMagnetComment(int $magnetCommentId) { $this->_debug->query->select->total++; @@ -1355,18 +1366,29 @@ class Database { return $query->rowCount(); } - public function getMagnetStarsTotal(int $magnetId) : int { + public function findMagnetStarsTotalByMagnetId(int $magnetId) : int { $this->_debug->query->select->total++; - $query = $this->_db->prepare('SELECT COUNT(*) AS `result` FROM `magnetStar` WHERE `magnetId` = ?'); + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `userId`) AS `result` FROM `magnetStar` WHERE `magnetId` = ?'); $query->execute([$magnetId]); return $query->fetch()->result; } - public function findMagnetStarsTotalByUserId(int $magnetId, int $userId) : int { + public function findMagnetStarsTotalByUserId(int $userId) : int { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `magnetId`) AS `result` FROM `magnetStar` WHERE `userId` = ?'); + + $query->execute([$userId]); + + return $query->fetch()->result; + } + + public function findMagnetStarsTotal(int $magnetId, int $userId) : int { $this->_debug->query->select->total++; @@ -1377,6 +1399,17 @@ class Database { return $query->fetch()->result; } + public function findMagnetStarByUserId(int $magnetId, int $userId) : int { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT * FROM `magnetStar` WHERE `magnetId` = ? AND `userId` = ?'); + + $query->execute([$magnetId, $userId]); + + return $query->fetch(); + } + // Magnet download public function addMagnetDownload(int $magnetId, int $userId, int $timeAdded) : int { @@ -1389,7 +1422,18 @@ class Database { return $this->_db->lastInsertId(); } - public function getMagnetDownloadsTotalByUserId(int $magnetId) : int { + public function findMagnetDownloadsTotal(int $magnetId, int $userId) : int { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT COUNT(*) AS `result` FROM `magnetDownload` WHERE `magnetId` = ? AND `userId` = ?'); + + $query->execute([$magnetId, $userId]); + + return $query->fetch()->result; + } + + public function findMagnetDownloadsTotalByMagnetId(int $magnetId) : int { $this->_debug->query->select->total++; @@ -1400,13 +1444,13 @@ class Database { return $query->fetch()->result; } - public function findMagnetDownloadsTotalByUserId(int $magnetId, int $userId) : int { + public function findMagnetDownloadsTotalByUserId(int $userId) : int { $this->_debug->query->select->total++; - $query = $this->_db->prepare('SELECT COUNT(*) AS `result` FROM `magnetDownload` WHERE `magnetId` = ? AND `userId` = ?'); + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `magnetId`) AS `result` FROM `magnetDownload` WHERE `userId` = ?'); - $query->execute([$magnetId, $userId]); + $query->execute([$userId]); return $query->fetch()->result; } @@ -1434,13 +1478,13 @@ class Database { return $query->fetch()->result; } - public function findMagnetViewsTotalByUserId(int $magnetId, int $userId) : int { + public function findMagnetViewsTotalByUserId(int $userId) : int { $this->_debug->query->select->total++; - $query = $this->_db->prepare('SELECT COUNT(*) AS `result` FROM `magnetView` WHERE `magnetId` = ? AND `userId` = ?'); + $query = $this->_db->prepare('SELECT COUNT(*) AS `result` FROM `magnetView` WHERE `userId` = ?'); - $query->execute([$magnetId, $userId]); + $query->execute([$userId]); return $query->fetch()->result; } diff --git a/src/public/action.php b/src/public/action.php index 82e8414..833a222 100644 --- a/src/public/action.php +++ b/src/public/action.php @@ -421,7 +421,7 @@ switch (isset($_GET['target']) ? urldecode($_GET['target']) : false) else { // Star exists, trigger delete - if ($db->findMagnetStarsTotalByUserId($magnet->magnetId, $userId)) + if ($db->findMagnetStarsTotal($magnet->magnetId, $userId)) { $db->deleteMagnetStarByUserId($magnet->magnetId, $userId); } diff --git a/src/public/index.php b/src/public/index.php index 51fe65f..27d14bc 100644 --- a/src/public/index.php +++ b/src/public/index.php @@ -107,12 +107,6 @@ else $accessRead = ($user->address == $db->getUser($magnet->userId)->address || in_array($user->address, MODERATOR_IP_LIST) || ($magnet->public && $magnet->approved)); $accessEdit = ($user->address == $db->getUser($magnet->userId)->address || in_array($user->address, MODERATOR_IP_LIST)); - // Update magnet viewed - if ($accessRead) - { - $db->addMagnetView($magnet->magnetId, $userId, time()); - } - // Keywords $keywords = []; @@ -139,18 +133,18 @@ else 'keywords' => $keywords, 'comment' => (object) [ - 'total' => $db->getMagnetCommentsTotal($magnet->magnetId), - 'status' => $db->findMagnetCommentsTotalByUserId($magnet->magnetId, $userId), + 'total' => $db->findMagnetCommentsTotalByMagnetId($magnet->magnetId), + 'status' => $db->findMagnetCommentsTotal($magnet->magnetId, $userId), ], 'download' => (object) [ - 'total' => $db->getMagnetDownloadsTotalByUserId($magnet->magnetId), - 'status' => $db->findMagnetDownloadsTotalByUserId($magnet->magnetId, $userId), + 'total' => $db->findMagnetDownloadsTotalByMagnetId($magnet->magnetId), + 'status' => $db->findMagnetDownloadsTotal($magnet->magnetId, $userId), ], 'star' => (object) [ - 'total' => $db->getMagnetStarsTotal($magnet->magnetId), - 'status' => $db->findMagnetStarsTotalByUserId($magnet->magnetId, $userId), + 'total' => $db->findMagnetStarsTotalByMagnetId($magnet->magnetId), + 'status' => $db->findMagnetStarsTotal($magnet->magnetId, $userId), ], 'access' => (object) [ diff --git a/src/public/magnet.php b/src/public/magnet.php index da3efcc..c725625 100644 --- a/src/public/magnet.php +++ b/src/public/magnet.php @@ -121,18 +121,18 @@ else 'keywords' => $keywords, 'comment' => (object) [ - 'total' => $db->getMagnetCommentsTotal($magnet->magnetId), - 'status' => $db->findMagnetCommentsTotalByUserId($magnet->magnetId, $userId), + 'total' => $db->findMagnetCommentsTotalByMagnetId($magnet->magnetId), + 'status' => $db->findMagnetCommentsTotal($magnet->magnetId, $userId), ], 'download' => (object) [ - 'total' => $db->getMagnetDownloadsTotalByUserId($magnet->magnetId), - 'status' => $db->findMagnetDownloadsTotalByUserId($magnet->magnetId, $userId), + 'total' => $db->findMagnetDownloadsTotalByMagnetId($magnet->magnetId), + 'status' => $db->findMagnetDownloadsTotal($magnet->magnetId, $userId), ], 'star' => (object) [ - 'total' => $db->getMagnetStarsTotal($magnet->magnetId), - 'status' => $db->findMagnetStarsTotalByUserId($magnet->magnetId, $userId), + 'total' => $db->findMagnetStarsTotalByMagnetId($magnet->magnetId), + 'status' => $db->findMagnetStarsTotal($magnet->magnetId, $userId), ], 'access' => (object) [ diff --git a/src/public/node.php b/src/public/node.php index edba7c8..c299268 100644 --- a/src/public/node.php +++ b/src/public/node.php @@ -121,13 +121,13 @@ else if (is_null($user->public))