Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean:content command #55

Merged
merged 5 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
79 changes: 79 additions & 0 deletions commands/clean/content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types = 1);

use Kirby\CLI\CLI;

function clean(
Generator $collection,
array|null $ignore = null,
string|null $lang = null
): void {
foreach($collection as $item) {
bastianallgeier marked this conversation as resolved.
Show resolved Hide resolved
// 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 {

$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());
}

bastianallgeier marked this conversation as resolved.
Show resolved Hide resolved
} else {
clean($collection, $ignore);
}

$cli->success('The content files have been cleaned');
}
];
Loading