Organized common paging code into abstraction

This commit is contained in:
Marcin Kurczewski 2014-05-04 09:11:08 +02:00
parent 97c17c68a0
commit 923207fdfa
9 changed files with 83 additions and 80 deletions

View file

@ -0,0 +1,33 @@
<?php
abstract class AbstractPageJob extends AbstractJob
{
protected $pageSize = null;
public abstract function getDefaultPageSize();
public function getPageSize()
{
return $this->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;
}
}

View file

@ -1,8 +1,9 @@
<?php <?php
class GetLogJob extends AbstractJob class GetLogJob extends AbstractPageJob
{ {
public function execute() public function execute()
{ {
$pageSize = $this->getPageSize();
$page = $this->getArgument(self::PAGE_NUMBER); $page = $this->getArgument(self::PAGE_NUMBER);
$name = $this->getArgument(self::LOG_ID); $name = $this->getArgument(self::LOG_ID);
$query = $this->getArgument(self::QUERY); $query = $this->getArgument(self::QUERY);
@ -28,18 +29,14 @@ class GetLogJob extends AbstractJob
} }
$lineCount = count($lines); $lineCount = count($lines);
$logsPerPage = intval(getConfig()->browsing->logsPerPage); $lines = array_slice($lines, ($page - 1) * $pageSize, $pageSize);
$pageCount = ceil($lineCount / $logsPerPage);
$page = min($pageCount, $page);
$lines = array_slice($lines, ($page - 1) * $logsPerPage, $logsPerPage); return $this->getPager($lines, $lineCount, $page, $pageSize);
}
$ret = new StdClass; public function getDefaultPageSize()
$ret->lines = $lines; {
$ret->lineCount = $lineCount; return intval(getConfig()->browsing->logsPerPage);
$ret->page = $page;
$ret->pageCount = $pageCount;
return $ret;
} }
public function requiresPrivilege() public function requiresPrivilege()

View file

@ -1,18 +1,14 @@
<?php <?php
class ListCommentsJob extends AbstractJob class ListCommentsJob extends AbstractPageJob
{ {
public function execute() public function execute()
{ {
$pageSize = $this->getPageSize();
$page = $this->getArgument(self::PAGE_NUMBER); $page = $this->getArgument(self::PAGE_NUMBER);
$query = 'comment_min:1 order:comment_date,desc';
$page = max(1, intval($page)); $posts = PostSearchService::getEntities($query, $pageSize, $page);
$commentsPerPage = intval(getConfig()->comments->commentsPerPage); $postCount = PostSearchService::getEntityCount($query);
$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);
PostModel::preloadTags($posts); PostModel::preloadTags($posts);
PostModel::preloadComments($posts); PostModel::preloadComments($posts);
@ -21,12 +17,12 @@ class ListCommentsJob extends AbstractJob
$comments = array_merge($comments, $post->getComments()); $comments = array_merge($comments, $post->getComments());
CommentModel::preloadCommenters($comments); CommentModel::preloadCommenters($comments);
$ret = new StdClass; return $this->getPager($posts, $postCount, $page, $pageSize);
$ret->posts = $posts; }
$ret->postCount = $postCount;
$ret->page = $page; public function getDefaultPageSize()
$ret->pageCount = $pageCount; {
return $ret; return intval(getConfig()->comments->commentsPerPage);
} }
public function requiresPrivilege() public function requiresPrivilege()

View file

@ -1,27 +1,23 @@
<?php <?php
class ListPostsJob extends AbstractJob class ListPostsJob extends AbstractPageJob
{ {
public function execute() public function execute()
{ {
$pageSize = $this->getPageSize();
$page = $this->getArgument(self::PAGE_NUMBER); $page = $this->getArgument(self::PAGE_NUMBER);
$query = $this->getArgument(self::QUERY); $query = $this->getArgument(self::QUERY);
$page = max(1, intval($page)); $posts = PostSearchService::getEntities($query, $pageSize, $page);
$postsPerPage = intval(getConfig()->browsing->postsPerPage);
$posts = PostSearchService::getEntities($query, $postsPerPage, $page);
$postCount = PostSearchService::getEntityCount($query); $postCount = PostSearchService::getEntityCount($query);
$pageCount = ceil($postCount / $postsPerPage);
$page = min($pageCount, $page);
PostModel::preloadTags($posts); PostModel::preloadTags($posts);
$ret = new StdClass; return $this->getPager($posts, $postCount, $page, $pageSize);
$ret->posts = $posts; }
$ret->postCount = $postCount;
$ret->page = $page; public function getDefaultPageSize()
$ret->pageCount = $pageCount; {
return $ret; return intval(getConfig()->browsing->postsPerPage);
} }
public function requiresPrivilege() public function requiresPrivilege()

View file

@ -1,25 +1,21 @@
<?php <?php
class ListTagsJob extends AbstractJob class ListTagsJob extends AbstractPageJob
{ {
public function execute() public function execute()
{ {
$pageSize = $this->getPageSize();
$page = $this->getArgument(self::PAGE_NUMBER); $page = $this->getArgument(self::PAGE_NUMBER);
$query = $this->getArgument(self::QUERY); $query = $this->getArgument(self::QUERY);
$page = max(1, intval($page)); $tags = TagSearchService::getEntitiesRows($query, $pageSize, $page);
$tagsPerPage = intval(getConfig()->browsing->tagsPerPage);
$tags = TagSearchService::getEntitiesRows($query, $tagsPerPage, $page);
$tagCount = TagSearchService::getEntityCount($query); $tagCount = TagSearchService::getEntityCount($query);
$pageCount = ceil($tagCount / $tagsPerPage);
$page = min($pageCount, $page);
$ret = new StdClass; return $this->getPager($tags, $tagCount, $page, $pageSize);
$ret->tags = $tags; }
$ret->tagCount = $tagCount;
$ret->page = $page; public function getDefaultPageSize()
$ret->pageCount = $pageCount; {
return $ret; return intval(getConfig()->browsing->tagsPerPage);
} }
public function requiresPrivilege() public function requiresPrivilege()

View file

@ -10,12 +10,8 @@ class CommentController
]); ]);
$context = getContext(); $context = getContext();
$context->transport->posts = $ret->posts; $context->transport->posts = $ret->entities;
$context->transport->paginator = new StdClass; $context->transport->paginator = $ret;
$context->transport->paginator->page = $ret->page;
$context->transport->paginator->pageCount = $ret->pageCount;
$context->transport->paginator->entityCount = $ret->postCount;
$context->transport->paginator->entities = $ret->posts;
} }
public function previewAction() public function previewAction()

