Completed
Push — develop-3.2.x ( 87a321...45a2bf )
by Matt
04:26 queued 01:12
created

fulltext_support::is_index()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 12
nc 5
nop 1
1
<?php
2
/**
3
 *
4
 * Precise Similar Topics
5
 *
6
 * @copyright (c) 2014 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\similartopics\core;
12
13
class fulltext_support
14
{
15
	/** @var \phpbb\db\driver\driver_interface */
16
	protected $db;
17
18
	/** @var string */
19
	protected $engine;
20
21
	/**
22
	 * Constructor
23
	 *
24
	 * @access public
25
	 * @param  \phpbb\db\driver\driver_interface
26
	 */
27
	public function __construct(\phpbb\db\driver\driver_interface $db)
28
	{
29
		$this->db = $db;
30
	}
31
32
	/**
33
	 * Check if the database is using MySQL
34
	 *
35
	 * @access public
36
	 * @return bool True if is mysql, false otherwise
37
	 */
38
	public function is_mysql()
39
	{
40
		return ($this->db->get_sql_layer() === 'mysql4' || $this->db->get_sql_layer() === 'mysqli');
41
	}
42
43
	/**
44
	 * Check for FULLTEXT index support
45
	 *
46
	 * @access public
47
	 * @return bool True if FULLTEXT is supported, false otherwise
48
	 */
49
	public function is_supported()
50
	{
51
		// FULLTEXT is supported on InnoDB since MySQL 5.6.4 according to
52
		// http://dev.mysql.com/doc/refman/5.6/en/innodb-storage-engine.html
53
		return ($this->get_engine() === 'myisam' || ($this->get_engine() === 'innodb' && phpbb_version_compare($this->db->sql_server_info(true), '5.6.4', '>=')));
54
	}
55
56
	/**
57
	 * Get the database storage engine name
58
	 *
59
	 * @access public
60
	 * @return string The storage engine name
61
	 */
62
	public function get_engine()
63
	{
64
		return isset($this->engine) ? $this->engine : $this->set_engine();
65
	}
66
67
	/**
68
	 * Set the database storage engine name
69
	 *
70
	 * @access public
71
	 * @return string The storage engine name
72
	 */
73
	public function set_engine()
74
	{
75
		$this->engine = '';
76
77
		if ($this->is_mysql())
78
		{
79
			$info = $this->get_table_info();
80
81
			// Modern MySQL uses 'Engine', but older may still use 'Type'
82
			foreach (array('Engine', 'Type') as $name)
83
			{
84
				if (isset($info[$name]))
85
				{
86
					$this->engine = strtolower($info[$name]);
87
					break;
88
				}
89
			}
90
		}
91
92
		return $this->engine;
93
	}
94
95
	/**
96
	 * Check if a field is a FULLTEXT index
97
	 *
98
	 * @access public
99
	 * @param string $field name of a field
100
	 * @return bool True if field is a FULLTEXT index, false otherwise
101
	 */
102
	public function is_index($field = 'topic_title')
103
	{
104
		$is_index = false;
105
106
		$sql = 'SHOW INDEX
107
			FROM ' . TOPICS_TABLE;
108
		$result = $this->db->sql_query($sql);
109
110
		while ($row = $this->db->sql_fetchrow($result))
111
		{
112
			// Older MySQL versions didn't use Index_type, so fallback to Comment
113
			$index_type = isset($row['Index_type']) ? $row['Index_type'] : $row['Comment'];
114
115
			if ($index_type === 'FULLTEXT' && $row['Key_name'] === $field)
116
			{
117
				$is_index = true;
118
				break;
119
			}
120
		}
121
122
		$this->db->sql_freeresult($result);
123
124
		return $is_index;
125
	}
126
127
	/**
128
	 * Get topics table information
129
	 *
130
	 * @access protected
131
	 * @return mixed Array with the table info, false if the table does not exist
132
	 */
133
	protected function get_table_info()
134
	{
135
		$result = $this->db->sql_query('SHOW TABLE STATUS LIKE \'' . TOPICS_TABLE . '\'');
136
		$info = $this->db->sql_fetchrow($result);
137
		$this->db->sql_freeresult($result);
138
139
		return $info;
140
	}
141
}
142