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