Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed silent failure when no post_date_gmt was present in "post" item #8

Merged
merged 4 commits into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/wp2ghost.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,17 @@ exports.fromStream = function(stream) {
xml.on('endElement: item', function(item) {
if (item['wp:post_type'] != "post" && item['wp:post_type'] != "page") return;

var date;

if (item['wp:post_date_gmt'] !== "0000-00-00 00:00:00") {
date = item['wp:post_date_gmt'].match(/(\d{4})-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
} else {
date = item['wp:post_date'].match(/(\d{4})-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
// if post_date_gmt is undefined or 0000 then we check post_date in hope of finding a better date.
// if post_date is undefined we dont know the time an assume 0000
var date = item['wp:post_date_gmt'];
if (date == undefined || date == "0000-00-00 00:00:00") {
date = item['wp:post_date'];
if (date == undefined) {
date='1970-01-01 00:00:00'
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if/else should not be necessary any more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, did not notice it was the same match thing. Will fix.


date = date.match(/(\d{4})-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
date = date.map(function(e) { return parseInt(e, 10); });
var d = new Date(Date.UTC(date[1], date[2]-1, date[3], date[4], date[5], date[6], 0));

Expand Down
42 changes: 38 additions & 4 deletions test/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ var g;
var p;

before(function(done) {
when.then(function(data) { g = data.data; p = g.posts[0]; done(); },
when.then(function(data) {
g = data.data;
p = g.posts[0];
post_missing_both_post_fields = g.posts[1];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think post_missing_all_date_fields would be a better name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well there is still a pubDate field?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True.. I wonder if we should add fallback to that too. But that's for another PR. post_missing_post_date_fields?

post_missing_post_date_gmt = g.posts[2];
done();
},
function(err) { done(new Error(err)); });
});

Expand All @@ -16,13 +22,24 @@ describe('Wordpress XML', function() {
});
});

describe('Post', function(){
describe('itself', function(){
describe('Posts', function(){
describe('simple post with text', function(){
it("should exist", function() {
p = g.posts[0];
if (!p) throw new Exception();
});
});
describe('post without both date fields', function(){
it("should exist", function() {
if (!post_missing_both_post_fields)
throw new Exception();
});
});
describe('post without post_date_gmt', function(){
it("should exist", function() {
if (!post_missing_post_date_gmt)
throw new Exception();
});
});

describe('title', function(){
it("should be preserved", function() {
Expand Down Expand Up @@ -71,6 +88,23 @@ describe('Post', function(){
});
});

describe('Post date fallbacks', function(){
describe('no post_date_gmt or post_date', function(){
it("should return a default year 1970-01-01 00:00:00 AC", function() {
var date = new Date("1970-01-01 00:00:00 UTC");
var unixtime = date.getTime()
post_missing_both_post_fields.created_at.should.equal(unixtime);
});
});
describe('no post_date_gmt', function(){
it("should fallback to post_date", function() {
var date = new Date("2011-04-18 08:41:09 UTC");
var unixtime = date.getTime()
post_missing_post_date_gmt.created_at.should.equal(unixtime);
});
});
});

describe('author', function(){
it("should be reset to admin", function() {
p.created_by.should.equal(1);
Expand Down
18 changes: 18 additions & 0 deletions test/wordpress-export.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,23 @@ It has multiple lines, some <code>code</code>, and a bit more text.]]></wp:comme
<wp:comment_user_id>0</wp:comment_user_id>
</wp:comment>
</item>

<item>
<!-- A post missing both post_date_gmt and post_date -->
<pubDate>Mon, 18 Apr 2011 08:41:09 +0000</pubDate>
<content:encoded>stuff</content:encoded>
<wp:post_type>post</wp:post_type>
<wp:post_id>1</wp:post_id>
</item>

<item>
<!-- A post missing post_date_gmt but post_date is present -->
<pubDate>Mon, 18 Apr 2011 08:41:09 +0000</pubDate>
<content:encoded>stuff</content:encoded>
<wp:post_date>2011-04-18 08:41:09</wp:post_date>
<wp:post_type>post</wp:post_type>
<wp:post_id>1</wp:post_id>
</item>

</channel>
</rss>