Skip to content

Instantly share code, notes, and snippets.

@flohei
Created August 26, 2012 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flohei/3481503 to your computer and use it in GitHub Desktop.
Save flohei/3481503 to your computer and use it in GitHub Desktop.
WPFix is a little script, that shrinks down the WordPress post ids. That's useful when you imported your Tumblr blog and the post ids got way to huge for WP to handle them.
package WPFix;
# pragmas
use warnings;
use strict 'vars';
use Data::Dumper;
use English qw(-no_match_vars);
# preparation
my $post_count = 20;
our $post_map = {};
# wp_posts
my $post_file = "wp-posts.sql";
my $post_output_file = "edited-wp-posts.sql";
my $post_regex = "\\((\\d+),";
open POST_INPUT_FILE, $post_file or die $!;
open POST_OUTPUT_FILE, ">", $post_output_file or die $!;
for (my $i = 0; <POST_INPUT_FILE>; $i++) {
my ($line) = $_;
chomp($line);
# check if the current line has a WP id in the beginning
if ($line =~ /$post_regex/) {
# get the old id
my $old_post_id = $1;
# save it to the map and assing a new id
$post_map->{$old_post_id} = $post_count;
$post_count++;
# replace the old value with the new one
substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[1] - $LAST_MATCH_START[1], $post_map->{$old_post_id};
}
print POST_OUTPUT_FILE $line;
print POST_OUTPUT_FILE "\n";
}
close POST_INPUT_FILE;
close POST_OUTPUT_FILE;
# wp_comments
my $comment_file = "wp-comments.sql";
my $comment_output_file = "edited-wp-comments.sql";
my $comment_regex = "\\(\\d+,.(\\d+)";
open COMMENT_INPUT_FILE, $comment_file or die $!;
open COMMENT_OUTPUT_FILE, ">", $comment_output_file or die $!;
for (my $i = 0; <COMMENT_INPUT_FILE>; $i++) {
my ($line) = $_;
chomp($line);
if ($line =~ /$comment_regex/) {
# get the old id
my $old_post_id = $1;
# replace the old value with the new one
substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[1] - $LAST_MATCH_START[1], $post_map->{$old_post_id};
}
print COMMENT_OUTPUT_FILE $line;
print COMMENT_OUTPUT_FILE "\n";
}
close COMMENT_INPUT_FILE;
close COMMENT_OUTPUT_FILE;
# wp_postmeta
my $postmeta_file = "wp-postmeta.sql";
my $postmeta_output_file = "edited-wp-postmeta.sql";
my $postmeta_regex = "\\(\\d+,.(\\d+)";
open POSTMETA_INPUT_FILE, $postmeta_file or die $!;
open POSTMETA_OUTPUT_FILE, ">", $postmeta_output_file or die $!;
for (my $i = 0; <POSTMETA_INPUT_FILE>; $i++) {
my ($line) = $_;
chomp($line);
if ($line =~ /$postmeta_regex/) {
# get the old id
my $old_post_id = $1;
# replace the old value with the new one
substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[1] - $LAST_MATCH_START[1], $post_map->{$old_post_id};
}
print POSTMETA_OUTPUT_FILE $line;
print POSTMETA_OUTPUT_FILE "\n";
}
close POSTMETA_INPUT_FILE;
close POSTMETA_OUTPUT_FILE;
# wp_term_relationships
my $term_relationship_file = "wp-term-relationships.sql";
my $term_relationship_output_file = "edited-wp-term-relationships.sql";
my $term_relationship_regex = "\\((\\d+),";
open TERM_RELATIONSHIP_INPUT_FILE, $term_relationship_file or die $!;
open TERM_RELATIONSHIP_OUTPUT_FILE, ">", $term_relationship_output_file or die $!;
for (my $i = 0; <TERM_RELATIONSHIP_INPUT_FILE>; $i++) {
my ($line) = $_;
chomp($line);
if ($line =~ /$term_relationship_regex/) {
# get the old id
my $old_post_id = $1;
# replace the old value with the new one
substr $line, $LAST_MATCH_START[1], $LAST_MATCH_END[1] - $LAST_MATCH_START[1], $post_map->{$old_post_id};
}
print TERM_RELATIONSHIP_OUTPUT_FILE $line;
print TERM_RELATIONSHIP_OUTPUT_FILE "\n";
}
close TERM_RELATIONSHIP_INPUT_FILE;
close TERM_RELATIONSHIP_OUTPUT_FILE;
@flohei
Copy link
Author

flohei commented Aug 27, 2012

See http://blog.flohei.de/2012/08/fixing-the-post-id-231-1-issue/ for more information on why I wrote that script and a warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment