]> git.mxchange.org Git - friendica.git/blob - src/Database/DBM.php
Class file relocations
[friendica.git] / src / Database / DBM.php
1 <?php
2 namespace Friendica\Database;
3
4 use dba;
5 /**
6  * @brief This class contain functions for the database management
7  *
8  * This class contains functions that doesn't need to know if pdo, mysqli or whatever is used.
9  */
10 class DBM {
11         /**
12          * @brief Return a list of database processes
13          *
14          * @return array
15          *      'list' => List of processes, separated in their different states
16          *      'amount' => Number of concurrent database processes
17          */
18         public static function processlist() {
19                 $r = q("SHOW PROCESSLIST");
20                 $s = array();
21
22                 $processes = 0;
23                 $states = array();
24                 foreach ($r AS $process) {
25                         $state = trim($process["State"]);
26
27                         // Filter out all non blocking processes
28                         if (!in_array($state, array("", "init", "statistics", "updating"))) {
29                                 ++$states[$state];
30                                 ++$processes;
31                         }
32                 }
33
34                 $statelist = "";
35                 foreach ($states AS $state => $usage) {
36                         if ($statelist != "")
37                                 $statelist .= ", ";
38                         $statelist .= $state.": ".$usage;
39                 }
40                 return(array("list" => $statelist, "amount" => $processes));
41         }
42
43         /**
44          * Checks if $array is a filled array with at least one entry.
45          *
46          * @param       $array  mixed   A filled array with at least one entry
47          * @return      Whether $array is a filled array or an object with rows
48          */
49         public static function is_result($array) {
50                 // It could be a return value from an update statement
51                 if (is_bool($array)) {
52                         return $array;
53                 }
54
55                 if (is_object($array)) {
56                         return dba::num_rows($array) > 0;
57                 }
58
59                 return (is_array($array) && (count($array) > 0));
60         }
61
62         /**
63          * @brief Callback function for "esc_array"
64          *
65          * @param mixed $value Array value
66          * @param string $key Array key
67          * @param boolean $add_quotation add quotation marks for string values
68          */
69         private static function esc_array_callback(&$value, $key, $add_quotation) {
70
71                 if (!$add_quotation) {
72                         if (is_bool($value)) {
73                                 $value = ($value ? '1' : '0');
74                         } else {
75                                 $value = dbesc($value);
76                         }
77                         return;
78                 }
79
80                 if (is_bool($value)) {
81                         $value = ($value ? 'true' : 'false');
82                 } elseif (is_float($value) || is_integer($value)) {
83                         $value = (string)$value;
84                 } else {
85                          $value = "'".dbesc($value)."'";
86                 }
87         }
88
89         /**
90          * @brief Escapes a whole array
91          *
92          * @param mixed $arr Array with values to be escaped
93          * @param boolean $add_quotation add quotation marks for string values
94          */
95         public static function esc_array(&$arr, $add_quotation = false) {
96                 array_walk($arr, 'self::esc_array_callback', $add_quotation);
97         }
98
99         /**
100          * Checks Converts any date string into a SQL compatible date string
101          *
102          * @param string $date a date string in any format
103          * @return string SQL style date string
104          */
105         public static function date($date = 'now') {
106                 $timestamp = strtotime($date);
107
108                 // Don't allow lower date strings as '0001-01-01 00:00:00'
109                 if ($timestamp < -62135596800) {
110                         $timestamp = -62135596800;
111                 }
112
113                 return date('Y-m-d H:i:s', (int)$timestamp);
114         }
115 }