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