Skip to content

Commit

Permalink
✨ trash command (remove cache entries by key)
Browse files Browse the repository at this point in the history
  • Loading branch information
bnomei committed Sep 1, 2024
1 parent 70e5952 commit 92c1654
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ This plugin comes with a [few commands](https://github.com/bnomei/kirby3-janitor
- `janitor:cleancontent`, removes fields from content file that are not defined in your blueprints
- `janitor:clipboard`, copies a defined value to your clipboard
- `janitor:download`, triggers a download of an URL
- `janitor:flush`, flush a cache by providing its name
- `janitor:flush`, flush a cache by providing its name (default: pages cache)
- `janitor:job`, run a callback
- `janitor:maintenance`, toggle maintenance mode
- `janitor:open`, triggers opening of an URL in panel
Expand All @@ -146,6 +146,7 @@ This plugin comes with a [few commands](https://github.com/bnomei/kirby3-janitor
- `janitor:render`, render a certain page or all pages (to create thumb jobs)
- `janitor:thumbs`, process thumb jobs of a certain page or all pages
- `janitor:tinker`, run a REPL session in terminal
- `janitor:trash`, removes an entry from given cache by key or page (default: pages cache)
- `janitor:undertaker`, backups a page and its subpages to a zip. You need to manually trigger it with a [hook](https://github.com/bnomei/kirby3-janitor/blob/master/tests/site/config/config.php).

The plugin will register these commands starting with `janitor:*` automatically - no copying required.<br>But if you want to re-use any of the other example provided you need to copy them to your `site/commands`-folder
Expand Down
85 changes: 85 additions & 0 deletions commands/trash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

if (! class_exists('Bnomei\Janitor')) {
require_once __DIR__.'/../classes/Janitor.php';
}

use Bnomei\Janitor;
use Kirby\CLI\CLI;

/*
* PANEL:
* janitor:trash --name pages --key "home.en.html"
* janitor:trash --name pages --page "page://vf0xqIlpU0ZlSorI"
* janitor:trash --page "page://vf0xqIlpU0ZlSorI"
* janitor:trash
*
* CLI:
* env KIRBY_HOST=janitor.test vendor/bin/kirby janitor:trash --page "page://vf0xqIlpU0ZlSorI"
*/
return [
'description' => 'Removes an entry from a cache',
'args' => [
'name' => [
'prefix' => 'n',
'longPrefix' => 'name',
'description' => 'Name of the cache',
'defaultValue' => 'pages',
],
'key' => [
'prefix' => 'k',
'longPrefix' => 'key',
'description' => 'Key for a cache entry',
'required' => false,
],
] + Janitor::ARGS, // page, file, user, site, data, model
'command' => static function (CLI $cli): void {
$name = $cli->arg('name');
$name = empty($name) ? 'pages' : $name;

$page = $cli->arg('page');
if ($page && $name === 'pages') {
if ($page = $cli->kirby()->page($page)) {
if ($cli->kirby()->multilang()) {
foreach ($cli->kirby()->languages() as $language) {
$cacheId = [$page->id(), $language->code()];
if ($name === 'pages') {
$cacheId[] = 'html'; // render content type
}
$cacheId = implode('.', $cacheId);
$cli->kirby()->cache($name)->remove($cacheId);
$cli->success('The entry "'.$cacheId.'" in the cache "'.$name.'" has been removed.');
}
} else {
$cacheId = [$page->id()];

if ($name === 'pages') {
$cacheId[] = 'html'; // render content type
}

$cacheId = implode('.', $cacheId);
$cli->kirby()->cache($name)->remove($cacheId);
$cli->success('The entry "'.$cacheId.'" in the cache "'.$name.'" has been removed.');
}

janitor()->data($cli->arg('command'), [
'status' => 200,
]);

return;
}
}

// if it has a key just remove that
if ($key = $cli->arg('key')) {
$cli->kirby()->cache($name)->remove($key);
$cli->success('The entry "'.$key.'" in the cache "'.$name.'" has been removed.');
}

janitor()->data($cli->arg('command'), [
'status' => 200,
]);
},
];
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bnomei/kirby3-janitor",
"type": "kirby-plugin",
"version": "4.3.1",
"version": "4.4.0",
"license": "MIT",
"homepage": "https://github.com/bnomei/kirby3-janitor",
"description": "Kirby Plugin for running commands like cleaning the cache from within the Panel, PHP code or a cronjob",
Expand Down
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'janitor:render' => require __DIR__.'/commands/render.php',
'janitor:thumbs' => require __DIR__.'/commands/thumbs.php',
'janitor:tinker' => require __DIR__.'/commands/tinker.php',
'janitor:trash' => require __DIR__.'/commands/trash.php',
'janitor:undertaker' => require __DIR__.'/commands/undertaker.php',
],
'fields' => [
Expand Down
6 changes: 6 additions & 0 deletions tests/site/blueprints/pages/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ fields:
success: Pong
error: BAMM

janitor_trash:
type: janitor
command: 'janitor:trash'
label: Removes Pages Cache for this page
icon: trash

janitor_flushpages:
type: janitor
command: 'janitor:flush --name pages'
Expand Down
6 changes: 6 additions & 0 deletions tests/site/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
'debug' => true,
'languages' => true,

'cache' => [
'pages' => [
'active' => true,
],
],

'bnomei.janitor.secret' => 'e9fe51f94eadabf54',

// janitor v2 job callback
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php return array(
'root' => array(
'name' => 'bnomei/kirby3-janitor',
'pretty_version' => '4.3.1',
'version' => '4.3.1.0',
'pretty_version' => '4.4.0',
'version' => '4.4.0.0',
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
Expand All @@ -11,8 +11,8 @@
),
'versions' => array(
'bnomei/kirby3-janitor' => array(
'pretty_version' => '4.3.1',
'version' => '4.3.1.0',
'pretty_version' => '4.4.0',
'version' => '4.4.0.0',
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
Expand Down

0 comments on commit 92c1654

Please sign in to comment.