MediaWiki  master
PageHTMLHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
5 use Config;
6 use LogicException;
14 use TitleFormatter;
15 use Wikimedia\Assert\Assert;
17 
26 
28  private $htmlHelper;
29 
31  private $contentHelper;
32 
33  public function __construct(
35  RevisionLookup $revisionLookup,
36  TitleFormatter $titleFormatter,
37  ParserCacheFactory $parserCacheFactory,
38  GlobalIdGenerator $globalIdGenerator,
39  PageLookup $pageLookup
40  ) {
41  $this->contentHelper = new PageContentHelper(
42  $config,
43  $revisionLookup,
44  $titleFormatter,
45  $pageLookup
46  );
47  $this->htmlHelper = new ParsoidHTMLHelper(
48  $parserCacheFactory->getParserCache( 'parsoid' ),
49  $parserCacheFactory->getRevisionOutputCache( 'parsoid' ),
50  $globalIdGenerator
51  );
52  }
53 
54  protected function postValidationSetup() {
55  $this->contentHelper->init( $this->getAuthority(), $this->getValidatedParams() );
56 
57  $page = $this->contentHelper->getPage();
58  if ( $page ) {
59  $this->htmlHelper->init( $page );
60  }
61  }
62 
67  public function run(): Response {
68  $this->contentHelper->checkAccess();
69 
70  $page = $this->contentHelper->getPage();
71 
72  // The call to $this->contentHelper->getPage() should not return null if
73  // $this->contentHelper->checkAccess() did not throw.
74  Assert::invariant( $page !== null, 'Page should be known' );
75 
76  $outputMode = $this->getOutputMode();
77  switch ( $outputMode ) {
78  case 'html':
79  $parserOutput = $this->htmlHelper->getHtml();
80  $response = $this->getResponseFactory()->create();
81  // TODO: need to respect content-type returned by Parsoid.
82  $response->setHeader( 'Content-Type', 'text/html' );
83  $this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
84  $response->setBody( new StringStream( $parserOutput->getText() ) );
85  break;
86  case 'with_html':
87  $parserOutput = $this->htmlHelper->getHtml();
88  $body = $this->contentHelper->constructMetadata();
89  $body['html'] = $parserOutput->getText();
90  $response = $this->getResponseFactory()->createJson( $body );
91  $this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
92  break;
93  default:
94  throw new LogicException( "Unknown HTML type $outputMode" );
95  }
96 
97  return $response;
98  }
99 
106  protected function getETag(): ?string {
107  if ( !$this->contentHelper->isAccessible() ) {
108  return null;
109  }
110  return $this->htmlHelper->getETag();
111  }
112 
116  protected function getLastModified(): ?string {
117  if ( !$this->contentHelper->isAccessible() ) {
118  return null;
119  }
120  return $this->htmlHelper->getLastModified();
121  }
122 
123  private function getOutputMode(): string {
124  return $this->getConfig()['format'];
125  }
126 
127  public function needsWriteAccess(): bool {
128  return false;
129  }
130 
131  public function getParamSettings(): array {
132  return $this->contentHelper->getParamSettings();
133  }
134 }
MediaWiki\Rest\Handler\ParsoidHTMLHelper
Helper for getting output of a given wikitext page rendered by parsoid.
Definition: ParsoidHTMLHelper.php:49
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
Wikimedia\UUID\GlobalIdGenerator
Class for getting statistically unique IDs without a central coordinator.
Definition: GlobalIdGenerator.php:34
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:170
MediaWiki\Rest\Handler\PageHTMLHandler\$htmlHelper
ParsoidHTMLHelper $htmlHelper
Definition: PageHTMLHandler.php:28
MediaWiki\Rest\StringStream
A stream class which uses a string as the underlying storage.
Definition: StringStream.php:16
MediaWiki\Rest\Handler\PageHTMLHandler\getLastModified
getLastModified()
Definition: PageHTMLHandler.php:116
MediaWiki\Rest\Handler\PageHTMLHandler\postValidationSetup
postValidationSetup()
The handler can override this to do any necessary setup after validate() has been called.
Definition: PageHTMLHandler.php:54
Parser\ParserCacheFactory
Definition: ParserCacheFactory.php:40
MediaWiki\Revision\RevisionLookup
Service for looking up page revisions.
Definition: RevisionLookup.php:38
MediaWiki\Rest\Handler\PageHTMLHandler\getETag
getETag()
Returns an ETag representing a page's source.
Definition: PageHTMLHandler.php:106
Config
Interface for configuration instances.
Definition: Config.php:30
MediaWiki\Rest\Handler\PageHTMLHandler\run
run()
Definition: PageHTMLHandler.php:67
MediaWiki\Rest\Handler\PageHTMLHandler\__construct
__construct(Config $config, RevisionLookup $revisionLookup, TitleFormatter $titleFormatter, ParserCacheFactory $parserCacheFactory, GlobalIdGenerator $globalIdGenerator, PageLookup $pageLookup)
Definition: PageHTMLHandler.php:33
MediaWiki\Rest\Response
Definition: Response.php:8
MediaWiki\Rest\Handler\PageHTMLHandler\getOutputMode
getOutputMode()
Definition: PageHTMLHandler.php:123
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
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\PageHTMLHandler\$contentHelper
PageContentHelper $contentHelper
Definition: PageHTMLHandler.php:31
MediaWiki\Rest\Handler\PageHTMLHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: PageHTMLHandler.php:127
Parser\ParserCacheFactory\getRevisionOutputCache
getRevisionOutputCache(string $name)
Get a RevisionOutputCache instance by $name.
Definition: ParserCacheFactory.php:153
Parser\ParserCacheFactory\getParserCache
getParserCache(string $name)
Get a ParserCache instance by $name.
Definition: ParserCacheFactory.php:127
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:35
MediaWiki\Rest\Handler\getAuthority
getAuthority()
Get the current acting authority.
Definition: Handler.php:148
MediaWiki\Rest\Handler\PageHTMLHandler
Definition: PageHTMLHandler.php:25
MediaWiki\Rest\Handler\PageHTMLHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: PageHTMLHandler.php:131
MediaWiki\Rest\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:15