Increased API readability

- Removed Abstract*Job hierarchy
- Introduced EntityRetrievers
- Introduced JobPager
- Moved files around
This commit is contained in:
Marcin Kurczewski 2014-05-12 16:46:14 +02:00
parent 484adbbf49
commit 098f11bd09
68 changed files with 857 additions and 472 deletions

View file

@ -33,12 +33,12 @@ final class Api
return $statuses;
}
public static function checkArguments(AbstractJob $job)
public static function checkArguments(IJob $job)
{
self::runArgumentCheck($job, $job->getRequiredArguments());
}
public static function checkPrivileges(AbstractJob $job)
public static function checkPrivileges(IJob $job)
{
if ($job->isAuthenticationRequired())
Access::assertAuthentication();
@ -57,7 +57,7 @@ final class Api
}
}
private static function runArgumentCheck($job, $item)
private static function runArgumentCheck(IJob $job, $item)
{
if (is_array($item))
throw new Exception('Argument definition cannot be an array.');

View file

@ -1,7 +1,7 @@
<?php
class ApiJobUnsatisfiedException extends SimpleException
{
public function __construct(AbstractJob $job, $arg = null)
public function __construct(IJob $job, $arg = null)
{
parent::__construct('%s cannot be run due to unsatisfied execution conditions (%s).',
get_class($job),

View file

@ -0,0 +1,36 @@
<?php
class CommentRetriever implements IEntityRetriever
{
private $job;
public function __construct(IJob $job)
{
$this->job = $job;
}
public function tryRetrieve()
{
if ($this->job->hasArgument(JobArgs::ARG_COMMENT_ENTITY))
return $this->job->getArgument(JobArgs::ARG_COMMENT_ENTITY);
if ($this->job->hasArgument(JobArgs::ARG_COMMENT_ID))
return CommentModel::getById($this->job->getArgument(JobArgs::ARG_COMMENT_ID));
return null;
}
public function retrieve()
{
$comment = $this->tryRetrieve();
if ($comment)
return $comment;
throw new ApiJobUnsatisfiedException($this->job);
}
public function getRequiredArguments()
{
return JobArgs::Alternative(
JobArgs::ARG_COMMENT_ID,
JobArgs::ARG_COMMENT_ENTITY);
}
}

View file

@ -0,0 +1,8 @@
<?php
interface IEntityRetriever
{
public function __construct(IJob $job);
public function tryRetrieve();
public function retrieve();
public function getRequiredArguments();
}

View file

@ -0,0 +1,40 @@
<?php
class PostRetriever implements IEntityRetriever
{
private $job;
public function __construct(IJob $job)
{
$this->job = $job;
}
public function tryRetrieve()
{
if ($this->job->hasArgument(JobArgs::ARG_POST_ENTITY))
return $this->job->getArgument(JobArgs::ARG_POST_ENTITY);
if ($this->job->hasArgument(JobArgs::ARG_POST_ID))
return PostModel::getById($this->job->getArgument(JobArgs::ARG_POST_ID));
if ($this->job->hasArgument(JobArgs::ARG_POST_NAME))
return PostModel::getByName($job->getArgument(JobArgs::ARG_POST_NAME));
return null;
}
public function retrieve()
{
$post = $this->tryRetrieve();
if ($post)
return $post;
throw new ApiJobUnsatisfiedException($this->job);
}
public function getRequiredArguments()
{
return JobArgs::Alternative(
JobArgs::ARG_POST_ID,
JobArgs::ARG_POST_NAME,
JobArgs::ARG_POST_ENTITY);
}
}

View file

@ -0,0 +1,36 @@
<?php
class SafePostRetriever implements IEntityRetriever
{
private $job;
public function __construct(IJob $job)
{
$this->job = $job;
}
public function tryRetrieve()
{
if ($this->job->hasArgument(JobArgs::ARG_POST_ENTITY))
return $this->job->getArgument(JobArgs::ARG_POST_ENTITY);
if ($this->job->hasArgument(JobArgs::ARG_POST_NAME))
return PostModel::getByName($this->job->getArgument(JobArgs::ARG_POST_NAME));
return null;
}
public function retrieve()
{
$post = $this->tryRetrieve();
if ($post)
return $post;
throw new ApiJobUnsatisfiedException($this->job);
}
public function getRequiredArguments()
{
return JobArgs::Alternative(
JobArgs::ARG_POST_NAME,
JobArgs::ARG_POST_ENTITY);
}
}

View file

@ -0,0 +1,37 @@
<?php
class UserRetriever implements IEntityRetriever
{
private $job;
public function __construct(IJob $job)
{
$this->job = $job;
}
public function tryRetrieve()
{
if ($this->job->hasArgument(JobArgs::ARG_USER_ENTITY))
return $this->job->getArgument(JobArgs::ARG_USER_ENTITY);
if ($this->job->hasArgument(JobArgs::ARG_USER_NAME))
return UserModel::getByNameOrEmail($this->job->getArgument(JobArgs::ARG_USER_NAME));
return null;
}
public function retrieve()
{
$user = $this->tryRetrieve();
if ($user)
return $user;
throw new ApiJobUnsatisfiedException($this->job);
}
public function getRequiredArguments()
{
return JobArgs::Alternative(
JobArgs::ARG_USER_NAME,
JobArgs::ARG_USER_EMAIL,
JobArgs::ARG_USER_ENTITY);
}
}

View file

@ -0,0 +1,50 @@
<?php
class JobPager
{
private $job;
public function __construct(IJob $job)
{
$this->pageSize = 20;
$this->job = $job;
}
public function setPageSize($newPageSize)
{
$this->pageSize = $newPageSize;
}
public function getPageSize()
{
return $this->pageSize;
}
public function getPageNumber()
{
if ($this->job->hasArgument(JobArgs::ARG_PAGE_NUMBER))
return (int) $this->job->getArgument(JobArgs::ARG_PAGE_NUMBER);
return 1;
}
public function getRequiredArguments()
{
return JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER);
}
public function serialize($entities, $totalEntityCount)
{
$pageSize = $this->getPageSize();
$pageNumber = $this->getPageNumber();
$pageCount = ceil($totalEntityCount / $pageSize);
$pageNumber = $this->getPageNumber();
$pageNumber = min($pageCount, $pageNumber);
$ret = new StdClass;
$ret->entities = $entities;
$ret->entityCount = $totalEntityCount;
$ret->page = $pageNumber;
$ret->pageCount = $pageCount;
return $ret;
}
}

View file

@ -1,5 +1,5 @@
<?php
abstract class AbstractJob
abstract class AbstractJob implements IJob
{
const CONTEXT_NORMAL = 1;
const CONTEXT_BATCH_EDIT = 2;
@ -14,10 +14,7 @@ abstract class AbstractJob
public abstract function execute();
public function getRequiredArguments()
{
return null;
}
public abstract function getRequiredArguments();
public function getRequiredPrivileges()
{

View file

@ -1,29 +0,0 @@
<?php
abstract class AbstractCommentJob extends AbstractJob
{
protected $comment;
public function prepare()
{
if ($this->hasArgument(JobArgs::ARG_COMMENT_ENTITY))
{
$this->comment = $this->getArgument(JobArgs::ARG_COMMENT_ENTITY);
}
else
{
$commentId = $this->getArgument(JobArgs::ARG_COMMENT_ID);
$this->comment = CommentModel::getById($commentId);
}
}
public function getRequiredArguments()
{
return JobArgs::Conjunction(
JobArgs::Alternative(
JobArgs::ARG_COMMENT_ID,
JobArgs::ARG_COMMENT_ENTITY),
$this->getRequiredSubArguments());
}
public abstract function getRequiredSubArguments();
}

View file

@ -1,9 +1,16 @@
<?php
class AddCommentJob extends AbstractPostJob
class AddCommentJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$user = Auth::getCurrentUser();
$text = $this->getArgument(JobArgs::ARG_NEW_TEXT);
@ -21,9 +28,11 @@ class AddCommentJob extends AbstractPostJob
return $comment;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_TEXT;
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_TEXT);
}
public function getRequiredPrivileges()

View file

@ -1,27 +1,35 @@
<?php
class DeleteCommentJob extends AbstractCommentJob
class DeleteCommentJob extends AbstractJob
{
protected $commentRetriever;
public function __construct()
{
$this->commentRetriever = new CommentRetriever($this);
}
public function execute()
{
$post = $this->comment->getPost();
$comment = $this->commentRetriever->retrieve();
$post = $comment->getPost();
CommentModel::remove($this->comment);
CommentModel::remove($comment);
Logger::log('{user} removed comment from {post}', [
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
'post' => TextHelper::reprPost($post)]);
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return null;
return $this->commentRetriever->getRequiredArguments();
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::DeleteComment,
Access::getIdentity($this->comment->getCommenter()));
Access::getIdentity($this->commentRetriever->retrieve()->getCommenter()));
}
public function isAuthenticationRequired()

View file

@ -1,9 +1,16 @@
<?php
class EditCommentJob extends AbstractCommentJob
class EditCommentJob extends AbstractJob
{
protected $commentRetriever;
public function __construct()
{
$this->commentRetriever = new CommentRetriever($this);
}
public function execute()
{
$comment = $this->comment;
$comment = $this->commentRetriever->retrieve();
$comment->setCreationTime(time());
$comment->setText($this->getArgument(JobArgs::ARG_NEW_TEXT));
@ -16,16 +23,18 @@ class EditCommentJob extends AbstractCommentJob
return $comment;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_TEXT;
return JobArgs::Conjunction(
$this->commentRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_TEXT);
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::EditComment,
Access::getIdentity($this->comment->getCommenter()));
Access::getIdentity($this->commentRetriever->retrieve()->getCommenter()));
}
public function isAuthenticationRequired()

View file

@ -1,10 +1,23 @@
<?php
class ListCommentsJob extends AbstractPageJob
class ListCommentsJob extends AbstractJob implements IPagedJob
{
protected $pager;
public function __construct()
{
$this->pager = new JobPager($this);
$this->pager->setPageSize(getConfig()->comments->commentsPerPage);
}
public function getPager()
{
return $this->pager;
}
public function execute()
{
$pageSize = $this->getPageSize();
$page = $this->getArgument(JobArgs::ARG_PAGE_NUMBER);
$pageSize = $this->pager->getPageSize();
$page = $this->pager->getPageNumber();
$query = 'comment_min:1 order:comment_date,desc';
$posts = PostSearchService::getEntities($query, $pageSize, $page);
@ -17,17 +30,12 @@ class ListCommentsJob extends AbstractPageJob
$comments = array_merge($comments, $post->getComments());
CommentModel::preloadCommenters($comments);
return $this->getPager($posts, $postCount, $page, $pageSize);
return $this->pager->serialize($posts, $postCount);
}
public function getDefaultPageSize()
public function getRequiredArguments()
{
return intval(getConfig()->comments->commentsPerPage);
}
public function getRequiredSubArguments()
{
return null;
return $this->pager->getRequiredArguments();
}
public function getRequiredPrivileges()

View file

@ -1,21 +1,27 @@
<?php
class PreviewCommentJob extends AbstractJob
{
protected $commentRetriever;
protected $postRetriever;
public function __construct()
{
$this->commentRetriever = new CommentRetriever($this);
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$user = Auth::getCurrentUser();
$text = $this->getArgument(JobArgs::ARG_NEW_TEXT);
if ($this->hasArgument(JobArgs::ARG_POST_ID))
$comment = $this->commentRetriever->tryRetrieve();
if (!$comment)
{
$post = PostModel::getById($this->getArgument(JobArgs::ARG_POST_ID));
$post = $this->postRetriever->retrieve();
$comment = CommentModel::spawn();
$comment->setPost($post);
}
else
{
$comment = CommentModel::getById($this->getArgument(JobArgs::ARG_COMMENT_ID));
}
$comment->setCommenter($user);
$comment->setCreationTime(time());
@ -31,8 +37,8 @@ class PreviewCommentJob extends AbstractJob
return JobArgs::Conjunction(
JobArgs::ARG_NEW_TEXT,
JobArgs::Alternative(
JobArgs::ARG_COMMENT_ID,
JobArgs::ARG_POST_ID));
$this->commentRetriever->getRequiredArguments(),
$this->postRetriever->getRequiredArguments()));
}
public function getRequiredPrivileges()

17
src/Api/Jobs/IJob.php Normal file
View file

@ -0,0 +1,17 @@
<?php
interface IJob
{
public function prepare();
public function execute();
public function getRequiredArguments();
public function getRequiredPrivileges();
public function isAuthenticationRequired();
public function isConfirmedEmailRequired();
public function getArgument($key);
public function getArguments();
public function hasArgument($key);
public function setArgument($key, $value);
public function setArguments(array $arguments);
}

View file

@ -0,0 +1,5 @@
<?php
interface IPagedJob
{
public function getPager();
}

View file

@ -1,10 +1,23 @@
<?php
class GetLogJob extends AbstractPageJob
class GetLogJob extends AbstractJob implements IPagedJob
{
protected $pager;
public function __construct()
{
$this->pager = new JobPager($this);
$this->pager->setPageSize(getConfig()->browsing->logsPerPage);
}
public function getPager()
{
return $this->pager;
}
public function execute()
{
$pageSize = $this->getPageSize();
$page = $this->getArgument(JobArgs::ARG_PAGE_NUMBER);
$pageSize = $this->pager->getPageSize();
$page = $this->pager->getPageNumber();
$name = $this->getArgument(JobArgs::ARG_LOG_ID);
$query = $this->getArgument(JobArgs::ARG_QUERY);
@ -31,17 +44,13 @@ class GetLogJob extends AbstractPageJob
$lineCount = count($lines);
$lines = array_slice($lines, ($page - 1) * $pageSize, $pageSize);
return $this->getPager($lines, $lineCount, $page, $pageSize);
return $this->pager->serialize($lines, $lineCount);
}
public function getDefaultPageSize()
{
return intval(getConfig()->browsing->logsPerPage);
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::Conjunction(
$this->pager->getRequiredArguments(),
JobArgs::ARG_LOG_ID,
JobArgs::ARG_QUERY);
}

View file

@ -1,42 +0,0 @@
<?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;
}
public function getRequiredArguments()
{
return JobArgs::Conjunction(
JobArgs::ARG_PAGE_NUMBER,
$this->getRequiredSubArguments());
}
public abstract function getRequiredSubArguments();
}

