Repository navigation
Expand file tree
/
Copy pathWordPressReader.cs
More file actions
96 lines (79 loc) · 2.81 KB
/
Copy pathWordPressReader.cs
File metadata and controls
96 lines (79 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
namespace MigrateBlog;
public class WordPressReader : SQLiteReaderBase
{
public WordPressReader(string filename) : base(filename) { }
protected override IEnumerable<Post> GetPosts()
{
var postCategories = GetPostCategories();
using var command = connection.CreateCommand();
command.CommandText = "select p.ID, u.user_login, post_date, post_title, post_name, post_content from wp_posts p, wp_users u where post_type = 'post' and post_status = 'publish' and p.post_author = u.ID";
var emptyList = new List<string>();
var posts = new List<Post>();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var id = reader.GetInt32(0);
var author = reader.GetString(1);
var date = reader.GetDateTime(2);
var title = reader.GetString(3);
var slug = reader.GetString(4);
var content = reader.GetString(5);
if (!postCategories.TryGetValue(id, out var categories))
categories = emptyList;
posts.Add(new Post(id, author, date, title, slug, content, categories.ToArray(), emptyList.ToArray()));
}
}
return posts;
}
private Dictionary<int, List<string>> GetPostCategories()
{
using var metaCmd = connection.CreateCommand();
metaCmd.CommandText = "select object_id, name from wp_term_relationships r, wp_terms t where object_id in (select ID from wp_posts where post_type = 'post' and post_status = 'publish') and term_taxonomy_id = t.term_id";
Dictionary<int, List<string>> categories = new();
using (var reader = metaCmd.ExecuteReader())
{
while (reader.Read())
{
var postId = reader.GetInt32(0);
var category = reader.GetString(1);
if (categories.TryGetValue(postId, out var postCategories))
postCategories.Add(category);
else
categories.Add(postId, new List<string>([category]));
}
}
return categories;
}
protected override IEnumerable<Comment> GetComments()
{
using var command = connection.CreateCommand();
command.CommandText = "select comment_ID, comment_post_ID, comment_author, comment_date, comment_content, comment_parent from wp_comments";
var commentGuids = new Dictionary<int, Guid>();
var comments = new List<Comment>();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var id = reader.GetInt32(0);
var postId = reader.GetInt32(1);
var author = reader.GetString(2);
var date = reader.GetDateTime(3);
var content = reader.GetString(4);
var parentId = reader.GetInt32(5);
if(!commentGuids.TryGetValue(id, out var guid))
{
guid = new Guid();
commentGuids.Add(id, guid);
}
if (!commentGuids.TryGetValue(parentId, out var parentGuid))
{
parentGuid = new Guid();
commentGuids.Add(parentId, parentGuid);
}
comments.Add(new Comment(guid, postId, author, date, content, parentGuid));
}
}
return comments;
}
}