{"id":1229,"date":"2013-11-12T10:11:40","date_gmt":"2013-11-12T15:11:40","guid":{"rendered":"https:\/\/risacher.org\/jfdi\/?p=1229"},"modified":"2016-08-23T11:48:56","modified_gmt":"2016-08-23T15:48:56","slug":"ancient-history-yak-shaving","status":"publish","type":"post","link":"https:\/\/risacher.org\/jfdi\/2013\/11\/ancient-history-yak-shaving\/","title":{"rendered":"Ancient History &#038; Yak Shaving"},"content":{"rendered":"<p>From April 2003 until January of 2006, I wrote a blog which was incinerated in May 2011 by a house fire.\u00a0\u00a0 This weekend, 2 1\/2 years after the fire, I found a backup of the old blog and restored the posts into my current, <a title=\"WordPress\" href=\"http:\/\/wordpress.org\/\">WordPress<\/a>-based blog.<\/p>\n<p>The old posts (and this post) are tagged with the category &#8216;<a title=\"ancient category\" href=\"https:\/\/risacher.org\/jfdi\/category\/ancient\/\">ancient<\/a>&#8216;.<\/p>\n<p>Below follows a description of the <a title=\"define: yak shaving\" href=\"https:\/\/www.google.com\/search?q=define%3A+yak+shaving\">Yak Shaving<\/a> necessary to restore those posts.\u00a0 The tale is possibly interesting to <em>someone,<\/em> in that it includes a working example of using the <a title=\"WordPress XML-RPC API\" href=\"http:\/\/codex.wordpress.org\/XML-RPC_WordPress_API\">WordPress XML-RPC API<\/a> from <a title=\"node.js\" href=\"http:\/\/nodejs.org\/\">node.js<\/a>.<!--more--><\/p>\n<p>It was non-trivial to import the old posts into WordPress, because the old posts were found in a <a title=\"PostgreSQL\" href=\"http:\/\/www.postgresql.org\/\">PostgreSQL<\/a> database backup file, in a custom schema of my own devising, and a custom markup language.\u00a0 I don&#8217;t use PostgreSQL any more, so I had to install postgres, restore my database, export it to <a title=\"JSON\" href=\"http:\/\/www.json.org\/\">JSON<\/a>. Then I wrote a pair of scripts (in node.js) &#8211; one of which imported the old posts into WordPress, the other which deleted them again (used while I debugged the other script).<\/p>\n<p>The documentation for both <a title=\"node-wordpress on github\" href=\"https:\/\/github.com\/scottgonzalez\/node-wordpress\">node-wordpress<\/a> and the WordPress XML-RPC API was either obscure or non-existent, so here&#8217;s two code snippets, for reference of anyone else trying to use those interfaces.<\/p>\n<p>Getting existing posts from the server:<\/p>\n<pre><code>\r\nvar posts = wp_client.getPosts({status:'draft', number: 100}, null, function(nullity, postarray){\r\n    postarray.forEach(function (p) {\r\n        console.log(p);\r\n    });\r\n});<\/code><\/pre>\n<p>Creating new posts (from the old blog) on the server:<\/p>\n<pre><code>\r\nstories.forEach(function(story) {\r\n    var post = { };\r\n    post.title = story.subject;\r\n    post.status = 'publish';\r\n    post.type = 'post';\r\n    \/\/ [new Date(2016, 05, 08, 11, 35, 10)] ?\r\n    post.date = new Date(story.date); \/\/ story.date is like '2005-03-04 00:00:00'\r\n    post.name = story.shortname;\r\n    post.author = 1;\r\n    post.content = sanitize(story.data);\r\n    post.format = 'standard';\r\n    post.sticky = false;\r\n    post.terms = { category: [19] } ; \/\/note that it is an array. 19 is the term id for 'ancient'\r\n    if (! story.private) {\r\n        wp_client.newPost(post, console.log);\r\n    }\r\n});\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>From April 2003 until January of 2006, I wrote a blog which was incinerated in May 2011 by a house fire.\u00a0\u00a0 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-1229","post","type-post","status-publish","format-standard","hentry","category-ancient"],"_links":{"self":[{"href":"https:\/\/risacher.org\/jfdi\/wp-json\/wp\/v2\/posts\/1229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/risacher.org\/jfdi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/risacher.org\/jfdi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/risacher.org\/jfdi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/risacher.org\/jfdi\/wp-json\/wp\/v2\/comments?post=1229"}],"version-history":[{"count":9,"href":"https:\/\/risacher.org\/jfdi\/wp-json\/wp\/v2\/posts\/1229\/revisions"}],"predecessor-version":[{"id":1238,"href":"https:\/\/risacher.org\/jfdi\/wp-json\/wp\/v2\/posts\/1229\/revisions\/1238"}],"wp:attachment":[{"href":"https:\/\/risacher.org\/jfdi\/wp-json\/wp\/v2\/media?parent=1229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/risacher.org\/jfdi\/wp-json\/wp\/v2\/categories?post=1229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/risacher.org\/jfdi\/wp-json\/wp\/v2\/tags?post=1229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}