Completed
Push — release-2.1 ( 6f6d35...abeae7 )
by Mathias
08:46
created

Sources/DbSearch-postgresql.php (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * This file contains database functions specific to search related activity.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines http://www.simplemachines.org
10
 * @copyright 2017 Simple Machines and individual contributors
11
 * @license http://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 Beta 4
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 *  Add the file functions to the $smcFunc array.
21
 */
22
function db_search_init()
23
{
24
	global $smcFunc;
25
26 View Code Duplication
	if (!isset($smcFunc['db_search_query']) || $smcFunc['db_search_query'] != 'smf_db_search_query')
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
		$smcFunc += array(
28
			'db_search_query' => 'smf_db_search_query',
29
			'db_search_support' => 'smf_db_search_support',
30
			'db_create_word_search' => 'smf_db_create_word_search',
31
			'db_support_ignore' => false,
32
			'db_search_language' => 'smf_db_search_language',
33
		);
34
35
	db_extend();
36
37
	//pg 9.5 got ignore support
38
	$version = $smcFunc['db_get_version']();
39
	// if we got a Beta Version
40 View Code Duplication
	if (stripos($version, 'beta') !== false)
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
		$version = substr($version, 0, stripos($version, 'beta')).'.0';
42
	// or RC
43 View Code Duplication
	if (stripos($version, 'rc') !== false)
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
		$version = substr($version, 0, stripos($version, 'rc')).'.0';
45
46
	if (version_compare($version,'9.5.0','>='))
47
		$smcFunc['db_support_ignore'] = true;
48
}
49
50
/**
51
 * This function will tell you whether this database type supports this search type.
52
 *
53
 * @param string $search_type The search type
54
 * @return boolean Whether or not the specified search type is supported by this DB system.
55
 */
56
function smf_db_search_support($search_type)
57
{
58
	$supported_types = array('custom','fulltext');
59
60
	return in_array($search_type, $supported_types);
61
}
62
63
/**
64
 * Returns the correct query for this search type.
65
 *
66
 * @param string $identifier A query identifier
67
 * @param string $db_string The query text
68
 * @param array $db_values An array of values to pass to $smcFunc['db_query']
69
 * @param resource $connection The current DB connection resource
70
 * @return resource The query result resource from $smcFunc['db_query']
71
 */
72
function smf_db_search_query($identifier, $db_string, $db_values = array(), $connection = null)
73
{
74
	global $smcFunc;
75
76
	$replacements = array(
77
		'create_tmp_log_search_topics' => array(
78
			'~ENGINE=MEMORY~i' => '',
79
		),
80
		'create_tmp_log_search_messages' => array(
81
			'~ENGINE=MEMORY~i' => '',
82
		),
83
		'insert_into_log_messages_fulltext' => array(
84
			'~LIKE~i' => 'iLIKE',
85
			'~NOT\sLIKE~i' => '~NOT iLIKE',
86
			'~NOT\sRLIKE~i' => '!~*',
87
			'~RLIKE~i' => '~*',
88
		),
89
		'insert_log_search_results_subject' => array(
90
			'~LIKE~i' => 'iLIKE',
91
			'~NOT\sLIKE~i' => 'NOT iLIKE',
92
			'~NOT\sRLIKE~i' => '!~*',
93
			'~RLIKE~i' => '~*',
94
		),
95
	);
96
97 View Code Duplication
	if (isset($replacements[$identifier]))
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
		$db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string);
99
	if (preg_match('~^\s*INSERT\sIGNORE~i', $db_string) != 0)
100
	{
101
		$db_string = preg_replace('~^\s*INSERT\sIGNORE~i', 'INSERT', $db_string);
102
		if ($smcFunc['db_support_ignore']){
103
			//pg style "INSERT INTO.... ON CONFLICT DO NOTHING"
104
			$db_string = $db_string.' ON CONFLICT DO NOTHING';
105
		}
106
		else
107
		{
108
			// Don't error on multi-insert.
109
			$db_values['db_error_skip'] = true;
110
		}
111
	}
112
113
	//fix double quotes
114
	if ($identifier == 'insert_into_log_messages_fulltext')
115
		$db_values = str_replace('"', "'", $db_values);
116
117
	$return = $smcFunc['db_query']('', $db_string,
118
		$db_values, $connection
119
	);
120
121
	return $return;
122
}
123
124
/**
125
 * Highly specific function, to create the custom word index table.
126
 *
127
 * @param string $size The column size type (int, mediumint (8), etc.). Not used here.
128
 */
129
function smf_db_create_word_search($size)
0 ignored issues
show
The function smf_db_create_word_search() has been defined more than once; this definition is ignored, only the first definition in Sources/DbSearch-mysql.php (L53-75) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
The parameter $size is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
{
131
	global $smcFunc;
132
133
	$size = 'int';
134
135
	$smcFunc['db_query']('', '
136
		CREATE TABLE {db_prefix}log_search_words (
137
			id_word {raw:size} NOT NULL default {string:string_zero},
138
			id_msg int NOT NULL default {string:string_zero},
139
			PRIMARY KEY (id_word, id_msg)
140
		)',
141
		array(
142
			'size' => $size,
143
			'string_zero' => '0',
144
		)
145
	);
146
}
147
148
/**
149
* Return the language for the textsearch index
150
*/
151
function smf_db_search_language()
152
{
153
	global $smcFunc, $modSettings;
154
155
	$language_ftx = 'english';
156
157
	if (!empty($modSettings['search_language']))
158
		$language_ftx = $modSettings['search_language'];
159
	else
160
	{
161
		$request = $smcFunc['db_query']('','
162
			SELECT cfgname FROM pg_ts_config WHERE oid = current_setting({string:default_language})::regconfig',
163
			array(
164
			'default_language' => 'default_text_search_config'
165
			)
166
		);
167
168
		if ($request !== false && $smcFunc['db_num_rows']($request) == 1)
169
		{
170
			$row = $smcFunc['db_fetch_assoc']($request);
171
			$language_ftx = $row['cfgname'];
172
173
			$smcFunc['db_insert']('replace',
174
				'{db_prefix}settings',
175
				array('variable' => 'string', 'value' => 'string'),
176
				array('search_language', $language_ftx),
177
				array('variable')
178
			);
179
		}
180
	}
181
182
	return $language_ftx;
183
}
184
185
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...