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