Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
CRAP
32.14% covered (danger)
32.14%
9 / 28
FormsField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
17.25
32.14% covered (danger)
32.14%
9 / 28
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getMapping
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 16
 getFieldData
0.00% covered (danger)
0.00%
0 / 1
3.01
90.00% covered (success)
90.00%
9 / 10
<?php
namespace Wikibase\Lexeme\Search\Elastic;
use CirrusSearch\CirrusSearch;
use CirrusSearch\Search\KeywordIndexField;
use SearchEngine;
use SearchIndexField;
use Wikibase\DataModel\Entity\EntityDocument;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\Lexeme\Domain\Model\Lexeme;
use Wikibase\Search\Elastic\Fields\TermIndexField;
/**
 * Field for Lexeme Forms
 */
class FormsField extends TermIndexField {
    public const NAME = 'lexeme_forms';
    public function __construct() {
        parent::__construct( static::NAME, \SearchIndexField::INDEX_TYPE_TEXT );
    }
    /**
     * @param SearchEngine $engine
     * @return array
     */
    public function getMapping( SearchEngine $engine ) {
        // Since we need a specially tuned field, we can not use
        // standard search engine types.
        if ( !( $engine instanceof CirrusSearch ) ) {
            // For now only Cirrus/Elastic is supported
            return [];
        }
        $reprConfig = $this->getUnindexedField();
        $reprConfig['fields']['prefix'] =
            $this->getSubfield( 'prefix_asciifolding', 'near_match_asciifolding' );
        $reprConfig['fields']['near_match'] = $this->getSubfield( 'near_match' );
        $reprConfig['fields']['near_match_folded'] = $this->getSubfield( 'near_match_asciifolding' );
        // TODO: we don't seem to be using this, check if we need it?
        $reprConfig['copy_to'] = 'labels_all';
        $keyword = new KeywordIndexField( $this->getName(), SearchIndexField::INDEX_TYPE_KEYWORD,
                $engine->getConfig() );
        $keyword->setFlag( self::FLAG_CASEFOLD );
        $keywordMapping = $keyword->getMapping( $engine );
        $config = [
            'type' => 'object',
            'properties' => [
                'id' => $keywordMapping,
                'representation' => $reprConfig,
            ]
        ];
        return $config;
    }
    /**
     * @param EntityDocument $entity
     *
     * @return mixed Get the value of the field to be indexed when a page/document
     *               is indexed. This might be an array with nested data, if the field
     *               is defined with nested type or an int or string for simple field types.
     */
    public function getFieldData( EntityDocument $entity ) {
        if ( !( $entity instanceof Lexeme ) ) {
            return [];
        }
        /**
         * @var Lexeme $entity
         */
        $data = [];
        foreach ( $entity->getForms()->toArray() as $form ) {
            $data[] = [
                "id" => $form->getId()->getSerialization(),
                "representation" => array_values( $form->getRepresentations()->toTextArray() ),
                "features" => array_map( static function ( ItemId $item ) {
                                    return $item->getSerialization();
                }, $form->getGrammaticalFeatures() )
            ];
        }
        return $data;
    }
}