misuzu/src/Changelog/ChangelogRoutes.php

206 lines
7.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;
use Index\Routing\IRouter;
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\Users;
final class ChangelogRoutes {
private IConfig $config;
private Changelog $changelog;
private Users $users;
private AuthInfo $authInfo;
private Comments $comments;
2022-09-13 13:14:49 +00:00
2023-07-15 23:58:17 +00:00
private array $userInfos = [];
private array $userColours = [];
2023-07-15 23:58:17 +00:00
public function __construct(
IRouter $router,
IConfig $config,
Changelog $changelog,
Users $users,
AuthInfo $authInfo,
Comments $comments
) {
$this->config = $config;
$this->changelog = $changelog;
$this->users = $users;
$this->authInfo = $authInfo;
$this->comments = $comments;
$router->get('/changelog', [$this, 'getIndex']);
$router->get('/changelog.rss', [$this, 'getFeedRSS']);
$router->get('/changelog.atom', [$this, 'getFeedAtom']);
$router->get('/changelog/change/:id', [$this, 'getChange']);
$router->get('/changelog.php', function($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);
});
}
private function getCommentsInfo(string $categoryName): object {
$comments = new CommentsEx($this->authInfo, $this->comments, $this->users, $this->userInfos, $this->userColours);
return $comments->getCommentsForLayout($categoryName);
}
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->users->getUser($filterUser, 'id');
} 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->countAllChanges($filterUser, $filterDate, $filterTags);
2022-09-13 13:14:49 +00:00
$pagination = new Pagination($count, 30);
if(!$pagination->hasValidOffset())
return 404;
$changeInfos = $this->changelog->getAllChanges(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) {
$userId = $changeInfo->getUserId();
2023-07-15 23:58:17 +00:00
if(array_key_exists($userId, $this->userInfos)) {
$userInfo = $this->userInfos[$userId];
$userColour = $this->userColours[$userId];
2023-07-15 02:05:49 +00:00
} else {
try {
$userInfo = $this->users->getUser($userId, 'id');
$userColour = $this->users->getUserColour($userInfo);
} catch(RuntimeException $ex) {
2023-07-15 02:05:49 +00:00
$userInfo = null;
$userColour = null;
2023-07-15 02:05:49 +00:00
}
2023-07-15 23:58:17 +00:00
$this->userInfos[$userId] = $userInfo;
$this->userColours[$userId] = $userColour;
2023-07-15 02:05:49 +00:00
}
$changes[] = [
'change' => $changeInfo,
'user' => $userInfo,
'user_colour' => $userColour,
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
}
public function getChange($response, $request, string $changeId) {
2022-09-13 13:14:49 +00:00
try {
$changeInfo = $this->changelog->getChangeById($changeId, withTags: true);
2023-07-15 02:05:49 +00:00
} catch(RuntimeException $ex) {
2022-09-13 13:14:49 +00:00
return 404;
}
2023-07-15 02:05:49 +00:00
try {
$userInfo = $this->users->getUser($changeInfo->getUserId(), 'id');
$userColour = $this->users->getUserColour($userInfo);
} catch(RuntimeException $ex) {
2023-07-15 02:05:49 +00:00
$userInfo = null;
$userColour = 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,
2023-07-15 02:05:49 +00:00
'change_user_info' => $userInfo,
'change_user_colour' => $userColour,
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->getAllChanges(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;
}
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
}
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
}
}