Fix bugs and optimized upload time

This commit is contained in:
Stéphane Goetz 2015-05-22 16:15:28 +02:00 committed by Stéphane Goetz
parent 63d6b17ec4
commit 8901634790
2 changed files with 70 additions and 19 deletions

View File

@ -74,6 +74,42 @@ class Api
return new BadResponseException($message, $request, $response, $e->getPrevious()); 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} * /rest/api/content/{id}/child/{type}
* *
@ -82,22 +118,34 @@ class Api
*/ */
public function getHierarchy($rootPage) public function getHierarchy($rootPage)
{ {
try { //We do a limit of 15 as it appears that confluence has
$hierarchy = $this->getClient()->get("content/$rootPage/child/page?expand=version,body.storage")->json(); //a bug when retrieving more than 20 entries with "body.storage"
} catch (BadResponseException $e) { $url = "content/$rootPage/child/page?expand=version,body.storage&limit=15";
throw $this->handleError($e);
}
$children = []; $children = [];
foreach ($hierarchy['results'] as $result) {
$children[$result['title']] = [ do {
"id" => $result['id'], try {
"title" => $result['title'], $hierarchy = $this->getClient()->get($url)->json();
"version" => $result['version']['number'], } catch (BadResponseException $e) {
"content" => $result['body']['storage']['value'], throw $this->handleError($e);
"children" => $this->getHierarchy($result['id']) }
];
} 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; return $children;
} }

View File

@ -31,18 +31,21 @@ class Publisher
public function publish(array $tree) 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"; echo "Finding Root Page...\n";
$published = []; $pages = $this->client->getList($this->confluence['ancestor_id']);
foreach ($all_published as $page) { $published = null;
foreach ($pages as $page) {
if ($page['title'] == $tree['title']) { if ($page['title'] == $tree['title']) {
$published = $page; $published = $page;
break; break;
} }
} }
echo "Getting already published pages...\n";
if ($published != null) {
$published['children'] = $this->client->getHierarchy($published['id']);
}
echo "Create placeholder pages...\n"; echo "Create placeholder pages...\n";
$published = $this->createRecursive($this->confluence['ancestor_id'], $tree, $published); $published = $this->createRecursive($this->confluence['ancestor_id'], $tree, $published);