PluginProbe
Docket Cache – Object Cache Accelerator / 22.07.01
Docket Cache – Object Cache Accelerator v22.07.01
26.04.05 trunk 22.07.01 22.07.02 22.07.03 22.07.04 22.07.05 23.08.01 23.08.02 24.07.01 24.07.02 24.07.03 24.07.04 24.07.05 24.07.06 24.07.07 26.04.03 26.04.04
docket-cache / includes / src / OPcacheView.php

OPcacheView.php in Docket Cache – Object Cache Accelerator 22.07.01, at includes/src/OPcacheView.php

304 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Docket Cache.
4 *
5 * @author Nawawi Jamili
6 * @license MIT
7 *
8 * @see https://github.com/nawawi/docket-cache
9 */
10
11 namespace Nawawi\DocketCache;
12
13 \defined('ABSPATH') || exit;
14
15 if (!class_exists('\\WP_List_Table', false)) {
16 require_once ABSPATH.'wp-admin/includes/class-wp-list-table.php';
17 }
18
19 class OPcacheView extends \WP_List_Table
20 {
21 private $pt;
22
23 public function __construct(Plugin $pt)
24 {
25 parent::__construct(
26 [
27 'singular' => 'opclist-info',
28 'plural' => 'opclist-infos',
29 'ajax' => false,
30 'screen' => 'opclist-infos',
31 ]
32 );
33
34 $this->pt = $pt;
35 }
36
37 public function get_doc($name)
38 {
39 $name = str_replace('_', '-', $name);
40
41 return 'https://www.php.net/manual/en/opcache.configuration.php#ini.'.$name;
42 }
43
44 public function get_config()
45 {
46 static $data = false;
47
48 try {
49 if ($this->pt->opcache_function_exists('opcache_get_configuration') && (empty($data) || !\is_array($data))) {
50 $data = @opcache_get_configuration();
51 }
52 } catch (\Throwable $e) {
53 nwdcx_throwable(__METHOD__, $e);
54 }
55
56 return $data;
57 }
58
59 private function get_status()
60 {
61 $data = $this->pt->get_opcache_status(true);
62 if (!empty($data) && \is_array($data)) {
63 return $data;
64 }
65
66 return false;
67 }
68
69 public function get_usage()
70 {
71 $stats = [
72 'num_cached_scripts' => 0,
73 'num_cached_keys' => 0,
74 'max_cached_keys' => 0,
75 'hits' => 0,
76 'start_time' => 0,
77 'last_restart_time' => 0,
78 'oom_restarts' => 0,
79 'hash_restarts' => 0,
80 'manual_restarts' => 0,
81 'misses' => 0,
82 'blacklist_misses' => 0,
83 'blacklist_miss_ratio' => 0,
84 'opcache_hit_rate' => 0,
85 'used_memory' => 0,
86 'free_memory' => 0,
87 'wasted_memory' => 0,
88 'current_wasted_percentage' => 0,
89 ];
90
91 $data = $this->get_status();
92 if (!empty($data) && \is_array($data)) {
93 if (isset($data['opcache_statistics']) && isset($data['memory_usage']) && \is_array($data['opcache_statistics']) && \is_array($data['memory_usage'])) {
94 $stats = array_merge($data['opcache_statistics'], $data['memory_usage']);
95 } elseif (!empty($data['file_cache_only'])) {
96 $stats['file_cache_only'] = 1;
97 $stats['file_cache'] = $data['file_cache'];
98 $stats['statsfile'] = sprintf(esc_html__(_n('%1$s size of %2$s file', '%1$s size of %2$s files', $data['_numfile'], 'docket-cache')), $this->pt->normalize_size($data['_sizefile']), $data['_numfile']);
99 }
100 }
101
102 return (object) $stats;
103 }
104
105 private function get_files()
106 {
107 $stats = [];
108 $data = $this->get_status();
109 if (!empty($data) && \is_array($data)) {
110 if (!empty($data['scripts']) && \is_array($data['scripts'])) {
111 $sstr = !empty($_GET['s']) ? sanitize_text_field(wp_unslash($_GET['s'])) : '';
112 $sftr = !empty($_GET['sf']) ? sanitize_text_field($_GET['sf']) : 'obc';
113 $smtr = !empty($_GET['sm']) ? sanitize_text_field($_GET['sm']) : '1k';
114
115 $cnt = 0;
116 $limit = 0;
117
118 if ('all' !== $smtr) {
119 $smtr = strtok($smtr, 'k').'000';
120 $limit = (int) $smtr;
121 }
122
123 foreach ($data['scripts'] as $script => $arr) {
124 $cpath = wp_normalize_path($arr['full_path']);
125 $script = wp_normalize_path($script);
126
127 if (!empty($sstr) && false === strpos($cpath, $sstr)) {
128 continue;
129 }
130
131 if (!empty($sftr)) {
132 $cfile = basename($cpath);
133 $cdir = \dirname($cpath);
134
135 if ('obc' === $sftr && (false === strpos($script, $this->pt->cache_path) || !$this->pt->is_docketcachedir($cdir) || !@preg_match('@^([a-z0-9]{12})\-([a-z0-9]{12})\.php(\.bin)?$@', $cfile))) {
136 continue;
137 }
138
139 if ('wpc' === $sftr && (false !== strpos($script, $this->pt->cache_path) && $this->pt->is_docketcachedir($cdir) && @preg_match('@^([a-z0-9]{12})\-([a-z0-9]{12})\.php(\.bin)?$@', $cfile))) {
140 continue;
141 }
142
143 if ('dfc' === $sftr && @is_file($cpath)) {
144 continue;
145 }
146 }
147
148 $sort = $arr['hits'];
149 if (!empty($_GET['orderby'])) {
150 switch ($_GET['orderby']) {
151 case 'file':
152 $sort = $cpath;
153 break;
154 case 'mem':
155 $sort = $arr['memory_consumption'];
156 break;
157 case 'stmp':
158 $sort = $arr['last_used_timestamp'];
159 break;
160 }
161 }
162
163 $fxfile = $this->pt->sanitize_rootpath($cpath);
164
165 if (!empty($data['file_cache_only']) && !empty($data['file_cache'])) {
166 $fxfile = preg_replace('@'.preg_quote(wp_normalize_path($data['file_cache'])).'([0-9a-zA-z]+)@', '[$1] ', $fxfile);
167 }
168
169 $stats[$sort.$script] = (object) [
170 'is_exists' => @is_file($cpath),
171 'file' => $fxfile,
172 'hits' => $arr['hits'],
173 'mem' => $this->pt->normalize_size($arr['memory_consumption']),
174 'stmp' => wp_date('Y-m-d H:i:s', $arr['last_used_timestamp']),
175 ];
176
177 ++$cnt;
178 if ($limit > 0 && $cnt >= $limit) {
179 break;
180 }
181 }
182
183 if (!empty($_GET['orderby']) && !empty($_GET['order'])) {
184 $order = sanitize_text_field($_GET['order']);
185 $orderby = sanitize_text_field($_GET['orderby']);
186
187 if ('desc' === $order) {
188 if ('file' !== $orderby) {
189 krsort($stats, \SORT_NUMERIC);
190 } else {
191 krsort($stats, \SORT_STRING);
192 }
193 } else {
194 if ('file' !== $orderby) {
195 ksort($stats, \SORT_NUMERIC);
196 } else {
197 ksort($stats, \SORT_STRING);
198 }
199 }
200 } else {
201 krsort($stats, \SORT_NUMERIC);
202 }
203 }
204 }
205
206 unset($data);
207
208 return $stats;
209 }
210
211 public function prepare_items()
212 {
213 $stats = $this->get_files();
214
215 $count = \count($stats);
216 $per_page = 50;
217 $offset = ($this->get_pagenum() - 1) * $per_page;
218
219 $this->items = \array_slice($stats, $offset, $per_page);
220
221 $this->set_pagination_args(
222 [
223 'total_items' => $count,
224 'per_page' => $per_page,
225 'total_pages' => ceil($count / $per_page),
226 ]
227 );
228 }
229
230 public function get_columns()
231 {
232 /* translators: %s = utc offset */
233 $lastused = sprintf(esc_html__('Last Used %s', 'docket-cache'), '('.$this->pt->get_utc_offset().')');
234
235 $cols = [
236 'opclist_file' => esc_html__('Cached Files', 'docket-cache'),
237 'opclist_hits' => esc_html__('Cache Hits', 'docket-cache'),
238 'opclist_mem' => esc_html__('Memory Usage', 'docket-cache'),
239 'opclist_timestamp' => $lastused,
240 ];
241
242 if ($this->pt->is_opcache_filecache_only()) {
243 unset($cols['opclist_hits']);
244 $cols['opclist_mem'] = esc_html__('File Size', 'docket-cache');
245 }
246
247 return $cols;
248 }
249
250 public function get_sortable_columns()
251 {
252 $cols = [
253 'opclist_file' => ['file', false],
254 'opclist_hits' => ['hits', false],
255 'opclist_mem' => ['mem', false],
256 'opclist_timestamp' => ['stmp', false],
257 ];
258
259 if ($this->pt->is_opcache_filecache_only()) {
260 unset($cols['opclist_hits']);
261 }
262
263 return $cols;
264 }
265
266 public function get_table_classes()
267 {
268 return ['widefat', 'striped', $this->_args['plural']];
269 }
270
271 public function column_opclist_hits($stats)
272 {
273 return '<code>'.$stats->hits.'</code>';
274 }
275
276 public function column_opclist_mem($stats)
277 {
278 return '<code>'.$stats->mem.'</code>';
279 }
280
281 public function column_opclist_file($stats)
282 {
283 if ($stats->is_exists) {
284 return esc_html($stats->file);
285 }
286
287 return '<span class="text-red">'.esc_html($stats->file).'</span>';
288 }
289
290 public function column_opclist_timestamp($stats)
291 {
292 return esc_html($stats->stmp);
293 }
294
295 public function no_items()
296 {
297 if (empty($_GET['s'])) {
298 esc_html_e('OPcache items not available.', 'docket-cache');
299 } else {
300 esc_html_e('No matching OPcache Cached Files.', 'docket-cache');
301 }
302 }
303 }
304