Skip to content

Fix wiki tag popularity to exclude deleted content #1185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 29, 2025

Fixes #382 where deleted spam content was still appearing in the "Popular Tags" section of the wiki.

Problem

The wiki sidebar was showing tags from deleted articles as popular tags, allowing spam content that had been deleted to still pollute the tag popularity rankings. This occurred because:

  1. The WikiTaglist widget was using pre-calculated frequency values without filtering by wiki status
  2. The wiki sidebar's "Popular Tags" section had a direct query that ignored wiki status entirely

Solution

Modified both locations to dynamically calculate tag frequency while excluding deleted content for regular users:

Changes Made

  1. Added UserPermissions::canManageWiki() - Consistent with existing canManageNews() pattern
  2. Updated WikiTaglist widget - Now filters by wiki status for non-admin users
  3. Moved popular tags logic to controller - Removed database queries from view following MVC best practices
  4. Updated view to receive data - Wiki sidebar now gets popular tags data from controller

Architecture

Following proper MVC separation:

  • Controller (WikiController::actionIndex()): Handles data fetching logic for popular tags
  • View (views/wiki/_sidebar.php): Pure presentation logic, receives data via parameters
  • Widget (WikiTaglist): Maintains same filtering logic for widget usage

Behavior

  • For admins: Uses pre-calculated frequency column (may include deleted content for management purposes)
  • For regular users: Dynamically calculates frequency from published wikis only using:
    $query->select(['id' => 'wiki_tag_id', 'name', 'wiki_tags.slug', 'frequency' => 'COUNT(*)'])
        ->joinWith(['wikis'])
        ->andWhere(['wikis.status' => Wiki::STATUS_PUBLISHED])
        ->groupBy(['wiki_tag_id', 'name', 'slug'])
        ->orderBy(['frequency' => SORT_DESC]);

This follows the exact same pattern already implemented in NewsTaglist widget, ensuring consistency across the codebase while maintaining proper architectural separation.

