Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.29% |
25 / 28 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
| RevisionLintHandler | |
89.29% |
25 / 28 |
|
57.14% |
4 / 7 |
9.10 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| postValidationSetup | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| run | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
3 | |||
| needsWriteAccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getParamSettings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasRepresentation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getResponseBodySchemaFileName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Rest\Handler; |
| 4 | |
| 5 | use MediaWiki\Parser\Parsoid\LintErrorChecker; |
| 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 Wikimedia\Assert\Assert; |
| 12 | use Wikimedia\Message\MessageValue; |
| 13 | use Wikimedia\Parsoid\Core\ClientError; |
| 14 | use Wikimedia\Parsoid\Core\ResourceLimitExceededException; |
| 15 | |
| 16 | /** |
| 17 | * A handler that returns linter errors for main (text) content revisions |
| 18 | * |
| 19 | * @package MediaWiki\Rest\Handler |
| 20 | */ |
| 21 | class RevisionLintHandler extends SimpleHandler { |
| 22 | |
| 23 | private RevisionContentHelper $contentHelper; |
| 24 | private LintErrorChecker $lintErrorChecker; |
| 25 | |
| 26 | public function __construct( |
| 27 | PageRestHelperFactory $helperFactory, |
| 28 | LintErrorChecker $lintErrorChecker |
| 29 | ) { |
| 30 | $this->contentHelper = $helperFactory->newRevisionContentHelper(); |
| 31 | $this->lintErrorChecker = $lintErrorChecker; |
| 32 | } |
| 33 | |
| 34 | protected function postValidationSetup() { |
| 35 | $authority = $this->getAuthority(); |
| 36 | $this->contentHelper->init( $authority, $this->getValidatedParams() ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @return Response |
| 41 | * @throws LocalizedHttpException |
| 42 | */ |
| 43 | public function run(): Response { |
| 44 | $this->contentHelper->checkAccess(); |
| 45 | |
| 46 | $page = $this->contentHelper->getPage(); |
| 47 | $revisionRecord = $this->contentHelper->getTargetRevision(); |
| 48 | |
| 49 | // The page should be set if checkAccess() didn't throw |
| 50 | Assert::invariant( $page !== null, 'Page should be known' ); |
| 51 | // The revision should be set if checkAccess() didn't throw |
| 52 | Assert::invariant( $revisionRecord !== null, 'Revision should be known' ); |
| 53 | |
| 54 | // Get the content and make sure that it is text content |
| 55 | $content = $this->contentHelper->getContent(); |
| 56 | |
| 57 | try { |
| 58 | $lintErrors = $this->lintErrorChecker->check( $content->getText() ); |
| 59 | $response = $this->getResponseFactory()->createJson( $lintErrors ); |
| 60 | $this->contentHelper->setCacheControl( $response ); |
| 61 | } catch ( ClientError $e ) { |
| 62 | throw new LocalizedHttpException( |
| 63 | new MessageValue( "rest-lint-backend-error", [ $e->getMessage() ] ), |
| 64 | 400 |
| 65 | ); |
| 66 | } catch ( ResourceLimitExceededException $e ) { |
| 67 | throw new LocalizedHttpException( |
| 68 | new MessageValue( "rest-resource-limit-exceeded", [ $e->getMessage() ] ), |
| 69 | 413 |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | return $response; |
| 74 | } |
| 75 | |
| 76 | public function needsWriteAccess(): bool { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | public function getParamSettings(): array { |
| 81 | return $this->contentHelper->getParamSettings(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @inheritDoc |
| 86 | */ |
| 87 | protected function hasRepresentation() { |
| 88 | return $this->contentHelper->hasContent(); |
| 89 | } |
| 90 | |
| 91 | public function getResponseBodySchemaFileName( string $method ): ?string { |
| 92 | return __DIR__ . '/Schema/ContentLintErrors.json'; |
| 93 | } |
| 94 | } |