Skip to content
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

Add wxr-parser-large-file. #140

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably don't want to include this file :-)

"plugins": ["."],
"config": {
"WP_UPLOAD_MAX_FILESIZE": "128M",
"WP_MEMORY_LIMIT": "256M"
}
}
72 changes: 72 additions & 0 deletions src/helpers/class-xml-character-filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* XML_Character_Filter file.
*
* @package WordPress
* @subpackage Importer
*/

// String which will be prefixed to stream URLs to add this filter.
define( 'XML_CHARACTER_FILTER_PREFIX', 'php://filter/read=xml_character_filter/resource=' );

/**
* Class XML_Character_Filter.
*
* XML Character Stream Filter to sanitize XML input. Removes control characters except newline, tab and return.
*
* Usage: php://filter/read=xml_character_filter/resource=zip://archive.zip#import.xml;
*/
class XML_Character_Filter extends php_user_filter {
/**
* List of control characters to remove.
*
* @var array
*/
private $chars = array();

/**
* This method is called whenever data is read from or written to the attached stream (such as with fread() or fwrite()).
*
* @param resource $in A resource pointing to a bucket brigade which contains one or more bucket objects containing data to be filtered.
* @param resource $out A resource pointing to a second bucket brigade into which the modified buckets should be placed.
* @param int $consumed Reference to the length of the data that the filter reads in and alters.
* @param bool $closing Whether the stream is in the process of closing.
* @return int PSFS_PASS_ON|PSFS_FEED_ME|PSFS_ERR_FATAL.
*/
public function filter( $in, $out, &$consumed, $closing ) {
while ( $bucket = stream_bucket_make_writeable( $in ) ) { //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition
$consumed += $bucket->datalen;
$bucket->data = $this->replace_chars( $bucket->data );
stream_bucket_append( $out, $bucket );
}

return PSFS_PASS_ON;
}

/**
* This method is called during instantiation of the filter class object.
*
* @return bool
*/
public function onCreate() {
for ( $ascii_num = 0; $ascii_num < 32; $ascii_num++ ) {
if ( 9 !== $ascii_num && 10 !== $ascii_num && 13 !== $ascii_num ) {
$this->chars[] = chr( $ascii_num );
}
}
$this->chars[] = chr( 127 );

return true;
}

/**
* Replace control characters.
*
* @param string $string Data to replace.
* @return string
*/
private function replace_chars( $string ) {
return str_replace( $this->chars, ' ', $string );
}
}
stream_filter_register( 'xml_character_filter', 'XML_Character_Filter' );
Loading