From 40a0a0431ebef9f7e3c59589366410d3ba33f797 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Thu, 7 Apr 2011 21:44:44 +0000 Subject: [PATCH] Second part of bug bug 22881: Allow exporting files along with the XML as mimepart/related file. The patch attached to that bug was unfortunately not up to coding standards, so I rewrote this part myself. --- includes/Export.php | 51 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/includes/Export.php b/includes/Export.php index 91eb7af375..9157f4bc7d 100644 --- a/includes/Export.php +++ b/includes/Export.php @@ -35,6 +35,8 @@ class WikiExporter { var $author_list = "" ; var $dumpUploads = false; + var $multiPart = false; + var $files = array(); const FULL = 1; const CURRENT = 2; @@ -86,12 +88,23 @@ class WikiExporter { public function openStream() { $output = $this->writer->openStream(); - $this->sink->writeOpenStream( $output ); + + if ( $this->multiPart ) { + $this->openMultipart(); + $this->sink->write( $output ); + } else { + $this->sink->writeOpenStream( $output ); + } } public function closeStream() { $output = $this->writer->closeStream(); - $this->sink->writeCloseStream( $output ); + if ( $this->multiPart ) { + $this->sink->write( $output ); + $this->closeMultipart(); + } else { + $this->sink->writeCloseStream( $output ); + } } /** @@ -314,6 +327,7 @@ class WikiExporter { $output = ''; if ( $this->dumpUploads ) { $output .= $this->writer->writeUploads( $last ); + $this->attachUploads( $last ); } $output .= $this->writer->closePage(); $this->sink->writeClosePage( $output ); @@ -329,6 +343,7 @@ class WikiExporter { $output = ''; if ( $this->dumpUploads ) { $output .= $this->writer->writeUploads( $last ); + $this->attachUploads( $last ); } $output .= $this->author_list; $output .= $this->writer->closePage(); @@ -342,6 +357,38 @@ class WikiExporter { $this->sink->writeLogItem( $row, $output ); } } + + protected function attachUploads( $row ) { + $title = Title::newFromRow( $row ); + $file = wfLocalFile( $title ); + $this->files[] = $file; + $this->files = array_merge( $this->files, $file->getHistory() ); + } + + protected function openMultipart() { + # Multipart boundary purposely invalid XML + $this->boundary = '<' . dechex( mt_rand() ) . dechex( mt_rand() ) . '<'; + $this->sink->writeOpenStream( + "Content-Type: multipart/related; boundary={$this->boundary};" . + " type=text/xml\n\n" . + "--{$this->boundary}\nContent-Type: text/xml\n\n" + ); + } + + protected function closeMultipart() { + $output = ''; + + foreach ( $this->files as $file ) { + $output .= "\n--{$this->boundary}\n" . + 'Content-Type: ' . $file->getMimeType() . "\n" . + 'Content-ID: ' . $file->getRel() . "\n" . + 'Content-Length: ' . $file->getSize() . "\n" . + 'X-Sha1Base36: ' . $file->getSha1() . "\n\n"; + $this->sink->write( $output ); + $this->sink->write( file_get_contents( $file->getPath() ) ); + } + $this->sink->writeCloseStream( "\n--{$this->boundary}\n" ); + } } /** -- 2.20.1