misuzu/src/Changelog/ChangelogRoutes.php

151 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};
2025-01-29 23:13:17 +00:00
use Index\Http\Routing\{HttpGet,RouteHandler,RouteHandlerCommon};
2024-10-05 02:40:29 +00:00
use Index\Syndication\FeedBuilder;
2025-01-29 23:13:17 +00:00
use Index\Urls\{UrlFormat,UrlRegistry,UrlSource,UrlSourceCommon};
2024-03-30 03:14:03 +00:00
use Misuzu\{Pagination,SiteInfo,Template};
use Misuzu\Auth\AuthInfo;
use Misuzu\Comments\CommentsContext;
use Misuzu\Users\UsersContext;
2024-10-05 02:40:29 +00:00
final class ChangelogRoutes implements RouteHandler, UrlSource {
2025-01-29 23:13:17 +00:00
use RouteHandlerCommon, UrlSourceCommon;
2024-10-05 02:40:29 +00:00
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,
2025-01-29 23:36:35 +00:00
private ChangelogData $changelog,
private UsersContext $usersCtx,
private CommentsContext $commentsCtx,
private AuthInfo $authInfo,
) {}
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);
2024-12-19 01:22:26 +00:00
$pagination = Pagination::fromRequest($request, $count, 30, 'p');
if(!$pagination->validOffset)
2020-05-20 18:09:38 +00:00
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;
if(empty($filterDate))
$commentsCategoryName = null;
elseif($commentsCategoryName) // should this be run here?
$this->commentsCtx->categories->ensureCategoryExists($commentsCategoryName);
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,
'comments_category_name' => $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
// should this be run here?
$this->commentsCtx->categories->ensureCategoryExists($changeInfo->commentsCategoryName);
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),
]);
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;
2025-01-29 23:13:17 +00:00
$feed->title = sprintf('%s » Changelog', $siteName);
$feed->description = sprintf('Live feed of changes to %s.', $siteName);
$feed->contentUrl = $siteUrl . $this->urls->format('changelog-index');
$feed->feedUrl = $siteUrl . $this->urls->format('changelog-feed');
2024-10-05 02:40:29 +00:00
foreach($changes as $change)
$feed->createEntry(function($item) use ($change, $siteUrl) {
2025-01-29 23:13:17 +00:00
$item->title = sprintf('%s: %s', $change->actionText, $change->summary);
$item->createdAt = $change->createdTime;
$item->contentUrl = $siteUrl . $this->urls->format('changelog-change', ['change' => $change->id]);
$item->commentsUrl = $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
}
}