Merge "Handle missing namespace prefix in XML dumps more gracefully"
[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 * SearchIndexFieldDefinition constructor.
38 * @param string $name Field name
39 * @param int $type Index type
40 */
41 public function __construct( $name, $type ) {
42 $this->name = $name;
43 $this->type = $type;
44 }
45
46 /**
47 * Get field name
48 * @return string
49 */
50 public function getName() {
51 return $this->name;
52 }
53
54 /**
55 * Get index type
56 * @return int
57 */
58 public function getIndexType() {
59 return $this->type;
60 }
61
62 /**
63 * Set global flag for this field.
64 *
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
68 */
69 public function setFlag( $flag, $unset = false ) {
70 if ( $unset ) {
71 $this->flags &= ~$flag;
72 } else {
73 $this->flags |= $flag;
74 }
75 return $this;
76 }
77
78 /**
79 * Check if flag is set.
80 * @param $flag
81 * @return int 0 if unset, !=0 if set
82 */
83 public function checkFlag( $flag ) {
84 return $this->flags & $flag;
85 }
86
87 /**
88 * Merge two field definitions if possible.
89 *
90 * @param SearchIndexField $that
91 * @return SearchIndexField|false New definition or false if not mergeable.
92 */
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;
99 }
100 return false;
101 }
102
103 /**
104 * Get subfields
105 * @return SearchIndexFieldDefinition[]
106 */
107 public function getSubfields() {
108 return $this->subfields;
109 }
110
111 /**
112 * Set subfields
113 * @param SearchIndexFieldDefinition[] $subfields
114 * @return $this
115 */
116 public function setSubfields( array $subfields ) {
117 $this->subfields = $subfields;
118 return $this;
119 }
120
121 /**
122 * @param SearchEngine $engine
123 *
124 * @return array
125 */
126 abstract public function getMapping( SearchEngine $engine );
127
128 }