MediaWiki  master
RevisionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
11 use RequestContext;
12 use User;
15 
21  private $revisionLookup;
22 
25 
27  private $user;
28 
32  private $revision = null;
33 
38  public function __construct(
41  ) {
42  $this->revisionLookup = $revisionLookup;
43  $this->permissionManager = $permissionManager;
44 
45  // @todo Inject this, when there is a good way to do that
46  $this->user = RequestContext::getMain()->getUser();
47  }
48 
52  private function getRevision() {
53  $id = $this->getValidatedParams()['id'];
54 
55  if ( $this->revision === null ) {
56  $rev = $this->revisionLookup->getRevisionById( $id );
57 
58  // If null was returned, remember it as false, since null means uninitialized.
59  $this->revision = $rev ?: false;
60  }
61 
62  return $this->revision;
63  }
64 
70  public function run( $id ) {
71  $rev = $this->getRevision();
72  if ( !$rev ) {
73  throw new LocalizedHttpException(
74  new MessageValue( 'rest-nonexistent-revision', [ $id ] ), 404 );
75  }
76  if ( !$this->permissionManager->userCan( 'read', $this->user, $rev->getPageAsLinkTarget() ) ) {
77  throw new LocalizedHttpException(
78  new MessageValue( 'rest-permission-denied-revision', [ $id ] ), 403 );
79  }
80 
81  $responseData = [
82  'id' => $rev->getId(),
83  'page' => [
84  'id' => $rev->getPageId(),
85  'title' => $rev->getPageAsLinkTarget()->getText(),
86  ],
87  'size' => $rev->getSize(),
88  'minor' => $rev->isMinor(),
89  'timestamp' => wfTimestamp( TS_ISO_8601, $rev->getTimestamp() ),
90  ];
91 
92  $revUser = $rev->getUser( RevisionRecord::FOR_THIS_USER, $this->user );
93  if ( $revUser ) {
94  $responseData['user'] = [
95  'id' => $revUser->isRegistered() ? $revUser->getId() : null,
96  'name' => $revUser->getName()
97  ];
98  } else {
99  $responseData['user'] = null;
100  }
101 
102  $comment = $rev->getComment( RevisionRecord::FOR_THIS_USER, $this->user );
103  $responseData['comment'] = $comment ? $comment->text : null;
104 
105  $parent = $this->revisionLookup->getPreviousRevision( $rev );
106  if ( $parent ) {
107  $responseData['delta'] = $rev->getSize() - $parent->getSize();
108  } else {
109  $responseData['delta'] = null;
110  }
111  return $this->getResponseFactory()->createJson( $responseData );
112  }
113 
114  public function needsWriteAccess() {
115  return false;
116  }
117 
118  public function getParamSettings() {
119  return [
120  'id' => [
121  self::PARAM_SOURCE => 'path',
122  ParamValidator::PARAM_TYPE => 'integer',
123  ParamValidator::PARAM_REQUIRED => true,
124  ],
125  ];
126  }
127 
135  protected function getETag(): ?string {
136  $rev = $this->getRevision();
137  if ( !$rev || $rev->getVisibility() !== 0 ) {
138  return null;
139  }
140 
141  $tag = $rev->getId();
142 
143  return '"' . $tag . '"';
144  }
145 
153  protected function getLastModified(): ?string {
154  $rev = $this->getRevision();
155  if ( !$rev || $rev->getVisibility() !== 0 ) {
156  return null;
157  }
158 
159  return $rev->getTimestamp();
160  }
161 
165  protected function hasRepresentation() {
166  $rev = $this->getRevision();
167  return $rev ? true : false;
168  }
169 }
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
MediaWiki\Rest\Handler\RevisionHandler\__construct
__construct(RevisionLookup $revisionLookup, PermissionManager $permissionManager)
Definition: RevisionHandler.php:38
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:151
MediaWiki\Rest\Handler\RevisionHandler\run
run( $id)
Definition: RevisionHandler.php:70
true
return true
Definition: router.php:90
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1809
MediaWiki\Rest\Handler\RevisionHandler\getLastModified
getLastModified()
Returns the requested revision's timestamp.
Definition: RevisionHandler.php:153
MediaWiki\Rest\Handler\RevisionHandler\$permissionManager
PermissionManager $permissionManager
Definition: RevisionHandler.php:24
Revision\RevisionLookup
Service for looking up page revisions.
Definition: RevisionLookup.php:38
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
MediaWiki\Rest\Handler\RevisionHandler\getETag
getETag()
Returns an ETag representing the requested revision.
Definition: RevisionHandler.php:135
MediaWiki\Rest\Handler\RevisionHandler\$user
User $user
Definition: RevisionHandler.php:27
MediaWiki\Rest\Handler\RevisionHandler\hasRepresentation
hasRepresentation()
Definition: RevisionHandler.php:165
MediaWiki\Rest\Handler\RevisionHandler\getRevision
getRevision()
Definition: RevisionHandler.php:52
MediaWiki\Rest\Response
Definition: Response.php:8
MediaWiki\Rest\Handler\RevisionHandler\$revision
RevisionRecord bool null $revision
Definition: RevisionHandler.php:32
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\Handler\RevisionHandler\$revisionLookup
RevisionLookup $revisionLookup
Definition: RevisionHandler.php:21
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:49
MediaWiki\Rest\Handler\RevisionHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: RevisionHandler.php:118
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:451
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
MediaWiki\Rest\Handler\RevisionHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: RevisionHandler.php:114
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
MediaWiki\Rest\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10
MediaWiki\Rest\Handler\RevisionHandler
Handler class for Core REST API endpoints that perform operations on revisions.
Definition: RevisionHandler.php:19
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:15