Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
InfoActionHookHandler
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 4
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 getSenseAndFormCount
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 formatProperties
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Wikibase\Lexeme\MediaWiki\Actions;
4
5use IContextSource;
6use MediaWiki\Page\PageProps;
7use MediaWiki\Title\Title;
8use Wikibase\Lexeme\Domain\Model\LexemeId;
9use Wikibase\Lib\Store\EntityIdLookup;
10use Wikibase\Lib\Store\EntityNamespaceLookup;
11
12/**
13 * @license GPL-2.0-or-later
14 * @author Amir Sarabadani <ladsgroup@gmail.com>
15 */
16class InfoActionHookHandler {
17
18    /**
19     * @var EntityNamespaceLookup
20     */
21    private $namespaceChecker;
22
23    /**
24     * @var EntityIdLookup
25     */
26    private $entityIdLookup;
27
28    /**
29     * @var PageProps
30     */
31    private $pageProps;
32
33    /**
34     * @var IContextSource
35     */
36    private $context;
37
38    public function __construct(
39        EntityNamespaceLookup $namespaceChecker,
40        EntityIdLookup $entityIdLookup,
41        PageProps $pageProps,
42        IContextSource $context
43    ) {
44        $this->namespaceChecker = $namespaceChecker;
45        $this->entityIdLookup = $entityIdLookup;
46        $this->pageProps = $pageProps;
47        $this->context = $context;
48    }
49
50    /**
51     * @param IContextSource $context
52     * @param array $pageInfo
53     *
54     * @return array[]
55     */
56    public function handle( IContextSource $context, array $pageInfo ) {
57        // Check if wikibase namespace is enabled
58        $title = $context->getTitle();
59
60        if ( !$this->namespaceChecker->isNamespaceWithEntities( $title->getNamespace() )
61            || !$title->exists()
62        ) {
63            return $pageInfo;
64        }
65
66        $entityId = $this->entityIdLookup->getEntityIdForTitle( $title );
67        if ( !$entityId instanceof LexemeId ) {
68            return $pageInfo;
69        }
70
71        $pageInfo['header-basic'] = array_merge(
72            $pageInfo['header-basic'],
73            $this->getSenseAndFormCount( $title )
74        );
75
76        return $pageInfo;
77    }
78
79    /**
80     * @param Title $title
81     *
82     * @return string[] HTML
83     */
84    private function getSenseAndFormCount( Title $title ) {
85        $properties = $this->pageProps->getProperties( $title, [ 'wbl-forms', 'wbl-senses' ] );
86
87        if ( $properties ) {
88            return $this->formatProperties( $properties );
89        }
90
91        return [];
92    }
93
94    /**
95     * @param array $properties
96     *
97     * @return string[] HTML
98     */
99    private function formatProperties( array $properties ) {
100        $output = [];
101
102        foreach ( $properties as $pageId => $pageProperties ) {
103            foreach ( $pageProperties as $property => $value ) {
104                $output[] = [
105                    $this->context->msg( 'wikibase-pageinfo-' . $property )->parse(),
106                    $this->context->getLanguage()->formatNum( (int)$value ),
107                ];
108            }
109        }
110
111        return $output;
112    }
113
114}