Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.56% covered (success)
95.56%
43 / 45
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
LexemeDescription
95.56% covered (success)
95.56%
43 / 45
80.00% covered (warning)
80.00%
4 / 5
9
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getLabelOrDefault
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 createDescription
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 createFormDescription
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
2
 parseOrNull
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
2.50
1<?php
2
3namespace Wikibase\Lexeme\DataAccess;
4
5use MediaWiki\Language\Language;
6use Wikibase\DataModel\Entity\EntityId;
7use Wikibase\DataModel\Entity\EntityIdParser;
8use Wikibase\DataModel\Entity\EntityIdParsingException;
9use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
10
11/**
12 * Class for generating Lexeme description strings
13 *
14 * @license GPL-2.0-or-later
15 */
16class LexemeDescription {
17    /**
18     * @var LabelDescriptionLookup
19     */
20    private $lookup;
21    /**
22     * @var EntityIdParser
23     */
24    private $idParser;
25
26    /**
27     * Display language
28     * @var Language
29     */
30    private $displayLanguage;
31
32    public function __construct(
33        LabelDescriptionLookup $lookup,
34        EntityIdParser $idParser,
35        Language $displayLanguage
36    ) {
37        $this->lookup = $lookup;
38        $this->idParser = $idParser;
39        $this->displayLanguage = $displayLanguage;
40    }
41
42    /**
43     * Get label or return empty string.
44     * @param EntityId|null $id
45     * @param string $default Default value if unable to retrieve label
46     * @return string Label or "" if does not exist.
47     */
48    public function getLabelOrDefault(
49        ?EntityId $id = null,
50        $default = ""
51    ) {
52        if ( $id ) {
53            $label = $this->lookup->getLabel( $id );
54            if ( $label ) {
55                return $label->getText();
56            }
57        }
58        return $default;
59    }
60
61    /**
62     * Create short lexeme description, e.g.: "German noun" or "English verb"
63     * Currently not uses the ID, may change later
64     * @param EntityId $id Lexeme ID
65     * @param string $language Language ID, as string
66     * @param string $category Lexical category ID, as string
67     * @return string
68     */
69    public function createDescription( EntityId $id, $language, $category ) {
70        $languageId = self::parseOrNull( $language, $this->idParser );
71        $categoryId = self::parseOrNull( $category, $this->idParser );
72        return wfMessage( 'wikibaselexeme-description' )
73            ->inLanguage( $this->displayLanguage )
74            ->params(
75                $this->getLabelOrDefault( $languageId,
76                    wfMessage( 'wikibaselexeme-unknown-language' )
77                        ->inLanguage( $this->displayLanguage )
78                        ->text() ),
79                $this->getLabelOrDefault( $categoryId,
80                    wfMessage( 'wikibaselexeme-unknown-category' )
81                        ->inLanguage( $this->displayLanguage )
82                        ->text() )
83            )->text();
84    }
85
86    /**
87     * Create Form descriptions, along the lines of:
88     * singular genitive for Leiter (L1): German noun
89     *
90     * @param EntityId $lexemeId Main lexeme
91     * @param EntityId[] $features Form feature IDs list
92     * @param string $lemma Lexeme's lemma
93     * @param string $language Language ID, as string
94     * @param string $category Lexical category ID, as string
95     * @return string
96     */
97    public function createFormDescription(
98        EntityId $lexemeId, array $features, $lemma, $language, $category
99    ) {
100        $lemmaDescription = $this->createDescription( $lexemeId, $language, $category );
101        // Create list of feature labels, should match what FormsView.php is doing
102        $comma = wfMessage( 'comma-separator' )->inLanguage( $this->displayLanguage )->text();
103        $featuresString = implode( $comma, array_filter( array_map(
104            function ( EntityId $featureId ) {
105                // TODO: do we need separate string for this?
106                return $this->getLabelOrDefault( $featureId,
107                    wfMessage( 'wikibaselexeme-unknown-category' )
108                        ->inLanguage( $this->displayLanguage )->text() );
109            }, $features ) ) );
110        if ( $featuresString === '' ) {
111            $featuresString = wfMessage( 'wikibaselexeme-no-features' )
112                ->inLanguage( $this->displayLanguage )->text();
113        }
114        return wfMessage( 'wikibaselexeme-form-description' )
115            ->inLanguage( $this->displayLanguage )
116            ->params(
117                $featuresString,
118                $lemma,
119                $lexemeId->getSerialization(),
120                $lemmaDescription
121            )->text();
122    }
123
124    /**
125     * Parse entity ID or return null
126     * @param string $text
127     * @param EntityIdParser $idParser
128     * @return null|EntityId
129     */
130    public static function parseOrNull( $text, EntityIdParser $idParser ) {
131        try {
132            $id = $idParser->parse( $text );
133        } catch ( EntityIdParsingException $ex ) {
134            return null;
135        }
136        return $id;
137    }
138
139}