Ancient History & Yak Shaving

From April 2003 until January of 2006, I wrote a blog which was incinerated in May 2011 by a house fire.   This weekend, 2 1/2 years after the fire, I found a backup of the old blog and restored the posts into my current, WordPress-based blog.

The old posts (and this post) are tagged with the category ‘ancient‘.

Below follows a description of the Yak Shaving necessary to restore those posts.  The tale is possibly interesting to someone, in that it includes a working example of using the WordPress XML-RPC API from node.js.

It was non-trivial to import the old posts into WordPress, because the old posts were found in a PostgreSQL database backup file, in a custom schema of my own devising, and a custom markup language.  I don’t use PostgreSQL any more, so I had to install postgres, restore my database, export it to JSON. Then I wrote a pair of scripts (in node.js) – one of which imported the old posts into WordPress, the other which deleted them again (used while I debugged the other script).

The documentation for both node-wordpress and the WordPress XML-RPC API was either obscure or non-existent, so here’s two code snippets, for reference of anyone else trying to use those interfaces.

Getting existing posts from the server:


var posts = wp_client.getPosts({status:'draft', number: 100}, null, function(nullity, postarray){
    postarray.forEach(function (p) {
        console.log(p);
    });
});

Creating new posts (from the old blog) on the server:


stories.forEach(function(story) {
    var post = { };
    post.title = story.subject;
    post.status = 'publish';
    post.type = 'post';
    // [new Date(2016, 05, 08, 11, 35, 10)] ?
    post.date = new Date(story.date); // story.date is like '2005-03-04 00:00:00'
    post.name = story.shortname;
    post.author = 1;
    post.content = sanitize(story.data);
    post.format = 'standard';
    post.sticky = false;
    post.terms = { category: [19] } ; //note that it is an array. 19 is the term id for 'ancient'
    if (! story.private) {
        wp_client.newPost(post, console.log);
    }
});

Leave a Reply

Your email address will not be published.