Completed
Push — development ( ac22bd...5236fa )
by Stephen
14:46
created

DbSearch_MySQL::skip_next_error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This class handles database search. (MySQL)
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * @version 1.1
11
 *
12
 */
13
14
/**
15
 * MySQL implementation of DbSearch
16
 */
17
class DbSearch_MySQL implements DbSearch
18
{
19
	/**
20
	 * This instance of the search
21
	 * @var DbSearch_MySQL
22
	 */
23
	private static $_search = null;
24
25
	/**
26
	 * The way to skip a database error
27
	 * @var boolean
28
	 */
29
	protected $_skip_error = false;
30
31
	/**
32
	 * This method will tell you whether this database type supports this search type.
33
	 *
34
	 * @param string $search_type
35
	 */
36
	public function search_support($search_type)
37
	{
38
		$supported_types = array('fulltext');
39
40
		return in_array($search_type, $supported_types);
41
	}
42
43
	/**
44
	 * Execute the appropriate query for the search.
45 33
	 *
46
	 * @param string $identifier
47 33
	 * @param string $db_string
48
	 * @param mixed[] $db_values
49
	 * @param resource|null $connection
50 33
	 */
51
	public function search_query($identifier, $db_string, $db_values = array(), $connection = null)
52
	{
53
		$db = database();
54
55
		if ($this->_skip_error === true)
56
		{
57
			$db->skip_next_error();
58
			$this->_skip_error = false;
59
		}
60
61
		// Simply delegate to the database adapter method.
62
		return $db->query($identifier, $db_string, $db_values, $connection);
63
	}
64
65
	/**
66
	 * {@inheritDoc}
67
	 */
68
	public function skip_next_error()
69
	{
70
		$this->_skip_error = true;
71
	}
72
73
	/**
74
	 * Returns some basic info about the {db_prefix}messages table
75
	 * Used in ManageSearch.controller.php in the page to select the index method
76
	 */
77
	public function membersTableInfo()
78
	{
79
		global $db_prefix;
80
81
		$db = database();
82
83
		$table_info = array();
84
85 View Code Duplication
		if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) !== 0)
86
			$request = $db->query('', '
87
				SHOW TABLE STATUS
88
				FROM {string:database_name}
89
				LIKE {string:table_name}',
90
				array(
91
					'database_name' => '`' . strtr($match[1], array('`' => '')) . '`',
92
					'table_name' => str_replace('_', '\_', $match[2]) . 'messages',
93
				)
94
			);
95
		else
96
			$request = $db->query('', '
97
				SHOW TABLE STATUS
98
				LIKE {string:table_name}',
99
				array(
100
					'table_name' => str_replace('_', '\_', $db_prefix) . 'messages',
101
				)
102
			);
103
104 View Code Duplication
		if ($request !== false && $db->num_rows($request) == 1)
105
		{
106
			// Only do this if the user has permission to execute this query.
107
			$row = $db->fetch_assoc($request);
108
			$table_info['data_length'] = $row['Data_length'];
109
			$table_info['index_length'] = $row['Index_length'];
110
			$table_info['fulltext_length'] = $row['Index_length'];
111
			$db->free_result($request);
112
		}
113
114
		// Now check the custom index table, if it exists at all.
115 View Code Duplication
		if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) !== 0)
116
			$request = $db->query('', '
117
				SHOW TABLE STATUS
118
				FROM {string:database_name}
119
				LIKE {string:table_name}',
120
				array(
121
					'database_name' => '`' . strtr($match[1], array('`' => '')) . '`',
122
					'table_name' => str_replace('_', '\_', $match[2]) . 'log_search_words',
123
				)
124
			);
125
		else
126
			$request = $db->query('', '
127
				SHOW TABLE STATUS
128
				LIKE {string:table_name}',
129
				array(
130
					'table_name' => str_replace('_', '\_', $db_prefix) . 'log_search_words',
131
				)
132
			);
133
134 View Code Duplication
		if ($request !== false && $db->num_rows($request) == 1)
135
		{
136
			// Only do this if the user has permission to execute this query.
137
			$row = $db->fetch_assoc($request);
138
			$table_info['index_length'] += $row['Data_length'] + $row['Index_length'];
139
			$table_info['custom_index_length'] = $row['Data_length'] + $row['Index_length'];
140
			$db->free_result($request);
141
		}
142
143
		return $table_info;
144
	}
145
146
	/**
147
	 * Method for the custom word index table.
148
	 *
149
	 * @param string $size
150
	 */
151
	public function create_word_search($size)
152
	{
153
		$db = database();
154
155
		if ($size == 'small')
156
			$size = 'smallint(5)';
157
		elseif ($size == 'medium')
158 33
			$size = 'mediumint(8)';
159
		else
160 33
			$size = 'int(10)';
161 22
162
		$db->query('', '
163
			CREATE TABLE {db_prefix}log_search_words (
164
				id_word {raw:size} unsigned NOT NULL default {string:string_zero},
165 33
				id_msg int(10) unsigned NOT NULL default {string:string_zero},
166
				PRIMARY KEY (id_word, id_msg)
167
			) ENGINE=InnoDB',
168
			array(
169
				'string_zero' => '0',
170
				'size' => $size,
171
			)
172
		);
173
	}
174
175
	/**
176
	 * Static method that allows to retrieve or create an instance of this class.
177
	 */
178
	public static function db_search()
179
	{
180
		if (is_null(self::$_search))
181
		{
182
			self::$_search = new self();
183
		}
184
185
		return self::$_search;
186
	}
187
}