Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.48% covered (success)
93.48%
43 / 46
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
LexemeDescription
93.48% covered (success)
93.48%
43 / 46
60.00% covered (warning)
60.00%
3 / 5
9.02
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
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 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 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            return $default;
54        }
55        $label = $this->lookup->getLabel( $id );
56        if ( !$label ) {
57            return $default;
58        }
59        return $label->getText();
60    }
61
62    /**
63     * Create short lexeme description, e.g.: "German noun" or "English verb"
64     * Currently not uses the ID, may change later
65     * @param EntityId $id Lexeme ID
66     * @param string $language Language ID, as string
67     * @param string $category Lexical category ID, as string
68     * @return string
69     */
70    public function createDescription( EntityId $id, $language, $category ) {
71        $languageId = self::parseOrNull( $language, $this->idParser );
72        $categoryId = self::parseOrNull( $category, $this->idParser );
73        return wfMessage( 'wikibaselexeme-description' )
74            ->inLanguage( $this->displayLanguage )
75            ->params(
76                $this->getLabelOrDefault( $languageId,
77                    wfMessage( 'wikibaselexeme-unknown-language' )
78                        ->inLanguage( $this->displayLanguage )
79                        ->text() ),
80                $this->getLabelOrDefault( $categoryId,
81                    wfMessage( 'wikibaselexeme-unknown-category' )
82                        ->inLanguage( $this->displayLanguage )
83                        ->text() )
84            )->text();
85    }
86
87    /**
88     * Create Form descriptions, along the lines of:
89     * singular genitive for Leiter (L1): German noun
90     *
91     * @param EntityId $lexemeId Main lexeme
92     * @param EntityId[] $features Form feature IDs list
93     * @param string $lemma Lexeme's lemma
94     * @param string $language Language ID, as string
95     * @param string $category Lexical category ID, as string
96     * @return string
97     */
98    public function createFormDescription(
99        EntityId $lexemeId, array $features, $lemma, $language, $category
100    ) {
101        $lemmaDescription = $this->createDescription( $lexemeId, $language, $category );
102        // Create list of feature labels, should match what FormsView.php is doing
103        $comma = wfMessage( 'comma-separator' )->inLanguage( $this->displayLanguage )->text();
104        $featuresString = implode( $comma, array_filter( array_map(
105            function ( EntityId $featureId ) {
106                // TODO: do we need separate string for this?
107                return $this->getLabelOrDefault( $featureId,
108                    wfMessage( 'wikibaselexeme-unknown-category' )
109                        ->inLanguage( $this->displayLanguage )->text() );
110            }, $features ) ) );
111        if ( $featuresString === '' ) {
112            $featuresString = wfMessage( 'wikibaselexeme-no-features' )
113                ->inLanguage( $this->displayLanguage )->text();
114        }
115        return wfMessage( 'wikibaselexeme-form-description' )
116            ->inLanguage( $this->displayLanguage )
117            ->params(
118                $featuresString,
119                $lemma,
120                $lexemeId->getSerialization(),
121                $lemmaDescription
122            )->text();
123    }
124
125    /**
126     * Parse entity ID or return null
127     * @param string $text
128     * @param EntityIdParser $idParser
129     * @return null|EntityId
130     */
131    public static function parseOrNull( $text, EntityIdParser $idParser ) {
132        try {
133            $id = $idParser->parse( $text );
134        } catch ( EntityIdParsingException $ex ) {
135            return null;
136        }
137        return $id;
138    }
139
140}