Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
87.50% |
28 / 32 |
|
72.73% |
8 / 11 |
CRAP | |
0.00% |
0 / 1 |
RevisionSourceHandler | |
87.50% |
28 / 32 |
|
72.73% |
8 / 11 |
14.38 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
postValidationSetup | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
constructHtmlUrl | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
run | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
4.00 | |||
getResponseBodySchema | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getETag | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLastModified | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOutputMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
needsWriteAccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getParamSettings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasRepresentation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Rest\Handler; |
4 | |
5 | use LogicException; |
6 | use MediaWiki\Rest\Handler\Helper\PageRestHelperFactory; |
7 | use MediaWiki\Rest\Handler\Helper\RevisionContentHelper; |
8 | use MediaWiki\Rest\LocalizedHttpException; |
9 | use MediaWiki\Rest\Response; |
10 | use MediaWiki\Rest\SimpleHandler; |
11 | use MediaWiki\Revision\RevisionRecord; |
12 | |
13 | /** |
14 | * A handler that returns page source and metadata for the following routes: |
15 | * - /revision/{revision} |
16 | * - /revision/{revision}/bare |
17 | */ |
18 | class RevisionSourceHandler extends SimpleHandler { |
19 | |
20 | /** @var RevisionContentHelper */ |
21 | private $contentHelper; |
22 | |
23 | /** |
24 | * @param PageRestHelperFactory $helperFactory |
25 | */ |
26 | public function __construct( PageRestHelperFactory $helperFactory ) { |
27 | $this->contentHelper = $helperFactory->newRevisionContentHelper(); |
28 | } |
29 | |
30 | protected function postValidationSetup() { |
31 | $this->contentHelper->init( $this->getAuthority(), $this->getValidatedParams() ); |
32 | } |
33 | |
34 | /** |
35 | * @param RevisionRecord $rev |
36 | * @return string |
37 | */ |
38 | private function constructHtmlUrl( RevisionRecord $rev ): string { |
39 | return $this->getRouter()->getRouteUrl( |
40 | '/v1/revision/{id}/html', |
41 | [ 'id' => $rev->getId() ] |
42 | ); |
43 | } |
44 | |
45 | /** |
46 | * @return Response |
47 | * @throws LocalizedHttpException |
48 | */ |
49 | public function run() { |
50 | $this->contentHelper->checkAccess(); |
51 | |
52 | $outputMode = $this->getOutputMode(); |
53 | switch ( $outputMode ) { |
54 | case 'bare': |
55 | $revisionRecord = $this->contentHelper->getTargetRevision(); |
56 | $body = $this->contentHelper->constructMetadata(); |
57 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable revisionRecord is set when used |
58 | $body['html_url'] = $this->constructHtmlUrl( $revisionRecord ); |
59 | $response = $this->getResponseFactory()->createJson( $body ); |
60 | $this->contentHelper->setCacheControl( $response ); |
61 | break; |
62 | case 'source': |
63 | $content = $this->contentHelper->getContent(); |
64 | $body = $this->contentHelper->constructMetadata(); |
65 | $body['source'] = $content->getText(); |
66 | break; |
67 | default: |
68 | throw new LogicException( "Unknown output mode $outputMode" ); |
69 | } |
70 | |
71 | $response = $this->getResponseFactory()->createJson( $body ); |
72 | $this->contentHelper->setCacheControl( $response ); |
73 | |
74 | return $response; |
75 | } |
76 | |
77 | protected function getResponseBodySchema(): array { |
78 | $schema = $this->contentHelper->getResponseBodySchema(); |
79 | // TODO: add fields based on the output mode |
80 | return $schema; |
81 | } |
82 | |
83 | /** |
84 | * @return string|null |
85 | */ |
86 | protected function getETag(): ?string { |
87 | return $this->contentHelper->getETag(); |
88 | } |
89 | |
90 | /** |
91 | * @return string|null |
92 | */ |
93 | protected function getLastModified(): ?string { |
94 | return $this->contentHelper->getLastModified(); |
95 | } |
96 | |
97 | private function getOutputMode(): string { |
98 | return $this->getConfig()['format']; |
99 | } |
100 | |
101 | public function needsWriteAccess(): bool { |
102 | return false; |
103 | } |
104 | |
105 | public function getParamSettings(): array { |
106 | return $this->contentHelper->getParamSettings(); |
107 | } |
108 | |
109 | /** |
110 | * @return bool |
111 | */ |
112 | protected function hasRepresentation() { |
113 | return $this->contentHelper->hasContent(); |
114 | } |
115 | } |