2017-11-07 22:08:30 +01:00
|
|
|
<?php namespace Todaymade\Daux\Format\Confluence;
|
|
|
|
|
|
|
|
class PublisherDelete
|
|
|
|
{
|
|
|
|
/**
|
2017-11-07 22:44:27 +01:00
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface
|
2017-11-07 22:08:30 +01:00
|
|
|
*/
|
|
|
|
public $output;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array files that can be deleted
|
|
|
|
*/
|
|
|
|
protected $deletable;
|
|
|
|
|
|
|
|
/**
|
2020-04-22 22:24:52 +02:00
|
|
|
* @var bool should delete ?
|
2017-11-07 22:08:30 +01:00
|
|
|
*/
|
|
|
|
protected $delete;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Api
|
|
|
|
*/
|
|
|
|
protected $client;
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
public function __construct($output, bool $delete, $client)
|
2017-11-07 22:08:30 +01:00
|
|
|
{
|
|
|
|
$this->output = $output;
|
|
|
|
$this->delete = $delete;
|
|
|
|
$this->client = $client;
|
2017-11-07 22:54:31 +01:00
|
|
|
|
|
|
|
$this->deletable = [];
|
2017-11-07 22:08:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function listDeletable($published, $prefix = '')
|
|
|
|
{
|
|
|
|
foreach ($published['children'] as $child) {
|
|
|
|
if (array_key_exists('children', $child) && count($child['children'])) {
|
|
|
|
$this->listDeletable($child, $child['title'] . '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists('needed', $child)) {
|
|
|
|
$this->deletable[$child['id']] = $prefix . $child['title'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle($published)
|
|
|
|
{
|
|
|
|
$this->listDeletable($published);
|
|
|
|
|
|
|
|
if (!count($this->deletable)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->delete) {
|
|
|
|
$this->doDelete();
|
|
|
|
} else {
|
|
|
|
$this->displayDeletable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 21:55:53 +02:00
|
|
|
protected function doDelete()
|
|
|
|
{
|
2017-11-07 22:08:30 +01:00
|
|
|
$this->output->writeLn('Deleting obsolete pages...');
|
|
|
|
foreach ($this->deletable as $id => $title) {
|
|
|
|
$this->output->writeLn("- $title");
|
|
|
|
$this->client->deletePage($id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 21:55:53 +02:00
|
|
|
protected function displayDeletable()
|
|
|
|
{
|
2017-11-07 22:08:30 +01:00
|
|
|
$this->output->writeLn('Listing obsolete pages...');
|
2020-04-22 22:24:52 +02:00
|
|
|
$this->output->writeLn('> The following pages will not be deleted, but just listed for information.');
|
|
|
|
$this->output->writeLn('> If you want to delete these pages, you need to set the --delete flag on the command.');
|
2017-11-07 22:08:30 +01:00
|
|
|
foreach ($this->deletable as $id => $title) {
|
|
|
|
$this->output->writeLn("- $title");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|