misuzu/src/Changelog/ChangelogRoutes.php

171 lines
6.2 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu\Changelog;
2022-09-13 13:14:49 +00:00
use ErrorException;
2023-07-15 02:05:49 +00:00
use RuntimeException;
2023-09-08 00:13:30 +00:00
use Index\Routing\Route;
use Index\Routing\RouteHandler;
2022-09-13 13:14:49 +00:00
use Misuzu\Pagination;
use Misuzu\Template;
use Misuzu\Auth\AuthInfo;
use Misuzu\Comments\Comments;
2023-07-15 23:58:17 +00:00
use Misuzu\Comments\CommentsEx;
use Misuzu\Config\IConfig;
2022-09-13 13:14:49 +00:00
use Misuzu\Feeds\Feed;
use Misuzu\Feeds\FeedItem;
use Misuzu\Feeds\AtomFeedSerializer;
use Misuzu\Feeds\RssFeedSerializer;
use Misuzu\Users\UsersContext;
2023-09-08 00:13:30 +00:00
final class ChangelogRoutes extends RouteHandler {
public function __construct(
private IConfig $config,
private Changelog $changelog,
private UsersContext $usersCtx,
private AuthInfo $authInfo,
private Comments $comments
) {}
private function getCommentsInfo(string $categoryName): object {
$comments = new CommentsEx($this->authInfo, $this->comments, $this->usersCtx);
return $comments->getCommentsForLayout($categoryName);
}
2023-09-08 00:13:30 +00:00
#[Route('GET', '/changelog')]
public function getIndex($response, $request) {
2023-07-15 02:05:49 +00:00
$filterDate = (string)$request->getParam('date');
$filterUser = (string)$request->getParam('user', FILTER_SANITIZE_NUMBER_INT);
2023-07-15 02:05:49 +00:00
$filterTags = (string)$request->getParam('tags');
2022-09-13 13:14:49 +00:00
2023-07-15 02:05:49 +00:00
if(empty($filterDate))
$filterDate = null;
else
2022-09-13 13:14:49 +00:00
try {
$dateParts = explode('-', $filterDate, 3);
$filterDate = gmmktime(12, 0, 0, (int)$dateParts[1], (int)$dateParts[2], (int)$dateParts[0]);
2022-09-13 13:14:49 +00:00
} catch(ErrorException $ex) {
return 404;
}
if(empty($filterUser))
$filterUser = null;
else
2022-09-13 13:14:49 +00:00
try {
$filterUser = $this->usersCtx->getUserInfo($filterUser);
} catch(RuntimeException $ex) {
2022-09-13 13:14:49 +00:00
return 404;
}
2023-07-15 02:05:49 +00:00
if(empty($filterTags))
$filterTags = null;
else {
$filterTags = explode(',', $filterTags);
foreach($filterTags as &$tag)
$tag = trim($tag);
}
2022-09-13 13:14:49 +00:00
$count = $this->changelog->countChanges($filterUser, $filterDate, $filterTags);
2022-09-13 13:14:49 +00:00
$pagination = new Pagination($count, 30);
if(!$pagination->hasValidOffset())
return 404;
$changeInfos = $this->changelog->getChanges(userInfo: $filterUser, dateTime: $filterDate, tags: $filterTags, pagination: $pagination);
2023-07-15 02:05:49 +00:00
if(empty($changeInfos))
2022-09-13 13:14:49 +00:00
return 404;
2023-07-15 02:05:49 +00:00
$changes = [];
foreach($changeInfos as $changeInfo) {
$userInfo = $changeInfo->hasUserId() ? $this->usersCtx->getUserInfo($changeInfo->getUserId()) : null;
2023-07-15 02:05:49 +00:00
$changes[] = [
'change' => $changeInfo,
'user' => $userInfo,
'user_colour' => $this->usersCtx->getUserColour($userInfo),
2023-07-15 02:05:49 +00:00
];
}
return Template::renderRaw('changelog.index', [
2022-09-13 13:14:49 +00:00
'changelog_infos' => $changes,
'changelog_date' => $filterDate,
'changelog_user' => $filterUser,
2023-07-15 02:05:49 +00:00
'changelog_tags' => $filterTags,
2022-09-13 13:14:49 +00:00
'changelog_pagination' => $pagination,
2023-07-15 23:58:17 +00:00
'comments_info' => empty($filterDate) ? null : $this->getCommentsInfo($changeInfos[0]->getCommentsCategoryName()),
]);
2023-07-15 02:05:49 +00:00
}
2023-09-08 00:13:30 +00:00
#[Route('GET', '/changelog/change/:id')]
public function getChange($response, $request, string $changeId) {
2022-09-13 13:14:49 +00:00
try {
$changeInfo = $this->changelog->getChange($changeId);
2023-07-15 02:05:49 +00:00
} catch(RuntimeException $ex) {
2022-09-13 13:14:49 +00:00
return 404;
}
$tagInfos = $this->changelog->getTags(changeInfo: $changeInfo);
$userInfo = $changeInfo->hasUserId() ? $this->usersCtx->getUserInfo($changeInfo->getUserId()) : null;
2023-07-15 02:05:49 +00:00
return Template::renderRaw('changelog.change', [
2022-09-13 13:14:49 +00:00
'change_info' => $changeInfo,
'change_tags' => $tagInfos,
2023-07-15 02:05:49 +00:00
'change_user_info' => $userInfo,
'change_user_colour' => $this->usersCtx->getUserColour($userInfo),
2023-07-15 23:58:17 +00:00
'comments_info' => $this->getCommentsInfo($changeInfo->getCommentsCategoryName()),
]);
2022-09-13 13:14:49 +00:00
}
private function createFeed(string $feedMode): Feed {
$siteName = $this->config->getString('site.name', 'Misuzu');
$changes = $this->changelog->getChanges(pagination: new Pagination(10));
2022-09-13 13:14:49 +00:00
$feed = (new Feed)
2023-07-18 21:48:44 +00:00
->setTitle($siteName . ' » Changelog')
->setDescription('Live feed of changes to ' . $siteName . '.')
2022-09-13 13:14:49 +00:00
->setContentUrl(url_prefix(false) . url('changelog-index'))
->setFeedUrl(url_prefix(false) . url("changelog-feed-{$feedMode}"));
foreach($changes as $change) {
$changeUrl = url_prefix(false) . url('changelog-change', ['change' => $change->getId()]);
$commentsUrl = url_prefix(false) . url('changelog-change-comments', ['change' => $change->getId()]);
$feedItem = (new FeedItem)
2023-07-15 02:05:49 +00:00
->setTitle($change->getActionText() . ': ' . $change->getSummary())
2022-09-13 13:14:49 +00:00
->setCreationDate($change->getCreatedTime())
->setUniqueId($changeUrl)
->setContentUrl($changeUrl)
->setCommentsUrl($commentsUrl);
$feed->addItem($feedItem);
}
return $feed;
}
2023-09-08 00:13:30 +00:00
#[Route('GET', '/changelog.rss')]
public function getFeedRSS($response) {
$response->setContentType('application/rss+xml; charset=utf-8');
return (new RssFeedSerializer)->serializeFeed($this->createFeed('rss'));
2022-09-13 13:14:49 +00:00
}
2023-09-08 00:13:30 +00:00
#[Route('GET', '/changelog.atom')]
public function getFeedAtom($response) {
$response->setContentType('application/atom+xml; charset=utf-8');
return (new AtomFeedSerializer)->serializeFeed($this->createFeed('atom'));
2022-09-13 13:14:49 +00:00
}
2023-09-08 00:13:30 +00:00
#[Route('GET', '/changelog.php')]
public static function getChangelogPHP($response, $request) {
$changeId = $request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
if($changeId) {
$response->redirect(url('changelog-change', ['change' => $changeId]), true);
return;
}
$response->redirect(url('changelog-index', [
'date' => $request->getParam('d'),
'user' => $request->getParam('u', FILTER_SANITIZE_NUMBER_INT),
]), true);
}
2022-09-13 13:14:49 +00:00
}