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