MediaWiki
REL1_35
RevisionHandler.php
Go to the documentation of this file.
1
<?php
2
3
namespace
MediaWiki\Rest\Handler
;
4
5
use
MediaWiki\Permissions\PermissionManager
;
6
use
MediaWiki\Rest\LocalizedHttpException
;
7
use
MediaWiki\Rest\Response
;
8
use
MediaWiki\Rest\SimpleHandler
;
9
use
MediaWiki\Revision\RevisionLookup
;
10
use
MediaWiki\Revision\RevisionRecord
;
11
use
RequestContext
;
12
use
User
;
13
use
Wikimedia\Message\MessageValue
;
14
use
Wikimedia\ParamValidator\ParamValidator
;
15
19
class
RevisionHandler
extends
SimpleHandler
{
21
private
$revisionLookup
;
22
24
private
$permissionManager
;
25
27
private
$user
;
28
32
private
$revision
=
null
;
33
38
public
function
__construct
(
39
RevisionLookup
$revisionLookup
,
40
PermissionManager
$permissionManager
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
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition
GlobalFunctions.php:1803
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition
PermissionManager.php:49
MediaWiki\Rest\Handler\RevisionHandler
Handler class for Core REST API endpoints that perform operations on revisions.
Definition
RevisionHandler.php:19
MediaWiki\Rest\Handler\RevisionHandler\$permissionManager
PermissionManager $permissionManager
Definition
RevisionHandler.php:24
MediaWiki\Rest\Handler\RevisionHandler\getETag
getETag()
Returns an ETag representing the requested revision.
Definition
RevisionHandler.php:135
MediaWiki\Rest\Handler\RevisionHandler\$revisionLookup
RevisionLookup $revisionLookup
Definition
RevisionHandler.php:21
MediaWiki\Rest\Handler\RevisionHandler\run
run( $id)
Definition
RevisionHandler.php:70
MediaWiki\Rest\Handler\RevisionHandler\getRevision
getRevision()
Definition
RevisionHandler.php:52
MediaWiki\Rest\Handler\RevisionHandler\$user
User $user
Definition
RevisionHandler.php:27
MediaWiki\Rest\Handler\RevisionHandler\__construct
__construct(RevisionLookup $revisionLookup, PermissionManager $permissionManager)
Definition
RevisionHandler.php:38
MediaWiki\Rest\Handler\RevisionHandler\getLastModified
getLastModified()
Returns the requested revision's timestamp.
Definition
RevisionHandler.php:153
MediaWiki\Rest\Handler\RevisionHandler\hasRepresentation
hasRepresentation()
Definition
RevisionHandler.php:165
MediaWiki\Rest\Handler\RevisionHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition
RevisionHandler.php:114
MediaWiki\Rest\Handler\RevisionHandler\$revision
RevisionRecord bool null $revision
Definition
RevisionHandler.php:32
MediaWiki\Rest\Handler\RevisionHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition
RevisionHandler.php:118
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition
Handler.php:257
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition
Handler.php:151
MediaWiki\Rest\LocalizedHttpException
@newable
Definition
LocalizedHttpException.php:10
MediaWiki\Rest\Response
Definition
Response.php:8
MediaWiki\Rest\SimpleHandler
Definition
SimpleHandler.php:15
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition
RevisionRecord.php:46
RequestContext
Group all the pieces relevant to the context of a request into one instance @newable.
Definition
RequestContext.php:38
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition
User.php:60
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition
MessageValue.php:16
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition
ParamValidator.php:42
MediaWiki\Revision\RevisionLookup
Service for looking up page revisions.
Definition
RevisionLookup.php:38
MediaWiki\Rest\Handler
Definition
ActionModuleBasedHandler.php:3
true
return true
Definition
router.php:92
includes
Rest
Handler
RevisionHandler.php
Generated on Sat Apr 6 2024 00:07:43 for MediaWiki by
1.9.8