2015-05-21 17:39:11 +02:00
|
|
|
<?php namespace Todaymade\Daux\Format\Confluence;
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
2015-05-22 14:48:09 +02:00
|
|
|
use GuzzleHttp\Exception\BadResponseException;
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
class Api
|
|
|
|
{
|
2015-05-21 17:39:11 +02:00
|
|
|
protected $base_url;
|
|
|
|
protected $user;
|
|
|
|
protected $pass;
|
|
|
|
|
|
|
|
protected $space;
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
public function __construct($base_url, $user, $pass)
|
|
|
|
{
|
2015-05-21 17:39:11 +02:00
|
|
|
$this->base_url = $base_url;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->pass = $pass;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
public function setSpace($space_id)
|
|
|
|
{
|
2015-05-21 17:39:11 +02:00
|
|
|
$this->space = $space_id;
|
|
|
|
}
|
|
|
|
|
2016-04-09 16:42:46 +02:00
|
|
|
/**
|
|
|
|
* This method is public due to test purposes
|
|
|
|
* @return Client
|
|
|
|
*/
|
|
|
|
public function getClient()
|
2015-05-22 14:48:09 +02:00
|
|
|
{
|
|
|
|
$options = [
|
2016-04-09 16:42:46 +02:00
|
|
|
'base_uri' => $this->base_url . 'rest/api/',
|
2016-07-27 21:32:51 +02:00
|
|
|
'auth' => [$this->user, $this->pass],
|
2015-05-22 14:48:09 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
return new Client($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The standard error message from guzzle is quite poor in informations,
|
|
|
|
* this will give little bit more sense to it and return it
|
|
|
|
*
|
|
|
|
* @param BadResponseException $e
|
2016-07-29 23:20:01 +02:00
|
|
|
* @return \Exception
|
2015-05-22 14:48:09 +02:00
|
|
|
*/
|
|
|
|
protected function handleError(BadResponseException $e)
|
|
|
|
{
|
|
|
|
$request = $e->getRequest();
|
|
|
|
$response = $e->getResponse();
|
|
|
|
|
|
|
|
$level = floor($response->getStatusCode() / 100);
|
|
|
|
|
|
|
|
if ($level == '4') {
|
|
|
|
$label = 'Client error response';
|
|
|
|
} elseif ($level == '5') {
|
|
|
|
$label = 'Server error response';
|
|
|
|
} else {
|
|
|
|
$label = 'Unsuccessful response';
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = $label .
|
2017-11-06 22:46:07 +01:00
|
|
|
"\n [url] " . $request->getUri() .
|
|
|
|
"\n [status code] " . $response->getStatusCode() .
|
|
|
|
"\n [message] ";
|
2015-05-22 14:48:09 +02:00
|
|
|
|
2016-04-15 10:43:44 +02:00
|
|
|
$body = $response->getBody();
|
|
|
|
$json = json_decode($body, true);
|
|
|
|
$message .= ($json != null && array_key_exists('message', $json)) ? $json['message'] : $body;
|
2015-05-22 14:48:09 +02:00
|
|
|
|
2016-07-27 21:32:51 +02:00
|
|
|
if ($level == '4' && strpos($message, 'page with this title already exists') !== false) {
|
2016-01-25 14:47:37 +01:00
|
|
|
return new DuplicateTitleException($message, 0, $e->getPrevious());
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
return new BadResponseException($message, $request, $response, $e->getPrevious());
|
|
|
|
}
|
|
|
|
|
2016-01-26 12:00:07 +01:00
|
|
|
public function getPage($id)
|
|
|
|
{
|
2016-07-27 21:32:51 +02:00
|
|
|
$url = "content/$id?expand=ancestors,version,body.storage";
|
2016-01-26 12:00:07 +01:00
|
|
|
|
|
|
|
try {
|
2016-04-15 10:43:44 +02:00
|
|
|
$result = json_decode($this->getClient()->get($url)->getBody(), true);
|
2016-01-26 12:00:07 +01:00
|
|
|
} catch (BadResponseException $e) {
|
|
|
|
throw $this->handleError($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
$ancestor_id = null;
|
|
|
|
if (array_key_exists('ancestors', $result) && count($result['ancestors'])) {
|
|
|
|
$ancestor_page = end($result['ancestors']); // We need the direct parent
|
|
|
|
$ancestor_id = $ancestor_page['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2016-07-27 21:32:51 +02:00
|
|
|
'id' => $result['id'],
|
|
|
|
'ancestor_id' => $ancestor_id,
|
|
|
|
'title' => $result['title'],
|
|
|
|
'version' => $result['version']['number'],
|
|
|
|
'content' => $result['body']['storage']['value'],
|
2016-01-26 12:00:07 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-05-22 16:15:28 +02:00
|
|
|
/**
|
2015-07-17 23:38:06 +02:00
|
|
|
* Get a list of pages
|
2015-05-22 16:15:28 +02:00
|
|
|
*
|
2016-07-27 21:32:51 +02:00
|
|
|
* @param int $rootPage
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param bool $recursive
|
|
|
|
* @return array
|
2015-05-22 16:15:28 +02:00
|
|
|
*/
|
2015-07-17 23:38:06 +02:00
|
|
|
public function getList($rootPage, $recursive = false)
|
2015-05-22 16:15:28 +02:00
|
|
|
{
|
2015-07-17 23:38:06 +02:00
|
|
|
$increment = 15;
|
|
|
|
|
|
|
|
// We set a limit of 15 as it appears that
|
|
|
|
// Confluence fails silently when retrieving
|
|
|
|
// more than 20 entries with "body.storage"
|
|
|
|
$base_url = $url = "content/$rootPage/child/page?expand=version,body.storage&limit=$increment";
|
|
|
|
$start = 0;
|
2015-05-22 16:15:28 +02:00
|
|
|
|
|
|
|
$pages = [];
|
|
|
|
|
|
|
|
do {
|
|
|
|
try {
|
2016-04-15 10:43:44 +02:00
|
|
|
$hierarchy = json_decode($this->getClient()->get($url)->getBody(), true);
|
2015-05-22 16:15:28 +02:00
|
|
|
} catch (BadResponseException $e) {
|
|
|
|
throw $this->handleError($e);
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
foreach ($hierarchy['results'] as $result) {
|
2015-05-22 16:15:28 +02:00
|
|
|
$pages[$result['title']] = [
|
2016-07-27 21:32:51 +02:00
|
|
|
'id' => $result['id'],
|
|
|
|
'title' => $result['title'],
|
|
|
|
'version' => $result['version']['number'],
|
|
|
|
'content' => $result['body']['storage']['value'],
|
2015-05-22 16:15:28 +02:00
|
|
|
];
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
if ($recursive) {
|
|
|
|
$pages[$result['title']]['children'] = $this->getList($result['id'], true);
|
|
|
|
}
|
2015-05-22 16:15:28 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
// We don't use _links->next as after ~30 elements
|
|
|
|
// it doesn't show any new elements. This seems
|
|
|
|
// to be a bug in Confluence
|
|
|
|
$start += $increment;
|
|
|
|
$url = "$base_url&start=$start";
|
|
|
|
} while (!empty($hierarchy['results']));
|
2015-05-22 16:15:28 +02:00
|
|
|
|
|
|
|
return $pages;
|
|
|
|
}
|
|
|
|
|
2015-05-21 17:39:11 +02:00
|
|
|
/**
|
2016-07-27 21:32:51 +02:00
|
|
|
* @param int $parent_id
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param string $title
|
|
|
|
* @param string $content
|
2016-07-27 21:32:51 +02:00
|
|
|
* @return int
|
2015-05-21 17:39:11 +02:00
|
|
|
*/
|
2015-05-22 14:48:09 +02:00
|
|
|
public function createPage($parent_id, $title, $content)
|
|
|
|
{
|
2015-05-21 17:39:11 +02:00
|
|
|
$body = [
|
|
|
|
'type' => 'page',
|
|
|
|
'space' => ['key' => $this->space],
|
|
|
|
'title' => $title,
|
2016-07-27 21:32:51 +02:00
|
|
|
'body' => ['storage' => ['value' => $content, 'representation' => 'storage']],
|
2015-05-21 17:39:11 +02:00
|
|
|
];
|
|
|
|
|
2016-01-26 12:00:07 +01:00
|
|
|
if ($parent_id) {
|
|
|
|
$body['ancestors'] = [['type' => 'page', 'id' => $parent_id]];
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
try {
|
2016-04-15 10:43:44 +02:00
|
|
|
$response = json_decode($this->getClient()->post('content', ['json' => $body])->getBody(), true);
|
2015-05-22 14:48:09 +02:00
|
|
|
} catch (BadResponseException $e) {
|
|
|
|
throw $this->handleError($e);
|
|
|
|
}
|
2015-05-21 17:39:11 +02:00
|
|
|
|
|
|
|
return $response['id'];
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
2016-07-27 21:32:51 +02:00
|
|
|
* @param int $parent_id
|
|
|
|
* @param int $page_id
|
|
|
|
* @param int $newVersion
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param string $title
|
|
|
|
* @param string $content
|
|
|
|
*/
|
2015-05-22 14:48:09 +02:00
|
|
|
public function updatePage($parent_id, $page_id, $newVersion, $title, $content)
|
|
|
|
{
|
2015-05-21 17:39:11 +02:00
|
|
|
$body = [
|
|
|
|
'type' => 'page',
|
|
|
|
'space' => ['key' => $this->space],
|
2016-07-27 21:32:51 +02:00
|
|
|
'version' => ['number' => $newVersion, 'minorEdit' => true],
|
2015-05-21 17:39:11 +02:00
|
|
|
'title' => $title,
|
2016-07-27 21:32:51 +02:00
|
|
|
'body' => ['storage' => ['value' => $content, 'representation' => 'storage']],
|
2015-05-21 17:39:11 +02:00
|
|
|
];
|
|
|
|
|
2016-01-26 12:00:07 +01:00
|
|
|
if ($parent_id) {
|
|
|
|
$body['ancestors'] = [['type' => 'page', 'id' => $parent_id]];
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
try {
|
2016-04-15 10:43:44 +02:00
|
|
|
$this->getClient()->put("content/$page_id", ['json' => $body]);
|
2015-05-22 14:48:09 +02:00
|
|
|
} catch (BadResponseException $e) {
|
2017-11-06 22:46:07 +01:00
|
|
|
$error = $this->handleError($e);
|
|
|
|
|
|
|
|
$re = '/\[([0-9]*),([0-9]*)\]$/';
|
|
|
|
preg_match($re, $error->getMessage(), $matches, PREG_OFFSET_CAPTURE, 0);
|
|
|
|
|
|
|
|
if (count($matches) == 3) {
|
|
|
|
echo "\nContent: \n";
|
|
|
|
echo $this->showSourceCode($content, $matches[1][0], $matches[2][0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function showSourceCode($css, $lineNumber, $column)
|
|
|
|
{
|
|
|
|
$lines = preg_split("/\r?\n/", $css);
|
|
|
|
$start = max($lineNumber - 3, 0);
|
|
|
|
$end = min($lineNumber + 2, count($lines));
|
|
|
|
|
|
|
|
$maxWidth = strlen("$end");
|
|
|
|
|
|
|
|
$filtered = array_slice($lines, $start, $end - $start);
|
|
|
|
|
|
|
|
$prepared = [];
|
|
|
|
foreach ($filtered as $index => $line) {
|
|
|
|
|
|
|
|
$number = $start + 1 + $index;
|
|
|
|
$gutter = substr(' ' . (' ' . $number), -$maxWidth) . ' | ';
|
|
|
|
|
|
|
|
if ($number == $lineNumber) {
|
|
|
|
$spacing = str_repeat(" ", strlen($gutter) + $column - 2);
|
|
|
|
$prepared[] = '>' . $gutter . $line . "\n " . $spacing . '^';
|
|
|
|
} else {
|
|
|
|
$prepared[] = ' ' . $gutter . $line;
|
|
|
|
}
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
2017-11-06 22:46:07 +01:00
|
|
|
|
|
|
|
return implode("\n", $prepared);
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* Delete a page
|
|
|
|
*
|
2016-07-27 21:32:51 +02:00
|
|
|
* @param int $page_id
|
2015-07-17 23:38:06 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-05-22 14:48:09 +02:00
|
|
|
public function deletePage($page_id)
|
|
|
|
{
|
|
|
|
try {
|
2016-04-15 10:43:44 +02:00
|
|
|
return json_decode($this->getClient()->delete('content/' . $page_id)->getBody(), true);
|
2015-05-22 14:48:09 +02:00
|
|
|
} catch (BadResponseException $e) {
|
|
|
|
throw $this->handleError($e);
|
|
|
|
}
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
|
|
|
|
2017-11-07 22:08:30 +01:00
|
|
|
private function getAttachment($id, $attachment)
|
2015-05-22 14:48:09 +02:00
|
|
|
{
|
2015-08-02 15:42:23 +02:00
|
|
|
// Check if an attachment with
|
|
|
|
// this name is uploaded
|
2015-05-22 14:48:09 +02:00
|
|
|
try {
|
2017-03-02 18:17:29 +01:00
|
|
|
$url = "content/$id/child/attachment?filename=" . urlencode($attachment['filename']);
|
2017-11-07 22:08:30 +01:00
|
|
|
return json_decode($this->getClient()->get($url)->getBody(), true);
|
2015-05-22 14:48:09 +02:00
|
|
|
} catch (BadResponseException $e) {
|
|
|
|
throw $this->handleError($e);
|
|
|
|
}
|
2017-11-07 22:08:30 +01:00
|
|
|
}
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2017-11-07 22:08:30 +01:00
|
|
|
private function putAttachment($url, $id, $attachment)
|
|
|
|
{
|
2017-03-02 18:17:29 +01:00
|
|
|
$contents = array_key_exists('file', $attachment) ? fopen($attachment['file']->getPath(), 'r') : $attachment['content'];
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
try {
|
|
|
|
$this->getClient()->post(
|
|
|
|
$url,
|
|
|
|
[
|
2017-03-02 18:17:29 +01:00
|
|
|
'multipart' => [['name' => 'file', 'contents' => $contents, 'filename' => $attachment['filename']]],
|
2015-05-22 14:48:09 +02:00
|
|
|
'headers' => ['X-Atlassian-Token' => 'nocheck'],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (BadResponseException $e) {
|
|
|
|
throw $this->handleError($e);
|
|
|
|
}
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
2017-11-07 22:08:30 +01:00
|
|
|
|
|
|
|
private function getFileSize($attachment)
|
|
|
|
{
|
|
|
|
if (array_key_exists('file', $attachment)) {
|
|
|
|
return filesize($attachment['file']->getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (function_exists('mb_strlen')) {
|
|
|
|
return mb_strlen($attachment['content']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return strlen($attachment['content']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
* @param array $attachment
|
|
|
|
* @param callback $write Write output to the console
|
|
|
|
*/
|
|
|
|
public function uploadAttachment($id, $attachment, $write)
|
|
|
|
{
|
|
|
|
$result = $this->getAttachment($id, $attachment);
|
|
|
|
|
|
|
|
$url = "content/$id/child/attachment";
|
|
|
|
|
|
|
|
// If the attachment is already uploaded,
|
|
|
|
// the update URL is different
|
|
|
|
if (count($result['results'])) {
|
|
|
|
|
|
|
|
if ($this->getFileSize($attachment) == $result['results'][0]['extensions']['fileSize']) {
|
|
|
|
$write(" ( An attachment of the same size already exists, skipping. )");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$url .= "/{$result['results'][0]['id']}/data";
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->putAttachment($url, $id, $attachment);
|
|
|
|
}
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|