From fd26c83cfd341b2521902fb5fa8a9b5e6a5455a7 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 5 Feb 2024 17:19:07 +0100 Subject: [PATCH 01/11] Improve exception handling for argument parsing --- src/CLI/CLI.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/CLI/CLI.php b/src/CLI/CLI.php index 58614e1..58a2471 100644 --- a/src/CLI/CLI.php +++ b/src/CLI/CLI.php @@ -518,12 +518,11 @@ public function run(?string $name = null, ...$args): void ...$args ]; - $exception = null; - try { $this->climate->arguments->parse($argv); } catch (Throwable $e) { - $exception = $e; + $this->error($e->getMessage()); + exit; } // enable quiet mode @@ -537,10 +536,6 @@ public function run(?string $name = null, ...$args): void return; } - if ($exception !== null) { - throw $exception; - } - $command['command']($this); } From c8678775e296205884b9be8085726c95bb40e8f7 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Wed, 29 Nov 2023 23:38:56 +0800 Subject: [PATCH 02/11] `clean:content` command --- commands/clean/content.php | 77 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 commands/clean/content.php diff --git a/commands/clean/content.php b/commands/clean/content.php new file mode 100644 index 0000000..d8049c2 --- /dev/null +++ b/commands/clean/content.php @@ -0,0 +1,77 @@ +content($lang)->fields(); + + // unset all fields in the `$ignore` array + foreach ($ignore as $field) { + if (array_key_exists($field, $contentFields) === true) { + unset($contentFields[$field]); + } + } + + // get the keys + $contentFields = array_keys($contentFields); + + // get all field keys from blueprint + $blueprintFields = array_keys($item->blueprint()->fields()); + + // get all field keys that are in $contentFields but not in $blueprintFields + $fieldsToBeDeleted = array_diff($contentFields, $blueprintFields); + + // update page only if there are any fields to be deleted + if (count($fieldsToBeDeleted) > 0) { + + // flip keys and values and set new values to null + $data = array_map(fn ($value) => null, array_flip($fieldsToBeDeleted)); + + // try to update the page with the data + try { + $item->update($data, $lang); + } catch (Exception $e) { + throw $e->getMessage(); + } + } + } +} + +return [ + 'description' => 'Deletes all fields from page, file or user content files that are not defined in the blueprint, no matter if they contain content or not.', + 'command' => static function (CLI $cli): void { + $kirby = $cli->kirby()->root('media'); + + // Authenticate as almighty + $kirby->impersonate('kirby'); + + // Define your collection + $collection = $kirby->models(); + + // set the fields to be ignored + $ignore = ['uuid', 'title', 'slug', 'template', 'sort', 'focus']; + + // call the script for all languages if multilang + if ($kirby->multilang() === true) { + $languages = $kirby->languages(); + + foreach ($languages as $language) { + clean($collection, $ignore, $language->code()); + } + + } else { + clean($collection, $ignore); + } + + $cli->success('The content files have been cleaned'); + } +]; From c7693514be4294becd4cf07831ef88bbc0b9e4da Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 5 Feb 2024 17:26:11 +0100 Subject: [PATCH 03/11] Fix CS issue and weird parts of the code --- commands/clean/content.php | 73 +++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/commands/clean/content.php b/commands/clean/content.php index d8049c2..4c9dd66 100644 --- a/commands/clean/content.php +++ b/commands/clean/content.php @@ -3,53 +3,52 @@ declare(strict_types = 1); use Kirby\CLI\CLI; -use Kirby\Cms\Collection; function clean( - Collection $collection, + Generator $collection, array|null $ignore = null, string|null $lang = null ): void { - foreach($collection as $item) { - // get all fields in the content file - $contentFields = $item->content($lang)->fields(); - - // unset all fields in the `$ignore` array - foreach ($ignore as $field) { - if (array_key_exists($field, $contentFields) === true) { - unset($contentFields[$field]); - } - } - - // get the keys - $contentFields = array_keys($contentFields); - - // get all field keys from blueprint - $blueprintFields = array_keys($item->blueprint()->fields()); - - // get all field keys that are in $contentFields but not in $blueprintFields - $fieldsToBeDeleted = array_diff($contentFields, $blueprintFields); - - // update page only if there are any fields to be deleted - if (count($fieldsToBeDeleted) > 0) { - - // flip keys and values and set new values to null - $data = array_map(fn ($value) => null, array_flip($fieldsToBeDeleted)); - - // try to update the page with the data - try { - $item->update($data, $lang); - } catch (Exception $e) { - throw $e->getMessage(); - } - } - } + foreach($collection as $item) { + // get all fields in the content file + $contentFields = $item->content($lang)->fields(); + + // unset all fields in the `$ignore` array + foreach ($ignore as $field) { + if (array_key_exists($field, $contentFields) === true) { + unset($contentFields[$field]); + } + } + + // get the keys + $contentFields = array_keys($contentFields); + + // get all field keys from blueprint + $blueprintFields = array_keys($item->blueprint()->fields()); + + // get all field keys that are in $contentFields but not in $blueprintFields + $fieldsToBeDeleted = array_diff($contentFields, $blueprintFields); + + // update page only if there are any fields to be deleted + if (count($fieldsToBeDeleted) > 0) { + + // flip keys and values and set new values to null + $data = array_map(fn ($value) => null, array_flip($fieldsToBeDeleted)); + + // try to update the page with the data + try { + $item->update($data, $lang); + } catch (Exception $e) { + throw $e->getMessage(); + } + } + } } return [ 'description' => 'Deletes all fields from page, file or user content files that are not defined in the blueprint, no matter if they contain content or not.', 'command' => static function (CLI $cli): void { - $kirby = $cli->kirby()->root('media'); + $kirby = $cli->kirby(); // Authenticate as almighty $kirby->impersonate('kirby'); From 22c12d82f87c74d4c6897988b2acdcbaa3a5b923 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 5 Feb 2024 17:37:39 +0100 Subject: [PATCH 04/11] Update the rules and CS version --- .github/workflows/ci.yml | 2 +- .php-cs-fixer.dist.php | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eec74dc..0279f10 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -176,7 +176,7 @@ jobs: uses: shivammathur/setup-php@e6f75134d35752277f093989e72e140eaa222f35 # pin@v2 with: coverage: none - tools: php-cs-fixer:3.8.0 + tools: php-cs-fixer:3.49.0 - name: Cache analysis data id: finishPrepare diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index f7c903b..68766fe 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -1,20 +1,17 @@ exclude('node_modules') - ->exclude('_templates') ->in(__DIR__); $config = new PhpCsFixer\Config(); return $config ->setRules([ - '@PSR1' => true, - '@PSR2' => true, + '@PSR12' => true, 'align_multiline_comment' => ['comment_type' => 'phpdocs_like'], 'array_indentation' => true, 'array_syntax' => ['syntax' => 'short'], 'cast_spaces' => ['space' => 'none'], - 'class_keyword_remove' => false, + // 'class_keyword_remove' => true, // replaces static::class with 'static' (won't work) 'combine_consecutive_issets' => true, 'combine_consecutive_unsets' => true, 'combine_nested_dirname' => true, @@ -45,6 +42,7 @@ 'no_unused_imports' => true, 'no_useless_return' => true, 'ordered_imports' => ['sort_algorithm' => 'alpha'], + // 'phpdoc_add_missing_param_annotation' => ['only_untyped' => false], // adds params in the wrong order 'phpdoc_align' => ['align' => 'left'], 'phpdoc_indent' => true, 'phpdoc_scalar' => true, From 9e3937f4abf3ca40e1fd78747a402c55c05b91bd Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 5 Feb 2024 17:41:06 +0100 Subject: [PATCH 05/11] Bring back the ignore rules --- .php-cs-fixer.dist.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 68766fe..3957f88 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -1,6 +1,8 @@ exclude('node_modules') + ->exclude('_templates') ->in(__DIR__); $config = new PhpCsFixer\Config(); From 8ae14079ad84b5ff3852457fc5a21ff6e70b8c0b Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 5 Feb 2024 17:48:30 +0100 Subject: [PATCH 06/11] Add confirmation --- commands/clean/content.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/commands/clean/content.php b/commands/clean/content.php index 4c9dd66..d470afb 100644 --- a/commands/clean/content.php +++ b/commands/clean/content.php @@ -48,6 +48,9 @@ function clean( return [ 'description' => 'Deletes all fields from page, file or user content files that are not defined in the blueprint, no matter if they contain content or not.', 'command' => static function (CLI $cli): void { + + $cli->confirmToContinue('This will delete all fields from content files that are not defined in blueprints, no matter if they contain content or not. Are you sure?'); + $kirby = $cli->kirby(); // Authenticate as almighty From 31c0241a7069e061ce05e60cf28917837a5774a1 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Tue, 6 Feb 2024 10:32:51 +0100 Subject: [PATCH 07/11] Fix broken installation commands when the folder has whitespaces --- commands/download.php | 3 ++- commands/unzip.php | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/commands/download.php b/commands/download.php index b42b805..995f9cc 100755 --- a/commands/download.php +++ b/commands/download.php @@ -20,6 +20,7 @@ 'command' => static function (CLI $cli): void { $client = new Client(); $progress = $cli->progress()->total(100); + $file = $cli->arg('file'); try { $response = $client->get($cli->arg('url'), [ @@ -35,7 +36,7 @@ }, ]); - file_put_contents($cli->arg('file'), (string)$response->getBody()); + file_put_contents($file, (string)$response->getBody()); } catch (Throwable $e) { throw new Exception('The file could not be downloaded. (Status: ' . $e->getResponse()->getStatusCode() . ')'); } diff --git a/commands/unzip.php b/commands/unzip.php index 067272f..8b25d53 100755 --- a/commands/unzip.php +++ b/commands/unzip.php @@ -29,7 +29,7 @@ } // extract the zip file - exec('unzip ' . $file . ' -d ' . $to); + exec('unzip ' . escapeshellarg($file) . ' -d ' . escapeshellarg($to)); $to = realpath($to); @@ -40,7 +40,7 @@ throw new Exception('The archive directory could not be found'); } - exec('mv ' . $to . '/*/{.[!.],}* ' . $to . '/'); - exec('rm -rf ' . $archive); + exec('mv ' . escapeshellarg($to) . '/*/{.[!.],}* ' . escapeshellarg($to) . '/'); + exec('rm -rf ' . escapeshellarg($archive)); } ]; From b8d161da7c884ba133de7a657f21517e7078e887 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Tue, 6 Feb 2024 10:37:09 +0100 Subject: [PATCH 08/11] Bump the version number --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a5627a8..c6a51c0 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "getkirby/cli", "description": "Kirby command line interface", "license": "MIT", - "version": "1.2.0", + "version": "1.3.0", "keywords": [ "kirby", "cms", From 632f1af8e096b2d1bb1493fbf437447a59cd8b59 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Tue, 6 Feb 2024 10:46:37 +0100 Subject: [PATCH 09/11] Update phpunit config and gitignore --- .gitignore | 15 +++++++------- phpunit.xml.dist | 53 ++++++++++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index aec3c3f..e73d216 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,14 @@ -# OS files +# os .DS_Store -# Vendor files +# vendor files /node_modules /vendor -# Cache and temporary files -/.cache -/.php-cs-fixer.cache -/.phpunit.result.cache +# cs fixer +.php-cs-fixer.cache + +# tests +.phpunit.cache /tests/coverage -/tests/*/tmp \ No newline at end of file +/tests/tmp diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a64a205..28dbd48 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,32 +1,41 @@ - - + - - - ./src - - - - ./src/Kql/Interceptors - - + + + ./src + + - - ./tests - - + + ./tests + + + + + + - - - + From 1875453bba96183b1a46f3ee93cdea612905fcac Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Tue, 6 Feb 2024 10:49:01 +0100 Subject: [PATCH 10/11] Update composer.lock --- composer.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.lock b/composer.lock index a85e0d6..faffede 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f843b10a4714c43702cdb0414727e0e9", + "content-hash": "919c6e5cb0284e3c87cf7cf7b6d96eb0", "packages": [ { "name": "guzzlehttp/guzzle", From 0f613f89f5989b66174cdd184611f7187a6234a7 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Tue, 6 Feb 2024 11:10:18 +0100 Subject: [PATCH 11/11] Add new command to readme and remove outdated copyright date --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e39b36e..7580bd0 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ This should print the Kirby CLI version and a list of available commands ## Available core commands ``` +- kirby clean:content - kirby clear:cache - kirby clear:media - kirby clear:sessions @@ -279,7 +280,7 @@ return [ --- -© 2009-2022 Bastian Allgeier +© 2009 Bastian Allgeier [getkirby.com](https://getkirby.com) · [License agreement](./LICENSE.md)