|
10 | 10 | class Cli |
11 | 11 | { |
12 | 12 | /** |
13 | | - * @var \BlueChip\Cache\Core |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + private const UNKNOWN_VALUE = '???'; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var Core |
14 | 19 | */ |
15 | 20 | private $cache; |
16 | 21 |
|
17 | 22 | /** |
18 | | - * @var \BlueChip\Cache\Crawler|null |
| 23 | + * @var Crawler|null |
19 | 24 | */ |
20 | 25 | private $cache_crawler; |
21 | 26 |
|
22 | 27 | /** |
23 | | - * @var \BlueChip\Cache\Feeder|null |
| 28 | + * @var Feeder|null |
24 | 29 | */ |
25 | 30 | private $cache_feeder; |
26 | 31 |
|
27 | 32 |
|
28 | 33 | /** |
29 | | - * @param \BlueChip\Cache\Core $cache |
30 | | - * @param \BlueChip\Cache\Crawler|null $cache_crawler Null value signals that cache warm up is disabled. |
31 | | - * @param \BlueChip\Cache\Feeder|null $cache_feeder Null value signals that cache warm up is disabled. |
| 34 | + * @param Core $cache |
| 35 | + * @param Crawler|null $cache_crawler Null value signals that cache warm up is disabled. |
| 36 | + * @param Feeder|null $cache_feeder Null value signals that cache warm up is disabled. |
32 | 37 | */ |
33 | 38 | public function __construct(Core $cache, ?Crawler $cache_crawler, ?Feeder $cache_feeder) |
34 | 39 | { |
@@ -171,6 +176,123 @@ private function erase(string $url, ?int $post_id = null): void |
171 | 176 | } |
172 | 177 |
|
173 | 178 |
|
| 179 | + /** |
| 180 | + * List cache entries. |
| 181 | + * |
| 182 | + * By default following columns are printed: |
| 183 | + * - URL (url) |
| 184 | + * - Request variant (request_variant) |
| 185 | + * - Created (created) |
| 186 | + * - Size (size) |
| 187 | + * |
| 188 | + * Note: Request variant column is printed only if there are multiple request variants configured or column is explicitly requested (see options below). |
| 189 | + * |
| 190 | + * ## OPTIONS |
| 191 | + * |
| 192 | + * [<column>...] |
| 193 | + * : Explicitly set columns (incl. their order) to print: proper column keys has to be given (see the list above). |
| 194 | + * |
| 195 | + * [--format=<format>] |
| 196 | + * : Output format to use. Can be 'table', 'json', 'csv', 'yaml' or 'count'. Default is 'table'. |
| 197 | + * |
| 198 | + * [--plain] |
| 199 | + * : Print URL including scheme and host, request variant as a key only, creation time as Unix timestamp and size as number of bytes without unit. |
| 200 | + * |
| 201 | + * [--sort-by=<column>] |
| 202 | + * : Sort by given column in ascending order. It is possible to sort by column that is not being printed out. |
| 203 | + * |
| 204 | + * ## EXAMPLES |
| 205 | + * |
| 206 | + * wp bc-cache list url request-variant size |
| 207 | + */ |
| 208 | + public function list(array $args, array $assoc_args): void |
| 209 | + { |
| 210 | + $available_columns = [ |
| 211 | + 'url', |
| 212 | + 'request_variant', |
| 213 | + 'created', |
| 214 | + 'size', |
| 215 | + ]; |
| 216 | + |
| 217 | + // Explicitly set columns to display? |
| 218 | + if ($args !== []) { |
| 219 | + // Set and validate columns to display. |
| 220 | + $columns_to_display = []; |
| 221 | + foreach ($args as $arg) { |
| 222 | + if (\array_search($arg, $available_columns, true) === false) { |
| 223 | + \WP_CLI::error(sprintf('Unknown column key given: "%s". Exiting ...', $arg)); |
| 224 | + } |
| 225 | + |
| 226 | + $columns_to_display[] = $arg; |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + $format = $assoc_args['format'] ?? 'table'; |
| 231 | + $plain = $assoc_args['plain'] ?? false; |
| 232 | + $sort_by = $assoc_args['sort-by'] ?? ''; |
| 233 | + |
| 234 | + // Validate sort by value. |
| 235 | + if ($sort_by) { |
| 236 | + if (\array_search($sort_by, $available_columns, true) === false) { |
| 237 | + \WP_CLI::error(sprintf('Unknown column key given for --sort-by argument: "%s". Exiting ...', $sort_by)); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + $request_variants = $this->cache->getRequestVariants(); |
| 242 | + |
| 243 | + if (!isset($columns_to_display)) { |
| 244 | + // Columns to display have not been set explicitly, so use all available columns... |
| 245 | + $columns_to_display = $available_columns; |
| 246 | + // ...but unset request variant if there is only single (default) variant configured. |
| 247 | + if (\count($request_variants) === 1) { |
| 248 | + $columns_to_display = \array_diff($columns_to_display, ['request_variant']); |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + $cache_items = $this->cache->inspect(); |
| 253 | + |
| 254 | + if ($cache_items === null) { |
| 255 | + \WP_CLI::error('Cache items could not be fetched due to I/O error. Exiting ...'); |
| 256 | + } |
| 257 | + |
| 258 | + // Prepare items. |
| 259 | + $items = array_map( |
| 260 | + function (ListTableItem $item) use ($plain, $request_variants): array { |
| 261 | + $request_variant = $item->getRequestVariant(); |
| 262 | + $timestamp = $item->getTimestamp(); |
| 263 | + $total_size = $item->getTotalSize(); |
| 264 | + $url = $item->getUrl(); |
| 265 | + |
| 266 | + return [ |
| 267 | + 'url' => $plain ? $url : \parse_url($url, PHP_URL_PATH), |
| 268 | + 'request_variant' => $plain ? $request_variant : $request_variants[$request_variant], |
| 269 | + 'created' => $timestamp ? ($plain ? $timestamp : wp_date('Y-m-d H:i:s', $timestamp)) : self::UNKNOWN_VALUE, |
| 270 | + 'size' => $plain ? $total_size : size_format($total_size), |
| 271 | + ]; |
| 272 | + }, |
| 273 | + $cache_items |
| 274 | + ); |
| 275 | + |
| 276 | + // Sort items? |
| 277 | + if ($sort_by) { |
| 278 | + usort( |
| 279 | + $items, |
| 280 | + function (array $a, array $b) use ($sort_by): int { |
| 281 | + if ($a[$sort_by] < $b[$sort_by]) { |
| 282 | + return -1; |
| 283 | + } |
| 284 | + if ($a[$sort_by] > $b[$sort_by]) { |
| 285 | + return 1; |
| 286 | + } |
| 287 | + return 0; |
| 288 | + } |
| 289 | + ); |
| 290 | + } |
| 291 | + |
| 292 | + \WP_CLI\Utils\format_items($format, $items, $columns_to_display); |
| 293 | + } |
| 294 | + |
| 295 | + |
174 | 296 | /** |
175 | 297 | * Warm up cache. |
176 | 298 | * |
|
0 commit comments