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
Scribunto_LuaWikibaseMediaInfoLibrary
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\Scribunto_LuaWikibaseLibrary;
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 Scribunto_LuaWikibaseMediaInfoLibrary extends Scribunto_LuaWikibaseLibrary {
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        // @phan-suppress-next-line PhanUndeclaredMethod
36        return $this->getEngine()->registerInterface(
37            __DIR__ . '/mw.wikibase.mediainfo.lua', $lib, []
38        );
39    }
40
41    /**
42     * Alternative for parent's getEntityId, but specific to MediaInfo entities,
43     * for which there is no $globalSiteId-based lookup.
44     *
45     * @param string $pageTitle
46     * @return array
47     */
48    public function getMediaInfoId( $pageTitle ) {
49        // @phan-suppress-next-line PhanUndeclaredMethod
50        $this->checkType( 'getMediaInfoId', 1, $pageTitle, 'string' );
51
52        $title = Title::newFromDBkey( $pageTitle );
53        if ( $title === null ) {
54            return [ null ];
55        }
56
57        $entityId = $this->getEntityIdLookup()->getEntityIdForTitle( $title );
58        if ( !$entityId instanceof MediaInfoId ) {
59            return [ null ];
60        }
61
62        $this->getUsageAccumulator()->addTitleUsage( $entityId );
63
64        return [ $entityId->getSerialization() ];
65    }
66
67    /**
68     * @return EntityIdLookup
69     */
70    private function getEntityIdLookup() {
71        if ( $this->entityIdLookup === null ) {
72            $this->entityIdLookup = MediaInfoServices::getMediaInfoIdLookup();
73        }
74
75        return $this->entityIdLookup;
76    }
77
78}