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..3957f88 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -8,13 +8,12 @@ $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 +44,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, diff --git a/commands/clean/content.php b/commands/clean/content.php new file mode 100644 index 0000000..d470afb --- /dev/null +++ b/commands/clean/content.php @@ -0,0 +1,79 @@ +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 { + + $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 + $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'); + } +];