-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
290 lines (251 loc) · 8.01 KB
/
main.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"bytes"
"compress/zlib"
"database/sql"
"encoding/base64"
"flag"
"fmt"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
"github.com/mylxsw/wizard-migration/showdoc"
"github.com/mylxsw/wizard-migration/wizard"
"io"
"log"
"path/filepath"
"strings"
"time"
)
var Version = "1.0"
var GitCommit = "000000000000000000000000000"
var showdocDBConn string
var wizardDBConn string
var importUserID int64
var replaceUrl string
var replaceUrlTo string
func main() {
flag.StringVar(&showdocDBConn, "showdoc_db", "/Users/mylxsw/codes/github/showdoc/Sqlite/showdoc.db.php", "ShowDoc 数据库文件路径")
flag.StringVar(&wizardDBConn, "wizard_db", "root:@tcp(127.0.0.1:3306)/wizard_migration", "Wizard 数据库连接地址")
flag.Int64Var(&importUserID, "import_user_id", 1, "导入后在 Wizard 中使用的 UID")
flag.StringVar(&replaceUrl, "replace_url", "http://showdoc.local.yunsom.space", "替换地址,用于替换正文中的图片地址")
flag.StringVar(&replaceUrlTo, "replace_url_to", "/storage/showdoc/", "替换地址,用于替换正文中的图片地址")
flag.Parse()
log.Printf("Version=%s. GitCommit=%s", Version, GitCommit)
showdocDB, err := sql.Open("sqlite3", showdocDBConn)
if err != nil {
panic(err)
}
defer showdocDB.Close()
wizardDB, err := sql.Open("mysql", fmt.Sprintf("%s?parseTime=true", wizardDBConn))
if err != nil {
panic(err)
}
defer wizardDB.Close()
tx, err := wizardDB.Begin()
if err != nil {
panic(err)
}
(func() {
defer func() {
if err := recover(); err != nil {
_ = tx.Rollback()
log.Printf("migrate to wizard failed: %v", err)
}
}()
if err := migrate(showdocDB, tx); err != nil {
panic(err)
}
if err := tx.Commit(); err != nil {
panic(err)
}
})()
log.Println("migration finished")
}
// migrate 执行迁移
func migrate(showdocDB *sql.DB, wizardDB *sql.Tx) error {
showdocItemModel := showdoc.NewItemModel(showdocDB)
showdocPageModel := showdoc.NewPageModel(showdocDB)
showdocCatalogModel := showdoc.NewCatalogModel(showdocDB)
wizardProjectModel := wizard.NewProjectModel(wizardDB)
wizardPageModel := wizard.NewPageModel(wizardDB)
items, err := showdocItemModel.GetItems()
if err != nil {
return err
}
// 遍历项目
for _, item := range items {
log.Printf("> %s", item.ItemName)
// 创建项目
projectID, err := wizardProjectModel.CreateProject(wizard.Project{
Name: item.ItemName,
Description: item.ItemDescription,
UserID: importUserID,
CreatedAt: time.Unix(item.AddTime, 0),
UpdatedAt: time.Unix(item.AddTime, 0),
SortLevel: 0,
CatalogId: 0,
})
if err != nil {
return err
}
// 查询项目下的目录树
tree, err := showdocCatalogModel.GetCatalogTreeInItem(item.ItemId)
if err != nil {
return err
}
// 遍历目录树
traverseCatalogTree(tree, func(id int64, name string, level int64, pid int64) int64 {
log.Printf("%s △ %s(%d)", strings.Repeat(" ", int(level)), name, id)
var catalogID int64 = 0
// 创建目录(在Wizard中作为Markdown文档)
if id > 0 {
catalogID, err = wizardPageModel.CreatePage(wizard.Page{
PID: pid,
Title: name,
Description: "",
Content: "",
ProjectID: projectID,
UserID: importUserID,
LastModifiedUserID: importUserID,
Type: wizard.TypeMarkdown,
Status: wizard.StatusNormal,
HistoryID: 0,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
SortLevel: 0,
})
if err != nil {
panic(err)
}
}
// 查询目录下所有的文章
pages, err := showdocPageModel.GetPagesInItemAndCatalog(item.ItemId, id)
if err != nil {
panic(err)
}
// 新增文章到当前目录
for _, page := range pages {
log.Printf("%s - [%s]", strings.Repeat(" ", int(level)), page.PageTitle)
pageID, err := wizardPageModel.CreatePage(wizard.Page{
PID: catalogID,
Title: page.PageTitle,
Description: page.PageComments,
Content: preProcess(page.PageContent, replaceUrl, replaceUrlTo),
ProjectID: projectID,
UserID: importUserID,
LastModifiedUserID: importUserID,
Type: wizard.TypeMarkdown,
Status: wizard.StatusNormal,
HistoryID: 0,
CreatedAt: time.Unix(page.AddTime, 0),
UpdatedAt: time.Unix(page.AddTime, 0),
SortLevel: page.SNumber,
})
if err != nil {
panic(err)
}
// 添加附件
attachments, err := showdocPageModel.GetAttachmentsInPage(page.PageId)
if err != nil {
panic(err)
}
for _, att := range attachments {
log.Printf("%s ☁️ [%s]", strings.Repeat(" ", int(level)), att.DisplayName)
if _, err := wizardPageModel.AddAttachment(wizard.Attachment{
Name: att.DisplayName,
Path: preProcess(att.RealURL, replaceUrl, replaceUrlTo),
UserID: importUserID,
PageID: pageID,
ProjectID: projectID,
CreatedAt: time.Unix(att.AddTime, 0),
UpdatedAt: time.Unix(att.AddTime, 0),
}); err != nil {
panic(err)
}
}
// 添加历史记录
histories, err := showdocPageModel.GetPageHistories(page.PageId)
if err != nil {
panic(err)
}
log.Printf("%s ❄️️ 导入历史记录 (%d) 条", strings.Repeat(" ", int(level)), len(histories)+1)
for _, his := range histories {
pageContent, err := stringDecode(his.PageContent)
if err != nil {
log.Printf("decode history page content failed: %v", err)
continue
}
if _, err := wizardPageModel.AddHistory(wizard.PageHistory{
PageID: pageID,
PID: catalogID,
Title: his.PageTitle,
Description: his.PageComments,
Content: pageContent,
ProjectID: projectID,
Type: wizard.TypeMarkdown,
Status: wizard.StatusNormal,
UserID: importUserID,
OperatorID: importUserID,
CreatedAt: time.Unix(his.AddTime, 0),
UpdatedAt: time.Unix(his.AddTime, 0),
SortLevel: 0,
}); err != nil {
panic(err)
}
}
// 文档最新内容作为最后一条历史记录
lastHistoryID, err := wizardPageModel.AddHistory(wizard.PageHistory{
PageID: pageID,
PID: catalogID,
Title: page.PageTitle,
Description: page.PageComments,
Content: page.PageContent,
ProjectID: projectID,
Type: wizard.TypeMarkdown,
Status: wizard.StatusNormal,
UserID: importUserID,
OperatorID: importUserID,
CreatedAt: time.Unix(page.AddTime, 0),
UpdatedAt: time.Unix(page.AddTime, 0),
SortLevel: 0,
})
if err != nil {
panic(err)
}
// 更新文档最后历史记录ID
if _, err := wizardPageModel.UpdatePageHistoryID(pageID, lastHistoryID); err != nil {
panic(err)
}
}
return catalogID
}, 0)
}
return nil
}
// traverseCatalogTree 遍历目录树
func traverseCatalogTree(tree showdoc.CatalogTree, callback func(id int64, name string, level int64, pid int64) int64, pid int64) {
newID := callback(tree.ID, tree.Name, tree.Level, pid)
if len(tree.SubCatalogs) > 0 {
for _, sc := range tree.SubCatalogs {
traverseCatalogTree(sc, callback, newID)
}
}
}
// preProcess 预处理内容(替换 url 地址)
func preProcess(content string, replaceUrl string, replaceUrlTo string) string {
return strings.ReplaceAll(content, filepath.Join(replaceUrl, "/server/../Public/Uploads/"), replaceUrlTo)
}
// stringDecode 字符串解码 zlib(base64)
func stringDecode(src string) (string, error) {
pageContent, err := base64.StdEncoding.DecodeString(src)
if err != nil {
return "", err
}
reader, err := zlib.NewReader(bytes.NewReader(pageContent))
if err != nil {
return "", err
}
var content bytes.Buffer
_, _ = io.Copy(&content, reader)
return content.String(), nil
}