Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
FullViewSchemaDataLookup
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFullViewSchemaData
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getFullViewSchemaDataForTitle
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare( strict_types = 1 );
4
5namespace EntitySchema\DataAccess;
6
7use EntitySchema\Domain\Model\EntitySchemaId;
8use EntitySchema\MediaWiki\Content\EntitySchemaContent;
9use EntitySchema\Services\Converter\EntitySchemaConverter;
10use EntitySchema\Services\Converter\FullViewEntitySchemaData;
11use MediaWiki\Page\PageIdentity;
12use MediaWiki\Page\WikiPageFactory;
13use MediaWiki\Title\TitleFactory;
14
15/**
16 * Lookup for loading the full-view data of an EntitySchema.
17 * Mainly for usage in other, more specific lookups.
18 *
19 * @license GPL-2.0-or-later
20 */
21class FullViewSchemaDataLookup {
22
23    private TitleFactory $titleFactory;
24    private WikiPageFactory $wikiPageFactory;
25
26    public function __construct(
27        TitleFactory $titleFactory,
28        WikiPageFactory $wikiPageFactory
29    ) {
30        $this->titleFactory = $titleFactory;
31        $this->wikiPageFactory = $wikiPageFactory;
32    }
33
34    /**
35     * Load the full-view EntitySchema data for the given ID.
36     * Returns null if the EntitySchema does not exist.
37     */
38    public function getFullViewSchemaData( EntitySchemaId $id ): ?FullViewEntitySchemaData {
39        $title = $this->titleFactory->makeTitleSafe( NS_ENTITYSCHEMA_JSON, $id->getId() );
40        if ( $title === null ) {
41            return null;
42        }
43
44        return $this->getFullViewSchemaDataForTitle( $title );
45    }
46
47    /**
48     * Load the full-view EntitySchema data for the given page identity,
49     * which must be in the EntitySchema namespace.
50     * (Note that $title can also be a WikiPage, which implements PageIdentity.)
51     * Returns null if the EntitySchema does not exist.
52     */
53    public function getFullViewSchemaDataForTitle( PageIdentity $title ): ?FullViewEntitySchemaData {
54        $wikiPage = $this->wikiPageFactory->newFromTitle( $title );
55        $content = $wikiPage->getContent();
56        if ( !( $content instanceof EntitySchemaContent ) ) {
57            return null;
58        }
59
60        $schema = $content->getText();
61
62        $converter = new EntitySchemaConverter();
63        return $converter->getFullViewSchemaData( $schema );
64    }
65
66}