2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu\Http\Handlers;
|
|
|
|
|
|
|
|
use ErrorException;
|
|
|
|
use Misuzu\Config;
|
2023-01-06 20:50:41 +00:00
|
|
|
use Misuzu\Config\IConfig;
|
2022-09-13 13:14:49 +00:00
|
|
|
use Misuzu\Pagination;
|
|
|
|
use Misuzu\Template;
|
|
|
|
use Misuzu\Changelog\ChangelogChange;
|
|
|
|
use Misuzu\Changelog\ChangelogChangeNotFoundException;
|
|
|
|
use Misuzu\Changelog\ChangelogTag;
|
|
|
|
use Misuzu\Changelog\ChangelogTagNotFoundException;
|
|
|
|
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) {
|
|
|
|
$filterDate = $request->getParam('date');
|
|
|
|
$filterUser = $request->getParam('user', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
//$filterTags = $request->getParam('tags');
|
|
|
|
|
|
|
|
if($filterDate !== null)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($filterUser !== null)
|
|
|
|
try {
|
|
|
|
$filterUser = User::byId($filterUser);
|
|
|
|
} catch(UserNotFoundException $ex) {
|
|
|
|
return 404;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*if($filterTags !== null) {
|
|
|
|
$splitTags = explode(',', $filterTags);
|
|
|
|
$filterTags = [];
|
|
|
|
for($i = 0; $i < min(10, count($splitTags)); ++$i)
|
|
|
|
try {
|
|
|
|
$filterTags[] = ChangelogTag::byId($splitTags[$i]);
|
|
|
|
} catch(ChangelogTagNotFoundException $ex) {
|
|
|
|
return 404;
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
|
|
|
$count = $filterDate !== null ? -1 : ChangelogChange::countAll($filterDate, $filterUser);
|
|
|
|
$pagination = new Pagination($count, 30);
|
|
|
|
if(!$pagination->hasValidOffset())
|
|
|
|
return 404;
|
|
|
|
|
|
|
|
$changes = ChangelogChange::all($pagination, $filterDate, $filterUser);
|
|
|
|
if(empty($changes))
|
|
|
|
return 404;
|
|
|
|
|
|
|
|
$response->setContent(Template::renderRaw('changelog.index', [
|
|
|
|
'changelog_infos' => $changes,
|
|
|
|
'changelog_date' => $filterDate,
|
|
|
|
'changelog_user' => $filterUser,
|
|
|
|
'changelog_pagination' => $pagination,
|
|
|
|
'comments_user' => User::getCurrent(),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function change($response, $request, int $changeId) {
|
|
|
|
try {
|
|
|
|
$changeInfo = ChangelogChange::byId($changeId);
|
|
|
|
} catch(ChangelogChangeNotFoundException $ex) {
|
|
|
|
return 404;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->setContent(Template::renderRaw('changelog.change', [
|
|
|
|
'change_info' => $changeInfo,
|
|
|
|
'comments_user' => User::getCurrent(),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createFeed(string $feedMode): Feed {
|
|
|
|
$changes = ChangelogChange::all(new Pagination(10));
|
|
|
|
|
|
|
|
$feed = (new Feed)
|
2023-01-06 20:50:41 +00:00
|
|
|
->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)
|
|
|
|
->setTitle($change->getActionString() . ': ' . $change->getHeader())
|
|
|
|
->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'));
|
|
|
|
}
|
|
|
|
}
|