-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explore Data Liberation exporter #2078
Draft
brandonpayton
wants to merge
6
commits into
trunk
Choose a base branch
from
explore/data-liberation-exporter
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5156616
Add WP_Exporter class
brandonpayton 7988e23
Add temp test endpoint for getting export package
brandonpayton 3a1f2e9
Try to flush output per file
brandonpayton dcdccc8
Place uploads under wp-content/uploads in zip
brandonpayton a651b4d
Remove unused use declaration
brandonpayton 328a4d3
Add temporary attachment URL substitution hack
brandonpayton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/playground/data-liberation/src/export/WP_Exporter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
use WordPress\Zip\ZipStreamWriter; | ||
|
||
class WP_Exporter { | ||
public static function stream_export( $output_stream = false ) { | ||
// @TODO: This is a hack. Maybe we should have a way to export without setting headers. | ||
$preexisting_response_headers = headers_list(); | ||
|
||
require_once ABSPATH . 'wp-admin/includes/export.php'; | ||
ob_start(); | ||
export_wp(); | ||
|
||
// @TODO: This is a hack to avoid headers set by export_wp(). Maybe we should have a way to export without setting headers. | ||
header_remove(); | ||
foreach ( $preexisting_response_headers as $header ) { | ||
header( $header, false ); | ||
} | ||
|
||
$wxr_content = ob_get_clean(); | ||
|
||
// @TODO: Replace upload URLs with relative file URLs. | ||
|
||
$uploads = wp_upload_dir(); | ||
// @TODO: This is a hack and kind of broken. Replace attachment URLs using proper XML and URL parsing libraries. | ||
$wxr_content = str_replace( | ||
trailingslashit( $uploads['baseurl'] ), | ||
'file://./wp-content/uploads/', | ||
$wxr_content | ||
); | ||
|
||
header('Content-Type: application/zip'); | ||
|
||
// @TODO: Can we get rid of this open-stdout-on-demand workaround? | ||
// NOTE: Opening stdout on demand after output buffering the export | ||
// because output buffering seemed to interfere with a preexisting stdout stream. | ||
// By opening stdout after output buffering, streaming the zip to stdout appears to work. | ||
if ( !$output_stream ) { | ||
$output_stream = fopen('php://output', 'wb'); | ||
} | ||
$zip_writer = new ZipStreamWriter( $output_stream ); | ||
$zip_writer->writeFileFromString( 'META-INF/export.wxr', $wxr_content ); | ||
|
||
$uploads_path = $uploads['basedir']; | ||
|
||
$flags = \FilesystemIterator::SKIP_DOTS; | ||
$uploads_iterator = new \RecursiveIteratorIterator( | ||
new \RecursiveDirectoryIterator( | ||
$uploads_path, | ||
$flags | ||
) | ||
); | ||
|
||
foreach ( $uploads_iterator as $file ) { | ||
if ( $file->isDir() ) { | ||
continue; | ||
} | ||
$absolute_path = $file->getPathname(); | ||
$relative_path = substr( $absolute_path, strlen($uploads_path) + 1 ); | ||
$zip_writer->writeFileFromPath( | ||
// TODO: How to handle unconventional upload locations? | ||
"wp-content/uploads/$relative_path", | ||
$absolute_path | ||
); | ||
|
||
// TODO: Is this necessary to make sure per-file output is flushed? | ||
fflush( $output_stream ); | ||
} | ||
|
||
$zip_writer->finish(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's try packaging the data using the same
WP_Entity
objects as the importer. We could then have a single streaming export pipeline that knows how to deal with entities on one end, and uses an arbitrary export drivers on the other end, e.g. WXR, Markdown, HTML, etc.Even more importantly, we could serialize the exported entities, send them over the wire, and import without using any particular data format. That's important for site sync protocol and for things like the Try WordPress extension. Plus we could extend it to more data types, e.g. SQL dumps, Blueprint steps, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ @adamziel, I don't know exactly what this means yet but will look at the importer work for reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spoke with Adam, and what we are talking about is basically making a WP_Entity iterator API that can be used to read WP entities from a site. Then the entity iterator API can be used to implement multiple exporters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The plan for this PR is to just tweak URL replacement to work properly and then leave open as a draft until it can be replaced with a proper exporter based on the entity iterator.