From 0f1a8886a62f3a19ca9d72425692c577d91acee0 Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 19 Dec 2024 01:22:26 +0000 Subject: [PATCH] Rewrote the Pagination handler. --- public-legacy/forum/forum.php | 4 +- public-legacy/forum/topic.php | 12 +-- public-legacy/manage/changelog/index.php | 4 +- public-legacy/manage/forum/redirs.php | 4 +- public-legacy/manage/general/logs.php | 5 +- public-legacy/manage/news/categories.php | 5 +- public-legacy/manage/news/posts.php | 4 +- public-legacy/manage/users/bans.php | 5 +- public-legacy/manage/users/index.php | 5 +- public-legacy/manage/users/notes.php | 5 +- public-legacy/manage/users/roles.php | 5 +- public-legacy/manage/users/warnings.php | 5 +- public-legacy/members.php | 2 +- public-legacy/settings/logs.php | 4 +- public-legacy/settings/sessions.php | 2 +- src/AuditLog/AuditLog.php | 4 +- src/Auth/LoginAttempts.php | 4 +- src/Auth/Sessions.php | 4 +- src/Changelog/Changelog.php | 4 +- src/Changelog/ChangelogRoutes.php | 4 +- src/Comments/Comments.php | 4 +- src/Counters/Counters.php | 4 +- src/Forum/ForumCategories.php | 4 +- src/Forum/ForumPosts.php | 4 +- src/Forum/ForumPostsRoutes.php | 4 +- src/Forum/ForumTopicRedirects.php | 4 +- src/Forum/ForumTopics.php | 4 +- src/LegacyRoutes.php | 2 +- src/Messages/MessagesDatabase.php | 4 +- src/Messages/MessagesRoutes.php | 4 +- src/News/News.php | 8 +- src/News/NewsRoutes.php | 10 +- src/Pagination.php | 115 +++++++++++------------ src/Users/Bans.php | 4 +- src/Users/ModNotes.php | 4 +- src/Users/Roles.php | 4 +- src/Users/Users.php | 4 +- src/Users/Warnings.php | 4 +- 38 files changed, 135 insertions(+), 147 deletions(-) diff --git a/public-legacy/forum/forum.php b/public-legacy/forum/forum.php index 7b4e33e..a0bd412 100644 --- a/public-legacy/forum/forum.php +++ b/public-legacy/forum/forum.php @@ -32,13 +32,13 @@ if($categoryInfo->isLink) { return; } -$forumPagination = new Pagination($msz->forumCtx->topics->countTopics( +$forumPagination = Pagination::fromInput($msz->forumCtx->topics->countTopics( categoryInfo: $categoryInfo, global: true, deleted: $perms->check(Perm::F_POST_DELETE_ANY) ? null : false ), 20); -if(!$forumPagination->hasValidOffset()) +if(!$forumPagination->validOffset) Template::throwError(404); $children = []; diff --git a/public-legacy/forum/topic.php b/public-legacy/forum/topic.php index 4bb9994..2d32148 100644 --- a/public-legacy/forum/topic.php +++ b/public-legacy/forum/topic.php @@ -101,18 +101,18 @@ $topicPosts = $topicInfo->postsCount; if($canDeleteAny) $topicPosts += $topicInfo->deletedPostsCount; -$topicPagination = new Pagination($topicPosts, 10, 'page'); - if(isset($preceedingPostCount)) - $topicPagination->setPage((int)floor($preceedingPostCount / $topicPagination->getRange()), true); + $pagination = Pagination::fromPage($topicPosts, (int)floor($preceedingPostCount / 10), 10, 0); +else + $pagination = Pagination::fromInput($topicPosts, 10, 'page'); -if(!$topicPagination->hasValidOffset()) +if(!$pagination->validOffset) Template::throwError(404); $postInfos = $msz->forumCtx->posts->getPosts( topicInfo: $topicInfo, deleted: $perms->check(Perm::F_POST_DELETE_ANY) ? null : false, - pagination: $topicPagination, + pagination: $pagination, ); if(empty($postInfos)) @@ -163,7 +163,7 @@ Template::render('forum.topic', [ 'category_info' => $categoryInfo, 'topic_posts' => $posts, 'can_reply' => $canReply, - 'topic_pagination' => $topicPagination, + 'topic_pagination' => $pagination, 'topic_can_delete' => $canDelete, 'topic_can_delete_any' => $canDeleteAny, 'topic_can_nuke_or_restore' => $canNukeOrRestore, diff --git a/public-legacy/manage/changelog/index.php b/public-legacy/manage/changelog/index.php index 13bc9c3..e467f7f 100644 --- a/public-legacy/manage/changelog/index.php +++ b/public-legacy/manage/changelog/index.php @@ -9,8 +9,8 @@ if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext)) if(!$msz->authInfo->getPerms('global')->check(Perm::G_CL_CHANGES_MANAGE)) Template::throwError(403); -$pagination = new Pagination($msz->changelog->countChanges(), 30); -if(!$pagination->hasValidOffset()) +$pagination = Pagination::fromInput($msz->changelog->countChanges(), 30); +if(!$pagination->validOffset) Template::throwError(404); $changeInfos = $msz->changelog->getChanges(pagination: $pagination); diff --git a/public-legacy/manage/forum/redirs.php b/public-legacy/manage/forum/redirs.php index d364199..da97b0d 100644 --- a/public-legacy/manage/forum/redirs.php +++ b/public-legacy/manage/forum/redirs.php @@ -31,8 +31,8 @@ if(filter_input(INPUT_GET, 'm') === 'explode') { return; } -$pagination = new Pagination($msz->forumCtx->topicRedirects->countTopicRedirects(), 20); -if(!$pagination->hasValidOffset()) +$pagination = Pagination::fromInput($msz->forumCtx->topicRedirects->countTopicRedirects(), 20); +if(!$pagination->validOffset) Template::throwError(404); $redirs = $msz->forumCtx->topicRedirects->getTopicRedirects(pagination: $pagination); diff --git a/public-legacy/manage/general/logs.php b/public-legacy/manage/general/logs.php index c738d91..12c3aba 100644 --- a/public-legacy/manage/general/logs.php +++ b/public-legacy/manage/general/logs.php @@ -9,9 +9,8 @@ if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext)) if(!$msz->authInfo->getPerms('global')->check(Perm::G_LOGS_VIEW)) Template::throwError(403); -$pagination = new Pagination($msz->auditLog->countLogs(), 50); - -if(!$pagination->hasValidOffset()) +$pagination = Pagination::fromInput($msz->auditLog->countLogs(), 50); +if(!$pagination->validOffset) Template::throwError(404); $logs = iterator_to_array($msz->auditLog->getLogs(pagination: $pagination)); diff --git a/public-legacy/manage/news/categories.php b/public-legacy/manage/news/categories.php index 43b2810..7bd41b1 100644 --- a/public-legacy/manage/news/categories.php +++ b/public-legacy/manage/news/categories.php @@ -7,9 +7,8 @@ if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext)) if(!$msz->authInfo->getPerms('global')->check(Perm::G_NEWS_CATEGORIES_MANAGE)) Template::throwError(403); -$pagination = new Pagination($msz->news->countCategories(), 15); - -if(!$pagination->hasValidOffset()) +$pagination = Pagination::fromInput($msz->news->countCategories(), 15); +if(!$pagination->validOffset) Template::throwError(404); $categories = $msz->news->getCategories(pagination: $pagination); diff --git a/public-legacy/manage/news/posts.php b/public-legacy/manage/news/posts.php index 6cd06a5..ccdda30 100644 --- a/public-legacy/manage/news/posts.php +++ b/public-legacy/manage/news/posts.php @@ -7,12 +7,12 @@ if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext)) if(!$msz->authInfo->getPerms('global')->check(Perm::G_NEWS_POSTS_MANAGE)) Template::throwError(403); -$pagination = new Pagination($msz->news->countPosts( +$pagination = Pagination::fromInput($msz->news->countPosts( includeScheduled: true, includeDeleted: true ), 15); -if(!$pagination->hasValidOffset()) +if(!$pagination->validOffset) Template::throwError(404); $posts = $msz->news->getPosts( diff --git a/public-legacy/manage/users/bans.php b/public-legacy/manage/users/bans.php index f4af383..b2b985a 100644 --- a/public-legacy/manage/users/bans.php +++ b/public-legacy/manage/users/bans.php @@ -19,9 +19,8 @@ if(filter_has_var(INPUT_GET, 'u')) { } } -$pagination = new Pagination($msz->usersCtx->bans->countBans(userInfo: $filterUser), 10); - -if(!$pagination->hasValidOffset()) +$pagination = Pagination::fromInput($msz->usersCtx->bans->countBans(userInfo: $filterUser), 10); +if(!$pagination->validOffset) Template::throwError(404); $banList = []; diff --git a/public-legacy/manage/users/index.php b/public-legacy/manage/users/index.php index 8bde1a3..331ec38 100644 --- a/public-legacy/manage/users/index.php +++ b/public-legacy/manage/users/index.php @@ -9,9 +9,8 @@ if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext)) if(!$msz->authInfo->getPerms('user')->check(Perm::U_USERS_MANAGE)) Template::throwError(403); -$pagination = new Pagination($msz->usersCtx->users->countUsers(), 30); - -if(!$pagination->hasValidOffset()) +$pagination = Pagination::fromInput($msz->usersCtx->users->countUsers(), 30); +if(!$pagination->validOffset) Template::throwError(404); $userList = []; diff --git a/public-legacy/manage/users/notes.php b/public-legacy/manage/users/notes.php index 16aba96..1489b4a 100644 --- a/public-legacy/manage/users/notes.php +++ b/public-legacy/manage/users/notes.php @@ -19,9 +19,8 @@ if(filter_has_var(INPUT_GET, 'u')) { } } -$pagination = new Pagination($msz->usersCtx->modNotes->countNotes(userInfo: $filterUser), 10); - -if(!$pagination->hasValidOffset()) +$pagination = Pagination::fromInput($msz->usersCtx->modNotes->countNotes(userInfo: $filterUser), 10); +if(!$pagination->validOffset) Template::throwError(404); $notes = []; diff --git a/public-legacy/manage/users/roles.php b/public-legacy/manage/users/roles.php index 2ba7b61..9915cda 100644 --- a/public-legacy/manage/users/roles.php +++ b/public-legacy/manage/users/roles.php @@ -8,9 +8,8 @@ if(!$msz->authInfo->getPerms('user')->check(Perm::U_ROLES_MANAGE)) Template::throwError(403); $roles = $msz->usersCtx->roles; -$pagination = new Pagination($roles->countRoles(), 10); - -if(!$pagination->hasValidOffset()) +$pagination = Pagination::fromInput($roles->countRoles(), 10); +if(!$pagination->validOffset) Template::throwError(404); $rolesAll = []; diff --git a/public-legacy/manage/users/warnings.php b/public-legacy/manage/users/warnings.php index e227925..a35af7e 100644 --- a/public-legacy/manage/users/warnings.php +++ b/public-legacy/manage/users/warnings.php @@ -19,9 +19,8 @@ if(filter_has_var(INPUT_GET, 'u')) { } } -$pagination = new Pagination($msz->usersCtx->warnings->countWarnings(userInfo: $filterUser), 10); - -if(!$pagination->hasValidOffset()) +$pagination = Pagination::fromInput($msz->usersCtx->warnings->countWarnings(userInfo: $filterUser), 10); +if(!$pagination->validOffset) Template::throwError(404); $warnList = []; diff --git a/public-legacy/members.php b/public-legacy/members.php index 87e396c..8d84f0d 100644 --- a/public-legacy/members.php +++ b/public-legacy/members.php @@ -76,7 +76,7 @@ $canManageUsers = $msz->authInfo->getPerms('user')->check(Perm::U_USERS_MANAGE); $deleted = $canManageUsers ? null : false; $rolesAll = $msz->usersCtx->roles->getRoles(hidden: false); -$pagination = new Pagination($msz->usersCtx->users->countUsers(roleInfo: $roleInfo, deleted: $deleted), 15); +$pagination = Pagination::fromInput($msz->usersCtx->users->countUsers(roleInfo: $roleInfo, deleted: $deleted), 15); $userList = []; $userInfos = $msz->usersCtx->users->getUsers( diff --git a/public-legacy/settings/logs.php b/public-legacy/settings/logs.php index ce5e4c1..82f34f6 100644 --- a/public-legacy/settings/logs.php +++ b/public-legacy/settings/logs.php @@ -10,8 +10,8 @@ $currentUser = $msz->authInfo->userInfo; if($currentUser === null) Template::throwError(401); -$loginHistoryPagination = new Pagination($msz->authCtx->loginAttempts->countAttempts(userInfo: $currentUser), 5, 'hp'); -$accountLogPagination = new Pagination($msz->auditLog->countLogs(userInfo: $currentUser), 10, 'ap'); +$loginHistoryPagination = Pagination::fromInput($msz->authCtx->loginAttempts->countAttempts(userInfo: $currentUser), 5, 'hp'); +$accountLogPagination = Pagination::fromInput($msz->auditLog->countLogs(userInfo: $currentUser), 10, 'ap'); $loginHistory = iterator_to_array($msz->authCtx->loginAttempts->getAttempts(userInfo: $currentUser, pagination: $loginHistoryPagination)); $auditLogs = iterator_to_array($msz->auditLog->getLogs(userInfo: $currentUser, pagination: $accountLogPagination)); diff --git a/public-legacy/settings/sessions.php b/public-legacy/settings/sessions.php index a325ea1..cd722bc 100644 --- a/public-legacy/settings/sessions.php +++ b/public-legacy/settings/sessions.php @@ -42,7 +42,7 @@ while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) { } else break; } -$pagination = new Pagination($msz->authCtx->sessions->countSessions(userInfo: $currentUser), 10); +$pagination = Pagination::fromInput($msz->authCtx->sessions->countSessions(userInfo: $currentUser), 10); $sessionList = []; $sessionInfos = $msz->authCtx->sessions->getSessions(userInfo: $currentUser, pagination: $pagination); diff --git a/src/AuditLog/AuditLog.php b/src/AuditLog/AuditLog.php index 7ed4ce7..eb1149e 100644 --- a/src/AuditLog/AuditLog.php +++ b/src/AuditLog/AuditLog.php @@ -87,8 +87,8 @@ class AuditLog { if($hasRemoteAddr) $stmt->addParameter(++$args, $remoteAddr); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Auth/LoginAttempts.php b/src/Auth/LoginAttempts.php index 5325556..ba604c5 100644 --- a/src/Auth/LoginAttempts.php +++ b/src/Auth/LoginAttempts.php @@ -112,8 +112,8 @@ class LoginAttempts { if($hasTimeRange) $stmt->addParameter(++$args, $timeRange); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Auth/Sessions.php b/src/Auth/Sessions.php index 40db162..7aa0eca 100644 --- a/src/Auth/Sessions.php +++ b/src/Auth/Sessions.php @@ -78,8 +78,8 @@ class Sessions { if($hasUserInfo) $stmt->addParameter(++$args, $userInfo); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Changelog/Changelog.php b/src/Changelog/Changelog.php index 97baf63..3ef1caf 100644 --- a/src/Changelog/Changelog.php +++ b/src/Changelog/Changelog.php @@ -166,8 +166,8 @@ class Changelog { foreach($tags as $tag) $stmt->addParameter(++$args, (string)$tag); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Changelog/ChangelogRoutes.php b/src/Changelog/ChangelogRoutes.php index 97cf824..be91941 100644 --- a/src/Changelog/ChangelogRoutes.php +++ b/src/Changelog/ChangelogRoutes.php @@ -64,8 +64,8 @@ final class ChangelogRoutes implements RouteHandler, UrlSource { } $count = $this->changelog->countChanges($filterUser, $filterDate, $filterTags); - $pagination = new Pagination($count, 30); - if(!$pagination->hasValidOffset()) + $pagination = Pagination::fromRequest($request, $count, 30, 'p'); + if(!$pagination->validOffset) return 404; $changes = []; diff --git a/src/Comments/Comments.php b/src/Comments/Comments.php index f55d0ea..6f5fd5b 100644 --- a/src/Comments/Comments.php +++ b/src/Comments/Comments.php @@ -65,8 +65,8 @@ class Comments { if($hasOwner) $stmt->addParameter(++$args, $owner); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Counters/Counters.php b/src/Counters/Counters.php index 6fb005f..e7cb45f 100644 --- a/src/Counters/Counters.php +++ b/src/Counters/Counters.php @@ -41,8 +41,8 @@ class Counters { $args = 0; $stmt = $this->cache->get($query); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Forum/ForumCategories.php b/src/Forum/ForumCategories.php index f8cebbf..d8a4cdb 100644 --- a/src/Forum/ForumCategories.php +++ b/src/Forum/ForumCategories.php @@ -154,8 +154,8 @@ class ForumCategories { if($hasType) $stmt->addParameter(++$args, $type); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Forum/ForumPosts.php b/src/Forum/ForumPosts.php index 77dc7c1..de429b8 100644 --- a/src/Forum/ForumPosts.php +++ b/src/Forum/ForumPosts.php @@ -186,8 +186,8 @@ class ForumPosts { if($hasSearchQuery) $stmt->addParameter(++$args, $searchQuery); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Forum/ForumPostsRoutes.php b/src/Forum/ForumPostsRoutes.php index 600a1b1..a841db5 100644 --- a/src/Forum/ForumPostsRoutes.php +++ b/src/Forum/ForumPostsRoutes.php @@ -42,12 +42,12 @@ class ForumPostsRoutes implements RouteHandler, UrlSource { upToPostInfo: $post, deleted: $canDeleteAny ? null : false ); - $pageNumber = (int)ceil($postsCount / 10); // epic magic number + $pageNumber = ((int)floor($postsCount / 10)) + 1; // epic magic number return $response->redirect($this->urls->format('forum-topic', [ 'topic' => $post->topicId, 'page' => $pageNumber, - 'post' => sprintf('p%s', $post->id), + 'topic_fragment' => sprintf('p%s', $post->id), ])); } diff --git a/src/Forum/ForumTopicRedirects.php b/src/Forum/ForumTopicRedirects.php index 4760813..c04f551 100644 --- a/src/Forum/ForumTopicRedirects.php +++ b/src/Forum/ForumTopicRedirects.php @@ -56,8 +56,8 @@ class ForumTopicRedirects { if($hasUserInfo) $stmt->addParameter(++$args, $userInfo); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Forum/ForumTopics.php b/src/Forum/ForumTopics.php index c1ecd91..1146eec 100644 --- a/src/Forum/ForumTopics.php +++ b/src/Forum/ForumTopics.php @@ -193,8 +193,8 @@ class ForumTopics { if($hasSearchQuery) $stmt->addParameter(++$args, $searchQuery); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/LegacyRoutes.php b/src/LegacyRoutes.php index bbb8f78..df5d906 100644 --- a/src/LegacyRoutes.php +++ b/src/LegacyRoutes.php @@ -34,7 +34,7 @@ class LegacyRoutes implements RouteHandler, UrlSource { $urls->register('forum-reply-new', '/forum/posting.php', ['t' => '']); $urls->register('forum-category', '/forum/forum.php', ['f' => '', 'p' => '']); $urls->register('forum-category-root', '/forum/index.php', fragment: ''); - $urls->register('forum-topic', '/forum/topic.php', ['t' => '', 'page' => ''], ''); + $urls->register('forum-topic', '/forum/topic.php', ['t' => '', 'page' => ''], ''); $urls->register('forum-topic-create', '/forum/posting.php', ['f' => '']); $urls->register('forum-post', '/forum/topic.php', ['p' => ''], 'p'); $urls->register('forum-post-create', '/forum/posting.php', ['t' => '']); diff --git a/src/Messages/MessagesDatabase.php b/src/Messages/MessagesDatabase.php index 994b023..fdc52f6 100644 --- a/src/Messages/MessagesDatabase.php +++ b/src/Messages/MessagesDatabase.php @@ -148,8 +148,8 @@ class MessagesDatabase { if($hasReplyTo) $stmt->addParameter(++$args, $replyTo instanceof MessageInfo ? $replyTo->replyToId : $replyTo); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Messages/MessagesRoutes.php b/src/Messages/MessagesRoutes.php index 8cb1d80..b89e018 100644 --- a/src/Messages/MessagesRoutes.php +++ b/src/Messages/MessagesRoutes.php @@ -116,13 +116,13 @@ class MessagesRoutes implements RouteHandler, UrlSource { $sent = $folderTrash ? null : !$folderDrafts; $deleted = $folderTrash; - $pagination = new Pagination($msgsDb->countMessages( + $pagination = Pagination::fromRequest($request, $msgsDb->countMessages( ownerInfo: $selfInfo, authorInfo: $authorInfo, recipientInfo: $recipientInfo, sent: $sent, deleted: $deleted, - ), 50, 'page'); + ), 50); $messageInfos = $msgsDb->getMessages( ownerInfo: $selfInfo, diff --git a/src/News/News.php b/src/News/News.php index 8e9b4fc..bd6a466 100644 --- a/src/News/News.php +++ b/src/News/News.php @@ -54,8 +54,8 @@ class News { $args = 0; if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); @@ -259,8 +259,8 @@ class News { if($hasSearchQuery) $stmt->addParameter(++$args, $searchQuery); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/News/NewsRoutes.php b/src/News/NewsRoutes.php index 2f09ce8..15bbfcb 100644 --- a/src/News/NewsRoutes.php +++ b/src/News/NewsRoutes.php @@ -101,11 +101,11 @@ class NewsRoutes implements RouteHandler, UrlSource { #[HttpGet('/news')] #[UrlFormat('news-index', '/news', ['p' => ''])] - public function getIndex(): int|string { + public function getIndex(HttpResponseBuilder $response, HttpRequest $request): int|string { $categories = $this->news->getCategories(hidden: false); - $pagination = new Pagination($this->news->countPosts(onlyFeatured: true), 5); - if(!$pagination->hasValidOffset()) + $pagination = Pagination::fromRequest($request, $this->news->countPosts(onlyFeatured: true), 5, 'p'); + if(!$pagination->validOffset) return 404; $posts = $this->getNewsPostsForView($pagination); @@ -129,8 +129,8 @@ class NewsRoutes implements RouteHandler, UrlSource { if($type !== '') return $this->getFeed($response, $request, $categoryInfo); - $pagination = new Pagination($this->news->countPosts(categoryInfo: $categoryInfo), 5); - if(!$pagination->hasValidOffset()) + $pagination = Pagination::fromRequest($request, $this->news->countPosts(categoryInfo: $categoryInfo), 5, 'p'); + if(!$pagination->validOffset) return 404; $posts = $this->getNewsPostsForView($pagination, $categoryInfo); diff --git a/src/Pagination.php b/src/Pagination.php index 7a1115d..4b7ba18 100644 --- a/src/Pagination.php +++ b/src/Pagination.php @@ -1,79 +1,74 @@ count = max(0, $count); - $this->range = $range < 0 ? $count : $range; + if($range < 1) + $this->range = $count; - if(!empty($readParam)) - $this->readPage($readParam); + $this->validOffset = $offset >= 0 && $offset < $count; + $this->offset = max(0, $offset); } - public function getCount(): int { - return $this->count; + public int $pages { + get => (int)ceil($this->count / $this->range); } - public function getRange(): int { - return $this->range; + public int $page { + get => (int)floor($this->offset / $this->range) + 1; } - public function getPages(): int { - return (int)ceil($this->getCount() / $this->getRange()); + public static function fromPage( + int $count, + int $page, + int $range = 0, + int $firstPage = 1 + ): self { + if($range < 1) + $range = $count; + + $offset = $range * ($page - $firstPage); + if($offset < 0 || $offset >= $count) + $offset = -1; + + return new Pagination($count, $range, $offset); } - public function hasValidOffset(): bool { - return $this->offset !== self::INVALID_OFFSET; + public static function fromInput( + int $count, + int $range = 0, + string $pageParam = 'p', + int $firstPage = 1 + ): self { + return self::fromPage( + $count, + (int)(filter_input(INPUT_GET, $pageParam, FILTER_SANITIZE_NUMBER_INT) ?? $firstPage), + $range, + $firstPage + ); } - public function getOffset(): int { - return $this->hasValidOffset() ? $this->offset : 0; - } - - public function setOffset(int $offset): self { - if($offset < 0) - $offset = self::INVALID_OFFSET; - - $this->offset = $offset; - return $this; - } - - public function getPage(): int { - if($this->getOffset() < 1) - return self::START_PAGE; - - return (int)floor($this->getOffset() / $this->getRange()) + self::START_PAGE; - } - - public function setPage(int $page, bool $zeroBased = false): self { - if(!$zeroBased) - $page -= self::START_PAGE; - - $this->setOffset($this->getRange() * $page); - return $this; - } - - /** @param ?array $source */ - public function readPage(string $name = self::DEFAULT_PARAM, int $default = self::START_PAGE, ?array $source = null): self { - $this->setPage(self::param($name, $default, $source)); - return $this; - } - - /** @param ?array $source */ - public static function param(string $name = self::DEFAULT_PARAM, int $default = self::START_PAGE, ?array $source = null): int { - $source ??= $_GET; - - if(isset($source[$name]) && is_string($source[$name]) && ctype_digit($source[$name])) - return (int)$source[$name]; - - return $default; + public static function fromRequest( + HttpRequest $request, + int $count, + int $range = 0, + string $pageParam = 'page', + int $firstPage = 1 + ): Pagination { + return self::fromPage( + $count, + (int)($request->getParam($pageParam, FILTER_SANITIZE_NUMBER_INT) ?? $firstPage), + $range, + $firstPage + ); } } diff --git a/src/Users/Bans.php b/src/Users/Bans.php index df272ad..e318144 100644 --- a/src/Users/Bans.php +++ b/src/Users/Bans.php @@ -95,8 +95,8 @@ class Bans { if($hasUserInfo) $stmt->addParameter(++$args, $userInfo); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Users/ModNotes.php b/src/Users/ModNotes.php index 64b1760..34534e9 100644 --- a/src/Users/ModNotes.php +++ b/src/Users/ModNotes.php @@ -87,8 +87,8 @@ class ModNotes { if($hasAuthorInfo) $stmt->addParameter(++$args, $authorInfo); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Users/Roles.php b/src/Users/Roles.php index f55066a..f4c41fd 100644 --- a/src/Users/Roles.php +++ b/src/Users/Roles.php @@ -86,8 +86,8 @@ class Roles { if($hasUserInfo) $stmt->nextParameter($userInfo); if($hasPagination) { - $stmt->nextParameter($pagination->getRange()); - $stmt->nextParameter($pagination->getOffset()); + $stmt->nextParameter($pagination->range); + $stmt->nextParameter($pagination->offset); } $stmt->execute(); diff --git a/src/Users/Users.php b/src/Users/Users.php index cac0c01..d3ba114 100644 --- a/src/Users/Users.php +++ b/src/Users/Users.php @@ -207,8 +207,8 @@ class Users { if($hasSearchQuery) $stmt->addParameter(++$args, $searchQuery); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute(); diff --git a/src/Users/Warnings.php b/src/Users/Warnings.php index 36046bf..4c21c38 100644 --- a/src/Users/Warnings.php +++ b/src/Users/Warnings.php @@ -102,8 +102,8 @@ class Warnings { if($hasBacklog) $stmt->addParameter(++$args, $backlog); if($hasPagination) { - $stmt->addParameter(++$args, $pagination->getRange()); - $stmt->addParameter(++$args, $pagination->getOffset()); + $stmt->addParameter(++$args, $pagination->range); + $stmt->addParameter(++$args, $pagination->offset); } $stmt->execute();