MediaWiki  master
PageSourceHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
5 use Config;
6 use LogicException;
13 use TitleFormatter;
14 use Wikimedia\Assert\Assert;
15 
22 
24  private $titleFormatter;
25 
27  private $contentHelper;
28 
29  public function __construct(
31  RevisionLookup $revisionLookup,
33  PageLookup $pageLookup
34  ) {
35  $this->titleFormatter = $titleFormatter;
36  $this->contentHelper = new PageContentHelper(
37  $config,
38  $revisionLookup,
40  $pageLookup
41  );
42  }
43 
44  protected function postValidationSetup() {
45  $this->contentHelper->init( $this->getAuthority(), $this->getValidatedParams() );
46  }
47 
52  private function constructHtmlUrl( PageReference $page ): string {
53  return $this->getRouter()->getRouteUrl(
54  '/v1/page/{title}/html',
55  [ 'title' => $this->titleFormatter->getPrefixedText( $page ) ]
56  );
57  }
58 
63  public function run(): Response {
64  $this->contentHelper->checkAccess();
65 
66  $page = $this->contentHelper->getPage();
67 
68  // The call to $this->contentHelper->getPage() should not return null if
69  // $this->contentHelper->checkAccess() did not throw.
70  Assert::invariant( $page !== null, 'Page should be known' );
71 
72  $outputMode = $this->getOutputMode();
73  switch ( $outputMode ) {
74  case 'bare':
75  $body = $this->contentHelper->constructMetadata();
76  $body['html_url'] = $this->constructHtmlUrl( $page );
77  break;
78  case 'source':
79  $content = $this->contentHelper->getContent();
80  $body = $this->contentHelper->constructMetadata();
81  $body['source'] = $content->getText();
82  break;
83  default:
84  throw new LogicException( "Unknown HTML type $outputMode" );
85  }
86 
87  $response = $this->getResponseFactory()->createJson( $body );
88  $this->contentHelper->setCacheControl( $response );
89 
90  return $response;
91  }
92 
99  protected function getETag(): string {
100  return $this->contentHelper->getETag();
101  }
102 
106  protected function getLastModified(): ?string {
107  return $this->contentHelper->getLastModified();
108  }
109 
110  private function getOutputMode(): string {
111  return $this->getConfig()['format'];
112  }
113 
114  public function needsWriteAccess(): bool {
115  return false;
116  }
117 
118  public function getParamSettings(): array {
119  return $this->contentHelper->getParamSettings();
120  }
121 
125  protected function hasRepresentation() {
126  return $this->contentHelper->hasContent();
127  }
128 }
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
MediaWiki\Rest\Handler\PageSourceHandler\__construct
__construct(Config $config, RevisionLookup $revisionLookup, TitleFormatter $titleFormatter, PageLookup $pageLookup)
Definition: PageSourceHandler.php:29
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:170
MediaWiki\Rest\Handler\PageSourceHandler\constructHtmlUrl
constructHtmlUrl(PageReference $page)
Definition: PageSourceHandler.php:52
MediaWiki\Rest\Handler\PageSourceHandler\getLastModified
getLastModified()
Definition: PageSourceHandler.php:106
MediaWiki\Rest\Handler\PageSourceHandler\postValidationSetup
postValidationSetup()
The handler can override this to do any necessary setup after validate() has been called.
Definition: PageSourceHandler.php:44
Page\PageReference
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Definition: PageReference.php:49
MediaWiki\Revision\RevisionLookup
Service for looking up page revisions.
Definition: RevisionLookup.php:38
Config
Interface for configuration instances.
Definition: Config.php:30
MediaWiki\Rest\Handler\PageSourceHandler\$titleFormatter
TitleFormatter $titleFormatter
Definition: PageSourceHandler.php:24
MediaWiki\Rest\Handler\PageSourceHandler\$contentHelper
PageContentHelper $contentHelper
Definition: PageSourceHandler.php:27
MediaWiki\Rest\Response
Definition: Response.php:8
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:282
MediaWiki\Rest\Handler\getConfig
getConfig()
Get the configuration array for the current route.
Definition: Handler.php:159
MediaWiki\Rest\Handler\$config
array $config
Definition: Handler.php:38
$content
$content
Definition: router.php:76
MediaWiki\Rest\Handler\PageSourceHandler\hasRepresentation
hasRepresentation()
Definition: PageSourceHandler.php:125
Page\PageLookup
Service interface for looking up infermation about wiki pages.
Definition: PageLookup.php:14
MediaWiki\Rest\Handler\PageContentHelper
Definition: PageContentHelper.php:25
MediaWiki\Rest\Handler\PageSourceHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: PageSourceHandler.php:114
MediaWiki\Rest\Handler\PageSourceHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: PageSourceHandler.php:118
MediaWiki\Rest\Handler\PageSourceHandler\getETag
getETag()
Returns an ETag representing a page's source.
Definition: PageSourceHandler.php:99
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:35
MediaWiki\Rest\Handler\PageSourceHandler\run
run()
Definition: PageSourceHandler.php:63
MediaWiki\Rest\Handler\getRouter
getRouter()
Get the Router.
Definition: Handler.php:86
MediaWiki\Rest\Handler\getAuthority
getAuthority()
Get the current acting authority.
Definition: Handler.php:148
MediaWiki\Rest\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10
MediaWiki\Rest\Handler\PageSourceHandler
Handler class for Core REST API Page Source endpoint with the following routes:
Definition: PageSourceHandler.php:21
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:15
MediaWiki\Rest\Handler\PageSourceHandler\getOutputMode
getOutputMode()
Definition: PageSourceHandler.php:110