Skip to content

Commit

Permalink
Merge pull request #58 from getkirby/release/1.3.0
Browse files Browse the repository at this point in the history
1.3.0
  • Loading branch information
bastianallgeier authored Feb 6, 2024
2 parents 1d6d72f + 0f613f8 commit 1b7d168
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 47 deletions.
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
15 changes: 8 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# OS files
# os
.DS_Store

# Vendor files
# vendor files
/node_modules
/vendor

# Cache and temporary files
/.cache
/.php-cs-fixer.cache
/.phpunit.result.cache
# cs fixer
.php-cs-fixer.cache

# tests
.phpunit.cache
/tests/coverage
/tests/*/tmp
/tests/tmp
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ This should print the Kirby CLI version and a list of available commands
## Available core commands

```
- kirby clean:content
- kirby clear:cache
- kirby clear:media
- kirby clear:sessions
Expand Down Expand Up @@ -279,7 +280,7 @@ return [

---

© 2009-2022 Bastian Allgeier
© 2009 Bastian Allgeier
[getkirby.com](https://getkirby.com) · [License agreement](./LICENSE.md)


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

} else {
clean($collection, $ignore);
}

$cli->success('The content files have been cleaned');
}
];
3 changes: 2 additions & 1 deletion commands/download.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'command' => static function (CLI $cli): void {
$client = new Client();
$progress = $cli->progress()->total(100);
$file = $cli->arg('file');

try {
$response = $client->get($cli->arg('url'), [
Expand All @@ -35,7 +36,7 @@
},
]);

file_put_contents($cli->arg('file'), (string)$response->getBody());
file_put_contents($file, (string)$response->getBody());
} catch (Throwable $e) {
throw new Exception('The file could not be downloaded. (Status: ' . $e->getResponse()->getStatusCode() . ')');
}
Expand Down
6 changes: 3 additions & 3 deletions commands/unzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}

// extract the zip file
exec('unzip ' . $file . ' -d ' . $to);
exec('unzip ' . escapeshellarg($file) . ' -d ' . escapeshellarg($to));

$to = realpath($to);

Expand All @@ -40,7 +40,7 @@
throw new Exception('The archive directory could not be found');
}

exec('mv ' . $to . '/*/{.[!.],}* ' . $to . '/');
exec('rm -rf ' . $archive);
exec('mv ' . escapeshellarg($to) . '/*/{.[!.],}* ' . escapeshellarg($to) . '/');
exec('rm -rf ' . escapeshellarg($archive));
}
];
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "getkirby/cli",
"description": "Kirby command line interface",
"license": "MIT",
"version": "1.2.0",
"version": "1.3.0",
"keywords": [
"kirby",
"cms",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 31 additions & 22 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
<?xml version="1.0"?>

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"

xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
beStrictAboutOutputDuringTests="true"
bootstrap="tests/bootstrap.php"
cacheDirectory=".phpunit.cache"
colors="true"
forceCoversAnnotation="true"
verbose="true"
controlGarbageCollector="true"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
failOnDeprecation="true"
failOnEmptyTestSuite="true"
failOnIncomplete="true"
failOnNotice="true"
failOnRisky="true"
failOnWarning="true"
stderr="true"
>
<coverage>
<include>
<directory>./src</directory>
</include>

<exclude>
<directory suffix=".php">./src/Kql/Interceptors</directory>
</exclude>
</coverage>
<source>
<include>
<directory>./src</directory>
</include>
</source>

<testsuites>
<testsuite name="Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<testsuite name="Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>

<php>
<ini name="memory_limit" value="2048M"/>
</php>

<php>
<ini name="memory_limit" value="2048M" />
</php>
<coverage ignoreDeprecatedCodeUnits="true" />
</phpunit>
9 changes: 2 additions & 7 deletions src/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,11 @@ public function run(?string $name = null, ...$args): void
...$args
];

$exception = null;

try {
$this->climate->arguments->parse($argv);
} catch (Throwable $e) {
$exception = $e;
$this->error($e->getMessage());
exit;
}

// enable quiet mode
Expand All @@ -537,10 +536,6 @@ public function run(?string $name = null, ...$args): void
return;
}

if ($exception !== null) {
throw $exception;
}

$command['command']($this);
}

Expand Down

0 comments on commit 1b7d168

Please sign in to comment.