Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
fix: Typecho Importer some bugs.
Browse files Browse the repository at this point in the history
Signed-off-by: qwqcode <[email protected]>
  • Loading branch information
qwqcode committed Oct 26, 2021
1 parent 6a372ba commit fa2c760
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
10 changes: 6 additions & 4 deletions lib/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,17 @@ type BasicParams struct {
TargetSiteUrl string `json:"target_site_url"`
}

func GetBasicParamsFrom(payload []string) BasicParams {
func GetBasicParamsFrom(payload []string) *BasicParams {
basic := BasicParams{}
GetParamsFrom(payload).To(map[string]*string{
"target_site_name": &basic.TargetSiteName,
"target_site_url": &basic.TargetSiteUrl,
})

return &basic
}

func RequiredBasicTargetSite(basic *BasicParams) {
if basic.TargetSiteName == "" {
logrus.Fatal("请附带参数 `target_site_name:<站点名称>`")
}
Expand All @@ -140,8 +144,6 @@ func GetBasicParamsFrom(payload []string) BasicParams {
if !lib.ValidateURL(basic.TargetSiteUrl) {
logrus.Fatal("参数 `target_site_url:<站点根目录 URL>` 必须为 URL 格式")
}

return basic
}

func DbReady(payload []string) *gorm.DB {
Expand Down Expand Up @@ -189,7 +191,7 @@ func DbReady(payload []string) *gorm.DB {
}

// 站点准备
func SiteReady(basic BasicParams) model.Site {
func SiteReady(basic *BasicParams) model.Site {
site := model.FindSite(basic.TargetSiteName)
if site.IsEmpty() {
// 创建新站点
Expand Down
42 changes: 36 additions & 6 deletions lib/importer/typecho.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type _TypechoImporter struct {
ImporterInfo
TargetSite model.Site

Basic BasicParams
Basic *BasicParams
SrcVersion string
SrcSiteName string
SrcSiteURL string
Expand All @@ -56,7 +56,7 @@ type _TypechoImporter struct {
// Typecho 升级相关的代码 @see https://github.com/typecho/typecho/blob/64b8e686885d8ab4c7f0cdc3d6dc2d99fa48537c/var/Utils/Upgrade.php
// 路由 @see https://github.com/typecho/typecho/blob/530312443142577509df88ce88cf3274fac9b8c4/var/Widget/Options/Permalink.php#L319
// DB @see https://github.com/typecho/typecho/blob/6558fd5e030a950335d53038f82728b06ad6c32d/install/Mysql.sql
func (imp *_TypechoImporter) Run(basic BasicParams, payload []string) {
func (imp *_TypechoImporter) Run(basic *BasicParams, payload []string) {
// Ready
imp.Basic = basic
typechoDB := DbReady(payload)
Expand Down Expand Up @@ -87,6 +87,15 @@ func (imp *_TypechoImporter) Run(basic BasicParams, payload []string) {
}
}

// 若未指定 target site 则使用数据库数据
if basic.TargetSiteName == "" && imp.SrcSiteName != "" {
basic.TargetSiteName = imp.SrcSiteName
}
if basic.TargetSiteUrl == "" && imp.SrcSiteURL != "" && lib.ValidateURL(imp.SrcSiteURL) {
basic.TargetSiteUrl = imp.SrcSiteURL
}
RequiredBasicTargetSite(basic)

// 检查数据源版本号
imp.VersionCheck()

Expand Down Expand Up @@ -292,15 +301,16 @@ func (imp *_TypechoImporter) GetNewPageKey(content TypechoContent) string {
}

rewriteRule = "/" + strings.TrimPrefix(rewriteRule, "/")
return baseUrl + imp.ReplaceAll(rewriteRule, replaces)
return baseUrl + imp.ReplaceAllBracketed(rewriteRule, replaces)
}

// 替换字符串
func (imp *_TypechoImporter) ReplaceAll(data string, dict map[string]string) string {
// @note 特别注意:replaces 的 key 外侧无 “[]” 或 “{}”
func (imp *_TypechoImporter) ReplaceAllBracketed(data string, replaces map[string]string) string {
r := regexp.MustCompile(`(\[|\{)\s*(.*?)\s*(\]|\})`) // 同时支持 {} 和 []
return r.ReplaceAllStringFunc(data, func(m string) string {
key := r.FindStringSubmatch(m)[2]
if val, isExist := dict[key]; isExist {
if val, isExist := replaces[key]; isExist {
return val
} else {
logrus.Error(fmt.Sprintf("[重写规则] \"%s\" 变量无效", key))
Expand Down Expand Up @@ -414,7 +424,27 @@ func (imp *_TypechoImporter) RewriteRuleReady() {
return
}

*field = route.URL
readRule := route.URL

// 将 [year:digital:4] 变为 [year]
r := regexp.MustCompile(`\[(([a-zA-Z0-9]+):?(.*?))\]`)
subMatches := r.FindAllStringSubmatch(readRule, -1)
replaces := map[string]string{}
for _, m := range subMatches {
if len(m) < 1 {
continue
}

// 0 => [year:digital:4]
// 1 => year:digital:4
// 2 => year
// 3 => digital:4
replaces[m[1]] = "[" + m[2] + "]"
}
readRule = imp.ReplaceAllBracketed(readRule, replaces) // replaces keys 无括号

// 保存从数据库读取到的 rule
*field = readRule
logrus.Info("重写规则 \"" + nameText + "\" 读取成功")
} else {
logrus.Info("[重写规则] 自定义 \""+nameText+"\" 规则:", fmt.Sprintf("%#v", *field))
Expand Down
2 changes: 1 addition & 1 deletion lib/importer/wordpress.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ type _WordPressImporter struct {
ImporterInfo
}

func (i _WordPressImporter) Run(basic BasicParams, payload []string) {
func (i _WordPressImporter) Run(basic *BasicParams, payload []string) {
}

0 comments on commit fa2c760

Please sign in to comment.