#!/usr/bin/perl -w # use strict; use DBI; ### config my %DB_CONFIG = ( 'database' => 'wordpress', 'hostname' => 'localhost', 'port' => 3306, 'datasource' => 'DBI:mysql:wordpress', 'username' => 'wordpress', 'password' => 'mysecret' ); my $prefix = 'wp_'; my $DEBUG = 0; ### end config sub dbConnect { my ($config, $debug) = @_; print STDERR "getting new db connection.\n" if ($debug); my %config = %$config; my $dbh = DBI->connect($config{'datasource'}, $config{'username'}, $config{'password'}, { RaiseError => 1 }); return $dbh; } sub dbSQL { my ($dbh, $sql_statement, $debug) = @_; print STDERR "SQL: $sql_statement\n" if ($debug); my $sth = $dbh->prepare($sql_statement); $sth->execute; return $sth; } sub printOut { my $out = shift; print $out."\n"; } &printOut("Connecting to DB..."); my $dbh = &dbConnect(\%DB_CONFIG); &printOut("Cleaning categories ... "); &dbSQL($dbh, "TRUNCATE ${prefix}categories", $DEBUG); &dbSQL($dbh, "TRUNCATE ${prefix}post2cat", $DEBUG); my %terms; my $sth = &dbSQL($dbh, "select t.term_id, t.name, t.slug, x.description, x.parent, x.count from ${prefix}terms t, ${prefix}term_taxonomy x where t.term_id = x.term_id and x.taxonomy = 'category';", $DEBUG); my ($term_id, $name, $slug, $description, $parent, $count); while (($term_id, $name, $slug, $description, $parent, $count) = $sth->fetchrow_array) { my $query = "INSERT INTO ${prefix}categories (cat_name, category_nicename, category_description, category_parent, category_count) VALUES('$name', '$slug', '$description', $parent, $count);"; my $sth2 = &dbSQL($dbh, $query); $terms{$term_id} = $dbh->last_insert_id(undef, undef, undef, undef); } $sth->finish; my $id; foreach $id (sort keys %terms) { my $sth2 = &dbSQL($dbh, "SELECT object_id FROM ${prefix}term_relationships WHERE term_taxonomy_id = $id"); my $post_id; while (($post_id) = $sth2->fetchrow_array) { &dbSQL($dbh, "INSERT INTO ${prefix}post2cat(post_id, category_id) VALUES($post_id, $terms{$id})"); } } &printOut("fixing pages..."); &dbSQL($dbh, "UPDATE ${prefix}posts SET post_status='static' WHERE post_type='page'"); &printOut("done"); $dbh->disconnect;