MediaWiki REL1_35
UserContributionsHandler.php
Go to the documentation of this file.
1<?php
2
4
14use User;
15use Wikimedia\Assert\Assert;
19
24
29
33 private $userFactory;
34
36 private const MAX_LIMIT = 20;
37
41 private $me;
42
47
53 public function __construct(
57 ) {
58 $this->contributionsLookup = $contributionsLookup;
59 $this->userFactory = $userFactory;
60 $this->userNameUtils = $userNameUtils;
61 }
62
63 protected function postInitSetup() {
64 $this->me = $this->getConfig()['mode'] === 'me';
65 }
66
74 private function getTargetUser() {
75 if ( $this->me ) {
76 $user = RequestContext::getMain()->getUser();
77 if ( $user->isAnon() ) {
78 throw new LocalizedHttpException(
79 new MessageValue( 'rest-permission-denied-anon' ), 401
80 );
81 }
82
83 return $user;
84 }
85
86 $name = $this->getValidatedParams()['name'] ?? null;
87 Assert::invariant( $name !== null, '"name" parameter must be given if mode is not "me"' );
88
89 if ( $this->userNameUtils->isIP( $name ) ) {
90 // Create an anonymous user instance for the given IP
91 // NOTE: We can't use a UserIdentityValue, because we might need the actor ID
92 // TODO: We should create UserFactory::newFromIpAddress() for this purpose (T257464)
93 $user = new User();
94 $user->setName( $name );
95 return $user;
96 }
97
98 $user = $this->userFactory->newFromName( $name );
99 if ( !$user ) {
100 throw new LocalizedHttpException(
101 new MessageValue( 'rest-invalid-user', [ $name ] ), 400
102 );
103 }
104
105 if ( !$user->isRegistered() ) {
106 throw new LocalizedHttpException(
107 new MessageValue( 'rest-nonexistent-user', [ $user->getName() ] ), 404
108 );
109 }
110
111 return $user;
112 }
113
118 public function execute() {
119 $performer = RequestContext::getMain()->getUser();
120 $target = $this->getTargetUser();
121
122 $limit = $this->getValidatedParams()['limit'];
123 $segment = $this->getValidatedParams()['segment'];
124 $tag = $this->getValidatedParams()['tag'];
125 $contributionsSegment =
126 $this->contributionsLookup->getContributions( $target, $limit, $performer, $segment, $tag );
127
128 $revisions = $this->getRevisionsList( $contributionsSegment );
129 $urls = $this->constructURLs( $contributionsSegment );
130
131 $response = $urls + [ 'revisions' => $revisions ];
132
133 return $response;
134 }
135
143 private function getRevisionsList( ContributionsSegment $segment ) : array {
144 $revisionsData = [];
145 foreach ( $segment->getRevisions() as $revision ) {
146 $id = $revision->getId();
147 $revisionsData[] = [
148 "id" => $id,
149 "comment" => $revision->getComment()->text,
150 "timestamp" => wfTimestamp( TS_ISO_8601, $revision->getTimestamp() ),
151 "delta" => $segment->getDeltaForRevision( $id ) ,
152 "size" => $revision->getSize(),
153 "tags" => $segment->getTagsForRevision( $id ),
154 "page" => [
155 "id" => $revision->getPageId(),
156 "key" => $revision->getPageAsLinkTarget()->getDBkey(),
157 "title" => $revision->getPageAsLinkTarget()->getText()
158 ]
159 ];
160 }
161 return $revisionsData;
162 }
163
169 private function constructURLs( ContributionsSegment $segment ) {
170 $limit = $this->getValidatedParams()['limit'];
171 $tag = $this->getValidatedParams()['tag'];
172 $name = $this->getValidatedParams()['name'];
173 $urls = [];
174 $query = [ 'limit' => $limit, 'tag' => $tag ];
175 $pathParams = [ 'name' => $name ];
176
177 if ( $segment->isOldest() ) {
178 $urls['older'] = null;
179 } else {
180 $urls['older'] = $this->getRouteUrl( $pathParams, $query + [ 'segment' => $segment->getBefore() ] );
181 }
182
183 $urls['newer'] = $this->getRouteUrl( $pathParams, $query + [ 'segment' => $segment->getAfter() ] );
184 $urls['latest'] = $this->getRouteUrl( $pathParams, $query );
185 return $urls;
186 }
187
188 public function getParamSettings() {
189 return [
190 'name' => [
191 self::PARAM_SOURCE => 'path',
192 ParamValidator::PARAM_TYPE => 'string',
193 ParamValidator::PARAM_REQUIRED => false
194 ],
195 'limit' => [
196 self::PARAM_SOURCE => 'query',
197 ParamValidator::PARAM_TYPE => 'integer',
198 ParamValidator::PARAM_REQUIRED => false,
199 ParamValidator::PARAM_DEFAULT => self::MAX_LIMIT,
200 IntegerDef::PARAM_MIN => 1,
201 IntegerDef::PARAM_MAX => self::MAX_LIMIT,
202 ],
203 'segment' => [
204 self::PARAM_SOURCE => 'query',
205 ParamValidator::PARAM_TYPE => 'string',
206 ParamValidator::PARAM_REQUIRED => false,
207 ParamValidator::PARAM_DEFAULT => ''
208 ],
209 'tag' => [
210 self::PARAM_SOURCE => 'query',
211 ParamValidator::PARAM_TYPE => 'string',
212 ParamValidator::PARAM_REQUIRED => false,
213 ParamValidator::PARAM_DEFAULT => null,
214 ],
215 ];
216 }
217
218}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
getParamSettings()
Fetch ParamValidator settings for parameters.
__construct(ContributionsLookup $contributionsLookup, UserFactory $userFactory, UserNameUtils $userNameUtils)
const MAX_LIMIT
Hard limit results to 20 revisions.
getRevisionsList(ContributionsSegment $segment)
Returns list of revisions.
postInitSetup()
The handler can override this to do any necessary setup after init() is called to inject the dependen...
bool $me
User is requesting their own contributions.
getTargetUser()
Returns the user who's contributions we are requesting.
Base class for REST route handlers.
Definition Handler.php:16
getConfig()
Get the configuration array for the current route.
Definition Handler.php:140
getRouteUrl( $pathParams=[], $queryParams=[])
Get the URL of this handler's endpoint.
Definition Handler.php:92
getValidatedParams()
Fetch the validated parameters.
Definition Handler.php:257
getDeltaForRevision(int $revid)
Returns the difference in size of the given revision and its parent revision.
getTagsForRevision( $revId)
Returns an associative array mapping revision IDs to lists of tag names.
isOldest()
The value of the 'oldest' field of the flags passed to the constructor, or false if that field was no...
Creates User objects.
UserNameUtils service.
Group all the pieces relevant to the context of a request into one instance @newable.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
Value object representing a message for i18n.
Service for formatting and validating API parameters.
Type definition for integer types.
An interface similar to PSR-7's ResponseInterface, the primary difference being that it is mutable.
Interface for objects representing user identity.