diff --git a/src/Composer/ConfluenceComposer.php b/src/Composer/ConfluenceComposer.php index 40b5bdb..542ebf5 100644 --- a/src/Composer/ConfluenceComposer.php +++ b/src/Composer/ConfluenceComposer.php @@ -7,6 +7,7 @@ use HalloWelt\MediaWiki\Lib\Migration\DataBuckets; use HalloWelt\MediaWiki\Lib\Migration\IOutputAwareInterface; use HalloWelt\MediaWiki\Lib\Migration\Workspace; +use HalloWelt\MigrateConfluence\Utility\DrawIOFileHandler; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; use Symfony\Component\Console\Output\Output; @@ -95,10 +96,36 @@ public function buildXML( Builder $builder ) { $attachments = $pageAttachmentsMap[$pageTitle]; foreach ( $attachments as $attachment ) { $this->output->writeln( "Attachment: $attachment" ); + + $drawIoFileHandler = new DrawIOFileHandler(); + + // We do not need DrawIO data files in our wiki, just PNG image + if ( $drawIoFileHandler->isDrawIODataFile( $attachment ) ) { + continue; + } + if ( isset( $filesMap[$attachment] ) ) { $filePath = $filesMap[$attachment][0]; $attachmentContent = file_get_contents( $filePath ); + if ( $drawIoFileHandler->isDrawIOImage( $attachment ) ) { + // Find associated with DrawIO PNG image diagram XML + // If image has "image1.drawio.png" name, + // Then diagram XML will be stored in the "image1.drawio" file + $diagramFileName = substr( $attachment, 0, -4 ); + + if ( isset( $filesMap[$diagramFileName] ) ) { + $diagramContent = file_get_contents( $filesMap[$diagramFileName][0] ); + + // Need to bake DrawIO diagram XML into the PNG image + $attachmentContent = $drawIoFileHandler->bakeDiagramDataIntoImage( + $attachmentContent, $diagramContent + ); + } else { + $this->output->writeln( "No DrawIO diagram XML was found for image '$attachment'" ); + } + } + $this->workspace->saveUploadFile( $attachment, $attachmentContent ); $this->customBuckets->addData( 'title-uploads', $pageTitle, $attachment ); } else { diff --git a/src/Composer/_defaultpages/Template/Drawio b/src/Composer/_defaultpages/Template/Drawio index f9b5e2c..7578845 100644 --- a/src/Composer/_defaultpages/Template/Drawio +++ b/src/Composer/_defaultpages/Template/Drawio @@ -1 +1,4 @@ - \ No newline at end of file +{{#tag:drawio +| +|filename={{{diagramName}}} +}} \ No newline at end of file diff --git a/src/Utility/DrawIOFileHandler.php b/src/Utility/DrawIOFileHandler.php new file mode 100644 index 0000000..df53661 --- /dev/null +++ b/src/Utility/DrawIOFileHandler.php @@ -0,0 +1,61 @@ +assertTrue( $drawIoFileHandler->isDrawIODataFile( 'diagram.drawio' ) ); + $this->assertTrue( $drawIoFileHandler->isDrawIODataFile( 'diagram.drawio.tmp' ) ); + + $this->assertFalse( $drawIoFileHandler->isDrawIODataFile( 'diagram.drawio.png' ) ); + } + + /** + * @covers \HalloWelt\MigrateConfluence\Utility\DrawIOFileHandler::isDrawIOImage + */ + public function testIsDrawIOImage() { + $drawIoFileHandler = new DrawIOFileHandler(); + + $this->assertFalse( $drawIoFileHandler->isDrawIOImage( 'diagram.drawio' ) ); + $this->assertFalse( $drawIoFileHandler->isDrawIOImage( 'diagram.drawio.tmp' ) ); + + $this->assertTrue( $drawIoFileHandler->isDrawIOImage( 'diagram.drawio.png' ) ); + } + + /** + * @covers \HalloWelt\MigrateConfluence\Utility\DrawIOFileHandler::bakeDiagramDataIntoImage + */ + public function testBakeDiagramDataIntoImage() { + $drawIoFileHandler = new DrawIOFileHandler(); + + $diagramXml = file_get_contents( __DIR__ . '/data/diagram.drawio' ); + $imageContent = file_get_contents( __DIR__ . '/data/diagram.drawio.png' ); + + // Get expected diagram XML + $matches = []; + preg_match( '#(.*?)#s', $diagramXml, $matches ); + + $expectedDiagramXML = trim( $matches[0] ); + + // Bake diagram XML into PNG image meta data + $imageContent = $drawIoFileHandler->bakeDiagramDataIntoImage( $imageContent, $diagramXml ); + + // Extract and check diagram XML from the PNG + // Extraction is done with the same algorithm how it is done in the wiki + $encodedXML = preg_replace( + '#^.*?tEXt(.*?)IDAT.*?$#s', + '$1', + $imageContent + ); + $encodedXML = preg_replace( '/[[:^print:]]/', '', $encodedXML ); + $partiallyDecodedXML = urldecode( $encodedXML ); + + // Get actual diagram XML after extraction from PNG + $matches = []; + preg_match( '#(.*?)#s', $partiallyDecodedXML, $matches ); + + $actualDiagramXML = trim( $matches[0] ); + + $this->assertEquals( $expectedDiagramXML, $actualDiagramXML ); + } +} diff --git a/tests/phpunit/Utility/DrawIOFileHandler/data/diagram.drawio b/tests/phpunit/Utility/DrawIOFileHandler/data/diagram.drawio new file mode 100644 index 0000000..15f2a67 --- /dev/null +++ b/tests/phpunit/Utility/DrawIOFileHandler/data/diagram.drawio @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/phpunit/Utility/DrawIOFileHandler/data/diagram.drawio.png b/tests/phpunit/Utility/DrawIOFileHandler/data/diagram.drawio.png new file mode 100644 index 0000000..0568db3 Binary files /dev/null and b/tests/phpunit/Utility/DrawIOFileHandler/data/diagram.drawio.png differ