MediaWiki  master
AbstractContributionHandler.php
Go to the documentation of this file.
1 <?php
2 
4 
10 use RequestContext;
12 
16 abstract class AbstractContributionHandler extends Handler {
17 
22 
24  protected const MAX_LIMIT = 20;
25 
29  protected $me;
30 
34  protected $userNameUtils;
35 
40  public function __construct(
43  ) {
44  $this->contributionsLookup = $contributionsLookup;
45  $this->userNameUtils = $userNameUtils;
46  }
47 
48  protected function postInitSetup() {
49  $this->me = $this->getConfig()['mode'] === 'me';
50  }
51 
59  protected function getTargetUser() {
60  if ( $this->me ) {
61  $user = RequestContext::getMain()->getUser();
62  if ( $user->isAnon() ) {
63  throw new LocalizedHttpException(
64  new MessageValue( 'rest-permission-denied-anon' ), 401
65  );
66  }
67  return $user;
68  }
69 
70  /* @var UserIdentity $user */
71  $user = $this->getValidatedParams()['user'];
72  $name = $user->getName();
73  if ( !$this->userNameUtils->isIp( $name ) && !$user->isRegistered() ) {
74  throw new LocalizedHttpException(
75  new MessageValue( 'rest-nonexistent-user', [ $name ] ), 404
76  );
77  }
78  return $user;
79  }
80 }
MediaWiki\Rest\Handler\AbstractContributionHandler
Definition: AbstractContributionHandler.php:16
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
MediaWiki\Rest\Handler\AbstractContributionHandler\postInitSetup
postInitSetup()
The handler can override this to do any necessary setup after init() is called to inject the dependen...
Definition: AbstractContributionHandler.php:48
MediaWiki\Rest\Handler\AbstractContributionHandler\__construct
__construct(ContributionsLookup $contributionsLookup, UserNameUtils $userNameUtils)
Definition: AbstractContributionHandler.php:40
MediaWiki\Rest\Handler\AbstractContributionHandler\$contributionsLookup
ContributionsLookup $contributionsLookup
Definition: AbstractContributionHandler.php:21
MediaWiki\Rest\Handler\AbstractContributionHandler\$me
bool $me
User is requesting their own contributions.
Definition: AbstractContributionHandler.php:29
MediaWiki\Rest\Handler\AbstractContributionHandler\$userNameUtils
UserNameUtils $userNameUtils
Definition: AbstractContributionHandler.php:34
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
MediaWiki\Rest\Handler
Base class for REST route handlers.
Definition: Handler.php:16
RequestContext
Group all the pieces relevant to the context of a request into one instance @newable.
Definition: RequestContext.php:38
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:257
MediaWiki\Rest\Handler\getConfig
getConfig()
Get the configuration array for the current route.
Definition: Handler.php:140
MediaWiki\Rest\Handler\AbstractContributionHandler\getTargetUser
getTargetUser()
Returns the user who's contributions we are requesting.
Definition: AbstractContributionHandler.php:59
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:451
Revision\ContributionsLookup
Definition: ContributionsLookup.php:15
MediaWiki\Rest\Handler\AbstractContributionHandler\MAX_LIMIT
const MAX_LIMIT
Hard limit results to 20 contributions.
Definition: AbstractContributionHandler.php:24
MediaWiki\User\UserNameUtils
UserNameUtils service.
Definition: UserNameUtils.php:42
MediaWiki\Rest\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10