Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
CRAP
94.74% covered (success)
94.74%
18 / 19
LangWeightFunctionScoreBuilder
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
7.01
94.74% covered (success)
94.74%
18 / 19
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
8 / 8
 append
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
8 / 8
 getUserLang
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
<?php
namespace CirrusSearch\Search\Rescore;
use CirrusSearch\SearchConfig;
use Elastica\Query\FunctionScore;
use RequestContext;
/**
 * Boosts documents in user language and in wiki language if different
 * Uses getUserLanguage in SearchConfig and LanguageCode for language values
 * and CirrusSearchLanguageWeight['user'|'wiki'] for respective weights.
 */
class LangWeightFunctionScoreBuilder extends FunctionScoreBuilder {
    /**
     * @var string user language
     */
    private $userLang;
    /**
     * @var float user language weight
     */
    private $userWeight;
    /**
     * @var string wiki language
     */
    private $wikiLang;
    /**
     * @var float wiki language weight
     */
    private $wikiWeight;
    /**
     * @param SearchConfig $config
     * @param float $weight
     * @param string|null $userLang
     */
    public function __construct( SearchConfig $config, $weight, $userLang = null ) {
        parent::__construct( $config, $weight );
        $this->userLang = $userLang;
        $this->userWeight =
            $config->getElement( 'CirrusSearchLanguageWeight', 'user' );
        $this->wikiLang = $config->get( 'LanguageCode' );
        $this->wikiWeight =
            $config->getElement( 'CirrusSearchLanguageWeight', 'wiki' );
    }
    public function append( FunctionScore $functionScore ) {
        // Boost pages in a user's language
        $userLang = $this->getUserLang();
        if ( $this->userWeight ) {
            $functionScore->addWeightFunction( $this->userWeight * $this->weight,
                new \Elastica\Query\Term( [ 'language' => $userLang ] ) );
        }
        // And a wiki's language, if it's different
        if ( $this->wikiWeight && $this->userLang != $this->wikiLang ) {
            $functionScore->addWeightFunction( $this->wikiWeight * $this->weight,
                new \Elastica\Query\Term( [ 'language' => $this->wikiLang ] ) );
        }
    }
    private function getUserLang() {
        if ( $this->userLang === null ) {
            $this->userLang = RequestContext::getMain()->getLanguage()->getCode();
        }
        return $this->userLang;
    }
}