View file

@ -1,32 +0,0 @@
<?php
class ListPostsJob extends AbstractPageJob
{
public function execute()
{
$pageSize = $this->getPageSize();
$page = $this->getArgument(JobArgs::ARG_PAGE_NUMBER);
$query = $this->getArgument(JobArgs::ARG_QUERY);
$posts = PostSearchService::getEntities($query, $pageSize, $page);
$postCount = PostSearchService::getEntityCount($query);
PostModel::preloadTags($posts);
return $this->getPager($posts, $postCount, $page, $pageSize);
}
public function getDefaultPageSize()
{
return intval(getConfig()->browsing->postsPerPage);
}
public function getRequiredSubArguments()
{
return JobArgs::ARG_QUERY;
}
public function getRequiredPrivileges()
{
return new Privilege(Privilege::ListPosts);
}
}

View file

@ -1,30 +0,0 @@
<?php
class ListTagsJob extends AbstractPageJob
{
public function execute()
{
$pageSize = $this->getPageSize();
$page = $this->getArgument(JobArgs::ARG_PAGE_NUMBER);
$query = $this->getArgument(JobArgs::ARG_QUERY);
$tags = TagSearchService::getEntities($query, $pageSize, $page);
$tagCount = TagSearchService::getEntityCount($query);
return $this->getPager($tags, $tagCount, $page, $pageSize);
}
public function getDefaultPageSize()
{
return intval(getConfig()->browsing->tagsPerPage);
}
public function getRequiredSubArguments()
{
return JobArgs::ARG_QUERY;
}
public function getRequiredPrivileges()
{
return new Privilege(Privilege::ListTags);
}
}

