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 a CLI script for scrubbing email addresses etc from export files #69

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
67 changes: 67 additions & 0 deletions env/scrub-export.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* A CLI tool for scrubbing a WordPress WXR export file of likely PII.
*/

$opts = getopt( '', ['namespace:'], $last );

$namespace = $opts['namespace'] ?? 'http://wordpress.org/export/1.2/';
$infile = $argv[ $last ] ?? 'php://stdin';
$outfile = $argv[ $last + 1 ] ?? 'php://stdout';

if ( is_null( $infile ) ) {
die( "Usage: {$argv[0]} [--namespace=http://wordpress.org/export/1.2/] <infile.wxr> [outfile.wxr]\n" );
}

$doc = new DomDocument();
if ( !$doc->load( $infile ) ) {
fwrite( STDERR, "Unable to open $infile for writing.\n" );
die(1);
}

$fp_out = fopen( $outfile, 'x' );
if ( !$fp_out ) {
fwrite( STDERR, "Unable to open $outfile for writing.\n" );
die(1);
}


fwrite( STDERR, "Scrubbing $infile to $outfile\n" );

// These are all in the `<wp:...>` namespace
$wp_elements_to_scrub = [
'author_login',
'author_email',
'author_first_name',
'author_last_name',
'author_display_name',
'comment_author',
'comment_author_email',
'comment_author_url',
'comment_author_IP',
];

foreach ( $wp_elements_to_scrub as $tag ) {
$count_replaced = 0;
$nodes = $doc->getElementsByTagNameNS( $namespace, $tag );
foreach( $nodes as $node ) {
// There should only be one child (a text node) but let's loop just in case
$done = 0;
while ( $node->firstChild ) {
if ( $node->removeChild( $node->firstChild ) ) {
++ $done;
}
}
if ( $done ) {
$node->appendChild( new DOMText( '__REDACTED__' ) );
++ $count_replaced;
}
}

fwrite( STDERR, "Replaced $count_replaced instances of wp:$tag\n" );
}

fwrite( $fp_out, $doc->saveXML() );

fclose( $fp_out );
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fclose( $fp_out );
fclose( $fp_out );
readline( "\nMake sure you manually review the diff before committing this to the repository! This script might have missed some PII. \n" );