Skip to content

Commit 86dac2b

Browse files
committed
Fix WXRReader reentrancy tests by correctly matching '*' breadcrumbs to namespaced tags
1 parent 697768c commit 86dac2b

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

components/DataLiberation/EntityReader/WXREntityReader.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,7 @@ class WXREntityReader implements EntityReader {
171171
* @since WP_VERSION
172172
* @var int
173173
*/
174-
private $last_xml_byte_offset_outside_of_entity;
175-
176-
/**
177-
* The XML processor cursor of the last entity opener.
178-
*
179-
* @since WP_VERSION
180-
* @var string|null
181-
*/
182-
private $last_xml_cursor_outside_of_entity;
174+
private $entity_opener_byte_offset;
183175

184176
/**
185177
* Whether the current entity has been emitted.
@@ -431,13 +423,13 @@ public function get_reentrancy_cursor() {
431423
*/
432424
$xml_cursor = $this->xml->get_reentrancy_cursor();
433425
$xml_cursor = json_decode( base64_decode( $xml_cursor ), true );
434-
$xml_cursor['upstream_bytes_forgotten'] = $this->last_xml_byte_offset_outside_of_entity;
426+
$xml_cursor['upstream_bytes_forgotten'] = $this->entity_opener_byte_offset;
435427
$xml_cursor = base64_encode( json_encode( $xml_cursor ) );
436428

437429
return json_encode(
438430
array(
439431
'xml' => $xml_cursor,
440-
'upstream' => $this->last_xml_byte_offset_outside_of_entity,
432+
'upstream' => $this->entity_opener_byte_offset,
441433
'last_post_id' => $this->last_post_id,
442434
'last_comment_id' => $this->last_comment_id,
443435
)
@@ -663,8 +655,7 @@ private function read_next_entity() {
663655
}
664656

665657
if ( count( $breadcrumbs ) <= 2 && $this->xml->is_tag_opener() ) {
666-
$this->last_xml_byte_offset_outside_of_entity = $this->xml->get_token_byte_offset_in_the_input_stream();
667-
$this->last_xml_cursor_outside_of_entity = $this->xml->get_reentrancy_cursor();
658+
$this->entity_opener_byte_offset = $this->xml->get_token_byte_offset_in_the_input_stream();
668659
}
669660

670661
$tag_with_namespace = $this->xml->get_tag_name_with_namespace();
@@ -697,8 +688,7 @@ private function read_next_entity() {
697688
// the previous entity is finished.
698689
if ( $this->xml->is_tag_opener() ) {
699690
$this->set_entity_tag( $tag_with_namespace );
700-
$this->last_xml_byte_offset_outside_of_entity = $this->xml->get_token_byte_offset_in_the_input_stream();
701-
$this->last_xml_cursor_outside_of_entity = $this->xml->get_reentrancy_cursor();
691+
$this->entity_opener_byte_offset = $this->xml->get_token_byte_offset_in_the_input_stream();
702692
}
703693
continue;
704694
}
@@ -753,8 +743,9 @@ private function read_next_entity() {
753743
$this->xml->matches_breadcrumbs( array( 'rss', 'channel', '*' ) ) &&
754744
array_key_exists( $this->xml->get_tag_name_with_namespace(), static::KNOWN_SITE_OPTIONS )
755745
);
746+
756747
if ( $is_site_option_opener ) {
757-
$this->last_xml_byte_offset_outside_of_entity = $this->xml->get_token_byte_offset_in_the_input_stream();
748+
$this->entity_opener_byte_offset = $this->xml->get_token_byte_offset_in_the_input_stream();
758749
}
759750
continue;
760751
}

components/XML/Tests/XMLProcessorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,31 @@ public function test_matches_breadcrumbs() {
14611461
$this->assertTrue( $processor->matches_breadcrumbs( array( array( 'w.org', 'post' ), '*', 'image' ) ) );
14621462
}
14631463

1464+
/**
1465+
*
1466+
* @return void
1467+
*/
1468+
public function test_matches_breadcrumbs_wildcard_namespace() {
1469+
// Initialize the XMLProcessor with the given XML string
1470+
$processor = XMLProcessor::create_from_string(
1471+
<<<XML
1472+
<?xml version="1.0" encoding="UTF-8" ?>
1473+
<rss xmlns:wp="http://wordpress.org/export/1.2/">
1474+
<channel>
1475+
<wp:base_site_url>http://wordpress.com/</wp:base_site_url>
1476+
</channel>
1477+
</rss>
1478+
XML
1479+
);
1480+
1481+
$this->assertTrue( $processor->next_tag() ); // rss
1482+
$this->assertTrue( $processor->next_tag() ); // channel
1483+
$this->assertTrue( $processor->next_tag() ); // wp:base_site_url
1484+
1485+
// '*' should match wp:base_site_url
1486+
$this->assertTrue( $processor->matches_breadcrumbs( array( 'rss', 'channel', '*' ) ), 'A wildcard breadcrumb segment did not match the namespaced wp:base_site_url tag.' );
1487+
}
1488+
14641489
/**
14651490
*
14661491
* @return void

components/XML/XMLProcessor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3914,7 +3914,11 @@ public function matches_breadcrumbs( $breadcrumbs ) {
39143914

39153915
// Normalize crumb to [namespace, local_name]
39163916
if ( ! is_array( $crumb ) ) {
3917-
$crumb = array( '', $crumb );
3917+
if ( '*' === $crumb ) {
3918+
$crumb = ['*','*'];
3919+
} else {
3920+
$crumb = array( '', $crumb );
3921+
}
39183922
}
39193923
list( $namespace, $local_name ) = $crumb;
39203924

0 commit comments

Comments
 (0)