I have built a CLI command to update pages.
As soon as I call $page->update(), the call is executed, but the “out” never appears in the console.
$cli->out('start ');
if (count($fieldsToUpdate) > 0) {
$updatedPage = $page->update($fieldsToUpdate);
}
$cli->out('end ');
A simple $cli->out($page->title()); inside the if statement works.
texnixe
December 16, 2023, 7:24pm
2
Could you please provide your complete code for testing?
Extracted to the minimum:
\Kirby\Cms\App::plugin('xxx/api', [
'commands' => [
'test' => [
'args' => [],
'command' => function ($cli) {
kirby()->impersonate('kirby');
$siteIndex = site()->index();
$pageCount = $siteIndex->count();
$pages = $siteIndex->offset(2)->limit(2);
$index = 0;
foreach ($pages as $page) {
$cli->out($index . '/' . $pageCount . ' start ');
$updatedPage = $page->update([], null, true);
$cli->out($index . '/' . $pageCount . ' end');
$index++;
}
$cli->success('done!');
}
],
]
],
);```
texnixe
December 16, 2023, 11:09pm
4
Hm, getting this in console when running kirby test
0/24 start
0/24 end
1/24 start
1/24 end
done!
Thanks for testing, @texnixe !
My output is only:
0/866 start
Kirby 3.9.7 and 3.9.8
Do you have any idea what I can do?
texnixe
December 17, 2023, 8:53am
6
Very likely that you get an error while updating, try
foreach ($pages as $page) {
$cli->out($index . '/' . $pageCount . ' start ');
$updatedPage = $page->update([], null, true); // true validates the content to update, this might cause an error
dump($updatedPage->content()->toArray());
$cli->out($index . '/' . $pageCount . ' end');
$index++;
}
Thanks for your idea!
I tried it also without validation and to dump it like this:
dump($updatedPage->content()->toArray());
and this
$cli->out( dump($updatedPage->content()->toArray()));
and i tried different pages (via offset). I can save the pages without issues in the panel. And the content itself is updated if data is provided.
texnixe
December 17, 2023, 9:39am
8
And this does not output anything on the command line?
Sadly, nothing. I will run the code on a colleague’s computer for testing.
texnixe
December 17, 2023, 9:43am
10
It might be worthwhile to try it with a fresh Starterkit.
Are you using the latest version of the CLI (1.1.1)?
Which PHP version?
PHP 8.1.26
Kirby CLI 1.1.1
Kirby 3.9.7 + 3.9.8
And yes plain starterkit is the next step.
bnomei
December 17, 2023, 10:25pm
12
you could try wrapping the foreach loop in a try-catch-clause and write any error messages to file in appending mode.
It is a bug in Kirby if no URL is defined in the config. Would be good if it appears as error in the output. You can close the ticket.
bnomei
December 22, 2023, 5:31pm
14
you mean that running in the cli the $_SERVER['host'] is empty and kirby can not determine urls as usual? yes that can be fixed by hardcoding the url option in the config. there has been related discussions about enviroments/config in the cli repo.
opened 06:56PM - 30 Jan 23 UTC
For many tasks a user might want to do with a cli command, configuration differs… for production/staging/dev environments.
Kirby solves this very elegantly with `config.domain.ext.php` files, but these are not available in the cli commands (Logically, even though that was not a fun debugging session).
I'd like then, to somehow pass the environment - probably the domain to use, so we could use the existing configuration - to cli.
There are two options:
1.) Use a command argument
```shell
kirby custom:command -e production.com -a 'argument for command'
```
2.) Use an environment value
```shell
env env=production.com kirby custom:command -a 'argument for command'
```
I personally prefer the option 1 (argument), but that would be a first kind of argument not passed through to command, so I'm not sure if that's a good idea or not.