View file

@ -1,30 +0,0 @@
<?php
class ListUsersJob extends AbstractPageJob
{
public function execute()
{
$pageSize = $this->getPageSize();
$page = $this->getArgument(JobArgs::ARG_PAGE_NUMBER);
$filter = $this->getArgument(JobArgs::ARG_QUERY);
$users = UserSearchService::getEntities($filter, $pageSize, $page);
$userCount = UserSearchService::getEntityCount($filter);
return $this->getPager($users, $userCount, $page, $pageSize);
}
public function getDefaultPageSize()
{
return intval(getConfig()->browsing->usersPerPage);
}
public function getRequiredSubArguments()
{
return JobArgs::ARG_QUERY;
}
public function getRequiredPrivileges()
{
return new Privilege(Privilege::ListUsers);
}
}

View file

@ -1,35 +0,0 @@
<?php
abstract class AbstractPostJob extends AbstractJob
{
protected $post;
public function prepare()
{
if ($this->hasArgument(JobArgs::ARG_POST_ENTITY))
{
$this->post = $this->getArgument(JobArgs::ARG_POST_ENTITY);
}
elseif ($this->hasArgument(JobArgs::ARG_POST_ID))
{
$postId = $this->getArgument(JobArgs::ARG_POST_ID);
$this->post = PostModel::getById($postId);
}
else
{
$postName = $this->getArgument(JobArgs::ARG_POST_NAME);
$this->post = PostModel::getByName($postName);
}
}
public function getRequiredArguments()
{
return JobArgs::Conjunction(
JobArgs::Alternative(
JobArgs::ARG_POST_ID,
JobArgs::ARG_POST_NAME,
JobArgs::ARG_POST_ENTITY),
$this->getRequiredSubArguments());
}
public abstract function getRequiredSubArguments();
}

View file

@ -1,9 +1,16 @@
<?php
class DeletePostJob extends AbstractPostJob
class DeletePostJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
PostModel::remove($post);
@ -12,16 +19,16 @@ class DeletePostJob extends AbstractPostJob
'post' => TextHelper::reprPost($post)]);
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return null;
return $this->postRetriever->getRequiredArguments();
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::DeletePost,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
public function isAuthenticationRequired()

View file

