From 890163479009cdf1db91a6cd5d9e90e8e726103c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Fri, 22 May 2015 16:15:28 +0200 Subject: [PATCH] Fix bugs and optimized upload time --- libs/Format/Confluence/Api.php | 76 +++++++++++++++++++++++----- libs/Format/Confluence/Publisher.php | 13 +++-- 2 files changed, 70 insertions(+), 19 deletions(-) diff --git a/libs/Format/Confluence/Api.php b/libs/Format/Confluence/Api.php index 304e6b4..04a7d1e 100644 --- a/libs/Format/Confluence/Api.php +++ b/libs/Format/Confluence/Api.php @@ -74,6 +74,42 @@ class Api return new BadResponseException($message, $request, $response, $e->getPrevious()); } + /** + * /rest/api/content/{id}/child/{type} + * + * @param $rootPage + * @return mixed + */ + public function getList($rootPage) + { + $url = "content/$rootPage/child/page?expand=version"; + + $pages = []; + + do { + try { + $list = $this->getClient()->get($url)->json(); + } catch (BadResponseException $e) { + throw $this->handleError($e); + } + + foreach ($list['results'] as $result) { + $pages[$result['title']] = [ + "id" => $result['id'], + "title" => $result['title'], + "version" => $result['version']['number'], + ]; + } + + if (array_key_exists('next', $list['_links'])) { + $url = $list['_links']['next']; + } + + } while (array_key_exists('next', $list['_links'])); + + return $pages; + } + /** * /rest/api/content/{id}/child/{type} * @@ -82,22 +118,34 @@ class Api */ public function getHierarchy($rootPage) { - try { - $hierarchy = $this->getClient()->get("content/$rootPage/child/page?expand=version,body.storage")->json(); - } catch (BadResponseException $e) { - throw $this->handleError($e); - } + //We do a limit of 15 as it appears that confluence has + //a bug when retrieving more than 20 entries with "body.storage" + $url = "content/$rootPage/child/page?expand=version,body.storage&limit=15"; $children = []; - foreach ($hierarchy['results'] as $result) { - $children[$result['title']] = [ - "id" => $result['id'], - "title" => $result['title'], - "version" => $result['version']['number'], - "content" => $result['body']['storage']['value'], - "children" => $this->getHierarchy($result['id']) - ]; - } + + do { + try { + $hierarchy = $this->getClient()->get($url)->json(); + } catch (BadResponseException $e) { + throw $this->handleError($e); + } + + foreach ($hierarchy['results'] as $result) { + $children[$result['title']] = [ + "id" => $result['id'], + "title" => $result['title'], + "version" => $result['version']['number'], + "content" => $result['body']['storage']['value'], + "children" => $this->getHierarchy($result['id']) + ]; + } + + if (array_key_exists('next', $hierarchy['_links'])) { + $url = $hierarchy['_links']['next']; + } + + } while (array_key_exists('next', $hierarchy['_links'])); return $children; } diff --git a/libs/Format/Confluence/Publisher.php b/libs/Format/Confluence/Publisher.php index c53b05d..e0af7bd 100644 --- a/libs/Format/Confluence/Publisher.php +++ b/libs/Format/Confluence/Publisher.php @@ -31,18 +31,21 @@ class Publisher public function publish(array $tree) { - echo "Getting already published pages...\n"; - $all_published = $this->client->getHierarchy($this->confluence['ancestor_id']); - echo "Finding Root Page...\n"; - $published = []; - foreach ($all_published as $page) { + $pages = $this->client->getList($this->confluence['ancestor_id']); + $published = null; + foreach ($pages as $page) { if ($page['title'] == $tree['title']) { $published = $page; break; } } + echo "Getting already published pages...\n"; + if ($published != null) { + $published['children'] = $this->client->getHierarchy($published['id']); + } + echo "Create placeholder pages...\n"; $published = $this->createRecursive($this->confluence['ancestor_id'], $tree, $published);