Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/ConfluencePageContentDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Artemeon\Confluence\Endpoint\Download;
use Artemeon\Confluence\Endpoint\Dto\ConfluencePage;
use Artemeon\Confluence\MacroReplacer\MacroReplacerInterface;
use DOMDocument;
use Exception;

class ConfluencePageContentDownloader
Expand All @@ -25,6 +26,8 @@ public function __construct(Content $contentEndpoint, Download $downloadEndpoint

public function downloadPageContent(ConfluencePage $page, bool $withAttachments = true): void
{
$page = $this->repairPageContent($page);

try {
foreach ($this->macroReplacers as $macroReplacer) {
if ($macroReplacer instanceof MacroReplacerInterface) {
Expand All @@ -42,10 +45,29 @@ public function downloadPageContent(ConfluencePage $page, bool $withAttachments
foreach ($attachments as $attachment) {
$this->downloadEndpoint->downloadAttachment($attachment);
}

} catch (Exception $e) {
echo 'An error has occurred: ' . $e->getMessage();
}
}

private function repairPageContent(ConfluencePage $page): ConfluencePage
{
$previousLibxmlState = libxml_use_internal_errors(true);

$domDocument = new DOMDocument();
$domDocument->loadHTML($page->getContent());
if (!$domDocument->validate()) {
$pageContent = '';
foreach ($domDocument->getElementsByTagName('body')->item(0)->childNodes as $child) {
$pageContent .= $domDocument->saveHTML($child);
}

$page->setContent($pageContent);
}

libxml_clear_errors();
libxml_use_internal_errors($previousLibxmlState);

return $page;
}
}
6 changes: 5 additions & 1 deletion src/Endpoint/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public function findChildAttachments(string $pageId): array
{
$response = $this->client->get(
'wiki/rest/api/content/' . $pageId . '/child/attachment',
array_merge([], $this->auth->getAuthenticationArray())
array_merge([
'query' => [
'expand' => 'history,history.lastUpdated'
]
], $this->auth->getAuthenticationArray())
);

if ($response->getStatusCode() === 200) {
Expand Down
33 changes: 27 additions & 6 deletions src/Endpoint/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,33 @@ public function downloadAttachment(ConfluenceAttachment $attachment): void
return;
}

// Verwende den relativen Pfad aus der API, um das Attachment herunterzuladen
$attachmentContent = $this->client->get(
'/wiki/' . $attachment->findDownloadPath(),
array_merge([], $this->auth->getAuthenticationArray())
)->getBody()->getContents();
if ($this->shouldAttachmentBeUpdated($attachment)) {
// Verwende den relativen Pfad aus der API, um das Attachment herunterzuladen
$attachmentContent = $this->client->get(
'/wiki/' . $attachment->findDownloadPath(),
array_merge([], $this->auth->getAuthenticationArray())
)->getBody()->getContents();

file_put_contents($this->downloadFolder . '/' . $attachment->getTitle(), $attachmentContent);
file_put_contents($this->getAttachmentFilePath($attachment), $attachmentContent);
}
}

private function getAttachmentFilePath(ConfluenceAttachment $attachment): string
{
return $this->downloadFolder . '/' . $attachment->getTitle();
}

private function shouldAttachmentBeUpdated(ConfluenceAttachment $attachment): bool
{
$filepath = $this->getAttachmentFilePath($attachment);

if (file_exists($filepath)) {
$filemtime = filemtime($filepath);
if (is_int($filemtime)) {
return $filemtime < $attachment->getLastUpdated()->getTimestamp();
}
}

return true;
}
}
10 changes: 10 additions & 0 deletions src/Endpoint/Dto/ConfluenceAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@

namespace Artemeon\Confluence\Endpoint\Dto;

use DateTime;

class ConfluenceAttachment
{
private array $rawData;

private string $title;

private ?DateTime $lastUpdated;

public function __construct(array $rawData)
{
$this->rawData = $rawData;

$this->title = $rawData['title'];
$this->lastUpdated = isset($rawData['history']['lastUpdated']['when']) ? new DateTime($rawData['history']['lastUpdated']['when']) : null;
}

public function findDownloadPath(): ?string
Expand All @@ -26,4 +31,9 @@ public function getTitle(): string
{
return $this->title;
}

public function getLastUpdated(): ?DateTime
{
return $this->lastUpdated;
}
}
Loading