Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.96% |
20 / 23 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
LexemeMetaTagsCreator | |
86.96% |
20 / 23 |
|
50.00% |
2 / 4 |
8.14 | |
0.00% |
0 / 1 |
__construct | |
33.33% |
1 / 3 |
|
0.00% |
0 / 1 |
1.30 | |||
getMetaTags | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
getTitleText | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
getDescriptionText | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\Presentation\View; |
4 | |
5 | use Wikibase\DataModel\Entity\EntityDocument; |
6 | use Wikibase\Lexeme\Domain\Model\Lexeme; |
7 | use Wikibase\Lib\Store\FallbackLabelDescriptionLookup; |
8 | use Wikibase\View\EntityMetaTagsCreator; |
9 | use Wikimedia\Assert\Assert; |
10 | |
11 | /** |
12 | * Class for creating meta tags (i.e. title and description) for Lexemes |
13 | * @license GPL-2.0-or-later |
14 | */ |
15 | class LexemeMetaTagsCreator implements EntityMetaTagsCreator { |
16 | |
17 | private $lemmaSeparator; |
18 | private $labelDescriptionLookup; |
19 | |
20 | /** |
21 | * @param string $lemmaSeparator |
22 | * @param FallbackLabelDescriptionLookup $labelDescriptionLookup |
23 | */ |
24 | public function __construct( |
25 | $lemmaSeparator, |
26 | FallbackLabelDescriptionLookup $labelDescriptionLookup |
27 | ) { |
28 | Assert::parameterType( 'string', $lemmaSeparator, '$lemmaSeparator' ); |
29 | |
30 | $this->lemmaSeparator = $lemmaSeparator; |
31 | $this->labelDescriptionLookup = $labelDescriptionLookup; |
32 | } |
33 | |
34 | /** |
35 | * @param EntityDocument $entity |
36 | * |
37 | * @return array |
38 | */ |
39 | public function getMetaTags( EntityDocument $entity ): array { |
40 | Assert::parameterType( Lexeme::class, $entity, '$entity' ); |
41 | '@phan-var Lexeme $entity'; |
42 | |
43 | /** @var Lexeme $entity */ |
44 | $metaTags = [ |
45 | 'title' => $this->getTitleText( $entity ), |
46 | 'og:title' => $this->getTitleText( $entity ), |
47 | 'twitter:card' => 'summary', |
48 | ]; |
49 | |
50 | $description = $this->getDescriptionText( $entity ); |
51 | if ( $description !== '' ) { |
52 | $metaTags[ 'description' ] = $description; |
53 | $metaTags[ 'og:description' ] = $description; |
54 | |
55 | } |
56 | |
57 | return $metaTags; |
58 | } |
59 | |
60 | /** |
61 | * @param Lexeme $entity |
62 | * |
63 | * @return null|string |
64 | */ |
65 | private function getTitleText( Lexeme $entity ) { |
66 | $lemmas = $entity->getLemmas()->toTextArray(); |
67 | if ( !$lemmas ) { |
68 | return $entity->getId()->getSerialization(); |
69 | } |
70 | return implode( $this->lemmaSeparator, $lemmas ); |
71 | } |
72 | |
73 | private function getDescriptionText( Lexeme $entity ) { |
74 | $language = $this->labelDescriptionLookup->getLabel( $entity->getLanguage() ); |
75 | $category = $this->labelDescriptionLookup->getLabel( $entity->getLexicalCategory() ); |
76 | |
77 | if ( !$language || !$category ) { |
78 | return ''; |
79 | } |
80 | |
81 | return $language->getText() . ' ' . $category->getText(); |
82 | } |
83 | |
84 | } |