mirror of https://github.com/halo-dev/halo
Fix wordpress importor. (#536)
parent
6f6ed9ee07
commit
28553042cf
|
@ -14,7 +14,6 @@ import run.halo.app.model.enums.MigrateType;
|
|||
import run.halo.app.service.*;
|
||||
import run.halo.app.utils.XmlMigrateUtils;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
@ -23,6 +22,7 @@ import java.util.List;
|
|||
* WordPress migrate handler
|
||||
*
|
||||
* @author ryanwang
|
||||
* @author guqing
|
||||
* @date 2019-10-28
|
||||
*/
|
||||
@Slf4j
|
||||
|
@ -84,7 +84,7 @@ public class WordPressMigrateHandler implements MigrateHandler {
|
|||
public void migrate(MultipartFile file) {
|
||||
try {
|
||||
String migrationContent = FileCopyUtils.copyToString(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8));
|
||||
String jsonString = XmlMigrateUtils.xml2jsonString(new FileInputStream(migrationContent));
|
||||
String jsonString = XmlMigrateUtils.xml2jsonString(migrationContent);
|
||||
JSONObject json = JSONObject.parseObject(jsonString);
|
||||
Rss rss = json.getObject("rss", Rss.class);
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package run.halo.app.handler.migrate.converter;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import run.halo.app.handler.migrate.support.vo.PostVO;
|
||||
import run.halo.app.handler.migrate.support.wordpress.*;
|
||||
import run.halo.app.handler.migrate.utils.RelationMapperUtils;
|
||||
|
@ -7,19 +9,18 @@ import run.halo.app.model.entity.BaseComment;
|
|||
import run.halo.app.model.entity.BasePost;
|
||||
import run.halo.app.model.entity.Category;
|
||||
import run.halo.app.model.entity.Tag;
|
||||
import run.halo.app.utils.MarkdownUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* WordPress博客数据迁移转换器
|
||||
* WordPress 博客数据迁移转换器
|
||||
*
|
||||
* @author guqing
|
||||
* @author ryanwang
|
||||
* @date 2020-01-18 16:50
|
||||
*/
|
||||
public class WordPressConverter implements Converter<Rss, List<PostVO>> {
|
||||
|
@ -46,6 +47,12 @@ public class WordPressConverter implements Converter<Rss, List<PostVO>> {
|
|||
return getBasePost(items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets post vo list from items.
|
||||
*
|
||||
* @param items wordpress items.
|
||||
* @return a list of post vo.
|
||||
*/
|
||||
private List<PostVO> getBasePost(List<Item> items) {
|
||||
List<PostVO> posts = new ArrayList<>();
|
||||
if (items == null) {
|
||||
|
@ -88,6 +95,12 @@ public class WordPressConverter implements Converter<Rss, List<PostVO>> {
|
|||
return posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets item's comments.
|
||||
*
|
||||
* @param item wordpress item.
|
||||
* @return a list of baseComment.
|
||||
*/
|
||||
private List<BaseComment> getCommentsFromItem(Item item) {
|
||||
List<BaseComment> baseComments = new ArrayList<>();
|
||||
if (Objects.isNull(item) || Objects.isNull(item.getComments())) {
|
||||
|
@ -97,25 +110,30 @@ public class WordPressConverter implements Converter<Rss, List<PostVO>> {
|
|||
List<Comment> comments = item.getComments();
|
||||
for (Comment comment : comments) {
|
||||
BaseComment baseComment = RelationMapperUtils.convertFrom(comment, BaseComment.class);
|
||||
Date commentDate = DateUtil.parseDateTime(comment.getCommentDate());
|
||||
baseComment.setCreateTime(commentDate);
|
||||
baseComment.setUpdateTime(commentDate);
|
||||
baseComments.add(baseComment);
|
||||
}
|
||||
|
||||
return baseComments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets base post from item.
|
||||
*
|
||||
* @param item wordpress item.
|
||||
* @return base post.
|
||||
*/
|
||||
private BasePost getBasePostFromItem(Item item) {
|
||||
BasePost post = RelationMapperUtils.convertFrom(item, BasePost.class);
|
||||
String postDate = item.getPostDate();
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
if (postDate != null) {
|
||||
LocalDateTime dateTime = LocalDateTime.parse(postDate, formatter);
|
||||
Date date = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||
post.setEditTime(date);
|
||||
// 设置url为文章编辑时的时间毫秒数
|
||||
post.setUrl(post.getEditTime() + "");
|
||||
} else {
|
||||
post.setUrl(System.currentTimeMillis() + "");
|
||||
Date postDate = DateUtil.parseDateTime(item.getPostDate());
|
||||
if (StringUtils.isNoneEmpty(post.getFormatContent())) {
|
||||
post.setOriginalContent(MarkdownUtils.renderMarkdown(post.getFormatContent()));
|
||||
}
|
||||
post.setCreateTime(postDate);
|
||||
post.setUpdateTime(postDate);
|
||||
post.setEditTime(postDate);
|
||||
return post;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,11 +11,14 @@ import run.halo.app.handler.migrate.utils.PropertyMappingTo;
|
|||
* </p>
|
||||
*
|
||||
* @author guqing
|
||||
* @author ryanwang
|
||||
* @date 2019-11-17 14:01
|
||||
*/
|
||||
@Data
|
||||
public class Comment {
|
||||
|
||||
@JSONField(name = "wp:comment_id")
|
||||
@PropertyMappingTo("id")
|
||||
private String commentId;
|
||||
|
||||
@JSONField(name = "wp:comment_author")
|
||||
|
|
|
@ -11,31 +11,36 @@ import java.util.List;
|
|||
* <p> WordPress导出的xml中对于的item子节点的值将会被映射到该类的属性上,最终被解析为文章属性{@link BasePost} </p>
|
||||
*
|
||||
* @author guqing
|
||||
* @author ryanwang
|
||||
* @date 2019-11-17 13:59
|
||||
*/
|
||||
@Data
|
||||
public class Item {
|
||||
|
||||
private String title;
|
||||
|
||||
private String link;
|
||||
|
||||
private String pubDate;
|
||||
|
||||
@JSONField(name = "post_date")
|
||||
private String postDate;
|
||||
|
||||
@JSONField(name = "dc:creator")
|
||||
private String creator;
|
||||
|
||||
private String description;
|
||||
|
||||
@JSONField(name = "content:encoded")
|
||||
@PropertyMappingTo("formatContent")
|
||||
private String content;
|
||||
|
||||
@JSONField(name = "excerpt:encoded")
|
||||
@PropertyMappingTo("summary")
|
||||
private String excerpt;
|
||||
|
||||
@JSONField(name = "wp:post_date")
|
||||
private String postDate;
|
||||
|
||||
@JSONField(name = "wp:comment_status")
|
||||
private String commentStatus;
|
||||
|
||||
@JSONField(name = "wp:post_name")
|
||||
@PropertyMappingTo("url")
|
||||
private String postName;
|
||||
|
||||
@JSONField(name = "wp:status")
|
||||
private String status;
|
||||
|
||||
|
@ -44,6 +49,7 @@ public class Item {
|
|||
private String postPassword;
|
||||
|
||||
@JSONField(name = "wp:is_sticky")
|
||||
@PropertyMappingTo("topPriority")
|
||||
private Integer isSticky;
|
||||
|
||||
@JSONField(name = "wp:comment")
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.lang.annotation.*;
|
|||
|
||||
/**
|
||||
* 该注解用于定义两个对象之间的属性映射关系
|
||||
*
|
||||
* @author guqing
|
||||
* @date 2020-1-19 13:51
|
||||
*/
|
||||
|
@ -13,6 +14,7 @@ import java.lang.annotation.*;
|
|||
public @interface PropertyMappingTo {
|
||||
/**
|
||||
* value对应的是目标对象的属性名称
|
||||
*
|
||||
* @return 返回源对象属性对应的目标对象的属性名
|
||||
*/
|
||||
String value() default "";
|
||||
|
|
|
@ -7,16 +7,38 @@ import org.json.XML;
|
|||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* <p> 将xml解析为json </p>
|
||||
*
|
||||
* @author guqing
|
||||
* @author ryanwang
|
||||
* @date 2019-11-17 13:33
|
||||
*/
|
||||
public class XmlMigrateUtils {
|
||||
|
||||
/**
|
||||
* xml to json.
|
||||
*
|
||||
* @param in {@link java.io.FileInputStream}
|
||||
* @return json result
|
||||
* @throws JSONException throw JSONException
|
||||
* @throws IOException throw IOException
|
||||
*/
|
||||
public static String xml2jsonString(FileInputStream in) throws JSONException, IOException {
|
||||
String xml = IOUtils.toString(in, "UTF-8");
|
||||
String xml = IOUtils.toString(in, StandardCharsets.UTF_8);
|
||||
JSONObject jsonObject = XML.toJSONObject(xml);
|
||||
return jsonObject.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* xml to json.
|
||||
*
|
||||
* @param xml xml
|
||||
* @return json result
|
||||
*/
|
||||
public static String xml2jsonString(String xml) {
|
||||
JSONObject jsonObject = XML.toJSONObject(xml);
|
||||
return jsonObject.toString();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue