2017-11-08 01:37:53 +01:00
|
|
|
<?php
|
2017-11-08 23:02:50 +01:00
|
|
|
/**
|
|
|
|
* @file src/Database/DBM.php
|
|
|
|
*/
|
2017-11-08 01:37:53 +01:00
|
|
|
namespace Friendica\Database;
|
|
|
|
|
|
|
|
use dba;
|
2017-11-08 23:02:50 +01:00
|
|
|
|
2017-12-17 21:24:57 +01:00
|
|
|
require_once 'include/dba.php';
|
|
|
|
|
2017-11-08 01:37:53 +01:00
|
|
|
/**
|
|
|
|
* @brief This class contain functions for the database management
|
|
|
|
*
|
|
|
|
* This class contains functions that doesn't need to know if pdo, mysqli or whatever is used.
|
|
|
|
*/
|
2017-11-08 23:02:50 +01:00
|
|
|
class DBM
|
|
|
|
{
|
2017-11-08 01:37:53 +01:00
|
|
|
/**
|
|
|
|
* @brief Return a list of database processes
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* 'list' => List of processes, separated in their different states
|
|
|
|
* 'amount' => Number of concurrent database processes
|
|
|
|
*/
|
2017-11-08 23:02:50 +01:00
|
|
|
public static function processlist()
|
|
|
|
{
|
2017-11-08 01:37:53 +01:00
|
|
|
$r = q("SHOW PROCESSLIST");
|
2018-01-15 14:05:12 +01:00
|
|
|
$s = [];
|
2017-11-08 01:37:53 +01:00
|
|
|
|
|
|
|
$processes = 0;
|
2018-01-15 14:05:12 +01:00
|
|
|
$states = [];
|
2017-11-19 23:04:40 +01:00
|
|
|
foreach ($r as $process) {
|
2017-11-08 01:37:53 +01:00
|
|
|
$state = trim($process["State"]);
|
|
|
|
|
|
|
|
// Filter out all non blocking processes
|
2018-01-15 14:05:12 +01:00
|
|
|
if (!in_array($state, ["", "init", "statistics", "updating"])) {
|
2017-11-08 01:37:53 +01:00
|
|
|
++$states[$state];
|
|
|
|
++$processes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$statelist = "";
|
2017-11-19 23:04:40 +01:00
|
|
|
foreach ($states as $state => $usage) {
|
2017-11-08 23:02:50 +01:00
|
|
|
if ($statelist != "") {
|
2017-11-08 01:37:53 +01:00
|
|
|
$statelist .= ", ";
|
2017-11-08 23:02:50 +01:00
|
|
|
}
|
2017-11-08 01:37:53 +01:00
|
|
|
$statelist .= $state.": ".$usage;
|
|
|
|
}
|
2018-01-15 14:05:12 +01:00
|
|
|
return(["list" => $statelist, "amount" => $processes]);
|
2017-11-08 01:37:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if $array is a filled array with at least one entry.
|
|
|
|
*
|
2017-11-08 23:02:50 +01:00
|
|
|
* @param mixed $array A filled array with at least one entry
|
|
|
|
*
|
|
|
|
* @return boolean Whether $array is a filled array or an object with rows
|
2017-11-08 01:37:53 +01:00
|
|
|
*/
|
2017-11-08 23:02:50 +01:00
|
|
|
public static function is_result($array)
|
|
|
|
{
|
2017-11-08 01:37:53 +01:00
|
|
|
// It could be a return value from an update statement
|
|
|
|
if (is_bool($array)) {
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_object($array)) {
|
|
|
|
return dba::num_rows($array) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (is_array($array) && (count($array) > 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callback function for "esc_array"
|
|
|
|
*
|
2017-11-08 23:02:50 +01:00
|
|
|
* @param mixed $value Array value
|
|
|
|
* @param string $key Array key
|
2017-11-08 01:37:53 +01:00
|
|
|
* @param boolean $add_quotation add quotation marks for string values
|
2017-11-19 23:04:40 +01:00
|
|
|
* @return void
|
2017-11-08 01:37:53 +01:00
|
|
|
*/
|
2017-11-08 23:02:50 +01:00
|
|
|
private static function esc_array_callback(&$value, $key, $add_quotation)
|
|
|
|
{
|
2017-11-08 01:37:53 +01:00
|
|
|
if (!$add_quotation) {
|
|
|
|
if (is_bool($value)) {
|
|
|
|
$value = ($value ? '1' : '0');
|
|
|
|
} else {
|
|
|
|
$value = dbesc($value);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_bool($value)) {
|
|
|
|
$value = ($value ? 'true' : 'false');
|
|
|
|
} elseif (is_float($value) || is_integer($value)) {
|
|
|
|
$value = (string)$value;
|
|
|
|
} else {
|
|
|
|
$value = "'".dbesc($value)."'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Escapes a whole array
|
|
|
|
*
|
2017-11-08 23:02:50 +01:00
|
|
|
* @param mixed $arr Array with values to be escaped
|
2017-11-08 01:37:53 +01:00
|
|
|
* @param boolean $add_quotation add quotation marks for string values
|
2017-11-19 23:04:40 +01:00
|
|
|
* @return void
|
2017-11-08 01:37:53 +01:00
|
|
|
*/
|
2017-11-08 23:02:50 +01:00
|
|
|
public static function esc_array(&$arr, $add_quotation = false)
|
|
|
|
{
|
2017-11-08 01:37:53 +01:00
|
|
|
array_walk($arr, 'self::esc_array_callback', $add_quotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks Converts any date string into a SQL compatible date string
|
|
|
|
*
|
|
|
|
* @param string $date a date string in any format
|
2017-11-08 23:02:50 +01:00
|
|
|
*
|
2017-11-08 01:37:53 +01:00
|
|
|
* @return string SQL style date string
|
|
|
|
*/
|
2017-11-08 23:02:50 +01:00
|
|
|
public static function date($date = 'now')
|
|
|
|
{
|
2017-11-08 01:37:53 +01:00
|
|
|
$timestamp = strtotime($date);
|
|
|
|
|
|
|
|
// Don't allow lower date strings as '0001-01-01 00:00:00'
|
|
|
|
if ($timestamp < -62135596800) {
|
|
|
|
$timestamp = -62135596800;
|
|
|
|
}
|
|
|
|
|
2018-01-26 05:26:51 +01:00
|
|
|
return date(Temporal::MYSQL, (int)$timestamp);
|
2017-11-08 01:37:53 +01:00
|
|
|
}
|
|
|
|
}
|