]> git.mxchange.org Git - friendica.git/blob - include/dba.php
New function to detect heavily used indexes
[friendica.git] / include / dba.php
1 <?php
2 require_once("dbm.php");
3
4 # if PDO is avaible for mysql, use the new database abstraction
5 # TODO: PDO is disabled for release 3.3. We need to investigate why
6 # the update from 3.2 fails with pdo
7 /*
8 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
9   require_once("library/dddbl2/dddbl.php");
10   require_once("include/dba_pdo.php");
11 }
12 */
13
14
15 require_once('include/datetime.php');
16
17 /**
18  * @class MySQL database class
19  *
20  * For debugging, insert 'dbg(1);' anywhere in the program flow.
21  * dbg(0); will turn it off. Logging is performed at LOGGER_DATA level.
22  * When logging, all binary info is converted to text and html entities are escaped so that
23  * the debugging stream is safe to view within both terminals and web pages.
24  *
25  */
26
27 if (! class_exists('dba')) {
28 class dba {
29
30         private $debug = 0;
31         private $db;
32         private $result;
33         public  $mysqli = true;
34         public  $connected = false;
35         public  $error = false;
36
37         function __construct($server, $user, $pass, $db, $install = false) {
38                 $a = get_app();
39
40                 $stamp1 = microtime(true);
41
42                 $server = trim($server);
43                 $user = trim($user);
44                 $pass = trim($pass);
45                 $db = trim($db);
46
47                 if (!(strlen($server) && strlen($user))) {
48                         $this->connected = false;
49                         $this->db = null;
50                         return;
51                 }
52
53                 if ($install) {
54                         if (strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
55                                 if (! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
56                                         $this->error = sprintf( t('Cannot locate DNS info for database server \'%s\''), $server);
57                                         $this->connected = false;
58                                         $this->db = null;
59                                         return;
60                                 }
61                         }
62                 }
63
64                 if (class_exists('mysqli')) {
65                         $this->db = @new mysqli($server,$user,$pass,$db);
66                         if (! mysqli_connect_errno()) {
67                                 $this->connected = true;
68                         }
69                         if (isset($a->config["system"]["db_charset"])) {
70                                 $this->db->set_charset($a->config["system"]["db_charset"]);
71                         }
72                 } else {
73                         $this->mysqli = false;
74                         $this->db = mysql_connect($server,$user,$pass);
75                         if ($this->db && mysql_select_db($db,$this->db)) {
76                                 $this->connected = true;
77                         }
78                         if (isset($a->config["system"]["db_charset"]))
79                                 mysql_set_charset($a->config["system"]["db_charset"], $this->db);
80                 }
81                 if (!$this->connected) {
82                         $this->db = null;
83                         if (!$install) {
84                                 system_unavailable();
85                         }
86                 }
87
88                 $a->save_timestamp($stamp1, "network");
89         }
90
91         public function getdb() {
92                 return $this->db;
93         }
94
95         /**
96          * @brief Returns the MySQL server version string
97          * 
98          * This function discriminate between the deprecated mysql API and the current
99          * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
100          *
101          * @return string
102          */
103         public function server_info() {
104                 if ($this->mysqli) {
105                         $return = $this->db->server_info;
106                 } else {
107                         $return = mysql_get_server_info($this->db);
108                 }
109                 return $return;
110         }
111
112         /**
113          * @brief Returns the selected database name
114          *
115          * @return string
116          */
117         public function database_name() {
118                 $r = $this->q("SELECT DATABASE() AS `db`");
119
120                 return $r[0]['db'];
121         }
122
123         /**
124          * @brief Returns the number of rows
125          *
126          * @return integer
127          */
128         public function num_rows() {
129                 if (!$this->result) {
130                         return 0;
131                 }
132
133                 if ($this->mysqli) {
134                         $return = $this->result->num_rows;
135                 } else {
136                         $return = mysql_num_rows($this->result);
137                 }
138                 return $return;
139         }
140
141         public function log_index($query) {
142                 $a = get_app();
143
144                 if (($a->config["system"]["db_log_index"] == "") OR ($a->config["system"]["db_log_index_watch"] == "") OR
145                         (intval($a->config["system"]["db_loglimit_index"]) == 0)) {
146                         return;
147                 }
148
149                 if (strtolower(substr($query, 0, 7)) == "explain") {
150                         return;
151                 }
152
153                 $r = $this->q("EXPLAIN ".$query);
154                 if (!dbm::is_result($r)) {
155                         return;
156                 }
157
158                 $watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
159
160                 foreach ($r AS $row) {
161                         if (in_array($row['key'], $watchlist) AND
162                                 ($row['rows'] >= intval($a->config["system"]["db_loglimit_index"]))) {
163                                 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
164                                 @file_put_contents($a->config["system"]["db_log_index"], datetime_convert()."\t".
165                                                 $row['key']."\t".$row['rows']."\t".
166                                                 basename($backtrace[1]["file"])."\t".
167                                                 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
168                                                 substr($query, 0, 2000)."\n", FILE_APPEND);
169                         }
170                 }
171         }
172
173         public function q($sql, $onlyquery = false) {
174                 $a = get_app();
175
176                 if (!$this->db || !$this->connected) {
177                         return false;
178                 }
179
180                 $this->error = '';
181
182                 // Check the connection (This can reconnect the connection - if configured)
183                 if ($this->mysqli) {
184                         $connected = $this->db->ping();
185                 } else {
186                         $connected = mysql_ping($this->db);
187                 }
188                 $connstr = ($connected ? "Connected" : "Disonnected");
189
190                 $stamp1 = microtime(true);
191
192                 $orig_sql = $sql;
193
194                 if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
195                         $sql = "/*".$a->callstack()." */ ".$sql;
196                 }
197
198                 if ($this->mysqli) {
199                         $result = @$this->db->query($sql);
200                 } else {
201                         $result = @mysql_query($sql,$this->db);
202                 }
203                 $stamp2 = microtime(true);
204                 $duration = (float)($stamp2-$stamp1);
205
206                 $a->save_timestamp($stamp1, "database");
207
208                 if (strtolower(substr($orig_sql, 0, 6)) != "select") {
209                         $a->save_timestamp($stamp1, "database_write");
210                 }
211                 if (x($a->config,'system') && x($a->config['system'],'db_log')) {
212                         if (($duration > $a->config["system"]["db_loglimit"])) {
213                                 $duration = round($duration, 3);
214                                 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
215                                 @file_put_contents($a->config["system"]["db_log"], datetime_convert()."\t".$duration."\t".
216                                                 basename($backtrace[1]["file"])."\t".
217                                                 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
218                                                 substr($sql, 0, 2000)."\n", FILE_APPEND);
219                         }
220                 }
221
222                 if ($this->mysqli) {
223                         if ($this->db->errno) {
224                                 $this->error = $this->db->error;
225                                 $this->errorno = $this->db->errno;
226                         }
227                 } elseif (mysql_errno($this->db)) {
228                         $this->error = mysql_error($this->db);
229                         $this->errorno = mysql_errno($this->db);
230                 }
231
232                 if (strlen($this->error)) {
233                         logger('DB Error ('.$connstr.') '.$this->errorno.': '.$this->error);
234                 }
235
236                 if ($this->debug) {
237
238                         $mesg = '';
239
240                         if ($result === false) {
241                                 $mesg = 'false';
242                         } elseif ($result === true) {
243                                 $mesg = 'true';
244                         } else {
245                                 if ($this->mysqli) {
246                                         $mesg = $result->num_rows . ' results' . EOL;
247                                 } else {
248                                         $mesg = mysql_num_rows($result) . ' results' . EOL;
249                                 }
250                         }
251
252                         $str =  'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg
253                                 . (($this->error) ? ' error: ' . $this->error : '')
254                                 . EOL;
255
256                         logger('dba: ' . $str );
257                 }
258
259                 /**
260                  * If dbfail.out exists, we will write any failed calls directly to it,
261                  * regardless of any logging that may or may nor be in effect.
262                  * These usually indicate SQL syntax errors that need to be resolved.
263                  */
264
265                 if ($result === false) {
266                         logger('dba: ' . printable($sql) . ' returned false.' . "\n" . $this->error);
267                         if (file_exists('dbfail.out')) {
268                                 file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n" . $this->error . "\n", FILE_APPEND);
269                         }
270                 }
271
272                 if (($result === true) || ($result === false)) {
273                         return $result;
274                 }
275                 if ($onlyquery) {
276                         $this->result = $result;
277                         return true;
278                 }
279
280                 $r = array();
281                 if ($this->mysqli) {
282                         if ($result->num_rows) {
283                                 while($x = $result->fetch_array(MYSQLI_ASSOC))
284                                         $r[] = $x;
285                                 $result->free_result();
286                         }
287                 } else {
288                         if (mysql_num_rows($result)) {
289                                 while($x = mysql_fetch_array($result, MYSQL_ASSOC))
290                                         $r[] = $x;
291                                 mysql_free_result($result);
292                         }
293                 }
294
295                 //$a->save_timestamp($stamp1, "database");
296
297                 if ($this->debug) {
298                         logger('dba: ' . printable(print_r($r, true)));
299                 }
300                 return($r);
301         }
302
303         public function qfetch() {
304                 $x = false;
305
306                 if ($this->result) {
307                         if ($this->mysqli) {
308                                 if ($this->result->num_rows)
309                                         $x = $this->result->fetch_array(MYSQLI_ASSOC);
310                         } else {
311                                 if (mysql_num_rows($this->result))
312                                         $x = mysql_fetch_array($this->result, MYSQL_ASSOC);
313                         }
314                 }
315                 return($x);
316         }
317
318         public function qclose() {
319                 if ($this->result) {
320                         if ($this->mysqli) {
321                                 $this->result->free_result();
322                         } else {
323                                 mysql_free_result($this->result);
324                         }
325                 }
326         }
327
328         public function dbg($dbg) {
329                 $this->debug = $dbg;
330         }
331
332         public function escape($str) {
333                 if ($this->db && $this->connected) {
334                         if ($this->mysqli) {
335                                 return @$this->db->real_escape_string($str);
336                         } else {
337                                 return @mysql_real_escape_string($str,$this->db);
338                         }
339                 }
340         }
341
342         function connected() {
343                 if ($this->mysqli) {
344                         $connected = $this->db->ping();
345                 } else {
346                         $connected = mysql_ping($this->db);
347                 }
348                 return $connected;
349         }
350
351         function __destruct() {
352                 if ($this->db) {
353                         if ($this->mysqli) {
354                                 $this->db->close();
355                         } else {
356                                 mysql_close($this->db);
357                         }
358                 }
359         }
360 }}
361
362 if (! function_exists('printable')) {
363 function printable($s) {
364         $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
365         $s = str_replace("\x00",'.',$s);
366         if (x($_SERVER,'SERVER_NAME')) {
367                 $s = escape_tags($s);
368         }
369         return $s;
370 }}
371
372 // Procedural functions
373 if (! function_exists('dbg')) {
374 function dbg($state) {
375         global $db;
376         if ($db) {
377                 $db->dbg($state);
378         }
379 }}
380
381 if (! function_exists('dbesc')) {
382 function dbesc($str) {
383         global $db;
384         if ($db && $db->connected) {
385                 return($db->escape($str));
386         } else {
387                 return(str_replace("'","\\'",$str));
388         }
389 }}
390
391
392
393 // Function: q($sql,$args);
394 // Description: execute SQL query with printf style args.
395 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
396 //                   'user', 1);
397
398 if (! function_exists('q')) {
399 function q($sql) {
400
401         global $db;
402         $args = func_get_args();
403         unset($args[0]);
404
405         if ($db && $db->connected) {
406                 $stmt = @vsprintf($sql,$args); // Disabled warnings
407                 //logger("dba: q: $stmt", LOGGER_ALL);
408                 if ($stmt === false)
409                         logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
410
411                 $db->log_index($stmt);
412
413                 return $db->q($stmt);
414         }
415
416         /**
417          *
418          * This will happen occasionally trying to store the
419          * session data after abnormal program termination
420          *
421          */
422         logger('dba: no database: ' . print_r($args,true));
423         return false;
424
425 }}
426
427 /**
428  * @brief Performs a query with "dirty reads"
429  *
430  * By doing dirty reads (reading uncommitted data) no locks are performed
431  * This function can be used to fetch data that doesn't need to be reliable.
432  *
433  * @param $args Query parameters (1 to N parameters of different types)
434  * @return array Query array
435  */
436 function qu($sql) {
437
438         global $db;
439         $args = func_get_args();
440         unset($args[0]);
441
442         if ($db && $db->connected) {
443                 $stmt = @vsprintf($sql,$args); // Disabled warnings
444                 if ($stmt === false)
445                         logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
446
447                 $db->log_index($stmt);
448
449                 $db->q("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
450                 $retval = $db->q($stmt);
451                 $db->q("SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;");
452                 return $retval;
453         }
454
455         /**
456          *
457          * This will happen occasionally trying to store the
458          * session data after abnormal program termination
459          *
460          */
461         logger('dba: no database: ' . print_r($args,true));
462         return false;
463
464 }
465
466 /**
467  *
468  * Raw db query, no arguments
469  *
470  */
471
472 if (! function_exists('dbq')) {
473 function dbq($sql) {
474
475         global $db;
476         if ($db && $db->connected) {
477                 $ret = $db->q($sql);
478         } else {
479                 $ret = false;
480         }
481         return $ret;
482 }}
483
484
485 // Caller is responsible for ensuring that any integer arguments to
486 // dbesc_array are actually integers and not malformed strings containing
487 // SQL injection vectors. All integer array elements should be specifically
488 // cast to int to avoid trouble.
489
490
491 if (! function_exists('dbesc_array_cb')) {
492 function dbesc_array_cb(&$item, $key) {
493         if (is_string($item))
494                 $item = dbesc($item);
495 }}
496
497
498 if (! function_exists('dbesc_array')) {
499 function dbesc_array(&$arr) {
500         if (is_array($arr) && count($arr)) {
501                 array_walk($arr,'dbesc_array_cb');
502         }
503 }}
504
505
506 function dba_timer() {
507         return microtime(true);
508 }