Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.14% covered (warning)
57.14%
12 / 21
30.00% covered (danger)
30.00%
3 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchIndexFieldDefinition
60.00% covered (warning)
60.00%
12 / 20
30.00% covered (danger)
30.00%
3 / 10
32.38
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getIndexType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setFlag
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 checkFlag
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 merge
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
6
 getSubfields
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setSubfields
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getMapping
n/a
0 / 0
n/a
0 / 0
0
 setMergeCallback
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEngineHints
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Search;
4
5/**
6 * Basic infrastructure of the field definition.
7 *
8 * Specific engines should extend this class and at least,
9 * override the getMapping method, but can reuse other parts.
10 *
11 * @stable to extend
12 * @since 1.28
13 */
14abstract class SearchIndexFieldDefinition implements SearchIndexField {
15
16    /**
17     * Name of the field
18     *
19     * @var string
20     */
21    protected $name;
22
23    /**
24     * Type of the field, one of the constants above
25     *
26     * @var string
27     */
28    protected $type;
29
30    /**
31     * Bit flags for the field.
32     *
33     * @var int
34     */
35    protected $flags = 0;
36
37    /**
38     * Subfields
39     * @var SearchIndexFieldDefinition[]
40     */
41    protected $subfields = [];
42
43    /**
44     * @var callable
45     */
46    private $mergeCallback;
47
48    /**
49     * @param string $name Field name
50     * @param string $type Index type
51     */
52    public function __construct( $name, $type ) {
53        $this->name = $name;
54        $this->type = $type;
55    }
56
57    /**
58     * Get field name
59     * @return string
60     */
61    public function getName() {
62        return $this->name;
63    }
64
65    /**
66     * @return string
67     */
68    public function getIndexType() {
69        return $this->type;
70    }
71
72    /**
73     * Set global flag for this field.
74     * @stable to override
75     *
76     * @param int $flag Bit flag to set/unset
77     * @param bool $unset True if flag should be unset, false by default
78     * @return $this
79     */
80    public function setFlag( $flag, $unset = false ) {
81        if ( $unset ) {
82            $this->flags &= ~$flag;
83        } else {
84            $this->flags |= $flag;
85        }
86        return $this;
87    }
88
89    /**
90     * Check if flag is set.
91     * @stable to override
92     *
93     * @param int $flag
94     * @return int 0 if unset, !=0 if set
95     */
96    public function checkFlag( $flag ) {
97        return $this->flags & $flag;
98    }
99
100    /**
101     * Merge two field definitions if possible.
102     * @stable to override
103     *
104     * @param SearchIndexField $that
105     * @return SearchIndexField|false New definition or false if not mergeable.
106     */
107    public function merge( SearchIndexField $that ) {
108        if ( $this->mergeCallback ) {
109            return ( $this->mergeCallback )( $this, $that );
110        }
111        // TODO: which definitions may be compatible?
112        if ( ( $that instanceof self ) && $this->type === $that->type &&
113            $this->flags === $that->flags && $this->type !== self::INDEX_TYPE_NESTED
114        ) {
115            return $that;
116        }
117        return false;
118    }
119
120    /**
121     * @return SearchIndexFieldDefinition[]
122     */
123    public function getSubfields() {
124        return $this->subfields;
125    }
126
127    /**
128     * @param SearchIndexFieldDefinition[] $subfields
129     * @return $this
130     */
131    public function setSubfields( array $subfields ) {
132        $this->subfields = $subfields;
133        return $this;
134    }
135
136    /**
137     * @param SearchEngine $engine
138     *
139     * @return array
140     */
141    abstract public function getMapping( SearchEngine $engine );
142
143    /**
144     * Set field-specific merge strategy.
145     * @param callable $callback
146     */
147    public function setMergeCallback( $callback ) {
148        $this->mergeCallback = $callback;
149    }
150
151    /**
152     * @inheritDoc
153     */
154    public function getEngineHints( SearchEngine $engine ) {
155        return [];
156    }
157}
158
159/** @deprecated class alias since 1.46 */
160class_alias( SearchIndexFieldDefinition::class, 'SearchIndexFieldDefinition' );