1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* YOURLS simple logger |
5
|
|
|
* |
6
|
|
|
* @since 1.7.3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace YOURLS\Admin; |
10
|
|
|
|
11
|
|
|
use YOURLS\Database\YDB; |
12
|
|
|
|
13
|
|
|
class Logger extends \Aura\Sql\Profiler { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* the YDB instance |
17
|
|
|
* @var YOURLS\Database\YDB |
18
|
|
|
*/ |
19
|
|
|
protected $ydb; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Debug log where messages are stored |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $debug_log = array(); |
26
|
|
|
|
27
|
|
|
public function __construct(YDB $ydb) { |
28
|
|
|
$this->ydb = $ydb; |
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @todo Extend this to allow calling an external logger |
31
|
|
|
*/ |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $message |
36
|
|
|
*/ |
37
|
|
|
public function log($message) { |
38
|
|
|
yourls_do_action('debug_log', $message); |
39
|
|
|
$this->debug_log[] = $message; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function get_log() { |
43
|
|
|
return $this->debug_log; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Extends \Aura\Sql\Profiler::addProfile() to log queries in our YOURLS logger |
48
|
|
|
* |
49
|
|
|
* @since 1.7.3 |
50
|
|
|
* @param float $duration The query duration. |
51
|
|
|
* @param string $function The PDO method that made the entry. |
52
|
|
|
* @param string $statement The SQL query statement. |
53
|
|
|
* @param array $bind_values The values bound to the statement. |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function addProfile($duration, $function, $statement, array $bind_values = array() ) { |
57
|
|
|
parent::addProfile($duration, $function, $statement, $bind_values); |
58
|
|
|
|
59
|
|
|
if($function == 'connect') { |
60
|
|
|
$this->log( sprintf('SQL: CONNECT (%s s)', number_format($duration, 5)) ); |
61
|
|
|
} else { |
62
|
|
|
if ( $this->ydb->getAttribute(\PDO::ATTR_EMULATE_PREPARES) && $function !== 'prepare' ) { |
63
|
|
|
$this->log( sprintf('SQL: %s (%s s)', $this->pretty_format($statement, $bind_values), number_format($duration, 5) ) ); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Format PDO statement with bind/values replacement |
70
|
|
|
* |
71
|
|
|
* This replaces PDO binds such as 'key_name = :name' with corresponding array values, eg array('name'=>'some value') |
72
|
|
|
* This is merely a cosmetic replacement to allow for readability: the result WILL NOT be valid SQL! (eg no proper quotes) |
73
|
|
|
* |
74
|
|
|
* @since 1.7.3 |
75
|
|
|
* @param string $statement SQL query with PDO style named placeholders |
76
|
|
|
* @param array $values Optional array of values corresponding to placeholders |
77
|
|
|
* @return string Readable SQL query with placeholders replaced |
78
|
|
|
*/ |
79
|
|
|
public function pretty_format($statement, array $values = array() ) { |
80
|
|
|
if (!$values) { |
|
|
|
|
81
|
|
|
return $statement; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return preg_replace_callback( |
85
|
|
|
'/:([^\s;)]*)/', |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $matches |
89
|
|
|
*/ |
90
|
|
|
function ($matches) use ($values) { |
91
|
|
|
$replacement = isset( $values[$matches[1]] ) ? $values[$matches[1]] : ''; |
92
|
|
|
if(is_array($replacement)) { |
93
|
|
|
$replacement = implode(",", $replacement); |
94
|
|
|
} |
95
|
|
|
return "'$replacement'"; |
96
|
|
|
}, |
97
|
|
|
$statement |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..