From c8678775e296205884b9be8085726c95bb40e8f7 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Wed, 29 Nov 2023 23:38:56 +0800 Subject: [PATCH 1/5] `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 2/5] 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 3/5] 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 4/5] 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 5/5] 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