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

mysqli::is_mysql()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 2
nc 2
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
	 * Check if the database is using MySQL
75
	 *
76
	 * @access public
77
	 * @return bool True if is mysql, false otherwise
78
	 */
79
	public function is_mysql()
80
	{
81
		return ($this->db->get_sql_layer() === 'mysql4' || $this->db->get_sql_layer() === 'mysqli');
82
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 */
87
	public function is_supported()
88
	{
89
		// FULLTEXT is supported on InnoDB since MySQL 5.6.4 according to
90
		// http://dev.mysql.com/doc/refman/5.6/en/innodb-storage-engine.html
91
		return $this->is_mysql() && ($this->get_engine() === 'myisam' || ($this->get_engine() === 'innodb' && phpbb_version_compare($this->db->sql_server_info(true), '5.6.4', '>=')));
92
	}
93
94
	/**
95
	 * {@inheritdoc}
96
	 */
97
	public function is_index($field = 'topic_title')
98
	{
99
		$is_index = false;
100
101
		$sql = 'SHOW INDEX
102
			FROM ' . TOPICS_TABLE;
103
		$result = $this->db->sql_query($sql);
104
105
		while ($row = $this->db->sql_fetchrow($result))
106
		{
107
			// Older MySQL versions didn't use Index_type, so fallback to Comment
108
			$index_type = isset($row['Index_type']) ? $row['Index_type'] : $row['Comment'];
109
110
			if ($index_type === 'FULLTEXT' && $row['Key_name'] === $field)
111
			{
112
				$is_index = true;
113
				break;
114
			}
115
		}
116
117
		$this->db->sql_freeresult($result);
118
119
		return $is_index;
120
	}
121
122
	/**
123
	 * {@inheritdoc}
124
	 */
125
	public function create_fulltext_index($field = 'topic_title')
126
	{
127 View Code Duplication
		if (!$this->is_index($field))
128
		{
129
			$sql = 'ALTER TABLE ' . TOPICS_TABLE . ' ADD FULLTEXT (' . $field . ')';
130
			$this->db->sql_query($sql);
131
		}
132
	}
133
134
	/**
135
	 * Get the database storage engine name
136
	 *
137
	 * @access public
138
	 * @return string The storage engine name
139
	 */
140
	public function get_engine()
141
	{
142
		return isset($this->engine) ? $this->engine : $this->set_engine();
143
	}
144
145
	/**
146
	 * Set the database storage engine name
147
	 *
148
	 * @access public
149
	 * @return string The storage engine name
150
	 */
151
	public function set_engine()
152
	{
153
		$this->engine = '';
154
155
		if ($this->is_mysql())
156
		{
157
			$info = $this->get_table_info();
158
159
			// Modern MySQL uses 'Engine', but older may still use 'Type'
160
			foreach (array('Engine', 'Type') as $name)
161
			{
162
				if (isset($info[$name]))
163
				{
164
					$this->engine = strtolower($info[$name]);
165
					break;
166
				}
167
			}
168
		}
169
170
		return $this->engine;
171
	}
172
173
	/**
174
	 * Alter the database storage engine
175
	 *
176
	 * @access public
177
	 * @param string $engine The storage engine, i.e.: MYISAM|INNODB
178
	 * @return void
179
	 */
180
	public function alter_engine($engine = 'MYISAM')
181
	{
182
		// Alter the storage engine
183
		$sql = 'ALTER TABLE ' . TOPICS_TABLE . ' ENGINE = ' . $this->db->sql_escape(strtoupper($engine));
184
		$this->db->sql_query($sql);
185
	}
186
187
	/**
188
	 * Get topics table information
189
	 *
190
	 * @access protected
191
	 * @return mixed Array with the table info, false if the table does not exist
192
	 */
193
	protected function get_table_info()
194
	{
195
		$result = $this->db->sql_query('SHOW TABLE STATUS LIKE \'' . TOPICS_TABLE . '\'');
196
		$info = $this->db->sql_fetchrow($result);
197
		$this->db->sql_freeresult($result);
198
199
		return $info;
200
	}
201
}
202