From 923207fdfa2f71e75486e672f5b96b41edc56178 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 4 May 2014 09:11:08 +0200 Subject: [PATCH] Organized common paging code into abstraction --- src/Api/AbstractPageJob.php | 33 +++++++++++++++++++++++++++ src/Api/Jobs/GetLogJob.php | 19 +++++++-------- src/Api/Jobs/ListCommentsJob.php | 26 +++++++++------------ src/Api/Jobs/ListPostsJob.php | 22 ++++++++---------- src/Api/Jobs/ListTagsJob.php | 22 ++++++++---------- src/Controllers/CommentController.php | 8 ++----- src/Controllers/LogController.php | 17 ++++++-------- src/Controllers/PostController.php | 8 ++----- src/Controllers/TagController.php | 8 ++----- 9 files changed, 83 insertions(+), 80 deletions(-) create mode 100644 src/Api/AbstractPageJob.php diff --git a/src/Api/AbstractPageJob.php b/src/Api/AbstractPageJob.php new file mode 100644 index 00000000..35cbddd5 --- /dev/null +++ b/src/Api/AbstractPageJob.php @@ -0,0 +1,33 @@ +pageSize === null + ? $this->getDefaultPageSize() + : $this->pageSize; + } + + public function setPageSize($pageSize) + { + $this->pageSize = $pageSize; + return $this; + } + + public function getPager($entities, $entityCount, $page, $pageSize) + { + $pageCount = ceil($entityCount / $pageSize); + $page = min($pageCount, $page); + + $ret = new StdClass; + $ret->entities = $entities; + $ret->entityCount = $entityCount; + $ret->page = $page; + $ret->pageCount = $pageCount; + return $ret; + } +} diff --git a/src/Api/Jobs/GetLogJob.php b/src/Api/Jobs/GetLogJob.php index a886a6e5..efd203d5 100644 --- a/src/Api/Jobs/GetLogJob.php +++ b/src/Api/Jobs/GetLogJob.php @@ -1,8 +1,9 @@ getPageSize(); $page = $this->getArgument(self::PAGE_NUMBER); $name = $this->getArgument(self::LOG_ID); $query = $this->getArgument(self::QUERY); @@ -28,18 +29,14 @@ class GetLogJob extends AbstractJob } $lineCount = count($lines); - $logsPerPage = intval(getConfig()->browsing->logsPerPage); - $pageCount = ceil($lineCount / $logsPerPage); - $page = min($pageCount, $page); + $lines = array_slice($lines, ($page - 1) * $pageSize, $pageSize); - $lines = array_slice($lines, ($page - 1) * $logsPerPage, $logsPerPage); + return $this->getPager($lines, $lineCount, $page, $pageSize); + } - $ret = new StdClass; - $ret->lines = $lines; - $ret->lineCount = $lineCount; - $ret->page = $page; - $ret->pageCount = $pageCount; - return $ret; + public function getDefaultPageSize() + { + return intval(getConfig()->browsing->logsPerPage); } public function requiresPrivilege() diff --git a/src/Api/Jobs/ListCommentsJob.php b/src/Api/Jobs/ListCommentsJob.php index 463dc806..1b166e7f 100644 --- a/src/Api/Jobs/ListCommentsJob.php +++ b/src/Api/Jobs/ListCommentsJob.php @@ -1,18 +1,14 @@ getPageSize(); $page = $this->getArgument(self::PAGE_NUMBER); + $query = 'comment_min:1 order:comment_date,desc'; - $page = max(1, intval($page)); - $commentsPerPage = intval(getConfig()->comments->commentsPerPage); - $searchQuery = 'comment_min:1 order:comment_date,desc'; - - $posts = PostSearchService::getEntities($searchQuery, $commentsPerPage, $page); - $postCount = PostSearchService::getEntityCount($searchQuery); - $pageCount = ceil($postCount / $commentsPerPage); - $page = min($pageCount, $page); + $posts = PostSearchService::getEntities($query, $pageSize, $page); + $postCount = PostSearchService::getEntityCount($query); PostModel::preloadTags($posts); PostModel::preloadComments($posts); @@ -21,12 +17,12 @@ class ListCommentsJob extends AbstractJob $comments = array_merge($comments, $post->getComments()); CommentModel::preloadCommenters($comments); - $ret = new StdClass; - $ret->posts = $posts; - $ret->postCount = $postCount; - $ret->page = $page; - $ret->pageCount = $pageCount; - return $ret; + return $this->getPager($posts, $postCount, $page, $pageSize); + } + + public function getDefaultPageSize() + { + return intval(getConfig()->comments->commentsPerPage); } public function requiresPrivilege() diff --git a/src/Api/Jobs/ListPostsJob.php b/src/Api/Jobs/ListPostsJob.php index 524493ba..ba4490a3 100644 --- a/src/Api/Jobs/ListPostsJob.php +++ b/src/Api/Jobs/ListPostsJob.php @@ -1,27 +1,23 @@ getPageSize(); $page = $this->getArgument(self::PAGE_NUMBER); $query = $this->getArgument(self::QUERY); - $page = max(1, intval($page)); - $postsPerPage = intval(getConfig()->browsing->postsPerPage); - - $posts = PostSearchService::getEntities($query, $postsPerPage, $page); + $posts = PostSearchService::getEntities($query, $pageSize, $page); $postCount = PostSearchService::getEntityCount($query); - $pageCount = ceil($postCount / $postsPerPage); - $page = min($pageCount, $page); PostModel::preloadTags($posts); - $ret = new StdClass; - $ret->posts = $posts; - $ret->postCount = $postCount; - $ret->page = $page; - $ret->pageCount = $pageCount; - return $ret; + return $this->getPager($posts, $postCount, $page, $pageSize); + } + + public function getDefaultPageSize() + { + return intval(getConfig()->browsing->postsPerPage); } public function requiresPrivilege() diff --git a/src/Api/Jobs/ListTagsJob.php b/src/Api/Jobs/ListTagsJob.php index 2ebf6b83..288668d1 100644 --- a/src/Api/Jobs/ListTagsJob.php +++ b/src/Api/Jobs/ListTagsJob.php @@ -1,25 +1,21 @@ getPageSize(); $page = $this->getArgument(self::PAGE_NUMBER); $query = $this->getArgument(self::QUERY); - $page = max(1, intval($page)); - $tagsPerPage = intval(getConfig()->browsing->tagsPerPage); - - $tags = TagSearchService::getEntitiesRows($query, $tagsPerPage, $page); + $tags = TagSearchService::getEntitiesRows($query, $pageSize, $page); $tagCount = TagSearchService::getEntityCount($query); - $pageCount = ceil($tagCount / $tagsPerPage); - $page = min($pageCount, $page); - $ret = new StdClass; - $ret->tags = $tags; - $ret->tagCount = $tagCount; - $ret->page = $page; - $ret->pageCount = $pageCount; - return $ret; + return $this->getPager($tags, $tagCount, $page, $pageSize); + } + + public function getDefaultPageSize() + { + return intval(getConfig()->browsing->tagsPerPage); } public function requiresPrivilege() diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php index 3218d40b..aa670a0e 100644 --- a/src/Controllers/CommentController.php +++ b/src/Controllers/CommentController.php @@ -10,12 +10,8 @@ class CommentController ]); $context = getContext(); - $context->transport->posts = $ret->posts; - $context->transport->paginator = new StdClass; - $context->transport->paginator->page = $ret->page; - $context->transport->paginator->pageCount = $ret->pageCount; - $context->transport->paginator->entityCount = $ret->postCount; - $context->transport->paginator->entities = $ret->posts; + $context->transport->posts = $ret->entities; + $context->transport->paginator = $ret; } public function previewAction() diff --git a/src/Controllers/LogController.php b/src/Controllers/LogController.php index 36fa1130..69b48fa6 100644 --- a/src/Controllers/LogController.php +++ b/src/Controllers/LogController.php @@ -36,21 +36,18 @@ class LogController ]); //stylize important lines - foreach ($ret->lines as &$line) + $lines = $ret->entities; + foreach ($lines as &$line) if (strpos($line, 'flag') !== false) $line = '**' . $line . '**'; unset($line); - $ret->lines = join(PHP_EOL, $ret->lines); - $ret->lines = TextHelper::parseMarkdown($ret->lines, true); - $ret->lines = trim($ret->lines); + $lines = join(PHP_EOL, $lines); + $lines = TextHelper::parseMarkdown($lines, true); + $lines = trim($lines); - $context->transport->paginator = new StdClass; - $context->transport->paginator->page = $ret->page; - $context->transport->paginator->pageCount = $ret->pageCount; - $context->transport->paginator->entityCount = $ret->lineCount; - $context->transport->paginator->entities = $ret->lines; - $context->transport->lines = $ret->lines; + $context->transport->paginator = $ret; + $context->transport->lines = $lines; $context->transport->filter = $filter; $context->transport->name = $name; } diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 3cb17cd1..e11d08a5 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -46,12 +46,8 @@ class PostController ListPostsJob::QUERY => $query ]); - $context->transport->posts = $ret->posts; - $context->transport->paginator = new StdClass; - $context->transport->paginator->page = $ret->page; - $context->transport->paginator->pageCount = $ret->pageCount; - $context->transport->paginator->entityCount = $ret->postCount; - $context->transport->paginator->entities = $ret->posts; + $context->transport->posts = $ret->entities; + $context->transport->paginator = $ret; } public function favoritesView($page = 1) diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index f57185f8..c0b28a33 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -14,12 +14,8 @@ class TagController $context->viewName = 'tag-list-wrapper'; $context->highestUsage = TagSearchService::getMostUsedTag()['post_count']; $context->filter = $filter; - $context->transport->tags = $ret->tags; - $context->transport->paginator = new StdClass; - $context->transport->paginator->page = $ret->page; - $context->transport->paginator->pageCount = $ret->pageCount; - $context->transport->paginator->entityCount = $ret->tagCount; - $context->transport->paginator->entities = $ret->tags; + $context->transport->tags = $ret->entities; + $context->transport->paginator = $ret; } public function autoCompleteAction()