misuzu/src/Http/Handlers/ChangelogHandler.php

165 lines
5.8 KiB
PHP
Raw Normal View History

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\Config;
use Misuzu\Config\IConfig;
2022-09-13 13:14:49 +00:00
use Misuzu\Pagination;
use Misuzu\Template;
2023-07-15 02:05:49 +00:00
use Misuzu\Comments\CommentsCategory;
use Misuzu\Comments\CommentsCategoryNotFoundException;
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;
use Misuzu\Users\UserNotFoundException;
class ChangelogHandler extends Handler {
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);
$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);
} catch(UserNotFoundException $ex) {
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 = [];
$userInfos = [];
foreach($changeInfos as $changeInfo) {
$userId = $changeInfo->getUserId();
if(array_key_exists($userId, $userInfos)) {
$userInfo = $userInfos[$userId];
} else {
try {
$userInfo = User::byId($userId);
} catch(UserNotFoundException $ex) {
$userInfo = null;
}
$userInfos[$userId] = $userInfo;
}
$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,
'comments_user' => User::getCurrent(),
2023-07-15 02:05:49 +00:00
'comments_category' => empty($filterDate) ? null : self::getCommentsCategory($changeInfos[0]->getCommentsCategoryName()),
2022-09-13 13:14:49 +00:00
]));
}
2023-07-15 02:05:49 +00:00
private static function getCommentsCategory(string $categoryName): CommentsCategory {
try {
$category = CommentsCategory::byName($categoryName);
} catch(CommentsCategoryNotFoundException $ex) {
$category = new CommentsCategory($categoryName);
$category->save();
}
return $category;
}
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());
} catch(UserNotFoundException $ex) {
$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,
2022-09-13 13:14:49 +00:00
'comments_user' => User::getCurrent(),
2023-07-15 02:05:49 +00:00
'comments_category' => self::getCommentsCategory($changeInfo->getCommentsCategoryName()),
2022-09-13 13:14:49 +00:00
]));
}
private function createFeed(string $feedMode): Feed {
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)
->setTitle(Config::get('site.name', IConfig::T_STR, 'Misuzu') . ' » Changelog')
->setDescription('Live feed of changes to ' . Config::get('site.name', IConfig::T_STR, 'Misuzu') . '.')
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'));
}
}