diff --git a/docs/05_Configuration/Confluence_upload.md b/docs/05_Configuration/Confluence_upload.md index 810ab9fb..9402cdb6 100644 --- a/docs/05_Configuration/Confluence_upload.md +++ b/docs/05_Configuration/Confluence_upload.md @@ -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. diff --git a/libs/Format/Confluence/Config.php b/libs/Format/Confluence/Config.php index eedd3cae..3a6c5015 100644 --- a/libs/Format/Confluence/Config.php +++ b/libs/Format/Confluence/Config.php @@ -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; + } } diff --git a/libs/Format/Confluence/Publisher.php b/libs/Format/Confluence/Publisher.php index 3b158434..c451c00d 100644 --- a/libs/Format/Confluence/Publisher.php +++ b/libs/Format/Confluence/Publisher.php @@ -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) { @@ -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()) {