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