-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathGeneric_Page_PurgeLog.php
131 lines (115 loc) · 2.83 KB
/
Generic_Page_PurgeLog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* File: Generic_Page_PurgeLog.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class: Generic_Page_PurgeLog
*
* phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
*/
class Generic_Page_PurgeLog {
/**
* Plugins regex
*
* @var string
*/
private $plugins_regexp = null;
/**
* Constructor
*
* @return void
*/
public function __construct() {
$p = dirname( dirname( __FILE__ ) );
if ( $this->starts_with( $p, ABSPATH ) ) {
$p = substr( $p, strlen( ABSPATH ) );
}
$this->plugins_regexp = '~^(' . Util_Environment::preg_quote( $p ) . '/)([^/]+)(.*)~';
}
/**
* Render content
*
* @return void
*/
public function render_content() {
$module = Util_Request::get_label( 'module' );
$log_filename = Util_Debug::log_filename( $module . '-purge' );
if ( file_exists( $log_filename ) ) {
$log_filefize = $this->to_human( filesize( $log_filename ) );
} else {
$log_filefize = 'n/a';
}
$lines = Util_DebugPurgeLog_Reader::read( $module );
$purgelog_modules = array();
$c = Dispatcher::config();
if ( $c->get_boolean( 'pgcache.debug_purge' ) ) {
$purgelog_modules[] = array(
'label' => 'pagecache',
'name' => 'Page Cache',
'postfix' => '',
);
}
if ( $c->get_boolean( 'dbcache.debug_purge' ) ) {
$purgelog_modules[] = array(
'label' => 'dbcache',
'name' => 'Database Cache',
'postfix' => '',
);
}
if ( $c->get_boolean( 'objectcache.debug_purge' ) ) {
$purgelog_modules[] = array(
'label' => 'objectcache',
'name' => 'Object Cache',
'postfix' => '',
);
}
$module_count = count( $purgelog_modules );
for ( $n = 0; $n < $module_count - 1; $n++ ) {
$purgelog_modules[ $n ]['postfix'] = '|';
}
require __DIR__ . '/Generic_Page_PurgeLog_View.php';
}
/**
* Converts bytes to human readable
*
* @param int $bytes Bytes.
* @param int $decimals Decimal places.
*
* @return string
*/
private function to_human( $bytes, $decimals = 2 ) {
$size = array( 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' );
$factor = floor( ( strlen( $bytes ) - 1 ) / 3 );
return sprintf( "%.{$decimals}f", $bytes / pow( 1024, $factor ) ) . @$size[ $factor ];
}
/**
* Checks if string starts with pattern.
*
* @param string $s String to check.
* @param string $prefix Matching pattern.
*
* @return bool
*/
private function starts_with( $s, $prefix ) {
return substr( $s, 0, strlen( $prefix ) ) === $prefix;
}
/**
* Sanitizes filename.
*
* @param string $filename File name.
*
* @return string
*/
private function esc_filename( $filename ) {
$m = null;
if ( preg_match( $this->plugins_regexp, $filename, $m ) ) {
return esc_html( $m[1] ) .
'<strong>' . esc_html( $m[2] ) . '</strong>' .
esc_html( $m[3] );
}
return esc_html( $filename );
}
}