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