|
3 | 3 | declare(strict_types = 1); |
4 | 4 |
|
5 | 5 | use Kirby\CLI\CLI; |
| 6 | +use Kirby\Cms\Languages; |
6 | 7 |
|
7 | 8 | $cleanContent = function ( |
| 9 | + CLI $cli, |
8 | 10 | Generator $collection, |
9 | 11 | array|null $ignore = null, |
10 | | - string|null $lang = null |
| 12 | + string $lang = 'default', |
| 13 | + bool $dryrun = false, |
11 | 14 | ): void { |
12 | 15 | foreach ($collection as $item) { |
13 | 16 | // get all fields in the content file |
|
32 | 35 | $fieldsToBeDeleted = array_diff($contentFieldKeys, $blueprintFieldKeys); |
33 | 36 |
|
34 | 37 | // update page only if there are any fields to be deleted |
35 | | - if (count($fieldsToBeDeleted) > 0) { |
36 | | - // create a mapping: lowercase => original field name |
37 | | - $lowercaseToOriginal = array_combine($contentFieldKeys, $originalContentKeys); |
38 | | - |
39 | | - // build data array with original field names as keys and null as values |
40 | | - $data = []; |
41 | | - foreach ($fieldsToBeDeleted as $lowercaseField) { |
42 | | - $originalField = $lowercaseToOriginal[$lowercaseField]; |
43 | | - $data[$originalField] = null; |
44 | | - } |
| 38 | + if (count($fieldsToBeDeleted) === 0) { |
| 39 | + continue; |
| 40 | + } |
45 | 41 |
|
46 | | - // get the latest version of the item |
47 | | - $version = $item->version('latest'); |
| 42 | + // create a mapping: lowercase => original field name |
| 43 | + $lowercaseToOriginal = array_combine($contentFieldKeys, $originalContentKeys); |
48 | 44 |
|
49 | | - // check if the version exists for the given language |
50 | | - // and try to update the page with the data |
51 | | - if ($version->exists($lang ?? 'default') === true) { |
52 | | - $version->update($data, $lang ?? 'default'); |
53 | | - } |
| 45 | + // build data array with original field names as keys and null as values |
| 46 | + $data = []; |
| 47 | + |
| 48 | + foreach ($fieldsToBeDeleted as $lowercaseField) { |
| 49 | + $originalField = $lowercaseToOriginal[$lowercaseField]; |
| 50 | + $data[$originalField] = null; |
| 51 | + |
| 52 | + $cli->out('Remove "' . $originalField . '" from ' . $item::CLASS_ALIAS . ' (' . $item->id() . ')'); |
| 53 | + } |
| 54 | + |
| 55 | + // don't update models that have changes |
| 56 | + if ($item->version('changes')->exists() === true) { |
| 57 | + $cli->error('The ' . $item::CLASS_ALIAS . ' (' . $item->id() . ') has changes and cannot be cleaned. Save the changes and try again.'); |
| 58 | + } |
| 59 | + |
| 60 | + // in a dry-run, the models will not be updated |
| 61 | + if ($dryrun === true) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + // get the latest version of the item |
| 66 | + $version = $item->version('latest'); |
| 67 | + |
| 68 | + // check if the version exists for the given language |
| 69 | + // and try to update the page with the data |
| 70 | + if ($version->exists($lang) === true) { |
| 71 | + $version->update($data); |
54 | 72 | } |
55 | 73 | } |
56 | 74 | }; |
57 | 75 |
|
58 | 76 | return [ |
59 | 77 | '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.', |
| 78 | + 'args' => [ |
| 79 | + 'dry-run' => [ |
| 80 | + 'description' => 'Run the command without actually updating content', |
| 81 | + 'noValue' => true, |
| 82 | + ], |
| 83 | + ], |
60 | 84 | 'command' => static function (CLI $cli) use ($cleanContent): void { |
61 | 85 |
|
62 | | - $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?'); |
| 86 | + $kirby = $cli->kirby(); |
| 87 | + $dryrun = $cli->arg('dry-run'); |
63 | 88 |
|
64 | | - $kirby = $cli->kirby(); |
| 89 | + if ($dryrun === false) { |
| 90 | + $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?'); |
| 91 | + } |
65 | 92 |
|
66 | 93 | // Authenticate as almighty |
67 | 94 | $kirby->impersonate('kirby'); |
68 | 95 |
|
69 | 96 | // set the fields to be ignored |
70 | 97 | $ignore = ['uuid', 'title', 'slug', 'template', 'sort', 'focus']; |
71 | 98 |
|
72 | | - // call the script for all languages if multilang |
73 | | - if ($kirby->multilang() === true) { |
74 | | - $languages = $kirby->languages(); |
75 | | - |
76 | | - foreach ($languages as $language) { |
77 | | - // should call kirby models for each loop |
78 | | - // since generators cannot be cloned |
79 | | - // otherwise run into an exception |
80 | | - $collection = $kirby->models(); |
81 | | - |
82 | | - $cleanContent($collection, $ignore, $language->code()); |
83 | | - } |
84 | | - |
85 | | - } else { |
| 99 | + // go through all languages |
| 100 | + foreach (Languages::ensure() as $language) { |
| 101 | + // should call kirby models for each loop |
| 102 | + // since generators cannot be cloned |
| 103 | + // otherwise run into an exception |
86 | 104 | $collection = $kirby->models(); |
87 | | - $cleanContent($collection, $ignore); |
| 105 | + |
| 106 | + $cleanContent($cli, $collection, $ignore, $language->code(), $dryrun); |
88 | 107 | } |
89 | 108 |
|
90 | 109 | $cli->success('The content files have been cleaned'); |
|
0 commit comments