Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.57% covered (warning)
69.57%
16 / 23
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
LexemeResult
69.57% covered (warning)
69.57%
16 / 23
50.00% covered (danger)
50.00%
3 / 6
12.82
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 getTitleSnippet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTextSnippet
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getStatementCount
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getFormCount
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 isFormResult
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Wikibase\Lexeme\Search\Elastic;
3
4use CirrusSearch\Search\Result;
5use HtmlArmor;
6use Language;
7use MediaWiki\Html\Html;
8use Wikibase\Lexeme\DataAccess\LexemeDescription;
9
10/**
11 * Single CirrusSearch result for Lexeme fulltext search.
12 */
13class LexemeResult extends Result {
14    /**
15     * Raw label data from source.
16     * @var string
17     */
18    private $labelData;
19    /**
20     * Description data with highlighting.
21     * @var string
22     */
23    private $descriptionData;
24    /**
25     * Original source data
26     * @var array
27     */
28    private $sourceData;
29    /**
30     * @var bool
31     */
32    private $isFormResult;
33
34    /**
35     * @param Language $displayLanguage
36     * @param LexemeDescription $descriptionMaker
37     * @param array $result Result from LexemeFulltextResult
38     */
39    public function __construct(
40        Language $displayLanguage,
41        LexemeDescription $descriptionMaker,
42        array $result
43    ) {
44        // Let parent Result class handle the boring stuff
45        parent::__construct( null, $result['elasticResult'] );
46        $this->sourceData = $result['elasticResult']->getSource();
47        $this->isFormResult = isset( $result['formId'] );
48        if ( $this->isFormResult ) {
49            // Form
50            $this->descriptionData = $descriptionMaker->createFormDescription( $result['lexemeId'],
51                $result['features'], $result['lemma'], $result['lang'],
52                $result['category'] );
53            $this->labelData = $result['representation'];
54            // This copies FormTitleStoreLookup, we could instantiate one instead
55            // but that would add a lot of wrapper code.
56            if ( $this->mTitle !== null ) {
57                $this->mTitle->setFragment( '#' . $result['formId'] );
58            }
59        } else {
60            // Lexeme
61            $this->descriptionData = $descriptionMaker->createDescription( $result['lexemeId'],
62                $result['lang'], $result['category'] );
63            $this->labelData = $result['lemma'];
64        }
65    }
66
67    /**
68     * @return string
69     */
70    public function getTitleSnippet() {
71        return HtmlArmor::getHtml( $this->labelData );
72    }
73
74    /**
75     * @param array $terms
76     * @return string
77     */
78    public function getTextSnippet( $terms = [] ) {
79        $attr = [ 'class' => 'wb-itemlink-description' ];
80        return Html::rawElement( 'span', $attr, HtmlArmor::getHtml( $this->descriptionData ) );
81    }
82
83    /**
84     * Get number of statements
85     * @return int
86     */
87    public function getStatementCount() {
88        if ( !isset( $this->sourceData['statement_count'] ) ) {
89            return 0;
90        }
91        return (int)$this->sourceData['statement_count'];
92    }
93
94    /**
95     * Get number of statements
96     * @return int
97     */
98    public function getFormCount() {
99        if ( empty( $this->sourceData[FormsField::NAME] ) ) {
100            return 0;
101        }
102        return count( $this->sourceData[FormsField::NAME] );
103    }
104
105    /**
106     * Is this Form result?
107     * @return bool
108     */
109    public function isFormResult() {
110        return $this->isFormResult;
111    }
112
113}