2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
2023-08-04 20:51:02 +00:00
|
|
|
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;
|
2023-08-04 20:51:02 +00:00
|
|
|
use Misuzu\Auth\AuthInfo;
|
|
|
|
use Misuzu\Comments\Comments;
|
2023-07-15 23:58:17 +00:00
|
|
|
use Misuzu\Comments\CommentsEx;
|
2023-08-04 20:51:02 +00:00
|
|
|
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;
|
2023-09-06 13:50:19 +00:00
|
|
|
use Misuzu\Users\UsersContext;
|
2023-08-04 20:51:02 +00:00
|
|
|
|
2023-09-08 00:13:30 +00:00
|
|
|
final class ChangelogRoutes extends RouteHandler {
|
2023-08-04 20:51:02 +00:00
|
|
|
public function __construct(
|
2023-09-06 13:50:19 +00:00
|
|
|
private IConfig $config,
|
|
|
|
private Changelog $changelog,
|
|
|
|
private UsersContext $usersCtx,
|
|
|
|
private AuthInfo $authInfo,
|
|
|
|
private Comments $comments
|
|
|
|
) {}
|
2023-08-04 20:51:02 +00:00
|
|
|
|
|
|
|
private function getCommentsInfo(string $categoryName): object {
|
2023-09-06 13:50:19 +00:00
|
|
|
$comments = new CommentsEx($this->authInfo, $this->comments, $this->usersCtx);
|
2023-08-04 20:51:02 +00:00
|
|
|
return $comments->getCommentsForLayout($categoryName);
|
|
|
|
}
|
|
|
|
|
2023-09-08 00:13:30 +00:00
|
|
|
#[Route('GET', '/changelog')]
|
2023-08-04 20:51:02 +00:00
|
|
|
public function getIndex($response, $request) {
|
2023-07-15 02:05:49 +00:00
|
|
|
$filterDate = (string)$request->getParam('date');
|
2023-08-04 20:51:02 +00:00
|
|
|
$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);
|
2023-01-01 19:06:01 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2023-08-04 20:51:02 +00:00
|
|
|
if(empty($filterUser))
|
|
|
|
$filterUser = null;
|
|
|
|
else
|
2022-09-13 13:14:49 +00:00
|
|
|
try {
|
2023-09-06 13:50:19 +00:00
|
|
|
$filterUser = $this->usersCtx->getUserInfo($filterUser);
|
2023-07-22 15:02:41 +00:00
|
|
|
} 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
|
|
|
|
2023-08-05 13:50:15 +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;
|
|
|
|
|
2023-08-05 13:50:15 +00:00
|
|
|
$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) {
|
2023-09-06 13:50:19 +00:00
|
|
|
$userInfo = $changeInfo->hasUserId() ? $this->usersCtx->getUserInfo($changeInfo->getUserId()) : null;
|
2023-07-15 02:05:49 +00:00
|
|
|
|
|
|
|
$changes[] = [
|
|
|
|
'change' => $changeInfo,
|
|
|
|
'user' => $userInfo,
|
2023-09-06 13:50:19 +00:00
|
|
|
'user_colour' => $this->usersCtx->getUserColour($userInfo),
|
2023-07-15 02:05:49 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-08-04 20:51:02 +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-08-04 20:51:02 +00:00
|
|
|
]);
|
2023-07-15 02:05:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 00:13:30 +00:00
|
|
|
#[Route('GET', '/changelog/change/:id')]
|
2023-08-04 20:51:02 +00:00
|
|
|
public function getChange($response, $request, string $changeId) {
|
2022-09-13 13:14:49 +00:00
|
|
|
try {
|
2023-08-05 13:50:15 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2023-08-05 13:50:15 +00:00
|
|
|
$tagInfos = $this->changelog->getTags(changeInfo: $changeInfo);
|
2023-09-06 13:50:19 +00:00
|
|
|
$userInfo = $changeInfo->hasUserId() ? $this->usersCtx->getUserInfo($changeInfo->getUserId()) : null;
|
2023-07-15 02:05:49 +00:00
|
|
|
|
2023-08-04 20:51:02 +00:00
|
|
|
return Template::renderRaw('changelog.change', [
|
2022-09-13 13:14:49 +00:00
|
|
|
'change_info' => $changeInfo,
|
2023-08-05 13:50:15 +00:00
|
|
|
'change_tags' => $tagInfos,
|
2023-07-15 02:05:49 +00:00
|
|
|
'change_user_info' => $userInfo,
|
2023-09-06 13:50:19 +00:00
|
|
|
'change_user_colour' => $this->usersCtx->getUserColour($userInfo),
|
2023-07-15 23:58:17 +00:00
|
|
|
'comments_info' => $this->getCommentsInfo($changeInfo->getCommentsCategoryName()),
|
2023-08-04 20:51:02 +00:00
|
|
|
]);
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function createFeed(string $feedMode): Feed {
|
2023-08-04 20:51:02 +00:00
|
|
|
$siteName = $this->config->getString('site.name', 'Misuzu');
|
2023-08-05 13:50:15 +00:00
|
|
|
$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')]
|
2023-08-04 20:51:02 +00:00
|
|
|
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')]
|
2023-08-04 20:51:02 +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
|
|
|
}
|
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
|
|
|
}
|