2015-05-22 14:48:09 +02:00
|
|
|
<?php namespace Todaymade\Daux\Format\Confluence;
|
|
|
|
|
|
|
|
use GuzzleHttp\Exception\BadResponseException;
|
2015-07-23 17:44:24 +02:00
|
|
|
use Todaymade\Daux\Console\RunAction;
|
2015-05-22 14:48:09 +02:00
|
|
|
|
|
|
|
class Publisher
|
|
|
|
{
|
2015-07-14 22:06:01 +02:00
|
|
|
use RunAction;
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
/**
|
2020-04-22 22:24:52 +02:00
|
|
|
* @var int terminal width
|
2015-05-22 14:48:09 +02:00
|
|
|
*/
|
2020-04-22 22:24:52 +02:00
|
|
|
public $width;
|
2015-05-22 14:48:09 +02:00
|
|
|
|
|
|
|
/**
|
2020-04-22 22:24:52 +02:00
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface
|
2015-05-22 14:48:09 +02:00
|
|
|
*/
|
2020-04-22 22:24:52 +02:00
|
|
|
public $output;
|
2015-05-22 14:48:09 +02:00
|
|
|
|
2015-07-14 22:06:01 +02:00
|
|
|
/**
|
2020-04-22 22:24:52 +02:00
|
|
|
* @var Api
|
2015-07-14 22:06:01 +02:00
|
|
|
*/
|
2020-04-22 22:24:52 +02:00
|
|
|
protected $client;
|
2015-07-14 22:06:01 +02:00
|
|
|
|
|
|
|
/**
|
2020-04-22 22:24:52 +02:00
|
|
|
* @var Config
|
2015-07-14 22:06:01 +02:00
|
|
|
*/
|
2020-04-22 22:24:52 +02:00
|
|
|
protected $confluence;
|
2015-07-14 22:06:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $confluence
|
|
|
|
*/
|
2019-12-05 21:25:58 +01:00
|
|
|
public function __construct(Config $confluence)
|
2015-05-22 14:48:09 +02:00
|
|
|
{
|
|
|
|
$this->confluence = $confluence;
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
$this->client = new Api($confluence->getBaseUrl(), $confluence->getUser(), $confluence->getPassword());
|
|
|
|
$this->client->setSpace($confluence->getSpaceId());
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
public function run($title, $closure)
|
|
|
|
{
|
2015-07-14 22:06:01 +02:00
|
|
|
try {
|
2018-06-05 20:31:51 +02:00
|
|
|
return $this->runAction($title, $this->width, $closure);
|
2015-07-14 22:06:01 +02:00
|
|
|
} catch (BadResponseException $e) {
|
2017-11-06 22:46:07 +01:00
|
|
|
$this->output->writeLn('<fg=red>' . $e->getMessage() . '</>');
|
2015-07-14 22:06:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
public function publish(array $tree)
|
|
|
|
{
|
2017-11-07 22:08:30 +01:00
|
|
|
$this->output->writeLn('Finding Root Page...');
|
|
|
|
$published = $this->getRootPage($tree);
|
2016-01-26 12:00:07 +01:00
|
|
|
|
2015-07-14 22:06:01 +02:00
|
|
|
$this->run(
|
2016-07-27 21:32:51 +02:00
|
|
|
'Getting already published pages...',
|
2020-04-22 21:55:53 +02:00
|
|
|
function () use (&$published) {
|
2015-07-14 22:06:01 +02:00
|
|
|
if ($published != null) {
|
2015-07-17 23:38:06 +02:00
|
|
|
$published['children'] = $this->client->getList($published['id'], true);
|
2015-07-14 22:06:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-05-22 16:15:28 +02:00
|
|
|
|
2015-07-14 22:06:01 +02:00
|
|
|
$published = $this->run(
|
2020-04-22 22:24:52 +02:00
|
|
|
'Create placeholder pages...',
|
2020-04-22 21:55:53 +02:00
|
|
|
function () use ($tree, $published) {
|
2019-12-05 21:25:58 +01:00
|
|
|
return $this->createRecursive($this->confluence->getAncestorId(), $tree, $published);
|
2015-07-14 22:06:01 +02:00
|
|
|
}
|
|
|
|
);
|
2015-05-22 14:48:09 +02:00
|
|
|
|
2016-07-27 21:32:51 +02:00
|
|
|
$this->output->writeLn('Publishing updates...');
|
2019-12-05 21:25:58 +01:00
|
|
|
$published = $this->updateRecursive($this->confluence->getAncestorId(), $tree, $published);
|
2016-01-25 14:48:37 +01:00
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
$delete = new PublisherDelete($this->output, $this->confluence->shouldAutoDeleteOrphanedPages(), $this->client);
|
2017-11-07 22:08:30 +01:00
|
|
|
$delete->handle($published);
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
|
2017-11-07 22:08:30 +01:00
|
|
|
protected function getRootPage($tree)
|
2015-05-22 14:48:09 +02:00
|
|
|
{
|
2019-12-05 21:25:58 +01:00
|
|
|
if ($this->confluence->hasAncestorId()) {
|
|
|
|
$pages = $this->client->getList($this->confluence->getAncestorId());
|
2017-11-07 22:08:30 +01:00
|
|
|
foreach ($pages as $page) {
|
|
|
|
if ($page['title'] == $tree['title']) {
|
|
|
|
return $page;
|
|
|
|
}
|
|
|
|
}
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
if ($this->confluence->hasRootId()) {
|
|
|
|
$published = $this->client->getPage($this->confluence->getRootId());
|
|
|
|
$this->confluence->setAncestorId($published['ancestor_id']);
|
2020-04-22 22:24:52 +02:00
|
|
|
|
2017-11-07 22:08:30 +01:00
|
|
|
return $published;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \RuntimeException('You must at least specify a `root_id` or `ancestor_id` in your confluence configuration.');
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function createPage($parent_id, $entry, $published)
|
|
|
|
{
|
2017-11-07 22:08:30 +01:00
|
|
|
echo '- ' . PublisherUtilities::niceTitle($entry['file']->getUrl()) . "\n";
|
2015-05-22 14:48:09 +02:00
|
|
|
$published['version'] = 1;
|
2016-01-26 12:00:07 +01:00
|
|
|
$published['title'] = $entry['title'];
|
2016-07-27 21:32:51 +02:00
|
|
|
$published['id'] = $this->client->createPage($parent_id, $entry['title'], 'The content will come very soon !');
|
2015-05-22 14:48:09 +02:00
|
|
|
|
|
|
|
return $published;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createPlaceholderPage($parent_id, $entry, $published)
|
|
|
|
{
|
2016-07-27 21:32:51 +02:00
|
|
|
echo '- ' . $entry['title'] . "\n";
|
2015-05-22 14:48:09 +02:00
|
|
|
$published['version'] = 1;
|
2016-01-26 12:00:07 +01:00
|
|
|
$published['title'] = $entry['title'];
|
2016-07-27 21:32:51 +02:00
|
|
|
$published['id'] = $this->client->createPage($parent_id, $entry['title'], '');
|
2015-05-22 14:48:09 +02:00
|
|
|
|
|
|
|
return $published;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function recursiveWithCallback($parent_id, $entry, $published, $callback)
|
|
|
|
{
|
|
|
|
$published = $callback($parent_id, $entry, $published);
|
|
|
|
|
2016-01-25 14:48:37 +01:00
|
|
|
if (!array_key_exists('children', $entry)) {
|
|
|
|
return $published;
|
|
|
|
}
|
2015-05-22 14:48:09 +02:00
|
|
|
|
2016-01-25 14:48:37 +01:00
|
|
|
foreach ($entry['children'] as $child) {
|
|
|
|
$pub = [];
|
|
|
|
if (isset($published['children']) && array_key_exists($child['title'], $published['children'])) {
|
|
|
|
$pub = $published['children'][$child['title']];
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
2016-01-25 14:48:37 +01:00
|
|
|
|
|
|
|
$published['children'][$child['title']] = $this->recursiveWithCallback(
|
|
|
|
$published['id'],
|
|
|
|
$child,
|
|
|
|
$pub,
|
|
|
|
$callback
|
|
|
|
);
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $published;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createRecursive($parent_id, $entry, $published)
|
|
|
|
{
|
2020-04-22 21:55:53 +02:00
|
|
|
$callback = function ($parent_id, $entry, $published) {
|
2015-05-22 14:48:09 +02:00
|
|
|
// nothing to do if the ID already exists
|
|
|
|
if (array_key_exists('id', $published)) {
|
|
|
|
return $published;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('page', $entry)) {
|
|
|
|
return $this->createPage($parent_id, $entry, $published);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have no $entry['page'] it means the page
|
|
|
|
// doesn't exist, but to respect the hierarchy,
|
|
|
|
// we need a blank page there
|
|
|
|
return $this->createPlaceholderPage($parent_id, $entry, $published);
|
|
|
|
};
|
|
|
|
|
|
|
|
return $this->recursiveWithCallback($parent_id, $entry, $published, $callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function updateRecursive($parent_id, $entry, $published)
|
|
|
|
{
|
2020-04-22 21:55:53 +02:00
|
|
|
$callback = function ($parent_id, $entry, $published) {
|
2015-05-22 14:48:09 +02:00
|
|
|
if (array_key_exists('id', $published) && array_key_exists('page', $entry)) {
|
|
|
|
$this->updatePage($parent_id, $entry, $published);
|
|
|
|
}
|
2016-01-25 14:48:37 +01:00
|
|
|
$published['needed'] = true;
|
2015-05-22 14:48:09 +02:00
|
|
|
|
|
|
|
return $published;
|
|
|
|
};
|
|
|
|
|
|
|
|
return $this->recursiveWithCallback($parent_id, $entry, $published, $callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function updatePage($parent_id, $entry, $published)
|
|
|
|
{
|
2019-12-05 21:25:58 +01:00
|
|
|
$updateThreshold = $this->confluence->getUpdateThreshold();
|
2015-05-22 14:48:09 +02:00
|
|
|
|
2015-07-14 22:06:01 +02:00
|
|
|
$this->run(
|
2017-11-07 22:08:30 +01:00
|
|
|
'- ' . PublisherUtilities::niceTitle($entry['file']->getUrl()),
|
2020-04-22 21:55:53 +02:00
|
|
|
function () use ($entry, $published, $parent_id, $updateThreshold) {
|
2017-06-06 23:16:45 +02:00
|
|
|
$generated_content = $entry['page']->getContent();
|
2017-11-07 22:08:30 +01:00
|
|
|
if (PublisherUtilities::shouldUpdate($entry['page'], $generated_content, $published, $updateThreshold)) {
|
2015-07-14 22:06:01 +02:00
|
|
|
$this->client->updatePage(
|
|
|
|
$parent_id,
|
|
|
|
$published['id'],
|
|
|
|
$published['version'] + 1,
|
|
|
|
$entry['title'],
|
2017-06-06 23:16:45 +02:00
|
|
|
$generated_content
|
2015-07-14 22:06:01 +02:00
|
|
|
);
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-14 22:06:01 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (count($entry['page']->attachments)) {
|
|
|
|
foreach ($entry['page']->attachments as $attachment) {
|
|
|
|
$this->run(
|
|
|
|
" With attachment: $attachment[filename]",
|
2020-04-22 21:55:53 +02:00
|
|
|
function ($write) use ($published, $attachment) {
|
2017-11-06 23:20:42 +01:00
|
|
|
$this->client->uploadAttachment($published['id'], $attachment, $write);
|
2015-07-14 22:06:01 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|