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 }
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
getTargetUser()
Returns the user who's contributions we are requesting.
getParamSettings()
Fetch ParamValidator settings for parameters.
getRouteUrl( $pathParams=[], $queryParams=[])
Get the URL of this handler's endpoint.
Definition: Handler.php:110
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:358
getAuthority()
Get the current acting authority.
Definition: Handler.php:157
getDeltaForRevision(int $revid)
Returns the difference in size of the given revision and its parent revision.
getTagsForRevision( $revId)
Get tags and associated metadata for a given revision.
Service for formatting and validating API parameters.
Type definition for integer types.
Definition: IntegerDef.php:23
An interface similar to PSR-7's ResponseInterface, the primary difference being that it is mutable.
Interface for objects representing user identity.
Copyright (C) 2011-2020 Wikimedia Foundation and others.