@ -1,9 +1,16 @@
<?php
class EditPostContentJob extends AbstractPostJob
class EditPostContentJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
if ($this->hasArgument(JobArgs::ARG_NEW_POST_CONTENT_URL))
{
@ -26,11 +33,13 @@ class EditPostContentJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::Alternative(
JobArgs::ARG_NEW_POST_CONTENT,
JobArgs::ARG_NEW_POST_CONTENT_URL);
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
JobArgs::Alternative(
JobArgs::ARG_NEW_POST_CONTENT,
JobArgs::ARG_NEW_POST_CONTENT_URL));
}
public function getRequiredPrivileges()
@ -39,6 +48,6 @@ class EditPostContentJob extends AbstractPostJob
$this->getContext() == self::CONTEXT_BATCH_ADD
? Privilege::AddPostContent
: Privilege::EditPostContent,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
}

View file

@ -1,9 +1,16 @@
<?php
class EditPostJob extends AbstractPostJob
class EditPostJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
Logger::bufferChanges();
@ -43,9 +50,9 @@ class EditPostJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return null;
return $this->postRetriever->getRequiredArguments();
}
public function getRequiredPrivileges()
@ -54,6 +61,6 @@ class EditPostJob extends AbstractPostJob
$this->getContext() == self::CONTEXT_BATCH_ADD
? Privilege::AddPost
: Privilege::EditPost,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
}

View file

@ -1,9 +1,16 @@
<?php
class EditPostRelationsJob extends AbstractPostJob
class EditPostRelationsJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$relations = $this->getArgument(JobArgs::ARG_NEW_RELATED_POST_IDS);
$oldRelatedIds = array_map(function($post) { return $post->getId(); }, $post->getRelations());
@ -32,9 +39,11 @@ class EditPostRelationsJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_RELATED_POST_IDS;
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_RELATED_POST_IDS);
}
public function getRequiredPrivileges()
@ -43,6 +52,6 @@ class EditPostRelationsJob extends AbstractPostJob
$this->getContext() == self::CONTEXT_BATCH_ADD
? Privilege::AddPostRelations
: Privilege::EditPostRelations,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
}

View file

@ -1,9 +1,16 @@
<?php
class EditPostSafetyJob extends AbstractPostJob
class EditPostSafetyJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$newSafety = new PostSafety($this->getArgument(JobArgs::ARG_NEW_SAFETY));
$oldSafety = $post->getSafety();
@ -23,9 +30,11 @@ class EditPostSafetyJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_SAFETY;
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_SAFETY);
}
public function getRequiredPrivileges()
@ -34,6 +43,6 @@ class EditPostSafetyJob extends AbstractPostJob
$this->getContext() == self::CONTEXT_BATCH_ADD
? Privilege::AddPostSafety
: Privilege::EditPostSafety,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
}

View file

@ -1,9 +1,16 @@
<?php
class EditPostSourceJob extends AbstractPostJob
class EditPostSourceJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$newSource = $this->getArgument(JobArgs::ARG_NEW_SOURCE);
$oldSource = $post->getSource();
@ -23,9 +30,11 @@ class EditPostSourceJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_SOURCE;
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_SOURCE);
}
public function getRequiredPrivileges()
@ -34,6 +43,6 @@ class EditPostSourceJob extends AbstractPostJob
$this->getContext() == self::CONTEXT_BATCH_ADD
? Privilege::AddPostSource
: Privilege::EditPostSource,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
}

View file

@ -1,9 +1,16 @@
<?php
class EditPostTagsJob extends AbstractPostJob
class EditPostTagsJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$tagNames = $this->getArgument(JobArgs::ARG_NEW_TAG_NAMES);
if (!is_array($tagNames))
@ -40,9 +47,11 @@ class EditPostTagsJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_TAG_NAMES;
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_TAG_NAMES);
}
public function getRequiredPrivileges()
@ -51,6 +60,6 @@ class EditPostTagsJob extends AbstractPostJob
$this->getContext() == self::CONTEXT_BATCH_ADD
? Privilege::AddPostTags
: Privilege::EditPostTags,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
}

View file

@ -1,9 +1,16 @@
<?php
class EditPostThumbJob extends AbstractPostJob
class EditPostThumbJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$file = $this->getArgument(JobArgs::ARG_NEW_THUMB_CONTENT);
$post->setCustomThumbnailFromPath($file->filePath);
@ -18,9 +25,11 @@ class EditPostThumbJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_THUMB_CONTENT;
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_THUMB_CONTENT);
}
public function getRequiredPrivileges()
@ -29,6 +38,6 @@ class EditPostThumbJob extends AbstractPostJob
$this->getContext() == self::CONTEXT_BATCH_ADD
? Privilege::AddPostThumb
: Privilege::EditPostThumb,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
}

View file

@ -1,9 +1,16 @@
<?php
class FeaturePostJob extends AbstractPostJob
class FeaturePostJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
PropertyModel::set(PropertyModel::FeaturedPostId, $post->getId());
PropertyModel::set(PropertyModel::FeaturedPostUnixTime, time());
@ -20,16 +27,18 @@ class FeaturePostJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::Optional(JobArgs::ARG_ANONYMOUS);
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
JobArgs::Optional(JobArgs::ARG_ANONYMOUS));
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::FeaturePost,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
public function isAuthenticationRequired()

View file

@ -1,9 +1,16 @@
<?php
class FlagPostJob extends AbstractPostJob
class FlagPostJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$key = TextHelper::reprPost($post);
$flagged = SessionHelper::get('flagged', []);
@ -19,15 +26,15 @@ class FlagPostJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return null;
return $this->postRetriever->getRequiredArguments();
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::FlagPost,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
}

View file

@ -1,19 +1,16 @@
<?php
class GetPostContentJob extends AbstractJob
{
protected $post;
protected $postRetriever;
public function prepare()
public function __construct()
{
if ($this->hasArgument(JobArgs::ARG_POST_ENTITY))
$this->post = $this->getArgument(JobArgs::ARG_POST_ENTITY);
else
$this->post = PostModel::getByName($this->getArgument(JobArgs::ARG_POST_NAME));
$this->postRetriever = new SafePostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$config = getConfig();
$path = $config->main->filesPath . DS . $post->getName();
@ -35,14 +32,12 @@ class GetPostContentJob extends AbstractJob
public function getRequiredArguments()
{
return JobArgs::Alternative(
JobArgs::ARG_POST_NAME,
JobArgs::ARG_POST_ENTITY);
return $this->postRetriever->getRequiredArguments();
}
public function getRequiredPrivileges()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$privileges = [];
if ($post->isHidden())

View file

@ -1,23 +1,32 @@
<?php
class GetPostJob extends AbstractPostJob
class GetPostJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
CommentModel::preloadCommenters($post->getComments());
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return null;
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
null);
}
public function getRequiredPrivileges()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$privileges = [];
if ($post->isHidden())

