MediaWiki  master
RevisionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
10 use User;
15 
21  private $revisionLookup;
22 
25 
27  private $user;
28 
33  public function __construct(
36  ) {
37  $this->revisionLookup = $revisionLookup;
38  $this->permissionManager = $permissionManager;
39 
40  // @todo Inject this, when there is a good way to do that
41  $this->user = RequestContext::getMain()->getUser();
42  }
43 
49  public function run( $id ) {
50  $rev = $this->revisionLookup->getRevisionById( $id );
51  if ( !$rev ) {
52  throw new LocalizedHttpException(
53  new MessageValue( 'rest-nonexistent-revision', [ $id ] ), 404 );
54  }
55  if ( !$this->permissionManager->userCan( 'read', $this->user, $rev->getPageAsLinkTarget() ) ) {
56  throw new LocalizedHttpException(
57  new MessageValue( 'rest-permission-denied-revision', [ $id ] ), 403 );
58  }
59 
60  $responseData = [
61  'id' => $rev->getId(),
62  'page' => [
63  'id' => $rev->getPageId(),
64  'title' => $rev->getPageAsLinkTarget()->getText(),
65  ],
66  'size' => $rev->getSize(),
67  'minor' => $rev->isMinor(),
68  'timestamp' => wfTimestamp( TS_ISO_8601, $rev->getTimestamp() ),
69  ];
70 
71  $revUser = $rev->getUser( RevisionRecord::FOR_THIS_USER, $this->user );
72  if ( $revUser ) {
73  $responseData['user'] = [
74  'id' => $revUser->isRegistered() ? $revUser->getId() : null,
75  'name' => $revUser->getName()
76  ];
77  } else {
78  $responseData['user'] = null;
79  }
80 
81  $comment = $rev->getComment( RevisionRecord::FOR_THIS_USER, $this->user );
82  $responseData['comment'] = $comment ? $comment->text : null;
83 
84  $parent = $this->revisionLookup->getPreviousRevision( $rev );
85  if ( $parent ) {
86  $responseData['delta'] = $rev->getSize() - $parent->getSize();
87  } else {
88  $responseData['delta'] = null;
89  }
90  return $this->getResponseFactory()->createJson( $responseData );
91  }
92 
93  public function needsWriteAccess() {
94  return false;
95  }
96 
97  public function getParamSettings() {
98  return [
99  'id' => [
100  self::PARAM_SOURCE => 'path',
101  ParamValidator::PARAM_TYPE => 'integer',
102  ParamValidator::PARAM_REQUIRED => true,
103  ],
104  ];
105  }
106 }
Value object representing a message for i18n.
Handler class for Core REST API endpoints that perform operations on revisions.
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:92
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
static getMain()
Get the RequestContext object associated with the main request.
Service for looking up page revisions.
__construct(RevisionLookup $revisionLookup, PermissionManager $permissionManager)
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...