Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / search / SearchIndexFieldDefinition.php
blob8a06b65ed7bd3e8aebfafae7e6ccd1ff24e407c0
1 <?php
3 /**
4 * Basic infrastructure of the field definition.
6 * Specific engines should extend this class and at at least,
7 * override the getMapping method, but can reuse other parts.
9 * @since 1.28
11 abstract class SearchIndexFieldDefinition implements SearchIndexField {
12 /**
13 * Name of the field
15 * @var string
17 protected $name;
18 /**
19 * Type of the field, one of the constants above
21 * @var int
23 protected $type;
24 /**
25 * Bit flags for the field.
27 * @var int
29 protected $flags = 0;
30 /**
31 * Subfields
32 * @var SearchIndexFieldDefinition[]
34 protected $subfields = [];
36 /**
37 * SearchIndexFieldDefinition constructor.
38 * @param string $name Field name
39 * @param int $type Index type
41 public function __construct( $name, $type ) {
42 $this->name = $name;
43 $this->type = $type;
46 /**
47 * Get field name
48 * @return string
50 public function getName() {
51 return $this->name;
54 /**
55 * Get index type
56 * @return int
58 public function getIndexType() {
59 return $this->type;
62 /**
63 * Set global flag for this field.
65 * @param int $flag Bit flag to set/unset
66 * @param bool $unset True if flag should be unset, false by default
67 * @return $this
69 public function setFlag( $flag, $unset = false ) {
70 if ( $unset ) {
71 $this->flags &= ~$flag;
72 } else {
73 $this->flags |= $flag;
75 return $this;
78 /**
79 * Check if flag is set.
80 * @param $flag
81 * @return int 0 if unset, !=0 if set
83 public function checkFlag( $flag ) {
84 return $this->flags & $flag;
87 /**
88 * Merge two field definitions if possible.
90 * @param SearchIndexField $that
91 * @return SearchIndexField|false New definition or false if not mergeable.
93 public function merge( SearchIndexField $that ) {
94 // TODO: which definitions may be compatible?
95 if ( ( $that instanceof self ) && $this->type === $that->type &&
96 $this->flags === $that->flags && $this->type !== self::INDEX_TYPE_NESTED
97 ) {
98 return $that;
100 return false;
104 * Get subfields
105 * @return SearchIndexFieldDefinition[]
107 public function getSubfields() {
108 return $this->subfields;
112 * Set subfields
113 * @param SearchIndexFieldDefinition[] $subfields
114 * @return $this
116 public function setSubfields( array $subfields ) {
117 $this->subfields = $subfields;
118 return $this;
122 * @param SearchEngine $engine
124 * @return array
126 abstract public function getMapping( SearchEngine $engine );