summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/QueryEngine/Fulltext/SearchTableUpdater.php
blob: a329d664e1e71d8f2641ed532f4b3a26fa01f74b (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php

namespace SMW\SQLStore\QueryEngine\Fulltext;

use SMW\MediaWiki\Database;

/**
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class SearchTableUpdater {

	/**
	 * @var Database
	 */
	private $connection;

	/**
	 * @var SearchTable
	 */
	private $searchTable;

	/**
	 * @var TextSanitizer
	 */
	private $textSanitizer;

	/**
	 * @since 2.5
	 *
	 * @param Database $connection
	 * @param SearchTable $searchTable
	 * @param TextSanitizer $textSanitizer
	 */
	public function __construct( Database $connection, SearchTable $searchTable, TextSanitizer $textSanitizer ) {
		$this->connection = $connection;
		$this->searchTable = $searchTable;
		$this->textSanitizer = $textSanitizer;
	}

	/**
	 * @since 2.5
	 *
	 * @return SearchTable
	 */
	public function getSearchTable() {
		return $this->searchTable;
	}

	/**
	 * @since 2.5
	 *
	 * @return boolean
	 */
	public function isEnabled() {
		return $this->searchTable->isEnabled();
	}

	/**
	 * @since 2.5
	 *
	 * @return array
	 */
	public function getPropertyTables() {
		return $this->searchTable->getPropertyTables();
	}

	/**
	 * @see http://dev.mysql.com/doc/refman/5.7/en/fulltext-fine-tuning.html
	 * @see http://dev.mysql.com/doc/refman/5.7/en/optimize-table.html
	 *
	 * "Running OPTIMIZE TABLE on a table with a full-text index rebuilds the
	 * full-text index, removing deleted Document IDs and consolidating multiple
	 * entries for the same word, where possible."
	 *
	 * @since 2.5
	 *
	 * @return boolean
	 */
	public function optimize() {

		if ( !$this->connection->isType( 'mysql' ) ) {
			return false;
		}

		$this->connection->query(
			"OPTIMIZE TABLE " . $this->searchTable->getTableName(),
			__METHOD__
		);

		return true;
	}

	/**
	 * @since 2.5
	 *
	 * @param integer $sid
	 * @param integer $pid
	 *
	 * @return boolean
	 */
	public function exists( $sid, $pid ) {

		$row = $this->connection->selectRow(
			$this->searchTable->getTableName(),
			[ 's_id' ],
			[
				's_id' => (int)$sid,
				'p_id' => (int)$pid
			],
			__METHOD__
		);

		return $row !== false;
	}

	/**
	 * @since 2.5
	 *
	 * @param integer $sid
	 * @param integer $pid
	 *
	 * @return false|string
	 */
	public function read( $sid, $pid ) {
		$row = $this->connection->selectRow(
			$this->searchTable->getTableName(),
			[ 'o_text' ],
			[
				's_id' => (int)$sid,
				'p_id' => (int)$pid
			],
			__METHOD__
		);

		if ( $row === false ) {
			return false;
		}

		return $this->textSanitizer->sanitize( $row->o_text );
	}

	/**
	 * @since 2.5
	 *
	 * @param integer $sid
	 * @param integer $pid
	 * @param string $text
	 */
	public function update( $sid, $pid, $text ) {

		if ( trim( $text ) === '' || ( $indexableText = $this->textSanitizer->sanitize( $text ) ) === '' ) {
			return $this->delete( $sid, $pid );
		}

		$this->connection->update(
			$this->searchTable->getTableName(),
			[
				'o_text' => $indexableText,
				'o_sort' => mb_substr( $text, 0, 32 )
			],
			[
				's_id' => (int)$sid,
				'p_id' => (int)$pid
			],
			__METHOD__
		);
	}

	/**
	 * @since 2.5
	 *
	 * @param integer $sid
	 * @param integer $pid
	 */
	public function insert( $sid, $pid ) {
		$this->connection->insert(
			$this->searchTable->getTableName(),
			[
				's_id' => (int)$sid,
				'p_id' => (int)$pid,
				'o_text' => ''
			],
			__METHOD__
		);
	}

	/**
	 * @since 2.5
	 *
	 * @param integer $sid
	 * @param integer $pid
	 */
	public function delete( $sid, $pid ) {
		$this->connection->delete(
			$this->searchTable->getTableName(),
			[
				's_id' => (int)$sid,
				'p_id' => (int)$pid
			],
			__METHOD__
		);
	}

	/**
	 * @since 2.5
	 */
	public function flushTable() {
		$this->connection->delete(
			$this->searchTable->getTableName(),
			'*',
			__METHOD__
		);
	}

}