Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / search / SearchIndexFieldDefinition.php
blobff6af5eb0e7b72d00499c9ae4cbd8fc7357610db
1 <?php
3 /**
4 * Basic infrastructure of the field definition.
6 * Specific engines should extend this class and at least,
7 * override the getMapping method, but can reuse other parts.
9 * @stable to extend
10 * @since 1.28
12 abstract class SearchIndexFieldDefinition implements SearchIndexField {
14 /**
15 * Name of the field
17 * @var string
19 protected $name;
21 /**
22 * Type of the field, one of the constants above
24 * @var string
26 protected $type;
28 /**
29 * Bit flags for the field.
31 * @var int
33 protected $flags = 0;
35 /**
36 * Subfields
37 * @var SearchIndexFieldDefinition[]
39 protected $subfields = [];
41 /**
42 * @var callable
44 private $mergeCallback;
46 /**
47 * @param string $name Field name
48 * @param string $type Index type
50 public function __construct( $name, $type ) {
51 $this->name = $name;
52 $this->type = $type;
55 /**
56 * Get field name
57 * @return string
59 public function getName() {
60 return $this->name;
63 /**
64 * @return string
66 public function getIndexType() {
67 return $this->type;
70 /**
71 * Set global flag for this field.
72 * @stable to override
74 * @param int $flag Bit flag to set/unset
75 * @param bool $unset True if flag should be unset, false by default
76 * @return $this
78 public function setFlag( $flag, $unset = false ) {
79 if ( $unset ) {
80 $this->flags &= ~$flag;
81 } else {
82 $this->flags |= $flag;
84 return $this;
87 /**
88 * Check if flag is set.
89 * @stable to override
91 * @param int $flag
92 * @return int 0 if unset, !=0 if set
94 public function checkFlag( $flag ) {
95 return $this->flags & $flag;
98 /**
99 * Merge two field definitions if possible.
100 * @stable to override
102 * @param SearchIndexField $that
103 * @return SearchIndexField|false New definition or false if not mergeable.
105 public function merge( SearchIndexField $that ) {
106 if ( $this->mergeCallback ) {
107 return call_user_func( $this->mergeCallback, $this, $that );
109 // TODO: which definitions may be compatible?
110 if ( ( $that instanceof self ) && $this->type === $that->type &&
111 $this->flags === $that->flags && $this->type !== self::INDEX_TYPE_NESTED
113 return $that;
115 return false;
119 * @return SearchIndexFieldDefinition[]
121 public function getSubfields() {
122 return $this->subfields;
126 * @param SearchIndexFieldDefinition[] $subfields
127 * @return $this
129 public function setSubfields( array $subfields ) {
130 $this->subfields = $subfields;
131 return $this;
135 * @param SearchEngine $engine
137 * @return array
139 abstract public function getMapping( SearchEngine $engine );
142 * Set field-specific merge strategy.
143 * @param callable $callback
145 public function setMergeCallback( $callback ) {
146 $this->mergeCallback = $callback;
150 * @inheritDoc
152 public function getEngineHints( SearchEngine $engine ) {
153 return [];