Skip to content
This repository was archived by the owner on May 30, 2025. It is now read-only.

XMLProcessor: Support namespaces #23

Draft
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/DataLiberation/EntityReader/EPubEntityReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ private function parse_manifest() {
);
while ( $xml->next_tag() ) {
$parsed_entry = array();
$keys = $xml->get_attribute_names_with_prefix( '' );
$keys = $xml->get_attribute_qualified_names_with_prefix( '' );
foreach ( $keys as $key ) {
$parsed_entry[ $key ] = $xml->get_attribute( $key );
}
if ( $xml->matches_breadcrumbs( array( 'metadata', '*' ) ) ) {
$parsed['metadata'][] = array(
'tag' => $xml->get_tag(),
'tag' => $xml->get_tag_local_name(),
'attributes' => $parsed_entry,
);
} elseif ( $xml->matches_breadcrumbs( array( 'manifest', 'item' ) ) ) {
Expand Down
10 changes: 5 additions & 5 deletions components/DataLiberation/EntityReader/WXREntityReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ private function read_next_entity() {
$this->last_xml_cursor_outside_of_entity = $this->xml->get_reentrancy_cursor();
}

$tag = $this->xml->get_tag();
$tag = $this->xml->get_tag_local_name();
/**
* Custom adjustment: the Accessibility WXR file uses a non-standard
* wp:wp_author tag.
Expand Down Expand Up @@ -732,7 +732,7 @@ private function read_next_entity() {
*/
if ( $this->xml->is_tag_opener() ) {
$this->last_opener_attributes = array();
$names = $this->xml->get_attribute_names_with_prefix( '' );
$names = $this->xml->get_attribute_qualified_names_with_prefix( '' );
foreach ( $names as $name ) {
$this->last_opener_attributes[ $name ] = $this->xml->get_attribute( $name );
}
Expand All @@ -741,7 +741,7 @@ private function read_next_entity() {
$is_site_option_opener = (
count( $this->xml->get_breadcrumbs() ) === 3 &&
$this->xml->matches_breadcrumbs( array( 'rss', 'channel', '*' ) ) &&
array_key_exists( $this->xml->get_tag(), static::KNOWN_SITE_OPTIONS )
array_key_exists( $this->xml->get_tag_local_name(), static::KNOWN_SITE_OPTIONS )
);
if ( $is_site_option_opener ) {
$this->last_xml_byte_offset_outside_of_entity = $this->xml->get_token_byte_offset_in_the_input_stream();
Expand Down Expand Up @@ -848,13 +848,13 @@ private function read_next_entity() {
* @return bool Whether a site_option entity was emitted.
*/
private function parse_site_option() {
if ( ! array_key_exists( $this->xml->get_tag(), static::KNOWN_SITE_OPTIONS ) ) {
if ( ! array_key_exists( $this->xml->get_tag_local_name(), static::KNOWN_SITE_OPTIONS ) ) {
return false;
}

$this->entity_type = 'site_option';
$this->entity_data = array(
'option_name' => static::KNOWN_SITE_OPTIONS[ $this->xml->get_tag() ],
'option_name' => static::KNOWN_SITE_OPTIONS[ $this->xml->get_tag_local_name() ],
'option_value' => $this->text_buffer,
);
$this->emit_entity();
Expand Down
415 changes: 208 additions & 207 deletions components/XML/Tests/XMLProcessorTest.php

Large diffs are not rendered by default.

115 changes: 115 additions & 0 deletions components/XML/XMLAttributeToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
namespace WordPress\XML;

/**
* XML API: XMLAttributeToken class
*
* @package WordPress
* @subpackage XML-API
* @since 6.2.0
*/

/**
* Core class used by the XML tag processor as a data structure for the attribute token,
* allowing to drastically improve performance.
*
* This class is for internal usage of the XMLProcessor class.
*
* @see XMLProcessor
*/
class XMLAttributeToken {
/**
* Attribute name.
*
* @since 6.2.0
*
* @var string
*/
public $qualified_name;

/**
* Attribute value.
*
* @since 6.2.0
*
* @var int
*/
public $value_starts_at;

/**
* How many bytes the value occupies in the input XML.
*
* @since 6.2.0
*
* @var int
*/
public $value_length;

/**
* The string offset where the attribute name starts.
*
* @since 6.2.0
*
* @var int
*/
public $start;

/**
* Byte length of text spanning the attribute inside a tag.
*
* This span starts at the first character of the attribute name
* and it ends after one of three cases:
*
* - at the end of the attribute name for boolean attributes.
* - at the end of the value for unquoted attributes.
* - at the final single or double quote for quoted attributes.
*
* @var int
*/
public $length;

/**
* Namespace prefix.
*
* @var string
*/
public $namespace_prefix;

/**
* Local name.
*
* @var string
*/
public $local_name;

/**
* Namespace.
*
* @var string
*/
public $namespace;

/**
* Constructor.
*
* @param string $name Attribute name.
* @param int $value_start Attribute value.
* @param int $value_length Number of bytes attribute value spans.
* @param int $start The string offset where the attribute name starts.
* @param int $length Byte length of the entire attribute name or name and value pair expression.
* @param string $namespace_prefix Namespace prefix.
* @param string $local_name Local name.
* @param string $namespace Namespace.
*
*/
public function __construct( $value_start, $value_length, $start, $length, $namespace_prefix=null, $local_name=null, $namespace=null ) {
$this->value_starts_at = $value_start;
$this->value_length = $value_length;
$this->start = $start;
$this->length = $length;
$this->namespace_prefix = $namespace_prefix;
$this->local_name = $local_name;
$this->namespace = $namespace;
$this->qualified_name = $namespace_prefix ? $namespace_prefix . ':' . $local_name : $local_name;
}
}
76 changes: 76 additions & 0 deletions components/XML/XMLElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
namespace WordPress\XML;

/**
* XML API: XMLElement class
*
* @package WordPress
* @subpackage XML-API
* @since 6.2.0
*/

/**
* Core class used by the XML tag processor as a data structure for the attribute token,
* allowing to drastically improve performance.
*
* This class is for internal usage of the XMLProcessor class.
*
* @see XMLProcessor
*/
class XMLElement {
/**
* Local name.
*
* @var string
*/
public $local_name;

/**
* Namespace Prefix.
*
* @var string
*/
public $namespace_prefix;

/**
* Full XML namespace name.
*
* @var string
*/
public $namespace;

/**
* Namespaces in current element's scope.
*
* @var array<string, string>
*/
public $namespaces_in_scope;

/**
* Qualified name.
*
* @var string
*/
public $qualified_name;

/**
* Constructor.
*
* @param string $local_name Local name.
* @param string $namespace_prefix Namespace prefix.
* @param string $namespace Full XML namespace name.
* @param array<string, string> $namespaces_in_scope Namespaces in current element's scope.
*/
public function __construct( $local_name, $namespace_prefix, $namespace, $namespaces_in_scope ) {
$this->local_name = $local_name;
$this->namespace_prefix = $namespace_prefix;
$this->namespace = $namespace;
$this->namespaces_in_scope = $namespaces_in_scope;
$this->qualified_name = $namespace_prefix ? $namespace_prefix . ':' . $local_name : $local_name;
}

public function get_full_name() {
return $this->namespace ? '{' . $this->namespace . '}' . $this->local_name : $this->local_name;
}

}
Loading
Loading