Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WikibaseMediaInfoLibrary
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 register
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getMediaInfoId
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 getEntityIdLookup
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Wikibase\MediaInfo\DataAccess\Scribunto;
4
5use MediaWiki\Title\Title;
6use Wikibase\Client\DataAccess\Scribunto\WikibaseLibrary;
7use Wikibase\Lib\Store\EntityIdLookup;
8use Wikibase\MediaInfo\DataModel\MediaInfoId;
9use Wikibase\MediaInfo\Services\MediaInfoServices;
10
11/**
12 * Registers and defines functions to access WikibaseMediaInfo through the Scribunto extension
13 */
14class WikibaseMediaInfoLibrary extends WikibaseLibrary {
15
16    /**
17     * @var EntityIdLookup
18     */
19    private $entityIdLookup;
20
21    /**
22     * Register mw.wikibase.mediainfo.lua library
23     *
24     * @return array
25     */
26    public function register() {
27        // These functions will be exposed to the Lua module.
28        // They are member functions on a Lua table which is private to the module, thus
29        // these can't be called from user code, unless explicitly exposed in Lua.
30        $lib = [
31            'incrementStatsKey' => [ $this, 'incrementStatsKey' ],
32            'getMediaInfoId' => [ $this, 'getMediaInfoId' ],
33        ];
34
35        return $this->getEngine()->registerInterface(
36            __DIR__ . '/mw.wikibase.mediainfo.lua', $lib, []
37        );
38    }
39
40    /**
41     * Alternative for parent's getEntityId, but specific to MediaInfo entities,
42     * for which there is no $globalSiteId-based lookup.
43     *
44     * @param string $pageTitle
45     * @return array
46     */
47    public function getMediaInfoId( $pageTitle ) {
48        $this->checkType( 'getMediaInfoId', 1, $pageTitle, 'string' );
49
50        $title = Title::newFromDBkey( $pageTitle );
51        if ( $title === null ) {
52            return [ null ];
53        }
54
55        $entityId = $this->getEntityIdLookup()->getEntityIdForTitle( $title );
56        if ( !$entityId instanceof MediaInfoId ) {
57            return [ null ];
58        }
59
60        $this->getUsageAccumulator()->addTitleUsage( $entityId );
61
62        return [ $entityId->getSerialization() ];
63    }
64
65    /**
66     * @return EntityIdLookup
67     */
68    private function getEntityIdLookup() {
69        if ( $this->entityIdLookup === null ) {
70            $this->entityIdLookup = MediaInfoServices::getMediaInfoIdLookup();
71        }
72
73        return $this->entityIdLookup;
74    }
75
76}