Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
81.08% |
30 / 37 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
EntitySchemaText | |
81.08% |
30 / 37 |
|
50.00% |
3 / 6 |
10.68 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
80.00% |
12 / 15 |
|
0.00% |
0 / 1 |
3.07 | |||
getDescription | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
sendContentSchemaText | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
getIdFromSubpage | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
4.12 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace EntitySchema\MediaWiki\Specials; |
6 | |
7 | use EntitySchema\Domain\Model\EntitySchemaId; |
8 | use EntitySchema\MediaWiki\Content\EntitySchemaContent; |
9 | use EntitySchema\Services\Converter\EntitySchemaConverter; |
10 | use InvalidArgumentException; |
11 | use MediaWiki\Exception\HttpError; |
12 | use MediaWiki\MediaWikiServices; |
13 | use MediaWiki\Message\Message; |
14 | use MediaWiki\SpecialPage\SpecialPage; |
15 | use MediaWiki\Title\Title; |
16 | |
17 | /** |
18 | * @license GPL-2.0-or-later |
19 | */ |
20 | class EntitySchemaText extends SpecialPage { |
21 | |
22 | public function __construct() { |
23 | parent::__construct( |
24 | 'EntitySchemaText', |
25 | 'read' |
26 | ); |
27 | } |
28 | |
29 | /** @inheritDoc */ |
30 | public function execute( $subPage ): void { |
31 | parent::execute( $subPage ); |
32 | $entitySchemaId = $this->getIdFromSubpage( $subPage ); |
33 | if ( !$entitySchemaId ) { |
34 | $this->getOutput()->addWikiMsg( 'entityschema-schematext-text' ); |
35 | $this->getOutput()->returnToMain(); |
36 | return; |
37 | } |
38 | $title = Title::makeTitle( NS_ENTITYSCHEMA_JSON, $entitySchemaId->getId() ); |
39 | |
40 | if ( !$title->exists() ) { |
41 | throw new HttpError( 404, $this->getOutput()->msg( |
42 | 'entityschema-schematext-missing', $subPage |
43 | ) ); |
44 | } |
45 | |
46 | $this->sendContentSchemaText( |
47 | // @phan-suppress-next-line PhanTypeMismatchArgumentSuperType |
48 | MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title )->getContent(), |
49 | $entitySchemaId |
50 | ); |
51 | } |
52 | |
53 | public function getDescription(): Message { |
54 | return $this->msg( 'special-schematext' ); |
55 | } |
56 | |
57 | protected function getGroupName(): string { |
58 | return 'wikibase'; |
59 | } |
60 | |
61 | private function sendContentSchemaText( EntitySchemaContent $schemaContent, EntitySchemaId $id ): void { |
62 | $converter = new EntitySchemaConverter(); |
63 | $schemaText = $converter->getSchemaText( $schemaContent->getText() ); |
64 | $out = $this->getOutput(); |
65 | $out->disable(); |
66 | $webResponse = $out->getRequest()->response(); |
67 | $webResponse->header( 'Content-Type: text/shex; charset=UTF-8' ); |
68 | $webResponse->header( 'Content-Disposition: attachment; filename="' . $id->getId() . '.shex"' ); |
69 | // The data here is always public, so allow anyone to access it (similar to Special:EntityData) |
70 | $webResponse->header( 'Access-Control-Allow-Origin: *' ); |
71 | |
72 | ob_clean(); // remove anything that might already be in the output buffer. |
73 | echo $schemaText; |
74 | } |
75 | |
76 | private function getIdFromSubpage( ?string $subPage ): ?EntitySchemaId { |
77 | if ( !$subPage ) { |
78 | return null; |
79 | } |
80 | try { |
81 | $entitySchemaId = new EntitySchemaId( $subPage ); |
82 | } catch ( InvalidArgumentException $e ) { |
83 | return null; |
84 | } |
85 | return $entitySchemaId; |
86 | } |
87 | |
88 | } |