Debian Codegraph @

includes/Rest/Handler/Helper/RevisionContentHelper.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Rest\Handler\Helper;
4
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\Page\ExistingPageRecord;
7 use MediaWiki\Rest\Handler;
8 use MediaWiki\Rest\LocalizedHttpException;
9 use MediaWiki\Rest\ResponseInterface;
10 use MediaWiki\Revision\RevisionRecord;
11 use MediaWiki\Revision\SlotRecord;
12 use Wikimedia\Message\MessageValue;
13 use Wikimedia\ParamValidator\ParamValidator;
14
15 /**
16 * @internal for use by core REST infrastructure
17 */
18 class RevisionContentHelper extends PageContentHelper {
19 /**
20 * @return int|null The ID of the target revision
21 */
22 public function getRevisionId(): ?int {
23 return isset( $this->parameters['id'] ) ? (int)$this->parameters['id'] : null;
24 }
25
26 /**
27 * @return string|null title text or null if unable to retrieve title
28 */
29 public function getTitleText(): ?string {
30 $revision = $this->getTargetRevision();
31 return $revision
32 ? $this->titleFormatter->getPrefixedText( $revision->getPageAsLinkTarget() )
33 : null;
34 }
35
36 /**
37 * @return ExistingPageRecord|null
38 */
39 public function getPage(): ?ExistingPageRecord {
40 $revision = $this->getTargetRevision();
41 return $revision ? $this->pageLookup->getPageByReference( $revision->getPage() ) : null;
42 }
43
44 /**
45 * @return RevisionRecord|null latest revision or null if unable to retrieve revision
46 */
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
59 /**
60 * @return bool
61 */
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 ||
72 !$revision->audienceCan( RevisionRecord::DELETED_TEXT, RevisionRecord::FOR_PUBLIC )
73 ) {
74 return false;
75 }
76
77 return true;
78 }
79
80 /**
81 * @return bool
82 */
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
98 /**
99 * @return array
100 */
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 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable
116 'key' => $this->titleFormatter->getPrefixedDBkey( $page ),
117 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable
118 'title' => $this->titleFormatter->getPrefixedText( $page ),
119 ],
120 'license' => [
121 'url' => $this->options->get( MainConfigNames::RightsUrl ),
122 'title' => $this->options->get( MainConfigNames::RightsText )
123 ],
124 ];
125
126 $revUser = $revision->getUser( RevisionRecord::FOR_THIS_USER, $this->authority );
127 if ( $revUser ) {
128 $metadata['user'] = [
129 'id' => $revUser->isRegistered() ? $revUser->getId() : null,
130 'name' => $revUser->getName()
131 ];
132 } else {
133 $metadata['user'] = null;
134 }
135
136 $comment = $revision->getComment( RevisionRecord::FOR_THIS_USER, $this->authority );
137 $metadata['comment'] = $comment ? $comment->text : null;
138
139 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable
140 $parent = $this->revisionLookup->getPreviousRevision( $revision );
141 if ( $parent ) {
142 $metadata['delta'] = $revision->getSize() - $parent->getSize();
143 } else {
144 $metadata['delta'] = null;
145 }
146
147 // FIXME: test fall fields
148 return $metadata;
149 }
150
151 /**
152 * @return array[]
153 */
154 public function getParamSettings(): array {
155 return [
156 'id' => [
157 Handler::PARAM_SOURCE => 'path',
158 ParamValidator::PARAM_TYPE => 'integer',
159 ParamValidator::PARAM_REQUIRED => true,
160 Handler::PARAM_DESCRIPTION => new MessageValue( 'rest-param-desc-revision-id' )
161 ],
162 ];
163 }
164
165 /**
166 * @throws LocalizedHttpException if the content is not accessible
167 */
168 public function checkAccess() {
169 $revId = $this->getRevisionId() ?? '';
170
171 if ( !$this->hasContent() ) {
172 throw new LocalizedHttpException(
173 MessageValue::new( 'rest-nonexistent-revision' )->plaintextParams( $revId ),
174 404
175 );
176 }
177
178 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Validated by hasContent
179 if ( !$this->isAccessible() || !$this->authority->authorizeRead( 'read', $this->getPageIdentity() ) ) {
180 throw new LocalizedHttpException(
181 MessageValue::new( 'rest-permission-denied-revision' )->plaintextParams( $revId ),
182 403
183 );
184 }
185 }
186
187 }