MediaWiki  master
RevisionHTMLHandler.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 
27 
29  private $htmlHelper;
30 
32  private $contentHelper;
33 
34  public function __construct(
36  RevisionLookup $revisionLookup,
37  TitleFormatter $titleFormatter,
38  ParserCacheFactory $parserCacheFactory,
39  GlobalIdGenerator $globalIdGenerator,
40  PageLookup $pageLookup
41  ) {
42  $this->contentHelper = new RevisionContentHelper(
43  $config,
44  $revisionLookup,
45  $titleFormatter,
46  $pageLookup
47  );
48  $this->htmlHelper = new ParsoidHTMLHelper(
49  $parserCacheFactory->getParserCache( 'parsoid' ),
50  $parserCacheFactory->getRevisionOutputCache( 'parsoid' ),
51  $globalIdGenerator
52  );
53  }
54 
55  protected function postValidationSetup() {
56  $this->contentHelper->init( $this->getAuthority(), $this->getValidatedParams() );
57 
58  $page = $this->contentHelper->getPage();
59  $revision = $this->contentHelper->getTargetRevision();
60 
61  if ( $page && $revision ) {
62  $this->htmlHelper->init( $page, $revision );
63  }
64  }
65 
70  public function run(): Response {
71  $this->contentHelper->checkAccess();
72 
73  $page = $this->contentHelper->getPage();
74  $revisionRecord = $this->contentHelper->getTargetRevision();
75 
76  // The call to $this->contentHelper->getPage() should not return null if
77  // $this->contentHelper->checkAccess() did not throw.
78  Assert::invariant( $page !== null, 'Page should be known' );
79 
80  // The call to $this->contentHelper->getTargetRevision() should not return null if
81  // $this->contentHelper->checkAccess() did not throw.
82  Assert::invariant( $revisionRecord !== null, 'Revision should be known' );
83 
84  $outputMode = $this->getOutputMode();
85  switch ( $outputMode ) {
86  case 'html':
87  $parserOutput = $this->htmlHelper->getHtml();
88  $response = $this->getResponseFactory()->create();
89  // TODO: need to respect content-type returned by Parsoid.
90  $response->setHeader( 'Content-Type', 'text/html' );
91  $this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
92  $response->setBody( new StringStream( $parserOutput->getText() ) );
93  break;
94  case 'with_html':
95  $parserOutput = $this->htmlHelper->getHtml();
96  $body = $this->contentHelper->constructMetadata();
97  $body['html'] = $parserOutput->getText();
98  $response = $this->getResponseFactory()->createJson( $body );
99  $this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
100  break;
101  default:
102  throw new LogicException( "Unknown HTML type $outputMode" );
103  }
104 
105  return $response;
106  }
107 
114  protected function getETag(): ?string {
115  if ( !$this->contentHelper->isAccessible() ) {
116  return null;
117  }
118 
119  return $this->htmlHelper->getETag();
120  }
121 
125  protected function getLastModified(): ?string {
126  if ( !$this->contentHelper->isAccessible() ) {
127  return null;
128  }
129 
130  return $this->htmlHelper->getLastModified();
131  }
132 
133  private function getOutputMode(): string {
134  return $this->getConfig()['format'];
135  }
136 
137  public function needsWriteAccess(): bool {
138  return false;
139  }
140 
141  public function getParamSettings(): array {
142  return $this->contentHelper->getParamSettings();
143  }
144 
148  protected function hasRepresentation() {
149  return $this->contentHelper->hasContent();
150  }
151 }
MediaWiki\Rest\Handler\RevisionHTMLHandler\getETag
getETag()
Returns an ETag representing a page's source.
Definition: RevisionHTMLHandler.php:114
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
MediaWiki\Rest\Handler\RevisionHTMLHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: RevisionHTMLHandler.php:137
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\RevisionContentHelper
Definition: RevisionContentHelper.php:17
MediaWiki\Rest\StringStream
A stream class which uses a string as the underlying storage.
Definition: StringStream.php:16
MediaWiki\Rest\Handler\RevisionHTMLHandler\getLastModified
getLastModified()
Definition: RevisionHTMLHandler.php:125
MediaWiki\Rest\Handler\RevisionHTMLHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: RevisionHTMLHandler.php:141
MediaWiki\Rest\Handler\RevisionHTMLHandler\postValidationSetup
postValidationSetup()
The handler can override this to do any necessary setup after validate() has been called.
Definition: RevisionHTMLHandler.php:55
Parser\ParserCacheFactory
Definition: ParserCacheFactory.php:40
MediaWiki\Revision\RevisionLookup
Service for looking up page revisions.
Definition: RevisionLookup.php:38
MediaWiki\Rest\Handler\RevisionHTMLHandler\run
run()
Definition: RevisionHTMLHandler.php:70
MediaWiki\Rest\Handler\RevisionHTMLHandler\$htmlHelper
ParsoidHTMLHelper $htmlHelper
Definition: RevisionHTMLHandler.php:29
Config
Interface for configuration instances.
Definition: Config.php:30
MediaWiki\Rest\Handler\RevisionHTMLHandler\$contentHelper
RevisionContentHelper $contentHelper
Definition: RevisionHTMLHandler.php:32
MediaWiki\Rest\Response
Definition: Response.php:8
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:282
MediaWiki\Rest\Handler\RevisionHTMLHandler\__construct
__construct(Config $config, RevisionLookup $revisionLookup, TitleFormatter $titleFormatter, ParserCacheFactory $parserCacheFactory, GlobalIdGenerator $globalIdGenerator, PageLookup $pageLookup)
Definition: RevisionHTMLHandler.php:34
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
MediaWiki\Rest\Handler\RevisionHTMLHandler\getOutputMode
getOutputMode()
Definition: RevisionHTMLHandler.php:133
Page\PageLookup
Service interface for looking up infermation about wiki pages.
Definition: PageLookup.php:14
Parser\ParserCacheFactory\getRevisionOutputCache
getRevisionOutputCache(string $name)
Get a RevisionOutputCache instance by $name.
Definition: ParserCacheFactory.php:153
MediaWiki\Rest\Handler\RevisionHTMLHandler
Definition: RevisionHTMLHandler.php:26
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\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:15
MediaWiki\Rest\Handler\RevisionHTMLHandler\hasRepresentation
hasRepresentation()
Definition: RevisionHTMLHandler.php:148