misuzu/src/Changelog/ChangelogRoutes.php

149 lines
5.9 KiB
PHP
Raw Normal View History

2020-05-20 18:09:38 +00:00
<?php
namespace Misuzu\Changelog;
2020-05-20 18:09:38 +00:00
use ErrorException;
2023-07-15 02:05:49 +00:00
use RuntimeException;
2024-12-02 21:33:15 +00:00
use Index\Http\{HttpRequest,HttpResponseBuilder};
2024-10-05 02:40:29 +00:00
use Index\Http\Routing\{HttpGet,RouteHandler,RouteHandlerTrait};
use Index\Syndication\FeedBuilder;
use Index\Urls\{UrlFormat,UrlRegistry,UrlSource,UrlSourceTrait};
2024-03-30 03:14:03 +00:00
use Misuzu\{Pagination,SiteInfo,Template};
use Misuzu\Auth\AuthInfo;
2024-03-30 03:14:03 +00:00
use Misuzu\Comments\{Comments,CommentsEx};
use Misuzu\Users\UsersContext;
2024-10-05 02:40:29 +00:00
final class ChangelogRoutes implements RouteHandler, UrlSource {
use RouteHandlerTrait, UrlSourceTrait;
public function __construct(
2023-09-08 20:40:48 +00:00
private SiteInfo $siteInfo,
2024-10-05 02:40:29 +00:00
private UrlRegistry $urls,
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);
}
2024-03-30 03:14:03 +00:00
#[HttpGet('/changelog')]
2024-10-05 02:40:29 +00:00
#[UrlFormat('changelog-index', '/changelog', ['date' => '<date>', 'user' => '<user>', 'tags' => '<tags>', 'p' => '<page>'])]
2024-12-02 21:33:15 +00:00
public function getIndex(HttpResponseBuilder $response, HttpRequest $request): int|string {
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');
2020-05-20 18:09:38 +00:00
2023-07-15 02:05:49 +00:00
if(empty($filterDate))
$filterDate = null;
else
2020-05-20 18:09:38 +00:00
try {
$dateParts = explode('-', $filterDate, 3);
$filterDate = gmmktime(12, 0, 0, (int)$dateParts[1], (int)$dateParts[2], (int)$dateParts[0]);
2020-05-20 18:09:38 +00:00
} catch(ErrorException $ex) {
return 404;
}
if(empty($filterUser))
$filterUser = null;
else
2020-05-20 18:09:38 +00:00
try {
$filterUser = $this->usersCtx->getUserInfo($filterUser);
} catch(RuntimeException $ex) {
2020-05-20 18:09:38 +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);
}
2020-05-20 18:09:38 +00:00
$count = $this->changelog->countChanges($filterUser, $filterDate, $filterTags);
2020-05-20 18:09:38 +00:00
$pagination = new Pagination($count, 30);
if(!$pagination->hasValidOffset())
return 404;
2023-07-15 02:05:49 +00:00
$changes = [];
2024-02-07 00:04:45 +00:00
$changeInfos = $this->changelog->getChanges(userInfo: $filterUser, dateTime: $filterDate, tags: $filterTags, pagination: $pagination);
$commentsCategoryName = null;
2023-07-15 02:05:49 +00:00
foreach($changeInfos as $changeInfo) {
$userInfo = $changeInfo->userId !== null ? $this->usersCtx->getUserInfo($changeInfo->userId) : null;
2023-07-15 02:05:49 +00:00
2024-02-07 00:04:45 +00:00
if($commentsCategoryName === null)
$commentsCategoryName = $changeInfo->commentsCategoryName;
2024-02-07 00:04:45 +00:00
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
];
}
2024-02-07 00:04:45 +00:00
if(empty($changes))
return 404;
return Template::renderRaw('changelog.index', [
2020-05-20 18:09:38 +00:00
'changelog_infos' => $changes,
'changelog_date' => $filterDate,
'changelog_user' => $filterUser,
2023-07-15 02:05:49 +00:00
'changelog_tags' => $filterTags,
2020-05-20 18:09:38 +00:00
'changelog_pagination' => $pagination,
2024-02-07 00:04:45 +00:00
'comments_info' => empty($filterDate) && $commentsCategoryName !== null ? null : $this->getCommentsInfo($commentsCategoryName),
]);
2023-07-15 02:05:49 +00:00
}
2024-03-30 03:14:03 +00:00
#[HttpGet('/changelog/change/([0-9]+)')]
2024-10-05 02:40:29 +00:00
#[UrlFormat('changelog-change', '/changelog/change/<change>')]
#[UrlFormat('changelog-change-comments', '/changelog/change/<change>', fragment: 'comments')]
2024-12-02 21:33:15 +00:00
public function getChange(HttpResponseBuilder $response, HttpRequest $request, string $changeId): int|string {
2020-05-20 18:09:38 +00:00
try {
$changeInfo = $this->changelog->getChange($changeId);
2023-07-15 02:05:49 +00:00
} catch(RuntimeException $ex) {
2020-05-20 18:09:38 +00:00
return 404;
}
$tagInfos = $this->changelog->getTags(changeInfo: $changeInfo);
$userInfo = $changeInfo->userId !== null ? $this->usersCtx->getUserInfo($changeInfo->userId) : null;
2023-07-15 02:05:49 +00:00
return Template::renderRaw('changelog.change', [
2020-05-20 18:09:38 +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),
'comments_info' => $this->getCommentsInfo($changeInfo->commentsCategoryName),
]);
2020-05-20 18:09:38 +00:00
}
2024-10-05 02:40:29 +00:00
#[HttpGet('/changelog.(xml|rss|atom)')]
#[UrlFormat('changelog-feed', '/changelog.xml')]
2024-12-02 21:33:15 +00:00
public function getFeed(HttpResponseBuilder $response): string {
2024-10-05 02:40:29 +00:00
$response->setContentType('application/rss+xml; charset=utf-8');
$siteName = $this->siteInfo->name;
$siteUrl = $this->siteInfo->url;
$changes = $this->changelog->getChanges(pagination: new Pagination(10));
2020-05-20 18:09:38 +00:00
2024-10-05 02:40:29 +00:00
$feed = new FeedBuilder;
$feed->setTitle(sprintf('%s » Changelog', $siteName));
$feed->setDescription(sprintf('Live feed of changes to %s.', $siteName));
$feed->setContentUrl($siteUrl . $this->urls->format('changelog-index'));
$feed->setFeedUrl($siteUrl . $this->urls->format('changelog-feed'));
foreach($changes as $change)
$feed->createEntry(function($item) use ($change, $siteUrl) {
$item->setTitle(sprintf('%s: %s', $change->actionText, $change->summary));
$item->setCreatedAt($change->createdTime);
$item->setContentUrl($siteUrl . $this->urls->format('changelog-change', ['change' => $change->id]));
$item->setCommentsUrl($siteUrl . $this->urls->format('changelog-change-comments', ['change' => $change->id]));
2024-10-05 02:40:29 +00:00
});
return $feed->toXmlString();
2020-05-20 18:09:38 +00:00
}
}