Completed
Pull Request — patch_1-1-4 (#3191)
by Emanuele
11:20
created

DbSearch_MySQL::search_query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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