MediaWiki  master
PageSourceHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
11 use TextContent;
13 
18  private const MAX_AGE_200 = 5;
19 
20  // Default to main slot
21  private function getRole(): string {
22  return SlotRecord::MAIN;
23  }
24 
31  protected function getPageContent( string $slotRole, RevisionRecord $revision ): TextContent {
32  try {
33  $content = $revision
34  ->getSlot( $slotRole, RevisionRecord::FOR_THIS_USER, $this->user )
35  ->getContent()
36  ->convert( CONTENT_MODEL_TEXT );
37  if ( !( $content instanceof TextContent ) ) {
38  throw new LocalizedHttpException( MessageValue::new( 'rest-page-source-type-error' ), 400 );
39  }
40  } catch ( SuppressedDataException $e ) {
41  throw new LocalizedHttpException(
42  MessageValue::new( 'rest-permission-denied-revision' )->numParams( $revision->getId() ),
43  403
44  );
45  } catch ( RevisionAccessException $e ) {
46  throw new LocalizedHttpException(
47  MessageValue::new( 'rest-nonexistent-revision' )->numParams( $revision->getId() ),
48  404
49  );
50  }
51  return $content;
52  }
53 
59  public function run( string $title ): Response {
60  $titleObject = $this->getTitle();
61  if ( !$titleObject || !$titleObject->getArticleID() ) {
62  throw new LocalizedHttpException(
63  MessageValue::new( 'rest-nonexistent-title' )->plaintextParams( $title ),
64  404
65  );
66  }
67  if ( !$this->isAccessible( $titleObject ) ) {
68  throw new LocalizedHttpException(
69  MessageValue::new( 'rest-permission-denied-title' )->plaintextParams( $title ),
70  403
71  );
72  }
73  $revision = $this->getLatestRevision();
74  if ( !$revision ) {
75  throw new LocalizedHttpException(
76  MessageValue::new( 'rest-no-revision' ),
77  404
78  );
79  }
80  $content = $this->getPageContent( $this->getRole(), $revision );
81  $body = $this->constructMetadata( $titleObject, $revision );
82  $body['source'] = $content->getText();
83 
84  $response = $this->getResponseFactory()->createJson( $body );
85  $response->setHeader( 'Cache-Control', 'max-age=' . self::MAX_AGE_200 );
86  return $response;
87  }
88 
95  protected function getETag(): string {
96  $revision = $this->getLatestRevision();
97  $latestRevision = $revision ? $revision->getId() : 'e0';
98 
99  $isAccessible = $this->isAccessible( $this->getTitle() );
100  $accessibleTag = $isAccessible ? 'a1' : 'a0';
101 
102  $revisionTag = $latestRevision . $accessibleTag;
103  return '"' . sha1( "$revisionTag" ) . '"';
104  }
105 
109  protected function getLastModified(): ?string {
110  $revision = $this->getLatestRevision();
111  if ( $revision ) {
112  return $revision->getTimestamp();
113  }
114  return null;
115  }
116 }
MediaWiki\Rest\Handler
Definition: ActionModuleBasedHandler.php:3
Revision\RevisionAccessException
Exception representing a failure to look up a revision.
Definition: RevisionAccessException.php:33
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:93
$response
$response
Definition: opensearch_desc.php:44
MediaWiki\Rest\Handler\PageSourceHandler\getRole
getRole()
Definition: PageSourceHandler.php:21
Revision\SuppressedDataException
Exception raised in response to an audience check when attempting to access suppressed information wi...
Definition: SuppressedDataException.php:32
Title\getArticleID
getArticleID( $flags=0)
Get the article ID for this Title from the link cache, adding it if necessary.
Definition: Title.php:3171
Revision\RevisionRecord\getSlot
getSlot( $role, $audience=self::FOR_PUBLIC, User $user=null)
Returns meta-data for the given slot.
Definition: RevisionRecord.php:191
MediaWiki\Rest\Handler\PageSourceHandler\getPageContent
getPageContent(string $slotRole, RevisionRecord $revision)
Definition: PageSourceHandler.php:31
MediaWiki\Rest\Handler\PageSourceHandler\getLastModified
getLastModified()
Definition: PageSourceHandler.php:109
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:14
MediaWiki\Rest\Handler\LatestPageContentHandler
Definition: LatestPageContentHandler.php:16
MediaWiki\Rest\Response
Definition: Response.php:8
$title
$title
Definition: testCompression.php:38
Revision\RevisionRecord\getId
getId()
Get revision ID.
Definition: RevisionRecord.php:279
MediaWiki\Rest\Handler\LatestPageContentHandler\getLatestRevision
getLatestRevision()
Definition: LatestPageContentHandler.php:73
$content
$content
Definition: router.php:76
MediaWiki\Rest\Handler\LatestPageContentHandler\$latestRevision
RevisionRecord bool $latestRevision
Definition: LatestPageContentHandler.php:34
MediaWiki\Rest\Handler\LatestPageContentHandler\constructMetadata
constructMetadata(Title $titleObject, RevisionRecord $revision)
Definition: LatestPageContentHandler.php:89
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:37
MediaWiki\Rest\Handler\LatestPageContentHandler\isAccessible
isAccessible( $titleObject)
Definition: LatestPageContentHandler.php:85
MediaWiki\Rest\Handler\PageSourceHandler\getETag
getETag()
Returns an ETag representing a page's source.
Definition: PageSourceHandler.php:95
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:38
MediaWiki\Rest\Handler\PageSourceHandler\run
run(string $title)
Definition: PageSourceHandler.php:59
MediaWiki\Rest\Handler\LatestPageContentHandler\getTitle
getTitle()
Definition: LatestPageContentHandler.php:63
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:227
MediaWiki\Rest\Handler\PageSourceHandler\MAX_AGE_200
const MAX_AGE_200
Definition: PageSourceHandler.php:18
MediaWiki\Rest\Handler\LatestPageContentHandler\$titleObject
Title bool $titleObject
Definition: LatestPageContentHandler.php:37
MediaWiki\Rest\LocalizedHttpException
Definition: LocalizedHttpException.php:7
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39
MediaWiki\Rest\Handler\PageSourceHandler
Handler class for Core REST API Page Source endpoint.
Definition: PageSourceHandler.php:17