diff --git a/app/TGroup.php b/app/TGroup.php index 0a1e53f90..dd8e2ccf0 100644 --- a/app/TGroup.php +++ b/app/TGroup.php @@ -4,8 +4,9 @@ class TGroup extends BaseObject { - const CACHE_KEY = 'tg_%d'; - const CACHE_TLIST_KEY = 'tlist_%d'; + const CACHE_KEY = 'tg_%d'; + const CACHE_TLIST_KEY = 'tlist_%d'; + const CACHE_COVERART_KEY = 'tg_cover_%d'; const ARTIST_DISPLAY_TEXT = 1; const ARTIST_DISPLAY_HTML = 2; @@ -242,6 +243,57 @@ public function torrentTagList(): array { return $tag; } + public function addCoverArt(string $image, string $summary, int $userId, \Gazelle\Log $logger): int { + $this->db->prepared_query(" + INSERT IGNORE INTO cover_art + (GroupID, Image, Summary, UserID) + VALUES (?, ?, ?, ?) + ", $this->id, $image, $summary, $userId + ); + $n = $this->db->affected_rows(); + if ($n) { + $logger->group($this->id, $userId, "Additional cover \"$summary - $image\" added to group"); + $this->cache->delete_value(sprintf(self::CACHE_COVERART_KEY, $this->id)); + } + return $n; + } + + public function removeCoverArt(int $coverId, int $userId, \Gazelle\Log $logger): int { + [$image, $summary] = $this->db->row(" + SELECT Image, Summary + FROM cover_art + WHERE ID = ? + ", $coverId + ); + $this->db->prepared_query(" + DELETE FROM cover_art WHERE ID = ? + ", $coverId + ); + $n = $this->db->affected_rows(); + if ($n) { + $logger->group($this->id, $userId, "Additional cover \"$summary - $image\" removed from group"); + $this->cache->delete_value(sprintf(self::CACHE_COVERART_KEY, $this->id)); + } + return $n; + } + + public function coverArt(): array { + $key = sprintf(self::CACHE_COVERART_KEY, $this->id); + $list = $this->cache->get_value($key); + if ($list === false) { + $this->db->prepared_query(" + SELECT ID, Image, Summary, UserID, Time + FROM cover_art + WHERE GroupID = ? + ORDER BY Time ASC + ", $this->id + ); + $list = $this->db->to_array(false, MYSQLI_ASSOC, false); + $this->cache->cache_value($key, $list, 0); + } + return $list; + } + /** * Generate an HTML anchor or the name for an artist */ diff --git a/db/migrations/20210726154658_cover_art_now.php b/db/migrations/20210726154658_cover_art_now.php new file mode 100644 index 000000000..f5bc6b278 --- /dev/null +++ b/db/migrations/20210726154658_cover_art_now.php @@ -0,0 +1,25 @@ +table('cover_art') + ->changeColumn('Image', 'string', ['length' => 255, 'null' => false]) + ->changeColumn('Summary', 'string', ['length' => 100, 'null' => false]) + ->changeColumn('UserID', 'integer', ['length' => 10, 'null' => false]) + ->changeColumn('Time', 'datetime', ['null' => false, 'default' => 'CURRENT_TIMESTAMP']) + ->save(); + } + + public function down(): void { + $this->table('cover_art') + ->changeColumn('Image', 'string', ['length' => 255, 'null' => false, 'default' => '']) + ->changeColumn('Summary', 'string', ['length' => 100, 'null' => true]) + ->changeColumn('UserID', 'integer', ['length' => 10, 'null' => false, 'default' => 0]) + ->changeColumn('Time', 'datetime', ['null' => true]) + ->save(); + } +} diff --git a/sections/ajax/index.php b/sections/ajax/index.php index 64ac7cc50..7c5a57151 100644 --- a/sections/ajax/index.php +++ b/sections/ajax/index.php @@ -105,6 +105,9 @@ case 'torrentgroupalbumart': // so the album art script can function without breaking the ratelimit require('torrentgroupalbumart.php'); break; + case 'torrent_remove_cover_art': + require('torrent_remove_cover_art.php'); + break; case 'tcomments': require('tcomments.php'); break; diff --git a/sections/ajax/torrent_remove_cover_art.php b/sections/ajax/torrent_remove_cover_art.php new file mode 100644 index 000000000..7264ea0c2 --- /dev/null +++ b/sections/ajax/torrent_remove_cover_art.php @@ -0,0 +1,17 @@ +permitted('site_edit_wiki')) { + json_die('failure', 'forbidden'); +} +$tgroup = (new Gazelle\Manager\TGroup)->findById((int)$_GET['groupid']); +$coverId = (int)$_GET['id']; +if (!$coverId || is_null($tgroup)) { + json_die('failure', 'bad parameters'); +} + +if ($tgroup->removeCoverArt($coverId, $Viewer->id(), new Gazelle\Log)) { + json_print("success", ['id' => $coverId]); +} else { + json_die('failure', 'bad coverId'); +} diff --git a/sections/torrents/add_cover_art.php b/sections/torrents/add_cover_art.php index 52658005e..630dbbd15 100644 --- a/sections/torrents/add_cover_art.php +++ b/sections/torrents/add_cover_art.php @@ -1,44 +1,27 @@ permitted('site_edit_wiki')) { error(403); } +authorize(); -$GroupID = (int)$_POST['groupid']; -$Summaries = $_POST['summary']; -$Images = $_POST['image']; - -if (!$GroupID) { - error(0); +$tgroup = (new Gazelle\Manager\TGroup)->findById((int)$_POST['groupid']); +if (is_null($tgroup)) { + error(404); } - -if (count($Images) != count($Summaries)) { +$summaryList = $_POST['summary']; +$imageList = $_POST['image']; +if (count($imageList) != count($summaryList)) { error('Missing an image or a summary'); } -$Changed = false; -for ($i = 0; $i < count($Images); $i++) { - $Image = trim($Images[$i]); - if (ImageTools::blacklisted($Image, true) || !preg_match(IMAGE_REGEXP, $Image)) { +$logger = new Gazelle\Log; +for ($i = 0, $end = count($imageList); $i < $end; $i++) { + $image = trim($imageList[$i]); + if (ImageTools::blacklisted($image, true) || !preg_match(IMAGE_REGEXP, $image)) { continue; } - $Summary = trim($Summaries[$i]); - - $DB->prepared_query(" - INSERT IGNORE INTO cover_art - (GroupID, Image, Summary, UserID, Time) - VALUES (?, ?, ?, ?, now()) - ", $GroupID, $Image, $Summary, $Viewer->id() - ); - if ($DB->affected_rows()) { - $Changed = true; - (new Gazelle\Log)->group($GroupID, $Viewer->id(), "Additional cover \"$Summary - $Image\" added to group"); - } -} - -if ($Changed) { - $Cache->delete_value("torrents_cover_art_$GroupID"); + $tgroup->addCoverArt($image, trim($summaryList[$i]), $Viewer->id(), $logger); } -header("Location: " . $_SERVER['HTTP_REFERER'] ?? "torrents.php?id={$GroupID}"); +header("Location: " . redirectUrl("torrents.php?id=" . $tgroup->id())); diff --git a/sections/torrents/details.php b/sections/torrents/details.php index 0df5b1b88..ecffac411 100644 --- a/sections/torrents/details.php +++ b/sections/torrents/details.php @@ -7,6 +7,7 @@ } $GroupID = $tgroup->id(); $RevisionID = (int)($_GET['revisionid'] ?? 0); +$tgroup = (new Gazelle\Manager\TGroup)->findById($GroupID); [$TorrentDetails, $TorrentList] = get_group_info($GroupID, $RevisionID); @@ -45,19 +46,25 @@ $AltName .= " [$name] "; } -$CoverArt = $Cache->get_value("torrents_cover_art_$GroupID"); -if (!$CoverArt) { - $DB->prepared_query(' - SELECT ID, Image, Summary, UserID, Time - FROM cover_art - WHERE GroupID = ? - ORDER BY Time ASC', $GroupID); - $CoverArt = $DB->to_array(); - if ($DB->has_results()) { - $Cache->cache_value("torrents_cover_art_$GroupID", $CoverArt, 0); +$Tags = []; +if ($TorrentTags != '') { + $TorrentTags = explode('|', $TorrentTags); + $TorrentTagIDs = explode('|', $TorrentTagIDs); + $TorrentTagUserIDs = explode('|', $TorrentTagUserIDs); + $TagPositiveVotes = explode('|', $TagPositiveVotes); + $TagNegativeVotes = explode('|', $TagNegativeVotes); + + foreach ($TorrentTags as $TagKey => $TagName) { + $Tags[$TagKey]['name'] = $TagName; + $Tags[$TagKey]['score'] = ($TagPositiveVotes[$TagKey] - $TagNegativeVotes[$TagKey]); + $Tags[$TagKey]['id'] = $TorrentTagIDs[$TagKey]; + $Tags[$TagKey]['userid'] = $TorrentTagUserIDs[$TagKey]; } + uasort($Tags, 'compare'); } +$CoverArt = $tgroup->coverArt(); + // Comments (must be loaded before View::show_header so that subscriptions and quote notifications are handled properly) $commentPage = new Gazelle\Comment\Torrent($GroupID); if (isset($_GET['postid'])) { @@ -112,39 +119,35 @@