summaryrefslogtreecommitdiff
path: root/www/wiki/includes/search/SearchIndexFieldDefinition.php
blob: a11dff9fc70fe3377aa711380cdb66f49998b611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php

/**
 * Basic infrastructure of the field definition.
 *
 * Specific engines should extend this class and at at least,
 * override the getMapping method, but can reuse other parts.
 *
 * @since 1.28
 */
abstract class SearchIndexFieldDefinition implements SearchIndexField {

	/**
	 * Name of the field
	 *
	 * @var string
	 */
	protected $name;

	/**
	 * Type of the field, one of the constants above
	 *
	 * @var int
	 */
	protected $type;

	/**
	 * Bit flags for the field.
	 *
	 * @var int
	 */
	protected $flags = 0;

	/**
	 * Subfields
	 * @var SearchIndexFieldDefinition[]
	 */
	protected $subfields = [];

	/**
	 * @var callable
	 */
	private $mergeCallback;

	/**
	 * @param string $name Field name
	 * @param int $type Index type
	 */
	public function __construct( $name, $type ) {
		$this->name = $name;
		$this->type = $type;
	}

	/**
	 * Get field name
	 * @return string
	 */
	public function getName() {
		return $this->name;
	}

	/**
	 * Get index type
	 * @return int
	 */
	public function getIndexType() {
		return $this->type;
	}

	/**
	 * Set global flag for this field.
	 *
	 * @param int $flag Bit flag to set/unset
	 * @param bool $unset True if flag should be unset, false by default
	 * @return $this
	 */
	public function setFlag( $flag, $unset = false ) {
		if ( $unset ) {
			$this->flags &= ~$flag;
		} else {
			$this->flags |= $flag;
		}
		return $this;
	}

	/**
	 * Check if flag is set.
	 * @param int $flag
	 * @return int 0 if unset, !=0 if set
	 */
	public function checkFlag( $flag ) {
		return $this->flags & $flag;
	}

	/**
	 * Merge two field definitions if possible.
	 *
	 * @param SearchIndexField $that
	 * @return SearchIndexField|false New definition or false if not mergeable.
	 */
	public function merge( SearchIndexField $that ) {
		if ( !empty( $this->mergeCallback ) ) {
			return call_user_func( $this->mergeCallback, $this, $that );
		}
		// TODO: which definitions may be compatible?
		if ( ( $that instanceof self ) && $this->type === $that->type &&
			$this->flags === $that->flags && $this->type !== self::INDEX_TYPE_NESTED
		) {
			return $that;
		}
		return false;
	}

	/**
	 * Get subfields
	 * @return SearchIndexFieldDefinition[]
	 */
	public function getSubfields() {
		return $this->subfields;
	}

	/**
	 * Set subfields
	 * @param SearchIndexFieldDefinition[] $subfields
	 * @return $this
	 */
	public function setSubfields( array $subfields ) {
		$this->subfields = $subfields;
		return $this;
	}

	/**
	 * @param SearchEngine $engine
	 *
	 * @return array
	 */
	abstract public function getMapping( SearchEngine $engine );

	/**
	 * Set field-specific merge strategy.
	 * @param callable $callback
	 */
	public function setMergeCallback( $callback ) {
		$this->mergeCallback = $callback;
	}

	/**
	 * @inheritDoc
	 */
	public function getEngineHints( SearchEngine $engine ) {
		return [];
	}
}