From 12c4a150e0b5d34a98fb2aece2c7e3953b4de33a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Mon, 25 Jan 2016 14:48:37 +0100 Subject: [PATCH] [Confluence] Delete pages that exist on confluence but not in the documentation --- global.json | 3 +- libs/Console/Generate.php | 17 ++++--- libs/Format/Confluence/Publisher.php | 70 +++++++++++++++++++++------- 3 files changed, 66 insertions(+), 24 deletions(-) diff --git a/global.json b/global.json index 37c4bca..617e56f 100644 --- a/global.json +++ b/global.json @@ -42,6 +42,7 @@ }, "confluence": { - "prefix": "" + "prefix": "", + "delete": false } } diff --git a/libs/Console/Generate.php b/libs/Console/Generate.php index 45aafc9..4e058c0 100644 --- a/libs/Console/Generate.php +++ b/libs/Console/Generate.php @@ -1,8 +1,8 @@ setName('generate') ->setDescription('Generate documentation') - ->addOption('configuration', 'c', InputArgument::OPTIONAL, 'Configuration file') - ->addOption('format', 'f', InputArgument::OPTIONAL, 'Output format, html or confluence', 'html') - ->addOption('processor', 'p', InputArgument::OPTIONAL, 'Manipulations on the tree') - ->addOption('source', 's', InputArgument::OPTIONAL, 'Where to take the documentation from') - ->addOption('destination', 'd', InputArgument::OPTIONAL, $description, 'static'); + ->addOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Configuration file') + ->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format, html or confluence', 'html') + ->addOption('processor', 'p', InputOption::VALUE_REQUIRED, 'Manipulations on the tree') + ->addOption('source', 's', InputOption::VALUE_REQUIRED, 'Where to take the documentation from') + ->addOption('delete', null, InputOption::VALUE_NONE, 'Delete pages not linked to a documentation page (confluence)') + ->addOption('destination', 'd', InputOption::VALUE_REQUIRED, $description, 'static'); } protected function execute(InputInterface $input, OutputInterface $output) @@ -58,6 +59,10 @@ class Generate extends SymfonyCommand $daux->initializeConfiguration($input->getOption('configuration')); + if ($input->getOption('delete')) { + $daux->getParams()['confluence']['delete'] = true; + } + return $daux; } diff --git a/libs/Format/Confluence/Publisher.php b/libs/Format/Confluence/Publisher.php index 9c8cc9f..4796823 100644 --- a/libs/Format/Confluence/Publisher.php +++ b/libs/Format/Confluence/Publisher.php @@ -74,14 +74,21 @@ class Publisher ); $published = $this->run( - "Create placeholder pages...", + "Create placeholder pages...\n", function() use ($tree, $published) { return $this->createRecursive($this->confluence['ancestor_id'], $tree, $published); } ); $this->output->writeLn("Publishing updates..."); - $this->updateRecursive($this->confluence['ancestor_id'], $tree, $published); + $published = $this->updateRecursive($this->confluence['ancestor_id'], $tree, $published); + + $this->output->writeLn("Deleting obsolete pages..."); + if (!$this->shouldDelete()) { + echo "> The following pages will not be deleted, but just listed for information.\n"; + echo "> If you want to delete these pages, you need to set the --delete flag on the command."; + } + $this->deleteRecursive($published); } protected function niceTitle($title) @@ -115,20 +122,22 @@ class Publisher { $published = $callback($parent_id, $entry, $published); - if (array_key_exists('children', $entry)) { - foreach ($entry['children'] as $child) { - $pub = []; - if (isset($published['children']) && array_key_exists($child['title'], $published['children'])) { - $pub = $published['children'][$child['title']]; - } + if (!array_key_exists('children', $entry)) { + return $published; + } - $published['children'][$child['title']] = $this->recursiveWithCallback( - $published['id'], - $child, - $pub, - $callback - ); + foreach ($entry['children'] as $child) { + $pub = []; + if (isset($published['children']) && array_key_exists($child['title'], $published['children'])) { + $pub = $published['children'][$child['title']]; } + + $published['children'][$child['title']] = $this->recursiveWithCallback( + $published['id'], + $child, + $pub, + $callback + ); } return $published; @@ -137,9 +146,6 @@ class Publisher protected function createRecursive($parent_id, $entry, $published) { $callback = function($parent_id, $entry, $published) { - - //TODO :: remove deleted pages - // nothing to do if the ID already exists if (array_key_exists('id', $published)) { return $published; @@ -164,6 +170,7 @@ class Publisher if (array_key_exists('id', $published) && array_key_exists('page', $entry)) { $this->updatePage($parent_id, $entry, $published); } + $published['needed'] = true; return $published; }; @@ -171,6 +178,35 @@ class Publisher return $this->recursiveWithCallback($parent_id, $entry, $published, $callback); } + protected function shouldDelete() + { + return array_key_exists('delete', $this->confluence) && $this->confluence['delete']; + } + + protected function deleteRecursive($published, $prefix = '') + { + foreach($published['children'] as $child) { + if (array_key_exists('children', $child) && count($child['children'])) { + $this->deleteRecursive($child, $child['title'] . '/'); + } + + if (!array_key_exists('needed', $child)) { + + if ($this->shouldDelete()) { + $this->run( + "- " . $prefix . $child['title'], + function() use ($child) { + $this->client->deletePage($child['id']); + } + ); + + } else { + echo "- " . $prefix . $child['title'] . "\n"; + } + } + } + } + protected function shouldUpdate($local, $published) { if (!array_key_exists('content', $published)) {