2015-05-21 17:39:11 +02:00
|
|
|
<?php namespace Todaymade\Daux\Format\Confluence;
|
|
|
|
|
2015-07-19 00:55:57 +02:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2015-07-14 22:06:01 +02:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2015-06-29 16:11:01 +02:00
|
|
|
use Todaymade\Daux\Config;
|
2015-07-23 17:44:24 +02:00
|
|
|
use Todaymade\Daux\Console\RunAction;
|
2015-05-21 17:39:11 +02:00
|
|
|
use Todaymade\Daux\Daux;
|
|
|
|
use Todaymade\Daux\Tree\Content;
|
|
|
|
use Todaymade\Daux\Tree\Directory;
|
|
|
|
|
2015-07-19 00:55:57 +02:00
|
|
|
class Generator implements \Todaymade\Daux\Format\Base\Generator
|
2015-05-21 17:39:11 +02:00
|
|
|
{
|
2015-07-14 22:06:01 +02:00
|
|
|
use RunAction;
|
|
|
|
|
2015-07-19 12:21:09 +02:00
|
|
|
/** @var string */
|
2015-05-21 17:39:11 +02:00
|
|
|
protected $prefix;
|
|
|
|
|
2015-07-19 12:21:09 +02:00
|
|
|
/** @var Daux */
|
|
|
|
protected $daux;
|
|
|
|
|
2015-07-19 01:05:43 +02:00
|
|
|
/**
|
2015-07-19 12:21:09 +02:00
|
|
|
* @param Daux $daux
|
2015-07-19 01:05:43 +02:00
|
|
|
*/
|
2015-07-19 12:21:09 +02:00
|
|
|
public function __construct(Daux $daux)
|
|
|
|
{
|
|
|
|
$this->daux = $daux;
|
2016-09-15 17:20:24 +02:00
|
|
|
|
|
|
|
$this->checkConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkConfiguration()
|
|
|
|
{
|
|
|
|
$config = $this->daux->getParams();
|
|
|
|
$confluence = $config->getConfluenceConfiguration();
|
|
|
|
|
|
|
|
if ($confluence == null) {
|
2016-09-26 20:54:06 +02:00
|
|
|
throw new \RuntimeException('You must specify your Confluence configuration');
|
2016-09-15 17:20:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$mandatory = ['space_id', 'base_url', 'user', 'pass', 'prefix'];
|
|
|
|
$errors = [];
|
|
|
|
foreach ($mandatory as $key) {
|
|
|
|
if (!array_key_exists($key, $confluence)) {
|
|
|
|
$errors[] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($errors)) {
|
2016-09-26 20:54:06 +02:00
|
|
|
throw new \RuntimeException("The following options are mandatory for confluence : '" . implode("', '", $errors) . "'");
|
2016-09-15 17:20:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists('ancestor_id', $confluence) && !array_key_exists('root_id', $confluence)) {
|
|
|
|
throw new \RuntimeException("You must specify an 'ancestor_id' or a 'root_id' for confluence.");
|
|
|
|
}
|
2015-07-28 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getContentTypes()
|
|
|
|
{
|
|
|
|
return [
|
2016-07-27 21:32:51 +02:00
|
|
|
new ContentTypes\Markdown\ContentType($this->daux->getParams()),
|
2015-07-28 17:25:03 +02:00
|
|
|
];
|
2015-07-19 12:21:09 +02:00
|
|
|
}
|
2015-07-19 01:05:43 +02:00
|
|
|
|
2015-07-20 15:59:52 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-19 12:21:09 +02:00
|
|
|
public function generateAll(InputInterface $input, OutputInterface $output, $width)
|
2015-05-21 17:39:11 +02:00
|
|
|
{
|
2015-07-19 12:21:09 +02:00
|
|
|
$params = $this->daux->getParams();
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2015-07-19 01:05:43 +02:00
|
|
|
$confluence = $params['confluence'];
|
2016-07-27 21:32:51 +02:00
|
|
|
$this->prefix = trim($confluence['prefix']) . ' ';
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2015-07-14 22:06:01 +02:00
|
|
|
$tree = $this->runAction(
|
2016-07-27 21:32:51 +02:00
|
|
|
'Generating Tree ...',
|
2015-07-14 22:06:01 +02:00
|
|
|
$output,
|
|
|
|
$width,
|
2016-07-27 21:32:51 +02:00
|
|
|
function () use ($params) {
|
2015-07-19 12:21:09 +02:00
|
|
|
$tree = $this->generateRecursive($this->daux->tree, $params);
|
|
|
|
$tree['title'] = $this->prefix . $params['title'];
|
2015-07-14 22:06:01 +02:00
|
|
|
|
|
|
|
return $tree;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-07-27 21:32:51 +02:00
|
|
|
$output->writeln('Start Publishing...');
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
$publisher = new Publisher($confluence);
|
2015-07-14 22:06:01 +02:00
|
|
|
$publisher->output = $output;
|
|
|
|
$publisher->width = $width;
|
2015-05-22 14:48:09 +02:00
|
|
|
$publisher->publish($tree);
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 01:10:40 +02:00
|
|
|
private function generateRecursive(Directory $tree, Config $params, $base_url = '')
|
2015-05-21 17:39:11 +02:00
|
|
|
{
|
|
|
|
$final = ['title' => $this->prefix . $tree->getTitle()];
|
|
|
|
$params['base_url'] = $params['base_page'] = $base_url;
|
|
|
|
|
|
|
|
$params['image'] = str_replace('<base_url>', $base_url, $params['image']);
|
|
|
|
if ($base_url !== '') {
|
|
|
|
$params['entry_page'] = $tree->getFirstPage();
|
|
|
|
}
|
2015-07-18 14:37:18 +02:00
|
|
|
foreach ($tree->getEntries() as $key => $node) {
|
2015-05-21 17:39:11 +02:00
|
|
|
if ($node instanceof Directory) {
|
2015-05-22 14:48:09 +02:00
|
|
|
$final['children'][$this->prefix . $node->getTitle()] = $this->generateRecursive(
|
|
|
|
$node,
|
|
|
|
$params,
|
|
|
|
'../' . $base_url
|
|
|
|
);
|
|
|
|
} elseif ($node instanceof Content) {
|
2015-05-21 17:39:11 +02:00
|
|
|
$params['request'] = $node->getUrl();
|
|
|
|
|
2015-08-02 15:42:23 +02:00
|
|
|
$contentType = $this->daux->getContentTypeHandler()->getType($node);
|
|
|
|
|
2015-05-21 17:39:11 +02:00
|
|
|
$data = [
|
|
|
|
'title' => $this->prefix . $node->getTitle(),
|
|
|
|
'file' => $node,
|
2015-08-02 15:42:23 +02:00
|
|
|
'page' => ContentPage::fromFile($node, $params, $contentType),
|
2015-05-21 17:39:11 +02:00
|
|
|
];
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
// As the page is lazily generated
|
|
|
|
// We do it now to fail fast in case of problem
|
|
|
|
$data['page']->getContent();
|
|
|
|
|
2015-05-21 17:39:11 +02:00
|
|
|
if ($key == 'index.html') {
|
|
|
|
$final['title'] = $this->prefix . $tree->getTitle();
|
|
|
|
$final['file'] = $node;
|
|
|
|
$final['page'] = $data['page'];
|
|
|
|
} else {
|
|
|
|
$final['children'][$data['title']] = $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $final;
|
|
|
|
}
|
|
|
|
}
|