Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.65% |
22 / 23 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| LexemeMetaTagsCreator | |
95.65% |
22 / 23 |
|
75.00% |
3 / 4 |
8 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 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 | /** @var string */ |
| 18 | private $lemmaSeparator; |
| 19 | private FallbackLabelDescriptionLookup $labelDescriptionLookup; |
| 20 | |
| 21 | /** |
| 22 | * @param string $lemmaSeparator |
| 23 | * @param FallbackLabelDescriptionLookup $labelDescriptionLookup |
| 24 | */ |
| 25 | public function __construct( |
| 26 | $lemmaSeparator, |
| 27 | FallbackLabelDescriptionLookup $labelDescriptionLookup |
| 28 | ) { |
| 29 | Assert::parameterType( 'string', $lemmaSeparator, '$lemmaSeparator' ); |
| 30 | |
| 31 | $this->lemmaSeparator = $lemmaSeparator; |
| 32 | $this->labelDescriptionLookup = $labelDescriptionLookup; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @param EntityDocument $entity |
| 37 | * |
| 38 | * @return array |
| 39 | */ |
| 40 | public function getMetaTags( EntityDocument $entity ): array { |
| 41 | Assert::parameterType( Lexeme::class, $entity, '$entity' ); |
| 42 | '@phan-var Lexeme $entity'; |
| 43 | |
| 44 | /** @var Lexeme $entity */ |
| 45 | $metaTags = [ |
| 46 | 'title' => $this->getTitleText( $entity ), |
| 47 | 'og:title' => $this->getTitleText( $entity ), |
| 48 | 'twitter:card' => 'summary', |
| 49 | ]; |
| 50 | |
| 51 | $description = $this->getDescriptionText( $entity ); |
| 52 | if ( $description !== '' ) { |
| 53 | $metaTags[ 'description' ] = $description; |
| 54 | $metaTags[ 'og:description' ] = $description; |
| 55 | |
| 56 | } |
| 57 | |
| 58 | return $metaTags; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param Lexeme $entity |
| 63 | * |
| 64 | * @return null|string |
| 65 | */ |
| 66 | private function getTitleText( Lexeme $entity ) { |
| 67 | $lemmas = $entity->getLemmas()->toTextArray(); |
| 68 | if ( !$lemmas ) { |
| 69 | return $entity->getId()->getSerialization(); |
| 70 | } |
| 71 | return implode( $this->lemmaSeparator, $lemmas ); |
| 72 | } |
| 73 | |
| 74 | private function getDescriptionText( Lexeme $entity ): string { |
| 75 | $language = $this->labelDescriptionLookup->getLabel( $entity->getLanguage() ); |
| 76 | $category = $this->labelDescriptionLookup->getLabel( $entity->getLexicalCategory() ); |
| 77 | |
| 78 | if ( !$language || !$category ) { |
| 79 | return ''; |
| 80 | } |
| 81 | |
| 82 | return $language->getText() . ' ' . $category->getText(); |
| 83 | } |
| 84 | |
| 85 | } |