MediaWiki  master
RevisionContentHelper.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
13 
18 
22  public function getRevisionId(): ?int {
23  return isset( $this->parameters['id'] ) ? (int)$this->parameters['id'] : null;
24  }
25 
29  public function getTitleText(): ?string {
30  $revision = $this->getTargetRevision();
31  return $revision
32  ? $this->titleFormatter->getPrefixedText( $revision->getPageAsLinkTarget() )
33  : null;
34  }
35 
39  public function getPage(): ?ExistingPageRecord {
40  $revision = $this->getTargetRevision();
41  return $revision ? $this->pageLookup->getPageByReference( $revision->getPage() ) : null;
42  }
43 
47  public function getTargetRevision(): ?RevisionRecord {
48  if ( $this->targetRevision === false ) {
49  $revId = $this->getRevisionId();
50  if ( $revId ) {
51  $this->targetRevision = $this->revisionLookup->getRevisionById( $revId );
52  } else {
53  $this->targetRevision = null;
54  }
55  }
56  return $this->targetRevision;
57  }
58 
62  public function isAccessible(): bool {
63  if ( !parent::isAccessible() ) {
64  return false;
65  }
66 
67  $revision = $this->getTargetRevision();
68 
69  // TODO: allow authorized users to see suppressed content. Set cache control accordingly.
70 
71  if ( !$revision ||
73  ) {
74  return false;
75  }
76 
77  return true;
78  }
79 
83  public function hasContent(): bool {
84  return (bool)$this->getTargetRevision();
85  }
86 
87  public function setCacheControl( ResponseInterface $response, int $expiry = null ) {
88  $revision = $this->getTargetRevision();
89 
90  if ( $revision && $revision->getVisibility() !== 0 ) {
91  // The revision is not public, so it's not cacheable!
92  return;
93  }
94 
95  parent::setCacheControl( $response, $expiry );
96  }
97 
101  public function constructMetadata(): array {
102  $page = $this->getPage();
103  $revision = $this->getTargetRevision();
104 
105  $mainSlot = $revision->getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
106 
107  $metadata = [
108  'id' => $revision->getId(),
109  'size' => $revision->getSize(),
110  'minor' => $revision->isMinor(),
111  'timestamp' => wfTimestampOrNull( TS_ISO_8601, $revision->getTimestamp() ),
112  'content_model' => $mainSlot->getModel(),
113  'page' => [
114  'id' => $page->getId(),
115  'key' => $this->titleFormatter->getPrefixedDBkey( $page ),
116  'title' => $this->titleFormatter->getPrefixedText( $page ),
117  ],
118  'license' => [
119  'url' => $this->config->get( 'RightsUrl' ),
120  'title' => $this->config->get( 'RightsText' )
121  ],
122  ];
123 
124  $revUser = $revision->getUser( RevisionRecord::FOR_THIS_USER, $this->authority );
125  if ( $revUser ) {
126  $metadata['user'] = [
127  'id' => $revUser->isRegistered() ? $revUser->getId() : null,
128  'name' => $revUser->getName()
129  ];
130  } else {
131  $metadata['user'] = null;
132  }
133 
134  $comment = $revision->getComment( RevisionRecord::FOR_THIS_USER, $this->authority );
135  $metadata['comment'] = $comment ? $comment->text : null;
136 
137  $parent = $this->revisionLookup->getPreviousRevision( $revision );
138  if ( $parent ) {
139  $metadata['delta'] = $revision->getSize() - $parent->getSize();
140  } else {
141  $metadata['delta'] = null;
142  }
143 
144  // FIXME: test fall fields
145  return $metadata;
146  }
147 
151  public function getParamSettings(): array {
152  return [
153  'id' => [
154  Handler::PARAM_SOURCE => 'path',
155  ParamValidator::PARAM_TYPE => 'integer',
156  ParamValidator::PARAM_REQUIRED => true,
157  ],
158  ];
159  }
160 
164  public function checkAccess() {
165  $revId = $this->getRevisionId() ?? '';
166 
167  if ( !$this->hasContent() ) {
168  throw new LocalizedHttpException(
169  MessageValue::new( 'rest-nonexistent-revision' )->plaintextParams( $revId ),
170  404
171  );
172  }
173 
174  if ( !$this->authority->authorizeRead( 'read', $this->getPage() ) ) {
175  throw new LocalizedHttpException(
176  MessageValue::new( 'rest-permission-denied-revision' )->plaintextParams( $revId ),
177  403
178  );
179  }
180  }
181 
182 }
MediaWiki\Revision\RevisionRecord\RAW
const RAW
Definition: RevisionRecord.php:64
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
MediaWiki\Rest\Handler\RevisionContentHelper
Definition: RevisionContentHelper.php:17
MediaWiki\Rest\Handler\PARAM_SOURCE
const PARAM_SOURCE
(string) ParamValidator constant to specify the source of the parameter.
Definition: Handler.php:26
MediaWiki\Rest\Handler\RevisionContentHelper\getParamSettings
getParamSettings()
Definition: RevisionContentHelper.php:151
MediaWiki\Rest\Handler\RevisionContentHelper\getPage
getPage()
Definition: RevisionContentHelper.php:39
MediaWiki\Rest\Handler\RevisionContentHelper\getRevisionId
getRevisionId()
Definition: RevisionContentHelper.php:22
MediaWiki\Rest\Handler\RevisionContentHelper\constructMetadata
constructMetadata()
Definition: RevisionContentHelper.php:101
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
MediaWiki\Revision\RevisionRecord\DELETED_TEXT
const DELETED_TEXT
Definition: RevisionRecord.php:53
wfTimestampOrNull
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
Definition: GlobalFunctions.php:1707
MediaWiki\Rest\Handler\RevisionContentHelper\setCacheControl
setCacheControl(ResponseInterface $response, int $expiry=null)
Sets the 'Cache-Control' header no more then provided $expiry.
Definition: RevisionContentHelper.php:87
MediaWiki\Rest\Handler\RevisionContentHelper\getTitleText
getTitleText()
Definition: RevisionContentHelper.php:29
Page\ExistingPageRecord
Data record representing a page that currently exists as an editable page on a wiki.
Definition: ExistingPageRecord.php:15
MediaWiki\Rest\Handler\RevisionContentHelper\isAccessible
isAccessible()
Definition: RevisionContentHelper.php:62
MediaWiki\Rest\ResponseInterface
An interface similar to PSR-7's ResponseInterface, the primary difference being that it is mutable.
Definition: ResponseInterface.php:41
MediaWiki\Rest\Handler\RevisionContentHelper\checkAccess
checkAccess()
Definition: RevisionContentHelper.php:164
MediaWiki\Rest\Handler\PageContentHelper
Definition: PageContentHelper.php:25
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:42
MediaWiki\Revision\RevisionRecord\FOR_THIS_USER
const FOR_THIS_USER
Definition: RevisionRecord.php:63
MediaWiki\Rest\Handler\RevisionContentHelper\getTargetRevision
getTargetRevision()
Definition: RevisionContentHelper.php:47
MediaWiki\Rest\Handler\PageContentHelper\$targetRevision
RevisionRecord false null $targetRevision
Definition: PageContentHelper.php:47
MediaWiki\Revision\RevisionRecord\FOR_PUBLIC
const FOR_PUBLIC
Definition: RevisionRecord.php:62
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
MediaWiki\Rest\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10
MediaWiki\Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
MediaWiki\Rest\Handler\RevisionContentHelper\hasContent
hasContent()
Definition: RevisionContentHelper.php:83