Completed
Pull Request — master (#25)
by Matt
02:10
created

mysqli::get_name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
class mysqli implements driver_interface
14
{
15
	/** @var \phpbb\db\driver\driver_interface */
16
	protected $db;
17
18
	/** @var string */
19
	protected $engine;
20
21
	/**
22
	 * mysql constructor.
23
	 *
24
	 * @param \phpbb\db\driver\driver_interface $db
25
	 */
26
	public function __construct(\phpbb\db\driver\driver_interface $db)
27
	{
28
		$this->db = $db;
29
	}
30
31
	/**
32
	 * {@inheritdoc}
33
	 */
34
	public function get_name()
35
	{
36
		return 'mysqli';
37
	}
38
39
	/**
40
	 * {@inheritdoc}
41
	 */
42
	public function get_type()
43
	{
44
		return 'mysql';
45
	}
46
47
	/**
48
	 * {@inheritdoc}
49
	 */
50
	public function get_query($topic_id, $topic_title, $length, $sensitivity)
51
	{
52
		return array(
53
			'SELECT'	=> "f.forum_id, f.forum_name, t.*,
54
				MATCH (t.topic_title) AGAINST ('" . $this->db->sql_escape($topic_title) . "') AS score",
55
56
			'FROM'		=> array(
57
				TOPICS_TABLE	=> 't',
58
			),
59
			'LEFT_JOIN'	=> array(
60
				array(
61
					'FROM'	=>	array(FORUMS_TABLE	=> 'f'),
62
					'ON'	=> 'f.forum_id = t.forum_id',
63
				),
64
			),
65
			'WHERE'		=> "MATCH (t.topic_title) AGAINST ('" . $this->db->sql_escape($topic_title) . "') >= " . (float) $sensitivity . '
66
				AND t.topic_status <> ' . ITEM_MOVED . '
67
				AND t.topic_visibility = ' . ITEM_APPROVED . '
68
				AND t.topic_time > (UNIX_TIMESTAMP() - ' . (int) $length . ')
69
				AND t.topic_id <> ' . (int) $topic_id,
70
		);
71
	}
72
73
	/**
74
	 * {@inheritdoc}
75
	 */
76
	public function is_supported()
77
	{
78
		// FULLTEXT is supported on InnoDB since MySQL 5.6.4 according to
79
		// http://dev.mysql.com/doc/refman/5.6/en/innodb-storage-engine.html
80
		return $this->is_mysql() && ($this->get_engine() === 'myisam' || ($this->get_engine() === 'innodb' && phpbb_version_compare($this->db->sql_server_info(true), '5.6.4', '>=')));
81
	}
82
83
	/**
84
	 * {@inheritdoc}
85
	 */
86
	public function is_index($column = 'topic_title')
87
	{
88
		$is_index = false;
89
90
		$sql = 'SHOW INDEX
91
			FROM ' . TOPICS_TABLE;
92
		$result = $this->db->sql_query($sql);
93
94
		while ($row = $this->db->sql_fetchrow($result))
95
		{
96
			// Older MySQL versions didn't use Index_type, so fallback to Comment
97
			$index_type = isset($row['Index_type']) ? $row['Index_type'] : $row['Comment'];
98
99
			if ($index_type === 'FULLTEXT' && $row['Key_name'] === $column)
100
			{
101
				$is_index = true;
102
				break;
103
			}
104
		}
105
106
		$this->db->sql_freeresult($result);
107
108
		return $is_index;
109
	}
110
111
	/**
112
	 * {@inheritdoc}
113
	 */
114
	public function create_fulltext_index($column = 'topic_title')
115
	{
116 View Code Duplication
		if (!$this->is_index($column))
117
		{
118
			$sql = 'ALTER TABLE ' . TOPICS_TABLE . ' ADD FULLTEXT (' . $column . ')';
119
			$this->db->sql_query($sql);
120
		}
121
	}
122
123
	/**
124
	 * Get the database storage engine name
125
	 *
126
	 * @access public
127
	 * @return string The storage engine name
128
	 */
129
	public function get_engine()
130
	{
131
		return $this->engine !== null ? $this->engine : $this->set_engine();
132
	}
133
134
	/**
135
	 * Set the database storage engine name
136
	 *
137
	 * @access public
138
	 * @return string The storage engine name
139
	 */
140
	public function set_engine()
141
	{
142
		$this->engine = '';
143
144
		if ($this->is_mysql())
145
		{
146
			$info = $this->get_table_info();
147
148
			// Modern MySQL uses 'Engine', but older may still use 'Type'
149
			foreach (array('Engine', 'Type') as $name)
150
			{
151
				if (isset($info[$name]))
152
				{
153
					$this->engine = strtolower($info[$name]);
154
					break;
155
				}
156
			}
157
		}
158
159
		return $this->engine;
160
	}
161
162
	/**
163
	 * Alter the database storage engine
164
	 *
165
	 * @access public
166
	 * @param string $engine The storage engine, i.e.: MYISAM|INNODB
167
	 * @return void
168
	 */
169
	public function alter_engine($engine = 'MYISAM')
170
	{
171
		$sql = 'ALTER TABLE ' . TOPICS_TABLE . ' ENGINE = ' . $this->db->sql_escape(strtoupper($engine));
172
		$this->db->sql_query($sql);
173
		$this->set_engine();
174
	}
175
176
	/**
177
	 * Get topics table information
178
	 *
179
	 * @access protected
180
	 * @return mixed Array with the table info, false if the table does not exist
181
	 */
182
	protected function get_table_info()
183
	{
184
		$result = $this->db->sql_query('SHOW TABLE STATUS LIKE \'' . TOPICS_TABLE . '\'');
185
		$info = $this->db->sql_fetchrow($result);
186
		$this->db->sql_freeresult($result);
187
188
		return $info;
189
	}
190
191
	/**
192
	 * Check if the database is using MySQL
193
	 *
194
	 * @access public
195
	 * @return bool True if is mysql, false otherwise
196
	 */
197
	protected function is_mysql()
198
	{
199
		return ($this->db->get_sql_layer() === 'mysql4' || $this->db->get_sql_layer() === 'mysqli');
200
	}
201
}
202