2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu\Http\Handlers;
|
|
|
|
|
|
|
|
use ErrorException;
|
2023-07-15 02:05:49 +00:00
|
|
|
use RuntimeException;
|
2022-09-13 13:14:49 +00:00
|
|
|
use Misuzu\Pagination;
|
|
|
|
use Misuzu\Template;
|
2023-07-15 23:58:17 +00:00
|
|
|
use Misuzu\Comments\CommentsEx;
|
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\User;
|
|
|
|
|
|
|
|
class ChangelogHandler extends Handler {
|
2023-07-15 23:58:17 +00:00
|
|
|
private array $userInfos = [];
|
|
|
|
|
2022-09-13 13:14:49 +00:00
|
|
|
public function index($response, $request) {
|
2023-07-15 02:05:49 +00:00
|
|
|
$filterDate = (string)$request->getParam('date');
|
|
|
|
$filterUser = (int)$request->getParam('user', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
$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-07-15 02:05:49 +00:00
|
|
|
if($filterUser > 0)
|
2022-09-13 13:14:49 +00:00
|
|
|
try {
|
|
|
|
$filterUser = User::byId($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
|
|
|
else
|
|
|
|
$filterUser = null;
|
|
|
|
|
|
|
|
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-07-15 02:05:49 +00:00
|
|
|
$changelog = $this->context->getChangelog();
|
|
|
|
$count = $changelog->countAllChanges($filterUser, $filterDate, $filterTags);
|
2022-09-13 13:14:49 +00:00
|
|
|
$pagination = new Pagination($count, 30);
|
|
|
|
if(!$pagination->hasValidOffset())
|
|
|
|
return 404;
|
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
$changeInfos = $changelog->getAllChanges(userInfo: $filterUser, dateTime: $filterDate, tags: $filterTags, pagination: $pagination);
|
|
|
|
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];
|
2023-07-15 02:05:49 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$userInfo = User::byId($userId);
|
2023-07-22 15:02:41 +00:00
|
|
|
} catch(RuntimeException $ex) {
|
2023-07-15 02:05:49 +00:00
|
|
|
$userInfo = null;
|
|
|
|
}
|
|
|
|
|
2023-07-15 23:58:17 +00:00
|
|
|
$this->userInfos[$userId] = $userInfo;
|
2023-07-15 02:05:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$changes[] = [
|
|
|
|
'change' => $changeInfo,
|
|
|
|
'user' => $userInfo,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-09-13 13:14:49 +00:00
|
|
|
$response->setContent(Template::renderRaw('changelog.index', [
|
|
|
|
'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()),
|
2022-09-13 13:14:49 +00:00
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
2023-07-15 23:58:17 +00:00
|
|
|
private function getCommentsInfo(string $categoryName): object {
|
|
|
|
$comments = new CommentsEx($this->context->getComments(), $this->userInfos);
|
|
|
|
return $comments->getCommentsForLayout($categoryName);
|
2023-07-15 02:05:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function change($response, $request, string $changeId) {
|
2022-09-13 13:14:49 +00:00
|
|
|
try {
|
2023-07-15 02:05:49 +00:00
|
|
|
$changeInfo = $this->context->getChangelog()->getChangeById($changeId, withTags: true);
|
|
|
|
} catch(RuntimeException $ex) {
|
2022-09-13 13:14:49 +00:00
|
|
|
return 404;
|
|
|
|
}
|
|
|
|
|
2023-07-15 02:05:49 +00:00
|
|
|
try {
|
|
|
|
$userInfo = User::byId($changeInfo->getUserId());
|
2023-07-22 15:02:41 +00:00
|
|
|
} catch(RuntimeException $ex) {
|
2023-07-15 02:05:49 +00:00
|
|
|
$userInfo = null;
|
|
|
|
}
|
|
|
|
|
2022-09-13 13:14:49 +00:00
|
|
|
$response->setContent(Template::renderRaw('changelog.change', [
|
|
|
|
'change_info' => $changeInfo,
|
2023-07-15 02:05:49 +00:00
|
|
|
'change_user_info' => $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 {
|
2023-07-18 21:48:44 +00:00
|
|
|
$siteName = $this->context->getConfig()->getString('site.name', 'Misuzu');
|
2023-07-15 02:05:49 +00:00
|
|
|
$changes = $this->context->getChangelog()->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 feedAtom($response, $request) {
|
|
|
|
$response->setContentType('application/atom+xml; charset=utf-8');
|
|
|
|
return (new AtomFeedSerializer)->serializeFeed(self::createFeed('atom'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function feedRss($response, $request) {
|
|
|
|
$response->setContentType('application/rss+xml; charset=utf-8');
|
|
|
|
return (new RssFeedSerializer)->serializeFeed(self::createFeed('rss'));
|
|
|
|
}
|
|
|
|
}
|