Issues (1686)

sources/database/Database.subs.php (8 issues)

1
<?php
2
3
/**
4
 * This file has all the main functions in it that set up the database connection
5
 * and initializes the appropriate adapters.
6
 *
7
 * @package   ElkArte Forum
8
 * @copyright ElkArte Forum contributors
9
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
10
 *
11
 * @version 2.0 dev
12
 *
13
 */
14
15
/**
16
 * Initialize database classes and connection.
17
 *
18
 * @param string $db_server server name
19
 * @param string $db_name database name like forum
20
 * @param string $db_user userid to attempt db connection
21
 * @param string $db_passwd password of user to attempt db connection
22
 * @param string $db_prefix prefix of the database, like elkarte_
23
 * @param mixed[] $db_options
24
 * @param string $db_type
25
 *
26
 * @return \ElkArte\Database\QueryInterface
27
 */
28
function elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql')
0 ignored issues
show
The parameter $db_user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

28
function elk_db_initiate($db_server, $db_name, /** @scrutinizer ignore-unused */ $db_user, $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql')

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

Loading history...
The parameter $db_server is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

28
function elk_db_initiate(/** @scrutinizer ignore-unused */ $db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql')

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

Loading history...
The parameter $db_name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

28
function elk_db_initiate($db_server, /** @scrutinizer ignore-unused */ $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql')

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

Loading history...
The parameter $db_options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

28
function elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, /** @scrutinizer ignore-unused */ $db_options = array(), $db_type = 'mysql')

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

Loading history...
The parameter $db_prefix is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

28
function elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, /** @scrutinizer ignore-unused */ $db_prefix, $db_options = array(), $db_type = 'mysql')

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

Loading history...
The parameter $db_type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

28
function elk_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array(), /** @scrutinizer ignore-unused */ $db_type = 'mysql')

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

Loading history...
The parameter $db_passwd is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

28
function elk_db_initiate($db_server, $db_name, $db_user, /** @scrutinizer ignore-unused */ $db_passwd, $db_prefix, $db_options = array(), $db_type = 'mysql')

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

Loading history...
29
{
30
	return database(false);
31
}
32
33
/**
34
 * Retrieve existing instance of the active database class.
35
 *
36
 * @param bool $fatal - Stop the execution or throw an \Exception
37
 * @param bool $force - Force the re-creation of the database instance.
38
 *                      If set to true, from that moment onwards the old
39
 *                      instance will be lost and only the new one returned
40
 *
41
 * @return \ElkArte\Database\QueryInterface
42
 * @throws \Exception if fatal is false
43
 */
44
function database($fatal = true, $force = false)
45
{
46
	static $db = null;
47 387
48
	if ($db === null || $force === true)
49 387
	{
50
		global $db_persist, $db_server, $db_user, $db_passwd, $db_port;
51 1
		global $db_type, $db_name, $db_prefix, $mysql_set_mode;
52 1
53
		$db_options = [
54
			'persist' => $db_persist,
55 1
			'select_db' => true,
56
			'port' => $db_port,
57 1
			'mysql_set_mode' => (bool) ($mysql_set_mode ?? false)
58 1
		];
59
		$type = strtolower($db_type);
60 1
		$type = $type === 'mysql' ? 'mysqli' : $type;
61 1
62 1
		/** @var \ElkArte\Database\Mysqli\Connection $class */
63
		$class = '\\ElkArte\\Database\\' . ucfirst($type) . '\\Connection';
64
		try
65 1
		{
66
			$db = $class::initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options);
67
		}
68
		catch (\Exception $e)
69
		{
70
			if ($fatal === true)
71
			{
72
				\ElkArte\Errors\Errors::instance()->display_db_error($e->getMessage());
0 ignored issues
show
The type ElkArte\Errors\Errors was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
73
			}
74
			else
75
			{
76
				throw $e;
77
			}
78
		}
79
	}
80 387
81
	return $db;
82
}
83
84
/**
85
 * This function retrieves an existing instance of \ElkArte\AbstractTable
86
 * and returns it.
87
 *
88
 * @param object|null $db - A database object (e.g. \ElkArte\Mysqli\Query)
89
 * @param bool $fatal - Stop the execution or throw an \Exception
90
 *
91
 * @return \ElkArte\Database\AbstractTable
92
 */
93
function db_table($db = null, $fatal = false)
94
{
95 3
	global $db_prefix, $db_type;
96 3
	static $db_table = null;
97
98 3
	if ($db_table === null)
99
	{
100 1
		if ($db === null)
101
		{
102 1
			$db = database();
103
		}
104 1
		$db_type = strtolower($db_type);
105 1
		$db_type = $db_type === 'mysql' ? 'mysqli' : $db_type;
106 1
		$class = '\\ElkArte\\Database\\' . ucfirst($db_type) . '\\Table';
107
		try
108
		{
109 1
			$db_table = new $class($db, $db_prefix);
110
		}
111
		catch (\Exception $e)
112
		{
113
			if ($fatal === true)
114
			{
115
				\ElkArte\Errors\Errors::instance()->display_db_error($e->getMessage());
116
			}
117
			else
118
			{
119
				throw $e;
120
			}
121
		}
122
	}
123
124 3
	return $db_table;
125
}
126
127
/**
128
 * This function returns an instance of \ElkArte\AbstractSearch,
129
 * specifically designed for database utilities related to search.
130
 *
131
 * @return \ElkArte\Database\AbstractSearch
132
 */
133
function db_search()
134
{
135
	global $db_type;
136 30
	static $db_search = null;
137 30
138
	if ($db_search === null)
139 30
	{
140
		$db = database();
141
		$db_type = strtolower($db_type);
142
		$db_type = $db_type === 'mysql' ? 'mysqli' : $db_type;
143
		$class = '\\ElkArte\\Database\\' . ucfirst($db_type) . '\\Search';
144
		try
145
		{
146
			$db_search = new $class($db);
147
		}
148
		catch (\Exception $e)
149
		{
150
			\ElkArte\Errors\Errors::instance()->display_db_error($e->getMessage());
151
		}
152
	}
153
154
	return $db_search;
155
}
156