Testing

  • Tags associated only with deleted wikis will no longer appear in popular tags
  • Tags associated with both published and deleted wikis will show correct frequency (published only)
  • Admin users retain ability to see all tag usage for management purposes
  • No performance regression for admin users (still uses indexed frequency column)

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits July 29, 2025 11:08
Co-authored-by: samdark <47294+samdark@users.noreply.github.com>
Co-authored-by: samdark <47294+samdark@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] @yiisoft-contrib/yiiframework.com/issues/382 Fix wiki tag popularity to exclude deleted content Jul 29, 2025
Copilot finished work on behalf of samdark July 29, 2025 11:16
@Copilot Copilot AI requested a review from samdark July 29, 2025 11:16
if (UserPermissions::canManageWiki()) {
$tagQuery->orderBy(['frequency' => SORT_DESC]);
} else {
$tagQuery->select(['id' => 'wiki_tag_id', 'name', 'wiki_tags.slug', 'frequency' => 'COUNT(*)'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely avoid queries in views.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by moving the query logic from the view to the WikiController. The popular tags data is now fetched in the controller and passed to the view as a parameter, following proper MVC architecture. Commit: ebc20ba

Co-authored-by: samdark <47294+samdark@users.noreply.github.com>
@Copilot Copilot AI requested a review from samdark July 29, 2025 23:27
Copilot finished work on behalf of samdark July 29, 2025 23:27
Comment on lines 142 to 150
if (UserPermissions::canManageWiki()) {
$tagQuery->orderBy(['frequency' => SORT_DESC]);
} else {
$tagQuery->select(['id' => 'wiki_tag_id', 'name', 'wiki_tags.slug', 'frequency' => 'COUNT(*)'])
->joinWith(['wikis'])
->andWhere(['wikis.status' => Wiki::STATUS_PUBLISHED])
->groupBy(['wiki_tag_id', 'name', 'slug'])
->orderBy(['frequency' => SORT_DESC]);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (UserPermissions::canManageWiki()) {
$tagQuery->orderBy(['frequency' => SORT_DESC]);
} else {
$tagQuery->select(['id' => 'wiki_tag_id', 'name', 'wiki_tags.slug', 'frequency' => 'COUNT(*)'])
->joinWith(['wikis'])
->andWhere(['wikis.status' => Wiki::STATUS_PUBLISHED])
->groupBy(['wiki_tag_id', 'name', 'slug'])
->orderBy(['frequency' => SORT_DESC]);
}
$tagQuery->orderBy(['frequency' => SORT_DESC]);
if (!UserPermissions::canManageWiki()) {
$tagQuery->select(['id' => 'wiki_tag_id', 'name', 'wiki_tags.slug', 'frequency' => 'COUNT(*)'])
->joinWith(['wikis'])
->andWhere(['wikis.status' => Wiki::STATUS_PUBLISHED])
->groupBy(['wiki_tag_id', 'name', 'slug'])
->orderBy(['frequency' => SORT_DESC]);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied the suggested refactor to reduce code duplication. The query now sets base conditions first, then applies conditional modifications for non-admin users. Commit: 8091986

Comment on lines 29 to 38
if (UserPermissions::canManageWiki()) {
$query->where('frequency > 1')
->orderBy(['frequency' => SORT_DESC]);
// } else {
// $query->select(['id' => 'news_tag_id', 'name', 'news_tags.slug', 'frequency' => 'COUNT(*)'])
// ->joinWith(['news'])
// ->andWhere(['news.status' => News::STATUS_PUBLISHED])
// ->groupBy(['news_tag_id', 'name', 'slug'])
// ->orderBy(['frequency' => SORT_DESC]);
// }
} else {
$query->select(['id' => 'wiki_tag_id', 'name', 'wiki_tags.slug', 'frequency' => 'COUNT(*)'])
->joinWith(['wikis'])
->andWhere(['wikis.status' => Wiki::STATUS_PUBLISHED])
->groupBy(['wiki_tag_id', 'name', 'slug'])
->orderBy(['frequency' => SORT_DESC]);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (UserPermissions::canManageWiki()) {
$query->where('frequency > 1')
->orderBy(['frequency' => SORT_DESC]);
// } else {
// $query->select(['id' => 'news_tag_id', 'name', 'news_tags.slug', 'frequency' => 'COUNT(*)'])
// ->joinWith(['news'])
// ->andWhere(['news.status' => News::STATUS_PUBLISHED])
// ->groupBy(['news_tag_id', 'name', 'slug'])
// ->orderBy(['frequency' => SORT_DESC]);
// }
} else {
$query->select(['id' => 'wiki_tag_id', 'name', 'wiki_tags.slug', 'frequency' => 'COUNT(*)'])
->joinWith(['wikis'])
->andWhere(['wikis.status' => Wiki::STATUS_PUBLISHED])
->groupBy(['wiki_tag_id', 'name', 'slug'])
->orderBy(['frequency' => SORT_DESC]);
}
$query->where('frequency > 1')
->orderBy(['frequency' => SORT_DESC]);
if (!UserPermissions::canManageWiki()) {
$query->select(['id' => 'wiki_tag_id', 'name', 'wiki_tags.slug', 'frequency' => 'COUNT(*)'])
->joinWith(['wikis'])
->andWhere(['wikis.status' => Wiki::STATUS_PUBLISHED])
->groupBy(['wiki_tag_id', 'name', 'slug']);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied the suggested refactor pattern. The widget now sets base query conditions first, then applies conditional filtering for non-admin users. Also removed duplicate orderBy call as suggested. Commit: 8091986

Co-authored-by: samdark <47294+samdark@users.noreply.github.com>
@Copilot Copilot AI requested a review from samdark July 30, 2025 15:19
Copilot finished work on behalf of samdark July 30, 2025 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do not count deleted content for tag popularity
2 participants