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 HttpError; |
11 | use InvalidArgumentException; |
12 | use MediaWiki\MediaWikiServices; |
13 | use MediaWiki\SpecialPage\SpecialPage; |
14 | use MediaWiki\Title\Title; |
15 | use Message; |
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 | public function execute( $subPage ): void { |
30 | parent::execute( $subPage ); |
31 | $entitySchemaId = $this->getIdFromSubpage( $subPage ); |
32 | if ( !$entitySchemaId ) { |
33 | $this->getOutput()->addWikiMsg( 'entityschema-schematext-text' ); |
34 | $this->getOutput()->returnToMain(); |
35 | return; |
36 | } |
37 | $title = Title::makeTitle( NS_ENTITYSCHEMA_JSON, $entitySchemaId->getId() ); |
38 | |
39 | if ( !$title->exists() ) { |
40 | throw new HttpError( 404, $this->getOutput()->msg( |
41 | 'entityschema-schematext-missing', $subPage |
42 | ) ); |
43 | } |
44 | |
45 | $this->sendContentSchemaText( |
46 | // @phan-suppress-next-line PhanTypeMismatchArgumentSuperType |
47 | MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title )->getContent(), |
48 | $entitySchemaId |
49 | ); |
50 | } |
51 | |
52 | public function getDescription(): Message { |
53 | return $this->msg( 'special-schematext' ); |
54 | } |
55 | |
56 | protected function getGroupName(): string { |
57 | return 'wikibase'; |
58 | } |
59 | |
60 | private function sendContentSchemaText( EntitySchemaContent $schemaContent, EntitySchemaId $id ): void { |
61 | $converter = new EntitySchemaConverter(); |
62 | $schemaText = $converter->getSchemaText( $schemaContent->getText() ); |
63 | $out = $this->getOutput(); |
64 | $out->disable(); |
65 | $webResponse = $out->getRequest()->response(); |
66 | $webResponse->header( 'Content-Type: text/shex; charset=UTF-8' ); |
67 | $webResponse->header( 'Content-Disposition: attachment; filename="' . $id->getId() . '.shex"' ); |
68 | // The data here is always public, so allow anyone to access it (similar to Special:EntityData) |
69 | $webResponse->header( 'Access-Control-Allow-Origin: *' ); |
70 | |
71 | ob_clean(); // remove anything that might already be in the output buffer. |
72 | echo $schemaText; |
73 | } |
74 | |
75 | private function getIdFromSubpage( ?string $subPage ): ?EntitySchemaId { |
76 | if ( !$subPage ) { |
77 | return null; |
78 | } |
79 | try { |
80 | $entitySchemaId = new EntitySchemaId( $subPage ); |
81 | } catch ( InvalidArgumentException $e ) { |
82 | return null; |
83 | } |
84 | return $entitySchemaId; |
85 | } |
86 | |
87 | } |