View file

@ -1,13 +1,21 @@
<?php
class GetPostThumbJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new SafePostRetriever($this);
}
public function execute()
{
if ($this->getArgument(JobArgs::ARG_POST_NAME))
//optimize - save extra query to DB
if ($this->hasArgument(JobArgs::ARG_POST_NAME))
$name = $this->getArgument(JobArgs::ARG_POST_NAME);
else
{
$post = PostModel::getByName($this->getArgument(JobArgs::ARG_POST_ENTITY));
$post = $this->postRetriever->retrieve();
$name = $post->getName();
}
@ -21,10 +29,7 @@ class GetPostThumbJob extends AbstractJob
if (!file_exists($path))
{
$post = PostModel::getByName($name);
if ($post->isHidden())
Access::assert(new Privilege(Privilege::ListPosts, 'hidden'));
Access::assert(new Privilege(Privilege::ListPosts, $post->getSafety()->toString()));
$post = $this->postRetriever->retrieve();
$post->generateThumb($width, $height);
@ -45,16 +50,14 @@ class GetPostThumbJob extends AbstractJob
public function getRequiredArguments()
{
return JobArgs::Conjunction(
JobArgs::Alternative(
JobArgs::ARG_POST_ENTITY,
JobArgs::ARG_POST_NAME),
$this->postRetriever->getRequiredArguments(),
JobArgs::Optional(JobArgs::ARG_THUMB_WIDTH),
JobArgs::Optional(JobArgs::ARG_THUMB_HEIGHT));
}
public function getRequiredPrivileges()
{
//manually enforced in execute when post is retrieved
//privilege check removed to make thumbs faster
return false;
}
}

View file

@ -0,0 +1,42 @@
<?php
class ListPostsJob extends AbstractJob implements IPagedJob
{
protected $pager;
public function __construct()
{
$this->pager = new JobPager($this);
$this->pager->setPageSize(getConfig()->browsing->postsPerPage);
}
public function getPager()
{
return $this->pager;
}
public function execute()
{
$pageSize = $this->pager->getPageSize();
$page = $this->pager->getPageNumber();
$query = $this->getArgument(JobArgs::ARG_QUERY);
$posts = PostSearchService::getEntities($query, $pageSize, $page);
$postCount = PostSearchService::getEntityCount($query);
PostModel::preloadTags($posts);
return $this->pager->serialize($posts, $postCount);
}
public function getRequiredArguments()
{
return JobArgs::Conjunction(
$this->pager->getRequiredArguments(),
JobArgs::ARG_QUERY);
}
public function getRequiredPrivileges()
{
return new Privilege(Privilege::ListPosts);
}
}

View file

@ -1,9 +1,16 @@
<?php
class ScorePostJob extends AbstractPostJob
class ScorePostJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$score = intval($this->getArgument(JobArgs::ARG_NEW_POST_SCORE));
UserModel::updateUserScore(Auth::getCurrentUser(), $post, $score);
@ -11,16 +18,18 @@ class ScorePostJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_POST_SCORE;
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_POST_SCORE);
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::ScorePost,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
public function isAuthenticationRequired()

View file

@ -1,9 +1,16 @@
<?php
class TogglePostFavoriteJob extends AbstractPostJob
class TogglePostFavoriteJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$favorite = boolval($this->getArgument(JobArgs::ARG_NEW_STATE));
if ($favorite)
@ -19,16 +26,18 @@ class TogglePostFavoriteJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_STATE;
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_STATE);
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::FavoritePost,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
public function isAuthenticationRequired()

View file

@ -1,11 +1,18 @@
<?php
class TogglePostTagJob extends AbstractPostJob
class TogglePostTagJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$tagName = $this->getArgument(JobArgs::ARG_TAG_NAME);
$enable = boolval($this->getArgument(JobArgs::ARG_NEW_STATE));
$post = $this->post;
$post = $this->postRetriever->retrieve();
$tags = $post->getTags();
@ -50,17 +57,19 @@ class TogglePostTagJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
JobArgs::Conjunction(
JobArgs::ARG_TAG_NAME,
Jobargs::ARG_NEW_STATE);
Jobargs::ARG_NEW_STATE));
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::EditPostTags,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
}

View file

@ -1,9 +1,16 @@
<?php
class TogglePostVisibilityJob extends AbstractPostJob
class TogglePostVisibilityJob extends AbstractJob
{
protected $postRetriever;
public function __construct()
{
$this->postRetriever = new PostRetriever($this);
}
public function execute()
{
$post = $this->post;
$post = $this->postRetriever->retrieve();
$visible = boolval($this->getArgument(JobArgs::ARG_NEW_STATE));
$post->setHidden(!$visible);
@ -19,15 +26,17 @@ class TogglePostVisibilityJob extends AbstractPostJob
return $post;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_STATE;
return JobArgs::Conjunction(
$this->postRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_STATE);
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::HidePost,
Access::getIdentity($this->post->getUploader()));
Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
}
}

View file

