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
|
|
|
/**
|
|
|
|
* @var Api
|
|
|
|
*/
|
|
|
|
protected $client;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $confluence;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $previous_title;
|
|
|
|
|
2015-07-14 22:06:01 +02:00
|
|
|
/**
|
2016-07-27 21:32:51 +02:00
|
|
|
* @var int terminal width
|
2015-07-14 22:06:01 +02:00
|
|
|
*/
|
|
|
|
public $width;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var
|
|
|
|
*/
|
|
|
|
public $output;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $confluence
|
|
|
|
*/
|
2015-05-22 14:48:09 +02:00
|
|
|
public function __construct($confluence)
|
|
|
|
{
|
|
|
|
$this->confluence = $confluence;
|
|
|
|
|
|
|
|
$this->client = new Api($confluence['base_url'], $confluence['user'], $confluence['pass']);
|
|
|
|
$this->client->setSpace($confluence['space_id']);
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
public function run($title, $closure)
|
|
|
|
{
|
2015-07-14 22:06:01 +02:00
|
|
|
try {
|
|
|
|
return $this->runAction($title, $this->output, $this->width, $closure);
|
|
|
|
} catch (BadResponseException $e) {
|
2016-07-27 21:32:51 +02:00
|
|
|
$this->output->writeLn(' <error>' . $e->getMessage() . '</error>');
|
2015-07-14 22:06:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
public function publish(array $tree)
|
|
|
|
{
|
|
|
|
echo "Finding Root Page...\n";
|
2016-01-26 12:00:07 +01:00
|
|
|
if (array_key_exists('ancestor_id', $this->confluence)) {
|
|
|
|
$pages = $this->client->getList($this->confluence['ancestor_id']);
|
|
|
|
$published = null;
|
|
|
|
foreach ($pages as $page) {
|
|
|
|
if ($page['title'] == $tree['title']) {
|
|
|
|
$published = $page;
|
|
|
|
break;
|
|
|
|
}
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
2016-01-26 12:00:07 +01:00
|
|
|
} elseif (array_key_exists('root_id', $this->confluence)) {
|
|
|
|
$published = $this->client->getPage($this->confluence['root_id']);
|
|
|
|
$this->confluence['ancestor_id'] = $published['ancestor_id'];
|
|
|
|
} else {
|
2016-07-27 21:32:51 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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...',
|
|
|
|
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(
|
2016-01-25 14:48:37 +01:00
|
|
|
"Create placeholder pages...\n",
|
2016-07-27 21:32:51 +02:00
|
|
|
function () use ($tree, $published) {
|
2015-07-14 22:06:01 +02:00
|
|
|
return $this->createRecursive($this->confluence['ancestor_id'], $tree, $published);
|
|
|
|
}
|
|
|
|
);
|
2015-05-22 14:48:09 +02:00
|
|
|
|
2016-07-27 21:32:51 +02:00
|
|
|
$this->output->writeLn('Publishing updates...');
|
2016-01-25 14:48:37 +01:00
|
|
|
$published = $this->updateRecursive($this->confluence['ancestor_id'], $tree, $published);
|
|
|
|
|
2016-04-14 12:03:46 +02:00
|
|
|
|
|
|
|
if ($this->shouldDelete()) {
|
2016-07-27 21:32:51 +02:00
|
|
|
$this->output->writeLn('Deleting obsolete pages...');
|
2016-04-14 12:03:46 +02:00
|
|
|
} else {
|
2016-07-27 21:32:51 +02:00
|
|
|
$this->output->writeLn('Listing obsolete pages...');
|
2016-01-25 14:48:37 +01:00
|
|
|
echo "> The following pages will not be deleted, but just listed for information.\n";
|
2016-04-14 12:03:46 +02:00
|
|
|
echo "> If you want to delete these pages, you need to set the --delete flag on the command.\n";
|
2016-01-25 14:48:37 +01:00
|
|
|
}
|
|
|
|
$this->deleteRecursive($published);
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function niceTitle($title)
|
|
|
|
{
|
2016-07-27 21:32:51 +02:00
|
|
|
if ($title == 'index.html') {
|
|
|
|
return 'Homepage';
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
|
2016-07-27 21:32:51 +02:00
|
|
|
return rtrim(strtr($title, ['index.html' => '', '.html' => '']), '/');
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function createPage($parent_id, $entry, $published)
|
|
|
|
{
|
2016-07-27 21:32:51 +02:00
|
|
|
echo '- ' . $this->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)
|
|
|
|
{
|
2016-07-27 21:32:51 +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)
|
|
|
|
{
|
2016-07-27 21:32:51 +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);
|
|
|
|
}
|
|
|
|
|
2016-01-25 14:48:37 +01:00
|
|
|
protected function shouldDelete()
|
|
|
|
{
|
|
|
|
return array_key_exists('delete', $this->confluence) && $this->confluence['delete'];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function deleteRecursive($published, $prefix = '')
|
|
|
|
{
|
2016-07-27 21:32:51 +02:00
|
|
|
foreach ($published['children'] as $child) {
|
2016-01-25 14:48:37 +01:00
|
|
|
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(
|
2016-07-27 21:32:51 +02:00
|
|
|
'- ' . $prefix . $child['title'],
|
|
|
|
function () use ($child) {
|
2016-01-25 14:48:37 +01:00
|
|
|
$this->client->deletePage($child['id']);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
2016-07-27 21:32:51 +02:00
|
|
|
echo '- ' . $prefix . $child['title'] . "\n";
|
2016-01-25 14:48:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
protected function shouldUpdate($local, $published)
|
|
|
|
{
|
|
|
|
if (!array_key_exists('content', $published)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-29 16:16:55 +02:00
|
|
|
$trimmed_local = trim($local->getContent());
|
2015-05-22 14:48:09 +02:00
|
|
|
$trimmed_distant = trim($published['content']);
|
|
|
|
|
|
|
|
if ($trimmed_local == $trimmed_distant) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// I consider that if the files are 98% identical you
|
|
|
|
// don't need to update. This will work for false positives.
|
|
|
|
// But sadly will miss if it's just a typo update
|
2016-01-26 12:00:07 +01:00
|
|
|
// This is configurable with `update_threshold`
|
|
|
|
$threshold = 98;
|
|
|
|
if (array_key_exists('update_threshold', $this->confluence)) {
|
|
|
|
$threshold = 100 - $this->confluence['update_threshold'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($threshold < 100) {
|
|
|
|
similar_text($trimmed_local, $trimmed_distant, $percent);
|
|
|
|
if ($percent > $threshold) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
|
2015-06-29 16:16:55 +02:00
|
|
|
//DEBUG
|
2016-07-27 21:32:51 +02:00
|
|
|
if (getenv('DEBUG') && strtolower(getenv('DEBUG')) != 'false') {
|
2015-06-29 16:16:55 +02:00
|
|
|
$prefix = 'static/export/';
|
|
|
|
if (!is_dir($prefix)) {
|
|
|
|
mkdir($prefix, 0777, true);
|
|
|
|
}
|
|
|
|
$url = $local->getFile()->getUrl();
|
|
|
|
file_put_contents($prefix . strtr($url, ['/' => '_', '.html' => '_local.html']), $trimmed_local);
|
|
|
|
file_put_contents($prefix . strtr($url, ['/' => '_', '.html' => '_distant.html']), $trimmed_distant);
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function updatePage($parent_id, $entry, $published)
|
|
|
|
{
|
2016-07-27 21:32:51 +02:00
|
|
|
if ($this->previous_title != 'Updating') {
|
|
|
|
$this->previous_title = 'Updating';
|
2015-05-22 14:48:09 +02:00
|
|
|
echo "Updating Pages...\n";
|
|
|
|
}
|
|
|
|
|
2015-07-14 22:06:01 +02:00
|
|
|
$this->run(
|
2016-07-27 21:32:51 +02:00
|
|
|
'- ' . $this->niceTitle($entry['file']->getUrl()),
|
|
|
|
function () use ($entry, $published, $parent_id) {
|
2015-07-14 22:06:01 +02:00
|
|
|
if ($this->shouldUpdate($entry['page'], $published)) {
|
|
|
|
$this->client->updatePage(
|
|
|
|
$parent_id,
|
|
|
|
$published['id'],
|
|
|
|
$published['version'] + 1,
|
|
|
|
$entry['title'],
|
|
|
|
$entry['page']->getContent()
|
|
|
|
);
|
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]",
|
2016-07-27 21:32:51 +02:00
|
|
|
function () use ($published, $attachment) {
|
2015-07-14 22:06:01 +02:00
|
|
|
$this->client->uploadAttachment($published['id'], $attachment);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|