misuzu/public-legacy/forum/leaderboard.php

115 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
2023-08-28 01:17:34 +00:00
use RuntimeException;
2024-12-02 02:28:08 +00:00
if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext))
die('Script must be called through the Misuzu route dispatcher.');
if(!$msz->authInfo->getPerms('global')->check(Perm::G_FORUM_LEADERBOARD_VIEW))
Template::throwError(403);
2022-09-13 13:14:49 +00:00
2024-12-02 02:28:08 +00:00
$config = $msz->config->getValues([
2023-08-28 01:17:34 +00:00
['forum_leader.first_year:i', 2018],
['forum_leader.first_month:i', 12],
'forum_leader.unranked.forum:a',
'forum_leader.unranked.topic:a',
]);
$mode = (string)filter_input(INPUT_GET, 'mode');
$yearMonth = (string)filter_input(INPUT_GET, 'id');
$year = $month = 0;
$currentYear = (int)date('Y');
$currentMonth = (int)date('m');
if(!empty($yearMonth)) {
$yearMonthLength = strlen($yearMonth);
if(($yearMonthLength !== 4 && $yearMonthLength !== 6) || !ctype_digit($yearMonth))
Template::throwError(404);
2023-08-28 01:17:34 +00:00
$year = (int)substr($yearMonth, 0, 4);
if($year < $config['forum_leader.first_year'] || $year > $currentYear)
Template::throwError(404);
2023-08-28 01:17:34 +00:00
if($yearMonthLength === 6) {
$month = (int)substr($yearMonth, 4, 2);
if($month < 1 || $month > 12 || ($year === $config['forum_leader.first_year'] && $month < $config['forum_leader.first_month']))
Template::throwError(404);
2023-08-28 01:17:34 +00:00
}
}
if(filter_has_var(INPUT_GET, 'allow_unranked')) {
$unrankedForums = $unrankedTopics = [];
} else {
$unrankedForums = $config['forum_leader.unranked.forum'];
$unrankedTopics = $config['forum_leader.unranked.topic'];
}
$years = $months = [];
for($i = $currentYear; $i >= $config['forum_leader.first_year']; $i--)
$years[(string)$i] = sprintf('Leaderboard %d', $i);
2022-09-13 13:14:49 +00:00
2023-08-28 01:17:34 +00:00
for($i = $currentYear, $j = $currentMonth;;) {
$months[sprintf('%d%02d', $i, $j)] = sprintf('Leaderboard %d-%02d', $i, $j);
2022-09-13 13:14:49 +00:00
2023-08-28 01:17:34 +00:00
if($j <= 1) {
$i--; $j = 12;
} else $j--;
2023-07-18 21:48:44 +00:00
2023-08-28 01:17:34 +00:00
if($i <= $config['forum_leader.first_year'] && $j < $config['forum_leader.first_month'])
break;
}
$rankings = $msz->forumCtx->posts->generatePostRankings($year, $month, $unrankedForums, $unrankedTopics);
2023-08-28 01:17:34 +00:00
foreach($rankings as $ranking) {
$ranking->user = $ranking->colour = null;
2022-09-13 13:14:49 +00:00
2023-08-28 01:17:34 +00:00
if($ranking->userId !== '')
$ranking->user = $msz->usersCtx->getUserInfo($ranking->userId);
2023-08-28 01:17:34 +00:00
}
2022-09-13 13:14:49 +00:00
2023-08-28 01:17:34 +00:00
$name = 'All Time';
2022-09-13 13:14:49 +00:00
2023-08-28 01:17:34 +00:00
if($year > 0) {
$name = sprintf("Leaderboard %04d", $year);
2023-08-28 01:17:34 +00:00
if($month > 0)
$name .= sprintf("-%02d", $month);
2022-09-13 13:14:49 +00:00
}
2023-08-28 01:17:34 +00:00
if($mode === 'markdown') {
2022-09-13 13:14:49 +00:00
$markdown = <<<MD
2023-08-28 01:17:34 +00:00
# {$name}
2022-09-13 13:14:49 +00:00
| Rank | Usename | Post count |
| ----:|:------- | ----------:|
MD;
$totalPostsCount = 0;
foreach($rankings as $ranking) {
$totalPostsCount += $ranking->postsCount;
2023-08-28 01:17:34 +00:00
$markdown .= sprintf("| %s | [%s](%s%s) | %s |\r\n", $ranking->position,
2024-12-02 02:28:08 +00:00
$ranking->user?->name ?? 'Deleted User', // @phpstan-ignore-line: no, it can be null
$msz->siteInfo->url,
$msz->urls->format('user-profile', ['user' => $ranking->userId]),
2023-09-08 20:40:48 +00:00
number_format($ranking->postsCount));
}
$markdown .= sprintf("\r\nIn total %s posts were made!\r\n", number_format($totalPostsCount));
2022-09-13 13:14:49 +00:00
Template::set('leaderboard_markdown', $markdown);
}
Template::render('forum.leaderboard', [
2023-08-28 01:17:34 +00:00
'leaderboard_id' => $yearMonth,
'leaderboard_name' => $name,
'leaderboard_years' => $years,
'leaderboard_months' => $months,
'leaderboard_data' => $rankings,
'leaderboard_mode' => $mode,
2022-09-13 13:14:49 +00:00
]);