@ -1,10 +1,23 @@
<?php
class ListRelatedTagsJob extends ListTagsJob
class ListRelatedTagsJob extends AbstractJob implements IPagedJob
{
protected $pager;
public function __construct()
{
$this->pager = new JobPager($this);
$this->pager->setPageSize(getConfig()->browsing->tagsRelated);
}
public function getPager()
{
return $this->pager;
}
public function execute()
{
$pageSize = $this->getPageSize();
$page = $this->getArgument(JobArgs::ARG_PAGE_NUMBER);
$pageSize = $this->pager->getPageSize();
$page = $this->pager->getPageNumber();
$tag = $this->getArgument(JobArgs::ARG_TAG_NAME);
$otherTags = $this->hasArgument(JobArgs::ARG_TAG_NAMES) ? $this->getArgument(JobArgs::ARG_TAG_NAMES) : [];
@ -13,19 +26,19 @@ class ListRelatedTagsJob extends ListTagsJob
$tags = array_filter($tags, function($tag) use ($otherTags) { return !in_array($tag->getName(), $otherTags); });
$tags = array_slice($tags, 0, $pageSize);
return $this->getPager($tags, $tagCount, $page, $pageSize);
}
public function getDefaultPageSize()
{
return intval(getConfig()->browsing->tagsRelated);
return $this->pager->serialize($tags, $tagCount);
}
public function getRequiredArguments()
{
return JobArgs::Conjunction(
parent::getRequiredArguments(),
$this->pager->getRequiredArguments(),
Jobargs::ARG_TAG_NAME,
JobArgs::Optional(JobArgs::ARG_TAG_NAMES));
}
public function getRequiredPrivileges()
{
return new Privilege(Privilege::ListTags);
}
}

View file

@ -0,0 +1,40 @@
<?php
class ListTagsJob extends AbstractJob implements IPagedJob
{
protected $pager;
public function __construct()
{
$this->pager = new JobPager($this);
$this->pager->setPageSize(getConfig()->browsing->tagsPerPage);
}
public function getPager()
{
return $this->pager;
}
public function execute()
{
$pageSize = $this->pager->getPageSize();
$page = $this->pager->getPageNumber();
$query = $this->getArgument(JobArgs::ARG_QUERY);
$tags = TagSearchService::getEntities($query, $pageSize, $page);
$tagCount = TagSearchService::getEntityCount($query);
return $this->pager->serialize($tags, $tagCount);
}
public function getRequiredArguments()
{
return JobArgs::Conjunction(
$this->pager->getRequiredArguments(),
JobArgs::ARG_QUERY);
}
public function getRequiredPrivileges()
{
return new Privilege(Privilege::ListTags);
}
}

View file

@ -1,30 +0,0 @@
<?php
abstract class AbstractUserJob extends AbstractJob
{
protected $user;
public function prepare()
{
if ($this->hasArgument(JobArgs::ARG_USER_ENTITY))
{
$this->user = $this->getArgument(JobArgs::ARG_USER_ENTITY);
}
else
{
$userName = $this->getArgument(JobArgs::ARG_USER_NAME);
$this->user = UserModel::getByNameOrEmail($userName);
}
}
public function getRequiredArguments()
{
return JobArgs::Conjunction(
JobArgs::Alternative(
JobArgs::ARG_USER_NAME,
JobArgs::ARG_USER_EMAIL,
JobArgs::ARG_USER_ENTITY),
$this->getRequiredSubArguments());
}
public abstract function getRequiredSubArguments();
}

View file

@ -1,9 +1,16 @@
<?php
class AcceptUserRegistrationJob extends AbstractUserJob
class AcceptUserRegistrationJob extends AbstractJob
{
protected $userRetriever;
public function __construct()
{
$this->userRetriever = new UserRetriever($this);
}
public function execute()
{
$user = $this->user;
$user = $this->userRetriever->getRequiredArguments();
$user->setStaffConfirmed(true);
UserModel::save($user);
@ -13,9 +20,9 @@ class AcceptUserRegistrationJob extends AbstractUserJob
'subject' => TextHelper::reprUser($user)]);
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return null;
return $this->userRetriever->getRequiredArguments();
}
public function getRequiredPrivileges()

View file

@ -1,16 +1,18 @@
<?php
class ActivateUserEmailJob extends AbstractJob
{
protected $userRetriever;
public function __construct()
{
$this->userRetriever = new UserRetriever($this);
}
public function execute()
{
if (!$this->hasArgument(JobArgs::ARG_TOKEN))
{
if ($this->hasArgument(JobArgs::ARG_USER_ENTITY))
$user = $this->getArgument(JobArgs::ARG_USER_ENTITY);
elseif ($this->hasArgument(JobArgs::ARG_USER_NAME))
$user = UserModel::getByName($this->getArgument(JobArgs::ARG_USER_NAME));
else
$user = UserModel::getByEmail($this->getArgument(JobArgs::ARG_USER_EMAIL));
$user = $this->userRetriever->retrieve();
if (empty($user->getUnconfirmedEmail()))
{
@ -47,10 +49,7 @@ class ActivateUserEmailJob extends AbstractJob
{
return JobArgs::Alternative(
JobArgs::ARG_TOKEN,
JobArgs::Alternative(
JobArgs::ARG_USER_ENTITY,
JobArgs::ARG_USER_EMAIL,
JobArgs::ARG_USER_NAME));
$this->userRetriever->getRequiredArguments());
}
public static function sendEmail($user)

View file

@ -1,9 +1,16 @@
<?php
class DeleteUserJob extends AbstractUserJob
class DeleteUserJob extends AbstractJob
{
protected $userRetriever;
public function __construct()
{
$this->userRetriever = new UserRetriever($this);
}
public function execute()
{
$user = $this->user;
$user = $this->userRetriever->retrieve();
$name = $user->getName();
UserModel::remove($user);
@ -13,15 +20,15 @@ class DeleteUserJob extends AbstractUserJob
'subject' => TextHelper::reprUser($name)]);
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return null;
return $this->userRetriever->getRequiredArguments();
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::DeleteUser,
Access::getIdentity($this->user));
Access::getIdentity($this->userRetriever->retrieve()));
}
}

View file

@ -1,9 +1,16 @@
<?php
class EditUserAccessRankJob extends AbstractUserJob
class EditUserAccessRankJob extends AbstractJob
{
protected $userRetriever;
public function __construct()
{
$this->userRetriever = new UserRetriever($this);
}
public function execute()
{
$user = $this->user;
$user = $this->userRetriever->retrieve();
$newAccessRank = new AccessRank($this->getArgument(JobArgs::ARG_NEW_ACCESS_RANK));
$oldAccessRank = $user->getAccessRank();
@ -23,15 +30,17 @@ class EditUserAccessRankJob extends AbstractUserJob
return $user;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_ACCESS_RANK;
return JobArgs::Conjunction(
$this->userRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_ACCESS_RANK);
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::ChangeUserAccessRank,
Access::getIdentity($this->user));
Access::getIdentity($this->userRetriever->retrieve()));
}
}

View file

