Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WikibaseLexemeAbstractEntityLibrary
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 getUsageAccumulator
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getEntityIdParser
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 addAllUsage
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getParserOutput
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Wikibase\Lexeme\MediaWiki\Scribunto;
4
5use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LibraryBase;
6use MediaWiki\Parser\ParserOutput;
7use Wikibase\Client\ParserOutput\ParserOutputProvider;
8use Wikibase\Client\Usage\UsageAccumulator;
9use Wikibase\Client\WikibaseClient;
10use Wikibase\DataModel\Entity\EntityIdParser;
11use Wikibase\Lexeme\Domain\Model\LexemeSubEntityId;
12
13/**
14 * @license GPL-2.0-or-later
15 */
16abstract class WikibaseLexemeAbstractEntityLibrary
17    extends LibraryBase implements ParserOutputProvider {
18
19    /** @var UsageAccumulator|null */
20    private $usageAccumulator;
21
22    /** @var EntityIdParser|null */
23    private $entityIdParser;
24
25    private function getUsageAccumulator(): UsageAccumulator {
26        if ( $this->usageAccumulator === null ) {
27            $this->usageAccumulator = WikibaseClient::getUsageAccumulatorFactory()
28                ->newFromParserOutputProvider( $this );
29        }
30
31        return $this->usageAccumulator;
32    }
33
34    private function getEntityIdParser(): EntityIdParser {
35        if ( $this->entityIdParser === null ) {
36            $this->entityIdParser = WikibaseClient::getEntityIdParser();
37        }
38        return $this->entityIdParser;
39    }
40
41    public function addAllUsage( string $prefixedEntityId ) {
42        $entityId = $this->getEntityIdParser()->parse( $prefixedEntityId );
43        if ( $entityId instanceof LexemeSubEntityId ) {
44            $this->getUsageAccumulator()->addAllUsage( $entityId->getLexemeId() );
45        } else {
46            $this->getUsageAccumulator()->addAllUsage( $entityId );
47        }
48    }
49
50    public function getParserOutput(): ParserOutput {
51        return $this->getParser()->getOutput();
52    }
53}