MediaWiki  master
UserContributionsHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
12 
17 
22  public function execute() {
23  $target = $this->getTargetUser();
24  $limit = $this->getValidatedParams()['limit'];
25  $segment = $this->getValidatedParams()['segment'];
26  $tag = $this->getValidatedParams()['tag'];
27  $contributionsSegment =
28  $this->contributionsLookup->getContributions( $target, $limit, $this->getAuthority(), $segment, $tag );
29 
30  $contributions = $this->getContributionsList( $contributionsSegment );
31  $urls = $this->constructURLs( $contributionsSegment );
32 
33  $response = $urls + [ 'contributions' => $contributions ];
34 
35  return $response;
36  }
37 
45  private function getContributionsList( ContributionsSegment $segment ): array {
46  $revisionsData = [];
47  foreach ( $segment->getRevisions() as $revision ) {
48  $id = $revision->getId();
49  $tags = [];
50  foreach ( $segment->getTagsForRevision( $id ) as $tag => $message ) {
51  $tags[] = [ 'name' => $tag, 'description' => $message->parse() ];
52  }
53  $revisionsData[] = [
54  "id" => $id,
55  "comment" => $revision->getComment()->text,
56  "timestamp" => wfTimestamp( TS_ISO_8601, $revision->getTimestamp() ),
57  "delta" => $segment->getDeltaForRevision( $id ) ,
58  "size" => $revision->getSize(),
59  "tags" => $tags,
60  // Contribution type will always be MediaWiki revisions,
61  // until we can reliably include contributions from other sources. See T257839.
62  "type" => 'revision',
63  "page" => [
64  "id" => $revision->getPageId(),
65  "key" => $revision->getPageAsLinkTarget()->getDBkey(),
66  "title" => $revision->getPageAsLinkTarget()->getText()
67  ]
68  ];
69  }
70  return $revisionsData;
71  }
72 
78  private function constructURLs( ContributionsSegment $segment ): array {
79  $limit = $this->getValidatedParams()['limit'];
80  $tag = $this->getValidatedParams()['tag'];
82  $user = $this->getValidatedParams()['user'] ?? null;
83  $name = $user ? $user->getName() : null;
84 
85  $urls = [];
86  $query = [ 'limit' => $limit, 'tag' => $tag ];
87  $pathParams = [ 'user' => $name ];
88 
89  if ( $segment->isOldest() ) {
90  $urls['older'] = null;
91  } else {
92  $urls['older'] = $this->getRouteUrl( $pathParams, $query + [ 'segment' => $segment->getBefore() ] );
93  }
94 
95  $urls['newer'] = $this->getRouteUrl( $pathParams, $query + [ 'segment' => $segment->getAfter() ] );
96  $urls['latest'] = $this->getRouteUrl( $pathParams, $query );
97  return $urls;
98  }
99 
100  public function getParamSettings() {
101  $settings = [
102  'limit' => [
103  self::PARAM_SOURCE => 'query',
104  ParamValidator::PARAM_TYPE => 'integer',
105  ParamValidator::PARAM_REQUIRED => false,
106  ParamValidator::PARAM_DEFAULT => self::MAX_LIMIT,
107  IntegerDef::PARAM_MIN => 1,
108  IntegerDef::PARAM_MAX => self::MAX_LIMIT
109  ],
110  'segment' => [
111  self::PARAM_SOURCE => 'query',
112  ParamValidator::PARAM_TYPE => 'string',
113  ParamValidator::PARAM_REQUIRED => false,
114  ParamValidator::PARAM_DEFAULT => ''
115  ],
116  'tag' => [
117  self::PARAM_SOURCE => 'query',
118  ParamValidator::PARAM_TYPE => 'string',
119  ParamValidator::PARAM_REQUIRED => false,
120  ParamValidator::PARAM_DEFAULT => null
121  ],
122  ];
123  if ( $this->me === false ) {
124  $settings['user'] = [
125  self::PARAM_SOURCE => 'path',
126  ParamValidator::PARAM_REQUIRED => true,
127  ParamValidator::PARAM_TYPE => 'user',
128  UserDef::PARAM_RETURN_OBJECT => true,
129  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip' ],
130  ];
131  }
132  return $settings;
133  }
134 
135 }
MediaWiki\Rest\Handler\AbstractContributionHandler
Definition: AbstractContributionHandler.php:15
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
MediaWiki\Revision\ContributionsSegment\getRevisions
getRevisions()
Definition: ContributionsSegment.php:84
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1668
MediaWiki\Rest\Handler\UserContributionsHandler\constructURLs
constructURLs(ContributionsSegment $segment)
Definition: UserContributionsHandler.php:78
MediaWiki\Rest\Handler\UserContributionsHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: UserContributionsHandler.php:100
MediaWiki\Revision\ContributionsSegment\getBefore
getBefore()
Definition: ContributionsSegment.php:91
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:26
MediaWiki\Revision\ContributionsSegment\getAfter
getAfter()
Definition: ContributionsSegment.php:98
MediaWiki\Rest\Handler\UserContributionsHandler
Definition: UserContributionsHandler.php:16
MediaWiki\Revision\ContributionsSegment\getTagsForRevision
getTagsForRevision( $revId)
Get tags and associated metadata for a given revision.
Definition: ContributionsSegment.php:77
MediaWiki\Rest\Handler\getRouteUrl
getRouteUrl( $pathParams=[], $queryParams=[])
Get the URL of this handler's endpoint.
Definition: Handler.php:101
MediaWiki\Revision\ContributionsSegment
@newable
Definition: ContributionsSegment.php:11
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:282
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:58
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:23
MediaWiki\Revision\ContributionsSegment\getDeltaForRevision
getDeltaForRevision(int $revid)
Returns the difference in size of the given revision and its parent revision.
Definition: ContributionsSegment.php:108
MediaWiki\Rest\Handler\UserContributionsHandler\execute
execute()
Definition: UserContributionsHandler.php:22
MediaWiki\Rest\Handler\UserContributionsHandler\getContributionsList
getContributionsList(ContributionsSegment $segment)
Returns list of revisions.
Definition: UserContributionsHandler.php:45
MediaWiki\Rest\Handler\getAuthority
getAuthority()
Get the current acting authority.
Definition: Handler.php:148
MediaWiki\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
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
MediaWiki\Rest\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10