@ -1,13 +1,20 @@
<?php
class EditUserEmailJob extends AbstractUserJob
class EditUserEmailJob extends AbstractJob
{
protected $userRetriever;
public function __construct()
{
$this->userRetriever = new UserRetriever($this);
}
public function execute()
{
if (getConfig()->registration->needEmailForRegistering)
if (!$this->hasArgument(JobArgs::ARG_NEW_EMAIL) or empty($this->getArgument(JobArgs::ARG_NEW_EMAIL)))
throw new SimpleException('E-mail address is required - you will be sent confirmation e-mail.');
$user = $this->user;
$user = $this->userRetriever->retrieve();
$newEmail = $this->getArgument(JobArgs::ARG_NEW_EMAIL);
$oldEmail = $user->getConfirmedEmail();
@ -44,9 +51,11 @@ class EditUserEmailJob extends AbstractUserJob
}
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_EMAIL;
return JobArgs::Conjunction(
$this->userRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_EMAIL);
}
public function getRequiredPrivileges()
@ -55,6 +64,6 @@ class EditUserEmailJob extends AbstractUserJob
$this->getContext() == self::CONTEXT_BATCH_ADD
? Privilege::RegisterAccount
: Privilege::ChangeUserEmail,
Access::getIdentity($this->user));
Access::getIdentity($this->userRetriever->retrieve()));
}
}

View file

@ -1,10 +1,12 @@
<?php
class EditUserJob extends AbstractUserJob
class EditUserJob extends AbstractJob
{
protected $userRetriever;
protected $subJobs;
public function __construct()
{
$this->userRetriever = new UserRetriever($this);
$this->subJobs =
[
new EditUserAccessRankJob(),
@ -34,7 +36,7 @@ class EditUserJob extends AbstractUserJob
public function execute()
{
$user = $this->user;
$user = $this->userRetriever->retrieve();
Logger::bufferChanges();
@ -65,9 +67,9 @@ class EditUserJob extends AbstractUserJob
return $user;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return null;
return $this->userRetriever->getRequiredArguments();
}
public function getRequiredPrivileges()

View file

@ -1,9 +1,16 @@
<?php
class EditUserNameJob extends AbstractUserJob
class EditUserNameJob extends AbstractJob
{
protected $userRetriever;
public function __construct()
{
$this->userRetriever = new UserRetriever($this);
}
public function execute()
{
$user = $this->user;
$user = $this->userRetriever->retrieve();
$newName = $this->getArgument(JobArgs::ARG_NEW_USER_NAME);
$oldName = $user->getName();
@ -23,9 +30,11 @@ class EditUserNameJob extends AbstractUserJob
return $user;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_USER_NAME;
return JobArgs::Conjunction(
$this->userRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_USER_NAME);
}
public function getRequiredPrivileges()
@ -34,6 +43,6 @@ class EditUserNameJob extends AbstractUserJob
$this->getContext() == self::CONTEXT_BATCH_ADD
? Privilege::RegisterAccount
: Privilege::ChangeUserName,
Access::getIdentity($this->user));
Access::getIdentity($this->userRetriever->retrieve()));
}
}

View file

@ -1,9 +1,16 @@
<?php
class EditUserPasswordJob extends AbstractUserJob
class EditUserPasswordJob extends AbstractJob
{
protected $userRetriever;
public function __construct()
{
$this->userRetriever = new UserRetriever($this);
}
public function execute()
{
$user = $this->user;
$user = $this->userRetriever->retrieve();
$newPassword = $this->getArgument(JobArgs::ARG_NEW_PASSWORD);
$oldPasswordHash = $user->getPasswordHash();
@ -22,9 +29,11 @@ class EditUserPasswordJob extends AbstractUserJob
return $user;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_PASSWORD;
return JobArgs::Conjunction(
$this->userRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_PASSWORD);
}
public function getRequiredPrivileges()
@ -33,6 +42,6 @@ class EditUserPasswordJob extends AbstractUserJob
$this->getContext() == self::CONTEXT_BATCH_ADD
? Privilege::RegisterAccount
: Privilege::ChangeUserPassword,
Access::getIdentity($this->user));
Access::getIdentity($this->userRetriever->retrieve()));
}
}

View file

@ -1,9 +1,16 @@
<?php
class FlagUserJob extends AbstractUserJob
class FlagUserJob extends AbstractJob
{
protected $userRetriever;
public function __construct()
{
$this->userRetriever = new UserRetriever($this);
}
public function execute()
{
$user = $this->user;
$user = $this->userRetriever->retrieve();
$key = TextHelper::reprUser($user);
$flagged = SessionHelper::get('flagged', []);
@ -19,15 +26,15 @@ class FlagUserJob extends AbstractUserJob
return $user;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return null;
return $this->userRetriever->getRequiredArguments();
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::FlagUser,
Access::getIdentity($this->user));
Access::getIdentity($this->userRetriever->retrieve()));
}
}

View file

@ -1,20 +1,27 @@
<?php
class GetUserJob extends AbstractUserJob
class GetUserJob extends AbstractJob
{
public function execute()
protected $userRetriever;
public function __construct()
{
return $this->user;
$this->userRetriever = new UserRetriever($this);
}
public function getRequiredSubArguments()
public function execute()
{
return null;
return $this->userRetriever->retrieve();
}
public function getRequiredArguments()
{
return $this->userRetriever->getRequiredArguments();
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::ViewUser,
Access::getIdentity($this->user));
Access::getIdentity($this->userRetriever->retrieve()));
}
}

View file

@ -0,0 +1,40 @@
<?php
class ListUsersJob extends AbstractJob implements IPagedJob
{
protected $pager;
public function __construct()
{
$this->pager = new JobPager($this);
$this->pager->setPageSize(getConfig()->browsing->usersPerPage);
}
public function getPager()
{
return $this->pager;
}
public function execute()
{
$pageSize = $this->pager->getPageSize();
$page = $this->pager->getPageNumber();
$filter = $this->getArgument(JobArgs::ARG_QUERY);
$users = UserSearchService::getEntities($filter, $pageSize, $page);
$userCount = UserSearchService::getEntityCount($filter);
return $this->pager->serialize($users, $userCount);
}
public function getRequiredArguments()
{
return JobArgs::Conjunction(
$this->pager->getRequiredArguments(),
JobArgs::ARG_QUERY);
}
public function getRequiredPrivileges()
{
return new Privilege(Privilege::ListUsers);
}
}

