-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Data Liberation] Add XML API, Stream API, WXR URL Rewriter API (#1952)
A part of #1894. Follows up on #1893. This PR brings in a few more PHP APIs that were initially explored outside of Playground so that they can be incubated in Playground. See the linked descriptions for more details about each API: * XML Processor from WordPress/wordpress-develop#6713 * Stream chain from adamziel/wxr-normalize#1 * A draft of a WXR URL Rewriter class capable of rewriting URLs in WXR files ## Testing instructions * Confirm the PHPUnit tests pass in CI * Confirm the test suite looks reasonabel * That's it for now! It's all new code that's not actually used anywhere in Playground yet. I just want to merge it to keep iterating and improving.
- Loading branch information
Showing
25 changed files
with
6,400 additions
and
172 deletions.
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
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
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
47 changes: 47 additions & 0 deletions
47
packages/playground/data-liberation/src/WP_WXR_URL_Rewrite_Processor.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,47 @@ | ||
<?php | ||
|
||
class WP_WXR_URL_Rewrite_Processor { | ||
|
||
|
||
public static function stream( $current_site_url, $new_site_url ) { | ||
return WP_XML_Processor::stream( | ||
function ( $processor ) use ( $current_site_url, $new_site_url ) { | ||
if ( static::is_wxr_content_node( $processor ) ) { | ||
$text = $processor->get_modifiable_text(); | ||
$updated_text = wp_rewrite_urls( | ||
array( | ||
'block_markup' => $text, | ||
'current-site-url' => $current_site_url, | ||
'new-site-url' => $new_site_url, | ||
) | ||
); | ||
if ( $updated_text !== $text ) { | ||
$processor->set_modifiable_text( $updated_text ); | ||
} | ||
} | ||
} | ||
); | ||
} | ||
|
||
private static function is_wxr_content_node( WP_XML_Processor $processor ) { | ||
$breadcrumbs = $processor->get_breadcrumbs(); | ||
if ( | ||
! in_array( 'excerpt:encoded', $breadcrumbs, true ) && | ||
! in_array( 'content:encoded', $breadcrumbs, true ) && | ||
! in_array( 'guid', $breadcrumbs, true ) && | ||
! in_array( 'link', $breadcrumbs, true ) && | ||
! in_array( 'wp:attachment_url', $breadcrumbs, true ) && | ||
! in_array( 'wp:comment_content', $breadcrumbs, true ) && | ||
! in_array( 'wp:base_site_url', $breadcrumbs, true ) && | ||
! in_array( 'wp:base_blog_url', $breadcrumbs, true ) | ||
// Meta values are not supported yet. We'll need to support | ||
// WordPress core options that may be saved as JSON, PHP Deserialization, and XML, | ||
// and then provide extension points for plugins authors support | ||
// their own options. | ||
// !in_array('wp:postmeta', $processor->get_breadcrumbs()) | ||
) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/playground/data-liberation/src/stream-api/WP_Byte_Stream.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,80 @@ | ||
<?php | ||
|
||
abstract class WP_Byte_Stream { | ||
|
||
protected $state; | ||
|
||
public function __construct() { | ||
$this->state = new WP_Byte_Stream_State(); | ||
} | ||
|
||
public function is_eof(): bool { | ||
return ! $this->state->output_bytes && $this->state->state === WP_Byte_Stream_State::STATE_FINISHED; | ||
} | ||
|
||
public function get_file_id() { | ||
return $this->state->file_id; | ||
} | ||
|
||
public function skip_file(): void { | ||
$this->state->last_skipped_file = $this->state->file_id; | ||
} | ||
|
||
public function is_skipped_file() { | ||
return $this->state->file_id === $this->state->last_skipped_file; | ||
} | ||
|
||
public function get_chunk_type() { | ||
if ( $this->get_last_error() ) { | ||
return '#error'; | ||
} | ||
|
||
if ( $this->is_eof() ) { | ||
return '#eof'; | ||
} | ||
|
||
return '#bytes'; | ||
} | ||
|
||
public function append_eof() { | ||
$this->state->input_eof = true; | ||
} | ||
|
||
public function append_bytes( string $bytes, $context = null ) { | ||
$this->state->input_bytes .= $bytes; | ||
$this->state->input_context = $context; | ||
} | ||
|
||
public function get_bytes() { | ||
return $this->state->output_bytes; | ||
} | ||
|
||
public function next_bytes() { | ||
$this->state->reset_output(); | ||
if ( $this->is_eof() ) { | ||
return false; | ||
} | ||
|
||
// Process any remaining buffered input: | ||
if ( $this->generate_next_chunk() ) { | ||
return ! $this->is_skipped_file(); | ||
} | ||
|
||
if ( ! $this->state->input_bytes ) { | ||
if ( $this->state->input_eof ) { | ||
$this->state->finish(); | ||
} | ||
return false; | ||
} | ||
|
||
$produced_bytes = $this->generate_next_chunk(); | ||
|
||
return $produced_bytes && ! $this->is_skipped_file(); | ||
} | ||
|
||
abstract protected function generate_next_chunk(): bool; | ||
|
||
public function get_last_error(): string|null { | ||
return $this->state->last_error; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/playground/data-liberation/src/stream-api/WP_Byte_Stream_State.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,40 @@ | ||
<?php | ||
|
||
/** | ||
* This interface describes standalone streams, but it can also be | ||
* used to describe a stream Processor like WP_XML_Processor. | ||
* | ||
* In this prototype there are no pipes, streams, and processors. There | ||
* are only Byte Streams that can be chained together with the StreamChain | ||
* class. | ||
*/ | ||
class WP_Byte_Stream_State { | ||
const STATE_STREAMING = '#streaming'; | ||
const STATE_FINISHED = '#finished'; | ||
|
||
public $input_eof = false; | ||
public $input_bytes = null; | ||
public $output_bytes = null; | ||
public $state = self::STATE_STREAMING; | ||
public $last_error = null; | ||
public $input_context = null; | ||
|
||
public $file_id; | ||
public $last_skipped_file; | ||
|
||
public function reset_output() { | ||
$this->output_bytes = null; | ||
$this->file_id = 'default'; | ||
$this->last_error = null; | ||
} | ||
|
||
public function consume_input_bytes() { | ||
$bytes = $this->input_bytes; | ||
$this->input_bytes = null; | ||
return $bytes; | ||
} | ||
|
||
public function finish() { | ||
$this->state = self::STATE_FINISHED; | ||
} | ||
} |
Oops, something went wrong.