From 36206f83c158eeef5d1a2ec9dd6a37aee90ef265 Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Tue, 31 Aug 2021 09:08:49 +0800 Subject: [PATCH 1/2] =?UTF-8?q?#244#310=E4=B8=BB=E9=A2=98=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=B8=AD=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/models.go | 2 ++ models/topic.go | 7 ++++--- options/locale/locale_en-US.ini | 2 +- options/locale/locale_zh-CN.ini | 2 +- web_src/js/index.js | 6 ++++-- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/models/models.go b/models/models.go index 0e06c60b3..67892399a 100755 --- a/models/models.go +++ b/models/models.go @@ -185,6 +185,8 @@ func SetEngine() (err error) { x.SetMaxOpenConns(setting.Database.MaxOpenConns) x.SetMaxIdleConns(setting.Database.MaxIdleConns) x.SetConnMaxLifetime(setting.Database.ConnMaxLifetime) + x.Sync2(tables...) + MigrateCustom(x) return nil } diff --git a/models/topic.go b/models/topic.go index 4a5bffa08..b8d3d9d85 100644 --- a/models/topic.go +++ b/models/topic.go @@ -8,6 +8,7 @@ import ( "fmt" "regexp" "strings" + "unicode/utf8" "code.gitea.io/gitea/modules/timeutil" @@ -21,12 +22,12 @@ func init() { ) } -var topicPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*$`) +var topicPattern = regexp.MustCompile(`^[\x{4e00}-\x{9fa5}a-z0-9][\x{4e00}-\x{9fa5}a-z0-9-]*$`) // Topic represents a topic of repositories type Topic struct { ID int64 - Name string `xorm:"UNIQUE VARCHAR(25)"` + Name string `xorm:"UNIQUE VARCHAR(105)"` RepoCount int CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` @@ -56,7 +57,7 @@ func (err ErrTopicNotExist) Error() string { // ValidateTopic checks a topic by length and match pattern rules func ValidateTopic(topic string) bool { - return len(topic) <= 35 && topicPattern.MatchString(topic) + return utf8.RuneCountInString(topic) <= 35 && topicPattern.MatchString(topic) } // SanitizeAndValidateTopics sanitizes and checks an array or topics diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 61a233589..56cf564e7 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1815,7 +1815,7 @@ branch.included = Included topic.manage_topics = Manage Topics topic.done = Done topic.count_prompt = You can not select more than 25 topics -topic.format_prompt = Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long. +topic.format_prompt = Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long. [org] org_name_holder = Organization Name diff --git a/options/locale/locale_zh-CN.ini b/options/locale/locale_zh-CN.ini index 89db6536c..31398b927 100755 --- a/options/locale/locale_zh-CN.ini +++ b/options/locale/locale_zh-CN.ini @@ -1817,7 +1817,7 @@ branch.included=已包含 topic.manage_topics=管理主题 topic.done=保存 topic.count_prompt=您最多选择25个主题 -topic.format_prompt=主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符 +topic.format_prompt=主题必须以汉字、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符 [org] org_name_holder=组织名称 diff --git a/web_src/js/index.js b/web_src/js/index.js index 991a13c21..cad60cdc2 100755 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -4117,7 +4117,9 @@ function initTopicbar() { topics .last() .attr('data-value') - .match(regExp); + .match(regExp) || topics + .last() + .attr('data-value').length <= 35; if (!status) { topics .last() @@ -4136,7 +4138,7 @@ function initTopicbar() { rules: [ { type: 'validateTopic', - value: /^[a-z0-9][a-z0-9-]{0,35}$/, + value: /^[\u4e00-\u9fa5a-z0-9][\u4e00-\u9fa5a-z0-9-]{0,105}$/, prompt: topicPrompts.formatPrompt }, { From 32a46bfa96b38f814d51edffedb25b0832498b8a Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Tue, 31 Aug 2021 09:12:56 +0800 Subject: [PATCH 2/2] #fix-244 --- models/custom_migrations.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 models/custom_migrations.go diff --git a/models/custom_migrations.go b/models/custom_migrations.go new file mode 100644 index 000000000..b196c5a8b --- /dev/null +++ b/models/custom_migrations.go @@ -0,0 +1,36 @@ +package models + +import ( + "code.gitea.io/gitea/modules/log" + "xorm.io/xorm" +) + +type CustomMigration struct { + Description string + Migrate func(*xorm.Engine) error +} + +var customMigrations = []CustomMigration{ + {"Custom v1 Topic struct change to support chinese", syncTopicStruct}, +} + +func MigrateCustom(x *xorm.Engine) { + + for _, m := range customMigrations { + log.Info("Migration: %s", m.Description) + if err := m.Migrate(x); err != nil { + + log.Error("Migration: %v", err) + + } + } + +} + +func syncTopicStruct(x *xorm.Engine) error { + + query := "ALTER TABLE topic ALTER COLUMN name TYPE varchar(105);" + + _, err := x.Exec(query) + return err +}