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