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;
15 use RequestContext;
16 use TitleFactory;
17 use TitleFormatter;
18 use Wikimedia\Assert\Assert;
20 
29 
31  private $htmlHelper;
32 
34  private $contentHelper;
35 
36  public function __construct(
38  PermissionManager $permissionManager,
39  RevisionLookup $revisionLookup,
40  TitleFormatter $titleFormatter,
41  TitleFactory $titleFactory,
42  ParserCacheFactory $parserCacheFactory,
43  WikiPageFactory $wikiPageFactory,
44  GlobalIdGenerator $globalIdGenerator
45  ) {
46  $this->contentHelper = new PageContentHelper(
47  $config,
48  $permissionManager,
49  $revisionLookup,
50  $titleFormatter,
51  $titleFactory
52  );
53  $this->htmlHelper = new ParsoidHTMLHelper(
54  $parserCacheFactory->getParserCache( 'parsoid' ),
55  $parserCacheFactory->getRevisionOutputCache( 'parsoid' ),
56  $wikiPageFactory,
57  $globalIdGenerator
58  );
59  }
60 
61  protected function postValidationSetup() {
62  // TODO: inject user properly
63  $user = RequestContext::getMain()->getUser();
64  $this->contentHelper->init( $user, $this->getValidatedParams() );
65 
66  $title = $this->contentHelper->getTitle();
67  if ( $title ) {
68  $this->htmlHelper->init( $title );
69  }
70  }
71 
76  public function run(): Response {
77  $this->contentHelper->checkAccess();
78 
79  $titleObj = $this->contentHelper->getTitle();
80 
81  // The call to $this->contentHelper->getTitle() should not return null if
82  // $this->contentHelper->checkAccess() did not throw.
83  Assert::invariant( $titleObj !== null, 'Title should be known' );
84 
85  $outputMode = $this->getOutputMode();
86  switch ( $outputMode ) {
87  case 'html':
88  $parserOutput = $this->htmlHelper->getHtml();
89  $response = $this->getResponseFactory()->create();
90  // TODO: need to respect content-type returned by Parsoid.
91  $response->setHeader( 'Content-Type', 'text/html' );
92  $this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
93  $response->setBody( new StringStream( $parserOutput->getText() ) );
94  break;
95  case 'with_html':
96  $parserOutput = $this->htmlHelper->getHtml();
97  $body = $this->contentHelper->constructMetadata();
98  $body['html'] = $parserOutput->getText();
99  $response = $this->getResponseFactory()->createJson( $body );
100  $this->contentHelper->setCacheControl( $response, $parserOutput->getCacheExpiry() );
101  break;
102  default:
103  throw new LogicException( "Unknown HTML type $outputMode" );
104  }
105 
106  return $response;
107  }
108 
115  protected function getETag(): ?string {
116  if ( !$this->contentHelper->isAccessible() ) {
117  return null;
118  }
119  return $this->htmlHelper->getETag();
120  }
121 
125  protected function getLastModified(): ?string {
126  if ( !$this->contentHelper->isAccessible() ) {
127  return null;
128  }
129  return $this->htmlHelper->getLastModified();
130  }
131 
132  private function getOutputMode(): string {
133  return $this->getConfig()['format'];
134  }
135 
136  public function needsWriteAccess(): bool {
137  return false;
138  }
139 
140  public function getParamSettings(): array {
141  return $this->contentHelper->getParamSettings();
142  }
143 }
MediaWiki\Rest\Handler\ParsoidHTMLHelper
Helper for getting output of a given wikitext page rendered by parsoid.
Definition: ParsoidHTMLHelper.php:48
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
Wikimedia\UUID\GlobalIdGenerator
Class for getting statistically unique IDs without a central coordinator.
Definition: GlobalIdGenerator.php:36
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:154
MediaWiki\Rest\Handler\PageHTMLHandler\$htmlHelper
ParsoidHTMLHelper $htmlHelper
Definition: PageHTMLHandler.php:31
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:125
MediaWiki\Rest\Handler\PageHTMLHandler\postValidationSetup
postValidationSetup()
The handler can override this to do any necessary setup after validate() has been called.
Definition: PageHTMLHandler.php:61
Parser\ParserCacheFactory
Definition: ParserCacheFactory.php:38
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:115
Config
Interface for configuration instances.
Definition: Config.php:30
MediaWiki\Rest\Handler\PageHTMLHandler\run
run()
Definition: PageHTMLHandler.php:76
MediaWiki\Rest\Handler\PageHTMLHandler\__construct
__construct(Config $config, PermissionManager $permissionManager, RevisionLookup $revisionLookup, TitleFormatter $titleFormatter, TitleFactory $titleFactory, ParserCacheFactory $parserCacheFactory, WikiPageFactory $wikiPageFactory, GlobalIdGenerator $globalIdGenerator)
Definition: PageHTMLHandler.php:36
Page\WikiPageFactory
Definition: WikiPageFactory.php:19
MediaWiki\Rest\Response
Definition: Response.php:8
$title
$title
Definition: testCompression.php:38
MediaWiki\Rest\Handler\PageHTMLHandler\getOutputMode
getOutputMode()
Definition: PageHTMLHandler.php:132
RequestContext
Group all the pieces relevant to the context of a request into one instance @newable.
Definition: RequestContext.php:39
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:265
MediaWiki\Rest\Handler\getConfig
getConfig()
Get the configuration array for the current route.
Definition: Handler.php:143
MediaWiki\Rest\Handler\$config
array $config
Definition: Handler.php:34
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:51
MediaWiki\Rest\Handler\PageContentHelper
Definition: PageContentHelper.php:26
MediaWiki\Rest\Handler\PageHTMLHandler\$contentHelper
PageContentHelper $contentHelper
Definition: PageHTMLHandler.php:34
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:454
MediaWiki\Rest\Handler\PageHTMLHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: PageHTMLHandler.php:136
Parser\ParserCacheFactory\getRevisionOutputCache
getRevisionOutputCache(string $name)
Get a RevisionOutputCache instance by $name.
Definition: ParserCacheFactory.php:137
Parser\ParserCacheFactory\getParserCache
getParserCache(string $name)
Get a ParserCache instance by $name.
Definition: ParserCacheFactory.php:113
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:34
TitleFactory
Creates Title objects.
Definition: TitleFactory.php:33
MediaWiki\Rest\Handler\PageHTMLHandler
Definition: PageHTMLHandler.php:28
MediaWiki\Rest\Handler\PageHTMLHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: PageHTMLHandler.php:140
MediaWiki\Rest\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:15