MediaWiki  master
UserContributionsHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
9 // phpcs:ignore MediaWiki.Classes.UnusedUseStatement.UnusedUse
11 use RequestContext;
14 
19 
24  public function execute() {
25  $performer = RequestContext::getMain()->getUser();
26  $target = $this->getTargetUser();
27  $limit = $this->getValidatedParams()['limit'];
28  $segment = $this->getValidatedParams()['segment'];
29  $tag = $this->getValidatedParams()['tag'];
30  $contributionsSegment =
31  $this->contributionsLookup->getContributions( $target, $limit, $performer, $segment, $tag );
32 
33  $contributions = $this->getContributionsList( $contributionsSegment );
34  $urls = $this->constructURLs( $contributionsSegment );
35 
36  $response = $urls + [ 'contributions' => $contributions ];
37 
38  return $response;
39  }
40 
48  private function getContributionsList( ContributionsSegment $segment ) : array {
49  $revisionsData = [];
50  foreach ( $segment->getRevisions() as $revision ) {
51  $id = $revision->getId();
52  $tags = array_map( function ( $tag ) {
53  return [ 'text' => $tag ];
54  }, $segment->getTagsForRevision( $id ) );
55 
56  $revisionsData[] = [
57  "id" => $id,
58  "comment" => $revision->getComment()->text,
59  "timestamp" => wfTimestamp( TS_ISO_8601, $revision->getTimestamp() ),
60  "delta" => $segment->getDeltaForRevision( $id ) ,
61  "size" => $revision->getSize(),
62  "tags" => $tags,
63  // Contribution type will always be MediaWiki revisions,
64  // until we can reliably include contributions from other sources. See T257839.
65  "type" => 'revision',
66  "page" => [
67  "id" => $revision->getPageId(),
68  "key" => $revision->getPageAsLinkTarget()->getDBkey(),
69  "title" => $revision->getPageAsLinkTarget()->getText()
70  ]
71  ];
72  }
73  return $revisionsData;
74  }
75 
81  private function constructURLs( ContributionsSegment $segment ) {
82  $limit = $this->getValidatedParams()['limit'];
83  $tag = $this->getValidatedParams()['tag'];
84  /* @var UserIdentity $user */
85  $user = $this->getValidatedParams()['user'] ?? null;
86  $name = $user ? $user->getName() : null;
87 
88  $urls = [];
89  $query = [ 'limit' => $limit, 'tag' => $tag ];
90  $pathParams = [ 'user' => $name ];
91 
92  if ( $segment->isOldest() ) {
93  $urls['older'] = null;
94  } else {
95  $urls['older'] = $this->getRouteUrl( $pathParams, $query + [ 'segment' => $segment->getBefore() ] );
96  }
97 
98  $urls['newer'] = $this->getRouteUrl( $pathParams, $query + [ 'segment' => $segment->getAfter() ] );
99  $urls['latest'] = $this->getRouteUrl( $pathParams, $query );
100  return $urls;
101  }
102 
103  public function getParamSettings() {
104  $settings = [
105  'limit' => [
106  self::PARAM_SOURCE => 'query',
107  ParamValidator::PARAM_TYPE => 'integer',
108  ParamValidator::PARAM_REQUIRED => false,
109  ParamValidator::PARAM_DEFAULT => self::MAX_LIMIT,
110  IntegerDef::PARAM_MIN => 1,
111  IntegerDef::PARAM_MAX => self::MAX_LIMIT
112  ],
113  'segment' => [
114  self::PARAM_SOURCE => 'query',
115  ParamValidator::PARAM_TYPE => 'string',
116  ParamValidator::PARAM_REQUIRED => false,
117  ParamValidator::PARAM_DEFAULT => ''
118  ],
119  'tag' => [
120  self::PARAM_SOURCE => 'query',
121  ParamValidator::PARAM_TYPE => 'string',
122  ParamValidator::PARAM_REQUIRED => false,
123  ParamValidator::PARAM_DEFAULT => null
124  ],
125  ];
126  if ( $this->me === false ) {
127  $settings['user'] = [
128  self::PARAM_SOURCE => 'path',
129  ParamValidator::PARAM_REQUIRED => true,
130  ParamValidator::PARAM_TYPE => 'user',
131  UserDef::PARAM_RETURN_OBJECT => true,
132  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip' ],
133  ];
134  }
135  return $settings;
136  }
137 
138 }
MediaWiki\Rest\Handler\AbstractContributionHandler
Definition: AbstractContributionHandler.php:16
Revision\ContributionsSegment\isOldest
isOldest()
The value of the 'oldest' field of the flags passed to the constructor, or false if that field was no...
Definition: ContributionsSegment.php:128
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
Revision\ContributionsSegment\getBefore
getBefore()
Definition: ContributionsSegment.php:91
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1808
Revision\ContributionsSegment\getRevisions
getRevisions()
Definition: ContributionsSegment.php:84
MediaWiki\Rest\Handler\UserContributionsHandler\constructURLs
constructURLs(ContributionsSegment $segment)
Definition: UserContributionsHandler.php:81
MediaWiki\Rest\Handler\UserContributionsHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: UserContributionsHandler.php:103
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:25
MediaWiki\Rest\Handler\UserContributionsHandler
Definition: UserContributionsHandler.php:18
MediaWiki\Rest\Handler\getRouteUrl
getRouteUrl( $pathParams=[], $queryParams=[])
Get the URL of this handler's endpoint.
Definition: Handler.php:92
Revision\ContributionsSegment
@newable
Definition: ContributionsSegment.php:9
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\ResponseInterface
An interface similar to PSR-7's ResponseInterface, the primary difference being that it is mutable.
Definition: ResponseInterface.php:41
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:453
Revision\ContributionsSegment\getAfter
getAfter()
Definition: ContributionsSegment.php:98
Wikimedia\ParamValidator\TypeDef\IntegerDef
Type definition for integer types.
Definition: IntegerDef.php:23
MediaWiki\Rest\Handler\AbstractContributionHandler\MAX_LIMIT
const MAX_LIMIT
Hard limit results to 20 contributions.
Definition: AbstractContributionHandler.php:24
MediaWiki\Rest\Handler\UserContributionsHandler\execute
execute()
Definition: UserContributionsHandler.php:24
MediaWiki\Rest\Handler\UserContributionsHandler\getContributionsList
getContributionsList(ContributionsSegment $segment)
Returns list of revisions.
Definition: UserContributionsHandler.php:48
Revision\ContributionsSegment\getDeltaForRevision
getDeltaForRevision(int $revid)
Returns the difference in size of the given revision and its parent revision.
Definition: ContributionsSegment.php:108
Revision\ContributionsSegment\getTagsForRevision
getTagsForRevision( $revId)
Returns an associative array mapping revision IDs to lists of tag names.
Definition: ContributionsSegment.php:77
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
MediaWiki\Rest\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10