View file

@ -1,16 +1,18 @@
<?php
class PasswordResetJob extends AbstractJob
{
protected $userRetriever;
public function __construct()
{
$this->userRetriever = new UserRetriever($this);
}
public function execute()
{
if (!$this->hasArgument(JobArgs::ARG_TOKEN))
{
if ($this->hasArgument(JobArgs::ARG_USER_NAME))
$user = UserModel::getByNameOrEmail($this->getArgument(JobArgs::ARG_USER_NAME));
elseif ($this->hasArgument(JobArgs::ARG_USER_EMAIL))
$user = UserModel::getByNameOrEmail($this->getArgument(JobArgs::ARG_USER_EMAIL));
else
$user = $this->getArgument(JobArgs::ARG_USER_ENTITTY);
$user = $this->userRetriever->retrieve();
if (empty($user->getConfirmedEmail()))
throw new SimpleException('This user has no e-mail confirmed; password reset cannot proceed');
@ -50,9 +52,7 @@ class PasswordResetJob extends AbstractJob
public function getRequiredArguments()
{
return JobArgs::Alternative(
JobArgs::ARG_USER_NAME,
JobArgs::ARG_USER_EMAIL,
JobArgs::ARG_USER_ENTITY,
$this->userRetriever->getRequiredArguments(),
JobArgs::ARG_TOKEN);
}

View file

@ -1,9 +1,16 @@
<?php
class ToggleUserBanJob extends AbstractUserJob
class ToggleUserBanJob extends AbstractJob
{
protected $userRetriever;
public function __construct()
{
$this->userRetriever = new UserRetriever($this);
}
public function execute()
{
$user = $this->user;
$user = $this->userRetriever->retrieve();
$banned = boolval($this->getArgument(JobArgs::ARG_NEW_STATE));
if ($banned)
@ -22,15 +29,17 @@ class ToggleUserBanJob extends AbstractUserJob
return $user;
}
public function getRequiredSubArguments()
public function getRequiredArguments()
{
return JobArgs::ARG_NEW_STATE;
return JobArgs::Conjunction(
$this->userRetriever->getRequiredArguments(),
JobArgs::ARG_NEW_STATE);
}
public function getRequiredPrivileges()
{
return new Privilege(
Privilege::BanUser,
Access::getIdentity($this->user));
Access::getIdentity($this->userRetriever->retrieve()));
}
}

View file

@ -44,9 +44,7 @@ class ApiArgumentTest extends AbstractFullApiTest
public function testDeleteCommentJob()
{
$this->testArguments(new DeleteCommentJob(),
JobArgs::Alternative(
JobArgs::ARG_COMMENT_ENTITY,
JobArgs::ARG_COMMENT_ID));
$this->getCommentSelector());
}
public function testDeleteUserJob()
@ -60,9 +58,7 @@ class ApiArgumentTest extends AbstractFullApiTest
$this->testArguments(new EditCommentJob(),
JobArgs::Conjunction(
JobArgs::ARG_NEW_TEXT,
JobArgs::Alternative(
JobArgs::ARG_COMMENT_ENTITY,
JobArgs::ARG_COMMENT_ID)));
$this->getCommentSelector()));
}
public function testEditPostContentJob()
@ -185,7 +181,7 @@ class ApiArgumentTest extends AbstractFullApiTest
JobArgs::Conjunction(
JobArgs::ARG_QUERY,
JobArgs::ARG_LOG_ID,
JobArgs::ARG_PAGE_NUMBER));
JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER)));
}
public function testGetPostContentJob()
@ -218,7 +214,7 @@ class ApiArgumentTest extends AbstractFullApiTest
public function testListCommentsJob()
{
$this->testArguments(new ListCommentsJob(),
JobArgs::ARG_PAGE_NUMBER);
JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER));
}
public function testListLogsJob()
@ -232,7 +228,7 @@ class ApiArgumentTest extends AbstractFullApiTest
$this->testArguments(new ListPostsJob(),
JobArgs::Conjunction(
JobArgs::ARG_QUERY,
JobArgs::ARG_PAGE_NUMBER));
JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER)));
}
public function testListRelatedTagsJob()
@ -241,8 +237,7 @@ class ApiArgumentTest extends AbstractFullApiTest
JobArgs::Conjunction(
JobArgs::Optional(JobArgs::ARG_TAG_NAMES),
JobArgs::ARG_TAG_NAME,
JobArgs::ARG_QUERY,
JobArgs::ARG_PAGE_NUMBER));
JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER)));
}
public function testListTagsJob()
@ -250,7 +245,7 @@ class ApiArgumentTest extends AbstractFullApiTest
$this->testArguments(new ListTagsJob(),
JobArgs::Conjunction(
JobArgs::ARG_QUERY,
JobArgs::ARG_PAGE_NUMBER));
JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER)));
}
public function testListUsersJob()
@ -258,7 +253,7 @@ class ApiArgumentTest extends AbstractFullApiTest
$this->testArguments(new ListUsersJob(),
JobArgs::Conjunction(
JobArgs::ARG_QUERY,
JobArgs::ARG_PAGE_NUMBER));
JobArgs::Optional(JobArgs::ARG_PAGE_NUMBER)));
}
public function testMergeTagsJob()
@ -290,8 +285,8 @@ class ApiArgumentTest extends AbstractFullApiTest
$this->testArguments(new PreviewCommentJob(),
JobArgs::Conjunction(
JobArgs::Alternative(
JobArgs::ARG_POST_ID,
JobArgs::ARG_COMMENT_ID),
$this->getPostSelector(),
$this->getCommentSelector()),
JobArgs::ARG_NEW_TEXT));
}
@ -357,6 +352,13 @@ class ApiArgumentTest extends AbstractFullApiTest
JobArgs::ARG_POST_ENTITY);
}
protected function getCommentSelector()
{
return JobArgs::Alternative(
JobArgs::ARG_COMMENT_ENTITY,
JobArgs::ARG_COMMENT_ID);
}
protected function getUserSelector()
{
return JobArgs::Alternative(