|
3 | 3 | declare(strict_types = 1);
|
4 | 4 |
|
5 | 5 | use Kirby\CLI\CLI;
|
6 |
| -use Kirby\Cms\Collection; |
7 | 6 |
|
8 | 7 | function clean(
|
9 |
| - Collection $collection, |
| 8 | + Generator $collection, |
10 | 9 | array|null $ignore = null,
|
11 | 10 | string|null $lang = null
|
12 | 11 | ): void {
|
13 |
| - foreach($collection as $item) { |
14 |
| - // get all fields in the content file |
15 |
| - $contentFields = $item->content($lang)->fields(); |
16 |
| - |
17 |
| - // unset all fields in the `$ignore` array |
18 |
| - foreach ($ignore as $field) { |
19 |
| - if (array_key_exists($field, $contentFields) === true) { |
20 |
| - unset($contentFields[$field]); |
21 |
| - } |
22 |
| - } |
23 |
| - |
24 |
| - // get the keys |
25 |
| - $contentFields = array_keys($contentFields); |
26 |
| - |
27 |
| - // get all field keys from blueprint |
28 |
| - $blueprintFields = array_keys($item->blueprint()->fields()); |
29 |
| - |
30 |
| - // get all field keys that are in $contentFields but not in $blueprintFields |
31 |
| - $fieldsToBeDeleted = array_diff($contentFields, $blueprintFields); |
32 |
| - |
33 |
| - // update page only if there are any fields to be deleted |
34 |
| - if (count($fieldsToBeDeleted) > 0) { |
35 |
| - |
36 |
| - // flip keys and values and set new values to null |
37 |
| - $data = array_map(fn ($value) => null, array_flip($fieldsToBeDeleted)); |
38 |
| - |
39 |
| - // try to update the page with the data |
40 |
| - try { |
41 |
| - $item->update($data, $lang); |
42 |
| - } catch (Exception $e) { |
43 |
| - throw $e->getMessage(); |
44 |
| - } |
45 |
| - } |
46 |
| - } |
| 12 | + foreach($collection as $item) { |
| 13 | + // get all fields in the content file |
| 14 | + $contentFields = $item->content($lang)->fields(); |
| 15 | + |
| 16 | + // unset all fields in the `$ignore` array |
| 17 | + foreach ($ignore as $field) { |
| 18 | + if (array_key_exists($field, $contentFields) === true) { |
| 19 | + unset($contentFields[$field]); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + // get the keys |
| 24 | + $contentFields = array_keys($contentFields); |
| 25 | + |
| 26 | + // get all field keys from blueprint |
| 27 | + $blueprintFields = array_keys($item->blueprint()->fields()); |
| 28 | + |
| 29 | + // get all field keys that are in $contentFields but not in $blueprintFields |
| 30 | + $fieldsToBeDeleted = array_diff($contentFields, $blueprintFields); |
| 31 | + |
| 32 | + // update page only if there are any fields to be deleted |
| 33 | + if (count($fieldsToBeDeleted) > 0) { |
| 34 | + |
| 35 | + // flip keys and values and set new values to null |
| 36 | + $data = array_map(fn ($value) => null, array_flip($fieldsToBeDeleted)); |
| 37 | + |
| 38 | + // try to update the page with the data |
| 39 | + try { |
| 40 | + $item->update($data, $lang); |
| 41 | + } catch (Exception $e) { |
| 42 | + throw $e->getMessage(); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
47 | 46 | }
|
48 | 47 |
|
49 | 48 | return [
|
50 | 49 | '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.',
|
51 | 50 | 'command' => static function (CLI $cli): void {
|
52 |
| - $kirby = $cli->kirby()->root('media'); |
| 51 | + $kirby = $cli->kirby(); |
53 | 52 |
|
54 | 53 | // Authenticate as almighty
|
55 | 54 | $kirby->impersonate('kirby');
|
|
0 commit comments