Skip to content

Commit

Permalink
#358 add create_root_if_missing option
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Dec 14, 2022
1 parent 4c5cb80 commit 6d9e00f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
12 changes: 12 additions & 0 deletions docs/05_Configuration/Confluence_upload.md
Expand Up @@ -35,6 +35,18 @@ You can obtain a page's id by checking the links of the actions on the page (pag
}
```

If using an `ancestor_id` you may wish to create the page automatically if it doesn't exist.
To do so, set `create_root_if_missing` to true.

```json
{
"confluence": {
"ancestor_id": 50370632,
"create_root_if_missing": true
}
}
```

## Prefix

Because confluence can't have two pages with the same name in a space, I recommend you define a prefix for your pages.
Expand Down
5 changes: 5 additions & 0 deletions libs/Format/Confluence/Config.php
Expand Up @@ -86,4 +86,9 @@ public function getHeader()
{
return $this->getValue('header');
}

public function createRootIfMissing()
{
return $this->hasValue('create_root_if_missing') ? $this->getValue('create_root_if_missing') : false;
}
}
36 changes: 28 additions & 8 deletions libs/Format/Confluence/Publisher.php
Expand Up @@ -104,10 +104,31 @@ function () use ($ancestorId, $tree, $published) {
$delete->handle($published);
}

protected function rootNotFound(string $rootTitle, array $pages)
{
$pageNotFound = "Could not find a page named '$rootTitle'";
$configRecommendation = "To create the page automatically, add '\"create_root_if_missing\": true'"
. " in the 'confluence' section of your Daux configuration.";

if (empty($pages)) {
throw new ConfluenceConfigurationException(
"$pageNotFound as no page were found with the specified ancestor_id.$configRecommendation"
);
}

$pageNames = implode(
"', '",
array_map(function ($page) { return $page['title']; }, $pages)
);

throw new ConfluenceConfigurationException("$pageNotFound but found ['$pageNames'].$configRecommendation");
}

protected function getRootPage($tree)
{
if ($this->confluence->hasAncestorId()) {
$pages = $this->client->getList($this->confluence->getAncestorId());
$ancestorId = $this->confluence->getAncestorId();
$pages = $this->client->getList($ancestorId);
$rootTitle = $tree['title'];

foreach ($pages as $page) {
Expand All @@ -116,14 +137,13 @@ protected function getRootPage($tree)
}
}

$pageNames = implode(
"', '",
array_map(function ($page) { return $page['title']; }, $pages)
);
if ($this->confluence->createRootIfMissing()) {
$id = $this->client->createPage($ancestorId, $rootTitle, 'The content will come very soon !');

throw new ConfluenceConfigurationException(
"Could not find a page named '$rootTitle' but found ['$pageNames']."
);
return $this->client->getPage($id);
}

$this->rootNotFound($rootTitle, $pages);
}

if ($this->confluence->hasRootId()) {
Expand Down

0 comments on commit 6d9e00f

Please sign in to comment.