|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Elgg database procedural code. |
|
4
|
|
|
* |
|
5
|
|
|
* Includes functions for reading data, writing data, and escaping queries. |
|
6
|
|
|
* |
|
7
|
|
|
* @package Elgg.Core |
|
8
|
|
|
* @subpackage Database |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Queue a query for running during shutdown that writes to the database |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $query The query to execute |
|
15
|
|
|
* @param callable $callback The optional callback for processing. The callback will receive a |
|
16
|
|
|
* \Doctrine\DBAL\Driver\Statement object |
|
17
|
|
|
* @param array $params Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve'] |
|
18
|
|
|
* |
|
19
|
|
|
* @return boolean |
|
20
|
|
|
*/ |
|
21
|
|
|
function execute_delayed_write_query($query, $callback = null, array $params = []) { |
|
22
|
426 |
|
return _elgg_services()->db->registerDelayedQuery($query, 'write', $callback, $params); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Queue a query for running during shutdown that reads from the database |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $query The query to execute |
|
29
|
|
|
* @param callable $callback The optional callback for processing. The callback will receive a |
|
30
|
|
|
* \Doctrine\DBAL\Driver\Statement object |
|
31
|
|
|
* @param array $params Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve'] |
|
32
|
|
|
* |
|
33
|
|
|
* @return boolean |
|
34
|
|
|
*/ |
|
35
|
|
|
function execute_delayed_read_query($query, $callback = null, array $params = []) { |
|
36
|
1 |
|
return _elgg_services()->db->registerDelayedQuery($query, 'read', $callback, $params); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Retrieve rows from the database. |
|
41
|
|
|
* |
|
42
|
|
|
* Queries are executed with {@link \Elgg\Database::getResults} and results |
|
43
|
|
|
* are retrieved with {@link \PDO::fetchObject()}. If a callback |
|
44
|
|
|
* function $callback is defined, each row will be passed as the single |
|
45
|
|
|
* argument to $callback. If no callback function is defined, the |
|
46
|
|
|
* entire result set is returned as an array. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $query The query being passed. |
|
49
|
|
|
* @param callable $callback Optionally, the name of a function to call back to on each row |
|
50
|
|
|
* @param array $params Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve'] |
|
51
|
|
|
* |
|
52
|
|
|
* @return array An array of database result objects or callback function results. If the query |
|
53
|
|
|
* returned nothing, an empty array. |
|
54
|
|
|
*/ |
|
55
|
|
|
function get_data($query, $callback = null, array $params = []) { |
|
56
|
10 |
|
return _elgg_services()->db->getData($query, $callback, $params); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Retrieve a single row from the database. |
|
61
|
|
|
* |
|
62
|
|
|
* Similar to {@link get_data()} but returns only the first row |
|
63
|
|
|
* matched. If a callback function $callback is specified, the row will be passed |
|
64
|
|
|
* as the only argument to $callback. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $query The query to execute. |
|
67
|
|
|
* @param callable $callback A callback function to apply to the row |
|
68
|
|
|
* @param array $params Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve'] |
|
69
|
|
|
* |
|
70
|
|
|
* @return mixed A single database result object or the result of the callback function. |
|
71
|
|
|
*/ |
|
72
|
|
|
function get_data_row($query, $callback = null, array $params = []) { |
|
73
|
9 |
|
return _elgg_services()->db->getDataRow($query, $callback, $params); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Insert a row into the database. |
|
78
|
|
|
* |
|
79
|
|
|
* @note Altering the DB invalidates all queries in the query cache |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $query The query to execute. |
|
82
|
|
|
* @param array $params Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve'] |
|
83
|
|
|
* |
|
84
|
|
|
* @return int|false The database id of the inserted row if a AUTO_INCREMENT field is |
|
85
|
|
|
* defined, 0 if not, and false on failure. |
|
86
|
|
|
*/ |
|
87
|
|
|
function insert_data($query, array $params = []) { |
|
88
|
3 |
|
return _elgg_services()->db->insertData($query, $params); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Update a row in the database. |
|
93
|
|
|
* |
|
94
|
|
|
* @note Altering the DB invalidates all queries in the query cache |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $query The query to run. |
|
97
|
|
|
* @param array $params Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve'] |
|
98
|
|
|
* @param bool $get_num_rows Return the number of rows affected (default: false). |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
function update_data($query, array $params = [], $get_num_rows = false) { |
|
103
|
15 |
|
return _elgg_services()->db->updateData($query, $get_num_rows, $params); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Remove a row from the database. |
|
108
|
|
|
* |
|
109
|
|
|
* @note Altering the DB invalidates all queries in the query cache |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $query The SQL query to run |
|
112
|
|
|
* @param array $params Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve'] |
|
113
|
|
|
* |
|
114
|
|
|
* @return int|false The number of affected rows or false on failure |
|
115
|
|
|
*/ |
|
116
|
|
|
function delete_data($query, array $params = []) { |
|
117
|
4 |
|
return _elgg_services()->db->deleteData($query, $params); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Runs a full database script from disk. |
|
122
|
|
|
* |
|
123
|
|
|
* The file specified should be a standard SQL file as created by |
|
124
|
|
|
* mysqldump or similar. Statements must be terminated with ; |
|
125
|
|
|
* and a newline character (\n or \r\n) with only one statement per line. |
|
126
|
|
|
* |
|
127
|
|
|
* The special string 'prefix_' is replaced with the database prefix |
|
128
|
|
|
* as defined in {@link $CONFIG->dbprefix}. |
|
129
|
|
|
* |
|
130
|
|
|
* @warning Errors do not halt execution of the script. If a line |
|
131
|
|
|
* generates an error, the error message is saved and the |
|
132
|
|
|
* next line is executed. After the file is run, any errors |
|
133
|
|
|
* are displayed as a {@link DatabaseException} |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $scriptlocation The full path to the script |
|
136
|
|
|
* |
|
137
|
|
|
* @return void |
|
138
|
|
|
* @throws DatabaseException |
|
139
|
|
|
*/ |
|
140
|
|
|
function run_sql_script($scriptlocation) { |
|
141
|
|
|
_elgg_services()->db->runSqlScript($scriptlocation); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Enable the MySQL query cache |
|
146
|
|
|
* |
|
147
|
|
|
* @return void |
|
148
|
|
|
* |
|
149
|
|
|
* @since 2.0.0 |
|
150
|
|
|
*/ |
|
151
|
|
|
function elgg_enable_query_cache() { |
|
152
|
|
|
_elgg_services()->db->enableQueryCache(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Disable the MySQL query cache |
|
157
|
|
|
* |
|
158
|
|
|
* @note Elgg already manages the query cache sensibly, so you probably don't need to use this. |
|
159
|
|
|
* |
|
160
|
|
|
* @return void |
|
161
|
|
|
* |
|
162
|
|
|
* @since 2.0.0 |
|
163
|
|
|
*/ |
|
164
|
|
|
function elgg_disable_query_cache() { |
|
165
|
|
|
_elgg_services()->db->disableQueryCache(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Log db profiling information at NOTICE debug level upon shutdown. |
|
170
|
|
|
* |
|
171
|
|
|
* @return void |
|
172
|
|
|
* @access private |
|
173
|
|
|
*/ |
|
174
|
|
|
function _elgg_db_log_profiling_data() { |
|
175
|
|
|
if (!_elgg_services()->db) { |
|
176
|
|
|
return; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$db_calls = _elgg_services()->db->getQueryCount(); |
|
180
|
|
|
|
|
181
|
|
|
// demoted to NOTICE as it corrupts javascript at DEBUG |
|
182
|
|
|
elgg_log("DB Queries for this page: $db_calls", 'INFO'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get a new query counter that will begin counting from 0. For profiling isolated |
|
187
|
|
|
* sections of code. |
|
188
|
|
|
* |
|
189
|
|
|
* <code> |
|
190
|
|
|
* $counter = _elgg_db_get_query_counter(); |
|
191
|
|
|
* |
|
192
|
|
|
* ... code to profile |
|
193
|
|
|
* |
|
194
|
|
|
* $counter->setDeltaHeader(); |
|
195
|
|
|
* </code> |
|
196
|
|
|
* |
|
197
|
|
|
* @return \Elgg\Database\QueryCounter |
|
198
|
|
|
* @access private |
|
199
|
|
|
*/ |
|
200
|
|
|
function _elgg_db_get_query_counter() { |
|
201
|
|
|
return _elgg_services()->queryCounter; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Execute any delayed queries upon shutdown. |
|
206
|
|
|
* |
|
207
|
|
|
* @return void |
|
208
|
|
|
* @access private |
|
209
|
|
|
*/ |
|
210
|
|
|
function _elgg_db_run_delayed_queries() { |
|
211
|
|
|
if (!_elgg_services()->db) { |
|
212
|
|
|
return; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
_elgg_services()->db->executeDelayedQueries(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Runs unit tests for the database |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $hook 'unit_test' |
|
222
|
|
|
* @param string $type 'system' |
|
223
|
|
|
* @param array $value Array of tests |
|
224
|
|
|
* |
|
225
|
|
|
* @return array |
|
226
|
|
|
* @access private |
|
227
|
|
|
* @codeCoverageIgnore |
|
228
|
|
|
*/ |
|
229
|
|
|
function _elgg_db_test($hook, $type, $value) { |
|
|
|
|
|
|
230
|
|
|
$value[] = ElggDataFunctionsTest::class; |
|
231
|
|
|
return $value; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Register database seeds |
|
236
|
|
|
* |
|
237
|
|
|
* @elgg_plugin_hook seeds database |
|
238
|
|
|
* |
|
239
|
|
|
* @param \Elgg\Hook $hook Hook |
|
240
|
|
|
* @return array |
|
241
|
|
|
*/ |
|
242
|
|
|
function _elgg_db_register_seeds(\Elgg\Hook $hook) { |
|
243
|
|
|
|
|
244
|
|
|
$seeds = $hook->getValue(); |
|
245
|
|
|
|
|
246
|
|
|
$seeds[] = \Elgg\Database\Seeds\Users::class; |
|
247
|
|
|
$seeds[] = \Elgg\Database\Seeds\Groups::class; |
|
248
|
|
|
|
|
249
|
|
|
return $seeds; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Registers shutdown functions for database profiling and delayed queries. |
|
254
|
|
|
* |
|
255
|
|
|
* @return void |
|
256
|
|
|
* |
|
257
|
|
|
* @access private |
|
258
|
|
|
*/ |
|
259
|
|
|
function _elgg_db_init() { |
|
260
|
31 |
|
elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_db_test'); |
|
261
|
31 |
|
elgg_register_plugin_hook_handler('seeds', 'database', '_elgg_db_register_seeds'); |
|
262
|
31 |
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @see \Elgg\Application::loadCore Do not do work here. Just register for events. |
|
266
|
|
|
*/ |
|
267
|
|
|
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) { |
|
|
|
|
|
|
268
|
18 |
|
$events->registerHandler('init', 'system', '_elgg_db_init'); |
|
269
|
|
|
}; |
|
270
|
|
|
|