PluginProbe
Docket Cache – Object Cache Accelerator / 26.04.03
Docket Cache – Object Cache Accelerator v26.04.03
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 26.04.03, at includes/src/OPcacheView.php

310 lines 9.7 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 static $cache = null;
62
63 if (null === $cache) {
64 $data = $this->pt->get_opcache_status(true);
65 $cache = (!empty($data) && \is_array($data)) ? $data : false;
66 }
67
68 return $cache;
69 }
70
71 public function get_usage()
72 {
73 $stats = [
74 'num_cached_scripts' => 0,
75 'num_cached_keys' => 0,
76 'max_cached_keys' => 0,
77 'hits' => 0,
78 'start_time' => 0,
79 'last_restart_time' => 0,
80 'oom_restarts' => 0,
81 'hash_restarts' => 0,
82 'manual_restarts' => 0,
83 'misses' => 0,
84 'blacklist_misses' => 0,
85 'blacklist_miss_ratio' => 0,
86 'opcache_hit_rate' => 0,
87 'used_memory' => 0,
88 'free_memory' => 0,
89 'wasted_memory' => 0,
90 'current_wasted_percentage' => 0,
91 ];
92
93 $data = $this->get_status();
94 if (!empty($data) && \is_array($data)) {
95 if (isset($data['opcache_statistics']) && isset($data['memory_usage']) && \is_array($data['opcache_statistics']) && \is_array($data['memory_usage'])) {
96 $stats = array_merge($data['opcache_statistics'], $data['memory_usage']);
97 } elseif (!empty($data['file_cache_only'])) {
98 $stats['file_cache_only'] = 1;
99 $stats['file_cache'] = $data['file_cache'];
100 $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']);
101 }
102 }
103
104 return (object) $stats;
105 }
106
107 private function get_files()
108 {
109 $stats = [];
110 $data = $this->get_status();
111 if (!empty($data) && \is_array($data)) {
112 if (!empty($data['scripts']) && \is_array($data['scripts'])) {
113 $sstr = !empty($_GET['s']) ? sanitize_text_field(wp_unslash($_GET['s'])) : '';
114 $sftr = !empty($_GET['sf']) ? sanitize_text_field($_GET['sf']) : 'obc';
115 $smtr = !empty($_GET['sm']) ? sanitize_text_field($_GET['sm']) : '1k';
116
117 $cnt = 0;
118 $limit = 0;
119
120 if ('all' !== $smtr) {
121 $smtr = strtok($smtr, 'k').'000';
122 $limit = (int) $smtr;
123 }
124
125 foreach ($data['scripts'] as $script => $arr) {
126 $cpath = wp_normalize_path($arr['full_path']);
127 $script = wp_normalize_path($script);
128
129 if ($this->pt->cf()->is_dcfalse('OPCVIEWER_SHOWALL') && !(0 === strpos($cpath, ABSPATH))) {
130 continue;
131 }
132
133 if (!empty($sstr) && false === strpos($cpath, $sstr)) {
134 continue;
135 }
136
137 if (!empty($sftr)) {
138 $cfile = basename($cpath);
139 $cdir = \dirname($cpath);
140
141 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))) {
142 continue;
143 }
144
145 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))) {
146 continue;
147 }
148
149 if ('dfc' === $sftr && @is_file($cpath)) {
150 continue;
151 }
152 }
153
154 $sort = $arr['hits'];
155 if (!empty($_GET['orderby'])) {
156 switch ($_GET['orderby']) {
157 case 'file':
158 $sort = $cpath;
159 break;
160 case 'mem':
161 $sort = $arr['memory_consumption'];
162 break;
163 case 'stmp':
164 $sort = $arr['last_used_timestamp'];
165 break;
166 }
167 }
168
169 $fxfile = $this->pt->sanitize_rootpath($cpath);
170
171 if (!empty($data['file_cache_only']) && !empty($data['file_cache'])) {
172 $fxfile = preg_replace('@'.preg_quote(wp_normalize_path($data['file_cache'])).'([0-9a-zA-z]+)@', '[$1] ', $fxfile);
173 }
174
175 $stats[$sort.$script] = (object) [
176 'is_exists' => @is_file($cpath),
177 'file' => $fxfile,
178 'hits' => $arr['hits'],
179 'mem' => $this->pt->normalize_size($arr['memory_consumption']),
180 'stmp' => wp_date('Y-m-d H:i:s', $arr['last_used_timestamp']),
181 ];
182
183 ++$cnt;
184 if ($limit > 0 && $cnt >= $limit) {
185 break;
186 }
187 }
188
189 if (!empty($_GET['orderby']) && !empty($_GET['order'])) {
190 $order = sanitize_text_field($_GET['order']);
191 $orderby = sanitize_text_field($_GET['orderby']);
192
193 if ('desc' === $order) {
194 if ('file' !== $orderby) {
195 krsort($stats, \SORT_NUMERIC);
196 } else {
197 krsort($stats, \SORT_STRING);
198 }
199 } else {
200 if ('file' !== $orderby) {
201 ksort($stats, \SORT_NUMERIC);
202 } else {
203 ksort($stats, \SORT_STRING);
204 }
205 }
206 } else {
207 krsort($stats, \SORT_NUMERIC);
208 }
209 }
210 }
211
212 unset($data);
213
214 return $stats;
215 }
216
217 public function prepare_items()
218 {
219 $stats = $this->get_files();
220
221 $count = \count($stats);
222 $per_page = 50;
223 $offset = ($this->get_pagenum() - 1) * $per_page;
224
225 $this->items = \array_slice($stats, $offset, $per_page);
226
227 $this->set_pagination_args(
228 [
229 'total_items' => $count,
230 'per_page' => $per_page,
231 'total_pages' => ceil($count / $per_page),
232 ]
233 );
234 }
235
236 public function get_columns()
237 {
238 /* translators: %s = utc offset */
239 $lastused = sprintf(esc_html__('Last Used %s', 'docket-cache'), '('.$this->pt->get_utc_offset().')');
240
241 $cols = [
242 'opclist_file' => esc_html__('Cached Files', 'docket-cache'),
243 'opclist_hits' => esc_html__('Cache Hits', 'docket-cache'),
244 'opclist_mem' => esc_html__('Memory Usage', 'docket-cache'),
245 'opclist_timestamp' => $lastused,
246 ];
247
248 if ($this->pt->is_opcache_filecache_only()) {
249 unset($cols['opclist_hits']);
250 $cols['opclist_mem'] = esc_html__('File Size', 'docket-cache');
251 }
252
253 return $cols;
254 }
255
256 public function get_sortable_columns()
257 {
258 $cols = [
259 'opclist_file' => ['file', false],
260 'opclist_hits' => ['hits', false],
261 'opclist_mem' => ['mem', false],
262 'opclist_timestamp' => ['stmp', false],
263 ];
264
265 if ($this->pt->is_opcache_filecache_only()) {
266 unset($cols['opclist_hits']);
267 }
268
269 return $cols;
270 }
271
272 public function get_table_classes()
273 {
274 return ['widefat', 'striped', $this->_args['plural']];
275 }
276
277 public function column_opclist_hits($stats)
278 {
279 return '<code>'.$stats->hits.'</code>';
280 }
281
282 public function column_opclist_mem($stats)
283 {
284 return '<code>'.$stats->mem.'</code>';
285 }
286
287 public function column_opclist_file($stats)
288 {
289 if ($stats->is_exists) {
290 return esc_html($stats->file);
291 }
292
293 return '<span class="text-red">'.esc_html($stats->file).'</span>';
294 }
295
296 public function column_opclist_timestamp($stats)
297 {
298 return esc_html($stats->stmp);
299 }
300
301 public function no_items()
302 {
303 if (empty($_GET['s'])) {
304 esc_html_e('OPcache items not available.', 'docket-cache');
305 } else {
306 esc_html_e('No matching OPcache Cached Files.', 'docket-cache');
307 }
308 }
309 }
310