MediaWiki  master
RevisionSourceHandler.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 
21 
23  private $contentHelper;
24 
31  public function __construct(
33  RevisionLookup $revisionLookup,
34  TitleFormatter $titleFormatter,
35  PageLookup $pageLookup
36  ) {
37  $this->contentHelper = new RevisionContentHelper(
38  $config,
39  $revisionLookup,
40  $titleFormatter,
41  $pageLookup
42  );
43  }
44 
45  protected function postValidationSetup() {
46  $this->contentHelper->init( $this->getAuthority(), $this->getValidatedParams() );
47  }
48 
53  private function constructHtmlUrl( RevisionRecord $rev ): string {
54  return $this->getRouter()->getRouteUrl(
55  '/coredev/v0/revision/{id}/html',
56  [ 'id' => $rev->getId() ]
57  );
58  }
59 
64  public function run() {
65  $this->contentHelper->checkAccess();
66 
67  $outputMode = $this->getOutputMode();
68  switch ( $outputMode ) {
69  case 'bare':
70  $revisionRecord = $this->contentHelper->getTargetRevision();
71  $body = $this->contentHelper->constructMetadata();
72  $body['html_url'] = $this->constructHtmlUrl( $revisionRecord );
73  $response = $this->getResponseFactory()->createJson( $body );
74  $this->contentHelper->setCacheControl( $response );
75  break;
76  case 'source':
77  $content = $this->contentHelper->getContent();
78  $body = $this->contentHelper->constructMetadata();
79  $body['source'] = $content->getText();
80  break;
81  default:
82  throw new LogicException( "Unknown output mode $outputMode" );
83  }
84 
85  $response = $this->getResponseFactory()->createJson( $body );
86  $this->contentHelper->setCacheControl( $response );
87 
88  return $response;
89  }
90 
94  protected function getETag(): string {
95  return $this->contentHelper->getETag();
96  }
97 
101  protected function getLastModified(): ?string {
102  return $this->contentHelper->getLastModified();
103  }
104 
105  private function getOutputMode(): string {
106  return $this->getConfig()['format'];
107  }
108 
109  public function needsWriteAccess(): bool {
110  return false;
111  }
112 
113  public function getParamSettings(): array {
114  return $this->contentHelper->getParamSettings();
115  }
116 
120  protected function hasRepresentation() {
121  return $this->contentHelper->hasContent();
122  }
123 }
MediaWiki\Rest\Handler\RevisionSourceHandler\getETag
getETag()
Definition: RevisionSourceHandler.php:94
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
MediaWiki\Rest\Handler\RevisionSourceHandler\postValidationSetup
postValidationSetup()
The handler can override this to do any necessary setup after validate() has been called.
Definition: RevisionSourceHandler.php:45
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:170
MediaWiki\Rest\Handler\RevisionSourceHandler\getOutputMode
getOutputMode()
Definition: RevisionSourceHandler.php:105
MediaWiki\Rest\Handler\RevisionContentHelper
Definition: RevisionContentHelper.php:17
MediaWiki\Rest\Handler\RevisionSourceHandler\$contentHelper
RevisionContentHelper $contentHelper
Definition: RevisionSourceHandler.php:23
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\RevisionSourceHandler\__construct
__construct(Config $config, RevisionLookup $revisionLookup, TitleFormatter $titleFormatter, PageLookup $pageLookup)
Definition: RevisionSourceHandler.php:31
MediaWiki\Rest\Handler\RevisionSourceHandler\run
run()
Definition: RevisionSourceHandler.php:64
MediaWiki\Rest\Handler\RevisionSourceHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: RevisionSourceHandler.php:109
MediaWiki\Rest\Response
Definition: Response.php:8
MediaWiki\Rest\Handler\RevisionSourceHandler
A handler that returns page source and metadata for the following routes:
Definition: RevisionSourceHandler.php:20
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
Page\PageLookup
Service interface for looking up infermation about wiki pages.
Definition: PageLookup.php:14
MediaWiki\Rest\Handler\RevisionSourceHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: RevisionSourceHandler.php:113
MediaWiki\Rest\Handler\RevisionSourceHandler\getLastModified
getLastModified()
Definition: RevisionSourceHandler.php:101
MediaWiki\Revision\RevisionRecord\getId
getId( $wikiId=self::LOCAL)
Get revision ID.
Definition: RevisionRecord.php:279
MediaWiki\Rest\Handler\RevisionSourceHandler\constructHtmlUrl
constructHtmlUrl(RevisionRecord $rev)
Definition: RevisionSourceHandler.php:53
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:35
MediaWiki\Rest\Handler\getRouter
getRouter()
Get the Router.
Definition: Handler.php:86
MediaWiki\Rest\Handler\RevisionSourceHandler\hasRepresentation
hasRepresentation()
Definition: RevisionSourceHandler.php:120
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