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
|
|
|
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
|
|
|
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
|
99 |
|
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
|
1 |
|
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
|
2 |
|
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
|
4 |
|
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
|
3 |
|
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
|
|
|
* Sanitizes a string for use in a query |
146
|
|
|
* |
147
|
|
|
* @see Elgg\Database::sanitizeString |
148
|
|
|
* |
149
|
|
|
* @param string $string The string to sanitize |
150
|
|
|
* @return string |
151
|
|
|
* @deprecated Use query parameters where possible |
152
|
|
|
*/ |
153
|
|
|
function sanitize_string($string) { |
154
|
|
|
return _elgg_services()->db->sanitizeString($string); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Alias of sanitize_string |
159
|
|
|
* |
160
|
|
|
* @see Elgg\Database::sanitizeString |
161
|
|
|
* |
162
|
|
|
* @param string $string The string to sanitize |
163
|
|
|
* @return string |
164
|
|
|
* @deprecated Use query parameters where possible |
165
|
|
|
*/ |
166
|
|
|
function sanitise_string($string) { |
167
|
|
|
return _elgg_services()->db->sanitizeString($string); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Sanitizes an integer for database use. |
172
|
|
|
* |
173
|
|
|
* @see Elgg\Database::sanitizeInt |
174
|
|
|
* |
175
|
|
|
* @param int $int Value to be sanitized |
176
|
|
|
* @param bool $signed Whether negative values should be allowed (true) |
177
|
|
|
* @return int |
178
|
|
|
* @deprecated Use query parameters where possible |
179
|
|
|
*/ |
180
|
|
|
function sanitize_int($int, $signed = true) { |
181
|
|
|
return _elgg_services()->db->sanitizeInt($int, $signed); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Alias of sanitize_int |
186
|
|
|
* |
187
|
|
|
* @see sanitize_int |
188
|
|
|
* |
189
|
|
|
* @param int $int Value to be sanitized |
190
|
|
|
* @param bool $signed Whether negative values should be allowed (true) |
191
|
|
|
* @return int |
192
|
|
|
* @deprecated Use query parameters where possible |
193
|
|
|
*/ |
194
|
|
|
function sanitise_int($int, $signed = true) { |
195
|
|
|
return sanitize_int($int, $signed); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Enable the MySQL query cache |
200
|
|
|
* |
201
|
|
|
* @return void |
202
|
|
|
* |
203
|
|
|
* @since 2.0.0 |
204
|
|
|
*/ |
205
|
|
|
function elgg_enable_query_cache() { |
206
|
|
|
_elgg_services()->db->enableQueryCache(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Disable the MySQL query cache |
211
|
|
|
* |
212
|
|
|
* @note Elgg already manages the query cache sensibly, so you probably don't need to use this. |
213
|
|
|
* |
214
|
|
|
* @return void |
215
|
|
|
* |
216
|
|
|
* @since 2.0.0 |
217
|
|
|
*/ |
218
|
|
|
function elgg_disable_query_cache() { |
219
|
|
|
_elgg_services()->db->disableQueryCache(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Log db profiling information at NOTICE debug level upon shutdown. |
224
|
|
|
* |
225
|
|
|
* @return void |
226
|
|
|
* @access private |
227
|
|
|
*/ |
228
|
|
|
function _elgg_db_log_profiling_data() { |
229
|
|
|
$db_calls = _elgg_services()->db->getQueryCount(); |
230
|
|
|
|
231
|
|
|
// demoted to NOTICE as it corrupts javascript at DEBUG |
232
|
|
|
elgg_log("DB Queries for this page: $db_calls", 'INFO'); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Get a new query counter that will begin counting from 0. For profiling isolated |
237
|
|
|
* sections of code. |
238
|
|
|
* |
239
|
|
|
* <code> |
240
|
|
|
* $counter = _elgg_db_get_query_counter(); |
241
|
|
|
* |
242
|
|
|
* ... code to profile |
243
|
|
|
* |
244
|
|
|
* $counter->setDeltaHeader(); |
245
|
|
|
* </code> |
246
|
|
|
* |
247
|
|
|
* @return \Elgg\Database\QueryCounter |
248
|
|
|
* @access private |
249
|
|
|
*/ |
250
|
|
|
function _elgg_db_get_query_counter() { |
251
|
|
|
return _elgg_services()->queryCounter; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Execute any delayed queries upon shutdown. |
256
|
|
|
* |
257
|
|
|
* @return void |
258
|
|
|
* @access private |
259
|
|
|
*/ |
260
|
|
|
function _elgg_db_run_delayed_queries() { |
261
|
|
|
_elgg_services()->db->executeDelayedQueries(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Runs unit tests for the database |
266
|
|
|
* |
267
|
|
|
* @param string $hook unit_test |
268
|
|
|
* @param string $type system |
269
|
|
|
* @param array $value Array of tests |
270
|
|
|
* |
271
|
|
|
* @return array |
272
|
|
|
* @access private |
273
|
|
|
*/ |
274
|
|
|
function _elgg_db_test($hook, $type, $value) { |
|
|
|
|
275
|
|
|
$value[] = elgg_get_engine_path() . '/tests/simpletest/ElggDataFunctionsTest.php'; |
276
|
|
|
return $value; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Register database seeds |
281
|
|
|
* |
282
|
|
|
* @elgg_plugin_hook seeds database |
283
|
|
|
* |
284
|
|
|
* @param \Elgg\Hook $hook Hook |
285
|
|
|
* @return array |
286
|
|
|
*/ |
287
|
|
|
function _elgg_db_register_seeds(\Elgg\Hook $hook) { |
288
|
|
|
|
289
|
|
|
$seeds = $hook->getValue(); |
290
|
|
|
|
291
|
|
|
$seeds[] = \Elgg\Database\Seeds\Users::class; |
292
|
|
|
$seeds[] = \Elgg\Database\Seeds\Groups::class; |
293
|
|
|
|
294
|
|
|
return $seeds; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Registers shutdown functions for database profiling and delayed queries. |
299
|
|
|
* |
300
|
|
|
* @access private |
301
|
|
|
*/ |
302
|
|
|
function _elgg_db_init() { |
303
|
|
|
register_shutdown_function('_elgg_db_run_delayed_queries'); |
304
|
|
|
register_shutdown_function('_elgg_db_log_profiling_data'); |
305
|
|
|
elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_db_test'); |
306
|
|
|
elgg_register_plugin_hook_handler('seeds', 'database', '_elgg_db_register_seeds'); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @see \Elgg\Application::loadCore Do not do work here. Just register for events. |
311
|
|
|
*/ |
312
|
|
|
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) { |
|
|
|
|
313
|
|
|
$events->registerHandler('init', 'system', '_elgg_db_init'); |
314
|
|
|
}; |
315
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.