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
class GetLogJob extends AbstractJob
class GetLogJob extends AbstractPageJob
{
public function execute()
{
$pageSize = $this->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()

View file

@ -1,18 +1,14 @@
<?php
class ListCommentsJob extends AbstractJob
class ListCommentsJob extends AbstractPageJob
{
public function execute()
{
$pageSize = $this->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()

View file

@ -1,27 +1,23 @@
<?php
class ListPostsJob extends AbstractJob
class ListPostsJob extends AbstractPageJob
{
public function execute()
{
$pageSize = $this->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()

View file

@ -1,25 +1,21 @@
<?php
class ListTagsJob extends AbstractJob
class ListTagsJob extends AbstractPageJob
{
public function execute()
{
$pageSize = $this->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()

View file

@ -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()

View file

@ -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;
}

View file

@ -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)

View file

@ -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()