]> git.mxchange.org Git - friendica.git/blob - src/Database/DBM.php
Function call
[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          */
78         private static function esc_array_callback(&$value, $key, $add_quotation)
79         {
80                 if (!$add_quotation) {
81                         if (is_bool($value)) {
82                                 $value = ($value ? '1' : '0');
83                         } else {
84                                 $value = dbesc($value);
85                         }
86                         return;
87                 }
88
89                 if (is_bool($value)) {
90                         $value = ($value ? 'true' : 'false');
91                 } elseif (is_float($value) || is_integer($value)) {
92                         $value = (string)$value;
93                 } else {
94                          $value = "'".dbesc($value)."'";
95                 }
96         }
97
98         /**
99          * @brief Escapes a whole array
100          *
101          * @param mixed   $arr           Array with values to be escaped
102          * @param boolean $add_quotation add quotation marks for string values
103          */
104         public static function esc_array(&$arr, $add_quotation = false)
105         {
106                 array_walk($arr, 'self::esc_array_callback', $add_quotation);
107         }
108
109         /**
110          * Checks Converts any date string into a SQL compatible date string
111          *
112          * @param string $date a date string in any format
113          *
114          * @return string SQL style date string
115          */
116         public static function date($date = 'now')
117         {
118                 $timestamp = strtotime($date);
119
120                 // Don't allow lower date strings as '0001-01-01 00:00:00'
121                 if ($timestamp < -62135596800) {
122                         $timestamp = -62135596800;
123                 }
124
125                 return date('Y-m-d H:i:s', (int)$timestamp);
126         }
127 }