#!/usr/bin/perl use strict; use File::Find (); use File::Touch; use Data::Dumper; use WordPress::API::Post; use WordPress::API; use post; # # export PROXY=https://www.sdu.se/blogtest/xmlrpc.php)\n"; # export WUSERNAME=johndoe # export WPASSWORD=secret # my $proxy = $ENV{'PROXY' } || die "Environment variable PROXY must be set\n"; my $username = $ENV{'WUSERNAME'} || die "Environment variable WUSERNAME must be set\n"; my $password = $ENV{'WPASSWORD'} || die "Environment variable WPASSWORD must be set\n"; sub d { print Data::Dumper->Dump(@_); } my @post_file_names; sub find_pred { if (my ($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) { if (-f _) { if (/^.*\.post\z/s) { (my $tmp = $File::Find::name) =~ s/post$/html/; if (-f $tmp) { push @post_file_names, $tmp; } } } } } sub main() { my %slug_to_id; my $w = WordPress::API->new({ proxy => $proxy, username => $username, password => $password }); my $posts = $w->getRecentPosts() || die "getRecentPosts failed ($!)"; map { $slug_to_id{$_->{'wp_slug'}} = $_->{'postid'} } @$posts; my $len; my $total = 0; my $uploaded = 0; foreach $_ (@$posts) { $total++; my $slug = $_->{'wp_slug'}; if (length($slug) == 0) { ; } elsif (length($slug) < 5) { warn "Skipping article with very short slug ($!)\n"; } else { undef @post_file_names; File::Find::find({wanted => \&find_pred, no_chdir => 1}, $slug); #--- Find *.html files if (!@post_file_names) { print " No post found for $slug\n"; next; } elsif (@post_file_names > 1) { print " More than one post found for $slug\n"; next; } my $id = $_->{'postid'}; my $filename = shift @post_file_names; (my $ts = $filename) =~ s/html$/ts/; $len = length($slug) if (length($slug) > $len); if (! -e $filename) { printf(" Post %-*s does not exist\n", $len, $slug); #--- Paranoid double-check } elsif ((stat($filename))[9] < (stat($ts))[9]) { printf(" Post %-*s already uploaded\n", $len, $slug); } else { my $body = read_file($filename); my $len = length($body); if ($len < 1000) { warn("Skipping short post $slug (length is only $len bytes\n"); } else { my $p = WordPress::API::Post->new({ proxy => $proxy, username => $username, password => $password }); $p->id($_->{'postid'}); my $tmp = $p->load(); #--- Download post $p->description($body); #--- Update body print("Uploading [$id, $slug]\n"); $tmp = $p->save(); #--- Upload modified post touch($ts); #--- Update timestamp my $tmp = $p->load(); #--- Download post $uploaded++; } } } }; print("Uploaded $uploaded of $total files.\n"); } main();