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