View file

@ -36,21 +36,18 @@ class LogController
]); ]);
//stylize important lines //stylize important lines
foreach ($ret->lines as &$line) $lines = $ret->entities;
foreach ($lines as &$line)
if (strpos($line, 'flag') !== false) if (strpos($line, 'flag') !== false)
$line = '**' . $line . '**'; $line = '**' . $line . '**';
unset($line); unset($line);
$ret->lines = join(PHP_EOL, $ret->lines); $lines = join(PHP_EOL, $lines);
$ret->lines = TextHelper::parseMarkdown($ret->lines, true); $lines = TextHelper::parseMarkdown($lines, true);
$ret->lines = trim($ret->lines); $lines = trim($lines);
$context->transport->paginator = new StdClass; $context->transport->paginator = $ret;
$context->transport->paginator->page = $ret->page; $context->transport->lines = $lines;
$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->filter = $filter; $context->transport->filter = $filter;
$context->transport->name = $name; $context->transport->name = $name;
} }

View file

@ -46,12 +46,8 @@ class PostController
ListPostsJob::QUERY => $query ListPostsJob::QUERY => $query
]); ]);
$context->transport->posts = $ret->posts; $context->transport->posts = $ret->entities;
$context->transport->paginator = new StdClass; $context->transport->paginator = $ret;
$context->transport->paginator->page = $ret->page;
$context->transport->paginator->pageCount = $ret->pageCount;
$context->transport->paginator->entityCount = $ret->postCount;
$context->transport->paginator->entities = $ret->posts;
} }
public function favoritesView($page = 1) public function favoritesView($page = 1)

View file

@ -14,12 +14,8 @@ class TagController
$context->viewName = 'tag-list-wrapper'; $context->viewName = 'tag-list-wrapper';
$context->highestUsage = TagSearchService::getMostUsedTag()['post_count']; $context->highestUsage = TagSearchService::getMostUsedTag()['post_count'];
$context->filter = $filter; $context->filter = $filter;
$context->transport->tags = $ret->tags; $context->transport->tags = $ret->entities;
$context->transport->paginator = new StdClass; $context->transport->paginator = $ret;
$context->transport->paginator->page = $ret->page;
$context->transport->paginator->pageCount = $ret->pageCount;
$context->transport->paginator->entityCount = $ret->tagCount;
$context->transport->paginator->entities = $ret->tags;
} }
public function autoCompleteAction() public function autoCompleteAction()