MediaWiki REL1_35
RevisionHandler.php
Go to the documentation of this file.
1<?php
2
4
12use User;
15
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}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Handler class for Core REST API endpoints that perform operations on revisions.
getETag()
Returns an ETag representing the requested revision.
__construct(RevisionLookup $revisionLookup, PermissionManager $permissionManager)
getLastModified()
Returns the requested revision's timestamp.
needsWriteAccess()
Indicates whether this route requires write access.
getParamSettings()
Fetch ParamValidator settings for parameters.
getValidatedParams()
Fetch the validated parameters.
Definition Handler.php:257
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition Handler.php:151
Page revision base class.
Group all the pieces relevant to the context of a request into one instance @newable.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
Value object representing a message for i18n.
Service for formatting and validating API parameters.
Service for looking up page revisions.
return true
Definition router.php:92