Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FunctionScoreBuilder
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getOverriddenFactor
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * @license GPL-2.0-or-later
4 */
5
6namespace CirrusSearch\Search\Rescore;
7
8use CirrusSearch\SearchConfig;
9use MediaWiki\Context\RequestContext;
10
11abstract class FunctionScoreBuilder implements BoostFunctionBuilder {
12    /**
13     * @var SearchConfig
14     */
15    protected $config;
16
17    /**
18     * @var float global weight of this function score builder
19     */
20    protected $weight;
21
22    /**
23     * @param SearchConfig $config the search config
24     * @param float|array $weight the global weight
25     */
26    public function __construct( $config, $weight ) {
27        $this->config = $config;
28        $this->weight = $this->getOverriddenFactor( $weight );
29    }
30
31    /**
32     * Utility method to extract a factor (float) that can
33     * be overridden by a config value or an URI param
34     *
35     * @param float|array{value:float,config_override:string,uri_param_override:string} $value
36     * @return float
37     */
38    protected function getOverriddenFactor( $value ) {
39        if ( is_array( $value ) ) {
40            $returnValue = (float)$value['value'];
41
42            if ( isset( $value['config_override'] ) ) {
43                // Override factor with config
44                $fromConfig = $this->config->get( $value['config_override'] );
45                if ( $fromConfig !== null ) {
46                    $returnValue = (float)$fromConfig;
47                }
48            }
49
50            if ( isset( $value['uri_param_override'] ) ) {
51                // Override factor with uri param
52                $uriParam = $value['uri_param_override'];
53                $fromUri = RequestContext::getMain()->getRequest()->getVal( $uriParam );
54                if ( is_numeric( $fromUri ) ) {
55                    $returnValue = (float)$fromUri;
56                }
57            }
58            return $returnValue;
59        } else {
60            return (float)$value;
61        }
62    }
63}