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;
12 use RequestContext;
13 use Title;
14 use TitleFactory;
15 use TitleFormatter;
16 use Wikimedia\Assert\Assert;
17 
24 
26  private $contentHelper;
27 
28  public function __construct(
30  PermissionManager $permissionManager,
31  RevisionLookup $revisionLookup,
32  TitleFormatter $titleFormatter,
33  TitleFactory $titleFactory
34  ) {
35  $this->contentHelper = new PageContentHelper(
36  $config,
37  $permissionManager,
38  $revisionLookup,
39  $titleFormatter,
40  $titleFactory
41  );
42  }
43 
44  protected function postValidationSetup() {
45  // TODO: inject user properly
46  $user = RequestContext::getMain()->getUser();
47  $this->contentHelper->init( $user, $this->getValidatedParams() );
48  }
49 
54  private function constructHtmlUrl( Title $title ): string {
55  return $this->getRouter()->getRouteUrl(
56  '/v1/page/{title}/html',
57  [ 'title' => $title->getPrefixedText() ]
58  );
59  }
60 
65  public function run(): Response {
66  $this->contentHelper->checkAccess();
67 
68  $titleObj = $this->contentHelper->getTitle();
69 
70  // The call to $this->contentHelper->getTitle() should not return null if
71  // $this->contentHelper->checkAccess() did not throw.
72  Assert::invariant( $titleObj !== null, 'Title should be known' );
73 
74  $outputMode = $this->getOutputMode();
75  switch ( $outputMode ) {
76  case 'bare':
77  $body = $this->contentHelper->constructMetadata();
78  $body['html_url'] = $this->constructHtmlUrl( $titleObj );
79  break;
80  case 'source':
81  $content = $this->contentHelper->getContent();
82  $body = $this->contentHelper->constructMetadata();
83  $body['source'] = $content->getText();
84  break;
85  default:
86  throw new LogicException( "Unknown HTML type $outputMode" );
87  }
88 
89  $response = $this->getResponseFactory()->createJson( $body );
90  $this->contentHelper->setCacheControl( $response );
91 
92  return $response;
93  }
94 
101  protected function getETag(): string {
102  return $this->contentHelper->getETag();
103  }
104 
108  protected function getLastModified(): ?string {
109  return $this->contentHelper->getLastModified();
110  }
111 
112  private function getOutputMode(): string {
113  return $this->getConfig()['format'];
114  }
115 
116  public function needsWriteAccess(): bool {
117  return false;
118  }
119 
120  public function getParamSettings(): array {
121  return $this->contentHelper->getParamSettings();
122  }
123 
127  protected function hasRepresentation() {
128  return $this->contentHelper->hasContent();
129  }
130 }
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:154
Title\getPrefixedText
getPrefixedText()
Get the prefixed title with spaces.
Definition: Title.php:1884
MediaWiki\Rest\Handler\PageSourceHandler\getLastModified
getLastModified()
Definition: PageSourceHandler.php:108
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
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\constructHtmlUrl
constructHtmlUrl(Title $title)
Definition: PageSourceHandler.php:54
MediaWiki\Rest\Handler\PageSourceHandler\$contentHelper
PageContentHelper $contentHelper
Definition: PageSourceHandler.php:26
MediaWiki\Rest\Response
Definition: Response.php:8
$title
$title
Definition: testCompression.php:38
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
$content
$content
Definition: router.php:76
MediaWiki\Rest\Handler\PageSourceHandler\hasRepresentation
hasRepresentation()
Definition: PageSourceHandler.php:127
MediaWiki\Rest\Handler\PageSourceHandler\__construct
__construct(Config $config, PermissionManager $permissionManager, RevisionLookup $revisionLookup, TitleFormatter $titleFormatter, TitleFactory $titleFactory)
Definition: PageSourceHandler.php:28
MediaWiki\Rest\Handler\PageContentHelper
Definition: PageContentHelper.php:26
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:454
MediaWiki\Rest\Handler\PageSourceHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: PageSourceHandler.php:116
Title
Represents a title within MediaWiki.
Definition: Title.php:46
MediaWiki\Rest\Handler\PageSourceHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: PageSourceHandler.php:120
MediaWiki\Rest\Handler\PageSourceHandler\getETag
getETag()
Returns an ETag representing a page's source.
Definition: PageSourceHandler.php:101
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:34
TitleFactory
Creates Title objects.
Definition: TitleFactory.php:33
MediaWiki\Rest\Handler\PageSourceHandler\run
run()
Definition: PageSourceHandler.php:65
MediaWiki\Rest\Handler\getRouter
getRouter()
Get the Router.
Definition: Handler.php:80
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:23
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:15
MediaWiki\Rest\Handler\PageSourceHandler\getOutputMode
getOutputMode()
Definition: PageSourceHandler.php:112