Improve confluence export
Migrate from Parsedown to Commonmark
Dieser Commit ist enthalten in:
committet von
Stéphane Goetz
Ursprung
fa798ff1e4
Commit
1fe3e62df3
@ -1,6 +1,5 @@
|
||||
<?php namespace Todaymade\Daux\Format\Confluence;
|
||||
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Todaymade\Daux\Daux;
|
||||
use Todaymade\Daux\Tree\Content;
|
||||
use Todaymade\Daux\Tree\Directory;
|
||||
@ -8,112 +7,30 @@ use Todaymade\Daux\Tree\Entry;
|
||||
|
||||
class Generator
|
||||
{
|
||||
/**
|
||||
* @var Api
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
|
||||
public function generate(Daux $daux)
|
||||
{
|
||||
$confluence = $daux->getParams()['confluence'];
|
||||
|
||||
$this->prefix = trim($confluence['prefix']) . " ";
|
||||
|
||||
$main_title = $this->prefix . $daux->getParams()['title'];
|
||||
|
||||
$params = $daux->getParams();
|
||||
|
||||
echo "Generating Tree...\n";
|
||||
$tree = $this->generateRecursive($daux->tree, $params);
|
||||
$tree['title'] = $main_title;
|
||||
$tree['title'] = $this->prefix . $daux->getParams()['title'];
|
||||
|
||||
echo "Getting already published pages...\n";
|
||||
$this->client = new Api($confluence['base_url'], $confluence['user'], $confluence['pass'], $confluence['space_id']);
|
||||
$all_published = $this->client->getHierarchy($confluence['ancestor_id']);
|
||||
|
||||
echo "Finding Root Page...\n";
|
||||
$published = [];
|
||||
foreach ($all_published as $page) {
|
||||
if ($page['title'] == $main_title) {
|
||||
$published = $page;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
echo "Create placeholder pages...\n";
|
||||
$published = $this->createRecursive($confluence['ancestor_id'], $tree, $published);
|
||||
|
||||
echo "Publishing updates...\n";
|
||||
$this->updateRecursive($confluence['ancestor_id'], $tree, $published);
|
||||
echo "Start Publishing...\n";
|
||||
$publisher = new Publisher($confluence);
|
||||
$publisher->publish($tree);
|
||||
|
||||
echo "Done !\n";
|
||||
}
|
||||
|
||||
private function createRecursive($parent_id, $entry, $published)
|
||||
{
|
||||
//TODO :: remove deleted pages
|
||||
|
||||
if (!array_key_exists('id', $published)) {
|
||||
if (array_key_exists('page', $entry)) {
|
||||
echo "Creating: " . $entry['file']->getUrl() . "\n";
|
||||
$published['version'] = 1;
|
||||
$published['id'] = $this->client->createPage($parent_id, $entry['title'], "The content will come very soon !");
|
||||
} else {
|
||||
echo "Creating Placeholder page: " . $entry['title'] . "\n";
|
||||
$published['version'] = 1;
|
||||
$published['id'] = $this->client->createPage($parent_id, $entry['title'], "");
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('children', $entry)) {
|
||||
foreach($entry['children'] as $child) {
|
||||
$pub = [];
|
||||
if (array_key_exists('children', $published) && array_key_exists($child['title'], $published['children'])) {
|
||||
$pub = $published['children'][$child['title']];
|
||||
}
|
||||
|
||||
$published['children'][$child['title']] = $this->createRecursive($published['id'], $child, $pub);
|
||||
}
|
||||
}
|
||||
|
||||
return $published;
|
||||
}
|
||||
|
||||
private function updateRecursive($parent_id, $entry, $published)
|
||||
{
|
||||
if (array_key_exists('id', $published) && array_key_exists('page', $entry)) {
|
||||
echo "Updating: " . $entry['file']->getUrl() . "\n";
|
||||
try {
|
||||
$this->client->updatePage(
|
||||
$parent_id,
|
||||
$published['id'],
|
||||
$published['version'] + 1,
|
||||
$entry['title'],
|
||||
$entry['page']->getContent()
|
||||
);
|
||||
} catch (ClientException $e) {
|
||||
echo "-> Failed with message: " . $e->getResponse()->json()['message'] . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('children', $entry)) {
|
||||
foreach($entry['children'] as $child) {
|
||||
$pub = [];
|
||||
if (array_key_exists('children', $published) && array_key_exists($child['title'], $published['children'])) {
|
||||
$pub = $published['children'][$child['title']];
|
||||
}
|
||||
|
||||
$this->updateRecursive($published['id'], $child, $pub);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function generateRecursive(Entry $tree, array $params, $base_url = '')
|
||||
{
|
||||
$final = ['title' => $this->prefix . $tree->getTitle()];
|
||||
@ -125,9 +42,12 @@ class Generator
|
||||
}
|
||||
foreach ($tree->value as $key => $node) {
|
||||
if ($node instanceof Directory) {
|
||||
$final['children'][$this->prefix . $node->getTitle()] = $this->generateRecursive($node, $params, '../' . $base_url);
|
||||
} else if ($node instanceof Content) {
|
||||
|
||||
$final['children'][$this->prefix . $node->getTitle()] = $this->generateRecursive(
|
||||
$node,
|
||||
$params,
|
||||
'../' . $base_url
|
||||
);
|
||||
} elseif ($node instanceof Content) {
|
||||
$params['request'] = $node->getUrl();
|
||||
$params['file_uri'] = $node->getName();
|
||||
|
||||
@ -137,6 +57,10 @@ class Generator
|
||||
'page' => MarkdownPage::fromFile($node, $params),
|
||||
];
|
||||
|
||||
// As the page is lazily generated
|
||||
// We do it now to fail fast in case of problem
|
||||
$data['page']->getContent();
|
||||
|
||||
if ($key == 'index.html') {
|
||||
$final['title'] = $this->prefix . $tree->getTitle();
|
||||
$final['file'] = $node;
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren