Skip to content

Commit

Permalink
Kill classes/comments.class.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Spine authored and itismadness committed Aug 15, 2021
1 parent 4faf07a commit 752db71
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 101 deletions.
78 changes: 76 additions & 2 deletions app/Manager/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,43 @@ public function create(int $userId, string $page, int $pageId, string $body) {
return (new $className($pageId))->setPostId($postId);
}

public function merge(string $page, int $pageId, int $targetPageId) {
$qid = $this->db->get_query_id();

$this->db->prepared_query("
UPDATE comments SET
PageID = ?
WHERE Page = ? AND PageID = ?
", $targetPageId, $page, $pageId
);
$pageCount = $this->db->scalar("
SELECT ceil(count(*) / ?) AS Pages
FROM comments
WHERE Page = ? AND PageID = ?
GROUP BY PageID
", TORRENT_COMMENTS_PER_PAGE, $page, $targetPageId
);
$last = floor((TORRENT_COMMENTS_PER_PAGE * $pageCount - TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE);

// quote notifications
$this->db->prepared_query("
UPDATE users_notify_quoted SET
PageID = ?
WHERE Page = ? AND PageID = ?
", $targetPageId, $page, $pageId
);
$this->db->set_query_id($qid);

// comment subscriptions
$subscription = new \Gazelle\Manager\Subscription;
$subscription->move($page, $pageId, $targetPageId);

for ($i = 0; $i <= $last; ++$i) {
$this->cache->delete_value($page . "_comments_$targetPageId" . "_catalogue_$i");
}
$this->cache->delete_value($page."_comments_$targetPageId");
}

/**
* Remove all comments on $page/$pageId (handle quote notifications and subscriptions as well)
* @param string $page
Expand All @@ -86,6 +123,7 @@ public function remove(string $page, int $pageId) {
if ($pageCount === 0) {
return false;
}
$last = floor((TORRENT_COMMENTS_PER_PAGE * $pageCount - TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE);

$this->db->prepared_query("
DELETE FROM comments WHERE Page = ? AND PageID = ?
Expand All @@ -101,14 +139,13 @@ public function remove(string $page, int $pageId) {
DELETE FROM users_notify_quoted WHERE Page = ? AND PageID = ?
", $page, $pageId
);
$this->db->set_query_id($qid);

// Clear cache
$last = floor((TORRENT_COMMENTS_PER_PAGE * $CommPages - TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE);
for ($i = 0; $i <= $last; ++$i) {
$this->cache->delete_value($page . '_comments_' . $pageId . '_catalogue_' . $i);
}
$this->cache->delete_value($page . '_comments_' . $pageId);
$this->db->set_query_id($qid);

return true;
}
Expand All @@ -129,4 +166,41 @@ public function loadEdits(string $page, int $postId): array {
}
return $edits;
}

/**
* Load recent collage comments. Used for displaying recent comments on collage pages.
* @param int $CollageID ID of the collage
* @return array ($Comments)
* $Comments
* ID: Comment ID
* Body: Comment body
* AuthorID: Author of comment
* Username: Their username
* AddedTime: Date of comment creation
*/
public function collageSummary($collageId, $count = 5): array {
$key = "collages_comments_recent_$collageId";
$list = $this->cache->get_value($key);
if ($list === false) {
$qid = $this->db->get_query_id();
$this->db->prepared_query("
SELECT c.ID AS id,
c.Body as body,
c.AuthorID as author_id,
c.AddedTime as added
FROM comments AS c
LEFT JOIN users_main AS um ON (um.ID = c.AuthorID)
WHERE c.Page = ? AND c.PageID = ?
ORDER BY c.ID DESC
LIMIT ?
", 'collages', $collageId, $count
);
$list = $this->db->to_array(false, MYSQLI_ASSOC, false);
$this->db->set_query_id($qid);
if (count($list)) {
$this->cache->cache_value($key, $list, 7200);
}
}
return $list;
}
}
94 changes: 0 additions & 94 deletions classes/comments.class.php

This file was deleted.

2 changes: 1 addition & 1 deletion sections/artist/rename.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
Requests::update_sphinx_requests($RequestID);
}
}
Comments::merge('artist', $ArtistID, $TargetArtistID);
(new \Gazelle\Manager\Comment)->merge('artist', $ArtistID, $TargetArtistID);
}
}

Expand Down
2 changes: 1 addition & 1 deletion sections/collages/artist_collage.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
'can_post' => !$LoggedUser['DisablePosting'],
'category_id' => $Collage->categoryId(),
'category_name' => $CollageCats[$Collage->categoryId()],
'comments' => Comments::collageSummary($CollageID),
'comments' => (new Gazelle\Manager\Comment)->collageSummary($CollageID),
'contributors' => array_slice($Collage->contributors(), 0, 5, true),
'contributors_n' => $Collage->numContributors(),
'description' => Text::full_format($Collage->description()),
Expand Down
2 changes: 1 addition & 1 deletion sections/collages/torrent_collage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'can_post' => !$LoggedUser['DisablePosting'],
'category_id' => $Collage->categoryId(),
'category_name' => $CollageCats[$Collage->categoryId()],
'comments' => Comments::collageSummary($CollageID),
'comments' => (new Gazelle\Manager\Comment)->collageSummary($CollageID),
'contributors_n' => $Collage->numContributors(),
'contributors' => array_slice($Collage->contributors(), 0, 5, true),
'description' => Text::full_format($Collage->description()),
Expand Down
2 changes: 1 addition & 1 deletion sections/torrents/merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
);

// Comments
Comments::merge('torrents', $oldGroupId, $newGroupId);
(new \Gazelle\Manager\Comment)->merge('torrents', $oldGroupId, $newGroupId);

// Collages
$DB->prepared_query("
Expand Down
2 changes: 1 addition & 1 deletion templates/collage/sidebar.twig
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ collage by {{ user_id|user_url }}
<br />
<a href="reports.php?action=report&amp;type=collages_comment&amp;id={{ comment.id }}" class="brackets">Report</a>
</div>
<div class="pad">{{ comment.body|raw }}</div>
<div class="pad">{{ comment.body|bb_format }}</div>
</div>
{% endfor %}
<div class="box pad">
Expand Down

0 comments on commit 752db71

Please sign in to comment.