Completed
Push — master ( 0a928b...54fdeb )
by Matt
22:47 queued 22:44
created

mysqli::is_fulltext()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 *
4
 * Precise Similar Topics
5
 *
6
 * @copyright (c) 2018 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\similartopics\driver;
12
13
/**
14
 * This class handles similar topics queries for MySQLi dbms
15
 */
16
class mysqli implements driver_interface
17
{
18
	/** @var \phpbb\db\driver\driver_interface */
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
	protected $db;
20
21
	/** @var string */
22
	protected $engine;
23
24
	/**
25
	 * Constructor
26
	 *
27
	 * @param \phpbb\db\driver\driver_interface $db
28
	 */
29
	public function __construct(\phpbb\db\driver\driver_interface $db)
30
	{
31
		$this->db = $db;
32
	}
33
34
	/**
35
	 * {@inheritdoc}
36
	 */
37
	public function get_name()
38
	{
39
		return 'mysqli';
40
	}
41
42
	/**
43
	 * {@inheritdoc}
44
	 */
45
	public function get_type()
46
	{
47
		return 'mysql';
48
	}
49
50
	/**
51
	 * {@inheritdoc}
52
	 */
53
	public function get_query($topic_id, $topic_title, $length, $sensitivity)
54
	{
55
		return array(
56
			'SELECT'	=> "f.forum_id, f.forum_name, t.*,
57
				MATCH (t.topic_title) AGAINST ('" . $this->db->sql_escape($topic_title) . "') AS score",
58
59
			'FROM'		=> array(
60
				TOPICS_TABLE	=> 't',
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\driver\TOPICS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
61
			),
62
			'LEFT_JOIN'	=> array(
63
				array(
64
					'FROM'	=>	array(FORUMS_TABLE	=> 'f'),
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\driver\FORUMS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
65
					'ON'	=> 'f.forum_id = t.forum_id',
66
				),
67
			),
68
			'WHERE'		=> "MATCH (t.topic_title) AGAINST ('" . $this->db->sql_escape($topic_title) . "') >= " . (float) $sensitivity . '
69
				AND t.topic_status <> ' . ITEM_MOVED . '
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\driver\ITEM_MOVED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
70
				AND t.topic_visibility = ' . ITEM_APPROVED . '
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\driver\ITEM_APPROVED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
71
				AND t.topic_time > (UNIX_TIMESTAMP() - ' . (int) $length . ')
72
				AND t.topic_id <> ' . (int) $topic_id,
73
		);
74
	}
75
76
	/**
77
	 * {@inheritdoc}
78
	 */
79
	public function is_supported()
80
	{
81
		return $this->is_mysql() && $this->supported_engine();
82
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 */
87
	public function is_fulltext($column = 'topic_title', $table = TOPICS_TABLE)
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\driver\TOPICS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
88
	{
89
		foreach ($this->get_fulltext_indexes($column, $table) as $index)
90
		{
91
			if ($index === $column)
92
			{
93
				return true;
94
			}
95
		}
96
97
		return false;
98
	}
99
100
	/**
101
	 * {@inheritdoc}
102
	 */
103
	public function get_fulltext_indexes($column = 'topic_title', $table = TOPICS_TABLE)
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\driver\TOPICS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
104
	{
105
		$indexes = array();
106
107
		if (!$this->is_supported())
108
		{
109
			return $indexes;
110
		}
111
112
		$sql = 'SHOW INDEX
113
			FROM ' . $this->db->sql_escape($table);
114
		$result = $this->db->sql_query($sql);
115
116
		while ($row = $this->db->sql_fetchrow($result))
117
		{
118
			// Older MySQL versions didn't use Index_type, so fallback to Comment
119
			$index_type = isset($row['Index_type']) ? $row['Index_type'] : $row['Comment'];
120
121
			if ($index_type === 'FULLTEXT' && $row['Key_name'] === $column)
122
			{
123
				$indexes[] = $row['Key_name'];
124
			}
125
		}
126
127
		$this->db->sql_freeresult($result);
128
129
		return $indexes;
130
	}
131
132
	/**
133
	 * {@inheritdoc}
134
	 */
135
	public function create_fulltext_index($column = 'topic_title', $table = TOPICS_TABLE)
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\driver\TOPICS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
136
	{
137
		if (!$this->is_fulltext($column, $table))
138
		{
139
			// First see if we need to update the table engine to support fulltext indexes
140
			if (!$this->is_supported())
141
			{
142
				$sql = 'ALTER TABLE ' . $this->db->sql_escape($table) . ' ENGINE = MYISAM';
143
				$this->db->sql_query($sql);
144
				$this->set_engine();
145
			}
146
147
			$sql = 'ALTER TABLE ' . $this->db->sql_escape($table) . '
148
				ADD FULLTEXT (' . $this->db->sql_escape($column) . ')';
149
			$this->db->sql_query($sql);
150
		}
151
	}
152
153
	/**
154
	 * {@inheritdoc}
155
	 */
156
	public function get_engine()
157
	{
158
		return $this->engine !== null ? $this->engine : $this->set_engine();
159
	}
160
161
	/**
162
	 * Set the database storage engine name
163
	 *
164
	 * @access protected
165
	 * @return string The storage engine name
166
	 */
167
	protected function set_engine()
168
	{
169
		$this->engine = '';
170
171
		if ($this->is_mysql())
172
		{
173
			$info = $this->get_table_info();
174
175
			// Modern MySQL uses 'Engine', but older may still use 'Type'
176
			foreach (array('Engine', 'Type') as $name)
177
			{
178
				if (isset($info[$name]))
179
				{
180
					$this->engine = strtolower($info[$name]);
181
					break;
182
				}
183
			}
184
		}
185
186
		return $this->engine;
187
	}
188
189
	/**
190
	 * Get topics table information
191
	 *
192
	 * @access protected
193
	 * @param string $table Name of the table
194
	 * @return mixed Array with the table info, false if the table does not exist
195
	 */
196
	protected function get_table_info($table = TOPICS_TABLE)
0 ignored issues
show
Bug introduced by
The constant vse\similartopics\driver\TOPICS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
197
	{
198
		$result = $this->db->sql_query("SHOW TABLE STATUS LIKE '" . $this->db->sql_escape($table) . "'");
199
		$info = $this->db->sql_fetchrow($result);
200
		$this->db->sql_freeresult($result);
201
202
		return $info;
203
	}
204
205
	/**
206
	 * Check if the database is using MySQL
207
	 *
208
	 * @access public
209
	 * @return bool True if is mysql, false otherwise
210
	 */
211
	protected function is_mysql()
212
	{
213
		return ($this->db->get_sql_layer() === 'mysql4' || $this->db->get_sql_layer() === 'mysqli');
214
	}
215
216
	/**
217
	 * Check if the database engine is supported.
218
	 * FULLTEXT is supported on MyISAM, and also on InnoDB as of MySQL 5.6.4 according
219
	 * to http://dev.mysql.com/doc/refman/5.6/en/innodb-storage-engine.html
220
	 *
221
	 * @return bool True if supported, false otherwise
222
	 */
223
	protected function supported_engine()
224
	{
225
		if ($this->get_engine() === 'myisam')
226
		{
227
			return true;
228
		}
229
230
		if ($this->get_engine() === 'innodb')
231
		{
232
			return phpbb_version_compare($this->db->sql_server_info(true), '5.6.4', '>=');
0 ignored issues
show
Bug introduced by
The function phpbb_version_compare was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

232
			return /** @scrutinizer ignore-call */ phpbb_version_compare($this->db->sql_server_info(true), '5.6.4', '>=');
Loading history...
233
		}
234
235
		return false;
236
	}
237
}
238